-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MLIR][GPUToLLVMSPV] Use global & local memory scope for GPUBarrierConversion #169026
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
base: main
Are you sure you want to change the base?
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-mlir Author: Tomek Kuczyński (dev-tomek) ChangesThe MLIR GPU dialect docs specify that gpu::BarrierOp should make all memory accesses visible to all work items in the workgroup. This PR changes the barrier conversion to use CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE, This issue was discovered while investigating numerical instabilities on Intel Battlemage, Full diff: https://github.com/llvm/llvm-project/pull/169026.diff 2 Files Affected:
diff --git a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
index f143a9e505f4d..f62191425ba9f 100644
--- a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
+++ b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
@@ -96,12 +96,14 @@ namespace {
// Barriers
//===----------------------------------------------------------------------===//
-/// Replace `gpu.barrier` with an `llvm.call` to `barrier` with
-/// `CLK_LOCAL_MEM_FENCE` argument, indicating work-group memory scope:
+/// Replace `gpu.barrier` with an `llvm.call` to `barrier` using
+/// `CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE`, ensuring that all memory
+/// accesses are visible to all work-items in the work-group.
/// ```
/// // gpu.barrier
-/// %c1 = llvm.mlir.constant(1: i32) : i32
-/// llvm.call spir_funccc @_Z7barrierj(%c1) : (i32) -> ()
+/// // 3 = CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE
+/// %c3 = llvm.mlir.constant(3: i32) : i32
+/// llvm.call spir_funccc @_Z7barrierj(%c3) : (i32) -> ()
/// ```
struct GPUBarrierConversion final : ConvertOpToLLVMPattern<gpu::BarrierOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
@@ -119,12 +121,15 @@ struct GPUBarrierConversion final : ConvertOpToLLVMPattern<gpu::BarrierOp> {
lookupOrCreateSPIRVFn(moduleOp, funcName, flagTy, voidTy,
/*isMemNone=*/false, /*isConvergent=*/true);
- // Value used by SPIR-V backend to represent `CLK_LOCAL_MEM_FENCE`.
+ // Values used by SPIR-V backend to represent a combination of
+ // `CLK_LOCAL_MEM_FENCE` and `CLK_GLOBAL_MEM_FENCE`.
// See `llvm/lib/Target/SPIRV/SPIRVBuiltins.td`.
constexpr int64_t localMemFenceFlag = 1;
+ constexpr int64_t globalMemFenceFlag = 2;
+ constexpr int64_t localGlobalMemFenceFlag = localMemFenceFlag | globalMemFenceFlag;
Location loc = op->getLoc();
Value flag =
- LLVM::ConstantOp::create(rewriter, loc, flagTy, localMemFenceFlag);
+ LLVM::ConstantOp::create(rewriter, loc, flagTy, localGlobalMemFenceFlag);
rewriter.replaceOp(op, createSPIRVBuiltinCall(loc, rewriter, func, flag));
return success();
}
diff --git a/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir b/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
index 0c2c021b9d43c..227287a9adcee 100644
--- a/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
+++ b/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
@@ -215,7 +215,7 @@ gpu.module @barriers {
// CHECK-LABEL: gpu_barrier
func.func @gpu_barrier() {
- // CHECK: [[FLAGS:%.*]] = llvm.mlir.constant(1 : i32) : i32
+ // CHECK: [[FLAGS:%.*]] = llvm.mlir.constant(3 : i32) : i32
// CHECK: llvm.call spir_funccc @_Z7barrierj([[FLAGS]]) {
// CHECK-SAME-DAG: no_unwind
// CHECK-SAME-DAG: convergent
|
|
@llvm/pr-subscribers-mlir-gpu Author: Tomek Kuczyński (dev-tomek) ChangesThe MLIR GPU dialect docs specify that gpu::BarrierOp should make all memory accesses visible to all work items in the workgroup. This PR changes the barrier conversion to use CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE, This issue was discovered while investigating numerical instabilities on Intel Battlemage, Full diff: https://github.com/llvm/llvm-project/pull/169026.diff 2 Files Affected:
diff --git a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
index f143a9e505f4d..f62191425ba9f 100644
--- a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
+++ b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
@@ -96,12 +96,14 @@ namespace {
// Barriers
//===----------------------------------------------------------------------===//
-/// Replace `gpu.barrier` with an `llvm.call` to `barrier` with
-/// `CLK_LOCAL_MEM_FENCE` argument, indicating work-group memory scope:
+/// Replace `gpu.barrier` with an `llvm.call` to `barrier` using
+/// `CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE`, ensuring that all memory
+/// accesses are visible to all work-items in the work-group.
/// ```
/// // gpu.barrier
-/// %c1 = llvm.mlir.constant(1: i32) : i32
-/// llvm.call spir_funccc @_Z7barrierj(%c1) : (i32) -> ()
+/// // 3 = CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE
+/// %c3 = llvm.mlir.constant(3: i32) : i32
+/// llvm.call spir_funccc @_Z7barrierj(%c3) : (i32) -> ()
/// ```
struct GPUBarrierConversion final : ConvertOpToLLVMPattern<gpu::BarrierOp> {
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
@@ -119,12 +121,15 @@ struct GPUBarrierConversion final : ConvertOpToLLVMPattern<gpu::BarrierOp> {
lookupOrCreateSPIRVFn(moduleOp, funcName, flagTy, voidTy,
/*isMemNone=*/false, /*isConvergent=*/true);
- // Value used by SPIR-V backend to represent `CLK_LOCAL_MEM_FENCE`.
+ // Values used by SPIR-V backend to represent a combination of
+ // `CLK_LOCAL_MEM_FENCE` and `CLK_GLOBAL_MEM_FENCE`.
// See `llvm/lib/Target/SPIRV/SPIRVBuiltins.td`.
constexpr int64_t localMemFenceFlag = 1;
+ constexpr int64_t globalMemFenceFlag = 2;
+ constexpr int64_t localGlobalMemFenceFlag = localMemFenceFlag | globalMemFenceFlag;
Location loc = op->getLoc();
Value flag =
- LLVM::ConstantOp::create(rewriter, loc, flagTy, localMemFenceFlag);
+ LLVM::ConstantOp::create(rewriter, loc, flagTy, localGlobalMemFenceFlag);
rewriter.replaceOp(op, createSPIRVBuiltinCall(loc, rewriter, func, flag));
return success();
}
diff --git a/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir b/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
index 0c2c021b9d43c..227287a9adcee 100644
--- a/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
+++ b/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
@@ -215,7 +215,7 @@ gpu.module @barriers {
// CHECK-LABEL: gpu_barrier
func.func @gpu_barrier() {
- // CHECK: [[FLAGS:%.*]] = llvm.mlir.constant(1 : i32) : i32
+ // CHECK: [[FLAGS:%.*]] = llvm.mlir.constant(3 : i32) : i32
// CHECK: llvm.call spir_funccc @_Z7barrierj([[FLAGS]]) {
// CHECK-SAME-DAG: no_unwind
// CHECK-SAME-DAG: convergent
|
|
Ping |
The MLIR GPU dialect docs specify that gpu::BarrierOp should make all memory accesses visible to all work items in the workgroup.
Current implementation uses only CLK_LOCAL_MEM_FENCE, which per the OpenCL specification guarantees visibility of
only local memory accesses.
This PR changes the barrier conversion to use CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE,
ensuring both local and global memory operations are properly synchronized per the MLIR spec.
This issue was discovered while investigating numerical instabilities on Intel Battlemage,
where race conditions occurred due to incomplete memory synchronization.