perf(shader): compute v_dot2_f32_f16 in f32 outside the special cases #2428

Merged
karanvora2599 merged 2 commits from upstream-pr/2428 into main 2026-10-11 08:40:43 +00:00
karanvora2599 commented 2026-10-09 19:29:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-09T19:29:41Z.


What

v_dot2_f32_f16 (and v_dot2c_f32_f16) computes a.x * b.x + a.y * b.y + c from 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 one v_dot2_f32_f16 costs as much as about 44 v_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 + c is rounded once to nearest even (f32), with f32 denormal c flushed; NaN, infinity, invalid-operation and zero-sign results follow the rules in EmitFPDot2F32F16 (#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:

  1. the four halves decoded to f32 exactly (mantissa << 13 plus the exponent bias for normals, mantissa * 2^-24 for subnormals), and p = a.x * b.x, q = a.y * b.y (exact, NoContraction);
  2. (s, t) = TwoSum(q, c), (u, v) = TwoSum(p, s);
  3. 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;
  4. 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:

  • all four halves and c are finite,
  • c is zero (or a denormal, flushed to zero) or at least 2^-100 in magnitude,
  • and the result is at least 2^-100 and below 2^127 in magnitude.

Then the products are integer multiples of 2^-48 (each half is a multiple of 2^-24) and c is 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 nonzero c, 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 a DontInline function dot2_f32_f16_exact the branch calls (the way the BDA helpers are), so a shader with many v_dot2_f32_f16 doesn'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 on main at f8c2f072; after rebasing onto 8b393b6e, 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,680 v_dot2_f32_f16 cases in one dispatch (FLOAT_MODE 0xc0):

    • 60,000 random half pairs (every exponent, subnormals included) with a random c between 2^-67 and 2^43;
    • 30,000 with c = 0;
    • 60,000 cancellation cases, with c set to minus one product or minus their sum, then moved by up to 4 ulps, so the result keeps only the low bits;
    • 81,920 structured cases from the halves 1, 2, 0.5, 2^-14, 2^-24, 65504 and the neighbours of 1, with and without one factor negated, against c of 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 tiny c that takes the f64 lowering;
    • 8,000 special cases: infinities, quiet and signaling NaNs, ±0 and subnormal halves against c of ±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 of main, which checks the expectations against the code that was matched to hardware.

  • Negative checks, each fails the new test on both devices:

    • the errors added with round-to-nearest instead of round-to-odd (the double rounding the algorithm avoids): f32 dot2 rounding: v_dot2_f32_f16 0x5d8ecbc9, 0x48bacd99, 0x2c7fd8dd is 0x4567d370, expected 0x4567d371;
    • the f32 path for every input (no f64 fallback): 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, including agc_driver_float16_dot, which runs v_dot2_f32_f16 and v_dot2c_f32_f16 with 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 with FLOAT_MODE 0xc0 and created with APS5_PIPELINE_STATS=1; "Binary Size" / 16 bytes per instruction, compared with the same chain of v_add_nc_u32 steps. This counts the instructions in the binary, including the f64 lowering's call site, not the instructions executed.

main this PR
v_dot2_f32_f16: instructions 134 77
v_dot2_f32_f16: registers 80 59
v_dot2_f32_f16: SPIR-V words 1,114 614
v_dot2c_f32_f16: instructions 131.5 80.5

Throughput (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 1024 v_fma_f32 dispatched 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.

time per op, relative to one v_fma_f32 (median, range) main this PR
v_dot2_f32_f16 44.5 (31.8-60.0) 2.0 (1.6-4.1)

On main the f64 operations dominate: v_pk_fma_f16, emulated with a similar number of f32 instructions, measured 6-8 v_fma_f32 in the same runs.

The measurement programs aren't part of this PR; I can attach them if useful.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4804953687 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2428). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-09T19:29:41Z. --- ### What `v_dot2_f32_f16` (and `v_dot2c_f32_f16`) computes `a.x * b.x + a.y * b.y + c` from 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 one `v_dot2_f32_f16` costs as much as about 44 `v_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 + c` is rounded once to nearest even (f32), with f32 denormal `c` flushed; NaN, infinity, invalid-operation and zero-sign results follow the rules in `EmitFPDot2F32F16` (#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: 1. the four halves decoded to f32 exactly (`mantissa << 13` plus the exponent bias for normals, `mantissa * 2^-24` for subnormals), and `p = a.x * b.x`, `q = a.y * b.y` (exact, `NoContraction`); 2. `(s, t) = TwoSum(q, c)`, `(u, v) = TwoSum(p, s)`; 3. `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; 4. `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: - all four halves and `c` are finite, - `c` is zero (or a denormal, flushed to zero) or at least 2^-100 in magnitude, - and the result is at least 2^-100 and below 2^127 in magnitude. Then the products are integer multiples of 2^-48 (each half is a multiple of 2^-24) and `c` is 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 nonzero `c`, 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 a `DontInline` function `dot2_f32_f16_exact` the branch calls (the way the BDA helpers are), so a shader with many `v_dot2_f32_f16` doesn'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 on `main` at f8c2f072; after rebasing onto 8b393b6e, 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,680 `v_dot2_f32_f16` cases in one dispatch (`FLOAT_MODE` 0xc0): - 60,000 random half pairs (every exponent, subnormals included) with a random `c` between 2^-67 and 2^43; - 30,000 with `c = 0`; - 60,000 cancellation cases, with `c` set to minus one product or minus their sum, then moved by up to 4 ulps, so the result keeps only the low bits; - 81,920 structured cases from the halves 1, 2, 0.5, 2^-14, 2^-24, 65504 and the neighbours of 1, with and without one factor negated, against `c` of 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 tiny `c` that takes the f64 lowering; - 8,000 special cases: infinities, quiet and signaling NaNs, ±0 and subnormal halves against `c` of ±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 of `main`, which checks the expectations against the code that was matched to hardware. - Negative checks, each fails the new test on both devices: - the errors added with round-to-nearest instead of round-to-odd (the double rounding the algorithm avoids): `f32 dot2 rounding: v_dot2_f32_f16 0x5d8ecbc9, 0x48bacd99, 0x2c7fd8dd is 0x4567d370, expected 0x4567d371`; - the f32 path for every input (no f64 fallback): `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, including `agc_driver_float16_dot`, which runs `v_dot2_f32_f16` and `v_dot2c_f32_f16` with 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 with `FLOAT_MODE` 0xc0 and created with `APS5_PIPELINE_STATS=1`; "Binary Size" / 16 bytes per instruction, compared with the same chain of `v_add_nc_u32` steps. This counts the instructions in the binary, including the f64 lowering's call site, not the instructions executed. | | `main` | this PR | |---|---|---| | `v_dot2_f32_f16`: instructions | 134 | 77 | | `v_dot2_f32_f16`: registers | 80 | 59 | | `v_dot2_f32_f16`: SPIR-V words | 1,114 | 614 | | `v_dot2c_f32_f16`: instructions | 131.5 | 80.5 | **Throughput** (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 1024 `v_fma_f32` dispatched 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. | time per op, relative to one `v_fma_f32` (median, range) | `main` | this PR | |---|---|---| | `v_dot2_f32_f16` | 44.5 (31.8-60.0) | 2.0 (1.6-4.1) | On `main` the f64 operations dominate: `v_pk_fma_f16`, emulated with a similar number of f32 instructions, measured 6-8 `v_fma_f32` in the same runs. The measurement programs aren't part of this PR; I can attach them if useful. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-09 19:29:51 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6087825560 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2428#issuecomment-6087825560). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) 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.
adriadam10 commented 2026-10-11 08:40:39 +00:00 (Migrated from github.com)

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_rounding passes (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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107223859 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2428#issuecomment-6107223859). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T08:40:39Z. --- Checked on RADV and on CI's lavapipe (Mesa 25.2.8): `agc_driver_f32_dot2_rounding` passes (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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!2428
No description provided.