@@ -211,17 +211,19 @@ static ManagedValue borrowedCastToOriginalSelfType(SILGenFunction &SGF,
211
211
originalSelfType);
212
212
}
213
213
214
- static ManagedValue convertOwnershipConventionGivenParamInfo (SILGenFunction &SGF,
215
- SILParameterInfo param,
216
- ManagedValue value,
217
- SILLocation loc) {
214
+ static ManagedValue convertOwnershipConventionGivenParamInfo (
215
+ SILGenFunction &SGF, SILParameterInfo param, ManagedValue value,
216
+ SILLocation loc, bool isForCoroutine) {
218
217
if (param.isConsumed () &&
219
218
value.getOwnershipKind () == ValueOwnershipKind::Guaranteed) {
220
219
return value.copyUnmanaged (SGF, loc);
221
220
}
222
221
223
- if (SGF.F .getModule ().getOptions ().EnableSILOwnership &&
224
- value.getOwnershipKind () == ValueOwnershipKind::Owned) {
222
+ // If we are emitting arguments for a coroutine, we need to borrow owned
223
+ // values to ensure that they are live over the entire closure invocation. If
224
+ // we do not have a coroutine, then we have an immediate non-consuming use so
225
+ // no borrow is necessary.
226
+ if (isForCoroutine && value.getOwnershipKind () == ValueOwnershipKind::Owned) {
225
227
if (param.isDirectGuaranteed () || (!SGF.silConv .useLoweredAddresses () &&
226
228
param.isIndirectInGuaranteed ())) {
227
229
return value.borrow (SGF, loc);
@@ -232,16 +234,15 @@ static ManagedValue convertOwnershipConventionGivenParamInfo(SILGenFunction &SGF
232
234
}
233
235
234
236
static void convertOwnershipConventionsGivenParamInfos (
235
- SILGenFunction &SGF,
236
- ArrayRef<SILParameterInfo> params,
237
- ArrayRef<ManagedValue> values,
238
- SILLocation loc,
237
+ SILGenFunction &SGF, ArrayRef<SILParameterInfo> params,
238
+ ArrayRef<ManagedValue> values, SILLocation loc, bool isForCoroutine,
239
239
llvm::SmallVectorImpl<ManagedValue> &outVar) {
240
240
assert (params.size () == values.size () &&
241
241
" Different number of params from arguments" );
242
242
transform (indices (params), std::back_inserter (outVar),
243
243
[&](unsigned i) -> ManagedValue {
244
- return convertOwnershipConventionGivenParamInfo (SGF, params[i], values[i], loc);
244
+ return convertOwnershipConventionGivenParamInfo (
245
+ SGF, params[i], values[i], loc, isForCoroutine);
245
246
});
246
247
}
247
248
@@ -2571,6 +2572,7 @@ class ArgEmitter {
2571
2572
SILGenFunction &SGF;
2572
2573
SILFunctionTypeRepresentation Rep;
2573
2574
bool IsYield;
2575
+ bool IsForCoroutine;
2574
2576
Optional<ForeignErrorConvention> ForeignError;
2575
2577
ImportAsMemberStatus ForeignSelf;
2576
2578
ClaimedParamsRef ParamInfos;
@@ -2583,16 +2585,16 @@ class ArgEmitter {
2583
2585
Optional<ArgSpecialDestArray> SpecialDests;
2584
2586
public:
2585
2587
ArgEmitter (SILGenFunction &SGF, SILFunctionTypeRepresentation Rep,
2586
- bool isYield, ClaimedParamsRef paramInfos,
2588
+ bool isYield, bool isForCoroutine, ClaimedParamsRef paramInfos,
2587
2589
SmallVectorImpl<ManagedValue> &args,
2588
2590
SmallVectorImpl<DelayedArgument> &delayedArgs,
2589
2591
const Optional<ForeignErrorConvention> &foreignError,
2590
2592
ImportAsMemberStatus foreignSelf,
2591
2593
Optional<ArgSpecialDestArray> specialDests = None)
2592
- : SGF(SGF), Rep(Rep), IsYield(isYield), ForeignError(foreignError ),
2593
- ForeignSelf (foreignSelf),
2594
- ParamInfos(paramInfos),
2595
- Args(args), DelayedArguments(delayedArgs), SpecialDests(specialDests) {
2594
+ : SGF(SGF), Rep(Rep), IsYield(isYield), IsForCoroutine(isForCoroutine ),
2595
+ ForeignError (foreignError), ForeignSelf(foreignSelf),
2596
+ ParamInfos(paramInfos), Args(args), DelayedArguments(delayedArgs ),
2597
+ SpecialDests(specialDests) {
2596
2598
assert (!specialDests || specialDests->size () == paramInfos.size ());
2597
2599
}
2598
2600
@@ -2961,7 +2963,8 @@ class ArgEmitter {
2961
2963
auto loc = arg.getLocation ();
2962
2964
2963
2965
auto convertOwnershipConvention = [&](ManagedValue value) {
2964
- return convertOwnershipConventionGivenParamInfo (SGF, param, value, loc);
2966
+ return convertOwnershipConventionGivenParamInfo (SGF, param, value, loc,
2967
+ IsForCoroutine);
2965
2968
};
2966
2969
2967
2970
auto contexts = getRValueEmissionContexts (loweredSubstArgType, param);
@@ -3224,11 +3227,11 @@ void DelayedArgument::emitDefaultArgument(SILGenFunction &SGF,
3224
3227
SmallVector<ManagedValue, 4 > loweredArgs;
3225
3228
SmallVector<DelayedArgument, 4 > delayedArgs;
3226
3229
Optional<ForeignErrorConvention> errorConvention = None;
3227
- auto emitter = ArgEmitter (SGF, info. functionRepresentation , /* yield */ false ,
3228
- info.paramsToEmit ,
3229
- loweredArgs, delayedArgs ,
3230
- errorConvention, ImportAsMemberStatus ());
3231
-
3230
+ auto emitter =
3231
+ ArgEmitter (SGF, info.functionRepresentation , /* yield */ false ,
3232
+ /* coroutine */ false , info. paramsToEmit , loweredArgs ,
3233
+ delayedArgs, errorConvention, ImportAsMemberStatus ());
3234
+
3232
3235
emitter.emitSingleArg (ArgumentSource (info.loc , std::move (value)),
3233
3236
info.origResultType );
3234
3237
assert (delayedArgs.empty ());
@@ -3707,7 +3710,7 @@ void TupleShuffleArgEmitter::emit(ArgEmitter &parent) {
3707
3710
3708
3711
// Emit the inner expression.
3709
3712
if (!innerParams.empty ()) {
3710
- ArgEmitter (parent.SGF , parent.Rep , parent.IsYield ,
3713
+ ArgEmitter (parent.SGF , parent.Rep , parent.IsYield , parent. IsForCoroutine ,
3711
3714
ClaimedParamsRef (innerParams), innerArgs, innerDelayedArgs,
3712
3715
/* foreign error*/ None, /* foreign self*/ ImportAsMemberStatus (),
3713
3716
(innerSpecialDests ? ArgSpecialDestArray (*innerSpecialDests)
@@ -3965,16 +3968,17 @@ class CallSite {
3965
3968
3966
3969
// / Evaluate arguments and begin any inout formal accesses.
3967
3970
void emit (SILGenFunction &SGF, AbstractionPattern origFormalType,
3968
- ParamLowering &lowering, SmallVectorImpl<ManagedValue> &args,
3971
+ CanSILFunctionType substFnType, ParamLowering &lowering,
3972
+ SmallVectorImpl<ManagedValue> &args,
3969
3973
SmallVectorImpl<DelayedArgument> &delayedArgs,
3970
3974
const Optional<ForeignErrorConvention> &foreignError,
3971
3975
ImportAsMemberStatus foreignSelf) && {
3972
3976
auto params = lowering.claimParams (origFormalType, getParams (),
3973
3977
foreignError, foreignSelf);
3974
3978
3975
3979
ArgEmitter emitter (SGF, lowering.Rep , /* yield*/ false ,
3976
- params, args, delayedArgs ,
3977
- foreignError, foreignSelf);
3980
+ /* isForCoroutine */ substFnType-> isCoroutine (), params ,
3981
+ args, delayedArgs, foreignError, foreignSelf);
3978
3982
emitter.emitPreparedArgs (std::move (Args), origFormalType);
3979
3983
}
3980
3984
@@ -4603,8 +4607,8 @@ ApplyOptions CallEmission::emitArgumentsForNormalApply(
4603
4607
4604
4608
bool isParamSite = &site == &uncurriedSites.back ();
4605
4609
4606
- std::move (site).emit (SGF, origFormalType, paramLowering, args. back () ,
4607
- delayedArgs,
4610
+ std::move (site).emit (SGF, origFormalType, substFnType, paramLowering ,
4611
+ args. back (), delayedArgs,
4608
4612
// Claim the foreign error with the method
4609
4613
// formal params.
4610
4614
isParamSite ? foreignError : None,
@@ -4683,8 +4687,9 @@ RValue CallEmission::applyRemainingCallSites(RValue &&result,
4683
4687
ArgumentScope argScope (SGF, loc);
4684
4688
4685
4689
std::move (extraSites[i])
4686
- .emit (SGF, origFormalType, paramLowering, siteArgs, delayedArgs,
4687
- calleeTypeInfo.foreignError , calleeTypeInfo.foreignSelf );
4690
+ .emit (SGF, origFormalType, substFnType, paramLowering, siteArgs,
4691
+ delayedArgs, calleeTypeInfo.foreignError ,
4692
+ calleeTypeInfo.foreignSelf );
4688
4693
if (!delayedArgs.empty ()) {
4689
4694
emitDelayedArguments (SGF, delayedArgs, siteArgs);
4690
4695
}
@@ -5049,10 +5054,9 @@ void SILGenFunction::emitYield(SILLocation loc,
5049
5054
}
5050
5055
5051
5056
ArgEmitter emitter (*this , fnType->getRepresentation (), /* yield*/ true ,
5052
- ClaimedParamsRef (substYieldTys),
5057
+ /* isForCoroutine */ false , ClaimedParamsRef (substYieldTys),
5053
5058
yieldArgs, delayedArgs,
5054
- /* foreign error*/ None,
5055
- ImportAsMemberStatus ());
5059
+ /* foreign error*/ None, ImportAsMemberStatus ());
5056
5060
5057
5061
for (auto i : indices (valueSources)) {
5058
5062
emitter.emitSingleArg (std::move (valueSources[i]), origTypes[i]);
@@ -5234,8 +5238,9 @@ SILGenFunction::emitApplyOfLibraryIntrinsic(SILLocation loc,
5234
5238
5235
5239
SILFunctionConventions silConv (calleeTypeInfo.substFnType , getModule ());
5236
5240
llvm::SmallVector<ManagedValue, 8 > finalArgs;
5237
- convertOwnershipConventionsGivenParamInfos (*this , silConv.getParameters (),
5238
- args, loc, finalArgs);
5241
+ convertOwnershipConventionsGivenParamInfos (
5242
+ *this , silConv.getParameters (), args, loc,
5243
+ /* isForCoroutine*/ calleeTypeInfo.substFnType ->isCoroutine (), finalArgs);
5239
5244
5240
5245
ResultPlanPtr resultPlan =
5241
5246
ResultPlanBuilder::computeResultPlan (*this , calleeTypeInfo, loc, ctx);
@@ -5834,11 +5839,11 @@ SILGenFunction::prepareSubscriptIndices(SubscriptDecl *subscript,
5834
5839
SmallVector<ManagedValue, 4 > argValues;
5835
5840
SmallVector<DelayedArgument, 2 > delayedArgs;
5836
5841
5837
- ArgEmitter emitter (*this , SILFunctionTypeRepresentation::Thin, /* yield*/ false ,
5838
- ClaimedParamsRef (substParamTys),
5842
+ ArgEmitter emitter (*this , SILFunctionTypeRepresentation::Thin,
5843
+ /* yield*/ false ,
5844
+ /* isForCoroutine*/ false , ClaimedParamsRef (substParamTys),
5839
5845
argValues, delayedArgs,
5840
- /* foreign error*/ None,
5841
- ImportAsMemberStatus ());
5846
+ /* foreign error*/ None, ImportAsMemberStatus ());
5842
5847
5843
5848
emitter.emitTopLevel (indexExpr, AbstractionPattern (substFnType));
5844
5849
0 commit comments