Repository navigation
A shared Array's redirect is read without a race and without an atomic on the device, once per spin (#975) - #1155
Merged
Merged
Conversation
…c on the device, once per spin (#975) blk_loc reads a fork's redirect word as one relaxed 64-bit atomic on the CPU (the same load as a plain read) and plainly on Metal and CUDA, where the atomic halves of rfc_view 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, a spin that swaps its arrays each turn. 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]>
This was referenced Sep 28, 2026
…and plainly on a device, and rfc_view's comment no longer says the cell is never one word Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This was referenced Sep 28, 2026
Closed
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Oct 2, 2026
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on the device), as bendlang#1155 made blk_loc do: the target bits never change, so a torn read still finds them, and a shared node's fields never change either (span_fade only bumps its children). rfc_view's two atomic halves and its acquire go; ctr_take reads the count itself after copying and acquires only when it is the last holder, before it frees the cell and owns the node. blk_loc is term_peek for a shared block. FID_EXIT's continuation fast path stays: without it, code that applies a closure which may fork retires 28% more instructions at 8 threads. Full local suite on every lane: all pass but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Oct 3, 2026
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on the device), as bendlang#1155 made blk_loc do: the target bits never change, so a torn read still finds them, and a shared node's fields never change either (span_fade only bumps its children). rfc_view's two atomic halves and its acquire go; ctr_take reads the count itself after copying and acquires only when it is the last holder, before it frees the cell and owns the node. blk_loc is term_peek for a shared block. FID_EXIT's continuation fast path stays: without it, code that applies a closure which may fork retires 28% more instructions at 8 threads. Full local suite on every lane: all pass but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Oct 3, 2026
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on the device), as bendlang#1155 made blk_loc do: the target bits never change, so a torn read still finds them, and a shared node's fields never change either (span_fade only bumps its children). rfc_view's two atomic halves and its acquire go; ctr_take reads the count itself after copying and acquires only when it is the last holder, before it frees the cell and owns the node. blk_loc is term_peek for a shared block. FID_EXIT's continuation fast path stays: without it, code that applies a closure which may fork retires 28% more instructions at 8 threads. Full local suite on every lane: all pass but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Oct 3, 2026
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on the device), as bendlang#1155 made blk_loc do: the target bits never change, so a torn read still finds them, and a shared node's fields never change either (span_fade only bumps its children). rfc_view's two atomic halves and its acquire go; ctr_take reads the count itself after copying and acquires only when it is the last holder, before it frees the cell and owns the node. blk_loc is term_peek for a shared block. FID_EXIT's continuation fast path stays: without it, code that applies a closure which may fork retires 28% more instructions at 8 threads. Full local suite on every lane: all pass but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Oct 3, 2026
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on the device), as bendlang#1155 made blk_loc do: the target bits never change, so a torn read still finds them, and a shared node's fields never change either (span_fade only bumps its children). rfc_view's two atomic halves and its acquire go; ctr_take reads the count itself after copying and acquires only when it is the last holder, before it frees the cell and owns the node. blk_loc is term_peek for a shared block. FID_EXIT's continuation fast path stays: without it, code that applies a closure which may fork retires 28% more instructions at 8 threads. Full local suite on every lane: all pass but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Oct 3, 2026
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on the device), as bendlang#1155 made blk_loc do: the target bits never change, so a torn read still finds them, and a shared node's fields never change either (span_fade only bumps its children). rfc_view's two atomic halves and its acquire go; ctr_take reads the count itself after copying and acquires only when it is the last holder, before it frees the cell and owns the node. blk_loc is term_peek for a shared block. FID_EXIT's continuation fast path stays: without it, code that applies a closure which may fork retires 28% more instructions at 8 threads. Full local suite on every lane: all pass but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Oct 5, 2026
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on the device), as bendlang#1155 made blk_loc do: the target bits never change, so a torn read still finds them, and a shared node's fields never change either (span_fade only bumps its children). rfc_view's two atomic halves and its acquire go; ctr_take reads the count itself after copying and acquires only when it is the last holder, before it frees the cell and owns the node. blk_loc is term_peek for a shared block. FID_EXIT's continuation fast path stays: without it, code that applies a closure which may fork retires 28% more instructions at 8 threads. Full local suite on every lane: all pass but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Oct 5, 2026
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on the device), as bendlang#1155 made blk_loc do: the target bits never change, so a torn read still finds them, and a shared node's fields never change either (span_fade only bumps its children). rfc_view's two atomic halves and its acquire go; ctr_take reads the count itself after copying and acquires only when it is the last holder, before it frees the cell and owns the node. blk_loc is term_peek for a shared block. FID_EXIT's continuation fast path stays: without it, code that applies a closure which may fork retires 28% more instructions at 8 threads. Full local suite on every lane: all pass but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
nicolas-abril
added a commit
to nicolas-abril/bend
that referenced
this pull request
Oct 5, 2026
- term_peek reads a redirect cell with one relaxed 64-bit load (plain on the device), as bendlang#1155 made blk_loc do: the target bits never change, so a torn read still finds them, and a shared node's fields never change either (span_fade only bumps its children). rfc_view's two atomic halves and its acquire go; ctr_take reads the count itself after copying and acquires only when it is the last holder, before it frees the cell and owns the node. blk_loc is term_peek for a shared block. FID_EXIT's continuation fast path stays: without it, code that applies a closure which may fork retires 28% more instructions at 8 threads. Full local suite on every lane: all pass but the two display-dependent gfx tests, as on main. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.
Fixes #975. Supersedes #997, #999 and #1084, and takes a piece from each.
What changes
blk_loc: reads a fork's redirect word as one relaxed 64-bit atomic on the CPU, and plainly on Metal and CUDA.ldr/movqas a plain read.rfc_view's two atomic halves. That makes the shared-array path about 2× slower on the CPU and 4.5× slower on Metal.BLK_SHR 0), nothing changes.comp.ts.tests/run/array_redirect_race.bend.tests/reg/spin_array_hold.bend: a spin that swaps its two arrays each turn, plus holds over a fork's shared handle.blk_locread makes unnecessary, and A spin reads a shared Array's redirect once, atomically: blk_loc no longer races its count #1084's compile-time hoist (its author suggested A shared array resolves its redirect once: a spin takes each box word's storage #999's instead).Race check
ThreadSanitizer, 5 runs each at 1, 2, 4 and 8 workers.
6a77e124), the reproducer reports a race in every run at 2 or more workers.array_fork,array_fork_unshareandstencil3d.Measurements
Apple M4,
cc -std=c11 -O3, interleaved rounds.Scaled
stencil3d(2^21 cells, 128 leaves, 160 passes). Best of 10 rounds, since load was high in that run:blk_locfix aloneThe old racy plain read was 522 ms, 127 ms and 7.43 s.
The 16
bench/runtimeprograms:BLK_SHR 0, and each emits the same C as main except the deadblk_locbody. Every output matches its pin.Tests
tests/suite, run locally withgates/test.ts's rules on all four probes: check, interp, JS and C (with Metal for the!programs).gfx_windowandgfx_clicks, which expect the headless "no display" answer, and this Mac has a display.bun gates/repo.ts: 49 / 49 (comp.tsstays under its cap).Credit to @uta080522 (#997) and @costamatheus97 (#1084), who are co-authors on the commit.
🤖 Generated with Claude Code