Shaders: VOP3 forms of VOP1 and VOP2 16-bit operations ignore op_sel #3364

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

@⁠Lyrnc on GitHub · 2026-11-26 20:36 UTC · original
Lyrnc:fix/vop3-promoted-16bit-opsel → main · +80 −8 · 2 files · 1 commits · open

What was wrong

On RDNA2, the VOP3 op_sel bits do nothing on a VOP1 or VOP2 opcode in its VOP3 form. Each source is read from its low half. The result goes to the low half of the destination, and the high half is kept. Only the VOP3-only 16-bit opcodes use the bits.

half_src, write_half and the v_fmac_f16 accumulator honour op_sel for every 16-bit operation, so the VOP3 forms of the VOP1 and VOP2 ones read and write the wrong half.

Examples on gfx1036. Each register holds two different halves (v1 = 0xbe05_43e7, v2 = 0x4f2f_2804), and v0 starts as 0xa9f1_cf51:

instruction gfx1036 main
v_add_f16_e64 v0, v1, v2 op_sel:[1,0,0] 0xa9f1_43f7 (low + low) 0xa9f1_bde5 (high half of v1)
v_add_f16_e64 v0, v1, v2 op_sel:[0,0,1] (destination high) 0xa9f1_43f7 0x43f7_cf51
v_cvt_u16_f16_e64 v0, v1 op_sel:[0,1] 0xa9f1_0003 0x0003_cf51

It is the same with v_cvt_u16_f16 and v_cvt_i16_f16, the conversions between f16 and 16-bit integers or f32, and with v_rcp_f16, v_sqrt_f16, v_floor_f16, v_fract_f16, v_cvt_norm_i16_f16, v_add_f16, v_mul_f16, v_max_f16 and v_fmac_f16. On all of them, every op_sel value gives the result that op_sel 0 gives, on all 64 rows. The VOP3-only v_fma_f16, v_med3_f16, v_add_nc_u16, v_mul_lo_u16, v_max_u16 and v_lshlrev_b16 do use the bits, and main already matches them.

What changed

half_opsel(inst) returns the op_sel bits that pick halves. It is 0 for VOP3 opcodes 0x100..0x13f (VOP2) and 0x180..0x1ff (VOP1), and v.opsel otherwise. half_src, write_half and the v_fmac_f16 accumulator read it in place of v.opsel. Compares (VOPC) and the VOP3P paths are unchanged.

Tests

  • New test valu_tests::vop3_forms_of_vop1_and_vop2_16bit_ops_ignore_op_sel. It covers 15 VOP1/VOP2 operations with up to four op_sel values each, plus VOP3-only controls (v_fma_f16, v_add_nc_u16). The rows and results were measured on gfx1036 with the same instruction words.
    • Radeon gfx1036 (picked with a local adapter-name filter): passes. On main it fails at the first case, hardware: v_cvt_f16_u16 op_sel 0b0001.
    • RTX 5080 (610.88): passes. On main it fails at the same case.
    • WARP runs in the same test through run_cases, and passes.
  • cargo test -p victory-shader --lib, lib tests (cargo test -p victory-shader --lib -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index; that test crashes the process on main too): 297 passed, 5 failed, 3 ignored. main 63bcca4 on this machine: 296 passed and the same 5 failed (rect::tests::rectangles_get_their_fourth_corner, stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, synthetic_tests::gathers_at_a_level_match_the_hardware, tests::elements_sharing_a_row_keep_their_order).
  • cargo clippy -p victory-shader --lib --tests -- -D warnings: clean.
  • cargo xtask policy: ok (191 files).

Hardware comparison

A probe runs the same instruction words through the hw oracle on gfx1036 (one row per lane) and through the translator (a local build_valu_cs harness, a local adapter-name filter in hardware_adapter). The comparison covers 22 16-bit opcodes, 64 rows each, with op_sel 0 and three or seven other values, for 8,448 results.

host branch op_sel 0 op_sel ≠ 0
Radeon gfx1036 main 1,344 match (1 NaN payload only) 2,959 match, 3,633 differ
Radeon gfx1036 this PR 1,344 match (1 NaN payload only) 6,592 match (7 NaN payload only), 0 differ
RTX 5080 main 1,344 match (38 NaN payload only) 2,955 match, 3,637 differ
RTX 5080 this PR 1,344 match (38 NaN payload only) 6,592 match (118 NaN payload only), 0 differ

These counts leave out v_mad_u16: main refuses it, so its 512 results are not compared.

"NaN payload only" means both results are NaN with different payloads. These are the signalling NaN that v_fmac_f16 quietens, and, on the 5080, the host's NaN from v_sqrt_f16 of a negative number. Both are the same at op_sel 0.

  • #2963 (abs/neg on v_cvt_{u16,i16}_f16): independent. lower.rs merges cleanly. valu_tests.rs conflicts only because both PRs add a test at the end of the file; keep both tests.
    • The #2963 hardware probe has 2 opcodes × 4 abs/neg combinations × 192 halves × op_sel 0, 1, 8 and 9, plus the unmodified forms: 7,680 results.
    • #2963 alone matches all 1,536 op_sel 0 results, but 4,168 of the op_sel 1/8/9 results differ.
    • #2963 with this PR matches all 7,680, on the Radeon and on the 5080.
  • #926 / #833 (16-bit compares ignore op_sel): the same hardware rule for VOPC. This PR leaves compares alone. Both PRs edit the same line of half_src, so whichever lands second needs a small rebase. half_opsel could take the compares too.

