perf(shader): treat f32 min, max, med3, floor and ceil results as denormal-free #4490

Merged
karanvora2599 merged 1 commit from upstream-pr/4490 into main 2026-10-14 00:31:23 +00:00
karanvora2599 commented 2026-10-12 13:24:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T13:24:58Z.


What

In the float modes that flush f32 denormals, the result flush after v_min/max/min3/max3/med3_f32 and v_floor/ceil_f32 is now dropped when it can't change the value.

Background.

  • With f32 denormals flushed (FLOAT_MODE bits 5:4 = 0, as in titles' shaders; #1850), writeOperand flushes these instructions' results to a signed zero with an explicit select. f32DenormalFlushFor gives them 3.
  • DenormalFlushEliminator drops a flush whose value it can show is never a denormal: flushed values, conversions from integers or f16, sign changes, selects and phis of such values.
  • It had no rule for the min/max opcodes or for floor and ceil, so their result flush stayed even when every operand had already been flushed.

How. DenormalFreeValues::compute now treats:

  • FPMin32, FPMax32, FPMinTri32, FPMaxTri32 and FPMedTri32 as denormal-free when every operand is, through the same loop phis already use;
  • FPFloor32 and FPCeil32 as always denormal-free.

Why results don't change.

  • EmitMinMaxF32Value and EmitFPMedTri32 return one operand's bits, or the and/or of two zeros when both are zero. So they produce a denormal only if an operand is one.
  • GLSLstd450Floor and Ceil return an integer, a signed zero, an infinity or a NaN, none of which is a denormal.
  • A flush of a value that isn't a denormal returns the value unchanged.

Limit: IEEE mode.

  • With IEEE mode on, ieeeMinMaxF32 selects a quieted operand (bits | 0x00400000) for a signaling NaN, and floatUnary does the same for a NaN input. The eliminator can't show that or gives no denormal, so these results keep their flush.
  • The census in #4061 doesn't record each shader's IEEE mode, so I don't know how many shaders benefit.

Before / after on an RTX 4060 Laptop (Ada, driver 591.86). SASS instructions per instruction come from the APS5_PIPELINE_STATS binary size over a chain of 16 instructions, with FLOAT_MODE 0xc0 and IEEE mode off:

Instruction SASS before SASS after
v_min_f32 10.0 8.0
v_max_f32 11.0 9.0
v_max_f32 DPP quad_perm:[1,0,3,2] 14.5 12.0
v_min3_f32 15.0 13.0
v_max3_f32 16.0 14.0
v_med3_f32 30.0 28.0
v_floor_f32 7.0 5.0
v_ceil_f32 7.0 5.0
  • IEEE mode off: 8 of the 363 swept opcodes got smaller and none got bigger.
  • IEEE mode on: none of the 363 changed.
  • Census (#4061, 11 titles, 837 shaders): these instructions appear 6,885 times: v_max_f32 3,608, v_min_f32 1,568, v_min3_f32 539, v_max3_f32 371, v_med3_f32 371, v_floor_f32 284, v_ceil_f32 144.
  • Other GPUs: the SASS counts are from Ada. The change is in the IR, so it removes the same select on every GPU.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, Release, this branch on main at 2ccbd376f. Devices:

  • RTX 4060 Laptop (Ada, driver 591.86)

  • Intel UHD Graphics (Raptor Lake, driver 101.6790)

  • lavapipe from Mesa 25.2.8 (llvmpipe, LLVM 21.1.7): the version CI installs; Windows build with GALLIUM_OVERRIDE_CPU_CAPS=avx.

  • agc_denormal_flush_elimination, three new cases:

    • the five min/max opcodes over flushed operands lose their flush, and a v_max or v_med3 with an unknown operand keeps it;
    • floor and ceil of an unknown value lose their flush;
    • a max carried around a loop without a flush keeps it, and the search ends.
    • With the min/max rule changed to ignore the operands, the first case fails.
  • Results: agc_driver_f32_denormal_flush runs v_min_f32, v_max3_f32, v_med3_f32, v_floor_f32 and v_ceil_f32 over denormal and special rows captured on hardware with f32 denormals flushed (#1850). It passes on the three devices, and so does agc_driver_f32_min_max_nan.

  • Shader and driver tests (agc_driver_*, agc_shader_*, agc_spirv_*, vector_*, agc_masked_select, agc_validate_program, agc_denormal*, agc_float16_*; 338 tests):

    • RTX 4060: all pass. I left out the BDA tests that lose the device on driver 591.86 (#3291): agc_driver_flat_loads*, agc_driver_global_loads*, agc_driver_flat_apertures*, agc_driver_pixel_interlock.
    • Intel UHD: everything passes or is skipped for missing features, except agc_driver_ds_swizzle and agc_driver_f32_dot2_rounding, which fail the same way on main.
    • lavapipe 25.2.8: everything passes or is skipped for missing features, except agc_driver_depth_fast_clear_submit. It segfaults in 3 of 4 runs here, and did so intermittently with main's code before this change too.

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:4830064215 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4490). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-12T13:24:58Z. --- ### What In the float modes that flush f32 denormals, the result flush after `v_min/max/min3/max3/med3_f32` and `v_floor/ceil_f32` is now dropped when it can't change the value. **Background.** - With f32 denormals flushed (`FLOAT_MODE` bits 5:4 = 0, as in titles' shaders; #1850), `writeOperand` flushes these instructions' results to a signed zero with an explicit select. `f32DenormalFlushFor` gives them 3. - `DenormalFlushEliminator` drops a flush whose value it can show is never a denormal: flushed values, conversions from integers or f16, sign changes, selects and phis of such values. - It had no rule for the min/max opcodes or for floor and ceil, so their result flush stayed even when every operand had already been flushed. **How.** `DenormalFreeValues::compute` now treats: - `FPMin32`, `FPMax32`, `FPMinTri32`, `FPMaxTri32` and `FPMedTri32` as denormal-free when every operand is, through the same loop phis already use; - `FPFloor32` and `FPCeil32` as always denormal-free. **Why results don't change.** - `EmitMinMaxF32Value` and `EmitFPMedTri32` return one operand's bits, or the `and`/`or` of two zeros when both are zero. So they produce a denormal only if an operand is one. - `GLSLstd450Floor` and `Ceil` return an integer, a signed zero, an infinity or a NaN, none of which is a denormal. - A flush of a value that isn't a denormal returns the value unchanged. **Limit: IEEE mode.** - With IEEE mode on, `ieeeMinMaxF32` selects a quieted operand (`bits | 0x00400000`) for a signaling NaN, and `floatUnary` does the same for a NaN input. The eliminator can't show that `or` gives no denormal, so these results keep their flush. - The census in #4061 doesn't record each shader's IEEE mode, so I don't know how many shaders benefit. **Before / after** on an RTX 4060 Laptop (Ada, driver 591.86). SASS instructions per instruction come from the `APS5_PIPELINE_STATS` binary size over a chain of 16 instructions, with `FLOAT_MODE` 0xc0 and IEEE mode off: | Instruction | SASS before | SASS after | |---|---|---| | `v_min_f32` | 10.0 | 8.0 | | `v_max_f32` | 11.0 | 9.0 | | `v_max_f32` DPP `quad_perm:[1,0,3,2]` | 14.5 | 12.0 | | `v_min3_f32` | 15.0 | 13.0 | | `v_max3_f32` | 16.0 | 14.0 | | `v_med3_f32` | 30.0 | 28.0 | | `v_floor_f32` | 7.0 | 5.0 | | `v_ceil_f32` | 7.0 | 5.0 | - **IEEE mode off:** 8 of the 363 swept opcodes got smaller and none got bigger. - **IEEE mode on:** none of the 363 changed. - **Census** (#4061, 11 titles, 837 shaders): these instructions appear 6,885 times: `v_max_f32` 3,608, `v_min_f32` 1,568, `v_min3_f32` 539, `v_max3_f32` 371, `v_med3_f32` 371, `v_floor_f32` 284, `v_ceil_f32` 144. - **Other GPUs:** the SASS counts are from Ada. The change is in the IR, so it removes the same select on every GPU. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, Release, this branch on `main` at 2ccbd376f. Devices: - RTX 4060 Laptop (Ada, driver 591.86) - Intel UHD Graphics (Raptor Lake, driver 101.6790) - lavapipe from Mesa 25.2.8 (llvmpipe, LLVM 21.1.7): the version CI installs; Windows build with `GALLIUM_OVERRIDE_CPU_CAPS=avx`. - **`agc_denormal_flush_elimination`**, three new cases: - the five min/max opcodes over flushed operands lose their flush, and a `v_max` or `v_med3` with an unknown operand keeps it; - floor and ceil of an unknown value lose their flush; - a max carried around a loop without a flush keeps it, and the search ends. - With the min/max rule changed to ignore the operands, the first case fails. - **Results:** `agc_driver_f32_denormal_flush` runs `v_min_f32`, `v_max3_f32`, `v_med3_f32`, `v_floor_f32` and `v_ceil_f32` over denormal and special rows captured on hardware with f32 denormals flushed (#1850). It passes on the three devices, and so does `agc_driver_f32_min_max_nan`. - **Shader and driver tests** (`agc_driver_*`, `agc_shader_*`, `agc_spirv_*`, `vector_*`, `agc_masked_select`, `agc_validate_program`, `agc_denormal*`, `agc_float16_*`; 338 tests): - RTX 4060: all pass. I left out the BDA tests that lose the device on driver 591.86 (#3291): `agc_driver_flat_loads*`, `agc_driver_global_loads*`, `agc_driver_flat_apertures*`, `agc_driver_pixel_interlock`. - Intel UHD: everything passes or is skipped for missing features, except `agc_driver_ds_swizzle` and `agc_driver_f32_dot2_rounding`, which fail the same way on `main`. - lavapipe 25.2.8: everything passes or is skipped for missing features, except `agc_driver_depth_fast_clear_submit`. It segfaults in 3 of 4 runs here, and did so intermittently with `main`'s code before this change too. ### 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
xXPBSXx commented 2026-10-13 06:56:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T06:56:46Z.


Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on main 88107b7de together with thirteen other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it:

  • tests/DenormalFlushElimination.cpp (agc_denormal_flush_elimination): 3 of 3 runs passed.

Not tested: no game run. The batch was built once, not per PR; nothing failed, so no bisect was needed.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140095114 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4490#issuecomment-6140095114). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T06:56:46Z. --- Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on main `88107b7de` together with thirteen other open GPU PRs (their own commits cherry-picked, no merge commits), so only the tests that belong to this PR say something about it: - `tests/DenormalFlushElimination.cpp` (`agc_denormal_flush_elimination`): 3 of 3 runs passed. Not tested: no game run. The batch was built once, not per PR; nothing failed, so no bisect was needed. AI-assisted (Claude Code).
karanvora2599 commented 2026-10-13 21:53:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-13T21:53:57Z.


Thanks @xXPBSXx for the RTX 4080 SUPER run.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6156000342 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4490#issuecomment-6156000342). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-13T21:53:57Z. --- Thanks @xXPBSXx for the RTX 4080 SUPER run.
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!4490
No description provided.