Skip to content

Commit 95d6a07

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 95d6a07

File tree

5 files changed

+638
-0
lines changed

5 files changed

+638
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===-- OclGpuRuntimeWrappers.h - Ocl GPU wrappers --------------*- 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, void *dst,
42+
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 gcOclGpuKernelDestroy(size_t count, GcOclGpuKernel *kernels);
50+
51+
GC_DLL_EXPORT void gcOclGpuKernelLaunch(GcOclGpuContext *ctx,
52+
GcOclGpuKernel *kernel, ...);
53+
}
54+
#else
55+
#undef GC_OCL_GPU_DEF_ONLY
56+
#endif
57+
#endif

include/gc/Transforms/Passes.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ def LinalgToXeGPU : Pass<"linalg-to-xegpu", "func::FuncOp"> {
9393
"DPAS register block sizes MxNxK">,
9494
];
9595
}
96+
97+
def GpuToOclGpu : Pass<"gpu-to-oclgpu", "ModuleOp"> {
98+
let summary = "Convert the GPU operations to OclGpuRuntime calls.";
99+
let description = [{
100+
Convert the gpu alloc, dealloc, memcpy and launch operations to OclGpuRuntime calls.
101+
}];
102+
}
96103
#endif // GC_USE_IMEX
97104

98105
def IterativeTilingAndFusion : Pass<"iterative-tiling-and-fusion",

include/gc/Utils.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===-- Utils.h - Utils -----------------------------------------*- 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_UTILS_H
10+
#define GC_UTILS_H
11+
12+
#if defined _WIN32 || defined __CYGWIN__
13+
#define GC_DLL_EXPORT __declspec(dllexport)
14+
#else
15+
#define GC_DLL_EXPORT __attribute__((visibility("default")))
16+
#endif
17+
18+
#ifdef _NDEBUG
19+
#define gcLogD(...)
20+
#define gcLogE(...)
21+
#else
22+
#include <iostream>
23+
24+
static void _gcLogPrint(std::ostream &stream) { stream << std::endl; }
25+
26+
template <typename T, typename... Args>
27+
static void _gcLogPrint(std::ostream &stream, T first, Args... args) {
28+
stream << first;
29+
_gcLogPrint(stream, args...);
30+
}
31+
32+
template <typename... Args>
33+
static void _gcLog(std::ostream &stream, const char *pref, const char *fileName,
34+
int lineNum, Args... args) {
35+
stream << pref << " [" << fileName << ":" << lineNum << "] ";
36+
_gcLogPrint(stream, args...);
37+
}
38+
39+
#define gcLog(stream, pref, ...) \
40+
_gcLog(stream, pref, __FILE__, __LINE__, __VA_ARGS__)
41+
#define gcLogD(...) gcLog(std::cout, "[DEBUG]", __VA_ARGS__)
42+
#define gcLogE(...) gcLog(std::cerr, "[ERROR]", __VA_ARGS__)
43+
#endif
44+
45+
#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)