Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic support for PGO in rustbuild for rustc #80033

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub struct Config {
pub rust_thin_lto_import_instr_limit: Option<u32>,
pub rust_remap_debuginfo: bool,
pub rust_new_symbol_mangling: bool,
pub rust_profile_use: Option<String>,
pub rust_profile_generate: Option<String>,

pub build: TargetSelection,
pub hosts: Vec<TargetSelection>,
Expand Down Expand Up @@ -494,6 +496,8 @@ struct Rust {
llvm_libunwind: Option<String>,
control_flow_guard: Option<bool>,
new_symbol_mangling: Option<bool>,
profile_generate: Option<String>,
profile_use: Option<String>,
}

/// TOML representation of how each build target is configured.
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions src/bootstrap/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub struct Flags {
pub deny_warnings: Option<bool>,

pub llvm_skip_rebuild: Option<bool>,

pub rust_profile_use: Option<String>,
pub rust_profile_generate: Option<String>,
}

pub enum Subcommand {
Expand Down Expand Up @@ -219,6 +222,8 @@ To learn more about a subcommand, run `./x.py <subcommand> -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
Expand Down Expand Up @@ -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"),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
Expand Down
50 changes: 50 additions & 0 deletions src/ci/docker/host-x86_64/dist-x86_64-linux/pgo.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/ci/docker/scripts/pgo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}