[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 09:47:16 PDT 2026


Susan Tan =?utf-8?b?KOOCuS3jgrbjg7PjgIA=?Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/188157 at github.com>


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/7] 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/7] 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/7] 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");

>From eb7033fe7e59cefbb2498298417fe3cbef79b555 Mon Sep 17 00:00:00 2001
From: Susan Tan <zujunt at nvidia.com>
Date: Tue, 24 Mar 2026 09:36:19 -0700
Subject: [PATCH 4/7] tweak

---
 flang/lib/Lower/OpenACC.cpp | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)

diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index d9446fcdd24c8..a1a99a910faa1 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -4432,16 +4432,10 @@ void Fortran::lower::attachDeclarePostAllocAction(
   fctName << converter.mangleName(sym) << declarePostAllocSuffix.str();
   mlir::Operation *op = &builder.getInsertionBlock()->back();
 
-  if (auto resOp = mlir::dyn_cast<fir::ResultOp>(*op)) {
-    assert(resOp.getOperands().size() == 0 &&
-           "expect only fir.result op with no operand");
-    op = op->getPrevNode();
-  }
-  if (auto termOp = mlir::dyn_cast<mlir::acc::TerminatorOp>(*op)) {
-    if (termOp->getOperands().size() != 0) {
-      mlir::emitError(termOp.getLoc(),
-                      "expect only acc.terminator op with no operand");
-      return;
+  if (op && op->hasTrait<mlir::OpTrait::IsTerminator>()) {
+    if (op->getNumOperands() != 0) {
+      fir::emitFatalError(op->getLoc(),
+                          "expect only terminator op with no operand");
     }
     op = op->getPrevNode();
   }
@@ -4513,16 +4507,10 @@ void Fortran::lower::attachDeclarePostDeallocAction(
   std::stringstream fctName;
   fctName << converter.mangleName(sym) << declarePostDeallocSuffix.str();
   mlir::Operation *op = &builder.getInsertionBlock()->back();
-  if (auto resOp = mlir::dyn_cast<fir::ResultOp>(*op)) {
-    assert(resOp.getOperands().size() == 0 &&
-           "expect only fir.result op with no operand");
-    op = op->getPrevNode();
-  }
-  if (auto termOp = mlir::dyn_cast<mlir::acc::TerminatorOp>(*op)) {
-    if (termOp->getOperands().size() != 0) {
-      mlir::emitError(termOp.getLoc(),
-                      "expect only acc.terminator op with no operand");
-      return;
+  if (op && op->hasTrait<mlir::OpTrait::IsTerminator>()) {
+    if (op->getNumOperands() != 0) {
+      fir::emitFatalError(op->getLoc(),
+                          "expect only terminator op with no operand");
     }
     op = op->getPrevNode();
   }

>From 9958fdda46b0342064f07aebf5a1549ed655eaee Mon Sep 17 00:00:00 2001
From: Susan Tan <zujunt at nvidia.com>
Date: Tue, 24 Mar 2026 09:38:06 -0700
Subject: [PATCH 5/7] change emitError

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

diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index a1a99a910faa1..4e62feff4430d 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -4132,7 +4132,8 @@ void createOpenACCRoutineConstruct(
                                 workerDeviceTypes, vectorDeviceTypes) &&
           routineOp.getNohost() == hasNohost)
         return;
-      mlir::emitError(loc, "Routine already specified with different clauses");
+      fir::emitFatalError(loc,
+                          "Routine already specified with different clauses");
     }
   }
   std::stringstream routineOpName;

>From 81aef63a98dd692166af0a1771db7e0efda1250c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Susan=20Tan=20=28=E3=82=B9-=E3=82=B6=E3=83=B3=E3=80=80?=
 =?UTF-8?q?=E3=82=BF=E3=83=B3=29?= <zujunt at nvidia.com>
Date: Tue, 24 Mar 2026 12:46:47 -0400
Subject: [PATCH 6/7] Update flang/lib/Lower/OpenACC.cpp

Co-authored-by: Slava Zakharin <szakharin at nvidia.com>
---
 flang/lib/Lower/OpenACC.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 4e62feff4430d..10b7d6d5abf1f 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -4434,10 +4434,9 @@ void Fortran::lower::attachDeclarePostAllocAction(
   mlir::Operation *op = &builder.getInsertionBlock()->back();
 
   if (op && op->hasTrait<mlir::OpTrait::IsTerminator>()) {
-    if (op->getNumOperands() != 0) {
+    if (op->getNumOperands() != 0)
       fir::emitFatalError(op->getLoc(),
                           "expect only terminator op with no operand");
-    }
     op = op->getPrevNode();
   }
   assert(op && "expect operation to attach the post allocation action");

>From 330cf528ab2263b9ad94e96762ace1c00d4a56ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Susan=20Tan=20=28=E3=82=B9-=E3=82=B6=E3=83=B3=E3=80=80?=
 =?UTF-8?q?=E3=82=BF=E3=83=B3=29?= <zujunt at nvidia.com>
Date: Tue, 24 Mar 2026 12:47:02 -0400
Subject: [PATCH 7/7] Update flang/lib/Lower/OpenACC.cpp

Co-authored-by: Slava Zakharin <szakharin at nvidia.com>
---
 flang/lib/Lower/OpenACC.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 10b7d6d5abf1f..ffadef41bf174 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -4508,10 +4508,9 @@ void Fortran::lower::attachDeclarePostDeallocAction(
   fctName << converter.mangleName(sym) << declarePostDeallocSuffix.str();
   mlir::Operation *op = &builder.getInsertionBlock()->back();
   if (op && op->hasTrait<mlir::OpTrait::IsTerminator>()) {
-    if (op->getNumOperands() != 0) {
+    if (op->getNumOperands() != 0)
       fir::emitFatalError(op->getLoc(),
                           "expect only terminator op with no operand");
-    }
     op = op->getPrevNode();
   }
   assert(op && "expect operation to attach the post deallocation action");



More information about the flang-commits mailing list