fix(shader): select the written lane in v_writelane_b32 #254
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/254"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-01T19:54:49Z.
What
v_writelane_b32wrote its value to every lane. TheWriteLaneIR is{value, lane, previous}(that is howvWritelaneB32emits it and howReadLaneEliminatorreads it), butEmitWriteLaneusedArg(2)as the lane andArg(1)as the value:So after a
v_readlane/v_writelaneloop 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 of0x1fin the output). Fixed tohit = subgroupId == Arg(1),select(hit, Arg(0), Arg(2)). No other code buildsWriteLane.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 fromllvm-mc -mcpu=gfx1013): a scalar loop over the 32 lanes that reverses a vector withv_readlane+v_writelaneat lane31-i(SGPR lane select), copies another one at lanei, plus onev_writelanewith 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)VulkanTestDevice.hpp); review only the last commitImported 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, sov_writelane_b32 v1, s0, 37in 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 asselector % waveSize. Av_readlaneof lane 5 after the write above getss0when the eliminator folds it, but the old value when it doesn't.Masking the lane first fixes both:
v_readlane_b32has the same edge in wave32.Shufflepasses the lane straight toOpGroupNonUniformShuffle, 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.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-02T04:19:31Z.
Thanks, applied:
EmitWriteLanenow masks the lane select withWaveSize() - 1before 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/Shufflefor your follow-up, as you offered.