[Mlir-commits] [mlir] [GPUToLLVMSPV] Reject module-scope gpu.barrier during LLVM-SPIRV lowering (PR #187377)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Mar 18 13:55:34 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Ayush Kumar Gaur (Ayush3941)

<details>
<summary>Changes</summary>

gpu.barrier only makes sense inside an executable function/kernel body, not directly under gpu.module.
Without that context, lowering could produce a top-level llvm.call @<!-- -->_Z7barrierj, which later crashes with a parentless instruction.
Add a guard in GPUBarrierConversion so this case fails legalization cleanly instead of generating invalid LLVM IR.
Fixes #<!-- -->186567

---
Full diff: https://github.com/llvm/llvm-project/pull/187377.diff


2 Files Affected:

- (modified) mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp (+4) 
- (added) mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv-invalid.mlir (+8) 


``````````diff
diff --git a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
index 6efd137f513e9..701909b30ea00 100644
--- a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
+++ b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
@@ -113,6 +113,10 @@ struct GPUBarrierConversion final : ConvertOpToLLVMPattern<gpu::BarrierOp> {
                   ConversionPatternRewriter &rewriter) const final {
     constexpr StringLiteral funcName = "_Z7barrierj";
 
+    if (!op->getParentOfType<FunctionOpInterface>())
+      return rewriter.notifyMatchFailure(
+          op, "must be nested in a function body before LLVM-SPIRV lowering");
+
     Operation *moduleOp = op->getParentWithTrait<OpTrait::SymbolTable>();
     assert(moduleOp && "Expecting module");
     Type flagTy = rewriter.getI32Type();
diff --git a/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv-invalid.mlir b/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv-invalid.mlir
new file mode 100644
index 0000000000000..091798101539e
--- /dev/null
+++ b/mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv-invalid.mlir
@@ -0,0 +1,8 @@
+// RUN: mlir-opt -pass-pipeline="builtin.module(gpu.module(convert-gpu-to-llvm-spv))" -verify-diagnostics %s
+
+module attributes {gpu.container_module} {
+  gpu.module @kernels {
+    // expected-error @below {{failed to legalize operation 'gpu.barrier' that was explicitly marked illegal}}
+    gpu.barrier
+  }
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/187377


More information about the Mlir-commits mailing list