Repository navigation
perf(shader): the atomic locks bench times the turn and round forms - #261
Conversation
Issue pragyaangaur#240 measured the emulated 64-bit image atomics at ~6 s of every 7.4 s world frame on an M2 Pro, and its disassembly of the title's shaders found the one operation that costs it: an unsigned maximum of 64-bit keys into R32G32_UINT texels, all lanes of a wave, in alpha pre-pass pixel shaders. A faster exact form is being written on the issue. This adds the bench that form and any other will be measured against, before either lands — the method of the pagetable, extents and tex_refresh benches, applied to the lock loop. One wave of 32 lanes executes the lock loop's op stream on CPU: the lock words (ATOMIC_LOCKS, hashed from the coordinates), the spin's compare-exchange, the fenced read-modify-write and the `and` release are the emitted ones (lower.rs, "Atomic locks"). Two forms run at four shapes. The turn form emitted today walks every lane index, in turn one lane alone taking its lock, reading, writing and freeing it — two barriers each — whatever the other lanes' locks. The round form announced on pragyaangaur#240 has every lane not finished attempt its lock each round, the winners reading and writing in the same round, one fenced region. Three shapes are the runtime's own test's (image_atomics_of_64_bits, "as the alpha prepasses use them"): every lane into one texel, each lane its own, and the test's program of one colliding and one own umax; a quarter-res prepass sits between them. Both forms are asserted against a plain sequential umax at every shape: the same texels, the same old values lane by lane, every lock word freed — the exactness bar pragyaangaur#240's thread set for any faster form. The counted op stream prints beside the timings: the turn form pays two barriers per active lane at every shape; the round form two per round. The barrier is a sequentially-consistent fence on CPU, near-free where the GPU's threadgroup barrier serializes memory traffic, so the counts carry the structure and the ns the op streams. No other wave runs in the model, so the turn lane's bounded spin is not timed; it is the same in both forms. Co-Authored-By: GLM 5.3 <noreply@z.ai>
PR pragyaangaur#257 lands a different round form than pragyaangaur#240 announced: four rounds capped, one attempt per lane not done in each, nobody spinning — then today's turn loop whole, for whoever the rounds leave, its finished lanes passing through. The bench's round model was the announcement's unbounded rounds; this makes it the code that shipped. The worst case it models shrinks with the cap: at one texel for every lane, 150 attempts (122 in the rounds, 28 turns) against the modeled 528, the barriers unchanged at today's 64 — 4.7x today's lock-word traffic, not the announcement's 16.5x. A wave of distinct locks still finishes in the first round, paying three empty rounds for having no wave operation to leave by; four lanes per texel is unchanged, the cap exactly covering it. Co-Authored-By: GLM 5.3 <noreply@z.ai>
|
Model update: PR #257 landed a different round form than #240 announced, and the bench now models what shipped (2cbc7fb). #257 is rounds-then-turn-fallback: a new loop capped at
(barriers / lock attempts / loop iterations) Wine/Rosetta medians of the landed shape, 3 runs: turn 174.7 / 162.9 / 123.5 / 290.6 ns; round 1839.9 (one texel — the announcement model read 6712, the 4-round cap cuts the worst case's attempt storm 3.5x) / 733.8 (pool) / 110.1 (own — three empty rounds after the first, branches only) / 1851.5 (the test's program). So the ruler's readings, corrected: the turn form's cost is unconditional (64 barriers at every shape); the landed rounds collapse the structure at spread (2 barriers, one round) and exactly cover the four-lanes-per-texel prepass; at full collision the landed form still pays today's 64 barriers plus a 118-attempt storm on top — 4.7x today's lock-word traffic, not the 16.5x the announcement's unbounded rounds would have paid. The exactness assertions cover the landed form unchanged: same texels, same per-lane old values, every lock word freed, against a plain umax. |
Issue #240 measured the emulated 64-bit image atomics (
locked_atomic64,VICTORY_ATOMIC_LOCKS) at ~6 s of every 7.4 s world frame on an M2 Pro —VICTORY_ATOMIC_LOCKS=0runs 1.5 s frames but draws wrong (cut-out geometry: fences, hair, small props). The issue's disassembly also found the title uses one 64-bit image atomic:image_atomic_umaxof keys into R32G32_UINT texels, all lanes of a wave, in alpha pre-pass pixel shaders. A faster exact form is being written on the issue (the round form, announced 2026-11-26 09:34Z).This is the complementary piece: not a form, but the bench both that form and any other will be measured on — the method of the pagetable, extents and tex_refresh benches, applied to the lock loop. The round form below is the announced design as described on #240; when the landed code differs, it can update the model so both forms are measured on one stand.
The model
One wave of 32 lanes executes one umax's op stream on CPU: the coordinate hash into the
ATOMIC_LOCKSlock words, the spin's compare-exchange, the fenced read-modify-write, theandrelease — the emitted ones (victory-shaderlower.rs, "Atomic locks"). Two forms:locked_atomic64emits today: a loop over lane indices; in turnilaneialone takes its lock, reads, writes and frees it — two barriers each — whatever the other lanes' locks. The loop is unconditional: its best case is its worst case.atomic_locks) cost about 6 s of every world frame and cause the GPU resets; withVICTORY_ATOMIC_LOCKS=0frames drop from 7.4 s to 1.5 s #240: every lane not finished makes one attempt at its lock each round; the winners read-modify-write in the same round, one fenced region.Four shapes, three from the runtime's own test (
image_atomics_of_64_bits, "as the alpha prepasses use them"): every lane into one texel; each lane its own texel; the test's program of one colliding and one own umax per lane; and a quarter-res prepass (four lanes per texel) between them.Exactness, asserted every run
Both forms are checked against a plain sequential umax at every shape: the same texels, the same old values lane by lane, every lock word freed (
bench_atomic_locks_exact). That is the bar #240's thread set for any faster form — the returned old values included.Measured, Wine/Rosetta
6 runs of the committed bench (
profile atomic_locks_winein the campaign's records), median ns per wave visit:The counted op stream per visit (deterministic, printed by the bench):
Readings:
Reproducing
Natively,
cargo bench -p victory-formats --bench atomic_locksruns the same bench (native µs swing with core scheduling and code layout around the fences; Wine/Rosetta is the stable flavor).Verification
cargo build -p victory-gpu --target x86_64-pc-windows-gnullvmgreen.victory-gpusuite (WINE=/opt/homebrew/bin/wine tools/macos/test-wine.sh -p victory-gpu --no-fail-fast): 319 passed / 2 failed / 9 ignored — the two failures are feat(gpu): add opt-in mach_vm_remap zero-copy title buffer aliasing #207's own alias tests (aliased_shadow_refreshes_widened_rgb32_view,aliased_gpu_writes_notify_overlapping_texture_trackerat alias.rs:420/630), failing identically at clean HEAD, stash-verified; this PR touches no victory-gpu code.cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warningsexit 0, and the same with--all-features.perfops/bench.shdrift: 525 / 0 / 17, guard and texture metrics flat within the documented noise band (re-run per the monitoring protocol to confirm).