Skip to content

Shaders: fix 16-bit VALU SDWA placement/sext and inline float constants - #1285

Open
hamdog0203 wants to merge 2 commits into
Marvisak:mainfrom
hamdog0203:fix/valu16-sdwa-inline-const
Open

hamdog0203 wants to merge 2 commits into
Marvisak:mainfrom
hamdog0203:fix/valu16-sdwa-inline-const

Conversation

@hamdog0203

Copy link
Copy Markdown

Fixes three related 16-bit VALU / SDWA lowering bugs in crates/victory-shader/src/emit/lower.rs:

  1. Honor SDWA destination selection and UNUSED_SEXT in write_half()
    • Previously, write_half() rejected byte dst_sel (0..=3) and ignored dst_unused == 1 (UNUSED_SEXT), zero-extending even negative 16-bit results instead of sign-extending them across the upper bits.
    • Delegate SDWA dst_sel 0..=5 (BYTE_0..BYTE_3, WORD_0, WORD_1) and dst_unused 0..=2 (UNUSED_PAD, UNUSED_SEXT, UNUSED_PRESERVE) directly to place_bits(), matching 32-bit SDWA writes.
  2. Support SDWA byte source selections and sext on 16-bit integer sources in half_src_typed()
    • Previously, half_src() rejected byte src_sel (0..=3) and ignored sd.sext.
    • Route SDWA src_sel 0..=5 through select_bits(x, sel, sext) (applying sd.sext for 16-bit integer ops, masked to 16 bits as the operand width).
  3. Decode inline float constants (240..=248) using f32 bit patterns for 16-bit integer instructions (half_int_src())
    • On GFX10/RDNA2, 16-bit integer instructions (v_add_nc_u16, v_cvt_f16_i16, v_cmp_*_u16/i16, etc.) read the low/high halves of the 32-bit float constant rather than the f16 encoding (matching how packed_src(..., float) already distinguishes v_pk_*_f16 from v_pk_*_i16/u16).
    • Added half_int_src() for the 16-bit integer call sites while keeping half_src() for f16 instructions.

Verification

  • Added 3 end-to-end tests in crates/victory-shader/src/emit/valu_tests.rs:
    • sdwa_16bit_dst_word0_sext_and_byte_placement
    • sdwa_16bit_src_byte_select_and_sext
    • int16_inline_float_constants_use_f32_bits
  • Verified on Windows 11 with dxil.dll, NVIDIA GeForce RTX 4060, and WARP (cargo test --workspace --locked: 345 passed, 0 failed, 6 ignored; all 3 new tests fail on unpatched main ffe310e).
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features both pass cleanly.
  • cargo xtask policy passed (2 files ok, no game files/assets).

@geiyiliyabumo

Copy link
Copy Markdown
Contributor

Independent verification of \e34f8c3\ on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, \gfx1200) / Rust 1.98.1 MSVC:

  • Merge check vs \main\ (\ fe310e): clean
  • \cargo test -p victory-shader --lib valu_tests\ (real RX 9060 XT GPU execution): 16 passed, 0 failed (including the new SDWA placement/sext and 16-bit integer inline float constant tests)
  • \cargo clippy -p victory-shader --all-targets -- -D warnings\ (default and --all-features): clean
  • \cargo xtask policy: 154 files ok

