Stabilize -Zprofile-sample-use and -Zdebug-info-for-profiling#155942
Stabilize -Zprofile-sample-use and -Zdebug-info-for-profiling#155942zamazan4ik wants to merge 3 commits intorust-lang:mainfrom
-Zprofile-sample-use and -Zdebug-info-for-profiling#155942Conversation
- heavily based on the Sample-based PGO guide for Clang: https://clang.llvm.org/docs/UsersManual.html#using-sampling-profilers
- remove pages from the Unstable book for -Zprofile-sample-use and -Zdebug-info-for-profiling - add corresponding pages to the Rustc codegen docs
- update all related to the flags entities from unstable to stable ones: internal structs, tests - rename debug-info-for-profiling into debuginfo-for-profiling to be consistent with other "debuginfo" flags like -Cdebuginfo and -Csplit-debuginfo
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
That is fine, yes -- we are accustomed to flag name changes on stabilization etc. :)
Just a quick clarification: it is not a commit yet. While we plan to land it during this cycle, i.e. for v7.2, it has not seen use in mainline Linux yet. Thanks for this! @rustbot label +A-rust-for-linux |
I propose stabilizing the following options:
-Zprofile-sample-useoption into-Cprofile-sample-use-Zdebug-info-for-profilinginto-Cdebuginfo-for-profiling(see details about renaming below)Stabilization report
Summary
We stabilize Sample-based PGO for Rustc, and two corresponding flags for the feature:
-Zprofile-sample-use->-Cprofile-sample-useand-Zdebug-info-for-profiling->-Cdebuginfo-for-profiling.Sample-based PGO is an another way to perform PGO, in addition to already existing and stable Instrumentation PGO in Rustc (
-Cprofile-generate/-Cprofile-useflags). Sample-based PGO allows perform PGO on Rust binaries without instrumenting them, and that leads to a possibility to collect PGO-suitable profiles via an external profiler like Linux perf without a huge overhead (compared to Instrumentation PGO).More information can be found in the updated by the PR "Profile-guided Optimization" guide or Clang PGO guide.
Tracking:
profile-sample-useanddebug-info-for-profiling#155668Reference PRs:
-Zprofile-sample-useand-Zdebug-info-for-profiling#155942 - this PR itselfI wasn't sure should I create a dedicated Reference-only PRs or can put all the things in one PR. As a references, I've used #145974 where Reference documentation was updated in the same PR.
cc @rust-lang/lang @rust-lang/lang-advisors
What is stabilized
No new language syntax is introduced/stabilized by this stabilization PR - only compiler flags.
What isn't stabilized
I think this is the right section to compare Sample-based PGO (SPGO) implementation in Rustc vs its "big brother" - Sample-based PGO in Clang.
Besides
-Zprofile-sample-usein Rustc /-fprofile_sample_usein Clang and-Zdebug-info-for-profilingin Rustc /-fdebug-info-for-profiling, -fno-debug-info-for-profilingin Clang, Clang additionally supports the following SPGO-related switches:-fpseudo-probe-for-profiling, -fno-pseudo-probe-for-profilingflags. According to the Clang's PGO guide, this switch is optional for SPGO. This switch has originals from an extension of SPGO that is called "Context-sensitive Sample PGO with Pseudo-Instrumentation" or simply "CSSPGO". Here is original RFC for the thing, also I can link some LLVM commits/discussions about the topic. This flag is not required for regular SPGO - it's just an improvement idea over regular SPGO, and could be added later to the Rustc in a different process (initially to unstable, than later promoted to stable). But that's another story and we can consider it later.-f[no-]unique-internal-linkage-namesswitch is also mentioned in the Clang PGO guide. I don't think that the switch is applicable to Rustc. Correct me if I am wrong pls.-fsample-profile-use-profi, -fno-sample-profile-use-profiswitch. This switch is also marked as optional in the Clang PGO guide. This switch is an attempt to improve some inaccuracies in SPGO profile with some heuristics. SPGO in Rustc can be easily stabilized without this flag, since it's just a non-critical for regular SPGO usage heuristic. If we decide to add support for this switch to Rustc too - we can do in a separate activity without blocking with stabilization process.-fprofile-sample-accurate, -fauto-profile-accurate, -fno-profile-sample-accurateswitch. This switch is not mentioned even by the Clang PGO guide :) This flag resolves this issue/feature request from LLVM upstream in SPGO use case. According to the description from Clang: "Specifies that the sample profile is accurate. If the sample profile is accurate, callsites without profile samples are marked as cold. Otherwise, treat callsites without profile samples as if we have no profile". Since we don't specify the flag in Rustc, branches without profile samples are now considered as branches without a profile and optimized as regular release code. Having support for this in Rustc would be definitely a nice addition to be on par with Clang, since there are good use cases for that. But I do not think that this could be a blocker for stabilization of SPGO in Rustc without this functionality - we can add it later. As a proof that SPGO in Rustc works completely okay without it you can use Rust-for-Linux bench results with SPGO via AutoFDO.I am not aware about any other SPGO-related flags in Clang.
My personal opinion about the information above. We definitely have a gap in SPGO-related flags in Rustc compared to Clang, but none of these gaps is a blocker for stabilization
-Zdebug-info-for-profiling/-Zprofile-sample-useright now. However, it would be nice to resolve these gaps later: add them in unstable form, test, and later stabilize them to be on par with Clang from SPGO optimization perspective. Right now all the flags above are missing in Rustc even in the unstable form. Stabilization of-Zdebug-info-for-profiling/-Zprofile-sample-usedoes not prevent adding all missing SPGO related features later.Design
Reference
-Zprofile-sample-useand-Zdebug-info-for-profiling#155942 -this PR itselfRFC history
No RFC was created for these options. All original discussions for Unstable were done in the original Unstable PR: #87918
Answers to unresolved questions
No unresolved questions were found in the original PR.
Post-RFC changes
Compared to the unstable (RFC-like) state, I've made the following changes:
-Zdebug-info-for-profilinginto-Cdebuginfo-for-profiling(debug_info->debuginfo). At the time of writing of the original RFC, there was no other stable compiler flag with adebuginfoword, so wording was a bit unclear. Nowadays we havedebuginfoandsplit-debuginfoflags. That's why I decided to change user-facing flag name into-Cdebuginfo_for_profiling- for being consistent with other debuginfo-related flags and not confuse users for nothing. This change shall be adopted by the Rust-for-Linux dev team (if my change will be accepted), but they will need to do some changes anyway due to stabilization - replace-Z...with-C(see current patch) - so it shouldn't be an issue at all.-Zdebug_info_for_profilingswitch to be in the same way as Clang already has. It's kinda difficult to describe clearly, what the option does without exposing too much LLVM details - that's why I linked the documentation to the corresponding LLVM pass in the Reference for this option.Key points
No arguments were raised during stabilization discussion of the feature in any place yet, including Zulip discussion: #t-compiler > Stabilizing Sample PGO (SPGO): `-Zprofile-sample-use`
Nightly extensions
I am not aware of any other unstable SPGO-related switches.
Doors closed
No other proposals/experiments/etc. are affected.
Feedback
Call for testing
No, it wasn't done - it was slightly discussed #t-compiler > Stabilizing Sample PGO (SPGO): `-Zprofile-sample-use` @ 💬.
Right now this feature is already tested personally by me (local experiments with assembly changes verification before/after applying SPGO on an Intel-based (with LBR) Linux machine in some sample apps with
llvm-profgentool and by Rust-for-Linux project in this patch. Additionally, this feature was in unstable state for 5 years (since 2021) with no concerns (due to no bugs or no users - who knows. At least it was implemented for a reason 5 years ).I think that's enough verification for such kind of feature.
Nightly use
The only publicly-known user of this feature is Rust-for-Linux project (see this commit). Besides that, no other users were found on GitHub via GitHub search for
"-Zprofile-sample-use"query: almost all found entries are various copies of the Unstable book with the documentation for the option, and other places are related to the Linux kernel. Two found issues are related to the tracking issue of these two flags, and corresponding tracking issue in Rust-for-Linux.I am not personally aware of any closed-source users of this feature. Probably Google (Sampled-based PGO biggest user at least for C++) and other big techs use it somewhere internally too but it's just a guess.
Implementation
Major parts
-Zprofile-sample-useand-Zdebug-info-for-profiling#155942 - this PR, stabilization with some minor changesNo significant developments on the Rustc side - just propagating in a proper way SPGO profile to the LLVM part of the compiler and enabling an additional LLVM pass, when
-Cdebuginfo-for-profilingis enabled.Coverage
The only tests we have for these two compiler switches are added to the
tests.rsin the PR (they were originally added in the Unstable PR).If more testing is required and it's a blocker for stabilization - it should be discussed.
Outstanding bugs
I am not aware of any SPGO-related bug in Rustc right now.
Outstanding FIXMEs
No FIXMEs left in the code.
Tool changes
No changes are required in other tools for the stabilization.
Some things in the ecosystem like
cargo-pgocan be extended later with providing additional support for SPGO use cases, but definitely it isn't a blocker for the stabilization.Breaking changes
The only thing that probably can be considered as "breaking" is renaming
-Zdebug_info_for_profilinginto-Cdebuginfo_for_profilingthat is described above.No other breaking changes are expected from this stabilization.
Crater report:
Crater analysis:
PRs to affected crates:
Type system, opsem
Compile-time checks
No compile-time checks are required.
Type system rules
Not applicable
Sound by default?
Sound by default. No other additional UB checks are required.
Breaks the AM?
No, it's not possible.
Common interactions
Temporaries
No, it doesn't.
Drop order
No, it doesn't.
Pre-expansion / post-expansion
No, it doesn't.
Edition hygiene
Not applicable.
SemVer implications
No, it doesn't.
Exposing other features
No, there are not.
History
profile-sample-useanddebug-info-for-profiling#155668 - tracking issueAcknowledgments
cargo-pgoauthorI am not aware of any person, who is against stabilization of these two flags.
Open items
I am not aware of any open issue, that is a blocker for stabilization of this feature.
List of SPGO-related things, which are not stabilization blockers in my opinion:
llvm-profgencan be used instead (I've tested it locally by usingllvm-profgen-21for SPGO with Rustc 1.95, which is LLVM 22-based