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

Yuwei Zhao, Yuan-an Xiao, Qizhen Xiao, Zhao Zhang, Yingfei Xiong · DOI

Abstract

Automated code optimization aims to improve performance in programs by refactoring code, and recent studies focus on utilizing LLMs for the optimization. Typical existing approaches mine optimization commits from open-source codebases to construct a large-scale knowledge base, then employ information retrieval techniques such as BM25 to retrieve relevant optimization examples for hotspot code locations, thereby guiding LLMs to optimize these hotspots. However, since semantically equivalent optimizations can manifest in syntactically dissimilar code snippets, current retrieval methods often fail to identify pertinent examples, leading to suboptimal optimization performance. To address these limitations, we propose SemOpt, a novel framework that leverages static program analysis to precisely identify optimizable code segments, retrieve the corresponding optimization strategies, and generate the optimized results. SemOpt consists of three key components: (1) A strategy library builder that extracts and clusters optimization strategies from real-world code modifications; (2) A rule generator that generates Semgrep static analysis rules to capture the condition of applying the optimization strategy; and (3) An optimizer that utilizes the strategy library to generate optimized code results. On our benchmark containing 151 optimization tasks, SemOpt demonstrates its effectiveness under different LLMs by increasing the number of successful optimizations by 1.38 to 28 times compared to the baseline. Moreover, on popular large-scale C/C++ projects, it can improve individual performance metrics by 5.04% to 218.07%, demonstrating its practical utility.

Introduction

Automated code optimization—refactoring code to improve speed or resource efficiency—is a critical challenge in software engineering. While Large Language Models (LLMs) have shown promise in code generation, they face two primary hurdles in optimization: the "Where" problem (identifying performance hotspots) and the "How" problem (applying the correct strategy). Existing Retrieval-Augmented Generation (RAG) methods often rely on surface-level syntactic similarity, failing to recognize that code with different structures may require the same underlying optimization logic. SemOpt addresses these issues by combining the reasoning capabilities of LLMs with the precision of static analysis rules.

The SemOpt Framework

SemOpt introduces a three-stage pipeline designed to transform historical optimization data into actionable, symbolic rules that guide LLM-driven refactoring.

The SemOpt framework consists of a Strategy Library Builder, a Semgrep Rules Generator, and an Optimizer.
  1. Strategy Library Builder: The system mines thousands of real-world optimization commits from GitHub. An LLM summarizes the core strategy behind each commit (e.g., replacing linear searches with hash maps). These summaries are clustered using the DBSCAN algorithm to identify high-impact, recurring patterns.
  2. Rule Generator: For each strategy cluster, a specialized LLM agent generates Semgrep rules. Semgrep is a static analysis tool that uses pattern matching to find specific code structures. The agent employs an iterative "validate-and-refine" loop to ensure the rules accurately match the intended code patterns.
  3. Optimizer: When processing a new project, SemOpt applies its library of Semgrep rules to locate optimizable segments. Upon a match, it provides the LLM with the specific code snippet, line numbers, and the high-level strategy to apply, effectively removing the ambiguity inherent in standard RAG approaches.

Experimental Evaluation

The framework was evaluated using a comprehensive dataset and benchmark:

  • Mining Data: Over 108 million commits from 30,342 GitHub repositories were distilled into 35,668 verified optimization commits.
  • SemOpt Benchmark: A new benchmark consisting of 151 C/C++ optimization tasks sampled from real-world projects.
  • In-the-Wild Projects: Practical utility was tested on major open-source projects including RocksDB, Redis, gRPC, LevelDB, and spdlog.

Results and Discussion

SemOpt significantly outperforms traditional retrieval-based baselines across multiple LLM backends. By providing precise localization and strategy guidance, it avoids the hallucinations common in standard RAG methods.

Performance comparison of SemOpt against baselines (RAPGen, RAG, Direct) across different LLMs.

Key findings include:

  • Accuracy Gains: On the SemOpt benchmark, the framework increased successful optimizations by 1.38x to 28x compared to baselines like RAPGen and standard RAG.
  • Real-World Impact: In practical tests, SemOpt achieved a 218.07% performance improvement in a specific LevelDB test case, with average improvements across five major projects ranging from 1.68% to 10.40%.
  • Generalization: Because SemOpt relies on structural rules rather than syntactic similarity, it generalizes strategies across diverse codebases more effectively than text-based retrieval.

Conclusion

SemOpt demonstrates that the integration of symbolic rules with LLMs provides a robust solution for automated code optimization. By shifting the focus from syntactic similarity to structural intent, the framework provides the precision necessary for real-world software performance engineering. Future work will explore extending SemOpt to handle global, multi-function optimizations and incorporating more advanced program analysis techniques.

Summary updated

BibTeX
@misc{zhao2025semopt,
  author = {Yuwei Zhao and Yuan-an Xiao and Qizhen Xiao and Zhao Zhang and Yingfei Xiong},
  title = {{SemOpt: LLM-Driven Code Optimization via Rule-Based Analysis}},
  year = {2025},
  eprint = {2510.16384},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.16384},
}
Figure