[lld] [lld][WebAssembly] Fix stub library deps causing LTO archive members to be required post-LTO (PR #101894)

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 6 15:03:27 PDT 2024


https://github.com/sbc100 updated https://github.com/llvm/llvm-project/pull/101894

>From d7dfe2b0e70aff2ee4db9676281640056120ec4f Mon Sep 17 00:00:00 2001
From: Sam Clegg <sbc at chromium.org>
Date: Sun, 4 Aug 2024 07:17:55 -0700
Subject: [PATCH 1/3] [lld][WebAssembly] Fix stub library deps causing LTO
 archive members to be required post-LTO

Fixes: https://github.com/emscripten-core/emscripten/issues/16836
Fixes: https://github.com/emscripten-core/emscripten/issues/20275
---
 lld/test/wasm/lto/stub-library.s | 15 +++++++++------
 lld/wasm/Driver.cpp              | 11 +++++++++++
 lld/wasm/InputFiles.cpp          |  4 ++--
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/lld/test/wasm/lto/stub-library.s b/lld/test/wasm/lto/stub-library.s
index 20e2a62211413..a5c724012faec 100644
--- a/lld/test/wasm/lto/stub-library.s
+++ b/lld/test/wasm/lto/stub-library.s
@@ -1,12 +1,15 @@
 # RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
-# RUN: llvm-as %S/Inputs/foo.ll -o %t1.o
-# RUN: wasm-ld %t.o %t1.o %p/Inputs/stub.so -o %t.wasm
+# RUN: mkdir -p %t
+# RUN: llvm-as %S/Inputs/foo.ll -o %t/foo.o
+# RUN: rm -f %t/libfoo.a
+# RUN: llvm-ar rcs %t/libfoo.a %t/foo.o
+# RUN: wasm-ld %t.o %t/libfoo.a %p/Inputs/stub.so -o %t.wasm
 # RUN: obj2yaml %t.wasm | FileCheck %s
 
-# The function `bar` is declared in stub.so and depends on `foo`, which happens
-# be in an LTO object.
-# This verifies that stub library dependencies (required exports) can be defined
-# in LTO objects.
+## The function `bar` is declared in stub.so and depends on `foo`, which happens
+## be in an LTO object, in an archive file.
+## This verifies that stub library dependencies (required exports) can be defined
+## in LTO objects.
 .functype bar () -> ()
 
 .globl _start
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 8c83d17db02f5..44f35eb82aaa2 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -949,6 +949,17 @@ static void processStubLibrariesPreLTO() {
           auto* needed = symtab->find(dep);
           if (needed ) {
             needed->isUsedInRegularObj = true;
+            // Like with handleLibcall we have extract any LTO archive members
+            // that might need to be exported due to stub library symbol being
+            // used.  Without this the LTO object could be extracted during
+            // processStubLibraries, which is too late since LTO has already
+            // beeing performed at that point.
+            if (needed->isLazy() && isa<BitcodeFile>(needed->getFile())) {
+              if (!config->whyExtract.empty())
+                ctx.whyExtractRecords.emplace_back(toString(stub_file),
+                                                   needed->getFile(), *needed);
+              cast<LazySymbol>(needed)->extract();
+            }
           }
         }
       }
diff --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp
index f3f0ef9a99497..706ee25d5aae2 100644
--- a/lld/wasm/InputFiles.cpp
+++ b/lld/wasm/InputFiles.cpp
@@ -744,7 +744,7 @@ Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) {
   llvm_unreachable("unknown symbol kind");
 }
 
