fix(shader): give one-lane waves an elected atomic max per lane #12724

Merged
Gonic0 merged 1 commit from upstream-pr/12724 into main 2026-10-29 23:18:15 +00:00
Gonic0 commented 2026-10-29 05:18:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-10-29T05:18:58Z.


What

On a one-lane wave, every guest lane is its own host invocation: vertex and local programs on a device without vertex subgroups (MoltenVK, lavapipe), and merged hull programs. There, a wave64 f32 max half-wave scan throws at its v_permlanex16_b32:

key = active ? x : -inf                 (v_cndmask under s_or_saveexec -1)
row_shr 1/2/4/8 v_max_f32 DPP steps, v_permlanex16_b32, v_max_f32
R = max(v_readlane 31, v_readlane 63)
v_cmpx_eq_f32 R, x
buffer_atomic_umax R                    (no return)

The wave elects the lanes holding the maximum and lets them write it with one atomic. If every active lane instead writes its own key with the same atomic, memory ends with the same maximum, as long as unsigned order on the key bits matches float order. That holds when every key is a non-negative number.

EmitHalfWaveReduction therefore reads lane 31 as the invocation's own key and lane 63 as the identity (-inf) on a one-lane host, and v_permlanex16_b32 keeps its own value. It does so only when ElectedAtomicMax proves all of the following; otherwise both still throw as before:

  • The maximum has only these uses. The two lane reads feed one v_max_f32. Its result feeds only the compare against the same key and the data of a buffer_atomic_umax or smax whose result is unused. Its written-lane mask equals the scan's active mask.
  • The elected EXEC selects only the atomic. It may also feed a branch, and selects that only that atomic reads. A register the elected lanes write and something else reads is rejected.
  • The address is the same for every lane. The atomic's resource, index, offset and soffset are wave-uniform (WaveUniformValues). For an operand that the elected EXEC selects, this is checked on the arm those lanes take.
  • The keys are non-negative numbers. A greatest-fixpoint proof over the key's definitions accepts FPSaturate, non-negative constants, phis, f32 maxima with one such operand (as EmitMinMaxF32Value lowers them, NaN and ±0 included), and selects. A select's other arm is dropped when the scan's lane mask implies its condition through the exec words. That does not apply to a select reached through a loop back edge, whose condition may belong to an earlier iteration.
  • The permutation's value goes nowhere else. It may also reach the atomic's address, but only in lanes the elected EXEC excludes.

Denormal flushing matches: each lane's data is its flushed key, which the scan's own flush selects already build, and the console's maximum is the maximum of those flushed keys.

The cost is one atomic per lane instead of one per wave. The debt entry in docs/dev/debt/shader.md records what is accepted and what throws.

ASTRO BOT (PPSA21564 01.007.000) uses this shape in at least nine vertex shaders. They are what stopped it on MoltenVK after the title. The proof is general and does not depend on that title.

Tested

Apple M5 Pro, macOS 27.2, MoltenVK from Vulkan SDK 1.4.363 (no vertex subgroups), x86-64 Release under Rosetta 2. main alone does not build on macOS. I ran the tests in a tree with the open macOS port PRs; the emitter change, the new test function and the new execution test are identical there.

  • agc_shader_memory_tests, new verifySingleLaneElectedAtomicMax, on programs assembled with llvm-mc (gfx1013, wave64):
    • Four builds with no subgroup operation: umax, smax, an address held by the permutation register only in lanes without the atomic, and no s_cbranch_execz.
    • Eleven rejections with the original message:
      • a key without clamp;
      • a key of -0;
      • an atomic result that is read;
      • the maximum exported;
      • buffer_atomic_add;
      • a compare against another value;
      • an address from the permutation;
      • an index or an address from the vertex;
      • an elected-lane register that is exported;
      • the maximum written under EXEC -1.
    • With vertex subgroups, the scan still uses subgroup operations.
  • agc_driver_vertex_elected_atomic_max_tests (new): draws 3, 21, 33, 48 and 129 vertices, which covers more than one wave. Each vertex has the key saturate(id / 32), and the buffer must hold min(1, (N-1)/32) and leave the next word alone. It passes with VK_LAYER_KHRONOS_validation loaded and no VUID- messages. It skips on devices with vertex subgroups.
  • Negative controls, each of which fails a test:
    • lane 31 read as the identity (the buffer stays 0);
    • no sign proof;
    • no address check;
    • accepting any select under the elected EXEC.
  • PPSA21564 01.007.000: none of the nine v_permlanex16_b32 … outside the 1-lane host subgroup failures remains. The run now reaches about 400 s, where it stops at an unrelated sampler case of #12662.

Not tested on Linux, Windows or lavapipe.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M5 Pro, MoltenVK (Vulkan SDK 1.4.363), macOS 27.2
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5062691046 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12724). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-10-29T05:18:58Z. --- ### What On a one-lane wave, every guest lane is its own host invocation: vertex and local programs on a device without vertex subgroups (MoltenVK, lavapipe), and merged hull programs. There, a wave64 f32 max half-wave scan throws at its `v_permlanex16_b32`: ``` key = active ? x : -inf (v_cndmask under s_or_saveexec -1) row_shr 1/2/4/8 v_max_f32 DPP steps, v_permlanex16_b32, v_max_f32 R = max(v_readlane 31, v_readlane 63) v_cmpx_eq_f32 R, x buffer_atomic_umax R (no return) ``` The wave elects the lanes holding the maximum and lets them write it with one atomic. If every active lane instead writes its own key with the same atomic, memory ends with the same maximum, as long as unsigned order on the key bits matches float order. That holds when every key is a non-negative number. `EmitHalfWaveReduction` therefore reads lane 31 as the invocation's own key and lane 63 as the identity (`-inf`) on a one-lane host, and `v_permlanex16_b32` keeps its own value. It does so only when `ElectedAtomicMax` proves all of the following; otherwise both still throw as before: - **The maximum has only these uses.** The two lane reads feed one `v_max_f32`. Its result feeds only the compare against the same key and the data of a `buffer_atomic_umax` or `smax` whose result is unused. Its written-lane mask equals the scan's active mask. - **The elected EXEC selects only the atomic.** It may also feed a branch, and selects that only that atomic reads. A register the elected lanes write and something else reads is rejected. - **The address is the same for every lane.** The atomic's resource, index, offset and soffset are wave-uniform (`WaveUniformValues`). For an operand that the elected EXEC selects, this is checked on the arm those lanes take. - **The keys are non-negative numbers.** A greatest-fixpoint proof over the key's definitions accepts `FPSaturate`, non-negative constants, phis, f32 maxima with one such operand (as `EmitMinMaxF32Value` lowers them, NaN and ±0 included), and selects. A select's other arm is dropped when the scan's lane mask implies its condition through the exec words. That does not apply to a select reached through a loop back edge, whose condition may belong to an earlier iteration. - **The permutation's value goes nowhere else.** It may also reach the atomic's address, but only in lanes the elected EXEC excludes. Denormal flushing matches: each lane's data is its flushed key, which the scan's own flush selects already build, and the console's maximum is the maximum of those flushed keys. The cost is one atomic per lane instead of one per wave. The debt entry in `docs/dev/debt/shader.md` records what is accepted and what throws. ASTRO BOT (PPSA21564 01.007.000) uses this shape in at least nine vertex shaders. They are what stopped it on MoltenVK after the title. The proof is general and does not depend on that title. ### Tested Apple M5 Pro, macOS 27.2, MoltenVK from Vulkan SDK 1.4.363 (no vertex subgroups), x86-64 Release under Rosetta 2. `main` alone does not build on macOS. I ran the tests in a tree with the open macOS port PRs; the emitter change, the new test function and the new execution test are identical there. - `agc_shader_memory_tests`, new `verifySingleLaneElectedAtomicMax`, on programs assembled with `llvm-mc` (gfx1013, wave64): - Four builds with no subgroup operation: `umax`, `smax`, an address held by the permutation register only in lanes without the atomic, and no `s_cbranch_execz`. - Eleven rejections with the original message: - a key without clamp; - a key of -0; - an atomic result that is read; - the maximum exported; - `buffer_atomic_add`; - a compare against another value; - an address from the permutation; - an index or an address from the vertex; - an elected-lane register that is exported; - the maximum written under EXEC -1. - With vertex subgroups, the scan still uses subgroup operations. - `agc_driver_vertex_elected_atomic_max_tests` (new): draws 3, 21, 33, 48 and 129 vertices, which covers more than one wave. Each vertex has the key `saturate(id / 32)`, and the buffer must hold `min(1, (N-1)/32)` and leave the next word alone. It passes with `VK_LAYER_KHRONOS_validation` loaded and no `VUID-` messages. It skips on devices with vertex subgroups. - Negative controls, each of which fails a test: - lane 31 read as the identity (the buffer stays 0); - no sign proof; - no address check; - accepting any select under the elected EXEC. - PPSA21564 01.007.000: none of the nine `v_permlanex16_b32 … outside the 1-lane host subgroup` failures remains. The run now reaches about 400 s, where it stops at an unrelated sampler case of #12662. Not tested on Linux, Windows or lavapipe. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Apple M5 Pro, MoltenVK (Vulkan SDK 1.4.363), macOS 27.2 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12724
No description provided.