startup: gfx2hlsl lowers the non-fused v_mad_f32 family like the fused v_fma_f32 #1721

Closed
opened 2026-11-24 17:54:29 +00:00 by legacy-code-archaeologist · 5 comments
legacy-code-archaeologist commented 2026-11-24 17:54:29 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-24T17:54:29Z.


tools/startup/gfx2hlsl.py (main at 65e758b0, lines 1051–1054) lowers the gfx1013 non-fused multiply-adds to the same plain HLSL expression as the fused forms:

'v_fmac_f32': '{0} * {1} + {2}',  'v_mac_f32':   '{0} * {1} + {2}',
'v_fma_f32':  '{0} * {1} + {2}',  'v_mad_f32':   '{0} * {1} + {2}',  'v_mad_legacy_f32': 'mul_legacy({0}, {1}) + {2}',
'v_madmk_f32': '{0} * {1} + {2}', 'v_madak_f32': '{0} * {1} + {2}',

Why this is inexact. On gfx1013, v_mad_f32, v_mac_f32, v_madmk_f32, v_madak_f32 and v_mad_legacy_f32 round the product before the add, and they do not support binary32 denormals. tools/pixel_aot.py models this as "round the product before the add" with operands, product and sum flushed to signed zero (mad32, precise product and sum).

Without precise, the compiler may contract a * b + c into 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-fused v_mad_f32: LLVM 15 decodes the gfx1013 encoding d541… 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:

  • a result rounded to an integer index or texel coordinate;
  • a discard or alpha comparison;
  • an equality test against zero.

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:

  • Reuse pixel_aot's exact helper (flush to zero, then a precise product and a precise sum) for the five non-fused forms.
  • Test with an operand triple where fused and non-fused results differ, plus a denormal case, on WARP and one hardware adapter.
  • Keep a control that restores the plain expression and must fail.
  • Guaranteeing the fused v_fma_f32/v_fmac_f32 is a separate limitation, since HLSL has no guaranteed fused float fma.

Related, for reference. Decoding the same words with LLVM 15 for gfx1013 and gfx1030 shows one more reinterpretation:

word gfx1013 gfx1030
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.py and pixel_aot decode console code as gfx1013.
  • ffx_dispatch_aot's gfx1030 decoding is for desktop provider shaders.
  • compute_store_aot uses gfx1030 only with --cpu gfx1030.

This is a report, not a claim.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6588066516 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1728). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-24T17:54:29Z. --- `tools/startup/gfx2hlsl.py` (main at 65e758b0, lines 1051–1054) lowers the gfx1013 non-fused multiply-adds to the same plain HLSL expression as the fused forms: ``` 'v_fmac_f32': '{0} * {1} + {2}', 'v_mac_f32': '{0} * {1} + {2}', 'v_fma_f32': '{0} * {1} + {2}', 'v_mad_f32': '{0} * {1} + {2}', 'v_mad_legacy_f32': 'mul_legacy({0}, {1}) + {2}', 'v_madmk_f32': '{0} * {1} + {2}', 'v_madak_f32': '{0} * {1} + {2}', ``` **Why this is inexact.** On gfx1013, `v_mad_f32`, `v_mac_f32`, `v_madmk_f32`, `v_madak_f32` and `v_mad_legacy_f32` round the product before the add, and they do not support binary32 denormals. `tools/pixel_aot.py` models this as "round the product before the add" with operands, product and sum flushed to signed zero (`mad32`, `precise` product and sum). Without `precise`, the compiler may contract `a * b + c` into 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-fused `v_mad_f32`: LLVM 15 decodes the gfx1013 encoding `d541…` 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: - a result rounded to an integer index or texel coordinate; - a `discard` or alpha comparison; - an equality test against zero. 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: - Reuse `pixel_aot`'s exact helper (flush to zero, then a `precise` product and a `precise` sum) for the five non-fused forms. - Test with an operand triple where fused and non-fused results differ, plus a denormal case, on WARP and one hardware adapter. - Keep a control that restores the plain expression and must fail. - Guaranteeing the fused `v_fma_f32`/`v_fmac_f32` is a separate limitation, since HLSL has no guaranteed fused `float` fma. **Related, for reference.** Decoding the same words with LLVM 15 for gfx1013 and gfx1030 shows one more reinterpretation: | word | gfx1013 | gfx1030 | |---|---|---| | `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.py` and `pixel_aot` decode console code as gfx1013. - `ffx_dispatch_aot`'s gfx1030 decoding is for desktop provider shaders. - `compute_store_aot` uses gfx1030 only with `--cpu gfx1030`. This is a report, not a claim.
Jess-Doit commented 2026-11-24 18:03:00 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-24T17:56:53Z.


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 way pixel_aot already does. Those are v_mad_f32, v_mac_f32, v_madmk_f32, v_madak_f32 and v_mad_legacy_f32. Inputs are flushed to zero, then the product and the sum are each computed as a precise value and flushed. For v_mad_legacy_f32, the legacy multiply rule is kept.

