shader: lower gfx1013 v_mad_f32 and v_mac_f32 as unfused, denormal-flushing multiply-add #288

Manually merged
github merged 2 commits from gh-pr/288/head into gh-pr/288/base 2026-11-26 21:40:21 +00:00
Owner

@⁠Its3rr0rsWRLD on GitHub · 2026-11-21 20:53 UTC · original
Its3rr0rsWRLD:shader/mad-mac-unfused → main · +61 −2 · 3 files · 2 commits · merged 2026-11-22 02:38 UTC

Change

pixel_aot rejected v_mad_f32 and v_mac_f32. Both are gfx1013 multiply-adds that round the product to binary32 before the add and do not support binary32 denormals, so they cannot reuse the fma/fmac lowering. This lowers both through a mad32 helper that flushes each operand, the product and the sum to signed zero and keeps the two roundings with precise; v_mac_f32 accumulates into its destination like v_fmac_f32. Independent problem: it widens the arithmetic the pixel translator accepts. It touches the ALU table and the arithmetic match in tools/pixel_aot.py; I found no open PR that lowers either instruction.

Source for the semantics: LLVM 15 AMDGPU, SITargetLowering::isFMADLegal in llvm/lib/Target/AMDGPU/SIISelLowering.cpp, which selects v_mad_f32/v_mac_f32 for an unfused multiply-add only when f32 denormals are flushed ("v_mad_f32/v_mac_f32 do not support denormals").

Verification

Commit d311159 plus this change (0f6e404), Windows 10 Enterprise 26H2 build 26300.9974, MSVC 14.51.36231, Windows SDK 10.0.26100.0 DXC.

ctest --preset dev --no-tests=error -R '^pixel_aot_' -V
  • pixel_aot_hardware: Passed on NVIDIA GeForce RTX 4070 Ti (vendor 10de, device 2782), driver 32.0.16.1088.
  • pixel_aot_warp: Passed on Microsoft Basic Render Driver, WARP 10.0.26100.9818.
  • Both report debug_errors=0 debug_warnings=0 and max_absolute_error=0 for every draw. Each test runs 41 draws with this change and 40 on d311159; the extra draw is the new one.
  • Full suite on c832696, before rebasing: 133/133 passed (bvh_wave64_hardware, bvh_wave64_warp, wave_shuffle_warp capability-skipped).
  • tests/docs_links_check.py and tests/bidi_controls_check.py pass.
  • tests/pixel_aot_check.py binary passes with the program added to the binary fixtures: LLVM 15.0.6 llvm-mc and llvm-objcopy, disassembled through libLLVM-17.so and libLLVM-18.so on Linux (no shared LLVM 15 library was available). Unchanged main passes the same check with both libraries. The Ubuntu CI job runs it with LLVM 15.

Evidence and follow-up

The fixture is synthetic. 4097 × 4097 = 2^24 + 2^13 + 1 rounds to 2^24 + 2^13, so 4097 × 4097 − 16785408 is 0 unfused and 1.0 fused; it is checked for both v_mad_f32 and v_mac_f32. 2^-127 × 2^126 + 0.5 is 0.5 with the denormal input flushed and 1.0 without. Negative controls: references expecting the fused result (for each instruction) or the denormal-preserving result must report pixel_mismatch, and all three do. The translator on main rejects the program at its first v_mad_f32. The controls also assert both instructions use mad32 and reject a three-operand v_mac_f32 and a two-operand v_mad_f32.

Assumption not measured here: that Scarlett flushes a denormal product or sum, as the helper does, in addition to denormal inputs. Recorded in docs/experiments.md.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: yes. Written with Claude; verified on the hardware listed above.

