Skip to content

Commit 88f3a6e

Browse files
Utilize PGO for rustc linux dist builds
This implements support for applying PGO to the rustc compilation step (not standard library or any tooling, including rustdoc). Expanding PGO to more tools is not terribly difficult but will involve more work and greater CI time commitment. For the same reason of avoiding greater time commitment, this currently avoids implementing for platforms outside of x86_64-unknown-linux-gnu, though in practice it should be quite simple to extend over time to more platforms. The initial implementation is intentionally minimal here to avoid too much work investment before we start seeing wins for a subset of Rust users. The choice of workloads to profile here is somewhat arbitrary, but the general rationale was to aim for a small set that largely avoided time regressions on perf.rust-lang.org's full suite of crates. The set chosen is libcore, cargo (and its dependencies), and a few ad-hoc stress tests from perf.rlo. The stress tests are arguably the most controversial, but they benefit those cases (avoiding regressions) and do not really remove wins from other benchmarks. The primary next step after this PR lands is to implement support for PGO in LLVM. It is unclear whether we can afford a full LLVM rebuild in CI, though, so the approach taken there may need to be more staggered. rustc-only PGO seems well affordable on linux at least, giving us up to 20% wall time wins on some crates for 15 minutes of extra CI time (1 hour up from 45 minutes).
1 parent b32e6e6 commit 88f3a6e

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

src/bootstrap/compile.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,20 @@ impl Step for Rustc {
501501
let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "build");
502502
rustc_cargo(builder, &mut cargo, target);
503503

