Sub topic 15
Nightly options: parallel front end and Cranelift
This chapter covers two experimental compiler features that can shorten builds: the parallel front end and the Cranelift code generator. Both need the nightly toolchain. It assumes you can read Cargo.toml and .cargo/config.toml, and that you know the rustc, LLVM and linker phases and the cargo build --timings report from earlier chapters. You should also be able to switch toolchains with rustup.
Why these options are nightly-only
Nightly is the toolchain channel that ships unfinished features. Anything marked unstable is gated behind it, either as a -Z compiler flag or as a Cargo feature that you must enable by name. The gate exists because the behavior can change, or break, between releases.
The two features map onto the build phases like this:
- Parallel front end speeds up the front end of rustc. That covers parsing, name resolution, type checking and borrow checking. It helps
cargo checkand the first half ofcargo build. - Cranelift replaces LLVM as the code generator. It helps the back end, the phase that turns the compiler's intermediate form into machine code.
Neither option touches the linker. If the link step dominates your timings, go back to the faster-linker chapter first.
Setting up a nightly toolchain without changing your stable workflow
Do not make nightly your global default. Install it next to stable and select it per command or per project.
rustup toolchain install nightly
cargo +nightly --versionFor a project that uses nightly consistently, pin it with a rust-toolchain.toml file in the repository root. Everyone who builds the project, and your CI, then gets the same compiler.
# rust-toolchain.toml
[toolchain]
channel = "nightly"
components = ["rustc-codegen-cranelift-preview"]Pinning plain nightly still moves forward each day. If you want repeatable builds, pin a dated channel such as nightly-2026-11-20, and update it on purpose.
The parallel front end
Historically rustc ran its front end on a single thread, even though it is spread across many crates and many items inside each crate. Cargo already builds independent crates in parallel. The parallel front end adds parallelism inside a single crate's compilation, which matters most when one large crate is the bottleneck.
You enable it with the -Z threads=N flag. The default is 1, which means the old serial behavior.
RUSTFLAGS="-Zthreads=8" cargo +nightly checkTo make it permanent for a project, put the flag in .cargo/config.toml:
# .cargo/config.toml
[build]
rustflags = ["-Zthreads=8"]Reported savings vary by project. The Rust project's own measurements showed reductions of roughly 20 to 30 percent on average for front-end-heavy builds, with some larger cases. Treat those numbers as a hint, not a promise. A project dominated by LLVM or linking will see little change.
Choosing a thread count
More threads is not always faster. The threads contend for shared data structures inside the compiler, and Cargo may already be running several rustc processes at once.
- Start with 4 or 8, not the number of your logical cores.
- Compare against
-Zthreads=1on the same machine, since timings vary from run to run. - Check the timings report. If many crates already compile at the same time, the extra threads per crate have no idle cores to use.
A gotcha: rustflags sources do not merge
Cargo reads rustflags from several places, but it uses only the first source it finds. In priority order:
- The
CARGO_ENCODED_RUSTFLAGSenvironment variable. - The
RUSTFLAGSenvironment variable. target.<triple>.rustflags(and matchingtarget.<cfg>.rustflags) in config files.build.rustflagsin config files.
If you followed the linker chapter, you may already have a [target.x86_64-unknown-linux-gnu] section with rustflags. Adding [build] rustflags will then do nothing on that target. Put -Zthreads in the same list as the linker argument:
# .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=lld", "-Zthreads=8"]A second consequence is cache invalidation. Changing the rustflags changes the build fingerprint, so Cargo rebuilds everything the first time you switch, and again when you switch back.
The Cranelift backend
LLVM produces fast machine code, but it spends a lot of time doing so. Cranelift is a different code generator, written in Rust and designed for speed of compilation. It does far fewer optimizations, so it compiles quickly and produces slower programs. That trade suits debug builds, where you rebuild often and rarely care about peak speed.
The rustc integration is called rustc_codegen_cranelift. You install it as a nightly rustup component:
rustup component add rustc-codegen-cranelift-preview --toolchain nightlyEnabling it per profile
Cargo can select the backend per profile through the unstable codegen-backend feature. The cargo-features line must be the first thing in the manifest.
cargo-features = ["codegen-backend"]
[package]
name = "demo"
version = "0.1.0"
edition = "2021"
[profile.dev]
codegen-backend = "cranelift"Then build with nightly:
cargo +nightly buildRelease builds still use LLVM, because the setting applies only to the dev profile. This is the arrangement you want: quick iteration in development and optimized output when you ship.
You can also select the backend for a single command without editing the manifest:
RUSTFLAGS="-Zcodegen-backend=cranelift" cargo +nightly buildWhat you give up
- Runtime speed. Programs built with Cranelift can run noticeably slower than LLVM debug builds. Tests that do heavy computation may take longer to run even though they build sooner. Measure total time, build plus run.
- Target coverage. Support is best on x86-64 and AArch64 Linux, macOS and Windows. Other targets may be missing or incomplete.
- Feature gaps. Some intrinsics, SIMD paths and inline assembly cases are unsupported or behave differently. A crate that uses them may fail to compile or hit a runtime error.
- Different output. Floating-point edge cases and undefined behavior can show up differently from LLVM, which can hide or reveal bugs. Do not treat a Cranelift build as proof that your release build is correct.
Deciding whether to use them
Match the option to the bottleneck you measured:
| What `--timings` shows | Try first | Expected effect |
|---|---|---|
| One large crate with a long type-checking phase | `-Zthreads=4` or `8` | Shorter check and build time for that crate |
| Long code generation time in debug builds | Cranelift in `profile.dev` | Faster codegen, slower resulting binary |
| Long link step | Faster linker (earlier chapter) | Neither option helps |
| Many small crates, cores already busy | Neither | Little or no gain from extra threads |
| Release builds are slow | LTO and codegen-unit tuning | Cranelift does not apply to release |
Use this procedure to confirm a gain:
- Run
cargo +nightly clean, then time a full build withcargo +nightly build --timingsand keep the HTML report. - Enable one option.
- Clean and time again, and compare both the wall-clock time and the report.
- Time an incremental rebuild after a one-line change, because that is the loop you actually live in.
Enable one option at a time. If you turn on both and the build gets faster, you will not know which one helped, and if it breaks you will not know which one to blame.
Risks of depending on nightly
Nightly builds can break without warning. A compiler update may reject code, change flag names, or regress a feature. Plan for that:
- Pin a dated nightly in
rust-toolchain.tomland update it deliberately. - Keep your code compiling on stable. Use the nightly options only as build flags and profile settings, not as language features.
- Keep the configuration in one place, so a teammate who lacks nightly can remove it quickly.
- Check the unstable-flag documentation when you update, since names such as
codegen-backendhave changed during development.
Exercise
Your .cargo/config.toml already has a [target.x86_64-unknown-linux-gnu] section with rustflags = ["-C", "link-arg=-fuse-ld=lld"]. You add [build] rustflags = ["-Zthreads=8"] and see no change in the timings report. Why, and how do you fix it? Also, which build would you expect Cranelift to speed up: cargo build or cargo build --release?
Show answer
Cargo uses only the first rustflags source it finds, and the target-specific rustflags takes priority over build.rustflags. The -Zthreads=8 flag is therefore ignored. Fix it by adding "-Zthreads=8" to the target section's rustflags list. Cranelift set under [profile.dev] speeds up cargo build, the dev profile. The release profile keeps using LLVM.