Repository navigation
shader: lower unfused f32 multiply-adds in the direct compute path - #3007
legacy-code-archaeologist wants to merge 1 commit into
Conversation
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
@KelboLive Thanks for the reviews. On the points raised:
|
Summary
Lowers
v_mad_f32,v_mad_legacy_f32,v_mac_f32andv_mac_legacy_f32in the direct C++ compute path (float_alu). They were refused withdirect DXIL lowering for <name>(the VOP2 accumulators had no lowering). The semantics aregfx2hlsl.py'smad32, whichpixel_aotshares:v_mac_*add the destination's raw bits);The product and the sum are plain
fmul/faddwith no fast-math flags, so DXIL does not allow contraction intoFMad. Av_mac_*VOP3 form with abs or neg on the accumulator (src2) is refused instead of being dropped silently. The new block sits infloat_alu, away from the integer ALU chain that the open slices #2986, #2996, #2998 and #3004 edit.git merge-treeagainst each of those branches and againstcpp-migrationreports 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:The VOP3/VOP2 words were assembled with LLVM 15.0.7 (
clang -cc1as, gfx1013).ctest --preset dev --no-tests=error -R '^native_mad32_' -V:dx.op.tertiary46, and no fast/contract flags. The fused, unflushed and legacy-as-plain references each differ. An accumulator neg is refused.ctest --preset dev --no-tests=error -R '^native_'gives 121 of 125 passed. The 4 failures are the hardware testsnative_graphics_vertex_vcc_join,native_rtv_variant,native_startup_request_cacheandstartup_compute_dispatch_native. They fail identically with this change reverted, one0d83759(adapter/PSO limits of this machine).Environment: Windows 10.0.26100, VS 2026 BuildTools 18, base
cpp-migratione0d83759.AI-assisted: written with Claude Code. I reviewed it and ran the commands above.
🤖 Generated with Claude Code