Skip to content

mir: const-fold Int.div/Int.mod with a constant divisor to bare division (#434 static half)#435

Merged
jasisz merged 1 commit into
mainfrom
feat-int-div-const-fold
Jun 6, 2026
Merged

mir: const-fold Int.div/Int.mod with a constant divisor to bare division (#434 static half)#435
jasisz merged 1 commit into
mainfrom
feat-int-div-const-fold

Conversation

@jasisz
Copy link
Copy Markdown
Owner

@jasisz jasisz commented Jun 6, 2026

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.mod provably can't fail (except i64::MIN / -1), so the MIR optimizer folds the Result round-trip away — the explicit Result-returning function disappears in the binary when it can't fail.

  • Fold A (const_fold): Int.div(a, k) for literal k ∉ {0, -1}Result.Ok(<unchecked Euclidean div>); k == 0Result.Err("division by zero"); k == -1 left to the runtime path (can overflow). Same for Int.mod. The unchecked Euclidean div/mod is a new synthesis-only BuiltinIntrinsic, lowered per backend (VM opcodes, 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 collapses to the payload / default / matching arm (ctor fields bound via let). Any sum type.

So match Int.div(a, 10) { Ok / Err } and Result.withDefault(Int.div(a, 10), d) lower to 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 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.mod error strings now match VM/wasm-gc verbatim.

12 new const_fold unit tests; full VM / wasm-gc / Rust matrix + proof_spec green; fmt + clippy clean.

🤖 Generated with Claude Code

…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>
@jasisz jasisz merged commit 731304b into main Jun 6, 2026
5 checks passed
@jasisz jasisz deleted the feat-int-div-const-fold branch June 6, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant