[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 08:46:19 PDT 2026


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

>From b04f5be2d7087e5538023b1a848ac7483fc2edc4 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 +++++++++
 flang/test/Lower/OpenMP/Todo/allocate-module.f90 | 9 +++++++++
 2 files changed, 18 insertions(+)
 create mode 100644 flang/test/Lower/OpenMP/Todo/allocate-module.f90

diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index a16889aa365d9..6c0ce79066802 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. In the case of modules, there is no place in which to emit the
+  // deallocation cleanup when that stage is reached, crashing the compiler
+  // during teardown.
+  if (converter.getCurrentScope().kind() == semantics::Scope::Kind::Module)
+    TODO(converter.genLocation(allocate.source),
+         "OpenMP ALLOCATE directive in unsupported declaration scope");
+
   lower::StatementContext stmtCtx;
   ObjectList objects = makeObjects((allocate.BeginDir().Arguments()), semaCtx);
   const auto &clauseList = (allocate.BeginDir().Clauses());
diff --git a/flang/test/Lower/OpenMP/Todo/allocate-module.f90 b/flang/test/Lower/OpenMP/Todo/allocate-module.f90
new file mode 100644
index 0000000000000..e27ba83518562
--- /dev/null
+++ b/flang/test/Lower/OpenMP/Todo/allocate-module.f90
@@ -0,0 +1,9 @@
+! RUN: %not_todo_cmd bbc -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
+! RUN: %not_todo_cmd %flang_fc1 -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
+
+! CHECK: not yet implemented: OpenMP ALLOCATE directive in unsupported declaration scope
+module omp_allocate_module
+  implicit none
+  integer :: x
+  !$omp allocate(x)
+end module omp_allocate_module



More information about the flang-commits mailing list