[flang-commits] [flang] [Flang] Fix perfect loop nest detection (PR #161554)

Michael Kruse via flang-commits flang-commits at lists.llvm.org
Wed Oct 1 10:31:24 PDT 2025


https://github.com/Meinersbur updated https://github.com/llvm/llvm-project/pull/161554

>From e7b1c2e1225d57904bf32cccb5510c0ecdb2395f Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Wed, 1 Oct 2025 16:59:35 +0200
Subject: [PATCH 1/2] Unwrap block element

---
 flang/lib/Semantics/resolve-directives.cpp    | 13 ++++++++-----
 .../Lower/OpenMP/wsloop-collapse-continue.f90 | 19 +++++++++++++++++++
 flang/test/Semantics/OpenMP/do08.f90          |  1 -
 flang/test/Semantics/OpenMP/do13.f90          |  1 -
 4 files changed, 27 insertions(+), 7 deletions(-)
 create mode 100644 flang/test/Lower/OpenMP/wsloop-collapse-continue.f90

diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index bd7b8ac552fab..d32fa3eedae21 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -2303,14 +2303,17 @@ void OmpAttributeVisitor::CheckPerfectNestAndRectangularLoop(
     }
 
     auto checkPerfectNest = [&, this]() {
-      auto blockSize = block.size();
-      if (blockSize <= 1)
+      if (block.empty())
         return;
+      auto last = block.end();
+      --last;
 
-      if (parser::Unwrap<parser::ContinueStmt>(x))
-        blockSize -= 1;
+      // A trailing CONTINUE or CYCLE does not belong to the loop body
+      if (parser::Unwrap<parser::ContinueStmt>(*last))
+        --last;
 
-      if (blockSize <= 1)
+      // In a perfectly nested loop, the nested loop must be the only statement
+      if (last == block.begin())
         return;
 
       // Non-perfectly nested loop
diff --git a/flang/test/Lower/OpenMP/wsloop-collapse-continue.f90 b/flang/test/Lower/OpenMP/wsloop-collapse-continue.f90
new file mode 100644
index 0000000000000..fea7a8b335d63
--- /dev/null
+++ b/flang/test/Lower/OpenMP/wsloop-collapse-continue.f90
@@ -0,0 +1,19 @@
+! RUN: bbc -fopenmp -emit-hlfir %s -o - | FileCheck %s
+
+program wsloop_collapse_continue
+  integer i, j
+
+! CHECK: omp.wsloop {{.*}} {
+! CHECK: omp.loop_nest ({{.*}}) : i32 = ({{.*}}) to ({{.*}}) inclusive step ({{.*}}) collapse(2) {
+  !$omp do collapse(2)
+  do 50 i = 1, 42
+     do 51 j = 1, 84
+! CHECK: fir.call @_FortranAioOutputInteger32(
+        print *, i
+! CHECK: fir.call @_FortranAioOutputInteger32(
+        print *, j
+     51 continue
+  50 continue
+  !$omp end do
+
+end program wsloop_collapse_continue
diff --git a/flang/test/Semantics/OpenMP/do08.f90 b/flang/test/Semantics/OpenMP/do08.f90
index bb3c1d0cd3855..5143dff0dd315 100644
--- a/flang/test/Semantics/OpenMP/do08.f90
+++ b/flang/test/Semantics/OpenMP/do08.f90
@@ -61,7 +61,6 @@ program omp
   !$omp end do
 
 
-  !ERROR: Canonical loop nest must be perfectly nested.
   !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
   !$omp do collapse(3)
   do 60 i=2,200,2
diff --git a/flang/test/Semantics/OpenMP/do13.f90 b/flang/test/Semantics/OpenMP/do13.f90
index 8f7844f4136f9..6e9d1dddade4c 100644
--- a/flang/test/Semantics/OpenMP/do13.f90
+++ b/flang/test/Semantics/OpenMP/do13.f90
@@ -59,7 +59,6 @@ program omp
   !$omp end do
 
 
-  !ERROR: Canonical loop nest must be perfectly nested.
   !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
   !$omp do collapse(3)
   do 60 i=1,10

>From e8aa3615e63565cd0a5a1fff0364d88557749226 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Wed, 1 Oct 2025 19:31:02 +0200
Subject: [PATCH 2/2] Fix comment

---
 flang/lib/Semantics/resolve-directives.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index d32fa3eedae21..59513cca83fbe 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -2308,7 +2308,7 @@ void OmpAttributeVisitor::CheckPerfectNestAndRectangularLoop(
       auto last = block.end();
       --last;
 
-      // A trailing CONTINUE or CYCLE does not belong to the loop body
+      // A trailing CONTINUE is not considered part of the loop body
       if (parser::Unwrap<parser::ContinueStmt>(*last))
         --last;
 



More information about the flang-commits mailing list