Skip to content

Conversation

khaki3
Copy link
Contributor

@khaki3 khaki3 commented Sep 15, 2025

The following commit incorrectly prohibited nested gang loops inside acc routines. This PR limits the restriction to loops within kernels constructs only. 8470027

@llvmbot llvmbot added flang Flang issues not falling into any other category openacc flang:semantics labels Sep 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 15, 2025

@llvm/pr-subscribers-flang-semantics

@llvm/pr-subscribers-openacc

Author: None (khaki3)

Changes

The following commit incorrectly prohibited nested gang loops inside acc routines. This PR limits the restriction to loops within kernels constructs only. 8470027


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

3 Files Affected:

  • (modified) flang/lib/Semantics/check-acc-structure.cpp (+4-4)
  • (modified) flang/lib/Semantics/check-acc-structure.h (+1-1)
  • (modified) flang/test/Semantics/OpenACC/acc-loop.f90 (+31)
diff --git a/flang/lib/Semantics/check-acc-structure.cpp b/flang/lib/Semantics/check-acc-structure.cpp
index 6cb7e5e9e6e25..bcb485d4e0f47 100644
--- a/flang/lib/Semantics/check-acc-structure.cpp
+++ b/flang/lib/Semantics/check-acc-structure.cpp
@@ -136,10 +136,10 @@ void AccStructureChecker::CheckNotInComputeConstruct() {
   }
 }
 
-bool AccStructureChecker::IsInsideParallelConstruct() const {
+bool AccStructureChecker::IsInsideKernelsConstruct() const {
   if (auto directive = getParentComputeConstruct())
-    if (*directive == llvm::acc::ACCD_parallel ||
-        *directive == llvm::acc::ACCD_parallel_loop)
+    if (*directive == llvm::acc::ACCD_kernels ||
+        *directive == llvm::acc::ACCD_kernels_loop)
       return true;
   return false;
 }
@@ -293,7 +293,7 @@ void AccStructureChecker::CheckNotInSameOrSubLevelLoopConstruct() {
           bool invalid{false};
           if (parentClause == llvm::acc::Clause::ACCC_gang &&
               cl == llvm::acc::Clause::ACCC_gang) {
-            if (IsInsideParallelConstruct()) {
+            if (!IsInsideKernelsConstruct()) {
               auto parentDim = getGangDimensionSize(parent);
               auto currentDim = getGangDimensionSize(GetContext());
               std::int64_t parentDimNum = 1, currentDimNum = 1;
diff --git a/flang/lib/Semantics/check-acc-structure.h b/flang/lib/Semantics/check-acc-structure.h
index 711d0326349a4..09399297ca4be 100644
--- a/flang/lib/Semantics/check-acc-structure.h
+++ b/flang/lib/Semantics/check-acc-structure.h
@@ -101,7 +101,7 @@ class AccStructureChecker
   bool IsLoopConstruct(llvm::acc::Directive directive) const;
   std::optional<llvm::acc::Directive> getParentComputeConstruct() const;
   bool IsInsideComputeConstruct() const;
-  bool IsInsideParallelConstruct() const;
+  bool IsInsideKernelsConstruct() const;
   void CheckNotInComputeConstruct();
   std::optional<std::int64_t> getGangDimensionSize(
       DirectiveContext &dirContext);
diff --git a/flang/test/Semantics/OpenACC/acc-loop.f90 b/flang/test/Semantics/OpenACC/acc-loop.f90
index 77c427e0a85ae..d3777f48430e5 100644
--- a/flang/test/Semantics/OpenACC/acc-loop.f90
+++ b/flang/test/Semantics/OpenACC/acc-loop.f90
@@ -447,4 +447,35 @@ program openacc_loop_validity
     END DO
   END DO
 
+contains
+
+  subroutine sub1()
+    !$acc routine gang(dim:2)
+    implicit none
+    integer, parameter :: N = 256
+    integer :: i, j
+
+    !$acc loop gang(dim:2)
+    DO j = 1, N
+       !$acc loop gang(dim:1) vector
+       DO i = 1, N
+       END DO
+    END DO
+  end subroutine sub1
+
+  subroutine sub2()
+    !$acc routine gang(dim:2)
+    implicit none
+    integer, parameter :: N = 256
+    integer :: i, j
+
+    !$acc loop gang(dim:2)
+    DO j = 1, N
+       !ERROR: GANG(dim:2) clause is not allowed in the region of a loop with the GANG(dim:2) clause
+       !$acc loop gang(dim:2) vector
+       DO i = 1, N
+       END DO
+    END DO
+  end subroutine sub2
+
 end program openacc_loop_validity

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

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

LGTM , nit/suggestion inline.

@khaki3 khaki3 enabled auto-merge (squash) September 16, 2025 11:57
@khaki3 khaki3 merged commit c366cbd into llvm:main Sep 16, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics 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