Skip to content

A shared Array's redirect is read without a race and without an atomic on the device, once per spin (#975) - #1155

Merged
nicolas-abril merged 2 commits into
mainfrom
fix-975-redirect-race
Sep 28, 2026
Merged

nicolas-abril merged 2 commits into
mainfrom
fix-975-redirect-race

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Fixes #975. Supersedes #997, #999 and #1084, and takes a piece from each.

What changes

Race check

ThreadSanitizer, 5 runs each at 1, 2, 4 and 8 workers.

  • On the old plain read (6a77e124), the reproducer reports a race in every run at 2 or more workers.
  • On main and on this branch, it reports none. The same holds for array_fork, array_fork_unshare and stencil3d.

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:

lane main blk_loc fix alone this PR
1 thread 1095 ms 692 ms 447 ms
16 threads 250 ms 160 ms 110 ms
Metal 28.9 s 4.9 s 1.42 s

The old racy plain read was 522 ms, 127 ms and 7.43 s.

The 16 bench/runtime programs:

  • All are BLK_SHR 0, and each emits the same C as main except the dead blk_loc body. Every output matches its pin.
  • 1 thread and 16 threads: 32 rows over 5 rounds, all within ±3% except tree-radix single-threaded at −5.5%. That can't be the code, since the only difference is dead.
  • Metal: 6 rounds with a throwaway GPU run before each pair, all within ±3% except bfs at +6.4%. bfs's per-round times show the run order, not the code: its best is 0.748 s on main and 0.750 s here.

Tests

  • The full tests/ suite, run locally with gates/test.ts's rules on all four probes: check, interp, JS and C (with Metal for the ! programs).
    • This branch: 1499 / 1501. Main: 1497 / 1499.
    • The same 2 failures on both: gfx_window and gfx_clicks, which expect the headless "no display" answer, and this Mac has a display.
  • bun gates/repo.ts: 49 / 49 (comp.ts stays under its cap).
  • Not run: the cluster gates and CUDA.

Credit to @uta080522 (#997) and @costamatheus97 (#1084), who are co-authors on the commit.

🤖 Generated with Claude Code

…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]>
…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]>
@nicolas-abril
nicolas-abril merged commit ef66a7c into main Sep 28, 2026
@nicolas-abril
nicolas-abril deleted the fix-975-redirect-race branch September 28, 2026 10:23
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]>
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.

Shared Array redirect lookup races with atomic reference-count updates

1 participant