用大模型辅助重构代码,让编译器更容易实现向量化加速。
VecTrans: Enhancing Compiler Auto-Vectorization through LLM-Assisted Code Transformations
- 先分析代码找出可向量化区域,再用大模型改写为更适合向量化的模式。
- 在51个难处理函数中成功向量化24个,平均提速1.77倍。
- 兼顾准确性和效率,适合编译器开发者和性能优化工程师。
自动向量化是现代编译器利用SIMD并行性的关键优化手段。然而,现有方法在处理复杂代码模式时仍存在困难,常需人工提示或领域专业知识。大语言模型(LLMs)具备捕捉复杂模式的能力,有望解决该问题,但其在编译优化中的应用受限于幻觉和缺乏领域推理能力。本文提出VecTrans,一个利用LLMs增强编译器代码向量化的框架。该框架首先通过编译器分析识别潜在可向量化代码区域,再利用LLM将这些区域重构为更利于编译器自动向量化的新模式。为确保语义正确性,VecTrans在中间表示(IR)层集成混合验证机制。实验结果表明,在所有由GCC、ICC、Clang和BiSheng编译器无法向量化的TSVC函数中,VecTrans实现了1.77倍的几何平均加速,并成功向量化24/51个测试用例。该方案显著优于现有方法,且每函数优化成本仅0.012美元(基于LLM API),具有良好的性价比。
原文摘要 · Abstract (English)
Auto-vectorization is a fundamental optimization for modern compilers to exploit SIMD parallelism. However, state-of-the-art approaches still struggle to handle intricate code patterns, often requiring manual hints or domain-specific expertise. Large language models (LLMs), with their ability to capture intricate patterns, provide a promising solution, yet their effective application in compiler optimizations remains an open challenge due to issues such as hallucinations and a lack of domain-specific reasoning. In this paper, we present VecTrans, a novel framework that leverages LLMs to enhance compiler-based code vectorization. VecTrans first employs compiler analysis to identify potentially vectorizable code regions. It then utilizes an LLM to refactor these regions into patterns that are more amenable to the compilers auto-vectorization. To ensure semantic correctness, VecTrans further integrates a hybrid validation mechanism at the intermediate representation (IR) level. With the above efforts, VecTrans combines the adaptability of LLMs with the precision of compiler vectorization, thereby effectively opening up the vectorization opportunities. experimental results show that among all TSVC functions unvectorizable by GCC, ICC, Clang, and BiSheng Compiler, VecTrans achieves an geomean speedup of 1.77x and successfully vectorizes 24 of 51 test cases. This marks a significant advancement over state-of-the-art approaches while maintaining a cost efficiency of $0.012 per function optimization for LLM API usage.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。