Shaders/Lower: guard global_atomic_fmin and global_atomic_fmax against NaN operands #3056

Open
github-bot wants to merge 1 commit from github-bot/gh-3056 into main AGit
Owner

@⁠RVZO6 on GitHub · 2026-11-26 05:43 UTC · original
RVZO6:rvzo/global-atomic-fminmax-nan → main · +170 −7 · 2 files · 1 commits · open

Problem

In crates/victory-shader/src/emit/lower.rs (Lower::flat_write), global_atomic_fmin and global_atomic_fmax lower via two integer atomics split by sign bit (SLT(x, 0)) without checking for NaN:

  • A positive NaN (0x7FC0_0000 / 0x7F80_0001) has negative == false, so global_atomic_fmax executes SMAX(mem, +NaN) and overwrites any finite or +Inf value in memory with +NaN (since 0x7FC0_0000 > 0x7F80_0000 as signed i32).
  • A negative NaN (0xFFC0_0000 / 0xFF80_0001) has negative == true, so global_atomic_fmin executes UMAX(mem, -NaN) and overwrites any finite or -Inf value in memory with -NaN (since 0xFFC0_0000 > 0xFF80_0000 as unsigned u32).

Per AMD RDNA2/GFX10.3 GLOBAL_ATOMIC_FMIN/GLOBAL_ATOMIC_FMAX IEEE minNum/maxNum semantics, a NaN operand must leave a non-NaN value in memory unchanged.

Fix

When lowering float min/max atomics (ops.len() > 1), compute nan = (x & 0x7FFF_FFFF) >_u 0x7F80_0000 (pred::IUGT) and select the pass's neutral element in both passes when nan is true. Disjoint from #1915 (which covers integer global atomics).

Verification

  • Added emit::memory_tests::global_atomic_fmin_and_fmax_ignore_nan_operands testing +qNaN, -qNaN, +sNaN, and -sNaN against both finite and ±Inf memory cells, plus ±Inf and -0.0 updates.
  • Verified green on windows-latest MSVC + WARP SM 6.6 + dxil.dll CI (cargo clippy --workspace --all-targets --locked -- -D warnings, --all-features, xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45762903919
  • Not run: the title; any hardware GPU.

AI-assisted: T3 Code (Antigravity, gemini-4-neon)

**@⁠RVZO6** on GitHub · 2026-11-26 05:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/3056) `RVZO6:rvzo/global-atomic-fminmax-nan` → `main` · +170 −7 · 2 files · 1 commits · open ### Problem In `crates/victory-shader/src/emit/lower.rs` (`Lower::flat_write`), `global_atomic_fmin` and `global_atomic_fmax` lower via two integer atomics split by sign bit (`SLT(x, 0)`) without checking for `NaN`: - A positive NaN (`0x7FC0_0000` / `0x7F80_0001`) has `negative == false`, so `global_atomic_fmax` executes `SMAX(mem, +NaN)` and overwrites any finite or `+Inf` value in memory with `+NaN` (since `0x7FC0_0000 > 0x7F80_0000` as signed `i32`). - A negative NaN (`0xFFC0_0000` / `0xFF80_0001`) has `negative == true`, so `global_atomic_fmin` executes `UMAX(mem, -NaN)` and overwrites any finite or `-Inf` value in memory with `-NaN` (since `0xFFC0_0000 > 0xFF80_0000` as unsigned `u32`). Per AMD RDNA2/GFX10.3 `GLOBAL_ATOMIC_FMIN`/`GLOBAL_ATOMIC_FMAX` IEEE `minNum`/`maxNum` semantics, a `NaN` operand must leave a non-NaN value in memory unchanged. ### Fix When lowering float min/max atomics (`ops.len() > 1`), compute `nan = (x & 0x7FFF_FFFF) >_u 0x7F80_0000` (`pred::IUGT`) and select the pass's `neutral` element in both passes when `nan` is true. Disjoint from #1915 (which covers integer global atomics). ### Verification - Added `emit::memory_tests::global_atomic_fmin_and_fmax_ignore_nan_operands` testing `+qNaN`, `-qNaN`, `+sNaN`, and `-sNaN` against both finite and `±Inf` memory cells, plus `±Inf` and `-0.0` updates. - Verified green on `windows-latest` MSVC + WARP SM 6.6 + `dxil.dll` CI (`cargo clippy --workspace --all-targets --locked -- -D warnings`, `--all-features`, `xtask policy`, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45762903919 - **Not run:** the title; any hardware GPU. AI-assisted: T3 Code (Antigravity, gemini-4-neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:27:42 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 06:04 UTC · original

