Profile-directed Feedback (PDF)

Profile-directed feedback is a two-stage compilation process that lets you provide the compiler with data characteristic of typical program behavior. An instrumented executable is run in a number of different scenarios for an arbitrary amount of time, producing as a side effect a profile data file. A second compilation using the profile data produces an optimized executable.

PDF should be used mainly on code that has rarely executed conditional error handling or instrumentation. The technique has a neutral effect in the absence of firm profile information, but is not recommended if insufficient or uncharacteristic data is all that is available.

The following diagram illustrates the PDF process.
Profile-directed feedback

The two stages of the process are controlled by the compiler options -qpdf1 and -qpdf2. Stage 1 is a regular compilation using an arbitrary set of optimization options and -qpdf1, that produces an executable or shared object that can be run in a number of different scenarios for an arbitrary amount of time. Stage 2 is a recompilation using the same options, except -qpdf2 is used instead of -qpdf1, during which the compiler consumes previously collected data for the purpose of path-biased optimization.

Using Profile-directed Feedback (PDF)

You can optimize an application based on an analysis of how often it executes different sections of code, as follows:

  1. Compile the application with -qpdf1.
  2. Run the application using a typical data set or several typical data sets. When the application exits, it writes profiling information in the PDF file.
  3. Compile the application with -qpdf2.

If you want to see which functions are used the most often, do the following:

  1. Compile the application with -qpdf1 -qshowpdf.
  2. Run the application using a typical data set or several typical data sets. The application writes more detailed profiling information in the PDF file.
  3. Use the showpdf utility to view the information in the PDF file.

You can take more control of the PDF file generation, as follows:

  1. Compile the application with -qpdf1.
  2. Run the application using a typical data set or several typical data sets. This produces a PDF file in the current directory.
  3. Copy the application to another directory and run it again. This produces a PDF file in the second directory.
  4. Repeat the previous step as often as you want.
  5. Use the mergepdf utility to combine the PDF files into one PDF file. For example, if you produce three PDF files that represent usage patterns that will occur 53%, 32%, and 15% of the time respectively, you can use this command:
      mergepdf -r 53 path1  -r 32 path2  -r 15 path3
    
  6. Compile the application with -qpdf2.

To erase the information in the PDF directory, use the cleanpdf or resetpdf utility.

Optimizing Conditional Branching

The -qpdf option helps to fine-tune the areas around conditional branches so that the default choices correspond to the most likely execution paths. Sometimes instructions from the more likely execution path run before the branch, in parallel with other instructions so that there is no slowdown.

Because the -qpdf option requires some extra compilation overhead and sample execution that uses representative data, you should use it mainly near the end of the development cycle.

Related Information:
IBM Copyright 2003