fix(shader): flush ds_add_f32 denormals and NaNs as the hardware does #5492
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5492"
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 billythemenace2 on 2026-10-13T21:03:11Z.
What
Before:
ds_add_f32andds_add_rtn_f32were a plain hostOpFAdd. Denormal operands and results were not flushed when the shader'sFLOAT_MODEflushes f32 denormals. That includes 0xc0, the mode most titles use and the mode assumed for a shader without a known float mode. A signalling NaN was stored unquieted, and which NaN was stored, and the NaN from+inf + -inf, were whatever the host produced. The technical debt listedds_add_f32as not measured.After: the add follows the rule measured on hardware:
+inf + -infstores 0xffc00000.MemoryInfogets aflushDenormalResultsflag next toflushDenormals, because input and result flushing come from different bits.Hardware semantics, measured on RDNA1 (gfx1010, AMD Radeon RX 5700 XT) with the
tools/hw-oracletemplate. The kernels were assembled with the Windows driver'samd_comgr_2.dlland launched with its HIP runtime.IEEE_MODEandDX10_CLAMPhave no effect.The measurement write-up is attached.
measurement-results-ds-add-f32-gfx1010.md
Remaining gap, recorded in technical debt: the sum is still the host
OpFAddbetween the flushes. Where the shader keeps f32 denormals, the result also depends on the host preserving them, and the module does not requestDenormPreservefor 32-bit floats. The Windows AMD driver used here flushes them.Tested
agc_driver_lds_add_f32covers 64 lanes in two waves, withds_add_rtn_f32andds_add_f32, in five float modes (denormal field 0-3 with round 0, and field 3 with round 3). The expected values are the RX 5700 XT's measured results, not a model. The test checks the 209 of the 320 rows that do not depend on the host preserving f32 denormals, and it also checks the value returned byds_add_rtn_f32. It passes. Without the change it fails (a signalling NaN is stored unquieted).ctest -R "lds_|shader_memory": everything passes exceptagc_driver_lds_mixed_width_atomics, which also fails on unchangedmain(dacd78525) on this machine.python3 tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)SpirvMemoryInstructions.cppchanges merge cleanly with this one; whichever merges second rebases that line. #5474 merges cleanly with this one)Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-14T00:24:32Z.
Windows test of #5492: Windows 10 (19045), MinGW-w64 GCC 15.2, Release,
ninja -j 4, ctest called directly, GPU selected by the tests: NVIDIA GeForce RTX 4080 SUPER (Selected GPUlog line). Tree:maind1670009f+ merge of PR head2b9f7aad4d52695559ae5f45495c76e0eecf56e5(local mergea4b19949d).agc_driver_lds_add_f32andagc_driver_lds_addtid: exit code 0 in 3 of 3 runs each (6 of 6). Logs:rtx5492_*_N.log.Not tested: that the new test fails without the change (no baseline run), the AMD GPU (also present, not selected), other ctest targets, Linux, a title run.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:08:03Z.
Review: COMMENTED
A good, measured change. Two notes, one about cost and one about where the measurement comes from.
Hoist the data-only work out of the CAS loop.
SharedAtomicFAdd32goes throughAtomicUpdate, so the replacement lambda runs on every CAS retry. Three of its parts depend only ondata:flush(data),nan(data)anddata | quiet. Under contention (the usual case fords_add_f32: histograms, reductions, light-list counters), every extra instruction between theOpAtomicLoadand theOpAtomicCompareExchangewidens the window in which another lane can change the value, so more iterations fail and retry. Driver compilers may hoist this by LICM, but values that feed an atomic loop aren't always treated as invariant. Computing the flushed data, the data-is-NaN flag and the quieted data once beforeAtomicUpdate, and keeping only theold-dependent selects inside the loop, makes the loop body as short asmain's plus the old-value checks.RDNA1 against RDNA2. The other LDS float atomics in the same debt entry were measured on gfx1036 (RDNA2), which is the family the console's GPU belongs to. This one was measured on gfx1010 (RDNA1). The denormal and round-mode handling of LDS float atomics did change between some GCN/RDNA generations, so the debt entry should say that the gfx10.3 behaviour is assumed from gfx10.1. A rerun of the oracle on any RDNA2 card would settle it.
The test design is good: the expected values are the measured results rather than a model, and the 111 rows that depend on the host preserving denormals are left out.
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-14T01:50:17Z.
Correction and update to my Windows test above: (1) I wrote that the GPU was shown by a
Selected GPUlog line, but that line was not in the ctest logs of those runs (ctest without-Vdoes not print the output of passing tests); I could not re-verify it for this PR, see (2). (2) On currentmain1633843382ecb1434ac76ac0c4d4655d88bb241fthe PR (head2b9f7aad4d52695559ae5f45495c76e0eecf56e5) merges without a textual conflict but does not build:core/shader/recompiler/ShaderDiskCache.cpp:142:40: error: static assertion failed: MemoryInfo changed: update the plan encoder. The PR adds a field toMemoryInfo, and the shader disk cache onmainpins the field count of the structures its plan encoder writes (assertion added in664ad7703, 13 October); the encoder and the assertion need updating. My earlier passes (agc_driver_lds_add_f32andagc_driver_lds_addtid, 3 of 3 each) were onmaind1670009f, before that assertion was on the merge base. Not tested: the tests of this PR on currentmain(they cannot be built).AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T03:05:50Z.
This doesn't build on current main:
ShaderDiskCache.cpp:142: static assertion failed: MemoryInfo changed: update the plan encoder(the field counts were pinned in664ad7703). Please addflushDenormalResultsto the plan encoder, bumpFormatVersion, and rebase.CnRJay's two points are worth taking too: compute the data-only parts once before
AtomicUpdate, and say in the debt entry that it was measured on gfx1010 (RDNA1).Imported from boykopovar/AnyPS5. Originally posted by billythemenace2 on 2026-10-14T12:36:05Z.
Rebased on main.
flushDenormalResultsis now in the plan encoder (field count 31, minimum record 79 bytes,FormatVersion33). The data-only parts (flushed data, its NaN test and the quieted data) are computed once beforeAtomicUpdate, so the CAS loop only holds the work that depends onold. The debt entry now saysds_add_f32was measured on gfx1010 and gfx10.3 is assumed to match.agc_driver_lds_add_f32,agc_driver_lds_float_atomic_modesandagc_shader_disk_cachepass on an RX 5700 XT.Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-14T16:36:07Z.
RDNA2 rerun of the measurement, as CnRJay asked: on gfx1031 every row follows the rule in the description.
Setup: AMD Radeon RX 6700 XT (gfx1031), Windows 11 26200, Adrenalin 26.8.1 (driver 32.0.21045.5002). Same method as the attached gfx1010 write-up:
tools/hw-oracle/template.sfrommaine34c47921, assembled and linked by the driver'samd_comgr_2.dll(comgr 2.9) and launched through itsamdhip64_6.dll. Body, one LDS word per lane:Operands: 1574 pairs of the classes the write-up lists (24 x 24 specials, 400 random pairs with exponents 0-3, 598 pairs that nearly cancel or are 30 binades apart; the random pairs are mine). Modes: the same 18 (denormal field 0-3 x round mode 0-3 at IEEE 0 and DX10 clamp 1, plus IEEE 1, plus DX10 clamp 0). Each stored value is compared with the six-step rule, with the add done as an exactly rounded f32 add between the flushes.
ds_add_rtn_f32returns the old value_rtnand plain forms store the sameRows that differ between denormal modes at round 0 on this set: 0/3 346, 0/1 242, 0/2 71, 1/3 143, 2/3 275.
So gfx1010 and gfx1031 agree, and the debt sentence "RDNA2 (gfx10.3, the console's family) was not measured for
ds_add_f32: its behaviour is assumed to match gfx10" can say it was measured on both. Not tested: this branch's build andagc_driver_lds_add_f32(the branch conflicts withmainnow), and gfx1013 itself.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by billythemenace2 on 2026-10-14T18:34:21Z.
@thuanquang Thank you for the RDNA2 run. The debt entry now cites it instead of assuming gfx10.3 matches gfx10.1: "thuanquang measured the same rule on RDNA2 (gfx1031, RX 6700 XT) in #5492, with the same operand classes and 18 modes in wave32 and wave64; the PS5's gfx1013 is not measured."
Rebased onto
mainea30c9274and squashed into one commit (848ce1af8). That also drops the intermediate commit that still had conflict markers in the debt file.FormatVersionis now 34, sincemaintook 33.agc_driver_lds_add_f32,agc_driver_lds_float_atomic_modesandagc_shader_disk_cachepass on an RX 5700 XT, andtools/check_conventions.pypasses.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T20:57:57Z.
Checked on gfx1035 with the HW oracle:
ds_add_f32andds_add_rtn_f32over 1393 operand pairs (your 64 test rows, 23x23 edge classes, 800 random pairs near the denormal range and near-cancelling pairs) in IEEE 0/1 x every f32 denormal field x every f32 round mode, 44,576 rows: all match the rule in the description, and your test's measured values are bit-identical on gfx1035. Build and the AgcDriver/Agc tests pass, the new test fails without the change, and it passes on CI's lavapipe.Added one commit on your branch after rebasing onto
main:EmitSharedAtomicFAdd32(CONTRIBUTING: no comments except technical debt).Merging once the full Build is green.