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

Tracking Issue for stabilizing Profile-Guided Optimization support #59913

Closed
16 tasks done
michaelwoerister opened this issue Apr 12, 2019 · 11 comments
Closed
16 tasks done
Labels
A-codegen Area: Code generation C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. metabug Issues about issues themselves ("bugs about bugs") T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-llvm Working group: LLVM backend code generation

Comments

@michaelwoerister
Copy link
Member

michaelwoerister commented Apr 12, 2019

Profile-guided optimization (PGO) is a common optimization technique for ahead-of-time compilers. It works by collecting data about a programs typical execution (e.g. probability of branches taken, typical runtime values of variables, etc) and then uses this information during program optimization for things like inlining decisions, machine code layout, or indirect call promotion.

LLVM supports different forms of PGO, the most effective of which relies on generating instrumented binaries that collect the profiling information. The Rust compiler has had experimental support for this via the -Z pgo-gen and -Z pgo-use flags for a while. This issue tracks the progress of making this functionality available in stable Rust.

Steps:

Non-Goals:

Further Information:

cc @rust-lang/core @rust-lang/compiler

@michaelwoerister michaelwoerister added A-codegen Area: Code generation metabug Issues about issues themselves ("bugs about bugs") T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. WG-llvm Working group: LLVM backend code generation labels Apr 12, 2019
@alexcrichton
Copy link
Member

Thanks for opening this up @michaelwoerister!

One thing I might requests as well is a solid idea of how PGO could be integrated into a Cargo subcommand. I don't think we should add support to Cargo itself immediately, nor do I think a proof-of-concept is required to actually exist, but rather I think we should make sure that there's a solid idea of how this could be used by general folks. I suspect that such a command may not actually be used by codebases like Firefox, but for general optimization and usage I think it would be pretty valuable to have a solid "PGO in Rust story" in at least an MVP state which doesn't require tons of voodoo and working around various aspects of the build

@cuviper
Copy link
Member

cuviper commented Apr 12, 2019

Clarify if PGO support should be restricted to tier 1 platforms (because we need to provide the profiling runtime).

Why restricted -- shouldn't we welcome anyone to implement the other platforms? But it's understandable if it's only supported on tier 1 at first.

@michaelwoerister
Copy link
Member Author

Why restricted -- shouldn't we welcome anyone to implement the other platforms? But it's understandable if it's only supported on tier 1 at first.

Yeah, that's what I meant really: Stabilization would only entail support for tier 1 platforms. If other platforms work too that would be fine of course :)

@michaelwoerister
Copy link
Member Author

One thing I might requests as well is a solid idea of how PGO could be integrated into a Cargo subcommand.

Yes, that's a good idea. What form would you expect this to take exactly?

@alexcrichton
Copy link
Member

Heh well I've never actually done PGO myself, but knowing at least the basics of it in the very end it'd be cool to have something like:

$ cargo build --release --pgo foo

where foo is a binary in the project which is the PGO test suite and executes everything that needs to be profiled. That would build everything in release mode with PGO instrumentation, run the binary, and then rebuild everything afterwards without PGO instrumentation but using the PGO data. (magically passing all the right flags everywhere, handling build scripts and procedural macros that aren't PGO'd, etc).

In the meantime though I'd be fine with:

$ cargo pgo

which maybe runs the test suite by default? Something like cargo pgo --bin foo as well if you only wanted to execute one binary.

@michaelwoerister
Copy link
Member Author

michaelwoerister commented Apr 16, 2019

That sounds like it should be straightforward to implement. The only complication I see is that the llvm-profdata tool needs to be available to Cargo. The workflow is something like:

# Compile with instrumentation
rustc -Cprofile-generate=target/pgo src/main.rs
# Run the instrumented binary
target/release/proggy
# Run llvm-profdata to bring profiling data into usable state
llvm-profdata merge -output=target/pgo/proggy.profdata target/pgo
# Run the compiler again
rustc -Cprofile-use=target/pgo/proggy.profdata src/main.rs

@alexcrichton
Copy link
Member

Indeed yeah I suspect it's straightforward :)

I just want to make sure that the default use case isn't "oh be sure to install the compiler from source because we don't provide you everything" and shipping llvm-profdata with the llvm-tools component seems reasonable. (or maybe even renaming that to be rustc-specific rather than LLVM specific).

The other thing I'd want to make sure is that it works reasonable well for dependencies as well, where if you have rlib dependencies you don't have anything special to do it "just works" or works somehow. (also FWIW I haven't used PGO before, this may already be the case!)

@michaelwoerister
Copy link
Member Author

The other thing I'd want to make sure is that it works reasonable well for dependencies as well, where if you have rlib dependencies you don't have anything special to do it "just works" or works somehow. (also FWIW I haven't used PGO before, this may already be the case!)

Yes, that should already work if dependencies are built with instrumentation too (i.e. -Cprofile-generate and -Cprofile-use are passed to rustc when compiling them).

@gnzlbg
Copy link
Contributor

gnzlbg commented Jun 6, 2019

Is it possible to generate a pgo optimized rustc using ./x.py ? Ideally it would use the rustc-perf benchmarks as a source, and not rustc tests, for generating the profiles.

bors added a commit that referenced this issue Jul 2, 2019
Stabilize support for Profile-guided Optimization

This PR makes profile-guided optimization available via the `-C profile-generate` / `-C profile-use` pair of commandline flags and adds end-user documentation for the feature to the [rustc book](https://doc.rust-lang.org/rustc/). The PR thus ticks the last two remaining checkboxes of the [stabilization tracking issue](#59913).

From the tracking issue:
> Profile-guided optimization (PGO) is a common optimization technique for ahead-of-time compilers. It works by collecting data about a program's typical execution (e.g. probability of branches taken, typical runtime values of variables, etc) and then uses this information during program optimization for things like inlining decisions, machine code layout, or indirect call promotion.

If you are curious about how this can be used, there is a rendered version of the documentation this PR adds available [here](
https://github.com/michaelwoerister/rust/blob/stabilize-pgo/src/doc/rustc/src/profile-guided-optimization.md).

r? @alexcrichton
cc @rust-lang/compiler
@michaelwoerister
Copy link
Member Author

@gnzlbg x.py does not have any builtin support for this at the moment. It would be possible but quite a bit of work to implement, I suspect.

@michaelwoerister
Copy link
Member Author

PGO has been stabilized in #61268 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. metabug Issues about issues themselves ("bugs about bugs") T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-llvm Working group: LLVM backend code generation
Projects
None yet
Development

No branches or pull requests

4 participants