# 预取（计算机）

预取是计算机体系结构中的一种技术：在处理器实际请求之前，将数据或指令从内存取入缓存，从而把内存访问的延迟隐藏在其它工作之后。它之所以存在，是因为一次缓存缺失可能耗费数十到数百个处理器周期，而对于工作集超出片上存储容量的程序，仅靠缓存无法消除缺失。预取可由硬件完成——硬件在运行时观察内存访问流并预测未来的访问；也可由软件完成——编译器或程序员插入显式的预取指令。<sup>[1](https://arxiv.org/abs/2009.00715)</sup>

| 关键事实 | 详情 |
|---|---|
| 目的 | 预测未来的内存访问，在需求发生之前将数据取入片上缓存，隐藏漫长的内存延迟<sup>[1](https://arxiv.org/abs/2009.00715)</sup> |
| 两大类 | 硬件预取：由硬件独自决定预取什么、何时预取、预取到哪里；软件预取：由编译器或用户插入预取指令<sup>[2](https://www.researchgate.net/publication/247924889_Data_prefetching_on_the_HP_PA8000)</sup> |
| 模式类型 | 规则（步长和空间）预取器用于许多商用处理器；时间预取器针对不规则、重复的访问序列<sup>[3](https://developer.arm.com/community/arm-research/b/articles/posts/making-temporal-prefetchers-practical--the-misb-prefetcher)</sup> |
| 页限制 | 英特尔硬件预取器在 4 KB 页边界内工作，不跨页取数据<sup>[4](https://www.intel.com/content/www/us/en/developer/articles/technical/qcd-performance-optimization-with-hbm.html)</sup> |
| 实测收益 | HP PA-8000 上的软件预取使 SPECfp95 程序最高加速 100%<sup>[2](https://www.researchgate.net/publication/247924889_Data_prefetching_on_the_HP_PA8000)</sup> |
| 主要风险 | 不准确的预取浪费内存带宽，并可能逐出程序很快需要的缓存行，导致性能下降<sup>[5](https://documentation-service.arm.com/static/64a3f23238511951cb79a6cb?token=)</sup> |

## 预取为何存在

一次缓存缺失会使流水线停顿，停顿时间等于到内存层次下一级的往返时间。在 180 MHz 的 HP PA-8000 上，一次缓存缺失的最低代价超过 50 个周期，在四路超标量机器上，单次缺失的代价相当于 200 多条指令的工作量。<sup>[2](https://www.researchgate.net/publication/247924889_Data_prefetching_on_the_HP_PA8000)</sup> 非阻塞缓存、写缓冲区和乱序执行通过重叠缺失来容忍一部分延迟，但它们受限于缺失处理缓冲区的大小。预取则更早地着手解决这个问题：它在数据被需要之前，将数据从内存层次的较高层移到较低层，使需求访问能够在缓存中命中。<sup>[2](https://www.researchgate.net/publication/247924889_Data_prefetching_on_the_HP_PA8000)</sup>

## 硬件预取

**硬件预取器**对应用程序是透明的。它们在运行时观察内存访问序列，识别接下来将被请求的地址，并在处理器发出请求之前将其取入缓存。<sup>[3](https://developer.arm.com/community/arm-research/b/articles/posts/making-temporal-prefetchers-practical--the-misb-prefetcher)</sup> 如今几乎所有高性能处理器都集成了多个数据预取器，以捕捉不同的访问模式。<sup>[1](https://arxiv.org/abs/2009.00715)</sup>

英特尔 Sandy Bridge 微架构展示了典型的布局。两个预取器为 L1 数据缓存提供数据：数据缓存单元（DCU）预取器，又称流式预取器，由对最近加载数据的升序访问触发，取入下一行；以及基于指令指针的步长预取器，它跟踪各条加载指令，当某条加载呈现规则步长时，预取将步长加到当前地址上所形成的地址，可向前或向后，步长最大 2K 字节。另有两只预取器为 L2 和末级缓存提供数据：空间预取器用配对行补全每个已取入的行，形成 128 字节对齐的块；以及流预取器，它监视升序和降序请求序列，最多可领先加载请求 20 行，维护最多 32 条流，每个 4 KB 页有一条前向流和一条后向流。<sup>[6](https://www.intel.com/content/dam/doc/manual/64-ia-32-architectures-optimization-manual.pdf)</sup> 在英特尔 E 核上，L1 还包含一个下一页预取器，可降低页表遍历的开销；从 Crestmont 微架构起，还包含一个指针数组预取器，它通过自行生成第二次加载的地址来检测指针追逐。<sup>[7](https://intel.github.io/optimization-zone/hardware/HWPrefetchTuning/)</sup>

Arm 核心遵循同样的模式。Cortex-A35 的 L1 数据侧内存系统运行一个自动预取器，它监视缓存缺失，识别大约四条缓存行范围内固定步长的缺失序列；其间夹杂的命中不会干扰识别。Cortex-A715 的 L2 内存系统包含预取引擎，同时使用虚拟地址和程序计数器，可预取到 L2 和 L3。<sup>[8](https://support.arm.com/documentation/100236/0002/functional-description/l1-memory-system/about-data-prefetching)</sup><sup> • </sup><sup>[9](https://support.arm.com/documentation/101590/0103/L2-memory-system?lang=en)</sup>

**哪个预取器更重要。** 一项在英特尔至强 Skylake-SP 上对 SPEC CPU2006 和 CPU2017 进行的特性分析研究发现，L2 流预取器（L2P）是四个硬件预取器中效果最显著的：对大多数基准测试而言，仅它一个就贡献了启用全部预取器所获 CPI 降幅的 90% 以上；而 L2 空间预取器在两个测试套件中分别只对 8 个和 6 个基准测试实现了超过 5% 的 CPI 降幅。硬件预取的准确性也很高，每个套件中只有三个基准测试的内存带宽消耗显著上升。<sup>[10](https://pmc.ncbi.nlm.nih.gov/articles/PMC6675054/)</sup>

## 软件预取

在软件预取中，编译器（有时也可能是程序员）将预取指令插入代码中。<sup>[2](https://www.researchgate.net/publication/247924889_Data_prefetching_on_the_HP_PA8000)</sup> HP PA-8000 将向通用寄存器零的普通加载定义为预取指令，它把所寻址的 32 字节行取入数据缓存；这类加载在其引发的缺失得到处理之前就退出了，不会产生陷阱。HP-UX 10.2 的编译器实现了一种由编译器选项控制的预取生成算法，SPECfp95 的性能提升幅度达到 100%。<sup>[2](https://www.researchgate.net/publication/247924889_Data_prefetching_on_the_HP_PA8000)</sup> PA-8000 的编译器在具有规则访问步长的循环中插入预取，根据预期的循环迭代延迟和缓存缺失延迟计算出应提前多少个迭代进行预取。<sup>[11](https://www.researchgate.net/publication/3687706_Compiler_optimizations_for_the_PA-8000)</sup>

现代指令集直接暴露了同样的思想。Arm 提供 `PLD` 和 `PRFM` 预加载指令，它们查询缓存，并在缺失时启动行填充，行填充一开始就退出，使其它指令能在数据返回期间执行；`PRFM` 还可以针对 L2 缓存。<sup>[8](https://support.arm.com/documentation/100236/0002/functional-description/l1-memory-system/about-data-prefetching)</sup> 英特尔编译器接受 `!DIR$ PREFETCH` 指示，其中的提示选择目标缓存层级，距离给出预取在加载之前多少个循环迭代发出；`NOPREFETCH` 则对指定变量禁用预取。<sup>[12](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2025-0/prefetch-and-noprefetch-general-directives.html)</sup> 在英特尔 GPU 上，OpenMP 的 `prefetch` 编译指示将 64 字节缓存行取入 L1 和 L2；该操作是非阻塞的，必须远早于预期访问发出。<sup>[13](https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2024-0/prefetching.html)</sup>

**预取距离**是软件预取中的核心调节参数。它取决于工作集大小、循环体指令的延迟，以及数据所在的位置。预取太远，中间迭代会把预取的数据逐出缓存；预取太近，则无法隐藏内存延迟。两种错误都会浪费预取，甚至可能因向已经饱和的内存队列增加请求而降低性能。在英特尔的 HotQCD 格点 QCD 优化中，提前 10 个迭代向 L2 进行软件预取，相比仅用硬件预取获得了 1.13 倍的加速。<sup>[4](https://www.intel.com/content/www/us/en/developer/articles/technical/qcd-performance-optimization-with-hbm.html)</sup>

## 历史

诺曼·P·朱皮（Norman P. Jouppi）当时在[数字设备公司](https://www.edgepedia.org/zh/digital-equipment-corporation)（Digital Equipment Corporation）的西部研究实验室（Western Research Lab），他在 1990 年一篇关于改进直接映射缓存性能的论文中提出了流缓冲区。在缓存缺失时，流缓冲区将连续的缓存块预取到一个单独的 FIFO 缓冲区，而不是取入 L1 缓存，从而避免污染缓存本身；多路流缓冲区沿着多条交织的引用流进行预取。<sup>[14](https://doi.org/10.1145/325164.325162)</sup><sup> • </sup><sup>[15](https://www.researchgate.net/publication/299388164_A_Survey_of_Recent_Prefetching_Techniques_for_Processor_Caches)</sup> 硬件发起的预取也出现在 20 世纪 90 年代中期的处理器中：HP PA-7200 实现了一种带标记的、硬件发起的预取方案，最多可同时有四个未完成的数据预取请求。<sup>[16](https://www.ardent-tool.com/CPU/docs/AMD/anatomy/misc/articles/r7023.pdf)</sup>

## 局限与代价

预取只有在预测正确且及时时才有帮助。不准确的预取会产生不必要的内存访问，预取的数据还可能挤占程序稍后需要的缓存行，因此提高预取器的激进程度虽然能改善平均性能，却可能在某些应用上大幅降低性能。<sup>[5](https://documentation-service.arm.com/static/64a3f23238511951cb79a6cb?token=)</sup> 英特尔的优化手册从硬件角度给出了同样的图景：预取对顺序访问模式能提升性能，对稀疏模式可能因带宽争用而略有下降；在极少数情况下，当工作集被调整到恰好填满缓存时，不必要的预取会逐出所需的行，造成严重性能下降。<sup>[6](https://www.intel.com/content/dam/doc/manual/64-ia-32-architectures-optimization-manual.pdf)</sup> IBM 的一项研究将预取质量归结为及时性、覆盖率和准确性，结果表明，在理想条件下预取几乎可以消除缓存缺失带来的全部流水线停顿，但在真实处理器中，大部分收益都被侵蚀，预取有时反而会损失性能。<sup>[17](https://research.ibm.com/publications/when-prefetching-improvesdegrades-performance)</sup>

许多 x86 处理器的一个结构性限制是 4 KB 页：硬件预取器不跨页边界流式预取，因此步长大于一页的访问模式会使它们失效。在 HotQCD 的案例中，矩阵以 1,172 个 FP32 元素（4,688 字节）的步长访问，每次外层循环迭代都会跨入新的 4 KB 页，这就是那里需要显式软件预取的原因。<sup>[4](https://www.intel.com/content/www/us/en/developer/articles/technical/qcd-performance-optimization-with-hbm.html)</sup>

## 不规则预取与时间预取

步长预取器和空间预取器能以较低代价处理规则模式，但许多应用，尤其是服务器和横向扩展工作负载，会不规则地访问内存。时间预取器记录一段内存访问序列，并在模式重复时重放它。早期的方案将这些元数据存储在片外的全局历史缓冲区中，容量可达数十兆字节，带来高延迟和沉重的片外流量。ISB 预取器（MICRO 2013）将物理地址重新映射到一个结构化地址空间，使时间相关的地址连续排列，从而让映射本身可以缓存在片上。Arm 研究院与得克萨斯大学奥斯汀分校的 MISB 预取器（ISCA 2019）以更细的粒度、用于削减片外元数据请求的布隆过滤器以及元数据预取，改进了 ISB 的元数据管理；在 SPEC CPU2006 的不规则子集上，它将性能提升了 23%，而 STMS 为 10.6%，同时产生的流量开销不到 STMS 4.8 GB/s 的四分之一。<sup>[3](https://developer.arm.com/community/arm-research/b/articles/posts/making-temporal-prefetchers-practical--the-misb-prefetcher)</sup>

## 安全与近期方向

预取器与安全存在交互。英特尔的数据相关预取器（Data-Dependent Prefetcher，DDP）出现在一些较新的[英特尔酷睿](https://www.edgepedia.org/zh/intel-core)处理器中，它部分通过检查内存中的数据值来决定预取地址，而不仅仅依据先前访问的地址。由于预取会改变缓存及其他微架构结构的状态，而这些地址又源自内存内容，其副作用在原理上可能让攻击者推断出被检查的数据值的信息，但无法推断出被预取的那个解引用地址的内容。<sup>[18](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/data-dependent-prefetcher.html)</sup>

近期有两个研究方向直接针对准确性问题。Themis（2026）增加了一个软硬件接口，软件在页表项中标记应禁用预取的数据页；在剖析引导下使用，可将无用的预取请求减少约 40%，并在数据中心工作负载上加速了所评估的预取器，包括 BOP、SPP+PPF 和 Pythia。<sup>[19](https://arxiv.org/abs/2608.00259)</sup> 而一种由智能体驱动的设计流程，反复诊断已部署预取器失败的原因并合成专用的子预取器，产生了一个预取器混合体（Mixture of Prefetchers），在 SPEC CPU2006 和 CPU2017 上相比无预取取得了 61.1% 的几何平均 IPC 加速，分别超过人工设计的 Alecto、Berti 和 Pythia 预取器 14.5%、21.6% 和 23.6%，在 6 nm 综合下片上存储占用在 110 KB 以内。<sup>[20](https://arxiv.org/abs/2608.13027)</sup>

## References

1. [Bakhshalipour et al. (2020). A Survey on Recent Hardware Data Prefetching Approaches with An Emphasis on Servers. arXiv.](https://arxiv.org/abs/2009.00715)
2. [Santhanam, Gornish, Hsu (1997). Data Prefetching on the HP PA-8000. ISCA 1997.](https://www.researchgate.net/publication/247924889_Data_prefetching_on_the_HP_PA8000)
3. [Nathella, Sunwoo (2019). Making Temporal Prefetchers Practical: The MISB Prefetcher. Arm Community.](https://developer.arm.com/community/arm-research/b/articles/posts/making-temporal-prefetchers-practical--the-misb-prefetcher)
4. [Optimize QCD Performance on Intel Processors with HBM. Intel Developer Zone.](https://www.intel.com/content/www/us/en/developer/articles/technical/qcd-performance-optimization-with-hbm.html)
5. [Cortex-R82 Performance boost using data prefetch control. Arm documentation.](https://documentation-service.arm.com/static/64a3f23238511951cb79a6cb?token=)
6. [Intel 64 and IA-32 Architectures Optimization Reference Manual.](https://www.intel.com/content/dam/doc/manual/64-ia-32-architectures-optimization-manual.pdf)
7. [Hardware Prefetch Controls for Intel E-Cores. Intel Optimization Zone.](https://intel.github.io/optimization-zone/hardware/HWPrefetchTuning/)
8. [ARM Cortex-A35 Processor Technical Reference Manual: About data prefetching.](https://support.arm.com/documentation/100236/0002/functional-description/l1-memory-system/about-data-prefetching)
9. [Arm Cortex-A715 Core Technical Reference Manual: L2 memory system.](https://support.arm.com/documentation/101590/0103/L2-memory-system?lang=en)
10. [Memory hierarchy characterization of SPEC CPU2006 and SPEC CPU2017 on the Intel Xeon Skylake-SP. PLOS ONE / PMC.](https://pmc.ncbi.nlm.nih.gov/articles/PMC6675054/)
11. [Compiler optimizations for the PA-8000. Hewlett-Packard.](https://www.researchgate.net/publication/3687706_Compiler_optimizations_for_the_PA-8000)
12. [PREFETCH and NOPREFETCH General Directives. Intel Fortran Compiler Reference.](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2025-0/prefetch-and-noprefetch-general-directives.html)
13. [Prefetching. oneAPI GPU Optimization Guide, Intel.](https://www.intel.com/content/www/us/en/docs/oneapi/optimization-guide-gpu/2024-0/prefetching.html)
14. [Jouppi, N. P. (1990). Improving direct-mapped cache performance by the addition of a small fully-associative cache and prefetch buffers. ACM SIGARCH CAN 18(2SI).](https://doi.org/10.1145/325164.325162)
15. [Mittal et al. (2016). A Survey of Recent Prefetching Techniques for Processor Caches. ACM Computing Surveys.](https://www.researchgate.net/publication/299388164_A_Survey_of_Recent_Prefetching_Techniques_for_Processor_Caches)
16. [When Caches Aren't Enough: Data Prefetching Techniques.](https://www.ardent-tool.com/CPU/docs/AMD/anatomy/misc/articles/r7023.pdf)
17. [Puzak, Hartstein, Emma, Srinivasan (2005). When prefetching improves/degrades performance. CF 2005, IBM Research.](https://research.ibm.com/publications/when-prefetching-improvesdegrades-performance)
18. [Data Dependent Prefetcher. Intel Software Security Guidance.](https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/data-dependent-prefetcher.html)
19. [Kamahori et al. (2026). Themis: Software-Defined Hardware Prefetching. arXiv.](https://arxiv.org/abs/2608.00259)
20. [Sun et al. (2026). Why Do Prefetchers Fail? Let Agents Answer. arXiv.](https://arxiv.org/abs/2608.13027)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › Processors & processor engineering › Computer architecture theory › Memory hierarchy and caching

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/prefetching-computing

English original: https://www.edgepedia.org/prefetching-computing
