Skip to content

rdna2spirv: the legacy multiply tests |x|, |y| < FLT_MIN instead of flushing both operands, 11% smaller modules (SHADER-771) - #4345

Open
mrp1no wants to merge 5 commits into
ItsNotRexed:mainfrom
mrp1no:shader-legacy-mul-flush-cost
Open

mrp1no wants to merge 5 commits into
ItsNotRexed:mainfrom
mrp1no:shader-legacy-mul-flush-cost

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

SHADER-771. Claim: #4257 (comment)

Depends on: #3883 (@ray3242's re-anchor of the v_mul_legacy_f32 mutation; 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_f32 flush 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.

  • Cost per instruction. On a corpus sample, a translated v_mul_legacy_f32 went from about 5 SPIR-V instructions plus its operand reads and write to 44.8 body lines on average.
  • Share of all SPIR-V. Across all instructions, v_mul_legacy_f32 alone made up 28.6% of all body lines.
  • Whole modules. I bisected one 7.2 MiB compute module between ec8ec4c9 and aa2988cc: 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_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 operand is that small, the flush leaves both unchanged.

So:

  • Gen.legacy_zero(x, y) tests |x| < FLT_MIN || |y| < FLT_MIN on the unflushed operands. The product is taken from them, then flushed, as before.
  • The flush after the modifiers stays only for div:2, the one omod that can make a normal value subnormal. clamp, mul:2 and mul:4 can't.
  • v_mad_legacy_f32 / v_mac_legacy_f32 get the same rewrite for their product. Their addend is still flushed.

Commits

  1. 5ce236b2 — @ray3242's tests/semantic: re-anchor the v_mul_legacy_f32 mutation after the flush change #3883, unchanged.

  2. The rewrite. The structural count in test_valu_mul_legacy_denorm.py follows the new code (3 compares, 4 with div:2); all its value checks are unchanged and pass. The mutation in test_mutations.py is re-anchored on the new block and still drops the zero rule.

  3. test_valu_legacy_mul_matrix.py, a new semantic test against an independent Python model of RDNA2 mode 0:

    • v_mul_legacy_f32 over 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_f32 over 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's FClamp leaves them to the driver: a NaN under clamp, and the sign of a zero under clamp.

  4. 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), main vs this branch:

main this PR
translated / spirv-val 663 / 663 663 / 663
total SPIR-V 500 MiB 446 MiB (−10.8%)
modules changed 614 (median −8.1%, up to −18.8%)

The 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_f32 drops 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 legacy passes 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) and test_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_mad and test_alu_vop3_mods_hw pass at the head.

Counts only; no shader names or data from the corpus. AI-assisted (Claude Code).

🤖 Generated with Claude Code

ray3242 and others added 4 commits November 23, 2026 01:35
…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>
@MKmajster

Copy link
Copy Markdown

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 main aa2988c and by this branch (630a57d), run with vk_compute_test ... dump, and every word compared.

Cases per seed (1,929), lane 0 only, operands ORed with v0 so nothing folds:

  • v_mul_legacy_f32 on all 27×27 pairs of specials: ±0, ±min/max subnormal, ±FLT_MIN, FLT_MIN+1ulp, ±1, 0.5, 2, ±max finite, ±inf, quiet/signalling NaNs, 1e-30, -1e-38, 3e38, 0x00400000, 0x3effffff;
  • 1,200 random v_mul_legacy_f32 / v_mad_legacy_f32 / v_mac_legacy_f32 over those plus random words, with random neg/abs on both sources and random omod from "", clamp, mul:2, mul:4, div:2, clamp mul:2, clamp div:2.
cases compared differ
seeds 1, 2, 3: main vs this branch 5,787 0
negative control, seed 1: main vs f847748c (before #3318's flush) 1,929 7, all a -0 vs +0 legacy zero under mul:*/div:2

So the |x|, |y| < FLT_MIN test plus the div:2-only post-omod flush give the same bits as #3318's full flush on NVIDIA. The harness does see a one-bit sign-of-zero difference when there is one.

No game files. AI-assisted (Claude).

@Kam471 Kam471 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.

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).
  • compileall and git diff --check clean.

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.

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Automated test: 630a57d4 merged onto main 03b09a08 on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5), Mesa 25.2.8, Ubuntu 24.04, runtime rebuilt from this branch in a clean sandbox. Result: pass.

  • scripts/run_gpu_tests.py: 192/209 passed, 15 skipped (plain main: 192/209 passed, 15 skipped).
  • Python tests this PR touches: 9 passed, 42 subtests passed.

Same machine and setup as my earlier manual reports. AI-assisted (Claude Code).

@ricardotanaka

Copy link
Copy Markdown

