使用llvm-prof收集LLVM边缘概要分析

Joh*_*ugo 6 compiler-construction profiling llvm

我正在使用这些命令编译下面的代码,以便在trunk-llvm中收集边/块分析:

clang -emit-llvm -c sort.c -o sort.bc
opt -insert-edge-profiling sort.bc -o sort_prof.bc
clang sort_prof.bc -lprofile_rt -L/llvms/lib -o sort_prof
Run Code Online (Sandbox Code Playgroud)

然后我运行程序并使用llvm-prof sort_prof.bc显示分析信息,结果是:

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

 ##   Frequency
  1. 4.3e+05/708539 main
  2. 2.8e+05/708539 quickSort

  NOTE: 2 functions were never executed!
.....
Run Code Online (Sandbox Code Playgroud)

我的问题是关于执行频率.有没有意义主要执行4.3e + 05次?为什么这样?我正在编译的代码如下.

###################### sort.c ########################
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

const int MAX = 1000000;

void swap(int* a, int* b) {
  int tmp;
  tmp = *a;
  *a = *b;
  *b = tmp;
}

int partition(int vec[], int left, int right) {
  int i, j;

  i = left;
  for (j = left + 1; j <= right; ++j) {
    if (vec[j] < vec[left]) {
      ++i;
      swap(&vec[i], &vec[j]);
    }
  }
  swap(&vec[left], &vec[i]);

  return i;
}

void quickSort(int vec[], int left, int right) {
  int r;

  if (right > left) {
    r = partition(vec, left, right);
    quickSort(vec, left, r - 1);
    quickSort(vec, r + 1, right);
  }
}

int main(void) {

        int vet[MAX], i=0;

        srand(time(NULL));

        for (i=0; i<MAX; i++) {
                vet[i] = rand() % 654321;
        }

        quickSort(vet, 0, MAX-1);

        for (i=0; i<MAX; i++) {
                if ((rand() % 7) > 2) {
                        printf("Num$[%d] = %d\n", i, vet[i]);
                }
                else if ((rand() % 4) > 2) {
                        printf("Num@[%d] = %d\n", i, vet[i]);
                }
                else if ((rand() % 2) > 1) {
                        printf("Num#[%d] = %d\n", i, vet[i]);
                }
        }

        return 0;
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ugo 5

问题是我通过检测传递给llvm-prof bitcode文件,正确的是使用原始文件(没有检测):

llvm-prof sort.bc
Run Code Online (Sandbox Code Playgroud)

与llvm-prof相关的另一个问题是它由于科学记数法而将函数/块执行频率四舍五入.我已经向llvm提交了一个补丁来进行纠正.

另一个提示是默认情况下llvm-prof仅显示前20个最常执行的基本块,并且它不向用户提供任何改变它的方法.我已经提交了另一个补丁,它添加了一个命令行参数,使用户可以在输出中设置他/她想要的基本块数.