Not tested

  • No title run. I don't know how often the title sets op_sel on these forms. It would be rare, since a compiler for this hardware has no reason to set bits that do nothing.
  • v_ldexp_f16 and v_cvt_pkrtz-style 32-bit-source VOP2 ops in VOP3 form. Main refuses the first. The second has no 16-bit source.
  • v_cmp_class_f16 and the other compares (see #926).
  • Wave64.

AI use

Claude Code (Claude Opus 5.5) wrote the probes, the fix and the test, and ran them on the GPUs above. The numbers come from those runs. No game data was used.

🤖 Generated with Claude Code

**@⁠Lyrnc** on GitHub · 2026-11-26 20:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/3364) `Lyrnc:fix/vop3-promoted-16bit-opsel` → `main` · +80 −8 · 2 files · 1 commits · open ### What was wrong On RDNA2, the VOP3 `op_sel` bits do nothing on a VOP1 or VOP2 opcode in its VOP3 form. Each source is read from its low half. The result goes to the low half of the destination, and the high half is kept. Only the VOP3-only 16-bit opcodes use the bits. `half_src`, `write_half` and the `v_fmac_f16` accumulator honour `op_sel` for every 16-bit operation, so the VOP3 forms of the VOP1 and VOP2 ones read and write the wrong half. Examples on gfx1036. Each register holds two different halves (v1 = `0xbe05_43e7`, v2 = `0x4f2f_2804`), and v0 starts as `0xa9f1_cf51`: | instruction | gfx1036 | main | |---|---|---| | `v_add_f16_e64 v0, v1, v2 op_sel:[1,0,0]` | `0xa9f1_43f7` (low + low) | `0xa9f1_bde5` (high half of v1) | | `v_add_f16_e64 v0, v1, v2 op_sel:[0,0,1]` (destination high) | `0xa9f1_43f7` | `0x43f7_cf51` | | `v_cvt_u16_f16_e64 v0, v1 op_sel:[0,1]` | `0xa9f1_0003` | `0x0003_cf51` | It is the same with `v_cvt_u16_f16` and `v_cvt_i16_f16`, the conversions between f16 and 16-bit integers or f32, and with `v_rcp_f16`, `v_sqrt_f16`, `v_floor_f16`, `v_fract_f16`, `v_cvt_norm_i16_f16`, `v_add_f16`, `v_mul_f16`, `v_max_f16` and `v_fmac_f16`. On all of them, every op_sel value gives the result that op_sel 0 gives, on all 64 rows. The VOP3-only `v_fma_f16`, `v_med3_f16`, `v_add_nc_u16`, `v_mul_lo_u16`, `v_max_u16` and `v_lshlrev_b16` do use the bits, and main already matches them. ### What changed `half_opsel(inst)` returns the op_sel bits that pick halves. It is 0 for VOP3 opcodes 0x100..0x13f (VOP2) and 0x180..0x1ff (VOP1), and `v.opsel` otherwise. `half_src`, `write_half` and the `v_fmac_f16` accumulator read it in place of `v.opsel`. Compares (VOPC) and the VOP3P paths are unchanged. ### Tests - New test `valu_tests::vop3_forms_of_vop1_and_vop2_16bit_ops_ignore_op_sel`. It covers 15 VOP1/VOP2 operations with up to four op_sel values each, plus VOP3-only controls (`v_fma_f16`, `v_add_nc_u16`). The rows and results were measured on gfx1036 with the same instruction words. - Radeon gfx1036 (picked with a local adapter-name filter): passes. On main it fails at the first case, `hardware: v_cvt_f16_u16 op_sel 0b0001`. - RTX 5080 (610.88): passes. On main it fails at the same case. - WARP runs in the same test through `run_cases`, and passes. - `cargo test -p victory-shader --lib`, lib tests (`cargo test -p victory-shader --lib -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index`; that test crashes the process on main too): 297 passed, 5 failed, 3 ignored. main 63bcca4 on this machine: 296 passed and the same 5 failed (`rect::tests::rectangles_get_their_fourth_corner`, `stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride`, `stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros`, `synthetic_tests::gathers_at_a_level_match_the_hardware`, `tests::elements_sharing_a_row_keep_their_order`). - `cargo clippy -p victory-shader --lib --tests -- -D warnings`: clean. - `cargo xtask policy`: ok (191 files). ### Hardware comparison A probe runs the same instruction words through the hw oracle on gfx1036 (one row per lane) and through the translator (a local `build_valu_cs` harness, a local adapter-name filter in `hardware_adapter`). The comparison covers 22 16-bit opcodes, 64 rows each, with op_sel 0 and three or seven other values, for 8,448 results. | host | branch | op_sel 0 | op_sel ≠ 0 | |---|---|---|---| | Radeon gfx1036 | main | 1,344 match (1 NaN payload only) | 2,959 match, **3,633 differ** | | Radeon gfx1036 | this PR | 1,344 match (1 NaN payload only) | 6,592 match (7 NaN payload only), **0 differ** | | RTX 5080 | main | 1,344 match (38 NaN payload only) | 2,955 match, **3,637 differ** | | RTX 5080 | this PR | 1,344 match (38 NaN payload only) | 6,592 match (118 NaN payload only), **0 differ** | These counts leave out `v_mad_u16`: main refuses it, so its 512 results are not compared. "NaN payload only" means both results are NaN with different payloads. These are the signalling NaN that `v_fmac_f16` quietens, and, on the 5080, the host's NaN from `v_sqrt_f16` of a negative number. Both are the same at op_sel 0. ### Related - #2963 (abs/neg on `v_cvt_{u16,i16}_f16`): independent. `lower.rs` merges cleanly. `valu_tests.rs` conflicts only because both PRs add a test at the end of the file; keep both tests. - The #2963 hardware probe has 2 opcodes × 4 abs/neg combinations × 192 halves × op_sel 0, 1, 8 and 9, plus the unmodified forms: 7,680 results. - #2963 alone matches all 1,536 op_sel 0 results, but 4,168 of the op_sel 1/8/9 results differ. - #2963 with this PR matches all 7,680, on the Radeon and on the 5080. - #926 / #833 (16-bit compares ignore op_sel): the same hardware rule for VOPC. This PR leaves compares alone. Both PRs edit the same line of `half_src`, so whichever lands second needs a small rebase. `half_opsel` could take the compares too. ### Not tested - No title run. I don't know how often the title sets op_sel on these forms. It would be rare, since a compiler for this hardware has no reason to set bits that do nothing. - `v_ldexp_f16` and `v_cvt_pkrtz`-style 32-bit-source VOP2 ops in VOP3 form. Main refuses the first. The second has no 16-bit source. - `v_cmp_class_f16` and the other compares (see #926). - Wave64. ### AI use Claude Code (Claude Opus 5.5) wrote the probes, the fix and the test, and ran them on the GPUs above. The numbers come from those runs. No game data was used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On RDNA2 the op_sel bits of a VOP3-encoded VOP1 or VOP2 operation pick
no halves: v_cvt_u16_f16, v_cvt_i16_f16, the other f16 conversions,
v_rcp_f16, v_sqrt_f16, v_floor_f16, v_fract_f16, v_cvt_norm_i16_f16,
v_add_f16, v_mul_f16, v_max_f16 and v_fmac_f16 read the low halves and
write the low half whatever op_sel says (measured on gfx1036). The
VOP3-only 16-bit operations (v_fma_f16, v_med3_f16, v_add_nc_u16...)
honour the bits.

