perf(shader): drop the output flush of f32 products #4539

Closed
karanvora2599 wants to merge 1 commit from upstream-pr/4539 into main
karanvora2599 commented 2026-10-12 15:05:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T15:05:20Z.


What

DenormalFlushEliminator now recognizes a v_mul_f32 or v_mul_legacy_f32 result as never being an f32 denormal, so the output flush that writeOperand adds to it is removed. Results are unchanged.

This is stacked on #4287, whose NaN select it reads. Only the last commit is new. #4490 and #4737 add other cases to the same switch, and whichever lands second rebases. It first sat on #4322, which is closed; this version doesn't need it.

Background. In a FLOAT_MODE that flushes f32 denormals, writeOperand flushes every f32 result: a select on the exponent bits. #2103 removes such a flush when the value provably can't be a denormal, but it had no rule for products. In a v_mul_f32, the flush that a tiny product needs (#2839) is already done before the result is written, so the output flush is redundant. Yet it stayed on every multiply, the most common instruction in titles: 13.4% of the instructions in fma-rc's 11-title census, and 17% of Demon's Souls' by Raghav314's count in #4287.

How. Three facts, each local to one value:

  • The tiny-product flush (flushTinyProduct, round to nearest even, no addend) returns a signed zero or a product that isn't tiny after rounding. That product is at least 2^-126 in magnitude, or ±0 when the host flushes denormal results itself. Either way it's never a denormal. The select is flagged SelectFlags::notDenormal. An fma doesn't get the flag, because its addend can cancel the result into a denormal.
  • A product by a constant that is ±0 or at least 1 in magnitude gets no tiny flush (keepsProductOutOfTinyRange). If the other factor is denormal-free, the rounded product is at least that factor's magnitude, or ±0, infinity or NaN, so it isn't a denormal either.
  • The NaN select in #4287's nanResultF32, select(isnan(c), c or c | quiet bit, fallback), takes its first arm only when that arm is a NaN. So it's as denormal-free as its fallback. This is recognized from the IR pattern, with no flag.

With these, a multiply's result is denormal-free, and its output flush is removed like any other redundant flush.

Before / after on an RTX 4060 Laptop (Ada, driver 591.86), FLOAT_MODE 0xc0, compared with #4287. A temporary switch, not part of the PR, turned the new rules off for the comparison.

A dependent chain of 2048 instructions, 16384 workgroups, median of interleaved runs in one process:

Chain Without With
v_mul_f32 5.59 ms 5.10 ms (−9%)
v_mul_f32, NaN inputs 5.67 ms 5.32 ms
v_fma_f32 6.77 ms 6.74 ms
v_add_f32 3.88 ms 4.00 ms

