Skip to content

Commit f6e058c

Browse files
committed
Auto merge of rust-lang#129295 - Zalathar:profiler-builtins, r=Kobzol
Build `library/profiler_builtins` from `ci-llvm` if appropriate Running all of `tests/coverage` requires the LLVM profiler runtime, which requires setting `build.profiler = true`. Historically, doing that has required checking out the entire `src/llvm-project` submodule. For compiler contributors who otherwise don't need that submodule (thanks to `download-ci-vm`), that's quite inconvenient. However, thanks to rust-lang#129116, the downloaded CI LLVM tarball now contains a copy of LLVM's `compiler-rt` directory, which includes all the files needed to build the profiler runtime. So with a little bit of extra logic in bootstrap, we can have `library/profiler_builtins` look for the `compiler-rt` files in `ci-llvm` instead of the `src/llvm-project` submodule.
2 parents f167efa + 94aadf0 commit f6e058c

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

library/profiler_builtins/build.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! See the build.rs for libcompiler_builtins crate for details.
44
55
use std::env;
6-
use std::path::Path;
6+
use std::path::PathBuf;
77

88
fn main() {
99
println!("cargo:rerun-if-env-changed=LLVM_PROFILER_RT_LIB");
@@ -79,17 +79,25 @@ fn main() {
7979
cfg.define("COMPILER_RT_HAS_ATOMICS", Some("1"));
8080
}
8181

82-
// Note that this should exist if we're going to run (otherwise we just
83-
// don't build profiler builtins at all).
84-
let root = Path::new("../../src/llvm-project/compiler-rt");
82+
// Get the LLVM `compiler-rt` directory from bootstrap.
83+
println!("cargo:rerun-if-env-changed=RUST_COMPILER_RT_FOR_PROFILER");
84+
let root = PathBuf::from(env::var("RUST_COMPILER_RT_FOR_PROFILER").unwrap_or_else(|_| {
85+
let path = "../../src/llvm-project/compiler-rt";
86+
println!("RUST_COMPILER_RT_FOR_PROFILER was not set; falling back to {path:?}");
87+
path.to_owned()
88+
}));
8589

8690
let src_root = root.join("lib").join("profile");
91+
assert!(src_root.exists(), "profiler runtime source directory not found: {src_root:?}");
92+
let mut n_sources_found = 0u32;
8793
for src in profile_sources {
8894
let path = src_root.join(src);
8995
if path.exists() {
9096
cfg.file(path);
97+
n_sources_found += 1;
9198
}
9299
}
100+
assert!(n_sources_found > 0, "couldn't find any profiler runtime source files in {src_root:?}");
93101

94102
cfg.include(root.join("include"));
95103
cfg.warnings(false);

src/bootstrap/src/core/build_steps/compile.rs

+32-12
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,6 @@ impl Step for Std {
199199

200200
builder.require_submodule("library/stdarch", None);
201201

202-
// Profiler information requires LLVM's compiler-rt
203-
if builder.config.profiler {
204-
builder.require_submodule(
205-
"src/llvm-project",
206-
Some(
207-
"The `build.profiler` config option requires `compiler-rt` sources from LLVM.",
208-
),
209-
);
210-
}
211-
212202
let mut target_deps = builder.ensure(StartupObjects { compiler, target });
213203

214204
let compiler_to_use = builder.compiler_for(compiler.stage, compiler.host, target);
@@ -466,15 +456,45 @@ pub fn std_crates_for_run_make(run: &RunConfig<'_>) -> Vec<String> {
466456
}
467457
}
468458

459+
/// Tries to find LLVM's `compiler-rt` source directory, for building `library/profiler_builtins`.
460+
///
461+
/// Normally it lives in the `src/llvm-project` submodule, but if we will be using a
462+
/// downloaded copy of CI LLVM, then we try to use the `compiler-rt` sources from
463+
/// there instead, which lets us avoid checking out the LLVM submodule.
464+
fn compiler_rt_for_profiler(builder: &Builder<'_>) -> PathBuf {
465+
// Try to use `compiler-rt` sources from downloaded CI LLVM, if possible.
466+
if builder.config.llvm_from_ci {
467+
// CI LLVM might not have been downloaded yet, so try to download it now.
468+
builder.config.maybe_download_ci_llvm();
469+
let ci_llvm_compiler_rt = builder.config.ci_llvm_root().join("compiler-rt");
470+
if ci_llvm_compiler_rt.exists() {
471+
return ci_llvm_compiler_rt;
472+
}
473+
}
474+
475+
// Otherwise, fall back to requiring the LLVM submodule.
476+
builder.require_submodule("src/llvm-project", {
477+
Some("The `build.profiler` config option requires `compiler-rt` sources from LLVM.")
478+
});
479+
builder.src.join("src/llvm-project/compiler-rt")
480+
}
481+
469482
/// Configure cargo to compile the standard library, adding appropriate env vars
470483
/// and such.
471484
pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, cargo: &mut Cargo) {
472485
if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
473486
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
474487
}
475488

489+
// Paths needed by `library/profiler_builtins/build.rs`.
476490
if let Some(path) = builder.config.profiler_path(target) {
477491
cargo.env("LLVM_PROFILER_RT_LIB", path);
492+
} else if builder.config.profiler_enabled(target) {
493+
let compiler_rt = compiler_rt_for_profiler(builder);
494+
// Currently this is separate from the env var used by `compiler_builtins`
495+
// (below) so that adding support for CI LLVM here doesn't risk breaking
496+
// the compiler builtins. But they could be unified if desired.
497+
cargo.env("RUST_COMPILER_RT_FOR_PROFILER", compiler_rt);
478498
}
479499

480500
// Determine if we're going to compile in optimized C intrinsics to
@@ -507,8 +527,8 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
507527
);
508528
let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt");
509529
assert!(compiler_builtins_root.exists());
510-
// Note that `libprofiler_builtins/build.rs` also computes this so if
511-
// you're changing something here please also change that.
530+
// The path to `compiler-rt` is also used by `profiler_builtins` (above),
531+
// so if you're changing something here please also change that as appropriate.
512532
cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root);
513533
" compiler-builtins-c"
514534
} else {

src/bootstrap/src/utils/change_tracker.rs

+5
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
230230
severity: ChangeSeverity::Warning,
231231
summary: "./x test --rustc-args was renamed to --compiletest-rustc-args as it only applies there. ./x miri --rustc-args was also removed.",
232232
},
233+
ChangeInfo {
234+
change_id: 129295,
235+
severity: ChangeSeverity::Info,
236+
summary: "The `build.profiler` option now tries to use source code from `download-ci-llvm` if possible, instead of checking out the `src/llvm-project` submodule.",
237+
},
233238
];

0 commit comments

Comments
 (0)