perf(shader): fold the denormal flush into f32 compares against constants #2408

Merged
karanvora2599 merged 2 commits from upstream-pr/2408 into main 2026-10-11 08:14:42 +00:00
karanvora2599 commented 2026-10-09 18:29:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-09T18:29:05Z.


What

An f32 compare in a mode that flushes f32 denormals flushes both of its inputs first (#1850, measured on RDNA2). When one operand is a constant, the common case (x > 0.5, x != 0, x < 0), the flush of the other operand can be folded into the compare itself. This does that, so v_cmp_*_f32 x, <constant> is one native compare instead of a flush plus a compare. Results are bit-identical; on an RTX 4060 such a compare drops from 7 to 4 instructions (the same as an integer compare) and a compare-and-select chain runs about 1.6 times faster.

Background

FLOAT_MODE 0xc0, which titles use, and shaders without a known float mode flush f32 denormals. For the compares that means v_cmp_<op>_f32 a, b is flush(a) <op> flush(b), where flush turns a denormal into a zero of the same sign. The recompiler did that for every compare: each operand went through flushF32Denormal (an and, a compare and a select) before the compare. NVIDIA drivers don't let a shader ask for f32 flush-to-zero (shaderDenormFlushToZeroFloat32 is false on every NVIDIA driver), so the flush can't be left to the hardware.

The rewrite

Let x be the operand that is not a constant and c the constant, after its abs/neg modifiers.

  • c is a NaN, an infinity or a normal number. A denormal x and the zero it flushes to both lie strictly between -2^-126 and 2^-126, and |c| >= 2^-126, so flush(x) and x compare the same way with c under every predicate (and a NaN c makes every ordered predicate false and every unordered one true either way). The flush is dropped and x is compared with c directly.

  • c is ±0 or a denormal, which flushes to ±0. Comparing flush(x) with zero is comparing x with ±2^-126, the smallest normals:

    compare becomes
    flush(x) < 0 x <= -2^-126
    flush(x) <= 0 x < 2^-126
    flush(x) > 0 x >= 2^-126
    flush(x) >= 0 x > -2^-126
    flush(x) == 0 `
    flush(x) != 0 (lg) `

    The unordered predicates (nge, nlg, ngt, nle, neq, nlt) map the same way, as x is a NaN exactly when flush(x) is. For example nge: !(flush(x) >= 0) is !(x > -2^-126), which is x <= -2^-126 unordered.

  • When the constant is the first operand (the VOPC encodings only take a constant there), the predicate is mirrored first (c < x is x > c).

emitFloatCompare (Translation/src/CompareInstructions.cpp) reads both operands unflushed, evaluates an operand that is an inline constant or a literal through its abs/neg (the and/or/xor of immediates), and emits one compare in floatCompareWithConstant. Compares of two registers, the f16 compares and modes that keep f32 denormals are unchanged.

Every constant the rewritten compares use is a normal number, an infinity or a NaN, so they give the same result whether or not the driver flushes denormals itself; the previous lowering had the same property.

Where it matters

Compares against literals are among the most frequent instructions in title shaders (branch conditions, selects, saturate and threshold tests). The f32 compare of two registers keeps its flushes; #2103 removes those that can't see a denormal.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, ANYPS5_ENABLE_SPIRV_TOOLS=ON, main at d70b8998 (the branch is rebased onto f8c2f072, which only changes CI workflows). GPU tests on an NVIDIA GeForce RTX 4060 Laptop GPU (driver 591.86, subgroup 32), on lavapipe (Mesa 26.2.4 for Windows, GALLIUM_OVERRIDE_CPU_CAPS=avx), and the new test also on the laptop's Intel UHD Graphics.

Correctness

  • New agc_driver_f32_compare_constant: 32 lanes of inputs around the thresholds (±0, the smallest and largest denormals, ±2^-126 and its neighbours, values next to 0.5 and -4, ±infinity, quiet and signaling NaNs) against 9 constants (+0 inline, -0, two denormals, 0.5 and -4.0 inline, 2^-126, +infinity and a NaN as literals), each in all 12 ordered and unordered predicates, with the constant as the first operand (VOPC) and as the second (VOP3): 216 compares per lane, packed into result bits. It runs with no float mode, FLOAT_MODE 0xc0 and 0x00 (IEEE=1), once with the constants as operands and once with the same values in a register, which keeps the previous lowering, and checks both against flush(a) <op> flush(b) computed on the host. It passes on all three devices.
  • Negative checks, each fails the new test on the RTX 4060 and on lavapipe:
    • flush(x) < 0 as x < -2^-126 (off by one at the threshold): f32 compare constant: no float mode, constant operand lane 7 v_cmp_lt_f32 0x80800000, 0x00000000 is 0, expected 1;
    • no mirroring for a constant first operand: f32 compare constant: no float mode, constant operand lane 0 v_cmp_lt_f32 0x3f000000, 0x00000000 is 1, expected 0.
  • Not caught by the test: dropping the flush without the thresholds (comparing the unflushed x with 0) still passes on all three devices. Their drivers flush f32 denormals in compares by default when no float-controls mode asks otherwise, which is also why a FLOAT_MODE that keeps f32 denormals is not part of the test (TechnicalDebt: driver-dependent). On a driver that keeps them, that version would compare 0x00000001 > 0 as true.
  • agc_driver_f32_denormal_flush (compares of registers, measured on RDNA2) passes on both GPUs.
  • Every shader test (agc_driver_*, agc_shader_*, agc_spirv_*, vector_*, agc_masked_select, agc_validate_program, 235 tests): all pass on the RTX 4060 (1 skipped, agc_driver_storage_cache_budget); on lavapipe 209 pass and 26 are skipped for missing device features.

Instructions per compare (RTX 4060, not committed)

Method: a wave32 compute program with a dependent chain of 16 v_xor_b32 + compare + v_cndmask_b32 steps, recompiled with FLOAT_MODE 0xc0 and created with APS5_PIPELINE_STATS=1; "Binary Size" / 16 bytes per instruction, compared with the same chain of v_add_nc_u32 steps. The counts include the v_cndmask_b32 of each step.

main this PR
v_cmp_gt_f32 x, 0.5 7 4
v_cmp_gt_f32 x, 0 7 4
v_cmp_eq_f32 x, 0 7 5
v_cmp_lt_f32 0.5, x (VOPC, constant first) 7 4
v_cmp_lt_f32 x, v (two registers, unchanged) 7.5 7.5
v_cmp_lt_i32 x, v (for reference) 4 4

Throughput (RTX 4060, not committed)

Method: a dependent chain of 512 v_cmp_gt_f32 + v_cndmask_b32 steps, 16384 work-groups of 32, timed per dispatch against a chain of 1024 v_fma_f32 dispatched right before or after it in the same process (the laptop's clocks move too much to compare separate runs), 12 rounds per run, 2 runs per build, builds alternating.

time per step, relative to one v_fma_f32 (median, range) main this PR
v_cmp_gt_f32 x, 0.5 + v_cndmask_b32 0.45 (0.34-1.57) 0.28 (0.24-0.34)
v_cmp_gt_f32 x, v6 + v_cndmask_b32 (unchanged) 0.45 (0.29-1.13) 0.44 (0.39-0.52)

The measurement programs aren't part of this PR; I can attach them if useful.

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:4804350755 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2408). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-09T18:29:05Z. --- ### What An f32 compare in a mode that flushes f32 denormals flushes both of its inputs first (#1850, measured on RDNA2). When one operand is a constant, the common case (`x > 0.5`, `x != 0`, `x < 0`), the flush of the other operand can be folded into the compare itself. This does that, so `v_cmp_*_f32 x, <constant>` is one native compare instead of a flush plus a compare. Results are bit-identical; on an RTX 4060 such a compare drops from 7 to 4 instructions (the same as an integer compare) and a compare-and-select chain runs about 1.6 times faster. #### Background `FLOAT_MODE` 0xc0, which titles use, and shaders without a known float mode flush f32 denormals. For the compares that means `v_cmp_<op>_f32 a, b` is `flush(a) <op> flush(b)`, where `flush` turns a denormal into a zero of the same sign. The recompiler did that for every compare: each operand went through `flushF32Denormal` (an `and`, a compare and a select) before the compare. NVIDIA drivers don't let a shader ask for f32 flush-to-zero (`shaderDenormFlushToZeroFloat32` is false on every NVIDIA driver), so the flush can't be left to the hardware. #### The rewrite Let `x` be the operand that is not a constant and `c` the constant, after its `abs`/`neg` modifiers. - `c` is a NaN, an infinity or a normal number. A denormal `x` and the zero it flushes to both lie strictly between `-2^-126` and `2^-126`, and `|c| >= 2^-126`, so `flush(x)` and `x` compare the same way with `c` under every predicate (and a NaN `c` makes every ordered predicate false and every unordered one true either way). The flush is dropped and `x` is compared with `c` directly. - `c` is `±0` or a denormal, which flushes to `±0`. Comparing `flush(x)` with zero is comparing `x` with `±2^-126`, the smallest normals: | compare | becomes | |---|---| | `flush(x) < 0` | `x <= -2^-126` | | `flush(x) <= 0` | `x < 2^-126` | | `flush(x) > 0` | `x >= 2^-126` | | `flush(x) >= 0` | `x > -2^-126` | | `flush(x) == 0` | `|x| < 2^-126` | | `flush(x) != 0` (`lg`) | `|x| >= 2^-126` | The unordered predicates (`nge`, `nlg`, `ngt`, `nle`, `neq`, `nlt`) map the same way, as `x` is a NaN exactly when `flush(x)` is. For example `nge`: `!(flush(x) >= 0)` is `!(x > -2^-126)`, which is `x <= -2^-126` unordered. - When the constant is the first operand (the VOPC encodings only take a constant there), the predicate is mirrored first (`c < x` is `x > c`). `emitFloatCompare` (`Translation/src/CompareInstructions.cpp`) reads both operands unflushed, evaluates an operand that is an inline constant or a literal through its `abs`/`neg` (the and/or/xor of immediates), and emits one compare in `floatCompareWithConstant`. Compares of two registers, the f16 compares and modes that keep f32 denormals are unchanged. Every constant the rewritten compares use is a normal number, an infinity or a NaN, so they give the same result whether or not the driver flushes denormals itself; the previous lowering had the same property. #### Where it matters Compares against literals are among the most frequent instructions in title shaders (branch conditions, `select`s, saturate and threshold tests). The f32 compare of two registers keeps its flushes; #2103 removes those that can't see a denormal. ### Tested Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `ANYPS5_ENABLE_SPIRV_TOOLS=ON`, `main` at d70b8998 (the branch is rebased onto f8c2f072, which only changes CI workflows). GPU tests on an NVIDIA GeForce RTX 4060 Laptop GPU (driver 591.86, subgroup 32), on lavapipe (Mesa 26.2.4 for Windows, `GALLIUM_OVERRIDE_CPU_CAPS=avx`), and the new test also on the laptop's Intel UHD Graphics. **Correctness** - New `agc_driver_f32_compare_constant`: 32 lanes of inputs around the thresholds (±0, the smallest and largest denormals, ±2^-126 and its neighbours, values next to 0.5 and -4, ±infinity, quiet and signaling NaNs) against 9 constants (+0 inline, -0, two denormals, 0.5 and -4.0 inline, 2^-126, +infinity and a NaN as literals), each in all 12 ordered and unordered predicates, with the constant as the first operand (VOPC) and as the second (VOP3): 216 compares per lane, packed into result bits. It runs with no float mode, `FLOAT_MODE` 0xc0 and 0x00 (IEEE=1), once with the constants as operands and once with the same values in a register, which keeps the previous lowering, and checks both against `flush(a) <op> flush(b)` computed on the host. It passes on all three devices. - Negative checks, each fails the new test on the RTX 4060 and on lavapipe: - `flush(x) < 0` as `x < -2^-126` (off by one at the threshold): `f32 compare constant: no float mode, constant operand lane 7 v_cmp_lt_f32 0x80800000, 0x00000000 is 0, expected 1`; - no mirroring for a constant first operand: `f32 compare constant: no float mode, constant operand lane 0 v_cmp_lt_f32 0x3f000000, 0x00000000 is 1, expected 0`. - Not caught by the test: dropping the flush without the thresholds (comparing the unflushed `x` with 0) still passes on all three devices. Their drivers flush f32 denormals in compares by default when no float-controls mode asks otherwise, which is also why a `FLOAT_MODE` that keeps f32 denormals is not part of the test (TechnicalDebt: driver-dependent). On a driver that keeps them, that version would compare `0x00000001 > 0` as true. - `agc_driver_f32_denormal_flush` (compares of registers, measured on RDNA2) passes on both GPUs. - Every shader test (`agc_driver_*`, `agc_shader_*`, `agc_spirv_*`, `vector_*`, `agc_masked_select`, `agc_validate_program`, 235 tests): all pass on the RTX 4060 (1 skipped, `agc_driver_storage_cache_budget`); on lavapipe 209 pass and 26 are skipped for missing device features. **Instructions per compare** (RTX 4060, not committed) Method: a wave32 compute program with a dependent chain of 16 `v_xor_b32` + compare + `v_cndmask_b32` steps, recompiled with `FLOAT_MODE` 0xc0 and created with `APS5_PIPELINE_STATS=1`; "Binary Size" / 16 bytes per instruction, compared with the same chain of `v_add_nc_u32` steps. The counts include the `v_cndmask_b32` of each step. | | `main` | this PR | |---|---|---| | `v_cmp_gt_f32 x, 0.5` | 7 | 4 | | `v_cmp_gt_f32 x, 0` | 7 | 4 | | `v_cmp_eq_f32 x, 0` | 7 | 5 | | `v_cmp_lt_f32 0.5, x` (VOPC, constant first) | 7 | 4 | | `v_cmp_lt_f32 x, v` (two registers, unchanged) | 7.5 | 7.5 | | `v_cmp_lt_i32 x, v` (for reference) | 4 | 4 | **Throughput** (RTX 4060, not committed) Method: a dependent chain of 512 `v_cmp_gt_f32` + `v_cndmask_b32` steps, 16384 work-groups of 32, timed per dispatch against a chain of 1024 `v_fma_f32` dispatched right before or after it in the same process (the laptop's clocks move too much to compare separate runs), 12 rounds per run, 2 runs per build, builds alternating. | time per step, relative to one `v_fma_f32` (median, range) | `main` | this PR | |---|---|---| | `v_cmp_gt_f32 x, 0.5` + `v_cndmask_b32` | 0.45 (0.34-1.57) | 0.28 (0.24-0.34) | | `v_cmp_gt_f32 x, v6` + `v_cndmask_b32` (unchanged) | 0.45 (0.29-1.13) | 0.44 (0.39-0.52) | The measurement programs aren't part of this PR; I can attach them if useful. ### 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
github-actions[bot] commented 2026-10-09 18:29:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T18:29:17Z.


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086882666 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2408#issuecomment-6086882666). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T18:29:17Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.
adriadam10 commented 2026-10-11 08:14:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T08:14:40Z.


Checked on RADV: the new agc_driver_f32_compare_constant and agc_driver_f32_denormal_flush pass; the test fails if the < threshold is off by one (v_cmp_lt_f32 lane 7); the rewritten compares against 0 match flush(a) <op> flush(b) on hardware (8 predicates, denormal/±0/NaN/Inf rows). Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107029073 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2408#issuecomment-6107029073). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T08:14:40Z. --- Checked on RADV: the new `agc_driver_f32_compare_constant` and `agc_driver_f32_denormal_flush` pass; the test fails if the `<` threshold is off by one (`v_cmp_lt_f32` lane 7); the rewritten compares against 0 match `flush(a) <op> flush(b)` on hardware (8 predicates, denormal/±0/NaN/Inf rows). Merging once CI is green.
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!2408
No description provided.