Shaders: 16-bit SDWA WORD_0 with UNUSED_SEXT zero-extends instead of sign-extending (fix on a fork) #823

Open
opened 2026-11-26 04:14:01 +00:00 by github-bot · 4 comments
Owner

@⁠AndyLatimer on GitHub · 2026-11-24 11:39 UTC · original

Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a fork branch off main (ffe310e).

Bug

In crates/victory-shader/src/emit/lower.rs (Lower::write_half), SDWA destinations on 16-bit VALU instructions (v_cvt_i16_f16, v_cvt_norm_i16_f16, v_add_f16, etc.) match (sd.dst_sel, sd.dst_unused) as:

match (sd.dst_sel, sd.dst_unused) {
    (4, 2) => {}
    (5, 2) => high = true,
    (4 | 6, _) => keep = false,
    (5, _) => (high, keep) = (true, false),
    _ => return Err("an SDWA destination of a 16-bit operation"),
}

When dst_sel = WORD_0 (4) and dst_unused = UNUSED_SEXT (1) — the pattern AMDGPU's SDWA peephole pass emits to fold a 16-to-32-bit sign extension into a 16-bit instruction — the (4 | 6, _) arm treats UNUSED_SEXT identically to UNUSED_PAD, zero-extending bits [31:16] instead of sign-extending bit 15. For example, v_cvt_i16_f16_sdwa v0, v1 dst_sel:WORD_0 dst_unused:UNUSED_SEXT src0_sel:WORD_0 with -1.0f16 (0xBC00) silently produces 0x0000_FFFF (+65535) instead of 0xFFFF_FFFF (-1).

Separately, reserved dst_unused = 3 on WORD_0/WORD_1 was also silently treated as UNUSED_PAD instead of being refused.

Fix

  • Handle (4, 1) (WORD_0 + UNUSED_SEXT) in Lower::write_half by sign-extending the low 16 bits into bits [31:16] ((x << 16) >>a 16), matching RDNA2 SDWA semantics and 32-bit place_bits.
  • Keep WORD_0 + UNUSED_PAD, WORD_0 + UNUSED_PRESERVE, WORD_1 + {UNUSED_PAD, UNUSED_SEXT, UNUSED_PRESERVE}, and DWORD behavior intact, and refuse reserved dst_unused = 3 on WORD_0/WORD_1.
  • Added end-to-end synthetic GPU + WARP regression tests in crates/victory-shader/src/emit/valu_tests.rs (sdwa_16bit_dst_unused_sext_sign_extends_word0) covering v_cvt_i16_f16_sdwa, v_cvt_norm_i16_f16_sdwa (src0_sel:WORD_1), v_add_f16_sdwa, and all other dst_sel/dst_unused modes, plus a lowering unit test (sdwa_16bit_reserved_dst_unused_is_refused) in crates/victory-shader/src/emit/lower.rs.

Verification

  • Before fix: sdwa_16bit_dst_unused_sext_sign_extends_word0 failed on both hardware GPU and WARP (hardware: v_cvt_i16_f16_sdwa(0xbc00, WORD_0, UNUSED_SEXT) = 0x0000ffff, want 0xffffffff).
  • After fix (Windows 11, native SM 6.6 hardware GPU + WARP + dxil.dll validation):
    • cargo test -p victory-shader: 122 passed, 0 failed, 1 ignored.
    • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
    • cargo check -p victory --features diagnostics: clean.
    • rustfmt --edition 2024 --check on valu_tests.rs and all modified lines of lower.rs: clean.
    • cargo xtask policy: 154 files ok.

Disclosure

Synthetic tests only; no game data or console SDK assets were used. Assisted by Google Antigravity.

