Skip to content

Shaders: lower v_mad_u64_u32 and v_mad_i64_i32 - #171

Open
n-romanovich wants to merge 1 commit into
Marvisak:mainfrom
n-romanovich:feat/valu-mad64
Open

n-romanovich wants to merge 1 commit into
Marvisak:mainfrom
n-romanovich:feat/valu-mad64

Conversation

@n-romanovich

@n-romanovich n-romanovich commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

emit::lower previously lowered v_mad_u64_u32 and v_mad_i64_i32 in crates/victory-shader/src/emit/lower.rs, but only emitted the carry-out lane mask (SDST) for the unsigned form, causing shaders that read v_mad_i64_i32's SDST to be refused.

This PR adds signed carry-out for v_mad_i64_i32 as bit 64 of the exact 65-bit result, refuses clamp on both instructions, and adds a GPU execution test.

Changes

  • crates/victory-shader/src/emit/lower.rs (v_mad_u64_u32 | v_mad_i64_i32):
    • Refuse v.clamp with Err("a clamped integer operation").
    • Compute sum = prod + acc and unsigned carry ucarry = sum < prod (equivalent to sum < acc).
    • For v_mad_i64_i32 (signed == true), derive the signed carry-out (bit 64 of the exact 65-bit signed result) as ucarry ^ (prod < 0) ^ (acc < 0) and write carry to self.lane_bits for v.carry on both signed and unsigned forms.
  • crates/victory-shader/src/emit/mod.rs (emit::tests::valu_mad64_executes_on_gpu):
    • Add a GPU execution test covering v_mad_u64_u32 and v_mad_i64_i32 carry-out across -3 * 5 + 0, -3 * 5 + 25, and -15 + i64::MIN, as well as v.clamp refusal.

Verification

  • Hardware: Windows 11 x64, NVIDIA GeForce RTX 5060 Ti.
  • cargo test -p victory-shader --locked -- mad64: 1 passed; 0 failed (emit::tests::valu_mad64_executes_on_gpu ... ok).
  • No game data used.
  • AI-assisted (authored and verified with Antigravity).

@DaviTostes

Copy link
Copy Markdown

Tested 60b63b4 on Arch Linux x86_64, rustc 1.98.1: pass.

No game data used.

@Sunkiller-qc

Copy link
Copy Markdown

The unsigned form looks right. For v_mad_i64_i32, though, the SDST bit isn't signed overflow. AMD's machine-readable ISA spec defines it as { D1.i1, D0.i64 } = 65'I(S0.i32) * 65'I(S1.i32) + 65'I(S2.i64), so D1 is bit 64 of the exact 65-bit sum, i.e. unsigned_carry ^ sign(prod) ^ sign(acc). The overflow check at crates/victory-shader/src/emit/lower.rs:3540 gives a different result whenever the true sum is negative and doesn't overflow: for example, -3 * 5 + 0 should set the bit, but here it's 0. The test cases (10 and i64::MIN - 15) agree under both definitions, so they don't catch it.

#163 lowers the same two opcodes (wide_mad, lower.rs:5043 there) with the 65-bit form, and it also refuses clamp, which this version ignores.

Static review, not built or run.

@n-romanovich
n-romanovich force-pushed the feat/valu-mad64 branch 2 times, most recently from d9c8525 to a5af45f Compare November 24, 2026 01:39
@n-romanovich

Copy link
Copy Markdown
Author

Thanks for the review, @Sunkiller-qc!

Addressed in a5af45f:

  • Updated v_mad_i64_i32 carry-out to compute bit 64 of the 65-bit sum (unsigned_carry ^ sign(prod) ^ sign(acc)).
  • Refused v.clamp on both v_mad_u64_u32 and v_mad_i64_i32.
  • Added the -3 * 5 + 0 (SDST = 1) case to valu_mad64_executes_on_gpu and re-verified cargo test -p victory-shader (70 passed) on D3D12 (RTX 5060 Ti).

@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

