Skip to content

Commit 6e13abe

Browse files
Convert a subset of GPU dialect ops to the OpenCL GPU runtime calls (#333)
* Convert a subset of GPU dialect ops to the GPU OpenCL runtime calls Added a new path, that converts the following GPU dialect ops to the corresponding callsof the GPU OpenCL 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 71041d5 commit 6e13abe

File tree

6 files changed

+744
-0
lines changed

6 files changed

+744
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- GpuOclRuntime.h - GPU OpenCL runtime --------------------*- 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_GPUOCLRUNTIME_H
10+
#define GC_GPUOCLRUNTIME_H
11+
12+
namespace mlir::gc::gpu {
13+
constexpr char GPU_OCL_MALLOC[] = "gcGpuOclMalloc";
14+
constexpr char GPU_OCL_DEALLOC[] = "gcGpuOclDealloc";
15+
constexpr char GPU_OCL_MEMCPY[] = "gcGpuOclMemcpy";
16+
constexpr char GPU_OCL_KERNEL_CREATE[] = "gcGpuOclKernelCreate";
17+
constexpr char GPU_OCL_KERNEL_DESTROY[] = "gcGpuOclKernelDestroy";
18+
constexpr char GPU_OCL_KERNEL_LAUNCH[] = "gcGpuOclKernelLaunch";
19+
constexpr char GPU_OCL_MOD_DESTRUCTOR[] = "gcGpuOclModuleDestructor";
20+
} // namespace mlir::gc::gpu
21+
22+
#ifndef GC_GPU_OCL_CONST_ONLY
23+
24+
// TBD
25+
26+
#else
27+
#undef GC_GPU_OCL_CONST_ONLY
28+
#endif
29+
#endif

include/gc/Transforms/Passes.td

+14
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ def LinalgToXeGPU : Pass<"linalg-to-xegpu", "func::FuncOp"> {
9393
"DPAS register block sizes MxNxK">,
9494
];
9595
}
96+
97+
def AddContextArg : Pass<"add-ctx-arg", "func::FuncOp"> {
98+
let summary = "Add a context argument.";
99+
let description = [{
100+
Add a new memref argument to the function, that could be used to pass some context.
101+
}];
102+
}
103+
104+
def GpuToGpuOcl : Pass<"gpu-to-gpuocl", "ModuleOp"> {
105+
let summary = "Convert the GPU operations to GpuOclRuntime calls.";
106+
let description = [{
107+
Convert the gpu alloc, dealloc, memcpy and launch operations to GpuOclRuntime calls.
108+
}];
109+
}
96110
#endif // GC_USE_IMEX
97111

98112
def IterativeTilingAndFusion : Pass<"iterative-tiling-and-fusion",
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===-- AddContextArg.cpp - Add context argument ----------------*- 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+
#include "mlir/Conversion/Passes.h"
9+
#include "mlir/Dialect/Func/IR/FuncOps.h"
10+
11+
namespace mlir::gc {
12+
#define GEN_PASS_DECL_ADDCONTEXTARG
13+
#define GEN_PASS_DEF_ADDCONTEXTARG
14+
#include "gc/Transforms/Passes.h.inc"
15+
} // namespace mlir::gc
16+
17+
using namespace mlir;
18+
19+
namespace {
20+
struct AddContextArg final : gc::impl::AddContextArgBase<AddContextArg> {
21+
void runOnOperation() override {
22+
auto func = getOperation();
23+
if (func.isExternal()) {
24+
return;
25+
}
26+
27+
auto funcType = func.getFunctionType();
28+
auto argTypes = llvm::to_vector<8>(funcType.getInputs());
29+
auto resultTypes = llvm::to_vector<1>(funcType.getResults());
30+
auto ctx = func->getContext();
31+
auto newArgType = MemRefType::get({}, IntegerType::get(ctx, 8));
32+
argTypes.emplace_back(newArgType);
33+
auto newFuncType = FunctionType::get(ctx, argTypes, resultTypes);
34+
func.setType(newFuncType);
35+
func.getBody().front().addArgument(newArgType, func.getLoc());
36+
37+
// Find all function calls and append the last argument of the current
38+
// function to the call.
39+
auto module = func->getParentOfType<ModuleOp>();
40+
func.walk([&](func::CallOp call) {
41+
// If the function to be called is defined in the current module, then the
42+
// context arg will be added to this function signature either and, thus,
43+
// wee need add the context arg to the function call.
44+
if (auto callee = module.lookupSymbol<func::FuncOp>(call.getCallee());
45+
!callee || callee.isExternal()) {
46+
return;
47+
}
48+
auto args = llvm::to_vector<8>(call.getOperands());
49+
args.emplace_back(func.getArgument(func.getNumArguments() - 1));
50+
call->setOperands(args);
51+
});
52+
}
53+
};
54+
} // namespace

lib/gc/Transforms/GPU/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
gc_add_mlir_library(GcGpuPasses
2+
AddContextArg.cpp
3+
GpuToGpuOcl.cpp
24
LinalgToXeGPU.cpp
35
Pipeline.cpp
46

0 commit comments

Comments
 (0)