Skip to content

Commit 51c6826

Browse files
Rollup merge of rust-lang#137080 - jieyouxu:more-tracing, r=onur-ozkan
bootstrap: add more tracing to compiler/std/llvm flows - Add more tracing to compiler/std/llvm flows. - Two drive-by nits: 1. Take `TargetSelection` by-value for `builder.is_builder_target()`. Noticed while adding tracing; follow-up to rust-lang#136767. 2. Coalesce enzyme build logic into one branch. - Document `COMPILER{,_FOR}` tracing targets for rust-lang#96176. - No functional changes. ### Testing You can play with the tracing locally with: ``` $ BOOTSTRAP_TRACING=bootstrap=debug ./x build library $ BOOTSTRAP_TRACING=bootstrap=trace ./x build library $ BOOTSTRAP_TRACING=bootstrap=trace,COMPILER=trace,COMPILER_FOR=trace ./x build library ``` ### Previews ``` $ BOOTSTRAP_TRACING=bootstrap=debug ./x build library ``` ![Screenshot 2025-02-15 230824](https://github.com/user-attachments/assets/c3b02b62-d52e-4c03-a00a-da0d95618989) ``` $ BOOTSTRAP_TRACING=bootstrap=trace,COMPILER=trace,COMPILER_FOR=trace ./x build library ``` ![Screenshot 2025-02-15 233859](https://github.com/user-attachments/assets/842e4ece-4c26-4191-acbb-5f93e42de4dc) r? `@onur-ozkan` (or reroll)
2 parents 54fa2f8 + 05ba1a4 commit 51c6826

File tree

11 files changed

+340
-31
lines changed

11 files changed

+340
-31
lines changed

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

+181-11
Large diffs are not rendered by default.

src/bootstrap/src/core/build_steps/dist.rs

+43-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use std::{env, fs};
1616

1717
use object::BinaryFormat;
1818
use object::read::archive::ArchiveFile;
19+
#[cfg(feature = "tracing")]
20+
use tracing::instrument;
1921

2022
use crate::core::build_steps::doc::DocumentationFormat;
2123
use crate::core::build_steps::tool::{self, Tool};
@@ -30,7 +32,7 @@ use crate::utils::helpers::{
3032
exe, is_dylib, move_file, t, target_supports_cranelift_backend, timeit,
3133
};
3234
use crate::utils::tarball::{GeneratedTarball, OverlayKind, Tarball};
33-
use crate::{Compiler, DependencyType, LLVM_TOOLS, Mode};
35+
use crate::{Compiler, DependencyType, LLVM_TOOLS, Mode, trace};
3436