A note from comparing this with #163, which lowers the same two instructions: the 64-bit results agree, but the carry-out of v_mad_i64_i32 differs. This PR writes signed overflow of the 64-bit add; AMD's references define the flag as the top bit of the 65-bit result:

  • RDNA 3 ISA (Feb 2023), V_MAD_I64_I32: { D1.i1, D0.i64 } = 65'B(65'I(S0.i) * 65'I(S1.i) + 65'I(S2.i64))
  • RDNA 2 ISA, opcode 375: {vcc_out,D.i64} = S0.i32 * S1.i32 + S2.i64

With the operands sign-extended to 65 bits, bit 64 is the unsigned carry out of bit 63 xor the signs of the product and the addend. The two differ when both addends are negative and the add does not overflow, e.g. (-1) * 1 + (-1) = -2: bit 64 is 1, signed overflow is 0. The unsigned form here (sum < prod) matches the reference.

I have not run this on hardware; the above is from the documents only. No game data used.

@n-romanovich

Copy link
Copy Markdown
Author

Thanks, @r4tedx! This was updated in a5af45f to compute bit 64 of the 65-bit sum (unsigned_carry ^ sign(prod) ^ sign(acc)), so cases like -3 * 5 + 0 and (-1) * 1 + (-1) set SDST = 1 per the RDNA2/RDNA3 ISA spec.

edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement of both instructions, including the carry-out discussed above: the current head (a5af45f) has the hardware's rule.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. Hand-written instructions in a HIP kernel (assembled by the AMD driver's amd_comgr). RDNA2 desktop silicon, not the console's. No game data. 748 operand sets: all combinations of 0, 1, 3, 5, 0x7fffffff, 0x80000000, 0xffffffff, 0xfffffffd for the two 32-bit sources and 0, 1, 0xffffffff, 2^64-1, 2^63, 2^63-1, 0xffffffff00000000 for the 64-bit addend, plus 300 random sets. The carry SGPR was read with v_cndmask_b32.

match
v_mad_u64_u32: 64-bit result, carry = bit 64 of a*b + c 748/748
v_mad_i64_i32: 64-bit result 748/748
v_mad_i64_i32: carry = bit 64 of the 65-bit signed result 748/748
v_mad_i64_i32: carry = signed overflow of the 64-bit add (the earlier rule) 385/748

Both of your examples set the flag on the hardware: -3 * 5 + 0 → 1 and (-1) * 1 + (-1) → 1.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

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

Merges cleanly with current main (8e79c80); cargo test -p victory-shader --locked on macOS arm64 / rustc 1.98.1 → 71 passed / 0 failed (1 ignored, Windows execution).

