Skip to content

Commit f562aca

Browse files
committed
SIL: Kill off GenericContextScope
1 parent aae1252 commit f562aca

15 files changed

+283
-325
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,10 @@ class SILFunction
591591
/// therefore contain context archetypes, to an interface type.
592592
Type mapTypeOutOfContext(Type type) const;
593593

594+
/// Map the given type, which is based on a contextual SILFunctionType and may
595+
/// therefore contain context archetypes, to an interface type.
596+
SILType mapTypeOutOfContext(SILType type) const;
597+
594598
/// Converts the given function definition to a declaration.
595599
void convertToDeclaration();
596600

include/swift/SIL/SILType.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ class SILType {
128128

129129
/// Apply a substitution to the type to produce another lowered SIL type.
130130
static SILType substType(SILModule &silModule, ModuleDecl *astModule,
131-
const TypeSubstitutionMap &subs, SILType SrcTy);
131+
const TypeSubstitutionMap &subs, SILType SrcTy,
132+
CanGenericSignature genericSig=CanGenericSignature());
132133

133134
/// Apply a substitution to the function type.
134135
static CanSILFunctionType substFuncType(SILModule &silModule,
@@ -448,7 +449,8 @@ class SILType {
448449
ArrayRef<Substitution> Subs) const;
449450

450451
SILType subst(SILModule &silModule, ModuleDecl *astModule,
451-
const TypeSubstitutionMap &subs) const;
452+
const TypeSubstitutionMap &subs,
453+
CanGenericSignature genericSig=CanGenericSignature()) const;
452454

453455
/// If this is a specialized generic type, return all substitutions used to
454456
/// generate it.
@@ -548,6 +550,7 @@ NON_SIL_TYPE(LValue)
548550
CanSILFunctionType getNativeSILFunctionType(SILModule &M,
549551
Lowering::AbstractionPattern orig,
550552
CanAnyFunctionType substInterface,
553+
CanGenericSignature genericSig,
551554
Optional<SILDeclRef> constant = None,
552555
SILDeclRef::Kind kind = SILDeclRef::Kind::Func);
553556

include/swift/SIL/TypeLowering.h

Lines changed: 44 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ inline CanSILFunctionType adjustFunctionType(CanSILFunctionType t,
8585
}
8686

8787

