https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/239970
Fixes #141228 `EmitOMPAggregateInit` set up a source pointer and PHI over the original item for any `declare reduction`, but the task reduction init function only passes that address when the `declare reduction` has an `initializer`. For an array without one the `Address` was invalid and tripped the assert. Only the initializer reads `omp_orig`, so the source handling now keys on `DRD->getInitializer()`, matching the check in `emitReduceInitFunction`. AI Aided. >From 78c2d967789e7253aa64887fe53368bdb1a529bc Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Sat, 7 Nov 2026 11:51:17 +0530 Subject: [PATCH] [clang][OpenMP] Fix crash on task reduction of an array with a declare reduction that has no initializer Fixes #141228 `EmitOMPAggregateInit` set up a source pointer and PHI over the original item for any `declare reduction`, but the task reduction init function only passes that address when the `declare reduction` has an `initializer`. For an array without one the `Address` was invalid and tripped the assert. Only the initializer reads `omp_orig`, so the source handling now keys on `DRD->getInitializer()`, matching the check in `emitReduceInitFunction`. Assisted-by: LLMs --- clang/docs/ReleaseNotes.md | 2 + clang/lib/CodeGen/CGOpenMPRuntime.cpp | 16 +- clang/test/OpenMP/GH141228.cpp | 201 ++++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 clang/test/OpenMP/GH141228.cpp diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 6d1dbbbb6eb77b..255a20535847e6 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1507,6 +1507,8 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use `_BitInt` type wider than any standard integer type. (#GH140074) - 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 on a task reduction of an array whose `declare reduction` has + no `initializer` clause. (#GH141228) - Loop-transforming constructs such as `tile` and `unroll` are now allowed inside a `simd` region for OpenMP 6.0 or later, which classifies them as simdizable. diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp index d3a6ffee3ea748..a559cfc30aafc3 100644 --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -661,7 +661,8 @@ static void emitInitWithReductionInitializer(CodeGenFunction &CGF, /// \param DestAddr Address of the array. /// \param Type Type of array. /// \param Init Initial expression of array. -/// \param SrcAddr Address of the original array. +/// \param SrcAddr Address of the original array, required only if the declare +/// reduction has an initializer. static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr, QualType Type, bool EmitDeclareReductionInit, const Expr *Init, @@ -670,14 +671,19 @@ static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr, // Perform element-by-element initialization. QualType ElementTy; + // Only the initializer of a declare reduction reads the original item. + bool UseSrc = DRD && DRD->getInitializer(); + assert((!UseSrc || SrcAddr.isValid()) && + "Declare reduction initializer requires the original item address."); + // Drill down to the base element type on both arrays. const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe(); llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr); - if (DRD) + if (UseSrc) SrcAddr = SrcAddr.withElementType(DestAddr.getElementType()); llvm::Value *SrcBegin = nullptr; - if (DRD) + if (UseSrc) SrcBegin = SrcAddr.emitRawPointer(CGF); llvm::Value *DestBegin = DestAddr.emitRawPointer(CGF); // Cast from pointer to array type to pointer to single element. @@ -698,7 +704,7 @@ static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr, llvm::PHINode *SrcElementPHI = nullptr; Address SrcElementCurrent = Address::invalid(); - if (DRD) { + if (UseSrc) { SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); SrcElementPHI->addIncoming(SrcBegin, EntryBB); @@ -724,7 +730,7 @@ static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr, /*IsInitializer=*/false); } - if (DRD) { + if (UseSrc) { // Shift the address forward by one element. llvm::Value *SrcElementNext = CGF.Builder.CreateConstGEP1_32( SrcAddr.getElementType(), SrcElementPHI, /*Idx0=*/1, diff --git a/clang/test/OpenMP/GH141228.cpp b/clang/test/OpenMP/GH141228.cpp new file mode 100644 index 00000000000000..86a441a312917d --- /dev/null +++ b/clang/test/OpenMP/GH141228.cpp @@ -0,0 +1,201 @@ +// RUN: %clang_cc1 -verify -Wno-vla -fopenmp -x c++ -triple x86_64-apple-darwin10 -emit-llvm %s -o - | FileCheck %s +// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-apple-darwin10 -emit-pch -o %t %s +// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-apple-darwin10 -include-pch %t -verify -Wno-vla %s -emit-llvm -o - | FileCheck %s + +// RUN: %clang_cc1 -verify -Wno-vla -fopenmp-simd -x c++ -triple x86_64-apple-darwin10 -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s +// RUN: %clang_cc1 -fopenmp-simd -x c++ -triple x86_64-apple-darwin10 -emit-pch -o %t %s +// RUN: %clang_cc1 -fopenmp-simd -x c++ -triple x86_64-apple-darwin10 -include-pch %t -verify -Wno-vla %s -emit-llvm -o - | FileCheck --check-prefix SIMD-ONLY0 %s +// SIMD-ONLY0-NOT: {{__kmpc|__tgt}} +// expected-no-diagnostics +#ifndef HEADER +#define HEADER + +// Task reduction of an array with a declare reduction that has no initializer +// clause: the initializer function must not read the original item. + +struct A { + int a; +}; +#pragma omp declare reduction(+ : A : omp_out.a += omp_in.a) + +// Reproducer from GH141228. +void test_for() { + A a, b[2]; +#pragma omp parallel + { +#pragma omp for reduction(task, + : a, b) + for (int i = 0; i < 5; i++) + ; + } +} + +// CHECK-LABEL: define internal void @_Z8test_forv.omp_outlined( +// CHECK: store ptr @[[A_INIT:.red_init.[.0-9]*]], ptr +// CHECK: store ptr @[[B_INIT:.red_init.[.0-9]*]], ptr +// CHECK: call ptr @__kmpc_taskred_modifier_init( +// CHECK: define internal void @[[A_INIT]](ptr noalias noundef %{{.+}}, ptr noalias noundef %{{.+}}) +// CHECK-NOT: omp.arrayinit +// CHECK: ret void +// CHECK: define internal void @[[B_INIT]](ptr noalias noundef %{{.+}}, ptr noalias noundef %{{.+}}) +// CHECK-NOT: srcElementPast +// CHECK: omp.arrayinit.body: +// CHECK-NEXT: [[DST:%.+]] = phi ptr [ +// CHECK-NEXT: getelementptr %struct.A, ptr [[DST]], i32 1 +// CHECK: ret void + +void test_sections() { + A b[2]; +#pragma omp parallel + { +#pragma omp sections reduction(task, + : b) + { + b[0].a = 1; + } + } +} + +// CHECK-LABEL: define internal void @_Z13test_sectionsv.omp_outlined( +// CHECK: store ptr @[[SB_INIT:.red_init.[.0-9]*]], ptr +// CHECK: call ptr @__kmpc_taskred_modifier_init( +// CHECK: define internal void @[[SB_INIT]](ptr noalias noundef %{{.+}}, ptr noalias noundef %{{.+}}) +// CHECK-NOT: srcElementPast +// CHECK: omp.arrayinit.body: +// CHECK-NEXT: [[DST:%.+]] = phi ptr [ +// CHECK-NEXT: getelementptr %struct.A, ptr [[DST]], i32 1 +// CHECK: ret void + +void test_taskgroup() { + A m[2][3]; +#pragma omp taskgroup task_reduction(+ : m) + { +#pragma omp task in_reduction(+ : m) + m[1][2].a = 1; + } +} + +// CHECK-LABEL: define {{.*}}void @_Z14test_taskgroupv( +// CHECK: store ptr @[[M_INIT:.red_init.[.0-9]*]], ptr +// CHECK: call ptr @__kmpc_taskred_init( +// CHECK: define internal void @[[M_INIT]](ptr noalias noundef %{{.+}}, ptr noalias noundef %{{.+}}) +// CHECK-NOT: srcElementPast +// CHECK: omp.arrayinit.body: +// CHECK-NEXT: [[DST:%.+]] = phi ptr [ +// CHECK-NEXT: getelementptr %struct.A, ptr [[DST]], i32 1 +// CHECK: ret void + +void test_vla(int n) { + A v[n]; +#pragma omp parallel + { +#pragma omp for reduction(task, + : v) + for (int i = 0; i < 5; i++) + ; + } +} + +// CHECK-LABEL: define internal void @_Z8test_vlai.omp_outlined( +// CHECK: store ptr @[[V_INIT:.red_init.[.0-9]*]], ptr +// CHECK: call ptr @__kmpc_taskred_modifier_init( +// CHECK: define internal void @[[V_INIT]](ptr noalias noundef %{{.+}}, ptr noalias noundef %{{.+}}) +// CHECK-NOT: srcElementPast +// CHECK: omp.arrayinit.body: +// CHECK-NEXT: [[DST:%.+]] = phi ptr [ +// CHECK-NEXT: getelementptr %struct.A, ptr [[DST]], i32 1 +// CHECK: ret void + +// No default-initialization expression: elements are zero-initialized. +#pragma omp declare reduction(my_add : int : omp_out += omp_in) +void test_int() { + int c[2]; +#pragma omp parallel + { +#pragma omp for reduction(task, my_add : c) + for (int i = 0; i < 5; i++) + ; + } +} + +// CHECK-LABEL: define internal void @_Z8test_intv.omp_outlined( +// CHECK: store ptr @[[C_INIT:.red_init.[.0-9]*]], ptr +// CHECK: call ptr @__kmpc_taskred_modifier_init( +// CHECK: define internal void @[[C_INIT]](ptr noalias noundef %{{.+}}, ptr noalias noundef %{{.+}}) +// CHECK-NOT: srcElementPast +// CHECK: omp.arrayinit.body: +// CHECK-NEXT: [[DST:%.+]] = phi ptr [ +// CHECK-NEXT: [[ZERO:%.+]] = load i32, ptr @.init +// CHECK-NEXT: store i32 [[ZERO]], ptr [[DST]] +// CHECK-NEXT: getelementptr i32, ptr [[DST]], i32 1 +// CHECK: ret void + +// Non-trivial element type. +struct S { + int a; + S() : a(0) {} + ~S() {} +}; +#pragma omp declare reduction(+ : S : omp_out.a += omp_in.a) +void test_nontrivial() { + S s[2]; +#pragma omp parallel + { +#pragma omp for reduction(task, + : s) + for (int i = 0; i < 5; i++) + ; + } +} + +// CHECK-LABEL: define internal void @_Z15test_nontrivialv.omp_outlined( +// CHECK: store ptr @[[S_INIT:.red_init.[.0-9]*]], ptr +// CHECK: store ptr @[[S_FINI:.red_fini.[.0-9]*]], ptr +// CHECK: call ptr @__kmpc_taskred_modifier_init( +// CHECK: define internal void @[[S_INIT]](ptr noalias noundef %{{.+}}, ptr noalias noundef %{{.+}}) +// CHECK-NOT: srcElementPast +// CHECK: omp.arrayinit.body: +// CHECK-NEXT: [[DST:%.+]] = phi ptr [ +// CHECK-NEXT: call void @_ZN1SC1Ev(ptr {{[^,]*}}[[DST]]) +// CHECK-NEXT: getelementptr %struct.S, ptr [[DST]], i32 1 +// CHECK: ret void +// CHECK: define internal void @[[S_FINI]](ptr noundef %{{.+}}) +// CHECK: call void @_ZN1SD1Ev( +// CHECK: ret void + +// Control: the initializer reads the original item. +struct B { + int a; +}; +#pragma omp declare reduction(+ : B : omp_out.a += omp_in.a) initializer(omp_priv = omp_orig) +void test_with_initializer() { + B b[2]; +#pragma omp parallel + { +#pragma omp for reduction(task, + : b) + for (int i = 0; i < 5; i++) + ; + } +} + +// CHECK-LABEL: define internal void @_Z21test_with_initializerv.omp_outlined( +// CHECK: store ptr @[[WB_INIT:.red_init.[.0-9]*]], ptr +// CHECK: call ptr @__kmpc_taskred_modifier_init( +// CHECK: define internal void @[[WB_INIT]](ptr noalias noundef %{{.+}}, ptr noalias noundef %{{.+}}) +// CHECK: omp.arrayinit.body: +// CHECK-NEXT: [[SRC:%.+]] = phi ptr [ +// CHECK-NEXT: [[DST:%.+]] = phi ptr [ +// CHECK-NEXT: call void @.omp_initializer.{{[.0-9]*}}(ptr {{[^,]*}}[[DST]], ptr {{[^,]*}}[[SRC]]) +// CHECK-NEXT: getelementptr %struct.B, ptr [[SRC]], i32 1 +// CHECK: ret void + +// Control: non-task reduction. +void test_nontask() { + A b[2]; +#pragma omp parallel for reduction(+ : b) + for (int i = 0; i < 5; i++) + ; +} + +// CHECK-LABEL: define internal void @_Z12test_nontaskv.omp_outlined( +// CHECK: omp.arrayinit.body: +// CHECK-NEXT: [[DST:%.+]] = phi ptr [ +// CHECK-NEXT: getelementptr %struct.A, ptr [[DST]], i32 1 + +#endif _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
