ADR 0054: The IR is declared once, and def-use is derived from it¶
Status: Final Type: Architecture Created: 2026-08-29 Related-ADRs: 0014 (the verifier this feeds), 0033 (the analysis that now asks where a name binds), 0010 (the capture rule a missing binding position broke)
Context¶
Which fields of an instruction define a value and which read one is a fact about the IR's classes in ir.py. It was written out four times. The verifier's uses and definitions were a match per instruction listing field names. The terminators' value fields and their successor labels were listed again. The QBE backend built the same out-edges a third time. The QBE value-numbering pass kept a fourth list, of the slots it may repoint, with a comment beside one row explaining why a declaring assignment's target is excluded.
The four agreed. Nothing made them agree: each was a copy of ir.py obligated to track it. Adding an instruction meant editing all four and hoping. The compiler would run either way, so a miss would surface as a mis-optimization.
The repo already refuses this shape elsewhere. postpile's declarations of C helpers are derived from the C that defines them, on the stated ground that a signature written in two places is one signature with a chance to drift. A def-use table is the same claim about a different fact.
Decision¶
compiler/grammar.py declares the IR to astero; consumers ask that declaration.
The declaration reads the field names, sorts and shapes off ir.py's own classes, so those are never restated. The authored part is a role per field, for each instruction and each terminator. Two namespaces separate what looks alike: a block label is a str like a call's callee name, and only the namespace a field uses tells them apart. An assignment's target has a conditional role: it defines its target when the assignment declares it and reads it otherwise, because a declaring assignment defines its target where a plain one writes through it.
The four tables became three lines and a comprehension. The verifier's uses and definitions, like a terminator's uses, ask which fields read or bind a value. A terminator's successors ask which fields read a label; the QBE backend's control-flow graph calls the same query. The slots QBE's value numbering may repoint are the fields that read a value and, under no condition, introduce one. The declaring assignment's exclusion falls out of its two roles where a comment used to state it. A call's argument list is a sequence the declaration can say.
The test suite holds the declaration against ir.py: each instruction declared, each declared class the one in ir.py, each field holding a value given a role, at most one definition per instruction, a label distinguished from a value, and all backends handling all instructions.
The frontend asks the same library where a Python name binds¶
The same argument applies to Python's grammar. Three sites enumerated binding positions by hand, and enumerating them is what goes wrong:
- Definite assignment walked a statement to find what it binds, reaching through the
withitem, the import alias, the handler and the pattern, and stopping at a scope boundary. It asks the library now, checked identical to the walk it replaced over the standard library's statements. - The pygo capture check was wrong. A by-value capture is refused when the enclosing function rebinds the name after the
def. The walk skipped a nesteddefwhole to avoid descending into its locals. That also skipped the name thedefbinds here, so adefrebinding a captured name was approved. Every other form was refused, which is what made it hard to see. - The reservation of user-visible names, so a generated temporary cannot take one, collected stored names, parameters and handler names. Its comment argued the case for the third:
except E as ebinds without a name node. An import alias, amatchcapture and a nesteddefbind the same way and were missing. Derived, it reserves more names and loses none.
The suite also computes what nothing computed before: the union of the checker's refusals and the frontend's lowerings, measured against Python's declared grammar. A construct the checker stopped refusing and the frontend never learned to lower used to fall through to whatever the lowering does with an unknown node.
What this cost, and what it does not touch¶
It is a dependency. postpile builds on abilian-astero, whose notation now states the IR's shape. In exchange, ir.py stays the single source of the fields and the sorts; only the roles are ours to keep right, and tests hold them against ir.py.
The frontend's lowering is untouched. The expression and statement lowerings dispatch on many arms. They are folds: they build loops, temporaries and labels. A fold is not a declaration, and rewriting one as a table makes it longer. The same is true of the shape tests scattered through the type checker and the frontend, which ask about a value, not about the grammar.
The two operand mechanisms still disagree on one production, left as it is. The verifier's uses exclude a declaring assignment's target; QBE's reflective operand walk includes every value-typed field and reports it. Benign, because that walk feeds one loop-unrolling check of the form "skip if anything else uses the counter", so over-reporting refuses an optimization and cannot miscompile. It is the def-versus-use distinction the roles exist for. It becomes a real disagreement the day the walk acquires a second consumer.
The reservation change fixes nothing observable today. Nested def names are mangled, classes and function-body imports are PP900; a match capture named like a temporary compiles to identical IR. It is hardening against a class, and becomes live the day a third match subject or a function-body import is lowered.