Skip to content

rdna2spirv: a constant operand of a denormal-flushing op is flushed at translation, not at run time (-0.9% SPIR-V) - #5263

Open
mrp1no wants to merge 2 commits into
ItsNotRexed:mainfrom
mrp1no:shader-flush-constant-operands
Open

mrp1no wants to merge 2 commits into
ItsNotRexed:mainfrom
mrp1no:shader-flush-constant-operands

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

New SHADER item. Claim: #5156 (comment)

Gen.flush_f32 emulates the f32 denormal flush of v_mad_f32, v_mac_f32, v_mul_legacy_f32, v_mad_legacy_f32/v_mac_legacy_f32, the f32 operands of v_fma_mix_* and the accumulator of v_dot2_f32_f16 at run time: FAbs, a compare with FLT_MIN, the sign mask, a bitcast and a select per operand. It did that for inline constants and literals too (v_mad_f32 v0, v1, 0.5, v2), whose bits are known when the shader is translated.

Change

Gen.src_f_flushed(a) is flush_f32(src_f(a)), worked out in the translator when a is an imm or fimm operand:

  • a subnormal constant becomes the zero of its sign;
  • any other constant (normal, ±0, inf, NaN) is unchanged.

That is exactly what flush_f32 computes at run time (NaN and inf compare false and pass through, a zero keeps its sign). An operand with abs/neg, an SDWA select or sext still goes through flush_f32. The six call sites that flushed src_f(...) use it. The flushes of products, sums and omod results are unchanged.

Tests

tests/semantic/test_flush_constant_operands.py (translator + spirv_mini_interp, hand-written programs):

  • values: v_mad_f32 with a subnormal (1, 0x807fffff), -0, 0.5 or another literal in each of the three slots, against a flushed reference; v_mac_f32 with a constant multiplicand; v_fma_mix_f32 with 64 and 0.5; v_dot2_f32_f16 with a constant accumulator; v_mul_legacy_f32/v_mad_legacy_f32 with a subnormal constant times inf (+0 by the legacy rule) and as the addend;
  • structure: a constant operand of v_mad_f32, v_dot2_f32_f16 and v_fma_mix_f32 gets no run-time flush, while a modified one (|v2|) still does.

With tools/rdna2spirv.py from main the five structural checks fail and the value checks pass.

  • pytest tests/semantic -n 10: 6,251 passed, 5 failed, the same 5 test_loop_ballot_barrier subtests as main 3faac557.
  • pytest tests --ignore=tests/semantic -n 10: the same failures as main 3faac557 (none of them in the translator).
  • scripts/run_gpu_tests.py on Intel UHD Graphics 750 (Windows 11, LEONIDA_VK_DEVICE=Intel, LEONIDA_W64_EMULATE=1, test programs built from 98fa503f, which this PR's C++-free change doesn't touch): 180/217 passed, 37 skipped, every test with the same result as main 3faac557 on the same run setup.

Measured on my own 1.0.2.1 corpus (shard 0/40, counts only)

main 3faac557 against this PR, same machine, tools/rdna2spirv.py --shard 0/40:

main this PR
translated / pass spirv-val 663 / 663 663 / 663
function-body instructions 27,568,451 27,314,603 (−0.92%)
.spv bytes 521,677,524 516,745,620 (−0.95%)
FAbs 772,627 736,363
modules changed 527 of 644

Other PRs

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

🤖 Generated with Claude Code

@SweatierKey

Copy link
Copy Markdown
Contributor

