s19 — Optimizing Tier: Fact-Driven Mid-End
Goal
The optimizing tier exists: a rewrite framework over BIR (e-graph or rule-pipeline per the s03 decision informed by the s01 aegraph dossier) consuming the s18 fact surface, with the first two optimization waves landed, per-pass translation-validation hooks, and determinism/idempotency proven — never touching RC placement.
Implementation Sketch
- Framework per the s03/s01 decision: if aegraph-style — acyclic e-graph over BIR with elaboration; if rule-pipeline — ordered passes with a shared rewrite driver. Either way: passes are (BIR, FactTables, Config) -> BIR pure functions; deterministic; idempotent (pass twice == once, tested); pass-ordering documented in the pipeline module doc (impl-hygiene pass-composition rules apply verbatim).
- RC guardrail (mechanical, not aspirational): the s06 RC-op multiset preservation check runs AFTER EVERY PASS — any pass changing RC op count/placement fails immediately (LEAK:aims-ad-hoc-emission prevention as an executable invariant). Reuse/COW instruction forms count as RC ops.
- Wave 1 (from the s02 priority list, fact-leveraged): redundancy elimination (GVN-class over BIR, alias-precise via uniqueness/borrow facts instead of conservative AA), instruction scheduling within blocks (latency-aware list scheduling), isel-quality rewrites (addressing-mode fusion, compare-branch fusion, strength reduction).
- Wave 2: loop optimizations (LICM with effect-fact purity — may_allocate/may_throw facts replace LLVM’s conservative re-derivation; induction simplification), call-site optimizations using ReturnContract/ParamContract facts (argument-setup elision for borrowed_read_only, freshness-based store forwarding), BIR-level inlining decision record (whether inlining lives here or stays an upstream concern — decided with evidence, not assumed).
- Per-pass translation-validation hooks: each pass can emit a (before, after) pair into the s21 validation harness (sampling in CI); hooks land NOW with the framework.
- Opt-level config: fast tier bypasses the mid-end entirely (O0-equivalent); optimizing tier runs the full roster (release default candidate).
Test Strategy
- Per-pass matrix: pass x program-shape corpus — output parity (tri-exec) + the post-pass RC multiset check + idempotency (run-twice equality) + determinism (run-N byte-equality).
- Semantic pins: per wave-1 pass one before/after pin (FileCheck-style MIR/BIR snapshot proving the optimization FIRES on its target shape); negative pin proving it does NOT fire when a fact forbids it (e.g. GVN blocked by may_share).
- Perf evidence: per-pass measured wins on the benchmark corpus recorded (/calc-verified) — a pass with no measurable win on any corpus program gets a justify-or-park decision.
Work Items
- Mid-end framework (per s03 decision) with pure-pass contract, pass-ordering doc, opt-level config, and the post-every-pass RC-multiset guardrail.
- Per-pass translation-validation hooks wired into the (future s21) harness from day one.
- Wave 1: fact-precise GVN-class redundancy + list scheduling + isel-quality rewrites with fire/blocked pins per pass.
- Wave 2: effect-fact LICM + induction simplification + contract-fact call-site optimizations + inlining decision record.
- Determinism/idempotency proofs over the corpus + per-pass measured-win ledger (justify-or-park for no-win passes).