DXIL: saturate 16-bit integer valu16 ops when CLAMP=1 #2388
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2398"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Decodere on 2026-11-25T17:44:25Z.
Part of #1401
Part of #1833
What is in it
Implement RDNA2
CLAMP=116-bit integer saturation in Direct DXILLw::valu16(crates/xcl/src/gpu/dxil/lower_wide.rs):m.clamp):v_add_nc_u16,v_add_u16, andv_mad_u16compute the exact 32-bit unsigned sum/product-sum (0xFFFF * 0xFFFF + 0xFFFF = 0xFFFF_0000, nou32overflow and zeroInt64Ops) and saturate withumin(t, 0xFFFF).v_sub_nc_u16,v_sub_u16,v_subrev_nc_u16, andv_subrev_u16saturate underflow to0viaselc(cmp(ULt, lhs, rhs), 0, lhs - rhs).m.clamp):v_add_nc_i16,v_sub_nc_i16, andv_mad_i16sign-extend 16-bit inputs toi32(si(self, i)), compute the exact 32-bit signed result ([-1_073_741_824, 1_073_774_591], noi32overflow and zeroInt64Ops), masklow = t & 0xFFFFupfront, and select0x7FFF/0x8000viaicmp sgt i32 %t, 32767/icmp slt i32 %t, -32768, preservingOP_SELhigh-half merging and SDWA writeback.and 0xFFFF+icmp sgt/slt+selectinstead ofIMax(IMin(t, 0x7FFF), -32768) & 0xFFFFavoids an NVIDIA Ampere driver miscompilation reported by@DarkPanda69(RTX 3060 driver32.0.16.1088), where the driver dropped& 0xFFFFafterIMax(IMin(...))on negative results (-2 = 0xFFFF_FFFE), clobbering the preserved upper 16 bits (0xBEEF_0000 | 0xFFFF_FFFE -> 0xFFFF_FFFE).valu16_clamp_tests):valu16_int_clamp_emits_32bit_saturation_without_i64: pure-CPU DXC 1.9 validation and disassembly test confirming0Int64Ops/i64ops in@main, thatUMin(op 40),icmp ult i32,icmp sgt i32, andicmp slt i32are emitted iffCLAMP=1, and thatIMax/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) withCLAMP=0,CLAMP=1, andOP_SEL=0b1011high-half/low-half preservation.(Rebased onto
origin/mainbe67bfcf.)Tested
cargo test -p xcl --lib gpu::dxil::lower_wide::valu16_clamp_tests::valu16_int_clamp_emits_32bit_saturation_without_i64 -- --exact --nocaptureon68a284a5):0 passed, 1 failed(CLAMP=1 must emit @dx.op.binary.i32(i32 40,).cargo test --release -p xcl --lib gpu::dxil::lower_wide::valu16_clamp_tests -- --test-threads=1 --nocaptureon NVIDIA GeForce RTX 4060 (driver621.14, DXC1.9.2609.5):2 passed, 0 failed, 0 ignored(0.46 s,0skipped).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 with0skips, GPU test cleanlySKIPPED).cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocaptureon RTX 4060):24test binaries,1318 passed, 4 failed, 46 ignored— all170/170gpu::dxil::*tests passed (0failures); the 4 failures areorigin/mainbe67bfcf's pre-existing#2074test (a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, tracked by #2379) plus 3 unrelated parallel#1978title_textures/vramglobal-mutex flakes (placed_title_textures_share_vram_and_preserve_pixelspoisoningtransitions_apply_when_their_list_is_submittedandd32s8_targets_take_their_own_size_of_video_memory);29pre-existing hardware/debug-layerSKIPPEDlines (0invalu16_clamp_tests).Regression run
Base (
origin/mainbe67bfcfe8143e21a62bbfd5d279d292229e27f9):PR head (
e9d7aefc3a18bc5223790a9d928b1574bc1198c9):Not tested
621.14, where both the original and updated lowering pass; re-verification on Ampere driver32.0.16.1088is welcome).d3d12SDKLayers.dllis not installed on this host).AI use
Google Antigravity (Gemini 4 Neon) assisted with implementing 32-bit saturating integer
CLAMP=1lowering inLw::valu16, switching the signed clamp to Ampere-safeicmp sgt/slt+ pre-maskedselect, adding DXIL disassembly and GPU execution tests, and running the verification suite.Imported from ivall/xcl. Originally posted by emirduran707 on 2026-11-25T18:00:37Z.
Independent AMD RDNA4 (native wave64) test report on exact head
b9efbd932b48607b0366a7c0a822143efda17035(clean detached checkout, no local changes).Tested
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 inlower_dxil::tests::{f16,f32}_programs_on_the_gpurefused 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.gpu::dxil::lower_wide::valu16_clamp_tests::valu16_int_clamp_emits_32bit_saturation_without_i64andgpu_valu16_int_clamp_matches_reference.main68a284a:gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1(seed 722) andfuzz_validate_and_gpu_3(seed 1599) on this GPU, plusgpu::recomp::lower_dxil::tests::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier(from50a9fde/ #2074).Regression run (60 s retail-title smoke,
XCL_NETWORK=0, release)(Matched
main68a284abaseline on the same machine today: 1573 frames, 14420 MB peak, 0 device removals, 0 crashes.)Not tested
AI use
Imported from ivall/xcl. Originally posted by DarkPanda69 on 2026-11-25T18:02:08Z.
Update for head
e9d7aef(rerun as asked by @Decodere): fixed on the RTX 3060.cargo test -p xcl --lib -- valu16_clamp_tests --test-threads=1at 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 theicmp/selectform withoutIMax/IMinkeeps the upper half on this driver. The failure below is fromb9efbd9.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
b9efbd9applied onmain68a284a, Windows 11 Pro 26200, DXC 1.9.2602.17,cargo test -p xcl --lib -- valu16_clamp_tests --test-threads=1:gpu_valu16_int_clamp_matches_referencefails, 3 of 3 runsThe failure (
lower_wide.rs:1609), identical in every run:So the low half
0xFFFEmatches, and the expected upper half0xBEEFof the destination comes back as0xFFFF: on this driver the clamped 16-bit write forop=3(withopsel=0) doesn't preserve the other half.valu16_int_clamp_emits_32bit_saturation_without_i64passes 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, plusa_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, which fails onmaintoo, fixed by #2379); RX 9070 XT 649 passed, 3 failed (the samemaintest 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.
Imported from ivall/xcl. Originally posted by Decodere on 2026-11-25T18:46:17Z.
Thanks for testing on Ampere
@DarkPanda69— great catch!Looking closely at the failing case (
op=3V_SUB_NC_I16,lane=0:-1 - 1 = -2 = 0xFFFF_FFFE, first negative signed-clamped value in lane 0): the previous signed clamp emittedIMax(IMin(t, 0x7FFF), -32768) & 0xFFFFbeforeor(cur & 0xFFFF_0000, r). Returning0xFFFF_FFFEinstead of0xBEEF_FFFEwhileCLAMP=0'sand(sub, 0xFFFF)passed shows that the Ampere32.0.16.1088driver dropped the post-clamp& 0xFFFFonIMax(IMin(...)), leaving sign-extended0xFFFF_FFFEto clobber0xBEEF_0000.Updated the branch to
e9d7aefc(rebased ontobe67bfcf):CLAMP=1lowering inLw::valu16to masklow = t & 0xFFFFupfront and select unsigned 16-bit constants0x7FFF/0x8000viaicmp sgt i32 %t, 32767/icmp slt i32 %t, -32768(zeroIMax/IMinintrinsics, everyselectbranch is already in0x0000..=0xFFFF).valu16_int_clamp_emits_32bit_saturation_without_i64to asserticmp sgt/icmp sltand noIMax/IMin(op 37/38), verified2/0on RTX 4060 & WARP,170/170gpu::dxil::*, and paired 60 s smoke (1525 -> 1468,0removals/crashes).If you have a moment to re-run
valu16_clamp_testson the RTX 3060 ate9d7aefc, that would confirm the Ampere driver workaround directly!Imported from ivall/xcl. Originally posted by SlopTart on 2026-11-25T19:18:51Z.
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 targetx86_64-pc-windows-gnurun 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 newgpu_promoted_uav_clear_values_pass_create_committed_resourcefailure that current main carries (reported here), not caused by this PR. Everything else is green.Targeted run of
gpu_valu16_int_clamp_matches_referenceandvalu16_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.
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T19:27:59Z.
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: 1038 passed, 1 failed in one parallel run; the failing test passes on a single-threaded rerun (1039/0). Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.Thanks!
Imported from ivall/xcl. Originally posted by bmjask on 2026-11-25T20:32:27Z.
Review: COMMENTED
No concrete source-level defect found. Signed/unsigned clamp paths and preserved-half handling look sound; current Windows CI, RTX reproduction, AMD native-wave64 output, and smoke evidence are strong. Nonblocking coverage gap: the GPU test covers NC forms but not every separately matched v_add_u16/v_sub_u16/v_subrev_*_u16 encoding. A parameterized encoding-variant test would protect their shared clamp branch.
Imported from ivall/xcl. Originally posted by upipibi on 2026-11-26T16:23:44Z.
@Decodere a related site this PR doesn't touch: the packed form. On
main8b957bec,lower_wide.rs:182-185still builds thev_pk_add_i16/v_pk_sub_i16/v_pk_mad_i16CLAMP as IMin/IMax followed by& 0xFFFF, and on NVIDIA that loses the mask the same way DarkPanda69's Ampere run showed here: a negative clamped low half overwrites the high half (e.g.[-1,1] - [1,1]gives0xFFFFFFFE, not0x0000FFFE).A fix in this PR's masked-select form, with a GPU test, is on a branch you are welcome to take:
github.com/upipibi/xcl@6fe2b3e7(lower_wide.rsonly, +116/-4; testi16_clamp_mask_tests::gpu_i16_clamp_negative_low_half_keeps_high_half, withV_CVT_PK_I16_I32/V_CVT_I16_F16as controls).RTX 3070 8 GB, driver 616.56, Windows 11, debug, on
8b957bec:cvtcontrols: 0 mismatches onmain.cargo test -p xcl --lib -- --skip hle::xaudio2 --test-threads=41310 passed, 0 failed, 27 ignored;cargo test --workspace1435 passed (the 3e2efailures were the unbuilt test title;cargo build --workspacethen--test e2e4/4).Not tested: AMD/Intel, a title run.
AI-assisted (Claude Code, Claude Opus 5.5): wrote the change and test, ran them, wrote this.
Imported from ivall/xcl. Originally posted by Decodere on 2026-11-26T16:36:10Z.
@upipibi — Thanks for digging in and finding that sibling packed signed-16-bit clamp case in
crates/xcl/src/gpu/dxil/lower_wide.rs!Keeping
#2398(e9d7aefc) scoped to its already-reviewed hunk preserves its existing maintainer reviews and multi-GPU verification passes intact, so opening your own standalone PR directly fromupipibi/xcl@6fe2b3eis definitely the cleanest path (clear attribution and disjoint lines inlower_wide.rs). Happy to run an RTX 4060 release verification once you open it.Imported from ivall/xcl. Originally posted by upipibi on 2026-11-26T16:48:10Z.
@Decodere thanks, that makes sense. One catch: xcl's CONTRIBUTING asks that a person opens each PR (no PRs opened by an unattended agent), and this account's work here is agent-run, so I won't open it from here. The branch stays up (
upipibi/xcl@6fe2b3e7, one commit onmain8b957bec,lower_wide.rsonly, with the GPU test); anyone is welcome to take it into their own PR, credit optional. An RTX 4060 result on it would be useful either way.AI-assisted (Claude Code, Claude Opus 5.5).
Imported from ivall/xcl. Originally posted by Smokeyqdqd on 2026-11-26T16:52:14Z.
Independent RTX 3060 result for @upipibi’s packed signed-16-bit follow-up (commit
upipibi/xcl@6fe2b3e7discussed above).Tested
Windows 11 build 26200, Ryzen 7 5800X, RTX 3060 12 GB, NVIDIA 610.88, rustc 1.100.0 MSVC, debug. With
XCL_ADAPTER=3060, I rancargo test --locked --offline -p xcl --lib -- gpu_i16_clamp_negative_low_half_keeps_high_half --nocapture --test-threads=1three times per revision.8b957bec6f82495aec940944c6bf47faa02db590with only the test module added: 0 passed, 1 failed, 0 ignored on each run. Each run had the same 33 mismatches (19 packed additions and 14 packed subtractions). Lane 0 subtraction returned0xFFFFFFFE, expected0x0000FFFE.6fe2b3e7c910d667c9d02b516580ac9c705839e6: 1 passed, 0 failed, 0 ignored on each run. All six logs had zeroSKIPPEDlines.Not tested: full workspace suite, retail gameplay, other adapters.
AI use: Codex (GPT-6.2 Astra) ran the comparison, checked the logs and drafted this report.
Imported from ivall/xcl. Originally posted by Decodere on 2026-11-26T19:02:29Z.
Opened the standalone PR for the packed 16-bit integer
CLAMP=1lowering bug (v_pk_add_i16/v_pk_sub_i16/v_pk_mad_i16): #2855 (Direct DXIL: saturate v_pk_*_i16 CLAMP with masked select (NVIDIA)).Huge thanks to @upipibi (
upipibi/xcl@6fe2b3e7) and @Smokeyqdqd for tracking down and reproducing the Ampere/RTX 30-seriesLw::valu_pksaturation failure — you are both credited in the PR description. #2855 modifies onlycrates/xcl/src/gpu/dxil/lower_wide.rsinsideLw::valu_pk(plus an isolated test module betweencmp64_testsandrcp_d_tests) so it merges cleanly with bothmainand #2398 with0conflicts.Imported from ivall/xcl. Originally posted by andrewDoing on 2026-11-26T20:33:04Z.
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.
With direct DXIL and separate fresh writable caches, ran
cargo test --locked -p xcl --lib valu16_clamp_tests -- --test-threads=1 --nocaptureserially on the original revisionb9efbd932b48607b0366a7c0a822143efda17035and updated heade9d7aefc3a18bc5223790a9d928b1574bc1198c9. Both runs: 2 passed, 0 failed, 0 ignored, no reported skips. The unchanged GPU arithmetic fixture passed on both revisions; each revision also passed its corresponding DXIL disassembly check.This Blackwell card and driver did not reproduce the wrong result reported on the RTX 3060. Not tested: a full suite, retail title or performance.
AI use: OpenAI Codex with GPT-6.2 Astra ran the tests and reported results; Codex coordinated and prepared this summary.
Imported from ivall/xcl. Originally posted by throny on 2026-11-27T21:10:50Z.
merge-check claim
head:
e9d7aefc3aImported from ivall/xcl. Originally posted by throny on 2026-11-27T21:27:12Z.
merge-check
base:
6089122ahead:
e9d7aefc3amerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 1467 passed, 0 failed, 47 ignored (26 SKIPPED lines); base alone: 1465 passed, 0 failed, 47 ignored
review: semantics correct: unsigned sub / subrev floor at 0, add / mad use a minimum against the 16-bit maximum (mad cannot overflow 32 bits); signed add / sub / mad work on sign-extended 32-bit values and clamp to the 16-bit range. The test fails without the change. Gaps, from the thread and still open: no test for the subrev forms and the non-NC encodings; the default HLSL path still ignores CLAMP (out of scope; the packed sibling is #2855).
performance: none by default: opt-in direct-DXIL path, only its own cache is invalidated (reasoned).
verdict: safe to merge
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).
Imported from ivall/xcl. Originally posted by KOLESNiii on 2026-11-28T01:12:15Z.
merge-check claim
head:
e9d7aefc3aImported from ivall/xcl. Originally posted by JJAutoCompare on 2026-11-28T01:36:07Z.
Tested
Clean
e9d7aefc3a18bc5223790a9d928b1574bc1198c9on Windows 11 25H2 build 26200.9974, Ryzen 7 3800X, Intel Arc A770 (PCI8086:56a0, driver32.0.101.9175), Rust 1.100.1 MSVC debug, and DXC/DXIL 1.9.2602.17. I reran the pinned stock executable with<libtest.exe> --exact <test> --nocapture --test-threads=1. Both tests are undergpu::dxil::lower_wide::valu16_clamp_tests.valu16_int_clamp_emits_32bit_saturation_without_i64(CPU/DXC) checked the disassembly for 32-bit saturation with clamp enabled, its absence with clamp disabled, and no 64-bit integer operations.gpu_valu16_int_clamp_matches_referenceran on native Wave32 withXCL_ADAPTER=A770under the shared Arc lock. It matched all 144 full-dword comparisons across six signed and unsigned add, subtract and multiply-add operations, threeCLAMP/OP_SELsettings, and eight asserted lanes per dispatch. It verified that low-half writes preserve0xBEEFin the upper half and high-half writes preserve0xA55Ain the lower half.2 passed, 0 failed, 0 ignored, 0 runtime skips. Both native exits and the wrapper exit were 0. The executable hash matched before, during and after the lock.
Not tested
Wave64, packed
v_pk_*instructions, other GPUs or drivers, retail titles, performance, and the full suite.AI use
T3 Code through the Codex harness (
gpt-6.2-sol) verified the pinned executable and adapter selection, ran the stock fixtures and wrote this report.Imported from ivall/xcl. Originally posted by KOLESNiii on 2026-11-28T01:51:05Z.
merge-check
base:
2c717a043chead:
e9d7aefc3amerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 1468 passed, 0 failed, 47 ignored, 25 SKIPPED: 21 no wave64, 3 per-lane f16/f32 forms refused (#1426 semantics), 1 no heap windows; failing: none. The suite ran on this PC at the same time as 6 other PRs' suites, in one queued job.
review: none. Whole diff read (lower_wide.rs: CLAMP arms for v_add/sub/subrev/mad u16 and i16, 2 tests). Saturation is computed exactly in 32 bits (u16 mad stays below 2^32; i16 results are clamped with compares and selects, not IMax/IMin), without i64; unclamped instructions take the old arms.
performance: reasoned, not measured: a few extra ALU ops only in shaders that use CLAMP on 16-bit integer ops; translation-time otherwise.
verdict: safe to merge
hardware / tool and model: Windows 11 build 26300.9974, NVIDIA GeForce RTX 2070 SUPER 8 GB (driver 621.40, wave32 only), AMD Ryzen 5 3600XT; rustc 1.100.1 (20ddd0171 2026-11-25) x86_64-pc-windows-msvc, debug test profile; trial merge 0fa33341c1ce8d71b57c995c2609fe239f0eb692 (local, not pushed). Claude Code: Claude Opus 5.5 (coordination, full-diff review, draft).