fix(shader): round the v_mad_legacy_f32 product before the add #1281
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1281"
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 SirDarky on 2026-10-07T21:48:38Z.
What
VOP3 0x140 is
v_mad_legacy_f32on gfx10.1, the family of the PS5's gfx1013 (LLVM decodes it that way up to gfx10.1). It was translated as a fused multiply-add, an assumption recorded in TechnicalDebt as unmeasured. Measured on gfx1010, it is not fused:(1+2^-12)^2 - 1gives0x3a000000(fused:0x3a000400),max * 2 - maxgives+inf(fused:max);0 * inf + 1 = 1,-0 * 5 + -0 = +0);mainuses for the f32 arithmetic: a NaN source quieted only with IEEE=1,inf - infgiving0xffc00000, the zero rule hiding a NaN factor, and a NaN src2 winning.The translation now multiplies, applies the zero rule and
flushTinyProduct, adds, and keeps thenanResultF32wrapper withinvalidProductF32. A sum of two zeros takes its sign on the bits (+0 unless both are -0), because lavapipe returned -0 for +0 + -0. The flushing is an explicit exception inf32DenormalFlushFor: it returns 3 forVMadLegacyF32before readingFLOAT_MODE, soreadOperand,flushTinyProductandwriteOperandflush in every mode.FmaLegacy.cpppins the measured results of its five encodings (plain,-src0,|src0| -src2,src0=0,-src0 -|src1|). It covers the original 20 rows, 10 rows that separate fused from rounded and keep from flush, and 2 signaling NaN rows. NaN bits are compared exactly. TechnicalDebt: the 0x140 entry is replaced by the measurement, and the NaN and f32-denormal lines are updated. The entry also records @pandather's gfx1031 measurement, where the encoding isv_fma_legacy_f32and fused, so an RDNA2 measurement is not taken as the console's behaviour.Source of the semantics: measured on hardware, an AMD Radeon RX 5700 XT (gfx1010), with
tools/hw-oracle/template.sunchanged. It was assembled by the AMD Windows driver'samd_comgr_2.dlland launched through itsamdhip64_6.dllinstead oforacle.cand ROCr. 32 rows × 5 encodings, wave32 and wave64, IEEE 0 and 1, f32 denormal modes 0 to 3: all 16 combinations agree, except the signaling NaN rows, which are quieted only with IEEE=1. The probe, the runner and the raw output are attached in a comment.Tested
Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs r7), AMD Radeon RX 5700 XT, CI configure flags, full
ctest --output-on-failure --timeout 120onmainc963432dplus this change: 570 of 573 pass.agc_driver_pixel_interlock(wave64 BDA fault: pixel 0 counted 1 of 0 overlapping read-modify-writes),agc_driver_sdwa_float_selectorsandagc_driver_sdwa_ldexp_f16fail with the same messages on unmodifiedmainon this GPU and are unrelated.agc_driver_fma_legacyandvector_mad_legacypass. Withmain's fused translation,agc_driver_fma_legacyfails (lane 20 (0x3f800800, 0x3f800800, 0xbf800000) v_mad_legacy_f32 is 0x3a000400, expected 0x3a000000). Without thef32DenormalFlushForexception, it fails at lane 26 (-src0 is 0x80000000, expected 0x00000000).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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by pandather on 2026-10-08T02:00:28Z.
I had Hermes go over the technical debt and found a divergence from my card and this PR:
Measured VOP3 0x140 on RDNA2 desktop hardware (RX 6700 XT, gfx1031) with the HSA oracle from tools/hw-oracle/template.s, in all four IEEE/denorm MODE combinations: it is fused there. Your fix is still correct for the console family this emulator targets — filing the divergence so nobody "fixes" it back based on a desktop measurement.
Plus six CPU-fmaf-vs-mul-add discriminator rows: on gfx1031 the legacy result equals v_fma_f32 bit-exactly in every mode combination, and differs from mul-then-add by 1 ULP. The same kernel also confirms your zero rule and denormal flush carry over to RDNA2: +tiny x 1 + (-0) and -tiny x 1 + (-0) both give +0 for the legacy instruction while v_fma_f32 keeps the tiny — only the fusion differs between generations.
Corroborating LLVM evidence: for gfx1031, clang assembles this encoding only as v_fma_legacy_f32 and rejects v_mad_legacy_f32 with "instruction not supported on this GPU", and llvm-objdump disassembles D540 000A 041A 0B04 as v_fma_legacy_f32. The rename looks like a real behavior change at RDNA2, so the TechnicalDebt entry could note that RDNA2 desktop fuses (measured on RX 6700 XT) — otherwise a future contributor with RDNA2 hardware measures fused and flips the translation back.
Imported from boykopovar/AnyPS5. Originally posted by SirDarky on 2026-10-08T03:38:36Z.
Thanks for measuring it on gfx1031, that's useful. It matches the LLVM split (
v_mad_legacy_f32on gfx10.1 including gfx1013,v_fma_legacy_f32from gfx10.3), so fusion changed at RDNA2 while the zero rule and the denormal-source flush stayed.I added a commit that records it in the TechnicalDebt entry: gfx1013 is decoded as
v_mad_legacy_f32like gfx1010, and on gfx1031 (RX 6700 XT, your measurement) the encoding isv_fma_legacy_f32and fused, so an RDNA2 measurement is not evidence for the console. #1290 (thev_mac_legacy_f32follow-up) is rebased on top with the same note.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T22:42:30Z.
Review: CHANGES_REQUESTED
Thanks, the gfx1010 measurement and @pandather's gfx1031 cross-check make the case for a rounded product on gfx10.1 (it also matches LLVM decoding 0x140 as
v_mad_legacy_f32up to gfx10.1 and only selecting mad when f32 denormals are flushed). It now conflicts withmainin a way that isn't a mechanical rebase, though:FloatInstructions.cppvFmaLegacyF32: since1910d1456mainpicks the NaN result on the bits (nanResultF32({lhsFactor, rhsFactor, addend}, result, &invalidProductF32(lhsFactor, rhsFactor))). The rebased version should keep that wrapper around the new mul + add. Otherwise the NaN payload is left to the driver, and rows 49/50 ofFmaLegacy.cpp(0xffc00000/0x7fc00000) will fail on NVIDIA, which returns0x7fffffff.mainflushes f32 denormals according to the shader'sFLOAT_MODE(flushF32Denormal,OperandAccess.cpp). Your measurement says the mad form flushes in every mode, which is fine, but please keep it as a deliberate exception to that model rather than a local lambda that silently overrides it. Then say so in the TechnicalDebt entry.docs/dev/TechnicalDebt.md: besides the 0x140 entry, two lines onmainbecome false. The NaN line listsfma_legacy_f32among the fused forms and the "separately rounded mad/mac forms" as left to the driver; the f32-denormal line lists the mad forms as not covered. Please update both.amd_comgr/amdhip64on Windows. Please describe it that way, and attach the probe and raw output (30 rows × 4 modes) to the PR. We can't reproduce gfx1010 here (our oracle runs on gfx1035, which fuses 0x140), so your measurement is the only evidence for these rows.#1290 is stacked on this one and will need the same rebase.
Imported from boykopovar/AnyPS5. Originally posted by SirDarky on 2026-10-10T04:03:59Z.
Thanks for the review. Rebased onto
main(c963432d) as a new translation rather than a mechanical rebase:nanResultF32({lhsFactor, rhsFactor, addend}, result, &invalidProductF32(lhsFactor, rhsFactor))wrapper.FmaLegacy.cppnow compares NaN bits exactly instead of accepting any NaN, and has two signaling NaN rows (src0 and src2), measured at IEEE=0, the mode of a request without a float mode.f32DenormalFlushForreturns 3 forVMadLegacyF32before it readsFLOAT_MODE, soreadOperand,flushTinyProductandwriteOperandflush the sources, the product and the result in every mode. Modes 1 and 2 don't throw for this instruction: I measured them too, and the results are the same as in 0 and 3. The TechnicalDebt entry calls it an exception to the model.fma_legacy_f32dropped from the fused list,v_mad_legacy_f32described with its measured NaN cases,v_mac_legacy_f32still left to the driver) and the f32-denormal line (v_mad_legacy_f32flushes in every mode, modes 1 and 2 throw for the other instructions).tools/hw-oracle/template.s, unchanged, assembled by the AMD Windows driver'samd_comgr_2.dlland launched through itsamdhip64_6.dllinstead oforacle.cand ROCr. The runner, the probe and the raw output are below.0 * 1 + -0(src0=0): lavapipe returned-0for+0 + -0. A sum of two zeros now takes its sign on the bits (+0 unless both are -0), like the NaN result. I can't run lavapipe here (Windows), so the next CI run is the check for that.Raw output: 32 rows × 5 encodings, wave32 and wave64, IEEE 0 and 1, f32 denormal modes 0 to 3, on an RX 5700 XT (gfx1010), Adrenalin driver on Windows 11. All 16 combinations give the same results, except rows 30 and 31 (signaling NaNs), which are quieted only with IEEE=1. The
vop2_0x06part is for #1290.legacy-mad-gfx1010-raw.txt
probe_legacy_mad.py (the probe)
hw_oracle_win.py (the runner: template.s through amd_comgr + HIP)
#1290 will get the same rebase once this one is settled.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:26:52Z.
Review: APPROVED
mainmoved after the review (#2279 changednanResultF32for three sources), so the PR conflicted inFloatInstructions.cppandTechnicalDebt.md. I rebased it and pushed the result:vFmaLegacyF32: dropped theinvalidProductF32argument, which no longer exists; with the zero rule both factors are 0 whenever one is, so the product is never invalid and the NaN choice is the same.TechnicalDebt.md: thev_fma_legacy_f32entry onmainis replaced by thev_mad_legacy_f32one, and the two sentences are re-applied to the current NaN and f32 denormal entries.Build + scoped ctest green,
agc_driver_fma_legacy,vector_mad_legacyandvector_fma_nan_resultpass (the first and the last also on CI's lavapipe). Merging once CI is green; #1290 can rebase on top.