mir: const-fold Int.div/Int.mod with a constant divisor to bare division (#434 static half)#435
Merged
Merged
Conversation
…ion (#434 static half) When the divisor is a literal, `Int.div`/`Int.mod` provably can't fail (except `i64::MIN / -1`), so the Result round-trip is pure overhead. The MIR optimizer now folds it away — completing #408's "partial ops are functions": the explicit Result-returning function disappears in the binary when it can't fail. - **Fold A** (`const_fold`): `Int.div(a, k)` for a literal `k ∉ {0, -1}` → `Result.Ok(<unchecked Euclidean div>)`; `k == 0` → `Result.Err("division by zero")`; `k == -1` left to the runtime path (it can overflow). Same for `Int.mod` (`k ≠ 0`). The unchecked Euclidean div/mod is a new synthesis-only `BuiltinIntrinsic` (`MirCallee::Intrinsic`, like the deforestation `__buf_*` ops), lowered per backend to `div_euclid`/`rem_euclid` (VM opcodes 0x94/0x95, the wasm-gc helpers, Rust `div_euclid`, Lean/Dafny `/`/`%`). - **Fold B** (`const_fold`, general): `withDefault` / `match` over a *literal* `Result.Ok/Err` or `Option.Some/None` constructor collapses to the payload / default / matching arm (binding ctor fields via `let`). Works for any sum type, not just Result. So `match Int.div(a, 10) { Ok / Err }` and `Result.withDefault(Int.div(a, 10), d)` lower to a plain Euclidean division on VM, wasm-gc, and Rust (verified byte-identical incl. negative dividends; the `-1` overflow edge stays a runtime `Err`). The fused `IntDivOrDefaultLiteral` leaf-op is left intact (it now only sees non-const divisors) — retiring it + moving the runtime fusion to MIR is the 0.25 follow-on (#434). Also: the Rust backend's boxed `Int.div`/`Int.mod` error strings now match the VM/wasm-gc verbatim ("division by zero" / "division overflow") — they were a pre-existing cosmetic cross-backend divergence. 12 const_fold unit tests; full VM/wasm-gc/Rust matrix + proof_spec green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The static half of #434, for 0.24 "Divide". Completes #408's "partial ops are functions": when the divisor is a literal,
Int.div/Int.modprovably can't fail (excepti64::MIN / -1), so the MIR optimizer folds theResultround-trip away — the explicit Result-returning function disappears in the binary when it can't fail.const_fold):Int.div(a, k)for literalk ∉ {0, -1}→Result.Ok(<unchecked Euclidean div>);k == 0→Result.Err("division by zero");k == -1left to the runtime path (can overflow). Same forInt.mod. The unchecked Euclidean div/mod is a new synthesis-onlyBuiltinIntrinsic, lowered per backend (VM opcodes, wasm-gc helpers, Rustdiv_euclid, Lean/Dafny//%).const_fold, general):withDefault/matchover a literalResult.Ok/ErrorOption.Some/Nonecollapses to the payload / default / matching arm (ctor fields bound vialet). Any sum type.So
match Int.div(a, 10) { Ok / Err }andResult.withDefault(Int.div(a, 10), d)lower to plain Euclidean division on VM, wasm-gc, and Rust — verified byte-identical incl. negative dividends; the-1overflow edge stays a runtimeErr. The fusedIntDivOrDefaultLiteralleaf-op is untouched (now only sees non-const divisors); retiring it + moving runtime fusion to MIR is the 0.25 follow-on (#434).Also fixes a pre-existing cosmetic divergence: the Rust backend's boxed
Int.div/Int.moderror strings now match VM/wasm-gc verbatim.12 new
const_foldunit tests; full VM / wasm-gc / Rust matrix +proof_specgreen; fmt + clippy clean.🤖 Generated with Claude Code