Skip to content

DXIL: saturate 16-bit integer valu16 ops when CLAMP=1 - #2398

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-valu16-int-clamp
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-valu16-int-clamp

Conversation

@Decodere

@Decodere Decodere commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part of #1401
Part of #1833

What is in it

Implement RDNA2 CLAMP=1 16-bit integer saturation in Direct DXIL Lw::valu16 (crates/xcl/src/gpu/dxil/lower_wide.rs):

  1. Unsigned 16-bit saturating ops (m.clamp):
    • v_add_nc_u16, v_add_u16, and v_mad_u16 compute the exact 32-bit unsigned sum/product-sum (0xFFFF * 0xFFFF + 0xFFFF = 0xFFFF_0000, no u32 overflow and zero Int64Ops) and saturate with umin(t, 0xFFFF).
    • v_sub_nc_u16, v_sub_u16, v_subrev_nc_u16, and v_subrev_u16 saturate underflow to 0 via selc(cmp(ULt, lhs, rhs), 0, lhs - rhs).
  2. Signed 16-bit saturating ops (m.clamp):
    • v_add_nc_i16, v_sub_nc_i16, and v_mad_i16 sign-extend 16-bit inputs to i32 (si(self, i)), compute the exact 32-bit signed result ([-1_073_741_824, 1_073_774_591], no i32 overflow and zero Int64Ops), mask low = t & 0xFFFF upfront, and select 0x7FFF / 0x8000 via icmp sgt i32 %t, 32767 / icmp slt i32 %t, -32768, preserving OP_SEL high-half merging and SDWA writeback.
    • Using pre-masked and 0xFFFF + icmp sgt/slt + select instead of IMax(IMin(t, 0x7FFF), -32768) & 0xFFFF avoids an NVIDIA Ampere driver miscompilation reported by @DarkPanda69 (RTX 3060 driver 32.0.16.1088), where the driver dropped & 0xFFFF after IMax(IMin(...)) on negative results (-2 = 0xFFFF_FFFE), clobbering the preserved upper 16 bits (0xBEEF_0000 | 0xFFFF_FFFE -> 0xFFFF_FFFE).
  3. Regression tests (valu16_clamp_tests):
    • valu16_int_clamp_emits_32bit_saturation_without_i64: pure-CPU DXC 1.9 validation and disassembly test confirming 0 Int64Ops / i64 ops in @main, that UMin (op 40), icmp ult i32, icmp sgt i32, and icmp slt i32 are emitted iff CLAMP=1, and that IMax/IMin (ops 37/38) are not emitted on the signed clamp path.
    • gpu_valu16_int_clamp_matches_reference: wave32_supported()-guarded GPU execution test across 8 overflow/underflow/boundary lanes covering all six RDNA2 VOP3 16-bit integer ops (V_ADD_NC_U16, V_SUB_NC_U16, V_ADD_NC_I16, V_SUB_NC_I16, V_MAD_U16, V_MAD_I16) with CLAMP=0, CLAMP=1, and OP_SEL=0b1011 high-half/low-half preservation.

(Rebased onto origin/main be67bfcf.)

