[flang-commits] [flang] [flang][OpenACC] Resolve associations before testing declare flags (PR #221346)

via flang-commits flang-commits at lists.llvm.org
Fri Sep 4 14:18:33 PDT 2026


https://github.com/yebinchon updated https://github.com/llvm/llvm-project/pull/221346

>From ca733b075830a9b6c5ce08f3d3044352cd1001e9 Mon Sep 17 00:00:00 2001
From: Yebin Chon <ychon at nvidia.com>
Date: Fri, 4 Sep 2026 14:04:21 -0700
Subject: [PATCH 1/2] [flang][OpenACC] Resolve associations before testing
 declare flags

---
 flang/lib/Lower/Allocatable.cpp | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/flang/lib/Lower/Allocatable.cpp b/flang/lib/Lower/Allocatable.cpp
index 5f79b77592307..5f10319ab0101 100644
--- a/flang/lib/Lower/Allocatable.cpp
+++ b/flang/lib/Lower/Allocatable.cpp
@@ -484,9 +484,9 @@ class AllocateStmtHelper {
 
   void postAllocationAction(const Allocation &alloc,
                             const fir::MutableBoxValue &box) {
-    if (alloc.getSymbol().test(Fortran::semantics::Symbol::Flag::AccDeclare))
-      Fortran::lower::attachDeclarePostAllocAction(converter, builder,
-                                                   alloc.getSymbol());
+    auto &ult = alloc.getSymbol().GetUltimate();
+    if (ult.test(Fortran::semantics::Symbol::Flag::AccDeclare))
+      Fortran::lower::attachDeclarePostAllocAction(converter, builder, ult);
   }
 
   void setPinnedToFalse() {
@@ -929,16 +929,18 @@ static void preDeallocationAction(Fortran::lower::AbstractConverter &converter,
                                   fir::FirOpBuilder &builder,
                                   mlir::Value beginOpValue,
                                   const Fortran::semantics::Symbol &sym) {
-  if (sym.test(Fortran::semantics::Symbol::Flag::AccDeclare))
+  auto &ult = sym.GetUltimate();
+  if (ult.test(Fortran::semantics::Symbol::Flag::AccDeclare))
     Fortran::lower::attachDeclarePreDeallocAction(converter, builder,
-                                                  beginOpValue, sym);
+                                                  beginOpValue, ult);
 }
 
 static void postDeallocationAction(Fortran::lower::AbstractConverter &converter,
                                    fir::FirOpBuilder &builder,
                                    const Fortran::semantics::Symbol &sym) {
-  if (sym.test(Fortran::semantics::Symbol::Flag::AccDeclare))
-    Fortran::lower::attachDeclarePostDeallocAction(converter, builder, sym);
+  auto &ult = sym.GetUltimate();
+  if (ult.test(Fortran::semantics::Symbol::Flag::AccDeclare))
+    Fortran::lower::attachDeclarePostDeallocAction(converter, builder, ult);
 }
 
 static mlir::Value genCudaDeallocate(fir::FirOpBuilder &builder,

>From 30e0707b63e28191f5056685e65162019bfb1033 Mon Sep 17 00:00:00 2001
From: Yebin Chon <ychon at nvidia.com>
Date: Fri, 4 Sep 2026 14:18:19 -0700
Subject: [PATCH 2/2] add test

---
 ...cc-declare-host-associated-allocatable.f90 | 63 +++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 flang/test/Lower/OpenACC/acc-declare-host-associated-allocatable.f90

diff --git a/flang/test/Lower/OpenACC/acc-declare-host-associated-allocatable.f90 b/flang/test/Lower/OpenACC/acc-declare-host-associated-allocatable.f90
new file mode 100644
index 0000000000000..e3e2d9fbcdc35
--- /dev/null
+++ b/flang/test/Lower/OpenACC/acc-declare-host-associated-allocatable.f90
@@ -0,0 +1,63 @@
+! Test !$acc declare create on an allocatable that is allocated and deallocated
+! from an internal subprogram. The OpenACC flags are set on the host scope
+! symbol, so the host associated symbol seen at the ALLOCATE and DEALLOCATE
+! statements has to be resolved before the declare actions are attached.
+! Covers both the runtime call path (derived type) and the inlined path
+! (intrinsic type).
+
+! RUN: bbc -fopenacc -emit-hlfir %s -o - | FileCheck %s
+! RUN: bbc -fopenacc -emit-hlfir %s -o - | fir-opt --acc-declare-action-conversion | \
+! RUN:   FileCheck %s --check-prefix=CONV
+
+program acc_declare_host_assoc
+  type domain_type
+     integer :: nvar
+  end type domain_type
+
+  type(domain_type), allocatable :: domains(:)
+  integer, allocatable :: data(:)
+  !$acc declare create(domains, data)
+
+  call init
+  call fini
+
+contains
+
+  subroutine init()
+    allocate(domains(10))
+    allocate(data(10))
+  end subroutine init
+
+  subroutine fini()
+    deallocate(domains)
+    deallocate(data)
+  end subroutine fini
+
+end program acc_declare_host_assoc
+
+! CHECK-LABEL: func.func private @_QFPinit()
+! CHECK: fir.call @_FortranAAllocatableAllocate({{.*}}) fastmath<contract> {acc.declare_action = #acc.declare_action<postAlloc = @_QFEdomains_acc_declare_post_alloc>}
+! CHECK: fir.allocmem !fir.array<?xi32>
+! CHECK: fir.store %{{.*}} to %{{.*}} {acc.declare_action = #acc.declare_action<postAlloc = @_QFEdata_acc_declare_post_alloc>} : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+
+! CHECK-LABEL: func.func private @_QFPfini()
+! CHECK: fir.call @_FortranAAllocatableDeallocate({{.*}}) fastmath<contract> {acc.declare_action = #acc.declare_action<preDealloc = @_QFEdomains_acc_declare_pre_dealloc, postDealloc = @_QFEdomains_acc_declare_post_dealloc>}
+! CHECK: fir.box_addr %{{.*}} {acc.declare_action = #acc.declare_action<preDealloc = @_QFEdata_acc_declare_pre_dealloc>} : (!fir.box<!fir.heap<!fir.array<?xi32>>>) -> !fir.heap<!fir.array<?xi32>>
+! CHECK: fir.freemem
+! CHECK: fir.store %{{.*}} to %{{.*}} {acc.declare_action = #acc.declare_action<postDealloc = @_QFEdata_acc_declare_post_dealloc>} : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
+
+! The attributes must be actionable: the conversion pass has to find each
+! recipe and insert the call next to the allocation or deallocation.
+
+! CONV-LABEL: func.func private @_QFPinit()
+! CONV: fir.call @_FortranAAllocatableAllocate(
+! CONV: fir.call @_QFEdomains_acc_declare_post_alloc(%{{.*}}) : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.type<_QFTdomain_type{nvar:i32}>>>>>) -> ()
+! CONV: fir.call @_QFEdata_acc_declare_post_alloc(%{{.*}}) : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> ()
+
+! CONV-LABEL: func.func private @_QFPfini()
+! CONV: fir.call @_QFEdomains_acc_declare_pre_dealloc(%{{.*}}) : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.type<_QFTdomain_type{nvar:i32}>>>>>) -> ()
+! CONV: fir.call @_FortranAAllocatableDeallocate(
+! CONV: fir.call @_QFEdomains_acc_declare_post_dealloc(%{{.*}}) : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.type<_QFTdomain_type{nvar:i32}>>>>>) -> ()
+! CONV: fir.call @_QFEdata_acc_declare_pre_dealloc(%{{.*}}) : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> ()
+! CONV: fir.freemem
+! CONV: fir.call @_QFEdata_acc_declare_post_dealloc(%{{.*}}) : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> ()



More information about the flang-commits mailing list