Skip to content

Commit 6520b21

Browse files
authored
[DTLTO][LLVM] Integrated Distributed ThinLTO (DTLTO) (#127749)
This patch adds initial support for Integrated Distributed ThinLTO (DTLTO) in LLVM, which manages distribution internally during the traditional link step. This enables compatibility with any build system that supports in-process ThinLTO. In contrast, existing approaches to distributed ThinLTO, which split the thin-link (--thinlto-index-only), backend compilation, and final link into separate steps, require build system support, e.g. Bazel. This patch implements the core DTLTO mechanism, which enables delegation of ThinLTO backend jobs to an external process (the distributor). The distributor can then manage job distribution through systems like Incredibuild. A generic JSON interface is used to communicate with the distributor, allowing for the creation of new distributors (and thus integration with different distribution systems) without modifying LLVM. Please see llvm/docs/dtlto.rst for more details. RFC: https://discourse.llvm.org/t/rfc-integrated-distributed-thinlto/69641 Design Review: #126654
1 parent 9f5a670 commit 6520b21

File tree

17 files changed

+1147
-21
lines changed

17 files changed

+1147
-21
lines changed

llvm/docs/DTLTO.rst

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
===================
2+
DTLTO
3+
===================
4+
.. contents::
5+
:local:
6+
:depth: 2
7+
8+
.. toctree::
9+
:maxdepth: 1
10+
11+
Distributed ThinLTO (DTLTO)
12+
===========================
13+
14+
Distributed ThinLTO (DTLTO) enables the distribution of backend ThinLTO
15+
compilations via external distribution systems, such as Incredibuild, during the
16+
link step.
17+
18+
DTLTO extends the existing ThinLTO distribution support which uses separate
19+
*thin-link*, *backend compilation*, and *link* steps. This method is documented
20+
here:
21+
22+
https://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html
23+
24+
Using the *separate thin-link* approach requires a build system capable of
25+
handling the dynamic dependencies specified in the individual summary index
26+
files, such as Bazel. DTLTO removes this requirement, allowing it to be used
27+
with any build process that supports in-process ThinLTO.
28+
29+
The following commands show the steps used for the *separate thin-link*
30+
approach for a basic example:
31+
32+
.. code-block:: console
33+
34+
1. clang -flto=thin -O2 t1.c t2.c -c
35+
2. clang -flto=thin -O2 t1.o t2.o -fuse-ld=lld -Wl,--thinlto-index-only
36+
3. clang -O2 -o t1.native.o t1.o -c -fthinlto-index=t1.o.thinlto.bc
37+
4. clang -O2 -o t2.native.o t2.o -c -fthinlto-index=t2.o.thinlto.bc
38+
5. clang t1.native.o t2.native.o -o a.out -fuse-ld=lld
39+
40+
With DTLTO, steps 2-5 are performed internally as part of the link step. The
41+
equivalent DTLTO commands for the above are:
42+
43+
.. code-block:: console
44+
45+
clang -flto=thin -O2 t1.c t2.c -c
46+
clang -flto=thin -O2 t1.o t2.o -fuse-ld=lld -fthinlto-distributor=<distributor_process>
47+
48+
For DTLTO, LLD prepares the following for each ThinLTO backend compilation job:
49+
50+
- An individual index file and a list of input and output files (corresponds to
51+
step 2 above).
52+
- A Clang command line to perform the ThinLTO backend compilations.
53+
54+
This information is supplied, via a JSON file, to ``distributor_process``, which
55+
executes the backend compilations using a distribution system (corresponds to
56+
steps 3 and 4 above). Upon completion, LLD integrates the compiled native object
57+
files into the link process and completes the link (corresponds to step 5
58+
above).
59+
60+
This design keeps the details of distribution systems out of the LLVM source
61+
code.
62+
63+
An example distributor that performs all work on the local system is included in
64+
the LLVM source tree. To run an example with that distributor, a command line
65+
such as the following can be used:
66+
67+
.. code-block:: console
68+
69+
clang -flto=thin -fuse-ld=lld -O2 t1.o t2.o -fthinlto-distributor=$(which python3) \
70+
-Xthinlto-distributor=$LLVMSRC/llvm/utils/dtlto/local.py
71+
72+
Distributors
73+
------------
74+
75+
Distributors are programs responsible for:
76+
77+
1. Consuming the JSON backend compilations job description file.
78+
2. Translating job descriptions into requests for the distribution system.
79+
3. Blocking execution until all backend compilations are complete.
80+
81+
Distributors must return a non-zero exit code on failure. They can be
82+
implemented as platform native executables or in a scripting language, such as
83+
Python.
84+
85+
Clang and LLD provide options to specify a distributor program for managing
86+
backend compilations. Distributor options and backend compilation options can
87+
also be specified. Such options are transparently forwarded.
88+
89+
The backend compilations are currently performed by invoking Clang. For further
90+
details, refer to:
91+
92+
* Clang documentation: https://clang.llvm.org/docs/ThinLTO.html
93+
* LLD documentation: https://lld.llvm.org/DTLTO.html
94+
95+
When invoked with a distributor, LLD generates a JSON file describing the
96+
backend compilation jobs and executes the distributor, passing it this file.
97+
98+
JSON Schema
99+
-----------
100+
101+
The JSON format is explained by reference to the following example, which
102+
describes the backend compilation of the modules ``t1.o`` and ``t2.o``:
103+
104+
.. code-block:: json
105+
106+
{
107+
"common": {
108+
"linker_output": "dtlto.elf",
109+
"args": ["/usr/bin/clang", "-O2", "-c", "-fprofile-sample-use=my.prof"],
110+
"inputs": ["my.prof"]
111+
},
112+
"jobs": [
113+
{
114+
"args": ["t1.o", "-fthinlto-index=t1.o.thinlto.bc", "-o", "t1.native.o", "-fproc-stat-report=t1.stats.txt"],
115+
"inputs": ["t1.o", "t1.o.thinlto.bc"],
116+
"outputs": ["t1.native.o", "t1.stats.txt"]
117+
},
118+
{
119+
"args": ["t2.o", "-fthinlto-index=t2.o.thinlto.bc", "-o", "t2.native.o", "-fproc-stat-report=t2.stats.txt"],
120+
"inputs": ["t2.o", "t2.o.thinlto.bc"],
121+
"outputs": ["t2.native.o", "t2.stats.txt"]
122+
}
123+
]
124+
}
125+
126+
Each entry in the ``jobs`` array represents a single backend compilation job.
127+
Each job object records its own command-line arguments and input/output files.
128+
Shared arguments and inputs are defined once in the ``common`` object.
129+
130+
Reserved Entries:
131+
132+
- The first entry in the ``common.args`` array specifies the compiler
133+
executable to invoke.
134+
- The first entry in each job's ``inputs`` array is the bitcode file for the
135+
module being compiled.
136+
- The second entry in each job's ``inputs`` array is the corresponding
137+
individual summary index file.
138+
- The first entry in each job's ``outputs`` array is the primary output object
139+
file.
140+
141+
For the ``outputs`` array, only the first entry is reserved for the primary
142+
output file; there is no guaranteed order for the remaining entries. The primary
143+
output file is specified in a reserved entry because some distribution systems
144+
rely on this path - for example, to provide a meaningful user label for
145+
compilation jobs. Initially, the DTLTO implementation will not produce more than
146+
one output file. However, in the future, if LTO options are added that imply
147+
additional output files, those files will also be included in this array.
148+
149+
Command-line arguments and input/output files are stored separately to allow
150+
the remote compiler to be changed without updating the distributors, as the
151+
distributors do not need to understand the details of the compiler command
152+
line.
153+
154+
To generate the backend compilation commands, the common and job-specific
155+
arguments are concatenated.
156+
157+
When consuming the example JSON above, a distributor is expected to issue the
158+
following backend compilation commands with maximum parallelism:
159+
160+
.. code-block:: console
161+
162+
/usr/bin/clang -O2 -c -fprofile-sample-use=my.prof t1.o -fthinlto-index=t1.o.thinlto.bc -o t1.native.o \
163+
-fproc-stat-report=t1.stats.txt
164+
165+
/usr/bin/clang -O2 -c -fprofile-sample-use=my.prof t2.o -fthinlto-index=t2.o.thinlto.bc -o t2.native.o \
166+
-fproc-stat-report=t2.stats.txt
167+
168+
TODOs
169+
-----
170+
171+
The following features are planned for DTLTO but not yet implemented:
172+
173+
- Support for the ThinLTO in-process cache.
174+
- Support for platforms other than ELF and COFF.
175+
- Support for archives with bitcode members.
176+
- Support for more LTO configurations; only a very limited set of LTO
177+
configurations is supported currently, e.g., support for basic block sections
178+
is not currently available.
179+
180+
Constraints
181+
-----------
182+
183+
- Matching versions of Clang and LLD should be used.
184+
- The distributor used must support the JSON schema generated by the version of
185+
LLD in use.
186+

llvm/docs/UserGuides.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ intermediate LLVM representation.
3232
DebuggingJITedCode
3333
DirectXUsage
3434
Docker
35+
DTLTO
3536
FatLTO
3637
ExtendingLLVM
3738
GitHub
@@ -164,6 +165,11 @@ Optimizations
164165
This document describes the interface between LLVM intermodular optimizer
165166
and the linker and its design
166167

168+
:doc:`DTLTO`
169+
This document describes the DTLTO implementation, which allows for
170+
distributing ThinLTO backend compilations without requiring support from
171+
the build system.
172+
167173
:doc:`GoldPlugin`
168174
How to build your programs with link-time optimization on Linux.
169175

llvm/include/llvm/LTO/LTO.h

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ class InputFile {
199199

200200
using IndexWriteCallback = std::function<void(const std::string &)>;
201201

202+
using ImportsFilesContainer = llvm::SmallVector<std::string>;
203+
202204
/// This class defines the interface to the ThinLTO backend.
203205
class ThinBackendProc {
204206
protected:
@@ -223,13 +225,15 @@ class ThinBackendProc {
223225
BackendThreadPool(ThinLTOParallelism) {}
224226

225227
virtual ~ThinBackendProc() = default;
228+
virtual void setup(unsigned ThinLTONumTasks, unsigned ThinLTOTaskOffset,
229+
Triple Triple) {}
226230
virtual Error start(
227231
unsigned Task, BitcodeModule BM,
228232
const FunctionImporter::ImportMapTy &ImportList,
229233
const FunctionImporter::ExportSetTy &ExportList,
230234
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
231235
MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
232-
Error wait() {
236+
virtual Error wait() {
233237
BackendThreadPool.wait();
234238
if (Err)
235239
return std::move(*Err);
@@ -240,8 +244,15 @@ class ThinBackendProc {
240244

241245
// Write sharded indices and (optionally) imports to disk
242246
Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
243-
llvm::StringRef ModulePath,
244-
const std::string &NewModulePath) const;
247+
StringRef ModulePath, const std::string &NewModulePath) const;
248+
249+
// Write sharded indices to SummaryPath, (optionally) imports to disk, and
250+
// (optionally) record imports in ImportsFiles.
251+
Error emitFiles(const FunctionImporter::ImportMapTy &ImportList,
252+
StringRef ModulePath, const std::string &NewModulePath,
253+
StringRef SummaryPath,
254+
std::optional<std::reference_wrapper<ImportsFilesContainer>>
255+
ImportsFiles) const;
245256
};
246257

247258
/// This callable defines the behavior of a ThinLTO backend after the thin-link
@@ -294,6 +305,30 @@ ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism,
294305
bool ShouldEmitIndexFiles = false,
295306
bool ShouldEmitImportsFiles = false);
296307

308+
/// This ThinBackend generates the index shards and then runs the individual
309+
/// backend jobs via an external process. It takes the same parameters as the
310+
/// InProcessThinBackend; however, these parameters only control the behavior
311+
/// when generating the index files for the modules. Additionally:
312+
/// LinkerOutputFile is a string that should identify this LTO invocation in
313+
/// the context of a wider build. It's used for naming to aid the user in
314+
/// identifying activity related to a specific LTO invocation.
315+
/// Distributor specifies the path to a process to invoke to manage the backend
316+
/// job execution.
317+
/// DistributorArgs specifies a list of arguments to be applied to the
318+
/// distributor.
319+
/// RemoteCompiler specifies the path to a Clang executable to be invoked for
320+
/// the backend jobs.
321+
/// RemoteCompilerArgs specifies a list of arguments to be applied to the
322+
/// backend compilations.
323+
/// SaveTemps is a debugging tool that prevents temporary files created by this
324+
/// backend from being cleaned up.
325+
ThinBackend createOutOfProcessThinBackend(
326+
ThreadPoolStrategy Parallelism, IndexWriteCallback OnWrite,
327+
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
328+
StringRef LinkerOutputFile, StringRef Distributor,
329+
ArrayRef<StringRef> DistributorArgs, StringRef RemoteCompiler,
330+
ArrayRef<StringRef> RemoteCompilerArgs, bool SaveTemps);
331+
297332
/// This ThinBackend writes individual module indexes to files, instead of
298333
/// running the individual backend jobs. This backend is for distributed builds
299334
/// where separate processes will invoke the real backends.

llvm/include/llvm/Transforms/IPO/FunctionImport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@ Error EmitImportsFiles(
421421
StringRef ModulePath, StringRef OutputFilename,
422422
const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex);
423423

424+
/// Call \p F passing each of the files module \p ModulePath will import from.
425+
void processImportsFiles(
426+
StringRef ModulePath,
427+
const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex,
428+
function_ref<void(const std::string &)> F);
429+
424430
/// Based on the information recorded in the summaries during global
425431
/// summary-based analysis:
426432
/// 1. Resolve prevailing symbol linkages and constrain visibility (CanAutoHide

0 commit comments

Comments
 (0)