shader: lower compute v_mac_f32 as an unfused multiply-add #2156

Merged
henriazz10 merged 1 commit from upstream-pr/2164 into main 2026-11-25 02:31:19 +00:00
henriazz10 commented 2026-11-25 02:11:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by henriazz10 on 2026-11-25T02:11:53Z.


Change

Refs #721. This is the v_mac_f32 slice claimed in https://github.com/R0Y00/Xbox2Windows/issues/721#issuecomment-7144791814. It touches only tools/compute_store_aot.py, tests/compute_store_check.py, CMakeLists.txt and docs/experiments.md.

  • v_mac_f32_e32 vD, S0, vS1 (VOP2 0x1f) and v_mac_f32_e64 vD, S0, S1 (VOP3) compute D = S0 * S1 + D as an unfused binary32 multiply-add: the intermediate product S0 * S1 rounds to binary32 before the addition. D is both an input accumulator and the destination.
    • Lowering uses f32_mac(a, b, c) with separate precise float p = asfloat(a) * asfloat(b) and precise float r = p + asfloat(c) so DXC does not contract the operation to FMA.
  • Refused, each with its own control:
    • NaN, Inf, and denormal literal constants (0x7fc00000, 0x7f800000, 0x00000001);
    • unknown or pending S0/S1 (v40, s12), uninitialized accumulator vD, and vD above v255 (v256);
    • source modifiers (-0x45800800, |-2.0|), clamp, omod (mul:2), _e32 with SGPR S1, and bare v_mac_f32.
  • Removed the now-obsolete v_mac_f32_e32 refusal from f32_controls().

The existing f32_controls() entry that used v_mac_f32_e32 as an unsupported example now uses v_mad_f32, which is still refused, so that list keeps the same number of refusal checks.

Verification

Commit da769b07, rebased onto 8700da53. The draws, controls() and the LLVM 15 round trip were re-run on da769b07. Windows 11 build 26300, VS 2019 Build Tools 16.11 (MSVC 14.29), DXC 1.8.2502.11, Python 3.12, debug layer enabled. offscreen_check was built from an earlier main; this PR does not change it.

python -B tests/compute_store_check.py draw-mac-f32 <dxc> <offscreen_check> warp
python -B tests/compute_store_check.py draw-mac-f32 <dxc> <offscreen_check> hardware
python -B tests/docs_links_check.py
python -B tests/bidi_controls_check.py
git diff --check HEAD~1
Backend Adapter / driver Result
WARP Microsoft Basic Render Driver 10.0.26100.9818 exit 0, groups=1 compared_words=320 mismatches=0, debug_errors=0 debug_warnings=0
hardware NVIDIA GeForce RTX 5080, 32.0.16.1692 exit 0, groups=1 compared_words=320 mismatches=0, debug_errors=0 debug_warnings=0

docs_links_check.py (18 Markdown files, 398 links), bidi_controls_check.py (1439 tracked files), and git diff --check HEAD~1 all exited 0.

The LLVM 15 round trip in Docker (x2w-llvm15) exited 0 (binary_exit=0):

docker run --rm -v "C:\Proyectos\Xbox2Windows\wt-mac-f32:/src" x2w-llvm15 bash -c 'cp -r /src /w && cd /w && python3 -B tests/compute_store_check.py binary llvm-mc-15 llvm-objcopy-15 /usr/lib/x86_64-linux-gnu/libLLVM-15.so.1; echo binary_exit=$?'

mac_f32_binary() decodes the assembled MAC_F32 fixture back to the same v_mac_f32 lines and requires the same translation.

Negative control. With tools/compute_store_aot.py from origin/main, mac_f32_controls() fails with ValueError: Unsupported compute instruction: v_mac_f32_e32 v10, 0x45800800, v5.

Evidence

