feat(shader): implement ds_permute_b32 #334

Merged
adriadam10 merged 2 commits from upstream-pr/334 into main 2026-10-04 06:42:22 +00:00
adriadam10 commented 2026-10-02 06:58:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-02T06:58:45Z.


What

ds_permute_b32 (DS 0xb2) threw DS opcode is not supported. The ISA sweep found it. It is the forward (scatter) form of ds_bpermute_b32: every lane sends data to lane ((addr + offset) >> 2) & 31 of its wave.

Semantics measured on an RDNA2 GPU (wave32, 6 address patterns: reversed, all to lane 0, random; with offset:8; with EXEC = low 16 lanes). All matched by a Python model:

  • If several lanes target the same lane, the highest sending lane wins.
  • An active lane that no one writes to gets 0.
  • Inactive lanes neither send nor receive.

Lowering: PermuteU32 runs 32 unrolled steps, one per source lane of the wave. Each step shuffles the data, the address and the EXEC bit from that lane and keeps it when the target is this lane. Later lanes overwrite earlier ones. It uses the same wave base (lane & ~31) as ds_bpermute_b32. It is costly (96 shuffles), so estimatedSpirvWords reports it as such. The opcode is rare.

Test agc_driver_ds_permute: random targets, ds_bpermute_b32 on the same inputs as a reference, offset:8, and partial EXEC. Expected values were captured on the RDNA2 GPU. On devices whose subgroups are narrower than a wave (lavapipe: 8 lanes) the test reports itself as skipped, like the other cross-lane tests.

Tested

Linux, full ctest (100) passes.

  • RADV (Radeon 660M): the test passes. lavapipe: skipped (subgroup 8).
  • With the lowest sending lane winning instead: permute_offset8 is 0x00001017, expected 0x0000101e.

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: #251 (test harness), #320 (DS decoder tables); review only the last commit
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4713628438 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/334). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-02T06:58:45Z. --- ### What `ds_permute_b32` (DS 0xb2) threw `DS opcode is not supported`. The ISA sweep found it. It is the forward (scatter) form of `ds_bpermute_b32`: every lane sends `data` to lane `((addr + offset) >> 2) & 31` of its wave. Semantics measured on an RDNA2 GPU (wave32, 6 address patterns: reversed, all to lane 0, random; with `offset:8`; with EXEC = low 16 lanes). All matched by a Python model: - If several lanes target the same lane, the **highest** sending lane wins. - An active lane that no one writes to gets **0**. - Inactive lanes neither send nor receive. Lowering: `PermuteU32` runs 32 unrolled steps, one per source lane of the wave. Each step shuffles the data, the address and the EXEC bit from that lane and keeps it when the target is this lane. Later lanes overwrite earlier ones. It uses the same wave base (`lane & ~31`) as `ds_bpermute_b32`. It is costly (96 shuffles), so `estimatedSpirvWords` reports it as such. The opcode is rare. Test `agc_driver_ds_permute`: random targets, `ds_bpermute_b32` on the same inputs as a reference, `offset:8`, and partial EXEC. Expected values were **captured on the RDNA2 GPU**. On devices whose subgroups are narrower than a wave (lavapipe: 8 lanes) the test reports itself as skipped, like the other cross-lane tests. ### Tested Linux, full ctest (100) passes. - RADV (Radeon 660M): the test passes. lavapipe: skipped (subgroup 8). - With the lowest sending lane winning instead: `permute_offset8 is 0x00001017, expected 0x0000101e`. ### 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: #251 (test harness), #320 (DS decoder tables); review only the last commit - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-02 06:59:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-02T06:59:44Z.


Progress report (870bdd8 → 13ac232)

📈 GPU shader instructions: 73.24% (+0.08%, +1 implemented instructions)

✅ 1 implemented
Encoding Name
DS DS_PERMUTE_B32
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5947049944 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/334#issuecomment-5947049944). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-02T06:59:44Z. --- <!-- progress-report --> ### Progress report (870bdd8 → 13ac232) 📈 **GPU shader instructions**: 73.24% (+0.08%, +1 implemented instructions) <details> <summary>✅ 1 implemented</summary> | Encoding | Name | | - | - | | DS | `DS_PERMUTE_B32` | </details>
oneandonlydean commented 2026-10-02 10:01:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-02T10:01:56Z.


A heads-up on an interaction with my open #304, which neither PR's tests would catch.

#304 adds WaveUniformValues in SpirvAnalysis.cpp: on the two-lane wave64 path, a value whose inputs are all wave-uniform is emitted once and shared by both halves. Cross-lane ops whose result can differ per lane even with uniform inputs are listed in LaneSource (BpermuteU32, SwizzleU32, WriteLane, ...).

