[flang-commits] [flang] a5864f9 - [Flang][OpenMP] Emit TODO for non-rectangular collapsed loop nests (#205558)
via flang-commits
flang-commits at lists.llvm.org
Wed Jul 1 09:50:00 PDT 2026
Author: CHANDRA GHALE
Date: 2026-07-01T22:19:55+05:30
New Revision: a5864f9604e7d218c09d44d13c0a17a28f8456a5
URL: https://github.com/llvm/llvm-project/commit/a5864f9604e7d218c09d44d13c0a17a28f8456a5
DIFF: https://github.com/llvm/llvm-project/commit/a5864f9604e7d218c09d44d13c0a17a28f8456a5.diff
LOG: [Flang][OpenMP] Emit TODO for non-rectangular collapsed loop nests (#205558)
Non-rectangular loop nests (where an inner loop bound depends on an
outer IV) with collapse currently generate incorrect code that segfaults
at runtime, since OpenMPIRBuilder's collapseLoops assumes rectangular
iteration spaces.
Added a check during lowering to detect the non-rectangular case and
emit a clear "not yet implemented" error instead of silently producing
broken code.
minimal reproducer :
```
program repro
implicit none
integer, parameter :: N = 10
integer :: arr(N,N), i, j
arr = 0
!$omp parallel do collapse(2)
do i = 1, N
do j = 1, i ! non-rectangular: upper bound depends on i
arr(j,i) = 1
end do
end do
if (sum(arr) /= N*(N+1)/2) then
print , "FAIL: sum =", sum(arr), " expected", N(N+1)/2
stop 1
end if
print *, "PASS"
end program
```
```
flang -O1 -fopenmp -fopenmp-version=50 repro.f90 && ./a.out
Segmentation fault (signal 11)
```
---------
Co-authored-by: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>
Added:
flang/test/Lower/OpenMP/non-rectangular-collapse.f90
flang/test/Lower/OpenMP/non-rectangular-collapse2.f90
flang/test/Lower/OpenMP/non-rectangular-collapse3.f90
Modified:
flang/lib/Lower/OpenMP/Utils.cpp
Removed:
################################################################################
diff --git a/flang/lib/Lower/OpenMP/Utils.cpp b/flang/lib/Lower/OpenMP/Utils.cpp
index 382292b6c6c13..660a7f53f0096 100644
--- a/flang/lib/Lower/OpenMP/Utils.cpp
+++ b/flang/lib/Lower/OpenMP/Utils.cpp
@@ -800,6 +800,38 @@ void collectLoopRelatedInfo(
const parser::LoopControl::Bounds *bounds =
std::get_if<parser::LoopControl::Bounds>(&loopControl->u);
assert(bounds && "Expected bounds for worksharing do loop");
+ // Check for non-rectangular loops: if any bound of this loop references
+ // an iteration variable from an enclosing collapsed loop, this is a
+ // non-rectangular loop nest which is not yet supported by the
+ // OpenMPIRBuilder's collapseLoops.
+ if (!iv.empty()) {
+ auto referencesOuterIV = [&iv](const semantics::SomeExpr &expr) -> bool {
+ for (const semantics::SymbolRef &sym :
+ evaluate::GetSymbolVector(expr)) {
+ for (const semantics::Symbol *outerIV : iv) {
+ if (&sym->GetUltimate() == &outerIV->GetUltimate())
+ return true;
+ }
+ }
+ return false;
+ };
+ if (const auto *lowerExpr = semantics::GetExpr(bounds->Lower()))
+ if (referencesOuterIV(*lowerExpr))
+ TODO(currentLocation,
+ "Non-rectangular loop nests with COLLAPSE are not yet "
+ "supported");
+ if (const auto *upperExpr = semantics::GetExpr(bounds->Upper()))
+ if (referencesOuterIV(*upperExpr))
+ TODO(currentLocation,
+ "Non-rectangular loop nests with COLLAPSE are not yet "
+ "supported");
+ if (auto &step = bounds->Step())
+ if (const auto *stepExpr = semantics::GetExpr(step))
+ if (referencesOuterIV(*stepExpr))
+ TODO(currentLocation,
+ "Non-rectangular loop nests with COLLAPSE are not yet "
+ "supported");
+ }
lower::StatementContext stmtCtx;
result.loopLowerBounds.push_back(fir::getBase(
converter.genExprValue(*semantics::GetExpr(bounds->Lower()), stmtCtx)));
diff --git a/flang/test/Lower/OpenMP/non-rectangular-collapse.f90 b/flang/test/Lower/OpenMP/non-rectangular-collapse.f90
new file mode 100644
index 0000000000000..46780add84607
--- /dev/null
+++ b/flang/test/Lower/OpenMP/non-rectangular-collapse.f90
@@ -0,0 +1,23 @@
+! Test that non-rectangular collapsed loop nests produce a clear TODO error
+! rather than generating incorrect code that crashes at runtime.
+
+! RUN: not %flang_fc1 -emit-fir -fopenmp -fopenmp-version=50 %s -o - 2>&1 | FileCheck %s
+
+! CHECK: not yet implemented: Non-rectangular loop nests with COLLAPSE are not yet supported
+
+! Non-rectangular: upper bound of inner loop depends on outer IV
+subroutine non_rect_ub(N)
+ implicit none
+ integer, intent(in) :: N
+ integer :: arr(N,N)
+ integer :: i, j
+
+ !$omp parallel do collapse(2)
+ do i = 1, N
+ do j = 1, i
+ arr(j,i) = 1
+ end do
+ end do
+end subroutine
+
+
diff --git a/flang/test/Lower/OpenMP/non-rectangular-collapse2.f90 b/flang/test/Lower/OpenMP/non-rectangular-collapse2.f90
new file mode 100644
index 0000000000000..9514638a41a00
--- /dev/null
+++ b/flang/test/Lower/OpenMP/non-rectangular-collapse2.f90
@@ -0,0 +1,22 @@
+! Test that non-rectangular collapsed loop nests produce a clear TODO error
+! rather than generating incorrect code that crashes at runtime.
+
+! RUN: not %flang_fc1 -emit-fir -fopenmp -fopenmp-version=50 %s -o - 2>&1 | FileCheck %s
+
+! CHECK: not yet implemented: Non-rectangular loop nests with COLLAPSE are not yet supported
+
+! Non-rectangular: lower bound of inner loop depends on outer IV
+subroutine non_rect_lb(N)
+ implicit none
+ integer, intent(in) :: N
+ integer :: arr(N,N)
+ integer :: i, j
+
+ !$omp parallel do collapse(2)
+ do i = 1, N
+ do j = i, N
+ arr(j,i) = 1
+ end do
+ end do
+end subroutine
+
diff --git a/flang/test/Lower/OpenMP/non-rectangular-collapse3.f90 b/flang/test/Lower/OpenMP/non-rectangular-collapse3.f90
new file mode 100644
index 0000000000000..d73752d5538ec
--- /dev/null
+++ b/flang/test/Lower/OpenMP/non-rectangular-collapse3.f90
@@ -0,0 +1,22 @@
+! Test that non-rectangular collapsed loop nests produce a clear TODO error
+! rather than generating incorrect code that crashes at runtime.
+
+! RUN: not %flang_fc1 -emit-fir -fopenmp -fopenmp-version=50 %s -o - 2>&1 | FileCheck %s
+
+! CHECK: not yet implemented: Non-rectangular loop nests with COLLAPSE are not yet supported
+
+! Non-rectangular: step of inner loop depends on outer IV
+subroutine non_rect_step(N)
+ implicit none
+ integer, intent(in) :: N
+ integer :: arr(N,N)
+ integer :: i, j
+
+ !$omp parallel do collapse(2)
+ do i = 1, N
+ do j = 1, N, i
+ arr(j,i) = 1
+ end do
+ end do
+end subroutine
+
More information about the flang-commits
mailing list