[flang-commits] [flang] [openacc][flang] Avoid attaching declare post-actions to acc.terminator (PR #188157)

via flang-commits flang-commits at lists.llvm.org
Mon Mar 23 18:07:17 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-openacc

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

Author: Susan Tan (ス-ザン タン) (SusanTan)

<details>
<summary>Changes</summary>

Fix lowering so post declare-actions are not attached to acc.terminator (step back from trailing fir.result/acc.terminator before attaching).

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


2 Files Affected:

- (modified) flang/lib/Lower/OpenACC.cpp (+10) 
- (modified) flang/test/Lower/OpenACC/acc-declare.f90 (+14) 


``````````diff
diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 2136f1d540a6a..24a568ca85051 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -4437,6 +4437,11 @@ void Fortran::lower::attachDeclarePostAllocAction(
            "expect only fir.result op with no operand");
     op = op->getPrevNode();
   }
+  if (auto termOp = mlir::dyn_cast<mlir::acc::TerminatorOp>(*op)) {
+    assert(termOp->getOperands().size() == 0 &&
+           "expect only acc.terminator op with no operand");
+    op = op->getPrevNode();
+  }
   assert(op && "expect operation to attach the post allocation action");
 
   if (op->hasAttr(mlir::acc::getDeclareActionAttrName())) {
@@ -4510,6 +4515,11 @@ void Fortran::lower::attachDeclarePostDeallocAction(
            "expect only fir.result op with no operand");
     op = op->getPrevNode();
   }
+  if (auto termOp = mlir::dyn_cast<mlir::acc::TerminatorOp>(*op)) {
+    assert(termOp->getOperands().size() == 0 &&
+           "expect only acc.terminator op with no operand");
+    op = op->getPrevNode();
+  }
   assert(op && "expect operation to attach the post deallocation action");
   if (op->hasAttr(mlir::acc::getDeclareActionAttrName())) {
     auto attr = op->getAttrOfType<mlir::acc::DeclareActionAttr>(
diff --git a/flang/test/Lower/OpenACC/acc-declare.f90 b/flang/test/Lower/OpenACC/acc-declare.f90
index 12c32c58f86b9..74a043cec9254 100644
--- a/flang/test/Lower/OpenACC/acc-declare.f90
+++ b/flang/test/Lower/OpenACC/acc-declare.f90
@@ -431,9 +431,23 @@ subroutine init()
     integer :: stat
     allocate(x(10), y(10), stat=stat)
   end subroutine
+
+  subroutine init_in_data(e)
+    use iso_c_binding, only: c_ptr
+    type(c_ptr), intent(in) :: e
+    integer :: stat
+    !$acc data deviceptr(e)
+      allocate(x(10), stat=stat)
+      deallocate(x, stat=stat)
+    !$acc end data
+  end subroutine
 end module
 
 ! CHECK-LABEL: func.func @_QMacc_declare_post_action_statPinit()
 ! CHECK: fir.call @_FortranAAllocatableAllocate({{.*}}) fastmath<contract> {acc.declare_action = #acc.declare_action<postAlloc = @_QMacc_declare_post_action_statEx_acc_declare_post_alloc>} : (!fir.ref<!fir.box<none>>, !fir.ref<i64>, i1, !fir.box<none>, !fir.ref<i8>, i32, {{.*}}) -> i32
 ! CHECK: fir.if
 ! CHECK: fir.call @_FortranAAllocatableAllocate({{.*}}) fastmath<contract> {acc.declare_action = #acc.declare_action<postAlloc = @_QMacc_declare_post_action_statEy_acc_declare_post_alloc>} : (!fir.ref<!fir.box<none>>, !fir.ref<i64>, i1, !fir.box<none>, !fir.ref<i8>, i32, {{.*}}) -> i32
+! CHECK-LABEL: func.func @_QMacc_declare_post_action_statPinit_in_data(
+! CHECK: fir.call @_FortranAAllocatableAllocate({{.*}}) fastmath<contract> {acc.declare_action = #acc.declare_action<postAlloc = @_QMacc_declare_post_action_statEx_acc_declare_post_alloc>}
+! CHECK: fir.call @_FortranAAllocatableDeallocate({{.*}}) fastmath<contract> {acc.declare_action = #acc.declare_action<preDealloc = @_QMacc_declare_post_action_statEx_acc_declare_pre_dealloc, postDealloc = @_QMacc_declare_post_action_statEx_acc_declare_post_dealloc>}
+! CHECK-NOT: acc.terminator {acc.declare_action =

``````````

</details>


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


More information about the flang-commits mailing list