1 =============================
2 Advanced Build Configurations
3 =============================
11 `CMake <http://www.cmake.org/>`_ is a cross-platform build-generator tool. CMake
12 does not build the project, it generates the files needed by your build tool
13 (GNU make, Visual Studio, etc.) for building LLVM.
15 If **you are a new contributor**, please start with the :doc:`GettingStarted` or
16 :doc:`CMake` pages. This page is intended for users doing more complex builds.
18 Many of the examples below are written assuming specific CMake Generators.
19 Unless otherwise explicitly called out these commands should work with any CMake
22 Many of the build configurations mentioned on this documentation page can be
23 utilized by using a CMake cache. A CMake cache is essentially a configuration
24 file that sets the necessary flags for a specific build configuration. The caches
25 for Clang are located in :code:`/clang/cmake/caches` within the monorepo. They
26 can be passed to CMake using the :code:`-C` flag as demonstrated in the examples
27 below along with additional configuration flags.
32 The Clang CMake build system supports bootstrap (aka multi-stage) builds. At a
33 high level a multi-stage build is a chain of builds that pass data from one
34 stage into the next. The most common and simple version of this is a traditional
37 In a simple two-stage bootstrap build, we build clang using the system compiler,
38 then use that just-built clang to build clang again. In CMake this simplest form
39 of a bootstrap build can be configured with a single option,
40 CLANG_ENABLE_BOOTSTRAP.
42 .. code-block:: console
44 $ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release \
45 -DCLANG_ENABLE_BOOTSTRAP=On \
46 -DLLVM_ENABLE_PROJECTS="clang" \
50 This command itself isn't terribly useful because it assumes default
51 configurations for each stage. The next series of examples utilize CMake cache
52 scripts to provide more complex options.
54 By default, only a few CMake options will be passed between stages.
55 The list, called _BOOTSTRAP_DEFAULT_PASSTHROUGH, is defined in clang/CMakeLists.txt.
56 To force the passing of the variables between stages, use the -DCLANG_BOOTSTRAP_PASSTHROUGH
57 CMake option, each variable separated by a ";". As example:
59 .. code-block:: console
61 $ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release \
62 -DCLANG_ENABLE_BOOTSTRAP=On \
63 -DCLANG_BOOTSTRAP_PASSTHROUGH="CMAKE_INSTALL_PREFIX;CMAKE_VERBOSE_MAKEFILE" \
64 -DLLVM_ENABLE_PROJECTS="clang" \
68 CMake options starting by ``BOOTSTRAP_`` will be passed only to the stage2 build.
69 This gives the opportunity to use Clang specific build flags.
70 For example, the following CMake call will enabled '-fno-addrsig' only during
71 the stage2 build for C and C++.
73 .. code-block:: console
75 $ cmake [..] -DBOOTSTRAP_CMAKE_CXX_FLAGS='-fno-addrsig' -DBOOTSTRAP_CMAKE_C_FLAGS='-fno-addrsig' [..]
77 The clang build system refers to builds as stages. A stage1 build is a standard
78 build using the compiler installed on the host, and a stage2 build is built
79 using the stage1 compiler. This nomenclature holds up to more stages too. In
80 general a stage*n* build is built using the output from stage*n-1*.
82 Apple Clang Builds (A More Complex Bootstrap)
83 =============================================
85 Apple's Clang builds are a slightly more complicated example of the simple
86 bootstrapping scenario. Apple Clang is built using a 2-stage build.
88 The stage1 compiler is a host-only compiler with some options set. The stage1
89 compiler is a balance of optimization vs build time because it is a throwaway.
90 The stage2 compiler is the fully optimized compiler intended to ship to users.
92 Setting up these compilers requires a lot of options. To simplify the
93 configuration the Apple Clang build settings are contained in CMake Cache files.
94 You can build an Apple Clang compiler using the following commands:
96 .. code-block:: console
98 $ cmake -G Ninja -C <path to source>/clang/cmake/caches/Apple-stage1.cmake <path to source>/llvm
99 $ ninja stage2-distribution
101 This CMake invocation configures the stage1 host compiler, and sets
102 CLANG_BOOTSTRAP_CMAKE_ARGS to pass the Apple-stage2.cmake cache script to the
103 stage2 configuration step.
105 When you build the stage2-distribution target it builds the minimal stage1
106 compiler and required tools, then configures and builds the stage2 compiler
107 based on the settings in Apple-stage2.cmake.
109 This pattern of using cache scripts to set complex settings, and specifically to
110 make later stage builds include cache scripts is common in our more advanced
111 build configurations.
116 Profile-Guided Optimizations (PGO) is a really great way to optimize the code
117 clang generates. Our multi-stage PGO builds are a workflow for generating PGO
118 profiles that can be used to optimize clang.
120 At a high level, the way PGO works is that you build an instrumented compiler,
121 then you run the instrumented compiler against sample source files. While the
122 instrumented compiler runs it will output a bunch of files containing
123 performance counters (.profraw files). After generating all the profraw files
124 you use llvm-profdata to merge the files into a single profdata file that you
125 can feed into the LLVM_PROFDATA_FILE option.
127 Our PGO.cmake cache automates that whole process. You can use it for
128 configuration with CMake with the following command:
130 .. code-block:: console
132 $ cmake -G Ninja -C <path to source>/clang/cmake/caches/PGO.cmake \
133 <path to source>/llvm
135 There are several additional options that the cache file also accepts to modify
136 the build, particularly the PGO_INSTRUMENT_LTO option. Setting this option to
137 Thin or Full will enable ThinLTO or full LTO respectively, further enhancing
138 the performance gains from a PGO build by enabling interprocedural
139 optimizations. For example, to run a CMake configuration for a PGO build
140 that also enables ThinTLO, use the following command:
142 .. code-block:: console
144 $ cmake -G Ninja -C <path to source>/clang/cmake/caches/PGO.cmake \
145 -DPGO_INSTRUMENT_LTO=Thin \
146 <path to source>/llvm
148 After configuration, building the stage2-instrumented-generate-profdata target
149 will automatically build the stage1 compiler, build the instrumented compiler
150 with the stage1 compiler, and then run the instrumented compiler against the
153 .. code-block:: console
155 $ ninja stage2-instrumented-generate-profdata
157 If you let that run for a few hours or so, it will place a profdata file in your
158 build directory. This takes a really long time because it builds clang twice,
159 and you *must* have compiler-rt in your build tree.
161 This process uses any source files under the perf-training directory as training
162 data as long as the source files are marked up with LIT-style RUN lines.
164 After it finishes you can use :code:`find . -name clang.profdata` to find it, but it
165 should be at a path something like:
167 .. code-block:: console
169 <build dir>/tools/clang/stage2-instrumented-bins/utils/perf-training/clang.profdata
171 You can feed that file into the LLVM_PROFDATA_FILE option when you build your
174 It may be necessary to build additional targets before running perf training, such as
175 builtins and runtime libraries. You can use the :code:`CLANG_PERF_TRAINING_DEPS` CMake
176 variable for that purpose:
178 .. code-block:: cmake
180 set(CLANG_PERF_TRAINING_DEPS builtins runtimes CACHE STRING "")
182 The PGO cache has a slightly different stage naming scheme than other
183 multi-stage builds. It generates three stages: stage1, stage2-instrumented, and
184 stage2. Both of the stage2 builds are built using the stage1 compiler.
186 The PGO cache generates the following additional targets:
188 **stage2-instrumented**
189 Builds a stage1 compiler, runtime, and required tools (llvm-config,
190 llvm-profdata) then uses that compiler to build an instrumented stage2 compiler.
192 **stage2-instrumented-generate-profdata**
193 Depends on stage2-instrumented and will use the instrumented compiler to
194 generate profdata based on the training files in clang/utils/perf-training
197 Depends on stage2-instrumented-generate-profdata and will use the stage1
198 compiler with the stage2 profdata to build a PGO-optimized compiler.
200 **stage2-check-llvm**
201 Depends on stage2 and runs check-llvm using the stage2 compiler.
203 **stage2-check-clang**
204 Depends on stage2 and runs check-clang using the stage2 compiler.
207 Depends on stage2 and runs check-all using the stage2 compiler.
209 **stage2-test-suite**
210 Depends on stage2 and runs the test-suite using the stage2 compiler (requires
216 `BOLT <https://github.com/llvm/llvm-project/blob/main/bolt/README.md>`_
217 (Binary Optimization and Layout Tool) is a tool that optimizes binaries
218 post-link by profiling them at runtime and then using that information to
219 optimize the layout of the final binary among other optimizations performed
220 at the binary level. There are also CMake caches available to build
221 LLVM/Clang with BOLT.
223 To configure a single-stage build that builds LLVM/Clang and then optimizes
224 it with BOLT, use the following CMake configuration:
226 .. code-block:: console
228 $ cmake <path to source>/llvm -C <path to source>/clang/cmake/caches/BOLT.cmake
230 Then, build the BOLT-optimized binary by running the following ninja command:
232 .. code-block:: console
236 If you're seeing errors in the build process, try building with a recent
237 version of Clang/LLVM by setting the CMAKE_C_COMPILER and
238 CMAKE_CXX_COMPILER flags to the appropriate values.
240 It is also possible to use BOLT on top of PGO and (Thin)LTO for an even more
241 significant runtime speedup. To configure a three stage PGO build with ThinLTO
242 that optimizes the resulting binary with BOLT, use the following CMake
243 configuration command:
245 .. code-block:: console
247 $ cmake -G Ninja <path to source>/llvm \
248 -C <path to source>/clang/cmake/caches/BOLT-PGO.cmake \
249 -DBOOTSTRAP_LLVM_ENABLE_LLD=ON \
250 -DBOOTSTRAP_BOOTSTRAP_LLVM_ENABLE_LLD=ON \
251 -DPGO_INSTRUMENT_LTO=Thin
253 Then, to build the final optimized binary, build the stage2-clang-bolt target:
255 .. code-block:: console
257 $ ninja stage2-clang-bolt
259 3-Stage Non-Determinism
260 =======================
262 In the ancient lore of compilers non-determinism is like the multi-headed hydra.
263 Whenever its head pops up, terror and chaos ensue.
265 Historically one of the tests to verify that a compiler was deterministic would
266 be a three stage build. The idea of a three stage build is you take your sources
267 and build a compiler (stage1), then use that compiler to rebuild the sources
268 (stage2), then you use that compiler to rebuild the sources a third time
269 (stage3) with an identical configuration to the stage2 build. At the end of
270 this, you have a stage2 and stage3 compiler that should be bit-for-bit
273 You can perform one of these 3-stage builds with LLVM and clang using the
276 .. code-block:: console
278 $ cmake -G Ninja -C <path to source>/clang/cmake/caches/3-stage.cmake <path to source>/llvm
281 After the build you can compare the stage2 and stage3 compilers.