ADR 0014: An IR verifier and the guards that point at builds¶
Status: Final
Type: Architecture
Created: 2026-07-28
Amended-By: 0015 (appended IR16), 0021 (appended IR17 and corrected IR05's statement), 0024 (appended IR18), 0033 (answers the deferral recorded under "what this does not resolve"), 0055 (appended IR19), 0070 (appended IR20)
Related-ADRs: 0011 (the one-runtime-family ABI check this generalises). Provenance: notes/accept-then-wrong.md §§11–14.
Context¶
The running log of programs the compiler accepted and then compiled wrongly had, by this point, stopped being a list of unrelated mistakes and become repeating structures that the project's existing guards cannot see by construction:
- Cross-backend parity cannot see a case all three backends miscompile. A value crossing into a runtime helper at the wrong width passes the unit tests, the parity checks and the stdout differential, because the backends do not disagree: they all err in the same direction, against a C definition that is none of their business.
- A build failure has no output for a guard to compare. One backend refusing to build a program the others build and run correctly reads as "the third backend does not support this"; a linkage predicate spelled in three places stops linking on them all the day a fourth caller misses the exception.
- A rule that lives only in prose is a rule each new caller rediscovers by breaking it. Three separate lowerings learned block-creation order independently; only prose stood between it and a fourth.
Decision¶
1. compiler/verify.py: invariants checked on every lowered module¶
The verifier runs on every compiled module, after lowering and before any backend sees the IR. It raises an AssertionError, for the reason ADR 0011 gave PygoAbiError: no POST program can cause any of these. A verifier failure is a compiler bug and has to read like one, because a diagnostic invites the reader to fix their program.
Twenty invariants have stable codes that appear in failure messages. Fifteen shipped with this ADR; the later ones were appended by the ADRs named in the preamble, following the procedure under Consequences. This table is the one place the codes are written out by hand. A test holds it equal to the verifier's own list, so an invariant cannot be added without the ADR that documents it.
| Code | Invariant |
|---|---|
IR01 |
a function has at least one basic block |
IR02 |
block labels are unique within a function |
IR03 |
every basic block has a terminator |
IR04 |
every branch target names a block of the same function |
IR05 |
every use of a value follows its definition in emission order |
IR06 |
an opaque handle never reaches arithmetic, a cast or a condition |
IR07 |
an array value never reaches a scalar operand position |
IR08 |
an array operation's base is an array and its index an integer |
IR09 |
a value is only stored into a slot that can hold it |
IR10 |
a comparison or an overflow test produces a Bool and a condition consumes one; an overflow test reads two Int64s |
IR11 |
a constant's Python value matches its declared dtype |
IR12 |
a return matches the function's declared return type |
IR13 |
a call to a runtime helper matches the C definition it will link to |
IR14 |
a call to a POST function matches its declared signature |
IR15 |
a function another translation unit calls has external linkage |
IR16 |
an arithmetic operand is a value promotion is defined over |
IR17 |
every use of a value is dominated by its definition |
IR18 |
a reference is only released through a slot the entry block owns |
IR19 |
a counted-loop record names values the function defines |
IR20 |
a function value is a function of the module of its signature, called with its argument and result types |
The checks are written over the predicates the compiler already has. Which values are opaque handles, which slot can hold which value, which function has internal linkage: the verifier imports the typechecker's and the backends' own predicates. A predicate spelled in two places is one predicate with one chance to miss an exception. A verifier with a private copy of a rule would reproduce the drift it exists to catch.
2. compiler/abisig.py: one parser, one C vocabulary, one comparison¶
ADR 0011 answered the ABI-width class by parsing the C of the pygo wrapper unit and holding each emitted call to it. The lesson generalises: a rule about two files agreeing is a rule about there being two files. This ADR brings every other helper family (print, Str, list, scalar builtins, the exception runtime, the CPython FFI, arithmetic) under the same guard.
One module holds the definition parser, the closed C-type vocabulary and the per-call comparison. Each helper signature is parsed from the C that defines it; IR13 checks each emitted call against one. The idea extends further. A generated symbol's definition is emitted from the signature the verifier checks. A libm intrinsic is checked on the emitted IR against the signature of the function it stands for, so one question is answered once.
void * means "some handle": at the ABI it is one pointer. The alternative, a hand-maintained map from C parameter names to handle types with a permissive default, is the anti-pattern the campaign log is about. Which handle a value is remains checked by IR09, IR14 and the frontend's gates. The rule is this wide by choice, recorded at the moment it was chosen.
3. It runs in release builds too¶
The verifier is not gated on the build mode, on an environment variable or on pytest, for these reasons in order of weight:
- The bugs it catches are worst in release. A program that aborts in debug can run to completion with wrong answers in release, because the assertions are elided: the argument for these guards, in that configuration.
- A guard that runs in one configuration goes untrusted in the other. The whole campaign is about checks that were structurally blind; a check that is switched off is blind on purpose.
- The cost is a small fraction of frontend time, against a compile dominated by the C compiler by orders of magnitude. There is nothing to buy back.
That cost is a design constraint: each check is one linear pass over the module. What a check needs per module is computed once per module, never per instruction. The single exception, IR17's dominator tree, is skipped for single-block functions.
4. What it does not check¶
The verifier checks structure and types. Its scope excludes meaning. The campaign's scorecard names each finding it cannot see. The categories:
- Well-typed wrong answers.
s = "hello"; return slowering toConst(Int64, 0)is perfectly well-formed IR; a verifier that rejected it would reject every integer zero in the compiler. These need a diagnostic (the frontend's PP900/PP410) or a CPython differential; both exist. - Policy. Whether
Float16is lowered, whether an annotation is supported, whetherStrships. The IR of a rejected program is never built, so there is nothing to verify. - Backend semantics. What a backend does with a correct instruction (a libm quirk, constant emission, rounding) is the differential corpus's job; the verifier stops at the IR.
- Foreign ownership. A borrowed-argument refcount in a generated export wrapper is a CPython rule about C the verifier does not read.
- Test architecture. Defects of the suite itself (private corpora, stale caches, under-sampled generators) are not IR properties.
Naming these is the point. A clean sheet would mean the table had been edited.
5. The build assertion¶
A guard that compares output is blind to a build failure, which has none. A test takes a fixed set of programs chosen for shape (control-flow and binding forms that have broken a backend before, and each runtime helper family), emits each through all registered backends and compiles it to an object file. There is no expected output, no oracle and no argument list: the assertion is the toolchain's exit status.
It does not link, so a symbol with incorrect linkage is not caught there; IR15 catches it, answering the question on the IR without a linker and without pygo's runtime present. The guards complement each other. A backend that cannot handle a program skips with a stated reason, so a backend that drops out of the comparison without one fails the test, while one that says why merely thins it.
Consequences¶
How a new invariant gets added¶
- Append the invariant to the verifier's list with the next code and a one-line rule, lower case, no trailing period, and append the same row to the table above. The codes are dense and ordered, which a test checks: a gap would be a rule deleted while the ADR stayed silent about which.
- Add the check. If it needs a predicate the typechecker or a backend already has, import it; do not restate it.
- Add a test that the verifier fails on the smallest hand-built IR that violates it. A verifier that never fires is indistinguishable from no verifier.
- If the invariant comes from a campaign finding, add or update its row in the scorecard.
- Keep it linear: nothing that is linear in the module may run per instruction.
The obligation this creates¶
The checks read an instruction's shape only through the uses and definitions it declares. An IR node added without that declaration would be invisible to all invariants at once, so the verifier raises on an unknown node, which makes the failure loud. This is the one place the verifier can rot silently: check it when the IR gains a node.
What this does not resolve¶
- The build assertion covers a fixed set of shape-chosen programs. The right end state is that the whole-program corpora build on all backends as a matter of course; that is a much larger runtime bill and a separate decision.
IR05andIR17together defer a real decision. A future optimisation pass that reorders blocks legally would have to reorder declarations with them, or dropIR05, keepIR17, and give the C backend a declaration-hoisting pass. That decision is deferred; it must not be taken by accident.- A module built by hand is unverified unless its builder asks. Every compile path of the compiler runs the verifier (a multi-module program once more per unit, so a cross-module call has a callee to be compared against), so the gap exists in principle only.