[llvm-branch-commits] [llvm] release/22.x: [DTLTO] Fix handling of multi-module bitcode inputs (#174624) (PR #176066)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jan 14 16:28:50 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lto

Author: None (llvmbot)

<details>
<summary>Changes</summary>

Backport 663647f1b2befbb4eda52369438febf42e528921

Requested by: @<!-- -->bd1976bris

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


4 Files Affected:

- (added) cross-project-tests/dtlto/multimodule.test (+42) 
- (modified) llvm/include/llvm/LTO/LTO.h (+2) 
- (modified) llvm/lib/DTLTO/DTLTO.cpp (+1-1) 
- (modified) llvm/lib/LTO/LTO.cpp (+3-1) 


``````````diff
diff --git a/cross-project-tests/dtlto/multimodule.test b/cross-project-tests/dtlto/multimodule.test
new file mode 100644
index 0000000000000..b371a26ace399
--- /dev/null
+++ b/cross-project-tests/dtlto/multimodule.test
@@ -0,0 +1,42 @@
+REQUIRES: x86-registered-target,ld.lld,llvm-ar
+
+# Test that a DTLTO link succeeds with a multi-module (via -fsplit-lto-unit)
+# bitcode file. We use an archive, as archive member inputs exercise more of
+# the DTLTO specific code than other input file types.
+
+RUN: rm -rf %t && split-file %s %t && cd %t
+
+RUN: %clang -O2 --target=x86_64-linux-gnu -flto=thin -c usebar.cc \
+RUN:   -fno-rtti -fno-exceptions -fsplit-lto-unit 
+
+# Sanity check that multi-module bitcode was produced.
+RUN: not llvm-modextract -n 2 usebar.o -o - 2>&1 \
+RUN:   | FileCheck %s --check-prefix=TWO
+TWO: bitcode file contains 2 module(s)
+
+# Create an archive.
+RUN: llvm-ar rcs usebar.a usebar.o
+
+# Build with DTLTO.
+RUN: %clang -O2 --target=x86_64-linux-gnu -flto=thin -fuse-ld=lld \
+RUN:   -nostdlib -shared -Wl,--whole-archive,--allow-shlib-undefined usebar.a \
+RUN:   -fthinlto-distributor=%python \
+RUN:   -Xthinlto-distributor=%llvm_src_root/utils/dtlto/local.py \
+RUN:   -Wl,--save-temps
+
+RUN: ls | sort | FileCheck %s
+
+# DTLTO JSON file - confirms DTLTO occurred.
+CHECK: .dist-file.json
+
+# .native.o exists - confirms archive member usebar.o participated in DTLTO.
+CHECK: {{^}}usebar.a(usebar.o
+CHECK-SAME: .native.o
+
+#--- usebar.cc
+// Minimal C++ input to exercise multi-module emission with -fsplit-lto-unit.
+struct A { virtual int foo(); };
+int bar(A *a);
+
+struct B : A { int foo() { return 2; } };
+int use() { static B b; return bar(&b); }
diff --git a/llvm/include/llvm/LTO/LTO.h b/llvm/include/llvm/LTO/LTO.h
index 819be1909ec12..ea80a25ac4ed0 100644
--- a/llvm/include/llvm/LTO/LTO.h
+++ b/llvm/include/llvm/LTO/LTO.h
@@ -194,6 +194,8 @@ class InputFile {
 
   // Returns the only BitcodeModule from InputFile.
   LLVM_ABI BitcodeModule &getSingleBitcodeModule();
+  // Returns the primary BitcodeModule from InputFile.
+  LLVM_ABI BitcodeModule &getPrimaryBitcodeModule();
   // Returns the memory buffer reference for this input file.
   MemoryBufferRef getFileBuffer() const { return MbRef; }
   // Returns true if this input file is a member of an archive.
diff --git a/llvm/lib/DTLTO/DTLTO.cpp b/llvm/lib/DTLTO/DTLTO.cpp
index 7ba4bfd80b6ab..d41544277dd45 100644
--- a/llvm/lib/DTLTO/DTLTO.cpp
+++ b/llvm/lib/DTLTO/DTLTO.cpp
@@ -146,7 +146,7 @@ lto::DTLTO::addInput(std::unique_ptr<lto::InputFile> InputPtr) {
     return Input;
 
   SmallString<64> NewModuleId;
-  BitcodeModule &BM = Input->getSingleBitcodeModule();
+  BitcodeModule &BM = Input->getPrimaryBitcodeModule();
 
   // Check if the archive is a thin archive.
   Expected<bool> IsThin = isThinArchive(ArchivePath);
diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp
index ff6762ebb59be..4d69ffeda1280 100644
--- a/llvm/lib/LTO/LTO.cpp
+++ b/llvm/lib/LTO/LTO.cpp
@@ -606,6 +606,8 @@ BitcodeModule &InputFile::getSingleBitcodeModule() {
   return Mods[0];
 }
 
+BitcodeModule &InputFile::getPrimaryBitcodeModule() { return Mods[0]; }
+
 LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
                                       const Config &Conf)
     : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
@@ -804,7 +806,7 @@ LTO::addModule(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
   // If any of the modules inside of a input bitcode file was compiled with
   // ThinLTO, we assume that the whole input file also was compiled with
   // ThinLTO.
-  Input.IsThinLTO = IsThinLTO;
+  Input.IsThinLTO |= IsThinLTO;
 
   auto ModSyms = Input.module_symbols(ModI);
   addModuleToGlobalRes(ModSyms, Res,

``````````

</details>


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


More information about the llvm-branch-commits mailing list