shader: lower v_mad_u64_u32 and v_mad_i64_i32 in the direct C++ compute path #3004
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3004"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T04:12:25Z.
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
Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T04:14:15Z.
Review: COMMENTED
Strong implementation — the
mad64lowering is clean, followsgfx2hlsl.pyclosely, and the test suite exercises both the happy path and all the refusal edge cases well.Findings
✅ What works well
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()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.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.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.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.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.Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T04:18:10Z.
Thanks for the review. On the points:
sign ? sdst != 125 : (...)sits insiderefuse(...), so forv_mad_i64_i32any SDST other than 125 is refused. On gfx10, operand 125 isnull(SGPR_NULL); VCC is 106. The controlv_mad_i64_i32 v[10:11], s30, ...checks that an SGPR carry-out is refused withv_mad_i64_i32 carry-out destination.gfx2hlsl.pyrule:mad64raisesUnsupportedfor a factor inFLOATS. So this is not a divergence fromgfx2hlsl.py.1048576) is the DXIL Int64Ops shader flag. The existing 64-bit lowerings in this file (v_lshrrev_b64and the other 64-bit shifts) set the same literal without a named constant. I kept the convention rather than introduce one constant in a single place.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-27T05:21:36Z.
Review: COMMENTED
PR #3004 by @legacy-code-archaeologist — shader: lower v_mad_u64_u32 and v_mad_i64_i32 in direct compute
What works
Issues
Recommendations
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.
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-27T15:56:47Z.
@R0Y00 Moved #3004 (
shader/direct-mad64) ontomain(811c4ceb-> commit96c3b49a), keeping tests and review history intact. No dependency oncpp-migrationwas exposed (carry_destination()merged cleanly andarithmetic()only needed ordering after the new packed-16 handler).Verified on
96c3b49a:compile_dxil,native_store_replay,native_compute_ir_dumpnative_mad64_controls: Passed (0.78s)^native_.*_(controls|ir)$:37/37passed (19.43s)tests/docs_links_check.py: passed (92files,586links)native_mad64_hardware: skipped (77, Quadro M1000M SM 6.1 lacks SM 6.6 wave32; GPU readback unmeasured locally)AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-27T16:05:23Z.
Review: APPROVED
Independently reviewed
96c3b49aintegrated onmain55669240(tree35f9b13ec667b265007a0cf06415eae6091f0519). The factors are extended before multiplication, the addend is reconstructed from both words, and comparing the wrapped unsigned sum with the full addend identifies carry. The wave32 mask is written through the existing ballot path. The signed form remains limited to discarded carry.Built
compile_dxil native_store_replay native_compute_ir_dumpwithdevandrelease. Ranctest --test-dir <build> --no-tests=error -j 1 -V -R '^native_(mad64|mad32|salu64|pk_mul_mad_16)_'. Debug and Release/O2each passed 8/8, zero skips. Both signed and unsigned MAD outputs, the unsigned carry masks and guards matched. The operand refusal controls plus adjacent scalar, packed and unfused f32 MAD regressions passed.git diff --check HEAD^1 HEADpassed.NVIDIA GeForce RTX 4060 Laptop GPU, driver
32.0.16.1656, Windows26200.9974, SDK/DXC10.0.26100.0, wave32, Int64Ops, D3D12 debug layer disabled. The successful Windows, Linux Python and gfx1013 checks are from the rebased head. Hosted/O2skipped; local/O2passed.