Shaders: 16-bit compares read the low halves whatever op_sel says, as RDNA2 does #926

Open
github-bot wants to merge 3 commits from github-bot/gh-926 into main AGit
Owner

@⁠BastienRodz on GitHub · 2026-11-24 14:17 UTC · original
BastienRodz:shader/cmp16-ignores-op-sel → main · +120 −2 · 2 files · 3 commits · open

What's wrong

On RDNA2 the VOP3 op_sel bits are ignored by the 16-bit compares (v_cmp_* and v_cmpx_* on f16, u16, i16): both sources come from the low 16 bits. #833 measured it on gfx1036 (146 rows, every source kind, wave32 and wave64), with v_add_nc_u16 as the control that does honour the bits. half_src honours op_sel for every 16-bit operation:

let mut high = v.opsel >> k & 1 != 0;

So v_cmp_lt_f16_e64 vcc, v0, v1 op_sel:[1,0] with v0 = 0x4000_3C00 and v1 = 0x3C00_3E00 compares high(v0) 2.0 < low(v1) 1.5 and gives false, where the hardware compares 1.0 < 1.5 and gives true.

What the fix does

half_src, which half_fsrc also goes through, ignores op_sel when the instruction is a v_cmp* other than v_cmp_class* (whose behaviour was not measured, so it stays as it was). The SDWA select below it, which does pick the half on hardware, is unchanged, and so is every non-compare 16-bit operation.

How it behaves after the fix

16-bit compares read the low halves whatever op_sel says, as RDNA2 does. Nothing changes for Grand Theft Auto VI: #833 counted 166 VOP3 16-bit compares in its shaders, none with op_sel set.

Tests

cmp16_ignores_op_sel_as_rdna2_does (valu_tests.rs, run on the host GPU and WARP):

  • v_cmp_lt_f16, v_cmp_lt_u16 and v_cmp_lt_i16, each with op_sel 0b01, 0b10 and 0b11, on values where the high and low halves give opposite answers;
  • v_cmpx_lt_f16_e64 writing EXEC;
  • a control: v_add_nc_u16 with op_sel:[1,0] still adds the high half.