Independent Windows x64 verification of 26ee5818dffab1deaee89c199991987d6c3451fc against its exact parent 63bcca43c6a00a758510398fa43096ce7e4f7df0.

  • PR head: the synthetic min/max regression completed all nine dispatch and readback cases on WARP SM 6.6. Result was 1 passed, 0 failed, 0 ignored.
  • Exact-parent control: I kept the same synthetic test and WARP verification instrumentation, and restored the production lowering from the exact parent. The first dispatch and readback failed at +qNaN vs finite: the max cell read back 0x7fc00000, expected 0xbf800000 (-1.0). The assertion ended the run at that case.
  • Both runs confirmed Microsoft Basic Render Driver (vendor 0x1414, device 0x008c, software flag 0x2) before device creation. The fixture used the Windows SDK 10.0.26100.0 x64 dxil.dll and ran only the WARP branch.

Command: cargo test --locked -j 4 -p victory-shader --lib --features host -- --exact emit::memory_tests::global_atomic_fmin_and_fmax_ignore_nan_operands --nocapture.

AI-assisted (Codex, gpt-6.2-sol).

**@⁠oni-swr** on GitHub · 2026-11-26 06:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/3056#issuecomment-7184071125) Independent Windows x64 verification of `26ee5818dffab1deaee89c199991987d6c3451fc` against its exact parent `63bcca43c6a00a758510398fa43096ce7e4f7df0`. - PR head: the synthetic min/max regression completed all nine dispatch and readback cases on WARP SM 6.6. Result was 1 passed, 0 failed, 0 ignored. - Exact-parent control: I kept the same synthetic test and WARP verification instrumentation, and restored the production lowering from the exact parent. The first dispatch and readback failed at `+qNaN vs finite`: the max cell read back `0x7fc00000`, expected `0xbf800000` (-1.0). The assertion ended the run at that case. - Both runs confirmed Microsoft Basic Render Driver (vendor `0x1414`, device `0x008c`, software flag `0x2`) before device creation. The fixture used the Windows SDK `10.0.26100.0` x64 `dxil.dll` and ran only the WARP branch. Command: `cargo test --locked -j 4 -p victory-shader --lib --features host -- --exact emit::memory_tests::global_atomic_fmin_and_fmax_ignore_nan_operands --nocapture`. AI-assisted (Codex, gpt-6.2-sol).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 06:16 UTC · original

Automated triage for the maintainers.

Merge candidate. In crates/victory-shader/src/emit/lower.rs (Lower::flat_write), the two-pass integer lowering of global_atomic_fmin/global_atomic_fmax let a NaN operand win: a positive NaN is the largest signed integer seen by the SMAX pass, and a negative NaN the largest unsigned one seen by the UMAX pass. The PR computes (x & 0x7FFF_FFFF) >u 0x7F80_0000 once and feeds each pass its neutral element for NaN lanes; I checked the neutral values against the existing ops table, and integer atomics (ops.len() == 1) are unchanged. The new test drives the existing program() cells at +0x80/+0x84 with all four NaN classes, infinities and -0.0.

One point to confirm when reviewing: the test also asserts that a signalling NaN leaves memory unchanged. Under the IEEE 754-2008 minNum/maxNum the description cites, an sNaN operand gives a quiet NaN instead; a pointer to the RDNA2 ISA section that defines float memory-atomic NaN handling would settle which one the hardware does. A NaN already in memory is still not replaced by a number (out of scope here, but worth noting).

