Skip to content

Commit 3542ef2

Browse files
committed
Auto merge of #159642 - Kobzol:bootstrap-pgo-clippy, r=<try>
Optimize Clippy with PGO
2 parents cde3f8a + f2e4e7c commit 3542ef2

7 files changed

Lines changed: 70 additions & 10 deletions

File tree

bootstrap.example.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,11 @@
995995
# Instrument cargo, so that when executed, it will gather profiles
996996
# to this path.
997997
#pgo.cargo.generate = "/tmp/profiles/foo.profraw"
998+
# Use the following profile to PGO optimize clippy.
999+
#pgo.clippy.use = "/tmp/profiles/foo.profraw"
1000+
# Instrument clippy, so that when executed, it will gather profiles
1001+
# to this path.
1002+
#pgo.clippy.generate = "/tmp/profiles/foo.profraw"
9981003
# Use the following profile to PGO optimize LLVM.
9991004
#pgo.llvm.use = "/tmp/profiles/foo.profraw"
10001005
# Instrument LLVM, so that when executed, it will gather profiles

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ impl Step for ToolBuild {
141141
let pgo_config = match self.path {
142142
"src/tools/rustdoc" => Some(&builder.config.rustdoc_pgo),
143143
"src/tools/cargo" => Some(&builder.config.cargo_pgo),
144+
"src/tools/clippy" => Some(&builder.config.clippy_pgo),
144145
_ => None,
145146
};
146147
if let Some(pgo_config) = pgo_config {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ pub struct Config {
237237
pub rust_pgo: PgoConfig,
238238
pub rustdoc_pgo: PgoConfig,
239239
pub cargo_pgo: PgoConfig,
240+
pub clippy_pgo: PgoConfig,
240241

241242
pub llvm_libunwind_default: Option<LlvmLibunwind>,
242243
pub enable_bolt_settings: bool,
@@ -658,8 +659,13 @@ impl Config {
658659
libgccjit_libs_dir: gcc_libgccjit_libs_dir,
659660
} = toml_gcc.unwrap_or_default();
660661

661-
let Pgo { rustc: pgo_rustc, llvm: pgo_llvm, rustdoc: pgo_rustdoc, cargo: pgo_cargo } =
662-
toml_pgo.unwrap_or_default();
662+
let Pgo {
663+
rustc: pgo_rustc,
664+
rustdoc: pgo_rustdoc,
665+
cargo: pgo_cargo,
666+
clippy: pgo_clippy,
667+
llvm: pgo_llvm,
668+
} = toml_pgo.unwrap_or_default();
663669

664670
// Backcompat: flags have priority over config
665671
if flags_rust_profile_use.is_some() || flags_rust_profile_generate.is_some() {
@@ -714,6 +720,7 @@ impl Config {
714720

715721
let pgo_rustdoc = init_pgo(pgo_rustdoc, "rustdoc");
716722
let pgo_cargo = init_pgo(pgo_cargo, "cargo");
723+
let pgo_clippy = init_pgo(pgo_clippy, "clippy");
717724

718725
if rust_bootstrap_override_lld.is_some() && rust_bootstrap_override_lld_legacy.is_some() {
719726
panic!(
@@ -1413,6 +1420,7 @@ NOTE: Please add `--stage 2` to your command line, or if you're sure you want to
14131420
channel,
14141421
ci_env,
14151422
clippy_info,
1423+
clippy_pgo: pgo_clippy,
14161424
cmd: flags_cmd,
14171425
codegen_tests: rust_codegen_tests.unwrap_or(true),
14181426
color: flags_color,

src/bootstrap/src/core/config/toml/pgo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ define_config! {
2828
rustc: Option<PgoConfig> = "rustc",
2929
rustdoc: Option<PgoConfig> = "rustdoc",
3030
cargo: Option<PgoConfig> = "cargo",
31+
clippy: Option<PgoConfig> = "clippy",
3132
llvm: Option<PgoConfig> = "llvm",
3233
}
3334
}

src/tools/opt-dist/src/exec.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use camino::{Utf8Path, Utf8PathBuf};
77
use crate::environment::Environment;
88
use crate::metrics::{load_metrics, record_metrics};
99
use crate::timer::TimerSection;
10-
use crate::training::{BoltProfile, LlvmPGOProfile, RustcPGOProfile, RustdocPGOProfile};
10+
use crate::training::{
11+
BoltProfile, ClippyPGOProfile, LlvmPGOProfile, RustcPGOProfile, RustdocPGOProfile,
12+
};
1113
use crate::utils::io::normalize_path;
1214

1315
#[derive(Default)]
@@ -129,6 +131,11 @@ impl Bootstrap {
129131
self
130132
}
131133

134+
pub fn with_clippy(mut self) -> Self {
135+
self.cmd = self.cmd.arg("clippy");
136+
self
137+
}
138+
132139
pub fn dist(env: &Environment, dist_args: &[String]) -> Self {
133140
let metrics_path = env.build_root().join("metrics.json");
134141
let args = dist_args.iter().map(|arg| arg.as_str()).collect::<Vec<_>>();
@@ -218,6 +225,22 @@ impl Bootstrap {
218225
self
219226
}
220227

228+
pub fn clippy_pgo_instrument(mut self, profile_dir: &Utf8Path) -> Self {
229+
self.cmd = self
230+
.cmd
231+
.arg("--set")
232+
.arg(format!(r#"pgo.clippy.generate="{}""#, normalize_path(profile_dir).as_str()));
233+
self
234+
}
235+
236+
pub fn clippy_pgo_optimize(mut self, profile: &ClippyPGOProfile) -> Self {
237+
self.cmd = self
238+
.cmd
239+
.arg("--set")
240+
.arg(format!(r#"pgo.clippy.use="{}""#, normalize_path(&profile.0).as_str()));
241+
self
242+
}
243+
221244
pub fn with_llvm_bolt_ldflags(mut self) -> Self {
222245
self.cmd = self.cmd.arg("--set").arg("llvm.ldflags=-Wl,-q");
223246
self

src/tools/opt-dist/src/main.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::exec::{Bootstrap, cmd};
1010
use crate::tests::run_tests;
1111
use crate::timer::Timer;
1212
use crate::training::{
13-
gather_bolt_profiles, gather_llvm_profiles, gather_rustc_profiles, gather_rustdoc_profiles,
14-
llvm_benchmarks, rustc_benchmarks,
13+
gather_bolt_profiles, gather_clippy_profiles, gather_llvm_profiles, gather_rustc_profiles,
14+
gather_rustdoc_profiles, llvm_benchmarks, rustc_benchmarks,
1515
};
1616
use crate::utils::artifact_size::print_binary_sizes;
1717
use crate::utils::io::{copy_directory, reset_directory};
@@ -239,7 +239,7 @@ fn execute_pipeline(
239239
// Stage 1: Build PGO instrumented rustc
240240
// We use a normal build of LLVM, because gathering PGO profiles for LLVM and `rustc` at the
241241
// same time can cause issues, because the host and in-tree LLVM versions can diverge.
242-
let (rustc_pgo_profile, rustdoc_pgo_profile) =
242+
let (rustc_pgo_profile, rustdoc_pgo_profile, clippy_pgo_profile) =
243243
timer.section("Stage 1 (Rustc + rustdoc PGO)", |stage| {
244244
let rustc_profile_dir_root = env.artifact_dir().join("rustc-pgo");
245245

@@ -250,8 +250,10 @@ fn execute_pipeline(
250250
let mut builder = Bootstrap::build(env)
251251
.with_rustdoc()
252252
.with_cargo()
253+
.with_clippy()
253254
.rustc_pgo_instrument(&rustc_profile_dir_root)
254255
.cargo_pgo_instrument(&rustc_profile_dir_root)
256+
.clippy_pgo_instrument(&rustc_profile_dir_root)
255257
.rustdoc_pgo_instrument(&rustc_profile_dir_root);
256258

257259
if env.supports_shared_llvm() {
@@ -271,23 +273,28 @@ fn execute_pipeline(
271273
let rustdoc_profile = stage.section("Gather rustdoc profiles", |_| {
272274
gather_rustdoc_profiles(env, &rustc_profile_dir_root)
273275
})?;
276+
let clippy_profile = stage.section("Gather clippy profiles", |_| {
277+
gather_clippy_profiles(env, &rustc_profile_dir_root)
278+
})?;
274279
print_free_disk_space()?;
275280

276281
stage.section("Build PGO optimized rustc", |section| {
277282
let mut cmd = Bootstrap::build(env)
278283
.with_rustdoc()
279284
.with_cargo()
285+
.with_clippy()
280286
.rustc_pgo_optimize(&rustc_profile)
281287
.cargo_pgo_optimize(&rustc_profile)
282-
.rustdoc_pgo_optimize(&rustdoc_profile);
288+
.rustdoc_pgo_optimize(&rustdoc_profile)
289+
.clippy_pgo_optimize(&clippy_profile);
283290
if env.use_bolt() {
284291
cmd = cmd.with_rustc_bolt_ldflags();
285292
}
286293

287294
cmd.run(section)
288295
})?;
289296

290-
Ok((rustc_profile, rustdoc_profile))
297+
Ok((rustc_profile, rustdoc_profile, clippy_profile))
291298
})?;
292299

293300
// Stage 2: Gather LLVM PGO profiles
@@ -422,7 +429,8 @@ fn execute_pipeline(
422429
.llvm_pgo_optimize(llvm_pgo_profile.as_ref())
423430
.rustc_pgo_optimize(&rustc_pgo_profile)
424431
.cargo_pgo_optimize(&rustc_pgo_profile)
425-
.rustdoc_pgo_optimize(&rustdoc_pgo_profile);
432+
.rustdoc_pgo_optimize(&rustdoc_pgo_profile)
433+
.clippy_pgo_optimize(&clippy_pgo_profile);
426434

427435
// if LLVM is not built we'll have PGO optimized rustc
428436
dist = if env.supports_shared_llvm() || !env.build_llvm() {

src/tools/opt-dist/src/training.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,14 @@ pub fn rustc_benchmarks(env: &Environment) -> CmdBuilder {
115115
init_compiler_benchmarks(env, &["Check", "Debug", "Opt"], &["All"], RUSTC_PGO_CRATES)
116116
}
117117

118-
pub fn rustdoc_benchmarks(env: &Environment) -> CmdBuilder {
118+
fn rustdoc_benchmarks(env: &Environment) -> CmdBuilder {
119119
init_compiler_benchmarks(env, &["Doc"], &["Full"], RUSTC_PGO_CRATES)
120120
}
121121

122+
fn clippy_benchmarks(env: &Environment) -> CmdBuilder {
123+
init_compiler_benchmarks(env, &["Clippy"], &["All"], RUSTC_PGO_CRATES)
124+
}
125+
122126
pub struct LlvmPGOProfile(pub Utf8PathBuf);
123127

124128
pub fn gather_llvm_profiles(
@@ -165,6 +169,16 @@ pub fn gather_rustdoc_profiles(
165169
.map(RustdocPGOProfile)
166170
}
167171

172+
pub struct ClippyPGOProfile(pub Utf8PathBuf);
173+
174+
pub fn gather_clippy_profiles(
175+
env: &Environment,
176+
profile_root: &Utf8Path,
177+
) -> anyhow::Result<ClippyPGOProfile> {
178+
log::info!("Running benchmarks with PGO instrumented clippy");
179+
gather_pgo_profiles(env, profile_root, "clippy", clippy_benchmarks(env)).map(ClippyPGOProfile)
180+
}
181+
168182
pub fn gather_pgo_profiles(
169183
env: &Environment,
170184
profile_root: &Utf8Path,

0 commit comments

Comments
 (0)