Shaders: 64-bit integer compares on their dwords (AMD orders a shared-dword i64 compare wrong) #81

Open
github-bot wants to merge 1 commit from github-bot/gh-81 into main AGit
Owner

@⁠alexdeliu on GitHub · 2026-11-25 13:35 UTC · original
alexdeliu:fix/amd-i64-compare → main · +128 −14 · 2 files · 1 commits · open

What

64-bit integer compares (v_cmp_{lt,le,gt,ge}_{i64,u64}) are lowered on the two dwords instead of as one i64 icmp.

Why

AMD's D3D12 shader compiler orders an i64 compare wrongly when the two operands share a dword (for example v[1:2] against v[2:3]): it behaves as if each operand's dwords were swapped. Equality is not affected. WARP compares correctly.

Upstream this is Marvisak/VIctory#1599. It was reproduced there on RDNA2, RDNA3 (RX 7900 XT) and RDNA4, and there is a standalone DXIL repro in rdna2-conformance. This fork is at upstream 8e79c80 and does not have the fix.

The lowering compares the high dwords (signed for i64). When those are equal, it compares the low dwords unsigned. Literals and inline constants keep the extension this fork's raw_src64 gives them.

Tested

The new GPU test v_cmp_u64_and_i64_order_by_the_high_dword covers 12 ops × 100 operand pairs × {separate, shared} registers. The values are picked so that the two dword orders disagree.

on 71652a2 with this PR
RX 9070 (RDNA4, 32.0.32029.2010) fails: v_cmp_lt_i64(0x0, 0x1_0000_0000) passes
Ryzen 7 7700 iGPU (gfx1036, RDNA2, 32.0.21045.29003), VICTORY_ADAPTER=radeon(tm) fails, same case passes
WARP passes passes

cargo test -p victory-shader --lib (VICTORY_DXIL = dxil.dll 1.8.2502.11):

unmodified 71652a2 with this PR
RX 9070 266 passed, 0 failed, 6 ignored 267 passed, 0 failed, 6 ignored (3 runs)
gfx1036, --test-threads=1 264 passed, 2 failed 265 passed, 2 failed

The 2 gfx1036 failures are #2's wave-size tests, with the same output as on the RX 6950 XT there; this change does not touch them. One parallel run on each GPU ended once with 0xc0000005 mid-suite, without and with this PR; reruns were clean.

No game data was used; this is a unit-level change.

AI use: Claude Code (Claude Opus 5.5) ported the change and ran the tests on my machine.

🤖 Generated with Claude Code

**@⁠alexdeliu** on GitHub · 2026-11-25 13:35 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/81) `alexdeliu:fix/amd-i64-compare` → `main` · +128 −14 · 2 files · 1 commits · open ## What 64-bit integer compares (`v_cmp_{lt,le,gt,ge}_{i64,u64}`) are lowered on the two dwords instead of as one i64 `icmp`. ## Why AMD's D3D12 shader compiler orders an i64 compare wrongly when the two operands share a dword (for example `v[1:2]` against `v[2:3]`): it behaves as if each operand's dwords were swapped. Equality is not affected. WARP compares correctly. Upstream this is [Marvisak/VIctory#1599](https://github.com/Marvisak/VIctory/pull/1599). It was reproduced there on RDNA2, RDNA3 (RX 7900 XT) and RDNA4, and there is a standalone DXIL repro in [rdna2-conformance](https://github.com/alexdeliu/rdna2-conformance). This fork is at upstream `8e79c80` and does not have the fix. The lowering compares the high dwords (signed for i64). When those are equal, it compares the low dwords unsigned. Literals and inline constants keep the extension this fork's `raw_src64` gives them. ## Tested The new GPU test `v_cmp_u64_and_i64_order_by_the_high_dword` covers 12 ops × 100 operand pairs × {separate, shared} registers. The values are picked so that the two dword orders disagree. | | on `71652a2` | with this PR | |---|---|---| | RX 9070 (RDNA4, 32.0.32029.2010) | **fails**: `v_cmp_lt_i64(0x0, 0x1_0000_0000)` | passes | | Ryzen 7 7700 iGPU (gfx1036, RDNA2, 32.0.21045.29003), `VICTORY_ADAPTER=radeon(tm)` | **fails**, same case | passes | | WARP | passes | passes | `cargo test -p victory-shader --lib` (`VICTORY_DXIL` = dxil.dll 1.8.2502.11): | | unmodified `71652a2` | with this PR | |---|---|---| | RX 9070 | 266 passed, 0 failed, 6 ignored | 267 passed, 0 failed, 6 ignored (3 runs) | | gfx1036, `--test-threads=1` | 264 passed, 2 failed | 265 passed, 2 failed | The 2 gfx1036 failures are #2's wave-size tests, with the same output as on the RX 6950 XT there; this change does not touch them. One parallel run on each GPU ended once with `0xc0000005` mid-suite, without and with this PR; reruns were clean. No game data was used; this is a unit-level change. AI use: Claude Code (Claude Opus 5.5) ported the change and ran the tests on my machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
AMD's D3D12 shader compiler orders an i64 icmp whose operands share a
dword as if the dwords were swapped (RX 9070 and the gfx1036 iGPU alike;
equality is unaffected); WARP compares them right. Compare the high
dwords (signed for i64), and the low ones unsigned when the high are
equal. Port of Marvisak/VIctory#1599; the GPU test fails without it on
both AMD GPUs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:35:13 +00:00
Author
Owner

