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 d855aee
Show file tree
Hide file tree
Showing 5 changed files with 676 additions and 0 deletions.
58 changes: 58 additions & 0 deletions include/gc/ExecutionEngine/GPURuntime/OclGpuRuntimeWrappers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===-- OclGpuRuntimeWrappers.h ---------------------------------*- 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 @@ -95,6 +95,13 @@ def LinalgToXeGPU : Pass<"linalg-to-xegpu", "func::FuncOp"> {
}
#endif // GC_USE_IMEX

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.
}];
}

def IterativeTilingAndFusion : Pass<"iterative-tiling-and-fusion",
"func::FuncOp"> {
let summary = "Iterative tiling and fusion for any tilable operation";
Expand Down
56 changes: 56 additions & 0 deletions include/gc/Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2024 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

#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 d855aee

Please sign in to comment.