Skip to content

Commit 7b0d10e

Browse files
authored
Merge pull request #23004 from slavapestov/sil-resilience-expansion-plumbing
Continue plumbing resilience expansion through SIL type lowering
2 parents 4dc4879 + a9841e8 commit 7b0d10e

39 files changed

+276
-236
lines changed

include/swift/SIL/OptimizationRemark.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ struct Argument {
5050
Argument(StringRef Key, unsigned long long N);
5151

5252
Argument(StringRef Key, SILFunction *F);
53-
Argument(StringRef Key, SILType *Ty);
53+
Argument(StringRef Key, SILType Ty);
54+
Argument(StringRef Key, CanType Ty);
5455
};
5556

5657
/// Shorthand to insert named-value pairs.

include/swift/SIL/SILBuilder.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,10 @@ class SILBuilder {
918918
} \
919919
Copy##Name##ValueInst *createCopy##Name##Value(SILLocation Loc, \
920920
SILValue operand) { \
921+
auto type = getFunction().getLoweredType( \
922+
operand->getType().getASTType().getReferenceStorageReferent()); \
921923
return insert(new (getModule()) \
922-
Copy##Name##ValueInst(getSILDebugLocation(Loc), operand, getModule())); \
924+
Copy##Name##ValueInst(getSILDebugLocation(Loc), operand, type)); \
923925
}
924926
#define SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
925927
NEVER_LOADABLE_CHECKED_REF_STORAGE(Name, "...") \

include/swift/SIL/SILFunction.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class SILModule;
3838
class SILFunctionBuilder;
3939
class SILProfiler;
4040

41+
namespace Lowering {
42+
class TypeLowering;
43+
class AbstractionPattern;
44+
}
45+
4146
enum IsBare_t { IsNotBare, IsBare };
4247
enum IsTransparent_t { IsNotTransparent, IsTransparent };
4348
enum Inline_t { InlineDefault, NoInline, AlwaysInline };
@@ -464,6 +469,19 @@ class SILFunction
464469
: ResilienceExpansion::Maximal);
465470
}
466471

472+
const Lowering::TypeLowering &
473+
getTypeLowering(Lowering::AbstractionPattern orig, Type subst);
474+
475+
const Lowering::TypeLowering &getTypeLowering(Type t) const;
476+
477+
SILType getLoweredType(Lowering::AbstractionPattern orig, Type subst) const;
478+
479+
SILType getLoweredType(Type t) const;
480+
481+
SILType getLoweredLoadableType(Type t) const;
482+
483+
const Lowering::TypeLowering &getTypeLowering(SILType type) const;
484+
467485
/// Returns true if this function has a calling convention that has a self
468486
/// argument.
469487
bool hasSelfParam() const {

include/swift/SIL/SILInstruction.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6328,9 +6328,8 @@ class Copy##Name##ValueInst \
63286328
SingleValueInstruction> { \
63296329
friend class SILBuilder; \
63306330
Copy##Name##ValueInst(SILDebugLocation DebugLoc, SILValue operand, \
6331-
SILModule &M) \
6332-
: UnaryInstructionBase(DebugLoc, operand, \
6333-
operand->getType().getReferentType(M)) {} \
6331+
SILType type) \
6332+
: UnaryInstructionBase(DebugLoc, operand, type) {} \
63346333
};
63356334
#include "swift/AST/ReferenceStorage.def"
63366335

