Repository navigation
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
Closed
nicolas-abril wants to merge 2 commits into
nicolas-abril wants to merge 2 commits into
Conversation
…where clang could not merge its copies Co-Authored-By: Claude Opus 5.5 <[email protected]>
…time Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
Author
|
Moved to nicolas-abril#28, which keeps it saved off the main queue until it can be accepted. |
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.
comp.tsis 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: thetestandperfgates have not been run. Measured on main and merged onto a copy of the integration branch (opt/integrationat 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
letat the arm's head, when clang could not merge the two copies itself. C lane only; a pass on the checked terms infile_book, before emission, like #2175'sacc_def.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.divorU32.mod, applied to two arguments, and it appears at least twice in the arm (equal byterm_same). It is bound once when:nat_chkcallserr_post), and clang does not move or merge across that. Two divisions with nothing checked between them clang already merges;lets, and no copy is inside a closure;digitsum, the case that set the first condition. #2221 shared any division read twice. Indigitsumthe twoNat.div(n, 10n)are adjacent in the emitted C, and clang had already merged them: both builds run oneumulha 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). Inisqrta checked add runs between the twoNat.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,digitsumis not touched (its C is byte-identical to main's) andisqrtis.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
Nator aU32(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,primesand the forkedcollatz,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. Onlyisqrt(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.isqrtisqrtisqrt, forked portisqrt, forked portdigitsum(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 onapp_slash_boss_3d(about 1.6% of itscompile_book), 1 to 5 ms on each of the 17 benches. End-to-endcompile_bookagainst main over the 17 benches andapp_slash_boss_3d: +3.2% (least of 3; per-bench numbers move ±30% with the machine's load, both ways).Checks
Against main:
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.!calls changes.tsc: the same errors as main.tests/run/share_checked_ops.bend, its arguments read at run time fromIO.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'slet(left alone), copies inside a closure (left alone), a sharedNatpast 2^32,U32divisions with a checked add between, and a shared product past the Nat limit (stops on every lane).swap), binding aNatas aU32(type), binding aU32as aNat(u32): all caught by the new test; the repo's tests that change catchswapandtypeand missu32. Two more change nothing observable by construction: a copy that names an arm'slet, 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 deadletthe emitter never runs.Not run: the cluster gates, CUDA, Linux, x86.
Not covered
lets, stay where they are.🤖 Generated with Claude Code