Skip to content

bootstrap: Don't trigger an unnecessary LLVM build from check builds #144011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl Step for CodegenBackend {
cargo
.arg("--manifest-path")
.arg(builder.src.join(format!("compiler/rustc_codegen_{backend}/Cargo.toml")));
rustc_cargo_env(builder, &mut cargo, target, build_compiler.stage);
rustc_cargo_env(builder, &mut cargo, target);

let _guard = builder.msg_check(format!("rustc_codegen_{backend}"), target, None);

Expand Down
38 changes: 21 additions & 17 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,15 +1316,10 @@ pub fn rustc_cargo(
cargo.env("RUSTC_WRAPPER", ccache);
}

rustc_cargo_env(builder, cargo, target, build_compiler.stage);
rustc_cargo_env(builder, cargo, target);
}

pub fn rustc_cargo_env(
builder: &Builder<'_>,
cargo: &mut Cargo,
target: TargetSelection,
build_stage: u32,
) {
pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
// Set some configuration variables picked up by build scripts and
// the compiler alike
cargo
Expand Down Expand Up @@ -1379,18 +1374,24 @@ pub fn rustc_cargo_env(
cargo.rustflag("--cfg=llvm_enzyme");
}

// Note that this is disabled if LLVM itself is disabled or we're in a check
// build. If we are in a check build we still go ahead here presuming we've
// detected that LLVM is already built and good to go which helps prevent
// busting caches (e.g. like #71152).
// These conditionals represent a tension between three forces:
// - For non-check builds, we need to define some LLVM-related environment
// variables, requiring LLVM to have been built.
// - For check builds, we want to avoid building LLVM if possible.
// - Check builds and non-check builds should have the same environment if
// possible, to avoid unnecessary rebuilds due to cache-busting.
//
// Therefore we try to avoid building LLVM for check builds, but only if
// building LLVM would be expensive. If "building" LLVM is cheap
// (i.e. it's already built or is downloadable), we prefer to maintain a
// consistent environment between check and non-check builds.
if builder.config.llvm_enabled(target) {
let building_is_expensive =
let building_llvm_is_expensive =
crate::core::build_steps::llvm::prebuilt_llvm_config(builder, target, false)
.should_build();
// `top_stage == stage` might be false for `check --stage 1`, if we are building the stage 1 compiler
let can_skip_build = builder.kind == Kind::Check && builder.top_stage == build_stage;
let should_skip_build = building_is_expensive && can_skip_build;
if !should_skip_build {

let skip_llvm = (builder.kind == Kind::Check) && building_llvm_is_expensive;
if !skip_llvm {
rustc_llvm_env(builder, cargo, target)
}
}
Expand All @@ -1407,6 +1408,9 @@ pub fn rustc_cargo_env(

/// Pass down configuration from the LLVM build into the build of
/// rustc_llvm and rustc_codegen_llvm.
///
/// Note that this has the side-effect of _building LLVM_, which is sometimes
/// unwanted (e.g. for check builds).
fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
if builder.config.is_rust_llvm(target) {
cargo.env("LLVM_RUSTLLVM", "1");
Expand Down Expand Up @@ -1665,7 +1669,7 @@ impl Step for CodegenBackend {
cargo
.arg("--manifest-path")
.arg(builder.src.join(format!("compiler/rustc_codegen_{backend}/Cargo.toml")));
rustc_cargo_env(builder, &mut cargo, target, compiler.stage);
rustc_cargo_env(builder, &mut cargo, target);

// Ideally, we'd have a separate step for the individual codegen backends,
// like we have in tests (test::CodegenGCC) but that would require a lot of restructuring.
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3386,7 +3386,7 @@ impl Step for CodegenCranelift {
cargo
.arg("--manifest-path")
.arg(builder.src.join("compiler/rustc_codegen_cranelift/build_system/Cargo.toml"));
compile::rustc_cargo_env(builder, &mut cargo, target, compiler.stage);
compile::rustc_cargo_env(builder, &mut cargo, target);

// Avoid incremental cache issues when changing rustc
cargo.env("CARGO_BUILD_INCREMENTAL", "false");
Expand Down Expand Up @@ -3518,7 +3518,7 @@ impl Step for CodegenGCC {
cargo
.arg("--manifest-path")
.arg(builder.src.join("compiler/rustc_codegen_gcc/build_system/Cargo.toml"));
compile::rustc_cargo_env(builder, &mut cargo, target, compiler.stage);
compile::rustc_cargo_env(builder, &mut cargo, target);
add_cg_gcc_cargo_flags(&mut cargo, &gcc);

// Avoid incremental cache issues when changing rustc
Expand Down
16 changes: 8 additions & 8 deletions src/bootstrap/src/core/builder/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,11 @@ mod snapshot {
[build] llvm <host>
[build] rustc 0 <host> -> rustc 1 <host>
");
}

#[test]
fn build_rustc_no_explicit_stage() {
let ctx = TestCtx::new();
insta::assert_snapshot!(
ctx.config("build")
.path("rustc")
Expand Down Expand Up @@ -1294,17 +1298,19 @@ mod snapshot {
ctx.config("check")
.path("compiler")
.render_steps(), @r"
[build] llvm <host>
[check] rustc 0 <host> -> rustc 1 <host>
[check] rustc 0 <host> -> cranelift 1 <host>
[check] rustc 0 <host> -> gcc 1 <host>
");
}

#[test]
fn check_rustc_no_explicit_stage() {
let ctx = TestCtx::new();
insta::assert_snapshot!(
ctx.config("check")
.path("rustc")
.render_steps(), @r"
[build] llvm <host>
[check] rustc 0 <host> -> rustc 1 <host>
");
}
Expand All @@ -1324,7 +1330,6 @@ mod snapshot {
.path("compiler")
.stage(1)
.render_steps(), @r"
[build] llvm <host>
[check] rustc 0 <host> -> rustc 1 <host>
[check] rustc 0 <host> -> cranelift 1 <host>
[check] rustc 0 <host> -> gcc 1 <host>
Expand Down Expand Up @@ -1456,7 +1461,6 @@ mod snapshot {
.paths(&["library", "compiler"])
.args(&args)
.render_steps(), @r"
[build] llvm <host>
[check] rustc 0 <host> -> rustc 1 <host>
[check] rustc 0 <host> -> cranelift 1 <host>
[check] rustc 0 <host> -> gcc 1 <host>
Expand All @@ -1470,7 +1474,6 @@ mod snapshot {
ctx.config("check")
.path("miri")
.render_steps(), @r"
[build] llvm <host>
[check] rustc 0 <host> -> rustc 1 <host>
[check] rustc 0 <host> -> Miri 1 <host>
");
Expand All @@ -1491,7 +1494,6 @@ mod snapshot {
.path("miri")
.stage(1)
.render_steps(), @r"
[build] llvm <host>
[check] rustc 0 <host> -> rustc 1 <host>
[check] rustc 0 <host> -> Miri 1 <host>
");
Expand Down Expand Up @@ -1544,7 +1546,6 @@ mod snapshot {
ctx.config("check")
.path("rustc_codegen_cranelift")
.render_steps(), @r"
[build] llvm <host>
[check] rustc 0 <host> -> rustc 1 <host>
[check] rustc 0 <host> -> cranelift 1 <host>
[check] rustc 0 <host> -> gcc 1 <host>
Expand All @@ -1558,7 +1559,6 @@ mod snapshot {
ctx.config("check")
.path("rust-analyzer")
.render_steps(), @r"
[build] llvm <host>
[check] rustc 0 <host> -> rustc 1 <host>
[check] rustc 0 <host> -> rust-analyzer 1 <host>
");
Expand Down
Loading