perf(shader): skip the tiny product check that a constant factor settles #2020

Merged
karanvora2599 merged 1 commit from upstream-pr/2020 into main 2026-10-10 06:10:35 +00:00
karanvora2599 commented 2026-10-09 05:13:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-09T05:13:19Z.


What

Follow-up to #1850 (#981). With f32 denormals flushed (FLOAT_MODE 0xc0, or no known mode), every f32 multiply and fma checks whether the exact product is below 2^-126, so that a product which rounds up to 2^-126 still flushes as on RDNA: two exponent extracts, a UMulHi of the significands and a chain of compares and selects in flushTinyProduct. NVIDIA reports neither shaderDenormFlushToZeroFloat32 nor shaderDenormPreserveFloat32, so this stays in every shader there.

When one factor is a literal or an inline constant, its exponent settles the check during translation:

  • Exponent field 0, 255 or at least 127 (zero after the input flush, infinity, NaN, or |c| >= 1): the check needs both factors finite and nonzero, and for every normal x, e_x + e_c + carry >= 1 + 127 = 128. The product is never tiny, so the check is not emitted.
  • Significand field 0 (a power of two): the significand is 2^23, so UMulHi(sig_x, 2^23) = sig_x >> 9 <= 0x7fff and the carry is always 0. The UMulHi, its compare and its select are not emitted; the exponent test stays.
  • readOperand folds the abs, neg and input denormal flush of an immediate source into one constant, so the factor reaches flushTinyProduct as BitCastF32(constant). flushF32Denormal folds an immediate the same way. A plain read keeps its IR shape (vector_fused_multiply_add passes unchanged).

