[clang] 96011ef - [Clang][OpenMP] Fix crash on a range-based for loop counted by `ordered(n)` but not collapsed (#240126)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 8 10:29:32 PST 2026
Author: Akash Manna
Date: 2026-11-08T18:29:27Z
New Revision: 96011efeb0f4bd99627dde9d9dc914e8f42663d7
URL: https://github.com/llvm/llvm-project/commit/96011efeb0f4bd99627dde9d9dc914e8f42663d7
DIFF: https://github.com/llvm/llvm-project/commit/96011efeb0f4bd99627dde9d9dc914e8f42663d7.diff
LOG: [Clang][OpenMP] Fix crash on a range-based for loop counted by `ordered(n)` but not collapsed (#240126)
Fixes #199449
Sema analyzes `max(collapse, ordered)` loops and hoists their bounds
into the pre-inits, which for a range-based `for` refer to its implicit
`__range`, `__begin` and `__end`. CodeGen emitted those only for the
collapsed loops, so a loop counted by `ordered(n)` alone hit
`llvm_unreachable("DeclRefExpr for Decl not entered in LocalDeclMap?")`.
The collapsed shape from the report was already fixed by #146772; this
one still crashed on main.
CodeGen now walks the same loops Sema did. The helpers of the extra
loops serve the pre-inits only, since such a loop is emitted as an
ordinary loop in the body and declares its own, so their mappings are
dropped afterwards.
AI Assisted!
Added:
clang/test/OpenMP/GH199449.cpp
Modified:
clang/docs/ReleaseNotes.md
clang/lib/CodeGen/CGStmtOpenMP.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 110dc79b7a7ae8..603b3096259cb5 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1586,6 +1586,8 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
the clause was parsed but its condition was discarded without effect.
- Fixed a crash when the loop variable or a loop bound of an OpenMP loop has a
`_BitInt` type wider than any standard integer type. (#GH140074)
+- Fixed a crash when a C++ range-based `for` loop is counted by `ordered(n)`
+ but not by `collapse`. (#GH199449)
- Fixed a crash when a non-static data member appears in a `lastprivate` clause
inside a member function template of a non-template class. (#GH144302)
- Fixed a crash when a `linear` clause with the `uval` modifier names a
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index 8c91a4a3cbe329..e7e38ac2c99c33 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -173,6 +173,7 @@ class OMPLoopScope : public CodeGenFunction::RunCleanupsScope {
void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopBasedDirective &S) {
const Stmt *PreInits;
CodeGenFunction::OMPMapVars PreCondVars;
+ SmallVector<const Decl *, 4> DoacrossOnlyDecls;
if (auto *LD = dyn_cast<OMPLoopDirective>(&S)) {
// Emit init, __range, __begin and __end variables for C++ range loops.
(void)OMPLoopBasedDirective::doForAllLoops(
@@ -214,6 +215,30 @@ class OMPLoopScope : public CodeGenFunction::RunCleanupsScope {
}
}
(void)PreCondVars.apply(CGF);
+ // The pre-inits also use the bounds of the loops that an 'ordered(n)'
+ // clause counts beyond the collapsed ones. Such a loop is emitted as a
+ // regular loop in the body and declares its own helpers, so these copies
+ // only live until the pre-inits.
+ if (const auto *C = LD->getSingleClause<OMPOrderedClause>())
+ (void)OMPLoopBasedDirective::doForAllLoops(
+ LD->getInnermostCapturedStmt()->getCapturedStmt(),
+ /*TryImperfectlyNestedLoops=*/true,
+ C->getLoopNumIterations().size(),
+ [&CGF, LD, &DoacrossOnlyDecls](unsigned Cnt, const Stmt *CurStmt) {
+ const auto *CXXFor = dyn_cast<CXXForRangeStmt>(CurStmt);
+ if (!CXXFor || Cnt < LD->getLoopsNumber())
+ return false;
+ for (const Stmt *Helper : std::initializer_list<const Stmt *>{
+ CXXFor->getInit(), CXXFor->getRangeStmt(),
+ CXXFor->getBeginStmt(), CXXFor->getEndStmt()}) {
+ if (!Helper)
+ continue;
+ CGF.EmitStmt(Helper);
+ if (const auto *DS = dyn_cast<DeclStmt>(Helper))
+ llvm::append_range(DoacrossOnlyDecls, DS->decls());
+ }
+ return false;
+ });
PreInits = LD->getPreInits();
} else if (const auto *Tile = dyn_cast<OMPTileDirective>(&S)) {
PreInits = Tile->getPreInits();
@@ -234,6 +259,8 @@ class OMPLoopScope : public CodeGenFunction::RunCleanupsScope {
llvm_unreachable("Unknown loop-based directive kind.");
}
doEmitPreinits(PreInits);
+ for (const Decl *D : DoacrossOnlyDecls)
+ CGF.eraseLocalDecl(D);
PreCondVars.restore(CGF);
}
diff --git a/clang/test/OpenMP/GH199449.cpp b/clang/test/OpenMP/GH199449.cpp
new file mode 100644
index 00000000000000..1594fbc3944d45
--- /dev/null
+++ b/clang/test/OpenMP/GH199449.cpp
@@ -0,0 +1,228 @@
+// RUN: %clang_cc1 -verify -fopenmp -std=c++23 -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -verify -fopenmp-simd -std=c++23 -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s --check-prefix=SIMD-ONLY0
+// SIMD-ONLY0-NOT: {{__kmpc|__tgt}}
+// expected-no-diagnostics
+
+// C++ range-based for loops with class-type iterators under OpenMP loop
+// directives. The precondition Sema builds for such a loop is the loop's own
+// `__begin != __end` test when the iterator has no `operator<`, so CodeGen has
+// to emit `__begin` along with `__range` and `__end` before the pre-inits
+// (GH199449), and it has to do so for every loop whose bounds Sema hoists,
+// which includes the loops that an `ordered(n)` clause counts but `collapse`
+// does not.
+
+typedef __PTRDIFF_TYPE__ ptr
diff _t;
+
+template <int N>
+struct int_range {
+ struct iter {
+ int v;
+ int operator*() const { return v; }
+ iter &operator++() {
+ ++v;
+ return *this;
+ }
+ iter operator++(int) {
+ iter t = *this;
+ ++v;
+ return t;
+ }
+ bool operator!=(const iter &o) const { return v != o.v; }
+ int operator-(const iter &o) const { return v - o.v; }
+ iter operator+(ptr
diff _t n) const { return {v + (int)n}; }
+ iter &operator+=(ptr
diff _t n) {
+ v += (int)n;
+ return *this;
+ }
+ };
+ iter begin() const { return {0}; }
+ iter end() const { return {N}; }
+};
+
+// The reproducer of GH199449: collapse(2) over two range-for loops in a
+// function template, with a simd reduction in the body.
+template <int M, int N, int K>
+void matmul(const float *__restrict__ A, const float *__restrict__ B,
+ float *__restrict__ C) {
+#pragma omp parallel for collapse(2) schedule(static)
+ for (auto ii : int_range<M>()) {
+ for (auto jj : int_range<N>()) {
+ float sum = 0.f;
+#pragma omp simd reduction(+ : sum)
+ for (int k = 0; k < K; ++k)
+ sum += A[ii * K + k] * B[jj * K + k];
+ C[ii * N + jj] = sum;
+ }
+ }
+}
+
+void test(const float *A, const float *B, float *C) {
+ matmul<64, 512, 512>(A, B, C);
+}
+// CHECK-LABEL: define {{.*}}@_Z6matmulILi64ELi512ELi512EEv
+// CHECK: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{[0-9]+}}, i32 {{[0-9]+}}, ptr @[[MATMUL:[^,]+]],
+// CHECK: define internal void @[[MATMUL]](
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi64EE5beginEv(
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi512EE5beginEv(
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi64EE4itermiE
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi512EE4itermiE
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi64EE4iterneE
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi512EE4iterneE
+// CHECK-DAG: call void @__kmpc_for_static_init_{{4|8}}(
+// CHECK-DAG: call {{.*}}@_ZN{{K?}}9int_rangeILi64EE4iterp{{[lL]}}E
+// CHECK-DAG: call {{.*}}@_ZN{{K?}}9int_rangeILi512EE4iterp{{[lL]}}E
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi64EE4iterdeEv(
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi512EE4iterdeEv(
+// CHECK: call void @__kmpc_for_static_fini(
+
+// The same nest outside of a template.
+void matmul_plain(const float *__restrict__ A, const float *__restrict__ B,
+ float *__restrict__ C) {
+#pragma omp parallel for collapse(2) schedule(static)
+ for (auto ii : int_range<64>()) {
+ for (auto jj : int_range<512>()) {
+ float sum = 0.f;
+#pragma omp simd reduction(+ : sum)
+ for (int k = 0; k < 512; ++k)
+ sum += A[ii * 512 + k] * B[jj * 512 + k];
+ C[ii * 512 + jj] = sum;
+ }
+ }
+}
+// CHECK-LABEL: define {{.*}}@_Z{{[0-9]+}}matmul_plain
+// CHECK: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{[0-9]+}}, i32 {{[0-9]+}}, ptr @[[PLAIN:[^,]+]],
+// CHECK: define internal void @[[PLAIN]](
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi64EE4iterneE
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi512EE4iterneE
+// CHECK: call void @__kmpc_for_static_init_{{4|8}}(
+// CHECK: call void @__kmpc_for_static_fini(
+
+// The minimal shape: a single range-for loop, no collapse, no nested directive.
+void single_range(float *C) {
+#pragma omp for
+ for (auto ii : int_range<4>())
+ C[ii] = 0;
+}
+// CHECK-LABEL: define {{.*}}@_Z{{[0-9]+}}single_rangePf(
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi4EE5beginEv(
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi4EE4itermiE
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi4EE4iterneE
+// CHECK: call void @__kmpc_for_static_init_4(
+// CHECK: call {{.*}}@_ZN{{K?}}9int_rangeILi4EE4iterp{{[lL]}}E
+// CHECK: call {{.*}}@_ZNK9int_rangeILi4EE4iterdeEv(
+// CHECK: call void @__kmpc_for_static_fini(
+
+// A loop that 'ordered(2)' counts but 'collapse' does not is emitted as an
+// ordinary loop in the body, yet its trip count is hoisted for the doacross
+// initialization, so its __range, __begin and __end are needed up front too.
+void doacross_range_range(float *C) {
+#pragma omp parallel for ordered(2)
+ for (auto ii : int_range<4>())
+ for (auto jj : int_range<8>())
+ C[ii * 8 + jj] = 0;
+}
+// CHECK-LABEL: define {{.*}}@_Z{{[0-9]+}}doacross_range_rangePf(
+// CHECK: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{[0-9]+}}, i32 {{[0-9]+}}, ptr @[[RANGE_RANGE:[^,]+]],
+// CHECK: define internal void @[[RANGE_RANGE]](
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi4EE4iterneE
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi8EE5beginEv(
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi8EE4itermiE
+// CHECK-DAG: call void @__kmpc_doacross_init(ptr @{{[0-9]+}}, i32 %{{.+}}, i32 2, ptr %{{.+}})
+// CHECK-DAG: call void @__kmpc_for_static_init_{{4|8}}(
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi8EE4iterneE
+// CHECK-DAG: call {{.*}}@_ZN9int_rangeILi8EE4iterppEv(
+// CHECK: call void @__kmpc_doacross_fini(
+
+void doacross_plain_range(float *C, int n) {
+#pragma omp parallel for ordered(2)
+ for (int ii = 0; ii < n; ++ii)
+ for (auto jj : int_range<8>())
+ C[ii * 8 + jj] = 0;
+}
+// CHECK-LABEL: define {{.*}}@_Z{{[0-9]+}}doacross_plain_rangePfi(
+// CHECK: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{[0-9]+}}, i32 {{[0-9]+}}, ptr @[[PLAIN_RANGE:[^,]+]],
+// CHECK: define internal void @[[PLAIN_RANGE]](
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi8EE5beginEv(
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi8EE4itermiE
+// CHECK-DAG: call void @__kmpc_doacross_init(ptr @{{[0-9]+}}, i32 %{{.+}}, i32 2, ptr %{{.+}})
+// CHECK-DAG: call {{.*}}@_ZN9int_rangeILi8EE4iterppEv(
+// CHECK: call void @__kmpc_doacross_fini(
+
+// The same with an array range, where the hoisted __begin is a pointer.
+void doacross_carray(float *C, int n, int (&arr)[8]) {
+#pragma omp for ordered(2)
+ for (int ii = 0; ii < n; ++ii)
+ for (int jj : arr)
+ C[ii * 8 + jj] = 0;
+}
+// CHECK-LABEL: define {{.*}}@_Z{{[0-9]+}}doacross_carrayPfiRA8_i(
+// CHECK: call void @__kmpc_doacross_init(ptr @{{[0-9]+}}, i32 %{{.+}}, i32 2, ptr %{{.+}})
+// CHECK: call void @__kmpc_for_static_init_4(
+// CHECK: call void @__kmpc_doacross_fini(
+
+// An init-statement is hoisted along with the range of the loop it belongs to.
+void doacross_init_stmt(float *C, int n) {
+#pragma omp parallel for ordered(2)
+ for (int ii = 0; ii < n; ++ii)
+ for (int base = 1; auto jj : int_range<8>())
+ C[(ii + base) * 8 + jj] = 0;
+}
+// CHECK-LABEL: define {{.*}}@_Z{{[0-9]+}}doacross_init_stmtPfi(
+// CHECK: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{[0-9]+}}, i32 {{[0-9]+}}, ptr @[[INIT_STMT:[^,]+]],
+// CHECK: define internal void @[[INIT_STMT]](
+// CHECK-DAG: call void @__kmpc_doacross_init(ptr @{{[0-9]+}}, i32 %{{.+}}, i32 2, ptr %{{.+}})
+// CHECK-DAG: call {{.*}}@_ZN9int_rangeILi8EE4iterppEv(
+// CHECK: call void @__kmpc_doacross_fini(
+
+// Two collapsed range-for loops plus one that only 'ordered(3)' counts.
+void doacross_collapse2_ordered3(float *C) {
+#pragma omp parallel for collapse(2) ordered(3)
+ for (auto ii : int_range<4>())
+ for (auto jj : int_range<8>())
+ for (auto kk : int_range<16>())
+ C[(ii * 8 + jj) * 16 + kk] = 0;
+}
+// CHECK-LABEL: define {{.*}}@_Z{{[0-9]+}}doacross_collapse2_ordered3Pf(
+// CHECK: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{[0-9]+}}, i32 {{[0-9]+}}, ptr @[[COLLAPSE2_ORDERED3:[^,]+]],
+// CHECK: define internal void @[[COLLAPSE2_ORDERED3]](
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi4EE4iterneE
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi8EE4iterneE
+// CHECK-DAG: call {{.*}}@_ZNK9int_rangeILi16EE4itermiE
+// CHECK-DAG: call void @__kmpc_doacross_init(ptr @{{[0-9]+}}, i32 %{{.+}}, i32 3, ptr %{{.+}})
+// CHECK-DAG: call {{.*}}@_ZN9int_rangeILi16EE4iterppEv(
+// CHECK: call void @__kmpc_doacross_fini(
+
+// Control: with an operator< the precondition is built from the captured
+// bounds instead of the loop's own condition.
+struct lt_range {
+ struct iter {
+ int v;
+ int operator*() const { return v; }
+ iter &operator++() {
+ ++v;
+ return *this;
+ }
+ bool operator!=(const iter &o) const { return v != o.v; }
+ bool operator<(const iter &o) const { return v < o.v; }
+ int operator-(const iter &o) const { return v - o.v; }
+ iter &operator+=(ptr
diff _t n) {
+ v += (int)n;
+ return *this;
+ }
+ };
+ iter begin() const { return {0}; }
+ iter end() const { return {8}; }
+};
+
+void lt_control(float *C) {
+#pragma omp parallel for collapse(2)
+ for (auto ii : lt_range())
+ for (auto jj : lt_range())
+ C[ii * 8 + jj] = 0;
+}
+// CHECK-LABEL: define {{.*}}@_Z{{[0-9]+}}lt_controlPf(
+// CHECK: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{[0-9]+}}, i32 {{[0-9]+}}, ptr @[[LT:[^,]+]],
+// CHECK: define internal void @[[LT]](
+// CHECK: call {{.*}}@_ZNK8lt_range4iterltE
+// CHECK: call void @__kmpc_for_static_init_{{4|8}}(
+// CHECK: call void @__kmpc_for_static_fini(
More information about the cfe-commits
mailing list