Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
303 changes: 151 additions & 152 deletions lld/wasm/Driver.cpp

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions lld/wasm/InputChunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ uint32_t InputChunk::getSize() const {
return ms->builder.getSize();

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

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

bool is64 = config->is64.value_or(false);
bool is64 = ctx.arg.is64.value_or(false);
bool generated = false;
unsigned opcode_ptr_const = is64 ? WASM_OPCODE_I64_CONST
: WASM_OPCODE_I32_CONST;
Expand Down
4 changes: 2 additions & 2 deletions lld/wasm/InputChunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class InputChunk {
InputChunk(ObjFile *f, Kind k, StringRef name, uint32_t alignment = 0,
uint32_t flags = 0)
: name(name), file(f), alignment(alignment), flags(flags), sectionKind(k),
live(!config->gcSections), discarded(false) {}
live(!ctx.arg.gcSections), discarded(false) {}
ArrayRef<uint8_t> data() const { return rawData; }
uint64_t getTombstone() const;

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

uint32_t inputOff;
uint32_t live : 1;
Expand Down
4 changes: 2 additions & 2 deletions lld/wasm/InputElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace wasm {
class InputElement {
protected:
InputElement(StringRef name, ObjFile *f)
: file(f), live(!config->gcSections), name(name) {}
: file(f), live(!ctx.arg.gcSections), name(name) {}

public:
StringRef getName() const { return name; }
Expand Down Expand Up @@ -65,7 +65,7 @@ class InputGlobal : public InputElement {
const WasmInitExpr &getInitExpr() const { return initExpr; }

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

private:
Expand Down
14 changes: 7 additions & 7 deletions lld/wasm/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ std::string toString(const wasm::InputFile *file) {
namespace wasm {

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

void InputFile::checkArch(Triple::ArchType arch) const {
bool is64 = arch == Triple::wasm64;
if (is64 && !config->is64) {
if (is64 && !ctx.arg.is64) {
fatal(toString(this) +
": must specify -mwasm64 to process wasm64 object files");
} else if (config->is64.value_or(false) != is64) {
} else if (ctx.arg.is64.value_or(false) != is64) {
fatal(toString(this) +
": wasm32 object file can't be linked in wasm64 mode");
}
Expand Down Expand Up @@ -169,7 +169,7 @@ uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc, uint64_t tombstone,
uint32_t index = getFunctionSymbol(reloc.Index)->getTableIndex();
if (reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB ||
reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB64)
index -= config->tableBase;
index -= ctx.arg.tableBase;
return index;
}
case R_WASM_MEMORY_ADDR_LEB:
Expand Down Expand Up @@ -360,7 +360,7 @@ void ObjFile::addLegacyIndirectFunctionTableIfNeeded(
}

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

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

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

// ThinLTO assumes that all MemoryBufferRefs given to it have a unique
Expand Down
2 changes: 1 addition & 1 deletion lld/wasm/InputFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class InputFile {

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

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

Expand Down
80 changes: 40 additions & 40 deletions lld/wasm/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ using namespace lld::wasm;
using namespace lld;

static std::string getThinLTOOutputFile(StringRef modulePath) {
return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
config->thinLTOPrefixReplaceNew);
return lto::getThinLTOOutputFile(modulePath, ctx.arg.thinLTOPrefixReplaceOld,
ctx.arg.thinLTOPrefixReplaceNew);
}

static lto::Config createConfig() {
Expand All @@ -56,23 +56,23 @@ static lto::Config createConfig() {
c.Options.FunctionSections = true;
c.Options.DataSections = true;

c.DisableVerify = config->disableVerify;
c.DisableVerify = ctx.arg.disableVerify;
c.DiagHandler = diagnosticHandler;
c.OptLevel = config->ltoo;
c.OptLevel = ctx.arg.ltoo;
c.MAttrs = getMAttrs();
c.CGOptLevel = config->ltoCgo;
c.DebugPassManager = config->ltoDebugPassManager;
c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
c.CGOptLevel = ctx.arg.ltoCgo;
c.DebugPassManager = ctx.arg.ltoDebugPassManager;
c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty();

if (config->relocatable)
if (ctx.arg.relocatable)
c.RelocModel = std::nullopt;
else if (ctx.isPic)
c.RelocModel = Reloc::PIC_;
else
c.RelocModel = Reloc::Static;

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

BitcodeCompiler::BitcodeCompiler() {
// Initialize indexFile.
if (!config->thinLTOIndexOnlyArg.empty())
indexFile = openFile(config->thinLTOIndexOnlyArg);
if (!ctx.arg.thinLTOIndexOnlyArg.empty())
indexFile = openFile(ctx.arg.thinLTOIndexOnlyArg);

// Initialize ltoObj.
lto::ThinBackend backend;
auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
if (config->thinLTOIndexOnly) {
if (ctx.arg.thinLTOIndexOnly) {
backend = lto::createWriteIndexesThinBackend(
llvm::hardware_concurrency(config->thinLTOJobs),
std::string(config->thinLTOPrefixReplaceOld),
std::string(config->thinLTOPrefixReplaceNew),
std::string(config->thinLTOPrefixReplaceNativeObject),
config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
llvm::hardware_concurrency(ctx.arg.thinLTOJobs),
std::string(ctx.arg.thinLTOPrefixReplaceOld),
std::string(ctx.arg.thinLTOPrefixReplaceNew),
std::string(ctx.arg.thinLTOPrefixReplaceNativeObject),
ctx.arg.thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
} else {
backend = lto::createInProcessThinBackend(
llvm::heavyweight_hardware_concurrency(config->thinLTOJobs),
onIndexWrite, config->thinLTOEmitIndexFiles,
config->thinLTOEmitImportsFiles);
llvm::heavyweight_hardware_concurrency(ctx.arg.thinLTOJobs),
onIndexWrite, ctx.arg.thinLTOEmitIndexFiles,
ctx.arg.thinLTOEmitImportsFiles);
}
ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,
config->ltoPartitions);
ctx.arg.ltoPartitions);
}

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

if (config->thinLTOEmitIndexFiles) {
if (ctx.arg.thinLTOEmitIndexFiles) {
thinIndices.insert(obj.getName());
}

Expand All @@ -139,7 +139,7 @@ void BitcodeCompiler::add(BitcodeFile &f) {
// Once IRObjectFile is fixed to report only one symbol this hack can
// be removed.
r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
r.VisibleToRegularObj = config->relocatable || sym->isUsedInRegularObj ||
r.VisibleToRegularObj = ctx.arg.relocatable || sym->isUsedInRegularObj ||
sym->isNoStrip() ||
(r.Prevailing && sym->isExported());
if (r.Prevailing)
Expand Down Expand Up @@ -175,7 +175,7 @@ static void thinLTOCreateEmptyIndexFiles() {
ModuleSummaryIndex m(/*HaveGVs*/ false);
m.setSkipModuleByDistributedBackend();
writeIndexToFile(m, *os);
if (config->thinLTOEmitImportsFiles)
if (ctx.arg.thinLTOEmitImportsFiles)
openFile(path + ".imports");
}
}
Expand All @@ -191,8 +191,8 @@ std::vector<StringRef> BitcodeCompiler::compile() {
// to cache native object files for ThinLTO incremental builds. If a path was
// specified, configure LTO to use it as the cache directory.
FileCache cache;
if (!config->thinLTOCacheDir.empty())
cache = check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
if (!ctx.arg.thinLTOCacheDir.empty())
cache = check(localCache("ThinLTO", "Thin", ctx.arg.thinLTOCacheDir,
[&](size_t task, const Twine &moduleName,
std::unique_ptr<MemoryBuffer> mb) {
files[task] = std::move(mb);
Expand All @@ -210,16 +210,16 @@ std::vector<StringRef> BitcodeCompiler::compile() {
for (StringRef s : thinIndices) {
std::string path(s);
openFile(path + ".thinlto.bc");
if (config->thinLTOEmitImportsFiles)
if (ctx.arg.thinLTOEmitImportsFiles)
openFile(path + ".imports");
}

if (config->thinLTOEmitIndexFiles)
if (ctx.arg.thinLTOEmitIndexFiles)
thinLTOCreateEmptyIndexFiles();

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

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

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

std::vector<StringRef> ret;
for (unsigned i = 0; i != maxTasks; ++i) {
Expand All @@ -239,7 +239,7 @@ std::vector<StringRef> BitcodeCompiler::compile() {
if (objBuf.empty())
continue;
ret.emplace_back(objBuf.data(), objBuf.size());
if (!config->saveTemps)
if (!ctx.arg.saveTemps)
continue;

// If the input bitcode file is path/to/x.o and -o specifies a.out, the
Expand All @@ -248,7 +248,7 @@ std::vector<StringRef> BitcodeCompiler::compile() {
StringRef ltoObjName;
if (bitcodeFilePath == "ld-temp.o") {
ltoObjName =
saver().save(Twine(config->outputFile) + ".lto" +
saver().save(Twine(ctx.arg.outputFile) + ".lto" +
(i == 0 ? Twine("") : Twine('.') + Twine(i)) + ".o");
} else {
StringRef directory = sys::path::parent_path(bitcodeFilePath);
Expand All @@ -258,7 +258,7 @@ std::vector<StringRef> BitcodeCompiler::compile() {
StringRef baseName = bitcodeFilePath.ends_with(")")
? sys::path::filename(bitcodeFilePath)
: sys::path::stem(bitcodeFilePath);
StringRef outputFileBaseName = sys::path::filename(config->outputFile);
StringRef outputFileBaseName = sys::path::filename(ctx.arg.outputFile);
SmallString<256> path;
sys::path::append(path, directory,
outputFileBaseName + ".lto." + baseName + ".o");
Expand All @@ -268,10 +268,10 @@ std::vector<StringRef> BitcodeCompiler::compile() {
saveBuffer(objBuf, ltoObjName);
}

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

for (std::unique_ptr<MemoryBuffer> &file : files)
Expand Down
6 changes: 3 additions & 3 deletions lld/wasm/MapFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ getSymbolStrings(ArrayRef<Symbol *> syms) {
}

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

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

Expand Down
10 changes: 5 additions & 5 deletions lld/wasm/MarkLive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ void MarkLive::enqueueRetainedSegments(const ObjFile *file) {

void MarkLive::run() {
// Add GC root symbols.
if (!config->entry.empty())
enqueue(symtab->find(config->entry));
if (!ctx.arg.entry.empty())
enqueue(symtab->find(ctx.arg.entry));

// We need to preserve any no-strip or exported symbol
for (Symbol *sym : symtab->symbols())
Expand Down Expand Up @@ -166,7 +166,7 @@ void MarkLive::mark() {
}

void markLive() {
if (!config->gcSections)
if (!ctx.arg.gcSections)
return;

LLVM_DEBUG(dbgs() << "markLive\n");
Expand All @@ -175,7 +175,7 @@ void markLive() {
marker.run();

// Report garbage-collected sections.
if (config->printGcSections) {
if (ctx.arg.printGcSections) {
for (const ObjFile *obj : ctx.objectFiles) {
for (InputChunk *c : obj->functions)
if (!c->live)
Expand Down Expand Up @@ -207,7 +207,7 @@ void markLive() {

bool MarkLive::isCallCtorsLive() {
// In a reloctable link, we don't call `__wasm_call_ctors`.
if (config->relocatable)
if (ctx.arg.relocatable)
return false;

// In Emscripten-style PIC, we call `__wasm_call_ctors` which calls
Expand Down
Loading
Loading