arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2510.16384cs.SE
收藏

SemOpt: 基于规则分析的LLM驱动代码优化

SemOpt: LLM-Driven Code Optimization via Rule-Based Analysis

Key Laboratory of High Confidence Software Technologies (Peking University), Ministry of Education(北京大学计算机学院;教育部高可信软件技术重点实验室);School of Computer Science, Peking University

Yuwei Zhao, Yuan-An Xiao, Qianyu Xiao, Zhao Zhang, Yingfei Xiong

AI总结:

提出SemOpt框架,通过静态程序分析识别代码段、检索优化策略并生成优化结果,在C/C++和Python任务上显著提升优化成功率。

摘要:

自动化代码优化通过重构提升程序性能,近期研究利用LLM实现此目的。现有方法从开源代码库挖掘优化提交以构建大规模知识库,然后采用BM25等检索技术为热点代码获取相关示例,指导LLM进行优化。然而,语义等价的优化常出现在语法不相似的代码中,因此当前检索方法无法识别相关示例,导致结果欠佳。为解决这些局限,我们提出SemOpt,一个利用静态程序分析识别代码段、检索优化策略并生成优化结果的框架。SemOpt包含三个由LLM驱动的组件:(1) 策略库构建器,从代码修改中提取并聚类策略;(2) 规则生成器,生成Semgrep静态分析规则以捕获每个策略的适用性;(3) 优化器,利用策略库生成优化代码。在包含151个C/C++和150个Python优化任务的基准测试上,SemOpt在不同LLM上均显示出一致的改进,与基线相比,C/C++的成功优化次数提升1.38至28倍,Python提升4.60至6.33倍。在大型项目上,SemOpt将C/C++的性能指标提升5.04%至218.07%,Python提升61.77%至479.90%,展示了跨语言泛化能力和实际有效性。

英文摘要:

Automated code optimization improves program performance through refactoring, and recent studies leverage LLMs for this purpose. Existing approaches mine optimization commits from open-source codebases to build large-scale knowledge bases, then employ retrieval techniques such as BM25 to obtain relevant examples for hotspot code, guiding LLMs in optimization. However, semantically equivalent optimizations often appear in syntactically dissimilar code, so current retrieval methods fail to identify pertinent examples, leading to suboptimal results. To address these limitations, we propose SemOpt, a framework that leverages static program analysis to identify code segments, retrieve optimization strategies, and generate optimized results. SemOpt has three LLM-powered components: (1) a strategy library builder that extracts and clusters strategies from code modifications, (2) a rule generator that produces Semgrep static analysis rules to capture each strategy's applicability, and (3) an optimizer that generates optimized code using the strategy library. On a benchmark of 151 C/C++ and 150 Python optimization tasks, SemOpt shows consistent improvements across different LLMs, increasing successful optimizations by 1.38 to 28 times on C/C++ and 4.60 to 6.33 times on Python versus the baseline. On large-scale projects, SemOpt improves performance metrics by 5.04% to 218.07% on C/C++ and 61.77% to 479.90% on Python, showing cross-language generalization and practical effectiveness.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