LLVM Translation Validation Automated with Large Language Models and Lean
Summary (Overview)
- Trivet is a novel framework that combines Large Language Models (LLMs) with the Lean interactive theorem prover to automate translation validation of LLVM compiler transformations.
- The framework generates structured proof scaffolds from source and target functions, delegating transformation-specific proof obligations to LLMs while relying on the Lean kernel for rigorous certification of all verdicts.
- On a dataset of 148 real-world LLVM transformations, Trivet successfully validates all 74 valid transformations and refutes 73 of 74 invalid ones, leaving only one invalid case unresolved.
- Trivet overcomes three key limitations of Alive2 (the state-of-the-art SMT-based validator): solver scalability issues, lack of symbolic bitwidth support, and bounded loop unrolling.
- Scaffolding reduces mean proof time by 75.9% and mean monetary cost by 88% compared to an unscaffolded baseline, while enabling 26 additional proofs.
Introduction and Theoretical Foundation
Background: LLVM Transformations and Translation Validation
LLVM's Intermediate Representation (IR) is a strongly typed language with highly expressive semantics, including Undefined Behavior (UB). Optimizations must preserve observable program behavior, but the complexity of IR semantics makes correct implementation notoriously difficult. Translation validation addresses this by formally comparing source (pre-transformation) and target (post-transformation) functions.
The fundamental judgment for a transformation is formalized as:
where LHS denotes the pre-transformation fragment, RHS its replacement, and PRE is a logical predicate over operands defining when the transformation is valid.
Refinement Relation
Correctness is formulated as refinement (not equivalence) because optimizers exploit UB:
Definition 2.1 (Refinement): A target function tgt refines a source function src, denoted , if for every input:
- If src triggers immediate UB, tgt may exhibit any behavior
- If src returns poison, tgt may return poison or any other value, but must not trigger immediate UB
- If src returns a well-defined value, tgt must return the same value and must not trigger immediate UB
Limitations of Alive2 (SMT-based Validation)
Alive2, the state-of-the-art translation validator, has three inherent limitations:
- Solver Scalability: Bit-blasting fixed-width arithmetic to Boolean circuits becomes expensive for nonlinear operations and complex formulas.
- Fixed-Bitwidth Reasoning: Standard SMT bit-vector logics (QF_BV) cannot validate transformations over symbolic bitwidths.
- Bounded Loop Unrolling: Loops are validated only up to a fixed unrolling depth, leaving subsequent iterations unchecked.
Motivation: LLMs + Interactive Theorem Provers
Interactive theorem provers (ITPs) like Lean can handle symbolic bitwidths, large bitwidths, and loops, but historically require prohibitive proof engineering effort. Recent advances in LLMs make ITP-based workflows viable—LLMs can iteratively synthesize and repair proof scripts while the ITP kernel rigorously guarantees correctness.
Methodology
Trivet's Workflow Architecture
Trivet launches two parallel branches for each transformation:
- Refinement branch: Emits a refinement scaffold with typed holes; the LLM fills the holes.
- Counterexample branch: The LLM proposes a counterexample candidate; Trivet converts it into a complete Lean proof of non-refinement.
Lean checks each proof, with diagnostics guiding LLM revisions until acceptance or time budget expiration.
Refinement Proof Generation
UB-Sensitive Construct Identification
Trivet identifies nine cases where operators or instruction flags may produce UB (e.g., signed overflow for nsw, zero divisor for udiv). The scaffold splits cases on these conditions and automatically discharges obligations whenever the source yields UB.
Illustrative Example
For a transformation with symbolic bitwidth :
where , .
The scaffold:
- Identifies five UB-sensitive constructs (C1–C5)
- Performs case analysis on source conditions (C1–C4)
- Leaves proof holes for value equivalence and target-UB absence
- The LLM fills holes; Lean kernel type-checks the assembled proof
Proof Scaffold Generation Categories
- Source's UB-triggering circumstances (ordered by value demand)
- Target-specific circumstances that can trigger immediate UB
- Value-selection conditions (e.g., in
selectinstructions)
Counterexample Proof Generation
For invalid transformations, the LLM proposes a candidate counterexample with concrete bitwidths and input values. A deterministic scaffold then:
- Assumes the refinement theorem for contradiction
- Specializes it to the proposed counterexample
- Uses a computable
Decidableinstance for the refinement relation to evaluate concrete outcomes - Reduces the proposition to
False, establishing non-refinement
Loop-Containing Transformations
Trivet provides preliminary support for a restricted class: source functions with canonical loop form (entry preheader, single self-looping body block, dedicated exit block) transforming to loop-free targets.
Loop-specific proof holes include:
- Loop invariant: summarizes reachable source-loop state and function inputs
- Inductive step: preserves the invariant and proves equality with target result at loop exit
- Final assembly: establishes invariant at loop entry and uses induction for any iteration count
Implementation
Built on Lean-MLIR's LLVM dialect, extended with:
- Refined semantic foundations for immediate UB and transformation preconditions
- Missing bit-manipulation operators (ctpop, cttz, ctlz)
- Multi-parameter symbolic bitwidths and multi-block transformations
The LLM (gpt-5.5 via Codex CLI) operates as an agent; Trivet remains a deterministic harness accepting output only after Lean kernel verification.
Empirical Validation / Results
Dataset Composition
| Loop Structure | RQ | Validity | Fixed | Symbolic | Total |
|---|---|---|---|---|---|
| Loop-free | RQ1 | Valid | 30 | 30 | 60 |
| Loop-free | RQ2 | Invalid | 30 | 30 | 60 |
| Loop-containing | RQ3 | Valid | 7 | 7 | 14 |
| Loop-containing | RQ3 | Invalid | 7 | 7 | 14 |
| Overall | - | - | 74 | 74 | 148 |
RQ1: Validating Transformations
| Stratum | N | Trivet Proved | Trivet Mean (s) | Alive2 Proved | Alive2 Mean (s) |
|---|---|---|---|---|---|
| Symbolic bitwidth | 30 | 30 | 722 | n/a | n/a |
| Fixed bitwidth | 30 | 30 | 647 | 20 | 206 |
| Total | 60 | 60 | 684 | 20 | 206 |
Trivet validates all 60 transformations; Alive2 validates only 20 of 30 fixed-bitwidth cases (10 time out).
RQ2: Refuting Invalid Transformations
| Stratum | N | Trivet Refuted | Trivet Mean (s) | Alive2 Refuted | Alive2 Mean (s) |
|---|---|---|---|---|---|
| Symbolic bitwidth | 30 | 30 | 66 | n/a | n/a |
| Fixed bitwidth | 30 | 30 | 56 | 29 | 47 |
| Total | 60 | 60 | 61 | 29 | 47 |
RQ3: Loop-Containing Transformations
| Bitwidth | Task | N | Succeeded | Median (s) | Mean (s) |
|---|---|---|---|---|---|
| Symbolic | Validation | 7 | 7 | 1471.2 | 1673.5 |
| Fixed | Validation | 7 | 7 | 1741.7 | 1821.7 |
| Symbolic | Refutation | 7 | 7 | 87.5 | 88.6 |
| Fixed | Refutation | 7 | 6 | 211.1 | 207.8 |
The one unresolved invalid case: replacing a loop computing with return (x × n) fails when and , as the target propagates poison while the source returns 0.
RQ4: Ablation Study (Scaffold Impact)
| Task | Trivet⁻ Success | Trivet Success | Speedup (mean) | Cost Reduction |
|---|---|---|---|---|
| Refinement (loop-free) | 47/60 | 60/60 | 4.3× | 88.9% |
| Counterexample (loop-free) | 49/60 | 60/60 | 15.1× | 97.5% |
| Refinement (loop-containing) | 12/14 | 14/14 | 1.9× | 76.9% |
| Counterexample (loop-containing) | 13/14 | 13/13 | 5.3× | 89.7% |
Theoretical and Practical Implications
Advantages Over SMT-Based Approaches
-
Symbolic Bitwidth Reasoning: A single Trivet proof establishes refinement for all admissible widths, whereas Alive2 requires separate validation per concrete width.
-
Unbounded Loop Guarantees: Trivet proves correctness for all feasible loop iteration counts via induction, unlike Alive2's bounded unrolling which leaves subsequent iterations unchecked.
-
Scalability: Trivet decomposes complex obligations into focused subgoals, avoiding the performance cliff caused by whole-function formulation and bit-blasting in SMT solvers. Example: a simple transformation times out in Alive2 at i32 but Trivet validates it in 339s.
Cost-Effectiveness of Scaffolding
The scaffold approach reduces proof generation from scratch by:
- Guiding the LLM through well-defined subgoals
- Avoiding extensive tactic exploration
- Reducing monetary costs from 1.47 per refinement proof (88.9% reduction)
- Enabling proofs that unscaffolded generation cannot complete (26 additional proofs)
Conclusion
Trivet demonstrates that combining LLMs with interactive theorem provers offers a viable path for automated translation validation of LLVM transformations, addressing limitations of SMT-based approaches. Key takeaways:
- Comprehensive coverage: Validates all 74 valid transformations and refutes 73 of 74 invalid ones across 148 real-world cases
- Beyond Alive2's scope: Handles symbolic bitwidths, provides unbounded loop guarantees, and solves cases where Alive2 times out
- Trustworthy automation: Every verdict is checked by the Lean kernel, with proofs verified to contain no
sorry/admitand depend only on standard axioms
Future directions include:
- Extending formalization to memory operations and floating-point arithmetic
- Supporting richer loop classes beyond the restricted canonical loop form
- Generalizing to arbitrary control-flow graphs with CFG-specific invariants and induction obligations
Related papers
- DASC: Decay-Aware State Compression for Hybrid Linear-Attention Serving
DASC compresses recurrent state checkpoints by retaining only units with long retention horizons, achieving 2.63x compression, 42.6% lower TTFT, and 68.4% higher throughput with negligible quality loss.
- SWE-MeM: Learning Adaptive Memory Management for Long-Horizon Coding Agents
SWE-MeM trains agents to proactively compress their own context via a learned memory tool, achieving 60.2% on SWE-Bench Verified with a 30B model under a 32K budget, outperforming larger models and reducing token usage.
- PIVOT: Efficient Query-Group Indexing for Token-Level Sparse Attention
PIVOT groups nearby queries to share one proxy indexer scan, cutting indexing cost from O(gL) to O(L) and speeding up long-context inference up to 4.8x without accuracy loss.