The MAC_F32 fixture is source-built in LLVM 15 spelling. It runs one group of 64 threads (buffer_store_dwordx4, 256 output words plus 64 SENTINEL words), covering 4097 * 4097 + (-16785408) and (2 + 2^-23) * (1 + 2^-23) - (1 + 2^-22) (plus unique per-thread tid.x inputs) where unfused and fused results differ, and compares all 320 words against an independent Python model.

  • Translation controls. Verify the exact outputData.Store4(...) line, float_word literal refusals, unknown/pending sources, uninitialized vD, v256, modifiers, _e32 SGPR S1, and bare spelling.
  • Reference controls. Each fails at its first differing word:
    • fused FMA instead of unfused MAC: word 0;
    • product minus accumulator: word 2;
    • accumulator zeroed: word 3;
    • sentinel past group: word 256.
  • Recompiled-HLSL controls. Each fails on the device with Compute readback differs:
    • p - asfloat(c) instead of p + asfloat(c) (WARP and hardware);
    • accumulator dropped (WARP and hardware);
    • fused mad() instead of unfused multiply+add (hardware, where mad() contracts to FMA).

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 (docs/experiments.md).
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted. Gemini 4 Hydrogen (Antigravity CLI) wrote the change and ran the checks; Claude Code reviewed it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6595559656 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2164). Originally posted by [henriazz10](https://github.com/henriazz10) on 2026-11-25T02:11:53Z. --- ## Change Refs #721. This is the `v_mac_f32` slice claimed in https://github.com/R0Y00/Xbox2Windows/issues/721#issuecomment-7144791814. It touches only `tools/compute_store_aot.py`, `tests/compute_store_check.py`, `CMakeLists.txt` and `docs/experiments.md`. - `v_mac_f32_e32 vD, S0, vS1` (VOP2 `0x1f`) and `v_mac_f32_e64 vD, S0, S1` (VOP3) compute `D = S0 * S1 + D` as an unfused binary32 multiply-add: the intermediate product `S0 * S1` rounds to binary32 before the addition. `D` is both an input accumulator and the destination. - Lowering uses `f32_mac(a, b, c)` with separate `precise float p = asfloat(a) * asfloat(b)` and `precise float r = p + asfloat(c)` so DXC does not contract the operation to FMA. - Refused, each with its own control: - NaN, Inf, and denormal literal constants (`0x7fc00000`, `0x7f800000`, `0x00000001`); - unknown or pending `S0`/`S1` (`v40`, `s12`), uninitialized accumulator `vD`, and `vD` above `v255` (`v256`); - source modifiers (`-0x45800800`, `|-2.0|`), `clamp`, `omod` (`mul:2`), `_e32` with SGPR `S1`, and bare `v_mac_f32`. - Removed the now-obsolete `v_mac_f32_e32` refusal from `f32_controls()`. The existing `f32_controls()` entry that used `v_mac_f32_e32` as an unsupported example now uses `v_mad_f32`, which is still refused, so that list keeps the same number of refusal checks. ## Verification Commit `da769b07`, rebased onto `8700da53`. The draws, `controls()` and the LLVM 15 round trip were re-run on `da769b07`. Windows 11 build 26300, VS 2019 Build Tools 16.11 (MSVC 14.29), DXC 1.8.2502.11, Python 3.12, debug layer enabled. `offscreen_check` was built from an earlier `main`; this PR does not change it. ```powershell python -B tests/compute_store_check.py draw-mac-f32 <dxc> <offscreen_check> warp python -B tests/compute_store_check.py draw-mac-f32 <dxc> <offscreen_check> hardware python -B tests/docs_links_check.py python -B tests/bidi_controls_check.py git diff --check HEAD~1 ``` | Backend | Adapter / driver | Result | |---|---|---| | WARP | Microsoft Basic Render Driver 10.0.26100.9818 | exit 0, `groups=1 compared_words=320 mismatches=0`, `debug_errors=0 debug_warnings=0` | | hardware | NVIDIA GeForce RTX 5080, 32.0.16.1692 | exit 0, `groups=1 compared_words=320 mismatches=0`, `debug_errors=0 debug_warnings=0` | `docs_links_check.py` (18 Markdown files, 398 links), `bidi_controls_check.py` (1439 tracked files), and `git diff --check HEAD~1` all exited 0. The LLVM 15 round trip in Docker (`x2w-llvm15`) exited 0 (`binary_exit=0`): ```sh docker run --rm -v "C:\Proyectos\Xbox2Windows\wt-mac-f32:/src" x2w-llvm15 bash -c 'cp -r /src /w && cd /w && python3 -B tests/compute_store_check.py binary llvm-mc-15 llvm-objcopy-15 /usr/lib/x86_64-linux-gnu/libLLVM-15.so.1; echo binary_exit=$?' ``` `mac_f32_binary()` decodes the assembled `MAC_F32` fixture back to the same `v_mac_f32` lines and requires the same translation. **Negative control.** With `tools/compute_store_aot.py` from `origin/main`, `mac_f32_controls()` fails with `ValueError: Unsupported compute instruction: v_mac_f32_e32 v10, 0x45800800, v5`. ## Evidence The `MAC_F32` fixture is source-built in LLVM 15 spelling. It runs one group of 64 threads (`buffer_store_dwordx4`, 256 output words plus 64 `SENTINEL` words), covering `4097 * 4097 + (-16785408)` and `(2 + 2^-23) * (1 + 2^-23) - (1 + 2^-22)` (plus unique per-thread `tid.x` inputs) where unfused and fused results differ, and compares all 320 words against an independent Python model. - **Translation controls.** Verify the exact `outputData.Store4(...)` line, `float_word` literal refusals, unknown/pending sources, uninitialized `vD`, `v256`, modifiers, `_e32` SGPR `S1`, and bare spelling. - **Reference controls.** Each fails at its first differing word: - fused FMA instead of unfused MAC: word 0; - product minus accumulator: word 2; - accumulator zeroed: word 3; - sentinel past group: word 256. - **Recompiled-HLSL controls.** Each fails on the device with `Compute readback differs`: - `p - asfloat(c)` instead of `p + asfloat(c)` (WARP and hardware); - accumulator dropped (WARP and hardware); - fused `mad()` instead of unfused multiply+add (hardware, where `mad()` contracts to FMA). ## 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 (`docs/experiments.md`). - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted. Gemini 4 Hydrogen (Antigravity CLI) wrote the change and ran the checks; Claude Code reviewed it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-25 03:01:14 +00:00
tedin7 commented 2026-11-25 03:01:14 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:40:27Z.

Review: COMMENTED


Review of head da769b075d

Finding (P2): the new compute_store_aot_mac_f32_hardware test fails on Win2025. compute_store_aot_mac_f32_warp passes.

  • On the hardware adapter (Microsoft Basic Render Driver, vendor 1414 device 008c) the output matches the model: groups=1 compared_words=320 mismatches=0.
  • The test then fails in mac_f32_draw (tests/compute_store_check.py:3352): AssertionError: mac_f32: mutation 'fused mad() instead of unfused multiply+add' escaped detection.
  • The HLSL mutation at line ~3340 swaps the add for precise float r = mad(a, b, c). Because of precise, the compiler does not fuse it, so the mutant gives the same bits as the real lowering on this adapter. That makes it an equivalent mutant, not a weak lowering. Suggested fix: use a mutation that really changes results on every adapter (for example p - c, which is already in the list), or compare against a true fused reference such as fma() without precise. Do not require detection of mad on every backend.

What looked fine:

  • Lowering in tools/compute_store_aot.py follows the RDNA2 VOP2 0x1f semantics (D = S0*S1 + D, product rounded to binary32 before the add).
  • Refusals for NaN/Inf/denormal literals, pending or unknown sources, vD > 255, modifiers, clamp, omod and bare v_mac_f32 are covered.
  • The f32_mac helper sits next to the sibling f32 helpers, and the docs entry is present.
  • No other open or recently merged PR covers v_mac_f32.
  • CMake test registration follows the sibling dot_u pattern.

Commands executed locally (Linux):

  • python3 tests/docs_links_check.py: pass (18 files, 399 links)
  • python3 tests/bidi_controls_check.py: pass
  • git diff --check origin/main...HEAD: clean
  • mac_f32_controls(): pass. Mutation: the accumulator token replaced with 0x0 in the lowering made the controls fail, as expected. Mutation: p + c changed to p - c in the HLSL helper was NOT caught by the controls on Linux, because only the GPU draw test sees it.

Commands executed on Win2025 (x2w_pipe.py win2025 2164 --scope direct --targets offscreen_check --regex '^compute_store_aot_(mac_f32|dot_u|add_co)_(warp|hardware)$'):

  • add_co warp/hardware: pass
  • dot_u warp/hardware: pass
  • mac_f32 warp: pass
  • mac_f32 hardware: FAIL (mutation escaped, see above)

Not a merge candidate until the hardware test is fixed. Please push a fix and re-run.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6050991333 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2164#pullrequestreview-6050991333). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T02:40:27Z. **Review:** COMMENTED --- Review of head da769b075d5f8783a27a2e2e60e8546ed2137257 Finding (P2): the new `compute_store_aot_mac_f32_hardware` test fails on Win2025. `compute_store_aot_mac_f32_warp` passes. - On the hardware adapter (Microsoft Basic Render Driver, vendor 1414 device 008c) the output matches the model: `groups=1 compared_words=320 mismatches=0`. - The test then fails in `mac_f32_draw` (tests/compute_store_check.py:3352): `AssertionError: mac_f32: mutation 'fused mad() instead of unfused multiply+add' escaped detection`. - The HLSL mutation at line ~3340 swaps the add for `precise float r = mad(a, b, c)`. Because of `precise`, the compiler does not fuse it, so the mutant gives the same bits as the real lowering on this adapter. That makes it an equivalent mutant, not a weak lowering. Suggested fix: use a mutation that really changes results on every adapter (for example `p - c`, which is already in the list), or compare against a true fused reference such as `fma()` without `precise`. Do not require detection of `mad` on every backend. What looked fine: - Lowering in tools/compute_store_aot.py follows the RDNA2 VOP2 0x1f semantics (D = S0*S1 + D, product rounded to binary32 before the add). - Refusals for NaN/Inf/denormal literals, pending or unknown sources, vD > 255, modifiers, clamp, omod and bare `v_mac_f32` are covered. - The `f32_mac` helper sits next to the sibling f32 helpers, and the docs entry is present. - No other open or recently merged PR covers `v_mac_f32`. - CMake test registration follows the sibling `dot_u` pattern. Commands executed locally (Linux): - `python3 tests/docs_links_check.py`: pass (18 files, 399 links) - `python3 tests/bidi_controls_check.py`: pass - `git diff --check origin/main...HEAD`: clean - `mac_f32_controls()`: pass. Mutation: the accumulator token replaced with `0x0` in the lowering made the controls fail, as expected. Mutation: `p + c` changed to `p - c` in the HLSL helper was NOT caught by the controls on Linux, because only the GPU draw test sees it. Commands executed on Win2025 (`x2w_pipe.py win2025 2164 --scope direct --targets offscreen_check --regex '^compute_store_aot_(mac_f32|dot_u|add_co)_(warp|hardware)$'`): - add_co warp/hardware: pass - dot_u warp/hardware: pass - mac_f32 warp: pass - mac_f32 hardware: FAIL (mutation escaped, see above) Not a merge candidate until the hardware test is fixed. Please push a fix and re-run. AI-assisted; commands listed were executed locally.
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!2156
No description provided.