Skip to content

Commit 568816a

Browse files
committed
SILGen: Always use minimal resilience expansion for the calling convention
1 parent 8915f96 commit 568816a

File tree

5 files changed

+63
-30
lines changed

5 files changed

+63
-30
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5985,20 +5985,19 @@ ArgumentSource SILGenFunction::prepareAccessorBaseArg(SILLocation loc,
59855985
return Preparer.prepare();
59865986
}
59875987

5988-
static void collectFakeIndexParameters(SILGenModule &SGM,
5988+
static void collectFakeIndexParameters(SILGenFunction &SGF,
59895989
CanType substType,
59905990
SmallVectorImpl<SILParameterInfo> &params) {
59915991
if (auto tuple = dyn_cast<TupleType>(substType)) {
59925992
for (auto substEltType : tuple.getElementTypes())
5993-
collectFakeIndexParameters(SGM, substEltType, params);
5993+
collectFakeIndexParameters(SGF, substEltType, params);
59945994
return;
59955995
}
59965996

59975997
// Use conventions that will produce a +1 value.
5998-
auto &tl = SGM.Types.getTypeLowering(substType,
5999-
ResilienceExpansion::Minimal);
5998+
auto &tl = SGF.getTypeLowering(substType);
60005999
ParameterConvention convention;
6001-
if (tl.isFormallyPassedIndirectly()) {
6000+
if (tl.isAddressOnly()) {
60026001
convention = ParameterConvention::Indirect_In;
60036002
} else if (tl.isTrivial()) {
60046003
convention = ParameterConvention::Direct_Unowned;
@@ -6019,7 +6018,7 @@ static void emitPseudoFunctionArguments(SILGenFunction &SGF,
60196018
SmallVector<SILParameterInfo, 4> substParamTys;
60206019
for (auto substParam : substParams) {
60216020
auto substParamType = substParam.getParameterType()->getCanonicalType();
6022-
collectFakeIndexParameters(SGF.SGM, substParamType, substParamTys);
6021+
collectFakeIndexParameters(SGF, substParamType, substParamTys);
60236022
}
60246023

60256024
SmallVector<ManagedValue, 4> argValues;

lib/SILGen/SILGenBridging.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,15 @@ static SILFunctionType *emitObjCThunkArguments(SILGenFunction &SGF,
13701370
/*isCallResult*/ true);
13711371
SILValue argValue;
13721372

1373+
// This can happen if the value is resilient in the calling convention
1374+
// but not resilient locally.
1375+
if (nativeInputs[i].isFormalIndirect() &&
1376+
!native.getType().isAddress()) {
1377+
auto buf = SGF.emitTemporaryAllocation(loc, native.getType());
1378+
native.forwardInto(SGF, loc, buf);
1379+
native = SGF.emitManagedBufferWithCleanup(buf);
1380+
}
1381+
13731382
if (nativeInputs[i].isConsumed()) {
13741383
argValue = native.forward(SGF);
13751384
} else if (nativeInputs[i].isGuaranteed()) {

lib/SILGen/SILGenConstructor.cpp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static SILValue emitConstructorMetatypeArg(SILGenFunction &SGF,
5252
return SGF.AllocatorMetatype;
5353
}
5454

55+
// FIXME: Consolidate this with SILGenProlog
5556
static RValue emitImplicitValueConstructorArg(SILGenFunction &SGF,
5657
SILLocation loc,
5758
CanType interfaceType,
@@ -73,14 +74,26 @@ static RValue emitImplicitValueConstructorArg(SILGenFunction &SGF,
7374
AC.getIdentifier("$implicit_value"),
7475
DC);
7576
VD->setInterfaceType(interfaceType);
76-
SILFunctionArgument *arg =
77-
SGF.F.begin()->createFunctionArgument(SGF.getLoweredType(type), VD);
77+
78+
auto argType = SGF.SGM.Types.getLoweredType(type,
79+
ResilienceExpansion::Minimal);
80+
auto *arg = SGF.F.begin()->createFunctionArgument(argType, VD);
7881
ManagedValue mvArg;
7982
if (arg->getArgumentConvention().isOwnedConvention()) {
8083
mvArg = SGF.emitManagedRValueWithCleanup(arg);
8184
} else {
8285
mvArg = ManagedValue::forUnmanaged(arg);
8386
}
87+
88+
// This can happen if the value is resilient in the calling convention
89+
// but not resilient locally.
90+
if (argType.isLoadable(SGF.SGM.M) && argType.isAddress()) {
91+
if (mvArg.isPlusOne(SGF))
92+
mvArg = SGF.B.createLoadTake(loc, mvArg);
93+
else
94+
mvArg = SGF.B.createLoadBorrow(loc, mvArg);
95+
}
96+
8497
return RValue(SGF, loc, type, mvArg);
8598
}
8699

@@ -91,9 +104,8 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
91104
// FIXME: Handle 'self' along with the other arguments.
92105
auto *paramList = ctor->getParameters();
93106
auto *selfDecl = ctor->getImplicitSelfDecl();
94-
auto selfTyCan = selfDecl->getType();
95-
auto selfIfaceTyCan = selfDecl->getInterfaceType();
96-
SILType selfTy = SGF.getLoweredType(selfTyCan);
107+
auto selfIfaceTy = selfDecl->getInterfaceType();
108+
SILType selfTy = SGF.getLoweredType(selfDecl->getType());
97109

98110
// Emit the indirect return argument, if any.
99111
SILValue resultSlot;
@@ -105,8 +117,8 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
105117
SourceLoc(),
106118
AC.getIdentifier("$return_value"),
107119
ctor);
108-
VD->setInterfaceType(selfIfaceTyCan);
109-
resultSlot = SGF.F.begin()->createFunctionArgument(selfTy, VD);
120+
VD->setInterfaceType(selfIfaceTy);
121+
resultSlot = SGF.F.begin()->createFunctionArgument(selfTy.getAddressType(), VD);
110122
}
111123

112124
// Emit the elementwise arguments.
@@ -130,9 +142,9 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
130142
auto elti = elements.begin(), eltEnd = elements.end();
131143
for (VarDecl *field : decl->getStoredProperties()) {
132144
auto fieldTy = selfTy.getFieldType(field, SGF.SGM.M);
133-
auto &fieldTL = SGF.getTypeLowering(fieldTy);
134-
SILValue slot = SGF.B.createStructElementAddr(Loc, resultSlot, field,
135-
fieldTL.getLoweredType().getAddressType());
145+
SILValue slot =
146+
SGF.B.createStructElementAddr(Loc, resultSlot, field,
147+
fieldTy.getAddressType());
136148
InitializationPtr init(new KnownAddressInitialization(slot));
137149

138150
// An initialized 'let' property has a single value specified by the
@@ -378,12 +390,10 @@ void SILGenFunction::emitValueConstructor(ConstructorDecl *ctor) {
378390
}
379391

380392
void SILGenFunction::emitEnumConstructor(EnumElementDecl *element) {
381-
CanType enumIfaceTy = element->getParentEnum()
382-
->getDeclaredInterfaceType()
383-
->getCanonicalType();
384-
CanType enumTy = F.mapTypeIntoContext(enumIfaceTy)
385-
->getCanonicalType();
386-
auto &enumTI = getTypeLowering(enumTy);
393+
Type enumIfaceTy = element->getParentEnum()->getDeclaredInterfaceType();
394+
Type enumTy = F.mapTypeIntoContext(enumIfaceTy);
395+
auto &enumTI = SGM.Types.getTypeLowering(enumTy,
396+
ResilienceExpansion::Minimal);
387397

388398
RegularLocation Loc(element);
389399
CleanupLocation CleanupLoc(element);

lib/SILGen/SILGenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class LocalVariableInitialization : public SingleBufferInitialization {
374374

375375
auto boxType = SGF.SGM.Types
376376
.getContextBoxTypeForCapture(decl,
377-
SGF.getLoweredType(decl->getType()).getASTType(),
377+
SGF.SGM.Types.getLoweredRValueType(decl->getType()),
378378
SGF.F.getGenericEnvironment(),
379379
/*mutable*/ true);
380380

lib/SILGen/SILGenProlog.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
9696
}
9797

9898
ManagedValue visitType(CanType t, bool isInOut) {
99-
auto argType = SGF.getLoweredType(t);
99+
// The calling convention always uses minimal resilience expansion.
100+
auto argType =
101+
SGF.SGM.Types.getLoweredType(t, ResilienceExpansion::Minimal);
100102
if (isInOut)
101103
argType = SILType::getPrimitiveAddressType(argType.getASTType());
102104

@@ -123,6 +125,15 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
123125
if (isInOut)
124126
return mv;
125127

128+
// This can happen if the value is resilient in the calling convention
129+
// but not resilient locally.
130+
if (argType.isLoadable(SGF.SGM.M) && argType.isAddress()) {
131+
if (mv.isPlusOne(SGF))
132+
mv = SGF.B.createLoadTake(loc, mv);
133+
else
134+
mv = SGF.B.createLoadBorrow(loc, mv);
135+
}
136+
126137
// If the value is a (possibly optional) ObjC block passed into the entry
127138
// point of the function, then copy it so we can treat the value reliably
128139
// as a heap object. Escape analysis can eliminate this copy if it's
@@ -142,7 +153,7 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
142153
ManagedValue visitTupleType(CanTupleType t) {
143154
SmallVector<ManagedValue, 4> elements;
144155

145-
auto &tl = SGF.getTypeLowering(t);
156+
auto &tl = SGF.SGM.Types.getTypeLowering(t, ResilienceExpansion::Minimal);
146157
bool canBeGuaranteed = tl.isLoadable();
147158

148159
// Collect the exploded elements.
@@ -305,7 +316,8 @@ static void makeArgument(Type ty, ParamDecl *decl,
305316
for (auto fieldType : tupleTy->getElementTypes())
306317
makeArgument(fieldType, decl, args, SGF);
307318
} else {
308-
auto loweredTy = SGF.getLoweredType(ty);
319+
auto loweredTy = SGF.SGM.Types.getLoweredType(ty,
320+
ResilienceExpansion::Minimal);
309321
if (decl->isInOut())
310322
loweredTy = SILType::getPrimitiveAddressType(loweredTy.getASTType());
311323
auto arg = SGF.F.begin()->createFunctionArgument(loweredTy, decl);
@@ -348,7 +360,8 @@ static void emitCaptureArguments(SILGenFunction &SGF,
348360

349361
case CaptureKind::Constant: {
350362
auto type = getVarTypeInCaptureContext();
351-
auto &lowering = SGF.getTypeLowering(type);
363+
auto &lowering = SGF.SGM.Types.getTypeLowering(type,
364+
ResilienceExpansion::Minimal);
352365
// Constant decls are captured by value.
353366
SILType ty = lowering.getLoweredType();
354367
SILValue val = SGF.F.begin()->createFunctionArgument(ty, VD);
@@ -387,7 +400,7 @@ static void emitCaptureArguments(SILGenFunction &SGF,
387400
// the captured value.
388401
auto type = getVarTypeInCaptureContext();
389402
auto boxTy = SGF.SGM.Types.getContextBoxTypeForCapture(VD,
390-
SGF.getLoweredType(type).getASTType(),
403+
SGF.SGM.Types.getLoweredRValueType(type),
391404
SGF.F.getGenericEnvironment(), /*mutable*/ true);
392405
SILValue box = SGF.F.begin()->createFunctionArgument(
393406
SILType::getPrimitiveObjectType(boxTy), VD);
@@ -460,8 +473,10 @@ static void emitIndirectResultParameters(SILGenFunction &SGF, Type resultType,
460473

461474
// If the return type is address-only, emit the indirect return argument.
462475

463-
const TypeLowering &resultTI =
464-
SGF.getTypeLowering(DC->mapTypeIntoContext(resultType));
476+
// The calling convention always uses minimal resilience expansion.
477+
auto &resultTI =
478+
SGF.SGM.Types.getTypeLowering(DC->mapTypeIntoContext(resultType),
479+
ResilienceExpansion::Minimal);
465480
if (!SILModuleConventions::isReturnedIndirectlyInSIL(
466481
resultTI.getLoweredType(), SGF.SGM.M)) {
467482
return;

0 commit comments

Comments
 (0)