Only the multiply's SPIR-V changes; the fma and add rows are noise. In NVIDIA's compiled code (VK_KHR_pipeline_executable_properties), a v_mul_f32 goes from 16.5 to 14.5 SASS instructions, and v_mul_legacy_f32 from 20.0 to 18.0. The other 88 ops in the sweep compile to the same size.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, Release, main at d146ec1b plus #4287. 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_denormal_flush_elimination has a case for each rule: the flagged tiny flush, the NaN select, and the constant factor. Each case has a negative counterpart that must keep its flush:
    • an unflagged tiny-flush select;
    • a NaN select with an unknown fallback, or with a first arm other than the tested value;
    • a constant factor of 0.5.
  • agc_driver_f32_denormal_flush gets rows where a nonzero addend cancels a normal product into an exact denormal, for example 0x00800001 × 1.0 − 2^-126 = 2^-149. In every mode that flushes f32 denormals, AMD returns a signed zero for v_fma_f32, v_fmac_f32, v_fma_mix_f32, v_mad_legacy_f32 and v_mac_legacy_f32. No existing row covered this.
  • Negative check: flagging the fma's tiny flush as notDenormal too, so the fma loses its output flush, makes lavapipe fail those rows: v_fma_f32 v12, v4, v5, v6 is 0x00000001, expected 0x00000000. lavapipe keeps f32 denormal results. The RTX 4060 and the Intel UHD flush them in hardware in this mode, so only lavapipe catches it. Before the new rows, the 41 f32 and modifier tests all passed that broken build.
  • The 41 f32 and modifier execution tests (agc_driver_f32_*, agc_driver_fma_*, agc_driver_vop*, agc_denormal*, agc_driver_division_* and others) pass on the RTX 4060.
  • Shader tests (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.
    • RTX 4060: all pass.
    • Intel UHD: all pass or skip for missing features, except agc_driver_ds_swizzle and agc_driver_f32_dot2_rounding (no Float64), which fail the same way without this change.
    • lavapipe 25.2.8: all pass or skip for missing features, except the intermittent agc_driver_depth_fast_clear_submit segfault that also happens on main.
    • The new agc_driver_f32_denormal_flush rows pass on all three devices.

Why results don't change. A flush is removed only when its input can't be a denormal, which is the eliminator's existing contract. Each of the three facts depends only on one value and its operands, and holds whether the host keeps or flushes denormal results.

Limits.

  • I haven't run it on AMD hardware.
  • v_fma_f32 and v_add_f32 keep their output flush; both can produce exact denormals.
  • Directed f32 rounding modes don't get the flag.

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: #4287
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4831265596 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4539). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-12T15:05:20Z. --- ### What `DenormalFlushEliminator` now recognizes a `v_mul_f32` or `v_mul_legacy_f32` result as never being an f32 denormal, so the output flush that `writeOperand` adds to it is removed. Results are unchanged. This is stacked on #4287, whose NaN select it reads. Only the last commit is new. #4490 and #4737 add other cases to the same switch, and whichever lands second rebases. It first sat on #4322, which is closed; this version doesn't need it. **Background.** In a `FLOAT_MODE` that flushes f32 denormals, `writeOperand` flushes every f32 result: a select on the exponent bits. #2103 removes such a flush when the value provably can't be a denormal, but it had no rule for products. In a `v_mul_f32`, the flush that a tiny product needs (#2839) is already done before the result is written, so the output flush is redundant. Yet it stayed on every multiply, the most common instruction in titles: 13.4% of the instructions in fma-rc's 11-title census, and 17% of Demon's Souls' by Raghav314's count in #4287. **How.** Three facts, each local to one value: - **The tiny-product flush** (`flushTinyProduct`, round to nearest even, no addend) returns a signed zero or a product that isn't tiny after rounding. That product is at least 2^-126 in magnitude, or ±0 when the host flushes denormal results itself. Either way it's never a denormal. The select is flagged `SelectFlags::notDenormal`. An fma doesn't get the flag, because its addend can cancel the result into a denormal. - **A product by a constant** that is ±0 or at least 1 in magnitude gets no tiny flush (`keepsProductOutOfTinyRange`). If the other factor is denormal-free, the rounded product is at least that factor's magnitude, or ±0, infinity or NaN, so it isn't a denormal either. - **The NaN select** in #4287's `nanResultF32`, `select(isnan(c), c or c | quiet bit, fallback)`, takes its first arm only when that arm is a NaN. So it's as denormal-free as its fallback. This is recognized from the IR pattern, with no flag. With these, a multiply's result is denormal-free, and its output flush is removed like any other redundant flush. **Before / after** on an RTX 4060 Laptop (Ada, driver 591.86), `FLOAT_MODE` 0xc0, compared with #4287. A temporary switch, not part of the PR, turned the new rules off for the comparison. A dependent chain of 2048 instructions, 16384 workgroups, median of interleaved runs in one process: | Chain | Without | With | |---|---|---| | `v_mul_f32` | 5.59 ms | **5.10 ms** (−9%) | | `v_mul_f32`, NaN inputs | 5.67 ms | 5.32 ms | | `v_fma_f32` | 6.77 ms | 6.74 ms | | `v_add_f32` | 3.88 ms | 4.00 ms | Only the multiply's SPIR-V changes; the fma and add rows are noise. In NVIDIA's compiled code (`VK_KHR_pipeline_executable_properties`), a `v_mul_f32` goes from 16.5 to 14.5 SASS instructions, and `v_mul_legacy_f32` from 20.0 to 18.0. The other 88 ops in the sweep compile to the same size. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, Release, `main` at d146ec1b plus #4287. 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_denormal_flush_elimination` has a case for each rule: the flagged tiny flush, the NaN select, and the constant factor. Each case has a negative counterpart that must keep its flush: - an unflagged tiny-flush select; - a NaN select with an unknown fallback, or with a first arm other than the tested value; - a constant factor of 0.5. - `agc_driver_f32_denormal_flush` gets rows where a nonzero addend cancels a normal product into an exact denormal, for example `0x00800001 × 1.0 − 2^-126 = 2^-149`. In every mode that flushes f32 denormals, AMD returns a signed zero for `v_fma_f32`, `v_fmac_f32`, `v_fma_mix_f32`, `v_mad_legacy_f32` and `v_mac_legacy_f32`. No existing row covered this. - Negative check: flagging the fma's tiny flush as `notDenormal` too, so the fma loses its output flush, makes lavapipe fail those rows: `v_fma_f32 v12, v4, v5, v6 is 0x00000001, expected 0x00000000`. lavapipe keeps f32 denormal results. The RTX 4060 and the Intel UHD flush them in hardware in this mode, so only lavapipe catches it. Before the new rows, the 41 f32 and modifier tests all passed that broken build. - The 41 f32 and modifier execution tests (`agc_driver_f32_*`, `agc_driver_fma_*`, `agc_driver_vop*`, `agc_denormal*`, `agc_driver_division_*` and others) pass on the RTX 4060. - Shader tests (`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. - RTX 4060: all pass. - Intel UHD: all pass or skip for missing features, except `agc_driver_ds_swizzle` and `agc_driver_f32_dot2_rounding` (no `Float64`), which fail the same way without this change. - lavapipe 25.2.8: all pass or skip for missing features, except the intermittent `agc_driver_depth_fast_clear_submit` segfault that also happens on `main`. - The new `agc_driver_f32_denormal_flush` rows pass on all three devices. **Why results don't change.** A flush is removed only when its input can't be a denormal, which is the eliminator's existing contract. Each of the three facts depends only on one value and its operands, and holds whether the host keeps or flushes denormal results. **Limits.** - I haven't run it on AMD hardware. - `v_fma_f32` and `v_add_f32` keep their output flush; both can produce exact denormals. - Directed f32 rounding modes don't get the flag. ### 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: #4287 - [x] AI-assisted: yes
karanvora2599 commented 2026-10-12 22:42:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T22:42:32Z.


