Skip to content

Commit 7263bc5

Browse files
committed
[DTLTO][LLD][ELF] Add support for Integrated Distributed ThinLTO
This patch introduces support for Integrated Distributed ThinLTO (DTLTO) in ELF LLD. DTLTO enables the distribution of ThinLTO backend compilations via external distribution systems, such as Incredibuild, during the traditional link step: https://llvm.org/docs/DTLTO.html. It is expected that users will invoke DTLTO through the compiler driver (e.g., Clang) rather than calling LLD directly. A Clang-side interface for DTLTO will be added in a follow-up patch. Note: Bitcode members of non-thin archives are not currently supported. This will be addressed in a future change. Testing: - ELF LLD `lit` test coverage has been added, using a mock distributor to avoid requiring Clang. - Cross-project `lit` tests cover integration with Clang. For the design discussion of the DTLTO feature, see: llvm#126654
1 parent 389e9d3 commit 7263bc5

21 files changed

+590
-8
lines changed

cross-project-tests/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ set(CROSS_PROJECT_TEST_DEPS
1919
FileCheck
2020
check-gdb-llvm-support
2121
count
22-
llvm-dwarfdump
22+
llvm-ar
2323
llvm-config
24+
llvm-dwarfdump
2425
llvm-objdump
25-
split-file
2626
not
27+
split-file
2728
)
2829

2930
if ("clang" IN_LIST LLVM_ENABLE_PROJECTS)
@@ -94,6 +95,13 @@ add_lit_testsuite(check-cross-amdgpu "Running AMDGPU cross-project tests"
9495
DEPENDS clang
9596
)
9697

