Skip to content

Direct DXIL: saturate v_pk_*_i16 CLAMP with masked select (NVIDIA) - #2855

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-vpk-i16-clamp-masked-select
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-vpk-i16-clamp-masked-select

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

Direct DXIL lowering for packed signed 16-bit add/sub/mad with CLAMP=1 (v_pk_add_i16, v_pk_sub_i16, v_pk_mad_i16 in Lw::valu_pk, crates/xcl/src/gpu/dxil/lower_wide.rs) previously saturated each half via IMax(IMin(t, 0x7FFF), -32768) & 0xFFFF before OR-ing the low and high halves into the destination register. On NVIDIA Ampere drivers (RTX 3070 driver 616.56 and RTX 3060 driver 610.88), the driver drops the trailing & 0xFFFF mask for negative clamped low halves, leaving sign-extended 0xFFFF_xxxx to overwrite bits [31:16] of the packed destination (for example, [-1, 1] - [1, 1] returned 0xFFFF_FFFE instead of 0x0000_FFFE).

This is the packed-operation sibling to #2398 (which touches non-packed valu16 ops on disjoint lines and merges cleanly alongside this PR). Auditing all other imin/imax/& 0xFFFF sites across crates/xcl/src/gpu/dxil/ confirmed Lw::valu_pk was the only remaining IMax(IMin(t, 0x7FFF), -32768) & 0xFFFF site; v_cvt_i16_f16 (lower_wide.rs) and v_cvt_pk_i16_i32 (lower_valu.rs) use IMin(IMax(...)) and pass as controls on Ampere.

What is in it

  • Replace IMax(IMin(t, 0x7FFF), -32768) & 0xFFFF in "v_pk_add_i16" | "v_pk_sub_i16" | "v_pk_mad_i16" if clamp (crates/xcl/src/gpu/dxil/lower_wide.rs) with signed 32-bit comparisons (Pred::SGt against 0x7FFF, Pred::SLt against 0xFFFF_8000), t & 0xFFFF, and a select chain yielding 16-bit patterns (0x7FFF, 0x8000, or low), matching the masked-select form used in DXIL: saturate 16-bit integer valu16 ops when CLAMP=1 #2398.
  • Add i16_clamp_mask_tests::v_pk_i16_clamp_uses_masked_select_without_imin_imax: validates and disassembles Direct DXIL for V_PK_ADD_I16, V_PK_SUB_I16, and V_PK_MAD_I16 with CLAMP=1, asserting icmp sgt i32 / icmp slt i32 / select i1 and no IMax, IMin, or Int64Ops.
  • Add i16_clamp_mask_tests::gpu_i16_clamp_negative_low_half_keeps_high_half: runs V_PK_ADD_I16, V_PK_SUB_I16, and V_PK_MAD_I16 with CLAMP=1 (distinct source registers across saturation edges, negative in-range low halves, and 32-bit intermediate cancellation) alongside V_CVT_PK_I16_I32 and V_CVT_I16_F16 controls on the GPU.
  • Attribution: based on @upipibi's root-cause analysis and fix (upipibi/xcl@6fe2b3e7, RTX 3070 616.56 reproduction) and @Smokeyqdqd's independent RTX 3060 610.88 reproduction.