@⁠enriquebruno12 on GitHub · 2026-11-25 14:43 UTC · original

Confirmed on an RX 9070 XT with Windows driver 32.0.32029.2010. With only the new test applied, v_cmp_lt_i64(0, 0x100000000) returned 0 on hardware instead of 1. After applying the lowering change, the same test passed. No game files were used.

**@⁠enriquebruno12** on GitHub · 2026-11-25 14:43 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/81#issuecomment-7165726748) Confirmed on an RX 9070 XT with Windows driver 32.0.32029.2010. With only the new test applied, v_cmp_lt_i64(0, 0x100000000) returned 0 on hardware instead of 1. After applying the lowering change, the same test passed. No game files were used.
Author
Owner

@⁠ySKELLETONX on GitHub · 2026-11-25 16:35 UTC · original

Tested on an RX 7600 XT 16 GB (RDNA3, Navi 33), driver 32.0.32015.2008, Windows 11 Pro build 26300, Rust 1.98.1, at 7c8dc5b: cargo test -p victory-shader --lib --locked: 267 passed, 0 failed, 6 ignored, hardware dispatch tests included (main at 01aadd1: 266 passed). Workspace tests only; not run in the title.

**@⁠ySKELLETONX** on GitHub · 2026-11-25 16:35 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/81#issuecomment-7168229281) Tested on an RX 7600 XT 16 GB (RDNA3, Navi 33), driver 32.0.32015.2008, Windows 11 Pro build 26300, Rust 1.98.1, at `7c8dc5b`: `cargo test -p victory-shader --lib --locked`: **267 passed, 0 failed, 6 ignored**, hardware dispatch tests included (`main` at `01aadd1`: 266 passed). Workspace tests only; not run in the title.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-25 17:23 UTC · original

