Shaders: the v_mad_u64_u32 carry on its dwords (AMD's compiler gets the i64 compare wrong) #90

Closed
github-bot wants to merge 0 commits from github-bot/gh-90 into main AGit
Owner

@⁠murillojndem on GitHub · 2026-12-01 10:34 UTC · original
amd/mad-u64-carry-dwords → main · +94 −2 · 3 files · 1 commits · closed without merging

What

The carry out of v_mad_u64_u32 is r < c (unsigned), with r the low 64 bits of a * b + c. The translator lowers it as one i64 icmp. When the addend shares a register with the second factor (v_mad_u64_u32 v[5:6], s20, v1, v2, v[2:3]), AMD's D3D12 compiler gives the wrong carry. The 64-bit compares already avoid a single i64 compare for this compiler (upstream Marvisak/VIctory@cc7ec2a, in compare()); this is a different instruction with the same kind of fault.

Change

emit/lower.rs, the v_mad_u64_u32 arm: the carry is computed on the dwords of r and c, the high dwords first. The result dwords do not change. TRANSLATOR_VERSION goes from 4 to 5.

Tests

New test emit::valu_tests::v_mad_u64_u32_matches_its_definition: 6 x 6 factors and 9 addends, with the addend in v[3:4] and sharing v2 in v[2:3]. It checks the carry and both result dwords and counts every mismatch by device, form and output before it fails.

On an RX 7900 XT (driver 32.0.32015.7029, Windows 11), main 5d91fde gets the carry wrong in 124 of the 324 runs of the v[2:3] form (144 distinct inputs, since there the addend's low dword is the factor b) and everything else right. This branch gets every case right. WARP is right on both, so CI, which has no Radeon, passes with or without the change.

Also run:

  • victory-shader --lib on WARP: 310 passed, 4 failed, 4 ignored (main: 309, 4, 4). The four failures are the same on main: three fail PSV0 validation with the SDK 10.0.22621 dxil.dll on this PC, and a_lane_mask_joined_with_a_number_is_read_as_the_number_it_is is a hardware-only test that VICTORY_WARP=1 sends to WARP.
  • victory-shader --lib on the RX 7900 XT (and WARP, through the same harness): 311 passed, 3 failed, 4 ignored (main: 310, 3, 4). The three failures are the PSV0 ones.
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings, with and without --all-features: clean.
  • cargo test -p xtask: repository_passes_policy passes. documented_switches_are_read fails, the same way as on main (VICTORY_NAME in README.md).

Limits

  • I built and tested victory-shader only. My checkout leaves out crates/victory-formats/src/bcpack-tables.bin, so I did not build -p victory or run the workspace clippy.
  • No in-game run and no fps numbers. The test's operands come from root constants and are the same in every lane; whether the compiler's fault also hits values that differ per lane is not tested.
  • No environment switch: it is a translation fix, like the 64-bit compare one, which has none. I can put it behind a switch if you prefer.
  • Other translator PRs bump the version too (#72 goes to 6; my #91, #92 and #93 also go to 5). Whichever lands later needs the next number.

The same change is open as DexNom/VIctory#205, where the same test gave the same count on the same card.

Generated with Claude Code

**@⁠murillojndem** on GitHub · 2026-12-01 10:34 UTC · [original](https://github.com/eikkapine/victory-amd/pull/90) `amd/mad-u64-carry-dwords` → `main` · +94 −2 · 3 files · 1 commits · closed without merging ## What The carry out of `v_mad_u64_u32` is `r < c` (unsigned), with `r` the low 64 bits of `a * b + c`. The translator lowers it as one i64 `icmp`. When the addend shares a register with the second factor (`v_mad_u64_u32 v[5:6], s20, v1, v2, v[2:3]`), AMD's D3D12 compiler gives the wrong carry. The 64-bit compares already avoid a single i64 compare for this compiler (upstream Marvisak/VIctory@cc7ec2a, in `compare()`); this is a different instruction with the same kind of fault. ## Change `emit/lower.rs`, the `v_mad_u64_u32` arm: the carry is computed on the dwords of `r` and `c`, the high dwords first. The result dwords do not change. `TRANSLATOR_VERSION` goes from 4 to 5. ## Tests New test `emit::valu_tests::v_mad_u64_u32_matches_its_definition`: 6 x 6 factors and 9 addends, with the addend in `v[3:4]` and sharing `v2` in `v[2:3]`. It checks the carry and both result dwords and counts every mismatch by device, form and output before it fails. On an RX 7900 XT (driver 32.0.32015.7029, Windows 11), main 5d91fde gets the carry wrong in 124 of the 324 runs of the `v[2:3]` form (144 distinct inputs, since there the addend's low dword is the factor `b`) and everything else right. This branch gets every case right. WARP is right on both, so CI, which has no Radeon, passes with or without the change. Also run: - `victory-shader --lib` on WARP: 310 passed, 4 failed, 4 ignored (main: 309, 4, 4). The four failures are the same on main: three fail PSV0 validation with the SDK 10.0.22621 `dxil.dll` on this PC, and `a_lane_mask_joined_with_a_number_is_read_as_the_number_it_is` is a hardware-only test that `VICTORY_WARP=1` sends to WARP. - `victory-shader --lib` on the RX 7900 XT (and WARP, through the same harness): 311 passed, 3 failed, 4 ignored (main: 310, 3, 4). The three failures are the PSV0 ones. - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`, with and without `--all-features`: clean. - `cargo test -p xtask`: `repository_passes_policy` passes. `documented_switches_are_read` fails, the same way as on main (`VICTORY_NAME` in README.md). ## Limits - I built and tested `victory-shader` only. My checkout leaves out `crates/victory-formats/src/bcpack-tables.bin`, so I did not build `-p victory` or run the workspace clippy. - No in-game run and no fps numbers. The test's operands come from root constants and are the same in every lane; whether the compiler's fault also hits values that differ per lane is not tested. - No environment switch: it is a translation fix, like the 64-bit compare one, which has none. I can put it behind a switch if you prefer. - Other translator PRs bump the version too (#72 goes to 6; my #91, #92 and #93 also go to 5). Whichever lands later needs the next number. The same change is open as DexNom/VIctory#205, where the same test gave the same count on the same card. Generated with [Claude Code](https://claude.com/claude-code)
The carry out of v_mad_u64_u32 is r < c (unsigned), with r the low 64
bits of a * b + c. It was lowered as one i64 icmp. When the addend
shares a register with the second factor (v_mad_u64_u32 v[5:6], s20,
v1, v2, v[2:3]), AMD's D3D12 compiler gives the wrong carry. The carry
is now computed on the dwords of r and c, the high dwords first. The
64-bit compares already work on dwords for the same compiler (upstream
cc7ec2a531, in compare()). The result dwords do not change.

Test v_mad_u64_u32_matches_its_definition checks the carry and both
result dwords for 6 x 6 factors and 9 addends, with the addend in
v[3:4] and sharing v2 in v[2:3] (there the addend's low dword is the
factor b, so the 324 runs of that form hold 144 distinct inputs). It
counts every mismatch by device, form and output before it fails.

On an RX 7900 XT (driver 32.0.32015.7029), without the change the
carry is wrong in 124 of the 324 runs of the v[2:3] form and in none of
the v[3:4] form; with it, in none. WARP gets every case right either
way, so the test shows the difference only on a card with this
compiler.

TRANSLATOR_VERSION 4 -> 5, since programs with v_mad_u64_u32 translate
differently.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 10:38:51 +00:00
Author
Owner

@⁠alexdeliu on GitHub · 2026-12-01 18:26 UTC · original

Tested on RDNA 4 and RDNA 2, which the description doesn't cover. Same result as on the RX 7900 XT.

Windows 11, RX 9070 (RDNA 4, driver 32.0.32029.2010), Ryzen 7 7700 iGPU (RDNA 2, gfx1036, driver 32.0.21045.29003), WARP. cargo test --locked -p victory-shader --lib -- --exact emit::valu_tests::v_mad_u64_u32_matches_its_definition. "Before" is main 5d91fde with only this PR's valu_tests.rs; "after" is the PR head dcc35d0. Each has its own CARGO_TARGET_DIR.

before after
RX 9070 fails: ("hardware", "v[2:3]", "carry"): 124 of 324 passes
iGPU (RDNA 2) fails: the same 124 of 324 passes
WARP passes passes

So both AMD generations here get exactly the RX 7900 XT's count; the fault isn't RDNA 3 only.

Also at dcc35d0:

  • victory-shader --lib: RX 9070 313 passed, 0 failed (without size_tests::the_record_count_of_a_structured_buffer_read_by_index, which takes the test process down on this card on main too); iGPU 314 passed, 0 failed.
  • WARP, --test-threads=1: 312 passed, 1 failed (a_lane_mask_joined_with_a_number_is_read_as_the_number_it_is, the same as on main).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings: clean.

Not tested: in game.

Heads-up: my #117 also takes TRANSLATOR_VERSION from 4 to 5. Whichever lands second moves to the next number. I'll rebase mine if this one goes first.

**@⁠alexdeliu** on GitHub · 2026-12-01 18:26 UTC · [original](https://github.com/eikkapine/victory-amd/pull/90#issuecomment-7320955016) Tested on RDNA 4 and RDNA 2, which the description doesn't cover. Same result as on the RX 7900 XT. Windows 11, RX 9070 (RDNA 4, driver 32.0.32029.2010), Ryzen 7 7700 iGPU (RDNA 2, gfx1036, driver 32.0.21045.29003), WARP. `cargo test --locked -p victory-shader --lib -- --exact emit::valu_tests::v_mad_u64_u32_matches_its_definition`. "Before" is `main` 5d91fde with only this PR's `valu_tests.rs`; "after" is the PR head dcc35d0. Each has its own `CARGO_TARGET_DIR`. | | before | after | |---|---|---| | RX 9070 | fails: `("hardware", "v[2:3]", "carry"): 124` of 324 | passes | | iGPU (RDNA 2) | fails: the same 124 of 324 | passes | | WARP | passes | passes | So both AMD generations here get exactly the RX 7900 XT's count; the fault isn't RDNA 3 only. Also at dcc35d0: - `victory-shader --lib`: RX 9070 313 passed, 0 failed (without `size_tests::the_record_count_of_a_structured_buffer_read_by_index`, which takes the test process down on this card on `main` too); iGPU 314 passed, 0 failed. - WARP, `--test-threads=1`: 312 passed, 1 failed (`a_lane_mask_joined_with_a_number_is_read_as_the_number_it_is`, the same as on `main`). - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings`: clean. Not tested: in game. Heads-up: my #117 also takes `TRANSLATOR_VERSION` from 4 to 5. Whichever lands second moves to the next number. I'll rebase mine if this one goes first.
github-bot closed this pull request 2026-12-01 18:59:35 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.