Tested

  • Hardware & toolchain: Windows 11 Home (build 26200), Intel Core i5-11400F, 32 GB RAM, NVIDIA GeForce RTX 4060 8 GB (Ada, 32-lane waves, driver 621.14), Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5.
  • Baseline (e120925e clamp with i16_clamp_mask_tests added):
    • cargo test -p xcl --lib -- v_pk_i16_clamp_uses_masked_select_without_imin_imax --nocapture --test-threads=1: 0 passed, 1 failed (caught IMax emission on unmodified e120925e).
    • cargo test -p xcl --lib -- gpu_i16_clamp_negative_low_half_keeps_high_half --nocapture --test-threads=1 (3x Debug RTX 4060) and --release (3x Release RTX 4060): 1 passed, 0 failed in all 6 runs (0 skipped). RTX 4060 Ada driver 621.14 keeps the & 0xFFFF mask on IMax(IMin(...)), consistent with DXIL: saturate 16-bit integer valu16 ops when CLAMP=1 #2398 where Ada passed while Ampere drivers failed 3/3 (RTX 3070 616.56 and RTX 3060 610.88, 33 mismatched lanes).
  • Fixed (651fc050):
    • cargo clippy --all-targets -- -A clippy::approx_constant: exit 0, 0 new warnings in crates/xcl/src/gpu/dxil/lower_wide.rs.
    • Debug WARP (XCL_WARP=1): cargo test -p xcl --lib -- i16_clamp_mask_tests --nocapture --test-threads=1: 2 passed, 0 failed, 0 ignored in 3/3 runs (0 skipped); cargo test -p xcl --lib -- gpu::dxil:: --test-threads=4: 186 passed, 0 failed, 3 ignored (1 SKIPPED: dispatch_chain_native_wave64_builds_whole: no wave64).
    • Release RTX 4060: cargo test --release -p xcl --lib -- i16_clamp_mask_tests --nocapture --test-threads=1: 2 passed, 0 failed, 0 ignored in 3/3 runs (0 skipped); cargo test --release -p xcl --lib -- gpu::dxil:: --test-threads=4: 186 passed, 0 failed, 3 ignored (1 SKIPPED: dispatch_chain_native_wave64_builds_whole: no wave64).
    • cargo build --release --workspace && cargo test --release --workspace --no-fail-fast -- --test-threads=4: 1444 passed, 0 failed, 47 ignored across 38 top-level test targets (29 SKIPPED notices: 21 wave64, 3 per-lane refusals, 3 D3D12 debug layer, 1 heap-window fallback, 1 Windows driver-cache toggle).
    • Mergeability: clean git merge-tree --write-tree (exit 0) against both main (8b957bec) and DXIL: saturate 16-bit integer valu16 ops when CLAMP=1 #2398 (e9d7aefc).

Regression run
Paired quiet 60 s release runs (scripts/smoke.ps1 -Seconds 60, XCL_NETWORK=0, RTX 4060 8 GB, driver 621.14) on exact parent e120925e and PR head 651fc050:

Exact parent on main (e120925e, ancestor of 8b957bec):

xcl-smoke v1
commit: e120925e3abc9956a8b9bae631969d32e8ccd43c
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1629 (PresentX 1629)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14025 MB

PR (651fc050):

xcl-smoke v1
commit: 651fc0502da4efd6bbcefa9faa9aed3dafb22d40
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1590 (PresentX 1590)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14015 MB

Not tested

  • NVIDIA Ampere hardware locally (the V_PK_ADD_I16 / V_PK_SUB_I16 failure and masked-select fix were independently verified 3/3 on RTX 3070 616.56 by @upipibi and RTX 3060 610.88 by @Smokeyqdqd; V_PK_MAD_I16 shares the same clamp block and was verified here on WARP and RTX 4060 Ada).
  • AMD and Intel GPUs, native wave64 GPU tests, and D3D12 debug-layer tests (d3d12SDKLayers.dll not installed).

