shader: lower unfused f32 multiply-adds in the direct compute path #3007

Merged
legacy-code-archaeologist merged 1 commit from upstream-pr/3007 into main 2026-11-27 16:02:54 +00:00
legacy-code-archaeologist commented 2026-11-27 04:35:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T04:35:07Z.


Summary

Lowers v_mad_f32, v_mad_legacy_f32, v_mac_f32 and v_mac_legacy_f32 in the direct C++ compute path (float_alu). They were refused with direct DXIL lowering for <name> (the VOP2 accumulators had no lowering). The semantics are gfx2hlsl.py's mad32, which pixel_aot shares:

  • denormal inputs flushed to signed zero (an integer mask that keeps the sign);
  • the product rounded on its own, and the legacy forms make 0 * anything +0;
  • the flushed addend added (v_mac_* add the destination's raw bits);
  • the sum flushed.

The product and the sum are plain fmul/fadd with no fast-math flags, so DXIL does not allow contraction into FMad. A v_mac_* VOP3 form with abs or neg on the accumulator (src2) is refused instead of being dropped silently. The new block sits in float_alu, away from the integer ALU chain that the open slices #2986, #2996, #2998 and #3004 edit. git merge-tree against each of those branches and against cpp-migration reports no conflict.

Refs #3005, refs #2935

Tests

tests/native_mad32_check.py, tests/cmake/native_mad32.cmake, docs/experiments/native-direct-mad32.md. There are six cases, each checked in every lane of two groups:

  • (1+2^-12)^2-1, where fused and unfused rounding differ;
  • a denormal input;
  • 0·inf;
  • -0·1 + -0;
  • a denormal sum;
  • an ordinary value.

The VOP3/VOP2 words were assembled with LLVM 15.0.7 (clang -cc1as, gfx1013).

  • ctest --preset dev --no-tests=error -R '^native_mad32_' -V:
    • controls passed. They check for separate fmul and fadd, no dx.op.tertiary 46, and no fast/contract flags. The fused, unflushed and legacy-as-plain references each differ. An accumulator neg is refused.
    • hardware skipped (77) on NVIDIA Quadro M1000M (driver 32.0.15.7371, debug layer unavailable): the readback needs SM 6.6 wave32.
    • The readback is not measured. That includes whether a desktop driver honours the missing contract flag. A run on SM 6.6 hardware would be welcome.
  • With the refusal removed, the controls fail. With it restored, they pass.
  • ctest --preset dev --no-tests=error -R '^native_' gives 121 of 125 passed. The 4 failures are the hardware tests native_graphics_vertex_vcc_join, native_rtv_variant, native_startup_request_cache and startup_compute_dispatch_native. They fail identically with this change reverted, on e0d83759 (adapter/PSO limits of this machine).

Environment: Windows 10.0.26100, VS 2026 BuildTools 18, base cpp-migration e0d83759.

AI-assisted: written with Claude Code. I reviewed it and ran the commands above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5521252081 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3007). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T04:35:07Z. --- ## Summary Lowers `v_mad_f32`, `v_mad_legacy_f32`, `v_mac_f32` and `v_mac_legacy_f32` in the direct C++ compute path (`float_alu`). They were refused with `direct DXIL lowering for <name>` (the VOP2 accumulators had no lowering). The semantics are `gfx2hlsl.py`'s `mad32`, which `pixel_aot` shares: - denormal inputs flushed to signed zero (an integer mask that keeps the sign); - the product rounded on its own, and the legacy forms make 0 * anything +0; - the flushed addend added (`v_mac_*` add the destination's raw bits); - the sum flushed. The product and the sum are plain `fmul`/`fadd` with no fast-math flags, so DXIL does not allow contraction into `FMad`. A `v_mac_*` VOP3 form with abs or neg on the accumulator (src2) is refused instead of being dropped silently. The new block sits in `float_alu`, away from the integer ALU chain that the open slices #2986, #2996, #2998 and #3004 edit. `git merge-tree` against each of those branches and against `cpp-migration` reports no conflict. Refs #3005, refs #2935 ## Tests `tests/native_mad32_check.py`, `tests/cmake/native_mad32.cmake`, `docs/experiments/native-direct-mad32.md`. There are six cases, each checked in every lane of two groups: - (1+2^-12)^2-1, where fused and unfused rounding differ; - a denormal input; - 0·inf; - -0·1 + -0; - a denormal sum; - an ordinary value. The VOP3/VOP2 words were assembled with LLVM 15.0.7 (`clang -cc1as`, gfx1013). - `ctest --preset dev --no-tests=error -R '^native_mad32_' -V`: - controls **passed**. They check for separate fmul and fadd, no `dx.op.tertiary` 46, and no fast/contract flags. The fused, unflushed and legacy-as-plain references each differ. An accumulator neg is refused. - hardware **skipped (77)** on NVIDIA Quadro M1000M (driver 32.0.15.7371, debug layer unavailable): the readback needs SM 6.6 wave32. - **The readback is not measured.** That includes whether a desktop driver honours the missing contract flag. A run on SM 6.6 hardware would be welcome. - With the refusal removed, the controls fail. With it restored, they pass. - `ctest --preset dev --no-tests=error -R '^native_'` gives 121 of 125 passed. The 4 failures are the hardware tests `native_graphics_vertex_vcc_join`, `native_rtv_variant`, `native_startup_request_cache` and `startup_compute_dispatch_native`. They fail identically with this change reverted, on `e0d83759` (adapter/PSO limits of this machine). Environment: Windows 10.0.26100, VS 2026 BuildTools 18, base `cpp-migration` `e0d83759`. AI-assisted: written with Claude Code. I reviewed it and ran the commands above. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
KelboLive commented 2026-11-27 05:17:32 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T05:17:32Z.

