perf(shader): compute v_pk_add_f16 and v_pk_mul_f16 with native f16 math #3531
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3531"
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-11T12:34:12Z.
What
v_pk_add_f16andv_pk_mul_f16now use native f16 math when the device has it. Each half used to be converted to f32, computed in f32 and rounded back to f16. With native f16 the arithmetic is onef16vec2OpFAddorOpFMul, decoratedNoContraction. The existing NaN,FP16_OVFLand clamp rules still run on the result bits.Background. NVIDIA GPUs have packed f16 add and multiply instructions (
HADD2,HMUL2from Turing on), and so do Intel Xe and AMD RDNA.Float16,RoundingModeRTEandDenormPreservecapabilities.VulkanDeviceadds those only when the device reportsshaderFloat16together with RTE rounding, preserved denormals and preserved signed zero, inf and NaN for f16, the check #1252 introduced.FLOAT_MODEother than f16 denormals kept with round to nearest even. So the native path covers every mode that packed f16 supports.v_pk_fma_f16andv_pk_fmac_f16keep the f32 path with the round-to-odd sum from #2000. Vulkan gives GLSLFmaonly the precision ofOpFMulfollowed byOpFAdd, for f16 as for f32, so a native f16Fmamay round twice (the same reasonv_fma_f64is emulated, #446, and #2410's review).OpFmaKHRfromVK_KHR_shader_fma(shaderFmaFloat16) would guarantee a fused f16 fma; none of the drivers I can test exposes it yet.Before / after on an RTX 4060 Laptop (Ada, driver 591.86). SASS instructions per instruction come from
APS5_PIPELINE_STATSbinary size over a chain of 16 instructions,FLOAT_MODE0xc0:v_pk_add_f16v_pk_mul_f16The other 334 VOP1, VOP2, VOP3, VOP3P and VOPC opcodes in the same sweep compile to the same size. Most of what's left in each instruction is the NaN rule on the result bits.
Runtime on the same GPU, one build: each instruction is compiled once for the device and once for the same target without
Float16(the f32 pathmaintakes), as a dependent chain of 2048 instructions over 16384 groups of 32 threads,FLOAT_MODE0xc0; median of 15 interleaved dispatches. Av_fma_f32chain takes 6.7 ms.v_pk_add_f16v_pk_mul_f16Why the results don't change. Both paths give the correctly rounded f16 result before the NaN,
FP16_OVFLand clamp rules run.OpFAddandOpFMulto be correctly rounded, the module declares round to nearest even and preserved denormals for f16, andNoContractionkeeps the driver from fusing them with anything.0xfe00for an invalid operation.FP16_OVFLturns an infinite result from finite sources into ±0x7bff, asclampF16Overflowdoes in f32.clampF16Bits. Clamping commutes with round to nearest because 0 and 1 are representable, and withDX10_CLAMPany NaN clamps to +0 on both paths.Where it matters. Shaders compiled with packed f16 math. On devices without the three capabilities (Pascal has no
shaderFloat16; lavapipe doesn't preserve f16 denormals) the translation is unchanged.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release,
mainat1c75f4d2. RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790) and lavapipe from Mesa 25.2.8 (the version CI installs; Windows build,GALLIUM_OVERRIDE_CPU_CAPS=avx).agc_driver_packed_float16_native:Float16, and compares every result bit for bit. The shader coversv_pk_add_f16,v_pk_mul_f16,v_pk_fma_f16andv_pk_fmac_f16, withop_sel,op_sel_hi,neg_lo,neg_hiand clamp variants; the fma rows check that the fma forms are unaffected.FLOAT_MODE, and 0xc0 with every combination ofDX10_CLAMP,IEEE_MODEandFP16_OVFL.f16vec2add and mul and nof16vec2Fma, and that the build withoutFloat16uses nof16vec2math. It is skipped on a device without native f16.FP16_OVFLfix on the native path (v_pk_mul_f16gives 0xfc00 where the f32 path gives 0xfbff), and leaving out the bit fixes (v_pk_mul_f16with clamp gives 0xfc00, the unclamped -inf, where the f32 path gives 0).agc_driver_packed_alu,agc_driver_float16_fma_rounding,agc_driver_fp16_overflow,agc_driver_f16_min_max_nanandagc_driver_float16_ternary_clamp) pass on all three devices; the Intel UHD skips the three that needFloat64or interpolation features, lavapipe the native test andagc_driver_interpolation_f16.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 320 tests):agc_driver_flat_loads*,agc_driver_global_loads*,agc_driver_flat_apertures_2,agc_driver_flat_apertures_4(then0xc0000409) andagc_driver_pixel_interlock, which fail the same way onmain.agc_driver_image_plane_address_on_line_implicit_lod, which fails the same way withmain's recompiler.Limits.
v_pk_fma_f16,v_pk_fmac_f16,v_pk_min_f16,v_pk_max_f16, the scalar f16 instructions and the f16 transcendentals still compute in f32. Scalar add, sub and mul follow in a separate PR stacked on this one; f16 min/max is a separate PR.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 MarcZX10 on 2026-10-11T12:36:56Z.
The conventions and relinker checks stop during checkout, before reaching your code: the logs report
fatal: No url found for submodule path '3rdparty/imgui' in .gitmoduleson base1e8fff68. This is the shared main checkout issue tracked by #3511. The PR-specific register-context checks pass; the full Linux and Windows builds are skipped under the repository PR policy.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T16:44:25Z.
I added @jweidenh's scanner fix from #3557's review for this PR's test (f139b34b, with them as author):
FindF16x2Mathreadspirv[offset + 1]for every instruction once the f16 vector type was known, so the module's final one-wordOpFunctionEndread one word past the end.agc_driver_packed_float16_nativestill passes on an RTX 4060 and an Intel UHD (294,912 halves).Runtime on the RTX 4060 (same build, compiled for the device and for the same target without
Float16; chains of 2048 instructions over 16384 groups of 32 threads, median of 15 interleaved dispatches):v_pk_add_f1634.6 → 11.2 ms andv_pk_mul_f1633.3 → 11.3 ms, against 6.7 ms for av_fma_f32chain.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:32:22Z.
Rebased onto current
main(theconventionscheckout failure was the3rdparty/imguigitlink fixed by #3511).Checked on RDNA2 hardware (gfx1035, f16 denormals kept):
v_pk_add_f16,v_pk_mul_f16, theop_sel/negadd and the clamped mul from the test, in the test's nine float modes (DX10_CLAMP×IEEE_MODE×FP16_OVFL), over 3,072 rows covering every pair of 62 edge halves (±0, subnormals,0x7bff, infinities, quiet and signaling NaNs with payloads) plus random ones. The native path on RADV matches the hardware bit for bit on all 110,592 results, as does the f32 path.agc_driver_packed_float16_nativepasses on RADV with the native path active and fails without the change; the shader tests pass. Merging.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:53:56Z.
Rebased onto current
main(theconventionscheckout failure was the3rdparty/imguigitlink fixed by #3511).Checked on RDNA2 hardware (gfx1035, f16 denormals kept):
v_pk_add_f16,v_pk_mul_f16, theop_sel/negadd and the clamped mul from the test, in the test's nine float modes (DX10_CLAMP×IEEE_MODE×FP16_OVFL), over 3,072 rows covering every pair of 62 edge halves (±0, subnormals,0x7bff, infinities, quiet and signaling NaNs with payloads) plus random ones. The native path on RADV matches the hardware bit for bit on all 110,592 results, as does the f32 path.agc_driver_packed_float16_nativepasses on RADV with the native path active and fails without the change; the shader tests pass. Merging.