feat(shader): flush f32 denormals in the float modes that flush them #1850

Merged
adriadam10 merged 1 commit from upstream-pr/1850 into main 2026-10-08 22:24:54 +00:00
adriadam10 commented 2026-10-08 22:01:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T22:01:32Z.


What

Part of #981 (FLOAT_MODE family). f32 denormals were left to the driver's default behaviour, which Vulkan doesn't specify, in every shader. Most titles run with f32 denormals flushed: FLOAT_MODE 0xc0 or 0x00 in every Astro's Playroom shader. Translation now follows the FLOAT_MODE f32 denormal field (bits 5:4) from GuestContext::floatMode (#1816) when it flushes.

The flush is emulated with explicit selects, not the DenormFlushToZero execution mode. NVIDIA's proprietary driver reports shaderDenormFlushToZeroFloat32 and shaderDenormPreserveFloat32 as false on every GPU from Turing to Blackwell (vulkan.gpuinfo.org reports for the RTX 2070, 3080, 4090 and 5070), and Vulkan only requires a flushing driver to flush results, not inputs. Explicit selects give the same bits on every driver.

Measured on RDNA2 (gfx1035) with the HW oracle. Each instruction ran over 1444 rows (7 denormals and 12 specials or normals in each operand) with f32 denormals flushed and kept, at IEEE=0 and IEEE=1. Each rule below matches the hardware in every row:

  • Inputs and result flushed to a zero of the same sign: v_add/sub/subrev/mul/mul_legacy/fma/fmac/fmamk/fmaak/min/max/min3/max3/med3/fract/floor/ceil/frexp_mant/sin/cos_f32.
  • Only inputs flushed: the f32 compares, v_cvt_i32/u32/rpi_i32/flr_i32_f32, v_frexp_exp_i32_f32, v_cvt_f64_f32, v_cvt_f16_f32, v_cvt_pkrtz_f16_f32, v_cvt_pknorm_i16/u16_f32, v_cvt_pk_u8_f32.
  • Mode-independent (left unchanged): v_rcp/rsq/sqrt/exp/log_f32 (they already flush their input), v_cmp_class_f32, v_trunc/rndne_f32, v_cvt_f32_i32, v_cvt_f32_f16, v_cube*_f32, v_fma_legacy_f32, moves and selects.
  • Tininess before rounding: with flushing, a product whose exact value is below 2^-126 gives zero even if it rounds to 2^-126. For example 0x3f7fffff * 0x00800000 gives 0x00000000 when flushing and 0x00800000 when keeping. For v_mul_f32 and v_mul_legacy_f32 this is decided exactly from the significand product (UMulHi) and the exponents, and the same check covers the fma family (v_fma/fmac/fmamk/fmaak/fma_mix_f32) when the addend is zero. With a nonzero addend the exact sum isn't available, because Fma is not guaranteed fused; that gap is in TechnicalDebt.
  • IEEE_MODE doesn't change any of this.

Changes:

  • TranslationContext::f32DenormalFlushFor classifies each opcode. readOperand (f32 reads) and writeOperand (f32 results, before the output modifier) apply the flush. The few f32 sources read as raw bits (floatUnary, v_frexp_mant_f32, v_cvt_pknorm_*) flush explicitly.
  • A shader with no known float mode is translated as flushing, the mode titles use. Denormal modes 1 and 2 (flush only inputs or only outputs, not measured) throw for the classified instructions.
  • With denormals kept (3), nothing changes: the results still depend on the driver's default, as no DenormPreserve execution mode is requested yet. That is the next PR. TechnicalDebt lists it, together with the instructions not covered: the mad/mac family, which gfx1035 doesn't have, fma_mix, dot2, the division steps, f32 interpolation and LDS float atomics.

Tested

Linux (CachyOS, GCC), RADV (gfx1035) and CI's lavapipe (Mesa 25.2.8).

  • New agc_driver_f32_denormal_flush: 16 instructions × 32 rows captured on hardware at IEEE=0 with f32 denormals flushed. 168 of the 512 cells differ from the hardware's result with denormals kept, so the test pins the flushing; the rows include the tininess edges. The test runs with no mode, with 0xc0, and with 0x00 at IEEE=1 (equal to IEEE=0 on hardware), and checks that modes 0xd0 and 0xe0 are refused.
  • vector_fused_multiply_add: this test pins the IR shape of the mac/fma operand routing (a plain f32 register read per operand). It now translates with a mode that keeps denormals, so the reads carry no flush select. What it checks is unchanged.
  • Negative checks, each fails the new test: no input flush, no result flush, no exact tininess for products, ignoring the zero-addend condition on fma, the 1/2 modes not refused, and a shader without a known mode keeping denormals. The result-flush and tininess checks fail only on lavapipe: RADV flushes f32 denormals by default, so on RADV the test passes without them, as it does on main.
  • agc_driver_f32_output_modifier and agc_driver_f32_min_max_nan pass unchanged. Full Release ctest: 490/490.

