Skip to content

Shader: flush legacy f32 multiply operands and results in default MODE - #3318

Open
pmilewsskiedu-gif wants to merge 3 commits into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/shader-independent-20261122
Open

pmilewsskiedu-gif wants to merge 3 commits into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/shader-independent-20261122

Conversation

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor

Claimed in coordination: #3232 (comment).

What and why

Under the inferred RDNA2 MODE.denorm_mode_32=0 (IEEE off), DX9-style v_mul_legacy_f32 must flush f32 subnormal operands and results. It previously emitted raw OpFMul and checked only exact zero, permitting incorrect nonzero values on Vulkan drivers that preserve f32 denormals.

  • Explicitly flush both operands before the legacy zero override (0 * anything => +0, including an input that became -0).
  • Flush the product before VOP3 output scaling, flush the modified result, and apply the active modifier's -0 => +0 behavior after flushing.
  • Add signed subnormal/underflow and normal/NaN negative controls in tests/semantic/test_valu_mul_legacy_denorm.py, two entirely synthetic GPU fixtures (ps_mul_legacy_denorm and ps_mul_legacy_denorm_mods), expected words in both GPU harness definitions, and the documented mode assumption.

Tested

Windows 11 / NVIDIA RTX 4060 Ti driver 610.88 / Vulkan SDK 1.4.363, LEONIDA_VK_VALIDATION=1, LLVM AMDGPU toolchain.

Before: exact translator generated GPU outputs disagreeing in all four synthetic cases, 4,096/4,096 pixels: f32 subnormal inputs and products survived instead of flushing.

After: python -B -I tests/semantic/test_valu_mul_legacy_denorm.py -q 4/4 tests pass (also rechecked on rebased latest main). Related semantic tests 59 passed. python -I scripts/run_gpu_tests.py --require-pass --filter ps_mul_legacy_denorm ...: 2/2 GPU shaders, 8,192/8,192 pixels exact, 2/2 spirv-val both on initial main and again on rebased current main (c82fbe37).

Full GPU suite on original main: 196/196 SPIR-V valid, 144 GPU pass, 46 skipped, 1 failure. The sole failure ps_sdwa_f16_const is the existing separate FP16 subnormal issue already claimed by #3040 and unrelated to this v_mul_legacy_f32 patch. The unmerged FP16 fallback is not folded into this PR.

The implementation models the inferred game f32 MODE, not arbitrary dynamic denormal-preserving or IEEE modes. No native gfx1013 oracle or proprietary shader data is claimed or included; this is synthetic shader correctness, not genuine GTA VI execution. AI-assisted (GPT-6).

@bobcatchris

Copy link
Copy Markdown
Contributor

Apple M2 data point. Tested head c82fbe3 on Apple M2 (Mac14,3), macOS 26.4 (25E246), MoltenVK 1.4.2 (direct), subgroup 4..32, llvm-mc 23.1.6, Python 3.13.18.

python -I scripts/run_gpu_tests.py --filter ps_mul_legacy_denorm --build runtime/build/linux: 2 shaders translated, 2 pass spirv-val; 2/2 passed — ps_mul_legacy_denorm and ps_mul_legacy_denorm_mods each 4096/4096 pixels exact (8192/8192 total), matching the PR's reported results on other GPUs.

