Skip to content

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

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/global-atomic-fminmax-nan
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/global-atomic-fminmax-nan

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 26, 2026

Copy link
Copy Markdown

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)

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 26ee5818 line-by-line (CI green):

  • Computing mag = x & 0x7FFF_FFFF and nan = mag >u 0x7F80_0000 matches all IEEE-754 f32 NaNs (+qNaN, -qNaN, +sNaN, -sNaN) while leaving ±0.0, subnormals, finite values, and ±Inf untouched.
  • Selecting the neutral element in both passes (SMIN/UMAX for fmin, SMAX/UMIN for fmax) turns NaN operands into no-ops on finite/infinite memory without affecting single-pass integer atomics (ops.len() == 1).
  • The new global_atomic_fmin_and_fmax_ignore_nan_operands test exercises all four NaN bit patterns against finite and ±Inf memory cells as well as ±Inf and -0.0 operands on both hardware and WARP.

Clean and sound.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified: the two-pass int-atomic lowering of fmin/fmax had a real NaN hole — NaN's bit pattern reads as a large positive int (or huge unsigned with the sign bit), so it could win the max pass and clobber a finite/infinite cell, breaking IEEE minNum/maxNum. The mag > 0x7F800000 check catches both signs, the neutral-element selection in both passes yields exactly 'NaN operands ignored' (and NaN+NaN stays NaN), while ±Inf and -0.0 still participate. Detection is built only for two-pass ops — right, integer atomics have no NaN semantics.

@oni-swr

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown

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).

Copy link
Copy Markdown
Owner

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

@passerbyyhx

Copy link
Copy Markdown

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.

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):

  • Diff review: Detecting NaN operands via (x & 0x7FFF_FFFF) > 0x7F80_0000 and selecting the neutral element in both passes of global_atomic_fmin / global_atomic_fmax cleanly prevents NaN inputs from overwriting finite or infinite values in memory.
  • cargo test -p victory-shader emit::memory_tests -- --test-threads=1 --skip heap_direct_texture_reads_mark_their_heap_index (RTX 5090): 5 passed, 0 failed (including global_atomic_fmin_and_fmax_ignore_nan_operands). (Skipped pre-existing #2571 on NVIDIA.)
  • VICTORY_WARP=1 cargo test -p victory-shader emit::memory_tests -- --test-threads=1 (WARP): 6 passed, 0 failed.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants