[flang-commits] [flang] [llvm] [flang] Check allocation status in inlined ALLOCATE lowering (PR #223287)

via flang-commits flang-commits at lists.llvm.org
Sun Sep 13 19:21:06 PDT 2026


https://github.com/shivaramaarao created https://github.com/llvm/llvm-project/pull/223287


The inlined ALLOCATE path lowered the allocation without testing whether the object was already allocated. This commit Reports a fatal error that names the symbol when the object is already allocated.

Updated existing tests for the extra status-check IR, and added tests that cover lowering and runtime behavior of the new check.

Fixes issue #209709 

Assisted by Cursor in updating existing tests.

>From 0f540b50af5e81818ab4ad66b2b1018a7c79445f Mon Sep 17 00:00:00 2001
From: Shivarama Rao <shivarama.rao at amd.com>
Date: Mon, 14 Sep 2026 07:32:56 +0530
Subject: [PATCH] [flang] Check allocation status in inlined ALLOCATE lowering

The inlined ALLOCATE path lowered the allocation without testing
whether the object was already allocated. This commit Reports a
fatal error that names the symbol when the object is already allocated.

Updated existing tests for the extra status-check IR, and added tests
that cover lowering and runtime behavior of the new check.

Assisted by Cursor in updating existing tests.
---
 .../Driver/allocate-already-allocated.f90     | 14 +++++++++++
 .../test/Lower/allocate-already-allocated.f90 | 23 +++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 flang-rt/test/Driver/allocate-already-allocated.f90
 create mode 100644 flang/test/Lower/allocate-already-allocated.f90

diff --git a/flang-rt/test/Driver/allocate-already-allocated.f90 b/flang-rt/test/Driver/allocate-already-allocated.f90
new file mode 100644
index 00000000000000..f746e5e26a3396
--- /dev/null
+++ b/flang-rt/test/Driver/allocate-already-allocated.f90
@@ -0,0 +1,14 @@
+! UNSUPPORTED: offload-cuda
+
+! RUN: %flang %isysroot -L"%libdir" %s -o %t
+! RUN: not --crash env LD_LIBRARY_PATH="$LD_LIBRARY_PATH:%libdir" %t 2>&1 \
+! RUN:   | FileCheck %s
+
+! CHECK: fatal Fortran runtime error({{.*}}allocate-already-allocated.f90:{{[0-9]+}}): the object 'array' is already allocated
+
+program allocate_twice
+  integer, allocatable :: array(:)
+
+  allocate(array(5))
+  allocate(array(8))
+end program
diff --git a/flang/test/Lower/allocate-already-allocated.f90 b/flang/test/Lower/allocate-already-allocated.f90
new file mode 100644
index 00000000000000..44b1bd22404bbe
--- /dev/null
+++ b/flang/test/Lower/allocate-already-allocated.f90
@@ -0,0 +1,23 @@
+! RUN: bbc -emit-hlfir %s -o - | FileCheck %s
+
+! Verify that the inlined allocation path checks the allocation status before
+! calling the actual allocation function
+
+! CHECK-LABEL: func.func @_QPallocate_twice()
+subroutine allocate_twice()
+  integer, allocatable :: array(:)
+
+  ! CHECK: %[[IS_ALLOCATED_1:.*]] = arith.cmpi ne
+  ! CHECK: fir.if %[[IS_ALLOCATED_1]] {
+  ! CHECK:   fir.call @_FortranAReportFatalUserError
+  ! CHECK: }
+  ! CHECK: fir.allocmem
+  allocate(array(5))
+
+  ! CHECK: %[[IS_ALLOCATED_2:.*]] = arith.cmpi ne
+  ! CHECK: fir.if %[[IS_ALLOCATED_2]] {
+  ! CHECK:   fir.call @_FortranAReportFatalUserError
+  ! CHECK: }
+  ! CHECK: fir.allocmem
+  allocate(array(8))
+end subroutine



More information about the flang-commits mailing list