half_src, write_half and the v_fmac_f16 accumulator now read op_sel
through half_opsel, which is 0 for VOP3 opcodes 0x100..0x13f (VOP2)
and 0x180..0x1ff (VOP1). Compares are left as they are.

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

@⁠fuzeofficlal on GitHub · 2026-11-26 20:48 UTC · original · approved these changes

LGTM — half_opsel (Encoding::Vop3 with opcodes 0x100..0x140 | 0x180..0x200) cleanly masks out op_sel across all 27 promoted VOP1/VOP2 16-bit instructions reaching half_src, write_half, and the v_fmac_f16 accumulator while leaving VOP3-only ops, SDWA (Encoding::Vop1/Vop2), VOP3P, and VOPC intact.

Verified

  • crates/victory-shader/src/emit/lower.rs size: 521,548 bytes (under the 524,288-byte limit).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --all-features): clean.
  • rustfmt --edition 2024: clean on all PR-touched hunks in lower.rs and valu_tests.rs.
  • emit::valu_tests::vop3_forms_of_vop1_and_vop2_16bit_ops_ignore_op_sel (RTX 5090 + WARP): passed; emit::lower: 33 passed, 0 failed.

Minor non-blocking note

  • The test table covers 15 of the 27 affected promoted opcodes (plus VOP3-only controls); the remaining 12 (v_sub_f16, v_subrev_f16, v_min_f16, v_rsq_f16, v_log_f16, v_exp_f16, v_ceil_f16, v_trunc_f16, v_rndne_f16, v_sin_f16, v_cos_f16, v_cvt_norm_u16_f16) share the exact same half_opsel routing without dedicated rows.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 20:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3364#pullrequestreview-6080339492) · **approved these changes** LGTM — `half_opsel` (`Encoding::Vop3` with opcodes `0x100..0x140 | 0x180..0x200`) cleanly masks out `op_sel` across all 27 promoted VOP1/VOP2 16-bit instructions reaching `half_src`, `write_half`, and the `v_fmac_f16` accumulator while leaving VOP3-only ops, SDWA (`Encoding::Vop1`/`Vop2`), VOP3P, and VOPC intact. ### Verified - `crates/victory-shader/src/emit/lower.rs` size: **521,548 bytes** (under the 524,288-byte limit). - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default & `--all-features`): clean. - `rustfmt --edition 2024`: clean on all PR-touched hunks in `lower.rs` and `valu_tests.rs`. - `emit::valu_tests::vop3_forms_of_vop1_and_vop2_16bit_ops_ignore_op_sel` (RTX 5090 + WARP): **passed**; `emit::lower`: **33 passed, 0 failed**. ### Minor non-blocking note - The test table covers 15 of the 27 affected promoted opcodes (plus VOP3-only controls); the remaining 12 (`v_sub_f16`, `v_subrev_f16`, `v_min_f16`, `v_rsq_f16`, `v_log_f16`, `v_exp_f16`, `v_ceil_f16`, `v_trunc_f16`, `v_rndne_f16`, `v_sin_f16`, `v_cos_f16`, `v_cvt_norm_u16_f16`) share the exact same `half_opsel` routing without dedicated rows.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 20:51 UTC · original · approved these changes

