Skip to content

feat(shader): count and log the lanes that give up waiting for a 64-bit atomic's lock - #363

Open
hackgods wants to merge 3 commits into
pragyaangaur:mainfrom
hackgods:feat/atomic-lock-gave-up-count
Open

hackgods wants to merge 3 commits into
pragyaangaur:mainfrom
hackgods:feat/atomic-lock-gave-up-count

Conversation

@hackgods

Copy link
Copy Markdown
Contributor

Follow-up to #306. evan-kolberg's M4 Pro run there still lost 5–6 adds in 1 of 20 trials per mode at 2,048 groups. This makes every such loss visible, so we can tell whether a lane gave up its lock or something else went wrong.

What changed

  • A lane that waits out its bound now adds one to a count after the lock words (ATOMIC_LOCKS_GAVE_UP_AT, at the end of the u1 buffer, which grows by 4 bytes). The count rides on the atomic each lane already makes at the end of its step. A lane that took the lock adds all ones to it (it holds 1, so that frees it), and a lane that gave up adds one to the count. Before, it made a no-op and on the lock instead. There is no new branch and no extra atomic.
  • After every submission the host reads and clears the count, and logs lanes gave up waiting for a 64-bit atomic's lock and went on without it, so their updates may be lost (#240) each time the run's total passes a power of two. Freeing stuck locks after an ended submission (fix(shader): 64-bit atomics no longer lose updates under heavy lock contention #306) leaves the count alone.
  • Tests: adds64_apply_once reads the count and checks that every lost add has a lane that gave up behind it. A loss without one would be another fault. It then checks for no loss and no give-up. The unit test of the lock freeing also covers the count.
  • docs/macos.md and the module notes ("Atomic locks") describe the count and the log line.

Why

On an M5 Pro, with compute put back on 2^16 tries, every failing stress run had lanes that gave up behind it (from 1 to 448 lanes, in 13 failing runs out of 22 invocations on two bases), and no run lost more adds than lanes gave up. That pins the M5 Pro losses on the bound. Under that contention a try takes about 1.5 µs here, so 2^22 tries is a budget of several seconds for a dispatch of about 0.1 s. If the M4 Pro still loses adds at 2^22, the count will say whether it is the bound or a different fault there.

#304's reduced umax runs through the same step, so its representatives are counted too.

How tested

On an M5 Pro (64 GB, macOS 27.2.1, CrossOver 26.3 Wine, D3DMetal 4.0 beta 3):

  • Before and after, on main ae26882: cargo test --workspace 542 passed, 0 failed both times. tools/macos/test-wine.sh --workspace --no-fail-fast 1,104 passed, 0 failed both times (existing tests changed, none added).
  • image_atomic_adds_of_64_bits_apply_once_under_contention (ignored, D3DMetal): 3 of 3 invocations passed on 2^22. With compute back on 2^16, 3 of 4 failed, each with lanes that gave up (18, 214 and 91).
  • Cost, in µs per wave-wide umax: within this Mac's run-to-run noise. A lighter copy of atomic_lock_rounds_timing (16 and 256 per lane, the best of 4 warm runs, 6 runs per build) gave turns 15.9–18.1 before fix(shader): 64-bit atomics no longer lose updates under heavy lock contention #306, 16.7–18.9 with fix(shader): 64-bit atomics no longer lose updates under heavy lock contention #306 and 15.5–23.3 with this. Rounds gave 2.4–7.5, 3.2–7.1 and 3.3–7.0. The full test swung between about 21 and 32 µs in turns on every build, from run to run.
  • cargo clippy -p victory-shader -p victory-gpu --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings and cargo xtask policy pass. Only changed lines are formatted.

🤖 Generated with Claude Code

hackgods and others added 2 commits November 26, 2026 19:46
…c's lock

A lane that waits out its bound goes on without the lock and can lose its
update (pragyaangaur#240, pragyaangaur#306). Such a lane now adds one to a count after the lock
words (ATOMIC_LOCKS_GAVE_UP_AT; the u1 buffer grows by 4 bytes), so a loss
can be told apart from any other fault. The count rides on the atomic a
lane already makes at the end of its step: a lane that took the lock adds
all ones to it (it holds 1, so that frees it), and a lane that gave up
adds one to the count instead of an `and` that changed nothing. No new
branch and no extra atomic.

Test: adds64_apply_once checks that every lost add has a lane that gave
up behind it, then that there is no loss and no give-up. On an M5 Pro with
compute back on 2^16 tries, every failing stress run had lanes that gave
up behind it, and no run lost more adds than lanes gave up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After every submission the host reads and clears the count of lanes that
gave up their lock (crate::shadow::take_gave_up) and logs the run's total
each time it passes a power of two, so a game run shows whether the bound
is ever reached and updates may be lost. Freeing stuck locks after an
ended submission now leaves the count alone.

Test: freeing_atomic_locks_keeps_pages_and_read_marks covers the count.
docs/macos.md describes the count and the log line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants