Skip to content

Commit 9244825

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 archives (both thin and non-thin) 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 9244825

File tree

18 files changed

+401
-1
lines changed

18 files changed

+401
-1
lines changed

cross-project-tests/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ add_lit_testsuite(check-cross-amdgpu "Running AMDGPU cross-project tests"
9494
DEPENDS clang
9595
)
9696

97+
# DTLTO tests.
98+
add_lit_testsuite(check-cross-dtlto "Running DTLTO cross-project tests"
99+
${CMAKE_CURRENT_BINARY_DIR}/dtlto
100+
EXCLUDE_FROM_CHECK_ALL
101+
DEPENDS ${CROSS_PROJECT_TEST_DEPS}
102+
)
103+
97104
# Add check-cross-project-* targets.
98105
add_lit_testsuites(CROSS_PROJECT ${CMAKE_CURRENT_SOURCE_DIR}
99106
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.

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

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: 1 addition & 0 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"

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),

lld/ELF/Options.td

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,17 @@ def thinlto_object_suffix_replace_eq: JJ<"thinlto-object-suffix-replace=">;
710710
def thinlto_prefix_replace_eq: JJ<"thinlto-prefix-replace=">;
711711
def thinlto_single_module_eq: JJ<"thinlto-single-module=">,
712712
HelpText<"Specify a single module to compile in ThinLTO mode, for debugging only">;
713-
713+
def thinlto_distributor_eq: JJ<"thinlto-distributor=">,
714+
HelpText<"Distributor to use for ThinLTO backend compilations. If specified, "
715+
"ThinLTO backend compilations will be distributed.">;
716+
defm thinlto_distributor_arg: EEq<"thinlto-distributor-arg", "Arguments to "
717+
"pass to the ThinLTO distributor">;
718+
def thinlto_compiler_eq: JJ<"thinlto-remote-compiler=">,
719+
HelpText<"Compiler for the ThinLTO distributor to invoke for ThinLTO backend "
720+
"compilations">;
721+
defm thinlto_compiler_arg: EEq<"thinlto-remote-compiler-arg", "Compiler "
722+
"arguments for the ThinLTO distributor to pass for ThinLTO backend "
723+
"compilations">;
714724
defm fat_lto_objects: BB<"fat-lto-objects",
715725
"Use the .llvm.lto section, which contains LLVM bitcode, in fat LTO object files to perform LTO.",
716726
"Ignore the .llvm.lto section in relocatable object files (default).">;

lld/docs/DTLTO.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Integrated Distributed ThinLTO (DTLTO)
2+
======================================
3+
4+
Integrated Distributed ThinLTO (DTLTO) enables the distribution of backend
5+
ThinLTO compilations via external distribution systems, such as Incredibuild,
6+
during the traditional link step.
7+
8+
The implementation is documented here: https://llvm.org/docs/DTLTO.html.
9+
10+
Currently, DTLTO is only supported in ELF LLD. Support will be added to other
11+
LLD flavours in the future.
12+
13+
ELF LLD
14+
-------
15+
16+
The command-line interface is as follows:
17+
18+
- ``--thinlto-distributor=<path>``
19+
Specifies the file to execute as the distributor process. If specified,
20+
ThinLTO backend compilations will be distributed.
21+
22+
- ``--thinlto-remote-compiler=<path>``
23+
Specifies the path to the compiler that the distributor process will use for
24+
backend compilations. The compiler invoked must match the version of LLD.
25+
26+
- ``--thinlto-distributor-arg=<arg>``
27+
Specifies ``<arg>`` on the command line when invoking the distributor.
28+
Can be specified multiple times.
29+
30+
- ``--thinlto-remote-compiler-arg=<arg>``
31+
Appends ``<arg>`` to the remote compiler's command line.
32+
Can be specified multiple times.
33+
34+
Options that introduce extra input/output files may cause miscompilation if
35+
the distribution system does not automatically handle pushing/fetching them to
36+
remote nodes. In such cases, configure the distributor - possibly using
37+
``--thinlto-distributor-arg=`` - to manage these dependencies. See the
38+
distributor documentation for details.
39+
40+
Some LLD LTO options (e.g., ``--lto-sample-profile=<file>``) are supported.
41+
Currently, other options are silently accepted but do not have the intended
42+
effect. Support for such options will be expanded in the future.

0 commit comments

Comments
 (0)