Skip to content

Commit 3419662

Browse files
committed
[silgen] Split off SILGenSILBuilder from SILGenBuilder and have SILGenBuilder inherit from it.
This prevents ManagedValue APIs on SILGenBuilder from by mistake accesing non-conformance tracking APIs on SILBuilder. NOTE: Eventually, we should make SILGenBuilder compose with SILGenSILBuilder and then rename SILGenBuilder to ManagedValueBuilder. I did not do that in this PR since it becomes very disruptive since there is still a lot of code in SILGen that uses SILValue APIs and I have not found a concise way to write such code. But this patch at least defines away this error which has bitten us before.
1 parent 1e41b13 commit 3419662

File tree

5 files changed

+348
-269
lines changed

5 files changed

+348
-269
lines changed

lib/SILGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_swift_host_library(swiftSILGen STATIC
2828
SILGenPoly.cpp
2929
SILGenProlog.cpp
3030
SILGenStmt.cpp
31+
SILGenSILBuilder.cpp
3132
SILGenThunk.cpp
3233
SILGenType.cpp)
3334
target_link_libraries(swiftSILGen PRIVATE

lib/SILGen/SILGenBuilder.cpp

Lines changed: 11 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -33,153 +33,15 @@ SILGenModule &SILGenBuilder::getSILGenModule() const { return SGF.SGM; }
3333
//===----------------------------------------------------------------------===//
3434

3535
SILGenBuilder::SILGenBuilder(SILGenFunction &SGF)
36-
: SILBuilder(SGF.F), SGF(SGF) {}
36+
: SILGenSILBuilder(SGF), SGF(SGF) {}
3737

3838
SILGenBuilder::SILGenBuilder(SILGenFunction &SGF, SILBasicBlock *insertBB,
3939
SmallVectorImpl<SILInstruction *> *insertedInsts)
40-
: SILBuilder(insertBB, insertedInsts), SGF(SGF) {}
40+
: SILGenSILBuilder(SGF, insertBB, insertedInsts), SGF(SGF) {}
4141

4242
SILGenBuilder::SILGenBuilder(SILGenFunction &SGF, SILBasicBlock *insertBB,
4343
SILBasicBlock::iterator insertInst)
44-
: SILBuilder(insertBB, insertInst), SGF(SGF) {}
45-
46-
//===----------------------------------------------------------------------===//
47-
// Instruction Emission + Conformance Endowed APIs
48-
//===----------------------------------------------------------------------===//
49-
//
50-
// This section contains wrappers around SILBuilder SILValue APIs that add extra
51-
// conformances. These are the only places where we should be accessing
52-
// SILBuilder APIs directly.
53-
//
54-
55-
MetatypeInst *SILGenBuilder::createMetatype(SILLocation loc, SILType metatype) {
56-
auto theMetatype = metatype.castTo<MetatypeType>();
57-
// Getting a nontrivial metatype requires forcing any conformances necessary
58-
// to instantiate the type.
59-
switch (theMetatype->getRepresentation()) {
60-
case MetatypeRepresentation::Thin:
61-
break;
62-
case MetatypeRepresentation::Thick:
63-
case MetatypeRepresentation::ObjC: {
64-
// Walk the type recursively to look for substitutions we may need.
65-
theMetatype.getInstanceType().findIf([&](Type t) -> bool {
66-
auto *decl = t->getAnyNominal();
67-
if (!decl)
68-
return false;
69-
70-
if (isa<ProtocolDecl>(decl))
71-
return false;
72-
73-
auto *genericSig = decl->getGenericSignature();
74-
if (!genericSig)
75-
return false;
76-
77-
auto subMap = t->getContextSubstitutionMap(getSILGenModule().SwiftModule,
78-
decl);
79-
getSILGenModule().useConformancesFromSubstitutions(subMap);
80-
return false;
81-
});
82-
83-
break;
84-
}
85-
}
86-
87-
return SILBuilder::createMetatype(loc, metatype);
88-
}
89-
90-
ApplyInst *SILGenBuilder::createApply(SILLocation loc, SILValue fn,
91-
SILType substFnTy, SILType result,
92-
SubstitutionMap subs,
93-
ArrayRef<SILValue> args) {
94-
getSILGenModule().useConformancesFromSubstitutions(subs);
95-
return SILBuilder::createApply(loc, fn, subs, args, false);
96-
}
97-
98-
TryApplyInst *
99-
SILGenBuilder::createTryApply(SILLocation loc, SILValue fn, SILType substFnTy,
100-
SubstitutionMap subs, ArrayRef<SILValue> args,
101-
SILBasicBlock *normalBB, SILBasicBlock *errorBB) {
102-
getSILGenModule().useConformancesFromSubstitutions(subs);
103-
return SILBuilder::createTryApply(loc, fn, subs, args, normalBB, errorBB);
104-
}
105-
106-
BeginApplyInst *
107-
SILGenBuilder::createBeginApply(SILLocation loc, SILValue fn,
108-
SubstitutionMap subs,
109-
ArrayRef<SILValue> args) {
110-
getSILGenModule().useConformancesFromSubstitutions(subs);
111-
return SILBuilder::createBeginApply(loc, fn, subs, args, false);
112-
}
113-
114-
PartialApplyInst *
115-
SILGenBuilder::createPartialApply(SILLocation loc, SILValue fn,
116-
SILType substFnTy, SubstitutionMap subs,
117-
ArrayRef<SILValue> args, SILType closureTy) {
118-
getSILGenModule().useConformancesFromSubstitutions(subs);
119-
return SILBuilder::createPartialApply(
120-
loc, fn, subs, args,
121-
closureTy.getAs<SILFunctionType>()->getCalleeConvention());
122-
}
123-
124-
125-
BuiltinInst *SILGenBuilder::createBuiltin(SILLocation loc, Identifier name,
126-
SILType resultTy,
127-
SubstitutionMap subs,
128-
ArrayRef<SILValue> args) {
129-
getSILGenModule().useConformancesFromSubstitutions(subs);
130-
return SILBuilder::createBuiltin(loc, name, resultTy, subs, args);
131-
}
132-
133-
InitExistentialAddrInst *SILGenBuilder::createInitExistentialAddr(
134-
SILLocation loc, SILValue existential, CanType formalConcreteType,
135-
SILType loweredConcreteType,
136-
ArrayRef<ProtocolConformanceRef> conformances) {
137-
for (auto conformance : conformances)
138-
getSILGenModule().useConformance(conformance);
139-
140-
return SILBuilder::createInitExistentialAddr(
141-
loc, existential, formalConcreteType, loweredConcreteType, conformances);
142-
}
143-
144-
InitExistentialValueInst *SILGenBuilder::createInitExistentialValue(
145-
SILLocation Loc, SILType ExistentialType, CanType FormalConcreteType,
146-
SILValue Concrete, ArrayRef<ProtocolConformanceRef> Conformances) {
147-
for (auto conformance : Conformances)
148-
getSILGenModule().useConformance(conformance);
149-
150-
return SILBuilder::createInitExistentialValue(
151-
Loc, ExistentialType, FormalConcreteType, Concrete, Conformances);
152-
}
153-
154-
InitExistentialMetatypeInst *SILGenBuilder::createInitExistentialMetatype(
155-
SILLocation loc, SILValue metatype, SILType existentialType,
156-
ArrayRef<ProtocolConformanceRef> conformances) {
157-
for (auto conformance : conformances)
158-
getSILGenModule().useConformance(conformance);
159-
160-
return SILBuilder::createInitExistentialMetatype(
161-
loc, metatype, existentialType, conformances);
162-
}
163-
164-
InitExistentialRefInst *SILGenBuilder::createInitExistentialRef(
165-
SILLocation loc, SILType existentialType, CanType formalConcreteType,
166-
SILValue concreteValue, ArrayRef<ProtocolConformanceRef> conformances) {
167-
for (auto conformance : conformances)
168-
getSILGenModule().useConformance(conformance);
169-
170-
return SILBuilder::createInitExistentialRef(
171-
loc, existentialType, formalConcreteType, concreteValue, conformances);
172-
}
173-
174-
AllocExistentialBoxInst *SILGenBuilder::createAllocExistentialBox(
175-
SILLocation loc, SILType existentialType, CanType concreteType,
176-
ArrayRef<ProtocolConformanceRef> conformances) {
177-
for (auto conformance : conformances)
178-
getSILGenModule().useConformance(conformance);
179-
180-
return SILBuilder::createAllocExistentialBox(loc, existentialType,
181-
concreteType, conformances);
182-
}
44+
: SILGenSILBuilder(SGF, insertBB, insertInst), SGF(SGF) {}
18345

18446
//===----------------------------------------------------------------------===//
18547
// Managed Value APIs
@@ -209,8 +71,8 @@ SILGenBuilder::createConvertFunction(SILLocation loc, ManagedValue fn,
20971
SILType resultTy,
21072
bool withoutActuallyEscaping) {
21173
CleanupCloner cloner(*this, fn);
212-
SILValue result = SILBuilder::createConvertFunction(
213-
loc, fn.forward(getSILGenFunction()), resultTy, withoutActuallyEscaping);
74+
SILValue result = createConvertFunction(loc, fn.forward(getSILGenFunction()),
75+
resultTy, withoutActuallyEscaping);
21476
return cloner.clone(result);
21577
}
21678

@@ -741,7 +603,7 @@ ManagedValue SILGenBuilder::createUncheckedBitCast(SILLocation loc,
741603
CleanupCloner cloner(*this, value);
742604
SILValue cast = createUncheckedBitCast(loc, value.getValue(), type);
743605

744-
// Currently SILBuilder::createUncheckedBitCast only produces these
606+
// Currently createUncheckedBitCast only produces these
745607
// instructions. We assert here to make sure if this changes, this code is
746608
// updated.
747609
assert((isa<UncheckedTrivialBitCastInst>(cast) ||
@@ -962,14 +824,14 @@ void SILGenBuilder::emitDestructureValueOperation(
962824
SILLocation loc, ManagedValue value,
963825
llvm::function_ref<void(unsigned, ManagedValue)> func) {
964826
CleanupCloner cloner(*this, value);
965-
SILBuilder::emitDestructureValueOperation(
966-
loc, value.forward(SGF), [&](unsigned index, SILValue subValue) {
967-
return func(index, cloner.clone(subValue));
968-
});
827+
emitDestructureValueOperation(loc, value.forward(SGF),
828+
[&](unsigned index, SILValue subValue) {
829+
return func(index, cloner.clone(subValue));
830+
});
969831
}
970832

971833
ManagedValue SILGenBuilder::createProjectBox(SILLocation loc, ManagedValue mv,
972834
unsigned index) {
973-
auto *pbi = SILBuilder::createProjectBox(loc, mv.getValue(), index);
835+
auto *pbi = createProjectBox(loc, mv.getValue(), index);
974836
return ManagedValue::forUnmanaged(pbi);
975837
}

0 commit comments

Comments
 (0)