Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use profile guided optimizations in g++?

Also, can anyone point me to a good tutorial on the subject? I can't find any.

like image 253
nakiya Avatar asked Dec 06 '10 11:12

nakiya


People also ask

How does PGO work?

Rather than programmer-supplied frequency information, profile-guided optimization uses the results of profiling test runs of the instrumented program to optimize the final generated code. The compiler accesses profile data from a sample run of the program across a representative input set.

What is LTO GCC?

Link Time Optimization (LTO) gives GCC the capability of dumping its internal representation (GIMPLE) to disk, so that all the different compilation units that make up a single executable can be optimized as a single module.

What is PGO C++?

Profile-guided optimization (PGO) lets you optimize a whole executable file, where the optimizer uses data from test runs of the .exe or . dll file. The data represents the likely performance of the program in a production environment.

What is PGO in computer?

Profile guided optimization (PGO), also known as profile-directed feedback (PDF), is a compiler optimization technique in computer programming that uses profiling to improve program runtime performance. PGO is supported in IBM® Open XL Fortran for AIX® 17.1.


2 Answers

-fprofile-generate will instrument the application with profiling code. The application will, while actually running, log certain events that could improve performance if this usage pattern was known at compile time. Branches, possibility for inlining, etc, can all be logged, but I'm not sure in detail how GCC implements this.

After the program exits, it will dump all this data into *.gcda files, which are essentially log data for a test run. After rebuilding the application with -fprofile-use flag, GCC will take the *.gcda log data into account when doing its optimizations, usually increasing the performance significantly. Of course, this depends on many factors.

like image 54
Maister Avatar answered Sep 21 '22 20:09

Maister


From this example:

g++ -O3 -fprofile-generate [more params here, like -march=native ...] -o executable_name // run my program's benchmarks, or something to stress its most common path g++ -O3 -fprofile-use [more params here, like -march=native...] -o executable_name 

Basically, you initially compile and link with this extra flag for both compiling and linking: -fprofile-generate (from here).

Then, when you run it, by default it will create .gcda files "next" to your .o files, it seems (hard coded to the full path where they were built).

You can optionally change where it creates these .gcda files with the -fprofile-dir=XXX setting.

Then you re compile and relink using the -fprofile-use parameter, and it compiles it using profile guided goodness.

like image 42
rogerdpack Avatar answered Sep 19 '22 20:09

rogerdpack