Sub topic 15
Nightly options: parallel front end and Cranelift
This chapter assumes you can switch toolchains with rustup, edit Cargo.toml and .cargo/config.toml, and read a cargo build --timings report. Both options here need the nightly toolchain. Their flags can change or disappear without notice, so treat them as optional experiments, not as the foundation of your build.
Where each option helps
The earlier chapters split a build into the rustc front end (parsing, type checking, borrow checking), LLVM code generation, and linking. Nightly offers one option for each of the first two stages.
The parallel front end
By default, rustc runs the front end for one crate on a single thread. Cargo already builds separate crates in parallel, but a large crate, or a crate that the rest of the graph waits on, can leave most cores idle. The -Z threads=N flag lets rustc use several threads for the front end of a single crate.
Pass the flag with an environment variable so that stable builds are not affected:
RUSTFLAGS="-Z threads=8" cargo +nightly buildPick a value near your physical core count. Gains vary a lot between projects. Crates with many items and heavy type checking benefit most, and small crates may see little change. Always compare cargo build --timings before and after.
Changing RUSTFLAGS changes the compiler flags for every crate, so Cargo rebuilds everything the first time. Run one clean comparison, not a series of incremental ones.
The Cranelift backend
Cranelift is an alternative code generator. It does far less optimization than LLVM, so it compiles faster but produces slower programs. That is a good trade for a debug build you only run to check behavior, and a bad one for anything you benchmark or ship.
Install the component, then enable the backend in the dev profile:
rustup component add rustc-codegen-cranelift-preview --toolchain nightlycargo-features = ["codegen-backend"]
[package]
name = "demo"
version = "0.1.0"
edition = "2021"
[profile.dev]
codegen-backend = "cranelift"cargo-features must appear before [package]. Run cargo +nightly build and the dev profile now uses Cranelift, while --release keeps LLVM only if you do not set the backend there.
Common errors and limits
- "the option
Zis only accepted on the nightly compiler". A stable toolchain read your-Zflag. Usecargo +nightly, or pin nightly with arust-toolchain.tomlfile. - Putting
-Zflags in[build] rustflagsfor the whole repository. Every contributor and every CI job on stable then fails. Keep them in an environment variable or a local config. - Unsupported features. Cranelift does not support everything LLVM does. Some inline assembly and platform-specific SIMD code may fail to compile or behave differently.
- Misleading tests. Debug binaries from Cranelift run slower, so timing-sensitive tests can fail for reasons unrelated to your code.
- Mixed results. If the linker dominates your time, neither option will help. Check the timings first.
Choosing
| Symptom in your timings | Try |
|---|---|
| One large crate holds up the build | `-Z threads=N` |
| Code generation dominates debug builds | Cranelift |
| Link step dominates | A faster linker, covered earlier |
Exercise
Your team builds on stable in CI, but you want to try Cranelift on your own machine for debug builds. Where should you configure it, and why should you keep release builds on LLVM?
Show answer
Configure it locally, for example by building with cargo +nightly and keeping the codegen-backend setting out of the shared manifest, because a stable toolchain in CI would reject the nightly-only feature. Release builds should stay on LLVM because Cranelift does little optimization, so the resulting binary runs slower and may lack some features.