Peephole optimization is an essential class of compiler optimizations that targets small, inefficient instruction sequences within programs. By replacing such suboptimal instructions with refined and more optimal sequences, these optimizations not only directly optimize code size and performance, but also enable more transformations in the subsequent optimization pipeline. Despite their importance, discovering new and effective peephole optimizations remains challenging due to the complexity and breadth of instruction sets. Prior approaches either lack scalability or have significant restrictions on the peephole optimizations that they can find. This paper introduces LPO, a novel automated framework to discover missed peephole optimizations. Our key insight is that, Large Language Models (LLMs) are effective at creative exploration but susceptible to hallucinations; conversely, formal verification techniques provide rigorous guarantees but struggle with creative discovery. By synergistically combining the strengths of LLMs and formal verifiers in a closed-loop feedback mechanism, LPO can effectively discover verified peephole optimizations that were previously missed. We comprehensively evaluated LPO within LLVM ecosystems. Our evaluation shows that LPO can successfully identify up to 22 out of 25 previously reported missed optimizations in LLVM. In contrast, the recently proposed superoptimizers for LLVM, Souper and Minotaur detected 15 and 3 of them, respectively. More importantly, within eleven months of development and intermittent testing, LPO found 62 missed peephole optimizations, of which 28 were confirmed and an additional 13 had already been fixed in LLVM. These results demonstrate LPO's strong potential to continuously uncover new optimizations as LLMs' reasoning improves.
翻译:窥孔优化是一类重要的编译器优化技术,其针对程序中的低效短指令序列进行优化。通过将此类次优指令替换为精炼且更优的序列,这些优化不仅能直接改善代码大小与性能,还能为后续优化流程开启更多转换可能。尽管其重要性显著,但由于指令集的复杂性与广泛性,发现新颖有效的窥孔优化仍具挑战。现有方法或缺乏可扩展性,或对可发现的窥孔优化存在严重限制。本文提出LPO,一种用于发现遗漏窥孔优化的新型自动化框架。我们的核心洞见在于:大型语言模型(LLMs)擅长创造性探索但易产生幻觉;反之,形式化验证技术能提供严格保证却难以进行创造性发现。通过将LLMs与形式化验证器在闭环反馈机制中协同结合,LPO能够有效发现先前被遗漏的已验证窥孔优化。我们在LLVM生态系统中对LPO进行了全面评估。实验表明,LPO成功识别出LLVM中25个已报告遗漏优化中的22个;相比之下,近期提出的LLVM超级优化器Souper与Minotaur分别仅检测到15个与3个。更重要的是,在十一个月的开发与间歇测试中,LPO共发现62个遗漏窥孔优化,其中28个已获确认,另有13个已在LLVM中被修复。这些结果证明,随着LLMs推理能力的提升,LPO具有持续发现新优化方案的强大潜力。