perf(shader): check f32 product tininess in one multiply to nearest even #3278

Merged
oneandonlydean merged 1 commit from upstream-pr/3278 into main 2026-10-11 06:09:09 +00:00
oneandonlydean commented 2026-10-11 04:54:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T04:54:44Z.


Carried unchanged from @AlxcBTW's fork (AlxcBTW/AnyPS5#35, 4e7511dc, authorship kept); the intake pause closes PRs opened from that account.

What

#2839 decides f32 product tininess after rounding in all four f32 round modes. It does this from the exponents and a UMulHi/IMul of the significands, and builds the flushed value from them. To nearest even (no known float mode, and the 0xc0 most titles use), this replaces that with one multiply. One factor times 2^64, which is exact below 2^64, times the other factor rounds the exact product once to 24 bits in the normal range, and 0 < |that| < 2^-62 is tininess after rounding. A product that rounds up to 2^-126 is then not tiny and keeps the emitted product, which is 2^-126 there. The three directed modes and the before-rounding check of v_mad_legacy_f32 keep #2839's check. Its round-to-nearest branches are removed as unreachable.

  • The flush of v_mul_f32 v5, v6, v7 takes 12 IR instructions instead of 34, counted as the instructions the flush select needs beyond the product. vector_constant_factor_tininess pins 12 to nearest and 36 for the directed path of the same instruction. It also checks the shape of every row: the rounded 2^-62 compare to nearest, and #2839's exponent and significand checks toward +infinity, -infinity and zero and for v_mad_legacy_f32.
  • The f32 denormals entry in TechnicalDebt says which rounding the check inherits. The multiply is a plain OpFMul like the product, and no f32 rounding mode is requested, so the check relies on the driver rounding f32 to nearest even, as the products do.
  • F32ConstantFactorTininess names 0x3f7ffffe * 0x00800001 (lane 2) as the row a check before rounding flushes, which keeps the emitted 0x00800000.

Numbers

Main 77bd928a3 against main 77bd928a3 with this commit (same diff), Windows 11, WinLibs GCC 15.2.0, Release, RTX 5070 Laptop (NVIDIA 616.92). Another process was sharing the GPU during the runs, so the GPU ran in two speed states. Each comparison below is within one state.

Compile. 155 distinct compute recompile requests from Sword of the Sea (request version 15), replayed with agc_shader_replay --spv:

  • 39 shaders contain checked products, 8,475 in total. The other 116 come out the same size as on main.
  • SPIR-V words: 8,936,661 → 8,008,172 (-10.4%) over the 39 shaders, 14,003,387 → 13,074,898 (-6.6%) over all 155.
  • NVIDIA vkCreateComputePipelines for the six with the most checked products, fresh driver shader cache per run, median of 3 runs in alternating order:
Shader Checked products SPIR-V words Pipeline, main Pipeline, this PR Delta
1c004a6000 1,022 938,733 → 823,054 63.0 s 53.1 s -16%
1b403c8000 698 587,804 → 510,073 27.2 s 26.4 s -3% (runs 25.2/34.7/27.2 against 25.1/26.4/38.6 s)
1b004a9800 490 368,353 → 314,579 11.8 s 7.9 s -33%
1a402d1000 478 320,162 → 267,717 8.4 s 5.7 s -32%
1a80147800 362 319,130 → 278,732 16.8 s 12.1 s -28%
18c00b4000 172 158,334 → 139,159 4.9 s 3.9 s -22%
Six together 132.1 s 109.0 s -17.5%

Runtime. The kernel is a loop of 8,192 iterations with 16 checked products each: two v_fma_f32 and two v_mul_f32 chains, unrolled 4 times, with factors in registers. It runs in float mode 0xc0 over 8,192 groups of 32 lanes. Its SPIR-V is 8,914 → 7,074 words, and the outputs are identical in every run.

main this PR Delta
GPU time per dispatch, driver timestamps (APS5_PROFILE_GPU, per-program average per 10 s window), GPU at full speed 126.8-129.9 ms 69.0-71.6 ms -45%
The same, GPU shared with the other process 244.5-246.1 ms 133.3-134.5 ms -45%
Dispatch to idle, 20 interleaved process pairs, fastest of 9 dispatches per process, median 114.9 ms 66.6 ms -42%