**@⁠AndyLatimer** on GitHub · 2026-11-24 11:39 UTC · [original](https://github.com/Marvisak/VIctory/issues/823) Pull requests are currently restricted to collaborators (#717), so I am sharing this bug report and verified fix from a fork branch off `main` (`ffe310e`). - **Branch:** https://github.com/AndyLatimer/VIctory/tree/fix/shader-sdwa-word0-sext - **Commit:** https://github.com/AndyLatimer/VIctory/commit/24011cf1d2c4e002920df6362a80a35f7133e685 ### Bug In `crates/victory-shader/src/emit/lower.rs` (`Lower::write_half`), SDWA destinations on 16-bit VALU instructions (`v_cvt_i16_f16`, `v_cvt_norm_i16_f16`, `v_add_f16`, etc.) match `(sd.dst_sel, sd.dst_unused)` as: ```rust match (sd.dst_sel, sd.dst_unused) { (4, 2) => {} (5, 2) => high = true, (4 | 6, _) => keep = false, (5, _) => (high, keep) = (true, false), _ => return Err("an SDWA destination of a 16-bit operation"), } ``` When `dst_sel = WORD_0 (4)` and `dst_unused = UNUSED_SEXT (1)` — the pattern AMDGPU's SDWA peephole pass emits to fold a 16-to-32-bit sign extension into a 16-bit instruction — the `(4 | 6, _)` arm treats `UNUSED_SEXT` identically to `UNUSED_PAD`, zero-extending bits `[31:16]` instead of sign-extending bit 15. For example, `v_cvt_i16_f16_sdwa v0, v1 dst_sel:WORD_0 dst_unused:UNUSED_SEXT src0_sel:WORD_0` with `-1.0f16` (`0xBC00`) silently produces `0x0000_FFFF` (`+65535`) instead of `0xFFFF_FFFF` (`-1`). Separately, reserved `dst_unused = 3` on `WORD_0`/`WORD_1` was also silently treated as `UNUSED_PAD` instead of being refused. ### Fix - Handle `(4, 1)` (`WORD_0` + `UNUSED_SEXT`) in `Lower::write_half` by sign-extending the low 16 bits into bits `[31:16]` (`(x << 16) >>a 16`), matching RDNA2 SDWA semantics and 32-bit `place_bits`. - Keep `WORD_0 + UNUSED_PAD`, `WORD_0 + UNUSED_PRESERVE`, `WORD_1 + {UNUSED_PAD, UNUSED_SEXT, UNUSED_PRESERVE}`, and `DWORD` behavior intact, and refuse reserved `dst_unused = 3` on `WORD_0`/`WORD_1`. - Added end-to-end synthetic GPU + WARP regression tests in `crates/victory-shader/src/emit/valu_tests.rs` (`sdwa_16bit_dst_unused_sext_sign_extends_word0`) covering `v_cvt_i16_f16_sdwa`, `v_cvt_norm_i16_f16_sdwa` (`src0_sel:WORD_1`), `v_add_f16_sdwa`, and all other `dst_sel`/`dst_unused` modes, plus a lowering unit test (`sdwa_16bit_reserved_dst_unused_is_refused`) in `crates/victory-shader/src/emit/lower.rs`. ### Verification - **Before fix:** `sdwa_16bit_dst_unused_sext_sign_extends_word0` failed on both hardware GPU and WARP (`hardware: v_cvt_i16_f16_sdwa(0xbc00, WORD_0, UNUSED_SEXT) = 0x0000ffff, want 0xffffffff`). - **After fix (Windows 11, native SM 6.6 hardware GPU + WARP + `dxil.dll` validation):** - `cargo test -p victory-shader`: 122 passed, 0 failed, 1 ignored. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo check -p victory --features diagnostics`: clean. - `rustfmt --edition 2024 --check` on `valu_tests.rs` and all modified lines of `lower.rs`: clean. - `cargo xtask policy`: 154 files ok. ### Disclosure Synthetic tests only; no game data or console SDK assets were used. Assisted by Google Antigravity.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:01 +00:00
Author
Owner

@⁠CaravelaLabs on GitHub · 2026-11-24 15:38 UTC · original

Thanks @⁠AndyLatimer — this is real on main ffe310e.

v_cvt_i16_f16_sdwa v0, v1 dst_sel:WORD_0 dst_unused:UNUSED_SEXT src0_sel:WORD_0 encodes as f9 a6 00 7e 01 0c 04 00 (llvm-mc, gfx1030). write_half takes the (4 | 6, _) arm, so UNUSED_SEXT is zero-fill, the same as UNUSED_PAD. The 32-bit path in place_bits already sign-extends dst_unused == 1. A negative 16-bit result is masked to the low half, so -1 stays 0x0000FFFF. I did not run this on a GPU.

github.com/Marvisak/VIctory@ffe310eb20/crates/victory-shader/src/emit/lower.rs (L4378-L4385)

Reserved dst_unused 3 on WORD_0 hits that same arm, so it is accepted and zero-filled. llvm-mc rejects that spelling.

Your (4, 1) arm in 24011cf is the same shift place_bits uses, and reserved 3 falls through to the error. I did not compile that commit. Pull requests are open again, so that branch can come in as a PR whenever you like.

AI-assisted; checked on this machine.

**@⁠CaravelaLabs** on GitHub · 2026-11-24 15:38 UTC · [original](https://github.com/Marvisak/VIctory/issues/823#issuecomment-7136354901) Thanks @⁠AndyLatimer — this is real on main `ffe310e`. `v_cvt_i16_f16_sdwa v0, v1 dst_sel:WORD_0 dst_unused:UNUSED_SEXT src0_sel:WORD_0` encodes as `f9 a6 00 7e 01 0c 04 00` (llvm-mc, gfx1030). `write_half` takes the `(4 | 6, _)` arm, so UNUSED_SEXT is zero-fill, the same as UNUSED_PAD. The 32-bit path in `place_bits` already sign-extends `dst_unused == 1`. A negative 16-bit result is masked to the low half, so `-1` stays `0x0000FFFF`. I did not run this on a GPU. https://github.com/Marvisak/VIctory/blob/ffe310eb2098e52169f4c44db5ffdabf43a72622/crates/victory-shader/src/emit/lower.rs#L4378-L4385 Reserved `dst_unused` 3 on WORD_0 hits that same arm, so it is accepted and zero-filled. llvm-mc rejects that spelling. Your `(4, 1)` arm in `24011cf` is the same shift `place_bits` uses, and reserved 3 falls through to the error. I did not compile that commit. Pull requests are open again, so that branch can come in as a PR whenever you like. AI-assisted; checked on this machine.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:48 UTC · original

Automated triage for the maintainers. Confirmed on main. In crates/victory-shader/src/emit/lower.rs, Lower::write_half matches (4 | 6, _) => keep = false. So dst_sel WORD_0 with dst_unused UNUSED_SEXT (1) zero-fills bits 31:16 instead of sign-extending bit 15, and reserved dst_unused 3 is accepted the same way. The 32-bit path, place_bits, already sign-extends for unused == 1. Pull requests are open to everyone again (see #717), so please open the branch as a pull request and link this issue. Open PR #1285 (16-bit SDWA placement/sext) touches the same code, so compare it with your branch first to avoid a duplicate.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:48 UTC · [original](https://github.com/Marvisak/VIctory/issues/823#issuecomment-7152261265) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-24T15:38:02Z at=2026-11-25T03:48:34Z --> Automated triage for the maintainers. Confirmed on `main`. In `crates/victory-shader/src/emit/lower.rs`, `Lower::write_half` matches `(4 | 6, _) => keep = false`. So `dst_sel` WORD_0 with `dst_unused` UNUSED_SEXT (1) zero-fills bits 31:16 instead of sign-extending bit 15, and reserved `dst_unused` 3 is accepted the same way. The 32-bit path, `place_bits`, already sign-extends for `unused == 1`. Pull requests are open to everyone again (see #717), so please open the branch as a pull request and link this issue. Open PR #1285 (16-bit SDWA placement/sext) touches the same code, so compare it with your branch first to avoid a duplicate. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠KainanP on GitHub · 2026-11-26 09:08 UTC · original

Opened #3124 with this fix: @⁠AndyLatimer's branch (24011cf) rebased onto current main, with him credited as co-author. On main's write_half, the new hardware test fails on an RTX 4060 Laptop (0x0000ffff, want 0xffffffff), and so does the lowering test for reserved dst_unused 3, which needs no GPU. With the fix both pass on RTX 4060, Intel UHD and WARP, with no other changes in the suite. #1285 touches the same match but carries more and conflicts with main.

AI-assisted (Claude Code); the runs are from my machine.

**@⁠KainanP** on GitHub · 2026-11-26 09:08 UTC · [original](https://github.com/Marvisak/VIctory/issues/823#issuecomment-7187486820) Opened #3124 with this fix: @⁠AndyLatimer's branch (24011cf) rebased onto current `main`, with him credited as co-author. On `main`'s `write_half`, the new hardware test fails on an RTX 4060 Laptop (`0x0000ffff`, want `0xffffffff`), and so does the lowering test for reserved `dst_unused` 3, which needs no GPU. With the fix both pass on RTX 4060, Intel UHD and WARP, with no other changes in the suite. #1285 touches the same match but carries more and conflicts with `main`. _AI-assisted (Claude Code); the runs are from my machine._
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-26 18:52 UTC · original

Hardware results on gfx1036 (RDNA2) for 16-bit SDWA destinations. WORD_0 with UNUSED_SEXT sign-extends bit 15, as reported here. #3124 matches the hardware on every case below, on an AMD and an NVIDIA host.

Setup.

  • Hardware: the Radeon iGPU of a Ryzen 7 9800X3D, gfx1036, AMD driver 32.0.21042.62, Windows 11. A HIP kernel (assembled by the driver's amd_comgr) runs, per lane: v_mov_b32 vD, <old>, then v_cvt_i16_f16_sdwa vD, v4 or v_add_f16_sdwa vD, v4, v5, with src*_sel:WORD_0 and each dst_sel / dst_unused pair.
  • Inputs: 24 rows. f16 values give 16-bit results with bit 15 set and clear: ±1, -2.5, -32768, 300.5, -0, NaN, ±65504. The high halves are random, and the old value of D is 0xa5a55a5a or random.
  • Translator: the same words through build_valu_cs, on that Radeon and on an RTX 5080 (driver 610.88), one adapter at a time with a local switch.

PC RDNA2 silicon, not the console's GPU. No game data.

gfx1036. The result r is the 16-bit value; old is D before the instruction:

dst_sel, dst_unused D v_cvt_i16_f16(-1.0) v_add_f16 = 0xbe00
WORD_0, UNUSED_PAD 0x0000_rrrr 0x0000ffff 0x0000be00
WORD_0, UNUSED_SEXT bits 31:16 = bit 15 of r 0xffffffff 0xffffbe00
WORD_0, UNUSED_PRESERVE old[31:16] : r 0xa5a5ffff 0xa5a5be00
WORD_1, PAD or SEXT r : 0x0000 0xffff0000 0xbe000000
WORD_1, UNUSED_PRESERVE r : old[15:0] 0xffff5a5a
DWORD, PAD or SEXT 0x0000_rrrr (no extension) 0x0000ffff 0x0000be00
BYTE_0, UNUSED_SEXT r[7:0] sign-extended from bit 7 0xffffffff; 300 → 0x0000002c 0xc080 → 0xffffff80
BYTE_1, UNUSED_PRESERVE old[31:16] : r[7:0] : old[7:0] 0xa5a5ff5a

The f16 result is sign-extended as raw bits too (0xbe00 → 0xffffbe00). SEXT does nothing with WORD_1 or DWORD.

Translator, rows that differ (of 24):

case main 63bcca4, Radeon / RTX 5080 #3124 (ac958c9), Radeon / RTX 5080
v_cvt_i16_f16, WORD_0 + SEXT 10 / 10 0 / 0
v_add_f16, WORD_0 + SEXT 13 / 13 0 / 0
the other WORD_0, WORD_1 and DWORD pairs above 0 / 0 0 / 0
BYTE_0 / BYTE_1 destinations refused refused

main and #3124 refuse the byte destinations of a 16-bit operation ("an SDWA destination of a 16-bit operation"), so they never give a wrong value. The two byte rows above say what they would have to do. NaN payloads aside: the 5080 writes f16 NaNs as 0x7fff on every tree. #3124 ran on a local merge with #1327 and #2915, which touch other code.

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

**@⁠Lyrnc** on GitHub · 2026-11-26 18:52 UTC · [original](https://github.com/Marvisak/VIctory/issues/823#issuecomment-7200949379) Hardware results on gfx1036 (RDNA2) for 16-bit SDWA destinations. `WORD_0` with `UNUSED_SEXT` sign-extends bit 15, as reported here. #3124 matches the hardware on every case below, on an AMD and an NVIDIA host. **Setup.** - **Hardware:** the Radeon iGPU of a Ryzen 7 9800X3D, **gfx1036**, AMD driver 32.0.21042.62, Windows 11. A HIP kernel (assembled by the driver's amd_comgr) runs, per lane: `v_mov_b32 vD, <old>`, then `v_cvt_i16_f16_sdwa vD, v4` or `v_add_f16_sdwa vD, v4, v5`, with `src*_sel:WORD_0` and each `dst_sel` / `dst_unused` pair. - **Inputs:** 24 rows. f16 values give 16-bit results with bit 15 set and clear: `±1`, `-2.5`, `-32768`, `300.5`, `-0`, NaN, `±65504`. The high halves are random, and the old value of D is `0xa5a55a5a` or random. - **Translator:** the same words through `build_valu_cs`, on that Radeon and on an RTX 5080 (driver 610.88), one adapter at a time with a local switch. PC RDNA2 silicon, not the console's GPU. No game data. **gfx1036.** The result `r` is the 16-bit value; `old` is D before the instruction: | `dst_sel`, `dst_unused` | D | `v_cvt_i16_f16(-1.0)` | `v_add_f16 = 0xbe00` | |---|---|---|---| | `WORD_0`, `UNUSED_PAD` | `0x0000_rrrr` | `0x0000ffff` | `0x0000be00` | | `WORD_0`, `UNUSED_SEXT` | **bits 31:16 = bit 15 of `r`** | `0xffffffff` | `0xffffbe00` | | `WORD_0`, `UNUSED_PRESERVE` | `old[31:16] : r` | `0xa5a5ffff` | `0xa5a5be00` | | `WORD_1`, `PAD` or `SEXT` | `r : 0x0000` | `0xffff0000` | `0xbe000000` | | `WORD_1`, `UNUSED_PRESERVE` | `r : old[15:0]` | `0xffff5a5a` | | | `DWORD`, `PAD` or `SEXT` | `0x0000_rrrr` (no extension) | `0x0000ffff` | `0x0000be00` | | `BYTE_0`, `UNUSED_SEXT` | `r[7:0]` sign-extended from bit 7 | `0xffffffff`; `300 → 0x0000002c` | `0xc080 → 0xffffff80` | | `BYTE_1`, `UNUSED_PRESERVE` | `old[31:16] : r[7:0] : old[7:0]` | `0xa5a5ff5a` | | The f16 result is sign-extended as raw bits too (`0xbe00 → 0xffffbe00`). `SEXT` does nothing with `WORD_1` or `DWORD`. **Translator, rows that differ (of 24):** | case | `main` `63bcca4`, Radeon / RTX 5080 | #3124 (`ac958c9`), Radeon / RTX 5080 | |---|---|---| | `v_cvt_i16_f16`, `WORD_0` + `SEXT` | **10 / 10** | 0 / 0 | | `v_add_f16`, `WORD_0` + `SEXT` | **13 / 13** | 0 / 0 | | the other `WORD_0`, `WORD_1` and `DWORD` pairs above | 0 / 0 | 0 / 0 | | `BYTE_0` / `BYTE_1` destinations | refused | refused | `main` and #3124 refuse the byte destinations of a 16-bit operation ("an SDWA destination of a 16-bit operation"), so they never give a wrong value. The two byte rows above say what they would have to do. NaN payloads aside: the 5080 writes f16 NaNs as `0x7fff` on every tree. #3124 ran on a local merge with #1327 and #2915, which touch other code. AI use: Claude Code (Claude Opus 5.5) wrote the probe and ran it on the GPUs above; the numbers come from those runs.
This discussion has been locked. Commenting is limited to contributors.
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#823
No description provided.