[clang] [openmp] [Clang][OpenMP] Guard Inner flatten inits on enclosing loop trip counts (PR #237907)

Amit Tiwari via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 23:23:04 PST 2026


https://github.com/loopacino updated https://github.com/llvm/llvm-project/pull/237907

>From 1490cd15e57a66b9dc680cb75151db66e4663557 Mon Sep 17 00:00:00 2001
From: amtiwari <amtiwari at amd.com>
Date: Mon, 2 Nov 2026 05:42:33 -0500
Subject: [PATCH 1/2] gate_inner_inits

---
 clang/lib/Sema/SemaOpenMP.cpp                 | 40 ++++++++++++++++---
 clang/test/OpenMP/flatten_codegen.cpp         | 12 ++++++
 .../test/transform/flatten/final-values.c     | 37 +++++++++++++++++
 3 files changed, 84 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 9acf3b3724527c..0927c9e8c110f5 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -17580,12 +17580,24 @@ SemaOpenMP::ActOnOpenMPFlattenDirective(ArrayRef<OMPClause *> Clauses,
 
   // Collect pre-init statements in outer-to-inner order.
   SmallVector<Stmt *> PreInits;
+  SmallVector<SmallVector<Stmt *, 2>, 4> GatedInits(NumLoops);
   for (auto I : llvm::seq<unsigned>(NumLoops)) {
     OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
     assert(LoopHelper.Counters.size() == 1 &&
            "Single-dimensional loop iteration space expected");
-    addLoopPreInits(Context, LoopHelper, LoopStmts[I], OriginalInits[I],
-                    PreInits);
+    if (I == 0) {
+      addLoopPreInits(Context, LoopHelper, LoopStmts[I], OriginalInits[I],
+                      PreInits);
+      continue;
+    }
+    SmallVector<Stmt *, 2> AlwaysInits;
+    for (Stmt *S : OriginalInits[I]) {
+      if (isa<DeclStmt>(S))
+        AlwaysInits.push_back(S);
+      else
+        GatedInits[I].push_back(S);
+    }
+    addLoopPreInits(Context, LoopHelper, LoopStmts[I], AlwaysInits, PreInits);
   }
 
   CaptureVars CopyTransformer(SemaRef);
@@ -17710,6 +17722,23 @@ SemaOpenMP::ActOnOpenMPFlattenDirective(ArrayRef<OMPClause *> Clauses,
   if (!TripCount.isUsable())
     return StmtError();
 
+  // Inner assignments run only if every enclosing loop has at least one
+  // iteration.
+  for (unsigned I = 1; I < NumLoops; ++I) {
+    if (GatedInits[I].empty())
+      continue;
+    ExprResult InitCond = BuildHasIterations(/*Count=*/I);
+    if (!InitCond.isUsable())
+      return StmtError();
+    Stmt *ThenStmt = GatedInits[I].front();
+    if (GatedInits[I].size() > 1)
+      ThenStmt = CompoundStmt::Create(Context, GatedInits[I],
+                                      FPOptionsOverride(), CondLoc, CondLoc);
+    PreInits.push_back(IfStmt::Create(
+        Context, CondLoc, IfStatementKind::Ordinary, nullptr, nullptr,
+        InitCond.get(), CondLoc, CondLoc, ThenStmt, SourceLocation(), nullptr));
+  }
+
   auto MakeNumIterationsInIVTy = [&](unsigned I) -> ExprResult {
     ExprResult N = MakeNumIterations(I);
     if (!N.isUsable())
@@ -17847,9 +17876,10 @@ SemaOpenMP::ActOnOpenMPFlattenDirective(ArrayRef<OMPClause *> Clauses,
       OutermostHelper.Inc->getEndLoc());
 
   // A counter only reaches its final value if its own loop and all enclosing
-  // loops execute at least one iteration; otherwise it keeps the value assigned
-  // by the pre-inits. Guarding also avoids evaluating 'start + n * step' for an
-  // empty loop, whose trip count may have wrapped.
+  // loops execute at least one iteration. Otherwise it keeps the last value
+  // from its init, or the value from before the nest if that init did not run.
+  // Guarding also avoids evaluating 'start + n * step' for an empty loop,
+  // whose trip count may have wrapped.
   SmallVector<Stmt *, 4> Finals;
   for (unsigned I = 0; I < NumLoops; ++I) {
     assert(LoopHelpers[I].Finals.size() == 1 &&
diff --git a/clang/test/OpenMP/flatten_codegen.cpp b/clang/test/OpenMP/flatten_codegen.cpp
index f9bc03c55ed2cc..a8870c59ee49d0 100644
--- a/clang/test/OpenMP/flatten_codegen.cpp
+++ b/clang/test/OpenMP/flatten_codegen.cpp
@@ -142,4 +142,16 @@ extern "C" void mixed_sign(int n) {
       body((int)i, j);
 }
 
+// Empty outer loop: the outer init still runs, the inner init must not.
+// CHECK-LABEL: define {{.*}}void @empty_outer(
+extern "C" void empty_outer() {
+  int I = -1;
+  int J = -1;
+  int Zero = 0;
+#pragma omp flatten
+  for (I = 0; I < Zero; ++I)
+    for (J = 0; J < 3; ++J)
+      body(I, J);
+}
+
 #endif
diff --git a/openmp/runtime/test/transform/flatten/final-values.c b/openmp/runtime/test/transform/flatten/final-values.c
index d770cf0a1fa2c8..ed605e9849950a 100644
--- a/openmp/runtime/test/transform/flatten/final-values.c
+++ b/openmp/runtime/test/transform/flatten/final-values.c
@@ -33,9 +33,46 @@ int main(void) {
       ;
 
   printf("empty-inner-i=%d empty-inner-j=%d\n", i, j);
+
+  // An empty outer loop still runs its init, but never enters the body, so the
+  // inner init does not run and 'j' keeps the value from before the nest.
+  i = -1;
+  j = -1;
+#pragma omp flatten
+  for (i = 0; i < Zero; ++i)
+    for (j = 0; j < 3; ++j)
+      ;
+
+  printf("empty-outer-i=%d empty-outer-j=%d\n", i, j);
+
+  // The middle loop is empty, so the innermost init never runs.
+  int k = -1;
+  i = -1;
+  j = -1;
+#pragma omp flatten depth(3)
+  for (i = 0; i < 2; ++i)
+    for (j = 0; j < Zero; ++j)
+      for (k = 0; k < 3; ++k)
+        ;
+
+  printf("mid-empty-i=%d mid-empty-j=%d mid-empty-k=%d\n", i, j, k);
+
+  // Outer init is a DeclStmt (always run). The inner assignment is still
+  // skipped when that outer loop is empty.
+  j = -1;
+#pragma omp flatten
+  for (int ii = 0; ii < Zero; ++ii)
+    for (j = 0; j < 3; ++j)
+      ;
+
+  printf("decl-outer-j=%d\n", j);
+
   return EXIT_SUCCESS;
 }
 
 // CHECK:      i=2 j=3
 // CHECK-NEXT: descending-i=-1 descending-j=1
 // CHECK-NEXT: empty-inner-i=3 empty-inner-j=0
+// CHECK-NEXT: empty-outer-i=0 empty-outer-j=-1
+// CHECK-NEXT: mid-empty-i=2 mid-empty-j=0 mid-empty-k=-1
+// CHECK-NEXT: decl-outer-j=-1

>From 98725134ca18be5ae263595b57e68312e3d9508f Mon Sep 17 00:00:00 2001
From: amtiwari <amtiwari at amd.com>
Date: Wed, 4 Nov 2026 02:22:33 -0500
Subject: [PATCH 2/2] revised

---
 clang/lib/Sema/SemaOpenMP.cpp         | 11 +++++++----
 clang/test/OpenMP/flatten_codegen.cpp |  9 ++++++++-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 0927c9e8c110f5..79481a70996c59 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -17579,8 +17579,12 @@ SemaOpenMP::ActOnOpenMPFlattenDirective(ArrayRef<OMPClause *> Clauses,
   collectLoopStmts(AStmt, LoopStmts);
 
   // Collect pre-init statements in outer-to-inner order.
+  // Finals below already skip leftover writes when a loop has no trips, so a
+  // leftover value is whatever PreInits last wrote. Gate inner assignment
+  // inits the same way: otherwise J = 0 always runs, Finals skip, and J stays
+  // at that wrong PreInit value.
   SmallVector<Stmt *> PreInits;
-  SmallVector<SmallVector<Stmt *, 2>, 4> GatedInits(NumLoops);
+  SmallVector<SmallVector<Stmt *, 1>, 4> GatedInits(NumLoops);
   for (auto I : llvm::seq<unsigned>(NumLoops)) {
     OMPLoopBasedDirective::HelperExprs &LoopHelper = LoopHelpers[I];
     assert(LoopHelper.Counters.size() == 1 &&
@@ -17590,7 +17594,7 @@ SemaOpenMP::ActOnOpenMPFlattenDirective(ArrayRef<OMPClause *> Clauses,
                       PreInits);
       continue;
     }
-    SmallVector<Stmt *, 2> AlwaysInits;
+    SmallVector<Stmt *, 1> AlwaysInits;
     for (Stmt *S : OriginalInits[I]) {
       if (isa<DeclStmt>(S))
         AlwaysInits.push_back(S);
@@ -17721,9 +17725,8 @@ SemaOpenMP::ActOnOpenMPFlattenDirective(ArrayRef<OMPClause *> Clauses,
                                          TripCount.get(), ZeroTripCount);
   if (!TripCount.isUsable())
     return StmtError();
-
   // Inner assignments run only if every enclosing loop has at least one
-  // iteration.
+  // iteration. Otherwise the gated Finals leave that PreInit as the leftover.
   for (unsigned I = 1; I < NumLoops; ++I) {
     if (GatedInits[I].empty())
       continue;
diff --git a/clang/test/OpenMP/flatten_codegen.cpp b/clang/test/OpenMP/flatten_codegen.cpp
index a8870c59ee49d0..672ccf96637a2e 100644
--- a/clang/test/OpenMP/flatten_codegen.cpp
+++ b/clang/test/OpenMP/flatten_codegen.cpp
@@ -142,8 +142,15 @@ extern "C" void mixed_sign(int n) {
       body((int)i, j);
 }
 
-// Empty outer loop: the outer init still runs, the inner init must not.
+// Empty outer loop: outer I = 0 always runs; inner J = 0 is gated on the
+// outer PreCond, same idea as gated Finals leftover writes.
 // CHECK-LABEL: define {{.*}}void @empty_outer(
+// CHECK:       store i32 0, ptr %I,
+// CHECK:       %[[HAS_OUTER:.+]] = icmp slt i32 0, %{{.+}}
+// CHECK-NEXT:  br i1 %[[HAS_OUTER]], label %[[J_INIT:[^,]+]], label %[[AFTER_J:[^,]+]]
+// CHECK:     [[J_INIT]]:
+// CHECK:       store i32 0, ptr %J,
+// CHECK-NEXT:  br label %[[AFTER_J]]
 extern "C" void empty_outer() {
   int I = -1;
   int J = -1;



More information about the cfe-commits mailing list