perf(shader): drop the input flush that rcp, rsq, sqrt, exp and log results never use #4705

Merged
karanvora2599 merged 1 commit from upstream-pr/4705 into main 2026-10-13 15:38:07 +00:00
karanvora2599 commented 2026-10-12 19:22:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T19:22:34Z.


What

v_rcp_f32, v_rsq_f32, v_sqrt_f32, v_exp_f32 and v_log_f32 no longer flush their input a second time in the SPIR-V backend. The result for a denormal input already comes from the translator's special-value selects.

Background.

  • The emitters of FPRecip32, FPRecipSqrt32, FPSqrt, FPExp2 and FPLog2 flushed a denormal input to its signed zero (EmitFlushF32DenormToSignedZero) before the host operation.
  • Since #537, unaryFloatSpecials treats every input below 2^-126 as a zero of the same sign for these opcodes. #537 measured that on RDNA2: "f32 denormal inputs of v_rcp, v_rcp_iflag, v_rsq, v_sqrt, v_log and v_exp count as zero with their sign, in every mode". It replaces the host result for those inputs:
    • rcp and rsq: a signed infinity;
    • sqrt: the signed zero;
    • log: -inf;
    • exp: 1.0.
  • floatUnary and float16Unary both go through it. So the flushed input only changed values that are thrown away.
  • v_rcp_iflag_f32 (FPRecipIFlag32) gets the same specials and never had the backend flush.

How.

  • The five emitters pass their operand straight to the host operation, and EmitFlushF32DenormToSignedZero, which has no other user, is removed.
  • One FPRecip32 doesn't go through unaryFloatSpecials: the pixel prologue's PositionW, the reciprocal of FragCoord.w. The prologue now flushes FragCoord.w in the IR before the reciprocal, so it gives the same result as before.

Why results don't change.

  • For an input with a magnitude below 0x00800000, the zero select in unaryFloatSpecials is the last one applied to the value, so the host result is discarded. negative excludes those inputs, so the NaN select doesn't pick them either.
  • For every other input, the flush returned its input unchanged.
  • f16 inputs widened to f32 are never f32 denormals: the smallest f16 denormal is 2^-24.

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_rcp_f32 11.0 8.0
v_rsq_f32 14.0 11.0
v_sqrt_f32 13.0 10.0
v_exp_f32 15.0 11.0
v_log_f32 32.0 28.0
v_rcp_f16 41.0 38.5
v_rsq_f16 39.5 36.5
v_sqrt_f16 38.5 35.5
v_exp_f16 40.5 36.5
v_log_f16 57.5 53.0