98+
# DTLTO tests.
99+
add_lit_testsuite(check-cross-dtlto "Running DTLTO cross-project tests"
100+
${CMAKE_CURRENT_BINARY_DIR}/dtlto
101+
EXCLUDE_FROM_CHECK_ALL
102+
DEPENDS ${CROSS_PROJECT_TEST_DEPS}
103+
)
104+
97105
# Add check-cross-project-* targets.
98106
add_lit_testsuites(CROSS_PROJECT ${CMAKE_CURRENT_SOURCE_DIR}
99107
DEPENDS ${CROSS_PROJECT_TEST_DEPS}

cross-project-tests/dtlto/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Tests for DTLTO (Integrated Distributed ThinLTO) functionality.
2+
3+
These are integration tests as DTLTO invokes `clang` for code-generation.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# REQUIRES: x86-registered-target,ld.lld,llvm-ar
2+
3+
# Test that a DTLTO link succeeds and outputs the expected set of files
4+
# correctly when thin archives are present.
5+
6+
RUN: rm -rf %t && split-file %s %t && cd %t
7+
RUN: %clang --target=x86_64-linux-gnu -flto=thin -c \
8+
RUN: foo.c bar.c dog.c cat.c _start.c
9+
10+
RUN: llvm-ar rcs foo.a foo.o --thin
11+
# Create this bitcode thin archive in a subdirectory to test the expansion of
12+
# the path to a bitcode file that is referenced using "..", e.g., in this case
13+
# "../bar.o".
14+
RUN: mkdir lib
15+
RUN: llvm-ar rcs lib/bar.a bar.o --thin
16+
# Create this bitcode thin archive with an absolute path entry containing "..".
17+
RUN: llvm-ar rcs dog.a %t/lib/../dog.o --thin
18+
RUN: llvm-ar rcs cat.a cat.o --thin
19+
RUN: llvm-ar rcs _start.a _start.o --thin
20+
21+
RUN: mkdir %t/out && cd %t/out
22+
23+
RUN: ld.lld %t/foo.a %t/lib/bar.a ../_start.a %t/cat.a \
24+
RUN: --whole-archive ../dog.a \
25+
RUN: --thinlto-distributor=%python \
26+
RUN: --thinlto-distributor-arg=%llvm_src_root/utils/dtlto/local.py \
27+
RUN: --thinlto-remote-compiler=%clang \
28+
RUN: --save-temps
29+
30+
# Check that the required output files have been created.
31+
RUN: ls | FileCheck %s --check-prefix=OUTPUTS \
32+
RUN: --implicit-check-not=cat --implicit-check-not=foo
33+
34+
# JSON jobs description.
35+
OUTPUTS-DAG: a.[[PID:[a-zA-Z0-9_]+]].dist-file.json
36+
37+
# Individual summary index files.
38+
OUTPUTS-DAG: dog.1.[[PID]].native.o.thinlto.bc{{$}}
39+
OUTPUTS-DAG: _start.2.[[PID]].native.o.thinlto.bc{{$}}
40+
OUTPUTS-DAG: bar.3.[[PID]].native.o.thinlto.bc{{$}}
41+
42+
# Native output object files.
43+
OUTPUTS-DAG: dog.1.[[PID]].native.o{{$}}
44+
OUTPUTS-DAG: _start.2.[[PID]].native.o{{$}}
45+
OUTPUTS-DAG: bar.3.[[PID]].native.o{{$}}
46+
47+
#--- foo.c
48+
__attribute__((retain)) void foo() {}
49+
50+
#--- bar.c
51+
extern void foo();
52+
__attribute__((retain)) void bar() { foo(); }
53+
54+
#--- dog.c
55+
__attribute__((retain)) void dog() {}
56+
57+
#--- cat.c
58+
__attribute__((retain)) void cat() {}
59+
60+
#--- _start.c
61+
extern void bar();
62+
__attribute__((retain)) void _start() {
63+
bar();
64+
}
65+

cross-project-tests/dtlto/dtlto.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// REQUIRES: x86-registered-target,ld.lld
2+
3+
/// Simple test that DTLTO works with a single input bitcode file and that
4+
/// --save-temps can be applied to the remote compilation.
5+
// RUN: rm -rf %t && mkdir %t && cd %t
6+
7+
// RUN: %clang --target=x86_64-linux-gnu -c -flto=thin %s
8+
9+
// RUN: ld.lld dtlto.o \
10+
// RUN: --thinlto-distributor=%python \
11+
// RUN: --thinlto-distributor-arg=%llvm_src_root/utils/dtlto/local.py \
12+
// RUN: --thinlto-remote-compiler=%clang \
13+
// RUN: --thinlto-remote-compiler-arg=--save-temps
14+
15+
/// Check that the required output files have been created.
16+
// RUN: ls | count 10
17+
// RUN: ls | FileCheck %s
18+
19+
/// Produced by the bitcode compilation.
20+
// CHECK-DAG: {{^}}dtlto.o{{$}}
21+
22+
/// Linked ELF.
23+
// CHECK-DAG: {{^}}a.out{{$}}
24+
25+
/// --save-temps output for the backend compilation.
26+
// CHECK-DAG: {{^}}dtlto.s{{$}}
27+
// CHECK-DAG: {{^}}dtlto.s.0.preopt.bc{{$}}
28+
// CHECK-DAG: {{^}}dtlto.s.1.promote.bc{{$}}
29+
// CHECK-DAG: {{^}}dtlto.s.2.internalize.bc{{$}}
30+
// CHECK-DAG: {{^}}dtlto.s.3.import.bc{{$}}
31+
// CHECK-DAG: {{^}}dtlto.s.4.opt.bc{{$}}
32+
// CHECK-DAG: {{^}}dtlto.s.5.precodegen.bc{{$}}
33+
// CHECK-DAG: {{^}}dtlto.s.resolution.txt{{$}}
34+
35+
int _start() { return 0; }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if "clang" not in config.available_features:
2+
config.unsupported = True

cross-project-tests/lit.cfg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
2020

2121
# suffixes: A list of file extensions to treat as test files.
22-
config.suffixes = [".c", ".cl", ".cpp", ".m"]
22+
config.suffixes = [".c", ".cl", ".cpp", ".m", ".test"]
2323

2424
# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
2525
# subdirectories contain auxiliary inputs for various tests in their parent
@@ -107,6 +107,8 @@ def get_required_attr(config, attr_name):
107107
if lldb_path is not None:
108108
config.available_features.add("lldb")
109109

110+
if llvm_config.use_llvm_tool("llvm-ar"):
111+
config.available_features.add("llvm-ar")
110112

111113
def configure_dexter_substitutions():
112114
"""Configure substitutions for host platform and return list of dependencies"""

lld/ELF/Config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ struct Config {
249249
llvm::SmallVector<llvm::StringRef, 0> searchPaths;
250250
llvm::SmallVector<llvm::StringRef, 0> symbolOrderingFile;
251251
llvm::SmallVector<llvm::StringRef, 0> thinLTOModulesToCompile;
252+
llvm::StringRef dtltoDistributor;
253+
llvm::SmallVector<llvm::StringRef, 0> dtltoDistributorArgs;
254+
llvm::StringRef dtltoCompiler;
255+
llvm::SmallVector<llvm::StringRef, 0> dtltoCompilerArgs;
252256
llvm::SmallVector<llvm::StringRef, 0> undefined;
253257
llvm::SmallVector<SymbolVersion, 0> dynamicList;
254258
llvm::SmallVector<uint8_t, 0> buildIdVector;

lld/ELF/Driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,11 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
13411341
args.hasFlag(OPT_dependent_libraries, OPT_no_dependent_libraries, true);
13421342
ctx.arg.disableVerify = args.hasArg(OPT_disable_verify);
13431343
ctx.arg.discard = getDiscard(args);
1344+
ctx.arg.dtltoDistributor = args.getLastArgValue(OPT_thinlto_distributor_eq);
1345+
ctx.arg.dtltoDistributorArgs =
1346+
args::getStrings(args, OPT_thinlto_distributor_arg);
1347+
ctx.arg.dtltoCompiler = args.getLastArgValue(OPT_thinlto_compiler_eq);
1348+
ctx.arg.dtltoCompilerArgs = args::getStrings(args, OPT_thinlto_compiler_arg);
13441349
ctx.arg.dwoDir = args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq);
13451350
ctx.arg.dynamicLinker = getDynamicLinker(ctx, args);
13461351
ctx.arg.ehFrameHdr =

lld/ELF/InputFiles.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/CachedHashString.h"
2121
#include "llvm/ADT/STLExtras.h"
2222
#include "llvm/LTO/LTO.h"
23+
#include "llvm/Object/Archive.h"
2324
#include "llvm/Object/IRObjectFile.h"
2425
#include "llvm/Support/ARMAttributeParser.h"
2526
#include "llvm/Support/ARMBuildAttributes.h"
@@ -1739,6 +1740,33 @@ static uint8_t getOsAbi(const Triple &t) {
17391740
}
17401741
}
17411742

