Shaders: keep overlapping f64 source pairs distinct #198

Open
github-bot wants to merge 2 commits from github-bot/gh-198 into main AGit
Owner

@⁠loyodea on GitHub · 2026-11-25 21:12 UTC · original
loyodea:shader-f64-overlapping-sources → main · +75 −0 · 3 files · 2 commits · open

Part of #82. On the RX 9060 XT, f64 add and multiply can return the wrong result when two operand pairs share a VGPR. Reversing the low dword twice before makeDouble preserves the operand bits and keeps the two values distinct on the tested driver.

Changes

  • Preserve overlapping f64 operands in src_f64.
  • Compare both result words against CPU references for add and multiply, both overlap directions, identical and disjoint sources, signed zero, and source modifiers. The regression uses only synthetic inputs.

Tested

  • Windows 11 Pro 26300, Ryzen 7 5700X3D, Radeon RX 9060 XT driver 32.0.32029.2010, and WARP. Debug with Rust 1.100.0 GNU x64 and DXIL validator 1.9.2609.5.
  • At c289582, cargo test --locked -p victory-shader --lib f64_arithmetic_keeps_overlapping_source_pairs_distinct -- --test-threads=1 --show-output passed on the RX 9060 XT and WARP, with 336 exact result-word checks on each. The regression reproduced on 01aadd1 with the test added and the runtime change absent.
  • At c289582, cargo test --locked -p victory-shader --lib -- --test-threads=1 --nocapture reported 278 passed, 0 failed, and 6 ignored. Four of the 278 returned early because a required console or PC compiler was unavailable.
  • An earlier full workspace run of the same patch on 01aadd1 reported 555 passed, 8 failed, and 17 ignored. All eight failures reproduced with the runtime change absent. They concerned page tracking, memory mapping, GNU loader linking, and the existing size of lower.rs under the asset policy. Clippy in that setup also stopped on existing lints in victory-formats/src/xma.rs.

Limits

The pinned MSVC toolchain, other hardware drivers, retail rendering, and frame rate remain unverified. The workaround adds two integer operations per f64 source before driver optimization.

AI use

OpenAI Codex wrote the patch and synthetic regression and ran the checks at the contributor's direction. The exact model used for the original patch was unavailable.

Review follow-up

The added regression reads one double starting at an odd root-constant dword and multiplies it by an immediate constant. Both result words passed on the RX 9060 XT and WARP with DXIL validation. Removing the two emitted bit reversals reproduced the incorrect high word on the RX 9060 XT.

**@⁠loyodea** on GitHub · 2026-11-25 21:12 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/198) `loyodea:shader-f64-overlapping-sources` → `main` · +75 −0 · 3 files · 2 commits · open Part of #82. On the RX 9060 XT, f64 add and multiply can return the wrong result when two operand pairs share a VGPR. Reversing the low dword twice before `makeDouble` preserves the operand bits and keeps the two values distinct on the tested driver. **Changes** - Preserve overlapping f64 operands in `src_f64`. - Compare both result words against CPU references for add and multiply, both overlap directions, identical and disjoint sources, signed zero, and source modifiers. The regression uses only synthetic inputs. **Tested** - Windows 11 Pro 26300, Ryzen 7 5700X3D, Radeon RX 9060 XT driver 32.0.32029.2010, and WARP. Debug with Rust 1.100.0 GNU x64 and DXIL validator 1.9.2609.5. - At `c289582`, `cargo test --locked -p victory-shader --lib f64_arithmetic_keeps_overlapping_source_pairs_distinct -- --test-threads=1 --show-output` passed on the RX 9060 XT and WARP, with 336 exact result-word checks on each. The regression reproduced on `01aadd1` with the test added and the runtime change absent. - At `c289582`, `cargo test --locked -p victory-shader --lib -- --test-threads=1 --nocapture` reported 278 passed, 0 failed, and 6 ignored. Four of the 278 returned early because a required console or PC compiler was unavailable. - An earlier full workspace run of the same patch on `01aadd1` reported 555 passed, 8 failed, and 17 ignored. All eight failures reproduced with the runtime change absent. They concerned page tracking, memory mapping, GNU loader linking, and the existing size of `lower.rs` under the asset policy. Clippy in that setup also stopped on existing lints in `victory-formats/src/xma.rs`. **Limits** The pinned MSVC toolchain, other hardware drivers, retail rendering, and frame rate remain unverified. The workaround adds two integer operations per f64 source before driver optimization. **AI use** OpenAI Codex wrote the patch and synthetic regression and ran the checks at the contributor's direction. The exact model used for the original patch was unavailable. **Review follow-up** The added regression reads one double starting at an odd root-constant dword and multiplies it by an immediate constant. Both result words passed on the RX 9060 XT and WARP with DXIL validation. Removing the two emitted bit reversals reproduced the incorrect high word on the RX 9060 XT.
On AMD, f64 arithmetic can combine two makeDouble operands when their source register pairs overlap. Reverse the low dword twice so the operand bits are preserved and the pairs stay distinct. Add synthetic add and multiply coverage, including disjoint and identical pairs.

