Skip to content

Commit 731da3b

Browse files
committed
[NFC] Improve some SILGen functions for working with begin_apply.
1 parent 4504065 commit 731da3b

File tree

4 files changed

+66
-51
lines changed

4 files changed

+66
-51
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4344,30 +4344,41 @@ CallEmission::applyCoroutine(SmallVectorImpl<ManagedValue> &yields) {
43444344

43454345
auto fnValue = callee.getFnValue(SGF, isCurried, borrowedSelf);
43464346

4347-
// Emit the uncurried call.
4347+
return SGF.emitBeginApply(uncurriedLoc.getValue(), fnValue,
4348+
callee.getSubstitutions(), uncurriedArgs,
4349+
calleeTypeInfo.substFnType, options, yields);
4350+
}
4351+
4352+
CleanupHandle
4353+
SILGenFunction::emitBeginApply(SILLocation loc, ManagedValue fn,
4354+
SubstitutionMap subs,
4355+
ArrayRef<ManagedValue> args,
4356+
CanSILFunctionType substFnType,
4357+
ApplyOptions options,
4358+
SmallVectorImpl<ManagedValue> &yields) {
4359+
// Emit the call.
43484360
SmallVector<SILValue, 4> rawResults;
4349-
emitRawApply(SGF, uncurriedLoc.getValue(), fnValue, callee.getSubstitutions(),
4350-
uncurriedArgs, calleeTypeInfo.substFnType, options,
4361+
emitRawApply(*this, loc, fn, subs, args, substFnType, options,
43514362
/*indirect results*/ {}, rawResults);
43524363

43534364
auto token = rawResults.pop_back_val();
43544365
auto yieldValues = llvm::makeArrayRef(rawResults);
43554366

43564367
// Push a cleanup to end the application.
43574368
// TODO: destroy all the arguments at exactly this point?
4358-
SGF.Cleanups.pushCleanup<EndCoroutineApply>(token);
4359-
auto endApplyHandle = SGF.getTopCleanup();
4369+
Cleanups.pushCleanup<EndCoroutineApply>(token);
4370+
auto endApplyHandle = getTopCleanup();
43604371

43614372
// Manage all the yielded values.
4362-
auto yieldInfos = calleeTypeInfo.substFnType->getYields();
4373+
auto yieldInfos = substFnType->getYields();
43634374
assert(yieldValues.size() == yieldInfos.size());
43644375
for (auto i : indices(yieldValues)) {
43654376
auto value = yieldValues[i];
43664377
auto info = yieldInfos[i];
43674378
if (info.isIndirectInOut()) {
43684379
yields.push_back(ManagedValue::forLValue(value));
43694380
} else if (info.isConsumed()) {
4370-
yields.push_back(SGF.emitManagedRValueWithCleanup(value));
4381+
yields.push_back(emitManagedRValueWithCleanup(value));
43714382
} else if (info.isDirectGuaranteed()) {
43724383
yields.push_back(ManagedValue::forBorrowedRValue(value));
43734384
} else {
@@ -5115,11 +5126,12 @@ SILValue SILGenFunction::emitApplyWithRethrow(SILLocation loc, SILValue fn,
51155126
return normalBB->createPhiArgument(resultType, ValueOwnershipKind::Owned);
51165127
}
51175128

5118-
SILValue SILGenFunction::emitBeginApplyWithRethrow(SILLocation loc, SILValue fn,
5119-
SILType substFnType,
5120-
SubstitutionMap subs,
5121-
ArrayRef<SILValue> args,
5122-
SmallVectorImpl<SILValue> &yields) {
5129+
std::pair<SILValue, CleanupHandle>
5130+
SILGenFunction::emitBeginApplyWithRethrow(SILLocation loc, SILValue fn,
5131+
SILType substFnType,
5132+
SubstitutionMap subs,
5133+
ArrayRef<SILValue> args,
5134+
SmallVectorImpl<SILValue> &yields) {
51235135
// TODO: adjust this to create try_begin_apply when appropriate.
51245136
assert(!substFnType.castTo<SILFunctionType>()->hasErrorResult());
51255137

@@ -5128,7 +5140,12 @@ SILValue SILGenFunction::emitBeginApplyWithRethrow(SILLocation loc, SILValue fn,
51285140
auto yieldResults = beginApply->getYieldedValues();
51295141
yields.append(yieldResults.begin(), yieldResults.end());
51305142

5131-
return beginApply->getTokenResult();
5143+
auto token = beginApply->getTokenResult();
5144+
5145+
Cleanups.pushCleanup<EndCoroutineApply>(token);
5146+
auto abortCleanup = Cleanups.getTopCleanup();
5147+
5148+
return { token, abortCleanup };
51325149
}
51335150

51345151
void SILGenFunction::emitEndApplyWithRethrow(SILLocation loc, SILValue token) {

lib/SILGen/SILGenFunction.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,16 +1471,21 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
14711471
ArrayRef<ManagedValue> args,
14721472
SGFContext ctx);
14731473

1474+
CleanupHandle emitBeginApply(SILLocation loc, ManagedValue fn,
1475+
SubstitutionMap subs, ArrayRef<ManagedValue> args,
1476+
CanSILFunctionType substFnType,
1477+
ApplyOptions options,
1478+
SmallVectorImpl<ManagedValue> &yields);
1479+
14741480
SILValue emitApplyWithRethrow(SILLocation loc, SILValue fn,
14751481
SILType substFnType,
14761482
SubstitutionMap subs,
14771483
ArrayRef<SILValue> args);
14781484

1479-
SILValue emitBeginApplyWithRethrow(SILLocation loc, SILValue fn,
1480-
SILType substFnType,
1481-
SubstitutionMap subs,
1482-
ArrayRef<SILValue> args,
1483-
SmallVectorImpl<SILValue> &yields);
1485+
std::pair<SILValue, CleanupHandle>
1486+
emitBeginApplyWithRethrow(SILLocation loc, SILValue fn, SILType substFnType,
1487+
SubstitutionMap subs, ArrayRef<SILValue> args,
1488+
SmallVectorImpl<SILValue> &yields);
14841489
void emitEndApplyWithRethrow(SILLocation loc, SILValue token);
14851490

14861491
/// Emit a literal that applies the various initializers.

lib/SILGen/SILGenLValue.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,26 @@ namespace {
16021602
IndexExprForDiagnostics};
16031603
}
16041604
};
1605+
}
1606+
1607+
static void pushEndApplyWriteback(SILGenFunction &SGF, SILLocation loc,
1608+
CleanupHandle endApplyHandle,
1609+
LValueTypeData typeData,
1610+
ManagedValue base = ManagedValue(),
1611+
AbstractStorageDecl *storage = nullptr,
1612+
bool isSuper = false,
1613+
PreparedArguments &&indices
1614+
= PreparedArguments(),
1615+
Expr *indexExprForDiagnostics = nullptr) {
1616+
std::unique_ptr<LogicalPathComponent>
1617+
component(new EndApplyPseudoComponent(typeData, endApplyHandle,
1618+
storage, isSuper, std::move(indices),
1619+
indexExprForDiagnostics));
1620+
pushWriteback(SGF, loc, std::move(component), /*for diagnostics*/ base,
1621+
MaterializedLValue());
1622+
}
1623+
1624+
namespace {
16051625

16061626
/// A physical component which involves calling coroutine accessors.
16071627
class CoroutineAccessorComponent
@@ -1638,13 +1658,9 @@ namespace {
16381658
IsOnSelfParameter);
16391659

16401660
// Push a writeback that ends the access.
1641-
std::unique_ptr<LogicalPathComponent>
1642-
component(new EndApplyPseudoComponent(getTypeData(), endApplyHandle,
1643-
Storage, IsSuper,
1644-
std::move(peekedIndices),
1645-
IndexExprForDiagnostics));
1646-
pushWriteback(SGF, loc, std::move(component), /*for diagnostics*/ base,
1647-
MaterializedLValue());
1661+
pushEndApplyWriteback(SGF, loc, endApplyHandle, getTypeData(),
1662+
base, Storage, IsSuper, std::move(peekedIndices),
1663+
IndexExprForDiagnostics);
16481664

16491665
auto decl = cast<AccessorDecl>(Accessor.getFuncDecl());
16501666

lib/SILGen/SILGenPoly.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3659,25 +3659,6 @@ getWitnessFunctionRef(SILGenFunction &SGF,
36593659
llvm_unreachable("Unhandled WitnessDispatchKind in switch.");
36603660
}
36613661

3662-
namespace {
3663-
class EmitAbortApply : public Cleanup {
3664-
SILValue Token;
3665-
public:
3666-
EmitAbortApply(SILValue token) : Token(token) {}
3667-
void emit(SILGenFunction &SGF, CleanupLocation loc,
3668-
ForUnwind_t forUnwind) override {
3669-
SGF.B.createAbortApply(loc, Token);
3670-
}
3671-
void dump(SILGenFunction &SGF) const override {
3672-
#ifndef NDEBUG
3673-
llvm::errs() << "EmitAbortApply\n"
3674-
<< "State:" << getState() << "\n"
3675-
<< "Token:" << Token << "\n";
3676-
#endif
3677-
}
3678-
};
3679-
}
3680-
36813662
void SILGenFunction::emitProtocolWitness(AbstractionPattern reqtOrigTy,
36823663
CanAnyFunctionType reqtSubstTy,
36833664
SILDeclRef requirement,
@@ -3793,25 +3774,21 @@ void SILGenFunction::emitProtocolWitness(AbstractionPattern reqtOrigTy,
37933774

37943775
case SILCoroutineKind::YieldOnce: {
37953776
SmallVector<SILValue, 4> witnessYields;
3796-
auto token =
3777+
auto tokenAndCleanup =
37973778
emitBeginApplyWithRethrow(loc, witnessFnRef, witnessSILTy, witnessSubs,
37983779
args, witnessYields);
37993780

3800-
// Push a cleanup to abort the inner coroutine.
3801-
Cleanups.pushCleanup<EmitAbortApply>(token);
3802-
auto abortCleanup = Cleanups.getTopCleanup();
3803-
38043781
YieldInfo witnessYieldInfo(SGM, witness, witnessFTy, witnessSubs);
38053782
YieldInfo reqtYieldInfo(SGM, requirement, thunkTy,
38063783
reqtSubs.subst(getForwardingSubstitutionMap()));
38073784

38083785
translateYields(*this, loc, witnessYields, witnessYieldInfo, reqtYieldInfo);
38093786

38103787
// Kill the abort cleanup without emitting it.
3811-
Cleanups.setCleanupState(abortCleanup, CleanupState::Dead);
3788+
Cleanups.setCleanupState(tokenAndCleanup.second, CleanupState::Dead);
38123789

38133790
// End the inner coroutine normally.
3814-
emitEndApplyWithRethrow(loc, token);
3791+
emitEndApplyWithRethrow(loc, tokenAndCleanup.first);
38153792

38163793
reqtResultValue = B.createTuple(loc, {});
38173794
break;

0 commit comments

Comments
 (0)