Skip to content

Commit b894824

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 b894824

File tree

19 files changed

+405
-4
lines changed

19 files changed

+405
-4
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.

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

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).">;

0 commit comments

Comments
 (0)