[LLVMdev] LLVM instrumentation overhead

John Criswell criswell at illinois.edu
Fri Dec 9 11:21:10 PST 2011

On 12/7/11 4:51 PM, Nipun Arora wrote:
> Hi,
> I need to write a transform pass which instruments the target program to
> output the name of each function executed, and the rdtsc counter along
> with it.

Doing this in LLVM is really straightforward.  You simply iterate 
through all the functions in a module and add instructions to their 
entry basic blocks to do whatever it is that you want to do.

I believe you already know how to find all the functions and their entry 
blocks.  Review the Programmer's Guide and the doxygen docs on 
llvm::Module and llvm::Function if there's something you don't understand.

The only other question is how to insert instructions.  For that, you 
can take one of two approaches.  First, you can use the IRBuilder class 
(http://llvm.org/doxygen/classllvm_1_1IRBuilder.html).  Second, you can 
simply use the appropriate constructor/new methods of the Instruction 
classes to create and insert the instructions that you want.  I believe 
IRBuilder is now the preferred way to do things as its API changes less 

For the instrumentation that you want to do, the easiest thing to do 
would be to insert a call in every function to a function that you 
implement in a run-time library that does whatever the instrumentation 
should do.  This makes the compiler transform very simple.
> Can anyone give me an idea of how to go about it?(I've worked around
> with LLVM pass framework and opt to do static analysis, but would like
> to do a lightweight instrumentation). Also can anyone give an
> approximate idea of the overhead for such instrumentation?

To make things faster, you could compile your run-time library as a 
static library linked in using clang's/libLTO's link-time optimization.  
Your run-time library can then be inter-procedurally inlined with the 
program that you are instrumenting.

-- John T.

> Thanks
> Nipun
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

More information about the llvm-dev mailing list