Erik Rigtorp

Profile-guided optimization

Profile-guided optimization (PGO), also known as profile-directed feedback (PDF), and feedback-directed optimization (FDO) is a compiler optimization technique in computer programming that uses profiling to improve program runtime performance.

GCC PGO example

Build instrumented binary:

gcc foo.c -o foo_instrumented -fprofile-generate

Run instrumented binary with a representative workload:

./foo_instrumented

Then build optimized binary using generated profile:

gcc -O3 foo.c -o foo -fprofile-use=foo.gcda

Clang PGO example

Build instrumented binary:

clang foo.c -o foo_instrumented -fprofile-generate

or

clang foo.c -o foo_instrumented -fprofile-instr-generate

Run instrumented binary with a representative workload:

./foo_instrumented

Then build optimized binary using generated profile:

llvm-profdata merge -ouput foo.profdata default.profraw
clang -O3 foo.c -o foo -fprofile-use=foo.profdata