Skip to content

Conversation

clementval
Copy link
Contributor

@clementval clementval commented Aug 26, 2025

When the end of a block is inside a data region (not a compute region), generating an acc.terminator will lead to a missing terminator when translating to LLVM.

Only generate acc.terminator instead of fir.unreachable when nested in acc compute region.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir openacc labels Aug 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 26, 2025

@llvm/pr-subscribers-openacc

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

Author: Valentin Clement (バレンタイン クレメン) (clementval)

Changes

When the end of a block is inside a data region (not a compute region), generating an acc.terminator will lead to a mission terminator when translating to LLVM.

Only generate acc.terminator instead of fir.unreachable when nested in acc compute region.


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

2 Files Affected:

  • (modified) flang/lib/Lower/Runtime.cpp (+1-2)
  • (added) flang/test/Lower/OpenACC/acc-terminator.f90 (+53)
diff --git a/flang/lib/Lower/Runtime.cpp b/flang/lib/Lower/Runtime.cpp
index fc59a2414d539..494dd49e961b0 100644
--- a/flang/lib/Lower/Runtime.cpp
+++ b/flang/lib/Lower/Runtime.cpp
@@ -39,8 +39,7 @@ static void genUnreachable(fir::FirOpBuilder &builder, mlir::Location loc) {
   if (parentOp->getDialect()->getNamespace() ==
       mlir::omp::OpenMPDialect::getDialectNamespace())
     Fortran::lower::genOpenMPTerminator(builder, parentOp, loc);
-  else if (parentOp->getDialect()->getNamespace() ==
-           mlir::acc::OpenACCDialect::getDialectNamespace())
+  else if (Fortran::lower::isInsideOpenACCComputeConstruct(builder))
     Fortran::lower::genOpenACCTerminator(builder, parentOp, loc);
   else
     fir::UnreachableOp::create(builder, loc);
diff --git a/flang/test/Lower/OpenACC/acc-terminator.f90 b/flang/test/Lower/OpenACC/acc-terminator.f90
new file mode 100644
index 0000000000000..53ae1a5e54675
--- /dev/null
+++ b/flang/test/Lower/OpenACC/acc-terminator.f90
@@ -0,0 +1,53 @@
+! Check that acc.terminator is not inserted in data construct
+
+! RUN: bbc -fopenacc -emit-hlfir %s -o - | FileCheck %s
+
+program main
+  use, intrinsic :: iso_c_binding
+  implicit none
+
+  real(8), pointer :: a(:,:,:),b(:,:,:),c(:,:,:),c2(:,:,:)
+  integer, parameter :: n1 = 400, n2 = 20
+  integer*4 :: stat
+  integer :: i,j,k
+
+  stat = 0
+  do i=1,n2
+
+    !$acc data copyin(a(:,:,i),b(:,:,i),c(:,:,i)) copyout(c2(:,:,i))
+
+    !$acc host_data use_device(a(:,:,i),b(:,:,i),c(:,:,i))
+    
+    !$acc end host_data
+
+    if ( stat .ne. 0 ) then
+      print *, "stat = ",stat
+      stop ! terminator here should be fir.unreachable
+    end if
+
+    !$acc parallel loop present(c(:,:,i),c2(:,:,i))
+    do j = 1,n1
+       do k = 1,n1
+          c2(k,j,i) = 1.5d0 * c(k,j,i)
+       enddo
+    enddo
+    !$acc end parallel loop
+
+    !$acc end data
+
+  enddo
+
+  !$acc wait
+
+  deallocate(a,b,c,c2)
+end program
+
+! CHECK-LABEL: func.func @_QQmain()
+! CHECK: acc.data
+! CHECK: acc.host_data
+! CHECK: acc.terminator
+! CHECK: fir.call @_FortranAStopStatement
+! CHECK: fir.unreachable
+! CHECK: acc.parallel
+! CHECK-COUNT-3: acc.yield
+! CHECK: acc.terminator

Copy link
Contributor

@razvanlupusoru razvanlupusoru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Nice catch.

@clementval clementval enabled auto-merge (squash) August 26, 2025 21:49
@clementval clementval merged commit f80c05c into llvm:main Aug 26, 2025
13 checks passed
@clementval clementval deleted the acc_terminator branch August 27, 2025 06:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category openacc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants