Accelerating Verified-Compiler Development with a Verified Rewriting Engine

Jason Gross, Andres Erbsen, Jade Philipoom, Miraya Poddar-Agrawal, Adam Chlipala

TL;DR

The authors present the first proven-correct compiler-construction toolkit parameterized purely on user-provided rewrite rules, implemented within the Coq proof assistant without expanding its trusted code base. By combining normalization by evaluation (NbE), parametric higher-order abstract syntax (PHOAS), and let-lifting to preserve subterm sharing, the system achieves a 10×–1000× speedup over the previous Fiat Cryptography pipeline and can be extracted to a fast, standalone command-line compiler.

What Problem Does This Paper Address?

Formally verified compilers guarantee that compiled code preserves source program semantics. However, modifying or optimizing a verified compiler is notoriously difficult: even minor changes usually require tedious, labor-intensive proof repairs.

One elegant solution is to express compiler transformations as algebraic rewrite rules ($A \to B$), where each rule is proven correct independently. Unfortunately, existing proof-producing rewriting engines (such as Coq’s built-in setoid_rewrite or rewrite_strat) scale at least quadratically in the number of variable binders. In real-world systems like Fiat Cryptography, single generated functions contain thousands of let-binders and variables; standard tactics choke, consume tens of gigabytes of RAM, or time out entirely.

Methods

The authors build a verified, extraction-ready rewriter generator that operates via a single Coq command (Make):

  • Normal Gallina Implementation: Unlike prior work (e.g., Aehlig et al.) that relied on unverified ML engines or expanded the proof assistant's trusted computing base (TCB), the core engine is written and fully proved sound in standard Gallina (Coq's logic).
  • Fused NbE and Rewriting: It integrates Normalization by Evaluation (NbE) with top-level rewriting, reducing higher-order functions and $\beta/\eta$-redexes while firing algebraic simplification rules.
  • Efficient Pattern Matching via Decision Trees: Adapts Maranget’s decision-tree compilation algorithm inside Coq to eliminate redundant subterm inspections across multiple rules. The decision tree serves merely as an untrusted hint, requiring minimal verification.
  • PHOAS (Parametric Higher-Order Abstract Syntax): Employs PHOAS to delegate variable substitution and binder bookkeeping directly to Coq’s kernel or the extracted runtime, bypassing first-order representation penalties (such as de Bruijn index shifting).
  • Let-Lifting (UnderLets): Explicitly preserves subterm sharing by hoisting nested let bindings to the top level, avoiding exponential term-size explosion during partial evaluation.
  • Side Conditions via Boolean Reflection & Abstract Interpretation: Supports conditional rewrites with computable Boolean side conditions on constants. To handle overflow constraints in low-level arithmetic (e.g., bounds on fixed-precision integers), the compiler runs a verified abstract interpreter upfront to annotate AST nodes with bounding decorator functions (clip_{l,u}), which rules then inspect.

Datasets

  • Synthetic Microbenchmarks:
  • Nested binders with zero-additions: An expression tree scaling $m$ redundant additions of zero across $2^n$ subterms.
  • Deeply nested let-binders: Scaling up to 5,000 chained let-binders to stress-test variable bookkeeping and subterm sharing against Coq’s native tactics (rewrite!, setoid_rewrite, and rewrite_strat).
  • Macrobenchmark: The complete cryptographic primitive suite from Fiat Cryptography, covering field arithmetic across curves including Curve25519, P-256, and P-384.

Key Insights and Ideas

  • Preserving Subterm Sharing is Mandatory: Inlining shared variables causes catastrophic blow-ups. Generating 64-bit field arithmetic for P-256 ran out of memory on a 64 GB machine when let-binders were naively inlined.
  • Retiring Continuation-Passing Style (CPS): Previous versions of Fiat Cryptography used CPS purely to force Coq's evaluator to maintain let-binders, significantly complicating correctness proofs. The new engine's let-lifting enables writing libraries in intuitive direct style, drastically simplifying theorem statements.
  • Zero-Trust Metacompilation: Generating custom inductive types for constants and pre-specializing decision trees in Coq yields native dispatch speeds without adding any axioms or trusted code.

Key Results

  • 10×–1000× Speedup: Replaced Fiat Cryptography's reduction pipeline, reducing generation time by up to three orders of magnitude.
  • Compiling Previously Unreachable Curves: Enabled compiler synthesis of code for the widely used curve P-384, which previously timed out on the original Fiat Cryptography pipeline.
  • Linear Binder Scaling: Handles microbenchmarks with 5,000 nested let-binders in ~10 seconds, whereas existing Coq rewriting tactics timed out at just a few hundred binders.
  • Fast Standalone Binaries: Extracted standalone OCaml binaries run ~10× faster than running reduction inside Coq's virtual machine.

Limitations and Future Work

  • Side-Condition Expressiveness: Side conditions are restricted to executable Boolean functions evaluated over compile-time constants (marked explicitly with '). General relational side conditions or unsolved unification variables are not supported.
  • Decoupled Abstract Interpretation: Bounds analysis is currently executed as a separate pre-pass that decorates the AST; fully integrating abstract interpretation into the rewriting loop is left to future work.
  • Language & Rewrite Constraints: Lacks support for non-fully-applied functions in equality rewrites, custom equivalence relations (only Leibniz equality is supported), first-class polymorphism, and arbitrary user-defined container types without dedicated eliminator hooks.

Summary updated

BibTeX
@inproceedings{gross2022accelerating,
  author = {Jason Gross and Andres Erbsen and Jade Philipoom and Miraya Poddar-Agrawal and Adam Chlipala},
  title = {{Accelerating Verified-Compiler Development with a Verified Rewriting Engine}},
  booktitle = {ITP},
  year = {2022},
  url = {https://openreview.net/forum?id=_q0qkBUN7E},
}
Figure