Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][openacc] Enforce no branching out of compute region for combined construct #73581

Merged
merged 1 commit into from
Nov 28, 2023

Conversation

clementval
Copy link
Contributor

A program may not branch into or out of a compute construct. This restriction is also true for combined constructs. This patch enforce this rule.

@llvmbot llvmbot added flang Flang issues not falling into any other category openacc flang:semantics flang:parser labels Nov 27, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 27, 2023

@llvm/pr-subscribers-openacc
@llvm/pr-subscribers-flang-parser

@llvm/pr-subscribers-flang-semantics

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

Changes

A program may not branch into or out of a compute construct. This restriction is also true for combined constructs. This patch enforce this rule.


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

3 Files Affected:

  • (modified) flang/lib/Semantics/check-acc-structure.cpp (+5)
  • (modified) flang/test/Parser/acc-unparse.f90 (+1-1)
  • (modified) flang/test/Semantics/OpenACC/acc-branch.f90 (+21)
diff --git a/flang/lib/Semantics/check-acc-structure.cpp b/flang/lib/Semantics/check-acc-structure.cpp
index 64b07da2324d970..abda9409efd336b 100644
--- a/flang/lib/Semantics/check-acc-structure.cpp
+++ b/flang/lib/Semantics/check-acc-structure.cpp
@@ -218,6 +218,7 @@ void AccStructureChecker::Leave(const parser::OpenACCCombinedConstruct &x) {
   const auto &beginBlockDir{std::get<parser::AccBeginCombinedDirective>(x.t)};
   const auto &combinedDir{
       std::get<parser::AccCombinedDirective>(beginBlockDir.t)};
+  auto &doCons{std::get<std::optional<parser::DoConstruct>>(x.t)};
   switch (combinedDir.v) {
   case llvm::acc::Directive::ACCD_kernels_loop:
   case llvm::acc::Directive::ACCD_parallel_loop:
@@ -225,6 +226,10 @@ void AccStructureChecker::Leave(const parser::OpenACCCombinedConstruct &x) {
     // Restriction - line 1004-1005
     CheckOnlyAllowedAfter(llvm::acc::Clause::ACCC_device_type,
         computeConstructOnlyAllowedAfterDeviceTypeClauses);
+    if (doCons) {
+      const parser::Block &block{std::get<parser::Block>(doCons->t)};
+      CheckNoBranching(block, GetContext().directive, beginBlockDir.source);
+    }
     break;
   default:
     break;
diff --git a/flang/test/Parser/acc-unparse.f90 b/flang/test/Parser/acc-unparse.f90
index e0a6c164f0de12c..62e0d4487f3f745 100644
--- a/flang/test/Parser/acc-unparse.f90
+++ b/flang/test/Parser/acc-unparse.f90
@@ -9,7 +9,7 @@ program bug47659
     !$acc parallel loop
     do j = 1, 10
       if (j == 2) then
-        exit label1
+        stop 1
       end if
     end do
   end do label1
diff --git a/flang/test/Semantics/OpenACC/acc-branch.f90 b/flang/test/Semantics/OpenACC/acc-branch.f90
index 918f2b021c432f1..b3692d016589067 100644
--- a/flang/test/Semantics/OpenACC/acc-branch.f90
+++ b/flang/test/Semantics/OpenACC/acc-branch.f90
@@ -19,6 +19,27 @@ subroutine openacc_clause_validity
   end do
   !$acc end parallel
 
+  !$acc parallel loop
+  do i = 1, N
+    a(i) = 3.14
+    !ERROR: RETURN statement is not allowed in a PARALLEL LOOP construct
+    return
+  end do
+
+  !$acc serial loop
+  do i = 1, N
+    a(i) = 3.14
+    !ERROR: RETURN statement is not allowed in a SERIAL LOOP construct
+    return
+  end do
+
+  !$acc kernels loop
+  do i = 1, N
+    a(i) = 3.14
+    !ERROR: RETURN statement is not allowed in a KERNELS LOOP construct
+    return
+  end do
+
   !$acc parallel
   !$acc loop
   do i = 1, N

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.

Looks great! Thank you!

@clementval clementval merged commit f4c5c47 into llvm:main Nov 28, 2023
7 checks passed
@clementval clementval deleted the acc_no_branching branch November 28, 2023 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:parser 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.

None yet

3 participants