[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 01:29:08 PDT 2026
https://github.com/keshavvinayak01 created https://github.com/llvm/llvm-project/pull/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 .
>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] [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>
More information about the flang-commits
mailing list