Skip to content

Commit c354b0f

Browse files
committed
[TBDGen] Return a vector of symbols instead of set
A couple of clients are iterating over the result, so switch to a vector to ensure we don't accidentally introduce any non-determinism.
1 parent db7fea4 commit c354b0f

File tree

11 files changed

+40
-36
lines changed

11 files changed

+40
-36
lines changed

include/swift/AST/IRGenRequests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ struct IRGenDescriptor {
184184
ModuleDecl *getParentModule() const;
185185

186186
/// Compute the linker directives to emit.
187-
llvm::StringSet<> getLinkerDirectives() const;
187+
std::vector<std::string> getLinkerDirectives() const;
188188
};
189189

190190
/// Report that a request of the given kind is being evaluated, so it

include/swift/AST/TBDGenRequests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void simple_display(llvm::raw_ostream &out, const TBDGenDescriptor &desc);
7878
SourceLoc extractNearestSourceLoc(const TBDGenDescriptor &desc);
7979

8080
using TBDFileAndSymbols =
81-
std::pair<llvm::MachO::InterfaceFile, llvm::StringSet<>>;
81+
std::pair<llvm::MachO::InterfaceFile, std::vector<std::string>>;
8282

8383
/// Computes the TBD file and public symbols for a given module or file.
8484
class GenerateTBDRequest

include/swift/TBDGen/TBDGen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct TBDGenOptions {
8989
}
9090
};
9191

92-
llvm::StringSet<> getPublicSymbols(TBDGenDescriptor desc);
92+
std::vector<std::string> getPublicSymbols(TBDGenDescriptor desc);
9393

9494
void writeTBDFile(ModuleDecl *M, llvm::raw_ostream &os,
9595
const TBDGenOptions &opts);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ static bool writeLdAddCFileIfNeeded(CompilerInstance &Instance) {
10741074
llvm::raw_svector_ostream NameOS(NameBuffer);
10751075
NameOS << "ldAdd_" << Idx;
10761076
OS << "extern const char " << NameOS.str() << " __asm(\"" <<
1077-
changeToLdAdd(S.getKey()) << "\");\n";
1077+
changeToLdAdd(S) << "\");\n";
10781078
OS << "const char " << NameOS.str() << " = 0;\n";
10791079
++ Idx;
10801080
}

lib/FrontendTool/TBD.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ bool swift::inputFileKindCanHaveTBDValidated(InputFileKind kind) {
7373
llvm_unreachable("unhandled kind");
7474
}
7575

