Shaders: exact rewrites of float maximum, minimum and median on any host #1538
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1538"
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 trustno1-coder/VIctory-NVIDIA. Originally posted by mizzied on 2026-11-28T21:23:17Z.
What it does. Two exact rewrites that leave every stored bit unchanged and write fewer instructions. Each is on unless its switch is
0, and both are in the translation fingerprint, so the shader cache goes cold once.VICTORY_MINMAX_NONZERO(emit/cost.rs,lower/minmax.rs): a maximum or minimum with a constant whose magnitude bits are not all zero skips the two-zeros test and its select. That test is made on the bits, so it's false for any other operand and the select already picks FMax's own result. It's exact by construction on every host, and denormal or NaN constants count.VICTORY_MED3_CLAMP: the median of a value and two constants becomesmin(max(x, lo), hi)(a NaN value giveslo, as the general form does) where both constants are normal numbers or infinities.rdna::MED3already writes this clamp on RDNA hosts only; this writes it on any host. Zero bounds are left to the general form, because a host's maximum of -0 and +0 is its own (WARP's depends on operand order), so the forms would differ formed3(0, x, 1)withx = -0.max_minandmed3move fromlower.rstolower/minmax.rs(lower.rsshrinks by 59 lines).How it was tested. RTX 5070, Windows 11. New tests in
valu_tests/cost_checks.rs(with a test-onlycensus::counting) translate each program with no rewrite, with each one alone and with both on. They run it on the card and on WARP over denormals, signed zeros, NaNs and infinities in every operand. Every store is bit-identical, and the programs the rewrites apply to write fewer instructions:max(x, 1.0)goes from 19 to 13, med3 with bounds -1 and 2 from 20 to 14, and a run of five from 60 to 53. Programs they must leave alone (a zero bound, a denormal bound, a NaN bound, two variables) come out the same.cargo test --workspacepasses apart fromrepository_passes_policy, which already fails onmain:lower.rsis over the 640 KiB file-size limit there (#1493 raises it). Clippy reports only the 16 findings already onmain, none on lines this PR changes.