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

Merged
Its3rr0rsWRLD merged 2 commits from upstream-pr/288 into main 2026-11-22 02:38:22 +00:00
Its3rr0rsWRLD commented 2026-11-21 20:53:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Its3rr0rsWRLD on 2026-11-21T20:53:37Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6534256725 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/288). Originally posted by [Its3rr0rsWRLD](https://github.com/Its3rr0rsWRLD) on 2026-11-21T20:53:37Z. --- ## 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.
upstream-sync closed this pull request 2026-11-24 06:42:15 +00:00
R0Y00 commented 2026-11-24 06:48:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T20:57:37Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069132322 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069132322). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T20:57:37Z. --- 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.
Its3rr0rsWRLD commented 2026-11-24 06:48:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Its3rr0rsWRLD on 2026-11-21T21:01:04Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069174228 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069174228). Originally posted by [Its3rr0rsWRLD](https://github.com/Its3rr0rsWRLD) on 2026-11-21T21:01:04Z. --- 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`.
Its3rr0rsWRLD commented 2026-11-24 06:48:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Its3rr0rsWRLD on 2026-11-21T21:05:12Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069224375 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069224375). Originally posted by [Its3rr0rsWRLD](https://github.com/Its3rr0rsWRLD) on 2026-11-21T21:05:12Z. --- 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.
R0Y00 commented 2026-11-24 06:48:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T21:28:21Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069497662 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069497662). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T21:28:21Z. --- 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.
Its3rr0rsWRLD commented 2026-11-24 06:48:04 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Its3rr0rsWRLD on 2026-11-21T21:41:58Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069656949 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069656949). Originally posted by [Its3rr0rsWRLD](https://github.com/Its3rr0rsWRLD) on 2026-11-21T21:41:58Z. --- 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.
R0Y00 commented 2026-11-24 06:48:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T21:58:21Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069848413 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069848413). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T21:58:21Z. --- 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.
Its3rr0rsWRLD commented 2026-11-24 06:48:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Its3rr0rsWRLD on 2026-11-21T21:59:47Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7069864832 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/288#issuecomment-7069864832). Originally posted by [Its3rr0rsWRLD](https://github.com/Its3rr0rsWRLD) on 2026-11-21T21:59:47Z. --- 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.
Sign in to join this conversation.
No reviewers
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!284
No description provided.