Skip to content

Commit 0fb0ac7

Browse files
authored
[OMPIRBuilder] Simplify error handling while emitting target calls, NFC (#122477)
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.
1 parent f1b1c7f commit 0fb0ac7

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7363,7 +7363,9 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
73637363
auto TaskBodyCB =
73647364
[&](Value *DeviceID, Value *RTLoc,
73657365
IRBuilderBase::InsertPoint TargetTaskAllocaIP) -> Error {
7366-
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = [&]() {
7366+
// Assume no error was returned because EmitTargetCallFallbackCB doesn't
7367+
// produce any.
7368+
llvm::OpenMPIRBuilder::InsertPointTy AfterIP = cantFail([&]() {
73677369
// emitKernelLaunch makes the necessary runtime call to offload the
73687370
// kernel. We then outline all that code into a separate function
73697371
// ('kernel_launch_function' in the pseudo code above). This function is
@@ -7378,19 +7380,18 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
73787380
// When OutlinedFnID is set to nullptr, then it's not an offloading call.
73797381
// In this case, we execute the host implementation directly.
73807382
return EmitTargetCallFallbackCB(OMPBuilder.Builder.saveIP());
7381-
}();
7382-
7383-
if (!AfterIP)
7384-
return AfterIP.takeError();
7383+
}());
73857384

7386-
OMPBuilder.Builder.restoreIP(*AfterIP);
7385+
OMPBuilder.Builder.restoreIP(AfterIP);
73877386
return Error::success();
73887387
};
73897388

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

7405-
// Assume no error was returned because EmitTargetCallFallbackCB doesn't
7406-
// produce any. The 'if' check enables accessing the returned value.
7407-
if (AfterIP)
7408-
Builder.restoreIP(*AfterIP);
7406+
Builder.restoreIP(AfterIP);
74097407
return;
74107408
}
74117409

@@ -7480,23 +7478,21 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
74807478
NumTeamsC, NumThreadsC,
74817479
DynCGGroupMem, HasNoWait);
74827480

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

74907490
return OMPBuilder.emitKernelLaunch(Builder, OutlinedFnID,
74917491
EmitTargetCallFallbackCB, KArgs,
74927492
DeviceID, RTLoc, AllocaIP);
7493-
}();
7493+
}());
74947494

7495-
// Assume no error was returned because TaskBodyCB and
7496-
// EmitTargetCallFallbackCB don't produce any. The 'if' check enables
7497-
// accessing the returned value.
7498-
if (AfterIP)
7499-
Builder.restoreIP(*AfterIP);
7495+
Builder.restoreIP(AfterIP);
75007496
}
75017497

75027498
OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTarget(

0 commit comments

Comments
 (0)