Repository navigation
rdna2spirv: the legacy multiply tests |x|, |y| < FLT_MIN instead of flushing both operands, 11% smaller modules (SHADER-771) - #4345
Conversation
…d legacy multiply Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…lushing both operands (SHADER-771) Since ItsNotRexed#3318 every v_mul_legacy_f32 flushed both operands, the product and the result after the modifiers: about 29 instructions where the multiply used to take 5, and the largest translated compute modules grew by about a fifth (bisected: a 7.2 MiB module +21%, all from that merge). The flushed operands are never needed. flush_f32 makes an operand ±0 exactly when |x| < FLT_MIN, and the legacy rule then gives +0 whatever the other operand is (inf and NaN included); when neither is that small the flush leaves both unchanged. Gen.legacy_zero tests that on the unflushed operands, and the product is taken from them. The flush after the modifiers stays only for div:2, the one omod that can make a normal value subnormal. v_mad_legacy_f32/v_mac_legacy_f32 take the same rewrite for the product; their addend is still flushed. test_valu_mul_legacy_denorm.py's structural count follows (two operand tests and the product, plus one for div:2), and the v_mul_legacy_f32 mutation in test_mutations.py is re-anchored on the new block (it still drops the zero rule). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…special values test_valu_legacy_mul_matrix.py runs v_mul_legacy_f32 over every pair of 14 values (±0, ±the smallest and largest subnormals, ±FLT_MIN, 1.5, -2.5, 2^100, ±inf, a quiet NaN) under each output-modifier set, and v_mad_legacy_f32 over the same pairs with four addends, against an independent Python model of RDNA2 mode 0: flush-in, the +0 legacy rule, flush-out before omod, a second flush for div:2, and omod's -0 -> +0. A NaN result is compared as a NaN; a NaN or the sign of a zero under clamp is left out, as GLSL's FClamp leaves them to the driver. It passes with this branch's translator and with main's, so the rewrite before it changes no result. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d how it is cheaper now A "Cost (SHADER-771)" note under the default-mode DX9 multiply paragraph: why the flushed operands aren't needed, the div:2-only final flush, the matrix test, and the size measured on the corpus's 20 largest modules. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Differential check on a GPU that the rewrite changes no result: NVIDIA GeForce RTX 3070 (Ampere, f32 denormals kept by the driver), driver 617.42, Windows 11 24H2. The same programs were translated by Cases per seed (1,929), lane 0 only, operands ORed with
So the No game files. AI-assisted (Claude). |
Kam471
left a comment
There was a problem hiding this comment.
Ran this at 630a57d4 on Windows 11:
tests/semantic/test_valu_legacy_mul_matrix.py-> 2 tests, OK (the new matrix).tests/semantic/test_valu_mul_legacy_denorm.py-> 4 tests, OK.tests/semantic/test_mutations.py-> 3 tests, OK (112 s).compileallandgit diff --checkclean.
The rewrite expresses the legacy multiply's zero rule as |x| < FLT_MIN || |y| < FLT_MIN on the unflushed operands, which is equivalent to flushing both first because those are exactly the operands flush_f32 turns into ±0, and NaN compares false; the product still flushes, div:2 flushes before the omod, and mul:2/mul:4 keep the positive-zero rule. The matrix tests compare against the full flush path. No blocking issue.
|
Automated test:
Same machine and setup as my earlier manual reports. AI-assisted (Claude Code). |
|
Tested
I ran only the repo's fixtures, not a main-vs-branch word diff like @MKmajster's. AI-assisted (Claude Code). |
|
Tested 630a57d on Arch Linux (in a VM), CPython 3.14, LLVM 23.1.5, Mesa llvmpipe: pass.
|
whattheflop
left a comment
There was a problem hiding this comment.
AI-assisted Windows CPU verification at 630a57d4 with uv CPython 3.12.13 and SPIRV-Tools 1.4.363. A separate exact-rational round-to-nearest-even model agreed bit-for-bit on all 3,150 synthetic MUL, MAD, and MAC cases on both this head and upstream 03b09a08. Cases included signed zero, subnormals, near-FLT_MIN normals, and overflow.
On a validated ten-operation synthetic module, the incremental plain MUL cost fell from about 871 to 527 SPIR-V bytes per instruction. MAD and MAC each went from about 1,091 to 883. These sizes exclude the zero-operation module's baseline and apply only to the repeated-operation fixture.
The stock focused suites passed. Two optional coverage gaps surfaced when I changed one rule at a time in scratch: dropping the MAD product flush or taking MAC's addend from the wrong register still passed the stock legacy tests, but the independent probe caught both. Upstream's two mutation-anchor failures are handled by the #3883 prerequisite. No corpus or GPU used.
|
Correct, mergeable, but two assertions are too weak to guard the change.
The issue: reverting to the base translator leaves the updated structural test passing too - the old code emits 4 AI-assisted review, no game files, no GPU. |
|
Measured graphics pipeline creation for
The PR was faster in all 21/21 timed pairs. All 25 paired draws including warmups and controls matched every RGBA32F pixel (102,400/102,400). All 52 modules passed |
…s and selects exactly The counts were lower bounds, so the translator from before SHADER-771 (4 compares and 6 selects for each of these) passed them too. They are now exact: 3 and 3 for mul:2, 4 and 4 for div:2, and the old translator fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@tupacfan04 Agreed, thanks. Fixed in
On the old translator the test now fails with @akimfomin thanks for the pipeline-creation timing; it is the first measurement of what the smaller modules buy at run time (−27% to −50% AI-assisted (Claude Code). |
|
Full-corpus check on the game copy (Xbox Series 1.0.2.1; Windows 11, RX 9070 XT). Counts only, no shader names or data. PR head
This independently confirms the "11% smaller" figure on a second copy. I did not run GPU or semantic tests on this head. |
|
@mrp1no — merge-order decision from my side: please let #4345 land first. It already contains my #3883 re-anchor commit unchanged as its first commit, so once #4345 merges, #3883 is fully superseded and I'll close it rather than force a rebase of two PRs touching the same |
SHADER-771. Claim: #4257 (comment)
Depends on: #3883 (@ray3242's re-anchor of the
v_mul_legacy_f32mutation; its commit is the first one here, cherry-picked unchanged, so this PR reduces to my three commits once #3883 lands).Problem
#3318 made
v_mul_legacy_f32flush f32 denormals in the default MODE (flush-in/flush-out), which is right. But it flushes both operands, the product and the result after the modifiers.v_mul_legacy_f32went from about 5 SPIR-V instructions plus its operand reads and write to 44.8 body lines on average.v_mul_legacy_f32alone made up 28.6% of all body lines.ec8ec4c9andaa2988cc: it grew 21%, all of it from that merge. 9 of the corpus's 20 largest modules grew by 18–19%.Module size matters for driver compile time and memory (see SHADER-774: the largest module doesn't finish on RADV).
Change
The operands' flushed values are never needed:
flush_f32makes an operand ±0 exactly when|x| < FLT_MIN, and the legacy rule then gives +0 whatever the other operand is (inf and NaN included).So:
Gen.legacy_zero(x, y)tests|x| < FLT_MIN || |y| < FLT_MINon the unflushed operands. The product is taken from them, then flushed, as before.div:2, the one omod that can make a normal value subnormal.clamp,mul:2andmul:4can't.v_mad_legacy_f32/v_mac_legacy_f32get the same rewrite for their product. Their addend is still flushed.Commits
5ce236b2— @ray3242's tests/semantic: re-anchor the v_mul_legacy_f32 mutation after the flush change #3883, unchanged.The rewrite. The structural count in
test_valu_mul_legacy_denorm.pyfollows the new code (3 compares, 4 withdiv:2); all its value checks are unchanged and pass. The mutation intest_mutations.pyis re-anchored on the new block and still drops the zero rule.test_valu_legacy_mul_matrix.py, a new semantic test against an independent Python model of RDNA2 mode 0:v_mul_legacy_f32over every pair of 14 special values (±0, ±the smallest and largest subnormals, ±FLT_MIN, 1.5, −2.5, 2^100, ±inf, a quiet NaN) under 6 modifier sets;v_mad_legacy_f32over the same pairs with 4 addends.It passes with this branch's translator and with
main's, so the rewrite changes no result. Two cases are left out because GLSL'sFClampleaves them to the driver: a NaN underclamp, and the sign of a zero underclamp.docs/shader_validation.md: a "Cost (SHADER-771)" note under the DX9 multiply paragraph.Measured
Corpus sample. Shard 0/40 of my own 1.0.2.1 corpus (663 shaders),
mainvs this branch:mainspirv-valThe 20 largest modules: 224 → 214 MiB. The 9 that grew with #3318 are 11.4% smaller again.
Per instruction (body lines over 133 sampled shaders, 230,644 instructions):
v_mul_legacy_f32drops from 44.8 to 26.8 lines on average, and the sample's total body from 5.17 M to 4.58 M lines (−11.5%).GPU, RTX 3060 (NVIDIA 610.88, Windows 11):
scripts/run_gpu_tests.py --filter legacypasses 3/3 (ps_legacy_mul,ps_mul_legacy_denorm,ps_mul_legacy_denorm_mods), 4096/4096 pixels exact each.Semantic suite: 7,458 tests. The only failures are the ones main already has:
test_loop_ballot_barrier(5) andtest_cmp_class_f64(2), each with its own open fix.test_mutations,test_valu_mul_legacy_denorm,test_valu_legacy_mul_matrix,test_ps_semantics,test_negative_controls,test_value_semantics,test_valu_f32_min3_max3_madandtest_alu_vop3_mods_hwpass at the head.Counts only; no shader names or data from the corpus. AI-assisted (Claude Code).
🤖 Generated with Claude Code