Skip to content

Commit 9ecda0c

Browse files
committed
SIL: Plumb TypeExpansionContext through SIL
1 parent e67b961 commit 9ecda0c

21 files changed

+315
-202
lines changed

include/swift/SIL/Projection.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ class Projection {
293293
///
294294
/// WARNING: This is not a constant time operation because it is implemented
295295
/// in terms of getVarDecl, which requests all BaseType's stored properties.
296-
SILType getType(SILType BaseType, SILModule &M) const;
296+
SILType getType(SILType BaseType, SILModule &M,
297+
TypeExpansionContext context) const;
297298

298299
VarDecl *getVarDecl(SILType BaseType) const {
299300
assert(isValid());
@@ -402,6 +403,7 @@ class Projection {
402403
/// Given a specific SILType, return all first level projections if it is an
403404
/// aggregate.
404405
static void getFirstLevelProjections(SILType V, SILModule &Mod,
406+
TypeExpansionContext context,
405407
llvm::SmallVectorImpl<Projection> &Out);
406408

407409
/// Is this cast which only allows for equality?
@@ -584,6 +586,7 @@ class ProjectionPath {
584586
/// is a leaf node in the type tree.
585587
static void expandTypeIntoLeafProjectionPaths(SILType BaseType,
586588
SILModule *Mod,
589+
TypeExpansionContext context,
587590
ProjectionPathList &P);
588591

589592
/// Return true if the given projection paths in \p CPaths does not cover
@@ -626,26 +629,27 @@ class ProjectionPath {
626629
SILType getBaseType() const { return BaseType; }
627630

628631
/// Returns the most derived type of the projection path.
629-
SILType getMostDerivedType(SILModule &M) {
632+
SILType getMostDerivedType(SILModule &M, TypeExpansionContext context) {
630633
if (Path.empty())
631634
return getBaseType();
632635
if (MostDerivedType)
633636
return MostDerivedType;
634-
MostDerivedType = getDerivedType(Path.size(), M);
637+
MostDerivedType = getDerivedType(Path.size(), M, context);
635638
return MostDerivedType;
636639
}
637640

638641
/// Returns the ith derived type of the path. This is zero indexed with 0
639642
/// being the base type and n consisting of applying the up to n projections
640643
/// to the base type.
641-
SILType getDerivedType(unsigned i, SILModule &M) const {
644+
SILType getDerivedType(unsigned i, SILModule &M,
645+
TypeExpansionContext context) const {
642646
assert(i <= Path.size());
643647
SILType IterTy = getBaseType();
644648
if (i == 0)
645649
return IterTy;
646650
for (unsigned j : range(i)) {
647651
auto &Proj = Path[j];
648-
IterTy = Proj.getType(IterTy, M);
652+
IterTy = Proj.getType(IterTy, M, context);
649653
}
650654
return IterTy;
651655
}
@@ -671,10 +675,11 @@ class ProjectionPath {
671675
const_reverse_iterator rbegin() const { return Path.rbegin(); }
672676
const_reverse_iterator rend() const { return Path.rend(); }
673677

674-
void verify(SILModule &M);
678+
void verify(SILModule &M, TypeExpansionContext context);
675679

676-
raw_ostream &print(raw_ostream &OS, SILModule &M) const;
677-
void dump(SILModule &M) const;
680+
raw_ostream &print(raw_ostream &OS, SILModule &M,
681+
TypeExpansionContext context) const;
682+
void dump(SILModule &M, TypeExpansionContext context) const;
678683
};
679684

680685
/// Returns the hashcode for the new projection path.
@@ -813,9 +818,11 @@ class ProjectionTreeNode {
813818
llvm::SmallVectorImpl<ValueNodePair> &Worklist,
814819
SILValue Value);
815820

816-
void createNextLevelChildren(ProjectionTree &Tree);
821+
void createNextLevelChildren(ProjectionTree &Tree, TypeExpansionContext context);
817822

818-
void createNextLevelChildrenForStruct(ProjectionTree &Tree, StructDecl *SD);
823+
void createNextLevelChildrenForStruct(ProjectionTree &Tree,
824+
TypeExpansionContext context,
825+
StructDecl *SD);
819826

820827
void createNextLevelChildrenForTuple(ProjectionTree &Tree, TupleType *TT);
821828
};

include/swift/SIL/SILBasicBlock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
214214
/// Allocate a new argument of type \p Ty and append it to the argument
215215
/// list. Optionally you can pass in a value decl parameter.
216216
SILFunctionArgument *createFunctionArgument(SILType Ty,
217-
const ValueDecl *D = nullptr);
217+
const ValueDecl *D = nullptr,
218+
bool disableEntryBlockVerification = false);
218219

219220
SILFunctionArgument *insertFunctionArgument(unsigned Index, SILType Ty,
220221
ValueOwnershipKind OwnershipKind,

include/swift/SIL/SILBuilder.h

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,23 @@ class SILBuilder {
197197
assert(F && "cannot create this instruction without a function context");
198198
return *F;
199199
}
200+
201+
TypeExpansionContext getTypeExpansionContext() const {
202+
return TypeExpansionContext(getFunction());
203+
}
204+
200205
SILBuilderContext &getBuilderContext() const { return C; }
201206
SILModule &getModule() const { return C.Module; }
202207
ASTContext &getASTContext() const { return getModule().getASTContext(); }
203208
const Lowering::TypeLowering &getTypeLowering(SILType T) const {
204-
auto expansion = ResilienceExpansion::Maximal;
209+
210+
auto expansion = TypeExpansionContext::maximal(getModule().getSwiftModule(),
211+
getModule().isWholeModule());
205212
// If there's no current SILFunction, we're inserting into a global
206213
// variable initializer.
207-
if (F)
208-
expansion = F->getResilienceExpansion();
209-
214+
if (F) {
215+
expansion = TypeExpansionContext(getFunction());
216+
}
210217
return getModule().Types.getTypeLowering(T, expansion);
211218
}
212219

@@ -336,12 +343,11 @@ class SILBuilder {
336343
// Type remapping
337344
//===--------------------------------------------------------------------===//
338345

339-
static SILType
340-
getPartialApplyResultType(SILType Ty, unsigned ArgCount, SILModule &M,
341-
SubstitutionMap subs,
342-
ParameterConvention calleeConvention,
343-
PartialApplyInst::OnStackKind onStack =
344-
PartialApplyInst::OnStackKind::NotOnStack);
346+
static SILType getPartialApplyResultType(
347+
TypeExpansionContext context, SILType Ty, unsigned ArgCount, SILModule &M,
348+
SubstitutionMap subs, ParameterConvention calleeConvention,
349+
PartialApplyInst::OnStackKind onStack =
350+
PartialApplyInst::OnStackKind::NotOnStack);
345351

346352
//===--------------------------------------------------------------------===//
347353
// CFG Manipulation
@@ -574,7 +580,8 @@ class SILBuilder {
574580
FunctionRefBaseInst *createFunctionRefFor(SILLocation Loc, SILFunction *f) {
575581
if (f->isDynamicallyReplaceable())
576582
return createDynamicFunctionRef(Loc, f);
577-
else return createFunctionRef(Loc, f);
583+
else
584+
return createFunctionRef(Loc, f);
578585
}
579586

580587
FunctionRefBaseInst *createFunctionRef(SILLocation Loc, SILFunction *f,
@@ -590,37 +597,37 @@ class SILBuilder {
590597
}
591598

592599
FunctionRefInst *createFunctionRef(SILLocation Loc, SILFunction *f) {
593-
return insert(new (getModule())
594-
FunctionRefInst(getSILDebugLocation(Loc), f));
600+
return insert(new (getModule()) FunctionRefInst(getSILDebugLocation(Loc), f,
601+
getTypeExpansionContext()));
595602
}
596603

597604
DynamicFunctionRefInst *
598605
createDynamicFunctionRef(SILLocation Loc, SILFunction *f) {
599606
return insert(new (getModule()) DynamicFunctionRefInst(
600-
getSILDebugLocation(Loc), f));
607+
getSILDebugLocation(Loc), f, getTypeExpansionContext()));
601608
}
602609

603610
PreviousDynamicFunctionRefInst *
604611
createPreviousDynamicFunctionRef(SILLocation Loc, SILFunction *f) {
605612
return insert(new (getModule()) PreviousDynamicFunctionRefInst(
606-
getSILDebugLocation(Loc), f));
613+
getSILDebugLocation(Loc), f, getTypeExpansionContext()));
607614
}
608615

609616
AllocGlobalInst *createAllocGlobal(SILLocation Loc, SILGlobalVariable *g) {
610617
return insert(new (getModule())
611618
AllocGlobalInst(getSILDebugLocation(Loc), g));
612619
}
613620
GlobalAddrInst *createGlobalAddr(SILLocation Loc, SILGlobalVariable *g) {
614-
return insert(new (getModule())
615-
GlobalAddrInst(getSILDebugLocation(Loc), g));
621+
return insert(new (getModule()) GlobalAddrInst(getSILDebugLocation(Loc), g,
622+
getTypeExpansionContext()));
616623
}
617624
GlobalAddrInst *createGlobalAddr(SILLocation Loc, SILType Ty) {
618625
return insert(new (F->getModule())
619626
GlobalAddrInst(getSILDebugLocation(Loc), Ty));
620627
}
621628
GlobalValueInst *createGlobalValue(SILLocation Loc, SILGlobalVariable *g) {
622-
return insert(new (getModule())
623-
GlobalValueInst(getSILDebugLocation(Loc), g));
629+
return insert(new (getModule()) GlobalValueInst(getSILDebugLocation(Loc), g,
630+
getTypeExpansionContext()));
624631
}
625632
IntegerLiteralInst *createIntegerLiteral(IntegerLiteralExpr *E);
626633

@@ -1285,8 +1292,8 @@ class SILBuilder {
12851292
UncheckedEnumDataInst *createUncheckedEnumData(SILLocation Loc,
12861293
SILValue Operand,
12871294
EnumElementDecl *Element) {
1288-
SILType EltType =
1289-
Operand->getType().getEnumElementType(Element, getModule());
1295+
SILType EltType = Operand->getType().getEnumElementType(
1296+
Element, getModule(), getTypeExpansionContext());
12901297
return createUncheckedEnumData(Loc, Operand, Element, EltType);
12911298
}
12921299

@@ -1307,8 +1314,8 @@ class SILBuilder {
13071314
UncheckedTakeEnumDataAddrInst *
13081315
createUncheckedTakeEnumDataAddr(SILLocation Loc, SILValue Operand,
13091316
EnumElementDecl *Element) {
1310-
SILType EltType =
1311-
Operand->getType().getEnumElementType(Element, getModule());
1317+
SILType EltType = Operand->getType().getEnumElementType(
1318+
Element, getModule(), getTypeExpansionContext());
13121319
return createUncheckedTakeEnumDataAddr(Loc, Operand, Element, EltType);
13131320
}
13141321

@@ -1383,7 +1390,8 @@ class SILBuilder {
13831390

13841391
StructExtractInst *createStructExtract(SILLocation Loc, SILValue Operand,
13851392
VarDecl *Field) {
1386-
auto type = Operand->getType().getFieldType(Field, getModule());
1393+
auto type = Operand->getType().getFieldType(Field, getModule(),
1394+
getTypeExpansionContext());
13871395
return createStructExtract(Loc, Operand, Field, type);
13881396
}
13891397

@@ -1397,7 +1405,8 @@ class SILBuilder {
13971405

13981406
StructElementAddrInst *
13991407
createStructElementAddr(SILLocation Loc, SILValue Operand, VarDecl *Field) {
1400-
auto ResultTy = Operand->getType().getFieldType(Field, getModule());
1408+
auto ResultTy = Operand->getType().getFieldType(Field, getModule(),
1409+
getTypeExpansionContext());
14011410
return createStructElementAddr(Loc, Operand, Field, ResultTy);
14021411
}
14031412

@@ -1408,7 +1417,8 @@ class SILBuilder {
14081417
}
14091418
RefElementAddrInst *createRefElementAddr(SILLocation Loc, SILValue Operand,
14101419
VarDecl *Field) {
1411-
auto ResultTy = Operand->getType().getFieldType(Field, getModule());
1420+
auto ResultTy = Operand->getType().getFieldType(Field, getModule(),
1421+
getTypeExpansionContext());
14121422
return createRefElementAddr(Loc, Operand, Field, ResultTy);
14131423
}
14141424

@@ -2131,7 +2141,8 @@ class SILBuilder {
21312141

21322142
SILValue emitStructExtract(SILLocation Loc, SILValue Operand,
21332143
VarDecl *Field) {
2134-
auto type = Operand->getType().getFieldType(Field, getModule());
2144+
auto type = Operand->getType().getFieldType(Field, getModule(),
2145+
getTypeExpansionContext());
21352146
return emitStructExtract(Loc, Operand, Field, type);
21362147
}
21372148

include/swift/SIL/SILCloner.h

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
128128
/// blocks.
129129
///
130130
/// This is used to clone an entire function and should not mutate the
131-
/// original function.
131+
/// original function except if \p replaceOriginalFunctionInPlace is true.
132132
///
133133
/// entryArgs must have a SILValue from the cloned function corresponding to
134134
/// each argument in the original function `F`.
135135
///
136136
/// Cloned instructions are inserted starting at the end of clonedEntryBB.
137137
void cloneFunctionBody(SILFunction *F, SILBasicBlock *clonedEntryBB,
138-
ArrayRef<SILValue> entryArgs);
138+
ArrayRef<SILValue> entryArgs,
139+
bool replaceOriginalFunctionInPlace = false);
139140

140141
/// MARK: Callback utilities used from CRTP extensions during cloning.
141142
/// These should only be called from within an instruction cloning visitor.
@@ -354,8 +355,14 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
354355

355356
SILLocation remapLocation(SILLocation Loc) { return Loc; }
356357
const SILDebugScope *remapScope(const SILDebugScope *DS) { return DS; }
357-
SILType remapType(SILType Ty) { return Ty; }
358-
CanType remapASTType(CanType Ty) { return Ty; }
358+
SILType remapType(SILType Ty) {
359+
return Ty;
360+
}
361+
362+
CanType remapASTType(CanType Ty) {
363+
return Ty;
364+
}
365+
359366
ProtocolConformanceRef remapConformance(Type Ty, ProtocolConformanceRef C) {
360367
return C;
361368
}
@@ -612,9 +619,11 @@ void SILCloner<ImplClass>::cloneReachableBlocks(
612619
template <typename ImplClass>
613620
void SILCloner<ImplClass>::cloneFunctionBody(SILFunction *F,
614621
SILBasicBlock *clonedEntryBB,
615-
ArrayRef<SILValue> entryArgs) {
622+
ArrayRef<SILValue> entryArgs,
623+
bool replaceOriginalFunctionInPlace) {
616624

617-
assert(F != clonedEntryBB->getParent() && "Must clone into a new function.");
625+
assert((replaceOriginalFunctionInPlace || F != clonedEntryBB->getParent()) &&
626+
"Must clone into a new function.");
618627
assert(BBMap.empty() && "This API does not allow clients to map blocks.");
619628
assert(ValueMap.empty() && "Stale ValueMap.");
620629

@@ -972,9 +981,9 @@ SILCloner<ImplClass>::visitFunctionRefInst(FunctionRefInst *Inst) {
972981
getOpLocation(Inst->getLoc()), OpFunction));
973982
}
974983

975-
template<typename ImplClass>
976-
void
977-
SILCloner<ImplClass>::visitDynamicFunctionRefInst(DynamicFunctionRefInst *Inst) {
984+
template <typename ImplClass>
985+
void SILCloner<ImplClass>::visitDynamicFunctionRefInst(
986+
DynamicFunctionRefInst *Inst) {
978987
SILFunction *OpFunction =
979988
getOpFunction(Inst->getInitiallyReferencedFunction());
980989
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
@@ -2049,8 +2058,11 @@ SILCloner<ImplClass>::visitWitnessMethodInst(WitnessMethodInst *Inst) {
20492058
CanType Ty = conformance.getConcrete()->getType()->getCanonicalType();
20502059

20512060
if (Ty != newLookupType) {
2052-
assert(Ty->isExactSuperclassOf(newLookupType) &&
2053-
"Should only create upcasts for sub class.");
2061+
assert(
2062+
(Ty->isExactSuperclassOf(newLookupType) ||
2063+
getBuilder().getModule().Types.getLoweredRValueType(
2064+
getBuilder().getTypeExpansionContext(), Ty) == newLookupType) &&
2065+
"Should only create upcasts for sub class.");
20542066

20552067
// We use the super class as the new look up type.
20562068
newLookupType = Ty;

include/swift/SIL/SILFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ class SILFunction
515515

516516
SILType getLoweredLoadableType(Type t) const;
517517

518+
SILType getLoweredType(SILType t) const;
519+
518520
const Lowering::TypeLowering &getTypeLowering(SILType type) const;
519521

520522
bool isTypeABIAccessible(SILType type) const;

include/swift/SIL/SILGlobalVariable.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ class SILGlobalVariable
105105
CanSILFunctionType getLoweredFunctionType() const {
106106
return LoweredType.castTo<SILFunctionType>();
107107
}
108-
108+
SILType getLoweredTypeInContext(TypeExpansionContext context) const;
109+
CanSILFunctionType
110+
getLoweredFunctionTypeInContext(TypeExpansionContext context) const {
111+
return getLoweredTypeInContext(context).castTo<SILFunctionType>();
112+
}
113+
109114
StringRef getName() const { return Name; }
110115

111116
void setDeclaration(bool isD) { IsDeclaration = isD; }

0 commit comments

Comments
 (0)