Tested

  • Pre-fix failing-first (cargo test -p xcl --lib gpu::dxil::lower_wide::valu16_clamp_tests::valu16_int_clamp_emits_32bit_saturation_without_i64 -- --exact --nocapture on 68a284a5): 0 passed, 1 failed (CLAMP=1 must emit @dx.op.binary.i32(i32 40,).
  • Post-fix cargo test --release -p xcl --lib gpu::dxil::lower_wide::valu16_clamp_tests -- --test-threads=1 --nocapture on NVIDIA GeForce RTX 4060 (driver 621.14, DXC 1.9.2609.5): 2 passed, 0 failed, 0 ignored (0.46 s, 0 skipped).
  • Post-fix XCL_ADAPTER="Microsoft Basic Render Driver" cargo test --release -p xcl --lib gpu::dxil::lower_wide::valu16_clamp_tests -- --test-threads=1 --nocapture: 2 passed, 0 failed, 0 ignored (0.05 s; pure-CPU/DXC 1.9 test ran with 0 skips, GPU test cleanly SKIPPED).
  • Full release workspace (cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture on RTX 4060): 24 test binaries, 1318 passed, 4 failed, 46 ignored — all 170/170 gpu::dxil::* tests passed (0 failures); the 4 failures are origin/main be67bfcf's pre-existing #2074 test (a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, tracked by Tests: the bridged mask write checks for no barrier past the first branch #2379) plus 3 unrelated parallel #1978 title_textures/vram global-mutex flakes (placed_title_textures_share_vram_and_preserve_pixels poisoning transitions_apply_when_their_list_is_submitted and d32s8_targets_take_their_own_size_of_video_memory); 29 pre-existing hardware/debug-layer SKIPPED lines (0 in valu16_clamp_tests).

Regression run

Base (origin/main be67bfcfe8143e21a62bbfd5d279d292229e27f9):

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

PR head (e9d7aefc3a18bc5223790a9d928b1574bc1198c9):

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

Not tested

  • NVIDIA Ampere RTX 3060 hardware (this host has an RTX 4060 Ada driver 621.14, where both the original and updated lowering pass; re-verification on Ampere driver 32.0.16.1088 is welcome).
  • Wave64-capable hardware (RTX 4060 is wave32-only), AMD/Intel GPUs, and D3D12 debug layer tests (d3d12SDKLayers.dll is not installed on this host).

AI use

Google Antigravity (Gemini 4 Neon) assisted with implementing 32-bit saturating integer CLAMP=1 lowering in Lw::valu16, switching the signed clamp to Ampere-safe icmp sgt/slt + pre-masked select, adding DXIL disassembly and GPU execution tests, and running the verification suite.

@emirduran707

Copy link
Copy Markdown
Contributor

Independent AMD RDNA4 (native wave64) test report on exact head b9efbd932b48607b0366a7c0a822143efda17035 (clean detached checkout, no local changes).

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64, driver 32.0.31044.16), Ryzen 5 5600, 32 GB RAM, Windows 11 Pro 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2602.17; tests debug, smoke release.
  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 1289 passed, 3 failed, 46 ignored; 8 SKIPPED (6 in lower_dxil::tests::{f16,f32}_programs_on_the_gpu refused by per-lane form; heap_window_out_of_memory_falls_back_to_the_switch_form; pso_measurement_compiles_an_exact_synthetic_record). All native wave64 GPU tests ran and passed.
    • Both PR tests passed (no skips): gpu::dxil::lower_wide::valu16_clamp_tests::valu16_int_clamp_emits_32bit_saturation_without_i64 and gpu_valu16_int_clamp_matches_reference.
    • The 3 failures are pre-existing on unmodified main 68a284a: gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1 (seed 722) and fuzz_validate_and_gpu_3 (seed 1599) on this GPU, plus gpu::recomp::lower_dxil::tests::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier (from 50a9fde / Recompiler: a bridged mask write in the split form meets no barrier #2074).

Regression run (60 s retail-title smoke, XCL_NETWORK=0, release)

xcl-smoke v1
commit: b9efbd932b48607b0366a7c0a822143efda17035
gpu: AMD Radeon RX 9060 XT
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1613 (PresentX 1613)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14143 MB

(Matched main 68a284a baseline on the same machine today: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.)

Not tested

  • D3D12 debug layer; Intel GPUs.

AI use

  • Antigravity with Gemini 4 Neon ran the build, test suite, and smoke run and drafted this report.

@DarkPanda69

DarkPanda69 commented Nov 25, 2026 •

Copy link
Copy Markdown

Update for head e9d7aef (rerun as asked by @Decodere): fixed on the RTX 3060. cargo test -p xcl --lib -- valu16_clamp_tests --test-threads=1 at the head as is: RTX 3060 (Ampere, 32.0.16.1088) 2 passed, 0 failed, in 3 of 3 runs in debug and 3 of 3 in release; RX 9070 XT 2 passed in both. So the icmp/select form without IMax/IMin keeps the upper half on this driver. The failure below is from b9efbd9.


The new GPU test fails on an RTX 3060 (Ampere), passes on an RX 9070 XT. On the 3060 the 16-bit result is right but the destination's upper half is overwritten instead of kept.

Head b9efbd9 applied on main 68a284a, Windows 11 Pro 26200, DXC 1.9.2602.17, cargo test -p xcl --lib -- valu16_clamp_tests --test-threads=1:

debug release
RTX 3060 (Ampere, driver 32.0.16.1088) gpu_valu16_int_clamp_matches_reference fails, 3 of 3 runs fails the same way
RX 9070 XT (RDNA4, 32.0.32029.2010) ok ok (2 passed)

The failure (lower_wide.rs:1609), identical in every run:

assertion `left == right` failed: clamp=1 opsel=0b0 lane=0 op=3
  left: 4294967294     (0xFFFFFFFE)
 right: 3203399678     (0xBEEFFFFE)

So the low half 0xFFFE matches, and the expected upper half 0xBEEF of the destination comes back as 0xFFFF: on this driver the clamped 16-bit write for op=3 (with opsel=0) doesn't preserve the other half. valu16_int_clamp_emits_32bit_saturation_without_i64 passes on both. Your RTX 4060 (Ada) passes, so this looks specific to the Ampere driver, or to how it compiles the saturating path.

Whole gpu:: filter with this PR: RTX 3060 650 passed, 2 failed (this test, plus a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, which fails on main too, fixed by #2379); RX 9070 XT 649 passed, 3 failed (the same main test and the #2115 fuzz seeds).

Not tested: other ops / lanes past the first failing assertion on the 3060, other NVIDIA generations.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

Part of xcl-dev#1401
Part of xcl-dev#1833

Lower 16-bit integer `CLAMP=1` saturation in `Lw::valu16`
(`crates/xcl/src/gpu/dxil/lower_wide.rs`):

- When `m.clamp` is set on 16-bit integer `valu16` ops (`v_add_nc_u16`,
  `v_add_u16`, `v_sub_nc_u16`, `v_sub_u16`, `v_subrev_nc_u16`, `v_subrev_u16`,
  `v_mad_u16`, `v_add_nc_i16`, `v_sub_nc_i16`, `v_mad_i16`), compute the
  untruncated 32-bit integer result (zero `Int64Ops` needed: max unsigned
  `0xFFFF * 0xFFFF + 0xFFFF = 0xFFFF_0000` fits in `u32`, and signed
  `[-1_073_741_824, 1_073_774_591]` fits in `i32`) and saturate to
  `[0, 0xFFFF]` or `[-32768, 32767]` before `OP_SEL` / SDWA writeback.
- For signed ops (`v_add_nc_i16`, `v_sub_nc_i16`, `v_mad_i16`), mask
  `low = t & 0xFFFF` before selecting `0x7FFF` / `0x8000` via `icmp sgt` /
  `icmp slt` instead of `IMax(IMin(t, 0x7FFF), -32768) & 0xFFFF`, avoiding an
  NVIDIA Ampere driver miscompilation that drops the post-clamp `& 0xFFFF`
  mask and leaks `0xFFFF` into the preserved upper 16 bits on negative results.
- Add pure-CPU DXIL validation/disassembly test verifying `0` `Int64Ops`,
  clamp emission iff `CLAMP=1`, and no `IMax`/`IMin` on the signed clamp path,
  plus a `wave32_supported()`-guarded GPU test checking overflow, underflow,
  boundary values, and `OP_SEL` half preservation across all six RDNA2 VOP3
  16-bit integer ops (`CLAMP=0` and `CLAMP=1`).

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

Copy link
Copy Markdown
Contributor Author

Thanks for testing on Ampere @DarkPanda69 — great catch!

Looking closely at the failing case (op=3 V_SUB_NC_I16, lane=0: -1 - 1 = -2 = 0xFFFF_FFFE, first negative signed-clamped value in lane 0): the previous signed clamp emitted IMax(IMin(t, 0x7FFF), -32768) & 0xFFFF before or(cur & 0xFFFF_0000, r). Returning 0xFFFF_FFFE instead of 0xBEEF_FFFE while CLAMP=0's and(sub, 0xFFFF) passed shows that the Ampere 32.0.16.1088 driver dropped the post-clamp & 0xFFFF on IMax(IMin(...)), leaving sign-extended 0xFFFF_FFFE to clobber 0xBEEF_0000.

Updated the branch to e9d7aefc (rebased onto be67bfcf):

  • Switched signed 16-bit CLAMP=1 lowering in Lw::valu16 to mask low = t & 0xFFFF upfront and select unsigned 16-bit constants 0x7FFF / 0x8000 via icmp sgt i32 %t, 32767 / icmp slt i32 %t, -32768 (zero IMax/IMin intrinsics, every select branch is already in 0x0000..=0xFFFF).
  • Updated valu16_int_clamp_emits_32bit_saturation_without_i64 to assert icmp sgt/icmp slt and no IMax/IMin (op 37/38), verified 2/0 on RTX 4060 & WARP, 170/170 gpu::dxil::*, and paired 60 s smoke (1525 -> 1468, 0 removals/crashes).

If you have a moment to re-run valu16_clamp_tests on the RTX 3060 at e9d7aefc, that would confirm the Ampere driver workaround directly!

@SlopTart

Copy link
Copy Markdown

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

gpu_ suite, cold cache: 161 passed / 4 failed. Three are the known baseline trio on this lane; the fourth is the new gpu_promoted_uav_clear_values_pass_create_committed_resource failure that current main carries (reported here), not caused by this PR. Everything else is green.
Targeted run of gpu_valu16_int_clamp_matches_reference and valu16_int_clamp_emits_32bit_saturation_without_i64: 2 passed.

Not tested: retail titles (no title data on this lane, counts only).

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

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

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