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

Susan Tan ス-ザン タン via flang-commits flang-commits at lists.llvm.org
Tue Mar 24 08:49:35 PDT 2026


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

>From a7646d6d298d46c7438ea377d858d134fb01f6fa Mon Sep 17 00:00:00 2001
From: Susan Tan <zujunt at nvidia.com>
Date: Mon, 23 Mar 2026 17:29:49 -0700
Subject: [PATCH 1/3] acc.terminator handling

---
 flang/lib/Lower/OpenACC.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

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>(

>From bf543c53dc5a36432deaa1f7e0507f3986964255 Mon Sep 17 00:00:00 2001
From: Susan Tan <zujunt at nvidia.com>
Date: Mon, 23 Mar 2026 18:04:13 -0700
Subject: [PATCH 2/3] add test

---
 flang/test/Lower/OpenACC/acc-declare.f90 | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

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 =

>From 4fabfad23b10878ff891bad74070bd4eccb99290 Mon Sep 17 00:00:00 2001
From: Susan Tan <zujunt at nvidia.com>
Date: Tue, 24 Mar 2026 08:49:21 -0700
Subject: [PATCH 3/3] change to use emitError

---
 flang/lib/Lower/OpenACC.cpp | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 24a568ca85051..d9446fcdd24c8 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -4438,8 +4438,11 @@ void Fortran::lower::attachDeclarePostAllocAction(
     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");
+    if (termOp->getOperands().size() != 0) {
+      mlir::emitError(termOp.getLoc(),
+                      "expect only acc.terminator op with no operand");
+      return;
+    }
     op = op->getPrevNode();
   }
   assert(op && "expect operation to attach the post allocation action");
@@ -4516,8 +4519,11 @@ void Fortran::lower::attachDeclarePostDeallocAction(
     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");
+    if (termOp->getOperands().size() != 0) {
+      mlir::emitError(termOp.getLoc(),
+                      "expect only acc.terminator op with no operand");
+      return;
+    }
     op = op->getPrevNode();
   }
   assert(op && "expect operation to attach the post deallocation action");



More information about the flang-commits mailing list