Skip to content

Commit 0926d23

Browse files
committed
SIL: Sink GenericContextScope into IRGen.
All the context dependencies in SIL type lowering have been eradicated, but IRGen's type info lowering is still context-dependent and doesn't systemically pass generic contexts around. Sink GenericContextScope bookkeeping entirely into IRGen for now.
1 parent 5140174 commit 0926d23

File tree

15 files changed

+29
-137
lines changed

15 files changed

+29
-137
lines changed

include/swift/SIL/TypeLowering.h

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -654,20 +654,6 @@ class TypeConverter {
654654
/// Mapping for types independent on contextual generic parameters.
655655
llvm::DenseMap<CachingTypeKey, const TypeLowering *> LoweredTypes;
656656

657-
struct DependentTypeState {
658-
CanGenericSignature Sig;
659-
660-
explicit DependentTypeState(CanGenericSignature sig) : Sig(sig) {}
661-
662-
DependentTypeState(DependentTypeState &&) = default;
663-
664-
// No copy constructor or assignment.
665-
DependentTypeState(const DependentTypeState &) = delete;
666-
void operator=(const DependentTypeState &) = delete;
667-
};
668-
669-
llvm::SmallVector<DependentTypeState, 1> DependentTypes;
670-
671657
llvm::DenseMap<std::pair<TypeExpansionContext, SILDeclRef>, SILConstantInfo *>
672658
ConstantTypes;
673659

@@ -950,26 +936,6 @@ class TypeConverter {
950936
AbstractStorageDecl *value,
951937
Type lvalueType);
952938

953-
/// Push a generic function context. See GenericContextScope for an RAII
954-
/// interface to this function.
955-
///
956-
/// Types containing generic parameter references must be lowered in a generic
957-
/// context. There can be at most one level of generic context active at any
958-
/// point in time.
959-
void pushGenericContext(CanGenericSignature sig);
960-
961-
/// Return the current generic context. This should only be used in
962-
/// the type-conversion routines.
963-
CanGenericSignature getCurGenericContext() const {
964-
if (DependentTypes.empty())
965-
return CanGenericSignature();
966-
return DependentTypes.back().Sig;
967-
}
968-
969-
/// Pop a generic function context. See GenericContextScope for an RAII
970-
/// interface to this function. There must be an active generic context.
971-
void popGenericContext(CanGenericSignature sig);
972-
973939
/// Known types for bridging.
974940
#define BRIDGING_KNOWN_TYPE(BridgedModule,BridgedType) \
975941
CanType get##BridgedType##Type();
@@ -1080,26 +1046,6 @@ class TypeConverter {
10801046
bool suppressOptional);
10811047
};
10821048

1083-
/// RAII interface to push a generic context.
1084-
class GenericContextScope {
1085-
TypeConverter &TC;
1086-
CanGenericSignature Sig;
1087-
public:
1088-
GenericContextScope(TypeConverter &TC, CanGenericSignature sig)
1089-
: TC(TC), Sig(sig)
1090-
{
1091-
TC.pushGenericContext(sig);
1092-
}
1093-
1094-
~GenericContextScope() {
1095-
TC.popGenericContext(Sig);
1096-
}
1097-
1098-
private:
1099-
GenericContextScope(const GenericContextScope&) = delete;
1100-
GenericContextScope &operator=(const GenericContextScope&) = delete;
1101-
};
1102-
11031049
} // namespace Lowering
11041050
} // namespace swift
11051051

lib/IRGen/GenType.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,13 +1262,11 @@ TypeConverter::~TypeConverter() {
12621262
}
12631263
}
12641264

