Shaders: lower v_mad_u64_u32 and v_mad_i64_i32 #171
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/171"
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 Marvisak/VIctory. Originally posted by n-romanovich on 2026-11-24T01:05:31Z.
Summary
emit::lowerpreviously loweredv_mad_u64_u32andv_mad_i64_i32incrates/victory-shader/src/emit/lower.rs, but only emitted the carry-out lane mask (SDST) for the unsigned form, causing shaders that readv_mad_i64_i32'sSDSTto be refused.This PR adds signed carry-out for
v_mad_i64_i32as bit 64 of the exact 65-bit result, refusesclampon both instructions, and adds a GPU execution test.Changes
crates/victory-shader/src/emit/lower.rs(v_mad_u64_u32|v_mad_i64_i32):v.clampwithErr("a clamped integer operation").sum = prod + accand unsigned carryucarry = sum < prod(equivalent tosum < acc).v_mad_i64_i32(signed == true), derive the signed carry-out (bit 64 of the exact 65-bit signed result) asucarry ^ (prod < 0) ^ (acc < 0)and writecarrytoself.lane_bitsforv.carryon both signed and unsigned forms.crates/victory-shader/src/emit/mod.rs(emit::tests::valu_mad64_executes_on_gpu):v_mad_u64_u32andv_mad_i64_i32carry-out across-3 * 5 + 0,-3 * 5 + 25, and-15 + i64::MIN, as well asv.clamprefusal.Verification
cargo test -p victory-shader --locked -- mad64:1 passed; 0 failed(emit::tests::valu_mad64_executes_on_gpu ... ok).Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:19:34Z.
Tested
60b63b4on Arch Linux x86_64, rustc 1.98.1: pass.cargo test -p victory-shader: 57 passed, 0 failed (2 test binaries), including the 1 test this PR adds.victory-gpuE0425.No game data used.
Imported from Marvisak/VIctory. Originally posted by Sunkiller-qc on 2026-11-24T01:29:32Z.
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 atcrates/victory-shader/src/emit/lower.rs:3540gives a different result whenever the true sum is negative and doesn't overflow: for example,-3 * 5 + 0should set the bit, but here it's 0. The test cases (10andi64::MIN - 15) agree under both definitions, so they don't catch it.#163 lowers the same two opcodes (
wide_mad,lower.rs:5043there) with the 65-bit form, and it also refusesclamp, which this version ignores.Static review, not built or run.
Imported from Marvisak/VIctory. Originally posted by n-romanovich on 2026-11-24T01:42:23Z.
Thanks for the review, @Sunkiller-qc!
Addressed in
a5af45f:v_mad_i64_i32carry-out to compute bit 64 of the 65-bit sum (unsigned_carry ^ sign(prod) ^ sign(acc)).v.clampon bothv_mad_u64_u32andv_mad_i64_i32.-3 * 5 + 0(SDST = 1) case tovalu_mad64_executes_on_gpuand re-verifiedcargo test -p victory-shader(70 passed) on D3D12 (RTX 5060 Ti).Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-24T01:42:42Z.
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_i32differs. 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:V_MAD_I64_I32:{ D1.i1, D0.i64 } = 65'B(65'I(S0.i) * 65'I(S1.i) + 65'I(S2.i64)){vcc_out,D.i64} = S0.i32 * S1.i32 + S2.i64With 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.
Imported from Marvisak/VIctory. Originally posted by n-romanovich on 2026-11-24T01:44:20Z.
Thanks, @r4tedx! This was updated in
a5af45fto compute bit 64 of the 65-bit sum (unsigned_carry ^ sign(prod) ^ sign(acc)), so cases like-3 * 5 + 0and(-1) * 1 + (-1)setSDST = 1per the RDNA2/RDNA3 ISA spec.Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:26:40Z.
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, 0xfffffffdfor the two 32-bit sources and0, 1, 0xffffffff, 2^64-1, 2^63, 2^63-1, 0xffffffff00000000for the 64-bit addend, plus 300 random sets. The carry SGPR was read withv_cndmask_b32.v_mad_u64_u32: 64-bit result, carry = bit 64 ofa*b + cv_mad_i64_i32: 64-bit resultv_mad_i64_i32: carry = bit 64 of the 65-bit signed resultv_mad_i64_i32: carry = signed overflow of the 64-bit add (the earlier rule)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.
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T09:13:25Z.
Review: APPROVED
Merges cleanly with current main (
8e79c80);cargo test -p victory-shader --lockedon macOS arm64 / rustc 1.98.1 → 71 passed / 0 failed (1 ignored, Windows execution).The lowering looks right: both
v_mad_u64_u32andv_mad_i64_i32sign/zero-extend the 32-bit operands to i64 (can't overflow on the multiply), detect carry-out assum < produnsigned, and for the signed form XOR in the sign disagreement of prod/acc — the standard mad carry. Clamped forms are refused, the carry goes tolane_bits, and other writes are marked as lane-mask reads. The newvalu_mad64_executes_on_gputest is Windows-gated (validator + host execution), so CI covers it; I can't run it here.One note: the
victory-gpu/src/host.rshunk (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.Imported from Marvisak/VIctory. Originally posted by DeerSilver on 2026-11-24T10:51:04Z.
Tested head
f4a24e1merged ontomainffe310eon 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 toresource::track:There's no text conflict, so GitHub still shows the PR as mergeable, but merging it as it is would break
cargo testonmain. The call this PR adds needs the fourth argument.main's own tests passSome(&st);Nonekeeps the old three-argument behaviour.With
, Noneadded locally to that one call, everything passes:valu_mad64_executes_on_gpucargo test --workspace --locked: 343 passed, 0 failed, 6 ignored, against 342 passed / 6 ignored onmain.cargo clippy --workspace --all-targets --locked -- -D warnings: clean.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T17:23:16Z.
Merged onto
main@ffe310eb, this PR (f4a24e1e) no longer compiles its tests.crates/victory-shader/src/emit/mod.rs:835callsresource::trackwith 3 arguments. Since6638098cit takes a 4th,lanes: Option<&Structured>; main's tests passSome(&st)withlet 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/ef9ef0ebc1776ff104b5aa8eaea18e87Imported from Marvisak/VIctory. Originally posted by n-romanovich on 2026-11-24T17:53:59Z.
Thanks for testing and pointing this out, @alexdeliu!
Merged latest main and updated resource::track in the test to pass Some(&st) per
6638098c. Verified with cargo clippy --all-targets --locked -- -D warnings and re-ran the GPU test — both compile cleanly and pass with 0 warnings.Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T17:56:06Z.
@n-romanovich rechecked
9dc6a7e3merged ontomain@ffe310ebon the RX 9070: both CI clippy runs are clean andcargo test --workspacepasses (343 tests).Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:20:29Z.
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). Incrates/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)), andclampis 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+25and-3*5 + i64::MIN) give the same carry under the old signed-overflow rule. The-3*5+0case 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
Imported from Marvisak/VIctory. Originally posted by n-romanovich on 2026-11-25T00:35:56Z.
@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.
Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T02:35:07Z.
Verification on Windows 10 19045 (RX 7800 XT), your head
d1d21f9plus 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_gpuskips 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 forv_mad_i64_i32— e.g.max + 1gives 0,-2^63 + -2^63gives 1,1 + -1gives 0,-2^63 + maxgives 1. The unsigned form stayssum < prod. Refusingv.clampon 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_i32output for shaders that read SDST — it is the fix, but it is a behaviour change, not just new instructions.Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T01:11:23Z.
Code review and RTX 5090 + WARP + DXIL verification at head
d1d21f9c7076merged cleanly ontomain(63bcca43c6a0;emit/lower.rs=521,055B,3,233B under the 512 KiB cap):crates/victory-shader/src/emit/lower.rs:5422-5451):v.clampcleanly ("a clamped integer operation").v_mad_u64_u32:ZExt32->64 multiply-add with unsigned carryIULT(sum, prod).v_mad_i64_i32:SExt32->64 multiply-add whereucarry ^ (prod < 0) ^ (acc < 0)is exact for bit 64 of(i65)S0 * (i65)S1 + (i65)S2across all(i32, i32, i64)inputs, recorded intolane_bits(sdst/VCC) while writing the 64-bit result underguard.dxil.dll10.0.26100.0,VICTORY_D3D12_DEBUG=1, Rust 1.98.1):cargo xtask policy:191 files ok;cargo clippy(default +--features diagnostics): 0 warningsemit::tests(18/18, includingvalu_mad64_executes_on_gpu) andemit::semantics_tests(34/34, 1 ignored): passed on both RTX 5090 and WARP.LGTM.
Imported from Marvisak/VIctory. Originally posted by steven-seagull27 on 2026-11-26T02:58:48Z.
@n-romanovich Your signed carry implementation resolves a concrete conflict with the newer lane-mask PR #2967. I merged your original
d1d21f9into that PR and compared 65,536 signed multiply-add readbacks per adapter against ani128reference. With your code, the RTX 5080 and built-in Microsoft WARP both went from 32,759 incorrect SDST bits to zero. The low 64 bits and the unsigned control matched throughout. The integration keeps your commit and includes a small regression for the two PRs' different behavior. Results and fast-forward instructions on #2967.Imported from Marvisak/VIctory. Originally posted by pawrenity on 2026-11-26T04:33:34Z.
A small gap, read on
main63bcca4(not run). Thev_mad_u64_u32/v_mad_i64_i32arm inemit/lower.rs(around 6011-6041) writes both halves withself.write(...)and does not go throughwrite_i(around 2846-2851).write_irefusesclampandomodas "integer output modifiers". This PR adds thev.clamprefusal, butv.omod(VOP3B bits 59-60, decoded for these opcodes ingfx10/fields.rs) is still ignored silently. Refusing it next to the clamp check would make the arm match every other integer write.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-27T01:09:07Z.
Review: APPROVED
LGTM — verified on Windows (RTX 5090 + WARP):
cargo test -p victory-shader --lib valu_mad64_executes_on_gpupassed on RTX 5090 and underVICTORY_WARP=1v.clampand emitting the signed carry-out (ucarry ^ (prod < 0) ^ (acc < 0)) intolane_bitsis clean and accuratecargo clippy -p victory-shader --all-targets -- -D warningspassed with 0 warningscargo xtask policypassed (191 files ok)Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.