PGO — opt -instrprof pass programatically?

Hi,

I’m working to add PGO to the IR I’m producing. Currently I’m using opt -instrprof to expand the llvm.instrprof_increment calls into runtime calls. I’d like to be able to do this programmatically instead, and avoid calling an external process.

I gather from what I’ve read online that this is implemented inside of opt and clang using the InstrProf pass, but, frustratingly, I haven’t been able to track down how or where it’s invoked, or even defined. I’m aware of a large amount of the logic in InstrProf.cpp, but that doesn’t have a clear entry point. What I’ve read online always makes use of opt or clang, but does not show how to run this pass programatically.

Is this even possible to do cleanly, or are the implementations closely tied to opt/clang?

If somebody knows of any tutorials or examples of this they could share, that would be very helpful. Otherwise, direction towards the important locations in source would be useful.

Thanks in advance,
Connor

Hi,

I’m working to add PGO to the IR I’m producing. Currently I’m using opt
-instrprof to expand the llvm.instrprof_increment calls into runtime calls.

The intrinsic call won't be lowered into calls, but into counter update
inline sequence.

I’d like to be able to do this programmatically instead, and avoid calling
an external process.

Looks like you want to implement the instrumentation lowering. Why do you
want to do that? Can you just invoke the InstrProfiling (lowering) pass?

I gather from what I’ve read online that this is implemented inside of opt
and clang using the InstrProf pass, but, frustratingly, I haven’t been able
to track down how or where it’s invoked, or even defined. I’m aware of a
large amount of the logic in InstrProf.cpp, but that doesn’t have a clear
entry point. What I’ve read online always makes use of opt or clang, but
does not show how to run this pass programatically.

Is this even possible to do cleanly, or are the implementations closely
tied to opt/clang?

If somebody knows of any tutorials or examples of this they could share,
that would be very helpful. Otherwise, direction towards the important
locations in source would be useful.

You should look at Transforms/Instrumentation/InstrProfiling.cpp. The file
named InstrProf.cpp under ProfileData dir defines common APIs for
instr-gen, lowering, and profile reading/writing.

David