Shaders: an SDWA WORD_0 result with UNUSED_SEXT is sign-extended (#823) #3124
No reviewers
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!3124
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3124"
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?
@KainanP on GitHub · 2026-11-26 09:08 UTC · original
KainanP:fix/sdwa-word0-sext-823→main· +178 −16 · 2 files · 1 commits · openFixes #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, sodst_selWORD_0 withdst_unusedUNUSED_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_SEXTof -1.0 gave0x0000ffff, not0xffffffff. The 32-bit path,place_bits, already sign-extends forunused == 1. Now:(4, 1)sign-extends the low word ((x << 16) >>a 16), the same shiftplace_bitsuses.dst_unused3 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 currentmain. He is credited as co-author. The only conflict was the two test files, wheremainhad 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 withmainsince 2026-11-25. This PR is only the destination fix.Tests
emit::lower::tests::sdwa_16bit_reserved_dst_unused_is_refusedneeds no GPU (lowering only). Fails onmain, because the reserved encoding is accepted. Passes with the fix.emit::valu_tests::sdwa_16bit_dst_unused_sext_sign_extends_word0(hardware + WARP throughrun_cases) checksv_cvt_i16_f16_sdwa,v_cvt_norm_i16_f16_sdwa(src0 WORD_1) andv_add_f16_sdwawith UNUSED_SEXT, plus every other dst_sel/dst_unused mode.cargo test -p victory-shader --lib --lockedon Windows 11 withmain63bcca4:mainmain'swrite_halfheap_direct_texture_reads_mark_their_heap_index, #2571)gathers_at_a_level_match_the_hardware(#2317)hardware: v_cvt_i16_f16_sdwa(0xbc00, WORD_0, UNUSED_SEXT) = 0x0000ffff, want 0xffffffffVICTORY_WARP=1)The new tests pass on all three adapters. No test that passes on
mainfails with this PR.cargo clippy -p victory-shader --all-targets [--all-features] --locked -- -D warnings: clean.rustfmt --checkreports 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
@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 thatplace_bitsalready 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 matchesplace_bits. Reserveddst_unused3 is now refused for WORD_0 and WORD_1. I checked the opcode encodings in the tests and the newvop1_sdwa/vop2_sdwahelpers, and the expected values follow the RDNA2 ISA's SDWA definitions. Optional follow-up: DWORD with reserveddst_unused3 is still read as PAD and could be refused the same way.CI:
checkpassing.Generated by Claude Code
@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_refusedandsdwa_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_0results whenUNUSED_SEXTis set and refuses reserveddst_unusedencodings.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@fuzeofficlal on GitHub · 2026-11-26 10:03 UTC · original · reviewed
In
Lower::write_half,(x << 16) >>a 16for(4, 1)(WORD_0+UNUSED_SEXT) discards any stale upper bits and sign-extends bit 15, matchingplace_bits. The remainingWORD_0/WORD_1modes (PAD/PRESERVE/reserveddst_unused = 3) andvalu_tests.rsassertions (with0xABCD_1234pre-seed distinguishing sext/pad/preserve acrossv_cvt_i16_f16_sdwa,v_cvt_norm_i16_f16_sdwa, andv_add_f16_sdwa) are sound, andemit/lower.rsis521,941bytes (within the 512 KiB limit).One question:
(4, 0) | (6, _)still accepts reserveddst_unused = 3whendst_sel == 6(DWORD), matchingwrite/sdwa_dst(dst_sel >= 6), whereasWORD_0andWORD_1now reject3as tested insdwa_16bit_reserved_dst_unused_is_refused. Could you clarify whetherDWORDintentionally ignoresdst_unused = 3(sinceDWORDwrites 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): cleanemit::lower::tests: 34 passed, 0 failedsdwa_16bit_ -- --test-threads=1(RTX 5090 andVICTORY_WARP=1): 2 passed, 0 failed on both@Lyrnc on GitHub · 2026-11-26 17:21 UTC · original
Checked
ac958c9on RDNA2 hardware, for every SDWAdst_sel×dst_unusedcombination 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_f16andv_cvt_f16_i16;v_cvt_f16_f32, withsrc0_selDWORD;v_add_f16andv_mul_f16(VOP2).Each opcode runs with
dst_selWORD_0, WORD_1 and DWORD, and withdst_unusedPAD, SEXT and PRESERVE. There are 40 input rows: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_csharness (a local test, not part of this PR). I ran it onmain63bcca4and on the PR merged onto it, on the RTX 5080 (610.88) and on the same Radeon (picked with a local adapter-name filter inhardware_adapter).v_add_f16_sdwa(-1, -2)is0xffffc200on the hardware and0x0000c200on main.0xffff(v_cvt_i16_f16(-1.0)), with the destination preset to0xab8ed1ec:0x0000ffff0xffffffff0xab8effff0xffff00000xffff00000xffffd1ec0x0000ffff0x0000ffff0x0000ffffDWORD ignores PRESERVE for a 16-bit result, which is what
(6, _) => keep = falseassumes. I didn't test the reserveddst_unusedvalue 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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.