[LLVMdev] Determine branch coverage information

Andreas Neustifter astifter-llvm at gmx.at
Wed Nov 4 06:20:51 PST 2009


Hi Ahmed!

Adventure wrote:
> Hello everybody, I am a beginner in LLVM and need to know how to use 
> LLVM to instrument a C program and execute this instrumented program 
> with different test cases to determine the branch coverage information 
> for each test case. Any suggestion or help is more than welcomed. Thanks 
> in advance. Ahmed Raafat.

(In the following instructions you have to insert your own values for the <...> stuff.)

To instrument the C Program you have to compile it into a single bytecode file, I do this by translating each C file to bytecode

$> llvm-gcc -emit-llvm -c <sourcefile> -o <sourcefile>.bc

and then link them all together

$> llvm-ld -stats -time-passes -link-as-library -disable-opt *.bc -o <executable>.1.bc

This gives you an unoptimised bytecode file which is preferable in this case since it retains a somewhat 1:1 relation to your code so you can figure out the results afterwards.



Now its time to instrument the code, I do this by running

$> opt -stats -time-passes -insert-optimal-edge-profiling <executable>.1.bc -o <executable>.2.bc

and add the profiling runtime support

$> llvm-ld -stats -time-passes -link-as-library -disable-opt <path to llvm install>/lib/libprofile_rt.bca <executable>.2.bc -o <executable>.3.bc



You can then create a native executable by

$> llc <executable>.3.bc <executable>.s
$> gcc -g <executable>.s -o <executable>



When you run this executable (with your parameters) it creates a file called llvmprof.out which contains an edge profiling of your code. With

$> llvm-prof -print-all-code <executable>.1.bc

you can dump a bytecode file which is annotated with the recorded profiling information.

If you do several runs of your executable all runs are combined in the single llvmprof.out and the results are accumulated and you can check if every codepath was executed during your tests.



There is a tool in <source tree>/utils/profile.pl which takes a single bytecode file and parameters and does all the work of instrumenting, running and executing llvm-prof in one go, but its a little outdated, I attached a newer version, but I guess its easier to write a script that does exactly the steps you need. YMMV.

Andi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: profile.pl
Type: application/x-perl
Size: 2862 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20091104/ea0970e9/attachment.bin>


More information about the llvm-dev mailing list