1265-
void TypeConverter::pushGenericContext(CanGenericSignature signature) {
1265+
void TypeConverter::setGenericContext(CanGenericSignature signature) {
12661266
if (!signature)
12671267
return;
12681268

1269-
// Push the generic context down to the SIL TypeConverter, so we can share
1270-
// archetypes with SIL.
1271-
IGM.getSILTypes().pushGenericContext(signature);
1269+
CurGenericSignature = signature;
12721270

12731271
// Clear the dependent type info cache since we have a new active signature
12741272
// now.
@@ -1277,21 +1275,12 @@ void TypeConverter::pushGenericContext(CanGenericSignature signature) {
12771275
Types.getCacheFor(/*isDependent*/ true, Mode::CompletelyFragile).clear();
12781276
}
12791277

1280-
void TypeConverter::popGenericContext(CanGenericSignature signature) {
1281-
if (!signature)
1282-
return;
1283-
1284-
// Pop the SIL TypeConverter's generic context too.
1285-
IGM.getSILTypes().popGenericContext(signature);
1286-
1287-
Types.getCacheFor(/*isDependent*/ true, Mode::Normal).clear();
1288-
Types.getCacheFor(/*isDependent*/ true, Mode::Legacy).clear();
1289-
Types.getCacheFor(/*isDependent*/ true, Mode::CompletelyFragile).clear();
1278+
CanGenericSignature IRGenModule::getCurGenericContext() {
1279+
return Types.getCurGenericContext();
12901280
}
12911281

12921282
GenericEnvironment *TypeConverter::getGenericEnvironment() {
1293-
auto genericSig = IGM.getSILTypes().getCurGenericContext();
1294-
return genericSig->getCanonicalSignature()->getGenericEnvironment();
1283+
return CurGenericSignature->getGenericEnvironment();
12951284
}
12961285

12971286
GenericEnvironment *IRGenModule::getGenericEnvironment() {

lib/IRGen/GenType.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace swift {
5353

5454
namespace irgen {
5555
class Alignment;
56+
class GenericContextScope;
5657
class ProtocolInfo;
5758
class Size;
5859
class FixedTypeInfo;
@@ -86,6 +87,13 @@ class TypeConverter {
8687

8788
IRGenModule &IGM;
8889
private:
90+
// Set using the GenericContextScope RAII object.
91+
friend GenericContextScope;
92+
CanGenericSignature CurGenericSignature;
93+
// Enter a generic context for lowering the parameters of a generic function
94+
// type.
95+
void setGenericContext(CanGenericSignature signature);
96+
8997
Mode LoweringMode = Mode::Normal;
9098

9199
llvm::DenseMap<ProtocolDecl*, std::unique_ptr<const ProtocolInfo>> Protocols;
@@ -187,13 +195,10 @@ class TypeConverter {
187195

188196
llvm::Type *getExistentialType(unsigned numWitnessTables);
189197

190-
/// Enter a generic context for lowering the parameters of a generic function
191-
/// type.
192-
void pushGenericContext(CanGenericSignature signature);
198+
/// Retrieve the generic signature for the current generic context, or null if no
199+
/// generic environment is active.
200+
CanGenericSignature getCurGenericContext() { return CurGenericSignature; }
193201

194-
/// Exit a generic context.
195-
void popGenericContext(CanGenericSignature signature);
196-
197202
/// Retrieve the generic environment for the current generic context.
198203
///
199204
/// Fails if there is no generic context.
@@ -235,20 +240,23 @@ class TypeConverter {
235240
/// a scope.
236241
class GenericContextScope {
237242
TypeConverter &TC;
238-
CanGenericSignature sig;
243+
CanGenericSignature newSig, oldSig;
239244
public:
240245
GenericContextScope(TypeConverter &TC, CanGenericSignature sig)
241-
: TC(TC), sig(sig)
246+
: TC(TC), newSig(sig), oldSig(TC.CurGenericSignature)
242247
{
243-
TC.pushGenericContext(sig);
248+
TC.setGenericContext(newSig);
244249
}
245250

246251
GenericContextScope(IRGenModule &IGM, CanGenericSignature sig)
247252
: GenericContextScope(IGM.Types, sig)
248253
{}
249254

250255
~GenericContextScope() {
251-
TC.popGenericContext(sig);
256+
if (!newSig)
257+
return;
258+
assert(TC.CurGenericSignature == newSig);
259+
TC.setGenericContext(oldSig);
252260
}
253261
};
254262

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
841841
llvm::DINode::DIFlags Flags, unsigned &SizeInBits) {
842842
SmallVector<llvm::Metadata *, 16> Elements;
843843
unsigned OffsetInBits = 0;
844-
auto genericSig = IGM.getSILTypes().getCurGenericContext();
844+
auto genericSig = IGM.getCurGenericContext();
845845
for (auto ElemTy : TupleTy->getElementTypes()) {
846846
auto &elemTI = IGM.getTypeInfoForUnlowered(
847847
AbstractionPattern(genericSig, ElemTy->getCanonicalType()), ElemTy);

lib/IRGen/IRGenModule.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ class IRGenModule {
858858

859859
private:
860860
TypeConverter &Types;
861-
friend class TypeConverter;
861+
friend TypeConverter;
862862

863863
const clang::ASTContext *ClangASTContext;
864864
ClangTypeConverter *ClangTypes;
@@ -1423,6 +1423,10 @@ private: \
14231423

14241424
Address getAddrOfObjCISAMask();
14251425

1426+
/// Retrieve the generic signature for the current generic context, or null if no
1427+
/// generic environment is active.
1428+
CanGenericSignature getCurGenericContext();
1429+
14261430
/// Retrieve the generic environment for the current generic context.
14271431
///
14281432
/// Fails if there is no generic context.

lib/ParseSIL/ParseSIL.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3175,9 +3175,6 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
31753175
SmallVector<SILValue, 4> operands;
31763176

31773177
if (P.consumeIf(tok::l_paren)) {
3178-
Lowering::GenericContextScope scope(SILMod.Types,
3179-
patternEnv ? patternEnv->getGenericSignature()->getCanonicalSignature()
3180-
: nullptr);
31813178
while (true) {
31823179
SILValue v;
31833180

lib/SIL/SILType.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ SILResultInfo::getOwnershipKind(SILFunction &F) const {
422422
auto &M = F.getModule();
423423
auto FTy = F.getLoweredFunctionType();
424424
auto sig = FTy->getInvocationGenericSignature();
425-
GenericContextScope GCS(M.Types, sig);
426425

427426
bool IsTrivial = getSILStorageType(M, FTy).isTrivial(F);
428427
switch (getConvention()) {

lib/SIL/SILVerifier.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4478,9 +4478,6 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
44784478
"keypath value type should match value type of keypath pattern");
44794479

44804480
{
4481-
Lowering::GenericContextScope scope(F.getModule().Types,
4482-
pattern->getGenericSignature());
4483-
44844481
for (auto &component : pattern->getComponents()) {
44854482
bool hasIndices;
44864483
switch (component.getKind()) {
@@ -5130,7 +5127,6 @@ void SILProperty::verify(const SILModule &M) const {
51305127
}
51315128

51325129
auto canSig = sig ? sig->getCanonicalSignature() : nullptr;
5133-
Lowering::GenericContextScope scope(M.Types, canSig);
51345130

51355131
auto require = [&](bool reqt, StringRef message) {
51365132
if (!reqt) {

lib/SIL/TypeLowering.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,26 +2144,6 @@ SILType TypeConverter::getSubstitutedStorageType(TypeExpansionContext context,
21442144
return SILType::getPrimitiveAddressType(substLoweredType);
21452145
}
21462146

2147-
void TypeConverter::pushGenericContext(CanGenericSignature sig) {
2148-
// If the generic signature is empty, this is a no-op.
2149-
if (!sig)
2150-
return;
2151-
2152-
DependentTypeState state(sig);
2153-
DependentTypes.push_back(std::move(state));
2154-
}
2155-
2156-
void TypeConverter::popGenericContext(CanGenericSignature sig) {
2157-
// If the generic signature is empty, this is a no-op.
2158-
if (!sig)
2159-
return;
2160-
2161-
DependentTypeState &state = DependentTypes.back();
2162-
assert(state.Sig == sig && "unpaired push/pop");
2163-
2164-
DependentTypes.pop_back();
2165-
}
2166-
21672147
ProtocolDispatchStrategy
21682148
TypeConverter::getProtocolDispatchStrategy(ProtocolDecl *P) {
21692149
// ObjC protocols use ObjC method dispatch, and Swift protocols
@@ -2668,8 +2648,6 @@ TypeConverter::getInterfaceBoxTypeForCapture(ValueDecl *captured,
26682648

26692649
auto boxTy = SILBoxType::get(C, layout, subMap);
26702650
#ifndef NDEBUG
2671-
// FIXME: Map the box type out of context when asserting the field so
2672-
// we don't need to push a GenericContextScope (which really ought to die).
26732651
auto loweredContextType = loweredInterfaceType;
26742652
auto contextBoxTy = boxTy;
26752653
if (signature) {
@@ -2738,7 +2716,6 @@ CanSILBoxType TypeConverter::getBoxTypeForEnumElement(
27382716

27392717
// Lower the enum element's argument in the box's context.
27402718
auto eltIntfTy = elt->getArgumentInterfaceType();
2741-
GenericContextScope scope(*this, boxSignature);
27422719

27432720
auto boxVarTy = getLoweredRValueType(context,
27442721
getAbstractionPattern(elt), eltIntfTy);

lib/SILGen/SILGenBridging.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ emitBridgeObjectiveCToNative(SILGenFunction &SGF,
229229
auto subs = witness.getSubstitutions();
230230

231231
// Set up the generic signature, since formalResultTy is an interface type.
232-
GenericContextScope genericContextScope(SGF.SGM.Types, genericSig);
233232
CalleeTypeInfo calleeTypeInfo(
234233
witnessFnTy,
235234
AbstractionPattern(genericSig, formalResultTy),

0 commit comments

Comments
 (0)