Repository navigation
A shared array resolves its redirect once: a spin takes each box word's storage - #999
nicolas-abril wants to merge 1 commit into
Conversation
…'s storage With any Array shared (BLK_SHR), a handle is a redirect and every access loads its cell to find the storage. Clang cannot hoist that load out of a loop: array cells are written through u32a, which may_alias, so a cell store may be the redirect word. A spin now takes, beside each box word, the storage its caller resolved (q), and keeps the handle it came with (h); an access through a handle equal to one reads its q, and clang folds the test wherever the handle is visibly carried, so a loop reads the redirect once instead of per access. A handle that matches none, or a segment that is not a spin, reads it as before. The scaled stencil3d (2^21 cells, 128 leaves, 160 passes) runs 530 -> 445 ms on one thread, 119 -> 101 ms on ten, and 7.18 -> 1.31 s on Metal, on an M4. A handle that is a spin's own word is tested against that word alone: the DP rows of editdist, which swap every row, cost 2.3% otherwise. With no Array shared the words take nothing, so every bench in bench/runtime emits the C it emitted before, byte for byte. comp.ts 64941 -> 65174 ttok, over its 65000 cap. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
I only found this draft after opening #1084, which overlaps it. Sorry for the noise. #1084 has two parts, and only the first is new relative to this PR:
A data point that matters for whichever version lands: on its own, the atomic read makes unforked histogram (bend-parallel, 2^20–2^22 keys, b=0) 38–50% slower on this machine (Ryzen 5800XT, WSL2, 2.0.29). A hoist wins most of it back; with #1084's it ends at +14–18%, and sort 2^22 b=0 c1 goes 30 → 17 ms. So the race fix and a hoist probably want to land together. I'm happy to fold the atomic read into this PR, or rebase it onto this branch as a separate commit, and drop #1084's hoist in favour of yours. Tell me which you prefer. I can also run bend-parallel's sort/histogram/scan drivers against this branch on x86, and there's a regression test in #1084 you're welcome to take: |
|
Superseded by #1155, which carries this hoist ported onto current main, together with the race fix. |
…c on the device, once per spin (#1155) blk_loc reads a fork's redirect word through w64_load: one relaxed 64-bit atomic on the host (the same load as a plain read) and a plain read on Metal and CUDA, where rfc_view's atomic halves cost the shared-array path 4.5x; the target bits never change, so no read, torn or not, misses them. A spin takes, beside each box word, the storage its caller resolved, and a handle equal to the one it entered with reads that instead of the redirect (#999). Tests: #997's race reproducer and #1084's spin_array_hold. Fixes #975; supersedes #997, #999 and #1084. Co-authored-by: uta080522 <[email protected]> Co-authored-by: Matheus Costa <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Draft: this needs a decision on
bend2/comp.ts's ttok cap before it can land (see Size below).What this changes
With any Array shared,
BLK_SHRis on and a handle is a redirect:blk_locreads the handle's cell to find the storage. Clang cannot hoist that load out of a loop, because array cells are written throughu32a, which ismay_alias, so a cell store may be the redirect word. It does already merge the reads within one iteration, and it unswitches the rfc test when a handle is unshared at runtime, so what is left is one load per handle per iteration.A spin now takes, beside each box word, the storage its caller resolved (
q), and keeps the handle it was entered with (h):Once the spins are inlined, a handle a loop carries is visibly the same value as its
h, so clang folds the test and the loop reads the redirect once instead of per access. A handle that matches none reads it as before, so a loop that swaps its arrays, or hands over one it built itself, stays correct. Segments that are not spins are untouched.Three details each paid for themselves in a measurement:
blk_locis then a mask with no load, and the extra values only cost registers: 2.3% on editdist, 0.9% on bfs.Measurements
Apple M4, one thread unless noted,
cc -std=c11 -O3.A scaled
tests/run/stencil3d.bend(2^21 cells, 128 leaves, 160 passes: the GPGPU pattern, forked handles to one block, 7 gets and 1 set per cell):The kernel loop keeps its 7 cell reads and no redirect load. I have no account of the Metal factor beyond the loads being global memory there.
Programs that share one array and run their hot loops over unshared ones: editdist 2.139 → 2.139 s, bfs 4.00 → 3.93 s.
Every bench in
bench/runtimeemits the C it emitted before, byte for byte, so the suite is unchanged by construction.Tests
tests/built and run on the C lane locally: 617 pass, the same set as main, and the same 9tests/ioandtests/run/nat_overflowfailures on both.array_fork,array_fork_unshare,stencil3d,array_atomic_fadd,brw_audit2_array_field,brw_audit2_array_match) pass on Metal.Size
comp.ts64941 → 65174 ttok, over the 65000 cap ingates/repo.ts, sobun gates/repo.tsfails at 45/46. main sits 59 ttok under the cap. Every piece of the change was removed in turn and each one cost time, so the choice is to raise the cap by ~200 or to find that much elsewhere in the file.One sharp edge
qis trusted when a handle is bitwise equal toh. A redirect's storage is written only byrfc_wrap, when the redirect is made, so it never moves while a handle holds it. The exception would be a spin that drops the last reference to its entry handle and then shares a new array whose redirect reuses that freed cell with the same bits. Safe Bend never copies an Array, so this needs@unsafeand a fork-free def.🤖 Generated with Claude Code