Repository navigation
A loop whose Array indexes have a bound fixed for the loop runs a copy without the index masks - #2137
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
…y without the index masks An Array index wraps, so every access masks it by the block's size, and the mask keeps clang from reading and writing a loop's cells side by side. A spin is now emitted twice when the index of an access to a fixed array has a bound that can be computed before the loop: the copy without the masks runs when every bound is under its array's size, the copy as before otherwise. The bound is an interval over the operators of U32 and Nat, a Nat countdown, a U32 counter and the conversions. histogram: 1.572 s to 1.280 s on one thread. comp.ts is 69,018 ttok. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This was referenced Nov 13, 2026
Closed
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Nov 13, 2026
…o bound stays none A Nat that is fixed for a loop and matched in it (`case 1n+p`) gave p the bound of the whole word, so `a[x - from_nat(p)]` could pass its check one short and write past the array: ix_word's `same` branch ignored the successors the match took off. The branch is bendlang#2137's; the word-by-word roles of the commit before made it reachable. The successors now come off the bounds of any role. Three more, found reading every place a bound is carried: - A countdown past 2^32 saturates to 2^32 - 1, which stands for no bound; taking a successor off it made it a bound again (also in bendlang#2137). It stays 2^32 - 1. - The same under the countdown floor of the commit before: a counter's steps have no bound when the countdown's entry value has none. - An operator was known by its name alone, so a file's own `U32.is_far` had the bound of a comparison (also in bendlang#2137). An operator is now Base's def of that name. tests/run/array_index_carry.bend runs each shape in range and out of it; bendlang#2137 prints two of its 29 lines wrong and the commit before four. The benches' instruction counts are unchanged. comp.ts is 70,235 ttok. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ator is Base's def Two bounds that let an index out of range into the copy without masks, so it wrote outside the array where main wraps: - A countdown past 2^32 saturates to 2^32 - 1, which stands for no bound; taking a successor off it made it a bound again. With n = 2^32 + 5 the index (n - 1) - 4294967290 is 10 and the bound said 4. It stays 2^32 - 1. - An operator was known by its name alone, so a file's own `U32.is_far` had a comparison's bound of [0, 1]. An operator is now Base's def of that name. tests/run/array_index_bound.bend gets both shapes, in range and out of it, with counts the compiler cannot see; the commit before prints two of the five new lines wrong. bfs and terrain change only in the bound temporaries (same instruction counts); the other 15 benches emit the same C. comp.ts is 69,093 ttok. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This was referenced Nov 23, 2026
Closed
Collaborator
Author
|
Moved to nicolas-abril#9, 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 69,093 ttok, 3,093 over the 66,000 cap, sorepo-gatefails until that is settled. Measured on one Mac (M4), not on the cluster: thetestandperfgates have not been run. Independent of #2135.What it does
An Array index wraps, so every access masks it by the block's size (
blk_at). The mask keeps clang from reading and writing a loop's cells side by side: inhistogram's key fill it vectorises the hash and then stores the four keys one at a time.A spin (a flat def that calls itself in tail position) is now emitted twice when the index of an access has a bound that is fixed for the loop:
!DEVICE, so the device compiles the masked copy alone.The bound is an interval
[lo, hi], built bottom-up over the index's term and emitted as C over the spin's entry arguments, in 64 bits:[0, n0], its predecessor[0, n0 - 1]; a Nat past2^32 - 1has no bound, and neither has its predecessor); a U32 the self calls step by a constant (a counter,[j0, j0 + c * n0]); alet(its value's interval); anything else[0, 2^32 - 1].U32.add sub mul inc shl shr shln shrn and or xor div mod min max,Nat.add sub mul double min max,U32.to_nat,U32.from_nat, and the comparisons ([0, 1]). These are Base's defs: a file's own def with one of those names gives the whole range. A sum, product or shift that may pass2^32 - 1may wrap, so its interval is the whole range; a difference keeps its interval only when it cannot go under zero.Array.get,Array.setorArray.swap.So
a[j],a[i * cols + j],a[top - U32.from_nat(p)],a[x & 7],a[U32.min(x, k)]anda[x % m]all get a check; an index read from memory and used as it is does not.Results
Wall time, one M4, GPU off, means of 7 runs.
histogrambfseditdistterrainThe other 13
bench/runtimeprograms compile to byte-identical C.histogramgoes from 44.4G to 32.9G instructions; its C twin runs 28.0G.With every mask removed by hand in the emitted C,
histogramreaches 1.28x andbfs1.11x, so this collects most of what the masks cost there. What is left sits in accesses inside helper defs the loop calls (bump.at), which are not emitted in the loop.editdistshowed 1.28x with the masks removed by hand, and this change does not move it: its masked accesses are in helper defs. (An earlier version of this text put that down to clang's inlining. It is not: the inlining is the same either way. With the masks, clang reloadscur[j]each cell where it otherwise keeps it in a register, and the cycles on a P-core also moved with code placement. #2149 covers those accesses.)Compile time: emitting
histogram's C takes 101.6 ms against 100.1 ms, anddemos/app_slash_boss_3d650 ms against 638 ms. The C grows 5% forhistogramand 0.3% for the demo.Checks
All local, against main
9162bfe9:#|lines.-DBEND_POISONand a count of live heap words at exit: the same output and the same count as main on every one.tests/run/array_index_bound.bendruns the shapes above both in range and wrapping (twice around the array, under zero, a counter past2^32, clamps that fit and that do not, a countdown past2^32, a file's ownU32.is_far). It prints main's output on the C lane, the JS lane andbend file.bend.repo.tsfails only on the ttok cap.Fixed after opening (
debfcdc6): two bounds let an out-of-range index into the copy without masks, which then wrote outside the array where main wraps. A countdown past2^32saturates to2^32 - 1(no bound), and taking a successor off it made it a bound again; and an operator was known by its name alone, so a file's ownU32.is_fargot a comparison's[0, 1]. The test has both shapes now, with counts the compiler cannot see; the first commit prints two of its five new lines wrong. Rerun on the fix: the native lane (883 pass here and on the first commit, no status differs), and the memory check on the test (same as main).bfsandterrainchange only in their bound temporaries, with the same instruction counts; the other 15 benches emit the same C. Not rerun: the JS lane and the memory check on the other 882.Not covered
tscwas not run.Cost
+3,131 ttok. Getting under the cap needs that many tokens cut elsewhere in
comp.ts, or a raise.🤖 Generated with Claude Code