Skip to content

perf(shader): reduce a glc-clear 64-bit umax to a representative a texel - #304

Merged
pragyaangaur merged 5 commits into
pragyaangaur:mainfrom
GDemay:perf/atomic-umax-reduce
Nov 26, 2026
Merged

pragyaangaur merged 5 commits into
pragyaangaur:mainfrom
GDemay:perf/atomic-umax-reduce

Conversation

@GDemay

@GDemay GDemay commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

#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 found the title's one operation: an unsigned maximum of 64-bit keys into R32G32_UINT texels, all lanes of a wave, in the alpha pre-passes. The issue's negative result is the guide: most of these writes win, so the cost is not lanes waiting on locks but the serialized sections themselves — and the rounds of #257 (opt-in, merged) still leave the colliding shapes at today's cost.

The issue's thread also invited "a special case for 64-bit unsigned max", gated on glc. A lock-free special case cannot be exact — four interleavings close the door:

  • The low dword as a maximum is undisplaceable. Texel (0, 7), one lane with (1, 2): raise the high dword, install max(lo) → (1, 7), a key nobody wrote, bigger than every contribution, and no later umax can remove it.
  • A winner's low-dword store erases a same-high tie. W (5,3) raises; T (5,9) ties and writes 9; W installs 3 → (5,3); the max is (5,9).
  • Every check-then-write has a window. A racing raiser lands inside it; the repair must undo or absorb, and value-guarded repairs are ABA against later cohorts.
  • An unlocked high-dword write poisons the locked form. The locked lane reads a transient pair and writes it back: contributions die with the transient, in both install flavors.

So the form this lands keeps the lock and attacks the count of sections instead: with glc clear, a umax reads no old value and the maximum is associative, so the wave first folds each texel's lanes to their maximum key — in registers, one wave-uniform pass a group, two wave maxima a pass (the group's high dword, then the low dwords of its best) — and one representative a group alone runs the rounds and the turns. The reduced lanes are done from the start, exactly as the rounds' helper lanes are. A wave whose lanes meet on one texel locks once instead of 32 times; the tie case the issue set is decided inside the reduction, in registers, where no other wave can interleave.

Measured — the #261 ruler gains the form's model

Wine/Rosetta medians of the committed bench, 32 lanes a wave (barriers / lock attempts / loop iterations / wave ops a visit):

shape today's turns #257's rounds umax reduction + rounds
every lane one texel 64 / 32 / 32 / 0 64 / 150 / 36 / 0 2 / 1 / 4 / 6
four lanes a texel 64 / 32 / 32 / 0 8 / 80 / 4 / 0 2 / 8 / 4 / 41
own texel each 64 / 32 / 32 / 0 2 / 32 / 4 / 0 2 / 32 / 4 / 161
the runtime's test program 128 / 64 / 64 / 0 66 / 182 / 40 / 0 4 / 33 / 8 / 167

ns a visit (Wine/Rosetta): the colliding shape falls from 174.7 (turns) and 1839.9 (rounds) to 87.6; the honest price is the all-distinct shape (328.8 — the reduction's wave ops buy nothing there), reported, not hidden. The model asserts what the form claims: same texels as a plain umax, every form, the members never reading an old value, every lock word freed.

The exactness gate, per the issue's ask

image_atomics_of_64_bits_umax_matches_the_locks: random keys, 8 texels, high dwords 0 to 3 so keys often tie and the low dword decides, glc-clear, one group and eight, row 0 identical to the CPU model and to the unreduced rounds; no lock left taken. The existing rounds test now also runs reduced: its glc-set umax must keep the general form (the reduction's gate — the old value is read there), and its per-lane old-value checks would catch a mis-route. WARP gives the triple check where its heap_atomics64 allows.

The switch

VICTORY_ATOMIC_UMAX=1 (opt-in while measured, only with VICTORY_ATOMIC_LOCK_ROUNDS=1, its own translation-cache slot; the pipeline library's cache test covers the new key). The representative applies the group's maximum key, which equals every member's contribution in any order — so flipping the default after the M2 Pro tables would be its own commit.

Verification

  • Native suite 541 / 0 / 17, identical to the clean base (the new test is Windows-side, as the runtime's other atomics tests are).
  • cargo build -p victory-gpu --target x86_64-pc-windows-gnullvm green; clippy on both feature lanes, the CI-faithful target, true exit 0.
  • Wine victory-shader suite: 63 environment failures (no SM 6.6 device locally) identical to the clean base, stash-free A/B verified — zero new failures; the new tests' lowering runs locally (they skip only at the device gate).
  • Wine victory-gpu suite: the two pre-existing feat(gpu): add opt-in mach_vm_remap zero-copy title buffer aliasing #207 alias failures only.
  • The full lowering of every form runs in the bench's model asserts and the tests' lower_cs32_with path on every run.

guillaume-tesla and others added 5 commits November 26, 2026 15:26
The title's 64-bit image atomics are one operation (pragyaangaur#240): an unsigned
maximum of keys into R32G32_UINT texels, the alpha pre-passes' all lanes
a wave. The lock cost ~6 s of a 7.4 s world frame on an M2 Pro, and the
negative result on pragyaangaur#240 says why no lock-skipping form helps: most of
these writes win, so the cost is the serialized sections themselves.

A glc-clear umax reads no old value, and the maximum is associative, so
the wave first folds each texel's lanes to their maximum key, in
registers — one wave-uniform pass a group, two wave maxima a pass — and
one representative a group alone runs the rounds and the turns the
merged pragyaangaur#257 lands. The reduced lanes are done from the start, as the
rounds' helper lanes are. A wave whose lanes meet on one texel locks
once instead of 32 times.

The reduction rides the rounds (VICTORY_ATOMIC_UMAX=1, only with
VICTORY_ATOMIC_LOCK_ROUNDS=1, its own translation cache entry): without
them a reduced-to-nothing lane would still take its empty turn. The
representative applies the group's maximum key, which equals every
member's contribution in any order, ties included, so the result is
exact; the first ballot is taken where the guard is live, the loop is
wave-uniform, and the lanes the guard kept out contribute the wave
maxima's identity.

The lock-free forms this replaces cannot be exact: a low-dword maximum
is undisplaceable once written, a winner's low-dword store erases a
same-high tie's, and every check-then-write leaves a window a racing
raiser enters. The reduction never writes outside the lock.

The bench gains the form's model: one texel for every lane falls from
64 barriers and 32 attempts to 2 and 1; four lanes a texel to 2 and 8.
The test the issue asked for compares the form against the locks on
random keys with high-dword ties, one group and eight.

Co-Authored-By: GLM 5.3 <noreply@z.ai>
@pragyaangaur
pragyaangaur merged commit ae26882 into pragyaangaur:main Nov 26, 2026
1 check passed
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.

3 participants