Skip to content

[OpenMP][IRBuilder] Handle target directives with both if & nowait #125029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7438,10 +7438,15 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
// '@.omp_target_task_proxy_func' in the pseudo code above)
// "@.omp_target_task_proxy_func' is generated by
// emitTargetTaskProxyFunction.
if (OutlinedFnID)
if (OutlinedFnID && DeviceID)
return OMPBuilder.emitKernelLaunch(Builder, OutlinedFnID,
EmitTargetCallFallbackCB, KArgs,
DeviceID, RTLoc, TargetTaskAllocaIP);

// We only need to do the outlining if `DeviceID` is set to avoid calling
// `emitKernelLaunch` if we want to code-gen for the host; e.g. if we are
// generating the `else` branch of an `if` clause.
//
// When OutlinedFnID is set to nullptr, then it's not an offloading call.
// In this case, we execute the host implementation directly.
return EmitTargetCallFallbackCB(OMPBuilder.Builder.saveIP());
Expand Down
46 changes: 46 additions & 0 deletions mlir/test/Target/LLVMIR/omptarget-if-nowait.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s

module attributes {omp.is_target_device = false, omp.target_triples = ["amdgcn-amd-amdhsa"]} {
llvm.func @target_if_nowait(%arg0: !llvm.ptr, %arg1: !llvm.ptr) {
%0 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %0 x i32 {bindc_name = "cond"} : (i64) -> !llvm.ptr
%6 = llvm.load %3 : !llvm.ptr -> i32
%7 = llvm.mlir.constant(0 : i64) : i32
%8 = llvm.icmp "ne" %6, %7 : i32
%9 = omp.map.info var_ptr(%3 : !llvm.ptr, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr {name = "cond"}
%10 = omp.map.info var_ptr(%arg0 : !llvm.ptr, f32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr {name = "var"}
%11 = omp.map.info var_ptr(%arg1 : !llvm.ptr, f32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr {name = "val"}
omp.target if(%8) nowait map_entries(%10 -> %arg3, %11 -> %arg4 : !llvm.ptr, !llvm.ptr) {
%12 = llvm.load %arg4 : !llvm.ptr -> f32
llvm.store %12, %arg3 : f32, !llvm.ptr
omp.terminator
}
llvm.return
}
}

// CHECK: define void @target_if_nowait{{.*}} {
// CHECK: omp_if.then:
// CHECK: br label %[[TARGET_TASK_BB:.*]]

// CHECK: [[TARGET_TASK_BB]]:
// CHECK: call ptr @__kmpc_omp_target_task_alloc
// CHECK: br label %[[OFFLOAD_CONT:.*]]

// CHECK: [[OFFLOAD_CONT]]:
// CHECK: br label %omp_if.end

// CHECK: omp_if.else:
// CHECK: br label %[[HOST_TASK_BB:.*]]

// CHECK: [[HOST_TASK_BB]]:
// CHECK: call ptr @__kmpc_omp_task_alloc
// CHECK: br label %[[HOST_TASK_CONT:.*]]

// CHECK: [[HOST_TASK_CONT]]:
// CHECK: br label %omp_if.end

// CHECK: omp_if.end:
// CHECK: ret void
// CHECK: }