startup: gfx2hlsl lowers the non-fused v_mad_f32 family like the fused v_fma_f32 #1728
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows#1728
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@legacy-code-archaeologist on GitHub · 2026-11-24 17:54 UTC · original
tools/startup/gfx2hlsl.py(main at65e758b0, lines 1051–1054) lowers the gfx1013 non-fused multiply-adds to the same plain HLSL expression as the fused forms:Why this is inexact. On gfx1013,
v_mad_f32,v_mac_f32,v_madmk_f32,v_madak_f32andv_mad_legacy_f32round the product before the add, and they do not support binary32 denormals.tools/pixel_aot.pymodels this as "round the product before the add" with operands, product and sum flushed to signed zero (mad32,preciseproduct and sum).Without
precise, the compiler may contracta * b + cinto a fused multiply-add with one rounding, and the denormals are not flushed. So these forms can differ from the console in the last bit, and in results near zero. The difference can also vary between adapters, depending on whether the compiler or the driver fuses. Desktop RDNA2 has no non-fusedv_mad_f32: LLVM 15 decodes the gfx1013 encodingd541…as invalid for gfx1030. So a fused form there is likely; that is inferred, not measured.Where it can show. It is invisible in plain shading. Near a threshold it can show:
discardor alpha comparison;These can flip, giving single-pixel seams or flicker that differs between GPUs. It does not affect startup or stability.
Not measured yet. I have not checked whether DXC or any driver actually emits a fused form for today's output on a given adapter (WARP, NVIDIA, Intel). I have not built a reproducer.
Possible fix, for whoever takes it:
pixel_aot's exact helper (flush to zero, then apreciseproduct and aprecisesum) for the five non-fused forms.v_fma_f32/v_fmac_f32is a separate limitation, since HLSL has no guaranteed fusedfloatfma.Related, for reference. Decoding the same words with LLVM 15 for gfx1013 and gfx1030 shows one more reinterpretation:
0c020702(VOP2 op 0x06)v_mac_legacy_f32(non-fused)v_fmac_legacy_f32(fused)d5400001 …(VOP3 op 0x140)v_mad_legacy_f32(non-fused)v_fma_legacy_f32(fused)Neither is a bug today, as far as I can see:
disasm_rdna2.pyandpixel_aotdecode console code as gfx1013.ffx_dispatch_aot's gfx1030 decoding is for desktop provider shaders.compute_store_aotuses gfx1030 only with--cpu gfx1030.This is a report, not a claim.
@Jess-Doit on GitHub · 2026-11-24 17:56 UTC · original
Taking this, AI-assisted (Codex, driven with Claude Code), with Windows 10 22H2, a GTX 1060 and WARP.
Slice: in
tools/startup/gfx2hlsl.py, lower the five non-fused forms the waypixel_aotalready does. Those arev_mad_f32,v_mac_f32,v_madmk_f32,v_madak_f32andv_mad_legacy_f32. Inputs are flushed to zero, then the product and the sum are each computed as aprecisevalue and flushed. Forv_mad_legacy_f32, the legacy multiply rule is kept.Fixture: source-built encoded instructions, decoded and translated, then dispatched on WARP and the 1060.
a * b + con the triple, and dropping the flush on the denormal case.Not in this slice: guaranteeing a fused
v_fma_f32/v_fmac_f32, since HLSL has no guaranteed fused float FMA, and the gfx1030 reinterpretations listed above.@R0Y00 on GitHub · 2026-11-24 18:04 UTC · original
Please proceed with those five non-fused gfx1013 forms, including the legacy multiply rule. Keep the current fused spellings outside this change. For the rounding discriminator, use runtime operands and report the two reference words. If the plain-expression mutant does not contract on an adapter, record that observation and add a deterministic fused-result or wrong-reference control. An unchanged compiler choice should not make the control pass. For the denormal case, read back integer bits so the render target cannot hide a signed-zero or subnormal difference. Please keep this separate from the ordinary arithmetic coverage in #1712.
@KainanP on GitHub · 2026-11-24 18:18 UTC · original
Two WARP data points that bear on the fixture planned above (WARP + GTX 1060):
The plain-expression mutant will probably not contract on WARP. In xcl-dev/xcl#472, @LAEGGYMODZ ran 32,768 operand triples that separate fused from unfused results through four standalone kernels on WARP 10.0.26100.9818 with DXC 1.9.2609:
mad(a, b, c), apreciseproduct plus aprecisesum, both in one program, and a plaina*b + c. All four gave the unfused result for every vector: https://github.com/xcl-dev/xcl/issues/472#issuecomment-7128002850. So on WARP the "restore the plaina * b + c" control will most likely read the same words as the correct lowering. This is the case R0Y00 described ("If the plain-expression mutant does not contract on an adapter..."). The deterministic wrong-reference control is the one that will do the work there.WARP does not always match a two-rounding reference on
precisechains. In LooperSalty/AnyXboxX#145, a one-lane compute shaderprecise diff = b - a; precise prod = diff * f; precise result = a + prod;, with runtime inputsa = 0x3f59d9da, b = 0, f = 0x3f400000, gives0x3e59d9daon WARP and0x3e59d9d8on an RTX 4070 Ti SUPER (DXC 1.9.2609.5;-O3,-Odand-Gisall give the same result). The second value is the two-rounding result.dxc -dumpbinshows separatefsub/fmul/faddwithout fast-math flags. The WARP value equals the single-rounding result, but we have not found which step produces it: https://github.com/LooperSalty/AnyXboxX/issues/145Taken together: a direct
preciseproduct followed by aprecisesum, with runtime operands, came out unfused on WARP in point 1. A chain where a subtraction feeds the product came out with one rounding in point 2. If thev_mad_f32discriminator row stays a directa*b + ctriple, WARP should agree with the two-rounding reference. If a row ends up lerp-shaped, a WARP-only mismatch there is the known WARP behaviour and not the lowering. Both measurements come from desktop WARP builds on Windows 11, not from console hardware.AI-assisted (Claude Code). The numbers are quoted from the linked threads. I did not re-run them for this comment.
@Jess-Doit on GitHub · 2026-11-24 18:52 UTC · original
#1768 has merged (
3dc78117). Its measurements line up with the points above:a * b + ccontrol contracted on the GTX 1060 (driver 32.0.15.6094) for four of the five forms, reading40000003. It didn't contract on in-box WARP (10.0.19041.5794), which read40000002for all five. Because of that, the fixture also has a text-level control, which rejects the plain expression even where the adapter happens to produce the two-rounding word.a * b + ctriples with runtime operands read from a raw UAV. Both adapters matched the two-rounding reference (40000002), with no lerp-shaped chain involved.00800000to00c00000on both adapters, under SM 6.2-denorm preserve.The fused
v_fma_f32andv_fmac_f32spellings are unchanged, as agreed. That remaining part and the gfx1030 reinterpretations are why this issue can stay open, if you want to track them here.@R0Y00 on GitHub · 2026-11-24 18:53 UTC · original
The five non-fused gfx1013 forms are fixed in #1768, merged at
3dc78117. Independent Windows Release verification passed all five registered cases, including WARP and RTX 4060 Laptop dispatches, denormal-preserve checks, and their distinguishing controls. The fused forms are outside this report.