|
| 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 | + |
0 commit comments