Skip to content

Commit d434188

Browse files
committed
SIL: Refactor TypeConverter to not require a SILModule
This mostly requires changing various entry points to pass around a TypeConverter instead of a SILModule. I've left behind entry points that take a SILModule for a few methods like SILType::subst() to avoid creating even more churn.
1 parent f487f7b commit d434188

31 files changed

+355
-304
lines changed

include/swift/AST/Types.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4193,10 +4193,6 @@ class SILBoxType final : public TypeBase, public llvm::FoldingSetNode
41934193
SILLayout *getLayout() const { return Layout; }
41944194
SubstitutionMap getSubstitutions() const { return Substitutions; }
41954195

4196-
// In SILType.h:
4197-
CanType getFieldLoweredType(SILModule &M, unsigned index) const;
4198-
SILType getFieldType(SILModule &M, unsigned index) const;
4199-
42004196
// TODO: SILBoxTypes should be explicitly constructed in terms of specific
42014197
// layouts. As a staging mechanism, we expose the old single-boxed-type
42024198
// interface.

include/swift/SIL/Projection.h

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -292,30 +292,7 @@ class Projection {
292292
///
293293
/// WARNING: This is not a constant time operation because it is implemented
294294
/// in terms of getVarDecl, which requests all BaseType's stored properties.
295-
SILType getType(SILType BaseType, SILModule &M) const {
296-
assert(isValid());
297-
switch (getKind()) {
298-
case ProjectionKind::Struct:
299-
case ProjectionKind::Class:
300-
return BaseType.getFieldType(getVarDecl(BaseType), M);
301-
case ProjectionKind::Enum:
302-
return BaseType.getEnumElementType(getEnumElementDecl(BaseType), M);
303-
case ProjectionKind::Box:
304-
return BaseType.castTo<SILBoxType>()->getFieldType(M, getIndex());
305-
case ProjectionKind::Tuple:
306-
return BaseType.getTupleElementType(getIndex());
307-
case ProjectionKind::Upcast:
308-
case ProjectionKind::RefCast:
309-
case ProjectionKind::BitwiseCast:
310-
case ProjectionKind::TailElems:
311-
return getCastType(BaseType);
312-
case ProjectionKind::Index:
313-
// Index types do not change the underlying type.
314-
return BaseType;
315-
}
316-
317-
llvm_unreachable("Unhandled ProjectionKind in switch.");
318-
}
295+
SILType getType(SILType BaseType, SILModule &M) const;
319296

320297
VarDecl *getVarDecl(SILType BaseType) const {
321298
assert(isValid());

include/swift/SIL/SILBuilder.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,13 +1746,7 @@ class SILBuilder {
17461746
getSILDebugLocation(Loc), valueType, operand));
17471747
}
17481748
ProjectBoxInst *createProjectBox(SILLocation Loc, SILValue boxOperand,
1749-
unsigned index) {
1750-
auto boxTy = boxOperand->getType().castTo<SILBoxType>();
1751-
auto fieldTy = boxTy->getFieldType(getModule(), index);
1752-
1753-
return insert(new (getModule()) ProjectBoxInst(
1754-
getSILDebugLocation(Loc), boxOperand, index, fieldTy));
1755-
}
1749+
unsigned index);
17561750
ProjectExistentialBoxInst *createProjectExistentialBox(SILLocation Loc,
17571751
SILType valueTy,
17581752
SILValue boxOperand) {

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,7 @@ visitOpenExistentialMetatypeInst(OpenExistentialMetatypeInst *Inst) {
20892089
remapOpenedType(cast<OpenedArchetypeType>(openedType));
20902090

20912091
if (!Inst->getOperand()->getType().canUseExistentialRepresentation(
2092-
Inst->getModule(), ExistentialRepresentation::Class)) {
2092+
ExistentialRepresentation::Class)) {
20932093
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
20942094
recordClonedInstruction(Inst, getBuilder().createOpenExistentialMetatype(
20952095
getOpLocation(Inst->getLoc()),

include/swift/SIL/SILInstruction.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,9 +1643,7 @@ class AllocBoxInst final
16431643
bool hasDynamicLifetime() const { return dynamicLifetime; }
16441644

16451645
// Return the type of the memory stored in the alloc_box.
1646-
SILType getAddressType() const {
1647-
return getBoxType()->getFieldType(getModule(), 0).getAddressType();
1648-
}
1646+
SILType getAddressType() const;
16491647

16501648
/// Return the underlying variable declaration associated with this
16511649
/// allocation, or null if this is a temporary allocation.

include/swift/SIL/SILType.h

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -228,26 +228,29 @@ class SILType {
228228
/// something of unknown size.
229229
///
230230
/// This is equivalent to, but possibly faster than, calling
231-
/// M.Types.getTypeLowering(type).isAddressOnly().
232-
static bool isAddressOnly(CanType T, SILModule &M,
233-
CanGenericSignature Sig,
234-
ResilienceExpansion Expansion);
231+
/// tc.getTypeLowering(type).isAddressOnly().
232+
static bool isAddressOnly(CanType type, Lowering::TypeConverter &tc,
233+
CanGenericSignature sig,
234+
ResilienceExpansion expansion);
235+
235236
/// Return true if this type must be returned indirectly.
236237
///
237238
/// This is equivalent to, but possibly faster than, calling
238-
/// M.Types.getTypeLowering(type).isReturnedIndirectly().
239-
static bool isFormallyReturnedIndirectly(CanType type, SILModule &M,
240-
CanGenericSignature Sig) {
241-
return isAddressOnly(type, M, Sig, ResilienceExpansion::Minimal);
239+
/// tc.getTypeLowering(type).isReturnedIndirectly().
240+
static bool isFormallyReturnedIndirectly(CanType type,
241+
Lowering::TypeConverter &tc,
242+
CanGenericSignature sig) {
243+
return isAddressOnly(type, tc, sig, ResilienceExpansion::Minimal);
242244
}
243245

244246
/// Return true if this type must be passed indirectly.
245247
///
246248
/// This is equivalent to, but possibly faster than, calling
247-
/// M.Types.getTypeLowering(type).isPassedIndirectly().
248-
static bool isFormallyPassedIndirectly(CanType type, SILModule &M,
249-
CanGenericSignature Sig) {
250-
return isAddressOnly(type, M, Sig, ResilienceExpansion::Minimal);
249+
/// tc.getTypeLowering(type).isPassedIndirectly().
250+
static bool isFormallyPassedIndirectly(CanType type,
251+
Lowering::TypeConverter &tc,
252+
CanGenericSignature sig) {
253+
return isAddressOnly(type, tc, sig, ResilienceExpansion::Minimal);
251254
}
252255

253256
/// True if the type, or the referenced type of an address type, is loadable.
@@ -324,15 +327,13 @@ class SILType {
324327
/// representation kind for the type. Returns None if the type is not an
325328
/// existential type.
326329
ExistentialRepresentation
327-
getPreferredExistentialRepresentation(SILModule &M,
328-
Type containedType = Type()) const;
330+
getPreferredExistentialRepresentation(Type containedType = Type()) const;
329331

330332
/// Returns true if the existential type can use operations for the given
331333
/// existential representation when working with values of the given type,
332334
/// or when working with an unknown type if containedType is null.
333335
bool
334-
canUseExistentialRepresentation(SILModule &M,
335-
ExistentialRepresentation repr,
336+
canUseExistentialRepresentation(ExistentialRepresentation repr,
336337
Type containedType = Type()) const;
337338

338339
/// True if the type contains a type parameter.
@@ -394,11 +395,15 @@ class SILType {
394395
/// the given field. Applies substitutions as necessary. The
395396
/// result will be an address type if the base type is an address
396397
/// type or a class.
398+
SILType getFieldType(VarDecl *field, Lowering::TypeConverter &TC) const;
399+
397400
SILType getFieldType(VarDecl *field, SILModule &M) const;
398401

399402
/// Given that this is an enum type, return the lowered type of the
400403
/// data for the given element. Applies substitutions as necessary.
401404
/// The result will have the same value category as the base type.
405+
SILType getEnumElementType(EnumElementDecl *elt, Lowering::TypeConverter &TC) const;
406+
402407
SILType getEnumElementType(EnumElementDecl *elt, SILModule &M) const;
403408

404409
/// Given that this is a tuple type, return the lowered type of the
@@ -437,19 +442,29 @@ class SILType {
437442
/// generic args with the appropriate item from the substitution.
438443
///
439444
/// Only call this with function types!
445+
SILType substGenericArgs(Lowering::TypeConverter &TC,
446+
SubstitutionMap SubMap) const;
447+
440448
SILType substGenericArgs(SILModule &M,
441449
SubstitutionMap SubMap) const;
442450

443451
/// If the original type is generic, pass the signature as genericSig.
444452
///
445453
/// If the replacement types are generic, you must push a generic context
446454
/// first.
447-
SILType subst(SILModule &silModule, TypeSubstitutionFn subs,
455+
SILType subst(Lowering::TypeConverter &tc, TypeSubstitutionFn subs,
448456
LookupConformanceFn conformances,
449457
CanGenericSignature genericSig = CanGenericSignature(),
450458
bool shouldSubstituteOpaqueArchetypes = false) const;
451459

452-
SILType subst(SILModule &silModule, SubstitutionMap subs) const;
460+
SILType subst(SILModule &M, TypeSubstitutionFn subs,
461+
LookupConformanceFn conformances,
462+
CanGenericSignature genericSig = CanGenericSignature(),
463+
bool shouldSubstituteOpaqueArchetypes = false) const;
464+
465+
SILType subst(Lowering::TypeConverter &tc, SubstitutionMap subs) const;
466+
467+
SILType subst(SILModule &M, SubstitutionMap subs) const;
453468

454469
/// Return true if this type references a "ref" type that has a single pointer
455470
/// representation. Class existentials do not always qualify.
@@ -560,7 +575,7 @@ NON_SIL_TYPE(LValue)
560575
#undef NON_SIL_TYPE
561576

562577
CanSILFunctionType getNativeSILFunctionType(
563-
SILModule &M, Lowering::AbstractionPattern origType,
578+
Lowering::TypeConverter &TC, Lowering::AbstractionPattern origType,
564579
CanAnyFunctionType substType,
565580
Optional<SILDeclRef> origConstant = None,
566581
Optional<SILDeclRef> constant = None,
@@ -581,17 +596,24 @@ static inline llvm::hash_code hash_value(SILType V) {
581596
return llvm::hash_value(V.getOpaqueValue());
582597
}
583598

584-
inline SILType SILBoxType::getFieldType(SILModule &M, unsigned index) const {
585-
return SILType::getPrimitiveAddressType(getFieldLoweredType(M, index));
586-
}
587-
588599
inline SILType SILField::getAddressType() const {
589600
return SILType::getPrimitiveAddressType(getLoweredType());
590601
}
591602
inline SILType SILField::getObjectType() const {
592603
return SILType::getPrimitiveObjectType(getLoweredType());
593604
}
594605

606+
CanType getSILBoxFieldLoweredType(SILBoxType *type,
607+
Lowering::TypeConverter &TC,
608+
unsigned index);
609+
610+
inline SILType getSILBoxFieldType(SILBoxType *type,
611+
Lowering::TypeConverter &TC,
612+
unsigned index) {
613+
return SILType::getPrimitiveAddressType(
614+
getSILBoxFieldLoweredType(type, TC, index));
615+
}
616+
595617
} // end swift namespace
596618

597619
namespace llvm {

include/swift/SIL/TypeLowering.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace swift {
3535
class ForeignErrorConvention;
3636
enum IsInitialization_t : bool;
3737
enum IsTake_t : bool;
38+
class ModuleDecl;
3839
class SILBuilder;
3940
class SILLocation;
4041
class SILModule;
@@ -712,10 +713,10 @@ class TypeConverter {
712713
const TypeLowering *lowering);
713714

714715
public:
715-
SILModule &M;
716+
ModuleDecl &M;
716717
ASTContext &Context;
717718

718-
TypeConverter(SILModule &m);
719+
TypeConverter(ModuleDecl &m);
719720
~TypeConverter();
720721
TypeConverter(TypeConverter const &) = delete;
721722
TypeConverter &operator=(TypeConverter const &) = delete;
@@ -803,7 +804,8 @@ class TypeConverter {
803804
}
804805

805806
SILType getLoweredLoadableType(Type t,
806-
ResilienceExpansion forExpansion) {
807+
ResilienceExpansion forExpansion,
808+
SILModule &M) {
807809
const TypeLowering &ti = getTypeLowering(t, forExpansion);
808810
assert(
809811
(ti.isLoadable() || !SILModuleConventions(M).useLoweredAddresses()) &&

lib/IRGen/GenExistential.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ ContainedAddress irgen::emitBoxedExistentialProjection(IRGenFunction &IGF,
15621562
CanType projectedType) {
15631563
// TODO: Non-ErrorType boxed existentials.
15641564
assert(baseTy.canUseExistentialRepresentation(
1565-
IGF.getSILModule(), ExistentialRepresentation::Boxed, Type()));
1565+
ExistentialRepresentation::Boxed, Type()));
15661566

15671567
// Get the reference to the existential box.
15681568
llvm::Value *box = base.claimNext();
@@ -1616,7 +1616,7 @@ OwnedAddress irgen::emitBoxedExistentialContainerAllocation(IRGenFunction &IGF,
16161616
ArrayRef<ProtocolConformanceRef> conformances) {
16171617
// TODO: Non-Error boxed existentials.
16181618
assert(destType.canUseExistentialRepresentation(
1619-
IGF.getSILModule(), ExistentialRepresentation::Boxed, Type()));
1619+
ExistentialRepresentation::Boxed, Type()));
16201620

16211621
auto &destTI = IGF.getTypeInfo(destType).as<ErrorExistentialTypeInfo>();
16221622
auto srcMetadata = IGF.emitTypeMetadataRef(formalSrcType);
@@ -1656,7 +1656,7 @@ void irgen::emitBoxedExistentialContainerDeallocation(IRGenFunction &IGF,
16561656
CanType valueType) {
16571657
// TODO: Non-Error boxed existentials.
16581658
assert(containerType.canUseExistentialRepresentation(
1659-
IGF.getSILModule(), ExistentialRepresentation::Boxed, Type()));
1659+
ExistentialRepresentation::Boxed, Type()));
16601660

16611661
auto box = container.claimNext();
16621662
auto srcMetadata = IGF.emitTypeMetadataRef(valueType);
@@ -1803,7 +1803,7 @@ void irgen::emitMetatypeOfBoxedExistential(IRGenFunction &IGF, Explosion &value,
18031803
SILType type, Explosion &out) {
18041804
// TODO: Non-Error boxed existentials.
18051805
assert(type.canUseExistentialRepresentation(
1806-
IGF.getSILModule(), ExistentialRepresentation::Boxed, Type()));
1806+
ExistentialRepresentation::Boxed, Type()));
18071807

18081808
// Get the reference to the existential box.
18091809
llvm::Value *box = value.claimNext();

lib/IRGen/GenHeap.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "swift/AST/ASTContext.h"
2929
#include "swift/AST/GenericEnvironment.h"
3030
#include "swift/AST/IRGenOptions.h"
31+
#include "swift/SIL/SILModule.h"
3132

3233
#include "ConstantBuilder.h"
3334
#include "Explosion.h"
@@ -1527,7 +1528,7 @@ const TypeInfo *TypeConverter::convertBoxType(SILBoxType *T) {
15271528
assert(T->getLayout()->getFields().size() == 1
15281529
&& "multi-field boxes not implemented yet");
15291530
auto &eltTI = IGM.getTypeInfoForLowered(
1530-
T->getFieldLoweredType(IGM.getSILModule(), 0));
1531+
getSILBoxFieldLoweredType(T, IGM.getSILModule().Types, 0));
15311532
if (!eltTI.isFixedSize()) {
15321533
if (!NonFixedBoxTI)
15331534
NonFixedBoxTI = new NonFixedBoxTypeInfo(IGM);
@@ -1575,7 +1576,8 @@ const TypeInfo *TypeConverter::convertBoxType(SILBoxType *T) {
15751576
// Produce a tailored box metadata for the type.
15761577
assert(T->getLayout()->getFields().size() == 1
15771578
&& "multi-field boxes not implemented yet");
1578-
return new FixedBoxTypeInfo(IGM, T->getFieldType(IGM.getSILModule(), 0));
1579+
return new FixedBoxTypeInfo(IGM,
1580+
getSILBoxFieldType(T, IGM.getSILModule().Types, 0));
15791581
}
15801582

15811583
OwnedAddress
@@ -1586,8 +1588,8 @@ irgen::emitAllocateBox(IRGenFunction &IGF, CanSILBoxType boxType,
15861588
assert(boxType->getLayout()->getFields().size() == 1
15871589
&& "multi-field boxes not implemented yet");
15881590
return boxTI.allocate(IGF,
1589-
boxType->getFieldType(IGF.IGM.getSILModule(), 0), env,
1590-
name);
1591+
getSILBoxFieldType(boxType, IGF.IGM.getSILModule().Types, 0),
1592+
env, name);
15911593
}
15921594

15931595
void irgen::emitDeallocateBox(IRGenFunction &IGF,
@@ -1597,7 +1599,7 @@ void irgen::emitDeallocateBox(IRGenFunction &IGF,
15971599
assert(boxType->getLayout()->getFields().size() == 1
15981600
&& "multi-field boxes not implemented yet");
15991601
return boxTI.deallocate(IGF, box,
1600-
boxType->getFieldType(IGF.IGM.getSILModule(), 0));
1602+
getSILBoxFieldType(boxType, IGF.IGM.getSILModule().Types, 0));
16011603
}
16021604

16031605
Address irgen::emitProjectBox(IRGenFunction &IGF,
@@ -1607,7 +1609,7 @@ Address irgen::emitProjectBox(IRGenFunction &IGF,
16071609
assert(boxType->getLayout()->getFields().size() == 1
16081610
&& "multi-field boxes not implemented yet");
16091611
return boxTI.project(IGF, box,
1610-
boxType->getFieldType(IGF.IGM.getSILModule(), 0));
1612+
getSILBoxFieldType(boxType, IGF.IGM.getSILModule().Types, 0));
16111613
}
16121614

16131615
Address irgen::emitAllocateExistentialBoxInBuffer(

lib/IRGen/IRGenSIL.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,7 @@ void IRGenSILFunction::visitExistentialMetatypeInst(
20362036
SILValue op = i->getOperand();
20372037
SILType opType = op->getType();
20382038

2039-
switch (opType.getPreferredExistentialRepresentation(IGM.getSILModule())) {
2039+
switch (opType.getPreferredExistentialRepresentation()) {
20402040
case ExistentialRepresentation::Metatype: {
20412041
Explosion existential = getLoweredExplosion(op);
20422042
emitMetatypeOfMetatype(*this, existential, opType, result);
@@ -4188,8 +4188,8 @@ void IRGenSILFunction::visitDeallocBoxInst(swift::DeallocBoxInst *i) {
41884188
void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
41894189
assert(i->getBoxType()->getLayout()->getFields().size() == 1
41904190
&& "multi field boxes not implemented yet");
4191-
const TypeInfo &type = getTypeInfo(i->getBoxType()
4192-
->getFieldType(IGM.getSILModule(), 0));
4191+
const TypeInfo &type = getTypeInfo(
4192+
getSILBoxFieldType(i->getBoxType(), IGM.getSILModule().Types, 0));
41934193

41944194
// Derive name from SIL location.
41954195
bool IsAnonymous = false;
@@ -4224,7 +4224,7 @@ void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
42244224

42254225
assert(i->getBoxType()->getLayout()->getFields().size() == 1 &&
42264226
"box for a local variable should only have one field");
4227-
auto SILTy = i->getBoxType()->getFieldType(IGM.getSILModule(), 0);
4227+
auto SILTy = getSILBoxFieldType(i->getBoxType(), IGM.getSILModule().Types, 0);
42284228
auto RealType = SILTy.getASTType();
42294229
auto DbgTy = DebugTypeInfo::getLocalVariable(Decl, RealType, type);
42304230

0 commit comments

Comments
 (0)