Skip to content

Commit

Permalink
Convert a subset of GPU dialect ops to the OpenCL GPU 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 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.
  • Loading branch information
AndreyPavlenko committed Sep 10, 2024
1 parent 9658857 commit 95d6a07
Show file tree
Hide file tree
Showing 5 changed files with 638 additions and 0 deletions.
57 changes: 57 additions & 0 deletions include/gc/ExecutionEngine/GPURuntime/OclGpuRuntimeWrappers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===-- OclGpuRuntimeWrappers.h - Ocl GPU wrappers --------------*- 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_OCLGPURUNTIMEWRAPPERS_H
#define GC_OCLGPURUNTIMEWRAPPERS_H

#define GC_OCL_GPU_MALLOC "gcOclGpuMaloc"
#define GC_OCL_GPU_DEALLOC "gcOclGpuDealloc"
#define GC_OCL_GPU_MEMCPY "gcOclGpuMemcpy"
#define GC_OCL_GPU_KERNEL_CREATE "gcOclGpuKernelCreate"
#define GC_OCL_GPU_KERNEL_DESTROY "gcOclGpuKernelDestroy"
#define GC_OCL_GPU_KERNEL_LAUNCH "gcOclGpuKernelLaunch"

#ifndef GC_OCL_GPU_DEF_ONLY

#include "gc/Utils.h"
#include <CL/cl.h>

struct GcOclGpuKernel;

struct GcOclGpuContext {
cl_command_queue queue;

// The following fields are used only if the queue has the
// CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE property.
cl_uint waitListLen;
const cl_event *waitList;
cl_event *lastEvent;
};

extern "C" {
GC_DLL_EXPORT void *gcOclGpuMaloc(GcOclGpuContext *ctx, size_t size);

GC_DLL_EXPORT void gcOclGpuDealloc(GcOclGpuContext *ctx, void *ptr);

GC_DLL_EXPORT void gcOclGpuMemcpy(GcOclGpuContext *ctx, void *src, void *dst,
size_t size);

GC_DLL_EXPORT GcOclGpuKernel *
gcOclGpuKernelCreate(GcOclGpuContext *ctx, const char *spirv, const char *name,
const size_t *globalSize, const size_t *localSize,
size_t argNum, const size_t *argSize);

GC_DLL_EXPORT void gcOclGpuKernelDestroy(size_t count, GcOclGpuKernel *kernels);

GC_DLL_EXPORT void gcOclGpuKernelLaunch(GcOclGpuContext *ctx,
GcOclGpuKernel *kernel, ...);
}
#else
#undef GC_OCL_GPU_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-oclgpu", "ModuleOp"> {
let summary = "Convert the GPU operations to OclGpuRuntime calls.";
let description = [{
Convert the gpu alloc, dealloc, memcpy and launch operations to OclGpuRuntime calls.
}];
}
#endif // GC_USE_IMEX

def IterativeTilingAndFusion : Pass<"iterative-tiling-and-fusion",
Expand Down
45 changes: 45 additions & 0 deletions include/gc/Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===-- Utils.h - Utils -----------------------------------------*- 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_UTILS_H
#define GC_UTILS_H

#if defined _WIN32 || defined __CYGWIN__
#define GC_DLL_EXPORT __declspec(dllexport)
#else
#define GC_DLL_EXPORT __attribute__((visibility("default")))
#endif

#ifdef _NDEBUG
#define gcLogD(...)
#define gcLogE(...)
#else
#include <iostream>

static void _gcLogPrint(std::ostream &stream) { stream << std::endl; }

template <typename T, typename... Args>
static void _gcLogPrint(std::ostream &stream, T first, Args... args) {
stream << first;
_gcLogPrint(stream, args...);
}

template <typename... Args>
static void _gcLog(std::ostream &stream, const char *pref, const char *fileName,
int lineNum, Args... args) {
stream << pref << " [" << fileName << ":" << lineNum << "] ";
_gcLogPrint(stream, args...);
}

#define gcLog(stream, pref, ...) \
_gcLog(stream, pref, __FILE__, __LINE__, __VA_ARGS__)
#define gcLogD(...) gcLog(std::cout, "[DEBUG]", __VA_ARGS__)
#define gcLogE(...) gcLog(std::cerr, "[ERROR]", __VA_ARGS__)
#endif

#endif
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
GpuToOclGpu.cpp
LinalgToXeGPU.cpp

DEPENDS
Expand Down
Loading

0 comments on commit 95d6a07

Please sign in to comment.