Skip to content

Commit b3fd7ad

Browse files
committed
Add an option to tune compiler crates' CGUs to bootstrap
1 parent f55b002 commit b3fd7ad

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

config.toml.example

+6
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,12 @@ changelog-seen = 2
427427
# Uses the rustc defaults: https://doc.rust-lang.org/rustc/codegen-options/index.html#codegen-units
428428
#codegen-units = if incremental { 256 } else { 16 }
429429

430+
# Tweaks the numbers of codegen units to improve compile time, typically by
431+
# using a single codegen unit for crates. The `codegen-units` configuration is
432+
# preferred over this field. This is recommended to set for release builds and CI,
433+
# but not for development as it can slow down partial rebuilds.
434+
#codegen-units-fast = false
435+
430436
# Sets the number of codegen units to build the standard library with,
431437
# regardless of what the codegen-unit setting for the rest of the compiler is.
432438
# NOTE: building with anything other than 1 is known to occasionally have bugs.

src/bootstrap/bin/rustc.rs

+23
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ fn main() {
7070
cmd.arg("-Ztime-passes");
7171
}
7272
}
73+
74+
if let Some(profile) = env::var_os("RUSTC_TUNE_COMPILER_CODEGEN_UNITS") {
75+
let cgus = match crate_name {
76+
// This crate is large and at the tail of the compiler crates, give it extra CGUs.
77+
"rustc_query_impl" => Some(96),
78+
// This compiles after all other crates so give it default CGUs to speed it up.
79+
"rustc_driver" => None,
80+
_ => {
81+
if profile == "fast" {
82+
Some(1)
83+
} else {
84+
if crate_name.starts_with("rustc_") {
85+
None
86+
} else {
87+
// Compile crates.io crates with a single CGU for faster compile times
88+
Some(1)
89+
}
90+
}
91+
}
92+
};
93+
94+
cgus.map(|cgus| cmd.arg(&format!("-Ccodegen-units={}", cgus)));
95+
}
7396
}
7497

7598
// Print backtrace in case of ICE

src/bootstrap/builder.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,12 @@ impl<'a> Builder<'a> {
18841884
(Mode::Std, Some(n), _) | (_, _, Some(n)) => {
18851885
cargo.env(profile_var("CODEGEN_UNITS"), n.to_string());
18861886
}
1887+
(Mode::Rustc, _, _) => {
1888+
cargo.env(
1889+
"RUSTC_TUNE_COMPILER_CODEGEN_UNITS",
1890+
if self.config.rust_codegen_units_fast { "fast" } else { "1" },
1891+
);
1892+
}
18871893
_ => {
18881894
// Don't set anything
18891895
}

src/bootstrap/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ pub struct Config {
145145
pub rust_optimize: bool,
146146
pub rust_codegen_units: Option<u32>,
147147
pub rust_codegen_units_std: Option<u32>,
148+
pub rust_codegen_units_fast: bool,
148149
pub rust_debug_assertions: bool,
149150
pub rust_debug_assertions_std: bool,
150151
pub rust_overflow_checks: bool,
@@ -717,6 +718,7 @@ define_config! {
717718
debug: Option<bool> = "debug",
718719
codegen_units: Option<u32> = "codegen-units",
719720
codegen_units_std: Option<u32> = "codegen-units-std",
721+
codegen_units_fast: Option<bool> = "codegen-units-fast",
720722
debug_assertions: Option<bool> = "debug-assertions",
721723
debug_assertions_std: Option<bool> = "debug-assertions-std",
722724
overflow_checks: Option<bool> = "overflow-checks",
@@ -1130,6 +1132,7 @@ impl Config {
11301132

11311133
config.rust_codegen_units = rust.codegen_units.map(threads_from_config);
11321134
config.rust_codegen_units_std = rust.codegen_units_std.map(threads_from_config);
1135+
set(&mut config.rust_codegen_units_fast, rust.codegen_units_fast);
11331136
config.rust_profile_use = flags.rust_profile_use.or(rust.profile_use);
11341137
config.rust_profile_generate = flags.rust_profile_generate.or(rust.profile_generate);
11351138
config.download_rustc_commit = config.download_ci_rustc_commit(rust.download_rustc);

src/ci/run.sh

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --disable-manage-submodules"
5656
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-locked-deps"
5757
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-cargo-native-static"
5858
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.codegen-units-std=1"
59+
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --set rust.codegen-units-fast=true"
5960

6061
# Only produce xz tarballs on CI. gz tarballs will be generated by the release
6162
# process by recompressing the existing xz ones. This decreases the storage

0 commit comments

Comments
 (0)