propeller实操

  1. 首先建立a.c, 为待优化的bin。
//a.c
int fun(int x) {
  if(x>5) return x+1;
  else return x-1;
}

int main() {
  int ans = 0;
  for(int i=0; i<100000000; i++) ans += fun(i);
  return ans;
}
  1. 利用clang生成带bb块映射信息的object和exe
    注意这里clang要使用lld作为
clang a.c -c -fbasic-block-sections=labels -funique-internal-linkage-names  -o a.o
clang a.o -o a_with_bbam

其中funique-internal-linkage-names参数可以提升profile的精度,有助于优化效果。

  1. 使用perf采集程序运行时数据
    注意perf的硬件事件需要运行在实体机上,虚拟机或云主机有问题。
sudo perf record -e cycles:u -j any,u -- ./a_with_bbam
sudo chown wf:wf perf.data

便会生成一个采集lbr硬件提供的分支跳转信息的perf.data文件。

  1. 使用create_llvm_prof转化profile
    create_llvm_prof是autofdo中提供的一个工具,将perf.data的工具转化为clang/lld能识别的优化输入数据。autofdo前期为gcc也做了一个类似的转化工具。

构建create_llvm_prof遇到的一些问题。

  1. llvm-project最好用最新的分支
  2. autofdo的llvm_propeller_profile_writer.cc和perf_data_reader.cc中需要添加"llvm/ADT/Optional.h"头文件
  3. llvm_profile_reader.cc中接口llvm::sampleprof::SampleProfileReader::create()在最新版本的llvm中多了一个文件系统参数,第28行可修改为如下,传入的是实际的文件系统。
 auto reader_or_err = llvm::sampleprof::SampleProfileReader::create(
       filename, C, *llvm::vfs::getRealFileSystem(), discriminator_pass);
/path/to/autofdo/build/create_llvm_prof --format=propeller --binary=a_with_bbam \
--profile=perf.data --out=cluster.txt --propeller_symorder=symorder.txt --profiled_binary_name=./a_with_bbam
  1. 最后一步利用得到的反馈式数据,优化链接编译链接过程
    使用上面生成的cluter.txt和symorder.txt进行优化。
clang -funique-internal-linkage-names -fbasic-block-sections=list=./cluster.txt -c a.c
clang -Wl,--symbol-ordering-file=./symorder.txt -Wl,--no-warn-symbol-ordering -fuse-ld=lld a.o -o a_opt

这里展示列出这个简单问题的cluster.txt和symorder.txt的结果,cluster.txt中的结果

!fun
!!0 1 3
!main
!!0 1 2 3

symorder.txt

fun
main
fun.cold
main.cold
posted @ 2023-04-16 23:45  zwlwf  阅读(337)  评论(0)    收藏  举报