# 自动向量化

**自动向量化**（autovectorization）是一种编译器变换：它分析标量程序代码（通常是循环），在不改动源码的前提下将其重写为使用单指令多数据（SIMD）指令的形式，从而把程序员原本需要手工表达的数据级并行转换为打包的向量运算。<sup>[1](https://onlinelibrary.wiley.com/doi/10.1155/2021/3264624)</sup> 它内置于包括英特尔C++编译器（ICC）、[GNU编译器集合](https://www.edgepedia.org/zh/gnu-compiler-collection)（GCC）和LLVM在内的现代生产级编译器中。<sup>[2](https://www.sciencedirect.com/science/article/pii/S0141933117304970)</sup>

| 关键事实 | 详情 |
|---|---|
| 作用 | 编译器分析循环，找出数据级并行，并自动生成SIMD指令，无需改动源码。<sup>[1](https://onlinelibrary.wiley.com/doi/10.1155/2021/3264624)</sup> |
| 存在位置 | 内置于ICC、GCC和LLVM等编译器中。<sup>[2](https://www.sciencedirect.com/science/article/pii/S0141933117304970)</sup> |
| GCC起源 | 首个版本于2004年1月1日提交至GCC的lno分支；被纳入GCC 4.0发行版，归约向量化则在GCC 4.1中加入。<sup>[3](https://research.ibm.com/publications/autovectorization-in-gcc-two-years-later)</sup> |
| 默认状态 | 在GCC中，`-ftree-loop-vectorize` 和 `-ftree-slp-vectorize` 在 `-O3` 下默认启用；在LLVM中，循环向量化器和SLP向量化器均默认启用。<sup>[4](https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/Optimize-Options.html)</sup><sup> • </sup><sup>[5](https://llvm.org/docs/Vectorizers.html)</sup> |
| 实测覆盖率 | GCC 4.7.0、ICC 12.0和XLC 11.01在一个合成基准测试中向量化了45%–71%的循环，但在真实应用中仅能向量化少数循环。<sup>[6](https://www.microsoft.com/en-us/research/publication/an-evaluation-of-vectorizing-compilers/)</sup> |
| 近期趋势 | GCC在2017年向量化了ETSVC中52.3%的循环，高于2011年的32%。<sup>[7](https://2017.russianscdays.org/files/pdf17/55.pdf)</sup> |
| 诊断 | Clang的 `-Rpass=loop-vectorize`、`-Rpass-missed=loop-vectorize` 和 `-Rpass-analysis=loop-vectorize` 可报告哪些循环被向量化以及其余循环为何未被向量化；英特尔编译器使用 `-qopt-report`。<sup>[5](https://llvm.org/docs/Vectorizers.html)</sup><sup> • </sup><sup>[8](https://www.intel.com/content/www/us/en/developer/articles/technical/vectorization-and-optimization-reports.html)</sup> |

## 工作原理

向量化器必须为每个候选循环确立两件事：合法性与收益性。合法性是指该变换保持程序的语义，这要求各次迭代相互独立，或者依赖关系得到处理（例如通过归约）。收益性由代价模型判断。LLVM的循环向量化器使用目标变换信息分析来估算指令代价，判断向量化是否划算，并用该模型选定向量化因子（VF）和展开因子（UF）。<sup>[5](https://llvm.org/docs/Vectorizers.html)</sup><sup> • </sup><sup>[9](https://www.llvm.org/docs/VectorizationPlan.html)</sup> GCC同样采用向量化代价模型，通过 `-fvect-cost-model` 启用。<sup>[10](https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc.pdf)</sup>

**运行时检查。** 当编译器无法在编译期证明两个指针指向不相交的内存时，它仍可通过生成在运行时检查数组是否重叠的代码来向量化，若重叠则回退到标量循环。<sup>[5](https://llvm.org/docs/Vectorizers.html)</sup> GCC较早的 `-ftree-vect-loop-version` 选项工作方式相同，它会为一个循环生成向量化和非向量化两个版本，并在运行时检查对齐或依赖关系。<sup>[10](https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc.pdf)</sup>

**归约与浮点。** 诸如对数组求和之类的归约看似会产生循环携带依赖，但向量化器能够识别该模式，将结果累加进一个向量，最后再合并各条通道。<sup>[5](https://llvm.org/docs/Vectorizers.html)</sup> 由于重结合会改变求值顺序，从而改变浮点结果——这构成对C和C++标准的隐式违反——LLVM在大多数目标平台上只有在用户至少启用了 `-ffast-math` 中 `-fassociative-math -fno-signed-zeros -fno-trapping-math` 这部分选项时，才会对浮点归约进行向量化。在AArch64和RISC-V上，它也可以改为生成有序归约以保持精确结果，不过这类归约通常效率较低。<sup>[5](https://llvm.org/docs/Vectorizers.html)</sup>

**其他可处理的模式。** LLVM的循环向量化器支持迭代次数未知的循环、条件代码的条件转换、来自C++迭代器的指针归纳变量、反向迭代、散列与聚集内存访问、混合类型，以及在通过 `-fveclib` 提供向量数学库（可选Accelerate、libmvec、SVML、SLEEF、ArmPL和AMDLIBM）时对 `sin`、`exp`、`sqrt` 等内建数学函数的调用。<sup>[5](https://llvm.org/docs/Vectorizers.html)</sup> 当迭代次数不是向量宽度的整数倍时，由一个标量收尾循环处理剩余部分；LLVM还能以更小的因子对收尾部分本身进行向量化，使短循环也能在向量代码中执行。<sup>[5](https://llvm.org/docs/Vectorizers.html)</sup>

## 两大向量化器家族

LLVM提供两个面向不同机会的向量化器。循环向量化器加宽循环中的指令，使其作用于多个连续迭代；SLP向量化器（超字级并行）则把直线代码中发现的相似且独立的标量指令合并为向量指令，覆盖内存访问、算术、比较和PHI节点。<sup>[5](https://llvm.org/docs/Vectorizers.html)</sup> GCC以 `-ftree-loop-vectorize` 和 `-ftree-slp-vectorize` 对应这一划分。<sup>[4](https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/Optimize-Options.html)</sup>

## 历史

GCC的向量化器最初是IBM海法研究实验室由多里特·努兹曼和阿亚尔·扎克斯开展的工作。首个版本于2004年1月1日提交至lno分支，并在2004年6月的第二届GCC开发者峰会上展示，当时仅能处理简单构造：单基本块循环、对同尺寸类型的单位步长且对齐的访问，且无循环携带依赖。它随GCC 4.0进入主线，在GCC 4.1中获得归约向量化，在GCC 4.2中获得归约模式识别，之后又陆续支持基于指针和未对齐的引用、条件运算、类型转换以及2的幂次步长。<sup>[3](https://research.ibm.com/publications/autovectorization-in-gcc-two-years-later)</sup>

## 效果如何

实测覆盖率虽有增长，但仍不完整。[微软研究院](https://www.edgepedia.org/zh/microsoft-research)对GCC 4.7.0、ICC 12.0和XLC 11.01的评估发现，尽管向量化研究已开展四十年，在一个合成基准测试中仅有45%–71%的循环被向量化，而真实应用中仅能向量化少数循环。<sup>[6](https://www.microsoft.com/en-us/research/publication/an-evaluation-of-vectorizing-compilers/)</sup> 在ETSVC基准测试上，GCC的向量化器从2011年向量化32%的循环提升到2017年的52.3%。<sup>[7](https://2017.russianscdays.org/files/pdf17/55.pdf)</sup> 2025年一项在x86和ARM上的比较报告称，GCC向量化了其测试集中54%的循环，ICX为50%，Clang为46%，且有些循环只在两个平台之一上被向量化。<sup>[11](https://arxiv.org/abs/2502.11906)</sup>

结果在很大程度上取决于体系结构。2025年一项针对RISC-V上向量扩展（RVV）1.0的AI工作负载研究比较了GCC 15.1.0和LLVM/Clang 21.1.5，发现自动向量化的效果存在明显差异：启用RVV后LLVM取得了1.283倍的加速，而GCC则出现0.312倍的性能回退。<sup>[12](https://eurekamag.com/research/108/122/108122330.php)</sup> 另一项在RISC-V开发板上针对RVV 0.7和1.0的研究考察了GCC和LLVM在多种向量化模式下的表现，以量化各自所能达到的效果，并指出编译器对RVV的支持有限但仍在演进。<sup>[13](https://ieeexplore.ieee.org/document/10974828/)</sup>

2021年一项使用TSVC（向量化编译器测试套件）基准的评估——该套件已成为LLVM的基准测试集——比较了GCC、LLVM和ICC五年间的多个历史版本，得出三点发现：最新版本并非在每个TSVC程序上都优于历史版本；局限不仅在于向量化方法本身，还在于其周围的分析和辅助变换，例如依赖信息和不连续内存访问分析；工业实现与学术研究的侧重点不同。同一项研究还发现，ICC19拒绝向量化TSVC程序S252，因为它认为该程序存在阻止向量化的依赖关系。<sup>[1](https://onlinelibrary.wiley.com/doi/10.1155/2021/3264624)</sup><sup> • </sup><sup>[14](https://doi.org/10.1155/2021/3264624)</sup>

## 控制与诊断向量化

编译器通过标志、编译指示和报告来暴露这一变换。在GCC中，`-ftree-vectorize` 同时启用循环向量化和SLP向量化，两个组件在 `-O3` 下均默认开启；配置文件引导的构建（`-fprofile-use`）也会启用它们。<sup>[4](https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/Optimize-Options.html)</sup> Clang接受按循环使用的 `#pragma clang loop vectorize(enable)` 和 `vectorize_width(n)` 指令，这在以 `-Os` 编译（此时向量化默认关闭）时很有用。<sup>[5](https://llvm.org/docs/Vectorizers.html)</sup><sup> • </sup><sup>[15](https://clang.llvm.org/docs/LanguageExtensions.html)</sup> 英特尔编译器通过 `-qopt-report` 报告每个循环的向量化决策，显示哪些循环被向量化，并且至少同样重要的是显示其余循环为何未被向量化。<sup>[8](https://www.intel.com/content/www/us/en/developer/articles/technical/vectorization-and-optimization-reports.html)</sup>

**循环为何无法向量化。** 常见的阻碍包括复杂的控制流、无法向量化的类型和无法向量化的调用；Clang的 `-Rpass-analysis=loop-vectorize` 会指出导致失败的语句，例如循环内的switch语句。<sup>[5](https://llvm.org/docs/Vectorizers.html)</sup> 关于自动向量化潜力的研究发现，许多高性能计算应用所使用的编程策略和数据结构使编译器无法从自动向量化中获益，而诸如循环展开、拆分大循环、从循环中移除控制流等源码级改动则有所帮助。<sup>[16](https://www.sciencedirect.com/science/article/pii/S0141933116303702)</sup>

## 当前发展

GCC 16（2026年4月30日发布）将向量化器扩展到迭代次数无法确定的非计数循环，在识别归约的循环内并行性方面更加灵活，为向量长度无关的循环增加了使用掩码的对齐剥离，并为带提前跳出的循环生成更高效的代码；在x86上，当启用AVX-512并针对 `znver4`、`znver5` 或 `znver6` 调优时，它会尝试带掩码的向量化收尾。<sup>[17](https://gcc.gnu.org/gcc-16/changes.html)</sup><sup> • </sup><sup>[18](https://gcc.gnu.org/pipermail/gcc-announce/2026/000190.html)</sup> 英特尔记录了GCC 12中针对AVX-512和AVX-512-VNNI的持续SIMD向量化增强，以及LLVM和GCC针对英特尔CPU和GPU指令集的持续演进。<sup>[19](https://www.intel.com/content/www/us/en/developer/articles/technical/vectorization-llvm-gcc-cpus-gpus.html)</sup> 研究系统继续推进到生产级向量化器所能处理的范围之外：Autovesk使用图变换从非结构化静态内核生成向量化代码，并将其加速比与GCC、Clang/LLVM和英特尔自动向量化进行比较。<sup>[20](https://doi.org/10.1145%2F3631709)</sup>

## References

1. [Feng et al. (2021). Evaluation of Compilers' Capability of Automatic Vectorization Based on Source Code Analysis. Scientific Programming.](https://onlinelibrary.wiley.com/doi/10.1155/2021/3264624)
2. [Performance evaluation of implicit and explicit SIMDization. Journal of Systems Architecture (ScienceDirect).](https://www.sciencedirect.com/science/article/pii/S0141933117304970)
3. [Nuzman, D. and Zaks, A. (2006). Autovectorization in GCC—two years later. GCC and GNU Toolchain Developers' Summit, IBM Research.](https://research.ibm.com/publications/autovectorization-in-gcc-two-years-later)
4. [Optimize Options, Using the GNU Compiler Collection (GCC) 9.1.0.](https://gcc.gnu.org/onlinedocs/gcc-9.1.0/gcc/Optimize-Options.html)
5. [Auto-Vectorization in LLVM, LLVM documentation.](https://llvm.org/docs/Vectorizers.html)
6. [An Evaluation of Vectorizing Compilers, Microsoft Research.](https://www.microsoft.com/en-us/research/publication/an-evaluation-of-vectorizing-compilers/)
7. [Evaluating auto-vectorizing compilers with the ETSVC benchmark, Russian Supercomputing Days 2017.](https://2017.russianscdays.org/files/pdf17/55.pdf)
8. [Vectorization and Optimization Reports, Intel Developer Zone.](https://www.intel.com/content/www/us/en/developer/articles/technical/vectorization-and-optimization-reports.html)
9. [Vectorization Plan, LLVM documentation.](https://www.llvm.org/docs/VectorizationPlan.html)
10. [Using the GNU Compiler Collection (GCC) 4.7.0 (PDF).](https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc.pdf)
11. [Comparison of Vectorization Capabilities of Different Compilers for X86 and ARM CPUs. arXiv:2502.11906.](https://arxiv.org/abs/2502.11906)
12. [Compiler Performance Analysis for AI Workloads on RISC-V: A Comprehensive Comparison of GCC and LLVM.](https://eurekamag.com/research/108/122/108122330.php)
13. [A Performance Analysis of Autovectorization on RVV RISC-V Boards, IEEE Xplore.](https://ieeexplore.ieee.org/document/10974828/)
14. [Evaluation of Compilers' Capability of Automatic Vectorization Based on Source Code Analysis (DOI record).](https://doi.org/10.1155/2021/3264624)
15. [Clang Language Extensions, Clang documentation.](https://clang.llvm.org/docs/LanguageExtensions.html)
16. [An approach for analyzing auto-vectorization potential of emerging workloads. Journal of Systems Architecture (ScienceDirect).](https://www.sciencedirect.com/science/article/pii/S0141933116303702)
17. [GCC 16 Release Series — Changes, New Features, and Fixes, GNU Project.](https://gcc.gnu.org/gcc-16/changes.html)
18. [GCC 16.1 Released, gcc-announce mailing list.](https://gcc.gnu.org/pipermail/gcc-announce/2026/000190.html)
19. [SIMD vectorization in LLVM and GCC for Intel CPUs and GPUs, Intel Developer Zone.](https://www.intel.com/content/www/us/en/developer/articles/technical/vectorization-llvm-gcc-cpus-gpus.html)
20. [Autovesk: Automatic Vectorized Code Generation from Unstructured Static Kernels Using Graph Transformations. ACM Transactions on Architecture and Code Optimization.](https://doi.org/10.1145%2F3631709)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains › Loop and vectorizing optimizations

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/automatic-vectorization

English original: https://www.edgepedia.org/automatic-vectorization
