[flang-commits] [flang] [Flang][OpenMP] Prevent allocate directive ICE on module variables (PR #216021)

via flang-commits flang-commits at lists.llvm.org
Thu Aug 13 05:08:43 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: Sergio Afonso (skatrak)

<details>
<summary>Changes</summary>

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.

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


1 Files Affected:

- (modified) flang/lib/Lower/OpenMP/OpenMP.cpp (+9) 


``````````diff
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());

``````````

</details>


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


More information about the flang-commits mailing list