Skip to content

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

Open
legacy-code-archaeologist wants to merge 1 commit into
R0Y00:cpp-migrationfrom
legacy-code-archaeologist:shader/direct-mad32
Open

legacy-code-archaeologist wants to merge 1 commit into
R0Y00:cpp-migrationfrom
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

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>

@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 \

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.

2 participants