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

Merged
merged 8 commits into from
Sep 24, 2024

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, ...)
}

@AndreyPavlenko AndreyPavlenko force-pushed the gc-ocl-pass branch 2 times, most recently from d855aee to 95d6a07 Compare September 10, 2024 12:33
@kurapov-peter kurapov-peter self-requested a review September 10, 2024 15:02
@AndreyPavlenko AndreyPavlenko force-pushed the gc-ocl-pass branch 7 times, most recently from 03d6a65 to 51cf443 Compare September 12, 2024 13:20
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
@AndreyPavlenko AndreyPavlenko force-pushed the gc-ocl-pass branch 2 times, most recently from d9651f1 to 34415b1 Compare September 19, 2024 21:12
AndreyPavlenko added a commit to AndreyPavlenko/graph-compiler that referenced this pull request Sep 19, 2024
AndreyPavlenko added a commit to AndreyPavlenko/graph-compiler that referenced this pull request Sep 20, 2024
Comment on lines +22 to +27
#ifndef GC_GPU_OCL_CONST_ONLY

// TBD

#else
#undef GC_GPU_OCL_CONST_ONLY
Copy link
Contributor

Choose a reason for hiding this comment

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

dead code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a placeholder for #343. It also undefs this one

#define GC_GPU_OCL_CONST_ONLY

lib/gc/Transforms/GPU/Pipeline.cpp Outdated Show resolved Hide resolved
lib/gc/Transforms/GPU/Pipeline.cpp Outdated Show resolved Hide resolved
@@ -680,7 +680,7 @@ defaultTilingOfType(RewriterBase &rewriter, Operation *op,
} else {
defaultTileSize.resize(iteratorTypes.size(), rewriter.getIndexAttr(0));
// Try tileSize from `32` to `16`.
SmallVector<int64_t> tsOrder = {32, 16};
SmallVector<int64_t> tsOrder = {16, 32};
Copy link
Contributor

Choose a reason for hiding this comment

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

What does the change do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I've totally forgotten about this dirty hack. Without it the entire GPU pipeline does not work. Seems further investigation of this issue is required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems, this is caused by #332

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Worked around by increasing the shapes.

test/mlir/test/gc/gpu-runner/gpu-to-gpuocl.mlir Outdated Show resolved Hide resolved
lib/gc/Transforms/GPU/GpuToGpuOcl.cpp Show resolved Hide resolved
lib/gc/Transforms/GPU/GpuToGpuOcl.cpp Show resolved Hide resolved
Comment on lines +495 to +489
// Else block
// The kernel has already been created by another thread, destroying this
// one.
rewriter.setInsertionPointToStart(elseBlock);
helper.destroyKernels(rewriter, loc, result);
result = rewriter.create<LLVM::ExtractValueOp>(loc, helper.ptrType,
casResult, 0);
rewriter.create<LLVM::ReturnOp>(loc, result);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I follow, why do we destroy the kernel here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the current value of the global field is not null, then some other thread has already created and assigned the the value. In this case, we destroy the created kernel and return that one, created by another thread.

lib/gc/Transforms/GPU/AddContextArg.cpp Outdated Show resolved Hide resolved
@@ -93,6 +93,20 @@ def LinalgToXeGPU : Pass<"linalg-to-xegpu", "func::FuncOp"> {
"DPAS register block sizes MxNxK">,
];
}

def AddContextArg : Pass<"add-ctx-arg", "func::FuncOp"> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should it be applied to kernels only?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it should be applied to the functions, that launch the kernels. The argument is passed to the runtime.

AndreyPavlenko added a commit to AndreyPavlenko/graph-compiler that referenced this pull request Sep 23, 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 24, 2024
Comment on lines 120 to 113
pm.addPass(imex::createConvertGPUXToLLVMPass());
pm.addPass(createGpuToGpuOcl());
Copy link
Contributor

@dchigarev dchigarev Sep 24, 2024

Choose a reason for hiding this comment

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

maybe we should postpone changes to the pipeline until (#343) is merged? Otherwise we're breaking a working pipeline since there's no implementation for the new runtime-funcs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted changes to the pipeline.

Copy link
Contributor

@dchigarev dchigarev left a comment

Choose a reason for hiding this comment

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

LGTM

good to merge once this is addressed

else:
# FIXME: Enable when #343 is merged.
config.excludes = ['gpu-to-gpuocl.mlir']
Copy link
Contributor

Choose a reason for hiding this comment

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

The test should pass even without #343 since it's just the lowering, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, it should pass.

Copy link
Contributor

@dchigarev dchigarev left a comment

Choose a reason for hiding this comment

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

good to be merged from my side

@AndreyPavlenko AndreyPavlenko merged commit 6e13abe into intel:main Sep 24, 2024
6 checks passed
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