[flang-commits] [flang] [Flang][OpenMP] Prevent allocate directive ICE on module variables (PR #216021)
Sergio Afonso via flang-commits
flang-commits at lists.llvm.org
Thu Aug 13 05:07:52 PDT 2026
https://github.com/skatrak created https://github.com/llvm/llvm-project/pull/216021
The current lowering implementation for `allocate` directives assumes the MLIR function in which it is creating operations will still be there by finalization time, so that it can add a deallocation call.
When lowering Fortran modules, this is not the case (lowering happens in a temporary dummy function) and it results in a compiler crash while running cleanup callbacks. This patch adds a TODO for this case.
>From ebab8954bfafcbd0dc1f41877c285513fc167f1f Mon Sep 17 00:00:00 2001
From: Sergio Afonso <Sergio.AfonsoFumero at amd.com>
Date: Wed, 12 Aug 2026 16:00:02 +0100
Subject: [PATCH] [Flang][OpenMP] Prevent allocate directive ICE on module
variables
The current lowering implementation for `allocate` directives assumes
the MLIR function in which it is creating operations will still be there
by finalization time, so that it can add a deallocation call.
When lowering Fortran modules, this is not the case (lowering happens
in a temporary dummy function) and it results in a compiler crash
while running cleanup callbacks. This patch adds a TODO for this case.
---
flang/lib/Lower/OpenMP/OpenMP.cpp | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index a16889aa365d9..fad704cb90895 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -5788,6 +5788,15 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx,
lower::pft::Evaluation &eval,
const parser::OmpAllocateDirective &allocate) {
+ // The allocate directive is lowered as a runtime allocation with a matching
+ // deallocation registered as a cleanup at the exit of the enclosing function
+ // scope, which only works within a function. In the case of e.g. modules,
+ // there is no place in which to emit the deallocation cleanup when that stage
+ // is reached, crashing the compiler during teardown.
+ if (!converter.getFirOpBuilder().getFunction())
+ TODO(converter.genLocation(allocate.source),
+ "OpenMP ALLOCATE directive in non-function declaration scope");
+
lower::StatementContext stmtCtx;
ObjectList objects = makeObjects((allocate.BeginDir().Arguments()), semaCtx);
const auto &clauseList = (allocate.BeginDir().Clauses());
More information about the flang-commits
mailing list