[clang] fb23278 - [Clang][OpenMP] Add flatten empty-loop final value tests (#237907)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 01:48:53 PST 2026


Author: Amit Tiwari
Date: 2026-11-26T15:18:47+05:30
New Revision: fb232785d24b647d2e5962f33a95a45c2fa5571d

URL: https://github.com/llvm/llvm-project/commit/fb232785d24b647d2e5962f33a95a45c2fa5571d
DIFF: https://github.com/llvm/llvm-project/commit/fb232785d24b647d2e5962f33a95a45c2fa5571d.diff

LOG: [Clang][OpenMP] Add flatten empty-loop final value tests (#237907)

Flatten used to always run inner inits like J = 0 before the loop.
Finals already skip leftover writes when a loop has no trips, so that
PreInit value stuck and leftover J was wrong. In simple words, Flatten
was setting inner loop vars even when the outer loop never ran.

This patch adds tests for the fix already made. 

#225350

Added: 
    

Modified: 
    clang/test/OpenMP/flatten_codegen.cpp
    openmp/runtime/test/transform/flatten/final-values.c

Removed: 
    


################################################################################
diff  --git a/clang/test/OpenMP/flatten_codegen.cpp b/clang/test/OpenMP/flatten_codegen.cpp
index f9bc03c55ed2cc..672ccf96637a2e 100644
--- a/clang/test/OpenMP/flatten_codegen.cpp
+++ b/clang/test/OpenMP/flatten_codegen.cpp
@@ -142,4 +142,23 @@ extern "C" void mixed_sign(int n) {
       body((int)i, j);
 }
 
+// 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;
+  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


        


More information about the cfe-commits mailing list