[flang-commits] [flang] [Flang][OpenMP] Emit TODO for non-rectangular collapsed loop nests (PR #205558)
CHANDRA GHALE via flang-commits
flang-commits at lists.llvm.org
Wed Jul 1 02:51:04 PDT 2026
https://github.com/chandraghale updated https://github.com/llvm/llvm-project/pull/205558
>From e4c862bc3c0643571f1621e866d423011e16177f Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>
Date: Wed, 24 Jun 2026 08:28:11 -0500
Subject: [PATCH 1/2] emit todo for non-rectangular collapsed loop nests
---
flang/lib/Lower/OpenMP/Utils.cpp | 32 ++++++++++++
.../Lower/OpenMP/non-rectangular-collapse.f90 | 52 +++++++++++++++++++
2 files changed, 84 insertions(+)
create mode 100644 flang/test/Lower/OpenMP/non-rectangular-collapse.f90
diff --git a/flang/lib/Lower/OpenMP/Utils.cpp b/flang/lib/Lower/OpenMP/Utils.cpp
index 7a532e10f1a1e..1e6f32088a379 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..17fbbd1e49084
--- /dev/null
+++ b/flang/test/Lower/OpenMP/non-rectangular-collapse.f90
@@ -0,0 +1,52 @@
+! 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
+
+! 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
+
+! 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
+
+! 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
+
>From 942f3c0581bcb212276f9c47948f9dbd89405503 Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>
Date: Wed, 1 Jul 2026 04:48:53 -0500
Subject: [PATCH 2/2] lit test splited seperately
---
.../Lower/OpenMP/non-rectangular-collapse.f90 | 33 ++-----------------
.../OpenMP/non-rectangular-collapse2.f90 | 22 +++++++++++++
.../OpenMP/non-rectangular-collapse3.f90 | 22 +++++++++++++
3 files changed, 46 insertions(+), 31 deletions(-)
create mode 100644 flang/test/Lower/OpenMP/non-rectangular-collapse2.f90
create mode 100644 flang/test/Lower/OpenMP/non-rectangular-collapse3.f90
diff --git a/flang/test/Lower/OpenMP/non-rectangular-collapse.f90 b/flang/test/Lower/OpenMP/non-rectangular-collapse.f90
index 17fbbd1e49084..46780add84607 100644
--- a/flang/test/Lower/OpenMP/non-rectangular-collapse.f90
+++ b/flang/test/Lower/OpenMP/non-rectangular-collapse.f90
@@ -3,6 +3,8 @@
! 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
@@ -18,35 +20,4 @@ subroutine non_rect_ub(N)
end do
end subroutine
-! 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
-
-! 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
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