Skip to content

Commit 83433d9

Browse files
authored
[OpenMP][IRBuilder] Handle target ... nowait when codegen targets host (#124720)
Fixes #124578 Handles the `nowait` clause for `omp.target` ops when the actual target is the host (i.e. there is no target device). Rather than only checking for the `HasNoWait` boolean, we also check for the presence/absence of a `DeviceID` value. We only emit the target task if both are present.
1 parent 1b551e7 commit 83433d9

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7258,10 +7258,12 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::emitTargetTask(
72587258
// If `HasNoWait == true`, we call @__kmpc_omp_target_task_alloc to provide
72597259
// the DeviceID to the deferred task and also since
72607260
// @__kmpc_omp_target_task_alloc creates an untied/async task.
7261+
bool NeedsTargetTask = HasNoWait && DeviceID;
72617262
Function *TaskAllocFn =
7262-
!HasNoWait ? getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_task_alloc)
7263-
: getOrCreateRuntimeFunctionPtr(
7264-
OMPRTL___kmpc_omp_target_task_alloc);
7263+
!NeedsTargetTask
7264+
? getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_task_alloc)
7265+
: getOrCreateRuntimeFunctionPtr(
7266+
OMPRTL___kmpc_omp_target_task_alloc);
72657267

72667268
// Arguments - `loc_ref` (Ident) and `gtid` (ThreadID)
72677269
// call.
@@ -7310,8 +7312,10 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::emitTargetTask(
73107312
/*sizeof_task=*/TaskSize, /*sizeof_shared=*/SharedsSize,
73117313
/*task_func=*/ProxyFn};
73127314

7313-
if (HasNoWait)
7315+
if (NeedsTargetTask) {
7316+
assert(DeviceID && "Expected non-empty device ID.");
73147317
TaskAllocArgs.push_back(DeviceID);
7318+
}
73157319

73167320
TaskData = Builder.CreateCall(TaskAllocFn, TaskAllocArgs);
73177321

@@ -7333,7 +7337,7 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::emitTargetTask(
73337337
// ---------------------------------------------------------------
73347338
// The above means that the lack of a nowait on the target construct
73357339
// translates to '#pragma omp task if(0)'
7336-
if (!HasNoWait) {
7340+
if (!NeedsTargetTask) {
73377341
if (DepArray) {
73387342
Function *TaskWaitFn =
73397343
getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_wait_deps);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
2+
3+
// Tests `target ... nowait` when code gen targets the host rather than a
4+
// device.
5+
6+
module attributes {omp.is_target_device = false} {
7+
llvm.func @omp_target_nowait_() {
8+
%0 = llvm.mlir.constant(1 : i64) : i64
9+
%1 = llvm.alloca %0 x f32 {bindc_name = "x"} : (i64) -> !llvm.ptr
10+
%3 = omp.map.info var_ptr(%1 : !llvm.ptr, f32) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr {name = "x"}
11+
omp.target nowait map_entries(%3 -> %arg0 : !llvm.ptr) {
12+
%4 = llvm.mlir.constant(5.000000e+00 : f32) : f32
13+
llvm.store %4, %arg0 : f32, !llvm.ptr
14+
omp.terminator
15+
}
16+
llvm.return
17+
}
18+
}
19+
20+
// CHECK: define void @omp_target_nowait_()
21+
// CHECK-NOT: define {{.*}} @
22+
// CHECK-NOT: call ptr @__kmpc_omp_target_task_alloc({{.*}})
23+
// Verify that we directly emit a call to the "target" region's body from the
24+
// parent function of the the `omp.target` op.
25+
// CHECK: call void @__omp_offloading_[[DEV:.*]]_[[FIL:.*]]_omp_target_nowait__l[[LINE:.*]](ptr {{.*}})
26+
// CHECK-NEXT: ret void
27+
28+
// CHECK: define internal void @__omp_offloading_[[DEV]]_[[FIL]]_omp_target_nowait__l[[LINE]](ptr %[[ADDR_X:.*]])
29+
// CHECK: store float 5{{.*}}, ptr %[[ADDR_X]], align 4

0 commit comments

Comments
 (0)