GCC Static Feedback Optimization User Guide
Introduction
Feedback-directed compilation optimization, commonly known as Profile-Guided Optimization (PGO), is a compilation technique that optimizes program performance by collecting runtime information generated during program execution.
CFGO (Continuous Feature Guided Optimization) is the feedback optimization brand of GCC for openEuler and BiSheng Compiler. It refers to continuous feedback-directed optimization for multimodal files (source code and binaries) and the full lifecycle (compilation, linking, post-linking, runtime, OS, and libraries).
Main optimization points:
- Code layout optimization: Techniques such as basic block reordering, function reordering, and hot/cold partitioning are used to optimize the binary layout of the target program and improve the i-cache and i-TLB hit ratios.
- Advanced compiler optimization: Inlining, loop unrolling, vectorization, and indirect calls benefit from feedback information, enabling the compiler to make more accurate optimization decisions.
This document focuses on the static feedback optimization technologies in CFGO.
Installation and Deployment
Software Requirements
- OS: openEuler 24.03 LTS SP1
- Compiler: GCC 12.3.1-47 or later
Usage
The typical CFGO static feedback optimization process involves three optimization technologies: CFGO-PGO, CFGO-CSPGO, and CFGO-BOLT. CFGO-PGO is mainly used to enable optimizations such as inlining, constant propagation, and devirtualization. CFGO-CSPGO performs basic block reordering and register allocation based on the updated profile obtained after CFGO-PGO. CFGO-BOLT performs post-link binary code layout optimization.
To further improve the optimization effect, you are advised to use the CFGO series of optimizations together with link time optimization, that is, add the -flto=auto compilation option during the CFGO-PGO and CFGO-CSPGO processes.
For the best optimization effect, run the optimizations in the sequence of CFGO-PGO -> CFGO-CSPGO -> CFGO-BOLT.
1. CFGO-PGO
Description: Based on the open source PGO, AI4C tunes some optimization options and parameters to further improve performance.
Option: -fcfgo-profile-generate=${path}
Description: Enables the compilation instrumentation function to generate an instrumented executable program. If ${path} is specified, the program execution information file is written to this path. Otherwise, it is generated in the current path.
Option: -fcfgo-profile-use=${path}
Description: Performs compilation optimization using the program execution information. If ${path} is specified, the program execution information file is read from this path. Otherwise, it is read from the current path.
Typical procedure:
// 1. Compilation instrumentation
gcc -fcfgo-profile-generate=${path} test.c -o test
// 2. Profiling
./test
// 3. Use the profile
gcc -fcfgo-profile-use=${path} test.c -o test2. CFGO-CSPGO
Description: The profile of PGO is context-insensitive, which may result in suboptimal optimization. By adding a CFGO-CSPGO instrumentation optimization process after PGO, the program running information after inlining can be collected, providing more accurate execution information for reordering and register optimization, and ultimately further improving performance.
Option: -fcfgo-csprofile-generate=${path}
Description: Enables the post-inline compilation instrumentation function and must be used together with -fcfgo-profile-use. ${path} must be specified, and the path must be different from the path used in the CFGO-PGO phase.
Option: -fcfgo-csprofile-use=${path}
Description: Optimizes the program using the post-inline profile and must be used together with -fcfgo-profile-use. ${path} must be specified and must be the same as the path of -fcfgo-csprofile-generate=${path}.
Typical procedure:
// 1. Compilation instrumentation
gcc -fcfgo-profile-use={path_1} -fcfgo-csprofile-generate=${path_2} test.c -o test
// 2. Profiling
./test
// 3. Use the profile
gcc -fcfgo-profile-use={path_1} -fcfgo-csprofile-use=${path_2} test.c -o test3. CFGO-BOLT
Description: BOLT is a post-link binary code layout optimizer. This feature incorporates the binary instrumentation capability of the later version, so as to collect running information that is more accurate than sampling to guide binary code layout optimization and improve performance.
Option: -instrument
Description: Enables the software instrumentation function to collect program running information. Relocation information must be contained in the binary (for example, add -Wl,-q during compilation).
In addition to this option, the BOLT instrumentation function contains some other options. For details, see the BOLT instrumentation options section in llvm-bolt --help.
Typical procedure:
// 1. Retain relocation information during program compilation
gcc -Wl,-q test.c -o test
// 2. Perform BOLT instrumentation
llvm-bolt ./test -instrument -o test.inst -instrumentation-file=${test.fdata} --instrumentation-wait-forks --instrumentation-sleep-time=2 --instrumentation-no-counters-clear
// 3. Profiling
./test.inst
// 4. Use the profile
llvm-bolt ./test -o test.opt -data=test.fdataCompatibility Description
This section lists the compatibility issues in some special scenarios. This project is being continuously iterated, and the issues will be fixed as soon as possible. Developers are welcome to join us.
CFGO-CSPGOdoes not support dynamic library optimization currently.