perf(shader): find the ds_permute_b32 source lane with ballots #4181

Closed
karanvora2599 wants to merge 1 commit from upstream-pr/4181 into main
karanvora2599 commented 2026-10-12 00:51:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T00:51:31Z.


What

ds_permute_b32 finds each receiving lane's source with six ballots and fetches the data with one shuffle, instead of shuffling the data, address and EXEC bit of all 32 source lanes. Results are unchanged.

Background. ds_permute_b32 is the forward permute: each active lane sends its data to lane ((addr + offset) >> 2) & 31 of its 32-lane group. #334 implemented it, with the rules as measured on hardware: the highest sending lane wins, an active lane nobody writes to gets 0, and inactive lanes neither send nor receive. A receiving lane can't pull from a sender it doesn't know, so EmitPermuteU32 looped over all 32 possible senders: 3 shuffles, a compare and a select each, unrolled. On an RTX 4060 that is 144.5 SASS instructions per ds_permute_b32 when the address changes from one instruction to the next; ds_bpermute_b32, the pull form, is 6.

How. A receiving lane d needs the set of active lanes whose target is d. With T_b = the ballot of the active lanes whose target has bit b set, that set is

writers = ballot(active) & M_0 & M_1 & M_2 & M_3 & M_4, where M_b = T_b if bit b of d is set and ~T_b otherwise.

The highest set bit of writers (FindUMsb) is the winning lane, which keeps the highest-sender rule; one shuffle reads its data, and an empty set gives 0. The ballots are taken per 32-lane group the way the other cross-lane emitters take them (EmitWaveBallot, and the upper word for a wave64 in a 64-wide subgroup), and the shuffle goes through SpirvValueEmitContext::Shuffle, so wave64 at two lanes per invocation, split subgroups and the one-lane layout work as before. The instruction already required ballot and shuffle support.

Before / after on an RTX 4060 Laptop (Ada, driver 591.86). SASS instructions per instruction come from APS5_PIPELINE_STATS over a chain of 16, main's emitter against this one in the same tree:

ds_permute_b32 chain SASS before SASS after Registers before → after
address from the previous result 144.5 20 42 → 26
same address every step 124.5 3.5 74 → 21
same address, EXEC set at run time 133.5 5 76 → 23

With a fixed address the driver computes the ballots once for the whole chain, so 3.5 understates the cost of a real permute; the first row is the honest one. ds_bpermute_b32 with a changing address stays at 6.

Run time, one process, median of 15 interleaved dispatches over 16,384 groups of 32 lanes. A temporary switch, not part of the PR, selected the old loop for the comparison.

Chain old loop this PR
256 × ds_permute_b32, address from the previous result 7.41 ms 1.58 ms
2,048 × v_fma_f32 (reference) — 7.52 ms

Per instruction, ds_permute_b32 goes from about 8 v_fma_f32 to about 1.7. Both variants wrote the same results.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, Release, main at d0ca839c (rebased onto d371ed30, which only changes libkernel and libs). RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790), lavapipe from Mesa 25.2.8 (the version CI installs; Windows build, GALLIUM_OVERRIDE_CPU_CAPS=avx).

  • New agc_driver_ds_permute_lanes:
    • wave32 and wave64 programs in workgroups of 128 lanes, 16 batches of 4,096 lanes each, 131,072 results;
    • random targets with random high address bits and a DS offset that wraps;
    • batches with heavy collisions (4 targets per 32 lanes), full permutations, and EXEC set at run time from the input, so inactive lanes must neither send nor receive and keep their old value;
    • compared with a model of the rule from #334.
    • It passes with main's emitter and with this one on the RTX 4060 and the Intel UHD. On NVIDIA the wave64 program runs at two lanes per invocation. lavapipe skips it, as it skips agc_driver_ds_permute, because its subgroups are narrower than a wave.
  • agc_driver_ds_permute passes on the RTX 4060 and the Intel UHD.
  • Negative check: letting the lowest sending lane win (FindILsb instead of FindUMsb) makes both tests fail: agc_driver_ds_permute with lane 1 permute_offset8 is 0x00001017, expected 0x0000101e, and the new test with wave32 batch 0: lane 1 (address 0x3937c94a, data 0x4fd0e23e, exec 1) is 0x5cd936eb, expected 0x06a0efa9.
  • Shader tests (agc_driver_*, agc_shader_*, agc_spirv_*, vector_*, agc_masked_select, agc_validate_program, agc_denormal*, agc_float16_*; 339 tests). The BDA tests that hang on driver 591.86 until #3291 lands are left out.
    • RTX 4060: all pass.
    • Intel UHD: all pass or skip for missing features except agc_driver_ds_swizzle and agc_driver_f32_dot2_rounding (no Float64; it throws instead of skipping), which fail the same way without this change.
    • lavapipe 25.2.8: all pass or skip for missing features.

Why results don't change. For each receiving lane the old loop returned the data of the highest active lane whose target equals it, or 0. writers is exactly that set of lanes as a bit mask, so its highest bit is the same lane. I checked the formulation on the CPU against the old loop (200,000 random groups of 32 lanes, 6.4 million results, no differences), and the new test passes with both the old and the new emitter.

