feat(#240): Z3-verified algebraic mid-end (strength-reduction) + range-premise hook#259
Conversation
…e-premise hook
Tier 1 — unconditional, Z3-verified algebraic/strength-reduction rules added to
the term simplifier (rewrite_pure_impl), each confirmed accepted by loom's Z3
translation validator end-to-end (verify_or_revert does NOT revert):
* (x << k) >>u k -> x & (0xFFFFFFFF >>u k) [i32, 0<k<32] — folds synth's
literal `256*(x-c) >> 8` shape (== (x'<<8)>>u8) to a single mask
* (x << a) << b -> x << (a+b) [i32/i64, a+b<width]
* (x >>u a) >>u b -> x >>u (a+b) [i32/i64, a+b<width]
* (x & c1) & c2 -> x & (c1 & c2) [i32/i64] redundant-mask fusion
All gated on width so the wasm shift-amount-mod-width wrap can't change meaning.
Tier 2 — range-GATED flagship (x<<k)>>u k -> x. Adds a minimal IR premise
representation: OptimizationEnv.value_max (inclusive unsigned upper bound per
expression) + assume_max/fits_below_bit. The env-aware I32ShrU path folds to x
ONLY when a premise proves x < 2^(32-k); with no premise it degrades to the
unconditional Tier-1 mask. The real fact-source (Verus/Rocq value-range facts)
is DEFERRED (#240 part 2 / #231); tests inject the premise directly.
Soundness is discharged by the existing Z3 translation validator (instruction-
level bit-vector model), confirmed by the 4 loom-core pipeline tests. 13 new
tests (9 loom-shared unit + 4 loom-core integration). Full workspace suite green.
Refs: #240
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Automated review for PR #259pulseengine/loom: Verdict: 🔴 Request changes Summary: Adds Z3-verified algebraic mid-end optimizations to the Loom project. Findings: 7 mechanical (rivet) · 0 from local AI model. Findings (7):
Generated by a local AI model and post-validated against a strict JSON contract. Each finding includes the verbatim line being criticised — verify by reading the file at the cited location. Reviewed at |
Refs #240 (part 1 of 2). Extends the term simplifier with instruction-count-reducing rewrites toward beat-LLVM parity. Every rule is Z3-verified by loom's translation validator (the machine-checked proof); a rule the validator rejects reverts, so nothing unproven ships.
Tier 1 — unconditional folds (fire on real code today)
Each confirmed Z3-accepted (the 4 loom-core pipeline tests run full
optimize_modulewith verification on and assert the fold happened + re-encodes):(x<<k) >>u k → x & (0xFFFFFFFF >>u k)(i32, 0<k<32) — collapses synth's256*(ch-1024)>>8shape into a single mask today (i64 analogue already existed from opt: dissolve the seam fully — post-inline SROA/scalar-forwarding eliminates the u64 ABI pack/unpack (+ wasm-local mem2reg) → toward LLVM-LTO parity #219).(x<<a)<<b → x<<(a+b)(i32/i64, a+b<width).(x>>u a)>>u b → x>>u(a+b)(i32/i64, a+b<width).(x&c1)&c2 → x&(c1&c2)(i32/i64, recurses into the existing&-1/&0identities).All width-gated so the wasm shift-mod-width wrap can't change meaning. No existing rule regressed.
Tier 2 — range-premise hook (prototype; gust win gated on #231)
OptimizationEnv.value_max: HashMap<Value,u64>(inclusive unsigned upper bound) +assume_max/fits_below_bit. Empty by default ⇒ byte-identical to plain dataflow with no premises.(x<<k)>>u k → x(drop the mask entirely, LLVM parity) fires only when a premise provesx < 2^(width-k); else degrades to the Tier-1 mask. Z3-verified under the assumption; 3 tests (fires with x<2^24, not without, not under a too-weak 2^25 bound).value_maxfrom machine-checked Verus/Rocq value-range facts is Carry Verus value-range/no-alias facts as IR premises + algebraic mid-end — feed synth's beat-LLVM specialization #240 part 2 / Design: proof-carryingwsc.factssection — loom forwards Z3-discharged invariants to synth #231 (gale/synth fact-format co-design). Not faked; the gust beat-LLVM number does not land until that contract is wired. Co-design note: premises must key on the post-simplificationValuethe gate sees.Proofs
The Z3 translation validator is the machine-checked proof for every rule here. Matching Rocq theorems for
StrengthReduction.vwere drafted but not committed — the repo's Rocq build fails upstream inTermSemantics.v(the known Rocq CI breakage, #169) before reaching new content, so they can't be attested to compile; per the correctness philosophy, unverifiable.vis not shipped. Inline algebraic-soundness notes accompany each rule.Checks
13 new tests (9 loom-shared unit + 4 loom-core pipeline).
cargo test --release --workspacegreen; clippy clean; fmt clean.Part of v1.2.0 (with #257). 🤖 Generated with Claude Code