perf(shader): find the ds_permute_b32 source lane with ballots #4181
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4181"
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 karanvora2599 on 2026-10-12T00:51:31Z.
What
ds_permute_b32finds 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_b32is the forward permute: each active lane sends its data to lane((addr + offset) >> 2) & 31of 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, soEmitPermuteU32looped over all 32 possible senders: 3 shuffles, a compare and a select each, unrolled. On an RTX 4060 that is 144.5 SASS instructions perds_permute_b32when the address changes from one instruction to the next;ds_bpermute_b32, the pull form, is 6.How. A receiving lane
dneeds the set of active lanes whose target isd. WithT_b= the ballot of the active lanes whose target has bitbset, that set iswriters = ballot(active) & M_0 & M_1 & M_2 & M_3 & M_4, whereM_b = T_bif bitbofdis set and~T_botherwise.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 throughSpirvValueEmitContext::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_STATSover a chain of 16,main's emitter against this one in the same tree:ds_permute_b32chainWith 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_b32with 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.
ds_permute_b32, address from the previous resultv_fma_f32(reference)Per instruction,
ds_permute_b32goes from about 8v_fma_f32to about 1.7. Both variants wrote the same results.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release,
mainatd0ca839c(rebased ontod371ed30, 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).agc_driver_ds_permute_lanes: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 skipsagc_driver_ds_permute, because its subgroups are narrower than a wave.agc_driver_ds_permutepasses on the RTX 4060 and the Intel UHD.FindILsbinstead ofFindUMsb) makes both tests fail:agc_driver_ds_permutewithlane 1 permute_offset8 is 0x00001017, expected 0x0000101e, and the new test withwave32 batch 0: lane 1 (address 0x3937c94a, data 0x4fd0e23e, exec 1) is 0x5cd936eb, expected 0x06a0efa9.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.agc_driver_ds_swizzleandagc_driver_f32_dot2_rounding(noFloat64; it throws instead of skipping), which fail the same way without this change.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.
writersis 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.
ds_permute_b32permutes 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
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)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.Pull request closed