Fixture: source-built encoded instructions, decoded and translated, then dispatched on WARP and the 1060.

  • An operand triple where the fused and non-fused results differ in the last bit.
  • A denormal product and a denormal addend.
  • An expected-word reference computed independently of the translator.
  • Controls that must fail: restoring the plain a * b + c on 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7139935172 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1728#issuecomment-7139935172). Originally posted by [Jess-Doit](https://github.com/Jess-Doit) on 2026-11-24T17:56:53Z. --- 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 way `pixel_aot` already does. Those are `v_mad_f32`, `v_mac_f32`, `v_madmk_f32`, `v_madak_f32` and `v_mad_legacy_f32`. Inputs are flushed to zero, then the product and the sum are each computed as a `precise` value and flushed. For `v_mad_legacy_f32`, the legacy multiply rule is kept. **Fixture:** source-built encoded instructions, decoded and translated, then dispatched on WARP and the 1060. - An operand triple where the fused and non-fused results differ in the last bit. - A denormal product and a denormal addend. - An expected-word reference computed independently of the translator. - Controls that must fail: restoring the plain `a * b + c` on 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 commented 2026-11-24 19:01:38 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T18:04:14Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7140112236 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1728#issuecomment-7140112236). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T18:04:14Z. --- 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 commented 2026-11-24 19:01:39 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by KainanP on 2026-11-24T18:18:51Z.


Two WARP data points that bear on the fixture planned above (WARP + GTX 1060):

  1. 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), a precise product plus a precise sum, both in one program, and a plain a*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 plain a * 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.

  2. WARP does not always match a two-rounding reference on precise chains. In LooperSalty/AnyXboxX#145, a one-lane compute shader precise diff = b - a; precise prod = diff * f; precise result = a + prod;, with runtime inputs a = 0x3f59d9da, b = 0, f = 0x3f400000, gives 0x3e59d9da on WARP and 0x3e59d9d8 on an RTX 4070 Ti SUPER (DXC 1.9.2609.5; -O3, -Od and -Gis all give the same result). The second value is the two-rounding result. dxc -dumpbin shows separate fsub/fmul/fadd without 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/145

Taken together: a direct precise product followed by a precise sum, 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 the v_mad_f32 discriminator row stays a direct a*b + c triple, 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7140467349 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1728#issuecomment-7140467349). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-24T18:18:51Z. --- Two WARP data points that bear on the fixture planned above (WARP + GTX 1060): 1. **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)`, a `precise` product plus a `precise` sum, both in one program, and a plain `a*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 plain `a * 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. 2. **WARP does not always match a two-rounding reference on `precise` chains.** In LooperSalty/AnyXboxX#145, a one-lane compute shader `precise diff = b - a; precise prod = diff * f; precise result = a + prod;`, with runtime inputs `a = 0x3f59d9da, b = 0, f = 0x3f400000`, gives `0x3e59d9da` on WARP and `0x3e59d9d8` on an RTX 4070 Ti SUPER (DXC 1.9.2609.5; `-O3`, `-Od` and `-Gis` all give the same result). The second value is the two-rounding result. `dxc -dumpbin` shows separate `fsub`/`fmul`/`fadd` without 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/145 Taken together: a direct `precise` product followed by a `precise` sum, 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 the `v_mad_f32` discriminator row stays a direct `a*b + c` triple, 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 commented 2026-11-24 19:01:43 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Jess-Doit on 2026-11-24T18:52:28Z.


#1768 has merged (3dc78117). Its measurements line up with the points above:

  • Contraction: the plain a * b + c control contracted on the GTX 1060 (driver 32.0.15.6094) for four of the five forms, reading 40000003. It didn't contract on in-box WARP (10.0.19041.5794), which read 40000002 for 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.
  • Discriminator rows: these are direct a * b + c triples with runtime operands read from a raw UAV. Both adapters matched the two-rounding reference (40000002), with no lerp-shaped chain involved.
  • Flush: removing it changes the product and addend cases from 00800000 to 00c00000 on both adapters, under SM 6.2 -denorm preserve.

The fused v_fma_f32 and v_fmac_f32 spellings 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7141272478 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1728#issuecomment-7141272478). Originally posted by [Jess-Doit](https://github.com/Jess-Doit) on 2026-11-24T18:52:28Z. --- #1768 has merged (`3dc78117`). Its measurements line up with the points above: - **Contraction:** the plain `a * b + c` control contracted on the GTX 1060 (driver 32.0.15.6094) for four of the five forms, reading `40000003`. It didn't contract on in-box WARP (10.0.19041.5794), which read `40000002` for 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. - **Discriminator rows:** these are direct `a * b + c` triples with runtime operands read from a raw UAV. Both adapters matched the two-rounding reference (`40000002`), with no lerp-shaped chain involved. - **Flush:** removing it changes the product and addend cases from `00800000` to `00c00000` on both adapters, under SM 6.2 `-denorm preserve`. The fused `v_fma_f32` and `v_fmac_f32` spellings 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 commented 2026-11-24 19:01:43 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T18:53:53Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7141305514 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/1728#issuecomment-7141305514). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T18:53:53Z. --- 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.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows#1721
No description provided.