Part of #82.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:37:47 +00:00
Author
Owner

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

RDNA2 data point. This PR (c289582) merged on main 4f62c2a, cargo test --release -p victory-shader --lib on an RX 6950 XT (Windows 11 build 26300, Rust 1.98.1):

  • main: 275 passed, 2 failed.
  • This PR: 276 passed, 2 failed. The new test passes on the hardware adapter.

The 2 failures are the same as on main: the RDNA2 wave-size tests scalar_counted_in_a_divergent_loop_read_after_it and scalar_counted_in_nested_divergent_loops, fixed by #110 and #204. No regression here. One run ended without a result line, which this test binary does now and then on this machine, on main too. 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 05:00 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/198#issuecomment-7183090979) RDNA2 data point. This PR (`c289582`) merged on `main` `4f62c2a`, `cargo test --release -p victory-shader --lib` on an RX 6950 XT (Windows 11 build 26300, Rust 1.98.1): - `main`: 275 passed, 2 failed. - This PR: 276 passed, 2 failed. The new test passes on the hardware adapter. The 2 failures are the same as on `main`: the RDNA2 wave-size tests `scalar_counted_in_a_divergent_loop_read_after_it` and `scalar_counted_in_nested_divergent_loops`, fixed by #110 and #204. No regression here. One run ended without a result line, which this test binary does now and then on this machine, on `main` too. 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

@⁠loyodea on GitHub · 2026-11-26 18:53 UTC · original

Thanks for testing c289582 on the RX 6950 XT. The new overlapping-source regression passed on RDNA2. The two failures matched the 4f62c2a baseline and are tracked in #110 and #204.

**@⁠loyodea** on GitHub · 2026-11-26 18:53 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/198#issuecomment-7200956794) Thanks for testing `c289582` on the RX 6950 XT. The new overlapping-source regression passed on RDNA2. The two failures matched the `4f62c2a` baseline and are tracked in #110 and #204.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 19:46 UTC · original

Tested c289582 (base 4f62c2a) on Windows 11 26300 with an RX 7900 XT (RDNA3) and WARP. Same driver version as in the PR description (32.0.32029.2010). Rust 1.98.1.

New test on RDNA3:

  • f64_arithmetic_keeps_overlapping_source_pairs_distinct passes on the RX 7900 XT and WARP, 336 checks each.
  • It also passes with the lower.rs change left out (only the test applied on 4f62c2a): 336 checks each, no mismatches. On this driver the overlap bug does not reproduce on RDNA3 with these inputs, so the test cannot catch it on this GPU.
  • The full victory-shader lib suite gives the same counts on RDNA3 as in the PR description.

