[flang-commits] [flang] [flang] Reject module-scope fir.alloca (PR #214157)

via flang-commits flang-commits at lists.llvm.org
Wed Aug 5 01:30:01 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

Author: Keshav Vinayak Jha (keshavvinayak01)

<details>
<summary>Changes</summary>

`fir.alloca` represents a stack allocation, but its verifier currently accepts operations placed directly in a `builtin.module`. During FIR-to-LLVM conversion,  `AllocaOpConversion::matchAndRewrite` calls `getBlockForAllocaInsert`, which performs a `dyn_cast` to `OutlineableOpenMPOpInterface` on the alloca's parent op.

For a module-level alloca the expected parent function/block does not exist and the cast/assert fails.

This patch rejectsdirect module-scope `fir.alloca` operations during FIR verification.

Assisted-by: codex

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


2 Files Affected:

- (modified) flang/lib/Optimizer/Dialect/FIROps.cpp (+2) 
- (modified) flang/test/Fir/invalid.fir (+5) 


``````````diff
diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp
index 970efae5488a2..c681c03fba777 100644
--- a/flang/lib/Optimizer/Dialect/FIROps.cpp
+++ b/flang/lib/Optimizer/Dialect/FIROps.cpp
@@ -382,6 +382,8 @@ void fir::AllocaOp::print(mlir::OpAsmPrinter &p) {
 }
 
 llvm::LogicalResult fir::AllocaOp::verify() {
+  if (mlir::isa_and_nonnull<mlir::ModuleOp>((*this)->getParentOp()))
+    return emitOpError("must not be defined at module scope");
   llvm::SmallVector<llvm::StringRef> visited;
   if (verifyInType(getInType(), visited, numShapeOperands()))
     return emitOpError("invalid type for allocation");
diff --git a/flang/test/Fir/invalid.fir b/flang/test/Fir/invalid.fir
index ce31ceb3f069c..26f37a0c1b798 100644
--- a/flang/test/Fir/invalid.fir
+++ b/flang/test/Fir/invalid.fir
@@ -1,5 +1,10 @@
 // RUN: fir-opt -split-input-file -verify-diagnostics --strict-fir-volatile-verifier %s
 
+// expected-error at +1{{'fir.alloca' op must not be defined at module scope}}
+%0 = fir.alloca i32 {adapt.valuebyref}
+
+// -----
+
 // expected-error at +1{{custom op 'fir.string_lit' must have character type}}
 %0 = fir.string_lit "Hello, World!"(13) : !fir.int<32>
 

``````````

</details>


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


More information about the flang-commits mailing list