Each removed part evaluates to the same constant the current code computes at run time, so results are bit-identical. Registers and other sources are unchanged.

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 (591.86, subgroup 32) and on lavapipe (Mesa 26.2.4 for Windows, llvmpipe, GALLIUM_OVERRIDE_CPU_CAPS=avx as in CI).

  • New vector_constant_factor_tininess: translates v_mul_f32, v_mul_legacy_f32, v_fmamk_f32, v_fmaak_f32 and VOP3 v_fma_f32 with register, inline and literal factors under FLOAT_MODE 0xc0 and 0xf0, and counts the tiny-product compares and UMulHis: none for 2.0, -4.0, 1.0, inf, a denormal literal and integer inline 1; exponents only for 0.5, 0.25 and -|0.5|; the full check for 0x3f7fffff, 1/(2pi), a register pair and the v_fmaak_f32 literal addend. It also checks the folded constant of -|2.0| and of flushed denormal literals. On main it fails (v_mul_f32 v5, 2.0, v7 emits 1 tiny product checks, expected 0).

  • New agc_driver_f32_constant_factor_tininess: eight instructions, each run once with a constant factor and once with the same value in a VGPR (the path #1850 pins against hardware), over 32 rows around 2^-126, specials and ±0 addends, with no float mode, 0xc0, 0x00 (IEEE=1) and 0xf0. Every pair must match bit for bit. Six cells whose exact product is below 2^-126 and stays below it when rounded to 24 bits are pinned to the flushed zero: 0x3f7ffffe * 0x00800000, 0.5 * 0x00ffffff, 0.5 * 0x80ffffff, and v_fmamk_f32 by 0.25 with +0, -0 and a flushed denormal addend. Lane 2, 0x3f7ffffe * 0x00800001 = 2^-126 * (1 - 2^-46), is only compared between the two forms: hardware keeps 0x00800000 there (rounded to 24 bits it is 2^-126, measured by @CouchRiv below), while main's check from #1850 flushes it, and both forms here follow main. The row still shows a skipped check, because a host that flushes after rounding keeps the product with the constant while main's check flushes it with the register. It passes on main too.

  • Negative checks, each reverted:

    • skipping the check from exponent 126: v_mul_f32 v5, 0.5, v7 emits 0 tiny product checks, expected 1 and lane 2 no float mode v_mul_f32 0x3f7ffffe is 0x00800000 with a constant and 0x00000000 with a register.
    • a zero carry for every constant: v_mul_f32 v5, 0x3f7fffff, v7 emits 0 significand products, expected 1 and lane 3 no float mode v_mul_f32 0x3f7ffffe is 0x00000000 with a constant and 0x00fffffd with a register.
    • folding without neg: v_fma_f32 v5, v6, -|2.0|, v7 source 1 is folded to the wrong constant and lane 0 no float mode v_fma_f32 -|2.0| is 0x01000000 with a constant and 0x81000000 with a register.

    Each fails both new tests on both devices.

  • agc_driver_f32_denormal_flush, agc_driver_f32_output_modifier, agc_driver_f32_min_max_nan and vector_fused_multiply_add pass unchanged on both devices.

  • Rebased onto current main (6cc0503d, with #2083): the two new tests, agc_driver_f32_denormal_flush, agc_driver_f32_output_modifier, agc_driver_f32_min_max_nan, agc_driver_f32_arithmetic_nan, vector_fused_multiply_add and vector_mad_legacy pass on both devices. agc_driver and agc_shader_fusion don't build on current main (VulkanTestDevice.hpp includes spirv/unified1/spirv.hpp; #2093 and #2099 fix it).

  • Full ctest before the rebase (f1451396), after cmake --build build and --target libs, serial, --timeout 120, RTX 4060: 492 of 493 pass. windows_import_modules fails on this machine when Python removes its temporary directory (PermissionError: [WinError 5] on a .prx.guest.prx the test has just run); #2227 traces it to Windows Error Reporting keeping the GUI fixture's image open. It runs only the relinker, whose build contains no recompiler sources.

  • Pipeline statistics (APS5_PIPELINE_STATS=1, RTX 4060) of a compute program, not committed, with eight constant products typical of shaders (×2.0, ×0.5, ×255.0, ×1/255, v_fmamk_f32 ×3.0 and ×0.25, ×-1.0, ×4.0) under FLOAT_MODE 0xc0:

    main this PR
    SPIR-V words 3259 2838
    Binary Size 5888 B 5376 B
    Register Count 21 22

    The output is bit-identical (same checksum over all 512 result words). Only ×1/255 keeps the full check.

Touches OperandAccess.cpp like #1940, in other functions.

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:4796285559 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2020). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-09T05:13:19Z. --- ### What Follow-up to #1850 (#981). With f32 denormals flushed (`FLOAT_MODE` 0xc0, or no known mode), every f32 multiply and fma checks whether the exact product is below 2^-126, so that a product which rounds up to 2^-126 still flushes as on RDNA: two exponent extracts, a `UMulHi` of the significands and a chain of compares and selects in `flushTinyProduct`. NVIDIA reports neither `shaderDenormFlushToZeroFloat32` nor `shaderDenormPreserveFloat32`, so this stays in every shader there. When one factor is a literal or an inline constant, its exponent settles the check during translation: - **Exponent field 0, 255 or at least 127** (zero after the input flush, infinity, NaN, or `|c| >= 1`): the check needs both factors finite and nonzero, and for every normal `x`, `e_x + e_c + carry >= 1 + 127 = 128`. The product is never tiny, so the check is not emitted. - **Significand field 0** (a power of two): the significand is 2^23, so `UMulHi(sig_x, 2^23) = sig_x >> 9 <= 0x7fff` and the carry is always 0. The `UMulHi`, its compare and its select are not emitted; the exponent test stays. - `readOperand` folds the abs, neg and input denormal flush of an immediate source into one constant, so the factor reaches `flushTinyProduct` as `BitCastF32(constant)`. `flushF32Denormal` folds an immediate the same way. A plain read keeps its IR shape (`vector_fused_multiply_add` passes unchanged). Each removed part evaluates to the same constant the current code computes at run time, so results are bit-identical. Registers and other sources are unchanged. ### 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 (591.86, subgroup 32) and on lavapipe (Mesa 26.2.4 for Windows, llvmpipe, `GALLIUM_OVERRIDE_CPU_CAPS=avx` as in CI). - New `vector_constant_factor_tininess`: translates `v_mul_f32`, `v_mul_legacy_f32`, `v_fmamk_f32`, `v_fmaak_f32` and VOP3 `v_fma_f32` with register, inline and literal factors under `FLOAT_MODE` 0xc0 and 0xf0, and counts the tiny-product compares and `UMulHi`s: none for 2.0, -4.0, 1.0, inf, a denormal literal and integer inline 1; exponents only for 0.5, 0.25 and `-|0.5|`; the full check for 0x3f7fffff, 1/(2pi), a register pair and the `v_fmaak_f32` literal addend. It also checks the folded constant of `-|2.0|` and of flushed denormal literals. On main it fails (`v_mul_f32 v5, 2.0, v7 emits 1 tiny product checks, expected 0`). - New `agc_driver_f32_constant_factor_tininess`: eight instructions, each run once with a constant factor and once with the same value in a VGPR (the path #1850 pins against hardware), over 32 rows around 2^-126, specials and ±0 addends, with no float mode, 0xc0, 0x00 (IEEE=1) and 0xf0. Every pair must match bit for bit. Six cells whose exact product is below 2^-126 and stays below it when rounded to 24 bits are pinned to the flushed zero: `0x3f7ffffe * 0x00800000`, `0.5 * 0x00ffffff`, `0.5 * 0x80ffffff`, and `v_fmamk_f32` by 0.25 with +0, -0 and a flushed denormal addend. Lane 2, `0x3f7ffffe * 0x00800001` = 2^-126 * (1 - 2^-46), is only compared between the two forms: hardware keeps 0x00800000 there (rounded to 24 bits it is 2^-126, measured by @CouchRiv below), while `main`'s check from #1850 flushes it, and both forms here follow `main`. The row still shows a skipped check, because a host that flushes after rounding keeps the product with the constant while `main`'s check flushes it with the register. It passes on main too. - Negative checks, each reverted: - skipping the check from exponent 126: `v_mul_f32 v5, 0.5, v7 emits 0 tiny product checks, expected 1` and `lane 2 no float mode v_mul_f32 0x3f7ffffe is 0x00800000 with a constant and 0x00000000 with a register`. - a zero carry for every constant: `v_mul_f32 v5, 0x3f7fffff, v7 emits 0 significand products, expected 1` and `lane 3 no float mode v_mul_f32 0x3f7ffffe is 0x00000000 with a constant and 0x00fffffd with a register`. - folding without neg: `v_fma_f32 v5, v6, -|2.0|, v7 source 1 is folded to the wrong constant` and `lane 0 no float mode v_fma_f32 -|2.0| is 0x01000000 with a constant and 0x81000000 with a register`. Each fails both new tests on both devices. - `agc_driver_f32_denormal_flush`, `agc_driver_f32_output_modifier`, `agc_driver_f32_min_max_nan` and `vector_fused_multiply_add` pass unchanged on both devices. - Rebased onto current `main` (6cc0503d, with #2083): the two new tests, `agc_driver_f32_denormal_flush`, `agc_driver_f32_output_modifier`, `agc_driver_f32_min_max_nan`, `agc_driver_f32_arithmetic_nan`, `vector_fused_multiply_add` and `vector_mad_legacy` pass on both devices. `agc_driver` and `agc_shader_fusion` don't build on current `main` (`VulkanTestDevice.hpp` includes `spirv/unified1/spirv.hpp`; #2093 and #2099 fix it). - Full ctest before the rebase (f1451396), after `cmake --build build` and `--target libs`, serial, `--timeout 120`, RTX 4060: 492 of 493 pass. `windows_import_modules` fails on this machine when Python removes its temporary directory (`PermissionError: [WinError 5]` on a `.prx.guest.prx` the test has just run); #2227 traces it to Windows Error Reporting keeping the GUI fixture's image open. It runs only the relinker, whose build contains no recompiler sources. - Pipeline statistics (`APS5_PIPELINE_STATS=1`, RTX 4060) of a compute program, not committed, with eight constant products typical of shaders (×2.0, ×0.5, ×255.0, ×1/255, `v_fmamk_f32` ×3.0 and ×0.25, ×-1.0, ×4.0) under `FLOAT_MODE` 0xc0: | | main | this PR | |---|---|---| | SPIR-V words | 3259 | 2838 | | Binary Size | 5888 B | 5376 B | | Register Count | 21 | 22 | The output is bit-identical (same checksum over all 512 result words). Only ×1/255 keeps the full check. Touches `OperandAccess.cpp` like #1940, in other functions. ### 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
CouchRiv commented 2026-10-09 05:54:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-09T05:54:25Z.


Measured on RDNA2 (gfx1036, Radeon 610M) with tools/hw-oracle, wave32, using words 5-38 of F32ConstantFactorTininess's Code unchanged and its 32 rows. The constant and register forms are equal in every lane in FLOAT_MODE 0xc0 (IEEE=0), 0x00 (IEEE=1) and 0xf0, and six of the seven pinned cells match. Lane 2 does not: v_mul_f32 0x3f7ffffe times 0x00800001 is 0x00800000 on hardware, in 0xc0 and in 0x00, with the constant and with the register. That product is 2^-126 * (1 - 2^-46): below 2^-126 before rounding, exactly 2^-126 after rounding to 24 bits.

More rows (v_mul_f32, v_fma_f32 with a 0 addend and v_mul_legacy_f32 all agree), products 2^-126 * (1 - D/2^47), f32 denormals flushed:

x * y D nearest +inf -inf zero
0x00800001 * 0x3f7ffffe 2 0x00800000 0x00800000 0x00000000 0x00000000
0x80800001 * 0x3f7ffffe 2 0x80800000 0x80000000 0x80800000 0x80000000
0x00f80000 * 0x3f042108 2^22 (tie) 0x00800000 0x00800000 0x00000000 0x00000000
0x00800f95 * 0x3f7fe0d9 6646451 0x00000000 0x00800000 0x00000000 0x00000000
0x00ffffff * 0x3f000000 2^23 (exact) 0x00000000 0x00000000 0x00000000 0x00000000

So the hardware flushes when the product rounded to 24 bits in the current round mode (unbounded exponent) is below 2^-126, which is tininess after rounding. The 6646451 row rules out "round to a denormal, then flush": as a denormal it rounds to 0x00800000 (it does with denormals kept), yet it is flushed. main's check from #1850 tests the product before rounding, so main also gives 0x00000000 for lane 2 and for the nearest-mode rows above that hardware keeps. Your shortcuts are not affected by this: a power-of-two factor gives an exact product, and |c| >= 1 never gives a tiny one. Only the lane-2 pin, and the full check it is compared against, disagree with hardware.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6075201832 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2020#issuecomment-6075201832). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-09T05:54:25Z. --- Measured on RDNA2 (gfx1036, Radeon 610M) with `tools/hw-oracle`, wave32, using words 5-38 of `F32ConstantFactorTininess`'s `Code` unchanged and its 32 rows. The constant and register forms are equal in every lane in FLOAT_MODE 0xc0 (IEEE=0), 0x00 (IEEE=1) and 0xf0, and six of the seven pinned cells match. Lane 2 does not: `v_mul_f32 0x3f7ffffe` times 0x00800001 is 0x00800000 on hardware, in 0xc0 and in 0x00, with the constant and with the register. That product is 2^-126 * (1 - 2^-46): below 2^-126 before rounding, exactly 2^-126 after rounding to 24 bits. More rows (`v_mul_f32`, `v_fma_f32` with a 0 addend and `v_mul_legacy_f32` all agree), products 2^-126 * (1 - D/2^47), f32 denormals flushed: | x * y | D | nearest | +inf | -inf | zero | |---|---|---|---|---|---| | 0x00800001 * 0x3f7ffffe | 2 | 0x00800000 | 0x00800000 | 0x00000000 | 0x00000000 | | 0x80800001 * 0x3f7ffffe | 2 | 0x80800000 | 0x80000000 | 0x80800000 | 0x80000000 | | 0x00f80000 * 0x3f042108 | 2^22 (tie) | 0x00800000 | 0x00800000 | 0x00000000 | 0x00000000 | | 0x00800f95 * 0x3f7fe0d9 | 6646451 | 0x00000000 | 0x00800000 | 0x00000000 | 0x00000000 | | 0x00ffffff * 0x3f000000 | 2^23 (exact) | 0x00000000 | 0x00000000 | 0x00000000 | 0x00000000 | So the hardware flushes when the product rounded to 24 bits in the current round mode (unbounded exponent) is below 2^-126, which is tininess after rounding. The 6646451 row rules out "round to a denormal, then flush": as a denormal it rounds to 0x00800000 (it does with denormals kept), yet it is flushed. `main`'s check from #1850 tests the product before rounding, so `main` also gives 0x00000000 for lane 2 and for the nearest-mode rows above that hardware keeps. Your shortcuts are not affected by this: a power-of-two factor gives an exact product, and `|c| >= 1` never gives a tiny one. Only the lane-2 pin, and the full check it is compared against, disagree with hardware.
karanvora2599 commented 2026-10-09 06:26:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-09T06:26:04Z.


Thanks for measuring it. You're right: the lane 2 pin held main's before-rounding result, not the hardware's. I removed it (a29d2f55). Lane 2 is still compared between the constant and the register form, so a check skipped from exponent 126 still fails it, on the RTX 4060 and on lavapipe (lane 2 no float mode v_mul_f32 0x3f7ffffe is 0x00800000 with a constant and 0x00000000 with a register). The six remaining pins are cases where the product stays below 2^-126 after rounding to 24 bits. The description says this now.

The after-rounding rule has a cheap exact form. Scaling one factor by 2^24 is exact and keeps the product out of the denormal range, so |(x * 2^24) * y| < 2^-102 compares the product rounded to 24 bits in the current mode with 2^-126: two multiplies and a compare instead of the UMulHi chain. It gives the hardware result for all five nearest-mode rows in your table (0x0c800000 for the three kept, 0x0c7fffff for the two flushed). I'd open that as a separate fix(shader) PR on top of this one, citing your measurements, unless you or @adriadam10 already have it in progress.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6075616960 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2020#issuecomment-6075616960). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-09T06:26:04Z. --- Thanks for measuring it. You're right: the lane 2 pin held `main`'s before-rounding result, not the hardware's. I removed it (a29d2f55). Lane 2 is still compared between the constant and the register form, so a check skipped from exponent 126 still fails it, on the RTX 4060 and on lavapipe (`lane 2 no float mode v_mul_f32 0x3f7ffffe is 0x00800000 with a constant and 0x00000000 with a register`). The six remaining pins are cases where the product stays below 2^-126 after rounding to 24 bits. The description says this now. The after-rounding rule has a cheap exact form. Scaling one factor by 2^24 is exact and keeps the product out of the denormal range, so `|(x * 2^24) * y| < 2^-102` compares the product rounded to 24 bits in the current mode with 2^-126: two multiplies and a compare instead of the `UMulHi` chain. It gives the hardware result for all five nearest-mode rows in your table (`0x0c800000` for the three kept, `0x0c7fffff` for the two flushed). I'd open that as a separate `fix(shader)` PR on top of this one, citing your measurements, unless you or @adriadam10 already have it in progress.
adriadam10 commented 2026-10-10 06:08:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:08:02Z.


Checked on Linux (merged onto current main): build + ctest green for vector_constant_factor_tininess, agc_driver_f32_constant_factor_tininess, agc_driver_f32_denormal_flush and vector_fused_multiply_add. The unit test fails without the OperandAccess.cpp change. The execution test passes without it (it only compares constant vs register form), which is expected for a bit-identical optimization. The exponent/significand argument holds (e_x + e_c + carry >= 128 for e_c >= 127; significand 2^23 gives UMulHi <= 0x7fff). Hardware equality of constant and register forms was measured by @CouchRiv on gfx1036. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094503200 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2020#issuecomment-6094503200). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:08:02Z. --- Checked on Linux (merged onto current main): build + ctest green for `vector_constant_factor_tininess`, `agc_driver_f32_constant_factor_tininess`, `agc_driver_f32_denormal_flush` and `vector_fused_multiply_add`. The unit test fails without the `OperandAccess.cpp` change. The execution test passes without it (it only compares constant vs register form), which is expected for a bit-identical optimization. The exponent/significand argument holds (`e_x + e_c + carry >= 128` for `e_c >= 127`; significand 2^23 gives `UMulHi <= 0x7fff`). Hardware equality of constant and register forms was measured by @CouchRiv on gfx1036. 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!2020
No description provided.