Skip to content

rdna2spirv: f32 constants are module-scope OpConstants, not a bitcast at each use (-2.4% SPIR-V) - #4391

Open
mrp1no wants to merge 5 commits into
ItsNotRexed:mainfrom
mrp1no:shader-float-constants
Open

mrp1no wants to merge 5 commits into
ItsNotRexed:mainfrom
mrp1no:shader-float-constants

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

New SHADER item. Claim: #4257 (comment)

Gen.fconst wrote every f32 constant as OpBitcast %float %c<bits> in the function body, at each use. On a corpus sample (133 shaders, 5.29 M body lines) that was 185,918 bitcasts, 3.5% of all body lines. 81% of them are the same FLT_MIN of flush_f32.

Change

  1. rdna2spirv. A finite normal f32 value, or +0, is now one module-scope OpConstant %float per value (%cf_<bits>), declared right after the module's uint constants.
  2. tests/semantic/test_module_float_constants.py.
    • Assembles 4,013 f32 patterns (edge values plus random normals) exactly as fconst writes them, and checks every constant word: through spirv_asm.py always, and through spirv-as when it is on PATH.
    • Checks which values are hoisted, and that nothing is hoisted after the freeze.
    • Checks that a translated module declares FLT_MIN once and still runs in the semantic interpreter.
  3. test_alu_sincos: its negative control sabotages 2*pi in the new form.

Measured on my own 1.0.2.1 corpus (counts only)

main aa2988cc this PR
shard 0/40, translated / spirv-val 663 / 663 663 / 663
shard 0/40, total SPIR-V 500.1 MiB 488.2 MiB (−2.4%), 623 modules smaller (median −1.7%, up to −4.2%)
20 largest modules 224 MiB 221 MiB

Every distinct constant the shard uses (13) assembles to its exact bits with both assemblers.

The saving is in bytes and ids. Drivers fold these bitcasts anyway, so this mostly shortens parsing and the module's id space. With #4345 (SHADER-771) the two together remove most of the per-flush overhead.

Tests

  • Semantic suite: 7,456 tests. The only failures are the ones main aa2988cc already has: test_loop_ballot_barrier (5), test_cmp_class_f64 (2) and test_mutations' legacy-multiply anchor (2, fixed by tests/semantic: re-anchor the v_mul_legacy_f32 mutation after the flush change #3883). test_alu_sincos' turn-scaling negative control looked for 2*pi as the uint literal; a third commit points it at the module-scope constant, and it still catches the sabotage.
  • GPU suite, RTX 3060 (NVIDIA 610.88, Windows 11), scripts/run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed. Same as main on the same machine.

Independent of #2298's optional optimizer pass. Counts only; no shader names or data from the corpus. AI-assisted (Claude Code).

🤖 Generated with Claude Code

mrp1no and others added 3 commits November 23, 2026 02:01
… at each use

Gen.fconst wrote every f32 constant as OpBitcast %float %c<bits> in the
function body at each use. On a corpus sample that was 3.5% of all body
lines (185,918 of 5.29 M over 133 shaders), 81% of them the FLT_MIN of
flush_f32.