cmp16_sdwa_preserves_word_selects (by @⁠MichaelSmithAI, BastienRodz/VIctory#5): v_cmp_lt_{f16,i16,u16}_sdwa still honour the SDWA word selects, low/low and either high word.

On GitHub's windows-latest, Rust 1.98.1, with the upstream CI's commands (clippy -D warnings with and without --all-features, cargo test --workspace --locked -- --test-threads=1):

cargo xtask policy: ok. Synthetic programs only; no game data.

Not tested: v_cmp_class_* with op_sel (left as it was); a title that sets op_sel on a 16-bit compare.

AI-assisted: implemented with Gemini 4 Neon, reviewed and run with Claude Code.

Fixes #833.

🤖 Generated with Claude Code

**@⁠BastienRodz** on GitHub · 2026-11-24 14:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/926) `BastienRodz:shader/cmp16-ignores-op-sel` → `main` · +120 −2 · 2 files · 3 commits · open ## What's wrong On RDNA2 the VOP3 `op_sel` bits are ignored by the 16-bit compares (`v_cmp_*` and `v_cmpx_*` on `f16`, `u16`, `i16`): both sources come from the low 16 bits. #833 measured it on gfx1036 (146 rows, every source kind, wave32 and wave64), with `v_add_nc_u16` as the control that does honour the bits. `half_src` honours `op_sel` for every 16-bit operation: ```rust let mut high = v.opsel >> k & 1 != 0; ``` So `v_cmp_lt_f16_e64 vcc, v0, v1 op_sel:[1,0]` with v0 = `0x4000_3C00` and v1 = `0x3C00_3E00` compares high(v0) 2.0 < low(v1) 1.5 and gives false, where the hardware compares 1.0 < 1.5 and gives true. ## What the fix does `half_src`, which `half_fsrc` also goes through, ignores `op_sel` when the instruction is a `v_cmp*` other than `v_cmp_class*` (whose behaviour was not measured, so it stays as it was). The SDWA select below it, which does pick the half on hardware, is unchanged, and so is every non-compare 16-bit operation. ## How it behaves after the fix 16-bit compares read the low halves whatever `op_sel` says, as RDNA2 does. Nothing changes for Grand Theft Auto VI: #833 counted 166 VOP3 16-bit compares in its shaders, none with `op_sel` set. ## Tests `cmp16_ignores_op_sel_as_rdna2_does` (`valu_tests.rs`, run on the host GPU and WARP): - `v_cmp_lt_f16`, `v_cmp_lt_u16` and `v_cmp_lt_i16`, each with `op_sel` `0b01`, `0b10` and `0b11`, on values where the high and low halves give opposite answers; - `v_cmpx_lt_f16_e64` writing EXEC; - a control: `v_add_nc_u16` with `op_sel:[1,0]` still adds the high half. `cmp16_sdwa_preserves_word_selects` (by @⁠MichaelSmithAI, BastienRodz/VIctory#5): `v_cmp_lt_{f16,i16,u16}_sdwa` still honour the SDWA word selects, low/low and either high word. On GitHub's `windows-latest`, Rust 1.98.1, with the upstream CI's commands (clippy `-D warnings` with and without `--all-features`, `cargo test --workspace --locked -- --test-threads=1`): - **This change:** everything passes; `victory-shader` 121 passed, 0 failed (https://github.com/BastienRodz/VIctory/actions/runs/45389398916). - **With the SDWA test added (head `e477c4b`'s parent):** everything passes; `victory-shader` 122 passed, 0 failed (https://github.com/BastienRodz/VIctory/actions/runs/45414030202). - **The new test alone on `main`:** it fails at the first case, `hardware: v_cmp_lt_f16 opsel=0b1 (0x40003c00, 0x3c003e00) = 0, want 1` (https://github.com/BastienRodz/VIctory/actions/runs/45389413162). `cargo xtask policy`: ok. Synthetic programs only; no game data. **Not tested:** `v_cmp_class_*` with `op_sel` (left as it was); a title that sets `op_sel` on a 16-bit compare. AI-assisted: implemented with Gemini 4 Neon, reviewed and run with Claude Code. Fixes #833. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Previously `half_src` applied VOP3 `op_sel` bits to every 16-bit operation, causing 16-bit compares (`v_cmp_*` and `v_cmpx_*` on f16, u16, and i16) to read the high half when `op_sel` was set. As measured on gfx1036 (RDNA2) across 146 cases in #833 (VGPR, SGPR, literal, inline-constant sources, wave32/wave64, and `v_cmpx_*_e64`), RDNA2 hardware ignores VOP3 `op_sel` on 16-bit compares and always compares the low halves, while non-compare 16-bit ops such as `v_add_nc_u16` still honour `op_sel`.

Ignore VOP3 `op_sel` in `half_src` (which `half_fsrc` calls as well) for `v_cmp*` instructions except `v_cmp_class*` (not measured), while keeping SDWA word selects intact, and add an end-to-end VALU test covering f16/u16/i16 compares, `v_cmpx_lt_f16`, and `v_add_nc_u16` as a control.

Co-Authored-By: Gemini 4 Neon <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:16 +00:00
Author
Owner

@⁠pickles976 on GitHub · 2026-11-24 14:22 UTC · original

