Skip to content

[flang][OpenMP] Don't allow DO CONCURRENT inside of a loop nest #144506

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tblah
Copy link
Contributor

@tblah tblah commented Jun 17, 2025

I don't think DO CONCURRENT fits the definition of a Canonical Loop Nest (OpenMP 6.0 section 6.4.1).
It is however explicitly allowed for the LOOP construct (6.0 section 13.8).

There's some obscure language in OpenMP 6.0 for the LOOP construct:

If the collapsed loop is a DO CONCURRENT loop, neither the
data-sharing attribute clauses nor the collapse clause may be specified.

From the surrounding context, I think "collapsed loop" just means the
loop that the LOOP construct applies to. So I will interpret this to
mean that DO CONCURRENT can only be used with the LOOP construct if it
does not contain the COLLAPSE clause.

This also fixes a bug where the associated clause was never cleared
after it was set.

Fixes #144178

I don't think DO CONCURRENT fits the definition of a Canonical Loop Nest
(OpenMP 6.0 section 6.4.1).

Fixes llvm#144178
Comment on lines 1955 to 1960
if (loop->IsDoConcurrent()) {
auto &stmt =
std::get<parser::Statement<parser::NonLabelDoStmt>>(loop->t);
context_.Say(stmt.source,
"DO CONCURRENT loops cannot form part of a loop nest."_err_en_US);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

DO CONCURRENT is allowed in the LOOP construct, although, IIUC, only as the top-level loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ab3fba8 allows do concurrent as a single loop construct but not as part of a loop nest. Does that match your understanding of the standard?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it's allowed in any worksharing-loop construct, only in the LOOP construct [6.0:424:1] "The collapsed loop may be a DO CONCURRENT loop." No other constructs in that chapter have this annotation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out.

If the collapsed loop is a DO CONCURRENT loop, neither the data-sharing attribute clauses nor the collapse clause may be specified.

I am taking this to mean that DO CONCURRENT is allowed with LOOP, but not when LOOP has a COLLAPSE clause. "Collapsed loop" seems elsewhere to refer to anything enclosed by the LOOP construct.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:openmp flang:semantics labels Jun 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 17, 2025

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

@llvm/pr-subscribers-flang-openmp

Author: Tom Eccles (tblah)

Changes

I don't think DO CONCURRENT fits the definition of a Canonical Loop Nest (OpenMP 6.0 section 6.4.1).

Fixes #144178


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

2 Files Affected:

  • (modified) flang/lib/Semantics/resolve-directives.cpp (+7)
  • (added) flang/test/Semantics/OpenMP/do-concurrent-collapse.f90 (+19)
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index b5f8667fe36f2..05568cc0ba0ae 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -1952,6 +1952,13 @@ void OmpAttributeVisitor::PrivatizeAssociatedLoopIndexAndCheckLoopLevel(
   const auto &outer{std::get<std::optional<parser::DoConstruct>>(x.t)};
   if (outer.has_value()) {
     for (const parser::DoConstruct *loop{&*outer}; loop && level > 0; --level) {
+      // DO CONCURRENT is allowed for loop constructs but not loop nests
+      if (loop->IsDoConcurrent() && GetContext().associatedLoopLevel != 1) {
+        auto &stmt =
+            std::get<parser::Statement<parser::NonLabelDoStmt>>(loop->t);
+        context_.Say(stmt.source,
+            "DO CONCURRENT loops cannot form part of a loop nest."_err_en_US);
+      }
       // go through all the nested do-loops and resolve index variables
       const parser::Name *iv{GetLoopIndex(*loop)};
       if (iv) {
diff --git a/flang/test/Semantics/OpenMP/do-concurrent-collapse.f90 b/flang/test/Semantics/OpenMP/do-concurrent-collapse.f90
new file mode 100644
index 0000000000000..71e1928e1f245
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/do-concurrent-collapse.f90
@@ -0,0 +1,19 @@
+!RUN: %python %S/../test_errors.py %s %flang -fopenmp
+
+integer :: i, j
+!$omp parallel do collapse(2)
+do i = 1, 1
+  ! ERROR: DO CONCURRENT loops cannot form part of a loop nest.
+  do concurrent (j = 1:2)
+    print *, j
+  end do
+end do
+
+!$omp parallel do
+do i = 1, 1
+  ! This should not lead to an error because it is not part of a loop nest:
+  do concurrent (j = 1:2)
+    print *, j
+  end do
+end do
+end

tblah added 2 commits June 17, 2025 16:32
There's some obscure language in OpenMP 6.0:

> If the collapsed loop is a DO CONCURRENT loop, neither the
> data-sharing attribute clauses nor the collapse clause may be specified.

From the surrounding context, I think "collapsed loop" just means the
loop that the LOOP construct applies to. So I will interpret this to
mean that DO CONCURRENT can only be used with the LOOP construct if it
does not contain the COLLAPSE clause.

This also fixes a bug where the associated clause was never cleared
after it was set.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:openmp flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flang][OpenMP] crashes with OpenMP 'parallel do collapse' and 'do concurrent' nested loops
3 participants