TL;DR
This paper introduces "compiling by proving," a novel paradigm that transforms formal verification proofs into optimized executable code. By leveraging the detailed execution paths captured in these proofs, the authors demonstrate a way to automatically generate highly efficient code, bridging the gap between program verification and optimization.
What Problem Does This Paper Address?
Traditionally, program optimization (done by compilers) and program verification (done by verifiers) have operated in separate worlds. Compilers use heuristics and analyses to speed up code, while verifiers construct complex proofs to guarantee correctness. The artifacts produced by these communities are largely incompatible, meaning the detailed understanding of program behavior gained during verification is discarded after correctness is established. This separation leads to duplicated effort and missed opportunities for optimization. The authors aim to unify these processes by showing how the rich information within verification proofs can be directly used to generate optimized code, ensuring correctness by construction.
Methods
The core of the paper is the "compiling by proving" paradigm, implemented as an extension to the K framework. The process involves:
- Program Specifications: Defining the boundaries of a compilation unit (e.g., an opcode, a function) by specifying initial and final states. This determines the "scope" of optimization.
- All-Path Reachability Proofs (APRPs): An intermediate representation that captures all possible execution paths of a program segment as a directed graph. This graph is constructed using symbolic execution and deductive proving techniques.
- Proof Construction: An algorithm (Algorithm 1) that builds the APRP by exploring execution paths, handling branches, and identifying subsumption relationships between states. It uses K's symbolic execution engine and SMT solvers.
- Graph Transformations: The APRP graph is then transformed to maximize the length of deterministic "step" edges. This involves "step compression" (merging consecutive deterministic steps) and "branch lifting" (restructuring control flow to enable more compression).
- Compilation: The transformed graph is then compiled into a single, optimized rewrite rule for the specified compilation unit. This rule directly encodes the consolidated execution path, eliminating the overhead of multiple individual rule applications.
The system is designed to be language-agnostic, relying on the formal semantics defined within the K framework.
Datasets
Not discussed in the paper. The evaluation uses existing formal semantics for EVM and Rust's MIR, and standard conformance tests for EVM.
Key Insights and Ideas
- Verification Proofs as Optimization Artifacts: The most significant insight is that formal verification proofs are not just certificates of correctness but rich sources of information for optimization. They capture all execution paths and state transformations, which is precisely what compilers need.
- Unifying Verification and Optimization: The paper presents a concrete mechanism to bridge the long-standing gap between these two fields, showing how they can be performed in tandem.
- Correctness by Construction: By compiling directly from proofs, the generated optimized code inherits the correctness guarantees of the original proof, eliminating the need for separate verification of the optimized code.
- Scalability of Optimization: The paper demonstrates that the benefits of this approach scale significantly with the scope of compilation, from individual opcodes to entire programs.
Key Results
The approach was evaluated on two case studies:
- EVM Opcodes: For 131 out of 149 EVM opcodes, compilation by proving achieved an average 89.6% reduction in rewriting steps. This resulted in a geometric-mean speedup of 2.19x (median 2.00x) for concrete execution and 1.44x for symbolic execution, with no performance regressions.
- Rust Program (via MIR): For a Rust program involving 1000 loop iterations and function calls, compiling the entire program into a single rule resulted in a massive speedup of 522x compared to symbolic execution without compilation. This highlights how benefits scale dramatically with program complexity.
Limitations and Future Work
- Specification Coverage: While high, specification coverage for EVM opcodes was not 100%. Flow Control and Context opcodes presented challenges due to path-sensitive control transfers and environment-dependent side effects. Contract-level operations were left as future work due to complex state changes and nested contexts.
- Manual Specification: The process still requires users to provide program specifications that define compilation boundaries and termination conditions.
- Future Work: The authors suggest exploring contract-level operations for EVM and applying the framework to other languages and domains. They also highlight the potential for cross-model validation by compiling to formal proof assistants like Lean 4.
Summary updated