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

cmd/compile: design and implementation of Profile-Guided Optimization (PGO) #55025

Closed
jinlin-bayarea opened this issue Sep 12, 2022 · 11 comments
Closed
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Milestone

Comments

@jinlin-bayarea
Copy link
Contributor

jinlin-bayarea commented Sep 12, 2022

This proposal provides the detailed design and implementation of Profile-Guided Optimization (PGO) in the Go compiler. It augments the design and implementation aspects of PGO with the high-level issue related to PGO.

Background: Inefficiencies in Go programs can be isolated via profiling tools such as pprof and linux profiler perf. Such tools can pinpoint source code regions where most of the execution time is spent. Unlike other optimizing compilers such as LLVM, the Go compiler does not yet perform Profile-Guided Optimization(PGO). PGO uses information about the code’s runtime behavior to guide compiler optimizations such as inlining, code layout etc. PGO can improve application performance in the range 15-30% [LLVM, AutoFDO]. In this proposal, we extend the Go compiler with PGO.

In this proposal, we incorporate the profiles into the frontend of the compiler to build a call graph with node & edge weights (called WeightedCallGraph). The Inliner subsequently uses the WeightedCallGraph to perform profile-guided inlining which aggressively inlines hot functions. We introduce a profile-guided code specialization pass that is tightly integrated with the Inliner and eliminates indirect method call overheads in hot code paths. Furthermore, we annotate IR instructions with their associated profile weights and propagate these to the SSA-level in order to facilitate profile-guided basic-block layout optimization to benefit from better instruction-cache and TLB performance. Finally, we extend Go's linker to also consume the profiles directly and perform function reordering optimization across package boundaries -- which also helps instruction-cache and TLB performance.

The format of the profile file consumed by our PGO is identical to the protobuf format produced by the pprof tool. This format is rich enough to carry additional hardware performance counter information such as cache misses, LBR, etc. Existing perf_data_converter tool from Google can convert a perf.data file produced by the Linux perf into a profile.proto file in protobuf format.

The first version of the code that performs profile-guided inlining is available here. In summary, we introduce the following flags to the go compiler in our first released version:

**-profileuse <filename>**: filename corresponds to protobuf CPU profile. This flag will build the WeightedCallGraph and use it to perform profile-guided inlining.
**-inlinehotthreshold <string_float>** and **-inlinehotbudget <int>**: These two flags are optional as they have been provided with default values. In advanced settings, these flags can be tuned for controlling code size and performance.

Other PGO optimizations such as code specialization, basic block reordering, and function reordering across packages will be open-sourced in subsequent Go compiler releases.

Detailed design document cam be found here (https://go-review.googlesource.com/c/proposal/+/430398/1/design/55025-pgo-design.md)

@gopherbot gopherbot added this to the Proposal milestone Sep 12, 2022
@seankhliao seankhliao changed the title Proposal: Design and Implementation of Profile-Guided Optimization (PGO) for Go proposal: design and implementation of Profile-Guided Optimization (PGO) Sep 12, 2022
@gopherbot
Copy link

Change https://go.dev/cl/430398 mentions this issue: design/55025-pgo-design.md: add pgo design doc

@erifan
Copy link

erifan commented Sep 13, 2022

Is this a duplicate of #55022 ?

@mvdan
Copy link
Member

mvdan commented Sep 13, 2022

@erifan not as written; see the first paragraph:

It augments the design and implementation aspects of PGO with the high-level issue related to PGO.

Though I agree that it is unconventional to have two proposals. Usually we would have one proposal issue and one detailed proposal document, instead of two issues and one document.

@seankhliao seankhliao added the compiler/runtime Issues related to the Go compiler and/or runtime. label Sep 13, 2022
@ianlancetaylor
Copy link
Contributor

It's fine to have an issue to discuss the design and implementation, but I'm not sure why it has to be a proposal. @jinlin-bayarea is there an API change here that is not covered by #55022? Thanks.

@jinlin-bayarea
Copy link
Contributor Author

jinlin-bayarea commented Sep 14, 2022 via email

@rajbarik
Copy link
Contributor

@ianlancetaylor @aclements any recommendation on where to upload our design and implementation proposal? It is currently under review at https://go-review.googlesource.com/c/proposal/+/430398/1/design/55025-pgo-design.md. Thanks.

@rsc
Copy link
Contributor

rsc commented Sep 28, 2022

We asked Uber to send a design for their PGO to the proposal repo for reference in the Go pgo implementation (#55022). This issue is not necessary to submit that CL, so removing from proposal process.

@aclements
Copy link
Member

@rajbarik , that's the right place for your design doc. Once you've incorporated the proofreading comments and it's +2'd, we can submit it to the design repo.

@rsc
Copy link
Contributor

rsc commented Sep 28, 2022

Removed from the proposal process.
This was determined not to be a “significant change to the language, libraries, or tools”
or otherwise of significant importance or interest to the broader Go community.
— rsc for the proposal review group

@rajbarik
Copy link
Contributor

Thanks. It is up in gerrit.

@ianlancetaylor ianlancetaylor changed the title proposal: design and implementation of Profile-Guided Optimization (PGO) cmd/compile: design and implementation of Profile-Guided Optimization (PGO) Oct 5, 2022
@ianlancetaylor ianlancetaylor modified the milestones: Proposal, Backlog Oct 5, 2022
@prattmic
Copy link
Member

prattmic commented Oct 5, 2022

Closing in favor of #55022, which https://go.dev/cl/430398 now references.

@prattmic prattmic closed this as completed Oct 5, 2022
@golang golang deleted a comment from gopherbot Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

10 participants