The title's whole shaders.rpf (translate --validate --host-lanes 32-64, release build) compared with 4f62c2a:

  • Same counts: 26084 programs translated. All VS/PS (14247) and CS (2682) programs validate, as do the same 8694 MS programs. The 195 + 71 refusals are the same.
  • 5 programs get new DXIL, all compute shaders in the water_shading CS_TransmissionVolumetrics* family. They are the only programs in the archive with f64 instructions: 216 v_add_f64 and 36 v_cvt_f32_f64. There is no v_mul_f64 or v_fma_f64.
  • None of the 216 v_add_f64 has source pairs that share a VGPR. 180 use two separate VGPR pairs, and 36 use an SGPR pair and a VGPR pair.
  • In the DXIL of these programs, both before and after the PR, there is no double add or multiply. The v_add_f64 sequences are emitted as 36 WaveActiveOp sums on double. Their makeDouble inputs have a constant zero low dword, so the bfrev pair is folded away there.
  • In the 4f62c2a DXIL, no value is the low dword of one makeDouble and the high dword of another.

So the #82 pattern does not occur in this archive. The PR still changes these 5 programs. It adds 72 bfrev calls in total (8 to 24 per program), and each program gets 136 to 368 more bytes of DXIL. All 36 bfrev pairs sit before the makeDouble that feeds an f64-to-f32 conversion (fptrunc). None of them reaches an add or multiply.

Not tested: RDNA4, rendering or frame time of these programs in game, and whether the driver removes the bfrev pair itself.

**@⁠adrianmjim** on GitHub · 2026-11-26 19:46 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/198#issuecomment-7201953672) Tested c289582 (base 4f62c2a) on Windows 11 26300 with an RX 7900 XT (RDNA3) and WARP. Same driver version as in the PR description (32.0.32029.2010). Rust 1.98.1. New test on RDNA3: - `f64_arithmetic_keeps_overlapping_source_pairs_distinct` passes on the RX 7900 XT and WARP, 336 checks each. - It also passes with the `lower.rs` change left out (only the test applied on 4f62c2a): 336 checks each, no mismatches. On this driver the overlap bug does not reproduce on RDNA3 with these inputs, so the test cannot catch it on this GPU. - The full `victory-shader` lib suite gives the same counts on RDNA3 as in the PR description. The title's whole `shaders.rpf` (`translate --validate --host-lanes 32-64`, release build) compared with 4f62c2a: - Same counts: 26084 programs translated. All VS/PS (14247) and CS (2682) programs validate, as do the same 8694 MS programs. The 195 + 71 refusals are the same. - 5 programs get new DXIL, all compute shaders in the water_shading `CS_TransmissionVolumetrics*` family. They are the only programs in the archive with f64 instructions: 216 `v_add_f64` and 36 `v_cvt_f32_f64`. There is no `v_mul_f64` or `v_fma_f64`. - None of the 216 `v_add_f64` has source pairs that share a VGPR. 180 use two separate VGPR pairs, and 36 use an SGPR pair and a VGPR pair. - In the DXIL of these programs, both before and after the PR, there is no double add or multiply. The `v_add_f64` sequences are emitted as 36 `WaveActiveOp` sums on double. Their `makeDouble` inputs have a constant zero low dword, so the `bfrev` pair is folded away there. - In the 4f62c2a DXIL, no value is the low dword of one `makeDouble` and the high dword of another. So the #82 pattern does not occur in this archive. The PR still changes these 5 programs. It adds 72 `bfrev` calls in total (8 to 24 per program), and each program gets 136 to 368 more bytes of DXIL. All 36 `bfrev` pairs sit before the `makeDouble` that feeds an f64-to-f32 conversion (`fptrunc`). None of them reaches an add or multiply. Not tested: RDNA4, rendering or frame time of these programs in game, and whether the driver removes the `bfrev` pair itself.
Author
Owner

@⁠upipibi on GitHub · 2026-11-26 23:20 UTC · original

A pointer to related measurements upstream, since neither this thread nor #202 links them: Marvisak/VIctory#1262 (open, head faf20ac) ran into the same AMD bug and called it FIND-21. On an RX 6950 XT (driver 32.0.21045.29003), with reports from RDNA3 and a gfx1036 iGPU, AMD's D3D12 compiler reads a double's low dword as both halves when the double comes from dwords 1 and 2 of one uniform load (the root constants). That happens in two cases: the double alone (v[2:3] read as (v2, v2)), and two sources sharing a dword. NVIDIA and WARP give correct results. Its fix, c4da80b (lower/double.rs:43-56), XORs both dwords of every VGPR or SGPR pair source with WaveGetLaneIndex() >> 31. That value is always zero, but the compiler can't prove it. The test f64_reads_uniform_pairs_at_odd_dwords gave 45 wrong hardware results without the fix and none with it.

Comparing that with this PR, by reading, not run:

  • Shared dword: covered. The (1,2) and (2,1) cases put v[2:3] (root dwords 1 and 2) next to v[1:2].
  • A single double at an odd offset: the test never uses v[2:3] alone, with itself or with a constant. src_f64 (lower.rs:8374) does change the low dword of every source, so it may cover this case too, but nothing measures it. Only the low dword changes, and a bfrev pair is the identity, so an optimizer could in principle remove it (a guess, not checked). adrianmjim found that on RDNA3 the test passes even without the fix, so the RX 9060 XT is the only reproduction so far. Adding a v[2:3]-alone case to the test would check this.

NaN to int, also from #1262: v_cvt_i32_f64 and v_cvt_u32_f64 here are a plain fptosi/fptoui (lower.rs:8330-8341 on main), so a NaN input gives whatever the host returns. In that thread an RTX 5080 under Wine/vkd3d-proton returned 0x80000000 for both. A native RTX 3050 and WARP returned 0, which is what the hardware gives. This PR doesn't touch those lines; I'm noting it only because they sit in the same block.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠upipibi** on GitHub · 2026-11-26 23:20 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/198#issuecomment-7205452691) A pointer to related measurements upstream, since neither this thread nor #202 links them: Marvisak/VIctory#1262 (open, head `faf20ac`) ran into the same AMD bug and called it FIND-21. On an RX 6950 XT (driver 32.0.21045.29003), with reports from RDNA3 and a gfx1036 iGPU, AMD's D3D12 compiler reads a double's low dword as both halves when the double comes from dwords 1 and 2 of one uniform load (the root constants). That happens in two cases: the double alone (`v[2:3]` read as `(v2, v2)`), and two sources sharing a dword. NVIDIA and WARP give correct results. Its fix, `c4da80b` (`lower/double.rs:43-56`), XORs both dwords of every VGPR or SGPR pair source with `WaveGetLaneIndex() >> 31`. That value is always zero, but the compiler can't prove it. The test `f64_reads_uniform_pairs_at_odd_dwords` gave 45 wrong hardware results without the fix and none with it. Comparing that with this PR, by reading, not run: - Shared dword: covered. The `(1,2)` and `(2,1)` cases put `v[2:3]` (root dwords 1 and 2) next to `v[1:2]`. - A single double at an odd offset: the test never uses `v[2:3]` alone, with itself or with a constant. `src_f64` (`lower.rs:8374`) does change the low dword of every source, so it may cover this case too, but nothing measures it. Only the low dword changes, and a `bfrev` pair is the identity, so an optimizer could in principle remove it (a guess, not checked). adrianmjim found that on RDNA3 the test passes even without the fix, so the RX 9060 XT is the only reproduction so far. Adding a `v[2:3]`-alone case to the test would check this. NaN to int, also from #1262: `v_cvt_i32_f64` and `v_cvt_u32_f64` here are a plain `fptosi`/`fptoui` (`lower.rs:8330-8341` on `main`), so a NaN input gives whatever the host returns. In that thread an RTX 5080 under Wine/vkd3d-proton returned `0x80000000` for both. A native RTX 3050 and WARP returned 0, which is what the hardware gives. This PR doesn't touch those lines; I'm noting it only because they sit in the same block. AI-assisted (Claude Code, Claude Opus 5.5).
This pull request can be merged automatically.
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/198/head:github-bot/gh-198'
git switch 'github-bot/gh-198'

Merge

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