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] Support labeled DO loop after acc combined directive #66296

Merged
merged 1 commit into from
Sep 14, 2023

Conversation

clementval
Copy link
Contributor

This patch adds support for labeled do loop after combined directive. It relaxes the initial parser and canonicalize labeled do loop into the OpenACCCombinedConstruct.

@clementval clementval requested review from a team as code owners September 13, 2023 21:55
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser flang:fir-hlfir openacc labels Sep 13, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 13, 2023

@llvm/pr-subscribers-flang-parser

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

Changes This patch adds support for labeled do loop after combined directive. It relaxes the initial parser and canonicalize labeled do loop into the OpenACCCombinedConstruct. -- Full diff: https://github.com//pull/66296.diff

4 Files Affected:

  • (modified) flang/lib/Parser/openacc-parsers.cpp (+1-2)
  • (modified) flang/lib/Semantics/canonicalize-acc.cpp (+15-5)
  • (modified) flang/test/Lower/OpenACC/acc-parallel-loop.f90 (+7)
  • (modified) flang/test/Semantics/OpenACC/acc-combined-loop.f90 (+15-3)
diff --git a/flang/lib/Parser/openacc-parsers.cpp b/flang/lib/Parser/openacc-parsers.cpp
index afa12b88019bd9a..6f4341c29ab0346 100644
--- a/flang/lib/Parser/openacc-parsers.cpp
+++ b/flang/lib/Parser/openacc-parsers.cpp
@@ -257,8 +257,7 @@ TYPE_PARSER(startAccLine >>
 
 TYPE_PARSER(construct<OpenACCCombinedConstruct>(
     sourced(Parser<AccBeginCombinedDirective>{} / endAccLine),
-    withMessage("A DO loop must follow the combined construct"_err_en_US,
-        Parser<DoConstruct>{}),
+    maybe(Parser<DoConstruct>{}),
     maybe(Parser<AccEndCombinedDirective>{} / endAccLine)))
 
 } // namespace Fortran::parser
diff --git a/flang/lib/Semantics/canonicalize-acc.cpp b/flang/lib/Semantics/canonicalize-acc.cpp
index e79ab997637b083..595efd0be030a24 100644
--- a/flang/lib/Semantics/canonicalize-acc.cpp
+++ b/flang/lib/Semantics/canonicalize-acc.cpp
@@ -132,14 +132,24 @@ class CanonicalizationOfAcc {
     parser::Block::iterator nextIt;
     auto &beginDir{std::get<parser::AccBeginCombinedDirective>(x.t)};
     auto &dir{std::get<parser::AccCombinedDirective>(beginDir.t)};
-    const auto &doConstruct{std::get<std::optional<parser::DoConstruct>>(x.t)};
+    auto &nestedDo{std::get<std::optional<parser::DoConstruct>>(x.t)};
+
+    if (!nestedDo) {
+      nextIt = it;
+      if (++nextIt != block.end()) {
+        if (auto *doCons{parser::Unwrap<parser::DoConstruct>(*nextIt)}) {
+          nestedDo = std::move(*doCons);
+          nextIt = block.erase(nextIt);
+        }
+      }
+    }
 
-    if (doConstruct) {
+    if (nestedDo) {
       CheckDoConcurrentClauseRestriction<parser::OpenACCCombinedConstruct,
-          parser::AccBeginCombinedDirective>(x, *doConstruct);
+          parser::AccBeginCombinedDirective>(x, *nestedDo);
       CheckTileClauseRestriction<parser::OpenACCCombinedConstruct,
-          parser::AccBeginCombinedDirective>(x, *doConstruct);
-      if (!doConstruct->GetLoopControl()) {
+          parser::AccBeginCombinedDirective>(x, *nestedDo);
+      if (!nestedDo->GetLoopControl()) {
         messages_.Say(dir.source,
             "DO loop after the %s directive must have loop control"_err_en_US,
             parser::ToUpperCaseLetters(dir.source.ToString()));
diff --git a/flang/test/Lower/OpenACC/acc-parallel-loop.f90 b/flang/test/Lower/OpenACC/acc-parallel-loop.f90
index 4490a460afe969e..b9113437f86aa07 100644
--- a/flang/test/Lower/OpenACC/acc-parallel-loop.f90
+++ b/flang/test/Lower/OpenACC/acc-parallel-loop.f90
@@ -803,4 +803,11 @@ subroutine acc_parallel_loop
 ! CHECK:        acc.yield
 ! CHECK-NEXT: }{{$}}
 
+  !$acc parallel loop
+  do 10 i=0, n
+  10 continue
+! CHECK: acc.parallel
+! CHECK: acc.loop
+! CHECK: fir.do_loop
+
 end subroutine acc_parallel_loop
diff --git a/flang/test/Semantics/OpenACC/acc-combined-loop.f90 b/flang/test/Semantics/OpenACC/acc-combined-loop.f90
index 04ff6a308d55f4c..77ff8e1cc603c80 100644
--- a/flang/test/Semantics/OpenACC/acc-combined-loop.f90
+++ b/flang/test/Semantics/OpenACC/acc-combined-loop.f90
@@ -6,16 +6,28 @@ program openacc_combined_loop
 
   i = 1
 
+  !ERROR: A DO loop must follow the PARALLEL LOOP directive
   !$acc parallel loop
-  !ERROR: A DO loop must follow the combined construct
   i = 1
 
+  !ERROR: A DO loop must follow the KERNELS LOOP directive
   !$acc kernels loop
-  !ERROR: A DO loop must follow the combined construct
   i = 1
 
+  !ERROR: A DO loop must follow the SERIAL LOOP directive
   !$acc serial loop
-  !ERROR: A DO loop must follow the combined construct
   i = 1
 
+  !$acc parallel loop
+  do 10 i=0, n
+  10 continue
+
+  !$acc kernels loop
+  do 20 i=0, n
+  20 continue
+
+  !$acc serial loop
+  do 30 i=0, n
+  30 continue
+
 end

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

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

LGTM

@clementval clementval merged commit 16cf9c9 into llvm:main Sep 14, 2023
7 checks passed
@clementval clementval deleted the acc_labeled_loop_combined branch September 14, 2023 03:50
kstoimenov pushed a commit to kstoimenov/llvm-project that referenced this pull request Sep 14, 2023
…llvm#66296)

This patch adds support for labeled do loop after combined directive. It
relaxes the initial parser and canonicalize labeled do loop into the
OpenACCCombinedConstruct.
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
…llvm#66296)

This patch adds support for labeled do loop after combined directive. It
relaxes the initial parser and canonicalize labeled do loop into the
OpenACCCombinedConstruct.
Guzhu-AMD pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Sep 21, 2023
Local branch amd-gfx 251b3d8 Merged main:19b664d35266 into amd-gfx:f205aef70eb5
Remote branch main 16cf9c9 [flang][openacc] Support labeled DO loop after acc combined directive (llvm#66296)
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