3537
pub fn pkgname(builder: &Builder<'_>, component: &str) -> String {
3638
format!("{}-{}", component, builder.rust_package_vers())
@@ -582,7 +584,7 @@ impl Step for DebuggerScripts {
582584
fn skip_host_target_lib(builder: &Builder<'_>, compiler: Compiler) -> bool {
583585
// The only true set of target libraries came from the build triple, so
584586
// let's reduce redundant work by only producing archives from that host.
585-
if !builder.is_builder_target(&compiler.host) {
587+
if !builder.is_builder_target(compiler.host) {
586588
builder.info("\tskipping, not a build host");
587589
true
588590
} else {
@@ -637,7 +639,7 @@ fn copy_target_libs(
637639
for (path, dependency_type) in builder.read_stamp_file(stamp) {
638640
if dependency_type == DependencyType::TargetSelfContained {
639641
builder.copy_link(&path, &self_contained_dst.join(path.file_name().unwrap()));
640-
} else if dependency_type == DependencyType::Target || builder.is_builder_target(&target) {
642+
} else if dependency_type == DependencyType::Target || builder.is_builder_target(target) {
641643
builder.copy_link(&path, &dst.join(path.file_name().unwrap()));
642644
}
643645
}
@@ -786,7 +788,7 @@ impl Step for Analysis {
786788
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
787789
let compiler = self.compiler;
788790
let target = self.target;
789-
if !builder.is_builder_target(&compiler.host) {
791+
if !builder.is_builder_target(compiler.host) {
790792
return None;
791793
}
792794

@@ -2029,6 +2031,15 @@ fn install_llvm_file(
20292031
/// Maybe add LLVM object files to the given destination lib-dir. Allows either static or dynamic linking.
20302032
///
20312033
/// Returns whether the files were actually copied.
2034+
#[cfg_attr(
2035+
feature = "tracing",
2036+
instrument(
2037+
level = "trace",
2038+
name = "maybe_install_llvm",
2039+
skip_all,
2040+
fields(target = ?target, dst_libdir = ?dst_libdir, install_symlink = install_symlink),
2041+
),
2042+
)]
20322043
fn maybe_install_llvm(
20332044
builder: &Builder<'_>,
20342045
target: TargetSelection,
@@ -2052,6 +2063,7 @@ fn maybe_install_llvm(
20522063
// If the LLVM is coming from ourselves (just from CI) though, we
20532064
// still want to install it, as it otherwise won't be available.
20542065
if builder.is_system_llvm(target) {
2066+
trace!("system LLVM requested, no install");
20552067
return false;
20562068
}
20572069

@@ -2070,6 +2082,7 @@ fn maybe_install_llvm(
20702082
} else if let llvm::LlvmBuildStatus::AlreadyBuilt(llvm::LlvmResult { llvm_config, .. }) =
20712083
llvm::prebuilt_llvm_config(builder, target, true)
20722084
{
2085+
trace!("LLVM already built, installing LLVM files");
20732086
let mut cmd = command(llvm_config);
20742087
cmd.arg("--libfiles");
20752088
builder.verbose(|| println!("running {cmd:?}"));
@@ -2092,6 +2105,19 @@ fn maybe_install_llvm(
20922105
}
20932106

20942107
/// Maybe add libLLVM.so to the target lib-dir for linking.
2108+
#[cfg_attr(
2109+
feature = "tracing",
2110+
instrument(
2111+
level = "trace",
2112+
name = "maybe_install_llvm_target",
2113+
skip_all,
2114+
fields(
2115+
llvm_link_shared = ?builder.llvm_link_shared(),
2116+
target = ?target,
2117+
sysroot = ?sysroot,
2118+
),
2119+
),
2120+
)]
20952121
pub fn maybe_install_llvm_target(builder: &Builder<'_>, target: TargetSelection, sysroot: &Path) {
20962122
let dst_libdir = sysroot.join("lib/rustlib").join(target).join("lib");
20972123
// We do not need to copy LLVM files into the sysroot if it is not
@@ -2103,6 +2129,19 @@ pub fn maybe_install_llvm_target(builder: &Builder<'_>, target: TargetSelection,
21032129
}
21042130

21052131
/// Maybe add libLLVM.so to the runtime lib-dir for rustc itself.
2132+
#[cfg_attr(
2133+
feature = "tracing",
2134+
instrument(
2135+
level = "trace",
2136+
name = "maybe_install_llvm_runtime",
2137+
skip_all,
2138+
fields(
2139+
llvm_link_shared = ?builder.llvm_link_shared(),
2140+
target = ?target,
2141+
sysroot = ?sysroot,
2142+
),
2143+
),
2144+
)]
21062145
pub fn maybe_install_llvm_runtime(builder: &Builder<'_>, target: TargetSelection, sysroot: &Path) {
21072146
let dst_libdir =
21082147
sysroot.join(builder.sysroot_libdir_relative(Compiler { stage: 1, host: target }));

src/bootstrap/src/core/build_steps/llvm.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use std::{env, fs};
1616

1717
use build_helper::ci::CiEnv;
1818
use build_helper::git::get_closest_merge_commit;
19+
#[cfg(feature = "tracing")]
20+
use tracing::instrument;
1921

2022
use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
2123
use crate::core::config::{Config, TargetSelection};
@@ -24,7 +26,7 @@ use crate::utils::exec::command;
2426
use crate::utils::helpers::{
2527
self, exe, get_clang_cl_resource_dir, t, unhashed_basename, up_to_date,
2628
};
27-
use crate::{CLang, GitRepo, Kind};
29+
use crate::{CLang, GitRepo, Kind, trace};
2830

2931
#[derive(Clone)]
3032
pub struct LlvmResult {
@@ -516,7 +518,7 @@ impl Step for Llvm {
516518
}
517519

518520
// https://llvm.org/docs/HowToCrossCompileLLVM.html
519-
if !builder.is_builder_target(&target) {
521+
if !builder.is_builder_target(target) {
520522
let LlvmResult { llvm_config, .. } =
521523
builder.ensure(Llvm { target: builder.config.build });
522524
if !builder.config.dry_run() {
@@ -668,7 +670,7 @@ fn configure_cmake(
668670
}
669671
cfg.target(&target.triple).host(&builder.config.build.triple);
670672

671-
if !builder.is_builder_target(&target) {
673+
if !builder.is_builder_target(target) {
672674
cfg.define("CMAKE_CROSSCOMPILING", "True");
673675

674676
if target.contains("netbsd") {
@@ -934,6 +936,15 @@ impl Step for Enzyme {
934936
}
935937

936938
/// Compile Enzyme for `target`.
939+
#[cfg_attr(
940+
feature = "tracing",
941+
instrument(
942+
level = "debug",
943+
name = "Enzyme::run",
944+
skip_all,
945+
fields(target = ?self.target),
946+
),
947+
)]
937948
fn run(self, builder: &Builder<'_>) -> PathBuf {
938949
builder.require_submodule(
939950
"src/tools/enzyme",
@@ -959,7 +970,9 @@ impl Step for Enzyme {
959970
let out_dir = builder.enzyme_out(target);
960971
let stamp = BuildStamp::new(&out_dir).with_prefix("enzyme").add_stamp(smart_stamp_hash);
961972

973+
trace!("checking build stamp to see if we need to rebuild enzyme artifacts");
962974
if stamp.is_up_to_date() {
975+
trace!(?out_dir, "enzyme build artifacts are up to date");
963976
if stamp.stamp().is_empty() {
964977
builder.info(
965978
"Could not determine the Enzyme submodule commit hash. \
@@ -973,6 +986,7 @@ impl Step for Enzyme {
973986
return out_dir;
974987
}
975988

989+
trace!(?target, "(re)building enzyme artifacts");
976990
builder.info(&format!("Building Enzyme for {}", target));
977991
t!(stamp.remove());
978992
let _time = helpers::timeit(builder);
@@ -994,6 +1008,7 @@ impl Step for Enzyme {
9941008
(true, false) => "Release",
9951009
(true, true) => "RelWithDebInfo",
9961010
};
1011+
trace!(?profile);
9971012

9981013
cfg.out_dir(&out_dir)
9991014
.profile(profile)
@@ -1118,7 +1133,7 @@ impl Step for Lld {
11181133
.define("LLVM_CMAKE_DIR", llvm_cmake_dir)
11191134
.define("LLVM_INCLUDE_TESTS", "OFF");
11201135

1121-
if !builder.is_builder_target(&target) {
1136+
if !builder.is_builder_target(target) {
11221137
// Use the host llvm-tblgen binary.
11231138
cfg.define(
11241139
"LLVM_TABLEGEN_EXE",

src/bootstrap/src/core/build_steps/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2743,7 +2743,7 @@ impl Step for Crate {
27432743
cargo
27442744
} else {
27452745
// Also prepare a sysroot for the target.
2746-
if !builder.is_builder_target(&target) {
2746+
if !builder.is_builder_target(target) {
27472747
builder.ensure(compile::Std::new(compiler, target).force_recompile(true));
27482748
builder.ensure(RemoteCopyLibs { compiler, target });
27492749
}

src/bootstrap/src/core/build_steps/tool.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use std::path::PathBuf;
22
use std::{env, fs};
33

4+
#[cfg(feature = "tracing")]
5+
use tracing::instrument;
6+
47
use crate::core::build_steps::compile::is_lto_stage;
58
use crate::core::build_steps::toolstate::ToolState;
69
use crate::core::build_steps::{compile, llvm};
@@ -304,6 +307,14 @@ macro_rules! bootstrap_tool {
304307
});
305308
}
306309

310+
#[cfg_attr(
311+
feature = "tracing",
312+
instrument(
313+
level = "debug",
314+
name = $tool_name,
315+
skip_all,
316+
),
317+
)]
307318
fn run(self, builder: &Builder<'_>) -> PathBuf {
308319
$(
309320
for submodule in $submodules {
@@ -758,6 +769,15 @@ impl Step for LldWrapper {
758769
run.never()
759770
}
760771

772+
#[cfg_attr(
773+
feature = "tracing",
774+
instrument(
775+
level = "debug",
776+
name = "LldWrapper::run",
777+
skip_all,
778+
fields(build_compiler = ?self.build_compiler, target_compiler = ?self.target_compiler),
779+
),
780+
)]
761781
fn run(self, builder: &Builder<'_>) {
762782
if builder.config.dry_run() {
763783
return;
@@ -914,6 +934,10 @@ impl Step for LlvmBitcodeLinker {
914934
});
915935
}
916936

937+
#[cfg_attr(
938+
feature = "tracing",
939+
instrument(level = "debug", name = "LlvmBitcodeLinker::run", skip_all)
940+
)]
917941
fn run(self, builder: &Builder<'_>) -> PathBuf {
918942
let bin_name = "llvm-bitcode-linker";
919943

src/bootstrap/src/core/builder/mod.rs

+38-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use std::time::{Duration, Instant};
1010
use std::{env, fs};
1111

1212
use clap::ValueEnum;
13+
#[cfg(feature = "tracing")]
14+
use tracing::instrument;
1315

1416
pub use self::cargo::{Cargo, cargo_profile_var};
1517
pub use crate::Compiler;
@@ -21,7 +23,7 @@ use crate::core::config::{DryRun, TargetSelection};
2123
use crate::utils::cache::Cache;
2224
use crate::utils::exec::{BootstrapCommand, command};
2325
use crate::utils::helpers::{self, LldThreads, add_dylib_path, exe, libdir, linker_args, t};
24-
use crate::{Build, Crate};
26+
use crate::{Build, Crate, trace};
2527

2628
mod cargo;
2729

@@ -1214,6 +1216,19 @@ impl<'a> Builder<'a> {
12141216
/// compiler will run on, *not* the target it will build code for). Explicitly does not take
12151217
/// `Compiler` since all `Compiler` instances are meant to be obtained through this function,
12161218
/// since it ensures that they are valid (i.e., built and assembled).
1219+
#[cfg_attr(
1220+
feature = "tracing",
1221+
instrument(
1222+
level = "trace",
1223+
name = "Builder::compiler",
1224+
target = "COMPILER",
1225+
skip_all,
1226+
fields(
1227+
stage = stage,
1228+
host = ?host,
1229+
),
1230+
),
1231+
)]
12171232
pub fn compiler(&self, stage: u32, host: TargetSelection) -> Compiler {
12181233
self.ensure(compile::Assemble { target_compiler: Compiler { stage, host } })
12191234
}
@@ -1229,19 +1244,39 @@ impl<'a> Builder<'a> {
12291244
/// sysroot.
12301245
///
12311246
/// See `force_use_stage1` and `force_use_stage2` for documentation on what each argument is.
1247+
#[cfg_attr(
1248+
feature = "tracing",
1249+
instrument(
1250+
level = "trace",
1251+
name = "Builder::compiler_for",
1252+
target = "COMPILER_FOR",
1253+
skip_all,
1254+
fields(
1255+
stage = stage,
1256+
host = ?host,
1257+
target = ?target,
1258+
),
1259+
),
1260+
)]
12321261
pub fn compiler_for(
12331262
&self,
12341263
stage: u32,
12351264
host: TargetSelection,
12361265
target: TargetSelection,
12371266
) -> Compiler {
1238-
if self.build.force_use_stage2(stage) {
1267+
#![allow(clippy::let_and_return)]
1268+
let resolved_compiler = if self.build.force_use_stage2(stage) {
1269+
trace!(target: "COMPILER_FOR", ?stage, "force_use_stage2");
12391270
self.compiler(2, self.config.build)
12401271
} else if self.build.force_use_stage1(stage, target) {
1272+
trace!(target: "COMPILER_FOR", ?stage, "force_use_stage1");
12411273
self.compiler(1, self.config.build)
12421274
} else {
1275+
trace!(target: "COMPILER_FOR", ?stage, ?host, "no force, fallback to `compiler()`");
12431276
self.compiler(stage, host)
1244-
}
1277+
};
1278+
trace!(target: "COMPILER_FOR", ?resolved_compiler);
1279+
resolved_compiler
12451280
}
12461281

12471282
pub fn sysroot(&self, compiler: Compiler) -> PathBuf {

src/bootstrap/src/core/builder/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ fn test_is_builder_target() {
10801080
let build = Build::new(config);
10811081
let builder = Builder::new(&build);
10821082

1083-
assert!(builder.is_builder_target(&target1));
1084-
assert!(!builder.is_builder_target(&target2));
1083+
assert!(builder.is_builder_target(target1));
1084+
assert!(!builder.is_builder_target(target2));
10851085
}
10861086
}

src/bootstrap/src/core/config/config.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2747,6 +2747,15 @@ impl Config {
27472747
/// tarball). Typically [`crate::Build::require_submodule`] should be
27482748
/// used instead to provide a nice error to the user if the submodule is
27492749
/// missing.
2750+
#[cfg_attr(
2751+
feature = "tracing",
2752+
instrument(
2753+
level = "trace",
2754+
name = "Config::update_submodule",
2755+
skip_all,
2756+
fields(relative_path = ?relative_path),
2757+
),
2758+
)]
27502759
pub(crate) fn update_submodule(&self, relative_path: &str) {
27512760
if !self.submodules() {
27522761
return;

src/bootstrap/src/core/sanity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ than building it.
329329
if target.contains("musl") && !target.contains("unikraft") {
330330
// If this is a native target (host is also musl) and no musl-root is given,
331331
// fall back to the system toolchain in /usr before giving up
332-
if build.musl_root(*target).is_none() && build.is_builder_target(target) {
332+
if build.musl_root(*target).is_none() && build.is_builder_target(*target) {
333333
let target = build.config.target_config.entry(*target).or_default();
334334
target.musl_root = Some("/usr".into());
335335
}

0 commit comments

Comments
 (0)