AI use

  • Google Antigravity (Gemini 4 Neon) audited signed-16-bit clamp sites in crates/xcl/src/gpu/dxil/, applied the masked-select fix and i16_clamp_mask_tests (extending @upipibi's 6fe2b3e7 test with V_PK_MAD_I16 and an IR contract test), ran all local builds/tests/smokes, and drafted this PR description.

Packed signed 16-bit add/sub/mad with `CLAMP=1` in `Lw::valu_pk` previously
lowered saturation as `IMax(IMin(t, 0x7FFF), -32768) & 0xFFFF` before OR-ing
the low and high halves. On NVIDIA Ampere drivers (RTX 3070 616.56 and
RTX 3060 610.88), the driver drops the trailing `& 0xFFFF` mask for negative
clamped low halves, leaving sign-extended `0xFFFF_xxxx` to clobber bits
[31:16] of the packed destination (for example `[-1, 1] - [1, 1]` yields
`0xFFFF_FFFE` instead of `0x0000_FFFE`).

Replace the `IMin`/`IMax`/`& 0xFFFF` sequence with signed 32-bit comparisons
and a `select` chain over 16-bit constants (`0x7FFF`, `0x8000`) and
`t & 0xFFFF`, matching the masked-select saturation form used in xcl-dev#2398 for
non-packed 16-bit integer ops.

Add `i16_clamp_mask_tests::v_pk_i16_clamp_uses_masked_select_without_imin_imax`
(DXIL validator + IR check) and
`i16_clamp_mask_tests::gpu_i16_clamp_negative_low_half_keeps_high_half`
covering `V_PK_ADD_I16`, `V_PK_SUB_I16`, and `V_PK_MAD_I16` with `CLAMP=1`
alongside `V_CVT_PK_I16_I32` and `V_CVT_I16_F16` controls.

Based on @upipibi's root-cause analysis and fix (`upipibi/xcl@6fe2b3e`)
and @Smokeyqdqd's independent RTX 3060 reproduction.

Assisted-by: Google Antigravity (Gemini 4 Neon)
@DarkPanda69

Copy link
Copy Markdown

AMD run (listed as untested), plus one more Ampere card: no change from main on either, and both new tests pass.

Tested: pass, head 651fc05 applied on main 8b957be, debug, Rust 1.100.0, Windows 11 Pro 26200, Ryzen 7 5800X, DXC 1.9.2602.17, card picked with XCL_ADAPTER.
XCL_DIRECT_DXIL=1 cargo test -p xcl --lib -- gpu:: --test-threads=4 --nocapture, main and this PR one after the other on each card:

On both cards the only result changes are gpu_i16_clamp_negative_low_half_keeps_high_half and v_pk_i16_clamp_uses_masked_select_without_imin_imax, both ok. The SKIPPED sets are the same as on main.

The new GPU test uses KernelOptions { wave64: false, .. }, so it runs as wave32 on the RX 9070 XT too; this does not cover the clamp in native wave64.

Not tested: the clamp in a 64-lane wave, D3D12 debug-layer tests (no d3d12SDKLayers.dll here either), release, Intel.

AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

@andrewDoing

Copy link
Copy Markdown

Independent RTX 5070 check: NVIDIA driver 616.92; Ryzen 7 5700X3D; Windows build 26300.10075; Rust 1.100.0 MSVC, debug; SDK DXC/DXIL 1.8.2502.11.

Ran cargo test --locked -p xcl --lib i16_clamp_mask_tests -- --test-threads=1 --nocapture with XCL_ADAPTER=5070, XCL_DIRECT_DXIL=1, and separate writable caches:

  • Parent e120925e3abc9956a8b9bae631969d32e8ccd43c with only the PR's test module added: GPU packed arithmetic passed; disassembly assertion failed because IMax was present.
  • Clean PR head 651fc0502da4efd6bbcefa9faa9aed3dafb22d40: both tests passed, 0 failed, 0 ignored, no reported skips.

The arithmetic fixture did not reproduce the reported Ampere wrong-result signature on this Blackwell card and driver, including on the parent. This is not a performance or retail-title test; the full suite was not run on this PR.

AI use: OpenAI Codex with GPT-6.2 Astra ran the tests and reported results; Codex coordinated and prepared this summary.

@SlopTart

Copy link
Copy Markdown

Linux (Wine) dual-lane run at head 651fc050 - no regressions on either vendor, with one counting note.

  • Rig (Ryzen 9 9900X / RX 7900 XT / Mesa 26.2.5 RADV / wine-11.0 Proton): 602/3. Counting note: your head sits on an older main (e120925e, 5 commits behind current 8b957bec), so the three present::preparation tests that landed with xcl run: prepare shaders in the game window before title execution #2555 are absent from this tree - that is why the totals shift, not the CLAMP change itself. Reds = baseline two + the sparse::a_tile_decommitted_and_committed_again_keeps_its_video_memory load flake we documented separately today (solo 3/3 green; second suite-load appearance).
  • Boxb (i5-10600K / GTX 1070 550.163.01 / wine-10.0 + vkd3d-proton, xvfb): 602/3 = exactly the baseline three (gpu_promoted_uav_clear_values, write_buffer_immediate_on_open_existing_heap_visible_to_cpu, xma2_source_voice). No new NVIDIA reds, and none of the standing ones moved.

The masked-select saturation is exercised mainly in paths our Wine lane runs headless without real draw compare (--skip gpu::), so treat this as "does not break the suite on AMD or NVIDIA Wine" rather than a functional validation of the clamp fix itself.

Suite: cargo test --release --locked -p xcl --lib --target x86_64-pc-windows-gnu -- --test-threads=2 --skip gpu::.

AI use: Hermes agent (local Qwen-class) ran the tests and drafted this; I checked it before posting.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
@SzczepanGrela

Copy link
Copy Markdown

RX 9070 / RADV / Wine follow-up, including native wave64 and explicit plain/compact direct lowering. All captured GPU outputs match an independent CPU oracle on main, this head, and the head's automatic integration onto main. This is AMD non-regression coverage; the NVIDIA wrong-result case does not reproduce here.

Tested

  • Exact head 651fc0502da4efd6bbcefa9faa9aed3dafb22d40, current main 8b957bec6f82495aec940944c6bf47faa02db590, and a separate copy with the PR integrated onto that main. The head's parent is e120925e3abc9956a8b9bae631969d32e8ccd43c. Main's production lower_wide.rs is byte-identical to that parent; the integrated file matches the head. Local edits are test-only observations and the extra fixture described below.
  • RX 9070 16 GB, RADV GFX1201, Mesa 26.3.1-arch3.5; Ryzen 7 7700, CachyOS 7.3.0-5-cachyos. Windows GNU release executables under GE-Proton11-12 / Wine 11.0 Staging, native D3D12 through vkd3d-proton 8293130ebe370b1+ (local asserts-disabled build). DXC/DXIL 1.9.2602.17, Rust 1.97.1.
  • Build each source copy: cargo test --offline --locked --release --target x86_64-pc-windows-gnu -p xcl --lib --no-run --message-format=json-render-diagnostics.
  • Run each executable through Wine with i16_clamp_mask_tests --nocapture --test-threads=1, then --test-threads=4. Identical original assertions on all three copies; main receives the head's test module. Results for each thread count: main 1 passed, 1 failed; exact head 2 passed, 0 failed; integrated head 2 passed, 0 failed. The main failure is the expected IR-contract assertion about IMax. The original 32-lane GPU test passes in all six runs, including the negative-low-half case. Saved IR confirms IMin/IMax in main and signed compares/select without either intrinsic in the head's original IR fixture.
  • Extra owned workflow_packed_i16_matrix --nocapture --test-threads=1: three fresh process repetitions per source, 1 passed, 0 failed in every run. Each process executes 96 cases: 16 input batches, native wave32/wave64, HLSL, direct plain and explicitly selected direct compact. Compact selection is confirmed in translator logs; native WaveSize/NumThreads metadata is checked in saved DXIL.
  • The 208-byte straight-line program writes 24 packed results: ADD/SUB/MAD, CLAMP off/on, four source-half selector combinations. Two unchanged conversion controls follow: V_CVT_PK_I16_I32 and V_CVT_I16_F16. Per backend/process, native wave64 checks 1,024 input vectors and wave32 checks 512. Inputs cover signed-i16 edges, saturation, wrapping, MAD cancellation and deterministic random values. No NEG modifiers.
  • Across all 15 processes: 870 GPU dispatches, including 432 native wave64, and 1,079,232 readback u32 values, all independently recomputed from captured inputs. The Python verifier also reconstructs the extra dataset and RDNA2 instruction bytes. Captures are byte-identical across repetitions, and exact-head/integrated-head captures match. HLSL control containers match across sources. 0 ignored, 0 skipped throughout.

Runtime: inherited XCL_* variables cleared; XCL_NETWORK=0, XCL_ADAPTER=AMD Radeon RX 9070, XCL_REQUIRE_GPU=wave64, XCL_DXC_THREADS=1, XCL_DIRECT_COMPACT=0 for the original tests. The extra direct routes select plain/compact explicitly. Each process uses separate enabled XCL/vkd3d/Mesa cache directories.

Not tested

NVIDIA, native Windows, forced split wave64, other modifiers/control-flow shapes, full suite, retail workloads, scripts/smoke.ps1, performance or FPS. These synthetic results do not establish a retail regression run or reproduce the reported NVIDIA driver behavior.

AI use

OpenAI Codex (GPT-6; exact serving variant not exposed) prepared the local test-only fixture, ran the tests, independently checked the raw results and drafted this report. Publication authorized by the account owner.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants