1 00:00:01,000 --> 00:00:09,300 [Hal Turing] Alrighty! Thanks for tuning in! Hello AI world! I am your host, Hal Turing, and my co-host is Dr. Ada Shannon. 2 00:00:09,300 --> 00:00:44,974 [Hal Turing] Today we're looking at "Verifying Peephole Rewriting In SSA Compiler IRs," by Siddharth Bhat, Alex Keizer, Chris Hughes, Andrés Goens, and Tobias Grosser. It's a collaboration between Cambridge, the University of Edinburgh, and the University of Amsterdam, posted to arXiv in July 2024 and published at ITP that year. The core question is whether a Lean proof assistant can give MLIR-style compilers a verification workflow for small rewrites that feels push-button. The catch is that it has to do that without the cost of a fully verified compiler. 3 00:00:44,974 --> 00:01:05,000 [Dr. Ada Shannon] The stakes are that compilers rewrite your code thousands of times, and every rewrite is a chance to silently change what it computes. For ML people, that means a wrong kernel gives you slightly wrong numbers, and nobody blames the compiler. The paper wants those rewrites proved, including in the new domain-specific IRs where today's SMT tools don't reach. 4 00:01:05,000 --> 00:01:09,049 [Hal Turing] Start with the basic object. What's a peephole rewrite? 5 00:01:09,049 --> 00:01:31,150 [Dr. Ada Shannon] It replaces a small pattern of instructions with an equivalent, cheaper one. The classic is y = x + 1, then z = y - 1, which becomes z = x. McKeeman named the idea in 1965, for straight-line assembly. The paper cares about the SSA version. SSA, static single assignment, means every variable is assigned exactly once, so each use points straight at its definition. 6 00:01:31,150 --> 00:01:33,875 [Hal Turing] Why does that change the matching? 7 00:01:33,875 --> 00:01:55,250 [Dr. Ada Shannon] Because you follow the def-use chain instead of looking at neighbors. Put an unrelated instruction, say p = y, between those two lines. A straight-line matcher, like the CompCert peephole work by Mullen and colleagues in 2016, sees a gap and misses it. A dataflow matcher sees that z's definition uses y, and y's definition uses x, and rewrites to z = x anyway. 8 00:01:55,250 --> 00:01:57,775 [Hal Turing] And this matters at scale? 9 00:01:57,775 --> 00:02:19,349 [Dr. Ada Shannon] The paper says InstCombine, LLVM's peephole pass, is about ten percent of its IR-transforming code. That's more than the loop optimizer. And Alive is the most referenced tool in LLVM's commit log. Alive is from Lopes, Menendez, Nagarakatte, and Regehr in 2015. You write a rewrite in a small language and it translates to an SMT query for Z3. You get a proof or a counterexample. 10 00:02:19,349 --> 00:02:22,750 [Hal Turing] So that's already push-button. What's missing? 11 00:02:22,750 --> 00:02:51,675 [Dr. Ada Shannon] Alive works at one fixed bitwidth per query. It's tied to LLVM-style IR and to the theories SMT solvers support. Its successor, Alive2 from Lopes, Lee, Hur, Liu, and Regehr in 2021, validates real optimized functions against their originals, with loops handled by bounded unrolling. It's the practical baseline here. The opposite pole is end-to-end verification in a proof assistant, like CompCert, Leroy's verified C compiler in Coq. It's strong, but it takes years of expert effort and is tied to one IR version. 12 00:02:51,675 --> 00:02:55,800 [Hal Turing] And MLIR makes the moving-target problem worse. 13 00:02:55,800 --> 00:03:15,700 [Dr. Ada Shannon] Yes. MLIR, from Lattner and colleagues in 2021, is a framework where you define your own IRs, called dialects, for tensors, GPUs, quantum circuits, or encrypted computation. Many have value semantics, meaning no hidden side effects, so local reasoning is easy. New dialects keep appearing, and a CompCert-style proof can't track that. 14 00:03:15,700 --> 00:03:21,625 [Hal Turing] The paper also leans on something called regions. I don't have a picture of those. 15 00:03:21,625 --> 00:03:43,625 [Dr. Ada Shannon] A region is MLIR's way of nesting a program inside an operation. The operation receives sub-programs as arguments and decides whether to run them zero, one, or many times. So an if, a bounded for loop, or a tensor contraction is one operation with a body, not a graph of basic blocks and branches. If you know jax.lax.scan or torch.cond, it's the same shape: the control flow carries its body. 16 00:03:43,625 --> 00:03:46,225 [Hal Turing] And the proof tool is Lean. 17 00:03:46,225 --> 00:04:02,275 [Dr. Ada Shannon] Lean is an interactive theorem prover. A human guides the proof, and a small trusted core checks every step. Its big math library, mathlib, matters here, because it has ring and quotient-ring reasoning that SMT solvers can't do. 18 00:04:02,275 --> 00:04:05,500 [Hal Turing] Give me the example where that matters. 19 00:04:05,500 --> 00:04:28,125 [Dr. Ada Shannon] Fully homomorphic encryption, where polynomials live in a quotient ring: integers mod q, polynomials modulo X to the 2^n plus one. In that ring, a + (X^(2^n) + 1) rewrites to just a, because X^(2^n) + 1 is zero there. Algebraically that's trivial. Written as loops, loads, and branches in LLVM, it becomes a heroic proof effort. The high-level IR keeps the intent visible, and a ring-aware prover can use it. 20 00:04:28,125 --> 00:04:33,750 [Hal Turing] One more definition. When we say a rewrite is correct, correct against what? 21 00:04:33,750 --> 00:04:53,225 [Dr. Ada Shannon] Refinement. LLVM has poison, a deferred error value, and undefined behavior, which is immediate. The new program may be more defined than the old one, never less. The paper collapses both into one none value. That's safe only if the right-hand side triggers undefined behavior only when the left-hand side does. 22 00:04:53,225 --> 00:04:57,575 [Hal Turing] Is that the whole trust story? Prove each rewrite and we're done? 23 00:04:57,575 --> 00:05:19,500 [Dr. Ada Shannon] No, and this is my added context, not the paper's. Csmith, from Yang, Chen, Eide, and Regehr in 2011, found hundreds of bugs in GCC and LLVM with random programs. Its CompCert results are the telling part: bugs turned up in the unverified front end, not the verified middle and back end. A proved local rewrite doesn't cover parsing, lowering, or pass ordering. Keep that boundary in mind. 24 00:05:19,500 --> 00:05:22,100 [Hal Turing] Noted. So what do they build? 25 00:05:22,100 --> 00:05:40,650 [Dr. Ada Shannon] A Lean framework, which the paper calls LeanMLIR, that's generic over the dialect. They test it on three case studies: LLVM bitvector rewrites, structured control flow with regions, and the encrypted-computation polynomial IR. A user supplies surprisingly little, so let's look at how it's built. 26 00:05:40,650 --> 00:05:47,275 [Hal Turing] So what does a dialect author actually have to write? I'm picturing a mountain of dependent types. 27 00:05:47,275 --> 00:06:19,075 [Dr. Ada Shannon] Less than that. You supply three things: your types, your operations with their signatures, and a denotation, which is a plain Lean function saying what each operation computes. The framework builds everything else. Their running example is called QuotRing. It has two types, a ring element and a natural number, and three operations: add, monomial and constant. The ring element denotes an element of the quotient ring from the FHE example, with coefficients mod q, modulo X to the 2^n plus one. That's a mathlib type. 28 00:06:19,075 --> 00:06:22,701 [Hal Turing] And the framework turns those into whole programs? 29 00:06:22,701 --> 00:06:45,501 [Dr. Ada Shannon] Yes. It defines Expr for a single operation and Com for a sequence of them, each binding a new variable until a return. Both are well typed by construction. A context lists which variable types are in scope, and a variable carries a proof that its slot has the right type. A mis-typed program can't even be written down. The denotation of any program is then a Lean function from variable values to a result, built by lifting your operation denotations. 30 00:06:45,501 --> 00:06:48,051 [Hal Turing] So what does a rewrite look like? 31 00:06:48,051 --> 00:07:10,451 [Dr. Ada Shannon] A PeepholeRewrite holds a left-hand side, a right-hand side, and a proof field. Both sides are written in MLIR syntax, pasted into Lean through a syntax extension. The proof says the two denotations are equal, or for LLVM, that the right side refines the left. The goal is the Alive-style workflow: copy a snippet, get a response. They also patched the upstream MLIR docs where they found inconsistencies, but that's a side note. 32 00:07:10,451 --> 00:07:16,851 [Hal Turing] The proof is where I'd expect pain. The goal is full of Expr and valuation machinery. 33 00:07:16,851 --> 00:07:45,501 [Dr. Ada Shannon] That's what simp_peephole is for. It strips out the framework and leaves a statement about plain values. For the FHE rewrite, it's an equation in the quotient ring, which mathlib's algebra can close. For LLVM, it's a statement about bitvectors, and a second tactic, alive_auto, uses a commutative-ring decision procedure plus an extensionality lemma. That lemma says two bitvectors are equal if they agree at every bit index. Much of the bitvector theory didn't exist in Lean, so they built it and upstreamed it to mathlib and later Lean core. 34 00:07:45,501 --> 00:07:49,601 [Hal Turing] Now regions. Ops can take programs as arguments? 35 00:07:49,601 --> 00:08:21,801 [Dr. Ada Shannon] Right. An op receives sub-programs and its denotation decides how often to run them. Zero or one time gives an if, n times gives a bounded loop. The matcher treats regions as black boxes: it checks them for structural equality but doesn't look inside. So a rewrite can have free variables but not free region arguments. Loop fusion, loop reversal and deleting a zero-iteration loop hold for any body, so they're proved as separate theorems, not peepholes. But rewriting a loop that adds a constant delta n times into one multiplication works as a peephole, because the body is a known constant. 36 00:08:21,801 --> 00:08:26,627 [Hal Turing] How do they know a local rewrite is safe in a whole program? 37 00:08:26,627 --> 00:08:55,852 [Dr. Ada Shannon] Through a zipper. Split the program at a position into the bindings already seen and the rest. Match the pattern against the def-use tree ending at that position, substitute, insert the right-hand side, and stitch back, redirecting later uses to the new result. They prove split and stitch each preserve meaning, so the local proof gives the theorem denote_rewritePeephole, which says the rewritten program denotes the same thing. They also did dead-code elimination in about 400 lines, and common-subexpression elimination, as reuse tests. 38 00:08:55,852 --> 00:08:59,102 [Hal Turing] What about operations with side effects? 39 00:08:59,102 --> 00:09:16,402 [Dr. Ada Shannon] Each op is marked pure or impure. Pure ops denote in the identity monad, impure ones in an IR-chosen monad, and rewrites touch only pure ops inside effectful programs. One stated limitation: all regions are currently assumed possibly side-effecting. 40 00:09:16,402 --> 00:09:23,502 [Hal Turing] Back to LLVM, since we flagged poison and undefined behavior earlier. How is that modeled? 41 00:09:23,502 --> 00:09:44,952 [Dr. Ada Shannon] Bitvectors denote into an option type, and both poison and undefined behavior collapse to none. Only division and remainder can trigger undefined behavior, so the authors check each such rewrite by hand. They don't model the nsw and nuw flags. To trust the semantics, they ran every modeled instruction on all inputs up to bitwidth 8 and compared against LLVM's opt constant-folding the same instruction. 42 00:09:44,952 --> 00:09:50,102 [Hal Turing] So the model is checked up to 8 bits, but the proofs claim all widths. 43 00:09:50,102 --> 00:09:57,177 [Dr. Ada Shannon] Yes, and it's differential testing of a spec, not a proof of fidelity. It covers only pure instructions. 44 00:09:57,177 --> 00:09:59,352 [Hal Turing] And the proof results? 45 00:09:59,352 --> 00:10:21,377 [Dr. Ada Shannon] Alive's suite has 435 tests. They translated 93, meaning those in their fragment with no preconditions. Of those, 54 are proved automatically and 6 by hand, so 60 of 93, about two out of three. That's about 14 percent of the full suite. The 6 were picked because SMT solvers are slow on them even at 64 bits. The paper doesn't say why the other 33 are unproved. 46 00:10:21,377 --> 00:10:24,627 [Hal Turing] What about effort? Does this cost a lot? 47 00:10:24,627 --> 00:10:35,527 [Dr. Ada Shannon] The reusable core is about 2,200 lines. The three case studies total about 5,600. That's the only cost evidence offered. 48 00:10:35,527 --> 00:10:37,977 [Hal Turing] Two case studies left. 49 00:10:37,977 --> 00:11:18,052 [Dr. Ada Shannon] Structured control flow is scf(X), parametric over another IR, with if and bounded for. The loop proofs come from mathlib's theory of iterating a function: applying it zero times is the identity, and k times then l times is k plus l times. The FHE one is the Poly IR, which is still in flux in the HEIR and open-standard discussions. It's quotient-ring based, but to_tensor, from_tensor and leading_term depend on a non-canonical choice of representatives. They proved commutativity of multiplication, from_tensor of to_tensor giving back p, and adding a monomial 2^n being the same as subtracting 1. SMT solvers have no theory for this kind of ring, so this is where the prover earns its place. 50 00:11:18,052 --> 00:11:27,277 [Hal Turing] Let me start with the one number we have. Sixty of the ninety-three translatable Alive rewrites. What happened to the other thirty-three? 51 00:11:27,277 --> 00:11:50,252 [Dr. Ada Shannon] The paper doesn't say. The ninety-three already exclude rewrites with preconditions, and nsw and nuw flags aren't modeled. So the thirty-three failed inside a fragment that was already filtered. Weak automation and unmodeled features are both possible, and the source can't separate them. It also doesn't say whether anyone tried hand proofs on those thirty-three. The six manual proofs were picked because an SMT solver is slow on them, so they say nothing about the unpicked ones. 52 00:11:50,252 --> 00:11:53,152 [Hal Turing] And the Alive2 comparison? 53 00:11:53,152 --> 00:12:10,477 [Dr. Ada Shannon] There isn't one. Alive2 is push-button and bounded, and it checks per width. To settle it you'd run Alive2 on the same ninety-three, count how many of the sixty proofs only exist because of the universal bitwidth quantifier, and record proof times. None of that is reported. 54 00:12:10,477 --> 00:12:17,452 [Hal Turing] So does arbitrary bitwidth earn its keep? Most InstCombine rewrites look width-uniform to me. 55 00:12:17,452 --> 00:12:45,752 [Dr. Ada Shannon] For the LLVM bitvector case, that's the weakest justification. Checking a handful of widths gives most of the practical confidence. The paper's own caveat shows the cost: a plus b equals a xor b holds only at width one, so generality sometimes needs side conditions. The strong cases are elsewhere. One is the FHE quotient ring, where SMT has no theory at all. The other is region-bearing code. Wide bitvectors in hardware contexts are a third, but the paper only gestures at them. 56 00:12:45,752 --> 00:12:50,578 [Hal Turing] What about trusting the semantics? We mentioned the check against opt. 57 00:12:50,578 --> 00:13:22,253 [Dr. Ada Shannon] Exhaustive up to width eight, on pure instructions, using LLVM's constant folding as ground truth. That tests agreement with one folder, not with the language reference, and it says nothing directly about behavior above eight bits. The poison and undefined-behavior collapse is weaker still. The refinement order from Lee and colleagues' 2017 paper, Taming Undefined Behavior in LLVM, puts ub below poison below values. The framework flattens that into Option.none. That's sound only if the right side triggers ub when the left side does, and for division and remainder the authors check it by hand. 58 00:13:22,253 --> 00:13:27,278 [Hal Turing] So the subtlest semantic step sits outside the machine-checked part. 59 00:13:27,278 --> 00:13:45,428 [Dr. Ada Shannon] Yes. The paper doesn't mechanize that check, so its reliability depends on how carefully each manual check was done. Add no nsw or nuw flags and no preconditions, and you've dropped a large share of what real InstCombine does. I can't give you a fraction. The paper doesn't compute it. 60 00:13:45,428 --> 00:13:50,478 [Hal Turing] Regions were the headline. But matching treats them as black boxes. 61 00:13:50,478 --> 00:14:12,478 [Dr. Ada Shannon] Right. So the loop fusion, reversal and dead-loop theorems are standalone lemmas about Nat.iterate. They aren't instances of the verified rewriter. The rewriter helps around a region but not inside one. Effects make it worse: every region is assumed possibly side-effecting, so pure rewrites can't fire inside loop bodies, which is where tensor computation lives. The bounded-loop restriction also rules out anything that might not terminate. 62 00:14:12,478 --> 00:14:18,578 [Hal Turing] And the original motivation, that CompCert-style proofs are too costly to maintain? 63 00:14:18,578 --> 00:14:37,703 [Dr. Ada Shannon] The paper reports lines of code, about 2,200 for the core and 5,600 for the case studies. It reports no maintenance experiment: no added op, no changed semantics, no port to a newer MLIR. The FHE Poly IR being in flux is a live test of that claim, but it isn't run as one. 64 00:14:37,703 --> 00:14:42,603 [Hal Turing] Is the verified rewriter connected to the real compiler at all? 65 00:14:42,603 --> 00:15:00,203 [Dr. Ada Shannon] Not in the paper. So this verifies a model of rewriting, with a gap to any shipping compiler pass. A proved local rule also doesn't rule out bugs in parsing, lowering or pass ordering, which is the Csmith point from earlier. Again, that's my addition, not the paper's claim. 66 00:15:00,203 --> 00:15:02,753 [Hal Turing] So who should use this today? 67 00:15:02,753 --> 00:15:19,778 [Dr. Ada Shannon] Dialect authors who need semantics for high-level IRs and algebraic rewrites, especially where SMT has no theory. If you do LLVM scalar peepholes, keep Alive2. Its push-button workflow is proven at scale, and nothing here shows this one beats it. 68 00:15:19,778 --> 00:15:21,853 [Hal Turing] And where does it go next? 69 00:15:21,853 --> 00:15:37,578 [Dr. Ada Shannon] The authors name several directions: separating ub from poison, interaction-tree semantics for effects, richer region side-effect tracking, a verified SAT checker for bitvector goals, more dialects, and coinductive semantics for nontermination. 70 00:15:37,578 --> 00:16:01,228 [Hal Turing] So, what's established: a mechanized, generic, region-aware peephole framework with a proved rewriting theorem and a workable MLIR-syntax workflow, shown on three IRs. The boundary is that the case against Alive or Alive2 on LLVM is thin. Sixty of ninety-three, no head-to-head, and no count of proofs that needed generality. Thanks for listening, everyone.