Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert a subset of GPU dialect ops to the OpenCL GPU runtime calls #333

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

AndreyPavlenko
Copy link
Contributor

@AndreyPavlenko AndreyPavlenko commented Sep 10, 2024

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.

Example of code produced by this path:

module @fragment_name {
  llvm.mlir.global internal constant @gcGpuOclKernel_entry_kernel_SPIRV("SPIRV string") {addr_space = 0 : i32}
  llvm.mlir.global internal constant @gcGpuOclKernel_entry_kernel_Name("entry_kernel\00") {addr_space = 0 : i32}
  llvm.mlir.global internal @gcGpuOclKernel_entry_kernel_Ptr() {addr_space = 0 : i32} : !llvm.ptr

  llvm.func @entry(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: i64, %arg3: i64, %arg4: i64, %arg5: i64, %arg6: i64, %arg7: !llvm.ptr, %arg8: !llvm.ptr, %arg9: i64, %arg10: i64, %arg11: i64, %arg12: i64, %arg13: i64, %arg14: !llvm.ptr, %arg15: !llvm.ptr, %arg16: i64, %arg17: i64, %arg18: i64, %arg19: i64, %arg20: i64, %arg21: !llvm.ptr, %arg22: !llvm.ptr, %arg23: i64) attributes {llvm.emit_c_interface} {
    %0 = llvm.mlir.constant(0 : index) : i64
    %1 = llvm.call @getGcGpuOclKernel_entry_kernel(%arg21) : (!llvm.ptr) -> !llvm.ptr
    llvm.call @gcGpuOclKernelLaunch(%arg21, %1, %arg8, %0, %arg1, %arg15) vararg(!llvm.func<void (ptr, ptr, ...)>) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, i64, !llvm.ptr, !llvm.ptr) -> ()
    llvm.return
  }

  llvm.func internal @getGcGpuOclKernel_entry_kernel(%arg0: !llvm.ptr) -> !llvm.ptr {
    %0 = llvm.mlir.zero : !llvm.ptr
    %1 = llvm.mlir.addressof @gcGpuOclKernel_entry_kernel_Ptr : !llvm.ptr
    %2 = llvm.load %1 : !llvm.ptr -> !llvm.ptr
    %3 = llvm.icmp "eq" %2, %0 : !llvm.ptr
    llvm.cond_br %3, ^bb1, ^bb2
  ^bb1:  // pred: ^bb0
    %4 = llvm.call @createGcGpuOclKernel_entry_kernel(%arg0) : (!llvm.ptr) -> !llvm.ptr
    llvm.return %4 : !llvm.ptr
  ^bb2:  // pred: ^bb0
    llvm.return %2 : !llvm.ptr
  }

  llvm.func internal @createGcGpuOclKernel_entry_kernel(%arg0: !llvm.ptr) -> !llvm.ptr {
    %0 = llvm.mlir.addressof @gcGpuOclKernel_entry_kernel_Ptr : !llvm.ptr
    %1 = llvm.mlir.zero : !llvm.ptr
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.mlir.constant(64 : i64) : i64
    %4 = llvm.mlir.constant(3 : i64) : i64
    %5 = llvm.mlir.constant(4 : i64) : i64
    %6 = llvm.mlir.addressof @gcGpuOclKernel_entry_kernel_SPIRV : !llvm.ptr
    %7 = llvm.mlir.addressof @gcGpuOclKernel_entry_kernel_Name : !llvm.ptr
    %8 = llvm.getelementptr %7[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<13 x i8>
    %9 = llvm.getelementptr %6[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<3820 x i8>
    %10 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
    llvm.store %3, %10 : i64, !llvm.ptr
    %11 = llvm.getelementptr %10[1] : (!llvm.ptr) -> !llvm.ptr, i64
    llvm.store %3, %11 : i64, !llvm.ptr
    %12 = llvm.getelementptr %10[2] : (!llvm.ptr) -> !llvm.ptr, i64
    llvm.store %2, %12 : i64, !llvm.ptr
    %13 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
    llvm.store %2, %13 : i64, !llvm.ptr
    %14 = llvm.getelementptr %13[1] : (!llvm.ptr) -> !llvm.ptr, i64
    llvm.store %2, %14 : i64, !llvm.ptr
    %15 = llvm.getelementptr %13[2] : (!llvm.ptr) -> !llvm.ptr, i64
    llvm.store %2, %15 : i64, !llvm.ptr
    %16 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
    llvm.store %3, %16 : i64, !llvm.ptr
    %17 = llvm.getelementptr %16[1] : (!llvm.ptr) -> !llvm.ptr, i64
    llvm.store %3, %17 : i64, !llvm.ptr
    %18 = llvm.getelementptr %16[2] : (!llvm.ptr) -> !llvm.ptr, i64
    llvm.store %3, %18 : i64, !llvm.ptr
    %19 = llvm.getelementptr %16[3] : (!llvm.ptr) -> !llvm.ptr, i64
    llvm.store %3, %19 : i64, !llvm.ptr
    %20 = llvm.call @gcGpuOclKernelCreate(%arg0, %9, %8, %10, %13, %5, %16) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr, i64, !llvm.ptr) -> !llvm.ptr
    %21 = llvm.cmpxchg %0, %1, %20 acq_rel monotonic : !llvm.ptr, !llvm.ptr
    %22 = llvm.extractvalue %21[1] : !llvm.struct<(ptr, i1)> 
    llvm.cond_br %22, ^bb1, ^bb2
  ^bb1:  // pred: ^bb0
    llvm.return %20 : !llvm.ptr
  ^bb2:  // pred: ^bb0
    %23 = llvm.alloca %2 x !llvm.ptr : (i64) -> !llvm.ptr
    llvm.store %20, %23 : !llvm.ptr, !llvm.ptr
    llvm.call @gcGpuOclKernelDestroy(%2, %23) : (i64, !llvm.ptr) -> ()
    %24 = llvm.extractvalue %21[0] : !llvm.struct<(ptr, i1)> 
    llvm.return %24 : !llvm.ptr
  }

  llvm.func @gcGpuOclModuleDestructor() {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.mlir.addressof @gcGpuOclKernel_entry_kernel_Ptr : !llvm.ptr
    llvm.fence acquire
    %2 = llvm.load %1 : !llvm.ptr -> !llvm.ptr
    %3 = llvm.alloca %0 x !llvm.ptr : (i64) -> !llvm.ptr
    llvm.store %2, %3 : !llvm.ptr, !llvm.ptr
    llvm.call @gcGpuOclKernelDestroy(%0, %3) : (i64, !llvm.ptr) -> ()
    llvm.return
  }

  llvm.func @gcGpuOclKernelCreate(!llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr, i64, !llvm.ptr) -> !llvm.ptr
  llvm.func @gcGpuOclKernelDestroy(i64, !llvm.ptr)
  llvm.func @gcGpuOclKernelLaunch(!llvm.ptr, !llvm.ptr, ...)
}

Copy link
Contributor

@kurapov-peter kurapov-peter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add lits? Would help reviewing.

include/gc/ExecutionEngine/GPURuntime/GpuOclRuntime.h Outdated Show resolved Hide resolved
lib/gc/Transforms/GPU/GpuToGpuOcl.cpp Show resolved Hide resolved
@AndreyPavlenko AndreyPavlenko marked this pull request as ready for review September 14, 2024 20:03
AndreyPavlenko added a commit to AndreyPavlenko/graph-compiler that referenced this pull request Sep 14, 2024
AndreyPavlenko added a commit to AndreyPavlenko/graph-compiler that referenced this pull request Sep 15, 2024
AndreyPavlenko added a commit to AndreyPavlenko/graph-compiler that referenced this pull request Sep 16, 2024
@dchigarev dchigarev mentioned this pull request Sep 17, 2024
AndreyPavlenko added a commit to AndreyPavlenko/graph-compiler that referenced this pull request Sep 18, 2024
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.
AndreyPavlenko added a commit to AndreyPavlenko/graph-compiler that referenced this pull request Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants