instrument a byte code with llvm

hi,
I’m newer in llvm
i would like to instrument a byte code with a pass and as a result i would like to get an instrumented byte code.
i would like that a pass add a method (which calculate the number of instructions) in the end of each block.
the instrumented code should contain in each block a method that calculate the number of instructions
Please Help me
thank you

hi,
I’m newer in llvm
i would like to instrument a byte code with a pass and as a result i would like to get an instrumented byte code.

To rephrase, you want to instrument a program so that, when you run it, it computes the number of instructions executed at run-time. Is this correct?

By number of instructions, do you mean LLVM instructions or native code instructions?

i would like that a pass add a method (which calculate the number of instructions) in the end of each block.
the instrumented code should contain in each block a method that calculate the number of instructions

Have you read the documentation from the web site:

How to Write an LLVM Pass
LLVM Programmer’s Manual
LLVM Language Reference Manual (you just need to skim it to get a basic understanding of what the IR looks like)

– John T.

Okay. That’s going to make things more difficult but I would think is doable.

So, the instrumentation at the LLVM IR is easy: you simply add a CallInst (i.e., a call instruction) at the end of each basic block (technically, before the BasicBlock’s terminator instruction) that calls an external function. This external function will be implemented in a run-time library that you link with the program after it is compiled with LLVM.

The tricky part is to get the count of native code instructions. For that, I think you’ll need to write a MachineFunctionPass (). This sort of pass is part of the code generator and works on the IR representing the generated code. You will need to have it count the instructions and then modify the call instruction so that it passes the count of machine instructions to the run-time library. I haven’t written a MachineFunctionPass before, so others who have will need to answer questions on it. However, the doxygen docs and the existing MachienFunctionPass passes in LLVM may be a good way to learn how they work. Good luck. – John T.

John Criswell <criswell@illinois.edu> writes:

            hi,
            I'm newer in llvm
            i would like to instrument a byte code with a pass and as a result i would like to get an instrumented byte
            code.

        To rephrase, you want to instrument a program so that, when you run it, it computes the number of instructions
        executed at run-time. Is this correct?
       
        By number of instructions, do you mean LLVM instructions or native code instructions?

    native code instruction a .bc code

Hi Nabila :wink:

That reminds me a lot the work of Patrice Gerin:

http://tima.imag.fr/tima/fr/mediatheque/PhDthesisresult_id316.html

and

@conference{bouchhima2009automatic,
  title={{Automatic instrumentation of embedded software for high level
  hardware/software co-simulation}},
  author={Bouchhima, A. and Gerin, P. and P{\'e}trot, F.},
  booktitle={Proceedings of the 2009 Asia and South Pacific Design
  Automation Conference},
  pages={546--551},
  year={2009},
  organization={IEEE Press}
}

hi,

My work consist in :
1)translating the c code program into a bc code with llvm-gcc (done)
2)write a pass to be used to instrument the bc code resulted from 2 ( i will use insert block profiling pass so done )
3)Use LLVM’s llc program to generate C or assembly code of your instrumented program (not yet).
4) Compile the C/asm code to native code with gcc and link it with any native code libraries that it needs.
5) Run the program and gather the information from your instrumentation instructions.
wihich i find the same with this article

http://old.nabble.com/Determine-branch-coverage-information-td26141617.html#a26440560

but the last command in that article doesn’t works fr me:
llvm-prof -print-all-code .1.bc
llvm-prof: Error opening ‘llvmprof.out’: No such file or directory

i found that profile.pl which takes a single bytecode file and parameters could do all the work of instrumenting, running and executing llvm-prof in one go.
but i try this :

sudo /usr/local/llvm-2.8/utils/profile.pl -edge hello.bc
Error opening ‘/usr/lib/llvm-2.8/lib/profile_rt.so’: /usr/lib/llvm-2.8/lib/profile_rt.so: cannot open shared object file: No such file or directory
-load request ignored.
LLVM ERROR: Program used external function ‘llvm_start_edge_profiling’ which could not be resolved!

and when i tried to make each command in the profile.pl manually step by step i found this problems

opt -q -f -insert-block-profiling hello.bc -o result.bc.inst
opt: Unknown command line argument ‘-insert-block-profiling’. Try: ‘opt -help’

thanks