[lld] [lld][WebAssembly] Fix reported names of LTO output file (PR #138789)

via llvm-commits llvm-commits at lists.llvm.org
Tue May 6 17:47:48 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld

Author: Sam Clegg (sbc100)

<details>
<summary>Changes</summary>

This change was made in the ELF linker in #<!-- -->78835 but somehow never made it over the wasm port.

---
Full diff: https://github.com/llvm/llvm-project/pull/138789.diff


4 Files Affected:

- (modified) lld/test/wasm/lto/signature-mismatch.ll (+1-1) 
- (modified) lld/wasm/LTO.cpp (+16-10) 
- (modified) lld/wasm/LTO.h (+2-1) 
- (modified) lld/wasm/SymbolTable.cpp (+2-2) 


``````````diff
diff --git a/lld/test/wasm/lto/signature-mismatch.ll b/lld/test/wasm/lto/signature-mismatch.ll
index cf1a998826fc0..6580c8cf71b33 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 ab63281012eae..a86ec9f878567 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<StringRef> BitcodeCompiler::compile() {
+std::vector<MemoryBufferRef> 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<StringRef> BitcodeCompiler::compile() {
   if (!ctx.arg.thinLTOCacheDir.empty())
     pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files);
 
-  std::vector<StringRef> ret;
+  std::vector<MemoryBufferRef> 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,10 @@ std::vector<StringRef> BitcodeCompiler::compile() {
       sys::path::remove_dots(path, true);
       ltoObjName = saver().save(path.str());
     }
-    saveBuffer(objBuf, ltoObjName);
+    if (ctx.arg.saveTemps)
+      saveBuffer(objBuf, ltoObjName);
+    llvm::dbgs() << "YYY: " << ltoObjName << "\n";
+    ret.emplace_back(MemoryBufferRef(objBuf, ltoObjName));
   }
 
   if (!ctx.arg.ltoObjPath.empty()) {
@@ -275,10 +285,6 @@ std::vector<StringRef> BitcodeCompiler::compile() {
       saveBuffer(buf[i].second, ctx.arg.ltoObjPath + Twine(i));
   }
 
-  for (std::unique_ptr<MemoryBuffer> &file : files)
-    if (file)
-      ret.push_back(file->getBuffer());
-
   return ret;
 }
 
diff --git a/lld/wasm/LTO.h b/lld/wasm/LTO.h
index 43c7672fb5639..cbd10d59e9f3c 100644
--- a/lld/wasm/LTO.h
+++ b/lld/wasm/LTO.h
@@ -45,13 +45,14 @@ class BitcodeCompiler {
   ~BitcodeCompiler();
 
   void add(BitcodeFile &f);
-  std::vector<StringRef> compile();
+  std::vector<MemoryBufferRef> compile();
 
 private:
   std::unique_ptr<llvm::lto::LTO> ltoObj;
   // An array of (module name, native relocatable file content) pairs.
   SmallVector<std::pair<std::string, SmallString<0>>, 0> buf;
   std::vector<std::unique_ptr<MemoryBuffer>> files;
+  SmallVector<std::string, 0> filenames;
   std::unique_ptr<llvm::raw_fd_ostream> indexFile;
   llvm::DenseSet<StringRef> thinIndices;
 };
diff --git a/lld/wasm/SymbolTable.cpp b/lld/wasm/SymbolTable.cpp
index bbe48b03f77e5..4a56b50211c4d 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<ObjFile>(MemoryBufferRef(filename, "lto.tmp"), "");
+  for (auto membuf : lto->compile()) {
+    auto *obj = make<ObjFile>(membuf, "");
     obj->parse(true);
     ctx.objectFiles.push_back(obj);
   }

``````````

</details>


https://github.com/llvm/llvm-project/pull/138789


More information about the llvm-commits mailing list