Skip to content

Commit e9208f4

Browse files
authored
Merge pull request #23263 from slavapestov/final-preparations-for-resilience-expansion
Final preparations for using best resilience expansion in SIL
2 parents 01a86cf + 50b1bae commit e9208f4

File tree

15 files changed

+94
-65
lines changed

15 files changed

+94
-65
lines changed

include/swift/SIL/TypeLowering.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,8 @@ class TypeConverter {
717717
TypeConverter &operator=(TypeConverter const &) = delete;
718718

719719
/// Return the CaptureKind to use when capturing a decl.
720-
CaptureKind getDeclCaptureKind(CapturedValue capture);
720+
CaptureKind getDeclCaptureKind(CapturedValue capture,
721+
ResilienceExpansion expansion);
721722

722723
/// Return a most-general-possible abstraction pattern.
723724
AbstractionPattern getMostGeneralAbstraction() {

include/swift/SILOptimizer/Utils/Generics.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ class ReabstractionInfo {
155155
return Serialized;
156156
}
157157

158+
ResilienceExpansion getResilienceExpansion() const {
159+
// FIXME: Expansion
160+
return ResilienceExpansion::Minimal;
161+
}
162+
158163
/// Returns true if the \p ParamIdx'th (non-result) formal parameter is
159164
/// converted from indirect to direct.
160165
bool isParamConverted(unsigned ParamIdx) const {

lib/SIL/SILFunctionType.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ static std::pair<AbstractionPattern, CanType> updateResultTypeForForeignError(
714714
static void
715715
lowerCaptureContextParameters(SILModule &M, AnyFunctionRef function,
716716
CanGenericSignature genericSig,
717+
ResilienceExpansion expansion,
717718
SmallVectorImpl<SILParameterInfo> &inputs) {
718719

719720
// NB: The generic signature may be elided from the lowered function type
@@ -746,13 +747,11 @@ lowerCaptureContextParameters(SILModule &M, AnyFunctionRef function,
746747
auto type = VD->getInterfaceType();
747748
auto canType = type->getCanonicalType(origGenericSig);
748749

749-
// FIXME: Could be changed to Maximal at some point without impacting ABI,
750-
// as long as we make sure all call sites are non inlinable.
751750
auto &loweredTL =
752751
Types.getTypeLowering(AbstractionPattern(genericSig, canType), canType,
753-
ResilienceExpansion::Minimal);
752+
expansion);
754753
auto loweredTy = loweredTL.getLoweredType();
755-
switch (Types.getDeclCaptureKind(capture)) {
754+
switch (Types.getDeclCaptureKind(capture, expansion)) {
756755
case CaptureKind::None:
757756
break;
758757
case CaptureKind::Constant: {
@@ -985,7 +984,10 @@ static CanSILFunctionType getSILFunctionType(
985984
// from the function to which the argument is attached.
986985
if (constant && !constant->isDefaultArgGenerator()) {
987986
if (auto function = constant->getAnyFunctionRef()) {
988-
lowerCaptureContextParameters(M, *function, genericSig, inputs);
987+
// FIXME: Expansion
988+
auto expansion = ResilienceExpansion::Minimal;
989+
lowerCaptureContextParameters(M, *function, genericSig, expansion,
990+
inputs);
989991
}
990992
}
991993

lib/SIL/SILType.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,15 +446,21 @@ SILModuleConventions::SILModuleConventions(const SILModule &M)
446446

447447
bool SILModuleConventions::isReturnedIndirectlyInSIL(SILType type,
448448
SILModule &M) {
449-
if (SILModuleConventions(M).loweredAddresses)
450-
return type.isAddressOnly(M);
449+
if (SILModuleConventions(M).loweredAddresses) {
450+
return M.Types.getTypeLowering(type,
451+
ResilienceExpansion::Minimal)
452+
.isAddressOnly();
453+
}
451454

452455
return false;
453456
}
454457

455458
bool SILModuleConventions::isPassedIndirectlyInSIL(SILType type, SILModule &M) {
456-
if (SILModuleConventions(M).loweredAddresses)
457-
return type.isAddressOnly(M);
459+
if (SILModuleConventions(M).loweredAddresses) {
460+
return M.Types.getTypeLowering(type,
461+
ResilienceExpansion::Minimal)
462+
.isAddressOnly();
463+
}
458464

459465
return false;
460466
}

lib/SIL/TypeLowering.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ static bool hasSingletonMetatype(CanType instanceType) {
8686
return HasSingletonMetatype().visit(instanceType);
8787
}
8888

89-
CaptureKind TypeConverter::getDeclCaptureKind(CapturedValue capture) {
89+
CaptureKind TypeConverter::getDeclCaptureKind(CapturedValue capture,
90+
ResilienceExpansion expansion) {
9091
auto decl = capture.getDecl();
9192
if (auto *var = dyn_cast<VarDecl>(decl)) {
9293
assert(var->hasStorage() &&
@@ -97,9 +98,7 @@ CaptureKind TypeConverter::getDeclCaptureKind(CapturedValue capture) {
9798
// by its address (like a var) instead.
9899
if (var->isImmutable() &&
99100
(!SILModuleConventions(M).useLoweredAddresses() ||
100-
// FIXME: Expansion
101-
!getTypeLowering(var->getType(),
102-
ResilienceExpansion::Minimal).isAddressOnly()))
101+
!getTypeLowering(var->getType(), expansion).isAddressOnly()))
103102
return CaptureKind::Constant;
104103

105104
// In-out parameters are captured by address.

lib/SILGen/SILGenApply.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4507,8 +4507,6 @@ CallEmission::applyEnumElementConstructor(SGFContext C) {
45074507
// correctly.
45084508
firstLevelResult.formalType = callee.getSubstFormalType();
45094509
auto origFormalType = callee.getOrigFormalType();
4510-
auto substFnType =
4511-
SGF.getSILFunctionType(origFormalType, firstLevelResult.formalType);
45124510

45134511
// We have a fully-applied enum element constructor: open-code the
45144512
// construction.
@@ -4539,8 +4537,6 @@ CallEmission::applyEnumElementConstructor(SGFContext C) {
45394537
assert(uncurriedSites.size() == 1);
45404538
}
45414539

4542-
assert(substFnType->getNumResults() == 1);
4543-
(void)substFnType;
45444540
ManagedValue resultMV = SGF.emitInjectEnum(
45454541
uncurriedLoc, std::move(payload),
45464542
SGF.getLoweredType(formalResultType),

lib/SILGen/SILGenBridging.cpp

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -333,26 +333,15 @@ static ManagedValue emitManagedParameter(SILGenFunction &SGF, SILLocation loc,
333333
llvm_unreachable("bad convention");
334334
}
335335

336-
static void expandTupleTypes(CanType type, SmallVectorImpl<CanType> &results) {
337-
if (auto tuple = dyn_cast<TupleType>(type)) {
338-
for (auto eltType : tuple.getElementTypes())
339-
expandTupleTypes(eltType, results);
340-
} else {
341-
results.push_back(type);
342-
}
343-
}
344-
345-
/// Recursively expand all the tuples in the given parameter list.
346-
/// Callers assume that the resulting array will line up with the
347-
/// SILFunctionType's parameter list, which is true as along as there
348-
/// aren't any indirectly-passed tuples; we should be safe from that
349-
/// here in the bridging code.
336+
/// Get the type of each parameter, filtering out empty tuples.
350337
static SmallVector<CanType, 8>
351-
expandTupleTypes(AnyFunctionType::CanParamArrayRef params) {
338+
getParameterTypes(AnyFunctionType::CanParamArrayRef params) {
352339
SmallVector<CanType, 8> results;
353340
for (auto param : params) {
354341
assert(!param.isInOut() && !param.isVariadic());
355-
expandTupleTypes(param.getPlainType(), results);
342+
if (param.getPlainType()->isVoid())
343+
continue;
344+
results.push_back(param.getPlainType());
356345
}
357346
return results;
358347
}
@@ -403,8 +392,8 @@ static void buildFuncToBlockInvokeBody(SILGenFunction &SGF,
403392
assert(blockTy->getParameters().size() == funcTy->getParameters().size()
404393
&& "block and function types don't match");
405394

406-
auto nativeParamTypes = expandTupleTypes(formalFuncType.getParams());
407-
auto bridgedParamTypes = expandTupleTypes(formalBlockType.getParams());
395+
auto nativeParamTypes = getParameterTypes(formalFuncType.getParams());
396+
auto bridgedParamTypes = getParameterTypes(formalBlockType.getParams());
408397

409398
SmallVector<ManagedValue, 4> args;
410399
for (unsigned i : indices(funcTy->getParameters())) {
@@ -839,8 +828,8 @@ static void buildBlockToFuncThunkBody(SILGenFunction &SGF,
839828
}
840829
}
841830

842-
auto formalBlockParams = expandTupleTypes(formalBlockTy.getParams());
843-
auto formalFuncParams = expandTupleTypes(formalFuncTy.getParams());
831+
auto formalBlockParams = getParameterTypes(formalBlockTy.getParams());
832+
auto formalFuncParams = getParameterTypes(formalFuncTy.getParams());
844833
assert(formalBlockParams.size() == blockTy->getNumParameters());
845834
assert(formalFuncParams.size() == funcTy->getNumParameters());
846835

@@ -1297,10 +1286,10 @@ static SILFunctionType *emitObjCThunkArguments(SILGenFunction &SGF,
12971286
assert(objcFnTy->getNumIndirectFormalResults() == 0
12981287
&& "Objective-C methods cannot have indirect results");
12991288

1300-
auto bridgedFormalTypes = expandTupleTypes(objcFormalFnTy.getParams());
1289+
auto bridgedFormalTypes = getParameterTypes(objcFormalFnTy.getParams());
13011290
bridgedFormalResultTy = objcFormalFnTy.getResult();
13021291

1303-
auto nativeFormalTypes = expandTupleTypes(swiftFormalFnTy.getParams());
1292+
auto nativeFormalTypes = getParameterTypes(swiftFormalFnTy.getParams());
13041293
nativeFormalResultTy = swiftFormalFnTy.getResult();
13051294

13061295
// Emit the other arguments, taking ownership of arguments if necessary.
@@ -1697,9 +1686,9 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
16971686

16981687
{
16991688
auto foreignFormalParams =
1700-
expandTupleTypes(foreignCI.LoweredType.getParams());
1689+
getParameterTypes(foreignCI.LoweredType.getParams());
17011690
auto nativeFormalParams =
1702-
expandTupleTypes(nativeCI.LoweredType.getParams());
1691+
getParameterTypes(nativeCI.LoweredType.getParams());
17031692

17041693
for (unsigned nativeParamIndex : indices(params)) {
17051694
// Bring the parameter to +1.

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -856,11 +856,7 @@ emitRValueForDecl(SILLocation loc, ConcreteDeclRef declRef, Type ncRefType,
856856

857857
// If this is a decl that we have an lvalue for, produce and return it.
858858
ValueDecl *decl = declRef.getDecl();
859-
860-
if (!ncRefType) {
861-
ncRefType = decl->getInnermostDeclContext()->mapTypeIntoContext(
862-
decl->getInterfaceType());
863-
}
859+
864860
CanType refType = ncRefType->getCanonicalType();
865861

866862
// If this is a reference to a module, produce an undef value. The

lib/SILGen/SILGenFunction.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ void SILGenFunction::emitCaptures(SILLocation loc,
197197

198198
auto *vd = capture.getDecl();
199199

200-
switch (SGM.Types.getDeclCaptureKind(capture)) {
200+
// FIXME: Expansion
201+
auto expansion = ResilienceExpansion::Minimal;
202+
switch (SGM.Types.getDeclCaptureKind(capture, expansion)) {
201203
case CaptureKind::None:
202204
break;
203205

lib/SILGen/SILGenProlog.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,15 @@ static void emitCaptureArguments(SILGenFunction &SGF,
354354
closure.getGenericEnvironment(), interfaceType);
355355
};
356356

357-
switch (SGF.SGM.Types.getDeclCaptureKind(capture)) {
357+
// FIXME: Expansion
358+
auto expansion = ResilienceExpansion::Minimal;
359+
switch (SGF.SGM.Types.getDeclCaptureKind(capture, expansion)) {
358360
case CaptureKind::None:
359361
break;
360362

361363
case CaptureKind::Constant: {
362364
auto type = getVarTypeInCaptureContext();
363-
auto &lowering = SGF.SGM.Types.getTypeLowering(type,
364-
ResilienceExpansion::Minimal);
365+
auto &lowering = SGF.getTypeLowering(type);
365366
// Constant decls are captured by value.
366367
SILType ty = lowering.getLoweredType();
367368
SILValue val = SGF.F.begin()->createFunctionArgument(ty, VD);

lib/SILOptimizer/IPO/CapturePromotion.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ computeNewArgInterfaceTypes(SILFunction *F,
365365
assert(paramBoxTy->getLayout()->getFields().size() == 1
366366
&& "promoting compound box not implemented yet");
367367
auto paramBoxedTy = paramBoxTy->getFieldType(F->getModule(), 0);
368+
// FIXME: Expansion
368369
auto &paramTL = Types.getTypeLowering(paramBoxedTy,
369370
ResilienceExpansion::Minimal);
370371
ParameterConvention convention;

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,12 @@ static SILFunction *genGetterFromInit(SILOptFunctionBuilder &FunctionBuilder,
294294
auto V = Store->getSrc();
295295

296296
SmallVector<SILInstruction *, 8> Insts;
297-
if (!analyzeStaticInitializer(V, Insts))
297+
if (!analyzeStaticInitializer(V, Insts)) {
298+
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: can't analyze static initializer for "
299+
<< SILG->getName() << '\n');
298300
return nullptr;
301+
}
302+
299303
Insts.push_back(cast<SingleValueInstruction>(Store->getDest()));
300304
Insts.push_back(Store);
301305

@@ -637,6 +641,9 @@ replaceLoadsByKnownValue(BuiltinInst *CallToOnce, SILFunction *AddrF,
637641
SILFunction *InitF, SILGlobalVariable *SILG,
638642
SingleValueInstruction *InitVal,
639643
GlobalInitCalls &Calls) {
644+
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: replacing loads with known value for "
645+
<< SILG->getName() << '\n');
646+
640647
assert(isAssignedOnlyOnceInInitializer(SILG) &&
641648
"The value of the initializer should be known at compile-time");
642649
assert(SILG->getDecl() &&
@@ -736,6 +743,9 @@ void SILGlobalOpt::optimizeInitializer(SILFunction *AddrF,
736743

737744
// Remove "once" call from the addressor.
738745
if (!isAssignedOnlyOnceInInitializer(SILG) || !SILG->getDecl()) {
746+
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: building static initializer for "
747+
<< SILG->getName() << '\n');
748+
739749
removeToken(CallToOnce->getOperand(0));
740750
CallToOnce->eraseFromParent();
741751
StaticInitCloner::appendToInitializer(SILG, InitVal);
@@ -819,6 +829,8 @@ void SILGlobalOpt::collectGlobalAccess(GlobalAddrInst *GAI) {
819829
auto *F = GAI->getFunction();
820830

821831
if (!SILG->getLoweredType().isTrivial(*F)) {
832+
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: type is not trivial: "
833+
<< SILG->getName() << '\n');
822834
GlobalVarSkipProcessing.insert(SILG);
823835
return;
824836
}
@@ -843,6 +855,9 @@ void SILGlobalOpt::collectGlobalAccess(GlobalAddrInst *GAI) {
843855
continue;
844856
}
845857

858+
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: has non-store, non-load use: "
859+
<< SILG->getName() << '\n';
860+
Op->getUser()->dump());
846861
// This global is not initialized by a simple
847862
// constant value at this moment.
848863
GlobalVarSkipProcessing.insert(SILG);
@@ -858,16 +873,23 @@ void SILGlobalOpt::optimizeGlobalAccess(SILGlobalVariable *SILG,
858873
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: use static initializer for "
859874
<< SILG->getName() << '\n');
860875

861-
if (GlobalVarSkipProcessing.count(SILG))
876+
if (GlobalVarSkipProcessing.count(SILG)) {
877+
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: already decided to skip: "
878+
<< SILG->getName() << '\n');
862879
return;
880+
}
863881

864-
if (//!isAssignedOnlyOnceInInitializer(SILG) ||
865-
!SILG->getDecl()) {
882+
if (!SILG->getDecl()) {
883+
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: no AST declaration: "
884+
<< SILG->getName() << '\n');
866885
return;
867886
}
868887

869-
if (!GlobalLoadMap.count(SILG))
888+
if (!GlobalLoadMap.count(SILG)) {
889+
LLVM_DEBUG(llvm::dbgs() << "GlobalOpt: not in load map: "
890+
<< SILG->getName() << '\n');
870891
return;
892+
}
871893

872894
// Generate a getter only if there are any loads from this variable.
873895
SILFunction *GetterF = genGetterFromInit(FunctionBuilder, SI, SILG);

lib/SILOptimizer/Transforms/ObjectOutliner.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ bool ObjectOutliner::optimizeObjectAllocation(
339339
<< ARI->getFunction()->getName() << '\n');
340340

341341
SILModule *Module = &ARI->getFunction()->getModule();
342+
// FIXME: Expansion
342343
assert(!Cl->isResilient(Module->getSwiftModule(),
343344
ResilienceExpansion::Minimal) &&
344345
"constructor call of resilient class should prevent static allocation");
@@ -448,6 +449,7 @@ void ObjectOutliner::replaceFindStringCall(ApplyInst *FindStringCall) {
448449
return;
449450

450451

452+
// FIXME: Expansion
451453
assert(!cacheDecl->isResilient(Module->getSwiftModule(),
452454
ResilienceExpansion::Minimal));
453455

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,7 @@ bool swift::canDevirtualizeClassMethod(FullApplySite AI,
695695
}
696696

697697
// We need to disable the “effectively final” opt if a function is inlinable
698-
if (isEffectivelyFinalMethod && AI.getFunction()->getResilienceExpansion() ==
699-
ResilienceExpansion::Minimal) {
698+
if (isEffectivelyFinalMethod && AI.getFunction()->isSerialized()) {
700699
LLVM_DEBUG(llvm::dbgs() << " FAIL: Could not optimize function "
701700
"because it is an effectively-final inlinable: "
702701
<< AI.getFunction()->getName() << "\n");

0 commit comments

Comments
 (0)