LGTM — verified on RTX 5090 and VICTORY_WARP=1 (Windows / D3D12) against origin/main (63bcca4).

  • Ignoring op_sel on VOP3-encoded forms of VOP1 and VOP2 16-bit operations matches GFX10/RDNA2 ISA semantics cleanly on both RTX 5090 and WARP.
  • cargo test -p victory-shader --lib emit::valu_tests::vop3_forms_of_vop1_and_vop2_16bit_ops_ignore_op_sel -- --test-threads=1: passed on RTX 5090 and VICTORY_WARP=1.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 20:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/3364#pullrequestreview-6080361795) · **approved these changes** LGTM — verified on RTX 5090 and `VICTORY_WARP=1` (Windows / D3D12) against `origin/main` (`63bcca4`). - Ignoring `op_sel` on VOP3-encoded forms of VOP1 and VOP2 16-bit operations matches GFX10/RDNA2 ISA semantics cleanly on both RTX 5090 and WARP. - `cargo test -p victory-shader --lib emit::valu_tests::vop3_forms_of_vop1_and_vop2_16bit_ops_ignore_op_sel -- --test-threads=1`: passed on RTX 5090 and `VICTORY_WARP=1`. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 21:12 UTC · original

I combined 55d58e0 with the published #2623/#3192 resolution in d7e90f3. The new regression uses v_fmac_f16_e64 with distinct register halves, four accumulator modifier combinations, and five op_sel patterns. It checks the low-half result and the untouched upper half, including signed subnormals on either side of the minimum normal. Your gfx1036 measurements fit the LLVM GFX1030 operand table, which lists op_sel for v_fma_f16 and omits it for v_fmac_f16_e64.

The #2623/#3192 control matches all 16 cases with op_sel=0 and differs in all 64 with nonzero bits. With #3364 integrated, all 80 match on app-local WARP. The exact final tree passes 112 VALU, semantics and lowering tests, with one ignored. Both strict Clippy configurations pass. The combined lower.rs first exceeded the asset limit by 61 bytes, so the same half selection rule now lives on the decoded IR instruction. lower.rs is 523,424 bytes and the asset policy check passes.

@⁠Lyrnc, if you take the regression or the integration fix, please keep the Git author or use Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 21:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/3364#issuecomment-7203493095) I combined `55d58e0` with the published #2623/#3192 resolution in [`d7e90f3`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/d7e90f3822ae5cc18f1bb77fbe49afffd9a56bae). The new regression uses `v_fmac_f16_e64` with distinct register halves, four accumulator modifier combinations, and five `op_sel` patterns. It checks the low-half result and the untouched upper half, including signed subnormals on either side of the minimum normal. Your gfx1036 measurements fit the [LLVM GFX1030 operand table](https://releases.llvm.org/18.1.6/docs/AMDGPU/AMDGPUAsmGFX1030.html), which lists `op_sel` for `v_fma_f16` and omits it for `v_fmac_f16_e64`. The #2623/#3192 control matches all 16 cases with `op_sel=0` and differs in all 64 with nonzero bits. With #3364 integrated, all 80 match on app-local WARP. The exact final tree passes 112 VALU, semantics and lowering tests, with one ignored. Both strict Clippy configurations pass. The combined `lower.rs` first exceeded the asset limit by 61 bytes, so the same half selection rule now lives on the decoded IR instruction. `lower.rs` is 523,424 bytes and the asset policy check passes. @⁠Lyrnc, if you take the regression or the integration fix, please keep the Git author or use `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`.
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/3364/head:github-bot/gh-3364'
git switch 'github-bot/gh-3364'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3364'
git switch 'github-bot/gh-3364'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3364'
git switch 'github-bot/gh-3364'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3364'
git switch 'main'
git merge --squash 'github-bot/gh-3364'
git switch 'main'
git merge --ff-only 'github-bot/gh-3364'
git switch 'main'
git merge 'github-bot/gh-3364'
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!3364
No description provided.