Skip to content

A loop whose Array indexes have a bound fixed for the loop runs a copy without the index masks - #9

Closed
nicolas-abril wants to merge 2 commits into
mainfrom
opt/index-bounds
Closed

nicolas-abril wants to merge 2 commits into
mainfrom
opt/index-bounds

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Owner

Moved from bendlang#2137 (opened 2026-11-12).

comp.ts is 69,093 ttok, 3,093 over the 66,000 cap, so repo-gate fails until that is settled. Measured on one Mac (M4), not on the cluster: the test and perf gates have not been run. Independent of #8.

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: in histogram'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:

  • The check: before the loop, each such bound is compared with its array's size, once.
  • The copy without the masks runs when every check holds; the copy as before runs otherwise, so a loop that does wrap behaves as it did.
  • Host only: the check starts with !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:

  • Leaves: a constant; a parameter every self call passes back as it is (its entry value); a Nat every self call passes a predecessor of (a countdown, [0, n0], its predecessor [0, n0 - 1]; a Nat past 2^32 - 1 has no bound, and neither has its predecessor); a U32 the self calls step by a constant (a counter, [j0, j0 + c * n0]); a let (its value's interval); anything else [0, 2^32 - 1].
  • Operators: 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 pass 2^32 - 1 may wrap, so its interval is the whole range; a difference keeps its interval only when it cannot go under zero.
  • The array must be a parameter the self calls pass back as it is, directly or through Array.get, Array.set or Array.swap.

So a[j], a[i * cols + j], a[top - U32.from_nat(p)], a[x & 7], a[U32.min(x, k)] and a[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.

bench 1 thread, main this 8 threads, main this
histogram 1.572 s 1.280 s 1.23x 350 ms 289 ms 1.21x
bfs 3.937 s 3.750 s 1.05x 620 ms 568 ms 1.09x
editdist 2.636 s 2.639 s 1.00x 492 ms 491 ms 1.00x
terrain 2.186 s 2.175 s 1.00x 379 ms 377 ms 1.01x

The other 13 bench/runtime programs compile to byte-identical C. histogram goes from 44.4G to 32.9G instructions; its C twin runs 28.0G.

With every mask removed by hand in the emitted C, histogram reaches 1.28x and bfs 1.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. editdist showed 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 reloads cur[j] each cell where it otherwise keeps it in a register, and the cycles on a P-core also moved with code placement. #11 covers those accesses.)

Compile time: emitting histogram's C takes 101.6 ms against 100.1 ms, and demos/app_slash_boss_3d 650 ms against 638 ms. The C grows 5% for histogram and 0.3% for the demo.

Checks

All local, against main 9162bfe9:

  • Native lane: every test that builds and passes on main (882) passes here, the GPU off.
  • JS lane: the same 882 print their #| lines.
  • Memory: each of the 882 built with -DBEND_POISON and a count of live heap words at exit: the same output and the same count as main on every one.
  • New test: tests/run/array_index_bound.bend runs the shapes above both in range and wrapping (twice around the array, under zero, a counter past 2^32, clamps that fit and that do not, a countdown past 2^32, a file's own U32.is_far). It prints main's output on the C lane, the JS lane and bend file.bend.
  • repo.ts fails 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 past 2^32 saturates to 2^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 own U32.is_far got 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). bfs and terrain change 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

  • Cluster gates, Metal and CUDA have not run.
  • Accesses in a helper def the loop calls keep their masks.
  • Segments: only flat defs (spins) get the two copies; a def that forks or makes non-tail calls keeps its masks.
  • Calls in an index (a def that is no operator) give the whole range.
  • tsc was 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

nicolas-abril and others added 2 commits November 12, 2026 19:40
…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]>
…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]>
@nicolas-abril

Copy link
Copy Markdown
Owner Author

Ported onto main together with related PRs as #61.

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