The lowering looks right: both v_mad_u64_u32 and v_mad_i64_i32 sign/zero-extend the 32-bit operands to i64 (can't overflow on the multiply), detect carry-out as sum < prod unsigned, and for the signed form XOR in the sign disagreement of prod/acc — the standard mad carry. Clamped forms are refused, the carry goes to lane_bits, and other writes are marked as lane-mask reads. The new valu_mad64_executes_on_gpu test is Windows-gated (validator + host execution), so CI covers it; I can't run it here.

One note: the victory-gpu/src/host.rs hunk (mod tests → mod batch) is already on main — it merges as a no-op, so the PR's real content is just the shader lowering + test.

@DeerSilver

Copy link
Copy Markdown

Tested head f4a24e1 merged onto main ffe310e on NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1. No game files: the test suite only.

Merged onto current main, the test build fails. 6638098 ("per-lane descriptors chosen by control flow", merged with #566 at 09:53) added a fourth argument to resource::track:

error[E0061]: this function takes 4 arguments but 3 arguments were supplied
  --> crates/victory-shader/src/emit/mod.rs (in the new test)
   |     resource::track(&p, &native.layout, native.stage)
   |     argument #4 of type `Option<&structure::Structured>` is missing

There's no text conflict, so GitHub still shows the PR as mergeable, but merging it as it is would break cargo test on main. The call this PR adds needs the fourth argument. main's own tests pass Some(&st); None keeps the old three-argument behaviour.

With , None added locally to that one call, everything passes:

  • The PR's GPU test ran on the RTX 5060 (DXGI adapter 0 on this laptop; no "no SM 6.6 device" skip) and passed:
    • valu_mad64_executes_on_gpu
  • cargo test --workspace --locked: 343 passed, 0 failed, 6 ignored, against 342 passed / 6 ignored on main.
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.

AI-assisted (Claude Code).

@alexdeliu

Copy link
Copy Markdown

Merged onto main @ ffe310eb, this PR (f4a24e1e) no longer compiles its tests. crates/victory-shader/src/emit/mod.rs:835 calls resource::track with 3 arguments. Since 6638098 it takes a 4th, lanes: Option<&Structured>; main's tests pass Some(&st) with let st = structure::structure(&p).unwrap();.

Checked with CI's steps (both clippy runs with -D warnings, cargo test --workspace) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87

@n-romanovich

Copy link
Copy Markdown
Author

Thanks for testing and pointing this out, @alexdeliu!

Merged latest main and updated resource::track in the test to pass Some(&st) per 6638098. Verified with cargo clippy --all-targets --locked -- -D warnings and re-ran the GPU test — both compile cleanly and pass with 0 warnings.

@alexdeliu

Copy link
Copy Markdown

@n-romanovich rechecked 9dc6a7e3 merged onto main @ ffe310eb on the RX 9070: both CI clippy runs are clean and cargo test --workspace passes (343 tests).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Recommended for a maintainer's look. The verdict hasn't changed. The new head is a rebase onto current main (940d32f). In crates/victory-shader/src/emit/lower.rs (v_mad_u64_u32 | v_mad_i64_i32), the signed carry-out is bit 64 of the exact 65-bit result (unsigned carry ^ sign(prod) ^ sign(acc)), and clamp is refused. This matches the ISA pseudocode and Lyrnc's RDNA2 measurement (748/748). I rechecked the test's expected values by hand.

A small optional addition: the two signed cases in valu_mad64_executes_on_gpu (-3*5+25 and -3*5 + i64::MIN) give the same carry under the old signed-overflow rule. The -3*5+0 case from the earlier thread, with carry 1, isn't in this head. Adding it back would make the test catch a regression to the old rule.

CI: passing.


Generated by Claude Code

@n-romanovich

Copy link
Copy Markdown
Author

@Marvisak, thanks for the review.

In 6b5e0b4, I removed the redundant flags::INT64 assignment (as src64 already sets it) and updated the PR description to reflect the 65-bit carry rule.
Re-ran cargo test -p victory-shader --locked -- mad64 on Windows 11 (NVIDIA GeForce RTX 5060 Ti): 1 passed, 0 failed.

@n-romanovich
n-romanovich force-pushed the feat/valu-mad64 branch 2 times, most recently from 953f4da to 25d8524 Compare November 25, 2026 01:47
@Calvix2k

Copy link
Copy Markdown
Contributor

Verification on Windows 10 19045 (RX 7800 XT), your head d1d21f9 plus the "skip the GPU suites a device cannot run" patch from #1991 (this host reports SM 6.5, so the probe suites would otherwise fail for an unrelated reason):

cargo test -p victory-shader --lib — 248 passed, 0 failed, 3 ignored. valu_mad64_executes_on_gpu skips here (no SM 6.6 device); CI runs it.

On the carry, I checked the formula rather than only the test: for the signed form ucarry ^ sign(prod) ^ sign(acc) is bit 64 of the exact (65-bit) sum, which is what AMD's SDST holds for v_mad_i64_i32 — e.g. max + 1 gives 0, -2^63 + -2^63 gives 1, 1 + -1 gives 0, -2^63 + max gives 1. The unsigned form stays sum < prod. Refusing v.clamp on both is right; it has no 64-bit DXIL equivalent here.

One thing worth a line in the PR body, if you agree: the previous code only wrote the carry for the unsigned form, so this changes v_mad_i64_i32 output for shaders that read SDST — it is the fix, but it is a behaviour change, not just new instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants