Skip to content

Commit e9208f4

Browse files
authored
Merge pull request swiftlang#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);

0 commit comments

Comments
 (0)