LLVM instrumentation

Hi everyone,

I would like to instrument my code in order to know the number of times a function is called and its execution time.

After some research, I find several leads in llvm/lib/ProfilData/ and llvm/lib/Transform/Instrumentation/ but nothing conclusive.

Does anyone know if this is already possible with LLVM or has a good suggestion for the beginning?

I saw the -fprofile-instr-generate, -fprofile-instr-use, -fprofile-arcs, -ftest-coverage parameters but I can’t get the information I want with it.

It seem that the easiest solution is to write a pass who will instrument the code.

Greetings,

Johan

Have you seen intel pintools? You might not need to write near as much code, however it may not suit your use case because the compiler can see information that otherwise might be missed in the context of a fully compiled binary. Please let me know your thoughts and then we can pick up from there.

Yeah I have already see pintool but I need this to work with ARM and Intel.

If I remember correctly, pintool does not work on ARM (or quite bad).

My goal with the instrumentation is, once I have the information (time + call), to choose (during a second compilation) if some of my passes are applicable or not.

Greetings,

Johan

There’s a good example of a function pass in “Getting Started with LLVM Core Libraries”, but I’m really quite certain that there are already some excellent profiling tools out there. Are you sure that you’re not re-inventing the wheel? Can you tell me more about what the final end goal is?

[…]

My goal with the instrumentation is, once I have the information (time + call), to choose (during a second compilation) if some of my passes are applicable or not.

Profile-guided optimization (PGO) [1] … mumble…mumble. Clang/LLVM has infrastructure for that [2]. I somehow can’t imagine that it doesn’t provide call frequencies and timing. In any case that’s probably where to start.

HTH,

d

[1] Profile-guided optimization - Wikipedia
[2] Clang Compiler User’s Manual — Clang 16.0.0git documentation

The PGO was my first guess but I can get a lot of information.

At first, I follow the explanation at http://clang.llvm.org/docs/UsersManual.html#profiling-with-instrumentation but instead of llvm-profdata merge, I used llvm-profdata show *.profraw.

Sadly, the information I get is the total number of function, the maximum function count and the maximum internal block count.

Do you know if you can tune the -fprofile-instr-generate parameter?

I find another way for doing instrumentation (Check Code Coverage with Clang and LCOV | Logan's Note)

With this, I get the following information :

Function '_ZL8func_addii'
Lines executed:100.00% of 5
Branches executed:100.00% of 2
Taken at least once:100.00% of 2
No calls

Function 'main'
Lines executed:100.00% of 6
Branches executed:100.00% of 2
Taken at least once:100.00% of 2
No calls

File 'test.cpp'
Lines executed:100.00% of 11
Branches executed:100.00% of 4
Taken at least once:100.00% of 4
No calls
test.cpp:creating 'test.cpp.gcov'

With the following code :

static int func_add(int a, int b){
int res = 0;
for(int i = 0; i < 2; ++i){
res += a + b;
}

return res;
}

int main(int argc, char** argv){

int tab[10] = {5, 9, 7, 6, 2, 4, 3, 5, 8, 10};

for(int i = 0; i < 10; ++i){
printf("%d, ",func_add(tab[i],2));
}
    printf("\n");
return 0;
}

I can’t understand the part “taken at leat once”, why main is executed two time?

Greetings,

Johan

The PGO was my first guess but I can get a lot of information.

At first, I follow the explanation at http://clang.llvm.org/docs/UsersManual.html#profiling-with-instrumentation but instead of llvm-profdata merge, I used llvm-profdata show *.profraw.

Try

llvm-profdata show -all-functions -counts *.profraw

In any case the PGO stuff shows how to emit instrumentation and how to evaluate the gathered information in subsequent compilations. That should help to get you started.

d

Thanks for the tips, I will try that :wink: