Skip to content

Commit

Permalink
[OMPIRBuilder] Simplify error handling while emitting target calls, NFC
Browse files Browse the repository at this point in the history
The OMPIRBuilder uses `llvm::Error`s to allow callbacks passed to it to signal
errors and prevent OMPIRBuilder functions to continue after one has been
triggered. This means that OMPIRBuilder functions taking callbacks needs to be
able to forward these errors, which must always be checked.

However, in cases where these functions are called from within the OMPIRBuilder
with callbacks also defined inside of it, it can be known in advance that no
errors will be produced. This is the case of those defined in `emitTargetCall`.

This patch introduces calls to the `cantFail` function instead of the previous
superfluous checks that still assumed calls wouldn't fail, making these
assumptions more obvious and simplifying their implementation.
  • Loading branch information
skatrak committed Jan 10, 2025
1 parent 85ca551 commit cef1269
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7331,7 +7331,9 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
auto TaskBodyCB =
[&](Value *DeviceID, Value *RTLoc,
IRBuilderBase::InsertPoint TargetTaskAllocaIP) -> Error {
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = [&]() {
// Assume no error was returned because EmitTargetCallFallbackCB doesn't
// produce any.
llvm::OpenMPIRBuilder::InsertPointTy AfterIP = cantFail([&]() {
// emitKernelLaunch makes the necessary runtime call to offload the
// kernel. We then outline all that code into a separate function
// ('kernel_launch_function' in the pseudo code above). This function is
Expand All @@ -7346,19 +7348,18 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
// 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());
}();

if (!AfterIP)
return AfterIP.takeError();
}());

OMPBuilder.Builder.restoreIP(*AfterIP);
OMPBuilder.Builder.restoreIP(AfterIP);
return Error::success();
};

// If we don't have an ID for the target region, it means an offload entry
// wasn't created. In this case we just run the host fallback directly.
if (!OutlinedFnID) {
OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = [&]() {
// Assume no error was returned because EmitTargetCallFallbackCB doesn't
// produce any.
OpenMPIRBuilder::InsertPointTy AfterIP = cantFail([&]() {
if (RequiresOuterTargetTask) {
// Arguments that are intended to be directly forwarded to an
// emitKernelLaunch call are pased as nullptr, since
Expand All @@ -7368,12 +7369,9 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
Dependencies, HasNoWait);
}
return EmitTargetCallFallbackCB(Builder.saveIP());
}();
}());

// Assume no error was returned because EmitTargetCallFallbackCB doesn't
// produce any. The 'if' check enables accessing the returned value.
if (AfterIP)
Builder.restoreIP(*AfterIP);
Builder.restoreIP(AfterIP);
return;
}

Expand Down Expand Up @@ -7411,23 +7409,21 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
NumTargetItems, RTArgs, NumIterations, NumTeamsC, NumThreadsC,
DynCGGroupMem, HasNoWait);

// The presence of certain clauses on the target directive require the
// explicit generation of the target task.
OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = [&]() {
// Assume no error was returned because TaskBodyCB and
// EmitTargetCallFallbackCB don't produce any.
OpenMPIRBuilder::InsertPointTy AfterIP = cantFail([&]() {
// The presence of certain clauses on the target directive require the
// explicit generation of the target task.
if (RequiresOuterTargetTask)
return OMPBuilder.emitTargetTask(TaskBodyCB, DeviceID, RTLoc, AllocaIP,
Dependencies, HasNoWait);

return OMPBuilder.emitKernelLaunch(Builder, OutlinedFnID,
EmitTargetCallFallbackCB, KArgs,
DeviceID, RTLoc, AllocaIP);
}();
}());

// Assume no error was returned because TaskBodyCB and
// EmitTargetCallFallbackCB don't produce any. The 'if' check enables
// accessing the returned value.
if (AfterIP)
Builder.restoreIP(*AfterIP);
Builder.restoreIP(AfterIP);
}

OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTarget(
Expand Down

0 comments on commit cef1269

Please sign in to comment.