Tested 630a57d4 on AMD Radeon RX 7600 (RDNA3, gfx1102) with the proprietary AMD driver 26.11.2 (LLPC), Windows 11 26200, merged on main 03b09a08: pass. This adds the AMD Windows driver to the NVIDIA and RADV checks above.

run_gpu_tests.py --filter legacy with LEONIDA_VK_VALIDATION=1, natively and with LEONIDA_W64_EMULATE=1: 3/3 each, ps_legacy_mul, ps_mul_legacy_denorm and ps_mul_legacy_denorm_mods all 4096/4096 pixels exact; 3/3 translated and pass spirv-val.

I ran only the repo's fixtures, not a main-vs-branch word diff like @MKmajster's. AI-assisted (Claude Code).

@SphyserX

Copy link
Copy Markdown

Tested 630a57d on Arch Linux (in a VM), CPython 3.14, LLVM 23.1.5, Mesa llvmpipe: pass.

@whattheflop whattheflop 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.

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.

@tupacfan04

Copy link
Copy Markdown
Contributor

Correct, mergeable, but two assertions are too weak to guard the change.

compileall, regen_fixtures --check, test_valu_mul_legacy_denorm (4), test_valu_legacy_mul_matrix (2, translate + spirv_mini_interp), test_negative_controls, test_mutations (3) and 14 docs tests pass, merge clean.

The issue: reverting to the base translator leaves the updated structural test passing too - the old code emits 4 OpFOrdLessThan, the new emits 3 (mul:2) or 4 (div:2), and tests/semantic/test_valu_mul_legacy_denorm.py:79,81 only assert assertGreaterEqual. That does not guard the 11% size change; assert == 3 / == 4 (or count OpFOrdEqual) so the negative control actually fails.

AI-assisted review, no game files, no GPU.

@akimfomin

Copy link
Copy Markdown

Measured graphics pipeline creation for 03b09a08 vs 630a57d4 on RTX 4070 Ti SUPER / 617.14, Windows 11 28000.3501. The same isolated Vulkan harness timed vkCreateGraphicsPipelines with VK_NULL_HANDLE for the application pipeline cache. Fresh process per draw, unique paired shader inputs, alternating A/B order and seven timed pairs per size after a separate warmup.

Synthetic legacy multiplications Main median PR median Median SPIR-V bytes, main → PR
64 33.94 ms 24.85 ms 75,924 → 53,564
256 121.65 ms 61.85 ms 283,972 → 195,564
1,024 658.21 ms 327.47 ms 1,111,092 → 758,492

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 spirv-val --target-env vulkan1.2. These are synthetic repeated-operation shaders and pipeline creation timings. This does not measure FPS, the retail corpus, or the RADV large-shader case. AI-assisted (Codex).

…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>
@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@tupacfan04 Agreed, thanks. Fixed in 2c7af340: test_emits_explicit_f32_flushes_instead_of_a_driver_mode now asserts exact counts.

translator mul:2 div:2
this PR 3 compares, 3 selects 4 compares, 4 selects
before SHADER-771 (5ce236b2) 4 compares, 6 selects 4 compares, 6 selects

On the old translator the test now fails with 4 != 3. It still passes on this branch, and on a local merge with #4391 and #4510; neither changes these counts.

@akimfomin thanks for the pipeline-creation timing; it is the first measurement of what the smaller modules buy at run time (−27% to −50% vkCreateGraphicsPipelines on synthetic shaders, pixels identical).

AI-assisted (Claude Code).

@bielfreedom

Copy link
Copy Markdown

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 630a57d4, merge-base aa2988cc (same commit as my baseline). All 26,503 disassembled shaders translated with --no-asm, 8 shards + --merge:

main aa2988cc this PR
translated 26,503 26,503
pass spirv-val 26,422 26,422
tier A / B / C (32-wide) 510 / 11 / 1,161 510 / 11 / 1,161
total .spv bytes 18.75 GB 16.69 GB (−11.0%)
index.json — identical
  • The change is confined to the target instruction: 24,247 .spv differ (PS 11,218, MS 9,052, CS 2,315, VS 1,467, GS 195). That is exactly the set of modules whose disassembly contains v_mul_legacy_f32: every module with it changed, and none without it did.
  • The differing modules shrink by 11.1% in aggregate. Among the 20 largest modules, the reduction ranges from 0.1% to 11.7%. 11 of the 20 are ~11–12% smaller.
  • Validity is unchanged: the 81 invalid modules are the same pre-existing set as on main.

This independently confirms the "11% smaller" figure on a second copy. I did not run GPU or semantic tests on this head.

@ray3242

ray3242 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@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 test_mutations.py block. No action needed from you beyond keeping that commit intact. AI-assisted (OpenCode).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.