Skip to content

Commit d855aee

Browse files
Convert a subset of GPU dialect ops to the OpenCL GPU runtime calls
Added a new path, that converts the following GPU dialect ops to the corresponding callsof the OpenCL GPU runtime functions (to be implemented later): - gpu.alloc, gpu.dealloc, gpu.memcpy and gpu.launch The first argument of each runtime's function is a pointer to the context structure. This is not a cl_context, this is an execution context, i.e. a single execution of the module's main function. It contains the queue, wait list (in case of out-of-order mode) and someother data, required for the module ops execution. It's expected, that the pointer to the context is passed to the module's main function as the last argument of type memref with zero dims. For each gpu.launch operation, 2 additional functions are created: - getXXXKernel(): returns the kernel pointer, stored in a global variable. If it's NULL, calls createXXXKernel(). - createXXXKernel(): Calls the runtime's function, that creates a kernel. SPIRV, kernel name, and sizes are passed to the function. The returned pointer is saved in the global var using `llvm.cmpxchg`, to make sure it doesn't overwrite a kernel, created by another thread. Finally, a destructor function is created, that calls the corresponding runtime's kernel destroy function and passes the pointers, stored in the global vars. This function must be called by themodule owner, when destroying the module. The kernel is not a cl_kernel, but a runtime's internal structure, that contains a compiledcl_program, preconfigured cl_kernel and other data, required for the kernel execution. The runtime's launch function clones the preconfigured kernel, sets the arguments and enqueues a command to execute the kernel.
1 parent 9658857 commit d855aee

File tree

5 files changed

+676
-0
lines changed

5 files changed

+676
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===-- OclGpuRuntimeWrappers.h ---------------------------------*- C++ -*-===//
2+
//
3+
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef GC_OCLGPURUNTIMEWRAPPERS_H
10+
#define GC_OCLGPURUNTIMEWRAPPERS_H
11+
12+
#define GC_OCL_GPU_MALLOC "gcOclGpuMaloc"
13+
#define GC_OCL_GPU_DEALLOC "gcOclGpuDealloc"
14+
#define GC_OCL_GPU_MEMCPY "gcOclGpuMemcpy"
15+
#define GC_OCL_GPU_KERNEL_CREATE "gcOclGpuKernelCreate"
16+
#define GC_OCL_GPU_KERNEL_DESTROY "gcOclGpuKernelDestroy"
17+
#define GC_OCL_GPU_KERNEL_LAUNCH "gcOclGpuKernelLaunch"
18+
19+
#ifndef GC_OCL_GPU_DEF_ONLY
20+
21+
#include "gc/Utils.h"
22+
#include <CL/cl.h>
23+
24+
struct GcOclGpuKernel;
25+
26+
struct GcOclGpuContext {
27+
cl_command_queue queue;
28+
29+
// The following fields are used only if the queue has the
30+
// CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE property.
31+
cl_uint waitListLen;
32+
const cl_event* waitList;
33+
cl_event* lastEvent;
34+
};
35+
36+
extern "C" {
37+
GC_DLL_EXPORT void* gcOclGpuMaloc(GcOclGpuContext* ctx, size_t size);
38+
39+
GC_DLL_EXPORT void gcOclGpuDealloc(GcOclGpuContext* ctx, void* ptr);
40+
41+
GC_DLL_EXPORT void gcOclGpuMemcpy(GcOclGpuContext* ctx, void* src,
42+
void* dst, size_t size);
43+
44+
GC_DLL_EXPORT GcOclGpuKernel*
45+
gcOclGpuKernelCreate(GcOclGpuContext* ctx, const char* spirv, const char* name,
46+
const size_t* globalSize, const size_t* localSize,
47+
size_t argNum, const size_t* argSize);
48+
49+
GC_DLL_EXPORT void
50+
gcOclGpuKernelDestroy(size_t count, GcOclGpuKernel* kernels);
51+
52+
GC_DLL_EXPORT void
53+
gcOclGpuKernelLaunch(GcOclGpuContext* ctx, GcOclGpuKernel* kernel, ...);
54+
}
55+
#else
56+
#undef GC_OCL_GPU_DEF_ONLY
57+
#endif
58+
#endif

include/gc/Transforms/Passes.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ def LinalgToXeGPU : Pass<"linalg-to-xegpu", "func::FuncOp"> {
9595
}
9696
#endif // GC_USE_IMEX
9797

98+
def GpuToOclGpu : Pass<"gpu-to-oclgpu", "ModuleOp"> {
99+
let summary = "Convert the GPU operations to OclGpuRuntime calls.";
100+
let description = [{
101+
Convert the gpu alloc, dealloc, memcpy and launch operations to OclGpuRuntime calls.
102+
}];
103+
}
104+
98105
def IterativeTilingAndFusion : Pass<"iterative-tiling-and-fusion",
99106
"func::FuncOp"> {
100107
let summary = "Iterative tiling and fusion for any tilable operation";

include/gc/Utils.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions
14+
* and limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
#ifndef GC_UTILS_H
20+
#define GC_UTILS_H
21+
22+
#if defined _WIN32 || defined __CYGWIN__
23+
#define GC_DLL_EXPORT __declspec(dllexport)
24+
#else
25+
#define GC_DLL_EXPORT __attribute__((visibility("default")))
26+
#endif
27+
28+
#ifdef _NDEBUG
29+
#define gcLogD(...)
30+
#define gcLogE(...)
31+
#else
32+
#include <iostream>
33+
34+
static void _gcLogPrint(std::ostream &stream) {
35+
stream << std::endl;
36+
}
37+
38+
template <typename T, typename... Args>
39+
static void _gcLogPrint(std::ostream &stream, T first, Args... args) {
40+
stream << first;
41+
_gcLogPrint(stream, args...);
42+
}
43+
44+
template <typename... Args>
45+
static void _gcLog(std::ostream &stream, const char* pref,
46+
const char* fileName, int lineNum, Args... args) {
47+
stream << pref << " [" << fileName << ":" << lineNum << "] ";
48+
_gcLogPrint(stream, args...);
49+
}
50+
51+
#define gcLog(stream, pref, ...) _gcLog(stream, pref, __FILE__, __LINE__, __VA_ARGS__)
52+
#define gcLogD(...) gcLog(std::cout, "[DEBUG]", __VA_ARGS__)
53+
#define gcLogE(...) gcLog(std::cerr, "[ERROR]", __VA_ARGS__)
54+
#endif
55+
56+
#endif

lib/gc/Transforms/GPU/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
gc_add_mlir_library(GcGpuPasses
2+
GpuToOclGpu.cpp
23
LinalgToXeGPU.cpp
34

45
DEPENDS

0 commit comments

Comments
 (0)