v_rcp_iflag_f32, which has the same special values and never had the flush, is 8.0 on both.

  • Sweep: 10 of the 363 swept opcodes got smaller and none got bigger.
  • Census (#4061, 11 titles, 837 shaders): the f32 forms appear 8,589 times: v_rcp_f32 3,547, v_sqrt_f32 1,855, v_rsq_f32 1,758, v_exp_f32 927, v_log_f32 502. With the counts above, that's about 27,000 fewer SASS instructions. The f16 forms don't appear in the census.
  • Other GPUs: the SASS counts are from Ada. The SPIR-V for each use loses the same eight instructions (two bitcasts, two ands, two compares, a logical and and a 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 31ade9abc. The branch is now rebased onto 682ef0c17, where I reran agc_driver_vop1_float_unary, agc_driver_f32_denormal_flush, agc_driver_float16_misc, agc_driver_relative_stage_inputs and agc_driver_relative_ancillary on the three devices. They pass.

  • Results for denormal inputs: agc_driver_vop1_float_unary runs the five instructions over rows that include 0x007fffff and 0x807fffff, with the hardware results from #537. It passes on the three devices, and so do agc_driver_f32_denormal_flush and agc_driver_float16_misc.
    • With the five zero result selects in unaryFloatSpecials narrowed to exact zeros, it fails on lavapipe: sqrt_f32 v4 is 0xffc00000, expected 0x80000000. lavapipe keeps the denormal, so on that host the special values are now the only thing that gives these results. NVIDIA and Intel still pass with that change, because they flush the input themselves.
    • With the zero rule itself narrowed to exact zeros, it fails on all three devices with the same message.
  • PositionW: agc_driver_relative_stage_inputs and agc_driver_relative_ancillary load it, and pass on the three devices.
  • Shader and driver tests (agc_driver_*, agc_shader_*, agc_spirv_*, vector_*, agc_masked_select, agc_validate_program, agc_denormal*, agc_float16_*; 350 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.

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:4834006253 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4705). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-12T19:22:34Z. --- ### What `v_rcp_f32`, `v_rsq_f32`, `v_sqrt_f32`, `v_exp_f32` and `v_log_f32` no longer flush their input a second time in the SPIR-V backend. The result for a denormal input already comes from the translator's special-value selects. **Background.** - The emitters of `FPRecip32`, `FPRecipSqrt32`, `FPSqrt`, `FPExp2` and `FPLog2` flushed a denormal input to its signed zero (`EmitFlushF32DenormToSignedZero`) before the host operation. - Since #537, `unaryFloatSpecials` treats every input below 2^-126 as a zero of the same sign for these opcodes. #537 measured that on RDNA2: "f32 denormal inputs of `v_rcp`, `v_rcp_iflag`, `v_rsq`, `v_sqrt`, `v_log` and `v_exp` count as zero with their sign, in every mode". It replaces the host result for those inputs: - `rcp` and `rsq`: a signed infinity; - `sqrt`: the signed zero; - `log`: -inf; - `exp`: 1.0. - `floatUnary` and `float16Unary` both go through it. So the flushed input only changed values that are thrown away. - `v_rcp_iflag_f32` (`FPRecipIFlag32`) gets the same specials and never had the backend flush. **How.** - The five emitters pass their operand straight to the host operation, and `EmitFlushF32DenormToSignedZero`, which has no other user, is removed. - One `FPRecip32` doesn't go through `unaryFloatSpecials`: the pixel prologue's `PositionW`, the reciprocal of `FragCoord.w`. The prologue now flushes `FragCoord.w` in the IR before the reciprocal, so it gives the same result as before. **Why results don't change.** - For an input with a magnitude below 0x00800000, the `zero` select in `unaryFloatSpecials` is the last one applied to the value, so the host result is discarded. `negative` excludes those inputs, so the NaN select doesn't pick them either. - For every other input, the flush returned its input unchanged. - f16 inputs widened to f32 are never f32 denormals: the smallest f16 denormal is 2^-24. **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_rcp_f32` | 11.0 | 8.0 | | `v_rsq_f32` | 14.0 | 11.0 | | `v_sqrt_f32` | 13.0 | 10.0 | | `v_exp_f32` | 15.0 | 11.0 | | `v_log_f32` | 32.0 | 28.0 | | `v_rcp_f16` | 41.0 | 38.5 | | `v_rsq_f16` | 39.5 | 36.5 | | `v_sqrt_f16` | 38.5 | 35.5 | | `v_exp_f16` | 40.5 | 36.5 | | `v_log_f16` | 57.5 | 53.0 | `v_rcp_iflag_f32`, which has the same special values and never had the flush, is 8.0 on both. - **Sweep:** 10 of the 363 swept opcodes got smaller and none got bigger. - **Census** (#4061, 11 titles, 837 shaders): the f32 forms appear 8,589 times: `v_rcp_f32` 3,547, `v_sqrt_f32` 1,855, `v_rsq_f32` 1,758, `v_exp_f32` 927, `v_log_f32` 502. With the counts above, that's about 27,000 fewer SASS instructions. The f16 forms don't appear in the census. - **Other GPUs:** the SASS counts are from Ada. The SPIR-V for each use loses the same eight instructions (two bitcasts, two `and`s, two compares, a logical `and` and a 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 31ade9abc. The branch is now rebased onto 682ef0c17, where I reran `agc_driver_vop1_float_unary`, `agc_driver_f32_denormal_flush`, `agc_driver_float16_misc`, `agc_driver_relative_stage_inputs` and `agc_driver_relative_ancillary` on the three devices. They pass. - **Results for denormal inputs:** `agc_driver_vop1_float_unary` runs the five instructions over rows that include 0x007fffff and 0x807fffff, with the hardware results from #537. It passes on the three devices, and so do `agc_driver_f32_denormal_flush` and `agc_driver_float16_misc`. - With the five `zero` result selects in `unaryFloatSpecials` narrowed to exact zeros, it fails on lavapipe: `sqrt_f32 v4 is 0xffc00000, expected 0x80000000`. lavapipe keeps the denormal, so on that host the special values are now the only thing that gives these results. NVIDIA and Intel still pass with that change, because they flush the input themselves. - With the `zero` rule itself narrowed to exact zeros, it fails on all three devices with the same message. - **`PositionW`:** `agc_driver_relative_stage_inputs` and `agc_driver_relative_ancillary` load it, and pass on the three devices. - **Shader and driver tests** (`agc_driver_*`, `agc_shader_*`, `agc_spirv_*`, `vector_*`, `agc_masked_select`, `agc_validate_program`, `agc_denormal*`, `agc_float16_*`; 350 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. ### 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
adriadam10 commented 2026-10-13 15:17:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T15:17:17Z.


Checked the diff against unaryFloatSpecials: for |x| < 0x00800000 the zero select is the last one applied, so the host result for denormal inputs is discarded, and for every other input the removed flush was an identity. The PositionW reciprocal keeps its flush in the IR. Measured v_rcp/rsq/sqrt/exp/log_f32 on the HW oracle (IEEE=0, DENORM=0) over 0x007fffff, 0x807fffff, 0x00000001, 0x80000001, 0 and the smallest normals: denormals behave as signed zero, matching the selects. agc_driver_vop1_float_unary, agc_driver_f32_denormal_flush, agc_driver_float16_misc and agc_driver_relative_stage_inputs pass; merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148853898 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4705#issuecomment-6148853898). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T15:17:17Z. --- Checked the diff against `unaryFloatSpecials`: for |x| < 0x00800000 the `zero` select is the last one applied, so the host result for denormal inputs is discarded, and for every other input the removed flush was an identity. The `PositionW` reciprocal keeps its flush in the IR. Measured `v_rcp/rsq/sqrt/exp/log_f32` on the HW oracle (IEEE=0, DENORM=0) over 0x007fffff, 0x807fffff, 0x00000001, 0x80000001, 0 and the smallest normals: denormals behave as signed zero, matching the selects. `agc_driver_vop1_float_unary`, `agc_driver_f32_denormal_flush`, `agc_driver_float16_misc` and `agc_driver_relative_stage_inputs` pass; merging once the full Build is green.
karanvora2599 commented 2026-10-13 21:53:56 +00:00 (Migrated from github.com)

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


Thanks @adriadam10 for checking the denormal inputs on the hardware oracle.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6155999915 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4705#issuecomment-6155999915). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-13T21:53:56Z. --- Thanks @adriadam10 for checking the denormal inputs on the hardware oracle.
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!4705
No description provided.