| @@ -16,14 +16,15 @@ This ADR settles the passes, their order and what may be fused; the sub-issues # |
| 16 | 16 | ## Decision |
| 17 | 17 | |
| 18 | 18 | ### Pipeline |
| 19 | | −A pass pipeline `louise_core::opt` on Core, after lowering and — natively — after evidence specialisation, before Perceus: |
| 19 | +A pass pipeline `louise_core::opt` on Core, after lowering, before Perceus: |
| 20 | 20 | |
| 21 | 21 | ``` |
| 22 | | −lower → [native: evidence] → simplify ⇄ specialise ⇄ fuse → cpr → trmc → [native: perceus] |
| 22 | +lower → evidence → simplify ⇄ fuse ⇄ specialise → cpr → trmc → [native: perceus] |
| 23 | 23 | ``` |
| 24 | 24 | |
| 25 | +- **Evidence specialisation moves into the shared pipeline.** It ran natively only; both back ends run it now, first in the pipeline (its copies are ordinary functions to the later passes). Two reasons: the interpreter gets the devirtualised direct calls too, and fusion's purity gate (below) must see the same code in both back ends — a dictionary call that is opaque before evidence is an `Int` addition after it, and a pipeline that fused natively but not in the interpreter would break the "same optimised Core" rule that keeps panic imprecision invisible to the differential tests. `LOUISE_NO_SPECIALISE` still disables evidence alone; `LOUISE_NO_OPT` disables the whole pipeline including it. |
| 25 | 26 | - **TRMC moves out of lowering** to after optimisation (ADR 0039's "at the end of lowering" is amended; it gains a note in the PR that lands the move). Specialisation and fusion recognise the pristine recursion of `map`, `filter` and `fold`; after TRMC those functions are context loops. TRMC still runs before Perceus, and still applies to what optimisation leaves — an unfused effectful `map`, say. Running before TRMC and Perceus also means the optimiser's grammar has no `CtxExtend`/`CtxApply`/`Dup`/`Drop`/`Release`/`Reuse` nodes: those passes' invariants are structurally unaffected. |
| 26 | | −- **Both back ends run it.** `opt` runs on the functions reachable from the entries (as evidence does natively, issue #114), for the interpreter too: both back ends run the same optimised Core, so `tests/run` oracles every pass, and the interpreter gets faster as well (issue #196). |
| 27 | +- **Both back ends run it.** `opt` runs on the functions reachable from the entries (issue #114), for the interpreter too: both back ends run the same optimised Core, so `tests/run` oracles every pass, and the interpreter gets faster as well (issue #196). |
| 27 | 28 | - **Budgets.** The fixed point of simplify/specialise/fuse stops when a function stops changing or grows past a node budget; specialisation copies are bounded in total, as evidence's are. `scripts/bench-compile.sh` tracks the compile-time cost (issue #61). |
| 28 | 29 | - **Escape hatches and reporting.** `LOUISE_NO_OPT` disables the whole pipeline (alongside `LOUISE_NO_SPECIALISE` and `LOUISE_NO_TRMC`). `louise core` prints the optimised Core (`--no-opt` prints it as lowered); with `-v` it lists on stderr each rewrite that fired, with the rule's name and the span it fired at (issue #198's reporting question). |
| 29 | 30 | |
| @@ -43,13 +44,13 @@ A direct call of a recursive higher-order function whose closure argument is a k |
| 43 | 44 | ### Fusion, and purity (issue #198) |
| 44 | 45 | Rules on `std/list` pipeline shapes, fired only when every function argument of the stages involved is pure and the intermediate list is used once: |
| 45 | 46 | |
| 46 | | −- `map(map(xs, f), g)` → `map(xs, fn(x) { g(f(x)) })` |
| 47 | +- `map(map(xs, f), g)` → `map(xs, fn(x) { g(f(x)) })`; `filter(map(xs, f), p)` → `filter(xs, fn(x) { p(f(x)) })` |
| 47 | 48 | - `fold(map(xs, f), z, g)` → `fold(xs, z, fn(a, x) { g(a, f(x)) })` |
| 48 | 49 | - `fold(filter(xs, p), z, g)` → `fold(xs, z, fn(a, x) { if p(x) { g(a, x) } else { a } })` |
| 49 | 50 | - `fold(range(a, b), z, g)` → a counting loop |
| 50 | 51 | - `length(map(xs, f))` → `length(xs)`, and `each(map(xs, f), g)` likewise |
| 51 | 52 | |
| 52 | | −Not fused: an effectful stage, `try_map`, `flat_map`, `zip`, or an intermediate used twice. |
| 53 | +Not fused: an effectful stage, `try_map`, `flat_map`, `zip`, or an intermediate used twice. A `perform`, `handle`, `region` or `defer` anywhere in a stage's function makes it effectful for this gate (a local `handle` that masks its own effect would do — the purity analysis is deliberately intraprocedural there, and can be revisited if real pipelines need it). |
| 53 | 54 | |
| 54 | 55 | - **Purity is computed on Core**, not threaded from the type checker: Core is untyped, and after inlining the body is the truth. A function is pure when it performs no effect a `handle` of its own doesn't mask (the clauses recursively pure), opens no region, defers nothing, calls directly only pure functions and known pure closures, and calls externs only from the allocation-free list (`louise_perceus::fip::ALLOCATION_FREE`) plus the boxing float arithmetic — allocation is not observable (reference counting has no hooks), so it is not an effect. A greatest fixed point, so recursion between pure functions is pure. |
| 55 | 56 | - **Panics and divergence are imprecise.** Optimisation may reorder them, replace one with another, or skip one: `length(map(xs, f))` never runs `f`. A run that finishes normally computes the same value as before. That matches errors-as-values (ADR 0024 — a program that relies on *which* panic fires is already wrong) and GHC's imprecise exceptions; and since both back ends run the same optimised Core, they cannot disagree with each other. |