Skip to content

Conversation

@dev-tomek
Copy link

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.

@dev-tomek dev-tomek requested a review from fabianmcg as a code owner November 21, 2025 11:03
@github-actions
Copy link

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.

@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2025

@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

@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2025

@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

@dev-tomek
Copy link
Author

Ping

@dev-tomek
Copy link
Author

@fabianmcg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants