Skip to content

[lld][WebAssembly] Fix reported names of LTO output files #138789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2025

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented May 7, 2025

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

@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-lld

Author: Sam Clegg (sbc100)

Changes

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 --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);
   }

@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-lld-wasm

Author: Sam Clegg (sbc100)

Changes

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 --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);
   }

@sbc100 sbc100 force-pushed the fix_lto_filenames branch from 0099797 to ee44532 Compare May 7, 2025 00:51
@sbc100 sbc100 changed the title [lld][WebAssembly] Fix reported names of LTO output file [lld][WebAssembly] Fix reported names of LTO output files May 7, 2025
@sbc100 sbc100 force-pushed the fix_lto_filenames branch from ee44532 to 97a3ca2 Compare May 7, 2025 01:18
@sbc100 sbc100 requested a review from dschuff May 7, 2025 01:18
This change was made in the ELF linker in llvm#78835 but somehow never
made it over the wasm port.
@sbc100 sbc100 force-pushed the fix_lto_filenames branch from 97a3ca2 to 8b28a48 Compare May 7, 2025 01:20
Copy link

github-actions bot commented May 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@sbc100 sbc100 merged commit 8307d45 into llvm:main May 7, 2025
11 checks passed
@sbc100 sbc100 deleted the fix_lto_filenames branch May 7, 2025 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants