Direct DXIL: guard NaN operands in 32-bit and 64-bit float_minmax_mem #3283

Open
Kalmarv wants to merge 1 commit from upstream-pr/3283 into main
Kalmarv commented 2026-11-29 04:40:17 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T04:40:17Z.


What is in it

Guards NaN operands in Direct DXIL 32-bit and 64-bit memory float min/max atomics (buffer_atomic_fmin, buffer_atomic_fmax, global_atomic_fmin, global_atomic_fmax, flat_atomic_fmin, flat_atomic_fmax, and their _x2 64-bit variants) in crates/xcl/src/gpu/dxil/lower_mem.rs:

  • Before entering the CAS loop in float_minmax_mem (32-bit) and atomic64 "fmin" | "fmax" (64-bit), checks whether the incoming operand is NaN ((v & 0x7FFF_FFFF) > 0x7F80_0000 for f32, (v & 0x7FFF_FFFF_FFFF_FFFF) > 0x7FF0_0000_0000_0000 for f64).
  • When the operand is NaN, performs a loop-free read-only AtomicOp::Or with 0 instead of comparing and exchanging, preserving IEEE-754 / RDNA2 minNum/maxNum semantics (neither +qNaN nor -qNaN overwrites finite memory) while still returning the prior memory value when GLC=1.
  • Adds GPU unit test direct_atomic_fmin_fmax_nan_preserves_memory in crates/xcl/src/gpu/dxil/core_tests.rs (guarded by runner::wave32_supported() for headless/no-GPU runners) verifying 32-bit and 64-bit global/buffer fmin/fmax with +qNaN and -qNaN against positive and negative finite values, including GLC=1 return values.
  • Companion to open HLSL-only PR #1271 (which covers recomp/emit.rs and recomp/tests.rs); this PR targets the default Direct DXIL backend (dxil/lower_mem.rs and dxil/core_tests.rs) with zero file overlap.

Tested

Hardware: NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), AMD Ryzen 9 9950X, Windows 11 Pro (Build 26200), rustc 1.99.0 (b940084d7), XCL_DXC_DIR with DXC 1.9.2609.5.

  • cargo check --workspace --all-targets: 0 errors.
  • Targeted Direct DXIL GPU tests (cargo test --lib direct_atomic_fmin_fmax_nan_preserves_memory gpu_atomic_fmin_fmax_x2 gpu_atomic_fmin_fmax -- --test-threads=1): 3 passed, 0 failed.
  • Full workspace GPU test suite (cargo test --workspace -- --test-threads=4): 2609 passed, 0 failed, 49 ignored (26 SKIPPED: 21 no wave64 on NVIDIA wave32, 3 pre-existing #1426 f16 refusals, 2 no heap windows).

Regression run

main-ancestor baseline (f0e8fdd5c3942ddd399d5ee11384c8595c5aacff):

xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB

This branch (de4ebbb23ac84689501e59709cf6149e7a390ba9, 0 minidumps, 0 System display events):

xcl-smoke v1
commit: de4ebbb23ac84689501e59709cf6149e7a390ba9
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 2308 (PresentX 2308)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 32906 MB

Not tested

  • Native wave64 GPU hardware (AMD RDNA2/3/4); all 21 wave64-only GPU tests skipped on wave32 NVIDIA hardware.
  • Signalling NaNs or non-canonical NaN payloads in memory prior to an atomic fmin/fmax with a finite operand (this PR guards NaN source operands only).

AI use

Written, verified, and tested with an AI coding assistant (Antigravity), with all targeted GPU tests, full workspace tests, and 150 s xcl-smoke runs executed on local hardware.

<!-- greyfinch-sync:ivall/xcl:issue:5547613258 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3283). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T04:40:17Z. --- **What is in it** Guards NaN operands in Direct DXIL 32-bit and 64-bit memory float min/max atomics (`buffer_atomic_fmin`, `buffer_atomic_fmax`, `global_atomic_fmin`, `global_atomic_fmax`, `flat_atomic_fmin`, `flat_atomic_fmax`, and their `_x2` 64-bit variants) in `crates/xcl/src/gpu/dxil/lower_mem.rs`: - Before entering the CAS loop in `float_minmax_mem` (32-bit) and `atomic64` `"fmin" | "fmax"` (64-bit), checks whether the incoming operand is NaN (`(v & 0x7FFF_FFFF) > 0x7F80_0000` for f32, `(v & 0x7FFF_FFFF_FFFF_FFFF) > 0x7FF0_0000_0000_0000` for f64). - When the operand is NaN, performs a loop-free read-only `AtomicOp::Or` with `0` instead of comparing and exchanging, preserving IEEE-754 / RDNA2 `minNum`/`maxNum` semantics (neither `+qNaN` nor `-qNaN` overwrites finite memory) while still returning the prior memory value when `GLC=1`. - Adds GPU unit test `direct_atomic_fmin_fmax_nan_preserves_memory` in `crates/xcl/src/gpu/dxil/core_tests.rs` (guarded by `runner::wave32_supported()` for headless/no-GPU runners) verifying 32-bit and 64-bit global/buffer `fmin`/`fmax` with `+qNaN` and `-qNaN` against positive and negative finite values, including `GLC=1` return values. - Companion to open HLSL-only PR #1271 (which covers `recomp/emit.rs` and `recomp/tests.rs`); this PR targets the default Direct DXIL backend (`dxil/lower_mem.rs` and `dxil/core_tests.rs`) with zero file overlap. **Tested** Hardware: `NVIDIA GeForce RTX 5070 Ti` (driver `32.0.16.1074`), `AMD Ryzen 9 9950X`, `Windows 11 Pro (Build 26200)`, `rustc 1.99.0 (b940084d7)`, `XCL_DXC_DIR` with DXC `1.9.2609.5`. - `cargo check --workspace --all-targets`: 0 errors. - Targeted Direct DXIL GPU tests (`cargo test --lib direct_atomic_fmin_fmax_nan_preserves_memory gpu_atomic_fmin_fmax_x2 gpu_atomic_fmin_fmax -- --test-threads=1`): 3 passed, 0 failed. - Full workspace GPU test suite (`cargo test --workspace -- --test-threads=4`): 2609 passed, 0 failed, 49 ignored (26 `SKIPPED`: 21 `no wave64` on NVIDIA wave32, 3 pre-existing `#1426` f16 refusals, 2 `no heap windows`). **Regression run** `main`-ancestor baseline (`f0e8fdd5c3942ddd399d5ee11384c8595c5aacff`): ``` xcl-smoke v1 commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1695 (PresentX 1695) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 31546 MB ``` This branch (`de4ebbb23ac84689501e59709cf6149e7a390ba9`, 0 minidumps, 0 System display events): ``` xcl-smoke v1 commit: de4ebbb23ac84689501e59709cf6149e7a390ba9 gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 2308 (PresentX 2308) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 32906 MB ``` **Not tested** - Native wave64 GPU hardware (AMD RDNA2/3/4); all 21 wave64-only GPU tests skipped on wave32 NVIDIA hardware. - Signalling NaNs or non-canonical NaN payloads in memory prior to an atomic `fmin`/`fmax` with a finite operand (this PR guards NaN source operands only). **AI use** Written, verified, and tested with an AI coding assistant (`Antigravity`), with all targeted GPU tests, full workspace tests, and 150 s `xcl-smoke` runs executed on local hardware.
Foniass commented 2026-11-29 14:57:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T14:57:26Z.


merge-check
base: d8bd9a38204667f3ca7dd9eeffcc2f1c937b07b0
head: de4ebbb23ac84689501e59709cf6149e7a390ba9
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2615 passed, 4 failed, 49 ignored; SKIPPED: 44; failing: gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (driver E_OUTOFMEMORY, same on base on this machine)
review: no bug found. The test fails without the fix, it matches RDNA2 for a NaN operand, and LDS was already right. The PR's CI is red only on the pipeline_library flake that #3221 fixes. AI use names no model.
performance: 60 s smoke: 1483 frames (88.7 % of d8bd9a38), peak commit 17770 MB (99.0 %); a rerun of the same build gave 1680 frames (100.5 %) and 16959 MB, so the first run's low frame count was noise
verdict: safe to merge (after a green CI rerun; the model name is still missing)
hardware / tool and model: RTX 5070 Ti (driver 32.0.16.2114), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

Note: since this run the PR conflicts with main (3b6a91ed). The result above is for this head merged onto the base named above.

Merge-check run and written with Claude Code (Claude Opus 5.5): a test merge onto the base above, the full suite, and a 60 s scripts/smoke.ps1 run of a retail title against main on the same machine. The 4 fuzz_validate_and_gpu_* failures happen on main on this machine too.

<!-- greyfinch-sync:ivall/xcl:comment:7257706707 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3283#issuecomment-7257706707). Originally posted by [Foniass](https://github.com/Foniass) on 2026-11-29T14:57:26Z. --- ```text merge-check base: d8bd9a38204667f3ca7dd9eeffcc2f1c937b07b0 head: de4ebbb23ac84689501e59709cf6149e7a390ba9 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2615 passed, 4 failed, 49 ignored; SKIPPED: 44; failing: gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (driver E_OUTOFMEMORY, same on base on this machine) review: no bug found. The test fails without the fix, it matches RDNA2 for a NaN operand, and LDS was already right. The PR's CI is red only on the pipeline_library flake that #3221 fixes. AI use names no model. performance: 60 s smoke: 1483 frames (88.7 % of d8bd9a38), peak commit 17770 MB (99.0 %); a rerun of the same build gave 1680 frames (100.5 %) and 16959 MB, so the first run's low frame count was noise verdict: safe to merge (after a green CI rerun; the model name is still missing) hardware / tool and model: RTX 5070 Ti (driver 32.0.16.2114), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5 ``` Note: since this run the PR conflicts with main (3b6a91ed). The result above is for this head merged onto the base named above. Merge-check run and written with Claude Code (Claude Opus 5.5): a test merge onto the base above, the full suite, and a 60 s `scripts/smoke.ps1` run of a retail title against main on the same machine. The 4 `fuzz_validate_and_gpu_*` failures happen on main on this machine too.
bmjask commented 2026-12-01 21:12:35 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:12:35Z.

Code review location: crates/xcl/src/gpu/dxil/lower_mem.rs; line 1803

@@ -1791,14 +1791,25 @@ impl Lw {
         let tmp = self.tmp_var();
         let z = self.c(0);
         self.wr(tmp, z);
+        let av = self.andc(v, 0x7FFF_FFFF);
+        let nan = self.cmpc(Pred::UGt, av, 0x7F80_0000);
+        let not_nan = self.lnot(nan);
+        self.guarded(nan, &mut |lw| {
+            let z = lw.c(0);
+            let o = lw.mraw_atomic(AtomicOp::Or, a, z);
+            lw.wr(tmp, o);
+            Ok(())
+        })?;
         let pp = self.cmpc(Pred::ULe, v, 0x7F80_0000);

The operand-NaN guard looks right, but a NaN already in memory still isn't replaced. The signed/unsigned integer-atomic trick only follows minNum/maxNum when memory is finite. fmin with mem=-qNaN (0xFFC00000), v=1.0: pp is true, and IMin(-4194304, 0x3F800000) keeps 0xFFC00000, while minNum (and the LDS CAS+fmin path) gives 1.0. fmin mem=-qNaN, v=-1.0: UMax keeps the NaN. fmax mem=+qNaN (0x7FC00000), v=2.0: IMax keeps the NaN. Once a NaN gets into memory, every later fmin/fmax of that sign class sticks to it. The 64-bit copy (~1871) has the same problem. Sending NaN-involved cases through the existing CAS path, as rmw32 does for LDS, would cover both sides.

<!-- greyfinch-sync:ivall/xcl:review-comment:4761064761 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3283#discussion_r4761064761). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T21:12:35Z. **Code review location:** `crates/xcl/src/gpu/dxil/lower_mem.rs`; line 1803 ~~~~diff @@ -1791,14 +1791,25 @@ impl Lw { let tmp = self.tmp_var(); let z = self.c(0); self.wr(tmp, z); + let av = self.andc(v, 0x7FFF_FFFF); + let nan = self.cmpc(Pred::UGt, av, 0x7F80_0000); + let not_nan = self.lnot(nan); + self.guarded(nan, &mut |lw| { + let z = lw.c(0); + let o = lw.mraw_atomic(AtomicOp::Or, a, z); + lw.wr(tmp, o); + Ok(()) + })?; let pp = self.cmpc(Pred::ULe, v, 0x7F80_0000); ~~~~ --- The operand-NaN guard looks right, but a NaN already in memory still isn't replaced. The signed/unsigned integer-atomic trick only follows minNum/maxNum when memory is finite. fmin with mem=-qNaN (0xFFC00000), v=1.0: pp is true, and IMin(-4194304, 0x3F800000) keeps 0xFFC00000, while minNum (and the LDS CAS+fmin path) gives 1.0. fmin mem=-qNaN, v=-1.0: UMax keeps the NaN. fmax mem=+qNaN (0x7FC00000), v=2.0: IMax keeps the NaN. Once a NaN gets into memory, every later fmin/fmax of that sign class sticks to it. The 64-bit copy (~1871) has the same problem. Sending NaN-involved cases through the existing CAS path, as rmw32 does for LDS, would cover both sides.
bmjask commented 2026-12-01 21:12:35 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:12:35Z.

Review: COMMENTED


Review of de4ebbb2: 1 finding(s). 1 are inline.


These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

<!-- greyfinch-sync:ivall/xcl:review:6140249800 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3283#pullrequestreview-6140249800). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T21:12:35Z. **Review:** COMMENTED --- Review of `de4ebbb2`: 1 finding(s). 1 are inline. --- _These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it._
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3283:upstream-pr/3283'
git switch 'upstream-pr/3283'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3283'
git switch 'upstream-pr/3283'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3283'
git switch 'upstream-pr/3283'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3283'
git switch 'main'
git merge --squash 'upstream-pr/3283'
git switch 'main'
git merge --ff-only 'upstream-pr/3283'
git switch 'main'
git merge 'upstream-pr/3283'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!3283
No description provided.