[MLIR][GPUToLLVMSPV] Use global & local memory scope for GPUBarrierConversion
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.
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 @ followed by their GitHub username.
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)
Changes
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.
Full diff: https://github.com/llvm/llvm-project/pull/169026.diff
2 Files Affected:
- (modified) mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp (+11-6)
- (modified) mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir (+1-1)
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)
Changes
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.
Full diff: https://github.com/llvm/llvm-project/pull/169026.diff
2 Files Affected:
- (modified) mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp (+11-6)
- (modified) mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir (+1-1)
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
@fabianmcg
@kuhar , @antiagainst
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
@dev-tomek can you format the patch?
@dev-tomek Congratulations on having your first Pull Request (PR) merged into the LLVM Project!
Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.
Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.
How to do this, and the rest of the post-merge process, is covered in detail here.
If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.
If you don't get any reports, no action is required from you. Your changes are working as expected, well done!