diff --git a/config.toml.example b/config.toml.example index 299bfd779e57a..b0dcb0738ab20 100644 --- a/config.toml.example +++ b/config.toml.example @@ -427,6 +427,12 @@ changelog-seen = 2 # Uses the rustc defaults: https://doc.rust-lang.org/rustc/codegen-options/index.html#codegen-units #codegen-units = if incremental { 256 } else { 16 } +# Tweaks the numbers of codegen units to improve compile time, typically by +# using a single codegen unit for crates. The `codegen-units` configuration is +# preferred over this field. This is recommended to set for release builds and CI, +# but not for development as it can slow down partial rebuilds. +#codegen-units-fast = false + # Sets the number of codegen units to build the standard library with, # regardless of what the codegen-unit setting for the rest of the compiler is. # NOTE: building with anything other than 1 is known to occasionally have bugs. diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index 9611c866df599..79a115598994b 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -70,6 +70,29 @@ fn main() { cmd.arg("-Ztime-passes"); } } + + if let Some(profile) = env::var_os("RUSTC_TUNE_COMPILER_CODEGEN_UNITS") { + let cgus = match crate_name { + // This crate is large and at the tail of the compiler crates, give it extra CGUs. + //"rustc_query_impl" => Some(96), + // This compiles after all other crates so give it default CGUs to speed it up. + "rustc_driver" => None, + _ => { + if profile == "fast" { + Some(1) + } else { + if crate_name.starts_with("rustc_") { + None + } else { + // Compile crates.io crates with a single CGU for faster compile times + Some(1) + } + } + } + }; + + cgus.map(|cgus| cmd.arg(&format!("-Ccodegen-units={}", cgus))); + } } // Print backtrace in case of ICE diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index b4fc1d4f28da7..16ca1966823c3 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1884,6 +1884,9 @@ impl<'a> Builder<'a> { (Mode::Std, Some(n), _) | (_, _, Some(n)) => { cargo.env(profile_var("CODEGEN_UNITS"), n.to_string()); } + (Mode::Rustc, _, _) => { + cargo.env("RUSTC_TUNE_COMPILER_CODEGEN_UNITS", "fast"); + } _ => { // Don't set anything } diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index fdd659c60ca7f..a45ff0e798252 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -145,6 +145,7 @@ pub struct Config { pub rust_optimize: bool, pub rust_codegen_units: Option, pub rust_codegen_units_std: Option, + pub rust_codegen_units_fast: bool, pub rust_debug_assertions: bool, pub rust_debug_assertions_std: bool, pub rust_overflow_checks: bool, @@ -717,6 +718,7 @@ define_config! { debug: Option = "debug", codegen_units: Option = "codegen-units", codegen_units_std: Option = "codegen-units-std", + codegen_units_fast: Option = "codegen-units-fast", debug_assertions: Option = "debug-assertions", debug_assertions_std: Option = "debug-assertions-std", overflow_checks: Option = "overflow-checks", @@ -1130,6 +1132,7 @@ impl Config { config.rust_codegen_units = rust.codegen_units.map(threads_from_config); config.rust_codegen_units_std = rust.codegen_units_std.map(threads_from_config); + set(&mut config.rust_codegen_units_fast, rust.codegen_units_fast); config.rust_profile_use = flags.rust_profile_use.or(rust.profile_use); config.rust_profile_generate = flags.rust_profile_generate.or(rust.profile_generate); config.download_rustc_commit = config.download_ci_rustc_commit(rust.download_rustc); diff --git a/src/ci/run.sh b/src/ci/run.sh index 0db9c993eecb4..f6c3b2b3eb902 100755 --- a/src/ci/run.sh +++ b/src/ci/run.sh @@ -56,6 +56,7 @@ RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --disable-manage-submodules" RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-locked-deps" RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-cargo-native-static" RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.codegen-units-std=1" +RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.codegen-units-fast=true" # Only produce xz tarballs on CI. gz tarballs will be generated by the release # process by recompressing the existing xz ones. This decreases the storage