CI: check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 06:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/3056#issuecomment-7184268218) <!-- victory-triage v1 head=26ee5818dffab1deaee89c199991987d6c3451fc verdict=merge-candidate at=2026-11-26T06:16:24Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-shader/src/emit/lower.rs` (`Lower::flat_write`), the two-pass integer lowering of `global_atomic_fmin`/`global_atomic_fmax` let a NaN operand win: a positive NaN is the largest signed integer seen by the SMAX pass, and a negative NaN the largest unsigned one seen by the UMAX pass. The PR computes `(x & 0x7FFF_FFFF) >u 0x7F80_0000` once and feeds each pass its neutral element for NaN lanes; I checked the neutral values against the existing `ops` table, and integer atomics (`ops.len() == 1`) are unchanged. The new test drives the existing `program()` cells at +0x80/+0x84 with all four NaN classes, infinities and -0.0. One point to confirm when reviewing: the test also asserts that a signalling NaN leaves memory unchanged. Under the IEEE 754-2008 `minNum`/`maxNum` the description cites, an sNaN operand gives a quiet NaN instead; a pointer to the RDNA2 ISA section that defines float memory-atomic NaN handling would settle which one the hardware does. A NaN already in memory is still not replaced by a number (out of scope here, but worth noting). CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠passerbyyhx on GitHub · 2026-11-26 06:22 UTC · original

Native RTX 4090 Laptop readback (driver 621.14): all 9 min/max output pairs match at 26ee5818. At parent 63bcca43, the same test inputs produce 6 mismatches out of 9. Both runs resolved the D3D12 device LUID to NVIDIA 10de:2757 with the software flag clear. Both shaders passed DXIL validation with the Windows SDK 10.0.26100.0 validator.

With min/max cells initialized to +1/−1, the parent wrote the positive quiet or signaling NaN bits into the max cell (7fc00000 or 7f800001). Negative quiet or signaling NaNs overwrote the min cell (ffc00000 or ff800001). Positive and negative quiet NaNs also overwrote one cell when the pair started at −∞/+∞. At this head each of those six input pairs retained its initial bits. The three numeric controls (+∞, −∞, −0) passed on both revisions, including 80000000 in both cells for −0.

I used the PR fixture on both production revisions, locally making it hardware-only, logging the adapter and every pair, and checking the mismatch count after the ninth readback. Coordination.

**@⁠passerbyyhx** on GitHub · 2026-11-26 06:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/3056#issuecomment-7184371939) Native RTX 4090 Laptop readback (driver 621.14): all 9 min/max output pairs match at `26ee5818`. At parent `63bcca43`, the same test inputs produce 6 mismatches out of 9. Both runs resolved the D3D12 device LUID to NVIDIA `10de:2757` with the software flag clear. Both shaders passed DXIL validation with the Windows SDK 10.0.26100.0 validator. With min/max cells initialized to +1/−1, the parent wrote the positive quiet or signaling NaN bits into the max cell (`7fc00000` or `7f800001`). Negative quiet or signaling NaNs overwrote the min cell (`ffc00000` or `ff800001`). Positive and negative quiet NaNs also overwrote one cell when the pair started at −∞/+∞. At this head each of those six input pairs retained its initial bits. The three numeric controls (+∞, −∞, −0) passed on both revisions, including `80000000` in both cells for −0. I used the PR fixture on both production revisions, locally making it hardware-only, logging the adapter and every pair, and checking the mismatch count after the ninth readback. [Coordination](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3056/head:github-bot/gh-3056'
git switch 'github-bot/gh-3056'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3056'
git switch 'github-bot/gh-3056'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3056'
git switch 'github-bot/gh-3056'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3056'
git switch 'main'
git merge --squash 'github-bot/gh-3056'
git switch 'main'
git merge --ff-only 'github-bot/gh-3056'
git switch 'main'
git merge 'github-bot/gh-3056'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3056
No description provided.