-StringRef strip(StringRef s) { return s.trim(' '); }
+static StringRef strip(StringRef s) { return s.trim(' '); }
 
 void StubFile::parse() {
   bool first = true;
@@ -761,7 +761,7 @@ void StubFile::parse() {
     }
 
     // Lines starting with # are considered comments
-    if (line.starts_with("#"))
+    if (line.starts_with("#") || !line.size())
       continue;
 
     StringRef sym;

>From 7c29cef8fae1c8774cf97765f921dd9cfbb0e347 Mon Sep 17 00:00:00 2001
From: Sam Clegg <sbc at chromium.org>
Date: Tue, 6 Aug 2024 14:56:43 -0700
Subject: [PATCH 2/3] feedback

---
 lld/test/wasm/lto/stub-library.s |  8 ++++----
 lld/wasm/Driver.cpp              | 10 +++++-----
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/lld/test/wasm/lto/stub-library.s b/lld/test/wasm/lto/stub-library.s
index a5c724012faec..aa892b62b714c 100644
--- a/lld/test/wasm/lto/stub-library.s
+++ b/lld/test/wasm/lto/stub-library.s
@@ -6,10 +6,10 @@
 # RUN: wasm-ld %t.o %t/libfoo.a %p/Inputs/stub.so -o %t.wasm
 # RUN: obj2yaml %t.wasm | FileCheck %s
 
-## The function `bar` is declared in stub.so and depends on `foo`, which happens
-## be in an LTO object, in an archive file.
-## This verifies that stub library dependencies (required exports) can be defined
-## in LTO objects.
+## The function `bar` is declared in stub.so and depends on `foo`, which in this
+## case is defined in an LTO object, inside an archive file.
+## This verifies that stub library dependencies (required exports) can be
+## defined in LTO objects.
 .functype bar () -> ()
 
 .globl _start
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 44f35eb82aaa2..5368fe79b7eb8 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -949,11 +949,11 @@ static void processStubLibrariesPreLTO() {
           auto* needed = symtab->find(dep);
           if (needed ) {
             needed->isUsedInRegularObj = true;
-            // Like with handleLibcall we have extract any LTO archive members
-            // that might need to be exported due to stub library symbol being
-            // used.  Without this the LTO object could be extracted during
-            // processStubLibraries, which is too late since LTO has already
-            // beeing performed at that point.
+            // Like with handleLibcall we have to extract any LTO archive
+            // members that might need to be exported due to stub library
+            // symbols being referenced.  Without this the LTO object could be
+            // extracted during processStubLibraries, which is too late since
+            // LTO has already being performed at that point.
             if (needed->isLazy() && isa<BitcodeFile>(needed->getFile())) {
               if (!config->whyExtract.empty())
                 ctx.whyExtractRecords.emplace_back(toString(stub_file),

>From 0a2c7317018b33c9784ce0ae70a658b42942a613 Mon Sep 17 00:00:00 2001
From: Sam Clegg <sbc at chromium.org>
Date: Tue, 6 Aug 2024 15:03:11 -0700
Subject: [PATCH 3/3] feedback

---
 lld/test/wasm/lto/stub-library.s | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/lld/test/wasm/lto/stub-library.s b/lld/test/wasm/lto/stub-library.s
index aa892b62b714c..36d6b7b3f38aa 100644
--- a/lld/test/wasm/lto/stub-library.s
+++ b/lld/test/wasm/lto/stub-library.s
@@ -1,15 +1,21 @@
+## The function `bar` is declared in stub.so and depends on `foo` which is
+## defined in an LTO object.  We also test the case where the LTO object is
+## with an archive file.
+## This verifies that stub library dependencies (which are required exports) can
+## be defined in LTO objects, even when they are within archive files.
+
 # RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
 # RUN: mkdir -p %t
 # RUN: llvm-as %S/Inputs/foo.ll -o %t/foo.o
+# RUN: wasm-ld %t.o %t/foo.o %p/Inputs/stub.so -o %t.wasm
+# RUN: obj2yaml %t.wasm | FileCheck %s
+
+## Run the same test but with foo.o inside of an archive file.
 # RUN: rm -f %t/libfoo.a
 # RUN: llvm-ar rcs %t/libfoo.a %t/foo.o
-# RUN: wasm-ld %t.o %t/libfoo.a %p/Inputs/stub.so -o %t.wasm
-# RUN: obj2yaml %t.wasm | FileCheck %s
+# RUN: wasm-ld %t.o %t/libfoo.a %p/Inputs/stub.so -o %t2.wasm
+# RUN: obj2yaml %t2.wasm | FileCheck %s
 
-## The function `bar` is declared in stub.so and depends on `foo`, which in this
-## case is defined in an LTO object, inside an archive file.
-## This verifies that stub library dependencies (required exports) can be
-## defined in LTO objects.
 .functype bar () -> ()
 
 .globl _start



More information about the llvm-commits mailing list