Skip to content

Commit d49d3e9

Browse files
authored
Merge pull request #10394 from huonw/symbol-list-9
[AST] Move getEffectiveAccess calls into SIL.
2 parents b38cacb + a0bc3bd commit d49d3e9

File tree

9 files changed

+63
-34
lines changed

9 files changed

+63
-34
lines changed

include/swift/AST/ProtocolConformance.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,6 @@ class NormalProtocolConformance : public ProtocolConformance,
398398
return ContextAndInvalid.getPointer().dyn_cast<AbstractStorageDecl *>();
399399
}
400400

401-
bool isSerialized() const {
402-
auto *nominal = getType()->getAnyNominal();
403-
return nominal->hasFixedLayout() &&
404-
getProtocol()->getEffectiveAccess() >= Accessibility::Public &&
405-
nominal->getEffectiveAccess() >= Accessibility::Public;
406-
}
407-
408401
/// Retrieve the type witness and type decl (if one exists)
409402
/// for the given associated type.
410403
std::pair<Type, TypeDecl *>

include/swift/SIL/SILWitnessTable.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ namespace swift {
3434

3535
class SILFunction;
3636
class SILModule;
37+
class ProtocolConformance;
3738
class NormalProtocolConformance;
3839
enum IsSerialized_t : unsigned char;
40+
enum class ResilienceStrategy : unsigned;
3941

4042
/// A mapping from each requirement of a protocol to the SIL-level entity
4143
/// satisfying the requirement for a concrete type.
@@ -269,13 +271,18 @@ class SILWitnessTable : public llvm::ilist_node<SILWitnessTable>,
269271
void convertToDefinition(ArrayRef<Entry> newEntries,
270272
IsSerialized_t isSerialized);
271273

274+
// Whether a conformance should be serialized.
275+
static bool conformanceIsSerialized(ProtocolConformance *conformance,
276+
ResilienceStrategy strategy,
277+
bool silSerializeWitnessTables);
278+
272279
/// Print the witness table.
273280
void print(llvm::raw_ostream &OS, bool Verbose = false) const;
274281

275282
/// Dump the witness table to stderr.
276283
void dump() const;
277284
};
278-
285+
279286
} // end swift namespace
280287

281288
//===----------------------------------------------------------------------===//

include/swift/TBDGen/TBDGen.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace swift {
1818
class FileUnit;
1919

2020
void enumeratePublicSymbols(FileUnit *module, llvm::StringSet<> &symbols,
21-
bool hasMultipleIRGenThreads, bool isWholeModule);
21+
bool hasMultipleIRGenThreads, bool isWholeModule,
22+
bool silSerializeWitnessTables);
2223
} // end namespace swift
2324

2425
#endif

lib/FrontendTool/FrontendTool.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,10 @@ static bool performCompile(CompilerInstance &Instance,
665665
}
666666

667667
if (Action == FrontendOptions::EmitTBD) {
668-
auto hasMultipleIRGenThreads = Invocation.getSILOptions().NumThreads > 1;
668+
const auto &silOpts = Invocation.getSILOptions();
669+
auto hasMultipleIRGenThreads = silOpts.NumThreads > 1;
669670
return writeTBD(Instance.getMainModule(), hasMultipleIRGenThreads,
671+
silOpts.SILSerializeWitnessTables,
670672
opts.getSingleOutputFilename());
671673
}
672674

@@ -942,14 +944,16 @@ static bool performCompile(CompilerInstance &Instance,
942944
!astGuaranteedToCorrespondToSIL)
943945
break;
944946

945-
auto hasMultipleIRGenThreads = Invocation.getSILOptions().NumThreads > 1;
947+
const auto &silOpts = Invocation.getSILOptions();
948+
auto hasMultipleIRGenThreads = silOpts.NumThreads > 1;
946949
bool error;
947950
if (PrimarySourceFile)
948951
error = validateTBD(PrimarySourceFile, *IRModule, hasMultipleIRGenThreads,
949-
allSymbols);
952+
silOpts.SILSerializeWitnessTables, allSymbols);
950953
else
951954
error = validateTBD(Instance.getMainModule(), *IRModule,
952-
hasMultipleIRGenThreads, allSymbols);
955+
hasMultipleIRGenThreads,
956+
silOpts.SILSerializeWitnessTables, allSymbols);
953957
if (error)
954958
return true;
955959

lib/FrontendTool/TBD.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static std::vector<StringRef> sortSymbols(llvm::StringSet<> &symbols) {
3939
}
4040

4141
bool swift::writeTBD(ModuleDecl *M, bool hasMultipleIRGenThreads,
42-
StringRef OutputFilename) {
42+
bool silSerializeWitnessTables, StringRef OutputFilename) {
4343
std::error_code EC;
4444
llvm::raw_fd_ostream OS(OutputFilename, EC, llvm::sys::fs::F_None);
4545
if (EC) {
@@ -50,7 +50,7 @@ bool swift::writeTBD(ModuleDecl *M, bool hasMultipleIRGenThreads,
5050
llvm::StringSet<> symbols;
5151
for (auto file : M->getFiles())
5252
enumeratePublicSymbols(file, symbols, hasMultipleIRGenThreads,
53-
/*isWholeModule=*/true);
53+
/*isWholeModule=*/true, silSerializeWitnessTables);
5454

5555
// Ensure the order is stable.
5656
for (auto &symbol : sortSymbols(symbols)) {
@@ -125,22 +125,24 @@ static bool validateSymbolSet(DiagnosticEngine &diags,
125125

126126
bool swift::validateTBD(ModuleDecl *M, llvm::Module &IRModule,
127127
bool hasMultipleIRGenThreads,
128+
bool silSerializeWitnessTables,
128129
bool diagnoseExtraSymbolsInTBD) {
129130
llvm::StringSet<> symbols;
130131
for (auto file : M->getFiles())
131132
enumeratePublicSymbols(file, symbols, hasMultipleIRGenThreads,
132-
/*isWholeModule=*/true);
133+
/*isWholeModule=*/true, silSerializeWitnessTables);
133134

134135
return validateSymbolSet(M->getASTContext().Diags, symbols, IRModule,
135136
diagnoseExtraSymbolsInTBD);
136137
}
137138

138139
bool swift::validateTBD(FileUnit *file, llvm::Module &IRModule,
139140
bool hasMultipleIRGenThreads,
141+
bool silSerializeWitnessTables,
140142
bool diagnoseExtraSymbolsInTBD) {
141143
llvm::StringSet<> symbols;
142144
enumeratePublicSymbols(file, symbols, hasMultipleIRGenThreads,
143-
/*isWholeModule=*/false);
145+
/*isWholeModule=*/false, silSerializeWitnessTables);
144146

145147
return validateSymbolSet(file->getParentModule()->getASTContext().Diags,
146148
symbols, IRModule, diagnoseExtraSymbolsInTBD);

lib/FrontendTool/TBD.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ class FileUnit;
2525
class FrontendOptions;
2626

2727
bool writeTBD(ModuleDecl *M, bool hasMultipleIRGenThreads,
28-
llvm::StringRef OutputFilename);
28+
bool silSerializeWitnessTables, llvm::StringRef OutputFilename);
2929
bool inputFileKindCanHaveTBDValidated(InputFileKind kind);
3030
bool validateTBD(ModuleDecl *M, llvm::Module &IRModule,
31-
bool hasMultipleIRGenThreads, bool diagnoseExtraSymbolsInTBD);
31+
bool hasMultipleIRGenThreads, bool silSerializeWitnessTables,
32+
bool diagnoseExtraSymbolsInTBD);
3233
bool validateTBD(FileUnit *M, llvm::Module &IRModule,
33-
bool hasMultipleIRGenThreads, bool diagnoseExtraSymbolsInTBD);
34+
bool hasMultipleIRGenThreads, bool silSerializeWitnessTables,
35+
bool diagnoseExtraSymbolsInTBD);
3436
}
3537

3638
#endif