include/swift/SIL/SILModule.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,10 @@ class SILModule {
598598

599599
/// Can value operations (copies and destroys) on the given lowered type
600600
/// be performed in this module?
601-
bool isTypeABIAccessible(SILType type);
601+
// FIXME: Expansion
602+
bool isTypeABIAccessible(SILType type,
603+
ResilienceExpansion forExpansion
604+
= ResilienceExpansion::Minimal);
602605

603606
/// Can type metadata for the given formal type be fetched in
604607
/// the given module?

include/swift/SIL/SILType.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,6 @@ class SILType {
477477
/// representation. Class existentials do not always qualify.
478478
bool isHeapObjectReferenceType() const;
479479

480-
/// Return the SILType corresponding to the underlying type of the given
481-
/// metatype type.
482-
///
483-
/// *NOTE* Only call on SILTypes for metatype types.
484-
SILType getMetatypeInstanceType(SILModule& M) const;
485-
486480
/// Returns true if this SILType is an aggregate that contains \p Ty
487481
bool aggregateContainsRecord(SILType Ty, SILModule &SILMod) const;
488482

@@ -501,10 +495,6 @@ class SILType {
501495

502496
/// Returns true if this is the AnyObject SILType;
503497
bool isAnyObject() const { return getASTType()->isAnyObject(); }
504-
505-
/// Returns the underlying referent SILType of an @sil_unowned or @sil_weak
506-
/// Type.
507-
SILType getReferentType(SILModule &M) const;
508498

509499
/// Returns a SILType with any archetypes mapped out of context.
510500
SILType mapTypeOutOfContext() const;

include/swift/SIL/TypeLowering.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,13 +769,16 @@ class TypeConverter {
769769
ResilienceExpansion::Minimal);
770770

771771
// Returns the lowered SIL type for a Swift type.
772-
SILType getLoweredType(Type t) {
773-
return getTypeLowering(t, ResilienceExpansion::Minimal).getLoweredType();
772+
SILType getLoweredType(Type t, ResilienceExpansion forExpansion
773+
= ResilienceExpansion::Minimal) {
774+
return getTypeLowering(t, forExpansion).getLoweredType();
774775
}
775776

776777
// Returns the lowered SIL type for a Swift type.
777-
SILType getLoweredType(AbstractionPattern origType, Type substType) {
778-
return getTypeLowering(origType, substType, ResilienceExpansion::Minimal)
778+
SILType getLoweredType(AbstractionPattern origType, Type substType,
779+
ResilienceExpansion forExpansion =
780+
ResilienceExpansion::Minimal) {
781+
return getTypeLowering(origType, substType, forExpansion)
779782
.getLoweredType();
780783
}
781784

include/swift/SILOptimizer/Utils/Devirtualize.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Emitter;
4444
/// \p Subs a container to be used for storing the set of subclasses
4545
void getAllSubclasses(ClassHierarchyAnalysis *CHA,
4646
ClassDecl *CD,
47-
SILType ClassType,
47+
CanType ClassType,
4848
SILModule &M,
4949
ClassHierarchyAnalysis::ClassList &Subs);
5050

@@ -69,18 +69,19 @@ ApplySite tryDevirtualizeApply(ApplySite AI,
6969
ClassHierarchyAnalysis *CHA,
7070
OptRemark::Emitter *ORE = nullptr);
7171
bool canDevirtualizeApply(FullApplySite AI, ClassHierarchyAnalysis *CHA);
72-
bool isNominalTypeWithUnboundGenericParameters(SILType Ty, SILModule &M);
73-
bool canDevirtualizeClassMethod(FullApplySite AI, SILType ClassInstanceType,
72+
bool canDevirtualizeClassMethod(FullApplySite AI, ClassDecl *CD,
7473
OptRemark::Emitter *ORE = nullptr,
7574
bool isEffectivelyFinalMethod = false);
76-
SILFunction *getTargetClassMethod(SILModule &M, SILType ClassOrMetatypeType,
75+
SILFunction *getTargetClassMethod(SILModule &M, ClassDecl *CD,
7776
MethodInst *MI);
77+
CanType getSelfInstanceType(CanType ClassOrMetatypeType);
7878

7979
/// Devirtualize the given apply site, which is known to be devirtualizable.
8080
///
8181
/// The caller must call deleteDevirtualizedApply on the original apply site.
8282
FullApplySite devirtualizeClassMethod(FullApplySite AI,
8383
SILValue ClassInstance,
84+
ClassDecl *CD,
8485
OptRemark::Emitter *ORE);
8586

8687
/// Attempt to devirtualize the given apply site, which is known to be
@@ -89,7 +90,9 @@ FullApplySite devirtualizeClassMethod(FullApplySite AI,
8990
/// If this succeeds, the caller must call deleteDevirtualizedApply on
9091
/// the original apply site.
9192
FullApplySite
92-
tryDevirtualizeClassMethod(FullApplySite AI, SILValue ClassInstance,
93+
tryDevirtualizeClassMethod(FullApplySite AI,
94+
SILValue ClassInstance,
95+
ClassDecl *CD,
9396
OptRemark::Emitter *ORE,
9497
bool isEffectivelyFinalMethod = false);
9598

include/swift/SILOptimizer/Utils/Local.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,6 @@ bool simplifyUsers(SingleValueInstruction *I);
522522
/// without a significant increase to code size.
523523
bool shouldExpand(SILModule &Module, SILType Ty);
524524

525-
/// Check if a given type is a simple type, i.e. a builtin
526-
/// integer or floating point type or a struct/tuple whose members
527-
/// are of simple types.
528-
bool isSimpleType(SILType SILTy, SILModule& Module);
529-
530525
/// Check if the value of V is computed by means of a simple initialization.
531526
/// Store the actual SILValue into \p Val and the reversed list of instructions
532527
/// initializing it in \p Insns.

lib/IRGen/GenType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ llvm::PointerType *IRGenModule::getStoragePointerTypeForLowered(CanType T) {
14561456
}
14571457

14581458
llvm::Type *IRGenModule::getStorageTypeForUnlowered(Type subst) {
1459-
return getStorageType(getSILTypes().getLoweredType(subst));
1459+
return getStorageType(getLoweredType(subst));
14601460
}
14611461

14621462
llvm::Type *IRGenModule::getStorageType(SILType T) {
@@ -1486,7 +1486,7 @@ IRGenModule::getTypeInfoForUnlowered(AbstractionPattern orig, Type subst) {
14861486
/// have yet undergone SIL type lowering.
14871487
const TypeInfo &
14881488
IRGenModule::getTypeInfoForUnlowered(AbstractionPattern orig, CanType subst) {
1489-
return getTypeInfo(getSILTypes().getLoweredType(orig, subst));
1489+
return getTypeInfo(getLoweredType(orig, subst));
14901490
}
14911491

14921492
/// Get the fragile type information for the given type, which is known

lib/IRGen/LoadableByAddress.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,8 +2221,8 @@ static void rewriteFunction(StructLoweringState &pass,
22212221
retBuilder.createStore(regLoc, retOp, retArg,
22222222
getStoreInitOwnership(pass, retOp->getType()));
22232223
}
2224-
auto emptyTy = retBuilder.getModule().Types.getLoweredType(
2225-
TupleType::getEmpty(retBuilder.getModule().getASTContext()));
2224+
auto emptyTy = SILType::getPrimitiveObjectType(
2225+
retBuilder.getModule().getASTContext().TheEmptyTupleType);
22262226
auto newRetTuple = retBuilder.createTuple(regLoc, emptyTy, {});
22272227
retBuilder.createReturn(newRetTuple->getLoc(), newRetTuple);
22282228
instr->eraseFromParent();

lib/ParseSIL/ParseSIL.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4767,8 +4767,9 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
47674767
= OpenedArchetypeType::get(Val->getType().getASTType())
47684768
->getCanonicalType();
47694769

4770-
SILType LoweredTy = SILMod.Types.getLoweredType(
4771-
Lowering::AbstractionPattern(archetype), Ty)
4770+
auto &F = B.getFunction();
4771+
SILType LoweredTy = F.getLoweredType(
4772+
Lowering::AbstractionPattern(archetype), Ty)
47724773
.getAddressType();
47734774

47744775
// Collect conformances for the type.

lib/SIL/DynamicCasts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ namespace {
798798

799799
private:
800800
const TypeLowering &getTypeLowering(SILType type) {
801-
return M.Types.getTypeLowering(type);
801+
return B.getFunction().getTypeLowering(type);
802802
}
803803

804804
SILValue getOwnedScalar(Source source, const TypeLowering &srcTL) {

lib/SIL/OptimizationRemark.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ Argument::Argument(StringRef Key, SILFunction *F)
5858
Loc = F->getLocation().getSourceLoc();
5959
}
6060

61-
Argument::Argument(StringRef Key, SILType *Ty) : Key(Key) {
61+
Argument::Argument(StringRef Key, SILType Ty) : Key(Key) {
6262
llvm::raw_string_ostream OS(Val);
63-
Ty->print(OS);
63+
Ty.print(OS);
64+
}
65+
66+
Argument::Argument(StringRef Key, CanType Ty) : Key(Key) {
67+
llvm::raw_string_ostream OS(Val);
68+
Ty.print(OS);
6469
}
6570

6671
template <typename DerivedT> std::string Remark<DerivedT>::getMsg() const {

lib/SIL/SIL.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ static bool isTypeMetadataForLayoutAccessible(SILModule &M, SILType type) {
185185
/// that are ABI-private to their defining module. But if the type is not
186186
/// ABI-private, we can always at least fetch its metadata and use the
187187
/// value witness table stored there.
188-
bool SILModule::isTypeABIAccessible(SILType type) {
188+
bool SILModule::isTypeABIAccessible(SILType type,
189+
ResilienceExpansion forExpansion) {
189190
// Fixed-ABI types can have value operations done without metadata.
190-
if (Types.getTypeLowering(type).isFixedABI())
191+
if (Types.getTypeLowering(type, forExpansion).isFixedABI())
191192
return true;
192193

193194
assert(!type.is<ReferenceStorageType>() &&

lib/SIL/SILFunction.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,43 @@ bool SILFunction::isNoReturnFunction() const {
226226
.isNoReturnFunction();
227227
}
228228

229+
const TypeLowering &
230+
SILFunction::getTypeLowering(AbstractionPattern orig, Type subst) {
231+
// FIXME: Expansion
232+
return getModule().Types.getTypeLowering(orig, subst,
233+
ResilienceExpansion::Minimal);
234+
}
235+
236+
const TypeLowering &SILFunction::getTypeLowering(Type t) const {
237+
// FIXME: Expansion
238+
return getModule().Types.getTypeLowering(t, ResilienceExpansion::Minimal);
239+
}
240+
241+
SILType
242+
SILFunction::getLoweredType(AbstractionPattern orig, Type subst) const {
243+
// FIXME: Expansion
244+
return getModule().Types.getLoweredType(orig, subst,
245+
ResilienceExpansion::Minimal);
246+
}
247+
248+
SILType SILFunction::getLoweredType(Type t) const {
249+
// FIXME: Expansion
250+
return getModule().Types.getLoweredType(t,
251+
ResilienceExpansion::Minimal);
252+
}
253+
254+
SILType SILFunction::getLoweredLoadableType(Type t) const {
255+
// FIXME: Expansion
256+
return getModule().Types.getLoweredLoadableType(t,
257+
ResilienceExpansion::Minimal);
258+
}
259+
260+
const TypeLowering &SILFunction::getTypeLowering(SILType type) const {
261+
// FIXME: Expansion
262+
return getModule().Types.getTypeLowering(type,
263+
ResilienceExpansion::Minimal);
264+
}
265+
229266
SILBasicBlock *SILFunction::createBasicBlock() {
230267
return new (getModule()) SILBasicBlock(this, nullptr, false);
231268
}

lib/SIL/SILOpenedArchetypesTracker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ bool SILOpenedArchetypesTracker::registerUsedOpenedArchetypes(CanType Ty) {
6262
return;
6363

6464
auto *CurF = const_cast<SILFunction *>(this->getFunction());
65-
auto &SILMod = CurF->getModule();
65+
6666
// Create a placeholder representing a forward definition.
6767
// Add the placeholder at the beginning of the entry block.
6868
SingleValueInstruction *Placeholder;
6969
if (!CurF->getEntryBlock()->empty()) {
7070
SILBuilder B(CurF->getEntryBlock()->begin());
7171
Placeholder =
7272
B.createGlobalAddr(ArtificialUnreachableLocation(),
73-
SILMod.Types.getLoweredType(archetypeTy));
73+
SILType::getPrimitiveAddressType(archetypeTy));
7474
} else {
7575
SILBuilder B(CurF->getEntryBlock());
7676
Placeholder =
7777
B.createGlobalAddr(ArtificialUnreachableLocation(),
78-
SILMod.Types.getLoweredType(archetypeTy));
78+
SILType::getPrimitiveAddressType(archetypeTy));
7979
}
8080
// Make it available to SILBuilder, so that instructions using this
8181
// archetype can be constructed.

lib/SIL/SILType.cpp

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,6 @@ bool SILType::isHeapObjectReferenceType() const {
216216
return false;
217217
}
218218

219-
SILType SILType::getMetatypeInstanceType(SILModule &M) const {
220-
CanType MetatypeType = getASTType();
221-
assert(MetatypeType->is<AnyMetatypeType>() &&
222-
"This method should only be called on SILTypes with an underlying "
223-
"metatype type.");
224-
Type instanceType =
225-
MetatypeType->castTo<AnyMetatypeType>()->getInstanceType();
226-
227-
return M.Types.getLoweredType(instanceType->getCanonicalType());
228-
}
229-
230219
bool SILType::aggregateContainsRecord(SILType Record, SILModule &Mod) const {
231220
assert(!hasArchetype() && "Agg should be proven to not be generic "
232221
"before passed to this function.");
@@ -395,11 +384,6 @@ SILType::canUseExistentialRepresentation(SILModule &M,
395384
llvm_unreachable("Unhandled ExistentialRepresentation in switch.");
396385
}
397386

398-
SILType SILType::getReferentType(SILModule &M) const {
399-
auto Ty = castTo<ReferenceStorageType>();
400-
return M.Types.getLoweredType(Ty->getReferentType()->getCanonicalType());
401-
}
402-
403387
SILType SILType::mapTypeOutOfContext() const {
404388
return SILType::getPrimitiveType(getASTType()->mapTypeOutOfContext()
405389
->getCanonicalType(),
@@ -556,8 +540,7 @@ bool SILType::hasAbstractionDifference(SILFunctionTypeRepresentation rep,
556540
bool SILType::isLoweringOf(SILModule &Mod, CanType formalType) {
557541
SILType loweredType = *this;
558542

559-
// Optional lowers its contained type. The difference between Optional
560-
// and IUO is lowered away.
543+
// Optional lowers its contained type.
561544
SILType loweredObjectType = loweredType.getOptionalObjectType();
562545
CanType formalObjectType = formalType.getOptionalObjectType();
563546

@@ -567,10 +550,9 @@ bool SILType::isLoweringOf(SILModule &Mod, CanType formalType) {
567550
}
568551

569552
// Metatypes preserve their instance type through lowering.
570-
if (loweredType.is<MetatypeType>()) {
553+
if (auto loweredMT = loweredType.getAs<MetatypeType>()) {
571554
if (auto formalMT = dyn_cast<MetatypeType>(formalType)) {
572-
return loweredType.getMetatypeInstanceType(Mod).isLoweringOf(
573-
Mod, formalMT.getInstanceType());
555+
return loweredMT.getInstanceType() == formalMT.getInstanceType();
574556
}
575557
}
576558

0 commit comments

Comments
 (0)