perf(shader): fold the denormal flush into f32 compares against constants #2408
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2408"
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 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, sov_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_MODE0xc0, which titles use, and shaders without a known float mode flush f32 denormals. For the compares that meansv_cmp_<op>_f32 a, bisflush(a) <op> flush(b), whereflushturns a denormal into a zero of the same sign. The recompiler did that for every compare: each operand went throughflushF32Denormal(anand, a compare and a select) before the compare. NVIDIA drivers don't let a shader ask for f32 flush-to-zero (shaderDenormFlushToZeroFloat32is false on every NVIDIA driver), so the flush can't be left to the hardware.The rewrite
Let
xbe the operand that is not a constant andcthe constant, after itsabs/negmodifiers.cis a NaN, an infinity or a normal number. A denormalxand the zero it flushes to both lie strictly between-2^-126and2^-126, and|c| >= 2^-126, soflush(x)andxcompare the same way withcunder every predicate (and a NaNcmakes every ordered predicate false and every unordered one true either way). The flush is dropped andxis compared withcdirectly.cis±0or a denormal, which flushes to±0. Comparingflush(x)with zero is comparingxwith±2^-126, the smallest normals:flush(x) < 0x <= -2^-126flush(x) <= 0x < 2^-126flush(x) > 0x >= 2^-126flush(x) >= 0x > -2^-126flush(x) == 0flush(x) != 0(lg)The unordered predicates (
nge,nlg,ngt,nle,neq,nlt) map the same way, asxis a NaN exactly whenflush(x)is. For examplenge:!(flush(x) >= 0)is!(x > -2^-126), which isx <= -2^-126unordered.When the constant is the first operand (the VOPC encodings only take a constant there), the predicate is mirrored first (
c < xisx > c).emitFloatCompare(Translation/src/CompareInstructions.cpp) reads both operands unflushed, evaluates an operand that is an inline constant or a literal through itsabs/neg(the and/or/xor of immediates), and emits one compare infloatCompareWithConstant. 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,mainatd70b8998(the branch is rebased ontof8c2f072, 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
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_MODE0xc0 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 againstflush(a) <op> flush(b)computed on the host. It passes on all three devices.flush(x) < 0asx < -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;f32 compare constant: no float mode, constant operand lane 0 v_cmp_lt_f32 0x3f000000, 0x00000000 is 1, expected 0.xwith 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 aFLOAT_MODEthat keeps f32 denormals is not part of the test (TechnicalDebt: driver-dependent). On a driver that keeps them, that version would compare0x00000001 > 0as true.agc_driver_f32_denormal_flush(compares of registers, measured on RDNA2) passes on both GPUs.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_b32steps, recompiled withFLOAT_MODE0xc0 and created withAPS5_PIPELINE_STATS=1; "Binary Size" / 16 bytes per instruction, compared with the same chain ofv_add_nc_u32steps. The counts include thev_cndmask_b32of each step.mainv_cmp_gt_f32 x, 0.5v_cmp_gt_f32 x, 0v_cmp_eq_f32 x, 0v_cmp_lt_f32 0.5, x(VOPC, constant first)v_cmp_lt_f32 x, v(two registers, unchanged)v_cmp_lt_i32 x, v(for reference)Throughput (RTX 4060, not committed)
Method: a dependent chain of 512
v_cmp_gt_f32+v_cndmask_b32steps, 16384 work-groups of 32, timed per dispatch against a chain of 1024v_fma_f32dispatched 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.v_fma_f32(median, range)mainv_cmp_gt_f32 x, 0.5+v_cndmask_b32v_cmp_gt_f32 x, v6+v_cndmask_b32(unchanged)The measurement programs aren't part of this PR; I can attach them if useful.
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)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.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T08:14:40Z.
Checked on RADV: the new
agc_driver_f32_compare_constantandagc_driver_f32_denormal_flushpass; the test fails if the<threshold is off by one (v_cmp_lt_f32lane 7); the rewritten compares against 0 matchflush(a) <op> flush(b)on hardware (8 predicates, denormal/±0/NaN/Inf rows). Merging once CI is green.