504+
if let Some(path) = &builder.config.rust_profile_generate {
505+
if compiler.stage == 1 {
506+
cargo.rustflag(&format!("-Cprofile-generate={}", path));
507+
// Apparently necessary to avoid overflowing the counters during
508+
// a Cargo build profile
509+
cargo.rustflag("-Cllvm-args=-vp-counters-per-site=4");
510+
}
511+
}
512+
if let Some(path) = &builder.config.rust_profile_use {
513+
if compiler.stage == 1 {
514+
cargo.rustflag(&format!("-Cprofile-use={}", path));
515+
}
516+
}
517+
504518
builder.info(&format!(
505519
"Building stage{} compiler artifacts ({} -> {})",
506520
compiler.stage, &compiler.host, target
@@ -752,7 +766,7 @@ fn copy_codegen_backends_to_sysroot(
752766
// Here we're looking for the output dylib of the `CodegenBackend` step and
753767
// we're copying that into the `codegen-backends` folder.
754768
let dst = builder.sysroot_codegen_backends(target_compiler);
755-
t!(fs::create_dir_all(&dst));
769+
t!(fs::create_dir_all(&dst), dst);
756770

757771
if builder.config.dry_run {
758772
return;

src/bootstrap/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ pub struct Config {
133133
pub rust_thin_lto_import_instr_limit: Option<u32>,
134134
pub rust_remap_debuginfo: bool,
135135
pub rust_new_symbol_mangling: bool,
136+
pub rust_profile_use: Option<String>,
137+
pub rust_profile_generate: Option<String>,
136138

137139
pub build: TargetSelection,
138140
pub hosts: Vec<TargetSelection>,
@@ -494,6 +496,8 @@ struct Rust {
494496
llvm_libunwind: Option<String>,
495497
control_flow_guard: Option<bool>,
496498
new_symbol_mangling: Option<bool>,
499+
profile_generate: Option<String>,
500+
profile_use: Option<String>,
497501
}
498502

499503
/// TOML representation of how each build target is configured.
@@ -871,6 +875,11 @@ impl Config {
871875

872876
config.rust_codegen_units = rust.codegen_units.map(threads_from_config);
873877
config.rust_codegen_units_std = rust.codegen_units_std.map(threads_from_config);
878+
config.rust_profile_use = flags.rust_profile_use.or(rust.profile_use);
879+
config.rust_profile_generate = flags.rust_profile_generate.or(rust.profile_generate);
880+
} else {
881+
config.rust_profile_use = flags.rust_profile_use;
882+
config.rust_profile_generate = flags.rust_profile_generate;
874883
}
875884

876885
if let Some(t) = toml.target {

src/bootstrap/flags.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ pub struct Flags {
6868
pub deny_warnings: Option<bool>,
6969

7070
pub llvm_skip_rebuild: Option<bool>,
71+
72+
pub rust_profile_use: Option<String>,
73+
pub rust_profile_generate: Option<String>,
7174
}
7275

7376
pub enum Subcommand {
@@ -219,6 +222,8 @@ To learn more about a subcommand, run `./x.py <subcommand> -h`",
219222
VALUE overrides the skip-rebuild option in config.toml.",
220223
"VALUE",
221224
);
225+
opts.optopt("", "rust-profile-generate", "rustc error format", "FORMAT");
226+
opts.optopt("", "rust-profile-use", "rustc error format", "FORMAT");
222227

223228
// We can't use getopt to parse the options until we have completed specifying which
224229
// options are valid, but under the current implementation, some options are conditional on
@@ -674,6 +679,8 @@ Arguments:
674679
color: matches
675680
.opt_get_default("color", Color::Auto)
676681
.expect("`color` should be `always`, `never`, or `auto`"),
682+
rust_profile_use: matches.opt_str("rust-profile-use"),
683+
rust_profile_generate: matches.opt_str("rust-profile-generate"),
677684
}
678685
}
679686
}

src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ ENV CC=clang CXX=clang++
8585
COPY scripts/sccache.sh /scripts/
8686
RUN sh /scripts/sccache.sh
8787

88+
8889
ENV HOSTS=x86_64-unknown-linux-gnu
8990

9091
ENV RUST_CONFIGURE_ARGS \
@@ -98,9 +99,8 @@ ENV RUST_CONFIGURE_ARGS \
9899
--set llvm.thin-lto=true \
99100
--set llvm.ninja=false \
100101
--set rust.jemalloc
101-
ENV SCRIPT python2.7 ../x.py dist --host $HOSTS --target $HOSTS \
102-
--include-default-paths \
103-
src/tools/build-manifest
102+
COPY host-x86_64/dist-x86_64-linux/pgo.sh /tmp/
103+
ENV SCRIPT /tmp/pgo.sh
104104
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=clang
105105

106106
# This is the only builder which will create source tarballs
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
set -euxo pipefail
4+
5+
rm -rf /tmp/rustc-pgo
6+
7+
python2.7 ../x.py build --stage 2 library/std --rust-profile-generate=/tmp/rustc-pgo
8+
9+
./build/x86_64-unknown-linux-gnu/stage2/bin/rustc --edition=2018 \
10+
--crate-type=lib ../library/core/src/lib.rs
11+
12+
PERF=e095f5021bf01cf3800f50b3a9f14a9683eb3e4e
13+
14+
curl -o /tmp/externs.rs \
15+
https://raw.githubusercontent.com/rust-lang/rustc-perf/$PERF/collector/benchmarks/externs/src/lib.rs
16+
./build/x86_64-unknown-linux-gnu/stage2/bin/rustc --edition=2018 --crate-type=lib /tmp/externs.rs
17+
18+
curl -o /tmp/ctfe.rs \
19+
https://raw.githubusercontent.com/rust-lang/rustc-perf/$PERF/collector/benchmarks/ctfe-stress-4/src/lib.rs
20+
./build/x86_64-unknown-linux-gnu/stage2/bin/rustc --edition=2018 --crate-type=lib /tmp/ctfe.rs
21+
22+
cp -pri ../src/tools/cargo /tmp/cargo
23+
24+
RUSTC=./build/x86_64-unknown-linux-gnu/stage2/bin/rustc CARGO_INCREMENTAL=1 \
25+
./build/x86_64-unknown-linux-gnu/stage0/bin/cargo check \
26+
--manifest-path /tmp/cargo/Cargo.toml
27+
echo 'pub fn barbarbar() {}' >> /tmp/cargo/src/cargo/lib.rs
28+
RUSTC=./build/x86_64-unknown-linux-gnu/stage2/bin/rustc CARGO_INCREMENTAL=1 \
29+
./build/x86_64-unknown-linux-gnu/stage0/bin/cargo check \
30+
--manifest-path /tmp/cargo/Cargo.toml
31+
touch /tmp/cargo/src/cargo/lib.rs
32+
RUSTC=./build/x86_64-unknown-linux-gnu/stage2/bin/rustc CARGO_INCREMENTAL=1 \
33+
./build/x86_64-unknown-linux-gnu/stage0/bin/cargo check \
34+
--manifest-path /tmp/cargo/Cargo.toml
35+
RUSTC=./build/x86_64-unknown-linux-gnu/stage2/bin/rustc CARGO_INCREMENTAL=1 \
36+
./build/x86_64-unknown-linux-gnu/stage0/bin/cargo check \
37+
--manifest-path /tmp/cargo/Cargo.toml
38+
RUSTC=./build/x86_64-unknown-linux-gnu/stage2/bin/rustc \
39+
./build/x86_64-unknown-linux-gnu/stage0/bin/cargo build --release \
40+
--manifest-path /tmp/cargo/Cargo.toml
41+
./build/x86_64-unknown-linux-gnu/llvm/bin/llvm-profdata \
42+
merge -o /tmp/rustc-pgo.profdata /tmp/rustc-pgo
43+
44+
cp /tmp/rustc-pgo.profdata ./build/dist/rustc-pgo.profdata
45+
46+
# This produces the actual final set of artifacts
47+
python2.7 ../x.py dist --rust-profile-use=/tmp/rustc-pgo.profdata \
48+
--host $HOSTS --target $HOSTS \
49+
--include-default-paths \
50+
src/tools/build-manifest

src/ci/docker/scripts/pgo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

0 commit comments

Comments
 (0)