Code audit looks clean: delegating SDWA \dst_sel\ (\�..=5) / \dst_unused\ (\�..=2) through \place_bits\ / \write_placed\ and gating SDWA \sext\ on !float\ fixes byte/word SDWA handling alongside the 16-bit integer inline constant fix (note: overlaps #1295 on \half_src, while this PR additionally covers SDWA).

AI-assisted; synthetic tests only, no game files.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed write_half / half_src_typed and verified the 16-bit SDWA WORD_0 + UNUSED_SEXT (v_cvt_i16_f16_sdwa, v_cvt_norm_i16_f16_sdwa, v_add_f16_sdwa) + WORD_0/WORD_1/DWORD PAD/SEXT/PRESERVE matrix on NVIDIA GeForce RTX 5090, D3D12 WARP, and dxil.dll (reference test suite in fuzeofficlal/VIctory@b6c916b).

One small guard worth tightening in Lower::write_half:

  • (6, _) currently accepts the reserved dst_unused == 3 encoding when dst_sel == 6 (DWORD), whereas place_bits (and (0..=5, 0..=2)) rejects dst_unused == 3. Narrowing that arm to (6, 0..=2) keeps reserved dst_unused == 3 consistently rejected across all dst_sel values.

@muhammed-aksoy

Copy link
Copy Markdown

I tested e34f8c3 together with #1291 (94dbc0a) on ffe310e. Windows cargo test --locked -p victory-shader -- --test-threads=1 passed 124 tests with zero failures and one ignored at local merge 6d40e1b, with the Windows SDK DXIL validator configured. Git reports a separate overlap with #1314 in emit/valu_tests.rs. AI-assisted.

@adamdotpy

Copy link
Copy Markdown

Reviewed at head e34f8c3 (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked:

1. The SDWA rework is correct. half_src is split into half_src (f16 ops) / half_int_src (16-bit int ops) over half_src_typed(inst, k, float). Source side: with SDWA present, src_sel 0..=3 selects a byte (select_bits shift-left/shift-right extract), 4..=5 a word, 6 the low word; sd.sext bit k sign-extends via AShr, only for integer ops (!float) — matching GFX10, where SDWA takes precedence over opsel. Without SDWA, opsel bit k picks the high word as before. Destination side: write_half now routes SDWA through place_bits (byte/word placement with dst_unused 0 = pad zeros, 1 = sign-extend, 2 = preserve old bits via write_placed reading the old register value) and accepts all (0..=5, 0..=2) combos plus (6, _) = low word; opsel bit 3 remains the non-SDWA path. I read both helpers and the bit math is right in each mode.

2. Inline float constants: same rule as #1295 (independently): 16-bit integer ops read the f32 bit pattern of constants 240..=248 (operand), f16 ops read f16 bits (f16_operand). Note for whoever merges: this overlaps #1295 — both change half_src and add a near-identical test (int16_inline_float_constants_use_f32_bits here vs half_int_ops_decode_inline_float_constants_as_f32_bits there); the second to merge will conflict on those lines. Behavior is identical, so either can land first.

3. Hardware evidence on this machine. The new GPU tests pass: sdwa_16bit_dst_word0_sext_and_byte_placement (dst WORD_0 with SEXT of 0xBC00 → 0xFFFF_BC00 preserved-high case 0x0000_3C00, byte1-preserve 0xAABB_CCDD → 0xAABB_42DD, byte1-sext 0xC900 → 0xFFFF_F600), sdwa_16bit_src_byte_select_and_sext (BYTE_1 select with/without sext through v_cvt_f16_i16 and v_cmp_lt_i16), and the inline-constant test (0xF983 low half of 248). Ran here: the sdwa-filtered tests 2 passed, full cargo test -p victory-shader --lib 123 passed / 0 failed / 1 ignored (21 s, GPU tests included).

4. Checks: cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings clean; cargo build --release -p victory builds; rustfmt --edition 2024 --check on both files shows only the 3 pre-existing lower.rs hunks main has (shifted) — no new fmt diffs.

5. Behavior notes, not defects: SDWA byte/word selects on 16-bit ops and SEXT were previously refused ("a byte select on a 16-bit operation" / restricted dst combos), so this strictly widens translation; and like #1295, int-op inline constants now use f32 bits where they previously used f16 — the intended fix.

Nothing found that would block this.

@hamdog0203

Copy link
Copy Markdown
Author

Thank you all for the thorough independent verification across RDNA4, RTX 5090, WARP, and dxil.dll (@geiyiliyabumo, @fuzeofficlal, @muhammed-aksoy, @adamdotpy)!

Good catch on (6, _) in Lower::write_half, @fuzeofficlal — pushed ac1a6e2 narrowing it to (6, 0..=2) so reserved dst_unused == 3 is rejected consistently across all dst_sel values, along with a regression check in sdwa_16bit_dst_word0_sext_and_byte_placement (victory-shader: 123 passed, 0 failed, 1 ignored; both clippy profiles and xtask policy clean).

@adamdotpy

Copy link
Copy Markdown

Verified the new head ac1a6e2 here (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). The reserved-combo tightening is right: write_half now accepts (dst_sel 6, dst_unused 0..=2) and rejects (6, 3) — per the SDWA encoding, dst_unused=3 is reserved with dst_sel=DWORD, and refusing it beats silently treating it as pad. The new dword_pad GPU case (0xC500 → 0x0000_FFFB) and the reserved-reject case both pass on the GPU. Full cargo test -p victory-shader --lib 123 passed / 0 failed / 1 ignored, clippy --all-targets --all-features --locked -D warnings clean, release build ok, and rustfmt --check now shows only main's 3 pre-existing lower.rs hunks. One merge note: the try_build_valu_cs split here also exists in #1261 — whichever lands second will conflict on that helper; trivial to resolve.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/emit/lower.rs, crates/victory-shader/src/emit/valu_tests.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

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.

6 participants