Shaders: 16-bit SDWA WORD_0 with UNUSED_SEXT zero-extends instead of sign-extending (fix on a fork) #823
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory#823
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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).github.com/AndyLatimer/VIctory@24011cf1d2Bug
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:When
dst_sel = WORD_0 (4)anddst_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 treatsUNUSED_SEXTidentically toUNUSED_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_0with-1.0f16(0xBC00) silently produces0x0000_FFFF(+65535) instead of0xFFFF_FFFF(-1).Separately, reserved
dst_unused = 3onWORD_0/WORD_1was also silently treated asUNUSED_PADinstead of being refused.Fix
(4, 1)(WORD_0+UNUSED_SEXT) inLower::write_halfby sign-extending the low 16 bits into bits[31:16]((x << 16) >>a 16), matching RDNA2 SDWA semantics and 32-bitplace_bits.WORD_0 + UNUSED_PAD,WORD_0 + UNUSED_PRESERVE,WORD_1 + {UNUSED_PAD, UNUSED_SEXT, UNUSED_PRESERVE}, andDWORDbehavior intact, and refuse reserveddst_unused = 3onWORD_0/WORD_1.crates/victory-shader/src/emit/valu_tests.rs(sdwa_16bit_dst_unused_sext_sign_extends_word0) coveringv_cvt_i16_f16_sdwa,v_cvt_norm_i16_f16_sdwa(src0_sel:WORD_1),v_add_f16_sdwa, and all otherdst_sel/dst_unusedmodes, plus a lowering unit test (sdwa_16bit_reserved_dst_unused_is_refused) incrates/victory-shader/src/emit/lower.rs.Verification
sdwa_16bit_dst_unused_sext_sign_extends_word0failed on both hardware GPU and WARP (hardware: v_cvt_i16_f16_sdwa(0xbc00, WORD_0, UNUSED_SEXT) = 0x0000ffff, want 0xffffffff).dxil.dllvalidation):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 --checkonvalu_tests.rsand all modified lines oflower.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.
@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_0encodes asf9 a6 00 7e 01 0c 04 00(llvm-mc, gfx1030).write_halftakes the(4 | 6, _)arm, so UNUSED_SEXT is zero-fill, the same as UNUSED_PAD. The 32-bit path inplace_bitsalready sign-extendsdst_unused == 1. A negative 16-bit result is masked to the low half, so-1stays0x0000FFFF. I did not run this on a GPU.github.com/Marvisak/VIctory@ffe310eb20/crates/victory-shader/src/emit/lower.rs (L4378-L4385)Reserved
dst_unused3 on WORD_0 hits that same arm, so it is accepted and zero-filled. llvm-mc rejects that spelling.Your
(4, 1)arm in24011cfis the same shiftplace_bitsuses, 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.
@Marvisak on GitHub · 2026-11-25 03:48 UTC · original
Automated triage for the maintainers. Confirmed on
main. Incrates/victory-shader/src/emit/lower.rs,Lower::write_halfmatches(4 | 6, _) => keep = false. Sodst_selWORD_0 withdst_unusedUNUSED_SEXT (1) zero-fills bits 31:16 instead of sign-extending bit 15, and reserveddst_unused3 is accepted the same way. The 32-bit path,place_bits, already sign-extends forunused == 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
github-bot referenced this issue2026-11-26 11:07:57 +00:00
@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. Onmain'swrite_half, the new hardware test fails on an RTX 4060 Laptop (0x0000ffff, want0xffffffff), and so does the lowering test for reserveddst_unused3, 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 withmain.AI-assisted (Claude Code); the runs are from my machine.
@Lyrnc on GitHub · 2026-11-26 18:52 UTC · original
Hardware results on gfx1036 (RDNA2) for 16-bit SDWA destinations.
WORD_0withUNUSED_SEXTsign-extends bit 15, as reported here. #3124 matches the hardware on every case below, on an AMD and an NVIDIA host.Setup.
v_mov_b32 vD, <old>, thenv_cvt_i16_f16_sdwa vD, v4orv_add_f16_sdwa vD, v4, v5, withsrc*_sel:WORD_0and eachdst_sel/dst_unusedpair.±1,-2.5,-32768,300.5,-0, NaN,±65504. The high halves are random, and the old value of D is0xa5a55a5aor random.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
ris the 16-bit value;oldis D before the instruction:dst_sel,dst_unusedv_cvt_i16_f16(-1.0)v_add_f16 = 0xbe00WORD_0,UNUSED_PAD0x0000_rrrr0x0000ffff0x0000be00WORD_0,UNUSED_SEXTr0xffffffff0xffffbe00WORD_0,UNUSED_PRESERVEold[31:16] : r0xa5a5ffff0xa5a5be00WORD_1,PADorSEXTr : 0x00000xffff00000xbe000000WORD_1,UNUSED_PRESERVEr : old[15:0]0xffff5a5aDWORD,PADorSEXT0x0000_rrrr(no extension)0x0000ffff0x0000be00BYTE_0,UNUSED_SEXTr[7:0]sign-extended from bit 70xffffffff;300 → 0x0000002c0xc080 → 0xffffff80BYTE_1,UNUSED_PRESERVEold[31:16] : r[7:0] : old[7:0]0xa5a5ff5aThe f16 result is sign-extended as raw bits too (
0xbe00 → 0xffffbe00).SEXTdoes nothing withWORD_1orDWORD.Translator, rows that differ (of 24):
main63bcca4, Radeon / RTX 5080ac958c9), Radeon / RTX 5080v_cvt_i16_f16,WORD_0+SEXTv_add_f16,WORD_0+SEXTWORD_0,WORD_1andDWORDpairs aboveBYTE_0/BYTE_1destinationsmainand #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 as0x7fffon 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.