Skip to content

Commit 205b0bd

Browse files
authored
[OpenMP][IRBuilder] Handle target directives with both if & nowait (#125029)
This fixes a bug when a `target` directive has both an `if` and a `nowait` clauses. The bug happens because we tried to `emitKernelLaunch` for `else` branch of the `if` clause.
1 parent 9c0606a commit 205b0bd

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7438,10 +7438,15 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
74387438
// '@.omp_target_task_proxy_func' in the pseudo code above)
74397439
// "@.omp_target_task_proxy_func' is generated by
74407440
// emitTargetTaskProxyFunction.
7441-
if (OutlinedFnID)
7441+
if (OutlinedFnID && DeviceID)
74427442
return OMPBuilder.emitKernelLaunch(Builder, OutlinedFnID,
74437443
EmitTargetCallFallbackCB, KArgs,
74447444
DeviceID, RTLoc, TargetTaskAllocaIP);
7445+
7446+
// We only need to do the outlining if `DeviceID` is set to avoid calling
7447+
// `emitKernelLaunch` if we want to code-gen for the host; e.g. if we are
7448+
// generating the `else` branch of an `if` clause.
7449+
//
74457450
// When OutlinedFnID is set to nullptr, then it's not an offloading call.
74467451
// In this case, we execute the host implementation directly.
74477452
return EmitTargetCallFallbackCB(OMPBuilder.Builder.saveIP());
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
module attributes {omp.is_target_device = false, omp.target_triples = ["amdgcn-amd-amdhsa"]} {
4+
llvm.func @target_if_nowait(%arg0: !llvm.ptr, %arg1: !llvm.ptr) {
5+
%0 = llvm.mlir.constant(1 : i64) : i64
6+
%3 = llvm.alloca %0 x i32 {bindc_name = "cond"} : (i64) -> !llvm.ptr
7+
%6 = llvm.load %3 : !llvm.ptr -> i32
8+
%7 = llvm.mlir.constant(0 : i64) : i32
9+
%8 = llvm.icmp "ne" %6, %7 : i32
10+
%9 = omp.map.info var_ptr(%3 : !llvm.ptr, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr {name = "cond"}
11+
%10 = omp.map.info var_ptr(%arg0 : !llvm.ptr, f32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr {name = "var"}
12+
%11 = omp.map.info var_ptr(%arg1 : !llvm.ptr, f32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr {name = "val"}
13+
omp.target if(%8) nowait map_entries(%10 -> %arg3, %11 -> %arg4 : !llvm.ptr, !llvm.ptr) {
14+
%12 = llvm.load %arg4 : !llvm.ptr -> f32
15+
llvm.store %12, %arg3 : f32, !llvm.ptr
16+
omp.terminator
17+
}
18+
llvm.return
19+
}
20+
}
21+
22+
// CHECK: define void @target_if_nowait{{.*}} {
23+
// CHECK: omp_if.then:
24+
// CHECK: br label %[[TARGET_TASK_BB:.*]]
25+
26+
// CHECK: [[TARGET_TASK_BB]]:
27+
// CHECK: call ptr @__kmpc_omp_target_task_alloc
28+
// CHECK: br label %[[OFFLOAD_CONT:.*]]
29+
30+
// CHECK: [[OFFLOAD_CONT]]:
31+
// CHECK: br label %omp_if.end
32+
33+
// CHECK: omp_if.else:
34+
// CHECK: br label %[[HOST_TASK_BB:.*]]
35+
36+
// CHECK: [[HOST_TASK_BB]]:
37+
// CHECK: call ptr @__kmpc_omp_task_alloc
38+
// CHECK: br label %[[HOST_TASK_CONT:.*]]
39+
40+
// CHECK: [[HOST_TASK_CONT]]:
41+
// CHECK: br label %omp_if.end
42+
43+
// CHECK: omp_if.end:
44+
// CHECK: ret void
45+
// CHECK: }
46+

0 commit comments

Comments
 (0)