Skip to content

An arm computes a checked Nat operation or a division once, where clang could not merge its copies (from #27) - #28

Draft
nicolas-abril wants to merge 4 commits into
mainfrom
opt/share-checked-ops
Draft

nicolas-abril wants to merge 4 commits into
mainfrom
opt/share-checked-ops

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Owner

On main (6180be5b)

This PR is a port onto main, on main: 4 commits of its own, with no integration history (head 88cd807d).

Port notes

  • All 3 commits applied unchanged; one line wrapped.

Checks

  • Cluster gate (every test, every lane): 1873 / 1873 pass.
  • Locally: no new tsc errors against main, no new comp.ts lines over 80, its tests pass on the C and JS lanes (plus 40 lane runs of existing tests; run/array_slab fails on main too).
  • comp.ts: +1552 tokens (ttok -m gpt-3.5-turbo) over main, which is at 65,989 of the 66,000 cap.

Performance was measured on integration when this change merged there (see the original description or the replaced PRs); it has not been re-measured on main.

Original description (written for integration)

Moved from bendlang#2222 (opened 2026-11-22).

comp.ts is 67,545 ttok (main 65,995: +1,550); the cap is set aside for now. One rule extracted from #27 (the e-graph draft, left open), as a small pass without the e-graph. Measured on one Mac (M4), not on the cluster: the test and perf gates have not been run. Measured on main and merged onto a copy of the integration branch (opt/integration at bae6475 on nicolas-abril/bend).

What it does

An arm of a def (its expression below the lambdas and matches) that computes the same checked Nat operation or division twice computes it once, bound by a let at the arm's head, when clang could not merge the two copies itself. C lane only; a pass on the checked terms in file_book, before emission, like #18's acc_def.

# isqrt (koru): the Newton iterate is computed as an argument and again in the flag
iter(n, y, Nat.div(Nat.add(y, Nat.div(n, y)), 2n),
     Nat.is_lt(Nat.div(Nat.add(y, Nat.div(n, y)), 2n), y))
# compiled as if written
+x = Nat.div(Nat.add(y, Nat.div(n, y)), 2n)
iter(n, y, x, Nat.is_lt(x, y))

The rule

A term is a candidate when its head is Nat.add, Nat.mul, Nat.double (checked: they test the 2^48 - 1 limit), Nat.div, Nat.mod, U32.div or U32.mod, applied to two arguments, and it appears at least twice in the arm (equal by term_same). It is bound once when:

  • clang could not merge the copies: the term contains a checked operation, or a checked operation runs between two of its copies (in the order the arm runs: arguments left to right, each before its call). A checked operation posts an error past the limit (nat_chk calls err_post), and clang does not move or merge across that. Two divisions with nothing checked between them clang already merges;
  • it can move to the arm's head: none of its variables is bound by one of the arm's own lets, and no copy is inside a closure;
  • it is not inside another bound candidate.

digitsum, the case that set the first condition. #27 shared any division read twice. In digitsum the two Nat.div(n, 10n) are adjacent in the emitted C, and clang had already merged them: both builds run one umulh a step. Removing the duplicate in the source only changed the shape of clang's loop, +2.7% instructions on main (removing the second call by hand from main's C, keeping everything else, reproduces it exactly; the spins' order, which also changed in #27's C, is not the cause). In isqrt a checked add runs between the two Nat.div(n, y), clang keeps both, and sharing the division alone already gives the whole gain (164.0M to 128.6M instructions by hand). With the condition, digitsum is not touched (its C is byte-identical to main's) and isqrt is.

Soundness. The copies are equal pure terms, so the arm answers the same. What moves is when the term is computed: once, at the head of the arm, before the arm's other work. A program that stops on the Nat limit can stop at another point of the same run (as with #18 and #21). The bound term is a Nat or a U32 (a word), so no box becomes shared.

Reach

Of the 63 programs measured (17 benches, 22 koru kernels, 17 forked ports, 7 Perceus ports) 16 change their C, the same 16 on main and on the integration branch: exprtree, factorial, isqrt, mutual, powmod, primes and the forked collatz, digitsum, factorial, gcdsum, isqrt, mutual, powmod, primes, textstats, wordfreq. No bench of Bend's suite and no Perceus port changes. Of the tests, 2 change (run/nat_mul_wide, run/nat_div_native) besides the new one. Only isqrt (both ports) is hot.

Results

One M4, one thread, GPU off, cc -O3. Cycles are P-core cycles over 16 placements (least / median / most), 2 rounds.

program base instructions cycles, median (least / most)
koru isqrt main 165.0M to 129.6M (-21.5%) 60.0M (59.4 / 62.2) to 48.7M (48.0 / 52.0): -18.9%, ranges apart
koru isqrt integration 164.9M to 130.1M (-21.1%) 60.2M (58.8 / 61.1) to 48.8M (47.2 / 50.7): -19.0%, ranges apart
koru isqrt, forked port main 169.9M to 135.1M (-20.5%) 64.5M to 51.3M (-20.4%), ranges apart
koru isqrt, forked port integration 170.3M to 135.1M (-20.7%) 64.3M to 51.3M (-20.2%), ranges apart
the other 14 that change both -1.07% to +0.96% (median instructions over the sweep; the largest moves are on kernels of 17M to 75M instructions, mostly start-up, and change sign between the two bases) every median inside the base's range

digitsum (the kernel) is byte-identical to main on both bases; its forked port changes (another arm) and is level: 338.1M to 337.8M instructions on main, cycles inside the range.

Instructions here are medians over the sweep's runs: on this machine a binary's count moves by up to a few percent between sessions (start-up), so single runs do not settle sub-1% questions.

Compile time

The pass itself, timed inside compile_book (3 runs): 14 to 16 ms on app_slash_boss_3d (about 1.6% of its compile_book), 1 to 5 ms on each of the 17 benches. End-to-end compile_book against main over the 17 benches and app_slash_boss_3d: +3.2% (least of 3; per-bench numbers move ±30% with the machine's load, both ways).

Checks

Against main:

  • Every test whose C changes (run/nat_mul_wide, run/nat_div_native, run/share_checked_ops) and every one of the 16 programs above: the C lane prints the same as main, and the memory check (poisoned frees, live heap words at exit) is the same at 1 and 8 threads, 0 live words everywhere (the new test ends on the Nat limit, so it prints no count, on main too). All other tests emit byte-identical C.
  • JS: the pass is C only; the changed tests' JS is byte-identical. Metal: no test or program with ! calls changes. tsc: the same errors as main.
  • A new test, tests/run/share_checked_ops.bend, its arguments read at run time from IO.args (literal arguments are evaluated by the compiler and would hide the pass): the isqrt shape, two divisions with nothing checked between (left alone), copies naming an arm's let (left alone), copies inside a closure (left alone), a shared Nat past 2^32, U32 divisions with a checked add between, and a shared product past the Nat limit (stops on every lane).
  • Wrong versions: binding by head instead of the whole term (swap), binding a Nat as a U32 (type), binding a U32 as a Nat (u32): all caught by the new test; the repo's tests that change catch swap and type and miss u32. Two more change nothing observable by construction: a copy that names an arm's let, or that sits in a closure, cannot be found under its binder (the body is opened again with fresh variables), so moving one leaves a dead let the emitter never runs.

Not run: the cluster gates, CUDA, Linux, x86.

Not covered

  • Copies inside a closure, and copies that name one of the arm's lets, stay where they are.
  • Nothing is shared between two arms of a match, or hoisted out of one.
  • The condition is about clang's behaviour (what it merges), measured on these programs, not derived.

Updates

  • 2026-12-01: merged opt/integration (347d475) into this branch: three conflicts, all both-sides additions (the Share section beside closure-elim's, share_def beside acc_def in file_book); the test's Nat-limit run now expects 'exit 1' (the gate's newer convention). Merged into opt/integration at ea35452 (branch 4f2c89a). Against integration: 18 programs change C, none >5% slower (all within ±2.5% cycles; isqrt's gain is already in integration: 35.4M -> 35.6M cycles); 244 programs print expected (parallel ports at 10 threads); 14 tests with changed C pass the poison/live-word check at 1 and 8 threads; gate 0 real failures.

🤖 Generated with Claude Code

@nicolas-abril nicolas-abril changed the title An arm computes a checked Nat operation or a division once, where clang could not merge its copies (from bendlang/bend#2221) An arm computes a checked Nat operation or a division once, where clang could not merge its copies (from #27) Nov 24, 2026
nicolas-abril and others added 4 commits December 1, 2026 22:00
…where clang could not merge its copies

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 0891464)
…time

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 01f3f08)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 4f2c89a)
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