[LLVMdev] LLVM Profiler uses 32-bit counters for Basic Blocks?

Adam McLaughlin adam27x at gmail.com
Fri Apr 26 12:32:56 PDT 2013


Hi all,

I'm doing some simple profiling with LLVM's profile.pl script in the
llvm/utils/ directory. Some of the applications that I'm profiling are
potentially very large, which in turn leads to some basic blocks being
executed more than ~4 Billion (i.e. 2^32) times. Apparently the internal
counters used within this profiler have only 32 bits as evidenced by the
following simple example:

//test.c
#include "stdio.h"
#include "stdlib.h"

int main(int argc, char **argv)
{
unsigned long i;
unsigned long bb_count=0;
for(i=0;i<4294967296;i++)
{
bb_count++;
}

fprintf(stdout,"BB count: %ld\n",bb_count);

return 0;
}

$ clang -O0 -mllvm -disable-llvm-optzns test.c -emit-llvm -c -o test.bc
$ profile.pl test.bc
BB count: 4294967296
===-------------------------------------------------------------------------===
LLVM profiling output for execution:
test.bc

===-------------------------------------------------------------------------===
Function execution frequencies:

## Frequency
1. 1/1 main

===-------------------------------------------------------------------------===
Top 20 most frequently executed basic blocks:

## %% Frequency
1. 33.3333% 1/3 main() -
2. 33.3333% 1/3 main() -
3. 33.3333% 1/3 main() -



****
The results above say that only three basic blocks are executed, and that
they're all executed exactly one time. Of course, this isn't the case.
Here's the same test without overflow:

//test.c
#include "stdio.h"
#include "stdlib.h"

int main(int argc, char **argv)
{
unsigned long i;
unsigned long bb_count=0;
for(i=0;i<400;i++)
{
bb_count++;
}

fprintf(stdout,"BB count: %ld\n",bb_count);

return 0;
}

$ clang -O0 -mllvm -disable-llvm-optzns test.c -emit-llvm -c -o test.bc
$ profile.pl test.bc
BB count: 400
===-------------------------------------------------------------------------===
LLVM profiling output for execution:
test.bc

===-------------------------------------------------------------------------===
Function execution frequencies:

## Frequency
1. 1/1 main

===-------------------------------------------------------------------------===
Top 20 most frequently executed basic blocks:

## %% Frequency
1. 33.3333% 401/1203 main() -
2. 33.2502% 400/1203 main() -
3. 33.2502% 400/1203 main() -
4. 0.0831255% 1/1203 main() -
5. 0.0831255% 1/1203 main() -

****

Note how now the blocks corresponding to the loop have the correct number
of executions.

Is there an easy way to implement 64-bit counters to solve this problem?
Unfortunately it seems as though the values are compromised by the time
they get to llvm-prof.cpp. Thoughts?

Thanks,

Adam McLaughlin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130426/a6bcc380/attachment.html>


More information about the llvm-dev mailing list