perf(shader): round f16 interpolation and mix results in f32 when exact #4171
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4171"
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:34:13Z.
What
v_fma_mixlo/mixhi_f16,v_mad_mixlo/mixhi_f16,v_interp_p2_f16andv_interp_p1ll/p1lv_f16compute their single rounding in f32 when the operands allow it, and fall back to the existing f64 code otherwise. Results are unchanged.Background.
Interpolate(IRFPInterpolateF16/FPInterpolateF32) computes the exact product in f64, adds the third operand with round-to-odd, and rounds once to f16 or f32. On consumer NVIDIA GPUs FP64 runs at 1/32 (Pascal, Turing) or 1/64 (Ampere, Ada, Blackwell) of the f32 rate, so these instructions are the slowest common f16 arithmetic. Av_fma_mixlo_f16chain took 26× as long as av_fma_f32chain on an RTX 4060. #2428 solved the same problem forv_dot2_f32_f16with an f32 fast path and an f64 fallback; this does the same here.How.
x·4098, that is 2·2049), so both partial products with the 11-bit multiplicand are exact in f32. No fused multiply-add is relied on.v_dot2_f32_f16uses: two-sums, the error terms rounded to odd, one last addition. That is Boldo and Melquiond's correctly rounded sum of three floats.EmitF32ToF16BitsRte). Rounding to odd keeps 13 more bits than f16, so the double rounding is exact.writeF16treats both the same: withoutFP16_OVFLboth become infinity, andclampF16Overflowclamps any magnitude from 65520 up to infinity when no source is infinite.DontInlinefunction per result type and NaN mode, asdot2_f32_f16_exactis.Before / after on an RTX 4060 Laptop (Ada, driver 591.86).
Run time: dependent chains over 16,384 groups of 32 lanes, median of 15 interleaved dispatches in one process. For the measurement only, a temporary switch forced the f64 path; it isn't part of the PR.
v_fma_mixlo_f16(f16 sources)v_fma_mix_f32(f16 sources; doesn't useInterpolate, control)v_fma_f32(reference)Per instruction,
v_fma_mixlo_f16goes from about 25× av_fma_f32to about 4×.Static size goes the other way. SASS per instruction from
APS5_PIPELINE_STATSover chains of 16:v_fma_mixlo/mixhi_f16andv_mad_mixlo/mixhi_f16106 → 129,v_fma_mixlo_f16with f32 sources 90 → 114. Registers drop from 40 to 36. Every instruction now carries the inline fast path plus a call to the shared exact function, and a static count includes both even though only one runs.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release,
mainatbca7a29d. 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_fma_mix_f16_exact:v_fma_mixlo_f16with f16, f16, f16 sources, f32, f16, f32, f16, f32, f16 and f32, f32, f32 (the last mostly takes the fallback).FP16_OVFL, and compared with an exact oracle: the product in 80-bit precision, the sum rounded to odd, then rounded to f16.shaderFloat64, which the f64 fallback still needs, so it skips this test as it skipsagc_driver_mix_precision.agc_driver_interpolation_modes(all three ops, both result types, against the 32 RDNA2 hardware rows in 8 float modes) andagc_driver_mix_precisionpass on the RTX 4060 and lavapipe,agc_driver_interpolation_f16on the RTX 4060 (lavapipe and the Intel UHD skip it).interpolation_modesbuilds its module by hand, so there the fallback is inlined rather than called.v_fma_mixlo_f16 (f32, f16, f32): a=0xabf337d7 b=0x4282b6f6 c=0x40fdd000 is 0x000047ee, expected 0x000047ef).agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 338 tests). The BDA tests that hang on driver 591.86 until #3291 lands are left out.agc_driver_prepared_shaders_registered_workgroup_sync, which fails the same way (expected preparation failure) onmain.agc_driver_ds_swizzleandagc_driver_f32_dot2_rounding(noFloat64; it throws instead of skipping), which also fail onmain.registered_workgroup_syncfailure and the intermittentagc_driver_depth_fast_clear_submitsegfault that also happens onmain.Why results don't change. For eligible operands every step before the final rounding is exact or rounds to odd, so the result equals the f64 path's single rounding of the exact value. I checked the algorithm on the CPU against an exact oracle: about 19 million eligible random operand triples, f16 and f32 results, zero mismatches. That oracle computes the exact product in 80-bit precision, rounds the sum to odd, then rounds to f16 or f32. Leaving out either round-to-odd step makes it report mismatches (293 and 90).
Limits.
v_interp_p1ll/p1lv_f16) runs only in pixel shaders, so the random test covers it only through the shared code;agc_driver_interpolation_f16andagc_driver_interpolation_modescheck it against the hardware rows.v_fma_mixwith two f32 multiplicands), and NaN, infinity and extreme magnitudes keep the f64 path.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)mainonce it merges)Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T11:06:36Z.
Review: CHANGES_REQUESTED
Checked the fast path against the diff and ran the build and the AGC/AgcDriver ctest on Linux (RADV):
agc_driver_fma_mix_f16_exactpasses, and rounding the last f16 addition to nearest instead of to odd makes it fail (0x47ee, expected0x47ef), so the test does pin the double rounding. The algorithm itself looks right.One blocker:
InterpolateinSpirvAluEmitterFloat64.cppinto aDontInlinefunction emitted once per module, and touchesSpirvEmitterState.hppnext to yourinterpolateExactFunctions. The two conflict, and the overlap isn't separable. Please list #4932 under Depends on and rebase once it merges (or tell us if you'd rather it be the other way round), then rerunagc_driver_fma_mix_f16_exactandagc_driver_interpolation_modeson the rebased tree.Minor, for the same push:
x·4097, but0x45801000is 4098 (2·2049). The split is still exact (13 + 11 bits against a ≤11-bit multiplicand), so only the description needs fixing.SpirvAluEmitterFloat64.cpp:770adds a second blank line.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-13T21:55:05Z.
Thanks @adriadam10. Fine to go after #4932; it's now listed under Depends on. Once it merges I'll rebase, putting the f32 fast path in front of its shared
Interpolatefunction, and rerunagc_driver_fma_mix_f16_exactandagc_driver_interpolation_modeson the rebased tree.x·4098(2·2049), with the 13 + 11-bit split.SpirvAluEmitterFloat64.cpp:770goes with that rebase.SpirvEmitterState.hpp,SpirvEmitterHelpers.hppandSpirvModuleEmitter.cpp, so whichever merges second keeps both.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:39:03Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed