[flang-commits] [flang] [flang] Reject module-scope fir.alloca (PR #214157)
Keshav Vinayak Jha via flang-commits
flang-commits at lists.llvm.org
Wed Aug 5 22:26:05 PDT 2026
https://github.com/keshavvinayak01 updated https://github.com/llvm/llvm-project/pull/214157
>From 21a0f1a3d056088a8663841c174f907ed34be34f Mon Sep 17 00:00:00 2001
From: Keshav Vinayak Jha <keshavvinayakjha at gmail.com>
Date: Wed, 5 Aug 2026 13:54:14 +0530
Subject: [PATCH 1/2] [flang] Reject module-scope fir.alloca
Diagnose fir.alloca operations placed directly in a module before FIR-to-LLVM lowering searches for an insertion block.
Co-authored-by: Codex <noreply at openai.com>
Signed-off-by: Keshav Vinayak Jha <keshavvinayakjha at gmail.com>
---
flang/lib/Optimizer/Dialect/FIROps.cpp | 2 ++
flang/test/Fir/invalid.fir | 5 +++++
2 files changed, 7 insertions(+)
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>
>From 9641dea521077662cc2d87e6afcb16f7a47d2b36 Mon Sep 17 00:00:00 2001
From: Keshav Vinayak Jha <keshavvinayakjha at gmail.com>
Date: Thu, 6 Aug 2026 10:55:51 +0530
Subject: [PATCH 2/2] [flang] Document fir.alloca module-scope restriction
Clarify that module scope does not provide the stack allocation context required by fir.alloca.
Co-authored-by: Codex <noreply at openai.com>
Signed-off-by: Keshav Vinayak Jha <keshavvinayakjha at gmail.com>
---
flang/include/flang/Optimizer/Dialect/FIROps.td | 3 +++
1 file changed, 3 insertions(+)
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
More information about the flang-commits
mailing list