Description
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 thatinline
andcold
attributes are added to LLVM IR by LLVM's PGO passes. ( PGO: Add a run-make test that makes sure that PGO profiling data is used by the compiler during optimizations. #60262) - Add
codegen
test making sure that instrumentation is generated as expected. (Add codegen test for PGO instrumentation. #60038) - Add
run-make
test that makes sure PGO works in a mixed Rust/C++ codebase using ThinLTO done vialld
. (PGO - Add a smoketest for combining PGO with cross-language LTO. #61036) - Document the command line flags in the rustc book.
- Document the implementation in the rustc-guide. (Add documentation about profile-guided optimization. rustc-dev-guide#318)
- Fix suboptimal compilation of the profiling runtime (libprofiler_builtins is missing compile-time flags #59531, PR fixing it is libprofiler_builtins: Set compilation flags more correctly for C code. #60402)
- Fix instrumentation related linker errors on MSVC (Exclude profiler-generated symbols from MSVC __imp_-symbol workaround. #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
.
- I verified that
- Triage PGO-related issues reported so far
- PGO on Windows-GNU #49409 (support for Windows GNU)
- Profile Guided Optimization (PGO) not working on aarch64 #57257 (support for Aarch64, fixed)
- Unused symbol breaks Profiled Guided Optimization (PGO) #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 LLVM does not support PGO on Windows with -Cpanic=unwind #61002 and Emit error when trying to use PGO in conjunction with unwinding on Windows. #61005)
- Adapt PGO cli flags to how Clang) and GCC handle them. (Implement in Clean up handling of
-Z pgo-gen
commandline option. #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:
- This first round of PGO support does not yet include context-sensitive instrumentation, which might be added at a later point in time.
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 Use section/symbol ordering files for compiling rustc (e.g. BOLT) #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