Skip to content

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

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
legacy-code-archaeologist:shader/direct-mad32
Nov 27, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
legacy-code-archaeologist:shader/direct-mad32

Conversation

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

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

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

@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 Nov 27, 2026

Copy link
Copy Markdown
Owner

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.

v_mad_f32, v_mad_legacy_f32, v_mac_f32 and v_mac_legacy_f32 follow
gfx2hlsl.py's mad32: denormal inputs flushed to signed zero, a separately
rounded product (the legacy forms make 0 * anything +0), a flushed addend
and a flushed sum. Plain fmul and fadd without fast-math flags, so DXIL
does not contract them into FMad. A v_mac VOP3 form with abs or neg on the
accumulator is refused.

Refs R0Y00#3005

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	src/gfx1013_direct_compute.inl
@legacy-code-archaeologist
legacy-code-archaeologist changed the base branch from cpp-migration to main November 27, 2026 15:55
@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

@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 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@R0Y00
R0Y00 merged commit 5566924 into R0Y00:main Nov 27, 2026
6 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants