perf(shader): compute v_dot4 with packed integer dot products #4061
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4061"
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-11T22:26:52Z.
What
v_dot4_i32_i8,v_dot4c_i32_i8andv_dot4_u32_u8useOpSDot/OpUDoton packed 4×8-bit operands where the device has accelerated integer dot products. Results are unchanged.Background.
integerDotextracts the four bytes of each source, multiplies them pairwise and adds the products to the accumulator. On NVIDIA that is 12.5 SASS perv_dot4_i32_i8, while every NVIDIA GPU since Pascal has a single instruction for it, IDP.4A (dp4a).ptxasemits one instruction for it on sm_61, sm_75, sm_86, sm_89 and sm_120. The compiler doesn't recognise the extract/multiply/add sequence, butOpSDot/OpUDotfromVK_KHR_shader_integer_dot_product(core in Vulkan 1.3) map to it directly.How.
shaderIntegerDotProductand bothintegerDotProduct4x8BitPackedSignedAcceleratedandintegerDotProduct4x8BitPackedUnsignedAccelerated,VulkanDeviceenablesVK_KHR_shader_integer_dot_product. The target then gets theDotProductandDotProductInput4x8BitPackedcapabilities andSPV_KHR_integer_dot_product. The capabilities are also added to the graphics shader validation list.integerDotemits new IR opsSDot4x8/UDot4x8for the 8-bit forms and adds the accumulator withIAddas before. The backend lowers them toOpSDot(signedi32result, bitcast) andOpUDotwithPackedVectorFormat4x8Bit.v_dot2_*and the 4-bitv_dot8_*keep their current lowering; SPIR-V has no packed 2×16 or 8×4 format. Devices without the feature also keep the byte extracts.Before / after on an RTX 4060 Laptop (Ada, driver 591.86):
v_dot4_i32_i8v_dot4c_i32_i8v_dot4_u32_u8SASS per instruction is the
APS5_PIPELINE_STATSbinary size over a chain of 16. No other opcode in the sweep changes size.Run time: 2,048 dependent dot products, each feeding the next one's first source, over 16,384 groups of 32 lanes. Median of 15 interleaved dispatches in one process, with and without the capabilities:
v_dot4_i32_i8v_dot4_u32_u8v_dot2_i32_i16(unchanged, control)Why results don't change.
OpSDot/OpUDotreturn the sum of the four products of the sign- or zero-extended bytes. Its magnitude is at most 4 × 128 × 128, so it is exact in 32 bits. The accumulator is then added with the same wrappingIAddas before.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release,
mainat7c8df1f0. 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_integer_dot:OpSDot/OpUDotin the SPIR-V exactly when the capabilities are present.v_dot4_i32_i8withOpUDotinstead makes it fail (v_dot4_i32_i8 (packed dot product): thread 2 (0xffffffff, 0xffffffff, 0x00000001) is 0x0003f805, expected 0x00000005).agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 319 tests). I left out the BDA tests that hang on driver 591.86 until #3291 lands. I also built with #4021 applied locally, becausemainat7c8df1f0doesn't compilelibkernelon Windows.agc_driver_prepared_shaders_registered_workgroup_sync. That test (added by #2261) fails the same way,expected preparation failure, on a build without this change.agc_driver_ds_swizzleandagc_driver_f32_dot2_rounding(noFloat64; it throws instead of skipping), which fail the same way withmain's recompiler.agc_driver_gated_dispatch_capturefailed once and passed on rerun.Limits.
v_dot8_i32_i4/v_dot8_u32_u4(26 SASS) are unchanged; a packed 4-bit form would need unpacking to bytes first, which I left out.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-11T23:21:02Z.
Which title's shaders use
v_dot4_*? The 4.52 → 0.71 ms figure is for a synthetic chain of 2,048 dot products; a frame time before and after on a title that runs them is what's needed for this to go in.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T23:25:35Z.
None that I know of. I picked
v_dot4_*from an instruction-level comparison: the byte extract/multiply/add lowering against NVIDIA's single IDP.4A. It didn't come from a capture, so the synthetic chain is the only evidence I have.I'll leave this open. If someone finds
v_dot4_*in a title's shaders (throughAPS5_DUMP_SHADERSandagc_shader_replay --asm, for example), a frame time before and after can go here. Until then I agree it shouldn't go in on synthetic numbers alone.Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-12T00:35:10Z.
I looked for
v_dot4_*in what titles here actually submit. I dumped every shader request (compute and all draw stages) from eleven titles on Linux and counted opcodes fromagc_shader_replay --asm: 837 shaders, 316,352 instructions, and nov_dot4_i32_i8orv_dot4_u32_u8(VOP3P 0x16, 0x17), nov_dot4c_i32_i8(VOP2 0x0d) and nov_dot2_*. The only VOP3P opcodes in the whole set are 0x11, 0x12 and 0x20 to 0x22, 97 instructions.These are the shaders each title submitted in a run of one to three minutes: Playroom into gameplay in its first level, Astro Bot for 150 s (1,028 frames, I did not look at which screen it reached), the others to a menu or first scene, and Stray, LEGO Horizon, Hades II, Kena (62 s) and Ratchet & Clank (117 s) stop early on other errors. Later levels are not covered. Draw-stage shaders needed a local one-line change that dumps every draw request, because
APS5_DUMP_SHADERSalone only writes compute requests.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T02:31:59Z.
Thanks @fma-rc, this corpus is exactly what's been missing. With no
v_dot4_*orv_dot2_*in 301,896 instructions from nine titles, this PR doesn't speed up any of them; it would only help a title that does use them. Whether it's still worth merging is the maintainers' call.Could you post an opcode histogram (counts per opcode, wave32 and wave64 separately), or say where the
.reqdumps could be shared? I've been ranking NVIDIA work by SASS cost per instruction without knowing how often each instruction appears, and counts would show which instructions are worth optimising. The ones I'm weighing next:ds_permute_b32(#4181);v_cmp_class_f32/v_cmp_class_f16, and whether the mask operand is a constant;s_buffer_load_dword*;buffer_store_byte/buffer_store_short;v_interp_p2_f16,v_interp_p1ll_f16/v_interp_p1lv_f16(#4171 covers them along with the 33v_fma_mix*);ds_*_u64).Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-12T03:07:01Z.
@karanvora2599 the histogram is here, counts per opcode with wave32 and wave64 in separate columns, plus a per-title file and the script: https://github.com/fma-rc/AnyPS5/tree/evidence/shader-census
Same 837 shaders as above (45 wave32, 792 wave64), 316,352 instructions, 395 distinct family/opcode pairs. Names are the decoder's own table names; scalar,
VINTRPand export instructions have no flat table there, so those 133 rows carry only family and hardware opcode. I can't share the.reqfiles, they are the titles' own shader code.For the ones you listed:
s_buffer_load_dwordand x2 to x16v_cmp_class_f32v_mad_mixlo_f16/v_mad_mixhi_f16ds_permute_b32,v_cmp_class_f16,buffer_store_byte/_short,v_interp_*_f16, anyds_*_u64I did not decode whether the
v_cmp_class_f32mask operand is a constant. The top of the list isv_mul_f32(42,491),v_mac_f32(25,318) andv_mad_f32(22,914).Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-12T04:13:07Z.
I ran the census on a few more games and updated the two comments above and the branch. Ratchet & Clank: Rift Apart (52 shaders, stops at 117 s) and Kena (36 shaders, stops at 62 s) are in now; GTA V and Cult of the Lamb crash here before they submit a shader. The set is 837 shaders (45 wave32, 792 wave64) and 316,352 instructions.
What Ratchet adds that the first nine titles did not have:
ds_min_f32/ds_max_f32v_min3_f16/v_max3_f16v_pk_min_f16/v_pk_max_f16So your note on PR 4212 that no CAS-loop LDS atomic appears no longer holds: Ratchet uses the float min and max. It also has 7 more
v_cmp_class_f32in 5 shaders. Still nov_dot4_*,ds_permute_b32,buffer_store_byte/_short,image_sample_d/_cdorv_interp_*_f16anywhere.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T07:37:07Z.
Closing: no title's shaders run
v_dot4_*(none in fma-rc's 837 shaders from 11 titles), so there is no frame to measure it on. Please reopen when a title uses it.Pull request closed