Skip to content

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

Closed
nicolas-abril wants to merge 2 commits into
bendlang:mainfrom
nicolas-abril:opt/share-checked-ops
Closed

nicolas-abril wants to merge 2 commits into
bendlang:mainfrom
nicolas-abril:opt/share-checked-ops

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

comp.ts is 67,545 ttok (main 65,995: +1,550); the cap is set aside for now. One rule extracted from #2221 (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 #2175'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. #2221 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 #2221'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 #2175 and #2180). 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.

🤖 Generated with Claude Code

nicolas-abril and others added 2 commits November 22, 2026 15:39
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Moved to nicolas-abril#28, which keeps it saved off the main queue until it can be accepted.

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