https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/240126
>From 3714c60c656d09132f2bb33b9ecbbd2d89158721 Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Sun, 8 Nov 2026 13:25:22 +0530 Subject: [PATCH 1/2] [Clang][OpenMP] Fix crash on a range-based for loop counted by ordered(n) but not collapsed 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. LLM tools were used for this contribution. --- clang/docs/ReleaseNotes.md | 2 + clang/lib/CodeGen/CGStmtOpenMP.cpp | 39 +++-- clang/test/OpenMP/GH199449.cpp | 228 +++++++++++++++++++++++++++++ 3 files changed, 260 insertions(+), 9 deletions(-) create mode 100644 clang/test/OpenMP/GH199449.cpp diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 6d1dbbbb6eb77b..5b213345a9cd57 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1505,6 +1505,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) - Loop-transforming constructs such as `tile` and `unroll` are now allowed diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp index e63957df3e4069..cc8ee07d521764 100644 --- a/clang/lib/CodeGen/CGStmtOpenMP.cpp +++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp @@ -173,19 +173,38 @@ 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)) { + // The pre-inits also use the bounds of the loops that an 'ordered(n)' + // clause counts beyond the collapsed ones. + unsigned NumLoops = LD->getLoopsNumber(); + for (const auto *C : LD->getClausesOfKind<OMPOrderedClause>()) + if (C->getNumForLoops() && C->getLoopNumIterations().size() > NumLoops) + NumLoops = C->getLoopNumIterations().size(); // Emit init, __range, __begin and __end variables for C++ range loops. + // A loop that is not collapsed is emitted as a regular loop in the body + // and declares its own, so its copies only live until the pre-inits. (void)OMPLoopBasedDirective::doForAllLoops( LD->getInnermostCapturedStmt()->getCapturedStmt(), - /*TryImperfectlyNestedLoops=*/true, LD->getLoopsNumber(), - [&CGF](unsigned Cnt, const Stmt *CurStmt) { - if (const auto *CXXFor = dyn_cast<CXXForRangeStmt>(CurStmt)) { - if (const Stmt *Init = CXXFor->getInit()) - CGF.EmitStmt(Init); - CGF.EmitStmt(CXXFor->getRangeStmt()); - CGF.EmitStmt(CXXFor->getBeginStmt()); - CGF.EmitStmt(CXXFor->getEndStmt()); - } + /*TryImperfectlyNestedLoops=*/true, NumLoops, + [&CGF, LD, &DoacrossOnlyDecls](unsigned Cnt, const Stmt *CurStmt) { + const auto *CXXFor = dyn_cast<CXXForRangeStmt>(CurStmt); + if (!CXXFor) + return false; + const Stmt *Init = CXXFor->getInit(); + if (Init) + CGF.EmitStmt(Init); + CGF.EmitStmt(CXXFor->getRangeStmt()); + CGF.EmitStmt(CXXFor->getBeginStmt()); + CGF.EmitStmt(CXXFor->getEndStmt()); + if (Cnt < LD->getLoopsNumber()) + return false; + for (const Stmt *Helper : + {Init, static_cast<const Stmt *>(CXXFor->getRangeStmt()), + static_cast<const Stmt *>(CXXFor->getBeginStmt()), + static_cast<const Stmt *>(CXXFor->getEndStmt())}) + if (const auto *DS = dyn_cast_or_null<DeclStmt>(Helper)) + llvm::append_range(DoacrossOnlyDecls, DS->decls()); return false; }); llvm::DenseSet<const VarDecl *> EmittedAsPrivate; @@ -234,6 +253,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__ ptrdiff_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+(ptrdiff_t n) const { return {v + (int)n}; } + iter &operator+=(ptrdiff_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+=(ptrdiff_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( >From 7b310ba5b0c20bad7c3f463edbc19c271ce671c8 Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Sun, 8 Nov 2026 21:30:40 +0530 Subject: [PATCH 2/2] [Clang][OpenMP] Emit the `ordered(n)` range-for helpers in a separate walk Keep the range-for walk over the collapsed loops as it was and handle the loops that only ordered(n) counts in their own block after the counters are remapped. --- clang/lib/CodeGen/CGStmtOpenMP.cpp | 60 ++++++++++++++++-------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp index cc8ee07d521764..93bbb250e155fd 100644 --- a/clang/lib/CodeGen/CGStmtOpenMP.cpp +++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp @@ -175,36 +175,18 @@ class OMPLoopScope : public CodeGenFunction::RunCleanupsScope { CodeGenFunction::OMPMapVars PreCondVars; SmallVector<const Decl *, 4> DoacrossOnlyDecls; if (auto *LD = dyn_cast<OMPLoopDirective>(&S)) { - // The pre-inits also use the bounds of the loops that an 'ordered(n)' - // clause counts beyond the collapsed ones. - unsigned NumLoops = LD->getLoopsNumber(); - for (const auto *C : LD->getClausesOfKind<OMPOrderedClause>()) - if (C->getNumForLoops() && C->getLoopNumIterations().size() > NumLoops) - NumLoops = C->getLoopNumIterations().size(); // Emit init, __range, __begin and __end variables for C++ range loops. - // A loop that is not collapsed is emitted as a regular loop in the body - // and declares its own, so its copies only live until the pre-inits. (void)OMPLoopBasedDirective::doForAllLoops( LD->getInnermostCapturedStmt()->getCapturedStmt(), - /*TryImperfectlyNestedLoops=*/true, NumLoops, - [&CGF, LD, &DoacrossOnlyDecls](unsigned Cnt, const Stmt *CurStmt) { - const auto *CXXFor = dyn_cast<CXXForRangeStmt>(CurStmt); - if (!CXXFor) - return false; - const Stmt *Init = CXXFor->getInit(); - if (Init) - CGF.EmitStmt(Init); - CGF.EmitStmt(CXXFor->getRangeStmt()); - CGF.EmitStmt(CXXFor->getBeginStmt()); - CGF.EmitStmt(CXXFor->getEndStmt()); - if (Cnt < LD->getLoopsNumber()) - return false; - for (const Stmt *Helper : - {Init, static_cast<const Stmt *>(CXXFor->getRangeStmt()), - static_cast<const Stmt *>(CXXFor->getBeginStmt()), - static_cast<const Stmt *>(CXXFor->getEndStmt())}) - if (const auto *DS = dyn_cast_or_null<DeclStmt>(Helper)) - llvm::append_range(DoacrossOnlyDecls, DS->decls()); + /*TryImperfectlyNestedLoops=*/true, LD->getLoopsNumber(), + [&CGF](unsigned Cnt, const Stmt *CurStmt) { + if (const auto *CXXFor = dyn_cast<CXXForRangeStmt>(CurStmt)) { + if (const Stmt *Init = CXXFor->getInit()) + CGF.EmitStmt(Init); + CGF.EmitStmt(CXXFor->getRangeStmt()); + CGF.EmitStmt(CXXFor->getBeginStmt()); + CGF.EmitStmt(CXXFor->getEndStmt()); + } return false; }); llvm::DenseSet<const VarDecl *> EmittedAsPrivate; @@ -233,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(); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
