Skip to content

Fix the shared array redirect race - #997

Closed
ghost wants to merge 1 commit into
mainfrom
unknown repository
Closed

ghost wants to merge 1 commit into
mainfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Sep 22, 2026 •

Copy link
Copy Markdown

Problem

blk_loc reads a shared Array redirect as a u64 while other handles atomically change its 32-bit reference count. The previous patch used rfc_view for every lookup, adding atomic reads to the array-access path.

Change

A redirect still occupies one eight-byte slot, but its count and immutable target are separate u32 fields. Array and buffer handles carry the target's low eight bits in unused aux bits. blk_loc reads the other 32 bits from the redirect and reconstructs the location.

The lookup therefore uses one ordinary read from the same slot as before, with no atomic read or fence per array access. The full-word redirect reads in the runtime and Window renderer have also been changed to read the fields separately. Constructor IDs are unchanged.

On the Metal source path, the lookup remains one plain device-memory read, now of the upper 32 bits rather than the full 64-bit slot. The additional shifts and OR use bits already in the handle. Creating a redirect takes two field stores instead of one full-word store.

Validation

On unmodified main (6a77e124), the 256-leaf reproducer reports a race with two workers under ThreadSanitizer. With this change, five runs each at 1, 2, 4 and 8 workers printed 256 without diagnostics. Three other array-fork tests ran cleanly under ThreadSanitizer at 2 and 4 workers.

Nine RFC tests and eight array tests passed in the interpreter and generated JS, and in native CPU runs with 1 and 4 workers. A separate C probe covered 40-bit redirect targets, constructor IDs and the CPU Window lookup. The repository gate passed 46/46.

On WSL x86-64 with Clang -O3, a scaled stencil (2^18 cells, 64 leaves, 160 passes) took a median 0.1035 s on main and 0.1058 s with this change at one worker; at four workers the times were 0.0752 s and 0.0754 s. These are CPU measurements. For the shared-array path on Metal, the scaled stencil in #999 is the relevant comparison: all 16 existing runtime benchmarks compile with BLK_SHR=0 and do not exercise this lookup.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thank you for your contribution. Unfortunately your patch leads to very big performance regressions, so I can't merge it as it is. Especially large on metal, where I'm assuming you can't test yourself.
I actually also had a patch for the issue but it had similar performance issues.

I'd prefer to either find an alternative fix to the data race or change the underlying operations to something else rther than accept these changes as they are.
Feel free to contact me on discord if you need me to tests things out for you.

@ghost
ghost force-pushed the fix/975-shared-array-redirect branch from a22ca5f to c226c5b Compare September 23, 2026 15:28
@ghost

ghost commented Sep 23, 2026

Copy link
Copy Markdown
Author

I've replaced the rfc_view lookup with a split redirect. The count and target are separate fields in the same eight-byte slot. An Array handle supplies the target's low eight bits, so blk_loc reads only the immutable upper field: one ordinary read, with no atomic load or fence on each access.

The updated description includes the race check and paired CPU timings. I used the shared-array stencil for the performance comparison because the existing runtime benchmarks all compile with BLK_SHR=0; they do not exercise this lookup.

@nicolas-abril nicolas-abril self-assigned this Sep 24, 2026

@nicolas-abril nicolas-abril left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, the split redirect fixes the regression. On an Apple M4, with the scaled stencil3d from #999 (2^21 cells, 128 leaves, 160 passes, median of 10 runs), this branch is within noise of main on every lane: 518 → 519 ms single-threaded, 120 → 120 ms on 10 threads, 7.23 → 7.32 s on Metal. A control that reads the redirect through rfc_view in blk_loc gives 1091 ms, 237 ms and 32.9 s, so the benchmark does see the old cost. The 16 runtime benchmarks stay within ±3% of main in all three perf-gate modes, with outputs matching the pins.

One change before merge, in term_drop. The last-reference path now calls rfc_view(e, r), which adds an atomic load of the count word, where main read the redirect with one plain load. That load isn't needed: a32_sub_rel already returns the old count word, whose top eight bits are the storage's low bits, and loc_hi never changes. So the target can be rebuilt from the value in hand plus the immutable half:

u32 old = a32_sub_rel(p, 1);
if ((old & RFC_CNT) != 1) {
  t = 0;
} else {
  a32_acq(p);
  Loc src = ((u64)rfc_at(H, r)->loc_hi << 8) | (old >> 24);
  t = (t & ~(RFC_BIT | LOC_MASK)) | src;
  heap_free(e, 0, r);
}

This stays race-free, since it reads only the returned value and the immutable half, and drops one atomic load per last-reference drop. I haven't measured it: the only benchmark that moved with this branch is tree-bitonic, +2.1% single-threaded and +2.8% on 16 threads over 10 runs, and this path is my best guess for it. Could you check whether the change recovers that?

Note: this review was written by an AI after it tested the PR for me and I reviewed the findings. If anything here is wrong, reply and I will review it myself.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Correction to my previous comment: in further measurements we see a considerable performance loss on Metal. On an Apple M4, tree-bitonic on the GPU goes from 1.83 s on main to 2.06 s with this branch (about 12% slower, median of 7 interleaved runs), and the term_drop change I suggested doesn't recover it.

Note: this comment was written by an AI after it measured the PR for me.

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Superseded by #1155, which fixes the race by reading the redirect with one relaxed atomic on the CPU and a plain load on Metal and CUDA, so Metal keeps its speed. Your reproducer is in it as tests/run/array_redirect_race.bend, and you're a co-author on the commit. Thank you!

nicolas-abril added a commit that referenced this pull request Sep 28, 2026
…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]>
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