perf(shader): treat f32 fract, sin and cos results as denormal-free #4737

Closed
karanvora2599 wants to merge 2 commits from upstream-pr/4737 into main
karanvora2599 commented 2026-10-12 20:11:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T20:11:03Z.


What

In the float modes that flush f32 denormals, the result flush after v_fract_f32, v_sin_f32 and v_cos_f32 is now dropped when their input was already flushed.

Stacked on #4490: the first commit is #4490's, the second is this change.

Background.

  • With f32 denormals flushed (FLOAT_MODE bits 5:4 = 0, as in titles' shaders; #1850), f32DenormalFlushFor gives these three instructions 3: floatUnary flushes their input, and writeOperand flushes their result.
  • DenormalFlushEliminator drops a flush whose value it can show is never a denormal. It can follow floatUnary's selects and unaryFloatSpecials' picks when IEEE mode is off, but it had no rule for FPFract32, FPSin or FPCos. So the result flush stayed, although the input had just been flushed.

How. DenormalFreeValues::compute treats FPFract32, FPSin and FPCos as denormal-free when their operand is, in the same case as the min/max opcodes from #4490.

Why results don't change. For an input that is not a denormal, none of the three emitters returns a denormal:

  • EmitFPFract32 returns fract(|x|) or 1 - fract(|x|).
    • fract(|x|) is |x| itself below 1. From 1 up it is a multiple of the input's ulp, so 0 or at least 2^-23.
    • 1 - fract(|x|) is at least 2^-24, because fract(|x|) is at most 1 - 2^-24.
  • EmitFPSin and EmitFPCos reduce the input with x - roundEven(x), which is exact. The cycle is then 0 or at least 2^-126 in magnitude.
    • So is its distance from the nearest zero of the function (|cycle| or 0.5 - |cycle| for sin, 0.25 - |cycle| for cos). That subtraction is exact whenever the distance is below 1/8.
    • Within 2^-12 of a zero, TrigLinearNearZero returns the distance times about 2π (0x40c90fdb or 0x40c90fd5), so 0 or more than 2^-124.
    • Elsewhere, SeriesWhenFar returns the series value, or the driver's value when it is within 4 ulps of the series. The series is then at least sin(2π · 2^-12), about 1.5e-3.
  • A NaN or infinite input gives a NaN, which floatUnary replaces with its own NaN anyway.
  • A flush of a value that isn't a denormal returns the value unchanged.

Limit: IEEE mode. As in #4490, floatUnary quiets a NaN input with an or when IEEE mode is on, which the eliminator can't follow. These results keep their flush there.

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, compared against #4490 alone:

Instruction SASS with #4490 SASS with this change
v_fract_f32 15.0 13.0
v_sin_f32 49.0 47.0
v_cos_f32 52.0 50.0
  • Sweep: 3 of the 363 swept opcodes got smaller and none got bigger.
  • Census (#4061, 11 titles, 837 shaders): these instructions appear 1,834 times: v_sin_f32 811, v_fract_f32 760, v_cos_f32 263. That's about 3,700 fewer SASS instructions.
  • 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. 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.

The sweep and the full runs below were on main at 682ef0c17. The branch is now rebased onto 9742ec12c, where I reran agc_denormal_flush_elimination and the seven agc_driver_* tests listed under Results on the three devices. They pass.

  • agc_denormal_flush_elimination, one new case: a fract, sin or cos of a flushed value loses its flush, and one of an unknown value keeps it. With the three opcodes taken out of the rule, the case fails.
  • Results: these tests run v_fract_f32, v_sin_f32 or v_cos_f32 with f32 denormals flushed and IEEE mode off, so the change applies to them. They pass on the three devices:
    • agc_driver_sin_cos_near_zero, whose rows include ±2^-126 and inputs one ulp from 0.25 and 0.5 cycles, the cases closest to the bounds above;
    • agc_driver_sin_cos_reduction, agc_driver_nan_quieting, agc_driver_dx10_clamp and agc_driver_f32_result_clamp.
    • agc_driver_vop1_float_unary and agc_driver_f32_denormal_flush also pass.
  • Shader and driver tests (agc_driver_*, agc_shader_*, agc_spirv_*, vector_*, agc_masked_select, agc_validate_program, agc_denormal*, agc_float16_*; 351 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, which fails the same way on main.
    • lavapipe 25.2.8: everything passes or is skipped for missing features.

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: #4490
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4834470381 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4737). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-12T20:11:03Z. --- ### What In the float modes that flush f32 denormals, the result flush after `v_fract_f32`, `v_sin_f32` and `v_cos_f32` is now dropped when their input was already flushed. Stacked on #4490: the first commit is #4490's, the second is this change. **Background.** - With f32 denormals flushed (`FLOAT_MODE` bits 5:4 = 0, as in titles' shaders; #1850), `f32DenormalFlushFor` gives these three instructions 3: `floatUnary` flushes their input, and `writeOperand` flushes their result. - `DenormalFlushEliminator` drops a flush whose value it can show is never a denormal. It can follow `floatUnary`'s selects and `unaryFloatSpecials`' picks when IEEE mode is off, but it had no rule for `FPFract32`, `FPSin` or `FPCos`. So the result flush stayed, although the input had just been flushed. **How.** `DenormalFreeValues::compute` treats `FPFract32`, `FPSin` and `FPCos` as denormal-free when their operand is, in the same case as the min/max opcodes from #4490. **Why results don't change.** For an input that is not a denormal, none of the three emitters returns a denormal: - `EmitFPFract32` returns `fract(|x|)` or `1 - fract(|x|)`. - `fract(|x|)` is `|x|` itself below 1. From 1 up it is a multiple of the input's ulp, so 0 or at least 2^-23. - `1 - fract(|x|)` is at least 2^-24, because `fract(|x|)` is at most 1 - 2^-24. - `EmitFPSin` and `EmitFPCos` reduce the input with `x - roundEven(x)`, which is exact. The cycle is then 0 or at least 2^-126 in magnitude. - So is its distance from the nearest zero of the function (`|cycle|` or `0.5 - |cycle|` for sin, `0.25 - |cycle|` for cos). That subtraction is exact whenever the distance is below 1/8. - Within 2^-12 of a zero, `TrigLinearNearZero` returns the distance times about 2π (`0x40c90fdb` or `0x40c90fd5`), so 0 or more than 2^-124. - Elsewhere, `SeriesWhenFar` returns the series value, or the driver's value when it is within 4 ulps of the series. The series is then at least `sin(2π · 2^-12)`, about 1.5e-3. - A NaN or infinite input gives a NaN, which `floatUnary` replaces with its own NaN anyway. - A flush of a value that isn't a denormal returns the value unchanged. **Limit: IEEE mode.** As in #4490, `floatUnary` quiets a NaN input with an `or` when IEEE mode is on, which the eliminator can't follow. These results keep their flush there. **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, compared against #4490 alone: | Instruction | SASS with #4490 | SASS with this change | |---|---|---| | `v_fract_f32` | 15.0 | 13.0 | | `v_sin_f32` | 49.0 | 47.0 | | `v_cos_f32` | 52.0 | 50.0 | - **Sweep:** 3 of the 363 swept opcodes got smaller and none got bigger. - **Census** (#4061, 11 titles, 837 shaders): these instructions appear 1,834 times: `v_sin_f32` 811, `v_fract_f32` 760, `v_cos_f32` 263. That's about 3,700 fewer SASS instructions. - **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. 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`. The sweep and the full runs below were on `main` at 682ef0c17. The branch is now rebased onto 9742ec12c, where I reran `agc_denormal_flush_elimination` and the seven `agc_driver_*` tests listed under **Results** on the three devices. They pass. - **`agc_denormal_flush_elimination`**, one new case: a fract, sin or cos of a flushed value loses its flush, and one of an unknown value keeps it. With the three opcodes taken out of the rule, the case fails. - **Results:** these tests run `v_fract_f32`, `v_sin_f32` or `v_cos_f32` with f32 denormals flushed and IEEE mode off, so the change applies to them. They pass on the three devices: - `agc_driver_sin_cos_near_zero`, whose rows include ±2^-126 and inputs one ulp from 0.25 and 0.5 cycles, the cases closest to the bounds above; - `agc_driver_sin_cos_reduction`, `agc_driver_nan_quieting`, `agc_driver_dx10_clamp` and `agc_driver_f32_result_clamp`. - `agc_driver_vop1_float_unary` and `agc_driver_f32_denormal_flush` also pass. - **Shader and driver tests** (`agc_driver_*`, `agc_shader_*`, `agc_spirv_*`, `vector_*`, `agc_masked_select`, `agc_validate_program`, `agc_denormal*`, `agc_float16_*`; 351 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`, which fails the same way on `main`. - lavapipe 25.2.8: everything passes or is skipped for missing features. ### 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: #4490 - [x] AI-assisted: yes
xXPBSXx commented 2026-10-13 03:27:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T03:27:07Z.


Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on current main 0c3dc9ca2 together with eight 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; it had no failures, so no bisect was needed.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6137528083 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4737#issuecomment-6137528083). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T03:27:07Z. --- Windows test (MinGW GCC 15.2, Release, Windows 10, RTX 4080 SUPER). Built on current main `0c3dc9ca2` together with eight 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; it had no failures, so no bisect was needed. AI-assisted (Claude Code).
karanvora2599 commented 2026-10-13 21:53:59 +00:00 (Migrated from github.com)

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


Thanks @xXPBSXx for the RTX 4080 SUPER run. This one is stacked on #4490, so it needs that one merged first.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6156000654 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4737#issuecomment-6156000654). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-13T21:53:59Z. --- Thanks @xXPBSXx for the RTX 4080 SUPER run. This one is stacked on #4490, so it needs that one merged first.
oneandonlydean commented 2026-10-14 02:38:26 +00:00 (Migrated from github.com)

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


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:6159651734 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4737#issuecomment-6159651734). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:38:26Z. --- 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!4737
No description provided.