Repository navigation
rdna2spirv: the legacy multiply tests |x|, |y| < FLT_MIN instead of flushing both operands, 11% smaller modules (SHADER-771) - #4345
Conversation
|
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 |
|
@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 |
|
Tested 2c7af34 merged onto main d0835dd on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:
AI-assisted (Claude Code). |
Flyander
left a comment
There was a problem hiding this comment.
The legacy zero override is useful for its special operand cases, but this product path also flushes a normal-input subnormal result before OMOD. That reverses the RDNA2 output order and makes the new expected-zero case incorrect.
| self.emit(f"{either} = OpLogicalOr %bool {zx} {zy}") | ||
| self.emit(f"{r} = OpSelect %float {either} %f0 {self.flush_f32(m)}") | ||
| r = self.flush_f32(self.clamp_mods(r, mods)) | ||
| self.emit(f"{r} = OpSelect %float {self.legacy_zero(x, y)} %f0 {self.flush_f32(m)}") |
There was a problem hiding this comment.
[P2] Keep the product until after OMOD, then flush the output. self.flush_f32(m) turns FLT_MIN * 0.5 into zero before clamp_mods, so a following mul:2 cannot raise it back to FLT_MIN. The legacy zero override does not apply here because both inputs are normal. RDNA2 §6.2 says OMOD is applied when output denormals are disabled, then denormals are flushed. Please move this flush after the modifier and update the case in the matrix. AMD RDNA2 ISA.
|
Rebased onto
Whole
The branch also merges cleanly with the current |
2c7af34 to
0d8919b
Compare
Flyander
left a comment
There was a problem hiding this comment.
The revised tests still encode the pre-OMOD flush for a subnormal product. The RDNA2 output-modifier rule places the flush after OMOD.
| self.emit(f"{either} = OpLogicalOr %bool {zx} {zy}") | ||
| self.emit(f"{r} = OpSelect %float {either} %f0 {self.flush_f32(m)}") | ||
| r = self.flush_f32(self.clamp_mods(r, mods)) | ||
| self.emit(f"{r} = OpSelect %float {self.legacy_zero(x, y)} %f0 {self.flush_f32(m)}") |
There was a problem hiding this comment.
[P2] Apply OMOD before flushing the product. For FLT_MIN * 0.5, both inputs are normal and legacy_zero is false. RDNA2 §6.2 says that when output denormals are disabled, the output modifier is applied and denormals are then flushed; mul:2 must therefore raise this subnormal intermediate back to FLT_MIN. flush_f32(m) turns it into zero first, and test_product_flush_precedes_modifiers currently locks in that incorrect result. Please move the output flush after OMOD and update the reference model and regression case. AMD RDNA2 ISA.
There was a problem hiding this comment.
Reproduced at this HEAD with the PR fixture helper and its CPU SPIR-V interpreter (synthetic input only, no game files): run_mul(0x00800000, 0x3F000000, "mul:2") returns 0x00000000. Under the RDNA2 §6.2 OMOD order, the subnormal product is doubled before output flush and should return 0x00800000 (FLT_MIN). The current regression test asserts the reproduced zero, so it does not catch this mismatch.
|
Tested 0d8919b merged with main 98fa503 on Linux (WSL2), Python 3.12.16 (CPU only, no GPU): pass.
|
|
Driver compile time on an RTX 4080 SUPER (Windows 11,
Per module the six compile times are 349-412 s (one 782 s) on main and 347-359 s (one 779 s) here. Five of the six modules each shrink by about 11% in SPIR-V and the sixth by 7.7%. So the PR does what it says for size, and the driver emits about 6.6% less code, but compile time moves much less than the instruction count does: it is not the straight-line legacy-multiply lines that cost the NVIDIA compiler time in these modules. Sampled over 120 random corpus shaders, |
|
@Flyander, thanks, and sorry for the late reply. This ordering isn't something this PR introduces. It is
If that answers it, could you update your review? AI-assisted (Claude Code). |
…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>
…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>
|
Rebased onto Both suites against a
@Flyander, your request for changes is still open: my answer above (the pre-OMOD flush is main's behaviour since #3318, with the gfx1036 and gfx1010 sources) still applies to this head. AI-assisted (Claude Code). |
0d8919b to
73b5101
Compare
|
Tested 73b5101 on Intel UHD Graphics 630 (CFL GT2), Mesa ANV, subgroup 8..32: run_gpu_tests.py --filter legacy prints "PASS ps_legacy_mul 4096/4096 pixels exact", "PASS ps_mul_legacy_denorm 4096/4096 pixels exact", "PASS ps_mul_legacy_denorm_mods 4096/4096 pixels exact" and "3/3 passed". With LEONIDA_VK_DEVICE=NoSuchDevice the same filter fails 0/3 with "no Vulkan physical device matches". tests/semantic test_valu_mul_legacy_denorm, test_valu_legacy_mul_matrix and test_mutations run 9 tests OK. The full run_gpu_tests.py suite gives 167/232 passed, 63 skipped, with FAIL cs_fmt_int_narrow ("word 34: expected 0xffffffff, got 0x00000000") and FAIL cs_signed_zero_nan, and main 561b65a on the same GPU gives the same counts and the same two FAILs. The full tests/semantic suite was not run, and the RTX 3060 numbers in the description were not checked. AI-assisted. |
shayderrr
left a comment
There was a problem hiding this comment.
Review
Reviewed test coverage in PR #4345: rdna2spirv: the legacy multiply tests |x|, |y| < FLT_MIN instead of flushing both operands, 11% smaller modules (SHAD....
Scope: docs/shader_validation.md, tests/semantic/test_mutations.py, tests/semantic/test_valu_legacy_mul_matrix.py, +2 more
Diff: +173 / -19 across 5 file(s).
The change stays inside the stated scope and the touched files line up with the description. No unrelated production paths are pulled in, and the stated behavior is directly exercised by the diff. I'm approving this one.
Reviewed by shayderrr via automated batch review.
|
Tested 73b5101 merged with main 1cc6e3c on Linux/Python 3.12.16: pass The merge is clean. Ran tests/semantic test_valu_legacy_mul_matrix (2 tests), test_valu_mul_legacy_denorm (4), test_mutations (3), plus the other semantic files that mention legacy ops: test_valu_f32_min3_max3_mad (22), test_alu_vop3_mods_hw (2), test_ps_semantics (7), test_value_semantics (41), test_wave64_halves (125), test_salu_sext_flbit (30), test_bitcast_round_trip (3), test_negative_controls (1). All pass. Negative control: tools/rdna2spirv.py restored from upstream/main, then test_valu_mul_legacy_denorm fails (1 failure of 4); the matrix test still passes, as expected for an equivalence check. Restored the file afterwards. Redundancy: no legacy_zero helper or equivalent in main history (git log -S), and no merged PR covers it. |
|
@Flyander, there's now hardware data on the order you raised, in #5399 (@Vonsek). They ran So for This PR doesn't change that order: it is the same results in smaller modules. The other half of #5399's finding (the |
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