Skip to content

Commit f1bb4fb

Browse files
Meinersburmahesh-attarde
authored andcommitted
[Flang] Fix perfect loop nest detection (llvm#161554)
PR llvm#160283 uses `Unwrap` to detect a `continue` statement, but it applied it on the loop body itelf which sometimes finds a trailing continue statement, but not always. Apply `Unwrap` on the last body statement instead, where the `continue` is expected. Fixes llvm#161529
1 parent b1703e8 commit f1bb4fb

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,14 +2285,17 @@ void OmpAttributeVisitor::CheckPerfectNestAndRectangularLoop(
22852285
}
22862286

22872287
auto checkPerfectNest = [&, this]() {
2288-
auto blockSize = block.size();
2289-
if (blockSize <= 1)
2288+
if (block.empty())
22902289
return;
2290+
auto last = block.end();
2291+
--last;
22912292

2292-
if (parser::Unwrap<parser::ContinueStmt>(x))
2293-
blockSize -= 1;
2293+
// A trailing CONTINUE is not considered part of the loop body
2294+
if (parser::Unwrap<parser::ContinueStmt>(*last))
2295+
--last;
22942296

2295-
if (blockSize <= 1)
2297+
// In a perfectly nested loop, the nested loop must be the only statement
2298+
if (last == block.begin())
22962299
return;
22972300

22982301
// Non-perfectly nested loop
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! RUN: bbc -fopenmp -emit-hlfir %s -o - | FileCheck %s
2+
3+
program wsloop_collapse_continue
4+
integer i, j
5+
6+
! CHECK: omp.wsloop {{.*}} {
7+
! CHECK: omp.loop_nest ({{.*}}) : i32 = ({{.*}}) to ({{.*}}) inclusive step ({{.*}}) collapse(2) {
8+
!$omp do collapse(2)
9+
do 50 i = 1, 42
10+
do 51 j = 1, 84
11+
! CHECK: fir.call @_FortranAioOutputInteger32(
12+
print *, i
13+
! CHECK: fir.call @_FortranAioOutputInteger32(
14+
print *, j
15+
51 continue
16+
50 continue
17+
!$omp end do
18+
19+
end program wsloop_collapse_continue

flang/test/Semantics/OpenMP/do08.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ program omp
6161
!$omp end do
6262

6363

64-
!ERROR: Canonical loop nest must be perfectly nested.
6564
!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.
6665
!$omp do collapse(3)
6766
do 60 i=2,200,2

flang/test/Semantics/OpenMP/do13.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ program omp
5959
!$omp end do
6060

6161

62-
!ERROR: Canonical loop nest must be perfectly nested.
6362
!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.
6463
!$omp do collapse(3)
6564
do 60 i=1,10

0 commit comments

Comments
 (0)