PermuteU32 belongs in that list. With a uniform value and a uniform address, every lane writes to the same target lane, so only that lane gets the value and the rest read 0. Without the entry, #304 would treat that result as uniform and give the second half the first half's value.

Whichever of the two lands second needs this one line:

case IrOpcode::PermuteU32:

in LaneSource. If #334 merges first, I'll add it to #304.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5949848997 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/334#issuecomment-5949848997). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-02T10:01:56Z. --- A heads-up on an interaction with my open #304, which neither PR's tests would catch. #304 adds `WaveUniformValues` in `SpirvAnalysis.cpp`: on the two-lane wave64 path, a value whose inputs are all wave-uniform is emitted once and shared by both halves. Cross-lane ops whose result can differ per lane even with uniform inputs are listed in `LaneSource` (`BpermuteU32`, `SwizzleU32`, `WriteLane`, ...). `PermuteU32` belongs in that list. With a uniform value and a uniform address, every lane writes to the same target lane, so only that lane gets the value and the rest read 0. Without the entry, #304 would treat that result as uniform and give the second half the first half's value. Whichever of the two lands second needs this one line: ```cpp case IrOpcode::PermuteU32: ``` in `LaneSource`. If #334 merges first, I'll add it to #304.
adriadam10 commented 2026-10-02 10:06:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-02T10:06:49Z.


Thanks, agreed: PermuteU32 is lane-dependent even with uniform inputs, so it belongs in LaneSource. LaneSource isn't on main yet, so nothing to add here for now. If #304 lands first I'll rebase and add the case line here; otherwise thanks for picking it up in #304. Could you ping me here when #304 gets merged?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5949951892 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/334#issuecomment-5949951892). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-02T10:06:49Z. --- Thanks, agreed: `PermuteU32` is lane-dependent even with uniform inputs, so it belongs in `LaneSource`. `LaneSource` isn't on main yet, so nothing to add here for now. If #304 lands first I'll rebase and add the `case` line here; otherwise thanks for picking it up in #304. Could you ping me here when #304 gets merged?
oneandonlydean commented 2026-10-04 05:41:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:41:43Z.


Ping as promised: #304 is merged now, so LaneSource in SpirvAnalysis.cpp is on main. On the merge result PermuteU32 is not in it, so WaveUniformValues treats a permute with uniform inputs (say every lane sends a constant to lane 0) as uniform, and the two-lane wave64 path would share one value across both halves even though only the target lane receives it. Could you rebase and add case IrOpcode::PermuteU32: next to BpermuteU32 there? Everything else looks right to me; I checked the 0xb2 encoding with llvm-mc for gfx1013.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977014066 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/334#issuecomment-5977014066). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:41:43Z. --- Ping as promised: #304 is merged now, so `LaneSource` in `SpirvAnalysis.cpp` is on main. On the merge result `PermuteU32` is not in it, so `WaveUniformValues` treats a permute with uniform inputs (say every lane sends a constant to lane 0) as uniform, and the two-lane wave64 path would share one value across both halves even though only the target lane receives it. Could you rebase and add `case IrOpcode::PermuteU32:` next to `BpermuteU32` there? Everything else looks right to me; I checked the 0xb2 encoding with llvm-mc for gfx1013.
adriadam10 commented 2026-10-04 06:21:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:21:35Z.


Thanks for the reminder. Rebased onto current main and added case IrOpcode::PermuteU32: to LaneSource, next to BpermuteU32. agc_driver_ds_permute and agc_driver_wave_uniform pass on RADV. My machine has no host with 32-wide subgroups, so I couldn't run the two-lane wave64 path locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977269719 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/334#issuecomment-5977269719). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T06:21:35Z. --- Thanks for the reminder. Rebased onto current main and added `case IrOpcode::PermuteU32:` to `LaneSource`, next to `BpermuteU32`. `agc_driver_ds_permute` and `agc_driver_wave_uniform` pass on RADV. My machine has no host with 32-wide subgroups, so I couldn't run the two-lane wave64 path locally.
oneandonlydean commented 2026-10-04 06:29:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T06:29:25Z.


Thanks for adding it. My RTX 4090 runs 32-wide subgroups, so I'll test the two-lane wave64 path (agc_driver_ds_permute and agc_driver_wave_uniform) here and merge once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977320045 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/334#issuecomment-5977320045). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T06:29:25Z. --- Thanks for adding it. My RTX 4090 runs 32-wide subgroups, so I'll test the two-lane wave64 path (`agc_driver_ds_permute` and `agc_driver_wave_uniform`) here and merge once CI is green.
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!334
No description provided.