Shaders: lower v_mad_u64_u32 and v_mad_i64_i32 #171
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!171
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@n-romanovich on GitHub · 2026-11-24 01:05 UTC · original
n-romanovich:feat/valu-mad64→main· +162 −8 · 2 files · 1 commits · openSummary
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).@DaviTostes on GitHub · 2026-11-24 01:19 UTC · original
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.
@Sunkiller-qc on GitHub · 2026-11-24 01:29 UTC · original
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.
@n-romanovich on GitHub · 2026-11-24 01:42 UTC · original
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).@r4tedx on GitHub · 2026-11-24 01:42 UTC · original
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.
@n-romanovich on GitHub · 2026-11-24 01:44 UTC · original
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.@Lyrnc on GitHub · 2026-11-24 06:26 UTC · original
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.
@DeerSilver on GitHub · 2026-11-24 10:51 UTC · original
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).
@alexdeliu on GitHub · 2026-11-24 17:23 UTC · original
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/ef9ef0ebc1776ff104b5aa8eaea18e87@n-romanovich on GitHub · 2026-11-24 17:53 UTC · original
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.@alexdeliu on GitHub · 2026-11-24 17:56 UTC · original
@n-romanovich rechecked
9dc6a7e3merged ontomain@ffe310ebon the RX 9070: both CI clippy runs are clean andcargo test --workspacepasses (343 tests).@Marvisak on GitHub · 2026-11-25 00:20 UTC · original · edited 2026-11-25 02:30 UTC
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
@n-romanovich on GitHub · 2026-11-25 00:35 UTC · original
@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.
@Calvix2k on GitHub · 2026-11-25 02:35 UTC · original
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.@fuzeofficlal on GitHub · 2026-11-26 01:11 UTC · original
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.
@steven-seagull27 on GitHub · 2026-11-26 02:58 UTC · original
@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.@pawrenity on GitHub · 2026-11-26 04:33 UTC · original
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).
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.