Build note: full build fails on AppleClang 17 at vk_pipeline_check.cpp:567 (BUILD-822, fixed by open PR #2927); GPU binaries built, so the run above is valid. Head fetched unchanged into a separate worktree; no rebase/edit/push. Synthetic shaders and counts only; no game files.

AI-assisted.

Document f32 denormal flushing and signed-zero VOP3 behavior with synthetic validation references. AI-assisted (GPT-6).
@hidrogenone

Copy link
Copy Markdown
Contributor

Tested 5e356275 on NVIDIA GeForce RTX 5080 (Blackwell), driver 32.0.16.2124, Windows 11, Vulkan SDK 1.4.357.0. llvm-mc LLVM 21.1.8 (WSL Ubuntu).

scripts/run_gpu_tests.py --filter legacy:

ps_mul_legacy_denorm        4096/4096 pixels exact
ps_mul_legacy_denorm_mods   4096/4096 pixels exact
ps_legacy_mul               4096/4096 pixels exact

3/3, all translated and spirv-val clean — the legacy f32 multiply flushes its denormal operands and result in the default MODE, exact on this adapter. NVIDIA Blackwell (RTX 50-series) coverage. AI-assisted.

@Rainmaker33

Copy link
Copy Markdown

Tested 5e35627 on NVIDIA GeForce RTX 3090 (Ampere, subgroup 32..32), driver 610.88 (Vulkan 1.4.341), Windows 11 Pro 26300: pass.

The PR's diff applied cleanly to main f847748. VS 2022, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8; harness programs from a CMake build of that main; overlay layers off.

@slint6614-dev

Copy link
Copy Markdown
Contributor

Tested 5e356275 on AMD Radeon RX 9070 (RDNA4, subgroup 32..64), AMD proprietary driver 26.10.1 (LLPC), Vulkan device API 1.4.349, Windows 10.0.26300.9974. Toolchain: LLVM llvm-mc 21.1.8, SPIRV-Tools v2026.4, MSVC 19.44.35230.0.

run_gpu_tests.py on the PR build: 187/201 passed, 14 skipped, 0 failed. All 206/206 shaders translated and passed spirv-val. Both new fixtures, ps_mul_legacy_denorm and ps_mul_legacy_denorm_mods, passed with 4096/4096 pixels exact each. The 14 skips are the expected D3D12-backed cases. Runtime and test programs were rebuilt from this same PR commit.

Synthetic tests only; no game data.

@crastinator

Copy link
Copy Markdown
Contributor

Tested 5e35627 merged into main f847748c (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • New tests pass natively: ps_mul_legacy_denorm (4096/4096 pixels exact); ps_mul_legacy_denorm_mods (4096/4096 pixels exact).
  • Whole run_gpu_tests.py: 187/201 native, 187/201 with LEONIDA_W64_EMULATE=1 (the skips are the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 41/41.
  • tests/semantic/test_valu_mul_legacy_denorm.py: 4 passed, 16 subtests passed.

No game files. AI-assisted (Claude Code).

@Atlonsky

Copy link
Copy Markdown

Not a pass — the full semantic suite goes red at 5e356275.

Environment: Windows 11 Pro 26200, Python 3.15.2, NVIDIA RTX 3080 (driver 616.92), llvm-mc 23.1.6 (LLVM 23, AMDGPU), SPIRV-Tools v2026.4.

python -I -m unittest discover -s tests\semantic
FAIL: test_each_mutation_is_caught (test_mutations.MutationTests.test_each_mutation_is_caught)
      (mutation='v_mul_legacy_f32 drops zero-times-anything guard')
Ran 7326 tests in 545s
FAILED (failures=1)

main (1628210) and f847748 pass that same test in the same tree, so this is caused by the PR, not by the harness.

The GPU side is fine: run_gpu_tests.py --filter ps_mul_legacy_denorm → 2/2 passed, and tests\semantic\test_valu_mul_legacy_denorm.py → 4/4 OK. The problem is narrower: the new default-MODE flush makes the DX9 zero override unobservable to the mutation harness.

v_mul_legacy_f32 overriding 0.0 * x to +0 only differs from plain multiplication when x is inf or NaN (for any finite x, 0.0 * x is already +0). The fixtures only vary denormal inputs, and once both operands and the result are flushed in default MODE, mutant and real translator emit the same word for them — so test_mutations reports the mutant as surviving.

Two ways to close it:

  • Fixture: add a case with 0.0 × +inf (and ideally 0.0 × NaN) in default MODE that separates flush-before-multiply from multiply-then-flush, and register it in the mutation table so the guard stays observable.
  • Contract: if default MODE genuinely cannot preserve 0 * inf = NaN, record that in the docs/design.md contract row and mark this mutation as expected-surviving with the reason, so the harness stops flagging it.

Repro (clean tree at 5e35627): python -I -m unittest discover -s tests\semantic -p test_mutations.py → Ran 2 tests, 1 failure, 7.6 s.

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor Author

Addressed the full semantic-suite failure reported in #3318 (comment).

Reproduced: on original PR head 5e356275 in existing isolated Windows workspace, python -I -m unittest discover -s tests/semantic -p test_mutations.py -v gave 1 failure: the exact mutation old anchor occurs zero times after v_mul_legacy_f32 changed its operands from src_f to flush_f32(src_f). This is a stale exact-text mutation anchor, not evidence that the zero guard is semantically unobservable: test_ps_legacy_mul already tests 0 × +inf, 0 × qNaN and -0 × -inf.

Fix: commit b2635d58e11a8a773a93a0abcddcae546a73cff1 updates only tests/semantic/test_mutations.py to mutate the new OpSelect %float {either} %f0 {self.flush_f32(m)} into OpCopyObject %float {self.flush_f32(m)}, which actually removes the guard while retaining flush. The mutated translation now fails the existing test_ps_legacy_mul oracle, as intended. On that branch: mutation suite 2/2 PASS, denormal CPU 4/4 PASS, pixel semantics 7/7 PASS, diff check clean; the translator's production code is unchanged by this review response.

Upstream integration: I also addressed PR mergeability, preserving original worktree and changes by merging origin/main d0835dd9 in independent LeonidaShader3318MainIntegration20261122, merging both the new upstream Float controls (f16 denormals) and this PR's v_mul_legacy_f32 rows in docs/design.md. Merge commit 4fea3aec7447975567b6e4dff906d020e3301388 pushed by ordinary fast-forward to this existing PR (no force push). On merged tree: mutation suite now 3/3 PASS including CRLF-copy anchors; denormal 4/4 PASS; pixel semantics 7/7 PASS, and git diff origin/main HEAD --check clean. GitHub now verifies this PR OPEN, mergeable=true, exact head 4fea3aec and base d0835dd9.

No change to hardware-tested legacy-multiply semantics; prior RTX 4060 Ti and independent AMD RDNA2 GPU tests remain as reported. No new GPU or full 7,000+ test sweep claimed here. Only original test/contract merge, no game data, no DRM bypass, AI-assisted. Thank you for the concrete regression report.

ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
# Conflicts:
#	docs/design.md
mrp1no added a commit to mrp1no/Leonida that referenced this pull request Nov 23, 2026
…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>
@memorypressure

Copy link
Copy Markdown

Verified on Linux — the v_mul_legacy_f32 denorm flush this PR documents is already on main: the implementation via 533d553 (SHADER-340, flush-in/flush-out, with the semantic tests and ps_mul_legacy_denorm fixtures) and the contract row via 5e35627 (both verified present; main's translator carries flush_f32). Main's test_mutations entry was likewise re-keyed to the flushing code, while this branch's entry still targets the pre-flush source text, so the merge conflicts in tests/semantic/test_mutations.py and docs/design.md at current main. Recommend closing as superseded. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

mrp1no added a commit to mrp1no/Leonida that referenced this pull request Nov 23, 2026
…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>
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.

8 participants