Shaders: an SDWA WORD_0 result with UNUSED_SEXT is sign-extended (#823) #3124

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

@⁠KainanP on GitHub · 2026-11-26 09:08 UTC · original
KainanP:fix/sdwa-word0-sext-823 → main · +178 −16 · 2 files · 1 commits · open

Fixes #823

What

Lower::write_half (crates/victory-shader/src/emit/lower.rs) matched SDWA destinations of 16-bit VALU operations as (4 | 6, _) => keep = false, so dst_sel WORD_0 with dst_unused UNUSED_SEXT (1) zero-filled bits 31:16 instead of copying bit 15 into them. v_cvt_i16_f16_sdwa v0, v1 dst_sel:WORD_0 dst_unused:UNUSED_SEXT of -1.0 gave 0x0000ffff, not 0xffffffff. The 32-bit path, place_bits, already sign-extends for unused == 1. Now:

  • (4, 1) sign-extends the low word ((x << 16) >>a 16), the same shift place_bits uses.
  • WORD_0 + PAD, WORD_0 + PRESERVE, WORD_1 + PAD/SEXT/PRESERVE and DWORD behave as before.
  • Reserved dst_unused 3 on WORD_0/WORD_1 is refused instead of being read as PAD. llvm-mc rejects that encoding.

This is AndyLatimer's fork branch fix/shader-sdwa-word0-sext (24011cf) from the issue, rebased onto current main. He is credited as co-author. The only conflict was the two test files, where main had added tests at the same place. #1285 touches the same match, but it also rewrites the 16-bit source reads and inline constants. It has conflicted with main since 2026-11-25. This PR is only the destination fix.

Tests

  • emit::lower::tests::sdwa_16bit_reserved_dst_unused_is_refused needs no GPU (lowering only). Fails on main, because the reserved encoding is accepted. Passes with the fix.
  • emit::valu_tests::sdwa_16bit_dst_unused_sext_sign_extends_word0 (hardware + WARP through run_cases) checks v_cvt_i16_f16_sdwa, v_cvt_norm_i16_f16_sdwa (src0 WORD_1) and v_add_f16_sdwa with UNUSED_SEXT, plus every other dst_sel/dst_unused mode.

cargo test -p victory-shader --lib --locked on Windows 11 with main 63bcca4:

Adapter main this PR new tests on main's write_half
RTX 4060 Laptop (skipping heap_direct_texture_reads_mark_their_heap_index, #2571) 3 failed: two stride tests and gathers_at_a_level_match_the_hardware (#2317) 300 passed, the same 3 failed both fail: hardware: v_cvt_i16_f16_sdwa(0xbc00, WORD_0, UNUSED_SEXT) = 0x0000ffff, want 0xffffffff
Intel UHD (a78b), through a local adapter-selection hook that is not in this PR 292 passed, 10 failed 294 passed, the same 10 failed n/a
WARP (VICTORY_WARP=1) 298 passed, 4 failed 300 passed, the same 4 failed n/a

The new tests pass on all three adapters. No test that passes on main fails with this PR.

cargo clippy -p victory-shader --all-targets [--all-features] --locked -- -D warnings: clean. rustfmt --check reports nothing on the changed hunks.

Not tested: AMD hardware. Synthetic programs only, no game data.

AI use: Claude Code (Claude Opus 5.5) ported the fork commit, resolved the test-file conflict and ran the tests above on my machine. I reviewed the change.

🤖 Generated with Claude Code

**@⁠KainanP** on GitHub · 2026-11-26 09:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3124) `KainanP:fix/sdwa-word0-sext-823` → `main` · +178 −16 · 2 files · 1 commits · open Fixes #823 ## What `Lower::write_half` (`crates/victory-shader/src/emit/lower.rs`) matched SDWA destinations of 16-bit VALU operations as `(4 | 6, _) => keep = false`, so `dst_sel` WORD_0 with `dst_unused` UNUSED_SEXT (1) zero-filled bits 31:16 instead of copying bit 15 into them. `v_cvt_i16_f16_sdwa v0, v1 dst_sel:WORD_0 dst_unused:UNUSED_SEXT` of -1.0 gave `0x0000ffff`, not `0xffffffff`. The 32-bit path, `place_bits`, already sign-extends for `unused == 1`. Now: - `(4, 1)` sign-extends the low word (`(x << 16) >>a 16`), the same shift `place_bits` uses. - WORD_0 + PAD, WORD_0 + PRESERVE, WORD_1 + PAD/SEXT/PRESERVE and DWORD behave as before. - Reserved `dst_unused` 3 on WORD_0/WORD_1 is refused instead of being read as PAD. llvm-mc rejects that encoding. This is AndyLatimer's fork branch `fix/shader-sdwa-word0-sext` (24011cf) from the issue, rebased onto current `main`. He is credited as co-author. The only conflict was the two test files, where `main` had added tests at the same place. #1285 touches the same match, but it also rewrites the 16-bit source reads and inline constants. It has conflicted with `main` since 2026-11-25. This PR is only the destination fix. ## Tests - `emit::lower::tests::sdwa_16bit_reserved_dst_unused_is_refused` needs no GPU (lowering only). **Fails on `main`**, because the reserved encoding is accepted. Passes with the fix. - `emit::valu_tests::sdwa_16bit_dst_unused_sext_sign_extends_word0` (hardware + WARP through `run_cases`) checks `v_cvt_i16_f16_sdwa`, `v_cvt_norm_i16_f16_sdwa` (src0 WORD_1) and `v_add_f16_sdwa` with UNUSED_SEXT, plus every other dst_sel/dst_unused mode. `cargo test -p victory-shader --lib --locked` on Windows 11 with `main` 63bcca4: | Adapter | `main` | this PR | new tests on `main`'s `write_half` | |---|---|---|---| | RTX 4060 Laptop (skipping `heap_direct_texture_reads_mark_their_heap_index`, #2571) | 3 failed: two stride tests and `gathers_at_a_level_match_the_hardware` (#2317) | 300 passed, the same 3 failed | both fail: `hardware: v_cvt_i16_f16_sdwa(0xbc00, WORD_0, UNUSED_SEXT) = 0x0000ffff, want 0xffffffff` | | Intel UHD (a78b), through a local adapter-selection hook that is not in this PR | 292 passed, 10 failed | 294 passed, the same 10 failed | n/a | | WARP (`VICTORY_WARP=1`) | 298 passed, 4 failed | 300 passed, the same 4 failed | n/a | The new tests pass on all three adapters. No test that passes on `main` fails with this PR. `cargo clippy -p victory-shader --all-targets [--all-features] --locked -- -D warnings`: clean. `rustfmt --check` reports nothing on the changed hunks. Not tested: AMD hardware. Synthetic programs only, no game data. _AI use: Claude Code (Claude Opus 5.5) ported the fork commit, resolved the test-file conflict and ran the tests above on my machine. I reviewed the change._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
`write_half` took `dst_sel` WORD_0 with `dst_unused` UNUSED_SEXT (1) as
UNUSED_PAD, so a negative 16-bit result came out zero-extended
(v_cvt_i16_f16_sdwa of -1.0 gave 0x0000ffff, not 0xffffffff). The
32-bit path, `place_bits`, already sign-extends for `unused == 1`; the
16-bit path now does the same shift. Reserved `dst_unused` 3 on WORD_0
or WORD_1 is refused instead of being read as UNUSED_PAD.

Ported from AndyLatimer's fork branch fix/shader-sdwa-word0-sext
(24011cf), rebased onto current main: tests moved past the ones main
added since.

Co-authored-by: Manuel Pérez <[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 11:07:57 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 09:19 UTC · original

Automated triage for the maintainers.

Merge candidate. This fixes #823. On main, Lower::write_half (crates/victory-shader/src/emit/lower.rs) matches (4 | 6, _), so WORD_0 with UNUSED_SEXT zero-fills bits 31:16. The new (4, 1) arm sign-extends with the same shift-left/arithmetic-shift-right that place_bits already uses for 32-bit SDWA results. PAD, PRESERVE and DWORD come out as before, and WORD_1 with SEXT still zero-pads the low word, which matches place_bits. Reserved dst_unused 3 is now refused for WORD_0 and WORD_1. I checked the opcode encodings in the tests and the new vop1_sdwa/vop2_sdwa helpers, and the expected values follow the RDNA2 ISA's SDWA definitions. Optional follow-up: DWORD with reserved dst_unused 3 is still read as PAD and could be refused the same way.

CI: check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 09:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/3124#issuecomment-7187776374) <!-- victory-triage v1 head=ac958c93963c3d032d8e5670edd78e3c653f2950 verdict=merge-candidate at=2026-11-26T09:19:00Z --> Automated triage for the maintainers. **Merge candidate.** This fixes #823. On `main`, `Lower::write_half` (`crates/victory-shader/src/emit/lower.rs`) matches `(4 | 6, _)`, so WORD_0 with UNUSED_SEXT zero-fills bits 31:16. The new `(4, 1)` arm sign-extends with the same shift-left/arithmetic-shift-right that `place_bits` already uses for 32-bit SDWA results. PAD, PRESERVE and DWORD come out as before, and WORD_1 with SEXT still zero-pads the low word, which matches `place_bits`. Reserved `dst_unused` 3 is now refused for WORD_0 and WORD_1. I checked the opcode encodings in the tests and the new `vop1_sdwa`/`vop2_sdwa` helpers, and the expected values follow the RDNA2 ISA's SDWA definitions. Optional follow-up: DWORD with reserved `dst_unused` 3 is still read as PAD and could be refused the same way. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

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

Verified on RTX 5090 & WARP against origin/main (63bcca4):

  • cargo test -p victory-shader --lib sdwa_16bit_ -- --test-threads=1: 2/2 passed on RTX 5090 and WARP (sdwa_16bit_reserved_dst_unused_is_refused and sdwa_16bit_dst_unused_sext_sign_extends_word0).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly sign-extends SDWA WORD_0 results when UNUSED_SEXT is set and refuses reserved dst_unused encodings.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 09:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/3124#pullrequestreview-6071539746) · **approved these changes** Verified on RTX 5090 & WARP against `origin/main` (`63bcca4`): - `cargo test -p victory-shader --lib sdwa_16bit_ -- --test-threads=1`: 2/2 passed on RTX 5090 and WARP (`sdwa_16bit_reserved_dst_unused_is_refused` and `sdwa_16bit_dst_unused_sext_sign_extends_word0`). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly sign-extends SDWA `WORD_0` results when `UNUSED_SEXT` is set and refuses reserved `dst_unused` encodings. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 10:03 UTC · original · reviewed

In Lower::write_half, (x << 16) >>a 16 for (4, 1) (WORD_0 + UNUSED_SEXT) discards any stale upper bits and sign-extends bit 15, matching place_bits. The remaining WORD_0/WORD_1 modes (PAD/PRESERVE/reserved dst_unused = 3) and valu_tests.rs assertions (with 0xABCD_1234 pre-seed distinguishing sext/pad/preserve across v_cvt_i16_f16_sdwa, v_cvt_norm_i16_f16_sdwa, and v_add_f16_sdwa) are sound, and emit/lower.rs is 521,941 bytes (within the 512 KiB limit).

One question: (4, 0) | (6, _) still accepts reserved dst_unused = 3 when dst_sel == 6 (DWORD), matching write/sdwa_dst (dst_sel >= 6), whereas WORD_0 and WORD_1 now reject 3 as tested in sdwa_16bit_reserved_dst_unused_is_refused. Could you clarify whether DWORD intentionally ignores dst_unused = 3 (since DWORD writes the full 32-bit destination) or should reject it as well?

Verified locally on Windows 11 x64 (RTX 5090 + WARP + dxil.dll, ac958c93):

  • cargo run --locked -p xtask -- policy: passed (191 files ok)
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default and --all-features): clean
  • emit::lower::tests: 34 passed, 0 failed
  • sdwa_16bit_ -- --test-threads=1 (RTX 5090 and VICTORY_WARP=1): 2 passed, 0 failed on both
**@⁠fuzeofficlal** on GitHub · 2026-11-26 10:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/3124#pullrequestreview-6072052743) · **reviewed** In `Lower::write_half`, `(x << 16) >>a 16` for `(4, 1)` (`WORD_0` + `UNUSED_SEXT`) discards any stale upper bits and sign-extends bit 15, matching `place_bits`. The remaining `WORD_0`/`WORD_1` modes (`PAD`/`PRESERVE`/reserved `dst_unused = 3`) and `valu_tests.rs` assertions (with `0xABCD_1234` pre-seed distinguishing sext/pad/preserve across `v_cvt_i16_f16_sdwa`, `v_cvt_norm_i16_f16_sdwa`, and `v_add_f16_sdwa`) are sound, and `emit/lower.rs` is `521,941` bytes (within the 512 KiB limit). One question: `(4, 0) | (6, _)` still accepts reserved `dst_unused = 3` when `dst_sel == 6` (`DWORD`), matching `write`/`sdwa_dst` (`dst_sel >= 6`), whereas `WORD_0` and `WORD_1` now reject `3` as tested in `sdwa_16bit_reserved_dst_unused_is_refused`. Could you clarify whether `DWORD` intentionally ignores `dst_unused = 3` (since `DWORD` writes the full 32-bit destination) or should reject it as well? Verified locally on Windows 11 x64 (RTX 5090 + WARP + `dxil.dll`, `ac958c93`): - `cargo run --locked -p xtask -- policy`: passed (`191 files ok`) - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` (default and `--all-features`): clean - `emit::lower::tests`: 34 passed, 0 failed - `sdwa_16bit_ -- --test-threads=1` (RTX 5090 and `VICTORY_WARP=1`): 2 passed, 0 failed on both
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-26 17:21 UTC · original

Checked ac958c9 on RDNA2 hardware, for every SDWA dst_sel × dst_unused combination on 16-bit results, with the same instruction words on both sides.

  • Hardware: Radeon iGPU (Ryzen 7 9800X3D), gfx1036 / RDNA2, driver 32.0.21042.62. A HIP kernel (assembled by the driver's amd_comgr) runs 7 opcodes:

    • v_cvt_i16_f16, v_cvt_u16_f16, v_cvt_norm_i16_f16 and v_cvt_f16_i16;
    • v_cvt_f16_f32, with src0_sel DWORD;
    • v_add_f16 and v_mul_f16 (VOP2).

    Each opcode runs with dst_sel WORD_0, WORD_1 and DWORD, and with dst_unused PAD, SEXT and PRESERVE. There are 40 input rows:

    • negative and positive results, ±0, ±inf and NaNs;
    • saturating values and random values.

    Each destination is preset to a random word. That gives 2,520 results.

  • Translator: the same words through a local copy of the build_valu_cs harness (a local test, not part of this PR). I ran it on main 63bcca4 and on the PR merged onto it, on the RTX 5080 (610.88) and on the same Radeon (picked with a local adapter-name filter in hardware_adapter).

Host branch matches the hardware bit for bit NaN with a different payload other differences
Radeon gfx1036 main 2,408 4 108
Radeon gfx1036 #3124 2,520 0 0
RTX 5080 main 2,340 72 108
RTX 5080 #3124 2,448 72 0
  • main: all 108 differences are WORD_0 + SEXT with bit 15 set. The hardware sign-extends there and main zero-fills. This holds for all 7 opcodes, the float ones too: v_add_f16_sdwa(-1, -2) is 0xffffc200 on the hardware and 0x0000c200 on main.
  • This PR: it matches the hardware. Here is the hardware table for the result 0xffff (v_cvt_i16_f16(-1.0)), with the destination preset to 0xab8ed1ec:
dst_sel PAD SEXT PRESERVE
WORD_0 0x0000ffff 0xffffffff 0xab8effff
WORD_1 0xffff0000 0xffff0000 0xffffd1ec
DWORD 0x0000ffff 0x0000ffff 0x0000ffff

DWORD ignores PRESERVE for a 16-bit result, which is what (6, _) => keep = false assumes. I didn't test the reserved dst_unused value 3 on the hardware.

No game data was used.

AI use: Claude Code (Claude Opus 5.5) wrote the probe and the comparison and ran them on the GPUs above; the numbers come from those runs.

**@⁠Lyrnc** on GitHub · 2026-11-26 17:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/3124#issuecomment-7198886540) Checked `ac958c9` on RDNA2 hardware, for every SDWA `dst_sel` × `dst_unused` combination on 16-bit results, with the same instruction words on both sides. - **Hardware:** Radeon iGPU (Ryzen 7 9800X3D), gfx1036 / RDNA2, driver 32.0.21042.62. A HIP kernel (assembled by the driver's amd_comgr) runs 7 opcodes: - `v_cvt_i16_f16`, `v_cvt_u16_f16`, `v_cvt_norm_i16_f16` and `v_cvt_f16_i16`; - `v_cvt_f16_f32`, with `src0_sel` DWORD; - `v_add_f16` and `v_mul_f16` (VOP2). Each opcode runs with `dst_sel` WORD_0, WORD_1 and DWORD, and with `dst_unused` PAD, SEXT and PRESERVE. There are 40 input rows: - negative and positive results, ±0, ±inf and NaNs; - saturating values and random values. Each destination is preset to a random word. That gives 2,520 results. - **Translator:** the same words through a local copy of the `build_valu_cs` harness (a local test, not part of this PR). I ran it on `main` 63bcca4 and on the PR merged onto it, on the RTX 5080 (610.88) and on the same Radeon (picked with a local adapter-name filter in `hardware_adapter`). | Host | branch | matches the hardware bit for bit | NaN with a different payload | other differences | |---|---|---|---|---| | Radeon gfx1036 | main | 2,408 | 4 | 108 | | Radeon gfx1036 | #3124 | 2,520 | 0 | **0** | | RTX 5080 | main | 2,340 | 72 | 108 | | RTX 5080 | #3124 | 2,448 | 72 | **0** | - **main:** all 108 differences are WORD_0 + SEXT with bit 15 set. The hardware sign-extends there and main zero-fills. This holds for all 7 opcodes, the float ones too: `v_add_f16_sdwa(-1, -2)` is `0xffffc200` on the hardware and `0x0000c200` on main. - **This PR:** it matches the hardware. Here is the hardware table for the result `0xffff` (`v_cvt_i16_f16(-1.0)`), with the destination preset to `0xab8ed1ec`: | dst_sel | PAD | SEXT | PRESERVE | |---|---|---|---| | WORD_0 | `0x0000ffff` | `0xffffffff` | `0xab8effff` | | WORD_1 | `0xffff0000` | `0xffff0000` | `0xffffd1ec` | | DWORD | `0x0000ffff` | `0x0000ffff` | `0x0000ffff` | DWORD ignores PRESERVE for a 16-bit result, which is what `(6, _) => keep = false` assumes. I didn't test the reserved `dst_unused` value 3 on the hardware. No game data was used. AI use: Claude Code (Claude Opus 5.5) wrote the probe and the comparison and ran them on the GPUs above; the numbers come from those runs.
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/3124/head:github-bot/gh-3124'
git switch 'github-bot/gh-3124'

Merge

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