Not tested on NVIDIA. The flush uses only integer selects, the exact integer product check and the existing f32 ops, so it doesn't rely on the driver's denormal mode.

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 (#1816 is merged). Touches the same files as the output-modifier PR (#981 family) without conflicting.
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4793305122 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1850). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T22:01:32Z. --- ### What Part of #981 (FLOAT_MODE family). f32 denormals were left to the driver's default behaviour, which Vulkan doesn't specify, in every shader. Most titles run with f32 denormals flushed: `FLOAT_MODE` 0xc0 or 0x00 in every Astro's Playroom shader. Translation now follows the `FLOAT_MODE` f32 denormal field (bits 5:4) from `GuestContext::floatMode` (#1816) when it flushes. The flush is emulated with explicit selects, not the `DenormFlushToZero` execution mode. NVIDIA's proprietary driver reports `shaderDenormFlushToZeroFloat32` and `shaderDenormPreserveFloat32` as false on every GPU from Turing to Blackwell (vulkan.gpuinfo.org reports for the RTX 2070, 3080, 4090 and 5070), and Vulkan only requires a flushing driver to flush results, not inputs. Explicit selects give the same bits on every driver. Measured on RDNA2 (gfx1035) with the HW oracle. Each instruction ran over 1444 rows (7 denormals and 12 specials or normals in each operand) with f32 denormals flushed and kept, at IEEE=0 and IEEE=1. Each rule below matches the hardware in every row: - **Inputs and result flushed** to a zero of the same sign: `v_add/sub/subrev/mul/mul_legacy/fma/fmac/fmamk/fmaak/min/max/min3/max3/med3/fract/floor/ceil/frexp_mant/sin/cos_f32`. - **Only inputs flushed**: the f32 compares, `v_cvt_i32/u32/rpi_i32/flr_i32_f32`, `v_frexp_exp_i32_f32`, `v_cvt_f64_f32`, `v_cvt_f16_f32`, `v_cvt_pkrtz_f16_f32`, `v_cvt_pknorm_i16/u16_f32`, `v_cvt_pk_u8_f32`. - **Mode-independent** (left unchanged): `v_rcp/rsq/sqrt/exp/log_f32` (they already flush their input), `v_cmp_class_f32`, `v_trunc/rndne_f32`, `v_cvt_f32_i32`, `v_cvt_f32_f16`, `v_cube*_f32`, `v_fma_legacy_f32`, moves and selects. - **Tininess before rounding**: with flushing, a product whose exact value is below 2^-126 gives zero even if it rounds to 2^-126. For example `0x3f7fffff * 0x00800000` gives `0x00000000` when flushing and `0x00800000` when keeping. For `v_mul_f32` and `v_mul_legacy_f32` this is decided exactly from the significand product (`UMulHi`) and the exponents, and the same check covers the fma family (`v_fma/fmac/fmamk/fmaak/fma_mix_f32`) when the addend is zero. With a nonzero addend the exact sum isn't available, because `Fma` is not guaranteed fused; that gap is in TechnicalDebt. - `IEEE_MODE` doesn't change any of this. Changes: - `TranslationContext::f32DenormalFlushFor` classifies each opcode. `readOperand` (f32 reads) and `writeOperand` (f32 results, before the output modifier) apply the flush. The few f32 sources read as raw bits (`floatUnary`, `v_frexp_mant_f32`, `v_cvt_pknorm_*`) flush explicitly. - A shader with no known float mode is translated as flushing, the mode titles use. Denormal modes 1 and 2 (flush only inputs or only outputs, not measured) throw for the classified instructions. - With denormals kept (3), nothing changes: the results still depend on the driver's default, as no `DenormPreserve` execution mode is requested yet. That is the next PR. TechnicalDebt lists it, together with the instructions not covered: the mad/mac family, which gfx1035 doesn't have, `fma_mix`, dot2, the division steps, f32 interpolation and LDS float atomics. ### Tested Linux (CachyOS, GCC), RADV (gfx1035) and CI's lavapipe (Mesa 25.2.8). - New `agc_driver_f32_denormal_flush`: 16 instructions × 32 rows captured on hardware at IEEE=0 with f32 denormals flushed. 168 of the 512 cells differ from the hardware's result with denormals kept, so the test pins the flushing; the rows include the tininess edges. The test runs with no mode, with 0xc0, and with 0x00 at IEEE=1 (equal to IEEE=0 on hardware), and checks that modes 0xd0 and 0xe0 are refused. - `vector_fused_multiply_add`: this test pins the IR shape of the mac/fma operand routing (a plain f32 register read per operand). It now translates with a mode that keeps denormals, so the reads carry no flush select. What it checks is unchanged. - Negative checks, each fails the new test: no input flush, no result flush, no exact tininess for products, ignoring the zero-addend condition on fma, the 1/2 modes not refused, and a shader without a known mode keeping denormals. The result-flush and tininess checks fail only on lavapipe: RADV flushes f32 denormals by default, so on RADV the test passes without them, as it does on `main`. - `agc_driver_f32_output_modifier` and `agc_driver_f32_min_max_nan` pass unchanged. Full Release ctest: 490/490. Not tested on NVIDIA. The flush uses only integer selects, the exact integer product check and the existing f32 ops, so it doesn't rely on the driver's denormal mode. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 (#1816 is merged). Touches the same files as the output-modifier PR (#981 family) without conflicting. - [x] AI-assisted: yes
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!1850
No description provided.