perf(shader): compute v_dot2_f32_f16 in f32 outside the special cases #2428
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2428"
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-09T19:29:41Z.
What
v_dot2_f32_f16(andv_dot2c_f32_f16) computesa.x * b.x + a.y * b.y + cfrom f16 pairs into f32 with a single rounding. #669 matched it to hardware with f64 arithmetic: f64 TwoSums, round-to-odd and a hand-rounded result. GeForce GPUs run f64 at 1/32 (Pascal, Turing) or 1/64 (Ampere, Ada, Blackwell) of the f32 rate, so on an RTX 4060 onev_dot2_f32_f16costs as much as about 44v_fma_f32. This computes the common case in f32 with the same result and keeps the f64 lowering, unchanged, for the rest.Background
The exact value
a.x * b.x + a.y * b.y + cis rounded once to nearest even (f32), with f32 denormalcflushed; NaN, infinity, invalid-operation and zero-sign results follow the rules inEmitFPDot2F32F16(#669). The products of two halves are exact in f32 (11 × 11 significant bits, and their exponents stay between 2^-48 and 2^32), but the sum of three f32 values generally needs more than one rounding, which is why #669 used f64.After
EmitFPDot2F32F16(SpirvBackend/src/SpirvAlu/SpirvAluEmitterFloat64.cpp) now computes:mantissa << 13plus the exponent bias for normals,mantissa * 2^-24for subnormals), andp = a.x * b.x,q = a.y * b.y(exact,NoContraction);(s, t) = TwoSum(q, c),(u, v) = TwoSum(p, s);w = RO(v + t), the sum of the two errors rounded to odd, emulated in f32: the nearest-even sum and its TwoSum error; when the error is nonzero and the sum's last bit is even, the sum steps one ulp toward the error;r = u + w, rounded to nearest even.This is the correctly rounded sum of three numbers of Boldo and Melquiond ("Emulation of FMA and Correctly Rounded Sums: Proved Algorithms Using Rounding to Odd", IEEE Transactions on Computers 57(4), 2008), the same paper #2000 relies on. It holds when no step overflows or underflows, so it is used only when:
care finite,cis zero (or a denormal, flushed to zero) or at least 2^-100 in magnitude,Then the products are integer multiples of 2^-48 (each half is a multiple of 2^-24) and
cis a multiple of 2^-123, so every value in steps 2 to 4 is zero or a multiple of 2^-123: none can be a denormal, and a driver that flushes f32 denormals by default gets the same result. Everything else (NaN or infinite inputs, a tiny nonzeroc, a zero, tiny or huge result) takes the previous f64 lowering, unchanged, in a branch that is rarely taken. That lowering is emitted once per shader, as aDontInlinefunctiondot2_f32_f16_exactthe branch calls (the way the BDA helpers are), so a shader with manyv_dot2_f32_f16doesn't carry a copy of it per instruction: inlined, it made each instruction 208.5 NVIDIA instructions and 1,755 SPIR-V words instead of 77 and 614.Where it matters
Shaders that use 16-bit dot products (
dot(half2, half2)accumulated in f32). On GeForce the f64 lowering dominates their cost; the f32 path runs at the full f32 rate. Only an Ada GPU (sm_89) was measured.Tested
Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release,
ANYPS5_ENABLE_SPIRV_TOOLS=ON. GPU tests on an NVIDIA GeForce RTX 4060 Laptop GPU (driver 591.86, subgroup 32) and on lavapipe (Mesa 26.2.4 for Windows,GALLIUM_OVERRIDE_CPU_CAPS=avx). Everything below was run onmainatf8c2f072; after rebasing onto8b393b6e, the new test on both devices and the shader tests on the RTX 4060 were run again (the new test passes on both, and all 236 shader tests on the RTX 4060, 1 skipped).Correctness
New
agc_driver_f32_dot2_rounding: 239,680v_dot2_f32_f16cases in one dispatch (FLOAT_MODE0xc0):cbetween 2^-67 and 2^43;c = 0;cset to minus one product or minus their sum, then moved by up to 4 ulps, so the result keeps only the low bits;cof 0, ±1, 2^-24, 2^24, 1 + ulp, -16777215, 2^-100, 2^-126 and just below -2^-100, which produce exact ties, near-ties, and the tinycthat takes the f64 lowering;cof ±0, ±infinity, NaNs, 1 and an f32 denormal.Expected values come from an exact sum in 320-bit fixed point (units of 2^-149) rounded to nearest even on the host, with the NaN (first NaN source, quieted, in the order
a.x,b.x,a.y,b.y,c), invalid (0xffc00000), infinity and zero-sign rules of #669's lowering; cases whose result would be below 2^-100 or at least 2^127 in magnitude are left out, as the reference doesn't model those. It passes on both devices, and so does the f64 lowering ofmain, which checks the expectations against the code that was matched to hardware.Negative checks, each fails the new test on both devices:
f32 dot2 rounding: v_dot2_f32_f16 0x5d8ecbc9, 0x48bacd99, 0x2c7fd8dd is 0x4567d370, expected 0x4567d371;f32 dot2 rounding: v_dot2_f32_f16 0x7c007c00, 0x3c007c00, 0x00000000 is 0x4f800080, expected 0x7f800000.Every shader test (
agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program, 235 tests, includingagc_driver_float16_dot, which runsv_dot2_f32_f16andv_dot2c_f32_f16with negated halves,op_sel, clamp, inline and literal constants and DPP): all pass on the RTX 4060 (1 skipped,agc_driver_storage_cache_budget); on lavapipe 209 pass and 26 are skipped for missing device features.Instructions per op (RTX 4060, not committed)
Method: a wave32 compute program with a dependent chain of 16
v_xor_b32+<op>steps (the xor keeps the compiler from folding the chain), recompiled withFLOAT_MODE0xc0 and created withAPS5_PIPELINE_STATS=1; "Binary Size" / 16 bytes per instruction, compared with the same chain ofv_add_nc_u32steps. This counts the instructions in the binary, including the f64 lowering's call site, not the instructions executed.mainv_dot2_f32_f16: instructionsv_dot2_f32_f16: registersv_dot2_f32_f16: SPIR-V wordsv_dot2c_f32_f16: instructionsThroughput (RTX 4060, not committed)
Method: a dependent chain of 128
v_dot2_f32_f16(the accumulator carries the chain), 16384 work-groups of 32, timed per dispatch including submit and wait, against a chain of 1024v_fma_f32dispatched right before or after it in the same process (the laptop's clocks move too much to compare separate runs), 12 rounds per run, 2 runs per build.v_fma_f32(median, range)mainv_dot2_f32_f16On
mainthe f64 operations dominate:v_pk_fma_f16, emulated with a similar number of f32 instructions, measured 6-8v_fma_f32in the same runs.The measurement programs aren't part of this PR; I can attach them if useful.
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 github-actions[bot] on 2026-10-09T19:29:51Z.
Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T08:40:39Z.
Checked on RADV and on CI's lavapipe (Mesa 25.2.8):
agc_driver_f32_dot2_roundingpasses (239,680 cases) and the rest of the AGC tests are green. Mutating the round-to-odd step to round-to-nearest fails the test on the same case quoted in the description (0x5d8ecbc9, 0x48bacd99, 0x2c7fd8dd). The fast path is only taken where all steps are normal, and the f64 lowering stays for the rest; merging.