Skip to content

Nat bounds: drop 2^48 limit and zero-divisor tests that static bounds rule out - #48

Merged
nicolas-abril merged 10 commits into
opt/integrationfrom
opt/nat-tests
Nov 28, 2026
Merged

nicolas-abril merged 10 commits into
opt/integrationfrom
opt/nat-tests

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Merged into integration at 283a904 (opt/nat-tests a71fa62, after merging integration 4561e97; lane statuses equal to integration's plus the 7 new tests: nat_range_edge passes, the other 6 print their expected stdout, stderr and exit 1, the kind the lane counts as FAIL; 242 of 244 benchmark programs print expected at 1 thread and the 98 parallel ports at 10, the same two as integration exceed the memory caps).

Static bounds for Nat values let the C lane drop the 2^48-1 limit test on adds, muls and successors that cannot reach the limit, and the zero-divisor test of Nat.div/Nat.mod by a variable proven positive. comp.ts only (section "Nv"), plus seven tests.

How

nv_book runs once after file_book and computes, for every reachable def:

  • the bounds of each parameter: the join of what all of the def's calls pass. A def named anywhere other than as the head of a full call (a closure, a partial application, FL.fids, main) takes anything;
  • the bounds of its result.

The rules:

  • Base ops. The rules cover add, mul, sub, min, max, div and mod, where a division by zero gives 0 and a remainder by zero gives the dividend. They also cover U32.to_nat and literals.
  • Matches. A match on a Nat bounds the scrutinee in each arm and gives the predecessor's bounds.
  • Loops. Some loops have a parameter that every self-call steps down: a predecessor, or a positive value divided by a constant ≥ 2. That parameter bounds the number of turns. A parameter that every self-call passes as it is, or grows by at most B, is then bounded by its entry bound + turns × B. This is what bounds digitsum's and isqrt's sums.
  • Bool parameters. A Bool parameter that every call passes as a comparison of two other parameters (or of one and a constant) bounds them in the arms of a match on it. Callers can pass this on through their own parameters. This is digitsum's small.
  • Same variable on both operands. An add whose operands read the same variable is bounded separately at that variable's least value and above it. This gives y + n / y >= 2, so isqrt's y >= 1.
  • Widening. A stepped-down parameter's low bound goes straight to 0. Other bounds widen to the end of the range after 64 walks.

The emitter carries a word's bounds on its Val (nv). These come from the native's parameters, from literals, ops and call results, and from match fields.

Soundness. Every Nat is at most 2^48-1, and a test is dropped only when the bounds of its operands keep the result there. So a wrong bound would wrap silently. The tests below each target one way the analysis could trust a bound it should not.

Results

On opt/integration 5a4a9f5. 49 of the 244 programs' C changes. Instructions are from one run; cycles are the median of 5 runs at 1 thread.

program instructions main → branch Δ cycles main → branch Δ
osprey/isqrt/bend-par/isqrt.bend 169.7M → 96.6M -43.1% 62.7M → 35.4M -43.6%
osprey/isqrt/bend/isqrt.bend 164.8M → 92.6M -43.8% 58.5M → 35.1M -40.0%
respelled/isqrt/main.bend 129.7M → 92.6M -28.6% 47.1M → 35.1M -25.5%
osprey/digitsum/bend/digitsum.bend 134.5M → 108.7M -19.2% 25.9M → 20.0M -22.7%
osprey/digitsum/bend-par/digitsum.bend 138.5M → 110.7M -20.1% 27.2M → 21.5M -20.7%
osprey/nestedloop/bend/nestedloop.bend 310.5M → 232.4M -25.2% 164.8M → 146.3M -11.2%
osprey/collatz/bend/collatz.bend 131.8M → 142.5M +8.1% 53.9M → 49.8M -7.5%
osprey/fib/bend-par/fib.bend 582.8M → 553.0M -5.1% 106.9M → 103.0M -3.6%
osprey/wordfreq/bend-par/wordfreq.bend 18.3M → 17.4M -4.5% 4.8M → 4.6M -3.5%
osprey/textstats/bend/textstats.bend 44.5M → 44.4M -0.1% 9.4M → 9.1M -2.8%
osprey/fib/bend/fib.bend 306.4M → 276.6M -9.7% 53.3M → 51.9M -2.6%
osprey/mutual/bend-par/mutual.bend 51.2M → 51.1M -0.2% 11.3M → 11.0M -2.5%
osprey/wordfreq/bend/wordfreq.bend 17.6M → 17.6M -0.3% 4.7M → 4.6M -2.4%
respelled/listops/main.bend 14.3M → 14.2M -0.7% 4.4M → 4.3M -2.4%
osprey/nestedloop/bend-par/nestedloop.bend 310.6M → 216.6M -30.3% 164.7M → 162.0M -1.6%
osprey/textstats/bend-par/textstats.bend 46.9M → 45.5M -3.0% 9.6M → 9.5M -1.6%
osprey/mutual/bend/mutual.bend 52.5M → 52.4M -0.2% 11.0M → 10.9M -1.5%
osprey/primes/bend-par/primes.bend 84.9M → 84.9M -0.0% 21.3M → 21.0M -1.3%
parallel-ml/palindrome/main.bend 2543.7M → 2543.6M -0.0% 986.0M → 974.4M -1.2%
parallel-ml/fib/main.bend 8179.5M → 6406.3M -21.7% 1390.9M → 1375.9M -1.1%
clbg/pidigits/main.bend 7064.2M → 7064.4M +0.0% 2477.4M → 2451.9M -1.0%
respelled/powmod/main.bend 227.5M → 191.5M -15.8% 242.6M → 240.7M -0.8%
osprey/exprtree/bend/exprtree.bend 73.6M → 73.6M -0.1% 28.9M → 28.8M -0.5%
kostya/primes/main.bend 8354.7M → 8355.2M +0.0% 2964.4M → 2952.4M -0.4%
osprey/factorial/bend-par/factorial.bend 3613.9M → 3385.0M -6.3% 2191.3M → 2185.2M -0.3%
awfy/deltablue/main.bend (hoist set) 807.9M → 808.1M +0.0% 136.2M → 135.9M -0.2%
closures/palindrome/main.bend 2547.3M → 2546.2M -0.0% 965.5M → 963.8M -0.2%
osprey/gcdsum/bend-par/gcdsum.bend 164.3M → 160.3M -2.4% 306.9M → 306.7M -0.1%
respelled/factorial/main.bend 162.5M → 162.5M -0.0% 214.4M → 214.3M -0.1%
parallel-ml/delaunay-animation/main.bend 20069.9M → 20072.5M +0.0% 4864.2M → 4862.1M -0.0%
kostya/base64/main.bend 27221.0M → 27220.8M -0.0% 2918.2M → 2917.3M -0.0%
clbg/binary-trees/main.bend 862.9M → 863.0M +0.0% 171.6M → 171.5M -0.0%
osprey/factorial/bend/factorial.bend 2825.1M → 2825.1M +0.0% 2167.7M → 2167.6M -0.0%
kostya/matmul/main.bend 9051.3M → 9052.7M +0.0% 6566.1M → 6566.0M -0.0%
osprey/josephus/bend/josephus.bend 112.5M → 112.5M +0.0% 127.0M → 127.0M -0.0%
osprey/gcdsum/bend/gcdsum.bend 160.3M → 158.3M -1.2% 305.0M → 305.0M +0.0%
closures/delaunay-animation/main.bend 20070.4M → 20073.8M +0.0% 4864.2M → 4865.8M +0.0%
parallel-ml/delaunay/main.bend 18396.0M → 18397.4M +0.0% 9072.1M → 9078.8M +0.1%
osprey/listops/bend/listops.bend 14.3M → 14.2M -0.9% 4.1M → 4.1M +0.1%
awfy/havlak/main.bend (hoist set) 1055.2M → 1055.0M -0.0% 170.5M → 170.7M +0.1%
koka-extra/binarytrees-fbip/main.bend (hoist set) 1071.9M → 1071.0M -0.1% 384.4M → 385.0M +0.1%
parallel-ml/ocaml-binarytrees5/main.bend (hoist set) 2049.7M → 2049.6M -0.0% 398.4M → 399.0M +0.1%
osprey/primes/bend/primes.bend 84.7M → 84.6M -0.0% 21.4M → 21.5M +0.2%
osprey/powmod/bend/powmod.bend 4729.5M → 4759.2M +0.6% 3578.9M → 3602.6M +0.7%
osprey/powmod/bend-par/powmod.bend 4730.5M → 4760.1M +0.6% 3580.6M → 3604.6M +0.7%
respelled/exprtree/main.bend 50.4M → 50.4M -0.0% 12.3M → 12.4M +0.7%
koka-extra/splay/main.bend 1299.6M → 1301.0M +0.1% 1359.0M → 1397.0M +2.8%
osprey/collatz/bend-par/collatz.bend 142.8M → 153.4M +7.5% 48.6M → 51.2M +5.4%
respelled/mutual/main.bend 143.9M → 143.6M -0.2% 40.7M → 45.4M +11.7%

45 programs outside the bendlang#2180 hoist set: geomean 0.9530x cycles, 0.9318x instructions. The 4 programs in the hoist set are level. No output is wrong.

Losses over 5% in cycles:

  • osprey/collatz/bend-par +5.4% (16-placement sweep: +3.5% median, +1.8% to +6.2%). The only C change is the two 100 * x muls in its setup, which lose their tests. Clang then lays out the hot loop differently (+7.5% instructions). The sequential port, with the same change, is −7.5% (sweep: −2.9% median).
  • respelled/mutual +11.7% at −0.2% instructions. This is placement: the 16-position sweep is level, median 0.996, spread 0.86 to 1.21.

Also placement, under 5%: koka-extra/splay +2.8% (sweep median 0.982).

Test sites across the 22 Osprey kernels go from 314 to 136. Examples: powmod 69→2, exprtree 84→23, listops 28→6, factorial 10→2, isqrt 8→2, digitsum 6→2. In bend-runtime and polybench, no Nat test sits in a hot loop; their only one is List.length's counter.

Osprey board (hyperfine, ms):

kernel C (clang -O3) Koru Bend before Bend after after / Koru
isqrt 9.69 9.69 15.09 9.00 0.93
digitsum 4.86 5.63 6.60 5.11 0.91
collatz 9.58 12.02 13.72 12.74 1.06
nestedloop 44.70 44.71 41.73 37.08 0.83
fib 16.16 17.07 13.26 13.02 0.76
primes 5.97 5.98 5.50 5.46 0.91
mutual 11.29 11.49 2.90 2.86 0.25
gcdsum 76.96 77.54 77.04 77.11 0.99

In isqrt the zero-divisor test is gone too: n / y with y >= 1. The Newton step runs at the C reference's speed or better.

Checks

  • Native lane: 1129 tests, statuses identical to opt/integration; 111 tests' C changes.
  • Memory: -DBEND_POISON, 0 live words at 1 and 8 threads on those 111 and the new tests.
  • Benchmarks: all 244 print their expected.txt at 1 thread; the parallel ports also at 10 threads.
  • GPU: the bang tests whose C changes give the same output at --gpu on and --gpu off. No bend-runtime program and not respelled/queens changes.
  • New tests, in tests/run/nat_range_*:
    • edge: sums that land exactly on 2^48-1;
    • modzero: a remainder by a divisor in 0..2;
    • rel: a Bool relation broken at one call site;
    • escape: a def passed as a closure;
    • steps: a self-call that does not step the parameter down;
    • mid: an overflow in the middle of a loop that prints as it goes;
    • wrap: 2^40 × 2^40, which wraps to 0 in a u64.
  • Deliberately wrong versions, all caught: a mod rule that ignores a zero divisor; no escape on a partial application; a relation taken from one call site; self-calls that do not step the parameter ignored; a mul checked modulo 2^64.
  • Compile time: +1% to +7% on 7 programs: editdist +5.6%, lexer +1.3%, symreg −1.4%, havlak +4.7%, splay +6.8%, rbtree +3.0%, k-nucleotide +6.1% (median of 3).

comp.ts grows by about 8,400 tokens: roughly 8,620 added, 260 removed.

Not in this PR

Testing an accumulator only at loop exit (and every 2^15 turns, so its word cannot wrap) is on branch opt/nat-tests-sink.

  • Results: it reaches only collatz. The sequential port ran 3.8% fewer cycles; the forked port ran 5.2% more over a 16-placement sweep. Both ran about 30% more instructions, for the turn counter.
  • Hand-edit bound: 12.4 → 11.5 ms.
  • Not done: the 3n+1 test in collatz has no static bound, and stays.

2026-11-30 (97674ad): compile time — nv_book's relation rounds walk only a def whose own relations moved since its last walk (the only state such a walk reads), so the confirming round is mostly skipped. Same C on the 2,129-program corpus.

🤖 Generated with Claude Code

@nicolas-abril
nicolas-abril merged commit 283a904 into opt/integration Nov 28, 2026
nicolas-abril added a commit that referenced this pull request Dec 1, 2026
…33, #34, #36, #39, #44, #61-#68): record their heads

Each is a port onto main of integration's own branch, its content already
here (integration keeps the original commits; the ports add type fixes,
line wraps, moved pieces and #68's book_passes, applied in the previous
commit). Groups: #61 (#9 #11 #15 #32 #41 #42), #62 (#26 #56), #63 (#48 #50
and compile-time), #64 (#19 #20 #45), #67 (#55 #58), #68 (#54); par-defer
(#43) is now part of #34, lazy-startup's parts are in #16 and #34. This
merge records their heads (-s ours: no content change).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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