A second RDNA3 card, adding the run without the fix to ySKELLETONX's RX 7600 XT result above. RX 7900 XT (driver 32.0.32029.2010), Windows 11 build 26300, Rust 1.98.1, head 7c8dc5b on 01aadd1, each tree with its own target dir:

  • Without the fix (only this PR's valu_tests.rs applied to 01aadd1): v_cmp_u64_and_i64_order_by_the_high_dword fails 3 of 3 runs, on the same case as on the RX 9070: hardware: v_cmp_lt_i64(0x0000000000000000, 0x0000000100000000).
  • With the PR: cargo test -p victory-shader --lib -- --test-threads=1 gives 267 passed, 0 failed, 6 ignored.

I also translated the whole of shaders.rpf (26,084 programs) with victory translate --validate --host-lanes 32-64, on 01aadd1 and with this PR, and compared a hash of each program's DXIL. The output is deterministic: two runs of 01aadd1 give the same hashes. With this PR, 233 programs change (217 pixel, 16 compute) and all the others are byte for byte the same. Every changed program still emits and passes the DXIL validator, and the totals don't change: VS/PS 14,247/14,247, CS 2,682/2,682, MS 8,694/8,765 (the 71 mesh programs refused on main for outputs beyond 32 KiB are refused here too). No game data is in this comment: names and counts only. Not run in the game.

**@⁠adrianmjim** on GitHub · 2026-11-25 17:23 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/81#issuecomment-7169443900) A second RDNA3 card, adding the run without the fix to ySKELLETONX's RX 7600 XT result above. RX 7900 XT (driver 32.0.32029.2010), Windows 11 build 26300, Rust 1.98.1, head `7c8dc5b` on `01aadd1`, each tree with its own target dir: - **Without the fix** (only this PR's `valu_tests.rs` applied to `01aadd1`): `v_cmp_u64_and_i64_order_by_the_high_dword` fails 3 of 3 runs, on the same case as on the RX 9070: `hardware: v_cmp_lt_i64(0x0000000000000000, 0x0000000100000000)`. - **With the PR:** `cargo test -p victory-shader --lib -- --test-threads=1` gives 267 passed, 0 failed, 6 ignored. I also translated the whole of `shaders.rpf` (26,084 programs) with `victory translate --validate --host-lanes 32-64`, on `01aadd1` and with this PR, and compared a hash of each program's DXIL. The output is deterministic: two runs of `01aadd1` give the same hashes. With this PR, **233 programs change (217 pixel, 16 compute)** and all the others are byte for byte the same. Every changed program still emits and passes the DXIL validator, and the totals don't change: VS/PS 14,247/14,247, CS 2,682/2,682, MS 8,694/8,765 (the 71 mesh programs refused on `main` for outputs beyond 32 KiB are refused here too). No game data is in this comment: names and counts only. Not run in the game.
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-26 00:18 UTC · original

RDNA2 result. Hardware: RX 6950 XT, Windows 11 build 26300, Rust 1.98.1. This PR (7c8dc5b) merged on main 4f62c2a. Command: cargo test --release -p victory-shader --lib, two runs.

  • main: 275 passed, 2 failed.
  • This PR: 276 passed, 2 failed. The new compare test passes on this discrete RDNA2 card, as well as on WARP. That adds a Navi 21 data point to the RDNA2 iGPU in the description.

The 2 failures are the same on main: scalar_counted_in_a_divergent_loop_read_after_it and scalar_counted_in_nested_divergent_loops, the RDNA2 wave-size tests that #110 and #204 fix. So nothing regresses on this card. Not run in the game.

harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-26 00:18 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/81#issuecomment-7178615488) RDNA2 result. Hardware: RX 6950 XT, Windows 11 build 26300, Rust 1.98.1. This PR (`7c8dc5b`) merged on `main` `4f62c2a`. Command: `cargo test --release -p victory-shader --lib`, two runs. - `main`: 275 passed, 2 failed. - This PR: **276 passed, 2 failed**. The new compare test passes on this discrete RDNA2 card, as well as on WARP. That adds a Navi 21 data point to the RDNA2 iGPU in the description. The 2 failures are the same on `main`: `scalar_counted_in_a_divergent_loop_read_after_it` and `scalar_counted_in_nested_divergent_loops`, the RDNA2 wave-size tests that #110 and #204 fix. So nothing regresses on this card. Not run in the game. harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300 AI-assisted (Claude Code); no game data.
Author
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 21:28 UTC · original · reviewed

Verified on Linux (Rust 1.98.1)

  • cargo test -p victory-shader --lib with 7c8dc5b merged into current main (--no-commit): 196 passed, 0 failed, 6 ignored. The new test is in valu_tests (Windows + D3D12), so it does not run here. Others in this thread have already shown fail-without-fix on RDNA3 and RDNA4.
  • cargo clippy -p victory-shader --target x86_64-pc-windows-msvc --lib --tests: the same 10 warnings as main and none new. This also type-checks the new test.
  • git merge --no-commit --no-ff into main: merges cleanly.
  • llvm-mc --disassemble -triple=amdgcn -mcpu=gfx1030: the test's VOP3 opcodes 0x0a1..0x0a6 / 0x0e1..0x0e6 decode as v_cmp_{lt,eq,le,gt,ne,ge}_{i64,u64}_e64 s20, v[1:2], v[2:3], the shared-dword form the test is after.
  • I checked the (hi_p, lo_p) table in lower.rs:10080-10088 with a small Python model: "high dwords decide (signed for i64), equal high dwords fall back to an unsigned low compare". Over all 8 ordered ops × 196 operand pairs (the test's values plus 0x8000_0000, 0x7FFF_FFFF, i64::MIN and -1), it matches a native 64-bit compare with 0 mismatches.

Finding: the second copy of the 64-bit operand rules will miss #188

src64_dwords (lower.rs:8592-8620) copies raw_src64's operand rules (lower.rs:8416-8456): pairs, inline constants sign-extended, and literals plus the null register zero-extended. Today the two agree. But #188 (open) fixes raw_src64 so that a literal in a signed _i64 operand sign-extends, and its new test exercises exactly v_cmp_eq_i64 / v_cmp_lt_i64 v[1:2], -100.

I merged #188 into main and then this PR on top. The merge is textually clean, but #188's new arm is in raw_src64 only (lower.rs:8455 in that tree). The i64 compares now go through src64_dwords, whose _ => arm still returns (x, 0). Whichever of the two lands second will silently undo the other's fix for compares. #188's signed_64_bit_operands_sign_extend_their_literal should then fail on hardware for v_cmp_lt_i64(-200, -100). I reasoned this from the code; I could not run it.

Suggestion: keep only one source of the extension rules. For example, handle only the register-pair arms in src64_dwords and send every other operand through raw_src64 + split64:

match op {
    Operand::Vector { reg, .. } => pair(self, Reg::V(reg), Reg::V(reg + 1)),
    Operand::Scalar { code, .. } if code != NULL => pair(self, Reg::S(code), Reg::S(code + 1)),
    _ => {
        let x = self.raw_src64(inst, k, false)?; // constant: folds to two i32s
        Ok(self.split64(x))
    }
}

This keeps the compare itself off the i64 icmp, which is the point of the fix. It does set flags::INT64 for constant operands, as main does today. Alternatively, add #188's _i64 literal arm here too, and leave a comment in both places saying they must stay in sync.

Minor: there's a double blank line after src64_dwords (lower.rs:8621-8622).

Not verified

  • The GPU test on any adapter, and the 233 changed programs mentioned above. Neither runs without Windows and D3D12.

Reviewed with Claude Code.

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:28 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/81#pullrequestreview-6065219695) · **reviewed** ### Verified on Linux (Rust 1.98.1) - `cargo test -p victory-shader --lib` with 7c8dc5b merged into current main (`--no-commit`): 196 passed, 0 failed, 6 ignored. The new test is in `valu_tests` (Windows + D3D12), so it does not run here. Others in this thread have already shown fail-without-fix on RDNA3 and RDNA4. - `cargo clippy -p victory-shader --target x86_64-pc-windows-msvc --lib --tests`: the same 10 warnings as main and none new. This also type-checks the new test. - `git merge --no-commit --no-ff` into main: merges cleanly. - `llvm-mc --disassemble -triple=amdgcn -mcpu=gfx1030`: the test's VOP3 opcodes `0x0a1..0x0a6` / `0x0e1..0x0e6` decode as `v_cmp_{lt,eq,le,gt,ne,ge}_{i64,u64}_e64 s20, v[1:2], v[2:3]`, the shared-dword form the test is after. - I checked the `(hi_p, lo_p)` table in `lower.rs:10080-10088` with a small Python model: "high dwords decide (signed for i64), equal high dwords fall back to an unsigned low compare". Over all 8 ordered ops × 196 operand pairs (the test's values plus `0x8000_0000`, `0x7FFF_FFFF`, `i64::MIN` and `-1`), it matches a native 64-bit compare with 0 mismatches. ### Finding: the second copy of the 64-bit operand rules will miss #188 `src64_dwords` (`lower.rs:8592-8620`) copies `raw_src64`'s operand rules (`lower.rs:8416-8456`): pairs, inline constants sign-extended, and literals plus the null register zero-extended. Today the two agree. But #188 (open) fixes `raw_src64` so that a literal in a signed `_i64` operand sign-extends, and its new test exercises exactly `v_cmp_eq_i64` / `v_cmp_lt_i64 v[1:2], -100`. I merged #188 into main and then this PR on top. The merge is textually clean, but #188's new arm is in `raw_src64` only (`lower.rs:8455` in that tree). The i64 compares now go through `src64_dwords`, whose `_ =>` arm still returns `(x, 0)`. Whichever of the two lands second will silently undo the other's fix for compares. #188's `signed_64_bit_operands_sign_extend_their_literal` should then fail on hardware for `v_cmp_lt_i64(-200, -100)`. I reasoned this from the code; I could not run it. Suggestion: keep only one source of the extension rules. For example, handle only the register-pair arms in `src64_dwords` and send every other operand through `raw_src64` + `split64`: ```rust match op { Operand::Vector { reg, .. } => pair(self, Reg::V(reg), Reg::V(reg + 1)), Operand::Scalar { code, .. } if code != NULL => pair(self, Reg::S(code), Reg::S(code + 1)), _ => { let x = self.raw_src64(inst, k, false)?; // constant: folds to two i32s Ok(self.split64(x)) } } ``` This keeps the compare itself off the i64 `icmp`, which is the point of the fix. It does set `flags::INT64` for constant operands, as main does today. Alternatively, add #188's `_i64` literal arm here too, and leave a comment in both places saying they must stay in sync. Minor: there's a double blank line after `src64_dwords` (`lower.rs:8621-8622`). ### Not verified - The GPU test on any adapter, and the 233 changed programs mentioned above. Neither runs without Windows and D3D12. Reviewed with Claude Code.
Author
Owner

@⁠gut0to on GitHub · 2026-11-26 16:08 UTC · original · reviewed

AI-assisted review. I compared the new operand-lowering branches with the added test coverage; I did not run tests or a game.

**@⁠gut0to** on GitHub · 2026-11-26 16:08 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/81#pullrequestreview-6077285130) · **reviewed** AI-assisted review. I compared the new operand-lowering branches with the added test coverage; I did not run tests or a game.
@ -8589,0 +8607,4 @@
Operand::Scalar { code, .. } if code != NULL => {
pair(self, Reg::S(code), Reg::S(code + 1))
}
Operand::Const(k @ 128..=208) => {
Author
Owner

@⁠gut0to on GitHub · 2026-11-26 16:08 UTC · original

AI-assisted static review: I checked the new src64_dwords match against the added compare test; I did not run tests or a game. The test operands are VGPR pairs, so it does not exercise these newly added immediate paths. Could you add cases for a negative inline constant (sign-extended high dword) and a literal or NULL operand (zero-extended high dword)? A regression in either arm would otherwise leave the hardware comparison suite green.

**@⁠gut0to** on GitHub · 2026-11-26 16:08 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/81#discussion_r4710886945) AI-assisted static review: I checked the new `src64_dwords` match against the added compare test; I did not run tests or a game. The test operands are VGPR pairs, so it does not exercise these newly added immediate paths. Could you add cases for a negative inline constant (sign-extended high dword) and a literal or `NULL` operand (zero-extended high dword)? A regression in either arm would otherwise leave the hardware comparison suite green.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/81/head:github-bot/gh-81'
git switch 'github-bot/gh-81'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-81'
git switch 'github-bot/gh-81'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-81'
git switch 'github-bot/gh-81'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-81'
git switch 'main'
git merge --squash 'github-bot/gh-81'
git switch 'main'
git merge --ff-only 'github-bot/gh-81'
git switch 'main'
git merge 'github-bot/gh-81'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.