lib/SIL/SILWitnessTable.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "swift/SIL/SILWitnessTable.h"
2323
#include "swift/AST/ASTMangler.h"
24+
#include "swift/AST/Module.h"
2425
#include "swift/AST/ProtocolConformance.h"
2526
#include "swift/SIL/SILModule.h"
2627
#include "llvm/ADT/SmallString.h"
@@ -155,3 +156,18 @@ void SILWitnessTable::convertToDefinition(ArrayRef<Entry> entries,
155156
Identifier SILWitnessTable::getIdentifier() const {
156157
return Mod.getASTContext().getIdentifier(Name);
157158
}
159+
160+
bool SILWitnessTable::conformanceIsSerialized(ProtocolConformance *conformance,
161+
ResilienceStrategy strategy,
162+
bool silSerializeWitnessTables) {
163+
auto *nominal = conformance->getType()->getAnyNominal();
164+
// Only serialize if the witness table is sufficiently static, andresilience
165+
// is explicitly enabled for this compilation or if we serialize all eligible
166+
// witness tables.
167+
return (strategy == ResilienceStrategy::Resilient ||
168+
silSerializeWitnessTables) &&
169+
nominal->hasFixedLayout() &&
170+
conformance->getProtocol()->getEffectiveAccess() >=
171+
Accessibility::Public &&
172+
nominal->getEffectiveAccess() >= Accessibility::Public;
173+
}

lib/SILGen/SILGenType.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,17 +314,14 @@ class SILGenConformance : public SILGenWitnessTable<SILGenConformance> {
314314
Serialized = IsNotSerialized;
315315

316316
// Serialize the witness table if we're serializing everything with
317-
// -sil-serialize-all.
317+
// -sil-serialize-all....
318318
if (SGM.isMakeModuleFragile())
319319
Serialized = IsSerialized;
320320

321-
// Serialize the witness table if the conformance itself thinks it should be
322-
// and resilience is explicitly enabled for this compilaiton or if we serialize
323-
// all eligible witness tables.
324-
if ((SGM.M.getSwiftModule()->getResilienceStrategy() ==
325-
ResilienceStrategy::Resilient ||
326-
SGM.M.getOptions().SILSerializeWitnessTables) &&
327-
Conformance->isSerialized())
321+
// ... or if the conformance itself thinks it should be.
322+
if (SILWitnessTable::conformanceIsSerialized(
323+
Conformance, SGM.M.getSwiftModule()->getResilienceStrategy(),
324+
SGM.M.getOptions().SILSerializeWitnessTables))
328325
Serialized = IsSerialized;
329326

330327
// Not all protocols use witness tables; in this case we just skip

lib/TBDGen/TBDGen.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/IRGen/Linking.h"
2525
#include "swift/SIL/FormalLinkage.h"
2626
#include "swift/SIL/SILDeclRef.h"
27+
#include "swift/SIL/SILWitnessTable.h"
2728
#include "swift/SIL/TypeLowering.h"
2829
#include "llvm/ADT/StringSet.h"
2930

@@ -41,6 +42,7 @@ class TBDGenVisitor : public ASTVisitor<TBDGenVisitor> {
4142
const UniversalLinkageInfo &UniversalLinkInfo;
4243
ModuleDecl *SwiftModule;
4344
bool FileHasEntryPoint;
45+
bool SILSerializeWitnessTables;
4446

4547
void addSymbol(StringRef name) {
4648
auto isNewValue = Symbols.insert(name).second;
@@ -67,9 +69,11 @@ class TBDGenVisitor : public ASTVisitor<TBDGenVisitor> {
6769
public:
6870
TBDGenVisitor(StringSet &symbols,
6971
const UniversalLinkageInfo &universalLinkInfo,
70-
ModuleDecl *swiftModule, bool fileHasEntryPoint)
72+
ModuleDecl *swiftModule, bool fileHasEntryPoint,
73+
bool silSerializeWitnessTables)
7174
: Symbols(symbols), UniversalLinkInfo(universalLinkInfo),
72-
SwiftModule(swiftModule), FileHasEntryPoint(fileHasEntryPoint) {}
75+
SwiftModule(swiftModule), FileHasEntryPoint(fileHasEntryPoint),
76+
SILSerializeWitnessTables(silSerializeWitnessTables) {}
7377

7478
void visitMembers(Decl *D) {
7579
SmallVector<Decl *, 4> members;
@@ -190,10 +194,12 @@ void TBDGenVisitor::addConformances(DeclContext *DC) {
190194
// FIXME: the logic around visibility in extensions is confusing, and
191195
// sometimes witness thunks need to be manually made public.
192196

193-
auto conformanceIsSerialized = normalConformance->isSerialized();
197+
auto conformanceIsFixed = SILWitnessTable::conformanceIsSerialized(
198+
normalConformance, SwiftModule->getResilienceStrategy(),
199+
SILSerializeWitnessTables);
194200
auto addSymbolIfNecessary = [&](ValueDecl *valueReq,
195201
SILLinkage witnessLinkage) {
196-
if (conformanceIsSerialized &&
202+
if (conformanceIsFixed &&
197203
fixmeWitnessHasLinkageThatNeedsToBePublic(witnessLinkage)) {
198204
Mangle::ASTMangler Mangler;
199205
addSymbol(Mangler.mangleWitnessThunk(normalConformance, valueReq));
@@ -390,7 +396,8 @@ void TBDGenVisitor::visitProtocolDecl(ProtocolDecl *PD) {
390396

391397
void swift::enumeratePublicSymbols(FileUnit *file, StringSet &symbols,
392398
bool hasMultipleIRGenThreads,
393-
bool isWholeModule) {
399+
bool isWholeModule,
400+
bool silSerializeWitnessTables) {
394401
UniversalLinkageInfo linkInfo(file->getASTContext().LangOpts.Target,
395402
hasMultipleIRGenThreads, isWholeModule);
396403

@@ -400,7 +407,7 @@ void swift::enumeratePublicSymbols(FileUnit *file, StringSet &symbols,
400407
auto hasEntryPoint = file->hasEntryPoint();
401408

402409
TBDGenVisitor visitor(symbols, linkInfo, file->getParentModule(),
403-
hasEntryPoint);
410+
hasEntryPoint, silSerializeWitnessTables);
404411
for (auto d : decls)
405412
visitor.visit(d);
406413

0 commit comments

Comments
 (0)