Shaders/Lower: guard global_atomic_fmin and global_atomic_fmax against NaN operands #3054
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3056"
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 Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-26T05:43:01Z.
Problem
In
crates/victory-shader/src/emit/lower.rs(Lower::flat_write),global_atomic_fminandglobal_atomic_fmaxlower via two integer atomics split by sign bit (SLT(x, 0)) without checking forNaN:0x7FC0_0000/0x7F80_0001) hasnegative == false, soglobal_atomic_fmaxexecutesSMAX(mem, +NaN)and overwrites any finite or+Infvalue in memory with+NaN(since0x7FC0_0000 > 0x7F80_0000as signedi32).0xFFC0_0000/0xFF80_0001) hasnegative == true, soglobal_atomic_fminexecutesUMAX(mem, -NaN)and overwrites any finite or-Infvalue in memory with-NaN(since0xFFC0_0000 > 0xFF80_0000as unsignedu32).Per AMD RDNA2/GFX10.3
GLOBAL_ATOMIC_FMIN/GLOBAL_ATOMIC_FMAXIEEEminNum/maxNumsemantics, aNaNoperand must leave a non-NaN value in memory unchanged.Fix
When lowering float min/max atomics (
ops.len() > 1), computenan = (x & 0x7FFF_FFFF) >_u 0x7F80_0000(pred::IUGT) and select the pass'sneutralelement in both passes whennanis true. Disjoint from #1915 (which covers integer global atomics).Verification
emit::memory_tests::global_atomic_fmin_and_fmax_ignore_nan_operandstesting+qNaN,-qNaN,+sNaN, and-sNaNagainst both finite and±Infmemory cells, plus±Infand-0.0updates.windows-latestMSVC + WARP SM 6.6 +dxil.dllCI (cargo clippy --workspace --all-targets --locked -- -D warnings,--all-features,xtask policy, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45762903919AI-assisted: T3 Code (Antigravity, gemini-4-neon)
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T05:55:30Z.
Review: APPROVED
Reviewed head
26ee5818line-by-line (CI green):mag = x & 0x7FFF_FFFFandnan = mag >u 0x7F80_0000matches all IEEE-754 f32 NaNs (+qNaN,-qNaN,+sNaN,-sNaN) while leaving±0.0, subnormals, finite values, and±Infuntouched.SMIN/UMAXforfmin,SMAX/UMINforfmax) turns NaN operands into no-ops on finite/infinite memory without affecting single-pass integer atomics (ops.len() == 1).global_atomic_fmin_and_fmax_ignore_nan_operandstest exercises all four NaN bit patterns against finite and±Infmemory cells as well as±Infand-0.0operands on both hardware and WARP.Clean and sound.
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T06:02:03Z.
Review: APPROVED
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 > 0x7F800000check 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.Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T06:04:41Z.
Independent Windows x64 verification of
26ee5818dffab1deaee89c199991987d6c3451fcagainst its exact parent63bcca43c6a00a758510398fa43096ce7e4f7df0.+qNaN vs finite: the max cell read back0x7fc00000, expected0xbf800000(-1.0). The assertion ended the run at that case.0x1414, device0x008c, software flag0x2) before device creation. The fixture used the Windows SDK10.0.26100.0x64dxil.dlland 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).
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T06:31:20Z.
Review: APPROVED
Tested locally merged onto
origin/main(63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):(x & 0x7FFF_FFFF) > 0x7F80_0000and selecting the neutral element in both passes ofglobal_atomic_fmin/global_atomic_fmaxcleanly 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 (includingglobal_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).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:16:34Z.
Automated triage for the maintainers.
Merge candidate. In
crates/victory-shader/src/emit/lower.rs(Lower::flat_write), the two-pass integer lowering ofglobal_atomic_fmin/global_atomic_fmaxlet 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_0000once and feeds each pass its neutral element for NaN lanes; I checked the neutral values against the existingopstable, and integer atomics (ops.len() == 1) are unchanged. The new test drives the existingprogram()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/maxNumthe 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:
checkpassing.Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-26T06:22:47Z.
Native RTX 4090 Laptop readback (driver 621.14): all 9 min/max output pairs match at
26ee5818. At parent63bcca43, the same test inputs produce 6 mismatches out of 9. Both runs resolved the D3D12 device LUID to NVIDIA10de:2757with 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 (
7fc00000or7f800001). Negative quiet or signaling NaNs overwrote the min cell (ffc00000orff800001). 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, including80000000in 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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.