# 性能分析引导的动态重编译

性能分析引导的动态重编译（profile-guided dynamic recompilation）是指在程序运行期间重新编译程序代码，利用运行时对执行行为的测量来决定重编译哪些代码以及如何优化。它结合了两个通常相互独立的思想：性能分析引导优化（PGO），即编译器利用测量得到的执行统计数据来指导优化决策；以及动态重编译，即在执行期间而非提前对代码进行翻译或重新生成。<sup>[1](https://arxiv.org/abs/2507.16649)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心思想 | 运行时性能分析识别热代码路径；重编译器根据观测到的行为选择优化方案，重新生成这些路径<sup>[1](https://arxiv.org/abs/2507.16649)</sup> |
| 分析单元 | Dynamo 是此类早期系统之一，它优化踪迹——即连续执行指令的动态序列——并将其作为可重定位代码发射到片段缓存中<sup>[2](https://www.complang.tuwien.ac.at/andi/bala.pdf)</sup> |
| 分析方法 | 运行时系统可通过解释执行、软件计数器或性能监控单元（PMU）事件的硬件采样来进行性能分析<sup>[2](https://www.complang.tuwien.ac.at/andi/bala.pdf)</sup><sup> • </sup><sup>[3](https://www.intel.com/content/www/us/en/developer/articles/technical/hwpgo.html)</sup> |
| 开销 | 硬件采样分析的开销约为 1%，低到足以在生产工作负载上收集数据<sup>[3](https://www.intel.com/content/www/us/en/developer/articles/technical/hwpgo.html)</sup> |
| 工作负载优势 | 与传统 PGO 不同，动态编译无需提前选择代表性训练工作负载<sup>[4](https://www-file.huawei.com/-/media/corp2020/pdf/publications/huawei-research/2024/huawei-research-issue6-en.pdf)</sup> |
| 硬件反馈 | PMU 采样可以构建分支误预测剖面，英特尔 2024.0 编译器利用该剖面优先选择条件移动（cmov）指令而非分支<sup>[3](https://www.intel.com/content/www/us/en/developer/articles/technical/hwpgo.html)</sup> |

## 循环如何运作

标准的 PGO 流水线是一个三步循环：程序经过插桩编译或准备进行采样；用代表性工作负载执行程序以收集运行时统计数据，即性能剖面；编译器利用该剖面重新优化代码。<sup>[1](https://arxiv.org/abs/2507.16649)</sup> 仅靠静态分析往往无法预测动态执行行为，例如哪些分支经常被采取，或哪些代码段执行最频繁——即热路径。<sup>[1](https://arxiv.org/abs/2507.16649)</sup>

动态重编译将此循环压缩为单次运行。运行时在程序执行过程中观察其行为，识别出主导执行时间的代码，并在程序继续运行的同时对其重新编译。由于性能剖面来自实际执行，运行时无需单独选择代表性工作负载，从而省去了传统提前编译 PGO 所需的一个步骤。<sup>[4](https://www-file.huawei.com/-/media/corp2020/pdf/publications/huawei-research/2024/huawei-research-issue6-en.pdf)</sup>

**优化什么。** 动态重编译器寻找仅在运行时上下文中出现的优化机会：跨越过程调用、返回、虚函数调用、间接分支和动态链接函数调用等静态程序边界的冗余。指令缓存的使用是另一个目标，因为动态连续的热执行指令序列在二进制文件中可能在静态上并不连续。<sup>[2](https://www.complang.tuwien.ac.at/andi/bala.pdf)</sup>

## 运行时性能分析

**基于解释的分析。** Dynamo 由惠普实验室（HP Labs）于 1990 年代末开发，通过软件解释应用程序的指令流来获得控制权，并利用该解释来观察执行行为，而无需对应用程序二进制进行插桩或其他扰动。<sup>[2](https://www.complang.tuwien.ac.at/andi/bala.pdf)</sup><sup> • </sup><sup>[5](https://www.sciencedirect.com/topics/computer-science/dynamic-binary-translation)</sup> 解释执行开销很大，但它避免干扰使用定时器中断的应用程序，允许直接选择热区域而无需整理采样统计数据，并提供确定性的踪迹选择方案。Dynamo 的设计者在热踪迹识别上追求可预测性而非精确性，并保持计数器更新和存储量较低，因为两者都会增加开销和内存占用。<sup>[2](https://www.complang.tuwien.ac.at/andi/bala.pdf)</sup>

一旦选定热踪迹，Dynamo 便对其进行优化并将可重定位代码发射到片段缓存中；退出优化代码的分支跳转到位于底部的出口存根。<sup>[2](https://www.complang.tuwien.ac.at/andi/bala.pdf)</sup>

**硬件采样分析。** 英特尔的硬件性能分析引导优化（HWPGO）自 2024.0 版本起包含在 [oneAPI](https://www.edgepedia.org/zh/oneapi) DPC++/C++ 编译器中，它使用 Linux `perf` 或 SEP 等工具对性能监控单元事件进行采样，以替代插桩；根据 PMU 数据和调试信息生成性能剖面。<sup>[6](https://www.intel.com/content/www/us/en/docs/dpcpp-cpp-compiler/developer-guide-reference/2024-2/hardware-profile-guided-optimization.html)</sup> 作为基于采样的 PGO 的一种形式，其分析开销远低于基于插桩的 PGO：PMU 计数器在事件每发生 N 次时触发一次采样，因此大量在线聚合由专用硬件完成，开销通常在 1% 量级。每次采样捕获最后分支记录（Last Branch Records），实际上形成指令踪迹。<sup>[3](https://www.intel.com/content/www/us/en/developer/articles/technical/hwpgo.html)</sup>

硬件采样还提供软件插桩无法提供的反馈。当硬件误预测分支时，PMU 可以触发采样，生成误预测剖面；结合执行频率剖面，编译器可以看到误预测比率。在 2024.0 英特尔编译器中，当分支预测效果不佳时，此类剖面可引导编译器消除控制流，转而使用条件移动指令。<sup>[3](https://www.intel.com/content/www/us/en/developer/articles/technical/hwpgo.html)</sup>

## 应用场景

**托管语言运行时。** 动态编译用于托管静态类型语言（如 Java 和 C#），以及动态类型语言（如 [JavaScript](https://www.edgepedia.org/zh/javascript)），程序翻译延迟到执行时才进行。<sup>[4](https://www-file.huawei.com/-/media/corp2020/pdf/publications/huawei-research/2024/huawei-research-issue6-en.pdf)</sup>

**二进制翻译与仿真。** 动态二进制翻译是仿真器以及性能分析、缺陷检测、程序分析和安全工具的基础。<sup>[5](https://www.sciencedirect.com/topics/computer-science/dynamic-binary-translation)</sup> QEMU 使用其微型代码生成器（Tiny Code Generator），通过中间表示翻译客户机代码；一个概念验证仿真器在 RISC-V、x86 和 ARM 等常见配对架构之间直接翻译，运行速度比使用 TCG 的 QEMU 快达 35 倍，表明这些中间步骤增加了多少开销。<sup>[7](https://arxiv.org/abs/2501.03427)</sup> 动态生成的代码带来了特殊挑战，因为翻译器必须在 JIT 重新生成代码时保持翻译后的应用程序与源程序一致；在 DynamoRIO 中实现的技术自动推断 JIT 的存在，并对其写指令插桩以执行翻译一致性操作，使 JIT 应用程序的性能比基础 DynamoRIO 和 Pin 提升多达 7.3 倍。<sup>[8](https://research.google/pubs/optimizing-binary-translation-for-dynamically-generated-code/)</sup>

**链接后二进制优化。** 英特尔的二进制优化工具（iBOT）将同样的思想应用于已发布的软件。CPU 中的硬件性能计数器在生产二进制运行时捕获缓存未命中、分支误预测、自旋锁和硬件中断；英特尔通过运行时翻译层重写低效指令，将优化打包为性能剖面并分发给用户。测试报告称游戏性能提升高达 18%，平均提升 8%。<sup>[9](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> iBOT 不进行指令集之间的翻译；它优化 x86 应用程序以使用最高效的指令，一些已经充分优化的应用程序则看不到收益。<sup>[10](https://www.tomshardware.com/pc-components/cpus/intel-expands-new-game-boosting-ibot-software-with-seven-more-games-up-to-a-27-percent-improvement-team-blue-claims-12-percent-average-jump-in-newly-supported-titles)</sup>

## 权衡

动态重编译以运行时工作为代价换取性能剖面的保真度。重编译器在程序运行时花费周期进行观察、翻译和优化，而动态翻译器必须处理静态编译从未面对的情况，如自修改代码和运行时生成的代码。<sup>[2](https://www.complang.tuwien.ac.at/andi/bala.pdf)</sup><sup> • </sup><sup>[8](https://research.google/pubs/optimizing-binary-translation-for-dynamically-generated-code/)</sup> 提前编译 PGO 避免了这种开销，但依赖于训练工作负载与真实使用情况相似；如果程序发生变化，编译器可以警告动态剖面不再对应于修改后的函数。<sup>[11](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/profile-guided-optimization.html)</sup> 硬件采样分析从另一个方向缩小了差距，将收集开销降至约 1%，并允许在生产环境中对生产二进制收集性能剖面。<sup>[3](https://www.intel.com/content/www/us/en/developer/articles/technical/hwpgo.html)</sup><sup> • </sup><sup>[6](https://www.intel.com/content/www/us/en/docs/dpcpp-cpp-compiler/developer-guide-reference/2024-2/hardware-profile-guided-optimization.html)</sup>

## References

1. [From Profiling to Optimization: Unveiling the Profile Guided Optimization, alphaXiv survey](https://arxiv.org/abs/2507.16649)
2. [Bala, Duesterwald, Banerjee. Dynamo: A Transparent Dynamic Optimization System](https://www.complang.tuwien.ac.at/andi/bala.pdf)
3. [Boost Performance with Hardware Counter Assisted Profile Guided Optimization (HWPGO), Intel](https://www.intel.com/content/www/us/en/developer/articles/technical/hwpgo.html)
4. [Huawei Research Issue 6 (2024)](https://www-file.huawei.com/-/media/corp2020/pdf/publications/huawei-research/2024/huawei-research-issue6-en.pdf)
5. [Dynamic Binary Translation, ScienceDirect Topics overview](https://www.sciencedirect.com/topics/computer-science/dynamic-binary-translation)
6. [Hardware Profile-Guided Optimization, Intel oneAPI DPC++/C++ Compiler Developer Guide](https://www.intel.com/content/www/us/en/docs/dpcpp-cpp-compiler/developer-guide-reference/2024-2/hardware-profile-guided-optimization.html)
7. [Boosting Cross-Architectural Emulation Performance by Foregoing the Intermediate Representation Model, arXiv:2501.03427](https://arxiv.org/abs/2501.03427)
8. [Hawkins et al. Optimizing Binary Translation for Dynamically Generated Code, CGO 2015](https://research.google/pubs/optimizing-binary-translation-for-dynamically-generated-code/)
9. [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)
10. [Intel expands new game-boosting iBOT software with seven more games, Tom's Hardware](https://www.tomshardware.com/pc-components/cpus/intel-expands-new-game-boosting-ibot-software-with-seven-more-games-up-to-a-27-percent-improvement-team-blue-claims-12-percent-average-jump-in-newly-supported-titles)
11. [Profile-Guided Optimization, Intel Fortran Compiler Developer Guide](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/profile-guided-optimization.html)

---
*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-dynamic-recompilation

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