[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:40 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-fir-hlfir
Author: shivaramaarao
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/223287.diff
2 Files Affected:
- (added) flang-rt/test/Driver/allocate-already-allocated.f90 (+14)
- (added) flang/test/Lower/allocate-already-allocated.f90 (+23)
``````````diff
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 0000000000000..f746e5e26a339
--- /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 0000000000000..44b1bd22404bb
--- /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
``````````
</details>
https://github.com/llvm/llvm-project/pull/223287
More information about the flang-commits
mailing list