Manuscript Offprint

arXiv:2610.30188

Agentic Verified Optimization of Real-World Software

Zhe Ye, Udaya Ghai, Zhizhen Qin, Dawn Song, Soonho Kong

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original. This text is shared under the same licence.

ABSTRACT

Program optimization aims to improve performance while preserving behavior. Coding agents can already make programs faster by changing algorithms, data structures, and execution strategies. However, existing equivalence checks mostly rely on tests, which agents have been shown to exploit, passing the tests without preserving the intended behavior. We propose agentic verified optimization, in which an agent produces a faster implementation together with a machine-checked proof of equivalence to the existing one. Because the existing implementation serves as the specification, the task avoids the specification-writing bottleneck of verified code generation, and recent advances in theorem proving make the proofs attainable. We formulate the task at the function and repository levels in Lean 4, where programs can be both executed and reasoned about. Few real-world applications are written in Lean, so we develop AQUEDUCT, an agentic framework that migrates C, C++, Go, and Rust programs into native Lean, validates each port by differential testing against its original. On the 189 reference implementations in Verina, agents produce faster proved rewrites for 156, including algorithmic changes such as replacing exponential recursion with dynamic programming. On 17 migrated applications, their 92 proved rewrites speed up 47 of 51 application scenarios. These application-level gains are smaller than the function-level ones, making the choice of functions to optimize an important problem. Current agents can thus already carry out verified optimization on both standalone functions and applications migrated from real programs. We discuss the remaining challenges and the steps toward verified optimization in production.

1 INTRODUCTION

Coding agents are increasingly used to write and modify software [6, 30, 82]. They are now also asked to make code faster [26, 18, 74]. This goal continues a long line of work on automatic optimization. Before language models, superoptimizers searched instruction sequences [47, 62], learned models guided search over programs [5], and reinforcement learning discovered faster algorithms [46]. Language models and coding agents have since taken on the task, as PIE [65] rewrites programs for speed, KernelBench [53] studies efficient GPU kernel generation, and Jitskit [40] synthesizes systems specialized to a workload. These optimizations range from local changes that eliminate redundant computation to new data representations and algorithms. Whatever the strategy, a faster replacement must preserve the behavior of the program it replaces.

Agents can already find substantial performance gains, but existing work checks behavior preservation with tests, which, as Dijkstra [16] observed, can show the presence of errors but not their absence. This limitation matters more when the tests also define an agent's reward. Agents then learn to pass the tests rather than preserve the behavior, a pattern studied as reward hacking [67] and observed in coding agents that special-case the tested inputs [92]. In optimization, reward hacking yields speedups from programs that do not preserve behavior. Jitskit [40] describes an apparent 6×6 \times improvement that came from regenerating test values rather than storing them, and an audit found correctness failures in 39.5% of the GPU kernels that an optimization harness had accepted [63]. A proof of equivalence avoids this problem because it covers every input, the criterion used in translation validation [55, 50]

*Work done during internship at Amazon Web Services.

and regression verification [24, 10]. With a proof as the check, an agent can change algorithms and data representations freely without weakening correctness.

Agents are now capable provers. Neural theorem proving has progressed from language-model proof search [56, 34] to provers trained with reinforcement learning that reach olympiad level [27, 57, 38], and agents now formalize research mathematics, from open Erdős problems [68, 72] to a complete Lean proof of Fermat's Last Theorem [7, 2]. They also build verified software in Lean, from repository-level implementations with proofs [84] to a verified compiler [58] and a verified DEFLATE library [49]. Program optimization is a natural application of these capabilities.

We study agentic verified optimization, in which an agent produces a faster implementation f′f' of a reference function ff together with a machine-checked equivalence proof. The proof establishes ∀x,f(x)=f′(x)\forall x, f(x) = f'(x), with the reference implementation ff as the specification for correctness. This avoids the specification-writing problem in verified code generation, where writing specifications that capture the intended behavior remains difficult, even manually [19, 86, 45]. This task can also be viewed as the refinement step that formal methods have long advocated, in which a simple implementation is proved correct and then refined into a fast one that inherits its guarantees. By hand, it is costly and has been reserved for critical domains such as cryptography [21]. Agents are a natural fit for this work. In verified code generation, proofs can push agents toward simple implementations [84]. Agentic verified optimization keeps the simple, proved version and then allow agent to search complicated, optimized version with the same correctness guarantee. The task in turn helps improve agents. Every function in an existing codebase is an instance with its specification in place, and because correctness is fixed while speedup is a continuous measure rather than a pass rate, the task is open-ended and does not saturate as agents improve.

The task couples implementation and proof search. For example, replacing recursive enumeration with dynamic programming can remove repeated work, but its proof needs an invariant connecting table entries to the original recursion. An implementation may be equivalent and fast yet difficult for the agent to prove within a limited search budget. Conversely, a readily proved rewrite may offer little improvement. The agent must decide whether to improve a candidate further, invest in its proof, or pursue another implementation. Verified optimization therefore tests how agents coordinate program reasoning, performance analysis, and proof construction.

We formulate the task in Lean 4 [14], where the same definitions can be executed and used in proofs, and whose compiler produces native code with a reference-counting runtime [73]. Lean's kernel checks each equality proof, and execution on the evaluation workloads measures the speedup. We study current agent capabilities at two levels. Function-level optimization asks the agent to improve a given implementation. We use the 189 reference implementations in Verina [85] to study this setting. Repository-level optimization asks the agent to improve an application by choosing and rewriting functions within its codebase. Here, function-level microbenchmarks measure individual rewrites, while application-level macrobenchmarks measure whether they speed up the application. This setting requires executable applications in Lean, but few real-world applications are written in Lean. We therefore build AQUEDUCT, an agentic framework that migrates C, C++, Go, and Rust programs into native Lean and checks each port against its original through differential testing [48]. The resulting ports keep the interfaces, behavior, and workloads of real software and serve as targets for verified optimization.

