From 88f3a6ebc6dbd6c8c4acdb67dee1aeec75d5f5ac Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Mon, 14 Dec 2020 13:50:59 -0500 Subject: [PATCH] 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). --- src/bootstrap/compile.rs | 16 +++++- src/bootstrap/config.rs | 9 ++++ src/bootstrap/flags.rs | 7 +++ .../host-x86_64/dist-x86_64-linux/Dockerfile | 6 +-- .../host-x86_64/dist-x86_64-linux/pgo.sh | 50 +++++++++++++++++++ src/ci/docker/scripts/pgo.rs | 1 + 6 files changed, 85 insertions(+), 4 deletions(-) create mode 100755 src/ci/docker/host-x86_64/dist-x86_64-linux/pgo.sh create mode 100644 src/ci/docker/scripts/pgo.rs diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index fbebb26c74620..2d88f8d9a92c6 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -501,6 +501,20 @@ impl Step for Rustc { let mut cargo = builder.cargo(compiler, Mode::Rustc, SourceType::InTree, target, "build"); rustc_cargo(builder, &mut cargo, target); + if let Some(path) = &builder.config.rust_profile_generate { + if compiler.stage == 1 { + cargo.rustflag(&format!("-Cprofile-generate={}", path)); + // Apparently necessary to avoid overflowing the counters during + // a Cargo build profile + cargo.rustflag("-Cllvm-args=-vp-counters-per-site=4"); + } + } + if let Some(path) = &builder.config.rust_profile_use { + if compiler.stage == 1 { + cargo.rustflag(&format!("-Cprofile-use={}", path)); + } + } + builder.info(&format!( "Building stage{} compiler artifacts ({} -> {})", compiler.stage, &compiler.host, target @@ -752,7 +766,7 @@ fn copy_codegen_backends_to_sysroot( // Here we're looking for the output dylib of the `CodegenBackend` step and // we're copying that into the `codegen-backends` folder. let dst = builder.sysroot_codegen_backends(target_compiler); - t!(fs::create_dir_all(&dst)); + t!(fs::create_dir_all(&dst), dst); if builder.config.dry_run { return; diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index fb2c6d1f92a80..58dc5f7af791d 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -133,6 +133,8 @@ pub struct Config { pub rust_thin_lto_import_instr_limit: Option, pub rust_remap_debuginfo: bool, pub rust_new_symbol_mangling: bool, + pub rust_profile_use: Option, + pub rust_profile_generate: Option, pub build: TargetSelection, pub hosts: Vec, @@ -494,6 +496,8 @@ struct Rust { llvm_libunwind: Option, control_flow_guard: Option, new_symbol_mangling: Option, + profile_generate: Option, + profile_use: Option, } /// TOML representation of how each build target is configured. @@ -871,6 +875,11 @@ impl Config { config.rust_codegen_units = rust.codegen_units.map(threads_from_config); config.rust_codegen_units_std = rust.codegen_units_std.map(threads_from_config); + config.rust_profile_use = flags.rust_profile_use.or(rust.profile_use); + config.rust_profile_generate = flags.rust_profile_generate.or(rust.profile_generate); + } else { + config.rust_profile_use = flags.rust_profile_use; + config.rust_profile_generate = flags.rust_profile_generate; } if let Some(t) = toml.target { diff --git a/src/bootstrap/flags.rs b/src/bootstrap/flags.rs index 5a8096674c6da..d6a45f1c17076 100644 --- a/src/bootstrap/flags.rs +++ b/src/bootstrap/flags.rs @@ -68,6 +68,9 @@ pub struct Flags { pub deny_warnings: Option, pub llvm_skip_rebuild: Option, + + pub rust_profile_use: Option, + pub rust_profile_generate: Option, } pub enum Subcommand { @@ -219,6 +222,8 @@ To learn more about a subcommand, run `./x.py -h`", VALUE overrides the skip-rebuild option in config.toml.", "VALUE", ); + opts.optopt("", "rust-profile-generate", "rustc error format", "FORMAT"); + opts.optopt("", "rust-profile-use", "rustc error format", "FORMAT"); // We can't use getopt to parse the options until we have completed specifying which // options are valid, but under the current implementation, some options are conditional on @@ -674,6 +679,8 @@ Arguments: color: matches .opt_get_default("color", Color::Auto) .expect("`color` should be `always`, `never`, or `auto`"), + rust_profile_use: matches.opt_str("rust-profile-use"), + rust_profile_generate: matches.opt_str("rust-profile-generate"), } } } diff --git a/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile b/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile index 14700aeea05af..3b411fd3b86fc 100644 --- a/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile +++ b/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile @@ -85,6 +85,7 @@ ENV CC=clang CXX=clang++ COPY scripts/sccache.sh /scripts/ RUN sh /scripts/sccache.sh + ENV HOSTS=x86_64-unknown-linux-gnu ENV RUST_CONFIGURE_ARGS \ @@ -98,9 +99,8 @@ ENV RUST_CONFIGURE_ARGS \ --set llvm.thin-lto=true \ --set llvm.ninja=false \ --set rust.jemalloc -ENV SCRIPT python2.7 ../x.py dist --host $HOSTS --target $HOSTS \ - --include-default-paths \ - src/tools/build-manifest +COPY host-x86_64/dist-x86_64-linux/pgo.sh /tmp/ +ENV SCRIPT /tmp/pgo.sh ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=clang # This is the only builder which will create source tarballs diff --git a/src/ci/docker/host-x86_64/dist-x86_64-linux/pgo.sh b/src/ci/docker/host-x86_64/dist-x86_64-linux/pgo.sh new file mode 100755 index 0000000000000..fba3a86bb78a0 --- /dev/null +++ b/src/ci/docker/host-x86_64/dist-x86_64-linux/pgo.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +set -euxo pipefail + +rm -rf /tmp/rustc-pgo + +python2.7 ../x.py build --stage 2 library/std --rust-profile-generate=/tmp/rustc-pgo + +./build/x86_64-unknown-linux-gnu/stage2/bin/rustc --edition=2018 \ + --crate-type=lib ../library/core/src/lib.rs + +PERF=e095f5021bf01cf3800f50b3a9f14a9683eb3e4e + +curl -o /tmp/externs.rs \ +https://raw.githubusercontent.com/rust-lang/rustc-perf/$PERF/collector/benchmarks/externs/src/lib.rs +./build/x86_64-unknown-linux-gnu/stage2/bin/rustc --edition=2018 --crate-type=lib /tmp/externs.rs + +curl -o /tmp/ctfe.rs \ +https://raw.githubusercontent.com/rust-lang/rustc-perf/$PERF/collector/benchmarks/ctfe-stress-4/src/lib.rs +./build/x86_64-unknown-linux-gnu/stage2/bin/rustc --edition=2018 --crate-type=lib /tmp/ctfe.rs + +cp -pri ../src/tools/cargo /tmp/cargo + +RUSTC=./build/x86_64-unknown-linux-gnu/stage2/bin/rustc CARGO_INCREMENTAL=1 \ + ./build/x86_64-unknown-linux-gnu/stage0/bin/cargo check \ + --manifest-path /tmp/cargo/Cargo.toml +echo 'pub fn barbarbar() {}' >> /tmp/cargo/src/cargo/lib.rs +RUSTC=./build/x86_64-unknown-linux-gnu/stage2/bin/rustc CARGO_INCREMENTAL=1 \ + ./build/x86_64-unknown-linux-gnu/stage0/bin/cargo check \ + --manifest-path /tmp/cargo/Cargo.toml +touch /tmp/cargo/src/cargo/lib.rs +RUSTC=./build/x86_64-unknown-linux-gnu/stage2/bin/rustc CARGO_INCREMENTAL=1 \ + ./build/x86_64-unknown-linux-gnu/stage0/bin/cargo check \ + --manifest-path /tmp/cargo/Cargo.toml +RUSTC=./build/x86_64-unknown-linux-gnu/stage2/bin/rustc CARGO_INCREMENTAL=1 \ + ./build/x86_64-unknown-linux-gnu/stage0/bin/cargo check \ + --manifest-path /tmp/cargo/Cargo.toml +RUSTC=./build/x86_64-unknown-linux-gnu/stage2/bin/rustc \ + ./build/x86_64-unknown-linux-gnu/stage0/bin/cargo build --release \ + --manifest-path /tmp/cargo/Cargo.toml +./build/x86_64-unknown-linux-gnu/llvm/bin/llvm-profdata \ + merge -o /tmp/rustc-pgo.profdata /tmp/rustc-pgo + +cp /tmp/rustc-pgo.profdata ./build/dist/rustc-pgo.profdata + +# This produces the actual final set of artifacts +python2.7 ../x.py dist --rust-profile-use=/tmp/rustc-pgo.profdata \ + --host $HOSTS --target $HOSTS \ + --include-default-paths \ + src/tools/build-manifest diff --git a/src/ci/docker/scripts/pgo.rs b/src/ci/docker/scripts/pgo.rs new file mode 100644 index 0000000000000..f328e4d9d04c3 --- /dev/null +++ b/src/ci/docker/scripts/pgo.rs @@ -0,0 +1 @@ +fn main() {}