Skip to content

Commit

Permalink
Convert a subset of GPU dialect ops to the GPU OpenCL runtime calls
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AndreyPavlenko committed Sep 12, 2024
1 parent 9658857 commit 03d6a65
Show file tree
Hide file tree
Showing 4 changed files with 566 additions and 0 deletions.
27 changes: 27 additions & 0 deletions include/gc/ExecutionEngine/GPURuntime/GpuOclRuntime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- GpuOclRuntime.h - GPU OpenCL runtime --------------------*- C++ -*-===//
//
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef GC_GPUOCLMODULE_H
#define GC_GPUOCLMODULE_H

#define GC_GPU_OCL_MALLOC "gcGpuOclMaloc"
#define GC_GPU_OCL_DEALLOC "gcGpuOclDealloc"
#define GC_GPU_OCL_MEMCPY "gcGpuOclMemcpy"
#define GC_GPU_OCL_KERNEL_CREATE "gcGpuOclKernelCreate"
#define GC_GPU_OCL_KERNEL_DESTROY "gcGpuOclKernelDestroy"
#define GC_GPU_OCL_KERNEL_LAUNCH "gcGpuOclKernelLaunch"
#define GC_GPU_OCL_MOD_DESTRUCTOR "gcGpuOclModuleDestructor"

#ifndef GC_GPU_OCL_DEF_ONLY

// TBD

#else
#undef GC_GPU_OCL_DEF_ONLY
#endif
#endif
7 changes: 7 additions & 0 deletions include/gc/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ def LinalgToXeGPU : Pass<"linalg-to-xegpu", "func::FuncOp"> {
"DPAS register block sizes MxNxK">,
];
}

def GpuToOclGpu : Pass<"gpu-to-gpuocl", "ModuleOp"> {
let summary = "Convert the GPU operations to GpuOclRuntime calls.";
let description = [{
Convert the gpu alloc, dealloc, memcpy and launch operations to GpuOclRuntime calls.
}];
}
#endif // GC_USE_IMEX

def IterativeTilingAndFusion : Pass<"iterative-tiling-and-fusion",
Expand Down
1 change: 1 addition & 0 deletions lib/gc/Transforms/GPU/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
gc_add_mlir_library(GcGpuPasses
GpuToGpuOcl.cpp
LinalgToXeGPU.cpp

DEPENDS
Expand Down
Loading

0 comments on commit 03d6a65

Please sign in to comment.