**@⁠Its3rr0rsWRLD** on GitHub · 2026-11-21 20:53 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/288) `Its3rr0rsWRLD:shader/mad-mac-unfused` → `main` · +61 −2 · 3 files · 2 commits · merged 2026-11-22 02:38 UTC ## Change `pixel_aot` rejected `v_mad_f32` and `v_mac_f32`. Both are gfx1013 multiply-adds that round the product to binary32 before the add and do not support binary32 denormals, so they cannot reuse the `fma`/`fmac` lowering. This lowers both through a `mad32` helper that flushes each operand, the product and the sum to signed zero and keeps the two roundings with `precise`; `v_mac_f32` accumulates into its destination like `v_fmac_f32`. Independent problem: it widens the arithmetic the pixel translator accepts. It touches the `ALU` table and the arithmetic match in `tools/pixel_aot.py`; I found no open PR that lowers either instruction. Source for the semantics: LLVM 15 AMDGPU, `SITargetLowering::isFMADLegal` in `llvm/lib/Target/AMDGPU/SIISelLowering.cpp`, which selects `v_mad_f32`/`v_mac_f32` for an unfused multiply-add only when f32 denormals are flushed ("v_mad_f32/v_mac_f32 do not support denormals"). ## Verification Commit d311159 plus this change (0f6e404), Windows 10 Enterprise 26H2 build 26300.9974, MSVC 14.51.36231, Windows SDK 10.0.26100.0 DXC. ```powershell ctest --preset dev --no-tests=error -R '^pixel_aot_' -V ``` - `pixel_aot_hardware`: Passed on NVIDIA GeForce RTX 4070 Ti (vendor 10de, device 2782), driver 32.0.16.1088. - `pixel_aot_warp`: Passed on Microsoft Basic Render Driver, WARP 10.0.26100.9818. - Both report `debug_errors=0 debug_warnings=0` and `max_absolute_error=0` for every draw. Each test runs 41 draws with this change and 40 on d311159; the extra draw is the new one. - Full suite on c832696, before rebasing: 133/133 passed (`bvh_wave64_hardware`, `bvh_wave64_warp`, `wave_shuffle_warp` capability-skipped). - `tests/docs_links_check.py` and `tests/bidi_controls_check.py` pass. - `tests/pixel_aot_check.py binary` passes with the program added to the binary fixtures: LLVM 15.0.6 `llvm-mc` and `llvm-objcopy`, disassembled through `libLLVM-17.so` and `libLLVM-18.so` on Linux (no shared LLVM 15 library was available). Unchanged main passes the same check with both libraries. The Ubuntu CI job runs it with LLVM 15. ## Evidence and follow-up The fixture is synthetic. 4097 × 4097 = 2^24 + 2^13 + 1 rounds to 2^24 + 2^13, so 4097 × 4097 − 16785408 is 0 unfused and 1.0 fused; it is checked for both `v_mad_f32` and `v_mac_f32`. 2^-127 × 2^126 + 0.5 is 0.5 with the denormal input flushed and 1.0 without. Negative controls: references expecting the fused result (for each instruction) or the denormal-preserving result must report `pixel_mismatch`, and all three do. The translator on main rejects the program at its first `v_mad_f32`. The controls also assert both instructions use `mad32` and reject a three-operand `v_mac_f32` and a two-operand `v_mad_f32`. Assumption not measured here: that Scarlett flushes a denormal product or sum, as the helper does, in addition to denormal inputs. Recorded in `docs/experiments.md`. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted: yes. Written with Claude; verified on the hardware listed above.
v_mad_f32 and v_mac_f32 round the product before the add and do not support binary32 denormals. Route both through a helper that flushes operands, product and sum and keeps the two roundings with precise; v_mac_f32 accumulates into its destination. The draw separates unfused from fused results with 4097*4097-16785408 and checks a flushed denormal input, with three wrong references as negative controls.

AI-assisted: yes
Assemble it with LLVM 15, disassemble it from the native program and require the same lowering as the source text, as for the other arithmetic fixtures.

AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:40:18 +00:00
github manually merged commit 2843f2e7d1 into gh-pr/288/base 2026-11-26 21:40:21 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 20:57 UTC · original

At 5b194053, tools/pixel_aot.py conflicts after #258. Please retain truncation in the arithmetic matcher alongside mad and mac and rerun the pixel checks, including the unfused and denormal controls.

**@⁠R0Y00** on GitHub · 2026-11-21 20:57 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069132322) At `5b194053`, `tools/pixel_aot.py` conflicts after #258. Please retain truncation in the arithmetic matcher alongside `mad` and `mac` and rerun the pixel checks, including the unfused and denormal controls.
Author
Owner

@⁠Its3rr0rsWRLD on GitHub · 2026-11-21 21:01 UTC · original

