[clang] 7f84220 - [Clang][OpenMP] Fix crash on task reduction of an array with a declare reduction that has no initializer (#239970)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 7 09:55:23 PST 2026
Author: Akash Manna
Date: 2026-11-07T17:55:18Z
New Revision: 7f84220a774a96f6d98bdad01cc47f54d2177278
URL: https://github.com/llvm/llvm-project/commit/7f84220a774a96f6d98bdad01cc47f54d2177278
DIFF: https://github.com/llvm/llvm-project/commit/7f84220a774a96f6d98bdad01cc47f54d2177278.diff
LOG: [Clang][OpenMP] Fix crash on task reduction of an array with a declare reduction that has no initializer (#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.
Added:
clang/test/OpenMP/GH141228.cpp
Modified:
clang/docs/ReleaseNotes.md
clang/lib/CodeGen/CGOpenMPRuntime.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 9219384616cb79..110dc79b7a7ae8 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1553,6 +1553,8 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
loop. `depth(k)` selects how many outermost loops to combine (default 2).
- Fixed a crash when the statement of an OpenMP `atomic` directive contains an
invalid expression, such as `0--;`. (#GH107979)
+- Fixed a crash on a task reduction of an array whose `declare reduction` has
+ no `initializer` clause. (#GH141228)
- Canonicalize intra-tiles in loop tiling. `#pragma omp tile` still emits a
min-bounded inner loop, which vectorizes well. When a parent directive such as
`for collapse(n)` needs a constant per-tile trip count, Clang rereads a
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 4312c2d239a83c..a88039aeb9cee6 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
More information about the cfe-commits
mailing list