Review: COMMENTED


PR #3007 by @legacy-code-archaeologist — shader: lower unfused f32 multiply-adds in the direct compute path

What works

  • Clean separation of mad32 semantics from the integer ALU chain (placed in float_alu, away from #2986, #2996, #3004)
  • Denormal flush lambda (ftz) correctly detects subnormals via comparison against 0x800000 (2^-126 threshold)
  • Sign preservation in the flush mask is correct: tiny ? 0x80000000 : 0xFFFFFFFF
  • Four forms covered: v_mad_f32, v_mad_legacy_f32, v_mac_f32, v_mac_legacy_f32
  • Test cases cover rounding differences ((1+2^-12)^2-1), denormals, 0*inf, signed zeros, denormal sums
  • Controls check for separate fmul/fadd (no contract), no dx.op.tertiary=46, and neg-accumulator refusal
  • No fast-math flags on fmul/fadd, correct: DXIL cannot contract into FMad without the contract flag
  • CMakeLists integration proper with SKIP_RETURN_CODE 77 for the hardware lane
  • git merge-tree checks against #2986, #2996, #3004, cpp-migration show no conflicts

Issues

  • The PR notes gfx2hlsl.py has no v_mac_legacy_f32 entry; semantics are derived from pixel_aot_check.py. This is acceptable for research, but the assumption should be explicitly flagged as unverified against hardware — legacy MAC semantics are tricky (0*anything = 0 behavior, denormal flush on addend vs product only).
  • The ftz threshold 8388608 (0x800000) is correct for subnormal detection in IEEE 754 binary32, but consider adding a comment with the hex value and a static_assert to prevent future accidental changes (e.g., 8388608 = FLT_MIN as integer encoding = 2^-126).
  • For v_mac_legacy forms: the legacy 0*anything=+0 behavior needs to apply to the product specifically, not the final sum. The code must ensure that if the product bit pattern is 0x00000000 (exact zero) and the form is legacy, the product is rewritten as +0 before the fadd. Missing this detail in the inline excerpt makes it hard to verify.
  • The test hardware skip is expected (SM 6.6 wave32 needed), but the controls-only verification means the rounding behavior is not measured end-to-end. The test exercises the compile pipeline but the rounding correctness depends on runtime behavior.

Recommendations

  • Add a hex comment: denormal_threshold = 0x800000U (2^-126) to prevent accidental decimal changes.
  • Where the legacy 0-product-to-+0 logic sits, add a FIXME or TODO if not yet hardware-verified.
  • Consider adding a v_mac_legacy_control test that verifies the refusal of abs/neg on accumulator (src2).

Overall

Well-scoped addition to the direct compute f32 path. The ftz design is sound and follows IEEE 754 subnormal rules. The controls verify the compilation pipeline is correct, and the test cases target the critical rounding-difference boundary. Prioritize hardware measurement on SM 6.6 hardware to confirm the rounding behavior matches console expectations.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6083793670 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3007#pullrequestreview-6083793670). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-27T05:17:32Z. **Review:** COMMENTED --- PR #3007 by @legacy-code-archaeologist — shader: lower unfused f32 multiply-adds in the direct compute path ## What works - Clean separation of mad32 semantics from the integer ALU chain (placed in float_alu, away from #2986, #2996, #3004) - Denormal flush lambda (ftz) correctly detects subnormals via comparison against 0x800000 (2^-126 threshold) - Sign preservation in the flush mask is correct: tiny ? 0x80000000 : 0xFFFFFFFF - Four forms covered: v_mad_f32, v_mad_legacy_f32, v_mac_f32, v_mac_legacy_f32 - Test cases cover rounding differences ((1+2^-12)^2-1), denormals, 0*inf, signed zeros, denormal sums - Controls check for separate fmul/fadd (no contract), no dx.op.tertiary=46, and neg-accumulator refusal - No fast-math flags on fmul/fadd, correct: DXIL cannot contract into FMad without the contract flag - CMakeLists integration proper with SKIP_RETURN_CODE 77 for the hardware lane - git merge-tree checks against #2986, #2996, #3004, cpp-migration show no conflicts ## Issues - The PR notes gfx2hlsl.py has no v_mac_legacy_f32 entry; semantics are derived from pixel_aot_check.py. This is acceptable for research, but the assumption should be explicitly flagged as unverified against hardware — legacy MAC semantics are tricky (0*anything = 0 behavior, denormal flush on addend vs product only). - The ftz threshold 8388608 (0x800000) is correct for subnormal detection in IEEE 754 binary32, but consider adding a comment with the hex value and a static_assert to prevent future accidental changes (e.g., 8388608 = FLT_MIN as integer encoding = 2^-126). - For v_mac_legacy forms: the legacy 0*anything=+0 behavior needs to apply to the product specifically, not the final sum. The code must ensure that if the product bit pattern is 0x00000000 (exact zero) and the form is legacy, the product is rewritten as +0 before the fadd. Missing this detail in the inline excerpt makes it hard to verify. - The test hardware skip is expected (SM 6.6 wave32 needed), but the controls-only verification means the rounding behavior is not measured end-to-end. The test exercises the compile pipeline but the rounding correctness depends on runtime behavior. ## Recommendations - Add a hex comment: denormal_threshold = 0x800000U (2^-126) to prevent accidental decimal changes. - Where the legacy 0-product-to-+0 logic sits, add a FIXME or TODO if not yet hardware-verified. - Consider adding a v_mac_legacy_control test that verifies the refusal of abs/neg on accumulator (src2). ## Overall Well-scoped addition to the direct compute f32 path. The ftz design is sound and follows IEEE 754 subnormal rules. The controls verify the compilation pipeline is correct, and the test cases target the critical rounding-difference boundary. Prioritize hardware measurement on SM 6.6 hardware to confirm the rounding behavior matches console expectations.
KelboLive commented 2026-11-27 05:33:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T05:33:03Z.

Review: COMMENTED


PR #3007 by @legacy-code-archaeologist -- shader: lower unfused f32 multiply-adds in the direct compute path

What works

  • Well-documented unfused f32 mad lowering that precisely replicates gfx2hlsl.py mad32 semantics
  • Correct handling of legacy forms: 0*anything=+0, denormal flushing to signed zero, sum flushing
  • The ftz helper uses 8388608 (2^23) for denormal detection -- correct IEEE754 single-precision threshold
  • Good approach of using plain fmul and fadd without fast-math flags to prevent DXIL contraction
  • The test fixture covers denormal edge cases (1+2^-12)^2-1, 0inf, and -01+-0 thoroughly

Issues

  • The v_mac_* forms add the destination as the third operand, but the ftz logic applies to product and sum separately -- verify the addend itself (src2 for v_mac) is also subject to the same flushing as the addend in v_mad
  • Consider adding a note about the performance cost: 4 floating-point ops (fmul, fadd) vs. 1 fused FMad may impact performance-critical paths
    • This seems like a direct compute path (DXIL), so performance may be less critical, worth noting

Recommendations

  • The implementation correctly refuses v_mac_v op1/v_mac_v op2 with abs or neg on accumulator, matching pixel_aot
  • Consider adding a TODO comment about adding v_mac to gpu2hlsl.py if this path gains wider adoption

Overall

A clean, correct extension of the direct compute path. The semantics match the existing gfx2hlsl reference. Ready for merge after the denormal flush edge case is confirmed.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6083896355 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3007#pullrequestreview-6083896355). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-27T05:33:03Z. **Review:** COMMENTED --- PR #3007 by @legacy-code-archaeologist -- shader: lower unfused f32 multiply-adds in the direct compute path ## What works - Well-documented unfused f32 mad lowering that precisely replicates gfx2hlsl.py mad32 semantics - Correct handling of legacy forms: 0*anything=+0, denormal flushing to signed zero, sum flushing - The ftz helper uses 8388608 (2^23) for denormal detection -- correct IEEE754 single-precision threshold - Good approach of using plain fmul and fadd without fast-math flags to prevent DXIL contraction - The test fixture covers denormal edge cases (1+2^-12)^2-1, 0*inf, and -0*1+-0 thoroughly ## Issues - The v_mac_* forms add the destination as the third operand, but the ftz logic applies to product and sum separately -- verify the addend itself (src2 for v_mac) is also subject to the same flushing as the addend in v_mad - Consider adding a note about the performance cost: 4 floating-point ops (fmul, fadd) vs. 1 fused FMad may impact performance-critical paths - This seems like a direct compute path (DXIL), so performance may be less critical, worth noting ## Recommendations - The implementation correctly refuses v_mac_v op1/v_mac_v op2 with abs or neg on accumulator, matching pixel_aot - Consider adding a TODO comment about adding v_mac to gpu2hlsl.py if this path gains wider adoption ## Overall A clean, correct extension of the direct compute path. The semantics match the existing gfx2hlsl reference. Ready for merge after the denormal flush edge case is confirmed.
legacy-code-archaeologist commented 2026-11-27 12:40:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T12:40:21Z.


@KelboLive Thanks for the reviews. On the points raised:

  • Accumulator FTZ on \ mac*: \c\ (
    ead(i.dst)\ for \ _mac_f32\ / \ _mac_legacy_f32, \ p(2)\ for \ _mad*) goes through \ tz(c)\ identically in
    esult = ftz(fadd(ftz(product), ftz(c))).
  • **Legacy \
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7218534929 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3007#issuecomment-7218534929). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T12:40:21Z. --- @KelboLive Thanks for the reviews. On the points raised: - **Accumulator FTZ on \ _mac_*\:** \c\ (\ ead(i.dst)\ for \ _mac_f32\ / \ _mac_legacy_f32\, \ p(2)\ for \ _mad*\) goes through \ tz(c)\ identically in \ esult = ftz(fadd(ftz(product), ftz(c)))\. - **Legacy \
R0Y00 commented 2026-11-27 15:50:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-27T15:50:44Z.


I checked this PR and your #3004 and #3011 against main 354c6870. Each PR compares to main with the intended feature delta, and each currently conflicts in src/gfx1013_direct_compute.inl. Please move them to main in their existing PRs as you refresh the branches, keeping their tests and existing review history. #3007 can go first because its floating-point handler is separate from the two integer changes. Let me know on the relevant PR if a rebase exposes a dependency, and I’ll review the updated integrated head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7222550863 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3007#issuecomment-7222550863). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-27T15:50:44Z. --- I checked this PR and your #3004 and #3011 against `main` `354c6870`. Each PR compares to `main` with the intended feature delta, and each currently conflicts in `src/gfx1013_direct_compute.inl`. Please move them to `main` in their existing PRs as you refresh the branches, keeping their tests and existing review history. #3007 can go first because its floating-point handler is separate from the two integer changes. Let me know on the relevant PR if a rebase exposes a dependency, and I’ll review the updated integrated head.
legacy-code-archaeologist commented 2026-11-27 15:55:17 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T15:55:17Z.


@R0Y00 Moved #3007 (shader/direct-mad32) onto main (f6691b67 -> commit 9869cc04), keeping tests and review history intact. No dependency on cpp-migration was exposed (only the float_alu() guard line needed merging alongside v_cvt_off_f32_i4).

Verified on 9869cc04:

  • Built compile_dxil, native_store_replay, native_compute_ir_dump
  • native_mad32_controls: Passed (0.65s)
  • ^native_.*_(controls|ir)$: 37/37 passed (19.41s)
  • tests/docs_links_check.py: passed (92 files, 586 links)
  • native_mad32_hardware: skipped (77, Quadro M1000M SM 6.1 lacks SM 6.6 wave32; GPU readback unmeasured locally)

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7222640994 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3007#issuecomment-7222640994). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-27T15:55:17Z. --- @R0Y00 Moved #3007 (`shader/direct-mad32`) onto `main` (`f6691b67` -> commit `9869cc04`), keeping tests and review history intact. No dependency on `cpp-migration` was exposed (only the `float_alu()` guard line needed merging alongside `v_cvt_off_f32_i4`). Verified on `9869cc04`: - Built `compile_dxil`, `native_store_replay`, `native_compute_ir_dump` - `native_mad32_controls`: Passed (`0.65s`) - `^native_.*_(controls|ir)$`: `37/37` passed (`19.41s`) - `tests/docs_links_check.py`: passed (`92` files, `586` links) - `native_mad32_hardware`: skipped (`77`, Quadro M1000M SM 6.1 lacks SM 6.6 wave32; GPU readback unmeasured locally) AI-assisted.
R0Y00 commented 2026-11-27 16:02:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-27T16:02:41Z.

Review: APPROVED


Independently reviewed 9869cc04 integrated on main 6c2cddec (tree 327ebf9973ddb45be26d2146a88563fb6c864647). The combined float_alu gate retains the adjacent i4 conversion. I checked separately rounded multiply/add, signed-zero flushing for inputs and intermediates, legacy zero-product handling, and the destination accumulator for both MAC forms.

Built compile_dxil native_store_replay native_compute_ir_dump in dev and release. Ran ctest --test-dir <build> --no-tests=error -j 1 -V -R '^native_(mad32|mad_f16|fma|v_f32)_'. Only the two MAD32 tests match on this tree. Debug and Release /O2 both passed 2/2 with no skips. Hardware replay matched 2,064 words (NaNs compared by class). Compile controls verified distinct fmul and fadd with no FMad or fast-math flags, rejected the accumulator modifier and detected the fused, unflushed and legacy-as-plain reference mutations. git diff --check HEAD^1 HEAD passed.

NVIDIA GeForce RTX 4060 Laptop GPU, driver 32.0.16.1656, Windows 26200.9974, SDK/DXC 10.0.26100.0, wave32, D3D12 debug layer disabled. Windows, Linux Python and gfx1013 CI succeeded on this head. Hosted /O2 was skipped.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6090727591 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3007#pullrequestreview-6090727591). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-27T16:02:41Z. **Review:** APPROVED --- Independently reviewed `9869cc04` integrated on `main` `6c2cddec` (tree `327ebf9973ddb45be26d2146a88563fb6c864647`). The combined `float_alu` gate retains the adjacent i4 conversion. I checked separately rounded multiply/add, signed-zero flushing for inputs and intermediates, legacy zero-product handling, and the destination accumulator for both MAC forms. Built `compile_dxil native_store_replay native_compute_ir_dump` in `dev` and `release`. Ran `ctest --test-dir <build> --no-tests=error -j 1 -V -R '^native_(mad32|mad_f16|fma|v_f32)_'`. Only the two MAD32 tests match on this tree. Debug and Release `/O2` both passed 2/2 with no skips. Hardware replay matched 2,064 words (NaNs compared by class). Compile controls verified distinct `fmul` and `fadd` with no FMad or fast-math flags, rejected the accumulator modifier and detected the fused, unflushed and legacy-as-plain reference mutations. `git diff --check HEAD^1 HEAD` passed. NVIDIA GeForce RTX 4060 Laptop GPU, driver `32.0.16.1656`, Windows `26200.9974`, SDK/DXC `10.0.26100.0`, wave32, D3D12 debug layer disabled. Windows, Linux Python and gfx1013 CI succeeded on this head. Hosted `/O2` was skipped.
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!3007
No description provided.