Skip to content

ADR 0055: The frontend records the loop it built, and the backend stops guessing

Status: Final Type: Architecture Created: 2026-09-01 Amends: 0001 (its loop passes read the record and stop matching a shape), 0002 (the loop shape leaves the byte-stability clause), 0014 (appends IR19, which checks the record) Related-ADRs: 0054 (the same argument, one level down)

Context

QBE has no optimiser, so postpile supplies a pair of passes for it: loop strength reduction and a 4x unroll. Both recognised a counted loop by matching the exact instruction sequence the frontend emits. The step had to be i = i + 1 with a literal 1, the start a literal 0, and the bound read directly, never through a copy.

Nothing declared that contract. It was a match arm in the backend and a docstring calling i = i + 1 "the frontend's canonical loop step", against a frontend free to emit what it liked.

It cost three times.

Twice the passes stopped firing with no test going red. The output stayed correct and merely slow. Both times the symptom was a vague regression in the benchmark, diagnosed by hand-diffing the emitted IL. test_qbe_unroll_fires.py was written after the second one.

The third cost is still visible in _lower_for, which held a pair of comments explaining which harmless-looking change would silently disable the unroll. One of them records that snapshotting a loop bound unconditionally, done for correctness, cost about 10% overall until it was narrowed. The frontend had been contorted to preserve the backend's pattern match.

Decision

BasicBlock.loop holds a CountedLoop: the induction variable, its start, stop and step, the label of the step block, and whether the loop drives the user's variable or a hidden counter. The frontend fills it where _loop_needs_counter already decides, because deciding that is the knowledge the backend was reconstructing.

QBE's two passes read the record. _steps_by_one and _starts_at_zero, which were the coupling, are deleted.

IR19 checks the record against the instructions. A record naming a variable the function does not have, a start, stop or step naming a value nothing defines, or a field holding something that is not a Value, raises. The verifier runs on every compile including release (0014), so a record that drifts from the lowering crashes the build. Without it the declaration would move the same failure one level up.

cse checks what it hands back. verify_module runs in the frontend and never sees a function again; the record is the first thing a backend can break. cse renames values across the whole function, while the record names values from outside any instruction list. That is the third such place, after cross-block uses and terminators, both found only when something failed. for i in range(1, n, 1) numbers both literal 1s together; the record kept naming the definition that was dropped. The pass now rewrites the record and calls verify_loop_record on its output.

The corpus comes from the decision. _loop_needs_counter has two branches that force a hidden counter. test_qbe_unroll_fires.py reads them off the function's source and requires one program per branch. The test's three original sources all sat on the same side of that boolean; on the other side neither pass fires.

Consequences

  • The lowering is free to change. Snapshotting the bound unconditionally, the change the old comment warns about, now leaves both passes firing. Verified by measuring the emitted IL.
  • QBE's geomean against C is unchanged at 0.77x, measured before and after on the cross benchmark, which 0001 names as the gate for this file.
  • 0002's byte-stability clause is narrower. Release IR still may not change arbitrarily; the loop shape specifically has left the contract.
  • A hidden counter still loses both passes. That is stated in _loop_needs_counter's docstring and pinned by a test. Teaching the passes that shape would make the predicate a pure correctness question again, and is left undone.
  • A record is data a backend trusts, so a pass that renames values dangles it without touching an instruction. cse did, from the day this landed. The rule that follows is Prism's: every pass hands back IR that is valid on its own terms. The module check in the frontend covers only what the frontend produced.
  • The check shipped raising IR16, the arithmetic promotion code, and with no test of its own, so every message it could have printed would have named the wrong rule. IR19 is its own code now, and test_docs_links.py::test_the_adr_table_is_the_verifiers_table compares 0014's table against INVARIANTS, which had already drifted by one before this.
  • One authored thing remains. A third reason added to _loop_needs_counter needs a program in the corpus; the test fails until it has one. That is the gate the earlier regressions lacked.