Skip to content

Commit 76a8950

Browse files
committed
[SILGen] Handle indirect errors in other thunk kinds
1 parent fc19b8e commit 76a8950

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

lib/SILGen/SILGenBackDeploy.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,18 @@ void SILGenFunction::emitBackDeploymentThunk(SILDeclRef thunk) {
221221
// Generate the thunk prolog by collecting parameters.
222222
SmallVector<ManagedValue, 4> params;
223223
SmallVector<ManagedValue, 4> indirectParams;
224-
collectThunkParams(loc, params, &indirectParams);
224+
SmallVector<ManagedValue, 4> indirectErrorResults;
225+
collectThunkParams(loc, params, &indirectParams, &indirectErrorResults);
225226

226227
// Build up the list of arguments that we're going to invoke the the real
227228
// function with.
228229
SmallVector<SILValue, 8> paramsForForwarding;
229230
for (auto indirectParam : indirectParams) {
230231
paramsForForwarding.emplace_back(indirectParam.getLValueAddress());
231232
}
233+
for (auto indirectErrorResult : indirectErrorResults) {
234+
paramsForForwarding.emplace_back(indirectErrorResult.getLValueAddress());
235+
}
232236

233237
for (auto param : params) {
234238
// We're going to directly call either the original function or the fallback

lib/SILGen/SILGenPoly.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5188,7 +5188,8 @@ static void buildWithoutActuallyEscapingThunkBody(SILGenFunction &SGF,
51885188

51895189
SmallVector<ManagedValue, 8> params;
51905190
SmallVector<ManagedValue, 8> indirectResults;
5191-
SGF.collectThunkParams(loc, params, &indirectResults);
5191+
SmallVector<ManagedValue, 1> indirectErrorResults;
5192+
SGF.collectThunkParams(loc, params, &indirectResults, &indirectErrorResults);
51925193

51935194
// Ignore the self parameter at the SIL level. IRGen will use it to
51945195
// recover type metadata.
@@ -5198,13 +5199,16 @@ static void buildWithoutActuallyEscapingThunkBody(SILGenFunction &SGF,
51985199
ManagedValue fnValue = params.pop_back_val();
51995200
auto fnType = fnValue.getType().castTo<SILFunctionType>();
52005201

5202+
// Forward indirect result arguments.
52015203
SmallVector<SILValue, 8> argValues;
52025204
if (!indirectResults.empty()) {
52035205
for (auto result : indirectResults)
52045206
argValues.push_back(result.getLValueAddress());
52055207
}
52065208

5207-
// Forward indirect result arguments.
5209+
// Forward indirect error arguments.
5210+
for (auto indirectError : indirectErrorResults)
5211+
argValues.push_back(indirectError.getLValueAddress());
52085212

52095213
// Add the rest of the arguments.
52105214
forwardFunctionArguments(SGF, loc, fnType, params, argValues);
@@ -5389,14 +5393,18 @@ ManagedValue SILGenFunction::getThunkedAutoDiffLinearMap(
53895393
SILGenFunction thunkSGF(SGM, *thunk, FunctionDC);
53905394
SmallVector<ManagedValue, 4> params;
53915395
SmallVector<ManagedValue, 4> thunkIndirectResults;
5392-
thunkSGF.collectThunkParams(loc, params, &thunkIndirectResults);
5396+
SmallVector<ManagedValue, 4> thunkIndirectErrorResults;
5397+
thunkSGF.collectThunkParams(
5398+
loc, params, &thunkIndirectResults, &thunkIndirectErrorResults);
53935399

53945400
SILFunctionConventions fromConv(fromType, getModule());
53955401
SILFunctionConventions toConv(toType, getModule());
53965402
if (!toConv.useLoweredAddresses()) {
53975403
SmallVector<ManagedValue, 4> thunkArguments;
53985404
for (auto indRes : thunkIndirectResults)
53995405
thunkArguments.push_back(indRes);
5406+
for (auto indErrRes : thunkIndirectErrorResults)
5407+
thunkArguments.push_back(indErrRes);
54005408
thunkArguments.append(params.begin(), params.end());
54015409
SmallVector<SILParameterInfo, 4> toParameters(
54025410
toConv.getParameters().begin(), toConv.getParameters().end());
@@ -5405,7 +5413,8 @@ ManagedValue SILGenFunction::getThunkedAutoDiffLinearMap(
54055413
// Handle self reordering.
54065414
// - For pullbacks: reorder result infos.
54075415
// - For differentials: reorder parameter infos and arguments.
5408-
auto numIndirectResults = thunkIndirectResults.size();
5416+
auto numIndirectResults =
5417+
thunkIndirectResults.size() + thunkIndirectErrorResults.size();
54095418
if (reorderSelf && linearMapKind == AutoDiffLinearMapKind::Pullback &&
54105419
toResults.size() > 1) {
54115420
std::rotate(toResults.begin(), toResults.end() - 1, toResults.end());
@@ -5477,6 +5486,8 @@ ManagedValue SILGenFunction::getThunkedAutoDiffLinearMap(
54775486
SmallVector<ManagedValue, 4> thunkArguments;
54785487
thunkArguments.append(thunkIndirectResults.begin(),
54795488
thunkIndirectResults.end());
5489+
thunkArguments.append(thunkIndirectErrorResults.begin(),
5490+
thunkIndirectErrorResults.end());
54805491
thunkArguments.append(params.begin(), params.end());
54815492
SmallVector<SILParameterInfo, 4> toParameters(toConv.getParameters().begin(),
54825493
toConv.getParameters().end());
@@ -5723,7 +5734,9 @@ SILFunction *SILGenModule::getOrCreateCustomDerivativeThunk(
57235734
SILGenFunction thunkSGF(*this, *thunk, customDerivativeFn->getDeclContext());
57245735
SmallVector<ManagedValue, 4> params;
57255736
SmallVector<ManagedValue, 4> indirectResults;
5726-
thunkSGF.collectThunkParams(loc, params, &indirectResults);
5737+
SmallVector<ManagedValue, 1> indirectErrorResults;
5738+
thunkSGF.collectThunkParams(
5739+
loc, params, &indirectResults, &indirectErrorResults);
57275740

57285741
auto *fnRef = thunkSGF.B.createFunctionRef(loc, customDerivativeFn);
57295742
auto fnRefType =
@@ -5767,6 +5780,8 @@ SILFunction *SILGenModule::getOrCreateCustomDerivativeThunk(
57675780
SmallVector<SILValue, 8> arguments;
57685781
for (auto indRes : indirectResults)
57695782
arguments.push_back(indRes.getLValueAddress());
5783+
for (auto indErrorRes : indirectErrorResults)
5784+
arguments.push_back(indErrorRes.getLValueAddress());
57705785
forwardFunctionArguments(thunkSGF, loc, fnRefType, params, arguments);
57715786

57725787
// Apply function argument.
@@ -6200,6 +6215,13 @@ SILGenFunction::emitVTableThunk(SILDeclRef base,
62006215
inputOrigType.getFunctionResultType(),
62016216
inputSubstType.getResult(),
62026217
derivedFTy, thunkTy);
6218+
6219+
// If the function we're calling has as indirect error result, create an
6220+
// argument for it.
6221+
if (auto innerIndirectErrorAddr =
6222+
emitThunkIndirectErrorArgument(*this, loc, derivedFTy)) {
6223+
args.push_back(*innerIndirectErrorAddr);
6224+
}
62036225
}
62046226

62056227
// Then, the arguments.
@@ -6584,13 +6606,13 @@ void SILGenFunction::emitProtocolWitness(
65846606
reqtOrigTy.getFunctionResultType(),
65856607
reqtSubstTy.getResult(),
65866608
witnessFTy, thunkTy);
6587-
}
65886609

6589-
// If the function we're calling has as indirect error result, create an
6590-
// argument for it.
6591-
if (auto innerIndirectErrorAddr =
6592-
emitThunkIndirectErrorArgument(*this, loc, witnessFTy)) {
6593-
args.push_back(*innerIndirectErrorAddr);
6610+
// If the function we're calling has as indirect error result, create an
6611+
// argument for it.
6612+
if (auto innerIndirectErrorAddr =
6613+
emitThunkIndirectErrorArgument(*this, loc, witnessFTy)) {
6614+
args.push_back(*innerIndirectErrorAddr);
6615+
}
65946616
}
65956617

65966618
// - the rest of the arguments

0 commit comments

Comments
 (0)