Same divergence confirmed on the sibling runtime (ivall/xcl) — our VOPC emitter selects the compared half with m.opsel for all 16-bit types (vopc's half closure: m.opsel >> i & 1, f16/u16/i16 alike; v_cmp_class too, which you deliberately left alone). Console-exact is your measured behavior (Series = gfx103x), so ours is the wrong side; it is dormant for this title by your own corpus number — 166 16-bit VOPC compares, none with op_sel set — which is exactly why we never tripped it. Filing it over here with your #833 measurement as the reference so nobody has to re-derive the quirk; thanks for the v_add_nc_u16 control, that is what makes the 146-row result airtight.

**@⁠pickles976** on GitHub · 2026-11-24 14:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/926#issuecomment-7134261430) Same divergence confirmed on the sibling runtime (ivall/xcl) — our VOPC emitter selects the compared half with `m.opsel` for **all** 16-bit types (`vopc`'s `half` closure: `m.opsel >> i & 1`, f16/u16/i16 alike; `v_cmp_class` too, which you deliberately left alone). Console-exact is your measured behavior (Series = gfx103x), so ours is the wrong side; it is **dormant for this title** by your own corpus number — 166 16-bit VOPC compares, none with `op_sel` set — which is exactly why we never tripped it. Filing it over here with your #833 measurement as the reference so nobody has to re-derive the quirk; thanks for the `v_add_nc_u16` control, that is what makes the 146-row result airtight.
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 14:23 UTC · original

Independent RTX 4090 Wine/vkd3d-proton 3.2 result: the authored VOP3/EXEC test passed on a8aa1d1. A separate nine-case readback confirmed that SDWA compares still honor source-word selection for f16, i16 and u16 on both ffe310e and a8aa1d1. Low/low compared true while choosing either high word compared false. The test-only follow-up adds that neighboring behavior to the regression suite. The class-compare run checks existing translator behavior only. RDNA2 class-compare op_sel remains unmeasured.

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 14:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/926#issuecomment-7134294224) Independent RTX 4090 Wine/vkd3d-proton 3.2 result: the authored VOP3/EXEC test passed on a8aa1d1. A separate nine-case readback confirmed that SDWA compares still honor source-word selection for f16, i16 and u16 on both ffe310e and a8aa1d1. Low/low compared true while choosing either high word compared false. The [test-only follow-up](https://github.com/BastienRodz/VIctory/pull/5) adds that neighboring behavior to the regression suite. The class-compare run checks existing translator behavior only. RDNA2 class-compare op_sel remains unmeasured.
Author
Owner

@⁠shortselow-byte on GitHub · 2026-11-24 14:27 UTC · original

Tested at head a8aa1d1 on an AMD host, to add a third backend to the NVIDIA + WARP run above: Windows 11, Rust 1.98.1 MSVC, AMD Radeon RX 6900 XT (RDNA2, driver 32.0.21045.16010).

  • cargo test --locked -p victory-shader cmp16_ignores_op_sel: passed on the AMD device and WARP (neither skipped).
  • cargo test --locked -p victory-shader valu_tests: 14 passed, 0 failed.
  • cargo clippy --workspace --all-targets --locked -- -D warnings, and with --all-features: clean.
  • With the half_src change reverted, the new test fails on the AMD device as it should: hardware: v_cmp_lt_f16 opsel=0b1 (0x40003c00, 0x3c003e00) = 0, want 1.

This checks our translation on AMD's driver, not the console's raw op_sel behaviour, which rests on #833's measurement.

Nit: half_src's doc comment still says op_sel picks the high half; it could mention that 16-bit compares (other than v_cmp_class) ignore it.

**@⁠shortselow-byte** on GitHub · 2026-11-24 14:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/926#issuecomment-7134398025) Tested at head `a8aa1d1` on an AMD host, to add a third backend to the NVIDIA + WARP run above: Windows 11, Rust 1.98.1 MSVC, **AMD Radeon RX 6900 XT** (RDNA2, driver 32.0.21045.16010). - `cargo test --locked -p victory-shader cmp16_ignores_op_sel`: passed on the AMD device and WARP (neither skipped). - `cargo test --locked -p victory-shader valu_tests`: 14 passed, 0 failed. - `cargo clippy --workspace --all-targets --locked -- -D warnings`, and with `--all-features`: clean. - With the `half_src` change reverted, the new test fails on the AMD device as it should: `hardware: v_cmp_lt_f16 opsel=0b1 (0x40003c00, 0x3c003e00) = 0, want 1`. This checks our translation on AMD's driver, not the console's raw `op_sel` behaviour, which rests on #833's measurement. Nit: `half_src`'s doc comment still says op_sel picks the high half; it could mention that 16-bit compares (other than `v_cmp_class`) ignore it.
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-24 14:28 UTC · original

Thanks all. Pulled in @⁠MichaelSmithAI's SDWA test (f459735, windows-latest: victory-shader 122 passed, https://github.com/BastienRodz/VIctory/actions/runs/45414030202) and fixed the half_src doc comment @⁠shortselow-byte pointed out (e477c4b, comment only).

**@⁠BastienRodz** on GitHub · 2026-11-24 14:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/926#issuecomment-7134440419) Thanks all. Pulled in @⁠MichaelSmithAI's SDWA test (`f459735`, windows-latest: `victory-shader` 122 passed, https://github.com/BastienRodz/VIctory/actions/runs/45414030202) and fixed the `half_src` doc comment @⁠shortselow-byte pointed out (`e477c4b`, comment only).
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 15:41 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at e477c4b: cargo test -p victory-shader passes, 122 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 15:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/926#issuecomment-7136436638) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at e477c4b: `cargo test -p victory-shader` passes, 122 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-24 21:50 UTC · original

Rebased onto main 70564ae; the only conflicts were test blocks added on both sides at the end of the file. On windows-latest the run is red, but only on emit::synthetic_tests::gathers_at_a_level_match_the_hardware and permutes_read_the_lane_named_and_zero_from_lanes_left_out. main alone fails those two the same way, in its own CI (https://github.com/Marvisak/VIctory/actions/runs/45492970391) and on our fork (https://github.com/BastienRodz/VIctory/actions/runs/45494314536): the runner's adapter is a software one. This PR's own tests pass there. A fix for those two tests is on its way as a separate PR.

**@⁠BastienRodz** on GitHub · 2026-11-24 21:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/926#issuecomment-7145450649) Rebased onto `main` `70564ae`; the only conflicts were test blocks added on both sides at the end of the file. On `windows-latest` the run is red, but only on `emit::synthetic_tests::gathers_at_a_level_match_the_hardware` and `permutes_read_the_lane_named_and_zero_from_lanes_left_out`. `main` alone fails those two the same way, in its own CI (https://github.com/Marvisak/VIctory/actions/runs/45492970391) and on our fork (https://github.com/BastienRodz/VIctory/actions/runs/45494314536): the runner's adapter is a software one. This PR's own tests pass there. A fix for those two tests is on its way as a separate PR.
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-24 21:59 UTC · original

The fix for those two main tests is #1752 (windows-latest green with it).

**@⁠BastienRodz** on GitHub · 2026-11-24 21:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/926#issuecomment-7145652617) The fix for those two `main` tests is #1752 (`windows-latest` green with it).
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-25 00:56 UTC · original

Still red on the same two victory-shader tests: main's own CI after 42363ab fails them too. The runner's Basic Render Driver doesn't set the software flag that commit checks. #1919 adds its vendor/device ids, and windows-latest is green with it. This PR's crate passes.

**@⁠BastienRodz** on GitHub · 2026-11-25 00:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/926#issuecomment-7149049592) Still red on the same two `victory-shader` tests: `main`'s own CI after `42363ab` fails them too. The runner's Basic Render Driver doesn't set the software flag that commit checks. #1919 adds its vendor/device ids, and `windows-latest` is green with it. This PR's crate passes.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 02:32 UTC · original · edited 2026-11-26 01:23 UTC

Automated triage for the maintainers.

Merge candidate (a recommendation to look, not an approval). The new push only rewords the doc comment on half_src (crates/victory-shader/src/emit/lower.rs) to say that 16-bit compares other than v_cmp_class ignore op_sel; the code is unchanged from the earlier review. half_src clears op_sel for v_cmp*/v_cmpx* except the class compares, while the SDWA word select below it still picks the half, which matches the gfx1036 measurement in #833 (with v_add_nc_u16 as the control). The tests in valu_tests.rs take their expected values from that hardware behaviour and include the SDWA and v_add_nc_u16 controls.

The branch merges cleanly. CI: check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 02:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/926#issuecomment-7150860084) · edited 2026-11-26 01:23 UTC <!-- victory-triage v1 head=d69cac127e53150c27dc86966a7557d3d2d7b3aa verdict=merge-candidate at=2026-11-26T01:23:23Z --> Automated triage for the maintainers. **Merge candidate** (a recommendation to look, not an approval). The new push only rewords the doc comment on `half_src` (`crates/victory-shader/src/emit/lower.rs`) to say that 16-bit compares other than `v_cmp_class` ignore op_sel; the code is unchanged from the earlier review. `half_src` clears op_sel for `v_cmp*`/`v_cmpx*` except the class compares, while the SDWA word select below it still picks the half, which matches the gfx1036 measurement in #833 (with `v_add_nc_u16` as the control). The tests in `valu_tests.rs` take their expected values from that hardware behaviour and include the SDWA and `v_add_nc_u16` controls. The branch merges cleanly. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:27 UTC · original

Code review and RTX 5090 + WARP + DXIL verification at head d69cac127e53 merged cleanly onto main (63bcca43c6a0; emit/lower.rs = 520,843 B, 3,445 B under the 512 KiB cap):

  • Code review (crates/victory-shader/src/emit/lower.rs:6806-6822):
    • let compare = name.starts_with("v_cmp") && !name.contains("class"); let mut high = !compare && v.opsel >> k & 1 != 0; covers all 16-bit v_cmp_* and v_cmpx_* (f16/u16/i16) comparisons while explicitly exempting v_cmp_class_f16, preserving v.opsel for 16-bit ALU/conversion ops, and keeping v.sdwa WORD_0/WORD_1 selection intact afterwards.
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, dxil.dll 10.0.26100.0, VICTORY_D3D12_DEBUG=1, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy (default + --all-features): 0 warnings
    • cmp16 emit::tests emit::lower::tests: 52/52 passed on both RTX 5090 and WARP (including cmp16_ignores_op_sel_as_rdna2_does and cmp16_sdwa_preserves_word_selects).

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/926#issuecomment-7179785490) Code review and RTX 5090 + WARP + DXIL verification at head `d69cac127e53` merged cleanly onto `main` (`63bcca43c6a0`; `emit/lower.rs` = `520,843` B, `3,445` B under the 512 KiB cap): - **Code review (`crates/victory-shader/src/emit/lower.rs:6806-6822`)**: - `let compare = name.starts_with("v_cmp") && !name.contains("class"); let mut high = !compare && v.opsel >> k & 1 != 0;` covers all 16-bit `v_cmp_*` and `v_cmpx_*` (`f16`/`u16`/`i16`) comparisons while explicitly exempting `v_cmp_class_f16`, preserving `v.opsel` for 16-bit ALU/conversion ops, and keeping `v.sdwa` `WORD_0`/`WORD_1` selection intact afterwards. - **Verified (Windows 11, NVIDIA RTX 5090 + WARP, `dxil.dll` 10.0.26100.0, `VICTORY_D3D12_DEBUG=1`, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy` (default + `--all-features`): 0 warnings - `cmp16 emit::tests emit::lower::tests`: **52/52 passed** on both RTX 5090 and WARP (including `cmp16_ignores_op_sel_as_rdna2_does` and `cmp16_sdwa_preserves_word_selects`). LGTM.
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/926/head:github-bot/gh-926'
git switch 'github-bot/gh-926'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-926'
git switch 'github-bot/gh-926'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-926'
git switch 'github-bot/gh-926'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-926'
git switch 'main'
git merge --squash 'github-bot/gh-926'
git switch 'main'
git merge --ff-only 'github-bot/gh-926'
git switch 'main'
git merge 'github-bot/gh-926'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!926
No description provided.