76-
static bool validateSymbolSet(DiagnosticEngine &diags,
77-
llvm::StringSet<> symbols,
78-
const llvm::Module &IRModule,
79-
bool diagnoseExtraSymbolsInTBD) {
76+
static bool validateSymbols(DiagnosticEngine &diags,
77+
const std::vector<std::string> &symbols,
78+
const llvm::Module &IRModule,
79+
bool diagnoseExtraSymbolsInTBD) {
80+
llvm::StringSet<> symbolSet;
81+
symbolSet.insert(symbols.begin(), symbols.end());
82+
8083
auto error = false;
8184

8285
// Diff the two sets of symbols, flagging anything outside their intersection.
@@ -101,13 +104,13 @@ static bool validateSymbolSet(DiagnosticEngine &diags,
101104
GV->hasExternalLinkage() && !GV->hasHiddenVisibility();
102105
if (!GV->isDeclaration() && externallyVisible) {
103106
// Is it listed?
104-
if (!symbols.erase(name))
107+
if (!symbolSet.erase(name))
105108
// Note: Add the unmangled name to the irNotTBD list, which is owned
106109
// by the IRModule, instead of the mangled name.
107110
irNotTBD.push_back(unmangledName);
108111
}
109112
} else {
110-
assert(symbols.find(name) == symbols.end() &&
113+
assert(symbolSet.find(name) == symbolSet.end() &&
111114
"non-global value in value symbol table");
112115
}
113116
}
@@ -121,7 +124,7 @@ static bool validateSymbolSet(DiagnosticEngine &diags,
121124

122125
if (diagnoseExtraSymbolsInTBD) {
123126
// Look for any extra symbols.
124-
for (auto &name : sortSymbols(symbols)) {
127+
for (auto &name : sortSymbols(symbolSet)) {
125128
diags.diagnose(SourceLoc(), diag::symbol_in_tbd_not_in_ir, name,
126129
Demangle::demangleSymbolAsString(name));
127130
error = true;
@@ -140,16 +143,15 @@ bool swift::validateTBD(ModuleDecl *M,
140143
const TBDGenOptions &opts,
141144
bool diagnoseExtraSymbolsInTBD) {
142145
auto symbols = getPublicSymbols(TBDGenDescriptor::forModule(M, opts));
143-
return validateSymbolSet(M->getASTContext().Diags, symbols, IRModule,
144-
diagnoseExtraSymbolsInTBD);
146+
return validateSymbols(M->getASTContext().Diags, symbols, IRModule,
147+
diagnoseExtraSymbolsInTBD);
145148
}
146149

147150
bool swift::validateTBD(FileUnit *file,
148151
const llvm::Module &IRModule,
149152
const TBDGenOptions &opts,
150153
bool diagnoseExtraSymbolsInTBD) {
151154
auto symbols = getPublicSymbols(TBDGenDescriptor::forFile(file, opts));
152-
return validateSymbolSet(file->getParentModule()->getASTContext().Diags,
153-
symbols, IRModule,
154-
diagnoseExtraSymbolsInTBD);
155+
return validateSymbols(file->getParentModule()->getASTContext().Diags,
156+
symbols, IRModule, diagnoseExtraSymbolsInTBD);
155157
}

lib/IRGen/GenDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ static bool hasCodeCoverageInstrumentation(SILFunction &f, SILModule &m) {
10311031
}
10321032

10331033
void IRGenerator::emitGlobalTopLevel(
1034-
const llvm::StringSet<> &linkerDirectives) {
1034+
const std::vector<std::string> &linkerDirectives) {
10351035
// Generate order numbers for the functions in the SIL module that
10361036
// correspond to definitions in the LLVM module.
10371037
unsigned nextOrderNumber = 0;
@@ -1051,8 +1051,8 @@ void IRGenerator::emitGlobalTopLevel(
10511051
CurrentIGMPtr IGM = getGenModule(wt.getProtocol()->getDeclContext());
10521052
ensureRelativeSymbolCollocation(wt);
10531053
}
1054-
for (auto &entry: linkerDirectives) {
1055-
createLinkerDirectiveVariable(*PrimaryIGM, entry.getKey());
1054+
for (auto &directive: linkerDirectives) {
1055+
createLinkerDirectiveVariable(*PrimaryIGM, directive);
10561056
}
10571057
for (SILGlobalVariable &v : PrimaryIGM->getSILModule().getSILGlobals()) {
10581058
Decl *decl = v.getDecl();

lib/IRGen/IRGenModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ class IRGenerator {
375375

376376
/// Emit functions, variables and tables which are needed anyway, e.g. because
377377
/// they are externally visible.
378-
void emitGlobalTopLevel(const llvm::StringSet<> &LinkerDirectives);
378+
void emitGlobalTopLevel(const std::vector<std::string> &LinkerDirectives);
379379

380380
/// Emit references to each of the protocol descriptors defined in this
381381
/// IR module.

lib/IRGen/IRGenRequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ ModuleDecl *IRGenDescriptor::getParentModule() const {
7676
return Ctx.get<ModuleDecl *>();
7777
}
7878

79-
llvm::StringSet<> IRGenDescriptor::getLinkerDirectives() const {
79+
std::vector<std::string> IRGenDescriptor::getLinkerDirectives() const {
8080
auto opts = TBDOpts;
8181
opts.LinkerDirectivesOnly = true;
8282
if (auto *SF = Ctx.dyn_cast<SourceFile *>()) {

lib/TBDGen/TBDGen.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ void TBDGenVisitor::addSymbolInternal(StringRef name,
6767
if (!isLinkerDirective && Opts.LinkerDirectivesOnly)
6868
return;
6969
Symbols.addSymbol(kind, name, Targets);
70-
if (StringSymbols && kind == SymbolKind::GlobalSymbol) {
71-
auto isNewValue = StringSymbols->insert(name).second;
72-
(void)isNewValue;
70+
if (kind == SymbolKind::GlobalSymbol) {
71+
StringSymbols.push_back(name);
7372
#ifndef NDEBUG
74-
if (!isNewValue) {
73+
if (!DuplicateSymbolChecker.insert(name).second) {
7574
llvm::dbgs() << "TBDGen duplicate symbol: " << name << '\n';
7675
assert(false && "TBDGen symbol appears twice");
7776
}
@@ -1135,10 +1134,8 @@ GenerateTBDRequest::evaluate(Evaluator &evaluator,
11351134
llvm::MachO::Target targetVar(*ctx.LangOpts.TargetVariant);
11361135
file.addTarget(targetVar);
11371136
}
1138-
StringSet symbols;
11391137
auto *clang = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
1140-
TBDGenVisitor visitor(file, {target}, &symbols,
1141-
clang->getTargetInfo().getDataLayout(),
1138+
TBDGenVisitor visitor(file, {target}, clang->getTargetInfo().getDataLayout(),
11421139
linkInfo, M, opts);
11431140

11441141
auto visitFile = [&](FileUnit *file) {
@@ -1187,10 +1184,10 @@ GenerateTBDRequest::evaluate(Evaluator &evaluator,
11871184
});
11881185
}
11891186

1190-
return std::make_pair(std::move(file), std::move(symbols));
1187+
return std::make_pair(std::move(file), std::move(visitor.StringSymbols));
11911188
}
11921189

1193-
StringSet swift::getPublicSymbols(TBDGenDescriptor desc) {
1190+
std::vector<std::string> swift::getPublicSymbols(TBDGenDescriptor desc) {
11941191
auto &evaluator = desc.getParentModule()->getASTContext().evaluator;
11951192
return llvm::cantFail(evaluator(GenerateTBDRequest{desc})).second;
11961193
}

lib/TBDGen/TBDGenVisitor.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ class TBDGenVisitor : public ASTVisitor<TBDGenVisitor> {
6363
public:
6464
llvm::MachO::InterfaceFile &Symbols;
6565
llvm::MachO::TargetList Targets;
66-
StringSet *StringSymbols;
66+
std::vector<std::string> StringSymbols;
6767
const llvm::DataLayout &DataLayout;
6868

69+
#ifndef NDEBUG
70+
/// Tracks the symbols emitted to ensure we don't emit any duplicates.
71+
llvm::StringSet<> DuplicateSymbolChecker;
72+
#endif
73+
6974
const UniversalLinkageInfo &UniversalLinkInfo;
7075
ModuleDecl *SwiftModule;
7176
const TBDGenOptions &Opts;
@@ -136,13 +141,13 @@ class TBDGenVisitor : public ASTVisitor<TBDGenVisitor> {
136141

137142
public:
138143
TBDGenVisitor(llvm::MachO::InterfaceFile &symbols,
139-
llvm::MachO::TargetList targets, StringSet *stringSymbols,
144+
llvm::MachO::TargetList targets,
140145
const llvm::DataLayout &dataLayout,
141146
const UniversalLinkageInfo &universalLinkInfo,
142147
ModuleDecl *swiftModule, const TBDGenOptions &opts)
143-
: Symbols(symbols), Targets(targets), StringSymbols(stringSymbols),
144-
DataLayout(dataLayout), UniversalLinkInfo(universalLinkInfo),
145-
SwiftModule(swiftModule), Opts(opts),
148+
: Symbols(symbols), Targets(targets), DataLayout(dataLayout),
149+
UniversalLinkInfo(universalLinkInfo), SwiftModule(swiftModule),
150+
Opts(opts),
146151
previousInstallNameMap(parsePreviousModuleInstallNameMap()) {}
147152
~TBDGenVisitor() { assert(DeclStack.empty()); }
148153
void addMainIfNecessary(FileUnit *file) {

0 commit comments

Comments
 (0)