Skip to content

rdna2spirv: a bitcast back to a value's original type is the original id (-1.4% SPIR-V) - #4510

Open
mrp1no wants to merge 3 commits into
ItsNotRexed:mainfrom
mrp1no:shader-bitcast-round-trips
Open

mrp1no wants to merge 3 commits into
ItsNotRexed:mainfrom
mrp1no:shader-bitcast-round-trips

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

New SHADER item. Claim: #4421 (comment)

Gen.tof/Gen.tou emitted a second OpBitcast to turn a value back into the type it had just been bitcast from. It happens when a register is read as float and then flushed (flush_f32 reads the float's bits), when a float result is stored back, and so on. On a 133-shader corpus sample, bitcasts are 14% of all body lines, and 1.9% are such round trips.

Commits

  1. rdna2spirv: Gen.bitcast_from maps each tof/tou result to its source, and the reverse bitcast returns that source.

    • This is always valid SSA: wherever the result is usable, its source is too, because the result's own definition uses it.
    • The keys are only ids that tof/tou just made, never module-scope constants, so nothing crosses a function.
  2. tests/semantic/test_bitcast_round_trip.py checks two things:

    • the helpers return the source without emitting a reverse bitcast;
    • a translated module with a v_mad_f32 and a v_mul_legacy_f32 has no OpBitcast whose operand is a bitcast from the same type.

    main's translator emits five such round trips in that module, and the test fails there.

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

main 03b09a08 this PR
translated / spirv-val 663 / 663 663 / 663
total SPIR-V 500.1 MiB 492.9 MiB (−1.4%), 624 modules smaller (median −1.0%, up to −2.6%)

Semantic suite: 7,458 tests. The only failures are the ones main already has: test_loop_ballot_barrier (5), test_cmp_class_f64 (2), and test_mutations' legacy anchor (2, fixed by #3883).

Independent of #4345, #4391, #4477 and @SweatierKey's %active reuse item. Each one removes a different redundancy, so the savings add up. AI-assisted (Claude Code).

🤖 Generated with Claude Code

mrp1no and others added 2 commits November 23, 2026 02:40
Gen.tof/Gen.tou emitted a second OpBitcast to turn a value back into the
type it was bitcast from: a register read as float and then flushed
(flush_f32 reads the float's bits), a float result stored back, and so on.
On a 133-shader corpus sample bitcasts are 14% of body lines, and 1.9%
are such round trips.

Gen.bitcast_from maps each tof/tou result to its source, and the reverse
bitcast returns the source. Always valid SSA: wherever the result is
usable its source is too, since the result's own definition uses it.
Keys are only ids tof/tou just made, never module-scope constants.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
test_bitcast_round_trip.py checks that tof/tou return the source for the
reverse bitcast without emitting one, and that a translated module with a
v_mad_f32 and a v_mul_legacy_f32 has no OpBitcast whose operand is a
bitcast from the same type. main's translator emits five such round trips
in that module.

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 7ea906d5 on Windows 11: tests/semantic/test_bitcast_round_trip.py -> 2 tests, OK; compileall and git diff --check clean.

tof/tou now reuse the original id when a bitcast returns to the source type, and the new cases verify no round-trip bitcast is emitted; the source's definition dominates every later use, so no dominance is lost. No blocking issue.

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Automated test: 7ea906d5 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: 2 passed.

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

@Atomic258

Copy link
Copy Markdown

Tested 7ea906d543 on Linux, Python 3.10, CPU only, synthetic fixtures, no game files: pass.

  • python -m unittest discover -s tests/semantic -p test_bitcast_round_trip.py → 2/2 pass.
  • Negative control on main: 2/2 fail (no bitcast_from round-trip), so the tests discriminate.
  • Read: tou(tof(u))/tof(tou(f)) return the original id; dominance argument holds since the bitcast's own definition uses the source. No behavior change beyond fewer OpBitcasts.
  • Full semantic suite not run here (LLVM/spirv-tools absent); translator unit path only.

AI-assisted (Devin).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 7ea906d merged with main 03b09a0 on Linux (WSL2), Python 3.12.16: pass. tests/semantic/test_bitcast_round_trip.py 2 tests OK. The whole tests/semantic directory (366 files, one unittest discover -p run each) gives 362 OK; the 4 that fail (test_cmp_class_f64, test_loop_ballot_barrier, test_vcmp16_opsel, test_mutations) fail with identical counts on main (2 / 3F+2E / 1E / 2F), so they are not from this change. Negative control: tools/rdna2spirv.py restored from main with the PR test kept -> FAILED (failures=2). Not run: spirv-val over the translated output. Diff read, one observation rather than a bug: bitcast_from is not keyed by type, so tof(tou(x)) returns x as is. Two existing callers feed tou an %int value (self.tou(self.i16_half(...)) in the v_mad_i16 path and the d16 tbuffer path); there the result is used directly and never goes back through tof, so nothing breaks today, but a future tof(tou(int_id)) would hand back an %int id as a float.

@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 independent Windows CPU check of 7ea906d5 against 03b09a08, plus a clean scratch merge with #4473 aa813789. uv CPython 3.12.13, Vulkan SDK 1.4.363.0, synthetic fragment shaders only.

  • All 1,006 cases that translated had bit-identical mrt0 output across the three trees, including signed zero, denormals, infinities and NaN payload inputs. All three rejected the same 18 multiply-with-clamp cases at the existing undefined FClamp check.
  • A separate 16-module sample assembled and passed spirv-val --target-env vulkan1.3 in all three trees. OpBitcast count fell from 456 to 399 in both the PR and the combined tree.
  • The existing %int inputs to tou() in signed d16 packing and v_mad_i16 feed integer consumers. I found no current route from those result IDs to tof(). A future such route would need to preserve the float destination type instead of reusing the signed source ID.

@mrbean56

Copy link
Copy Markdown

Tested 7ea906d543 on Linux (Debian 13), Python 3.13.5: pass, and the guard has teeth.

  • pytest tests/semantic/test_bitcast_round_trip.py -q: 2 passed.
  • Mutation: removing the if u in self.bitcast_from: return self.bitcast_from[u] early return from Gen.tou makes test_helpers_return_the_source fail, so the unit half pins the round trip.
  • Non-vacuous: the translated PS module contains 33 OpBitcast and zero chained ones, so back == [] is a real observation, not an empty set. mrt0_words still returns the 4 expected words.
  • The SSA argument in the docstring holds: the source dominates every use of the cast's result, so substituting it cannot create a use before definition. bitcast_from is populated on both directions, so tof(tou(u)) also returns u.

No GPU, no game data. AI-assisted.

tou remembers its argument so that tof can hand it back, which is only right when that argument is a %float.
v_mad_i16 and the signed d16 buffer store passed the %int halves from i16_half; nothing bitcast those results back
yet, but a later tof would have been given the %int as a float (@whattheflop's review). They now use int_bits, a
plain bitcast with no memo, and tof/tou say what they take. The new test checks the type of every tof/tou argument
in a module with v_mad_i16 and fails without the change; a corpus sample (133 shaders, 716,716 calls) already held it.

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

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@whattheflop Thanks, good catch. Fixed in 59dd5ceb:

  • v_mad_i16 and the signed d16 buffer store now turn their %int halves into bits with a new Gen.int_bits. It is a plain bitcast with no memo, so tof can never be handed an %int.
  • tof/tou now document their argument types.
  • A new test, test_tou_takes_a_float_and_tof_a_uint, wraps both helpers while translating a module with v_mad_i16 (with and without op_sel and clamp), v_mad_u16 and v_mad_f32. It checks the defined type of every argument.
    • It fails without the change: six ('tou', 'int').
    • On my 1.0.2.1 sample (133 shaders, counts only) all 716,716 tof/tou calls already had the right type, so the output is unchanged.
  • The semantic suite shows only main's known failures, plus test_vcmp16_opsel's spirv-val step, which fails identically without this commit because my shell lacks the tool on PATH.

AI-assisted (Claude Code).

@tylerstraub

Copy link
Copy Markdown
Contributor

Replying to @mrp1no.

Confirmed the signed D16 buffer-store caller in 59dd5cebf411c6af255d5ba6de8cf1191fc60b1b. I exercised the new test’s bitcast_arguments probe with buffer_store_format_d16_x v1, off, s[4:7], 0 and a runtime descriptor. The parent translator (7ea906d5) passed an %int to tou once in 14 calls. On the fixed head all 13 calls had the declared argument type. This store listing would also work as a regression case alongside the existing arithmetic listing.

For the GPU check I set the descriptor to 32_SINT and stored from v1 = 0x4321fffd. Both translators produced Vulkan 1.2-valid modules. On Linux RTX 5090 (595.99.02), both wrote 0xfffffffd (−3), and all 63 other buffer words retained the fill value. The helper trace distinguishes the fix even though the GPU output for this store was already correct.

AI-assisted.

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