[flang-commits] [flang] [flang][openacc] Enforce no branching out of compute region for combined construct (PR #73581)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Mon Nov 27 14:43:00 PST 2023


https://github.com/clementval created https://github.com/llvm/llvm-project/pull/73581

`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. 

>From 6b89e4f381dfbf6b15c55e3327b6d84f2cf81bb9 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Mon, 27 Nov 2023 14:40:33 -0800
Subject: [PATCH] [flang][openacc] Enforce no branching out of compute region
 for combined construct

---
 flang/lib/Semantics/check-acc-structure.cpp |  5 +++++
 flang/test/Parser/acc-unparse.f90           |  2 +-
 flang/test/Semantics/OpenACC/acc-branch.f90 | 21 +++++++++++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)

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



More information about the flang-commits mailing list