Repository navigation
shader: lower v_mad_u64_u32 and v_mad_i64_i32 in the direct C++ compute path - #3004
legacy-code-archaeologist wants to merge 1 commit into
Conversation
…te path The direct compute frontend refused the 64-bit multiply-adds. They now follow gfx2hlsl.py's mad64: D = S0 * S1 + S2 modulo 2^64 with zero- or sign-extended 32-bit factors and a VGPR- or SGPR-pair S2. The unsigned form writes the carry-out of the 64-bit add to its VOP3B SDST through the lane-mask path of the carry adds, and carry_destination names that SDST for the symbolic state. The signed form is lowered only with the null SDST, and inline float factors are refused, as in gfx2hlsl.py. tests/native_mad64_check.py dispatches products just below 2^64 so that exactly the odd lanes carry, checks the module, three refusals and three mutated references. Refs R0Y00#3000. AI-assisted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
KelboLive
left a comment
There was a problem hiding this comment.
Strong implementation — the mad64 lowering is clean, follows gfx2hlsl.py closely, and the test suite exercises both the happy path and all the refusal edge cases well.
Findings
✅ What works well
- Correct contract alignment:
D = S0 * S1 + S2 mod 2^64with properzext/sextsemantics matchesgfx2hlsl.py::mad64. The IR generation sequence (orfor S2 concatenation →mul i64→add i64→icmp ult i64carry compare) is textbook-correct. - Carry destination:
carry_destination()addition is minimal and mirrors the SDST-parsing pattern used by the existing carry-ins (v_add_co_u32, etc.). The(i.a >> 8) & 127decode is consistent with VOP3B encoding. - Refusal logic is tight:
pixel_stage,graphics_input,subword,dpp_control,neg,clamp,scaleall correctly refused. Inline float factors (VGPR 240–248) caught.vgpr_pair/sgpr_pairguards enforce the aligned-pair requirement. - Differences:
v_mad_i64_i32with non-null SDST → refused (correct — signed carry not established).v_mad_u64_u32with null SDST → refused by the symbolic transfer (unsupported carry mask destination), matchinggfx2hlsl.pybehavior. - Test fixture: The operand generator (
mul24-based pseudo-random) produces products just below 2^64, so exactly odd lanes carry — elegant design. Three mutant references (high word dropped, signed zero-extended, no carry) each independently differ from expected. - Documentation:
native-direct-mad64.mdaccurately captures the contract, refusal paths, and fixture layout. The note about wave64 SDST pairs being out of scope is honest and useful.
⚠️ Minor observations
-
ir.features |= 1048576;— magic number. Consider whether this feature flag (bit 20) corresponds to a named constant elsewhere (e.g.FEATURE_MAD64or similar). If it's already defined, use it; if not, a// SM6.6 / mad64 feature bitcomment would help future readers. -
v_mad_i64_i32guard:sdst != 125→ allowed (125 =vccin the AMDGCN VCC enum). But 126/127 are SGPR carry destinations — the conditionsign ? sdst != 125accepts SGPRs for the signed form. The comment says "lowered only with the null SDST". Please verify whether SGPR carry destination on the signed form is intentionally allowed (it would produce garbage) or was overlooked. If intentional, add// signed: SGPR carries accepted but discardedas a note. -
Inline float guard range:
i.src[j] >= 240 && i.src[j] <= 248— this is correct as documented (VGPRs 240–248 hold inline floats in the amdgcn encodings). A one-liner comment linking to the encoding spec would help:// VGPR 240-248 = inline float in VOP2/VOP3 encoding. -
gfx2hlsl.pyalignment note: The PR description says "inline float factors are refused, as ingfx2hlsl.py" but the code comment ingfx1013_direct_compute.inlonly mentions it parenthetically. If this is a divergence point fromgfx2hlsl.py's more permissive handling, consider a// gfx2hlsl.py allows inline floats as factors — we refuse them for narrow-compatibilitynote.
Recommendation
With the one question around the signed SDST guard (point 2), this is ready to land. Otherwise clean and well-documented.
🏷 One thought: if the native compute path eventually supports wave64, the fixture and native_mad64_hardware (which skips at 77) may need a wave64-aware variant — but that's future work, not a blocking concern.
|
Thanks for the review. On the points:
AI-assisted. |
KelboLive
left a comment
There was a problem hiding this comment.
PR #3004 by @legacy-code-archaeologist — shader: lower v_mad_u64_u32 and v_mad_i64_i32 in direct compute
What works
- Correct carry_destination hook: (i.a >> 8) & 127 extracts SDST from VOP3B for v_mad_u64_u32
- SDST validation is sound: signed only allows null (125), unsigned accepts VGPR 0..105 and VCC (106)
- Addend pair checks precise: vgpr_pair = 256:510 (even pairs), sgpr_pair = 0..104 (even)
- Inline float factor refusal uses same 240-248 range as gfx2hlsl.py
- Signed form refusal for non-null SDST follows gfx2hlsl.py semantics (signed carry-out not established)
- Modifier filtering correctly handles VOP3B where ABS field holds SDST: only NEG, clamp, output modifier refused
- Test fixture uses products just below 2^64 so odd lanes carry, making carry bit deterministic (mask 0xaaaa...))
- Three mutation checks in test (high word dropped, signed factors zero-extended, no carry) all fail the readback
- Merges cleanly with #2986, #2996, #3004 per merge-tree trials
- Documentation in native-direct-mad64.md is thorough and matches existing style
Issues
- Unsigned form with i.src[j] = v_gpr256 (VGPR 256) maps to vgpr_pair = addend >= 256 && addend < 511 which gives TRUE. But the addend value 256 means VPR 256 (even), so it forms a VGPR pair starting at 256. This is correct behavior.
- The v_mad_i64_i32 check refuses sdst > 124 (since sign ? sdst != 125 is the condition). But sdst values 126 and 107-127 are also invalid for the signed form (107+ are out-of-range VGPR SDST, 126 is VCC). The condition sdst != 125 correctly rejects all non-null SDSTs for the signed form, but the error message "carry-out destination" is identical regardless of what the actual invalid sdst value is. A more specific error for VCC vs VGPR might help debug.
- The carry_destination function adds v_mad_u64_u32 alongside v_add_co_u32 and friends. This means the symbolic state tracking for carry ops now includes this form, but a v_mad_u64_u32 without a valid SDST will pass the carry_destination check and fail later at the explicit sdst validation. If the intent is to refuse earlier, move the sdst check into carry_destination.
- Test fixtures are wave32 only. The PR notes wave64 SDST pairs are untested — consider whether the lane-mask path in write_ballot needs different handling for wave64 split lanes.
Recommendations
- Consider a specific error for signed form: "v_mad_i64_i32 requires null SDST (carry-out not established for signed multiply-add)".
- Add a debug-only assertion: assert(i.dst < 511) before the inline float check so we catch mis-decoded instructions earlier.
- For the hardware test, add a note about which adapters support SM 6.6 wave32; the Quadro M1000M is Pascal (SM 6.1): this limits what CI can verify.
Overall
Clean addition following established patterns. The carry_destination hook extension is correct, the SDST validation logic is sound, and the test fixture carefully crafts inputs that exercise both carry and no-carry paths. Prioritize hardware verification on SM 6.6 hardware to confirm the carry-out lane mask behavior.
Change
The direct compute frontend now lowers
v_mad_u64_u32andv_mad_i64_i32. Before, it refused them withdirect DXIL lowering for <name>. Refs #3000, part of the integer-ALU coverage in #2935.The contract is
gfx2hlsl.py'smad64: D = S0 * S1 + S2 modulo 2^64, with zero- or sign-extended 32-bit factors and a 64-bit S2.v_mad_u64_u32. The carry-out of the 64-bit add (sum < S2, unsigned) goes to its VOP3B SDST, VCC or an SGPR, through the lane-mask path (write_ballot) the carry adds use.carry_destinationinsrc/gfx1013_compute_native.cppnow names that SDST, so the symbolic state forgets the SGPR. With the null SDST it is refused (unsupported carry mask destination), as ingfx2hlsl.py.v_mad_i64_i32. Lowered only with the null SDST; the signed carry-out is not established. Any other SDST is refused.gfx2hlsl.py. Inline float factors are refused, as ingfx2hlsl.py.The new block sits right before the carry block, apart from the integer-ALU chain. This branch therefore merges cleanly with #2986, #2996 and #2998 (merge trials on
e0d83759).Verification
Commit on
cpp-migratione0d83759, Windows 10.0.26100.9711,devpreset. GPU: NVIDIA Quadro M1000M, driver 32.0.15.7371, debug layer unavailable.ctest --preset dev --no-tests=error -R '^native_mad64_|^native_direct_container$|^native_(sad|signed_forms|shift64)_controls$' -V: 5 passed, 1 skipped.native_mad64_controlschecks the module for twomul i64, a sign extension and the unsigned carry compare. It also checks three refusals: the signed form with an SGPR carry-out, the unsigned form with null, and an inline float factor. Both groups have mixed carries, and three mutated references differ (high word dropped, signed factors zero-extended, no carry).native_mad64_hardwareskipped (77). It needs shader model 6.6 with wave32. The readback is not measured here.python -B tests/docs_links_check.py: ok.Evidence and follow-up
All words were assembled with LLVM 15.0.7 (
clang -cc1as -triple amdgcn -target-cpu gfx1013 -filetype asm -show-encoding).With n = lane + 32 * group:
The products lie just below 2^64, so exactly the odd lanes carry (mask 0xaaaaaaaa). Each lane stores both results and the mask copied from s30.
Not covered: wave64 SDST pairs, because the fixture is wave32. The note is
docs/experiments/native-direct-mad64.md.Checklist
AI-assisted.
🤖 Generated with Claude Code