88-
/// Flag used to place context-dependent TypeLowerings in their own arena which
89-
/// can be disposed when a generic context is exited.
90-
enum IsDependent_t : unsigned {
91-
IsNotDependent = false,
92-
IsDependent = true
93-
};
94-
9588
/// Extended type information used by SIL.
9689
class TypeLowering {
9790
public:
@@ -335,10 +328,8 @@ class TypeLowering {
335328
}
336329

337330
/// Allocate a new TypeLowering using the TypeConverter's allocator.
338-
void *operator new(size_t size, TypeConverter &tc,
339-
IsDependent_t dependent);
340-
void *operator new[](size_t size, TypeConverter &tc,
341-
IsDependent_t dependent);
331+
void *operator new(size_t size, TypeConverter &tc);
332+
void *operator new[](size_t size, TypeConverter &tc);
342333

343334
// Forbid 'new FooTypeLowering' and try to forbid 'delete tl'.
344335
// The latter is made challenging because the existence of the
@@ -399,9 +390,6 @@ class TypeConverter {
399390
friend class TypeLowering;
400391

401392
llvm::BumpPtrAllocator IndependentBPA;
402-
/// BumpPtrAllocator for types dependent on contextual generic parameters,
403-
/// which is reset when the generic context is popped.
404-
llvm::BumpPtrAllocator DependentBPA;
405393

406394
enum : unsigned {
407395
/// There is a unique entry with this uncurry level in the
@@ -411,15 +399,17 @@ class TypeConverter {
411399
};
412400

413401
struct CachingTypeKey {
414-
GenericSignature *Sig;
402+
GenericSignature *OrigSig;
415403
AbstractionPattern::CachingKey OrigType;
404+
GenericSignature *SubstSig;
416405
CanType SubstType;
417406
unsigned UncurryLevel;
418407

419408
friend bool operator==(const CachingTypeKey &lhs,
420409
const CachingTypeKey &rhs) {
421-
return lhs.Sig == rhs.Sig
410+
return lhs.OrigSig == rhs.OrigSig
422411
&& lhs.OrigType == rhs.OrigType
412+
&& lhs.SubstSig == rhs.SubstSig
423413
&& lhs.SubstType == rhs.SubstType
424414
&& lhs.UncurryLevel == rhs.UncurryLevel;
425415
}
@@ -433,6 +423,10 @@ class TypeConverter {
433423
/// An unsubstituted version of a type, dictating its abstraction patterns.
434424
AbstractionPattern OrigType;
435425

426+
/// The generic signature describing dependent types in the substituted
427+
/// type.
428+
CanGenericSignature SubstSig;
429+
436430
/// The substituted version of the type, dictating the types that
437431
/// should be used in the lowered type.
438432
CanType SubstType;
@@ -446,26 +440,23 @@ class TypeConverter {
446440
? OrigType.getGenericSignature()
447441
: nullptr),
448442
OrigType.getCachingKey(),
443+
SubstSig,
449444
SubstType,
450445
UncurryLevel };
451446
}
452447

453448
bool isCacheable() const {
454449
return OrigType.hasCachingKey();
455450
}
456-
457-
IsDependent_t isDependent() const {
458-
if (SubstType->hasTypeParameter())
459-
return IsDependent;
460-
return IsNotDependent;
461-
}
462451
};
463452

464453
friend struct llvm::DenseMapInfo<CachingTypeKey>;
465454

466-
TypeKey getTypeKey(AbstractionPattern origTy, CanType substTy,
455+
TypeKey getTypeKey(AbstractionPattern origTy,
456+
CanGenericSignature substSig,
457+
CanType substTy,
467458
unsigned uncurryLevel) {
468-
return {origTy, substTy, uncurryLevel};
459+
return {origTy, substSig, substTy, uncurryLevel};
469460
}
470461

471462
struct OverrideKey {
@@ -491,12 +482,7 @@ class TypeConverter {
491482
/// Insert a mapping into the cache.
492483
void insert(TypeKey k, const TypeLowering *tl);
493484

494-
/// Mapping for types independent on contextual generic parameters, which is
495-
/// cleared when the generic context is popped.
496485
llvm::DenseMap<CachingTypeKey, const TypeLowering *> IndependentTypes;
497-
/// Mapping for types dependent on contextual generic parameters, which is
498-
/// cleared when the generic context is popped.
499-
llvm::DenseMap<CachingTypeKey, const TypeLowering *> DependentTypes;
500486

501487
llvm::DenseMap<SILDeclRef, SILConstantInfo> ConstantTypes;
502488

@@ -577,31 +563,42 @@ class TypeConverter {
577563

578564
/// Lowers a Swift type to a SILType, and returns the SIL TypeLowering
579565
/// for that type.
580-
const TypeLowering &getTypeLowering(Type t, unsigned uncurryLevel = 0) {
581-
AbstractionPattern pattern(CurGenericContext, t->getCanonicalType());
582-
return getTypeLowering(pattern, t, uncurryLevel);
566+
const TypeLowering &getTypeLowering(Type t, unsigned uncurryLevel = 0,
567+
CanGenericSignature genericSig
568+
= CanGenericSignature()) {
569+
if (!genericSig)
570+
genericSig = CurGenericContext;
571+
AbstractionPattern pattern(genericSig, t->getCanonicalType());
572+
return getTypeLowering(pattern, t, uncurryLevel, genericSig);
583573
}
584574

585575
/// Lowers a Swift type to a SILType according to the abstraction
586576
/// patterns of the given original type.
587577
const TypeLowering &getTypeLowering(AbstractionPattern origType,
588578
Type substType,
589-
unsigned uncurryLevel = 0);
579+
unsigned uncurryLevel = 0,
580+
CanGenericSignature genericSig
581+
= CanGenericSignature());
590582

591583
/// Returns the SIL TypeLowering for an already lowered SILType. If the
592584
/// SILType is an address, returns the TypeLowering for the pointed-to
593585
/// type.
594586
const TypeLowering &getTypeLowering(SILType t);
595587

596588
// Returns the lowered SIL type for a Swift type.
597-
SILType getLoweredType(Type t, unsigned uncurryLevel = 0) {
598-
return getTypeLowering(t, uncurryLevel).getLoweredType();
589+
SILType getLoweredType(Type t, unsigned uncurryLevel = 0,
590+
CanGenericSignature genericSig
591+
= CanGenericSignature()) {
592+
return getTypeLowering(t, uncurryLevel, genericSig).getLoweredType();
599593
}
600594

601595
// Returns the lowered SIL type for a Swift type.
602596
SILType getLoweredType(AbstractionPattern origType, Type substType,
603-
unsigned uncurryLevel = 0) {
604-
return getTypeLowering(origType, substType, uncurryLevel).getLoweredType();
597+
unsigned uncurryLevel = 0,
598+
CanGenericSignature genericSig
599+
= CanGenericSignature()) {
600+
return getTypeLowering(origType, substType, uncurryLevel, genericSig)
601+
.getLoweredType();
605602
}
606603

607604
SILType getLoweredLoadableType(Type t, unsigned uncurryLevel = 0) {
@@ -736,24 +733,6 @@ class TypeConverter {
736733
CanGenericSignature getEffectiveGenericSignature(AnyFunctionRef fn,
737734
CaptureInfo captureInfo);
738735

739-
/// Push a generic function context. See GenericContextScope for an RAII
740-
/// interface to this function.
741-
///
742-
/// Types containing generic parameter references must be lowered in a generic
743-
/// context. There can be at most one level of generic context active at any
744-
/// point in time.
745-
void pushGenericContext(CanGenericSignature sig);
746-
747-
/// Return the current generic context. This should only be used in
748-
/// the type-conversion routines.
749-
CanGenericSignature getCurGenericContext() const {
750-
return CurGenericContext;
751-
}
752-
753-
/// Pop a generic function context. See GenericContextScope for an RAII
754-
/// interface to this function. There must be an active generic context.
755-
void popGenericContext(CanGenericSignature sig);
756-
757736
/// Known types for bridging.
758737
#define BRIDGING_KNOWN_TYPE(BridgedModule,BridgedType) \
759738
CanType get##BridgedType##Type();
@@ -797,7 +776,8 @@ class TypeConverter {
797776
CanAnyFunctionType origInterfaceType);
798777
private:
799778
CanType getLoweredRValueType(AbstractionPattern origType, CanType substType,
800-
unsigned uncurryLevel);
779+
unsigned uncurryLevel,
780+
CanGenericSignature genericSig);
801781

802782
Type getLoweredCBridgedType(AbstractionPattern pattern, Type t,
803783
bool canBridgeBool,
@@ -826,26 +806,6 @@ TypeLowering::getSemanticTypeLowering(TypeConverter &TC) const {
826806
return *this;
827807
}
828808

829-
/// RAII interface to push a generic context.
830-
class GenericContextScope {
831-
TypeConverter &TC;
832-
CanGenericSignature Sig;
833-
public:
834-
GenericContextScope(TypeConverter &TC, CanGenericSignature sig)
835-
: TC(TC), Sig(sig)
836-
{
837-
TC.pushGenericContext(sig);
838-
}
839-
840-
~GenericContextScope() {
841-
TC.popGenericContext(Sig);
842-
}
843-
844-
private:
845-
GenericContextScope(const GenericContextScope&) = delete;
846-
GenericContextScope &operator=(const GenericContextScope&) = delete;
847-
};
848-
849809
} // namespace Lowering
850810
} // namespace swift
851811

@@ -860,19 +820,22 @@ namespace llvm {
860820

861821
// Use the second field because the first field can validly be null.
862822
static CachingTypeKey getEmptyKey() {
863-
return {nullptr, APCachingKey(), CanTypeInfo::getEmptyKey(), 0};
823+
return {nullptr, APCachingKey(), nullptr, CanTypeInfo::getEmptyKey(), 0};
864824
}
865825
static CachingTypeKey getTombstoneKey() {
866-
return {nullptr, APCachingKey(), CanTypeInfo::getTombstoneKey(), 0};
826+
return {nullptr, APCachingKey(), nullptr, CanTypeInfo::getTombstoneKey(), 0};
867827
}
868828
static unsigned getHashValue(CachingTypeKey val) {
869-
auto hashSig =
870-
DenseMapInfo<swift::GenericSignature *>::getHashValue(val.Sig);
829+
auto hashOrigSig =
830+
DenseMapInfo<swift::GenericSignature *>::getHashValue(val.OrigSig);
871831
auto hashOrig =
872832
CachingKeyInfo::getHashValue(val.OrigType);
873833
auto hashSubst =
874834
DenseMapInfo<swift::CanType>::getHashValue(val.SubstType);
875-
return hash_combine(hashSig, hashOrig, hashSubst, val.UncurryLevel);
835+
auto hashSubstSig =
836+
DenseMapInfo<swift::GenericSignature *>::getHashValue(val.SubstSig);
837+
return hash_combine(hashOrigSig, hashOrig, hashSubstSig, hashSubst,
838+
val.UncurryLevel);
876839
}
877840
static bool isEqual(CachingTypeKey LHS, CachingTypeKey RHS) {
878841
return LHS == RHS;

lib/AST/GenericEnvironment.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ void *GenericEnvironment::operator new(size_t bytes, const ASTContext &ctx) {
5858
}
5959

6060
Type GenericEnvironment::mapTypeOutOfContext(ModuleDecl *M, Type type) const {
61+
// FIXME: Nuke AllowLoweredTypes
6162
type = type.subst(M, ArchetypeToInterfaceMap, SubstFlags::AllowLoweredTypes);
6263
assert(!type->hasArchetype() && "not fully substituted");
6364
return type;
6465
}
6566

6667
Type GenericEnvironment::mapTypeIntoContext(ModuleDecl *M, Type type) const {
67-
type = type.subst(M, InterfaceToArchetypeMap, SubstFlags::AllowLoweredTypes);
68+
type = type.subst(M, InterfaceToArchetypeMap);
6869
assert((!type->hasTypeParameter() || type->hasError()) &&
6970
"not fully substituted");
7071
return type;

lib/IRGen/GenProto.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ class EmitPolymorphicParameters : public PolymorphicConvention {
448448
private:
449449
CanType getTypeInContext(CanType type) const;
450450

451+
SILType getTypeInContext(SILType type) const;
452+
451453
CanType getArgTypeInContext(unsigned paramIndex) const;
452454

453455
/// Fulfill local type data from any extra information associated with
@@ -476,8 +478,13 @@ CanType EmitPolymorphicParameters::getTypeInContext(CanType type) const {
476478
return Fn.mapTypeIntoContext(type)->getCanonicalType();
477479
}
478480

481+
SILType EmitPolymorphicParameters::getTypeInContext(SILType type) const {
482+
return Fn.mapTypeIntoContext(type);
483+
}
484+
479485
CanType EmitPolymorphicParameters::getArgTypeInContext(unsigned paramIndex) const {
480-
return getTypeInContext(FnType->getParameters()[paramIndex].getType());
486+
return getTypeInContext(FnType->getParameters()[paramIndex].getSILType())
487+
.getSwiftRValueType();
481488
}
482489

483490
void EmitPolymorphicParameters::bindExtraSource(const MetadataSource &source,

lib/IRGen/GenReflection.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -706,22 +706,21 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
706706
std::vector<CanType> CaptureTypes;
707707

708708
for (auto ElementType : getElementTypes()) {
709-
auto SwiftType = ElementType.getSwiftRValueType();
709+
auto InterfaceType = Caller.mapTypeOutOfContext(ElementType);
710+
auto SwiftType = InterfaceType.getSwiftRValueType();
710711

711712
// Erase pseudogeneric captures down to AnyObject.
712713
if (OrigCalleeType->isPseudogeneric()) {
713714
SwiftType = SwiftType.transform([&](Type t) -> Type {
714-
if (auto *archetype = t->getAs<ArchetypeType>()) {
715-
assert(archetype->requiresClass() && "don't know what to do");
715+
if (t->is<GenericTypeParamType>()) {
716716
return IGM.Context.getProtocol(KnownProtocolKind::AnyObject)
717717
->getDeclaredType();
718718
}
719719
return t;
720720
})->getCanonicalType();
721721
}
722722

723-
auto InterfaceType = Caller.mapTypeOutOfContext(SwiftType);
724-
CaptureTypes.push_back(InterfaceType->getCanonicalType());
723+
CaptureTypes.push_back(SwiftType);
725724
}
726725

727726
return CaptureTypes;

lib/IRGen/GenType.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -660,27 +660,23 @@ TypeConverter::~TypeConverter() {
660660
void TypeConverter::pushGenericContext(CanGenericSignature signature) {
661661
if (!signature)
662662
return;
663-
664-
// Push the generic context down to the SIL TypeConverter, so we can share
665-
// archetypes with SIL.
666-
IGM.getSILTypes().pushGenericContext(signature);
663+
assert(!CurGenericContext);
664+
CurGenericContext = signature;
667665
}
668666

669667
void TypeConverter::popGenericContext(CanGenericSignature signature) {
670668
if (!signature)
671669
return;
672-
673-
// Pop the SIL TypeConverter's generic context too.
674-
IGM.getSILTypes().popGenericContext(signature);
675-
670+
assert(signature == CurGenericContext);
671+
CurGenericContext = CanGenericSignature();
676672
Types.DependentCache.clear();
677673
}
678674

679675
ArchetypeBuilder &TypeConverter::getArchetypes() {
676+
assert(CurGenericContext);
680677
auto moduleDecl = IGM.getSwiftModule();
681-
auto genericSig = IGM.getSILTypes().getCurGenericContext();
682678
return *moduleDecl->getASTContext()
683-
.getOrCreateArchetypeBuilder(genericSig, moduleDecl);
679+
.getOrCreateArchetypeBuilder(CurGenericContext, moduleDecl);
684680
}
685681

686682
ArchetypeBuilder &IRGenModule::getContextArchetypes() {

0 commit comments

Comments
 (0)