Skip to content

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

Open
BastienRodz wants to merge 3 commits into
Marvisak:mainfrom
BastienRodz:shader/cmp16-ignores-op-sel
Open

BastienRodz wants to merge 3 commits into
Marvisak:mainfrom
BastienRodz:shader/cmp16-ignores-op-sel

Conversation

@BastienRodz

@BastienRodz BastienRodz commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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#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

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested head a8aa1d1 (fast-forward on main ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell) and WARP:

  • cargo test -p victory-shader valu_tests: 14 passed, 0 failed (including cmp16_ignores_op_sel_as_rdna2_does on both RTX 5090 and WARP)
  • cargo clippy -p victory-shader --all-targets: 0 warnings
  • cargo xtask policy: ok

Clean, focused fix matching RDNA2 16-bit compare op_sel semantics. Approving.

AI-assisted (Gemini 4 Neon).

@pickles976

Copy link
Copy Markdown

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.

@MichaelSmithAI

Copy link
Copy Markdown

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.

@shortselow-byte

Copy link
Copy Markdown

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.

@BastienRodz

Copy link
Copy Markdown
Contributor Author

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).

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested e477c4b on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 122 passed, including cmp16_ignores_op_sel_as_rdna2_does on the GPU backend, and cargo clippy -p victory-shader --all-targets is clean.

  • Ignoring op_sel only for v_cmp* (and v_cmpx*), not v_cmp_class, follows exactly what #833 measured, and the v_add_nc_u16 control keeps honouring it. The test's cases are built so that reading either high half flips the result, so a regression back to honouring op_sel fails it.
  • v_cmp_class is left as before ("not measured"). Worth a follow-up row in #833's harness so the exception is measured rather than assumed.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
… halves whatever op_sel says, as RDNA2 does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JMAN730 pushed a commit to JMAN730/project-flamingo that referenced this pull request Nov 24, 2026
… RDNA2 does

Upstream: Marvisak#926 by @BastienRodz

Co-authored-by: BastienRdz <58186763+BastienRodz@users.noreply.github.com>
@Zipettte

Copy link
Copy Markdown

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).

@BastienRodz

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Contributor Author

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

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@BastienRodz
BastienRodz force-pushed the shader/cmp16-ignores-op-sel branch from 3cdbc0c to ae9158a Compare November 25, 2026 00:46
@BastienRodz

Copy link
Copy Markdown
Contributor Author

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
BastienRodz force-pushed the shader/cmp16-ignores-op-sel branch from ae9158a to 13b6019 Compare November 25, 2026 01:14

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. 16-bit compares read the low halves whatever op_sel says — hardware-accurate compare semantics; oldest PR in the backlog (opened early), still mergeable.

BastienRodz and others added 3 commits November 25, 2026 21:50
… RDNA2 does

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 Marvisak#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 <noreply@google.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@BastienRodz
BastienRodz force-pushed the shader/cmp16-ignores-op-sel branch from 13b6019 to d69cac1 Compare November 25, 2026 21:52
@fuzeofficlal

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

16-bit compares honour VOP3 op_sel, RDNA2 ignores it (no effect on GTA VI)

10 participants