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

Ayush Kumar Gaur llvmlistbot at llvm.org
Wed Mar 18 13:55:01 PDT 2026


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

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

>From ce3efdc8f284385ecf8ef82f6149e5a012b57550 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Wed, 18 Mar 2026 16:50:43 -0400
Subject: [PATCH 1/2] [GPUToLLVMSPV] Reject gpu.barrier outside function body
 during lowering

---
 mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp         | 4 ++++
 .../Conversion/GPUToLLVMSPV/gpu-to-llvm-spv-invalid.mlir  | 8 ++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 mlir/test/Conversion/GPUToLLVMSPV/gpu-to-llvm-spv-invalid.mlir

diff --git a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
index 6efd137f513e9..428b7cd2978e9 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
+  }
+}

>From 6df7194b5a4be423e9e78e169413fc582f6a8d88 Mon Sep 17 00:00:00 2001
From: Ayush3941 <ayushkgaur1 at gmail.com>
Date: Wed, 18 Mar 2026 16:51:02 -0400
Subject: [PATCH 2/2] [GPUToLLVMSPV] Reject gpu.barrier outside function body
 during lowering v2

---
 mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
index 428b7cd2978e9..701909b30ea00 100644
--- a/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
+++ b/mlir/lib/Conversion/GPUToLLVMSPV/GPUToLLVMSPV.cpp
@@ -115,7 +115,7 @@ struct GPUBarrierConversion final : ConvertOpToLLVMPattern<gpu::BarrierOp> {
 
     if (!op->getParentOfType<FunctionOpInterface>())
       return rewriter.notifyMatchFailure(
-    op, "must be nested in a function body before LLVM-SPIRV lowering");
+          op, "must be nested in a function body before LLVM-SPIRV lowering");
 
     Operation *moduleOp = op->getParentWithTrait<OpTrait::SymbolTable>();
     assert(moduleOp && "Expecting module");



More information about the Mlir-commits mailing list