Skip to content

[flang][OpenMP] Support target ... nowait #111823

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
Oct 15, 2024
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
20 changes: 10 additions & 10 deletions llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2875,16 +2875,16 @@ class OpenMPIRBuilder {
/// instructions for passed in target arguments where neccessary
/// \param Dependencies A vector of DependData objects that carry
// dependency information as passed in the depend clause
InsertPointTy
createTarget(const LocationDescription &Loc, bool IsOffloadEntry,
OpenMPIRBuilder::InsertPointTy AllocaIP,
OpenMPIRBuilder::InsertPointTy CodeGenIP,
TargetRegionEntryInfo &EntryInfo, ArrayRef<int32_t> NumTeams,
ArrayRef<int32_t> NumThreads, SmallVectorImpl<Value *> &Inputs,
GenMapInfoCallbackTy GenMapInfoCB,
TargetBodyGenCallbackTy BodyGenCB,
TargetGenArgAccessorsCallbackTy ArgAccessorFuncCB,
SmallVector<DependData> Dependencies = {});
// \param HasNowait Whether the target construct has a `nowait` clause or not.
InsertPointTy createTarget(
const LocationDescription &Loc, bool IsOffloadEntry,
OpenMPIRBuilder::InsertPointTy AllocaIP,
OpenMPIRBuilder::InsertPointTy CodeGenIP,
TargetRegionEntryInfo &EntryInfo, ArrayRef<int32_t> NumTeams,
ArrayRef<int32_t> NumThreads, SmallVectorImpl<Value *> &Inputs,
GenMapInfoCallbackTy GenMapInfoCB, TargetBodyGenCallbackTy BodyGenCB,
TargetGenArgAccessorsCallbackTy ArgAccessorFuncCB,
SmallVector<DependData> Dependencies = {}, bool HasNowait = false);

/// Returns __kmpc_for_static_init_* runtime function for the specified
/// size \a IVSize and sign \a IVSigned. Will create a distribute call
Expand Down
50 changes: 32 additions & 18 deletions llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6968,8 +6968,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetTask(
}

OI.ExitBB = Builder.saveIP().getBlock();
OI.PostOutlineCB = [this, ToBeDeleted, Dependencies,
HasNoWait](Function &OutlinedFn) mutable {
OI.PostOutlineCB = [this, ToBeDeleted, Dependencies, HasNoWait,
DeviceID](Function &OutlinedFn) mutable {
assert(OutlinedFn.getNumUses() == 1 &&
"there must be a single user for the outlined function");

Expand All @@ -6989,9 +6989,15 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetTask(
getOrCreateSrcLocStr(LocationDescription(Builder), SrcLocStrSize);
Value *Ident = getOrCreateIdent(SrcLocStr, SrcLocStrSize);

// @__kmpc_omp_task_alloc
// @__kmpc_omp_task_alloc or @__kmpc_omp_target_task_alloc
//
// If `HasNoWait == true`, we call @__kmpc_omp_target_task_alloc to provide
// the DeviceID to the deferred task and also since
// @__kmpc_omp_target_task_alloc creates an untied/async task.
Function *TaskAllocFn =
getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_task_alloc);
!HasNoWait ? getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_task_alloc)
: getOrCreateRuntimeFunctionPtr(
OMPRTL___kmpc_omp_target_task_alloc);

// Arguments - `loc_ref` (Ident) and `gtid` (ThreadID)
// call.
Expand Down Expand Up @@ -7032,10 +7038,18 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetTask(
// Emit the @__kmpc_omp_task_alloc runtime call
// The runtime call returns a pointer to an area where the task captured
// variables must be copied before the task is run (TaskData)
CallInst *TaskData = Builder.CreateCall(
TaskAllocFn, {/*loc_ref=*/Ident, /*gtid=*/ThreadID, /*flags=*/Flags,
/*sizeof_task=*/TaskSize, /*sizeof_shared=*/SharedsSize,
/*task_func=*/ProxyFn});
CallInst *TaskData = nullptr;

SmallVector<llvm::Value *> TaskAllocArgs = {
/*loc_ref=*/Ident, /*gtid=*/ThreadID,
/*flags=*/Flags,
/*sizeof_task=*/TaskSize, /*sizeof_shared=*/SharedsSize,
/*task_func=*/ProxyFn};

if (HasNoWait)
TaskAllocArgs.push_back(DeviceID);

TaskData = Builder.CreateCall(TaskAllocFn, TaskAllocArgs);

if (HasShareds) {
Value *Shareds = StaleCI->getArgOperand(1);
Expand Down Expand Up @@ -7118,13 +7132,14 @@ void OpenMPIRBuilder::emitOffloadingArraysAndArgs(
emitOffloadingArraysArgument(Builder, RTArgs, Info, ForEndCall);
}

static void emitTargetCall(
OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
OpenMPIRBuilder::InsertPointTy AllocaIP, Function *OutlinedFn,
Constant *OutlinedFnID, ArrayRef<int32_t> NumTeams,
ArrayRef<int32_t> NumThreads, SmallVectorImpl<Value *> &Args,
OpenMPIRBuilder::GenMapInfoCallbackTy GenMapInfoCB,
SmallVector<llvm::OpenMPIRBuilder::DependData> Dependencies = {}) {
static void
emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
OpenMPIRBuilder::InsertPointTy AllocaIP, Function *OutlinedFn,
Constant *OutlinedFnID, ArrayRef<int32_t> NumTeams,
ArrayRef<int32_t> NumThreads, SmallVectorImpl<Value *> &Args,
OpenMPIRBuilder::GenMapInfoCallbackTy GenMapInfoCB,
SmallVector<llvm::OpenMPIRBuilder::DependData> Dependencies = {},
bool HasNoWait = false) {
// Generate a function call to the host fallback implementation of the target
// region. This is called by the host when no offload entry was generated for
// the target region and when the offloading call fails at runtime.
Expand All @@ -7135,7 +7150,6 @@ static void emitTargetCall(
return Builder.saveIP();
};

bool HasNoWait = false;
bool HasDependencies = Dependencies.size() > 0;
bool RequiresOuterTargetTask = HasNoWait || HasDependencies;

Expand Down Expand Up @@ -7211,7 +7225,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTarget(
SmallVectorImpl<Value *> &Args, GenMapInfoCallbackTy GenMapInfoCB,
OpenMPIRBuilder::TargetBodyGenCallbackTy CBFunc,
OpenMPIRBuilder::TargetGenArgAccessorsCallbackTy ArgAccessorFuncCB,
SmallVector<DependData> Dependencies) {
SmallVector<DependData> Dependencies, bool HasNowait) {

if (!updateToLocation(Loc))
return InsertPointTy();
Expand All @@ -7232,7 +7246,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTarget(
// that represents the target region. Do that now.
if (!Config.isTargetDevice())
emitTargetCall(*this, Builder, AllocaIP, OutlinedFn, OutlinedFnID, NumTeams,
NumThreads, Args, GenMapInfoCB, Dependencies);
NumThreads, Args, GenMapInfoCB, Dependencies, HasNowait);
return Builder.saveIP();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3242,11 +3242,6 @@ static bool targetOpSupported(Operation &opInst) {
return false;
}

if (targetOp.getNowait()) {
opInst.emitError("Nowait clause not yet supported");
return false;
}

if (!targetOp.getAllocateVars().empty() ||
!targetOp.getAllocatorVars().empty()) {
opInst.emitError("Allocate clause not yet supported");
Expand Down Expand Up @@ -3569,7 +3564,7 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
builder.restoreIP(moduleTranslation.getOpenMPBuilder()->createTarget(
ompLoc, isOffloadEntry, allocaIP, builder.saveIP(), entryInfo,
defaultValTeams, defaultValThreads, kernelInput, genMapInfoCB, bodyCB,
argAccessorCB, dds));
argAccessorCB, dds, targetOp.getNowait()));

// Remap access operations to declare target reference pointers for the
// device, essentially generating extra loadop's as necessary
Expand Down
54 changes: 23 additions & 31 deletions mlir/test/Target/LLVMIR/omptarget-nowait-llvm.mlir
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
// RUN: not mlir-translate -mlir-to-llvmir -split-input-file %s 2>&1 | FileCheck %s
// RUN: mlir-translate -mlir-to-llvmir %s 2>&1 | FileCheck %s

llvm.func @_QPopenmp_target_data_update() {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i32 {bindc_name = "i", in_type = i32, operand_segment_sizes = array<i32: 0, 0>, uniq_name = "_QFopenmp_target_dataEi"} : (i64) -> !llvm.ptr
%2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(to) capture(ByRef) -> !llvm.ptr {name = ""}
// Set a dummy target triple to enable target region outlining.
module attributes {omp.target_triples = ["dummy-target-triple"]} {
llvm.func @_QPfoo() {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr
%2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(implicit) capture(ByCopy) -> !llvm.ptr
omp.target nowait map_entries(%2 -> %arg0 : !llvm.ptr) {
%3 = llvm.mlir.constant(2 : i32) : i32
llvm.store %3, %arg0 : i32, !llvm.ptr
omp.terminator
}
llvm.return
}

// CHECK: error: `nowait` is not supported yet
omp.target_update map_entries(%2 : !llvm.ptr) nowait

llvm.return
}

// -----

llvm.func @_QPopenmp_target_data_enter() {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i32 {bindc_name = "i", in_type = i32, operand_segment_sizes = array<i32: 0, 0>, uniq_name = "_QFopenmp_target_dataEi"} : (i64) -> !llvm.ptr
%2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(to) capture(ByRef) -> !llvm.ptr {name = ""}

// CHECK: error: `nowait` is not supported yet
omp.target_enter_data map_entries(%2 : !llvm.ptr) nowait

llvm.return
}

// CHECK: define void @_QPfoo() {

// -----
// CHECK: %[[TASK:.*]] = call ptr @__kmpc_omp_target_task_alloc
// CHECK-SAME: (ptr @{{.*}}, i32 %{{.*}}, i32 {{.*}}, i64 {{.*}}, i64 {{.*}}, ptr
// CHECK-SAME: @[[TASK_PROXY_FUNC:.*]], i64 {{.*}})

llvm.func @_QPopenmp_target_data_exit() {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i32 {bindc_name = "i", in_type = i32, operand_segment_sizes = array<i32: 0, 0>, uniq_name = "_QFopenmp_target_dataEi"} : (i64) -> !llvm.ptr
%2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(from) capture(ByRef) -> !llvm.ptr {name = ""}
// CHECK: call i32 @__kmpc_omp_task(ptr {{.*}}, i32 %{{.*}}, ptr %[[TASK]])
// CHECK: }

// CHECK: error: `nowait` is not supported yet
omp.target_exit_data map_entries(%2 : !llvm.ptr) nowait

llvm.return
// CHECK: define internal void @[[TASK_PROXY_FUNC]](i32 %{{.*}}, ptr %{{.*}}) {
// CHECK: call void @_QPfoo..omp_par(i32 %{{.*}}, ptr %{{.*}})
// CHECK: }
}
39 changes: 39 additions & 0 deletions mlir/test/Target/LLVMIR/omptarget-nowait-unsupported-llvm.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: not mlir-translate -mlir-to-llvmir -split-input-file %s 2>&1 | FileCheck %s

llvm.func @_QPopenmp_target_data_update() {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i32 {bindc_name = "i", in_type = i32, operand_segment_sizes = array<i32: 0, 0>, uniq_name = "_QFopenmp_target_dataEi"} : (i64) -> !llvm.ptr
%2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(to) capture(ByRef) -> !llvm.ptr {name = ""}

// CHECK: error: `nowait` is not supported yet
omp.target_update map_entries(%2 : !llvm.ptr) nowait

llvm.return
}

// -----

llvm.func @_QPopenmp_target_data_enter() {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i32 {bindc_name = "i", in_type = i32, operand_segment_sizes = array<i32: 0, 0>, uniq_name = "_QFopenmp_target_dataEi"} : (i64) -> !llvm.ptr
%2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(to) capture(ByRef) -> !llvm.ptr {name = ""}

// CHECK: error: `nowait` is not supported yet
omp.target_enter_data map_entries(%2 : !llvm.ptr) nowait

llvm.return
}


// -----

llvm.func @_QPopenmp_target_data_exit() {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i32 {bindc_name = "i", in_type = i32, operand_segment_sizes = array<i32: 0, 0>, uniq_name = "_QFopenmp_target_dataEi"} : (i64) -> !llvm.ptr
%2 = omp.map.info var_ptr(%1 : !llvm.ptr, i32) map_clauses(from) capture(ByRef) -> !llvm.ptr {name = ""}

// CHECK: error: `nowait` is not supported yet
omp.target_exit_data map_entries(%2 : !llvm.ptr) nowait

llvm.return
}
Loading