sembr src/building/optimized-build.md

This commit is contained in:
Tshepang Mbambo 2026-02-03 00:46:00 +02:00
parent e83ee8a2be
commit 222e5216a0

View file

@ -2,15 +2,16 @@
There are multiple additional build configuration options and techniques that can be used to compile a
build of `rustc` that is as optimized as possible (for example when building `rustc` for a Linux
distribution). The status of these configuration options for various Rust targets is tracked [here].
distribution).
The status of these configuration options for various Rust targets is tracked [here].
This page describes how you can use these approaches when building `rustc` yourself.
[here]: https://github.com/rust-lang/rust/issues/103595
## Link-time optimization
Link-time optimization is a powerful compiler technique that can increase program performance. To
enable (Thin-)LTO when building `rustc`, set the `rust.lto` config option to `"thin"`
Link-time optimization is a powerful compiler technique that can increase program performance.
To enable (Thin-)LTO when building `rustc`, set the `rust.lto` config option to `"thin"`
in `bootstrap.toml`:
```toml
@ -29,8 +30,8 @@ Enabling LTO on Linux has [produced] speed-ups by up to 10%.
## Memory allocator
Using a different memory allocator for `rustc` can provide significant performance benefits. If you
want to enable the `jemalloc` allocator, you can set the `rust.jemalloc` option to `true`
Using a different memory allocator for `rustc` can provide significant performance benefits.
If you want to enable the `jemalloc` allocator, you can set the `rust.jemalloc` option to `true`
in `bootstrap.toml`:
```toml
@ -53,7 +54,8 @@ rust.codegen-units-std = 1
## Instruction set
By default, `rustc` is compiled for a generic (and conservative) instruction set architecture
(depending on the selected target), to make it support as many CPUs as possible. If you want to
(depending on the selected target), to make it support as many CPUs as possible.
If you want to
compile `rustc` for a specific instruction set architecture, you can set the `target_cpu` compiler
option in `RUSTFLAGS`:
@ -72,14 +74,16 @@ llvm.cflags = "-march=x86-64-v3"
## Profile-guided optimization
Applying profile-guided optimizations (or more generally, feedback-directed optimizations) can
produce a large increase to `rustc` performance, by up to 15% ([1], [2]). However, these techniques
produce a large increase to `rustc` performance, by up to 15% ([1], [2]).
However, these techniques
are not simply enabled by a configuration option, but rather they require a complex build workflow
that compiles `rustc` multiple times and profiles it on selected benchmarks.
There is a tool called `opt-dist` that is used to optimize `rustc` with [PGO] (profile-guided
optimizations) and [BOLT] (a post-link binary optimizer) for builds distributed to end users. You
can examine the tool, which is located in `src/tools/opt-dist`, and build a custom PGO build
workflow based on it, or try to use it directly. Note that the tool is currently quite hardcoded to
optimizations) and [BOLT] (a post-link binary optimizer) for builds distributed to end users.
You can examine the tool, which is located in `src/tools/opt-dist`, and build a custom PGO build
workflow based on it, or try to use it directly.
Note that the tool is currently quite hardcoded to
the way we use it in Rust's continuous integration workflows, and it might require some custom
changes to make it work in a different environment.
@ -93,9 +97,9 @@ changes to make it work in a different environment.
To use the tool, you will need to provide some external dependencies:
- A Python3 interpreter (for executing `x.py`).
- Compiled LLVM toolchain, with the `llvm-profdata` binary. Optionally, if you want to use BOLT,
the `llvm-bolt` and
`merge-fdata` binaries have to be available in the toolchain.
- Compiled LLVM toolchain, with the `llvm-profdata` binary.
Optionally, if you want to use BOLT,
the `llvm-bolt` and `merge-fdata` binaries have to be available in the toolchain.
These dependencies are provided to `opt-dist` by an implementation of the [`Environment`] struct.
It specifies directories where will the PGO/BOLT pipeline take place, and also external dependencies