1743+
// For DTLTO, bitcode member names must be valid paths to files on disk.
1744+
// For thin archives, resolve `memberPath` relative to the archive's location.
1745+
// Returns true if adjusted; false otherwise. Non-thin archives are unsupported.
1746+
static bool dtltoAdjustMemberPathIfThinArchive(Ctx &ctx, StringRef archivePath,
1747+
std::string &memberPath) {
1748+
assert(!archivePath.empty() && !ctx.arg.dtltoDistributor.empty());
1749+
1750+
// Check if the archive file is a thin archive by reading its header.
1751+
auto bufferOrErr =
1752+
MemoryBuffer::getFileSlice(archivePath, sizeof(ThinArchiveMagic) - 1, 0);
1753+
if (std::error_code ec = bufferOrErr.getError()) {
1754+
ErrAlways(ctx) << "cannot open " << archivePath << ": " << ec.message();
1755+
return false;
1756+
}
1757+
1758+
if (!bufferOrErr->get()->getBuffer().starts_with(ThinArchiveMagic))
1759+
return false;
1760+
1761+
SmallString<64> resolvedPath;
1762+
if (path::is_relative(memberPath)) {
1763+
resolvedPath = path::parent_path(archivePath);
1764+
path::append(resolvedPath, memberPath);
1765+
memberPath = resolvedPath.str();
1766+
}
1767+
return true;
1768+
}
1769+
17421770
BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
17431771
uint64_t offsetInArchive, bool lazy)
17441772
: InputFile(ctx, BitcodeKind, mb) {
@@ -1756,10 +1784,13 @@ BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
17561784
// symbols later in the link stage). So we append file offset to make
17571785
// filename unique.
17581786
StringSaver &ss = ctx.saver;
1759-
StringRef name = archiveName.empty()
1760-
? ss.save(path)
1761-
: ss.save(archiveName + "(" + path::filename(path) +
1762-
" at " + utostr(offsetInArchive) + ")");
1787+
StringRef name =
1788+
(archiveName.empty() ||
1789+
(!ctx.arg.dtltoDistributor.empty() &&
1790+
dtltoAdjustMemberPathIfThinArchive(ctx, archiveName, path)))
1791+
? ss.save(path)
1792+
: ss.save(archiveName + "(" + path::filename(path) + " at " +
1793+
utostr(offsetInArchive) + ")");
17631794
MemoryBufferRef mbref(mb.getBuffer(), name);
17641795

17651796
obj = CHECK2(lto::InputFile::create(mbref), this);

lld/ELF/LTO.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
180180
std::string(ctx.arg.thinLTOPrefixReplaceNew),
181181
std::string(ctx.arg.thinLTOPrefixReplaceNativeObject),
182182
ctx.arg.thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
183+
} else if (!ctx.arg.dtltoDistributor.empty()) {
184+
backend = lto::createOutOfProcessThinBackend(
185+
llvm::hardware_concurrency(ctx.arg.thinLTOJobs), onIndexWrite,
186+
ctx.arg.thinLTOEmitIndexFiles, ctx.arg.thinLTOEmitImportsFiles,
187+
ctx.arg.outputFile, ctx.arg.dtltoDistributor,
188+
ctx.arg.dtltoDistributorArgs, ctx.arg.dtltoCompiler,
189+
ctx.arg.dtltoCompilerArgs, !ctx.arg.saveTempsArgs.empty());
183190
} else {
184191
backend = lto::createInProcessThinBackend(
185192
llvm::heavyweight_hardware_concurrency(ctx.arg.thinLTOJobs),

0 commit comments

Comments
 (0)