# 配置文件引导优化

**配置文件引导优化**（Profile-guided optimization，PGO），又称**反馈导向优化**（feedback-directed optimization，FDO），是一种编译器技术：它收集程序实际执行时的数据，并据此指导优化决策，将静态分析与观测到的运行时行为结合起来以提升性能。<sup>[1](https://arxiv.org/abs/2507.16649)</sup> 该技术已集成到 GCC、LLVM/Clang 和 [Microsoft Visual C++](https://www.edgepedia.org/zh/microsoft-visual-c) 等主流编译器中，用于指导内联、代码布局等变换。<sup>[2](https://pldb.kirancodes.me/p/journals/pacmpl/FrohlichMP26)</sup>

| 关键事实 | 详情 |
|---|---|
| 其他名称 | 反馈导向优化（FDO）<sup>[1](https://arxiv.org/abs/2507.16649)</sup> |
| 编译器支持 | GCC、LLVM/Clang 和 Microsoft Visual C++<sup>[2](https://pldb.kirancodes.me/p/journals/pacmpl/FrohlichMP26)</sup> |
| 剖析方法 | 插桩与采样<sup>[1](https://arxiv.org/abs/2507.16649)</sup> |
| 优化阶段 | 编译期与链接/链接后阶段<sup>[1](https://arxiv.org/abs/2507.16649)</sup> |
| 典型引导优化 | 内联、代码布局<sup>[2](https://pldb.kirancodes.me/p/journals/pacmpl/FrohlichMP26)</sup> |
| 收益示例 | 使用 PGO 构建 Clang 和 LLVM 可将整体编译时间缩短 20%<sup>[3](https://llvm.org/docs/HowToBuildWithPGO.html)</sup> |
| 主要缺点 | 收集剖析数据成本高、操作繁琐，且需要定期更新以保持数据新鲜<sup>[4](https://arxiv.org/abs/2112.14679)</sup> |

## 工作原理

没有剖析数据的编译器只能猜测哪些分支可能被选中、哪些函数是热点、哪些代码路径很少执行。PGO 用实测数据取代这些猜测。该领域的一篇综述将这项技术描述为利用运行时剖析来指导编译器优化决策，从而有效地将静态分析与实际执行行为结合起来。<sup>[1](https://arxiv.org/abs/2507.16649)</sup>

研究文献中区分了两种剖析方法：插桩（instrumentation），即向程序中加入计数代码；以及采样（sampling），即在不修改程序的情况下收集数据。<sup>[1](https://arxiv.org/abs/2507.16649)</sup> 收集到的剖析数据可以在编译期使用，也可以在链接和链接后阶段使用。<sup>[1](https://arxiv.org/abs/2507.16649)</sup>

## GCC 中的插桩工作流

GCC 的实现展示了经典的三步循环。第一步，使用 `-fprofile-generate` 编译并链接源文件，该选项会启用包括 `-fprofile-arcs`、`-fprofile-values`、`-finline-functions` 和 `-fipa-bit-cp` 在内的插桩选项。<sup>[5](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html)</sup> 第二步，在代表性工作负载上运行插桩后的程序；程序执行期间会记录每个分支和调用的执行次数，并在程序退出时为每个源文件将数据保存到 `.gcda` 文件中。<sup>[5](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html)</sup> 第三步，使用相同的优化选项加上 `-fprofile-use` 重新编译程序，弧数据通过 `-fbranch-probabilities` 为配置文件引导的优化提供输入。<sup>[5](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html)</sup>

GCC 围绕这一循环增加了若干实用控制选项。`-fprofile-update=atomic` 会生成线程安全的计数器更新，防止多线程应用中的剖析数据损坏，其中 `prefer-atomic` 模式会在存在 `-pthread` 时由驱动程序自动选择。<sup>[5](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html)</sup> `-fprofile-filter-files` 和 `-fprofile-exclude-files` 将插桩限制在匹配的文件上，而 `-fprofile-reproducible` 控制所收集剖析数据的可复现程度，这在重新构建发行包时很重要。<sup>[5](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html)</sup>

## 编译器与厂商实现

**LLVM 与 Clang。** LLVM 为其自身工具链记录了 PGO 构建流程：用户报告称，对 Clang 和 LLVM 应用 PGO 可将整体编译时间缩短 20%，该项目还提供了一个脚本 `utils/collect_and_build_with_pgo.py`，它会执行四次 Clang 的干净构建，以生成一个经过配置文件优化的编译器。<sup>[3](https://llvm.org/docs/HowToBuildWithPGO.html)</sup>

**Microsoft Visual C++。** MSVC 链接器通过 `/USEPROFILE` 选项配合 `/LTCG`（链接时代码生成）来使用 PGO 训练数据。<sup>[6](https://learn.microsoft.com/en-us/cpp/build/reference/useprofile?view=msvc-170)</sup> MSVC 还引入了基于采样的 PGO：SPGO 随 MSVC Build Tools 14.51 版本发布，包含在 [Visual Studio](https://www.edgepedia.org/zh/visual-studio) 2026 版本 18.6 中。如果在收集剖析数据之后、优化编译之前修改了代码，SPGO 会继续将已收集的剖析信息应用于未被修改的例程，以及控制流图保持不变的例程。<sup>[7](https://devblogs.microsoft.com/cppblog/introducing-sample-profile-guided-optimization-in-msvc/)</sup>

## 剖析数据维护问题

PGO 的核心成本在于剖析数据本身。2021 年一项机器学习研究的作者将收集剖析数据描述为成本高昂、操作繁琐，且需要定期更新以保持数据新鲜。<sup>[4](https://arxiv.org/abs/2112.14679)</sup> 数据陈旧有两个来源。训练工作负载可能偏离程序的实际使用方式；优化器自身的变换会改变程序的控制流，随着编译的推进，已收集的剖析数据会变得陈旧或不准确。<sup>[2](https://pldb.kirancodes.me/p/journals/pacmpl/FrohlichMP26)</sup>

**机器学习替代方案。** 纳达夫·罗特姆（Nadav Rotem）和克里斯·康明斯（Chris Cummins）提出用统计方法推断分支概率，而不是直接测量：在大量携带分支概率信息的二进制文件语料库上离线训练模型，然后编译器为普通的未插桩程序预测分支概率。该技术已直接集成到 LLVM 中，补充了现有的人工设计启发式规则，部署时无需运行剖析，对编译时间的影响也可忽略不计。<sup>[4](https://arxiv.org/abs/2112.14679)</sup>

**剖析数据传播。** 在 OOPSLA 2026 上发表的研究针对第二个陈旧来源，让剖析数据在优化之后被复用，而不是重新运行程序，考察了两种互补技术：预测（prediction），即预测优化后代码中哪些路径可能成为热点；以及投影（projection），即将剖析细节从程序原始控制流图迁移到变换后生成的控制流图。在评估的技术中，一种递归比较代码区域操作码直方图的轻量级方法，始终比基于 GPT-4o 的大语言模型方法以及 LLVM 和 BOLT 二进制优化器中实现的现有预测与投影技术更为准确。<sup>[2](https://pldb.kirancodes.me/p/journals/pacmpl/FrohlichMP26)</sup>

## 基于硬件的 PGO

PGO 也可以在没有源码级插桩的情况下运行。英特尔的基于硬件的配置文件引导优化（Hardware-based Profile Guided Optimization，HWPGO）利用内置于 Arrow Lake Refresh CPU 及后续英特尔处理器中的硬件性能计数器来捕获缓存未命中、分支预测错误、自旋锁和硬件中断等事件。一套专有工具链监控这些指标，一个转换层在运行时重写生产二进制文件中效率低下的指令，从而避免说服开发者为某一特定架构重新构建程序。对英特尔二进制优化工具（Binary Optimization Tool，iBOT）的测试显示，游戏性能最高提升 18%，平均提升 8%。<sup>[8](https://www.tomshardware.com/pc-components/cpus/intels-binary-optimization-tool-tested-and-explained-how-the-ibot-translation-delivers-up-to-18-percent-faster-gaming-performance-8-percent-on-average)</sup>

## 超越速度：精度调优

剖析信息指导的不仅仅是指令调度。在数值软件中，TAFFO 工具中实现的一种配置文件引导的精度调优方法在 PolyBench/C 基准上与最坏情况静态标注进行了比较：配置文件引导的方法在提供相当加速比的同时，误差最多可降低 4 个数量级，且对程序员专业知识的要求更低。<sup>[9](https://explore.openaire.eu/search/result?pid=10.1145%2F3605098.3636080)</sup>

## References

1. [From Profiling to Optimization: Unveiling the Profile Guided Optimization, alphaXiv survey](https://arxiv.org/abs/2507.16649)
2. [Fröhlich, Moreira, Pereira (2026). Automatic Propagation of Profile Information through the Optimization Pipeline. PACMPL vol. 10, OOPSLA1.](https://pldb.kirancodes.me/p/journals/pacmpl/FrohlichMP26)
3. [How To Build Clang and LLVM with Profile-Guided Optimizations, LLVM documentation](https://llvm.org/docs/HowToBuildWithPGO.html)
4. [Rotem, Cummins (2021). Profile Guided Optimization without Profiles: A Machine Learning Approach. arXiv:2112.14679.](https://arxiv.org/abs/2112.14679)
5. [Instrumentation Options, Using the GNU Compiler Collection (GCC)](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html)
6. [/USEPROFILE (Use PGO data with LTCG), Microsoft Learn](https://learn.microsoft.com/en-us/cpp/build/reference/useprofile?view=msvc-170)
7. [Introducing Sample Profile Guided Optimization in MSVC, C++ Team Blog](https://devblogs.microsoft.com/cppblog/introducing-sample-profile-guided-optimization-in-msvc/)
8. [Intel's Binary Optimization Tool tested and explained, Tom's Hardware](https://www.tomshardware.com/pc-components/cpus/intels-binary-optimization-tool-tested-and-explained-how-the-ibot-translation-delivers-up-to-18-percent-faster-gaming-performance-8-percent-on-average)
9. [Denisov et al. (2024). Profiling vs Static Analysis: The Impact on Precision Tuning. ACM SAC 2024.](https://explore.openaire.eu/search/result?pid=10.1145%2F3605098.3636080)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains › Code generation and optimization techniques

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/profile-guided-optimization

English original: https://www.edgepedia.org/profile-guided-optimization
