feat(shader): flush f32 denormals in the float modes that flush them #1850
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1850"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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_MODE0xc0 or 0x00 in every Astro's Playroom shader. Translation now follows theFLOAT_MODEf32 denormal field (bits 5:4) fromGuestContext::floatMode(#1816) when it flushes.The flush is emulated with explicit selects, not the
DenormFlushToZeroexecution mode. NVIDIA's proprietary driver reportsshaderDenormFlushToZeroFloat32andshaderDenormPreserveFloat32as 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:
v_add/sub/subrev/mul/mul_legacy/fma/fmac/fmamk/fmaak/min/max/min3/max3/med3/fract/floor/ceil/frexp_mant/sin/cos_f32.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.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.0x3f7fffff * 0x00800000gives0x00000000when flushing and0x00800000when keeping. Forv_mul_f32andv_mul_legacy_f32this 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, becauseFmais not guaranteed fused; that gap is in TechnicalDebt.IEEE_MODEdoesn't change any of this.Changes:
TranslationContext::f32DenormalFlushForclassifies each opcode.readOperand(f32 reads) andwriteOperand(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.DenormPreserveexecution 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).
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.main.agc_driver_f32_output_modifierandagc_driver_f32_min_max_nanpass 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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)