Skip to content

Commit 3792b36

Browse files
authored
[lld][WebAssembly] Replace config-> with ctx.arg.
Change the global variable reference to a member access of another variable `ctx`. In the future, we may pass through `ctx` to functions to eliminate global variables. Pull Request: #119835
1 parent 8e40450 commit 3792b36

18 files changed

+374
-384
lines changed

lld/wasm/Config.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,9 @@ struct Config {
126126
llvm::SmallVector<uint8_t, 0> buildIdVector;
127127
};
128128

129-
struct ConfigWrapper {
130-
Config c;
131-
Config *operator->() { return &c; }
132-
};
133-
134-
// The only instance of Configuration struct.
135-
extern ConfigWrapper config;
136-
137129
// The Ctx object hold all other (non-configuration) global state.
138130
struct Ctx {
139-
Config &arg;
131+
Config arg;
140132

141133
llvm::SmallVector<ObjFile *, 0> objectFiles;
142134
llvm::SmallVector<StubFile *, 0> stubFiles;

lld/wasm/Driver.cpp

Lines changed: 152 additions & 154 deletions
Large diffs are not rendered by default.

lld/wasm/InputChunks.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ uint32_t InputChunk::getSize() const {
6767
return ms->builder.getSize();
6868

6969
if (const auto *f = dyn_cast<InputFunction>(this)) {
70-
if (config->compressRelocations && f->file) {
70+
if (ctx.arg.compressRelocations && f->file) {
7171
return f->getCompressedSize();
7272
}
7373
}
@@ -84,7 +84,7 @@ uint32_t InputChunk::getInputSize() const {
8484
// Copy this input chunk to an mmap'ed output file and apply relocations.
8585
void InputChunk::writeTo(uint8_t *buf) const {
8686
if (const auto *f = dyn_cast<InputFunction>(this)) {
87-
if (file && config->compressRelocations)
87+
if (file && ctx.arg.compressRelocations)
8888
return f->writeCompressed(buf);
8989
} else if (const auto *ms = dyn_cast<SyntheticMergedChunk>(this)) {
9090
ms->builder.write(buf + outSecOff);
@@ -269,7 +269,7 @@ static unsigned getRelocWidth(const WasmRelocation &rel, uint64_t value) {
269269
// This function only computes the final output size. It must be called
270270
// before getSize() is used to calculate of layout of the code section.
271271
void InputFunction::calculateSize() {
272-
if (!file || !config->compressRelocations)
272+
if (!file || !ctx.arg.compressRelocations)
273273
return;
274274

275275
LLVM_DEBUG(dbgs() << "calculateSize: " << name << "\n");
@@ -365,7 +365,7 @@ bool InputChunk::generateRelocationCode(raw_ostream &os) const {
365365
LLVM_DEBUG(dbgs() << "generating runtime relocations: " << name
366366
<< " count=" << relocations.size() << "\n");
367367

368-
bool is64 = config->is64.value_or(false);
368+
bool is64 = ctx.arg.is64.value_or(false);
369369
bool generated = false;
370370
unsigned opcode_ptr_const = is64 ? WASM_OPCODE_I64_CONST
371371
: WASM_OPCODE_I32_CONST;

lld/wasm/InputChunks.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class InputChunk {
112112
InputChunk(ObjFile *f, Kind k, StringRef name, uint32_t alignment = 0,
113113
uint32_t flags = 0)
114114
: name(name), file(f), alignment(alignment), flags(flags), sectionKind(k),
115-
live(!config->gcSections), discarded(false) {}
115+
live(!ctx.arg.gcSections), discarded(false) {}
116116
ArrayRef<uint8_t> data() const { return rawData; }
117117
uint64_t getTombstone() const;
118118

@@ -156,7 +156,7 @@ class SyntheticMergedChunk;
156156
// be found by looking at the next one).
157157
struct SectionPiece {
158158
SectionPiece(size_t off, uint32_t hash, bool live)
159-
: inputOff(off), live(live || !config->gcSections), hash(hash >> 1) {}
159+
: inputOff(off), live(live || !ctx.arg.gcSections), hash(hash >> 1) {}
160160

161161
uint32_t inputOff;
162162
uint32_t live : 1;

lld/wasm/InputElement.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace wasm {
2424
class InputElement {
2525
protected:
2626
InputElement(StringRef name, ObjFile *f)
27-
: file(f), live(!config->gcSections), name(name) {}
27+
: file(f), live(!ctx.arg.gcSections), name(name) {}
2828

2929
public:
3030
StringRef getName() const { return name; }
@@ -65,7 +65,7 @@ class InputGlobal : public InputElement {
6565
const WasmInitExpr &getInitExpr() const { return initExpr; }
6666

6767
void setPointerValue(uint64_t value) {
68-
initExpr = intConst(value, config->is64.value_or(false));
68+
initExpr = intConst(value, ctx.arg.is64.value_or(false));
6969
}
7070

7171
private:

lld/wasm/InputFiles.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ std::string toString(const wasm::InputFile *file) {
4747
namespace wasm {
4848

4949
std::string replaceThinLTOSuffix(StringRef path) {
50-
auto [suffix, repl] = config->thinLTOObjectSuffixReplace;
50+
auto [suffix, repl] = ctx.arg.thinLTOObjectSuffixReplace;
5151
if (path.consume_back(suffix))
5252
return (path + repl).str();
5353
return std::string(path);
5454
}
5555

5656
void InputFile::checkArch(Triple::ArchType arch) const {
5757
bool is64 = arch == Triple::wasm64;
58-
if (is64 && !config->is64) {
58+
if (is64 && !ctx.arg.is64) {
5959
fatal(toString(this) +
6060
": must specify -mwasm64 to process wasm64 object files");
61-
} else if (config->is64.value_or(false) != is64) {
61+
} else if (ctx.arg.is64.value_or(false) != is64) {
6262
fatal(toString(this) +
6363
": wasm32 object file can't be linked in wasm64 mode");
6464
}
@@ -169,7 +169,7 @@ uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc, uint64_t tombstone,
169169
uint32_t index = getFunctionSymbol(reloc.Index)->getTableIndex();
170170
if (reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB ||
171171
reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB64)
172-
index -= config->tableBase;
172+
index -= ctx.arg.tableBase;
173173
return index;
174174
}
175175
case R_WASM_MEMORY_ADDR_LEB:
@@ -360,7 +360,7 @@ void ObjFile::addLegacyIndirectFunctionTableIfNeeded(
360360
}
361361

362362
static bool shouldMerge(const WasmSection &sec) {
363-
if (config->optimize == 0)
363+
if (ctx.arg.optimize == 0)
364364
return false;
365365
// Sadly we don't have section attributes yet for custom sections, so we
366366
// currently go by the name alone.
@@ -383,7 +383,7 @@ static bool shouldMerge(const WasmSegment &seg) {
383383
// On a regular link we don't merge sections if -O0 (default is -O1). This
384384
// sometimes makes the linker significantly faster, although the output will
385385
// be bigger.
386-
if (config->optimize == 0)
386+
if (ctx.arg.optimize == 0)
387387
return false;
388388

389389
// A mergeable section with size 0 is useless because they don't have
@@ -845,7 +845,7 @@ BitcodeFile::BitcodeFile(MemoryBufferRef m, StringRef archiveName,
845845
this->archiveName = std::string(archiveName);
846846

847847
std::string path = mb.getBufferIdentifier().str();
848-
if (config->thinLTOIndexOnly)
848+
if (ctx.arg.thinLTOIndexOnly)
849849
path = replaceThinLTOSuffix(mb.getBufferIdentifier());
850850

851851
// ThinLTO assumes that all MemoryBufferRefs given to it have a unique

lld/wasm/InputFiles.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class InputFile {
7373

7474
protected:
7575
InputFile(Kind k, MemoryBufferRef m)
76-
: mb(m), fileKind(k), live(!config->gcSections) {}
76+
: mb(m), fileKind(k), live(!ctx.arg.gcSections) {}
7777

7878
void checkArch(llvm::Triple::ArchType arch) const;
7979

lld/wasm/LTO.cpp

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ using namespace lld::wasm;
4444
using namespace lld;
4545

4646
static std::string getThinLTOOutputFile(StringRef modulePath) {
47-
return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
48-
config->thinLTOPrefixReplaceNew);
47+
return lto::getThinLTOOutputFile(modulePath, ctx.arg.thinLTOPrefixReplaceOld,
48+
ctx.arg.thinLTOPrefixReplaceNew);
4949
}
5050

5151
static lto::Config createConfig() {
@@ -56,23 +56,23 @@ static lto::Config createConfig() {
5656
c.Options.FunctionSections = true;
5757
c.Options.DataSections = true;
5858

59-
c.DisableVerify = config->disableVerify;
59+
c.DisableVerify = ctx.arg.disableVerify;
6060
c.DiagHandler = diagnosticHandler;
61-
c.OptLevel = config->ltoo;
61+
c.OptLevel = ctx.arg.ltoo;
6262
c.MAttrs = getMAttrs();
63-
c.CGOptLevel = config->ltoCgo;
64-
c.DebugPassManager = config->ltoDebugPassManager;
65-
c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
63+
c.CGOptLevel = ctx.arg.ltoCgo;
64+
c.DebugPassManager = ctx.arg.ltoDebugPassManager;
65+
c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty();
6666

67-
if (config->relocatable)
67+
if (ctx.arg.relocatable)
6868
c.RelocModel = std::nullopt;
6969
else if (ctx.isPic)
7070
c.RelocModel = Reloc::PIC_;
7171
else
7272
c.RelocModel = Reloc::Static;
7373

74-
if (config->saveTemps)
75-
checkError(c.addSaveTemps(config->outputFile.str() + ".",
74+
if (ctx.arg.saveTemps)
75+
checkError(c.addSaveTemps(ctx.arg.outputFile.str() + ".",
7676
/*UseInputModulePath*/ true));
7777
return c;
7878
}
@@ -81,27 +81,27 @@ namespace lld::wasm {
8181

8282
BitcodeCompiler::BitcodeCompiler() {
8383
// Initialize indexFile.
84-
if (!config->thinLTOIndexOnlyArg.empty())
85-
indexFile = openFile(config->thinLTOIndexOnlyArg);
84+
if (!ctx.arg.thinLTOIndexOnlyArg.empty())
85+
indexFile = openFile(ctx.arg.thinLTOIndexOnlyArg);
8686

8787
// Initialize ltoObj.
8888
lto::ThinBackend backend;
8989
auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
90-
if (config->thinLTOIndexOnly) {
90+
if (ctx.arg.thinLTOIndexOnly) {
9191
backend = lto::createWriteIndexesThinBackend(
92-
llvm::hardware_concurrency(config->thinLTOJobs),
93-
std::string(config->thinLTOPrefixReplaceOld),
94-
std::string(config->thinLTOPrefixReplaceNew),
95-
std::string(config->thinLTOPrefixReplaceNativeObject),
96-
config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
92+
llvm::hardware_concurrency(ctx.arg.thinLTOJobs),
93+
std::string(ctx.arg.thinLTOPrefixReplaceOld),
94+
std::string(ctx.arg.thinLTOPrefixReplaceNew),
95+
std::string(ctx.arg.thinLTOPrefixReplaceNativeObject),
96+
ctx.arg.thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
9797
} else {
9898
backend = lto::createInProcessThinBackend(
99-
llvm::heavyweight_hardware_concurrency(config->thinLTOJobs),
100-
onIndexWrite, config->thinLTOEmitIndexFiles,
101-
config->thinLTOEmitImportsFiles);
99+
llvm::heavyweight_hardware_concurrency(ctx.arg.thinLTOJobs),
100+
onIndexWrite, ctx.arg.thinLTOEmitIndexFiles,
101+
ctx.arg.thinLTOEmitImportsFiles);
102102
}
103103
ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,
104-
config->ltoPartitions);
104+
ctx.arg.ltoPartitions);
105105
}
106106

107107
BitcodeCompiler::~BitcodeCompiler() = default;
@@ -123,7 +123,7 @@ void BitcodeCompiler::add(BitcodeFile &f) {
123123
ArrayRef<Symbol *> syms = f.getSymbols();
124124
std::vector<lto::SymbolResolution> resols(syms.size());
125125

126-
if (config->thinLTOEmitIndexFiles) {
126+
if (ctx.arg.thinLTOEmitIndexFiles) {
127127
thinIndices.insert(obj.getName());
128128
}
129129

@@ -139,7 +139,7 @@ void BitcodeCompiler::add(BitcodeFile &f) {
139139
// Once IRObjectFile is fixed to report only one symbol this hack can
140140
// be removed.
141141
r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
142-
r.VisibleToRegularObj = config->relocatable || sym->isUsedInRegularObj ||
142+
r.VisibleToRegularObj = ctx.arg.relocatable || sym->isUsedInRegularObj ||
143143
sym->isNoStrip() ||
144144
(r.Prevailing && sym->isExported());
145145
if (r.Prevailing)
@@ -175,7 +175,7 @@ static void thinLTOCreateEmptyIndexFiles() {
175175
ModuleSummaryIndex m(/*HaveGVs*/ false);
176176
m.setSkipModuleByDistributedBackend();
177177
writeIndexToFile(m, *os);
178-
if (config->thinLTOEmitImportsFiles)
178+
if (ctx.arg.thinLTOEmitImportsFiles)
179179
openFile(path + ".imports");
180180
}
181181
}
@@ -191,8 +191,8 @@ std::vector<StringRef> BitcodeCompiler::compile() {
191191
// to cache native object files for ThinLTO incremental builds. If a path was
192192
// specified, configure LTO to use it as the cache directory.
193193
FileCache cache;
194-
if (!config->thinLTOCacheDir.empty())
195-
cache = check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
194+
if (!ctx.arg.thinLTOCacheDir.empty())
195+
cache = check(localCache("ThinLTO", "Thin", ctx.arg.thinLTOCacheDir,
196196
[&](size_t task, const Twine &moduleName,
197197
std::unique_ptr<MemoryBuffer> mb) {
198198
files[task] = std::move(mb);
@@ -210,16 +210,16 @@ std::vector<StringRef> BitcodeCompiler::compile() {
210210
for (StringRef s : thinIndices) {
211211
std::string path(s);
212212
openFile(path + ".thinlto.bc");
213-
if (config->thinLTOEmitImportsFiles)
213+
if (ctx.arg.thinLTOEmitImportsFiles)
214214
openFile(path + ".imports");
215215
}
216216

217-
if (config->thinLTOEmitIndexFiles)
217+
if (ctx.arg.thinLTOEmitIndexFiles)
218218
thinLTOCreateEmptyIndexFiles();
219219

220-
if (config->thinLTOIndexOnly) {
221-
if (!config->ltoObjPath.empty())
222-
saveBuffer(buf[0].second, config->ltoObjPath);
220+
if (ctx.arg.thinLTOIndexOnly) {
221+
if (!ctx.arg.ltoObjPath.empty())
222+
saveBuffer(buf[0].second, ctx.arg.ltoObjPath);
223223

224224
// ThinLTO with index only option is required to generate only the index
225225
// files. After that, we exit from linker and ThinLTO backend runs in a
@@ -229,8 +229,8 @@ std::vector<StringRef> BitcodeCompiler::compile() {
229229
return {};
230230
}
231231

232-
if (!config->thinLTOCacheDir.empty())
233-
pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files);
232+
if (!ctx.arg.thinLTOCacheDir.empty())
233+
pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files);
234234

235235
std::vector<StringRef> ret;
236236
for (unsigned i = 0; i != maxTasks; ++i) {
@@ -239,7 +239,7 @@ std::vector<StringRef> BitcodeCompiler::compile() {
239239
if (objBuf.empty())
240240
continue;
241241
ret.emplace_back(objBuf.data(), objBuf.size());
242-
if (!config->saveTemps)
242+
if (!ctx.arg.saveTemps)
243243
continue;
244244

245245
// If the input bitcode file is path/to/x.o and -o specifies a.out, the
@@ -248,7 +248,7 @@ std::vector<StringRef> BitcodeCompiler::compile() {
248248
StringRef ltoObjName;
249249
if (bitcodeFilePath == "ld-temp.o") {
250250
ltoObjName =
251-
saver().save(Twine(config->outputFile) + ".lto" +
251+
saver().save(Twine(ctx.arg.outputFile) + ".lto" +
252252
(i == 0 ? Twine("") : Twine('.') + Twine(i)) + ".o");
253253
} else {
254254
StringRef directory = sys::path::parent_path(bitcodeFilePath);
@@ -258,7 +258,7 @@ std::vector<StringRef> BitcodeCompiler::compile() {
258258
StringRef baseName = bitcodeFilePath.ends_with(")")
259259
? sys::path::filename(bitcodeFilePath)
260260
: sys::path::stem(bitcodeFilePath);
261-
StringRef outputFileBaseName = sys::path::filename(config->outputFile);
261+
StringRef outputFileBaseName = sys::path::filename(ctx.arg.outputFile);
262262
SmallString<256> path;
263263
sys::path::append(path, directory,
264264
outputFileBaseName + ".lto." + baseName + ".o");
@@ -268,10 +268,10 @@ std::vector<StringRef> BitcodeCompiler::compile() {
268268
saveBuffer(objBuf, ltoObjName);
269269
}
270270

271-
if (!config->ltoObjPath.empty()) {
272-
saveBuffer(buf[0].second, config->ltoObjPath);
271+
if (!ctx.arg.ltoObjPath.empty()) {
272+
saveBuffer(buf[0].second, ctx.arg.ltoObjPath);
273273
for (unsigned i = 1; i != maxTasks; ++i)
274-
saveBuffer(buf[i].second, config->ltoObjPath + Twine(i));
274+
saveBuffer(buf[i].second, ctx.arg.ltoObjPath + Twine(i));
275275
}
276276

277277
for (std::unique_ptr<MemoryBuffer> &file : files)

lld/wasm/MapFile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ getSymbolStrings(ArrayRef<Symbol *> syms) {
103103
}
104104

105105
void lld::wasm::writeMapFile(ArrayRef<OutputSection *> outputSections) {
106-
if (config->mapFile.empty())
106+
if (ctx.arg.mapFile.empty())
107107
return;
108108

109109
// Open a map file for writing.
110110
std::error_code ec;
111-
raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
111+
raw_fd_ostream os(ctx.arg.mapFile, ec, sys::fs::OF_None);
112112
if (ec) {
113-
error("cannot open " + config->mapFile + ": " + ec.message());
113+
error("cannot open " + ctx.arg.mapFile + ": " + ec.message());
114114
return;
115115
}
116116

0 commit comments

Comments
 (0)