Rebased onto d311159. The only conflict was the arithmetic matcher in tools/pixel_aot.py; it now reads ...|floor|fract|trunc|add|...|fma|fmac|mad|mac, so truncation from #258 is retained alongside mad and mac. The docs/experiments.md entry now names d311159 as the tested commit; nothing else changed.

ctest --preset dev --no-tests=error -R '^pixel_aot_' -V

At 0f6e404 (d311159 plus this change), Windows 10 Enterprise 26H2 build 26300.9974, MSVC 14.51.36231, Windows SDK 10.0.26100.0 DXC:

  • pixel_aot_hardware: Passed, NVIDIA GeForce RTX 4070 Ti (10de:2782), driver 32.0.16.1088. 41 draws, every one debug_errors=0 and max_absolute_error=0.
  • pixel_aot_warp: Passed, WARP 10.0.26100.9818. 41 draws, same.
  • d311159 alone on the same machine: both pass with 40 draws; the extra draw is the mad/mac one.

The run includes the controls (both instructions lower through mad32, malformed operand counts are refused, and the truncation controls from #258 still pass) and the draw's three wrong references: the fused result for v_mad_f32, the fused result for v_mac_f32 and the denormal-preserving result. All three were reported as pixel_mismatch.

**@⁠Its3rr0rsWRLD** on GitHub · 2026-11-21 21:01 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069174228) Rebased onto d311159. The only conflict was the arithmetic matcher in `tools/pixel_aot.py`; it now reads `...|floor|fract|trunc|add|...|fma|fmac|mad|mac`, so truncation from #258 is retained alongside `mad` and `mac`. The `docs/experiments.md` entry now names d311159 as the tested commit; nothing else changed. ```powershell ctest --preset dev --no-tests=error -R '^pixel_aot_' -V ``` At 0f6e404 (d311159 plus this change), Windows 10 Enterprise 26H2 build 26300.9974, MSVC 14.51.36231, Windows SDK 10.0.26100.0 DXC: - `pixel_aot_hardware`: Passed, NVIDIA GeForce RTX 4070 Ti (10de:2782), driver 32.0.16.1088. 41 draws, every one `debug_errors=0` and `max_absolute_error=0`. - `pixel_aot_warp`: Passed, WARP 10.0.26100.9818. 41 draws, same. - d311159 alone on the same machine: both pass with 40 draws; the extra draw is the mad/mac one. The run includes the controls (both instructions lower through `mad32`, malformed operand counts are refused, and the truncation controls from #258 still pass) and the draw's three wrong references: the fused result for `v_mad_f32`, the fused result for `v_mac_f32` and the denormal-preserving result. All three were reported as `pixel_mismatch`.
Author
Owner

@⁠Its3rr0rsWRLD on GitHub · 2026-11-21 21:05 UTC · original

Added one follow-up commit (f7c8e23): the mad/mac program is now in the check_binary fixtures, like truncate_values() from #258, so the Ubuntu job assembles it with LLVM 15, disassembles it from the native program and compares the lowering. tests/pixel_aot_check.py binary passes locally with LLVM 15.0.6 tools and libLLVM 17 and 18. No change to the translator or the draws.

**@⁠Its3rr0rsWRLD** on GitHub · 2026-11-21 21:05 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069224375) Added one follow-up commit (f7c8e23): the mad/mac program is now in the `check_binary` fixtures, like `truncate_values()` from #258, so the Ubuntu job assembles it with LLVM 15, disassembles it from the native program and compares the lowering. `tests/pixel_aot_check.py binary` passes locally with LLVM 15.0.6 tools and libLLVM 17 and 18. No change to the translator or the draws.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 21:28 UTC · original

At f7c8e23, the merge conflicts are in tools/pixel_aot.py. Please retain ceil, trunc and the pending-destination check when integrating the unfused MAD/MAC matcher.

**@⁠R0Y00** on GitHub · 2026-11-21 21:28 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069497662) At `f7c8e23`, the merge conflicts are in `tools/pixel_aot.py`. Please retain `ceil`, `trunc` and the pending-destination check when integrating the unfused MAD/MAC matcher.
Author
Owner

@⁠Its3rr0rsWRLD on GitHub · 2026-11-21 21:41 UTC · original

Rebased onto b53bfb0 after #270 and #299. The arithmetic matcher now lists ceil from #270 alongside mad and mac, and the mad/mac binary fixture follows the cube fixture from #299. Nothing else changed.

