---
title: 选项 -fauto-bolt,-fbolt-use,-fbolt-target,-fbolt-option
description: "- -fauto-bolt选项用于使能AutoBOLT优化能力。该优化复用来自插桩反馈优化或自动反馈优化的profile，将其转换为BOLT格式的profile。调用BOLT，自动完成链接后优化。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengdevps/compilation/ug-hgcc/kunpenghgcc_06_0028.html
sourcePath: /source/zh/kunpengdevps/compilation/ug-hgcc/kunpenghgcc_06_0028.html
indexId: 677bed8b37c0e5f5bb9994b22e26a8e5e568500982be44133c303db484b4fa5868
---
# 选项 -fauto-bolt,-fbolt-use,-fbolt-target,-fbolt-option

#### 说明

- -fauto-bolt选项用于使能AutoBOLT优化能力。该优化复用来自插桩反馈优化或自动反馈优化的profile，将其转换为BOLT格式的profile。调用BOLT，自动完成链接后优化。

  - 转换后的profile默认保存在当前路径下。
  - 可以使用-fauto-bolt=PATH指定BOLT profile的保存路径，如-fauto-bolt=/tmp。

- -fbolt-use选项用于直接使用指定的profile完成链接后优化。

  - 默认使用当前路径下文件名为data.fdata的profile完成优化。
  - 可以使用-fbolt-use=FILE指定使用的profile，如-fbolt-use=/tmp/a.fdata。

- -fbolt-target=NAME用于指定BOLT的优化对象，使用该选项后除了NAME之外的二进制和动态库都不会优化。

- -fbolt-option=PARAM用于指定BOLT的优化选项，不同选项以逗号分隔，例如：-fbolt-option="-reorder-blocks=cache+,-reorder-functions=hfsort+"。使用该选项时，必须显式指定PARAM。


#### 使用方法

测试用例test.c代码如下：```
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define ARRAY_LEN 30000
static struct timeval tm1;

static inline void start()
{
    gettimeofday(&tm1, NULL);
}

static inline void stop()
{
    struct timeval tm2;
    gettimeofday(&tm2, NULL);
    unsigned long long t = 1000 * (tm2.tv_sec - tm1.tv_sec) +\
                           (tm2.tv_usec - tm1.tv_usec) / 1000;
    printf("%llu ms\n", t);
}

void bubble_sort (int *a, int n)
{
    int i, t, s = 1;
    while (s)
    {
        s = 0;
        for (i = 1; i < n; i++)
        {
            if (a[i] < a[i - 1])
            {
                t = a[i];
                a[i] = a[i - 1];
                a[i - 1] = t;
                s = 1;
            }
        }
    }
}

void sort_array()
{
    printf("Bubble sorting array of %d elements\n", ARRAY_LEN);
    int data[ARRAY_LEN], i;
    for(i=0; i<ARRAY_LEN; ++i)
    {
        data[i] = rand();
    }
    bubble_sort(data, ARRAY_LEN);
}

int main()
{
    start();
    sort_array();
    stop();
    return 0;
}
```


- profile获取
  使用perf采集profile文件

  1 2 gcc -g -O2 -o test test.c -Wl,-q perf record -e cycles:up -o pmu.data ./test

  AutoBOLT模式获取profile

  1 create_gcov --binary=test --profile=pmu.data --gcov=pmu.gcov --gcov_version=1 --use_lbr=0

  perf2bolt获取profile

  1 perf2bolt -p=pmu.data test -o pmu.fdata -nl


- AutoBOLT模式：
  该模式必须与-fauto-profile或-fprofile-use选项配合使用，并需添加-Wl,-q以保留重定位信息。以test程序为例：

  1 gcc -g -O2 -o test test.c -fauto-profile=pmu.gcov -fauto-bolt -Wl,-q

  或

  1 gcc -g -O2 -o test test.c -fprofile-use -fauto-bolt -Wl,-q

  本次更新中，-fprofile-use和-fauto-profile已支持-flto。典型使用流程如下：

  1 2 3 gcc -g -O2 -o test test.c -fprofile-generate=./profile -Wl,-q ./test gcc -g -O2 -o test test.c -fprofile-use=./profile -fauto-bolt -flto -Wl,-q


- BOLT use模式：
  该模式需要提前准备好BOLT优化所需要的profile。该profile可以使用AutoBOLT模式获取，也可以使用perf2bolt工具获取。

  1 gcc -g -O2 -o test test.c -fbolt-use=pmu.fdata -Wl,-q


- -fauto-bolt必须和-fauto-profile或-fprofile-use选项共同使用
- 当前-fauto-bolt和-fprofile-use支持和-flto共同使用
- -fauto-bolt或-fbolt-use必须与-Wl,-q选项共同使用
- -fbolt-target和-fbolt-option必须和-fauto-bolt或-fbolt-use共同使用
- 系统中需要先安装llvm-bolt软件包
