From b3fd7adc74f54961fcccf86d7d893fb8706e8fdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Wed, 1 Feb 2023 07:12:45 +0100 Subject: [PATCH 1/2] Add an option to tune compiler crates' CGUs to bootstrap --- config.toml.example | 6 ++++++ src/bootstrap/bin/rustc.rs | 23 +++++++++++++++++++++++ src/bootstrap/builder.rs | 6 ++++++ src/bootstrap/config.rs | 3 +++ src/ci/run.sh | 1 + 5 files changed, 39 insertions(+) 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..199fba3713f84 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..a37aed5b43b6a 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1884,6 +1884,12 @@ 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", + if self.config.rust_codegen_units_fast { "fast" } else { "1" }, + ); + } _ => { // 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 From 0c55b048347df8b682b83da8fcbbe9b147a03a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Wed, 1 Feb 2023 20:00:48 +0100 Subject: [PATCH 2/2] WIP performance test --- src/bootstrap/bin/rustc.rs | 2 +- src/bootstrap/builder.rs | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index 199fba3713f84..79a115598994b 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -74,7 +74,7 @@ fn main() { 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), + //"rustc_query_impl" => Some(96), // This compiles after all other crates so give it default CGUs to speed it up. "rustc_driver" => None, _ => { diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index a37aed5b43b6a..16ca1966823c3 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1885,10 +1885,7 @@ impl<'a> Builder<'a> { cargo.env(profile_var("CODEGEN_UNITS"), n.to_string()); } (Mode::Rustc, _, _) => { - cargo.env( - "RUSTC_TUNE_COMPILER_CODEGEN_UNITS", - if self.config.rust_codegen_units_fast { "fast" } else { "1" }, - ); + cargo.env("RUSTC_TUNE_COMPILER_CODEGEN_UNITS", "fast"); } _ => { // Don't set anything