fix(shader): select the written lane in v_writelane_b32 #254

Merged
adriadam10 merged 2 commits from upstream-pr/254 into main 2026-10-02 08:13:03 +00:00
adriadam10 commented 2026-10-01 19:54:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-01T19:54:49Z.


What

v_writelane_b32 wrote its value to every lane. The WriteLane IR is {value, lane, previous} (that is how vWritelaneB32 emits it and how ReadLaneEliminator reads it), but EmitWriteLane used Arg(2) as the lane and Arg(1) as the value:

hit = subgroupId == Arg(2)            // previous value, not the lane
return select(hit, Arg(1), Arg(0))    // lane index if hit, else the value

So after a v_readlane/v_writelane loop every lane held the last written value, and a lane whose old value happened to equal its id got the lane index instead (that is how I noticed it: a diagonal of 0x1f in the output). Fixed to hit = subgroupId == Arg(1), select(hit, Arg(0), Arg(2)). No other code builds WriteLane.

Found while writing an execution test for scalar ops that moves values through SGPRs with v_readlane/v_writelane.

Test agc_driver_write_lane: hand-assembled wave32 compute shader (words from llvm-mc -mcpu=gfx1013): a scalar loop over the 32 lanes that reverses a vector with v_readlane + v_writelane at lane 31-i (SGPR lane select), copies another one at lane i, plus one v_writelane with an inline-constant lane on a vector initialised to the lane id. All lanes and results are checked.

The test is skipped when the device subgroup is smaller than 32: lavapipe reports 8, so a wave32 cannot address all its lanes there (it read lane 7 instead of 31). It therefore runs on real GPUs, not in the lavapipe CI job.

Tested

Linux, RADV (Radeon 660M): passes; without the fix it fails (lane 0 result 1 is 1374938576, expected 1342177280). lavapipe: skipped (subgroup 8). Full ctest (99) passes.

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 helper VulkanTestDevice.hpp); review only the last commit
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4708840034 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/254). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-01T19:54:49Z. --- ### What `v_writelane_b32` wrote its value to **every** lane. The `WriteLane` IR is `{value, lane, previous}` (that is how `vWritelaneB32` emits it and how `ReadLaneEliminator` reads it), but `EmitWriteLane` used `Arg(2)` as the lane and `Arg(1)` as the value: ```cpp hit = subgroupId == Arg(2) // previous value, not the lane return select(hit, Arg(1), Arg(0)) // lane index if hit, else the value ``` So after a `v_readlane`/`v_writelane` loop every lane held the last written value, and a lane whose old value happened to equal its id got the lane index instead (that is how I noticed it: a diagonal of `0x1f` in the output). Fixed to `hit = subgroupId == Arg(1)`, `select(hit, Arg(0), Arg(2))`. No other code builds `WriteLane`. Found while writing an execution test for scalar ops that moves values through SGPRs with `v_readlane`/`v_writelane`. Test `agc_driver_write_lane`: hand-assembled wave32 compute shader (words from `llvm-mc -mcpu=gfx1013`): a scalar loop over the 32 lanes that reverses a vector with `v_readlane` + `v_writelane` at lane `31-i` (SGPR lane select), copies another one at lane `i`, plus one `v_writelane` with an inline-constant lane on a vector initialised to the lane id. All lanes and results are checked. The test is skipped when the device subgroup is smaller than 32: lavapipe reports 8, so a wave32 cannot address all its lanes there (it read lane 7 instead of 31). It therefore runs on real GPUs, not in the lavapipe CI job. ### Tested Linux, RADV (Radeon 660M): passes; without the fix it fails (`lane 0 result 1 is 1374938576, expected 1342177280`). lavapipe: skipped (subgroup 8). Full ctest (99) passes. ### 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 helper `VulkanTestDevice.hpp`); review only the last commit - [x] AI-assisted: yes
oneandonlydean commented 2026-10-02 00:51:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-02T00:51:00Z.


One more detail in EmitWriteLane, in case you want it here: the lane select wraps at the wave size. The ISA takes the lane from S1[4:0] in wave32 and S1[5:0] in wave64, so v_writelane_b32 v1, s0, 37 in wave32 writes lane 5.

With this PR, the raw lane is compared with the invocation's lane id. A lane select of 32 or more (64 or more in wave64) therefore writes no lane at all.

That also disagrees with ReadLaneEliminator, which already matches constant lanes as selector % waveSize. A v_readlane of lane 5 after the write above gets s0 when the eliminator folds it, but the old value when it doesn't.

Masking the lane first fixes both:

const auto lane = EmitBinaryU32(state, spv::OpBitwiseAnd, ctx.Arg(inst, 1), ConstantU32(state, state.program.WaveSize() - 1u));
state.module.AddFunction(spv::OpIEqual, TypeBool(state), hit, EmitSubgroupLocalInvocationId(state), lane);

v_readlane_b32 has the same edge in wave32. Shuffle passes the lane straight to OpGroupNonUniformShuffle, whose result is undefined for an id at or above the subgroup size. The two-lane wave64 path already wraps: it masks with 31 and picks the half from bit 5.

I can send this as a follow-up after this lands if you'd rather keep this PR as it is.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5943504713 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/254#issuecomment-5943504713). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-02T00:51:00Z. --- One more detail in `EmitWriteLane`, in case you want it here: the lane select wraps at the wave size. The ISA takes the lane from S1[4:0] in wave32 and S1[5:0] in wave64, so `v_writelane_b32 v1, s0, 37` in wave32 writes lane 5. With this PR, the raw lane is compared with the invocation's lane id. A lane select of 32 or more (64 or more in wave64) therefore writes no lane at all. That also disagrees with `ReadLaneEliminator`, which already matches constant lanes as `selector % waveSize`. A `v_readlane` of lane 5 after the write above gets `s0` when the eliminator folds it, but the old value when it doesn't. Masking the lane first fixes both: ```cpp const auto lane = EmitBinaryU32(state, spv::OpBitwiseAnd, ctx.Arg(inst, 1), ConstantU32(state, state.program.WaveSize() - 1u)); state.module.AddFunction(spv::OpIEqual, TypeBool(state), hit, EmitSubgroupLocalInvocationId(state), lane); ``` `v_readlane_b32` has the same edge in wave32. `Shuffle` passes the lane straight to `OpGroupNonUniformShuffle`, whose result is undefined for an id at or above the subgroup size. The two-lane wave64 path already wraps: it masks with 31 and picks the half from bit 5. I can send this as a follow-up after this lands if you'd rather keep this PR as it is.
adriadam10 commented 2026-10-02 04:19:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-02T04:19:31Z.


Thanks, applied: EmitWriteLane now masks the lane select with WaveSize() - 1 before comparing with the lane id. The test now writes with an inline lane of 37 on a wave32 and checks that lane 5 gets the value (without the mask it is lost: lane 5 result 2 is 5, expected 4660).

I left v_readlane_b32 / Shuffle for your follow-up, as you offered.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5945530208 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/254#issuecomment-5945530208). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-02T04:19:31Z. --- Thanks, applied: `EmitWriteLane` now masks the lane select with `WaveSize() - 1` before comparing with the lane id. The test now writes with an inline lane of 37 on a wave32 and checks that lane 5 gets the value (without the mask it is lost: `lane 5 result 2 is 5, expected 4660`). I left `v_readlane_b32` / `Shuffle` for your follow-up, as you offered.
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!254
No description provided.