<div dir="ltr"><br><div>Hi all,</div><div><br></div><div>I'm doing some simple profiling with LLVM's <a href="http://profile.pl">profile.pl</a> 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:</div>
<div><br></div><div>//test.c</div><div>#include "stdio.h"<br>#include "stdlib.h"<br><br>int main(int argc, char **argv)<br>{<br>  unsigned long i;<br>      unsigned long bb_count=0;<br>     for(i=0;i<4294967296;i++)<br>
        {<br>             bb_count++;<br>   }<br><br>   fprintf(stdout,"BB count: %ld\n",bb_count);<br><br>       return 0;<br>}</div><div><br></div><div>$ clang -O0 -mllvm -disable-llvm-optzns test.c -emit-llvm -c -o test.bc</div><div>$ <a href="http://profile.pl">profile.pl</a> test.bc<br>
BB count: 4294967296<br>===-------------------------------------------------------------------------===<br>LLVM profiling output for execution:<br>  test.bc <br><br>===-------------------------------------------------------------------------===<br>
Function execution frequencies:<br><br> ##   Frequency<br>  1.     1/1 main<br><br>===-------------------------------------------------------------------------===<br>Top 20 most frequently executed basic blocks:<br><br> ##      %%  Frequency<br>
  1. 33.3333%     1/3   main() - <br>  2. 33.3333%     1/3        main() - <br>  3. 33.3333%     1/3        main() -</div><div><br></div><div><br></div><div><br></div><div>****</div><div>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:</div>
<div><br></div><div>//test.c</div><div>#include "stdio.h"<br>#include "stdlib.h"<br><br>int main(int argc, char **argv)<br>{<br>  unsigned long i;<br>      unsigned long bb_count=0;<br>     for(i=0;i<400;i++)<br>
        {<br>             bb_count++;<br>   }<br><br>   fprintf(stdout,"BB count: %ld\n",bb_count);<br><br>       return 0;<br>}</div><div><br></div><div>$ clang -O0 -mllvm -disable-llvm-optzns test.c -emit-llvm -c -o test.bc</div><div>$ <a href="http://profile.pl">profile.pl</a> test.bc<br>
BB count: 400<br>===-------------------------------------------------------------------------===<br>LLVM profiling output for execution:<br>  test.bc <br><br>===-------------------------------------------------------------------------===<br>
Function execution frequencies:<br><br> ##   Frequency<br>  1.     1/1 main<br><br>===-------------------------------------------------------------------------===<br>Top 20 most frequently executed basic blocks:<br><br> ##      %%  Frequency<br>
  1. 33.3333%   401/1203        main() - <br>  2. 33.2502%   400/1203     main() - <br>  3. 33.2502%   400/1203     main() - <br>  4. 0.0831255%     1/1203   main() - <br>  5. 0.0831255%     1/1203   main() - <br></div><div><br></div><div>****</div>
<div><br></div><div>Note how now the blocks corresponding to the loop have the correct number of executions.</div><div><br></div><div>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?</div>
<div><br></div><div>Thanks,</div><div><br></div><div>Adam McLaughlin</div></div>