Skip to content

shader: lower v_mad_u64_u32 and v_mad_i64_i32 in the direct C++ compute path - #3004

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

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

Conversation

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Change

The direct compute frontend now lowers v_mad_u64_u32 and v_mad_i64_i32. Before, it refused them with direct DXIL lowering for <name>. Refs #3000, part of the integer-ALU coverage in #2935.

The contract is gfx2hlsl.py's mad64: 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_destination in src/gfx1013_compute_native.cpp now names that SDST, so the symbolic state forgets the SGPR. With the null SDST it is refused (unsupported carry mask destination), as in gfx2hlsl.py.
  • v_mad_i64_i32. Lowered only with the null SDST; the signed carry-out is not established. Any other SDST is refused.
  • Operands. S2 must be a VGPR pair or an aligned SGPR pair. Inline and literal 64-bit addends stay refused, which is narrower than gfx2hlsl.py. Inline float factors are refused, as in gfx2hlsl.py.
  • Modifiers. In VOP3B the ABS field holds SDST, so only NEG, clamp and the output modifier are refused among the modifiers. SDWA, DPP, pixel stages and graphics inputs are refused as for the carry adds.

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-migration e0d83759, Windows 10.0.26100.9711, dev preset. 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_controls checks the module for two mul 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_hardware skipped (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:

  • S0 = mul_u32_u24(n, 0x0f0f0f) ^ 0xffff0000
  • S1 = mul_u32_u24(n, 0x123457) ^ 0xfff00000
  • S2 = {((n & 1) << 31) | 0x10, mul_u32_u24(n, 0x777777) ^ 0x89abcdef}

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

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

🤖 Generated with Claude Code

…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 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.

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^64 with proper zext/sext semantics matches gfx2hlsl.py::mad64. The IR generation sequence (or for S2 concatenation → mul i64 → add i64 → icmp ult i64 carry 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) & 127 decode is consistent with VOP3B encoding.
  • Refusal logic is tight: pixel_stage, graphics_input, subword, dpp_control, neg, clamp, scale all correctly refused. Inline float factors (VGPR 240–248) caught. vgpr_pair / sgpr_pair guards enforce the aligned-pair requirement.
  • Differences: v_mad_i64_i32 with non-null SDST → refused (correct — signed carry not established). v_mad_u64_u32 with null SDST → refused by the symbolic transfer (unsupported carry mask destination), matching gfx2hlsl.py behavior.
  • 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.md accurately captures the contract, refusal paths, and fixture layout. The note about wave64 SDST pairs being out of scope is honest and useful.

⚠️ Minor observations

  1. ir.features |= 1048576; — magic number. Consider whether this feature flag (bit 20) corresponds to a named constant elsewhere (e.g. FEATURE_MAD64 or similar). If it's already defined, use it; if not, a // SM6.6 / mad64 feature bit comment would help future readers.

  2. v_mad_i64_i32 guard: sdst != 125 → allowed (125 = vcc in the AMDGCN VCC enum). But 126/127 are SGPR carry destinations — the condition sign ? sdst != 125 accepts 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 discarded as a note.

  3. 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.

  4. gfx2hlsl.py alignment note: The PR description says "inline float factors are refused, as in gfx2hlsl.py" but the code comment in gfx1013_direct_compute.inl only mentions it parenthetically. If this is a divergence point from gfx2hlsl.py's more permissive handling, consider a // gfx2hlsl.py allows inline floats as factors — we refuse them for narrow-compatibility note.

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.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

Thanks for the review. On the points:

  1. The signed guard refuses everything except null. sign ? sdst != 125 : (...) sits inside refuse(...), so for v_mad_i64_i32 any SDST other than 125 is refused. On gfx10, operand 125 is null (SGPR_NULL); VCC is 106. The control v_mad_i64_i32 v[10:11], s30, ... checks that an SGPR carry-out is refused with v_mad_i64_i32 carry-out destination.
  2. 240–248 are not VGPRs. They are the inline floating-point constant operand codes (0.5, -0.5, 1.0, -1.0, 2.0, -2.0, 4.0, -4.0, 1/(2π)), and VGPRs start at 256 in this encoding. The refusal is the gfx2hlsl.py rule: mad64 raises Unsupported for a factor in FLOATS. So this is not a divergence from gfx2hlsl.py.
  3. Bit 20 (1048576) is the DXIL Int64Ops shader flag. The existing 64-bit lowerings in this file (v_lshrrev_b64 and 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.

@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 #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.

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