From 8307d45cc855734650d9fff6778461687a40342b Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 7 May 2025 11:17:19 -0700 Subject: [PATCH] [lld][WebAssembly] Fix reported names of LTO output files (#138789) This change was made in the ELF linker in #78835 but somehow never made it over to the wasm port. --- lld/test/wasm/lto/signature-mismatch.ll | 2 +- lld/wasm/LTO.cpp | 25 +++++++++++++++---------- lld/wasm/LTO.h | 3 ++- lld/wasm/SymbolTable.cpp | 4 ++-- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lld/test/wasm/lto/signature-mismatch.ll b/lld/test/wasm/lto/signature-mismatch.ll index cf1a998826fc..6580c8cf71b3 100644 --- a/lld/test/wasm/lto/signature-mismatch.ll +++ b/lld/test/wasm/lto/signature-mismatch.ll @@ -17,4 +17,4 @@ define void @_start() { ; CHECK: error: function signature mismatch: f ; CHECK: >>> defined as (i32) -> void in {{.*}}signature-mismatch.ll.tmp1.o -; CHECK: >>> defined as () -> void in lto.tmp +; CHECK: >>> defined as () -> void in {{.*}}signature-mismatch.ll.tmp.wasm.lto.o diff --git a/lld/wasm/LTO.cpp b/lld/wasm/LTO.cpp index ab63281012ea..a877f067603e 100644 --- a/lld/wasm/LTO.cpp +++ b/lld/wasm/LTO.cpp @@ -183,10 +183,11 @@ static void thinLTOCreateEmptyIndexFiles() { // Merge all the bitcode files we have seen, codegen the result // and return the resulting objects. -std::vector BitcodeCompiler::compile() { +SmallVector BitcodeCompiler::compile() { unsigned maxTasks = ltoObj->getMaxTasks(); buf.resize(maxTasks); files.resize(maxTasks); + filenames.resize(maxTasks); // The --thinlto-cache-dir option specifies the path to a directory in which // to cache native object files for ThinLTO incremental builds. If a path was @@ -233,15 +234,21 @@ std::vector BitcodeCompiler::compile() { if (!ctx.arg.thinLTOCacheDir.empty()) pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files); - std::vector ret; + SmallVector ret; for (unsigned i = 0; i != maxTasks; ++i) { StringRef objBuf = buf[i].second; StringRef bitcodeFilePath = buf[i].first; + if (files[i]) { + // When files[i] is not null, we get the native relocatable file from the + // cache. filenames[i] contains the original BitcodeFile's identifier. + objBuf = files[i]->getBuffer(); + bitcodeFilePath = filenames[i]; + } else { + objBuf = buf[i].second; + bitcodeFilePath = buf[i].first; + } if (objBuf.empty()) continue; - ret.emplace_back(objBuf.data(), objBuf.size()); - if (!ctx.arg.saveTemps) - continue; // If the input bitcode file is path/to/x.o and -o specifies a.out, the // corresponding native relocatable file path will look like: @@ -266,7 +273,9 @@ std::vector BitcodeCompiler::compile() { sys::path::remove_dots(path, true); ltoObjName = saver().save(path.str()); } - saveBuffer(objBuf, ltoObjName); + if (ctx.arg.saveTemps) + saveBuffer(objBuf, ltoObjName); + ret.emplace_back(createObjectFile(MemoryBufferRef(objBuf, ltoObjName))); } if (!ctx.arg.ltoObjPath.empty()) { @@ -275,10 +284,6 @@ std::vector BitcodeCompiler::compile() { saveBuffer(buf[i].second, ctx.arg.ltoObjPath + Twine(i)); } - for (std::unique_ptr &file : files) - if (file) - ret.push_back(file->getBuffer()); - return ret; } diff --git a/lld/wasm/LTO.h b/lld/wasm/LTO.h index 43c7672fb563..21b1d5902466 100644 --- a/lld/wasm/LTO.h +++ b/lld/wasm/LTO.h @@ -45,13 +45,14 @@ public: ~BitcodeCompiler(); void add(BitcodeFile &f); - std::vector compile(); + SmallVector compile(); private: std::unique_ptr ltoObj; // An array of (module name, native relocatable file content) pairs. SmallVector>, 0> buf; std::vector> files; + SmallVector filenames; std::unique_ptr indexFile; llvm::DenseSet thinIndices; }; diff --git a/lld/wasm/SymbolTable.cpp b/lld/wasm/SymbolTable.cpp index bbe48b03f77e..91677b34ea2c 100644 --- a/lld/wasm/SymbolTable.cpp +++ b/lld/wasm/SymbolTable.cpp @@ -87,8 +87,8 @@ void SymbolTable::compileBitcodeFiles() { for (BitcodeFile *f : ctx.bitcodeFiles) lto->add(*f); - for (StringRef filename : lto->compile()) { - auto *obj = make(MemoryBufferRef(filename, "lto.tmp"), ""); + for (auto &file : lto->compile()) { + auto *obj = cast(file); obj->parse(true); ctx.objectFiles.push_back(obj); }