Rebased onto #4287 alone, since #4322 is closed (its per-instruction branches cost too much pipeline-creation time; details there).

What changed:

  • Dropped: the falseArmNotDenormal rule, which read #4322's rarely-true select, and its unit-test case. SelectFlags now has only notDenormal.
  • Kept: the other three rules (the flagged tiny-product flush, #4287's NaN select, a constant factor of at least 1), and they still prove a v_mul_f32 result denormal-free.
  • New numbers on #4287's straight-line code, RTX 4060:
    • a 2048-v_mul_f32 chain goes from 5.59 to 5.10 ms (−9%);
    • SASS per v_mul_f32 goes from 16.5 to 14.5, and v_mul_legacy_f32 from 20.0 to 18.0. The other 88 ops in the sweep compile to the same size.
  • Tests: the suites pass on the RTX 4060 (338 of 338), Intel UHD (only the two Float64 tests that fail on main) and lavapipe 25.2.8 (only the intermittent depth_fast_clear_submit segfault). With the fma's flush wrongly flagged, lavapipe still fails the cancelled-sum rows.

This version adds no branches, so it doesn't change the block structure the driver compiles, only removes one flush per multiply.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134023516 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4539#issuecomment-6134023516). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-12T22:42:32Z. --- Rebased onto #4287 alone, since #4322 is closed (its per-instruction branches cost too much pipeline-creation time; details there). What changed: - **Dropped:** the `falseArmNotDenormal` rule, which read #4322's rarely-true select, and its unit-test case. `SelectFlags` now has only `notDenormal`. - **Kept:** the other three rules (the flagged tiny-product flush, #4287's NaN select, a constant factor of at least 1), and they still prove a `v_mul_f32` result denormal-free. - **New numbers** on #4287's straight-line code, RTX 4060: - a 2048-`v_mul_f32` chain goes from 5.59 to 5.10 ms (−9%); - SASS per `v_mul_f32` goes from 16.5 to 14.5, and `v_mul_legacy_f32` from 20.0 to 18.0. The other 88 ops in the sweep compile to the same size. - **Tests:** the suites pass on the RTX 4060 (338 of 338), Intel UHD (only the two `Float64` tests that fail on `main`) and lavapipe 25.2.8 (only the intermittent `depth_fast_clear_submit` segfault). With the fma's flush wrongly flagged, lavapipe still fails the cancelled-sum rows. This version adds no branches, so it doesn't change the block structure the driver compiles, only removes one flush per multiply.
oneandonlydean commented 2026-10-14 02:38:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:38:51Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159656552 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4539#issuecomment-6159656552). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:38:51Z. --- 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

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!4539
No description provided.