Limits.

  • I haven't run it on AMD hardware.
  • Wave64 keeps the current rule that ds_permute_b32 permutes within each 32-lane half. The test from #334 (agc_driver_ds_permute) is wave32; this PR doesn't change or confirm the wave64 behaviour.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823643464 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4181). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-12T00:51:31Z. --- ### What `ds_permute_b32` finds each receiving lane's source with six ballots and fetches the data with one shuffle, instead of shuffling the data, address and EXEC bit of all 32 source lanes. Results are unchanged. **Background.** `ds_permute_b32` is the forward permute: each active lane sends its data to lane `((addr + offset) >> 2) & 31` of its 32-lane group. #334 implemented it, with the rules as measured on hardware: the highest sending lane wins, an active lane nobody writes to gets 0, and inactive lanes neither send nor receive. A receiving lane can't pull from a sender it doesn't know, so `EmitPermuteU32` looped over all 32 possible senders: 3 shuffles, a compare and a select each, unrolled. On an RTX 4060 that is 144.5 SASS instructions per `ds_permute_b32` when the address changes from one instruction to the next; `ds_bpermute_b32`, the pull form, is 6. **How.** A receiving lane `d` needs the set of active lanes whose target is `d`. With `T_b` = the ballot of the active lanes whose target has bit `b` set, that set is `writers = ballot(active) & M_0 & M_1 & M_2 & M_3 & M_4`, where `M_b = T_b` if bit `b` of `d` is set and `~T_b` otherwise. The highest set bit of `writers` (`FindUMsb`) is the winning lane, which keeps the highest-sender rule; one shuffle reads its data, and an empty set gives 0. The ballots are taken per 32-lane group the way the other cross-lane emitters take them (`EmitWaveBallot`, and the upper word for a wave64 in a 64-wide subgroup), and the shuffle goes through `SpirvValueEmitContext::Shuffle`, so wave64 at two lanes per invocation, split subgroups and the one-lane layout work as before. The instruction already required ballot and shuffle support. **Before / after** on an RTX 4060 Laptop (Ada, driver 591.86). SASS instructions per instruction come from `APS5_PIPELINE_STATS` over a chain of 16, `main`'s emitter against this one in the same tree: | `ds_permute_b32` chain | SASS before | SASS after | Registers before → after | |---|---|---|---| | address from the previous result | 144.5 | 20 | 42 → 26 | | same address every step | 124.5 | 3.5 | 74 → 21 | | same address, EXEC set at run time | 133.5 | 5 | 76 → 23 | With a fixed address the driver computes the ballots once for the whole chain, so 3.5 understates the cost of a real permute; the first row is the honest one. `ds_bpermute_b32` with a changing address stays at 6. Run time, one process, median of 15 interleaved dispatches over 16,384 groups of 32 lanes. A temporary switch, not part of the PR, selected the old loop for the comparison. | Chain | old loop | this PR | |---|---|---| | 256 × `ds_permute_b32`, address from the previous result | 7.41 ms | 1.58 ms | | 2,048 × `v_fma_f32` (reference) | — | 7.52 ms | Per instruction, `ds_permute_b32` goes from about 8 `v_fma_f32` to about 1.7. Both variants wrote the same results. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, Release, `main` at d0ca839c (rebased onto d371ed30, which only changes libkernel and libs). RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790), lavapipe from Mesa 25.2.8 (the version CI installs; Windows build, `GALLIUM_OVERRIDE_CPU_CAPS=avx`). - New `agc_driver_ds_permute_lanes`: - wave32 and wave64 programs in workgroups of 128 lanes, 16 batches of 4,096 lanes each, 131,072 results; - random targets with random high address bits and a DS offset that wraps; - batches with heavy collisions (4 targets per 32 lanes), full permutations, and EXEC set at run time from the input, so inactive lanes must neither send nor receive and keep their old value; - compared with a model of the rule from #334. - It passes with `main`'s emitter and with this one on the RTX 4060 and the Intel UHD. On NVIDIA the wave64 program runs at two lanes per invocation. lavapipe skips it, as it skips `agc_driver_ds_permute`, because its subgroups are narrower than a wave. - `agc_driver_ds_permute` passes on the RTX 4060 and the Intel UHD. - Negative check: letting the lowest sending lane win (`FindILsb` instead of `FindUMsb`) makes both tests fail: `agc_driver_ds_permute` with `lane 1 permute_offset8 is 0x00001017, expected 0x0000101e`, and the new test with `wave32 batch 0: lane 1 (address 0x3937c94a, data 0x4fd0e23e, exec 1) is 0x5cd936eb, expected 0x06a0efa9`. - Shader tests (`agc_driver_*`, `agc_shader_*`, `agc_spirv_*`, `vector_*`, `agc_masked_select`, `agc_validate_program`, `agc_denormal*`, `agc_float16_*`; 339 tests). The BDA tests that hang on driver 591.86 until #3291 lands are left out. - RTX 4060: all pass. - Intel UHD: all pass or skip for missing features except `agc_driver_ds_swizzle` and `agc_driver_f32_dot2_rounding` (no `Float64`; it throws instead of skipping), which fail the same way without this change. - lavapipe 25.2.8: all pass or skip for missing features. **Why results don't change.** For each receiving lane the old loop returned the data of the highest active lane whose target equals it, or 0. `writers` is exactly that set of lanes as a bit mask, so its highest bit is the same lane. I checked the formulation on the CPU against the old loop (200,000 random groups of 32 lanes, 6.4 million results, no differences), and the new test passes with both the old and the new emitter. **Limits.** - I haven't run it on AMD hardware. - Wave64 keeps the current rule that `ds_permute_b32` permutes within each 32-lane half. The test from #334 (`agc_driver_ds_permute`) is wave32; this PR doesn't change or confirm the wave64 behaviour. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
oneandonlydean commented 2026-10-13 00:33:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T00:33:20Z.


Closing, as with #4061: none of the 837 shaders in the census there has a ds_permute_b32, so there is no frame to measure it on. Please reopen when a title's shader uses it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6135331984 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4181#issuecomment-6135331984). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T00:33:20Z. --- Closing, as with #4061: none of the 837 shaders in the census there has a `ds_permute_b32`, so there is no frame to measure it on. Please reopen when a title's shader uses it.

Pull request closed

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!4181
No description provided.