Skip to content

Commit 4287afc

Browse files
authored
Merge pull request swiftlang#24268 from slavapestov/remove-sil-gen-sil-builder
Remove SILGenSILBuilder
2 parents f05c36d + 9a1abf7 commit 4287afc

18 files changed

+386
-506
lines changed

lib/SILGen/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ add_swift_host_library(swiftSILGen STATIC
2323
SILGenForeignError.cpp
2424
SILGenFunction.cpp
2525
SILGenGlobalVariable.cpp
26+
SILGenLazyConformance.cpp
2627
SILGenLValue.cpp
2728
SILGenPattern.cpp
2829
SILGenPoly.cpp
2930
SILGenProlog.cpp
3031
SILGenStmt.cpp
31-
SILGenSILBuilder.cpp
3232
SILGenThunk.cpp
3333
SILGenType.cpp)
3434
target_link_libraries(swiftSILGen PRIVATE

lib/SILGen/SILGen.cpp

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,8 @@ void SILGenModule::preEmitFunction(SILDeclRef constant,
757757

758758
void SILGenModule::postEmitFunction(SILDeclRef constant,
759759
SILFunction *F) {
760+
emitLazyConformancesForFunction(F);
761+
760762
assert(!F->isExternalDeclaration() && "did not emit any function body?!");
761763
LLVM_DEBUG(llvm::dbgs() << "lowered sil:\n";
762764
F->print(llvm::dbgs()));
@@ -1161,6 +1163,7 @@ SILFunction *SILGenModule::emitLazyGlobalInitializer(StringRef funcName,
11611163
f->setDebugScope(new (M) SILDebugScope(RegularLocation(binding), f));
11621164
auto dc = binding->getDeclContext();
11631165
SILGenFunction(*this, *f, dc).emitLazyGlobalInitializer(binding, pbdEntry);
1166+
emitLazyConformancesForFunction(f);
11641167
f->verify();
11651168

11661169
return f;
@@ -1518,40 +1521,6 @@ void SILGenModule::visitTopLevelCodeDecl(TopLevelCodeDecl *td) {
15181521
}
15191522
}
15201523

1521-
void SILGenModule::useConformance(ProtocolConformanceRef conformanceRef) {
1522-
// We don't need to emit dependent conformances.
1523-
if (conformanceRef.isAbstract())
1524-
return;
1525-
1526-
auto conformance = conformanceRef.getConcrete();
1527-
auto normal = dyn_cast<NormalProtocolConformance>(
1528-
conformance->getRootConformance());
1529-
if (normal == nullptr)
1530-
return;
1531-
1532-
// If we already emitted this witness table, we don't need to track the fact
1533-
// we need it.
1534-
if (emittedWitnessTables.count(normal))
1535-
return;
1536-
1537-
// If we delayed emitting this witness table, force it.
1538-
auto foundDelayed = delayedConformances.find(normal);
1539-
if (foundDelayed != delayedConformances.end()) {
1540-
forcedConformances.push_back(*foundDelayed);
1541-
delayedConformances.erase(foundDelayed);
1542-
return;
1543-
}
1544-
1545-
// Otherwise, just remember the fact we used this conformance.
1546-
usedConformances.insert(normal);
1547-
}
1548-
1549-
void SILGenModule::useConformancesFromSubstitutions(
1550-
const SubstitutionMap subs) {
1551-
for (auto conf : subs.getConformances())
1552-
useConformance(conf);
1553-
}
1554-
15551524
namespace {
15561525

15571526
/// An RAII class to scope source file codegen.

lib/SILGen/SILGen.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,16 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
428428
SILGlobalVariable *getSILGlobalVariable(VarDecl *gDecl,
429429
ForDefinition_t forDef);
430430

431+
/// Emit all lazy conformances referenced from this function body.
432+
void emitLazyConformancesForFunction(SILFunction *F);
433+
431434
/// Mark a protocol conformance as used, so we know we need to emit it if
432435
/// it's in our TU.
433436
void useConformance(ProtocolConformanceRef conformance);
434437

438+
/// Mark protocol conformances from the given type as used.
439+
void useConformancesFromType(CanType type);
440+
435441
/// Mark protocol conformances from the given set of substitutions as used.
436442
void useConformancesFromSubstitutions(SubstitutionMap subs);
437443

lib/SILGen/SILGenApply.cpp

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,21 +1781,19 @@ static void emitRawApply(SILGenFunction &SGF,
17811781
}
17821782

17831783
auto resultType = substFnConv.getSILResultType();
1784-
auto calleeType = SILType::getPrimitiveObjectType(substFnType);
17851784

17861785
// If the function is a coroutine, we need to use 'begin_apply'.
17871786
if (substFnType->isCoroutine()) {
17881787
assert(!substFnType->hasErrorResult());
1789-
auto apply = SGF.B.createBeginApply(loc, fnValue, subs, argValues);
1788+
auto apply = SGF.B.createBeginApply(loc, fnValue, subs, argValues, false);
17901789
for (auto result : apply->getAllResults())
17911790
rawResults.push_back(result);
17921791
return;
17931792
}
17941793

17951794
// If we don't have an error result, we can make a simple 'apply'.
17961795
if (!substFnType->hasErrorResult()) {
1797-
auto result = SGF.B.createApply(loc, fnValue, calleeType,
1798-
resultType, subs, argValues);
1796+
auto result = SGF.B.createApply(loc, fnValue, subs, argValues, false);
17991797
rawResults.push_back(result);
18001798

18011799
// Otherwise, we need to create a try_apply.
@@ -1809,7 +1807,7 @@ static void emitRawApply(SILGenFunction &SGF,
18091807
SGF.getTryApplyErrorDest(loc, substFnType->getErrorResult(),
18101808
options & ApplyOptions::DoesNotThrow);
18111809

1812-
SGF.B.createTryApply(loc, fnValue, calleeType, subs, argValues,
1810+
SGF.B.createTryApply(loc, fnValue, subs, argValues,
18131811
normalBB, errorBB);
18141812
SGF.B.emitBlock(normalBB);
18151813
}
@@ -4007,19 +4005,10 @@ CallEmission::applyPartiallyAppliedSuperMethod(SGFContext C) {
40074005
S.pop();
40084006
}
40094007
auto calleeConvention = ParameterConvention::Direct_Guaranteed;
4010-
auto closureTy = SILGenBuilder::getPartialApplyResultType(
4011-
constantInfo.getSILType(), 1, SGF.B.getModule(), subs, calleeConvention);
40124008

4013-
auto &module = SGF.getFunction().getModule();
4014-
4015-
auto partialApplyTy = functionTy;
4016-
if (constantInfo.SILFnType->isPolymorphic() && !subs.empty())
4017-
partialApplyTy = partialApplyTy.substGenericArgs(module, subs);
4018-
4019-
ManagedValue pa = SGF.B.createPartialApply(loc, superMethod, partialApplyTy,
4009+
ManagedValue pa = SGF.B.createPartialApply(loc, superMethod,
40204010
subs, {upcastedSelf},
4021-
closureTy);
4022-
assert(!closureTy.castTo<SILFunctionType>()->isNoEscape());
4011+
calleeConvention);
40234012
firstLevelResult.value = RValue(SGF, loc, formalApplyType.getResult(), pa);
40244013
return firstLevelResult;
40254014
}
@@ -4544,12 +4533,12 @@ SILValue SILGenFunction::emitApplyWithRethrow(SILLocation loc, SILValue fn,
45444533
SILType resultType = fnConv.getSILResultType();
45454534

45464535
if (!silFnType->hasErrorResult()) {
4547-
return B.createApply(loc, fn, substFnType, resultType, subs, args);
4536+
return B.createApply(loc, fn, subs, args, false);
45484537
}
45494538

45504539
SILBasicBlock *errorBB = createBasicBlock();
45514540
SILBasicBlock *normalBB = createBasicBlock();
4552-
B.createTryApply(loc, fn, substFnType, subs, args, normalBB, errorBB);
4541+
B.createTryApply(loc, fn, subs, args, normalBB, errorBB);
45534542

45544543
// Emit the rethrow logic.
45554544
{
@@ -4578,7 +4567,7 @@ SILGenFunction::emitBeginApplyWithRethrow(SILLocation loc, SILValue fn,
45784567
// TODO: adjust this to create try_begin_apply when appropriate.
45794568
assert(!substFnType.castTo<SILFunctionType>()->hasErrorResult());
45804569

4581-
auto beginApply = B.createBeginApply(loc, fn, subs, args);
4570+
auto beginApply = B.createBeginApply(loc, fn, subs, args, false);
45824571

45834572
auto yieldResults = beginApply->getYieldedValues();
45844573
yields.append(yieldResults.begin(), yieldResults.end());
@@ -5728,26 +5717,20 @@ static ManagedValue emitDynamicPartialApply(SILGenFunction &SGF,
57285717
CanAnyFunctionType nativeFormalType) {
57295718
auto calleeConvention = ParameterConvention::Direct_Guaranteed;
57305719

5731-
auto partialApplyTy =
5732-
SILBuilder::getPartialApplyResultType(method->getType(),
5733-
/*argCount*/ 1, SGF.SGM.M,
5734-
/*subs*/ {}, calleeConvention);
5735-
57365720
// Retain 'self' because the partial apply will take ownership.
57375721
// We can't simply forward 'self' because the partial apply is conditional.
57385722
if (!self->getType().isAddress())
57395723
self = SGF.B.emitCopyValueOperation(loc, self);
57405724

57415725
SILValue resultValue =
5742-
SGF.B.createPartialApply(loc, method, method->getType(), {},
5743-
self, partialApplyTy);
5726+
SGF.B.createPartialApply(loc, method, {}, self, calleeConvention);
57445727
ManagedValue result = SGF.emitManagedRValueWithCleanup(resultValue);
57455728

57465729
// If necessary, thunk to the native ownership conventions and bridged types.
57475730
auto nativeTy =
57485731
SGF.getLoweredLoadableType(nativeFormalType).castTo<SILFunctionType>();
57495732

5750-
if (nativeTy != partialApplyTy.getASTType()) {
5733+
if (nativeTy != resultValue->getType().getASTType()) {
57515734
result = SGF.emitBlockToFunc(loc, result, foreignFormalType,
57525735
nativeFormalType, nativeTy);
57535736
}

lib/SILGen/SILGenBridging.cpp

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,9 @@ emitBridgeNativeToObjectiveC(SILGenFunction &SGF,
154154
}
155155

156156
// Call the witness.
157-
SILType resultTy = SGF.getLoweredType(objcType);
158157
SILValue bridgedValue =
159-
SGF.B.createApply(loc, witnessRef, witnessFnTy, resultTy, typeSubMap,
160-
swiftValue.borrow(SGF, loc).getValue());
158+
SGF.B.createApply(loc, witnessRef, typeSubMap,
159+
swiftValue.borrow(SGF, loc).getValue(), false);
161160

162161
auto bridgedMV = SGF.emitManagedRValueWithCleanup(bridgedValue);
163162
bridgedMV = scope.popPreservingValue(bridgedMV);
@@ -252,11 +251,8 @@ static ManagedValue emitBridgeBoolToObjCBool(SILGenFunction &SGF,
252251
SILValue boolToObjCBoolFn
253252
= SGF.emitGlobalFunctionRef(loc, SGF.SGM.getBoolToObjCBoolFn());
254253

255-
SILType resultTy =SGF.getLoweredLoadableType(SGF.SGM.Types.getObjCBoolType());
256-
257254
SILValue result = SGF.B.createApply(loc, boolToObjCBoolFn,
258-
boolToObjCBoolFn->getType(),
259-
resultTy, {}, swiftBool.forward(SGF));
255+
{}, swiftBool.forward(SGF), false);
260256
return SGF.emitManagedRValueWithCleanup(result);
261257
}
262258

@@ -267,12 +263,8 @@ static ManagedValue emitBridgeBoolToDarwinBoolean(SILGenFunction &SGF,
267263
SILValue boolToDarwinBooleanFn
268264
= SGF.emitGlobalFunctionRef(loc, SGF.SGM.getBoolToDarwinBooleanFn());
269265

270-
SILType resultTy =
271-
SGF.getLoweredLoadableType(SGF.SGM.Types.getDarwinBooleanType());
272-
273266
SILValue result = SGF.B.createApply(loc, boolToDarwinBooleanFn,
274-
boolToDarwinBooleanFn->getType(),
275-
resultTy, {}, swiftBool.forward(SGF));
267+
{}, swiftBool.forward(SGF), false);
276268
return SGF.emitManagedRValueWithCleanup(result);
277269
}
278270

@@ -283,10 +275,8 @@ static ManagedValue emitBridgeForeignBoolToBool(SILGenFunction &SGF,
283275
// func _convertObjCBoolToBool(ObjCBool) -> Bool
284276
SILValue bridgingFn = SGF.emitGlobalFunctionRef(loc, bridgingFnRef);
285277

286-
SILType resultTy = SGF.getLoweredLoadableType(SGF.SGM.Types.getBoolType());
287-
288-
SILValue result = SGF.B.createApply(loc, bridgingFn, bridgingFn->getType(),
289-
resultTy, {}, foreignBool.forward(SGF));
278+
SILValue result = SGF.B.createApply(loc, bridgingFn, {},
279+
foreignBool.forward(SGF), false);
290280
return SGF.emitManagedRValueWithCleanup(result);
291281
}
292282

@@ -589,6 +579,7 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
589579
buildFuncToBlockInvokeBody(thunkSGF, loc, funcType, blockType,
590580
loweredFuncTy, loweredBlockTy, storageTy,
591581
useWithoutEscapingVerification);
582+
SGM.emitLazyConformancesForFunction(thunk);
592583
}
593584

594585
// Form the block on the stack.
@@ -946,6 +937,7 @@ SILGenFunction::emitBlockToFunc(SILLocation loc,
946937
auto loc = RegularLocation::getAutoGeneratedLocation();
947938
buildBlockToFuncThunkBody(thunkSGF, loc, blockType, funcType,
948939
loweredBlockTy, loweredFuncTyWithoutNoEscape);
940+
SGM.emitLazyConformancesForFunction(thunk);
949941
}
950942

951943
CanSILFunctionType substFnTy = thunkTy;
@@ -958,9 +950,8 @@ SILGenFunction::emitBlockToFunc(SILLocation loc,
958950
// Create it in the current function.
959951
auto thunkValue = B.createFunctionRefFor(loc, thunk);
960952
ManagedValue thunkedFn = B.createPartialApply(
961-
loc, thunkValue, SILType::getPrimitiveObjectType(substFnTy),
962-
interfaceSubs, block,
963-
SILType::getPrimitiveObjectType(loweredFuncTyWithoutNoEscape));
953+
loc, thunkValue, interfaceSubs, block,
954+
loweredFuncTy->getCalleeConvention());
964955

965956
if (!loweredFuncTy->isNoEscape()) {
966957
return thunkedFn;
@@ -1141,18 +1132,17 @@ ManagedValue SILGenFunction::emitBridgedToNativeError(SILLocation loc,
11411132
// a standard error for a nil NSError.
11421133
auto bridgeFn = emitGlobalFunctionRef(loc, SGM.getNSErrorToErrorFn());
11431134
auto bridgeFnType = bridgeFn->getType().castTo<SILFunctionType>();
1144-
SILFunctionConventions bridgeFnConv(bridgeFnType, SGM.M);
11451135
assert(bridgeFnType->getNumResults() == 1);
11461136
assert(bridgeFnType->getResults()[0].getConvention()
11471137
== ResultConvention::Owned);
1148-
auto nativeErrorType = bridgeFnConv.getSILType(bridgeFnType->getResults()[0]);
11491138

1150-
assert(bridgeFnType->getParameters()[0].getConvention() ==
1151-
ParameterConvention::Direct_Guaranteed);
1139+
assert(bridgeFnType->getParameters()[0].getConvention()
1140+
== ParameterConvention::Direct_Guaranteed);
1141+
(void) bridgeFnType;
1142+
11521143
SILValue arg = bridgedError.getValue();
11531144

1154-
SILValue nativeError = B.createApply(loc, bridgeFn, bridgeFn->getType(),
1155-
nativeErrorType, {}, arg);
1145+
SILValue nativeError = B.createApply(loc, bridgeFn, {}, arg, false);
11561146
return emitManagedRValueWithCleanup(nativeError);
11571147
}
11581148

@@ -1189,19 +1179,16 @@ ManagedValue SILGenFunction::emitNativeToBridgedError(SILLocation loc,
11891179

11901180
auto bridgeFn = emitGlobalFunctionRef(loc, SGM.getErrorToNSErrorFn());
11911181
auto bridgeFnType = bridgeFn->getType().castTo<SILFunctionType>();
1192-
SILFunctionConventions bridgeFnConv(bridgeFnType, SGM.M);
11931182
assert(bridgeFnType->getNumResults() == 1);
11941183
assert(bridgeFnType->getResults()[0].getConvention()
11951184
== ResultConvention::Owned);
1196-
auto loweredBridgedErrorType =
1197-
bridgeFnConv.getSILType(bridgeFnType->getResults()[0]);
1185+
assert(bridgeFnType->getParameters()[0].getConvention()
1186+
== ParameterConvention::Direct_Guaranteed);
1187+
(void) bridgeFnType;
11981188

1199-
assert(bridgeFnType->getParameters()[0].getConvention() ==
1200-
ParameterConvention::Direct_Guaranteed);
12011189
SILValue arg = nativeError.getValue();
12021190

1203-
SILValue bridgedError = B.createApply(loc, bridgeFn, bridgeFn->getType(),
1204-
loweredBridgedErrorType, {}, arg);
1191+
SILValue bridgedError = B.createApply(loc, bridgeFn, {}, arg, false);
12051192
return emitManagedRValueWithCleanup(bridgedError);
12061193
}
12071194

@@ -1403,7 +1390,6 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
14031390
auto nativeInfo = getConstantInfo(native);
14041391
auto subs = F.getForwardingSubstitutionMap();
14051392
auto substTy = nativeInfo.SILFnType->substGenericArgs(SGM.M, subs);
1406-
SILType substSILTy = SILType::getPrimitiveObjectType(substTy);
14071393
SILFunctionConventions substConv(substTy, SGM.M);
14081394

14091395
// Use the same generic environment as the native entry point.
@@ -1504,8 +1490,7 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
15041490
assert(foreignError.hasValue() == substTy->hasErrorResult());
15051491
if (!substTy->hasErrorResult()) {
15061492
// Create the apply.
1507-
result = B.createApply(loc, nativeFn, substSILTy,
1508-
swiftResultTy, subs, args);
1493+
result = B.createApply(loc, nativeFn, subs, args, false);
15091494

15101495
if (substConv.hasIndirectSILResults()) {
15111496
assert(substTy->getNumResults() == 1);
@@ -1523,8 +1508,7 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
15231508
SILBasicBlock *contBB = createBasicBlock();
15241509
SILBasicBlock *errorBB = createBasicBlock();
15251510
SILBasicBlock *normalBB = createBasicBlock();
1526-
B.createTryApply(loc, nativeFn, substSILTy, subs, args,
1527-
normalBB, errorBB);
1511+
B.createTryApply(loc, nativeFn, subs, args, normalBB, errorBB);
15281512

15291513
// Emit the non-error destination.
15301514
{

lib/SILGen/SILGenBuilder.cpp

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

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

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

4242
SILGenBuilder::SILGenBuilder(SILGenFunction &SGF, SILBasicBlock *insertBB,
4343
SILBasicBlock::iterator insertInst)
44-
: SILGenSILBuilder(SGF, insertBB, insertInst), SGF(SGF) {}
44+
: SILBuilder(insertBB, insertInst), SGF(SGF) {}
4545

4646
//===----------------------------------------------------------------------===//
4747
// Managed Value APIs
4848
//===----------------------------------------------------------------------===//
49-
//
50-
// *NOTE* Please use SILGenBuilder SILValue APIs below. Otherwise, we
51-
// potentially do not get all of the conformances that we need.
52-
//
5349

5450
ManagedValue SILGenBuilder::createPartialApply(SILLocation loc, SILValue fn,
55-
SILType substFnTy,
5651
SubstitutionMap subs,
5752
ArrayRef<ManagedValue> args,
58-
SILType closureTy) {
53+
ParameterConvention calleeConvention) {
5954
llvm::SmallVector<SILValue, 8> values;
6055
transform(args, std::back_inserter(values), [&](ManagedValue mv) -> SILValue {
6156
return mv.forward(getSILGenFunction());
6257
});
6358
SILValue result =
64-
createPartialApply(loc, fn, substFnTy, subs, values, closureTy);
59+
createPartialApply(loc, fn, subs, values, calleeConvention);
6560
// Partial apply instructions create a box, so we need to put on a cleanup.
6661
return getSILGenFunction().emitManagedRValueWithCleanup(result);
6762
}

0 commit comments

Comments
 (0)