Skip to content

Commit 5c02f1a

Browse files
authored
[OpenMP][IRBuilder] De-duplicate code that emit task dependencies (#132340)
A small clean-up following up on #131795. Seems like we had 2 quite similar implementations for the same thing: emit task dependencies struct and filling it. This PR unifies the 2 versions into one. This is better since we had to fix a bug in one of them in #131795 so this applies the fix for both.
1 parent b1a8e6e commit 5c02f1a

File tree

2 files changed

+8
-44
lines changed

2 files changed

+8
-44
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,8 @@ static Value *emitTaskDependencies(
18441844
Type *DepArrayTy = ArrayType::get(DependInfo, Dependencies.size());
18451845
DepArray = Builder.CreateAlloca(DepArrayTy, nullptr, ".dep.arr.addr");
18461846

1847+
Builder.restoreIP(OldIP);
1848+
18471849
for (const auto &[DepIdx, Dep] : enumerate(Dependencies)) {
18481850
Value *Base =
18491851
Builder.CreateConstInBoundsGEP2_64(DepArrayTy, DepArray, 0, DepIdx);
@@ -1868,7 +1870,6 @@ static Value *emitTaskDependencies(
18681870
static_cast<unsigned int>(Dep.DepKind)),
18691871
Flags);
18701872
}
1871-
Builder.restoreIP(OldIP);
18721873
return DepArray;
18731874
}
18741875

@@ -2047,46 +2048,7 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTask(
20472048
Builder.CreateStore(Priority, CmplrData);
20482049
}
20492050

2050-
Value *DepArray = nullptr;
2051-
if (Dependencies.size()) {
2052-
InsertPointTy OldIP = Builder.saveIP();
2053-
Builder.SetInsertPoint(
2054-
&OldIP.getBlock()->getParent()->getEntryBlock().back());
2055-
2056-
Type *DepArrayTy = ArrayType::get(DependInfo, Dependencies.size());
2057-
DepArray = Builder.CreateAlloca(DepArrayTy, nullptr, ".dep.arr.addr");
2058-
2059-
Builder.restoreIP(OldIP);
2060-
2061-
unsigned P = 0;
2062-
for (const DependData &Dep : Dependencies) {
2063-
Value *Base =
2064-
Builder.CreateConstInBoundsGEP2_64(DepArrayTy, DepArray, 0, P);
2065-
// Store the pointer to the variable
2066-
Value *Addr = Builder.CreateStructGEP(
2067-
DependInfo, Base,
2068-
static_cast<unsigned int>(RTLDependInfoFields::BaseAddr));
2069-
Value *DepValPtr =
2070-
Builder.CreatePtrToInt(Dep.DepVal, Builder.getInt64Ty());
2071-
Builder.CreateStore(DepValPtr, Addr);
2072-
// Store the size of the variable
2073-
Value *Size = Builder.CreateStructGEP(
2074-
DependInfo, Base,
2075-
static_cast<unsigned int>(RTLDependInfoFields::Len));
2076-
Builder.CreateStore(Builder.getInt64(M.getDataLayout().getTypeStoreSize(
2077-
Dep.DepValueType)),
2078-
Size);
2079-
// Store the dependency kind
2080-
Value *Flags = Builder.CreateStructGEP(
2081-
DependInfo, Base,
2082-
static_cast<unsigned int>(RTLDependInfoFields::Flags));
2083-
Builder.CreateStore(
2084-
ConstantInt::get(Builder.getInt8Ty(),
2085-
static_cast<unsigned int>(Dep.DepKind)),
2086-
Flags);
2087-
++P;
2088-
}
2089-
}
2051+
Value *DepArray = emitTaskDependencies(*this, Dependencies);
20902052

20912053
// In the presence of the `if` clause, the following IR is generated:
20922054
// ...

mlir/test/Target/LLVMIR/omptarget-depend.mlir

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ module attributes {omp.is_target_device = false, omp.target_triples = ["amdgcn-a
124124
// CHECK: define void @_QQmain() {
125125
// CHECK-DAG: %[[STRUCTARG:.+]] = alloca { ptr, ptr, ptr }, align 8
126126
// CHECK-DAG: %[[DEP_ARRAY:.+]] = alloca [1 x %struct.kmp_dep_info], align 8
127+
128+
// CHECK: %[[TASKDATA:.+]] = call ptr @__kmpc_omp_task_alloc({{.+}}, ptr @.omp_target_task_proxy_func)
129+
// CHECK: %[[SHARED_DATA:.+]] = load ptr, ptr %[[TASKDATA]], align 8
130+
// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %[[SHARED_DATA]], ptr align 1 %[[STRUCTARG]], i64 24, i1 false)
131+
127132
// CHECK: %[[DEP_INFO:.+]] = getelementptr inbounds [1 x %struct.kmp_dep_info], ptr %[[DEP_ARRAY]], i64 0, i64 0
128133
// CHECK: %[[PTR0:.+]] = getelementptr inbounds nuw %struct.kmp_dep_info, ptr %[[DEP_INFO]], i32 0, i32 0
129134
// CHECK: store i64 ptrtoint (ptr @_QFEa to i64), ptr %[[PTR0]], align 4
@@ -132,9 +137,6 @@ module attributes {omp.is_target_device = false, omp.target_triples = ["amdgcn-a
132137
// CHECK: %[[PTR2:.+]] = getelementptr inbounds nuw %struct.kmp_dep_info, ptr %[[DEP_INFO]], i32 0, i32 2
133138
// CHECK: store i8 1, ptr %[[PTR2]], align 1
134139

135-
// CHECK: %[[TASKDATA:.+]] = call ptr @__kmpc_omp_task_alloc({{.+}}, ptr @.omp_target_task_proxy_func)
136-
// CHECK: %[[SHARED_DATA:.+]] = load ptr, ptr %[[TASKDATA]], align 8
137-
// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %[[SHARED_DATA]], ptr align 1 %[[STRUCTARG]], i64 24, i1 false)
138140
// CHECK: call void @__kmpc_omp_wait_deps({{.+}}, i32 1, ptr %[[DEP_ARRAY]], i32 0, ptr null)
139141
// CHECK: call void @__kmpc_omp_task_begin_if0({{.+}}, ptr %[[TASKDATA]])
140142
// CHECK: call void @.omp_target_task_proxy_func({{.+}}, ptr %[[TASKDATA]])

0 commit comments

Comments
 (0)