Skip to content

Commit fb8bd3a

Browse files
authored
Merge pull request #24267 from slavapestov/unused-conformances
Remove per-SourceFile "used conformances" lists
2 parents d9c166f + be4e3f2 commit fb8bd3a

34 files changed

+192
-217
lines changed

include/swift/AST/Module.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -983,10 +983,6 @@ class SourceFile final : public FileUnit {
983983
/// If not, we can fast-path module checks.
984984
bool HasImplementationOnlyImports = false;
985985

986-
/// The list of protocol conformances that were "used" within this
987-
/// source file.
988-
llvm::SetVector<NormalProtocolConformance *> UsedConformances;
989-
990986
/// The scope map that describes this source file.
991987
ASTScope *Scope = nullptr;
992988

@@ -1146,17 +1142,6 @@ class SourceFile final : public FileUnit {
11461142

11471143
virtual bool walk(ASTWalker &walker) override;
11481144

1149-
/// Note that the given conformance was used by this source file.
1150-
void addUsedConformance(NormalProtocolConformance *conformance) {
1151-
UsedConformances.insert(conformance);
1152-
}
1153-
1154-
/// Retrieve the set of conformances that were used in this source
1155-
/// file.
1156-
ArrayRef<NormalProtocolConformance *> getUsedConformances() const {
1157-
return UsedConformances.getArrayRef();
1158-
}
1159-
11601145
/// @{
11611146

11621147
/// Look up the given operator in this file.

include/swift/Basic/Statistics.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,6 @@ FRONTEND_STATISTIC(AST, NumPrefixOperators)
141141
/// Number of precedence groups in the AST context.
142142
FRONTEND_STATISTIC(AST, NumPrecedenceGroups)
143143

144-
/// Number of conformances used by code processed by this frontend job.
145-
FRONTEND_STATISTIC(AST, NumUsedConformances)
146-
147144
/// Number of full function bodies parsed.
148145
FRONTEND_STATISTIC(Parse, NumFunctionsParsed)
149146

include/swift/SIL/SILBuilder.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -452,22 +452,13 @@ class SILBuilder {
452452

453453
ApplyInst *createApply(
454454
SILLocation Loc, SILValue Fn, SubstitutionMap Subs,
455-
ArrayRef<SILValue> Args, bool isNonThrowing,
455+
ArrayRef<SILValue> Args, bool isNonThrowing = false,
456456
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
457457
return insert(ApplyInst::create(getSILDebugLocation(Loc), Fn, Subs, Args,
458458
isNonThrowing, C.silConv, *F,
459459
C.OpenedArchetypes, SpecializationInfo));
460460
}
461461

462-
ApplyInst *createApply(
463-
SILLocation Loc, SILValue Fn, ArrayRef<SILValue> Args, bool isNonThrowing,
464-
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
465-
SILFunctionConventions conventions(Fn->getType().castTo<SILFunctionType>(),
466-
getModule());
467-
return createApply(Loc, Fn, SubstitutionMap(), Args, isNonThrowing,
468-
SpecializationInfo);
469-
}
470-
471462
TryApplyInst *createTryApply(
472463
SILLocation Loc, SILValue fn, SubstitutionMap subs,
473464
ArrayRef<SILValue> args, SILBasicBlock *normalBB, SILBasicBlock *errorBB,
@@ -490,7 +481,7 @@ class SILBuilder {
490481

491482
BeginApplyInst *createBeginApply(
492483
SILLocation Loc, SILValue Fn, SubstitutionMap Subs,
493-
ArrayRef<SILValue> Args, bool isNonThrowing,
484+
ArrayRef<SILValue> Args, bool isNonThrowing = false,
494485
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
495486
return insert(BeginApplyInst::create(
496487
getSILDebugLocation(Loc), Fn, Subs, Args, isNonThrowing, C.silConv, *F,

lib/FrontendTool/FrontendTool.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ static void countStatsOfSourceFile(UnifiedStatsReporter &Stats,
478478
C.NumPostfixOperators += SF->PostfixOperators.size();
479479
C.NumPrefixOperators += SF->PrefixOperators.size();
480480
C.NumPrecedenceGroups += SF->PrecedenceGroups.size();
481-
C.NumUsedConformances += SF->getUsedConformances().size();
482481

483482
auto bufID = SF->getBufferID();
484483
if (bufID.hasValue()) {

lib/SIL/Bridging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ Type TypeConverter::getLoweredCBridgedType(AbstractionPattern pattern,
226226
ProtocolConformanceRef::getTypeWitnessByName(
227227
t, ProtocolConformanceRef(conformance),
228228
M.getASTContext().Id_ObjectiveCType,
229-
nullptr);
229+
M.getASTContext().getLazyResolver());
230230
assert(bridgedTy && "Missing _ObjectiveCType witness?");
231231
if (purpose == BridgedTypePurpose::ForResult && clangTy)
232232
bridgedTy = OptionalType::get(bridgedTy);

lib/SIL/Linker.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,14 @@ void SILLinkerVisitor::visitProtocolConformance(
217217
// If the looked up witness table is a declaration, there is nothing we can
218218
// do here.
219219
if (WT == nullptr || WT->isDeclaration()) {
220-
assert(!mustDeserialize &&
221-
"unable to deserialize witness table when we must?!");
220+
#ifndef NDEBUG
221+
if (mustDeserialize) {
222+
llvm::errs() << "SILGen failed to emit required conformance:\n";
223+
ref.dump(llvm::errs());
224+
llvm::errs() << "\n";
225+
abort();
226+
}
227+
#endif
222228
return;
223229
}
224230

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,7 @@ class SourceFileScope {
16491649
LLVM_DEBUG(llvm::dbgs() << "lowered toplevel sil:\n";
16501650
toplevel->print(llvm::dbgs()));
16511651
toplevel->verify();
1652+
sgm.emitLazyConformancesForFunction(toplevel);
16521653
}
16531654

16541655
// If the source file contains an artificial main, emit the implicit
@@ -1689,10 +1690,6 @@ void SILGenModule::emitSourceFile(SourceFile *sf) {
16891690
FrontendStatsTracer StatsTracer(getASTContext().Stats, "SILgen-tydecl", D);
16901691
visit(D);
16911692
}
1692-
1693-
// Mark any conformances as "used".
1694-
for (auto conformance : sf->getUsedConformances())
1695-
useConformance(ProtocolConformanceRef(conformance));
16961693
}
16971694

16981695
//===----------------------------------------------------------------------===//

lib/SILGen/SILGen.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
8080
/// Set of used conformances for which witness tables need to be emitted.
8181
llvm::DenseSet<RootProtocolConformance *> usedConformances;
8282

83+
/// Bookkeeping to ensure that useConformancesFrom{ObjectiveC,}Type() is
84+
/// only called once for each unique type, as an optimization.
85+
llvm::DenseSet<TypeBase *> usedConformancesFromTypes;
86+
llvm::DenseSet<TypeBase *> usedConformancesFromObjectiveCTypes;
87+
8388
struct DelayedWitnessTable {
8489
NormalProtocolConformance *insertAfter;
8590
};
@@ -435,6 +440,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
435440
/// Emit all lazy conformances referenced from this function body.
436441
void emitLazyConformancesForFunction(SILFunction *F);
437442

443+
/// Emit all lazy conformances referenced from this type's signature and
444+
/// stored properties (or in the case of enums, associated values).
445+
void emitLazyConformancesForType(NominalTypeDecl *NTD);
446+
438447
/// Mark a protocol conformance as used, so we know we need to emit it if
439448
/// it's in our TU.
440449
void useConformance(ProtocolConformanceRef conformance);
@@ -445,6 +454,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
445454
/// Mark protocol conformances from the given set of substitutions as used.
446455
void useConformancesFromSubstitutions(SubstitutionMap subs);
447456

457+
/// Mark _ObjectiveCBridgeable conformances as used for any imported types
458+
/// mentioned by the given type.
459+
void useConformancesFromObjectiveCType(CanType type);
460+
448461
/// Emit a `mark_function_escape` instruction for top-level code when a
449462
/// function or closure at top level refers to script globals.
450463
void emitMarkFunctionEscapeForTopLevelCodeGlobals(SILLocation loc,

lib/SILGen/SILGenApply.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,15 +1785,15 @@ static void emitRawApply(SILGenFunction &SGF,
17851785
// If the function is a coroutine, we need to use 'begin_apply'.
17861786
if (substFnType->isCoroutine()) {
17871787
assert(!substFnType->hasErrorResult());
1788-
auto apply = SGF.B.createBeginApply(loc, fnValue, subs, argValues, false);
1788+
auto apply = SGF.B.createBeginApply(loc, fnValue, subs, argValues);
17891789
for (auto result : apply->getAllResults())
17901790
rawResults.push_back(result);
17911791
return;
17921792
}
17931793

17941794
// If we don't have an error result, we can make a simple 'apply'.
17951795
if (!substFnType->hasErrorResult()) {
1796-
auto result = SGF.B.createApply(loc, fnValue, subs, argValues, false);
1796+
auto result = SGF.B.createApply(loc, fnValue, subs, argValues);
17971797
rawResults.push_back(result);
17981798

17991799
// Otherwise, we need to create a try_apply.
@@ -4533,7 +4533,7 @@ SILValue SILGenFunction::emitApplyWithRethrow(SILLocation loc, SILValue fn,
45334533
SILType resultType = fnConv.getSILResultType();
45344534

45354535
if (!silFnType->hasErrorResult()) {
4536-
return B.createApply(loc, fn, subs, args, false);
4536+
return B.createApply(loc, fn, subs, args);
45374537
}
45384538

45394539
SILBasicBlock *errorBB = createBasicBlock();
@@ -4567,7 +4567,7 @@ SILGenFunction::emitBeginApplyWithRethrow(SILLocation loc, SILValue fn,
45674567
// TODO: adjust this to create try_begin_apply when appropriate.
45684568
assert(!substFnType.castTo<SILFunctionType>()->hasErrorResult());
45694569

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

45724572
auto yieldResults = beginApply->getYieldedValues();
45734573
yields.append(yieldResults.begin(), yieldResults.end());

lib/SILGen/SILGenBridging.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ emitBridgeNativeToObjectiveC(SILGenFunction &SGF,
156156
// Call the witness.
157157
SILValue bridgedValue =
158158
SGF.B.createApply(loc, witnessRef, typeSubMap,
159-
swiftValue.borrow(SGF, loc).getValue(), false);
159+
swiftValue.borrow(SGF, loc).getValue());
160160

161161
auto bridgedMV = SGF.emitManagedRValueWithCleanup(bridgedValue);
162162
bridgedMV = scope.popPreservingValue(bridgedMV);
@@ -252,7 +252,7 @@ static ManagedValue emitBridgeBoolToObjCBool(SILGenFunction &SGF,
252252
= SGF.emitGlobalFunctionRef(loc, SGF.SGM.getBoolToObjCBoolFn());
253253

254254
SILValue result = SGF.B.createApply(loc, boolToObjCBoolFn,
255-
{}, swiftBool.forward(SGF), false);
255+
{}, swiftBool.forward(SGF));
256256
return SGF.emitManagedRValueWithCleanup(result);
257257
}
258258

@@ -264,7 +264,7 @@ static ManagedValue emitBridgeBoolToDarwinBoolean(SILGenFunction &SGF,
264264
= SGF.emitGlobalFunctionRef(loc, SGF.SGM.getBoolToDarwinBooleanFn());
265265

266266
SILValue result = SGF.B.createApply(loc, boolToDarwinBooleanFn,
267-
{}, swiftBool.forward(SGF), false);
267+
{}, swiftBool.forward(SGF));
268268
return SGF.emitManagedRValueWithCleanup(result);
269269
}
270270

@@ -284,7 +284,7 @@ static ManagedValue emitBridgeForeignBoolToBool(SILGenFunction &SGF,
284284
SILValue bridgingFn = SGF.emitGlobalFunctionRef(loc, bridgingFnRef);
285285

286286
SILValue result = SGF.B.createApply(loc, bridgingFn, {},
287-
foreignBool.forward(SGF), false);
287+
foreignBool.forward(SGF));
288288
return SGF.emitManagedRValueWithCleanup(result);
289289
}
290290

@@ -1154,7 +1154,7 @@ ManagedValue SILGenFunction::emitBridgedToNativeError(SILLocation loc,
11541154

11551155
SILValue arg = bridgedError.getValue();
11561156

1157-
SILValue nativeError = B.createApply(loc, bridgeFn, {}, arg, false);
1157+
SILValue nativeError = B.createApply(loc, bridgeFn, {}, arg);
11581158
return emitManagedRValueWithCleanup(nativeError);
11591159
}
11601160

@@ -1200,7 +1200,7 @@ ManagedValue SILGenFunction::emitNativeToBridgedError(SILLocation loc,
12001200

12011201
SILValue arg = nativeError.getValue();
12021202

1203-
SILValue bridgedError = B.createApply(loc, bridgeFn, {}, arg, false);
1203+
SILValue bridgedError = B.createApply(loc, bridgeFn, {}, arg);
12041204
return emitManagedRValueWithCleanup(bridgedError);
12051205
}
12061206

@@ -1502,7 +1502,7 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
15021502
assert(foreignError.hasValue() == substTy->hasErrorResult());
15031503
if (!substTy->hasErrorResult()) {
15041504
// Create the apply.
1505-
result = B.createApply(loc, nativeFn, subs, args, false);
1505+
result = B.createApply(loc, nativeFn, subs, args);
15061506

15071507
if (substConv.hasIndirectSILResults()) {
15081508
assert(substTy->getNumResults() == 1);

lib/SILGen/SILGenDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ SILValue SILGenFunction::emitOSVersionRangeCheck(SILLocation loc,
12361236
loc, silDeclRef, getConstantInfo(silDeclRef));
12371237

12381238
SILValue args[] = {majorValue, minorValue, subminorValue};
1239-
return B.createApply(loc, availabilityGTEFn, args, false);
1239+
return B.createApply(loc, availabilityGTEFn, SubstitutionMap(), args);
12401240
}
12411241

12421242

@@ -1440,9 +1440,9 @@ void SILGenModule::emitExternalDefinition(Decl *d) {
14401440
nullptr)) {
14411441
auto *proto = c->getProtocol();
14421442
if (Lowering::TypeConverter::protocolRequiresWitnessTable(proto) &&
1443-
isa<NormalProtocolConformance>(c) &&
1444-
c->isComplete())
1443+
isa<NormalProtocolConformance>(c)) {
14451444
emitExternalWitnessTable(cast<NormalProtocolConformance>(c));
1445+
}
14461446
}
14471447
break;
14481448
}

lib/SILGen/SILGenDestructor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void SILGenFunction::emitDestroyingDestructor(DestructorDecl *dd) {
6969
= emitSiblingMethodRef(cleanupLoc, baseSelf, dtorConstant, subMap);
7070

7171
resultSelfValue = B.createApply(cleanupLoc, dtorValue.forward(*this),
72-
subMap, baseSelf, false);
72+
subMap, baseSelf);
7373
} else {
7474
resultSelfValue = selfValue;
7575
}
@@ -129,7 +129,7 @@ void SILGenFunction::emitDeallocatingDestructor(DestructorDecl *dd) {
129129
FullExpr CleanupScope(Cleanups, CleanupLocation::get(loc));
130130
ManagedValue borrowedSelf = emitManagedBeginBorrow(loc, initialSelfValue);
131131
selfForDealloc = B.createApply(loc, dtorValue.forward(*this), subMap,
132-
borrowedSelf.getUnmanagedValue(), false);
132+
borrowedSelf.getUnmanagedValue());
133133
}
134134

135135
// Balance out the +1 from the self argument using end_lifetime.
@@ -246,7 +246,7 @@ void SILGenFunction::emitObjCDestructor(SILDeclRef dtor) {
246246
= superclassTy->getContextSubstitutionMap(SGM.M.getSwiftModule(),
247247
superclass);
248248

249-
B.createApply(cleanupLoc, superclassDtorValue, subMap, superSelf, false);
249+
B.createApply(cleanupLoc, superclassDtorValue, subMap, superSelf);
250250

251251
// We know that the givne value came in at +1, but we pass the relevant value
252252
// as unowned to the destructor. Create a fake balance for the verifier to be

lib/SILGen/SILGenFunction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
516516
SILType::getPrimitiveObjectType(anyObjectMetaTy),
517517
{});
518518
SILValue optNameValue = B.createApply(
519-
mainClass, NSStringFromClass, {}, metaTy, false);
519+
mainClass, NSStringFromClass, {}, metaTy);
520520
ManagedValue optName = emitManagedRValueWithCleanup(optNameValue);
521521

522522
// Fix up the string parameters to have the right type.
@@ -556,7 +556,7 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
556556
SILValue args[] = {argc, managedArgv.getValue(), nilValue,
557557
optName.getValue()};
558558

559-
B.createApply(mainClass, UIApplicationMain, SubstitutionMap{}, args, false);
559+
B.createApply(mainClass, UIApplicationMain, SubstitutionMap(), args);
560560
SILValue r = B.createIntegerLiteral(mainClass,
561561
SILType::getBuiltinIntegerType(32, ctx), 0);
562562
auto rType = F.getConventions().getSingleSILResultType();
@@ -601,7 +601,7 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
601601
auto NSApplicationMain = B.createFunctionRef(mainClass, NSApplicationMainFn);
602602
SILValue args[] = { argc, argv };
603603

604-
B.createApply(mainClass, NSApplicationMain, SubstitutionMap{}, args, false);
604+
B.createApply(mainClass, NSApplicationMain, SubstitutionMap(), args);
605605
SILValue r = B.createIntegerLiteral(mainClass,
606606
SILType::getBuiltinIntegerType(32, getASTContext()), 0);
607607
auto rType = F.getConventions().getSingleSILResultType();

lib/SILGen/SILGenGlobalVariable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ SILGenFunction::emitGlobalVariableRef(SILLocation loc, VarDecl *var) {
7575
SILDeclRef(var, SILDeclRef::Kind::GlobalAccessor),
7676
NotForDefinition);
7777
SILValue accessor = B.createFunctionRefFor(loc, accessorFn);
78-
SILValue addr = B.createApply(loc, accessor, SubstitutionMap{}, {}, false);
78+
SILValue addr = B.createApply(loc, accessor, SubstitutionMap(), {});
7979
// FIXME: It'd be nice if the result of the accessor was natively an
8080
// address.
8181
addr = B.createPointerToAddress(

0 commit comments

Comments
 (0)