On Verina, agents produce 156 verified improvements among the 189 targets. Using the fastest verified implementation for each target, the geometric mean speedup is 1.851×1.851 \times and the median is 1.117×1.117 \times. On 17 migrated repositories, agents produce 92 rewrites proved equivalent to the Lean ports they replace, all faster on function-level microbenchmarks. The geometric mean speedup is 1.864×1.864 \times, with a median of 1.402×1.402 \times. Fifteen of these optimized functions outperform functions in the original source-language programs. On 51 application scenarios, execution improves in 47 cases with a geometric mean speedup of 1.120×1.120 \times over the migrated Lean ports. The repository rewrites remove repeated normalization and temporary lists, add fast paths for common input cases, and change traversal order while preserving the reference behavior. Together, these findings outline concrete steps toward verified optimization of real-world software, which we discuss in Section 6.

2 BACKGROUND AND RELATED WORK

Existing work on program optimization and verification shares several limitations that prevent agents from optimizing real-world software with guaranteed correctness.

First, checking optimization correctness with tests does not guarantee behavior preservation. PIE [65] generates program edits for speed, and LLM Compiler [11] selects compiler optimizations for code size. At the algorithm level, AlphaDev [46] and AlphaEvolve [51] search for improved algorithms. For GPU kernels, generation and optimization are studied in KernelBench [53], Sakana's CUDA Engineer [35], CUDA-L1 [37], Astra [76], and AsmEvo [39]. At system scale, Jitskit [40] synthesizes systems from specifications. These systems check correctness with tests or differential execution. When this assessment determines the reward for optimization, an agent can improve its score by exploiting the checks or omitting work needed outside the tested cases. Documented failures include exploiting harness bugs or weaknesses in timing methodology [35, 37], specializing to benchmark input distributions [40, 89], and passing standard tests while violating kernel contracts [60, 63]. In agentic verified optimization, an equivalence proof rules out these correctness failures because it covers every input.

Second, optimizers built on predefined transformations or representations cannot go beyond them. Verified compilers such as CompCert [36] and CakeML [32] apply passes proved correct in advance, and egg [77] explores the consequences of supplied rewrite rules. Superoptimizers [47] such as Denali [31], STOKE [62], and Souper [61] search short instruction sequences within a fixed instruction representation. At the source level, Fiat [15], the Isabelle Refinement Framework [33], and Cogent [52] derive implementations from refinement steps supplied by a developer or compiler, and LLMLift [4] verifies translations of code into domain-specific languages such as Spark and TACO, so any performance benefit comes from the optimizations those languages already provide. Even when every transformation is verified, such an optimizer finds only what its passes, rules, representation, or target language can express, which places a ceiling on its gains. An agent has no such fixed set, since it can write a new algorithm or data representation directly in Lean and prove it equivalent to the reference without moving the program to another language.

Third, agents that optimize under formal checks have limited ability to prove their rewrites. Translation validation [55, 50] checks each compiler output with an automated equivalence checker, as Alive2 [43] does for LLVM with bounded loop unrolling and HEC [87] does with equality saturation. LLM-Vectorizer [70], VecTrans [90], and LLM-VeriOpt [22] check their LLVM rewrites with Alive2, which accepts only 38.2% of LLM-Vectorizer's test programs, and T-LLM [23], which pairs the bounded model checker CBMC with a language-model judge, reports a 33.8% false-acceptance rate. AutoGate [75] and Ares [13] check hardware rewrites with commercial equivalence tools, CertiBlock [66] checks six predefined rewrite families for Scratch programs, and CktFormalizer [79] proves equivalence for circuits in a domain-specific language embedded in Lean. Other systems let the agent prove its rewrite against a written specification, such as IDS [1] for distributed systems, Solidus [54] for a verified Yul-to-EVM compiler, and lean-zip [49] for a verified DEFLATE implementation. These specifications are hard to write and must be written anew for each system [19, 86]. Our task uses the existing implementation as the specification, so no specification has to be written before a function can be optimized.

Fourth, verified code-generation benchmarks measure correctness but not performance. Function-level benchmarks in Lean, including miniCodeProps [42], FVAPPS [17], Verina [85], and CLEVER [71], benchmarks for SMT-based verifiers such as DafnyBench [44] and VerusBench [8], VeriCoding [9] across Dafny, Verus, and Lean, and repository-scale benchmarks such as RVBench [91], VeruSAGE-Bench [81], VeriSoftBench [78], and Vero [84] all check whether an implementation satisfies a specification, and none evaluates its speed.

Finally, Lean has few executable real-world applications to optimize. Lean is both a proof assistant and a general-purpose language with a native compiler [14, 73], but its software developments are formal models of languages, such as Aeneas [25] and Strata [69], and individual components, such as Cedar [12], lean-zip [49], and RyuLean4 [59], none of which has the callers and workloads of a deployed application. Language models can translate programs between languages [29], with translations checked by bounded model checking in VERT [80] or by differential fuzzing in FLOURINE [20]. AQUEDUCT migrates applications into native Lean and validates each port with differential testing [48], coverage-guided fuzzing [88, 41], and mutation analysis [28].

Figure

Figure 1: Agentic verified optimization in a repository. An agent selects a function ff from a Lean repository and jointly searches for a replacement f′f' and an equivalence proof π\pi. Keeping ff fixed, it uses Lean's errors and proof goals to revise both artifacts. The search returns the replacement with a checked proof that it preserves the reference's behavior. Microbenchmarks measure function-level speedup, and macrobenchmarks measure the effect on the application.

References (78)