Equivalence with #2839 to nearest even

  • Model: both checks, written bit for bit, agree with each other and with an exact rational reference on 1,074,977,546 operand pairs. The pairs, all in both operand orders, are every 24-bit significand of one factor at both rounding thresholds (with and without carry, ±2 neighbours, three binades, three exponent splits), 160M random pairs biased around 2^-126 and fully random, and specials. 52,335,224 of them round up to 2^-126.
  • GPU, RTX 5070 Laptop: #2839's own TinyCode and MixedTinyCode shaders were run over 165,298 and 164,008 generated rows (boundary rows as above, f16 factors for the mixed forms, mostly zero addends) in six float modes (none, 0xc0, 0x00, 0xc1, 0xc2, 0xc3). The outputs of all 18 instructions are bit-identical with #2839's lowering and with this one. v_mul_f32 also matches the exact reference on every row, about 34,000 of them kept at 2^-126 per mode. The same sweep with the compare off by one (<= 2^-62) differs on 33,995 rows per mode, so it does see the boundary.

Tested

Windows 11, WinLibs GCC 15.2.0, Release, NVIDIA RTX 5070 Laptop only (AMD Vulkan driver hidden).

  • vector_constant_factor_tininess, agc_driver_f32_constant_factor_tininess and agc_driver_f32_denormal_flush (#2839's, in all its modes and with mixed sources) pass.
  • Fork CI run 38096876119: Windows 756/756. Linux 749/752, with the same three failures as main's own run (agc_unit_shadow, agc_driver_flat_apertures timing out, agc_driver_image_packed16_load).
  • tools/check_conventions.py --base origin/main and git diff --check pass.
  • No title was run. The compile numbers come from replayed requests, and the runtime numbers from the kernel above.

Checklist

  • Based on current main; no other open PR implements the same functions (based on 6dcfc5b64, which includes #2839; merges cleanly into 77bd928a3)
  • 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 (builds on #2839, merged)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4816681621 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3278). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T04:54:44Z. --- Carried unchanged from @AlxcBTW's fork (AlxcBTW/AnyPS5#35, `4e7511dc`, authorship kept); the intake pause closes PRs opened from that account. ### What #2839 decides f32 product tininess after rounding in all four f32 round modes. It does this from the exponents and a `UMulHi`/`IMul` of the significands, and builds the flushed value from them. To nearest even (no known float mode, and the 0xc0 most titles use), this replaces that with one multiply. One factor times 2^64, which is exact below 2^64, times the other factor rounds the exact product once to 24 bits in the normal range, and `0 < |that| < 2^-62` is tininess after rounding. A product that rounds up to 2^-126 is then not tiny and keeps the emitted product, which is 2^-126 there. The three directed modes and the before-rounding check of `v_mad_legacy_f32` keep #2839's check. Its round-to-nearest branches are removed as unreachable. - The flush of `v_mul_f32 v5, v6, v7` takes 12 IR instructions instead of 34, counted as the instructions the flush select needs beyond the product. `vector_constant_factor_tininess` pins 12 to nearest and 36 for the directed path of the same instruction. It also checks the shape of every row: the rounded 2^-62 compare to nearest, and #2839's exponent and significand checks toward +infinity, -infinity and zero and for `v_mad_legacy_f32`. - The f32 denormals entry in TechnicalDebt says which rounding the check inherits. The multiply is a plain `OpFMul` like the product, and no f32 rounding mode is requested, so the check relies on the driver rounding f32 to nearest even, as the products do. - `F32ConstantFactorTininess` names `0x3f7ffffe * 0x00800001` (lane 2) as the row a check before rounding flushes, which keeps the emitted `0x00800000`. ### Numbers Main 77bd928a3 against main 77bd928a3 with this commit (same diff), Windows 11, WinLibs GCC 15.2.0, Release, RTX 5070 Laptop (NVIDIA 616.92). Another process was sharing the GPU during the runs, so the GPU ran in two speed states. Each comparison below is within one state. **Compile.** 155 distinct compute recompile requests from Sword of the Sea (request version 15), replayed with `agc_shader_replay --spv`: - 39 shaders contain checked products, 8,475 in total. The other 116 come out the same size as on main. - SPIR-V words: 8,936,661 → 8,008,172 (**-10.4%**) over the 39 shaders, 14,003,387 → 13,074,898 (-6.6%) over all 155. - NVIDIA `vkCreateComputePipelines` for the six with the most checked products, fresh driver shader cache per run, median of 3 runs in alternating order: | Shader | Checked products | SPIR-V words | Pipeline, main | Pipeline, this PR | Delta | | --- | --- | --- | --- | --- | --- | | 1c004a6000 | 1,022 | 938,733 → 823,054 | 63.0 s | 53.1 s | -16% | | 1b403c8000 | 698 | 587,804 → 510,073 | 27.2 s | 26.4 s | -3% (runs 25.2/34.7/27.2 against 25.1/26.4/38.6 s) | | 1b004a9800 | 490 | 368,353 → 314,579 | 11.8 s | 7.9 s | -33% | | 1a402d1000 | 478 | 320,162 → 267,717 | 8.4 s | 5.7 s | -32% | | 1a80147800 | 362 | 319,130 → 278,732 | 16.8 s | 12.1 s | -28% | | 18c00b4000 | 172 | 158,334 → 139,159 | 4.9 s | 3.9 s | -22% | | **Six together** | | | **132.1 s** | **109.0 s** | **-17.5%** | **Runtime.** The kernel is a loop of 8,192 iterations with 16 checked products each: two `v_fma_f32` and two `v_mul_f32` chains, unrolled 4 times, with factors in registers. It runs in float mode 0xc0 over 8,192 groups of 32 lanes. Its SPIR-V is 8,914 → 7,074 words, and the outputs are identical in every run. | | main | this PR | Delta | | --- | --- | --- | --- | | GPU time per dispatch, driver timestamps (`APS5_PROFILE_GPU`, per-program average per 10 s window), GPU at full speed | 126.8-129.9 ms | 69.0-71.6 ms | **-45%** | | The same, GPU shared with the other process | 244.5-246.1 ms | 133.3-134.5 ms | -45% | | Dispatch to idle, 20 interleaved process pairs, fastest of 9 dispatches per process, median | 114.9 ms | 66.6 ms | -42% | ### Equivalence with #2839 to nearest even - **Model:** both checks, written bit for bit, agree with each other and with an exact rational reference on 1,074,977,546 operand pairs. The pairs, all in both operand orders, are every 24-bit significand of one factor at both rounding thresholds (with and without carry, ±2 neighbours, three binades, three exponent splits), 160M random pairs biased around 2^-126 and fully random, and specials. 52,335,224 of them round up to 2^-126. - **GPU, RTX 5070 Laptop:** #2839's own `TinyCode` and `MixedTinyCode` shaders were run over 165,298 and 164,008 generated rows (boundary rows as above, f16 factors for the mixed forms, mostly zero addends) in six float modes (none, 0xc0, 0x00, 0xc1, 0xc2, 0xc3). The outputs of all 18 instructions are **bit-identical** with #2839's lowering and with this one. `v_mul_f32` also matches the exact reference on every row, about 34,000 of them kept at 2^-126 per mode. The same sweep with the compare off by one (`<= 2^-62`) differs on 33,995 rows per mode, so it does see the boundary. ### Tested Windows 11, WinLibs GCC 15.2.0, Release, NVIDIA RTX 5070 Laptop only (AMD Vulkan driver hidden). - `vector_constant_factor_tininess`, `agc_driver_f32_constant_factor_tininess` and `agc_driver_f32_denormal_flush` (#2839's, in all its modes and with mixed sources) pass. - Fork CI [run 38096876119](https://github.com/AlxcBTW/AnyPS5/actions/runs/38096876119): Windows 756/756. Linux 749/752, with the same three failures as main's own run (`agc_unit_shadow`, `agc_driver_flat_apertures` timing out, `agc_driver_image_packed16_load`). - `tools/check_conventions.py --base origin/main` and `git diff --check` pass. - No title was run. The compile numbers come from replayed requests, and the runtime numbers from the kernel above. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (based on 6dcfc5b64, which includes #2839; merges cleanly into 77bd928a3) - [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 (builds on #2839, merged) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!3278
No description provided.