Skip to content

Commit 3c58457

Browse files
authored
[lld][WebAssembly] Move input vectors from symtab to ctx. NFC (#78640)
Also convert from std::vector to SmallVector. This matches the ELF linker where these were moved into the ctx object in 9a57216 and converted to SmallVector in ba948c5.
1 parent 80fcf48 commit 3c58457

8 files changed

+59
-52
lines changed

lld/wasm/Config.h

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ enum class CodeGenOptLevel;
2323
namespace lld::wasm {
2424

2525
class InputFile;
26+
class StubFile;
27+
class ObjFile;
28+
class SharedFile;
29+
class BitcodeFile;
30+
class InputTable;
31+
class InputGlobal;
32+
class InputFunction;
2633
class Symbol;
2734

2835
// For --unresolved-symbols.
@@ -108,6 +115,14 @@ extern Configuration *config;
108115

109116
// The Ctx object hold all other (non-configuration) global state.
110117
struct Ctx {
118+
llvm::SmallVector<ObjFile *, 0> objectFiles;
119+
llvm::SmallVector<StubFile *, 0> stubFiles;
120+
llvm::SmallVector<SharedFile *, 0> sharedFiles;
121+
llvm::SmallVector<BitcodeFile *, 0> bitcodeFiles;
122+
llvm::SmallVector<InputFunction *, 0> syntheticFunctions;
123+
llvm::SmallVector<InputGlobal *, 0> syntheticGlobals;
124+
llvm::SmallVector<InputTable *, 0> syntheticTables;
125+
111126
// True if we are creating position-independent code.
112127
bool isPic;
113128

lld/wasm/Driver.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ static void createOptionalSymbols() {
899899

900900
static void processStubLibrariesPreLTO() {
901901
log("-- processStubLibrariesPreLTO");
902-
for (auto &stub_file : symtab->stubFiles) {
902+
for (auto &stub_file : ctx.stubFiles) {
903903
LLVM_DEBUG(llvm::dbgs()
904904
<< "processing stub file: " << stub_file->getName() << "\n");
905905
for (auto [name, deps]: stub_file->symbolDependencies) {
@@ -924,7 +924,7 @@ static void processStubLibraries() {
924924
bool depsAdded = false;
925925
do {
926926
depsAdded = false;
927-
for (auto &stub_file : symtab->stubFiles) {
927+
for (auto &stub_file : ctx.stubFiles) {
928928
LLVM_DEBUG(llvm::dbgs()
929929
<< "processing stub file: " << stub_file->getName() << "\n");
930930
for (auto [name, deps]: stub_file->symbolDependencies) {
@@ -1075,7 +1075,7 @@ static void wrapSymbols(ArrayRef<WrappedSymbol> wrapped) {
10751075
}
10761076

10771077
// Update pointers in input files.
1078-
parallelForEach(symtab->objectFiles, [&](InputFile *file) {
1078+
parallelForEach(ctx.objectFiles, [&](InputFile *file) {
10791079
MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
10801080
for (size_t i = 0, e = syms.size(); i != e; ++i)
10811081
if (Symbol *s = map.lookup(syms[i]))
@@ -1091,7 +1091,7 @@ static void splitSections() {
10911091
// splitIntoPieces needs to be called on each MergeInputChunk
10921092
// before calling finalizeContents().
10931093
LLVM_DEBUG(llvm::dbgs() << "splitSections\n");
1094-
parallelForEach(symtab->objectFiles, [](ObjFile *file) {
1094+
parallelForEach(ctx.objectFiles, [](ObjFile *file) {
10951095
for (InputChunk *seg : file->segments) {
10961096
if (auto *s = dyn_cast<MergeInputChunk>(seg))
10971097
s->splitIntoPieces();
@@ -1263,7 +1263,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
12631263
// We only need to add libcall symbols to the link before LTO if the symbol's
12641264
// definition is in bitcode. Any other required libcall symbols will be added
12651265
// to the link after LTO when we add the LTO object file to the link.
1266-
if (!symtab->bitcodeFiles.empty())
1266+
if (!ctx.bitcodeFiles.empty())
12671267
for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
12681268
handleLibcall(s);
12691269
if (errorCount())

lld/wasm/MapFile.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static void writeHeader(raw_ostream &os, int64_t vma, uint64_t lma,
5151
// Returns a list of all symbols that we want to print out.
5252
static std::vector<Symbol *> getSymbols() {
5353
std::vector<Symbol *> v;
54-
for (InputFile *file : symtab->objectFiles)
54+
for (InputFile *file : ctx.objectFiles)
5555
for (Symbol *b : file->getSymbols())
5656
if (auto *dr = dyn_cast<Symbol>(b))
5757
if ((!isa<SectionSymbol>(dr)) && dr->isLive() &&

lld/wasm/MarkLive.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void MarkLive::run() {
9797
enqueue(WasmSym::callDtors);
9898

9999
// Enqueue constructors in objects explicitly live from the command-line.
100-
for (const ObjFile *obj : symtab->objectFiles)
100+
for (const ObjFile *obj : ctx.objectFiles)
101101
if (obj->isLive())
102102
enqueueInitFunctions(obj);
103103

@@ -151,7 +151,7 @@ void markLive() {
151151

152152
// Report garbage-collected sections.
153153
if (config->printGcSections) {
154-
for (const ObjFile *obj : symtab->objectFiles) {
154+
for (const ObjFile *obj : ctx.objectFiles) {
155155
for (InputChunk *c : obj->functions)
156156
if (!c->live)
157157
message("removing unused section " + toString(c));
@@ -168,13 +168,13 @@ void markLive() {
168168
if (!t->live)
169169
message("removing unused section " + toString(t));
170170
}
171-
for (InputChunk *c : symtab->syntheticFunctions)
171+
for (InputChunk *c : ctx.syntheticFunctions)
172172
if (!c->live)
173173
message("removing unused section " + toString(c));
174-
for (InputGlobal *g : symtab->syntheticGlobals)
174+
for (InputGlobal *g : ctx.syntheticGlobals)
175175
if (!g->live)
176176
message("removing unused section " + toString(g));
177-
for (InputTable *t : symtab->syntheticTables)
177+
for (InputTable *t : ctx.syntheticTables)
178178
if (!t->live)
179179
message("removing unused section " + toString(t));
180180
}
@@ -192,7 +192,7 @@ bool MarkLive::isCallCtorsLive() {
192192

193193
// If there are any init functions, mark `__wasm_call_ctors` live so that
194194
// it can call them.
195-
for (const ObjFile *file : symtab->objectFiles) {
195+
for (const ObjFile *file : ctx.objectFiles) {
196196
const WasmLinkingData &l = file->getWasmObj()->linkingData();
197197
for (const WasmInitFunc &f : l.InitFunctions) {
198198
auto *sym = file->getFunctionSymbol(f.Symbol);

lld/wasm/SymbolTable.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ void SymbolTable::addFile(InputFile *file, StringRef symName) {
3434

3535
// .so file
3636
if (auto *f = dyn_cast<SharedFile>(file)) {
37-
sharedFiles.push_back(f);
37+
ctx.sharedFiles.push_back(f);
3838
return;
3939
}
4040

4141
// stub file
4242
if (auto *f = dyn_cast<StubFile>(file)) {
4343
f->parse();
44-
stubFiles.push_back(f);
44+
ctx.stubFiles.push_back(f);
4545
return;
4646
}
4747

@@ -52,15 +52,15 @@ void SymbolTable::addFile(InputFile *file, StringRef symName) {
5252
if (auto *f = dyn_cast<BitcodeFile>(file)) {
5353
// This order, first adding to `bitcodeFiles` and then parsing is necessary.
5454
// See https://github.com/llvm/llvm-project/pull/73095
55-
bitcodeFiles.push_back(f);
55+
ctx.bitcodeFiles.push_back(f);
5656
f->parse(symName);
5757
return;
5858
}
5959

6060
// Regular object file
6161
auto *f = cast<ObjFile>(file);
6262
f->parse(false);
63-
objectFiles.push_back(f);
63+
ctx.objectFiles.push_back(f);
6464
}
6565

6666
// This function is where all the optimizations of link-time
@@ -74,18 +74,18 @@ void SymbolTable::compileBitcodeFiles() {
7474
// Prevent further LTO objects being included
7575
BitcodeFile::doneLTO = true;
7676

77-
if (bitcodeFiles.empty())
77+
if (ctx.bitcodeFiles.empty())
7878
return;
7979

8080
// Compile bitcode files and replace bitcode symbols.
8181
lto.reset(new BitcodeCompiler);
82-
for (BitcodeFile *f : bitcodeFiles)
82+
for (BitcodeFile *f : ctx.bitcodeFiles)
8383
lto->add(*f);
8484

8585
for (StringRef filename : lto->compile()) {
8686
auto *obj = make<ObjFile>(MemoryBufferRef(filename, "lto.tmp"), "");
8787
obj->parse(true);
88-
objectFiles.push_back(obj);
88+
ctx.objectFiles.push_back(obj);
8989
}
9090
}
9191

@@ -218,7 +218,7 @@ DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name,
218218
InputFunction *function) {
219219
LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n");
220220
assert(!find(name));
221-
syntheticFunctions.emplace_back(function);
221+
ctx.syntheticFunctions.emplace_back(function);
222222
return replaceSymbol<DefinedFunction>(insertName(name).first, name,
223223
flags, nullptr, function);
224224
}
@@ -255,7 +255,7 @@ DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags,
255255
LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global
256256
<< "\n");
257257
assert(!find(name));
258-
syntheticGlobals.emplace_back(global);
258+
ctx.syntheticGlobals.emplace_back(global);
259259
return replaceSymbol<DefinedGlobal>(insertName(name).first, name, flags,
260260
nullptr, global);
261261
}
@@ -267,7 +267,7 @@ DefinedGlobal *SymbolTable::addOptionalGlobalSymbol(StringRef name,
267267
return nullptr;
268268
LLVM_DEBUG(dbgs() << "addOptionalGlobalSymbol: " << name << " -> " << global
269269
<< "\n");
270-
syntheticGlobals.emplace_back(global);
270+
ctx.syntheticGlobals.emplace_back(global);
271271
return replaceSymbol<DefinedGlobal>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN,
272272
nullptr, global);
273273
}
@@ -280,7 +280,7 @@ DefinedTable *SymbolTable::addSyntheticTable(StringRef name, uint32_t flags,
280280
assert(!s || s->isUndefined());
281281
if (!s)
282282
s = insertName(name).first;
283-
syntheticTables.emplace_back(table);
283+
ctx.syntheticTables.emplace_back(table);
284284
return replaceSymbol<DefinedTable>(s, name, flags, nullptr, table);
285285
}
286286

@@ -855,7 +855,7 @@ InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym,
855855
StringRef debugName) {
856856
auto *func = make<SyntheticFunction>(sig, sym->getName(), debugName);
857857
func->setBody(unreachableFn);
858-
syntheticFunctions.emplace_back(func);
858+
ctx.syntheticFunctions.emplace_back(func);
859859
// Mark new symbols as local. For relocatable output we don't want them
860860
// to be exported outside the object file.
861861
replaceSymbol<DefinedFunction>(sym, debugName, WASM_SYMBOL_BINDING_LOCAL,

lld/wasm/SymbolTable.h

-8
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,6 @@ class SymbolTable {
101101
void handleWeakUndefines();
102102
DefinedFunction *createUndefinedStub(const WasmSignature &sig);
103103

104-
std::vector<ObjFile *> objectFiles;
105-
std::vector<StubFile *> stubFiles;
106-
std::vector<SharedFile *> sharedFiles;
107-
std::vector<BitcodeFile *> bitcodeFiles;
108-
std::vector<InputFunction *> syntheticFunctions;
109-
std::vector<InputGlobal *> syntheticGlobals;
110-
std::vector<InputTable *> syntheticTables;
111-
112104
private:
113105
std::pair<Symbol *, bool> insert(StringRef name, const InputFile *file);
114106
std::pair<Symbol *, bool> insertName(StringRef name);

lld/wasm/SyntheticSections.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class SubSection {
5757
bool DylinkSection::isNeeded() const {
5858
return ctx.isPic ||
5959
config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic ||
60-
!symtab->sharedFiles.empty();
60+
!ctx.sharedFiles.empty();
6161
}
6262

6363
void DylinkSection::writeBody() {
@@ -72,10 +72,10 @@ void DylinkSection::writeBody() {
7272
sub.writeTo(os);
7373
}
7474

75-
if (symtab->sharedFiles.size()) {
75+
if (ctx.sharedFiles.size()) {
7676
SubSection sub(WASM_DYLINK_NEEDED);
77-
writeUleb128(sub.os, symtab->sharedFiles.size(), "Needed");
78-
for (auto *so : symtab->sharedFiles)
77+
writeUleb128(sub.os, ctx.sharedFiles.size(), "Needed");
78+
for (auto *so : ctx.sharedFiles)
7979
writeStr(sub.os, llvm::sys::path::filename(so->getName()), "so name");
8080
sub.writeTo(os);
8181
}

lld/wasm/Writer.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Writer {
133133
void Writer::calculateCustomSections() {
134134
log("calculateCustomSections");
135135
bool stripDebug = config->stripDebug || config->stripAll;
136-
for (ObjFile *file : symtab->objectFiles) {
136+
for (ObjFile *file : ctx.objectFiles) {
137137
for (InputChunk *section : file->customSections) {
138138
// Exclude COMDAT sections that are not selected for inclusion
139139
if (section->discarded)
@@ -207,7 +207,7 @@ void Writer::createRelocSections() {
207207
}
208208

209209
void Writer::populateProducers() {
210-
for (ObjFile *file : symtab->objectFiles) {
210+
for (ObjFile *file : ctx.objectFiles) {
211211
const WasmProducerInfo &info = file->getWasmObj()->getProducerInfo();
212212
out.producersSec->addInfo(info);
213213
}
@@ -591,7 +591,7 @@ void Writer::populateTargetFeatures() {
591591
}
592592

593593
// Find the sets of used, required, and disallowed features
594-
for (ObjFile *file : symtab->objectFiles) {
594+
for (ObjFile *file : ctx.objectFiles) {
595595
StringRef fileName(file->getName());
596596
for (auto &feature : file->getWasmObj()->getTargetFeatures()) {
597597
switch (feature.Prefix) {
@@ -654,7 +654,7 @@ void Writer::populateTargetFeatures() {
654654
}
655655

656656
// Validate the required and disallowed constraints for each file
657-
for (ObjFile *file : symtab->objectFiles) {
657+
for (ObjFile *file : ctx.objectFiles) {
658658
StringRef fileName(file->getName());
659659
SmallSet<std::string, 8> objectFeatures;
660660
for (const auto &feature : file->getWasmObj()->getTargetFeatures()) {
@@ -832,7 +832,7 @@ void Writer::populateSymtab() {
832832
if (sym->isUsedInRegularObj && sym->isLive())
833833
out.linkingSec->addToSymtab(sym);
834834

835-
for (ObjFile *file : symtab->objectFiles) {
835+
for (ObjFile *file : ctx.objectFiles) {
836836
LLVM_DEBUG(dbgs() << "Local symtab entries: " << file->getName() << "\n");
837837
for (Symbol *sym : file->getSymbols())
838838
if (sym->isLocal() && !isa<SectionSymbol>(sym) && sym->isLive())
@@ -848,7 +848,7 @@ void Writer::calculateTypes() {
848848
// 4. The signatures of all imported tags
849849
// 5. The signatures of all defined tags
850850

851-
for (ObjFile *file : symtab->objectFiles) {
851+
for (ObjFile *file : ctx.objectFiles) {
852852
ArrayRef<WasmSignature> types = file->getWasmObj()->types();
853853
for (uint32_t i = 0; i < types.size(); i++)
854854
if (file->typeIsUsed[i])
@@ -939,7 +939,7 @@ static void finalizeIndirectFunctionTable() {
939939
}
940940

941941
static void scanRelocations() {
942-
for (ObjFile *file : symtab->objectFiles) {
942+
for (ObjFile *file : ctx.objectFiles) {
943943
LLVM_DEBUG(dbgs() << "scanRelocations: " << file->getName() << "\n");
944944
for (InputChunk *chunk : file->functions)
945945
scanRelocations(chunk);
@@ -955,37 +955,37 @@ void Writer::assignIndexes() {
955955
// global are effected by the number of imports.
956956
out.importSec->seal();
957957

958-
for (InputFunction *func : symtab->syntheticFunctions)
958+
for (InputFunction *func : ctx.syntheticFunctions)
959959
out.functionSec->addFunction(func);
960960

961-
for (ObjFile *file : symtab->objectFiles) {
961+
for (ObjFile *file : ctx.objectFiles) {
962962
LLVM_DEBUG(dbgs() << "Functions: " << file->getName() << "\n");
963963
for (InputFunction *func : file->functions)
964964
out.functionSec->addFunction(func);
965965
}
966966

967-
for (InputGlobal *global : symtab->syntheticGlobals)
967+
for (InputGlobal *global : ctx.syntheticGlobals)
968968
out.globalSec->addGlobal(global);
969969

970-
for (ObjFile *file : symtab->objectFiles) {
970+
for (ObjFile *file : ctx.objectFiles) {
971971
LLVM_DEBUG(dbgs() << "Globals: " << file->getName() << "\n");
972972
for (InputGlobal *global : file->globals)
973973
out.globalSec->addGlobal(global);
974974
}
975975

976-
for (ObjFile *file : symtab->objectFiles) {
976+
for (ObjFile *file : ctx.objectFiles) {
977977
LLVM_DEBUG(dbgs() << "Tags: " << file->getName() << "\n");
978978
for (InputTag *tag : file->tags)
979979
out.tagSec->addTag(tag);
980980
}
981981

982-
for (ObjFile *file : symtab->objectFiles) {
982+
for (ObjFile *file : ctx.objectFiles) {
983983
LLVM_DEBUG(dbgs() << "Tables: " << file->getName() << "\n");
984984
for (InputTable *table : file->tables)
985985
out.tableSec->addTable(table);
986986
}
987987

988-
for (InputTable *table : symtab->syntheticTables)
988+
for (InputTable *table : ctx.syntheticTables)
989989
out.tableSec->addTable(table);
990990

991991
out.globalSec->assignIndexes();
@@ -1022,7 +1022,7 @@ OutputSegment *Writer::createOutputSegment(StringRef name) {
10221022
}
10231023

10241024
void Writer::createOutputSegments() {
1025-
for (ObjFile *file : symtab->objectFiles) {
1025+
for (ObjFile *file : ctx.objectFiles) {
10261026
for (InputChunk *segment : file->segments) {
10271027
if (!segment->live)
10281028
continue;
@@ -1639,7 +1639,7 @@ void Writer::calculateInitFunctions() {
16391639
if (!config->relocatable && !WasmSym::callCtors->isLive())
16401640
return;
16411641

1642-
for (ObjFile *file : symtab->objectFiles) {
1642+
for (ObjFile *file : ctx.objectFiles) {
16431643
const WasmLinkingData &l = file->getWasmObj()->linkingData();
16441644
for (const WasmInitFunc &f : l.InitFunctions) {
16451645
FunctionSymbol *sym = file->getFunctionSymbol(f.Symbol);

0 commit comments

Comments
 (0)