llvmpgo

How to profile a program with llvm tools with llvm 3.5 or greater?


I am looking into using llvm tools to generate block level profile of small programs. It looks like in older versions this was as simple as running:

perl utils/profile.pl -block program.bc

How is profiling done in newer versions of LLVM?


Solution

  • Use Clang and llvm-profdata

    Visit the Clang User's Manual profile with instrumentation

    llvm doc llvm-profdata

    In summary:

    1. Build an instrumented version of the code

      clang -O2 -fprofile-instr-generate code.c
      
    2. Run the instrumented executable to get the profile data file

      ./a.out
      
    3. Combine profiles from multiple runs and format the files by running

      llvm-profdata merge *.profraw -output=code.profdata
      
    4. Build the code again

      clang -O2 -fprofile-instr-use=code.profdata code.c
      

    (Optional?) 5. Display the profile counters for this file and for any of the specified function(s)

        llvm-profdata show -all-functions code.profdata