Tracking Issue for stabilizing Profile-Guided Optimization support

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:

  • Add run-make test that makes sure that inline and cold attributes are added to LLVM IR by LLVM’s PGO passes. (#60262)
  • Add codegen test making sure that instrumentation is generated as expected. (#60038)
  • Add run-make test that makes sure PGO works in a mixed Rust/C++ codebase using ThinLTO done via lld. (#61036)
  • Document the command line flags in the rustc book.
  • Document the implementation in the rustc-guide. (rust-lang/rustc-dev-guide#318)
  • Fix suboptimal compilation of the profiling runtime (#59531, PR fixing it is #60402)
  • Fix instrumentation related linker errors on MSVC (#59812)
  • Clarify role of the pre-inlining pass optionally done by LLVM when compiling with PGO.
    • I verified that rustc already behaves like Clang here: Pre-inlining is enabled except for -Copt-level=s and -Copt-level=z.
  • Triage PGO-related issues reported so far
    • #49409 (support for Windows GNU)
    • #57257 (support for Aarch64, fixed)
    • #57258 (linker error with rather specific setup, not a blocker)
  • Clarify whether reported problems related to SEH on Windows affect the stabilization of this feature. (see #61002 and #61005)
  • Adapt PGO cli flags to how Clang) and GCC handle them. (Implement in #59874)
  • Make PGO available via stable -C profile-generate and -C profile-use flags (mirroring the corresponding Clang flags).
  • Clarify if PGO support should be restricted to tier 1 platforms (because we need to provide the profiling runtime). cc @rust-lang/release

Non-Goals:

Further Information:

  • Some further information is provided at the WG-profile-guided-optimization page.
  • As far as I can tell, PGO subsumes “ordering file” related optimizations (see #50655). UPDATE: This should be verified. PGO might only affect basic block ordering, not the order of entire functions.

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

2 thoughts on “Tracking Issue for stabilizing Profile-Guided Optimization support

  1. 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.

  2. 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.

Comments are closed.