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

via flang-commits flang-commits at lists.llvm.org
Sat Aug 8 23:38:59 PDT 2026


Author: Keshav Vinayak Jha
Date: 2026-08-09T12:08:54+05:30
New Revision: 05142fc7308d336d316c2d705ee205393f7576ea

URL: https://github.com/llvm/llvm-project/commit/05142fc7308d336d316c2d705ee205393f7576ea
DIFF: https://github.com/llvm/llvm-project/commit/05142fc7308d336d316c2d705ee205393f7576ea.diff

LOG: [flang] Reject module-scope fir.alloca (#214157)

`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

---------

Signed-off-by: Keshav Vinayak Jha <keshavvinayakjha at gmail.com>

Added: 
    

Modified: 
    flang/include/flang/Optimizer/Dialect/FIROps.td
    flang/lib/Optimizer/Dialect/FIROps.cpp
    flang/test/Fir/invalid.fir

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Optimizer/Dialect/FIROps.td b/flang/include/flang/Optimizer/Dialect/FIROps.td
index 7635e3bf82035..9f9d45776b120 100644
--- a/flang/include/flang/Optimizer/Dialect/FIROps.td
+++ b/flang/include/flang/Optimizer/Dialect/FIROps.td
@@ -93,6 +93,9 @@ def fir_AllocaOp : fir_Op<"alloca", [
     an optional name.  The allocation may have a dynamic repetition count
     for allocating a sequence of locations for the specified type.
 
+    A `fir.alloca` cannot be defined directly in a `builtin.module` because
+    module scope does not provide a stack allocation context.
+
     ```
       %c = ... : i64
       %x = fir.alloca i32

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>
 


        


More information about the flang-commits mailing list