Skip to content

rdna2spirv: the f32 denormal flush tests the exponent bits, not |x| < FLT_MIN on the float (-2.9% SPIR-V) - #5347

Open
mrp1no wants to merge 1 commit into
ItsNotRexed:mainfrom
mrp1no:shader-flush-on-bits
Open

mrp1no wants to merge 1 commit into
ItsNotRexed:mainfrom
mrp1no:shader-flush-on-bits

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

New SHADER item. Claim: #5256 (comment)

Gen.flush_f32 is the f32 denormal flush 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 (SHADER-118, SHADER-298). Each operand, rounded product and sum gets it. It tested |x| < FLT_MIN on the float:

  • FAbs;
  • OpFOrdLessThan against FLT_MIN, which on main is itself an OpBitcast at each use;
  • an OpBitcast to uint for the sign;
  • the sign mask;
  • an OpBitcast back;
  • a float OpSelect.

That is about seven instructions per flush, and the flush is the largest single cost in translated modules: 772,627 FAbs on my 663-shader sample, nearly all of them flushes.

Change

Gen.flush_u(u) flushes the bits of an f32 with four integer ops: (u & 0x7F800000) == 0 ? u & 0x80000000 : u.

  • A zero exponent field (a zero or a subnormal) becomes the zero of its sign.
  • A normal, inf or NaN is unchanged.

This is the same result as before for every input. The float test sent zeros and subnormals to the signed zero (copysign(0, x) leaves a zero as it is), and inf and NaN compared false.

flush_f32(f) is now tof(flush_u(tou(f))), so no call site changes. Since nothing in the test is floating point, the driver's own f32 denormal handling in FAbs/compare, which Vulkan leaves open, no longer matters either.

Tests

  • New: tests/semantic/test_flush_on_bits.py (translator + spirv_mini_interp). It runs x * 1.0 + -0.0 through v_mad_f32 on the edges: FLT_MIN and −FLT_MIN stay, the largest subnormal of each sign becomes +0 / −0, 2^-149 becomes +0, ±0 and ±inf pass, and NaN stays a NaN. It also checks that the module has no FAbs or OpFOrdLessThan and has exactly five flushes (three operands, product, sum). With main's translator the structural check fails.
  • Adapted: two tests that looked for the float form.
    • test_valu_mul_legacy_denorm's flush count now counts exponent masks.
    • test_valu_f32_min3_max3_mad's sabotage check now turns exactly the flush selects (those whose condition is the exponent test) into pass-throughs, and still sees the subnormal cases change.
  • pytest tests/semantic -n 10: 6,246 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.
  • scripts/run_gpu_tests.py on Intel UHD Graphics 750 (Windows 11, LEONIDA_VK_DEVICE=Intel, LEONIDA_W64_EMULATE=1; the test programs are built from 98fa503f, since this PR changes no C++): 180/217 passed, 37 skipped, every test with the same result as main 3faac557 on the same setup.

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

main 3faac557 this PR
translated / pass spirv-val 663 / 663 663 / 663
function-body instructions 27,568,451 26,819,736 (−2.72%)
.spv bytes 521,677,524 506,703,992 (−2.87%)
modules changed 604 of 644 (−1.7% on the largest, up to −4.9%)

Other PRs

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

🤖 Generated with Claude Code

…he float

flush_f32 took FAbs, a compare with FLT_MIN, a bitcast to uint for the sign mask, a bitcast back and a float
select per flush. Gen.flush_u does the same on the bits with four integer ops: a zero exponent field (zero or
subnormal) becomes the sign bit alone, anything else (normal, inf, NaN) is unchanged. Integer ops only, so the
driver's own f32 denormal handling can't affect the test.