A finite normal value or +0 is now one OpConstant %float per value,
declared with the module's uint constants and written as repr() of its
exact value: spirv-as reads decimal straight to binary32 and
tools/spirv_asm.py via binary64 (ItsNotRexed#3911), and both give these exact bits
because the value is exactly representable in both. -0, subnormals, inf
and NaN keep the bitcast. fconsts_frozen is set when the module's
constants are written, so an fconst after that falls back to the bitcast
instead of naming a constant nobody declares (the %c80 failure ItsNotRexed#4071
fixes).

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

test_module_float_constants.py assembles 4,013 f32 bit patterns (edge
values and random normals) as fconst writes them and checks every
constant word: through tools/spirv_asm.py always, and through spirv-as
when it is on PATH. It also checks which values are hoisted (normals and
+0, not -0, subnormals, inf or NaN, nothing after the freeze) and that a
translated module declares FLT_MIN once and still runs in the semantic
interpreter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The turn-scaling negative control looked for 2*pi as the uint literal
1086918619 (0x40c90fdb) that the per-use bitcast referenced. It is now the
module-scope constant %cf_40c90fdb = OpConstant %float <exact decimal>, so
the control finds that line and sabotages its value to 1.0. It passes with
this branch and still fails the sabotaged module; with main's translator
it fails at the lookup, as it should.

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

@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 b8548bbb on Windows 11: tests/semantic/test_module_float_constants.py -> 4 tests, OK; tests/semantic/test_alu_sincos.py -> 30 tests, OK; git diff --check clean.

The translator now emits f32 constants as module-scope OpConstants instead of a bitcast at each use; the sincos test's sabotage was updated to the constant form, and both suites pass. No blocking issue.

@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 review at b8548bbb. Scratch merging #4391 and #4345 (630a57d4) onto 03b09a08 in either order produced tree 7c0833495530dfbfce6f91f2165bb6253ade8d6a.

A synthetic pixel module with 770 repeated legacy MUL/MAD/MAC operations measured 809,508 bytes on main, 747,956 with #4391, 649,076 with #4345, and 599,876 combined (−25.90% for this fixture). Both assembler outputs agreed except for the generator word and all validated for Vulkan 1.2. All four trees returned the same interpreted output words, including the negative underflow zero and positive legacy zero. The combined focused semantic suites passed 44/44, and the 4,013-pattern checks passed with both assemblers.

An injected late fconst call confirms the freeze fallback when the uint backing constant already exists. A deliberately fresh late 7.25 still references a missing uint constant. This was a scratch-only injection, not an observed input regression. No GPU or corpus measurements.

@MKmajster

Copy link
Copy Markdown

Tested b8548bb on NVIDIA GeForce RTX 3070 (Ampere, 32-wide), driver 617.42, Windows 11 24H2. No result changes against main aa2988c, with only tools/ differing on the same runtime build:

  • run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed on both. All 210 result lines are identical.
  • valu_fuzz --family f16 --wave 32 and --family mem --wave 32 (seed 13, 150 programs each): 150/150 match on both.
  • Size: the suite's translated fixtures total 6,523,016 bytes of SPIR-V here vs 6,542,984 on main (−0.3%). These are small hand-written shaders with few f32 literals, so the corpus number in the description is the meaningful one.

No game files. AI-assisted (Claude).

@mrbean56

Copy link
Copy Markdown

Tested b8548bbb8f on Linux (Debian 13), Python 3.13.5, SPIRV-Tools v2026.4 built from release tag ef96ed7: pass.

  • python -I tests/semantic/test_module_float_constants.py: 4 tests OK. The 4,013-pattern sample (edges + random finite normals) assembles to the exact bit pattern through both tools/spirv_asm.py and spirv-as; the negative-zero, subnormal, Inf and NaN patterns stay as bitcasts.
  • python -I tests/semantic/test_alu_sincos.py: 30 tests OK, including the re-pointed turn-scaling negative control.
  • End to end, a synthetic PS using v_sin/cos/sqrt/rsq/rcp/log/exp/fract_f32 hoists %cf_00000000 = OpConstant %float 0.0, %cf_3e800000 0.25, %cf_3f000000 0.5 and %cf_40c90fdb 6.2831854820251465; the module passes spirv-val --target-env vulkan1.2 v2025.1 and the excluded values still use OpBitcast in the body.

No GPU, no game data. AI-assisted.

@bobcatchris

Copy link
Copy Markdown
Contributor

Apple M2 no-regression check at head b8548bb (macOS 26.4, MoltenVK 1.4.2 direct, subgroup 4..32, llvm-mc 23.1.6, clean full build on AppleClang 17).

python -I scripts/run_gpu_tests.py --build runtime/build/linux: 213 shaders translated, 213 pass spirv-val; 127/210 passed, 76 skipped, 7 failed — identical to upstream/main aa2988c on this M2. The seven failures are the known MoltenVK expected set listed in PR #2909, none introduced by this change. Head fetched read-only, no rebase/edit/push. Synthetic shaders and counts only; no game files.

AI-assisted.

@TG0000 TG0000 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared b8548bbb with aa2988cc on Windows 11 build 26300, AMD Radeon RX 6800 (AMD 26.11.2 / 32.0.21045.29003, Vulkan 1.4.315), AMDGPU llvm-mc 23.1.5 and SPIRV-Tools v2026.4. Both runs used the same standalone Vulkan harnesses.

  • Native scripts/run_gpu_tests.py --require-pass: all 214 synthetic modules translated and passed spirv-val on both commits. Both runs reported 195 passed, 15 skipped and 0 failed. All 83 retained raw readbacks were byte-identical.
  • For the 214 indexed synthetic modules, total SPIR-V went from 6,541,344 to 6,521,376 bytes (−0.31%). 40 modules were smaller. This is a size measurement for the synthetic suite.
  • test_module_float_constants.py and test_alu_sincos.py passed all 34 tests, including the comparison against the native assembler.

AI-assisted (OpenAI Codex). Synthetic inputs only.

@Proudnoob4

Copy link
Copy Markdown
Contributor

Independent RTX 4080 check (Windows 10 22H2, driver 621.01): no result changes between 03b09a08 and b8548bbb, with the same Vulkan test binaries on both sides.

  • Both runs translated and validated all 214 GPU fixtures. Each full run exited 1 with 163 passes, 46 skips, and one setup error: ps_ssharp couldn't launch vk_sampler_test.exe (WinError 2).
  • After building that helper, the filtered ps_ssharp run passed at both revisions: eight cases, 4,096 pixels each, exact. Across the full run and filtered retry, that's 164 passes and 46 skips per revision. I did not rerun the full suite after building the helper.
  • At the PR head, test_module_float_constants.py (4 tests) and test_alu_sincos.py (30 tests) both passed.

This checks the GPU fixtures, not whole-corpus pipeline creation or execution.

@theswiss24

Copy link
Copy Markdown

Tested b8548bbb merged onto main 03b09a08 (clean) on two NVIDIA GPUs, test programs built from main 03b09a08: pass on both.

GPU driver, OS llvm-mc run_gpu_tests.py
RTX 4070 (Ada) 621.24, Windows 11 21.1.8 195/210 passed, 15 skipped, 0 failed
RTX 2060 SUPER (Turing) 580.178.04, Ubuntu 24.04 20.1.2 195/210 passed, 15 skipped, 0 failed

Every test has the same result as on main on both cards. LEONIDA_W64_EMULATE=1 on both. I haven't run a corpus comparison of the size change. No game files.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

@tupacfan04

Copy link
Copy Markdown
Contributor

APPROVE (b8548bbb) with nits, merge clean. New tests 4/4, test_alu_sincos 30/30, 159+53 targeted float tests, regen_fixtures --check and compileall clean. The full semantic suite has 7 failures/2 errors, but replaying those modules on base gives identical failures, so they are pre-existing. Negative control: on base the new tests fail (3/3 plus the reworked sin/cos sabotage).

Dedup works: one declaration per bit pattern, 4,013 decimal patterns round-trip through both assemblers, a translated PS validates, and the freeze is safe (no fconst calls after tools/rdna2spirv.py:12208).

Nits: no blank line between fconst_value and dconst (:5022); and +0/0.5/1.0 duplicate existing %f0/%fhalf_g/%f1 (:12131,12244) - e.g. both %f0 ... 0 and %cf_00000000 ... 0.0 appear, slightly undercutting the "one per value" claim.

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

…fhalf_g

Every module already declares %f0 and %f1, and %fhalf_g when uses_fhalf is set, so a hoisted %cf_ of the same value
was a second declaration of it (@tupacfan04's review). fconst now returns those, and the translated-module test
checks that no two float constants have the same value. Also the missing blank line before dconst.

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

Copy link
Copy Markdown
Contributor

Two unit tests under tests/ (not tests/semantic/) fail with this PR and pass on main 03b09a08. They match the SPIR-V text of the scale constant, which is now %cf_<bits> instead of OpConstant %uint <bits> + OpBitcast:

test main 03b09a0 merged with b8548bb merged with d1d9a2f
python -I tests/test_f64_conversion.py OK (1 test) FAILED FAILED (failures=4)
python -I tests/test_rcp_iflag_mods.py OK (5 tests) FAILED FAILED (failures=14)

Every failure is a missing pattern, for example:

tests/test_f64_conversion.py:45  Missing SPIR-V instruction: OpConstant %uint 1073741824   (mul:2; also 0x40800000 mul:4, 0x3F000000 div:2)
tests/test_rcp_iflag_mods.py:72  Missing SPIR-V instruction: OpConstant %uint 1073741824   (and OpBitcast %float %c3f000000 / %c40800000)

So the result itself isn't wrong: both tests need the same update test_alu_sincos.py got, i.e. to look for %cf_40000000 = OpConstant %float 2.0 (or %fhalf_g for 0.5 after d1d9a2f). The full semantic suite only shows main's existing 7 failures and 2 errors, the same names as on main, which is probably why the earlier runs missed these two.

Everything else here matches main on an RTX 4060 Ti (Ada, 32-wide), NVIDIA 621.24, Windows 11, MSVC 2022, merged onto 03b09a08: ctest 154/154; run_gpu_tests.py 214/214 translated and pass spirv-val, 164/210 passed and 46 skipped with LEONIDA_W64_EMULATE=0, and 195/210 passed and 15 skipped with =1. Zero failures in both modes, the same as main.

AI-assisted (Claude Code); I ran the tests and read the output.

@Bushyon

Bushyon commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested d1d9a2f on AMD Radeon RX 7800 XT (RDNA3), RADV Mesa 26.2.6 (ACO), Vulkan 1.4.354, subgroup 32..64, Fedora 44; llvm-mc 22.1.8, GCC 16.2.1. First AMD run. No GPU result changes against the base aa2988cc translator on the same runtime build (b8548bb gave the same):

test_f64_conversion and test_rcp_iflag_mods looked for an output modifier's scale as `OpConstant %uint` + `OpBitcast
%float`, and test_gather4_l_literal_lod_bounds_feed_mip_selection did the same for its MIN_LOD/MAX_LOD bounds. fconst
now declares them as `OpConstant %float` with repr() of the value (%fhalf_g for 0.5), so they look for that
(@troythedecoyx's review). tests/ outside tests/semantic now fails exactly as main does.

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

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@troythedecoyx Thanks, you're right: I had only run tests/semantic on this branch. Fixed in the latest commit.

  • tests/test_f64_conversion.py and tests/test_rcp_iflag_mods.py now look for the scale as OpConstant %float <repr of the value>. That covers %cf_40000000 … 2.0, %cf_40800000 … 4.0 and %fhalf_g … 0.5.
  • I then ran the whole of tests/ outside tests/semantic and found a third test of the same kind, tests/test_translate.py::test_gather4_l_literal_lod_bounds_feed_mip_selection. Its MIN_LOD/MAX_LOD bounds 0.25 and 0.75 are now module-scope constants, and the test looks for them that way.
  • pytest tests --ignore=tests/semantic -n 10 (Windows 11, Python 3.15.2) now fails exactly as main does: the same 8 failures on both trees (test_build_llvm_mc_batch, test_meta_to_xml_paths, test_check_no_game_data, test_u16_high_half, each with its own open fix).

@tupacfan04 your two nits are in d1d9a2f9. +0, 1 and 0.5 are now %f0, %f1 and %fhalf_g, and the translated-module test checks that no two float constants share a value. The missing blank line is fixed too.

AI-assisted (Claude Code).

@Proudnoob4

Copy link
Copy Markdown
Contributor

Whole-corpus A/B on my 1.0.2.1 copy (Windows 10, Vulkan SDK 1.4.363.1). This is for the earlier head b8548bbb against 03b09a08; it does not cover the later translator change in d1d9a2f9.

  • All three runs translated 26,503 inputs: base with spirv-as, PR with spirv-as, and PR with --assembler python. All three validated exactly the same 26,422; the other 81 were already invalid at the base. I checked the eight shard markers and indexes per run against the merged indexes and actual module sets.
  • On the 26,422 matched modules, spirv-as output fell from 17,881.2 to 17,454.9 MiB (−2.38%). 24,579 modules shrank, none grew, and 1,843 were byte-identical. The 20 largest by baseline size went from 227.1 to 223.8 MiB.
  • On shard 0/40, all 663 inputs validated on both sides, and size fell from 500.1 to 488.2 MiB (−2.39%).
  • At b8548bbb, every one of the 26,422 validated binaries matched byte-for-byte between the two assemblers except for the generator word (0x70000 vs 0x0).

These are translation, validation, and binary-size measurements, not pipeline compile time or runtime performance.

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.