By rchrd on Feb 16, 2009
Another useful optimization option available with Sun Studio compilers is profile feedback.
This option can be especially helpful with codes that contain a lot of branching. The compiler is unable to determine from the source code alone which branches in an IF or CASE statement are the most likely to be taken. Using the profile feedback feature, you can run an instrumented version of the code using typical data to collect statistics on code coverage and branching, and then recompile the code using this collected data.
Darryl Gove has a great description of profile feedback in his book Solaris Application Programming.
With profile feedback, the compiler is better able to do certain optimizations that it cannot do by just analyzing the source code:
- Layout the compiled code so that branches are rarely taken. The most frequent branches "fall-through" to the next memory location, avoiding a fetch and branch to a distant location.
- Inline routines called many times. This avoids costly function calls.
- Move infrequently executed code out of the "hot" parts of the code. This improves utilization of the instruction cache.
- Lots more optimizations based on how variables are and are not utilized, based on the mostly likely paths the program will take
Of course, all these optimizations will depend on the typicality of the test data collected in the profile. Some cases it might be useful to identify a set of "typical data", collect data for each set, and compile multiple versions using each profile. Of course, this all depends on the application.
To use profile feedback, the compilation is in three phases:
- Compile with -xprofile=collect to produce an instrumented executable.
- Run the instrumented executable with a typical data set to create a performance profile.
- Recompile with -xprofile=use and -xO5 to produce the optimized executable
% cc -xO3 -xprofile=collect:/tmp/profile myapp.c
Read about profile feedback in the compiler man pages: C++, C, Fortran