Skip to content

Commit bc82d1a

Browse files
authored
[OpenMPIRBuilder][MLIR] Pass target-cpu and target-features to outlined functions (#80283)
This patch adds support for forwarding the target-cpu and target-features attributes to functions outlined in the OpenMPIRBuilder. This, in turn, results in the addition of these attributes for functions created during the translation of the `omp.parallel`, `omp.task` and `omp.teams` operations, and for the `omp.wsloop` operation when doing codegen for an OpenMP target device.
1 parent 69ffa7b commit bc82d1a

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,15 @@ void OpenMPIRBuilder::finalize(Function *Fn) {
679679

680680
Function *OutlinedFn = Extractor.extractCodeRegion(CEAC);
681681

682+
// Forward target-cpu, target-features attributes to the outlined function.
683+
auto TargetCpuAttr = OuterFn->getFnAttribute("target-cpu");
684+
if (TargetCpuAttr.isStringAttribute())
685+
OutlinedFn->addFnAttr(TargetCpuAttr);
686+
687+
auto TargetFeaturesAttr = OuterFn->getFnAttribute("target-features");
688+
if (TargetFeaturesAttr.isStringAttribute())
689+
OutlinedFn->addFnAttr(TargetFeaturesAttr);
690+
682691
LLVM_DEBUG(dbgs() << "After outlining: " << *OuterFn << "\n");
683692
LLVM_DEBUG(dbgs() << " Outlined function: " << *OutlinedFn << "\n");
684693
assert(OutlinedFn->getReturnType()->isVoidTy() &&

mlir/test/Target/LLVMIR/omptarget-parallel-wsloop.mlir

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
// for nested omp do loop inside omp target region
55

66
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<"dlti.alloca_memory_space", 5 : ui32>>, llvm.data_layout = "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8", llvm.target_triple = "amdgcn-amd-amdhsa", omp.is_gpu = true, omp.is_target_device = true } {
7-
llvm.func @target_parallel_wsloop(%arg0: !llvm.ptr ){
7+
llvm.func @target_parallel_wsloop(%arg0: !llvm.ptr) attributes {
8+
target_cpu = "gfx90a",
9+
target_features = #llvm.target_features<["+gfx9-insts", "+wavefrontsize64"]>
10+
} {
811
omp.parallel {
912
%loop_ub = llvm.mlir.constant(9 : i32) : i32
1013
%loop_lb = llvm.mlir.constant(0 : i32) : i32
@@ -29,8 +32,16 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<"dlti.alloca_memo
2932
// CHECK: define internal void @[[PARALLEL_FUNC]]
3033
// CHECK-SAME: (ptr noalias noundef %[[TID_ADDR:.*]], ptr noalias noundef %[[ZERO_ADDR:.*]],
3134
// CHECK-SAME: ptr %[[ARG_PTR:.*]])
35+
// CHECK-SAME: #[[ATTRS1:[0-9]+]]
3236
// CHECK: call void @__kmpc_for_static_loop_4u(ptr addrspacecast (ptr addrspace(1) @[[GLOB]] to ptr),
3337
// CHECK-SAME: ptr @[[LOOP_BODY_FUNC:.*]], ptr %[[LOO_BODY_FUNC_ARG:.*]], i32 10,
3438
// CHECK-SAME: i32 %[[THREAD_NUM:.*]], i32 0)
3539

36-
// CHECK: define internal void @[[LOOP_BODY_FUNC]](i32 %[[CNT:.*]], ptr %[[LOOP_BODY_ARG_PTR:.*]]) {
40+
// CHECK: define internal void @[[LOOP_BODY_FUNC]](i32 %[[CNT:.*]], ptr %[[LOOP_BODY_ARG_PTR:.*]]) #[[ATTRS2:[0-9]+]] {
41+
42+
// CHECK: attributes #[[ATTRS2]] = {
43+
// CHECK-SAME: "target-cpu"="gfx90a"
44+
// CHECK-SAME: "target-features"="+gfx9-insts,+wavefrontsize64"
45+
// CHECK: attributes #[[ATTRS1]] = {
46+
// CHECK-SAME: "target-cpu"="gfx90a"
47+
// CHECK-SAME: "target-features"="+gfx9-insts,+wavefrontsize64"

mlir/test/Target/LLVMIR/openmp-llvm.mlir

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ llvm.func @test_omp_parallel_if_1(%arg0: i32) -> () {
179179

180180
// -----
181181

182+
// CHECK-LABEL: define void @test_omp_parallel_attrs()
183+
llvm.func @test_omp_parallel_attrs() -> () attributes {
184+
target_cpu = "x86-64",
185+
target_features = #llvm.target_features<["+mmx", "+sse"]>
186+
} {
187+
// CHECK: call void{{.*}}@__kmpc_fork_call{{.*}}@[[OMP_OUTLINED_FN:.*]])
188+
omp.parallel {
189+
omp.barrier
190+
omp.terminator
191+
}
192+
193+
llvm.return
194+
}
195+
196+
// CHECK: define {{.*}} @[[OMP_OUTLINED_FN]]{{.*}} #[[ATTRS:[0-9]+]]
197+
// CHECK: attributes #[[ATTRS]] = {
198+
// CHECK-SAME: "target-cpu"="x86-64"
199+
// CHECK-SAME: "target-features"="+mmx,+sse"
200+
201+
// -----
202+
182203
// CHECK-LABEL: define void @test_omp_parallel_3()
183204
llvm.func @test_omp_parallel_3() -> () {
184205
// CHECK: [[OMP_THREAD_3_1:%.*]] = call i32 @__kmpc_global_thread_num(ptr @{{[0-9]+}})
@@ -2238,6 +2259,28 @@ llvm.func @omp_task(%x: i32, %y: i32, %zaddr: !llvm.ptr) {
22382259

22392260
// -----
22402261

2262+
// CHECK-LABEL: define void @omp_task_attrs()
2263+
llvm.func @omp_task_attrs() -> () attributes {
2264+
target_cpu = "x86-64",
2265+
target_features = #llvm.target_features<["+mmx", "+sse"]>
2266+
} {
2267+
// CHECK: %[[task_data:.*]] = call {{.*}}@__kmpc_omp_task_alloc{{.*}}@[[outlined_fn:.*]])
2268+
// CHECK: call {{.*}}@__kmpc_omp_task(
2269+
// CHECK-SAME: ptr %[[task_data]]
2270+
omp.task {
2271+
omp.terminator
2272+
}
2273+
2274+
llvm.return
2275+
}
2276+
2277+
// CHECK: define {{.*}} @[[outlined_fn]]{{.*}} #[[attrs:[0-9]+]]
2278+
// CHECK: attributes #[[attrs]] = {
2279+
// CHECK-SAME: "target-cpu"="x86-64"
2280+
// CHECK-SAME: "target-features"="+mmx,+sse"
2281+
2282+
// -----
2283+
22412284
// CHECK-LABEL: define void @omp_task_with_deps
22422285
// CHECK-SAME: (ptr %[[zaddr:.+]])
22432286
// CHECK: %[[dep_arr_addr:.+]] = alloca [1 x %struct.kmp_dep_info], align 8

mlir/test/Target/LLVMIR/openmp-teams.mlir

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@ llvm.func @omp_teams_simple() {
1919

2020
// -----
2121

22+
llvm.func @foo()
23+
24+
// CHECK-LABEL: @omp_teams_func_attrs
25+
// CHECK: call void {{.*}} @__kmpc_fork_teams(ptr @{{.+}}, i32 0, ptr @[[OUTLINED_FN:.+]])
26+
llvm.func @omp_teams_func_attrs() attributes {
27+
target_cpu = "x86-64",
28+
target_features = #llvm.target_features<["+mmx", "+sse"]>
29+
} {
30+
omp.teams {
31+
llvm.call @foo() : () -> ()
32+
omp.terminator
33+
}
34+
llvm.return
35+
}
36+
37+
// CHECK: define internal void @[[OUTLINED_FN]](ptr {{.+}}, ptr {{.+}})
38+
// CHECK-SAME: #[[ATTR:[0-9]+]]
39+
// CHECK: call void @foo()
40+
41+
// CHECK: attributes #[[ATTR]] = {
42+
// CHECK-SAME: "target-cpu"="x86-64"
43+
// CHECK-SAME: "target-features"="+mmx,+sse"
44+
45+
// -----
46+
2247
llvm.func @foo(i32) -> ()
2348

2449
// CHECK-LABEL: @omp_teams_shared_simple

0 commit comments

Comments
 (0)