Independent CPU verification on Linux (Python 3.13.5 + spirv-as/spirv-val, head df5bf139, base main 3faac557):

  • git diff --check main...HEAD and python3 -I -m py_compile tools/rdna2spirv.py tests/semantic/test_flush_constant_operands.py — clean.
  • Verified Gen.src_f_flushed against src_u / src_f (tools/rdna2spirv.py): matches flush_f32(src_f(a)) bit-for-bit on imm/fimm (flushing subnormals to signed zero while leaving ±0, normals, ±inf, and NaN untouched), falls back to runtime flush_f32(self.src_f(a)) whenever abs/neg/sdwa_sel/sext are present, and converts every flush_f32(self.src_f(...)) call site in tools/rdna2spirv.py (none left behind).
  • Ran semantic suites (python3 -I):
    • tests/semantic/test_flush_constant_operands.py — 8/8 OK (confirmed main 3faac557 emits 5 / 1 / 3 FAbs flushes vs PR's 4 / 0 / 1 for v_mad_f32 v0, v1, 0.5, v2, v_dot2_f32_f16 v0, v1, v2, 1, and v_fma_mix_f32 v0, 0.5, v2, 64).
    • tests/semantic/test_valu_dot2_f16.py — 66/66 OK
    • tests/semantic/test_valu_f32_min3_max3_mad.py — 22/22 OK
    • tests/semantic/test_valu_fma_mix.py — 15/15 OK
    • tests/semantic/test_valu_mad_lit.py — 15/15 OK
    • tests/semantic/test_valu_mul_legacy_denorm.py — 4/4 OK
  • Validated 8 translated synthetic shaders covering subnormals (1, 0x807fffff, 64), -0.0, FLT_MIN (0x00800000), 0.5, +inf, and NaN across v_mad_f32, v_fma_mix_f32, v_dot2_f32_f16, and v_mul_legacy_f32 through spirv-as + spirv-val --target-env vulkan1.3 — 8/8 PASS.

Looks clean and solid.

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

Head df5bf13. Gen.src_f_flushed folds flush_f32 for imm/fimm operands; checked it is byte-for-byte what src_u would emit for those operands (shader_ir.py gives imm for integer/hex literals, fimm for decimal floats), and each of the six call sites previously did flush_f32(src_f(...)) on the same operand order, so only the redundant FAbs/compare/select goes away.

Commands and results (Python 3.13, Windows 11):

python tests/semantic/test_flush_constant_operands.py          # Ran 8 tests ... OK
python tests/semantic/test_valu_fma_mix.py                     # 15 OK
python tests/semantic/test_valu_mul_legacy_denorm.py           # 4 OK
python tests/semantic/test_valu_mad_lit.py                     # 15 OK
python tests/semantic/test_fma_exact.py                        # 9 OK
python tests/semantic/test_fma_khr.py                          # 9 OK
python tests/semantic/test_valu_dot2_f16.py                    # 66 OK
python tests/semantic/test_alu_dot_pk_fmac.py                  # 14 OK
python tests/semantic/test_valu_f32_min3_max3_mad.py           # 22 OK

Mutation check reproduced the PR's claim: with tools/rdna2spirv.py from main 3faac55 the new test fails 5 (structural FAbs counts at 5 != 4), values still pass.

git diff --check clean. Verdict: pass; no blocker. (Not re-run: full semantic suite and the GPU corpus run.)

@Atomic258

Copy link
Copy Markdown
Contributor

Tested df5bf13 on Windows Server 2022, CPython 3.13.12: pass

  • python -I -m unittest discover -s tests/semantic -p "test_flush_constant_operands.py" -> Ran 8 tests, OK
  • Negative control (tools/rdna2spirv.py swapped to upstream/main): FAILED (failures=5)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

New head 54e5d2dc, test only: flush_compares in test_flush_constant_operands.py also counts a flush done on the bits (one exponent-field mask), so its structural checks keep holding once #5347 (my flush_f32 on the exponent bits) lands too. Checked on main + #5347 + this: the new test, test_flush_on_bits, test_valu_mul_legacy_denorm and test_valu_f32_min3_max3_mad pass. No translator change since df5bf139. AI-assisted.

@abnalstlll-byte abnalstlll-byte 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.

Clean translation-time constant FTZ in Gen.src_f_flushed: properly guards imm/fimm without abs/neg/sdwa_sel/sext, preserves -0.0 (0x80000000) and NaN payloads via bits &= 0x80000000 + self.tof(self.cu(bits)), and routes all six denormal-flushing sites (v_fma_mix_* f32 inputs, v_dot2_f32_f16 acc, v_mul_legacy_f32, v_mad_legacy_f32/v_mac_legacy_f32, v_mad_f32/v_mac_f32) cleanly without touching f16 operands. Tested locally: tests/semantic/test_flush_constant_operands.py 8/8 passed, git diff --check clean. No game files. AI-assisted review (Gemini).

@jdPhobos

Copy link
Copy Markdown

Tested 54e5d2d on AMD Radeon RX 7800 XT (RDNA 3, subgroup 32..64), AMD proprietary driver 26.11.2 (LLPC), Vulkan 1.4.359, Windows 11 x64: pass

  • py -I -m unittest discover -s tests/semantic -p "test_flush_constant_operands.py": Ran 8 tests, OK
  • rdna2spirv.py: 221/221 translated, 221/221 pass spirv-val
  • scripts/run_gpu_tests.py (default): 202/217 passed, 15 skipped (0 failed)
  • scripts/run_gpu_tests.py (LEONIDA_W64_EMULATE=1): 202/217 passed, 15 skipped (0 failed)

No game files.

@lunarminus

Copy link
Copy Markdown

Tested 54e5d2dc merged onto main d0835dd9 on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass

  • Merge: d0835dd9 is an ancestor of this PR's base 3faac557, so the merge fast-forwarded and the tree tested is 54e5d2dc itself. To separate this PR from main, I also ran an unmodified 3faac557 control.
  • New test: python -I tests/semantic/test_flush_constant_operands.py -v ran 8 tests, OK. The same file run against 3faac557's tools/rdna2spirv.py gave FAILED (failures=5). All five are in the structural checks (ConstantOperandsNeedNoRunTimeFlush), and the five value checks pass.
  • Related suites: test_valu_mul_legacy_denorm (4), test_valu_f32_min3_max3_mad (22), test_valu_fma_mix (15), test_valu_dot2_f16 (66), test_valu_mad_lit (15), test_alu_dot_pk_fmac (14), test_alu_madak_subrev (18), test_alu_f32_fma_clamp (14) and test_alu_dot2_f16_unsupported (2) are all OK, both with this PR and on 3faac557.
  • Full semantic suite (python -I -m unittest discover -s tests/semantic): 6,253 tests with the PR and 6,245 on 3faac557. Both runs ended with failures=3, errors=2, skipped=1, expected failures=1, and the 5 failing tests (3 failures, 2 errors) are the same test_loop_ballot_barrier ones in both.
  • GPU suite (scripts/run_gpu_tests.py, with LEONIDA_W64_EMULATE unset and with it set to 1), with the test programs built from d0835dd9 (the PR has no C++ change): 198/217 passed, 15 skipped in both modes.
    • Compared per test with d0835dd9 without the PR (196/210, 14 skipped), the differences are the same in both modes:
      • cs_bvh and cs_bvh32 went from PASS to FAIL.
      • Seven tests are not in that baseline: cs_smem_sconst_off, ps_far_branch, ps_texture_cl and ps_texture_b_cl PASS, ps_m2_literal SKIP, ps_sample_c and ps_sample_c_b FAIL (vk_texture_test usage message).
      • None of these come from this PR: the 3faac557 control with the same programs is also 198/217 and identical per test in both modes.
    • With test programs built from 3faac557: PR and control are both 202/217 passed, 15 skipped, and identical per test in both modes. The four FAILs above pass there, so they come from the older test programs.
  • SPIR-V reached by the GPU suite: of the 235 .spv files in the GPU work folder, only ps_f32_mods.spv differs between 3faac557 and this PR. That fixture passes with 4096/4096 pixels exact in every run.
  • AMD / 32-wide: nothing in the PR or its comments names a specific command for these GPUs, so I ran nothing beyond the above.

No game files used.

AI-assisted (Claude Code).

@timbot103

Copy link
Copy Markdown

Tested 54e5d2d on an AMD Radeon Graphics (Ryzen 9000 iGPU, RDNA2, subgroup 32..64), AMD driver 25.10.30.02, and an NVIDIA RTX 5080 (32-wide only), driver 616.64. Windows 11 Pro 26200, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8. Merged onto main 3faac55. The test programs come from 3faac55, since this PR changes no C++. I ran scripts/run_gpu_tests.py natively and with LEONIDA_W64_EMULATE=1, and compared each test's result and printed words with main 3faac55 on the same machine: pass.

  • Build: 221 shaders, 221 translated, 221 pass spirv-val, on the base and on the branch.
  • Radeon native: 201/217 passed, 16 skipped. Emulated: 201/217 passed, 16 skipped.
  • RTX 5080 native: 170/217 passed, 47 skipped. Emulated: 201/217 passed, 16 skipped.
  • In all four runs, every test had the same result and printed the same words as on main.

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

@kacper-serewis kacper-serewis 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 CPU integration review at 54e5d2d with #5347 e3934c0. Selected the first eight filename-sorted, previously validated cached shaders containing v_mad_f32 with both immediate and register operands. All 24 binaries across three builds passed Vulkan 1.2 spirv-val. Totals were 12,552,188 bytes on #5263, 12,278,432 on #5347 and 12,147,468 in the private combination. The combination retained #5263's immediate folding and used #5347's runtime flush implementations. Each build passed 16 independent exact-word CPU cases covering signed zero and the subnormal/normal boundaries. Size measurements apply to this selected slice.

mrp1no and others added 2 commits November 23, 2026 12:49
flush_f32 tests |x| < FLT_MIN at run time on every operand of v_mad_f32, v_mac_f32, the legacy multiplies, the
f32 operands of v_fma_mix_* and the accumulator of v_dot2_f32_f16. For an inline constant or a literal the bits
are known here, so Gen.src_f_flushed writes the flushed value itself: the constant, or a zero of its sign when it
is subnormal. An operand with a source modifier or an SDWA select still goes through flush_f32.

tests/semantic/test_flush_constant_operands.py checks the values with the constant in each slot, and that a
constant operand gets no run-time flush (main still emits one).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A flush is an FAbs and a compare with FLT_MIN today, and one exponent-field mask if flush_f32 tests the bits
(the follow-up SHADER item); count both so the structural checks hold with or without it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

#5263 (head 54e5d2d, open)

Tested 54e5d2d on Windows 11, AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010: pass

  • python -I -m unittest discover -s tests/semantic -p test_flush_constant_operands.py: 8 tests OK. With the translator from main 3faac55: FAILED (failures=5), the five structural checks, as the PR says.
  • LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py with test programs built from main 3faac55 (the PR's merge base): 202/217 passed, 15 skipped, 0 failed. Main 3faac55 on the same setup: 202/217, 15 skipped. Per-test status identical for all 217.
  • Of the 447 .spv files the run writes, one differs from main (ps_f32_mods, same size, 7076 bytes). It passes, 4096/4096 pixels exact, on both.
  • Not done: the corpus count (needs the game files), other GPUs.

AI-assisted (Claude Code).

@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 561b65a8 (now 9cabfcf2). One conflict in tools/rdna2spirv.py: main's new dst_f64 and this PR's src_f_flushed were added at the same spot, and both are kept. The six call sites are unchanged. No call site main added since goes through flush_f32(self.src_f(...)) on a source that could be a constant.

Both suites against a 561b65a8 baseline on the same machine:

  • pytest tests/semantic -n 10: no failures, the same as main.
  • pytest tests --ignore=tests/semantic -n 10: the same 8 failures as main, all outside the translator.

AI-assisted (Claude Code).

@mrp1no
mrp1no force-pushed the shader-flush-constant-operands branch from 54e5d2d to 9cabfcf Compare November 23, 2026 12:46
@itsonlysimbo

Copy link
Copy Markdown
Contributor

Tested 9cabfcf2 on RTX 4070, NVIDIA 621.14, Windows 11 (10.0.26300), Vulkan SDK 1.4.363, llvm-mc 23.1.6, test programs from main 561b65a8: pass.

AI-assisted (Claude Code).

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.