[Mlir-commits] [mlir] 7745561 - [MLIR][GPUToLLVMSPV] Use global & local memory scope for GPUBarrierConversion (#169026)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Dec 17 07:29:01 PST 2025
Author: Tomek KuczyĆski
Date: 2025-12-17T10:28:57-05:00
New Revision: 77455615a4be9dc8be0486d15c0528704437b509
URL: https://github.com/llvm/llvm-project/commit/77455615a4be9dc8be0486d15c0528704437b509
DIFF: https://github.com/llvm/llvm-project/commit/77455615a4be9dc8be0486d15c0528704437b509.diff
LOG: [MLIR][GPUToLLVMSPV] Use global & local memory scope for GPUBarrierConversion (#169026)
The MLIR [GPU dialect
docs](https://mlir.llvm.org/docs/Dialects/GPU/#gpubarrier-gpubarrierop)
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](https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/barrier.html)
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.
Added:
Modified:
mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
index f143a9e505f4d..c0480a1dfb512 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,16 @@ 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);
+ Value flag = 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
More information about the Mlir-commits
mailing list