ctest --preset dev --no-tests=error -R '^pixel_aot_' -V

At 64d4207 (b53bfb0 plus this change), Windows 10 Enterprise 26H2 build 26300.9974, MSVC 14.51.36231, Windows SDK 10.0.26100.0 DXC:

  • pixel_aot_hardware: Passed, NVIDIA GeForce RTX 4070 Ti (10de:2782), driver 32.0.16.1088.
  • pixel_aot_warp: Passed, WARP 10.0.26100.9818.
  • 51 draws on each adapter against 50 on b53bfb0, every one debug_errors=0 and max_absolute_error=0.
  • tests/pixel_aot_check.py binary, the controls, docs_links_check.py and bidi_controls_check.py pass on Linux with LLVM 15.0.6 tools and libLLVM 17 and 18.

This branch merges cleanly with each of my other open pixel_aot pull requests.

**@⁠Its3rr0rsWRLD** on GitHub · 2026-11-21 21:41 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069656949) Rebased onto b53bfb0 after #270 and #299. The arithmetic matcher now lists `ceil` from #270 alongside `mad` and `mac`, and the mad/mac binary fixture follows the cube fixture from #299. Nothing else changed. ```powershell ctest --preset dev --no-tests=error -R '^pixel_aot_' -V ``` At 64d4207 (b53bfb0 plus this change), Windows 10 Enterprise 26H2 build 26300.9974, MSVC 14.51.36231, Windows SDK 10.0.26100.0 DXC: - `pixel_aot_hardware`: Passed, NVIDIA GeForce RTX 4070 Ti (10de:2782), driver 32.0.16.1088. - `pixel_aot_warp`: Passed, WARP 10.0.26100.9818. - 51 draws on each adapter against 50 on b53bfb0, every one `debug_errors=0` and `max_absolute_error=0`. - `tests/pixel_aot_check.py binary`, the controls, `docs_links_check.py` and `bidi_controls_check.py` pass on Linux with LLVM 15.0.6 tools and libLLVM 17 and 18. This branch merges cleanly with each of my other open pixel_aot pull requests.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 21:58 UTC · original

At 64d4207 the merge conflicts are in tests/pixel_aot_check.py. Keep the literal fused controls from #292 and your separate unfused/FTZ MAD and MAC controls.

**@⁠R0Y00** on GitHub · 2026-11-21 21:58 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069848413) At `64d4207` the merge conflicts are in `tests/pixel_aot_check.py`. Keep the literal fused controls from #292 and your separate unfused/FTZ MAD and MAC controls.
Author
Owner

@⁠Its3rr0rsWRLD on GitHub · 2026-11-21 21:59 UTC · original

Rebased onto 1292d15 after #279. The only conflict was in check_render: the mad/mac draw now runs after the 64-bit scalar draws from #279. Nothing else changed.

ctest --preset dev --no-tests=error -R '^pixel_aot_' -V

At 2843f2e (1292d15 plus this change), same machine as before: pixel_aot_hardware (RTX 4070 Ti, driver 32.0.16.1088) and pixel_aot_warp (WARP 10.0.26100.9818) pass with 55 draws each against 54 on 1292d15, every one debug_errors=0 and max_absolute_error=0. tests/pixel_aot_check.py binary, the controls and docs_links_check.py pass on Linux with LLVM 15.0.6 tools and libLLVM 17 and 18.

**@⁠Its3rr0rsWRLD** on GitHub · 2026-11-21 21:59 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069864832) Rebased onto 1292d15 after #279. The only conflict was in `check_render`: the mad/mac draw now runs after the 64-bit scalar draws from #279. Nothing else changed. ```powershell ctest --preset dev --no-tests=error -R '^pixel_aot_' -V ``` At 2843f2e (1292d15 plus this change), same machine as before: `pixel_aot_hardware` (RTX 4070 Ti, driver 32.0.16.1088) and `pixel_aot_warp` (WARP 10.0.26100.9818) pass with 55 draws each against 54 on 1292d15, every one `debug_errors=0` and `max_absolute_error=0`. `tests/pixel_aot_check.py binary`, the controls and `docs_links_check.py` pass on Linux with LLVM 15.0.6 tools and libLLVM 17 and 18.
This discussion has been locked. Commenting is limited to contributors.
No description provided.