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

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I prepared efe7a63 on main 63bcca4. It keeps ac1a6e2 as first parent, so both original hamdog0203 commits and their author are intact. The conflict resolution keeps main's low16_src handling when an SDWA selection fits in the known half, plus the typed test-builder errors and all main tests.

The same three existing fixtures run on both revisions with the typed error assertion. On main, WORD_0 sign extension returns 0x0000bc00, the byte-source conversion is refused, and the 16-bit integer inline operand returns 0x3118. On efe7a63, all three pass, including 0xffffbc00 for the sign extension and 0xf983 for the integer operand. The reserved DWORD selection control also passes. The overlapping inline-constant case is the one in #1295.

App-local WARP SM 6.6 with DXIL validation: 38 VALU tests pass, 34 semantics tests pass (1 ignored), 33 lowerer tests pass, and the existing partial interpolation-half test passes. Strict shader Clippy in both configurations and the asset policy pass. lower.rs is 521,544 bytes. The offered commit merges cleanly with 63bcca4.

The branch andrewpozdnakov7-jpg:codex2/merge-1285-current-main is available to bring into this PR. If the conflict resolution is folded into another commit, please retain Git authorship or Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>.

@rminzao

rminzao commented Nov 26, 2026

Copy link
Copy Markdown

Tested ac1a6e2 merged onto current main (63bcca4). It no longer merges cleanly. The conflicts are small, and the resolution I tested is below. Synthetic tests only, no game files.

Results with the merge

Host 3 new tests
Intel UHD (driver 31.0.101.4146) + WARP 3 passed, no backend skipped
RTX 4050 Laptop + WARP 3 passed
main's lower.rs with the PR's tests 3 failed
  • Full cargo test -p victory-shader --lib on Intel: 308 tests, 294 passed, 11 failed, 3 ignored. The 11 failures are the same set that fails on main on this machine (BVH, FMad 1-ULP, and the wave-32 pipelines this 16-lane GPU refuses).
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings is clean.
  • cargo xtask policy is clean. lower.rs is 521,563 bytes, under the limit.

Conflict 1: half_src_typed. main added the low16 cache, which half_src reads when op_sel and SDWA select nothing. That cache holds only the low word, so it can't feed an SDWA byte or word select or an op_sel high half. I kept it only for unselected reads, for both the float and the integer path:

            _ => {
                // A cached low half holds only the unselected low word.
                let plain = v.sdwa.is_none() && v.opsel >> k & 1 == 0;
                match self.low16_src(inst, k).filter(|_| plain) {
                    Some(x) => x,
                    None if float => self.f16_operand(inst, &op)?,
                    None => self.operand(inst, &op)?,
                }
            }

Conflict 2: valu_tests.rs. try_build_valu_cs on main now returns crate::ShaderError, not String, so I kept main's helper. The dst_unused=3 test then needs let err = format!("{err:?}"); before its err.contains("SDWA destination"). The other two hunks are tests both sides added next to each other, so I kept both.

The change itself looks right to me:

  • WORD_0 + UNUSED_SEXT now sign-extends through place_bits, the same path the 32-bit SDWA writes use.
  • Reserved dst_unused 3 is refused for every dst_sel.
  • sext is applied only to integer sources.
  • Float inline constants read as the low 16 bits of their f32 pattern in 16-bit integer ops. As far as I recall, that matches what LLVM documents for GFX10 in SIInstrInfo::isInlineConstant (bug 46302); I didn't check the source again. Neither WARP nor NVIDIA can confirm what the console does here, so an RDNA2 run of int16_inline_float_constants_use_f32_bits would settle it.

One nit: the new comment // v_cmp_lt_i16_sdwa vcc, sext(v1), v2 src0_sel:BYTE_1 src1_sel:WORD_0; v_cndmask_b32 v0, 0, 1, vcc is 103 columns.

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.

8 participants