Skip to content

Commit 46b5aa4

Browse files
jprotzeyuxuanchen1997
authored andcommitted
[OpenMP] Fix calculation of dependencies for multi-dimensional iteration space (#99347)
The expectation for multiple iterators used in a single depend clause (`depend(iterator(i=0:5,j=0:5), in:x[i][j])`) is that the iterator space is the product of the iteration vectors (25 in that case). The current codeGen only works correctly, if `numIterators() = 1`. For more iterators, the execution results in runtime assertions or segfaults. The modified codeGen first calculates the iteration space, then multiplies to the number of dependencies in the depend clause and finally adds to the total number of iterator dependencies.
1 parent 16f2761 commit 46b5aa4

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4259,14 +4259,18 @@ std::pair<llvm::Value *, Address> CGOpenMPRuntime::emitDependClause(
42594259
// Include number of iterations, if any.
42604260

42614261
if (const auto *IE = cast_or_null<OMPIteratorExpr>(D.IteratorExpr)) {
4262+
llvm::Value *ClauseIteratorSpace =
4263+
llvm::ConstantInt::get(CGF.IntPtrTy, 1);
42624264
for (unsigned I = 0, E = IE->numOfIterators(); I < E; ++I) {
42634265
llvm::Value *Sz = CGF.EmitScalarExpr(IE->getHelper(I).Upper);
42644266
Sz = CGF.Builder.CreateIntCast(Sz, CGF.IntPtrTy, /*isSigned=*/false);
4265-
llvm::Value *NumClauseDeps = CGF.Builder.CreateNUWMul(
4266-
Sz, llvm::ConstantInt::get(CGF.IntPtrTy, D.DepExprs.size()));
4267-
NumOfRegularWithIterators =
4268-
CGF.Builder.CreateNUWAdd(NumOfRegularWithIterators, NumClauseDeps);
4267+
ClauseIteratorSpace = CGF.Builder.CreateNUWMul(Sz, ClauseIteratorSpace);
42694268
}
4269+
llvm::Value *NumClauseDeps = CGF.Builder.CreateNUWMul(
4270+
ClauseIteratorSpace,
4271+
llvm::ConstantInt::get(CGF.IntPtrTy, D.DepExprs.size()));
4272+
NumOfRegularWithIterators =
4273+
CGF.Builder.CreateNUWAdd(NumOfRegularWithIterators, NumClauseDeps);
42704274
HasRegularWithIterators = true;
42714275
continue;
42724276
}

clang/test/OpenMP/depend_iterator_bug.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
int x[100];
77
int y[100];
8+
int z[100][100];
89

910
// CHECK-LABEL: @many_iterators_single_clause(
1011
// CHECK: [[VLA:%.*]] = alloca [[STRUCT_KMP_DEPEND_INFO:%.*]], i64 10, align 16
@@ -24,3 +25,31 @@ void many_iterators_many_clauses(void) {
2425
{
2526
}
2627
}
28+
29+
// CHECK-LABEL: @multidim_iterators_clause1(
30+
// CHECK: [[VLA:%.*]] = alloca [[STRUCT_KMP_DEPEND_INFO:%.*]], i64 1, align 16
31+
// CHECK: = call i32 @__kmpc_omp_task_with_deps(ptr {{.*}}, i32 {{.*}}, ptr {{.*}}, i32 1, ptr {{.*}}, i32 0, ptr null)
32+
void multidim_iterators_clause1(void) {
33+
#pragma omp task depend(iterator(i=0:1, j=0:1), in: z[i][j])
34+
{
35+
}
36+
}
37+
38+
// CHECK-LABEL: @multidim_iterators_offset_clause(
39+
// CHECK: [[VLA:%.*]] = alloca [[STRUCT_KMP_DEPEND_INFO:%.*]], i64 1, align 16
40+
// CHECK: = call i32 @__kmpc_omp_task_with_deps(ptr {{.*}}, i32 {{.*}}, ptr {{.*}}, i32 1, ptr {{.*}}, i32 0, ptr null)
41+
void multidim_iterators_offset_clause(void) {
42+
#pragma omp task depend(iterator(i=5:6, j=10:11), in: z[i][j])
43+
{
44+
}
45+
}
46+
47+
// CHECK-LABEL: @multidim_iterators_clause25(
48+
// CHECK: [[VLA:%.*]] = alloca [[STRUCT_KMP_DEPEND_INFO:%.*]], i64 25, align 16
49+
// CHECK: = call i32 @__kmpc_omp_task_with_deps(ptr {{.*}}, i32 {{.*}}, ptr {{.*}}, i32 25, ptr {{.*}}, i32 0, ptr null)
50+
void multidim_iterators_clause25(void) {
51+
#pragma omp task depend(iterator(i=0:5, j=0:5), in: z[i][j])
52+
{
53+
}
54+
}
55+

clang/test/OpenMP/task_codegen.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ for (int i = 0; i < 10; ++i)
139139
// CHECK: [[EB_SUB_2_ADD_1_SUB:%.+]] = sub i32 [[EB_SUB_2_ADD]], 1
140140
// CHECK: [[EB_SUB_2_ADD_1_SUB_2_DIV:%.+]] = udiv i32 [[EB_SUB_2_ADD_1_SUB]], 2
141141
// CHECK: [[ELEMS:%.+]] = zext i32 [[EB_SUB_2_ADD_1_SUB_2_DIV]] to i64
142-
// CHECK: [[NELEMS:%.+]] = mul nuw i64 [[ELEMS]], 1
142+
// CHECK: [[ELEMS2:%.+]] = mul nuw i64 [[ELEMS]], 1
143+
// CHECK: [[NELEMS:%.+]] = mul nuw i64 [[ELEMS2]], 1
143144

144145
// ITERATOR_TOTAL = NELEMS + 0;
145146
// CHECK: [[ITERATOR_TOTAL:%.+]] = add nuw i64 0, [[NELEMS]]

0 commit comments

Comments
 (0)