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 -DCLANG_ENABLE_BOOTSTRAP=On <path to source>
47 This command itself isn't terribly useful because it assumes default
48 configurations for each stage. The next series of examples utilize CMake cache
49 scripts to provide more complex options.
51 By default, only a few CMake options will be passed between stages.
52 The list, called _BOOTSTRAP_DEFAULT_PASSTHROUGH, is defined in clang/CMakeLists.txt.
53 To force the passing of the variables between stages, use the -DCLANG_BOOTSTRAP_PASSTHROUGH
54 CMake option, each variable separated by a ";". As example:
56 .. code-block:: console
58 $ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DCLANG_ENABLE_BOOTSTRAP=On -DCLANG_BOOTSTRAP_PASSTHROUGH="CMAKE_INSTALL_PREFIX;CMAKE_VERBOSE_MAKEFILE" <path to source>
61 CMake options starting by ``BOOTSTRAP_`` will be passed only to the stage2 build.
62 This gives the opportunity to use Clang specific build flags.
63 For example, the following CMake call will enabled '-fno-addrsig' only during
64 the stage2 build for C and C++.
66 .. code-block:: console
68 $ cmake [..] -DBOOTSTRAP_CMAKE_CXX_FLAGS='-fno-addrsig' -DBOOTSTRAP_CMAKE_C_FLAGS='-fno-addrsig' [..]
70 The clang build system refers to builds as stages. A stage1 build is a standard
71 build using the compiler installed on the host, and a stage2 build is built
72 using the stage1 compiler. This nomenclature holds up to more stages too. In
73 general a stage*n* build is built using the output from stage*n-1*.
75 Apple Clang Builds (A More Complex Bootstrap)
76 =============================================
78 Apple's Clang builds are a slightly more complicated example of the simple
79 bootstrapping scenario. Apple Clang is built using a 2-stage build.
81 The stage1 compiler is a host-only compiler with some options set. The stage1
82 compiler is a balance of optimization vs build time because it is a throwaway.
83 The stage2 compiler is the fully optimized compiler intended to ship to users.
85 Setting up these compilers requires a lot of options. To simplify the
86 configuration the Apple Clang build settings are contained in CMake Cache files.
87 You can build an Apple Clang compiler using the following commands:
89 .. code-block:: console
91 $ cmake -G Ninja -C <path to source>/clang/cmake/caches/Apple-stage1.cmake <path to source>
92 $ ninja stage2-distribution
94 This CMake invocation configures the stage1 host compiler, and sets
95 CLANG_BOOTSTRAP_CMAKE_ARGS to pass the Apple-stage2.cmake cache script to the
96 stage2 configuration step.
98 When you build the stage2-distribution target it builds the minimal stage1
99 compiler and required tools, then configures and builds the stage2 compiler
100 based on the settings in Apple-stage2.cmake.
102 This pattern of using cache scripts to set complex settings, and specifically to
103 make later stage builds include cache scripts is common in our more advanced
104 build configurations.
109 Profile-Guided Optimizations (PGO) is a really great way to optimize the code
110 clang generates. Our multi-stage PGO builds are a workflow for generating PGO
111 profiles that can be used to optimize clang.
113 At a high level, the way PGO works is that you build an instrumented compiler,
114 then you run the instrumented compiler against sample source files. While the
115 instrumented compiler runs it will output a bunch of files containing
116 performance counters (.profraw files). After generating all the profraw files
117 you use llvm-profdata to merge the files into a single profdata file that you
118 can feed into the LLVM_PROFDATA_FILE option.
120 Our PGO.cmake cache automates that whole process. You can use it for
121 configuration with CMake with the following command:
123 .. code-block:: console
125 $ cmake -G Ninja -C <path to source>/clang/cmake/caches/PGO.cmake \
126 <path to source>/llvm
128 There are several additional options that the cache file also accepts to modify
129 the build, particularly the PGO_INSTRUMENT_LTO option. Setting this option to
130 Thin or Full will enable ThinLTO or full LTO respectively, further enhancing
131 the performance gains from a PGO build by enabling interprocedural
132 optimizations. For example, to run a CMake configuration for a PGO build
133 that also enables ThinTLO, use the following command:
135 .. code-block:: console
137 $ cmake -G Ninja -C <path to source>/clang/cmake/caches/PGO.cmake \
138 -DPGO_INSTRUMENT_LTO=Thin \
139 <path to source>/llvm
141 After configuration, building the stage2-instrumented-generate-profdata target
142 will automatically build the stage1 compiler, build the instrumented compiler
143 with the stage1 compiler, and then run the instrumented compiler against the
146 .. code-block:: console
148 $ ninja stage2-instrumented-generate-profdata
150 If you let that run for a few hours or so, it will place a profdata file in your
151 build directory. This takes a really long time because it builds clang twice,
152 and you *must* have compiler-rt in your build tree.
154 This process uses any source files under the perf-training directory as training
155 data as long as the source files are marked up with LIT-style RUN lines.
157 After it finishes you can use :code:`find . -name clang.profdata` to find it, but it
158 should be at a path something like:
160 .. code-block:: console
162 <build dir>/tools/clang/stage2-instrumented-bins/utils/perf-training/clang.profdata
164 You can feed that file into the LLVM_PROFDATA_FILE option when you build your
167 It may be necessary to build additional targets before running perf training, such as
168 builtins and runtime libraries. You can use the :code:`CLANG_PERF_TRAINING_DEPS` CMake
169 variable for that purpose:
171 .. code-block:: cmake
173 set(CLANG_PERF_TRAINING_DEPS builtins runtimes CACHE STRING "")
175 The PGO cache has a slightly different stage naming scheme than other
176 multi-stage builds. It generates three stages: stage1, stage2-instrumented, and
177 stage2. Both of the stage2 builds are built using the stage1 compiler.
179 The PGO cache generates the following additional targets:
181 **stage2-instrumented**
182 Builds a stage1 compiler, runtime, and required tools (llvm-config,
183 llvm-profdata) then uses that compiler to build an instrumented stage2 compiler.
185 **stage2-instrumented-generate-profdata**
186 Depends on stage2-instrumented and will use the instrumented compiler to
187 generate profdata based on the training files in clang/utils/perf-training
190 Depends on stage2-instrumented-generate-profdata and will use the stage1
191 compiler with the stage2 profdata to build a PGO-optimized compiler.
193 **stage2-check-llvm**
194 Depends on stage2 and runs check-llvm using the stage2 compiler.
196 **stage2-check-clang**
197 Depends on stage2 and runs check-clang using the stage2 compiler.
200 Depends on stage2 and runs check-all using the stage2 compiler.
202 **stage2-test-suite**
203 Depends on stage2 and runs the test-suite using the stage2 compiler (requires
209 `BOLT <https://github.com/llvm/llvm-project/blob/main/bolt/README.md>`_
210 (Binary Optimization and Layout Tool) is a tool that optimizes binaries
211 post-link by profiling them at runtime and then using that information to
212 optimize the layout of the final binary among other optimizations performed
213 at the binary level. There are also CMake caches available to build
214 LLVM/Clang with BOLT.
216 To configure a single-stage build that builds LLVM/Clang and then optimizes
217 it with BOLT, use the following CMake configuration:
219 .. code-block:: console
221 $ cmake <path to source>/llvm -C <path to source>/clang/cmake/caches/BOLT.cmake
223 Then, build the BOLT-optimized binary by running the following ninja command:
225 .. code-block:: console
229 If you're seeing errors in the build process, try building with a recent
230 version of Clang/LLVM by setting the CMAKE_C_COMPILER and
231 CMAKE_CXX_COMPILER flags to the appropriate values.
233 It is also possible to use BOLT on top of PGO and (Thin)LTO for an even more
234 significant runtime speedup. To configure a three stage PGO build with ThinLTO
235 that optimizes the resulting binary with BOLT, use the following CMake
236 configuration command:
238 .. code-block:: console
240 $ cmake -G Ninja <path to source>/llvm \
241 -C <path to source>/clang/cmake/caches/BOLT-PGO.cmake \
242 -DBOOTSTRAP_LLVM_ENABLE_LLD=ON \
243 -DBOOTSTRAP_BOOTSTRAP_LLVM_ENABLE_LLD=ON \
244 -DPGO_INSTRUMENT_LTO=Thin
246 Then, to build the final optimized binary, build the stage2-clang-bolt target:
248 .. code-block:: console
250 $ ninja stage2-clang-bolt
252 3-Stage Non-Determinism
253 =======================
255 In the ancient lore of compilers non-determinism is like the multi-headed hydra.
256 Whenever its head pops up, terror and chaos ensue.
258 Historically one of the tests to verify that a compiler was deterministic would
259 be a three stage build. The idea of a three stage build is you take your sources
260 and build a compiler (stage1), then use that compiler to rebuild the sources
261 (stage2), then you use that compiler to rebuild the sources a third time
262 (stage3) with an identical configuration to the stage2 build. At the end of
263 this, you have a stage2 and stage3 compiler that should be bit-for-bit
266 You can perform one of these 3-stage builds with LLVM & clang using the
269 .. code-block:: console
271 $ cmake -G Ninja -C <path to source>/clang/cmake/caches/3-stage.cmake <path to source>
272 $ cmake --build . --target stage3 --parallel
274 After the build you can compare the stage2 & stage3 compilers.