tests/semantic/test_flush_on_bits.py checks the edges (FLT_MIN, the largest subnormals of each sign, +-0, inf,
NaN) through v_mad_f32 and that the module has no FAbs or float compare. The two tests that looked for the float
form (the legacy multiply's flush count, the min3/max3/mad sabotage check) now look for flush_u's.

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

Copy link
Copy Markdown

Checked e3934c08 on an RTX 3080 Ti, NVIDIA 620.73.04, CachyOS. I translated one hand-written fragment shader with four independent v_mad_f32 channels computing x * 1.0 + (-0.0). Each draw supplied four new input bit patterns through the user SGPR push constants, so the driver saw the same shader binary for every case.

Across four 64×64 draws, all 65,536 output words met the oracle. The smallest and largest subnormals of both signs flushed to the corresponding signed zero. Both zeros, ±FLT_MIN, the next normal magnitude above FLT_MIN on each sign, and ±infinity matched bit for bit. For the four signed quiet/signaling NaN inputs I checked only that the arithmetic result was NaN.

Negative control: in the generated assembly I replaced the signed-zero arm of the 20 flush selects with unsigned zero, then reassembled and validated for Vulkan 1.2. The same draws produced exactly 12,288 mismatched words: the two negative subnormal cases and the negative zero case each lost their sign at every pixel. All other cases still passed. AI-assisted (OpenAI Codex).

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

Independent Windows compute review at e3934c08b36abb1fb37d3d848e4331ae6808182c, compared with unchanged main 3faac557b1584d39004357918e60325b4d4ff0fe. No blocking findings in the changed flush helper. This adds Radeon native-subgroup and main/head comparison evidence to the existing fragment-shader review.

CPU and source review

Reviewed all four changed files, including preservation of the exponent/sign bits and the revised sabotage test. The three changed test files pass: 29 methods and 1,115 subtests. The adjacent FMA-mix and dot2 tests pass: 81 methods and 91 subtests. Against main, the new test file's value/NaN checks pass and only its integer-structure check fails, as expected.

GPU controls

Two external, hand-written compute fixtures use the ordinary LLVM 21.1.8 gfx1013 -> translator -> spirv-val --target-env vulkan1.2 pipeline. Main and head receive identical assembly listings and raw uint input buffers. Each fixture launches 64 threads over 32 distinct input triples, repeated once, and stores the source bits back alongside the results. The 32/64 metadata agrees with the requested native subgroup width; no wave64 emulation is used.

Coverage: positive/negative smallest and largest subnormals, signed zeros, FLT_MIN and the next normal value, finite extrema, signed infinity, signed quiet/signaling NaNs, product/sum underflow, pre/post-output-modifier flushing, and the legacy zero-times-infinity/NaN rule. Result columns exercise v_mad_f32, v_mac_f32, legacy multiply/MAD/MAC, div:2, mul:2, source negation/absolute value, and a runtime-input FMA-mix identity.

Device and Windows driver Native subgroup Main PR head
NVIDIA RTX 4080, 610.62 (32.0.16.1062) 32 pass pass
AMD Radeon(TM) Graphics, 32.0.21043.5001 32 pass pass
AMD Radeon(TM) Graphics, 32.0.21043.5001 64 pass pass

Each of the six runs checks 462 bit-exact arithmetic results, 114 NaN-class results, and 256 raw input/tag echo words, plus unchanged input and padding regions. The independent arithmetic oracle uses exact rational arithmetic with binary32 round-to-nearest-even. NaN payload/sign after arithmetic are not constrained; finite values, infinity signs and zero signs are exact. Main/head readbacks are byte-identical within each device/mode pair.

All four production modules validate. Runs use the same unchanged main-source vk_compute_test binary, Windows 11, MSVC 19.44 and SDK/Khronos validation 1.4.363, with error/warning output captured. Zero VUIDs or validation errors in the six runs.

Negative control: replace only the signed-zero arms of the head module's 44 flushes with positive zero, reassemble and validate, then repeat the RTX 4080/32 run. The oracle catches 84 result mismatches, including loss of negative zero; source echoes and padding remain intact.

This is translated Vulkan execution at native subgroup widths, not native AMD ISA measurement. Dot2 has CPU coverage here, not an added GPU claim. No game files, corpus result, timing claim or production edits. AI-assisted.

@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 independent review. At e3934c0, all 200 randomly selected cached shaders (seed 5347) translated and validated on both revisions. The 185 modules containing changed flushes all shrank; the other 15 were byte-identical. Total SPIR-V fell from 127,224,340 to 123,669,916 bytes (2.79%). In each module additional integer flush sequences matched the decreases in both FAbs and OpFOrdLessThan, totaling 177,734 replacements. CPU validation on this slice.

@SirKingg

Copy link
Copy Markdown
Contributor

Independent NVIDIA run on the exact PR head e3934c08b36abb1fb37d3d848e4331ae6808182c (AI-assisted). Environment: Windows 11 Pro 10.0.26300, GeForce RTX 4070 Ti, NVIDIA driver 610.88; LLVM llvm-mc 23.1.6 and SPIRV-Tools from Vulkan SDK 1.4.363.

CPU translator checks (no game data): test_flush_on_bits.py 3/3, test_valu_mul_legacy_denorm.py 4/4, and test_valu_f32_min3_max3_mad.py 22/22 passed, including its existing negative control.

GPU: filtered runs assembled/translated four existing synthetic fixtures; all four passed spirv-val and rendered exact results: ps_legacy_mul, ps_mul_legacy_denorm, and ps_mul_legacy_denorm_mods each 4096/4096 pixels; ps_f32_mods 4096/4096 pixels. The latter covers v_mac_f32 with normal operands; the legacy fixtures exercise subnormal input/output flush and signed underflow through v_mul_legacy_f32.

Coverage limit: these existing GPU fixtures do not directly exercise v_mad_legacy_f32/v_mac_legacy_f32, v_fma_mix_f32, or the f32 dot2 path. Their GPU semantics are not claimed here; the relevant CPU semantic checks above passed. No performance or corpus-size claim.

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

Reviewed e3934c0

  • python tests/semantic/test_flush_on_bits.py -v -> 3 tests OK; python tests/semantic/test_valu_mul_legacy_denorm.py -> 4 OK; python tests/semantic/test_valu_f32_min3_max3_mad.py -> 22 OK.
  • Flush consumers: test_alu_dot_complete.py 9 OK, test_valu_fma_mix.py 15 OK, test_valu_dot2_f16.py 66 OK.
  • Full tests/semantic (pytest-xdist is not installed here, so no -n 10; ran the 397 files as 8 parallel pytest processes): 6,243 passed, 4 skipped, 1 xfailed, 5 failed. The 5 failures are the test_loop_ballot_barrier subtests, and -k loop_ballot on origin/main 3faac557 gives the same 5 SUBFAILED names (emulate_64/128/40_sg32, nested loops, bug4b).
  • Negative control: test_flush_on_bits.py on 3faac557 -> FAILED (failures=1), test_integer_ops_only ("FAbs unexpectedly found"), as the PR says; the edge-value tests pass there because the old flush computes the same values.
  • git diff --check clean.

flush_u is equivalent on every exponent class (zeros/subnormals -> signed zero, normals/inf/NaN unchanged), and the new test pins the 5-flush shape for one mad.

Verdict: OK to merge. git diff --check exit 0.

@sovljanskii

Copy link
Copy Markdown
Contributor

Apple M4 run of e3934c08 (on main 3faac557): pass, no regressions. Setup: macOS 27.2.1, MoltenVK 1.4.2, AppleClang 21.0.0, Homebrew LLVM 23.1.6, Python 3.15.2 and 3.12.16. The test programs are built from main (the PR changes no C++).

  • CPU: test_flush_on_bits, test_valu_mul_legacy_denorm and test_valu_f32_min3_max3_mad pass on both Pythons. Three mutations of flush_u each fail all three files:

    mutation failures (in the order above)
    no flush 3 / 10 / 134
    flush to +0, dropping the sign 2 / 1 / 5
    exponent mask 0x7F000000 3 / 4 / 61
  • Semantic suite (6,248 tests): the same 32 failures and 2 errors as main on this machine (the 29 arm64 NaN rows from spirv_mini_interp: pin the sign of a NaN made from non-NaN operands (semantic suite on arm64) #5208 plus test_loop_ballot_barrier). Nothing new fails.

  • GPU suite: 152/217 passed, 58 skipped, identical to main per test. That includes ps_legacy_mul, ps_mul_legacy_denorm, ps_mul_legacy_denorm_mods and ps_valu16_mad. With gpu test: cs_mul_legacy_denorm pins the default-MODE legacy multiply flush #5355 merged in, cs_mul_legacy_denorm gives 0 0 0 0 0 2147483648 0 0 with the bit flush, and --filter legacy passes 4/4.

  • Corpus (my own copy, shard 0/64, counts only):

    • 415/415 translated and spirv-val-valid on both sides.
    • 385 modules smaller, 30 byte-identical, none larger (at most −5.25%).
    • Total .spv bytes went from 320,512,756 to 311,286,672 (−2.88%), and FAbs lines in the disassembly from 476,066 to 14,737.
  • Apple note: MoltenVK 1.4.2 reports shaderDenormFlushToZeroFloat32 = false, shaderDenormPreserveFloat32 = false and denormBehaviorIndependence = NONE. With every flush removed from cs_mul_legacy_denorm (FAbs 32 → 0 in the module), the M4 still returns the same eight words. So this GPU flushes those f32 denormals itself, and an Apple run can't tell a missing flush from a correct one; the RADV and NVIDIA runs are the ones that check the flush.

AI-assisted.

@lunarminus

Copy link
Copy Markdown

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

  • The merge fast-forwarded. d0835dd9 is an ancestor of this PR's base 3faac557, so the tree I tested is the PR head itself. To isolate this PR, I also ran everything on 3faac557 without it as a control.
  • Semantic tests (PR tree): test_flush_on_bits 3/3, test_valu_mul_legacy_denorm 4/4 and test_valu_f32_min3_max3_mad 22/22 pass. The two adapted tests also pass on 3faac557.
  • Negative control: with 3faac557's translator, test_flush_on_bits fails only test_integer_ops_only; test_edges and test_nan_stays_a_nan pass.
  • Full semantic suite (python -m unittest discover -s tests/semantic): 6,248 tests with the PR, 6,245 on 3faac557. Both have the same 3 failures and 2 errors, all in test_loop_ballot_barrier, with the same test IDs.
  • Other translator tests, same result with and without the PR:
    • pytest tests/test_translate.py: 596 passed.
    • pytest tests/test_mode_round.py: 74 passed.
    • test_shader_ir 37, test_spirv_opt 9, test_fma_mix_probe 18, test_f32_fuzz 29 (1 skipped) and test_rcp_iflag_mods 5: all OK.
  • GPU suite, scripts/run_gpu_tests.py, with LEONIDA_W64_EMULATE unset and with =1. The PR changes no C++, so I used existing test programs.
    • Test programs built from d0835dd9: 198/217 passed, 15 skipped in both modes. The 4 FAILs are cs_bvh, cs_bvh32, ps_sample_c and ps_sample_c_b. 3faac557 with the same programs gives the same result for every test, in both modes.
    • Test programs built from a tree whose runtime/ is identical to 3faac557's: 202/217 passed, 15 skipped, 0 FAIL in both modes, identical per test to 3faac557.
  • Per-test comparison with d0835dd9 without the PR (196/210, 14 skipped):
    • With the d0835dd9 programs, cs_bvh and cs_bvh32 go from PASS to FAIL.
    • Seven fixtures that came to main after d0835dd9 (not added by this PR) are new. cs_smem_sconst_off, ps_far_branch, ps_texture_b_cl and ps_texture_cl pass, ps_m2_literal is skipped, and ps_sample_c and ps_sample_c_b fail.
    • With the fresh programs, none of the 210 shared tests differs, ps_sample_c and ps_sample_c_b pass, and ps_m2_literal is still skipped.
    • All of these differences appear identically on 3faac557 without the PR, so none comes from this change.
  • Flush fixtures: ps_legacy_mul, ps_mul_legacy_denorm, ps_mul_legacy_denorm_mods and ps_f32_mods are 4096/4096 pixels exact in all four PR runs.
    • Their translated SPIR-V has 16/16/16/5 OpBitwiseAnd … 0x7F800000 exponent tests and no FAbs or OpFOrdLessThan.
    • On 3faac557 the same modules have 16/16/16/5 FAbs and OpFOrdLessThan, and no exponent mask.
    • Op instruction lines in the .spvasm go from 466/460/482/406 to 449/445/467/401.
  • Not covered: like the earlier testers' runs, these GPU fixtures don't test subnormal inputs through v_mad_f32, v_fma_mix_f32 or the f32 v_dot2 accumulator directly.

AI-assisted (Claude Code).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested e3934c0 on Intel UHD Graphics 630 (CFL GT2): test_flush_on_bits, test_valu_f32_min3_max3_mad and test_valu_mul_legacy_denorm pass (29 tests), tests/test_translate.py passes (596), and run_gpu_tests.py with LEONIDA_VK_DEVICE=Intel gives 154/217 passed, 61 skipped, with FAIL cs_fmt_int_narrow and FAIL cs_signed_zero_nan. Running test_flush_on_bits with the translator tools from the base commit 3faac55 gives failures=1: test_integer_ops_only fails at assertNotIn(" FAbs ", spv).

AI-assisted.

@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@gertify thanks. The two GPU FAILs aren't from this PR: cs_fmt_int_narrow.spv and cs_signed_zero_nan.spv are byte-identical when translated by e3934c08 and by its base 3faac557 (run_gpu_tests.py --filter, the same flags). Neither shader goes through flush_f32. Both pass with either translator on an Intel UHD 750 (Windows, LEONIDA_VK_DEVICE=Intel), so on the UHD 630 they should fail the same way on 3faac557 too. If you have the output lines, they'd be worth a separate issue for that GPU.

AI-assisted (Claude Code).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested e3934c0 on AMD RADV RAPHAEL_MENDOCINO (Ryzen 9 9950X3D iGPU), subgroup 32..64: run_gpu_tests.py with LEONIDA_VK_DEVICE=RADV gives 202/217 passed, 15 skipped, 0 FAIL. test_flush_on_bits (3 tests), test_valu_f32_min3_max3_mad (22) and test_valu_mul_legacy_denorm (4) pass. Running test_flush_on_bits with the translator tools from the parent commit 3faac55 gives failures=1: test_integer_ops_only fails at assertNotIn(" FAbs ", spv). The shader corpus from the description was not run.

AI-assisted.

@dandasma

Copy link
Copy Markdown
Contributor

Tested e3934c08 (on main 3faac55) on an AMD Radeon RX 9070 (RDNA4, subgroup 32..64), AMD Windows driver 26.10.1, Windows 11, Python 3.12.10: pass. First AMD run, as far as I can see.

  • Full run_gpu_tests.py: 202/217, 15 skipped, the same tests and skips as main, both native and with LEONIDA_W64_EMULATE=1.
  • Against main's translation, four modules change: ps_f32_mods, ps_legacy_mul, ps_mul_legacy_denorm, ps_mul_legacy_denorm_mods. All four pass 4096/4096 in both runs, so the driver's f32 denormal handling no longer matters for them on AMD either.
  • test_flush_on_bits 3 OK, test_valu_f32_min3_max3_mad 22 OK, test_valu_mul_legacy_denorm 4 OK.

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.

10 participants