Skip to content

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

Open
KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:fix/sdwa-word0-sext-823
Open

KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:fix/sdwa-word0-sext-823

Conversation

@KainanP

@KainanP KainanP commented Nov 26, 2026

Copy link
Copy Markdown

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

…visak#823)

`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 <manuelperezardanuy@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Owner

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

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants