Skip to content

ADR 0021: IR05 was one invariant wearing two backends' clothes

Status: Final Type: Architecture Created: 2026-07-29 Amends: 0014 (IR05's statement in its invariant table, and its rule that the verifier's cost stays linear); answers the item left open under "what this does not resolve" Amended-By: 0033 (definite assignment is the frontend's, and 0033 is where it is asked; the boundary drawn here is unchanged) Provenance: arbitrations A53, A100; upstream questions U30, U31.

Context

0014 shipped fifteen IR invariants and said of the fifth that it was stated as textual order because "textual order implies dominance in a single-entry CFG whose blocks are emitted in creation order". The claim is false, and the counterexample (notes/accept-then-wrong.md §19) is not a corner case: it is how anyone writes a handler that fans out and collects: a name first bound inside a with TaskGroup() body and read after the with. That lowering creates the unwind block before it lowers the body, so the body's block is earlier in emission order than the continuation and IR05 was satisfied, correctly. Yet the continuation has a predecessor that never ran the body, so no definition of the name dominates the read. The program was accepted with zero diagnostics, two backends built silently wrong code from it, and the only instrument that caught it was LLVM's own verifier: somebody else's. The defect itself was closed at once in the frontend, by a diagnostic on that read (today 0033's checked read); this ADR is the verifier's half.

The general shape is the reason this is an ADR and not a bug fix. IR05 was written from a C backend failure and has been treated ever since as "the block-order check". It is not: it is a statement about the C backend's model, where every block is flattened into one function scope and a declaration is visible to everything textually after it. LLVM and QBE have a different model. An invariant that is a statement about one backend, checked for all of them, is a check that passes for the wrong reason. pygo files the general question upstream as U30 (when an implementation's backends have different models, what must the internal check guarantee?) and proposes the answer this ADR implements: a multi-backend implementation owes invariants to the strictest backend's model. Here neither model is stronger than the other, so there are two invariants.

Decision

IR17: every use of a value is dominated by its definition

Appended per 0014's own procedure. IR05 is unchanged in what it checks; the statement is corrected to say which backend it is about and no longer claims to imply dominance.

Code Invariant Whose model
IR05 every use of a value follows the definition in emission order the C backend: one flat scope, blocks in list order
IR17 every use of a value is dominated by its definition LLVM and QBE: SSA

Neither implies the other, which is why both are kept and neither replaces the other. notes/accept-then-wrong.md §14's match arm violates emission order while dominance holds; §19's with TaskGroup() body violates dominance while emission order holds. A check that only asked the second would have let §14 through, and §14 is the finding the whole verifier exists for.

What IR17 excuses

A local the lowering assigns more than once is not an SSA value in any of these backends: LLVM and QBE alloca it in the entry block and turn every assignment into a store. Its address therefore dominates everything, and IR17 skips it (the memory-slot exemption).

That means IR17 says nothing about whether the slot was stored to on the path that reads it. That is definite assignment, a different question, and not the verifier's. No backend's verifier asks it either (LLVM will happily build a load from an untouched alloca). A verifier that did half of a second job is the recurring failure the campaign notes keep finding. The frontend asks it (0033).

The slot-versus-register predicate is asked: the SSA backends and the verifier consult one shared memory_slot_names, as IR15 asks has_internal_linkage, because a rule spelled in several places is a rule whose copies drift.

The algorithm, and the cost discipline

The algorithm is Cooper–Harvey–Kennedy ("A Simple, Fast Dominance Algorithm", 2001): reverse postorder by one iterative DFS, immediate dominators by intersecting each block's processed predecessors over that order, then one Euler tour of the dominator tree so a dominance query is two integer comparisons in constant time. No bit-vectors and no per-block set intersection: an iterative dataflow over sets of blocks is quadratic in block count, which 0014's "It runs in release builds too" rules out. Three things keep it off the hot path. The check is skipped for a function with fewer than two reachable blocks (most functions in any module). IR05 and IR17 run in one walk, because enumerating uses is the most expensive thing the verifier does. The passing path is inlined, with only the failure paths out of line. A structural test holds the check linear in block count with a chain of diamonds, the shape that would expose a quadratic term.

Consequences

  • When IR17 landed, the invariant was unsound and the compiler no longer was: the frontend diagnostic described above already refused the one lowering that could produce a violation. IR17's value is prospective: it is the guard the next with-shaped lowering meets in place of a three-backend divergence.
  • A future block-reordering pass is still a decision. A pass that reordered blocks legally would have to reorder declarations with them, or relax IR05 and give the C backend a declaration-hoisting pass. That trade now has a name, drop IR05 and keep IR17. It must not be taken by accident.
  • The audit question generalises. An instrument checks the invariant it was given. A passing check is evidence about that invariant only. The way to find the next one is to ask of each invariant whose model is this a statement about, and whether the other backends share it. Asked of all seventeen, IR05 was the only divided answer; IR13 is the one to re-ask first if a fourth backend arrives, because "the C definition it will link to" is a statement about there being a linker.