[flang-commits] [flang] [flang][openacc] Support labeled DO loop after acc combined directive (PR #66296)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Wed Sep 13 14:55:17 PDT 2023


https://github.com/clementval created https://github.com/llvm/llvm-project/pull/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.

>From 8ea754ed721f4c58863e6a423945bd671752928c Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Wed, 13 Sep 2023 14:53:20 -0700
Subject: [PATCH] [flang][openacc] Support labeled DO loop after acc combined
 directive

---
 flang/lib/Parser/openacc-parsers.cpp          |  3 +--
 flang/lib/Semantics/canonicalize-acc.cpp      | 20 ++++++++++++++-----
 .../test/Lower/OpenACC/acc-parallel-loop.f90  |  7 +++++++
 .../Semantics/OpenACC/acc-combined-loop.f90   | 18 ++++++++++++++---
 4 files changed, 38 insertions(+), 10 deletions(-)

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



More information about the flang-commits mailing list