Skip to content

rdna2spirv: take a v_fma_mix literal from the printed encoding when only the text is cut (SHADER-FMA-MIX-TEXT-CUT-LITERAL) - #2142

Merged
aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
DadaCap:mix-literal-text-cut
Nov 26, 2026
Merged

aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
DadaCap:mix-literal-text-cut

Conversation

@DadaCap

@DadaCap DadaCap commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-FMA-MIX-TEXT-CUT-LITERAL (#1608 (comment))

Depends on: nothing

What and why

llvm-mc 21.1.8 can print a v_fma_mix* literal cut to its low 16 bits in the text only, while the printed encoding keeps all 32 bits. With the test's own values: the stored literal is 0x40004200 (low half 3.0, high half 2.0), the text says 0x4200, and the printed encoding ends in 00 42 00 40.

  • The printed-encoding check (same_encoding) passes, because the printed bytes are the stored ones.
  • So the existing repair, mix_literal (for an encoding that is cut too), never runs.
  • The text alone reads the high half as +0.0: an f16 source that should read 2.0 reads 0, and an f32 source reads a denormal instead of its value.

restore_text_cut_mix_literal takes the literal from the printed encoding when the text names only its low half and the high half is not zero. It runs on both the clean-listing path (check_printed_encodings) and the re-anchoring path (listings with undecodable words). The same restore_mix_literal as before gives the 32 bits to every source the bytes encode as the literal.

Counts over 1.0.2.1 (one work folder's listings, llvm-mc 21.1.8)

Listings Instructions Programs
Pipeline library 47,910 1,218
FidelityFX FSPX 6 1
The game's own shaders 0 0

The FSPX program is the upscaler's history pass: one resampling weight lost its high half, so the weights summed to more than 1 and the history grew each frame.

Measured in the game

That one upscaler program retranslated with this change; RTX 5080, NVIDIA 621.24, Windows 11, a local tree with other open work:

  • Before, the 1080p output saturated at 64,000 and its history filled with infinities (target dump), and every displayed frame turned white after about 12 world frames.
  • After, no white frame in 13 captures of the run.

That run had other local changes too, so it is not a clean A/B. The whole library retranslated with this change: 2,479 of 2,479 programs translate as before, none lost.

Tested

  • tests/semantic/test_fma_mix_literal.py: new test_text_cut_but_printed_encoding_whole (clean and re-anchoring paths). It fails without the change (6.0 instead of 8.0) and passes with it; the file's other 18 tests pass.
  • Whole translator suite: 493 files, 6,319 tests, 0 files failed (24 jobs, Windows 11).
  • tests/test_docs_*.py, scripts/check_no_game_data.py on the changed files.
  • The retranslated upscaler module holds the full literal and passes spirv-val.

GPUs tested: NVIDIA GeForce RTX 5080, driver 621.24, Windows 11 (the game run above; the hand-written GPU tests were not run)

AI-assisted: yes (Claude Code, Claude Opus 5.5 wrote the change, the test and this text)

Checklist

  • Based on current main and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

…nly the text is cut (SHADER-FMA-MIX-TEXT-CUT-LITERAL)

llvm-mc 21.1.8 can print a v_fma_mix* literal cut to its low 16 bits in the text while the printed encoding keeps
all 32 bits. The printed-encoding check then passes, so mix_literal (for a cut encoding) never runs and the text
alone reads the high half as +0.0. restore_text_cut_mix_literal takes the literal from the printed encoding, on the
clean and the re-anchoring paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwRYcpySx4P4ncQ3wD5
@ecox982745

Copy link
Copy Markdown

On an RTX 4090, the parent rendered 8/24 synthetic draws correctly and this head rendered 24/24. I compared d3b67a704 with a449806dc on Windows 11, NVIDIA 616.64. All 24 fragment modules per revision passed spirv-val --target-env vulkan1.2. Each draw covered 64 × 64 pixels in an RGBA32 float target. Every channel of every pixel matched the reference on the head.

Input Parent readback PR readback
Packed literal, low half 3 and high half +2, computing 3 × 2 + 2 6 8
Same operation, high half −4 6 2
Full f32 source 3.0, text shows its low half as zero, multiplied by 2 0 6
Literal's low half spells 1 beside a separate inline 1, with the literal's high half selected 0 1

I exercised the first two rows on all three destinations, v_fma_mix_f32, v_fma_mixlo_f16 and v_fma_mixhi_f16, converting the selected f16 result back to f32 for readback. Each input went through both the clean-listing and re-anchoring paths. The zero-high-half and full-text controls passed on both revisions.

To reproduce the inputs, I extended the PR's test_fma_mix_literal.py fixture with the same short printed operand and full literal bytes in both the printed encoding and synthetic blob. This isolates the translation error on a second GPU. Whether the change alone fixes the white world frames still needs a controlled game run.

AI-assisted with OpenAI Codex (gpt-6.2-astra).

@DadaCap

DadaCap commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@ecox982745 thank you for running it on a GPU: 24/24 against 8/24 is the check I could not do here. AI-assisted (Claude Code, Claude Opus 5.5).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Verdict: ready to merge. This agrees with JordanWebDev's approval and ecox982745's GPU run. It adds a check with llvm-mc 21.1.8 itself, two mutations and a corpus sample. Three suggestions below, none blocking.

Setup: PR head a449806dc (checked again just before writing), on its base d3b67a704. Main is a73a57e13, 146 commits later. I merged main into the head locally (992543f60, no conflicts, no push). The merge differs from main only in the PR's 3 files. Windows 11 Pro 10.0.26300, Python 3.14.0. llvm-mc --version through WSL: Ubuntu LLVM version 21.1.8 (/usr/bin/llvm-mc-21, and the same version for LLVM_MC, which points at /usr/lib/llvm-21/bin/llvm-mc). SPIRV-Tools v2026.4 from Vulkan SDK 1.4.363.0. The tests run on the CPU SPIR-V interpreter; no GPU was involved. I ran nothing in the game.

Tests

  • tests/semantic/test_fma_mix_literal.py on main: 18/18 pass. On the merge: 19/19 pass.
  • Neighbouring files, same result on main and on the merge: test_printed_encoding 8/8, test_reanchor_llvm_offsets 3/3, tests/test_rdna2spirv_decode.py 25/25.
  • tests/test_docs_*.py on the merge: 13 files, 52 tests, all pass.
  • tests/semantic/test_lds_src2.py fails on main and on the merge alike (5 tests, 63 failures): llvm-mc reports No such file or directory for the test's temporary input file. My llvm-mc runs through WSL and cannot see the Windows temp folder. This is unrelated to the PR, and test_lds_src2: feed llvm-mc through stdin, so a WSL LLVM_MC can read it (TEST-LDS-SRC2-MC-STDIN) #1733 fixes it.
  • I did not run the whole translator suite.
  • scripts/check_no_game_data.py on the 3 changed files: exit 0, no output.

llvm-mc 21.1.8 check of the fixture. I gave the test's 12 stored bytes (01 60 20 cc ff 04 fe 0b 00 42 00 40) to llvm-mc 21.1.8, for -mcpu=gfx1013 and gfx1030. Both print the text v_fma_mix_f32 v1, 0x4200, v2, 0x4200 op_sel:[0,0,1] op_sel_hi:[1,0,1], and the printed encoding ends in 0x00,0x42,0x00,0x40. So the shape the new test builds (listing(STORED)) is exactly what this llvm-mc prints. I then fed llvm-mc 21.1.8's own text and encoding for 5 hand-made words into the translator, on both listing paths (10 runs):

Words (v2 = 2.0) Expected Main PR
The fixture: low half 3.0, high half 2.0 8.0 6.0 8.0
High half −4.0 2.0 6.0 2.0
src0 read as f32 (0x40490fdb), src2 the high half 8.425764 0.0 8.425764
neg on src0 −4.0 −6.0 −4.0
Literal in src2 only 2.0 0.0 2.0

The PR matches all 10 runs; main matches none. The f32 row confirms the doc's "an f32 source a denormal": llvm-mc prints it as 0xfdb.

Mutation

  • The PR's test file against main's tools/rdna2spirv.py: 1 of 19 fails, test_text_cut_but_printed_encoding_whole, at tests/semantic/test_fma_mix_literal.py:83: Lists differ: [1086324736, ...] != [1090519040, ...], which is 6.0 instead of 8.0.
  • Only the call in check_printed_encodings removed (tools/rdna2spirv.py:18054 in the merge): the same test fails at :83.
  • Only the call in the re-anchoring loop removed (:18204): the same test fails at :84. So each call site is pinned by its own assertion.
  • Each of the guards in restore_text_cut_mix_literal (:17669 and :17672) replaced by an always-true condition: 19/19 still pass. See suggestion 2.

Corpus sample (local only, counts only)

  • My own count over the local listings finds the PR's numbers exactly: 47,910 instructions in 1,218 pipeline-library listings, 6 in 1 FSPX listing, 0 in the game's own 25,734 listings. Their headers do not record the llvm-mc version, but the shape is the llvm-mc 21.1.8 one above.
  • A 1-in-9 sample of the pipeline library (--shard 0/9, 299 listings), translated with main and with the merge. Both: 298 translated, 298 pass spirv-val. The 1 left is the same s_sethalt refusal on both.
  • 132 modules change. The sample has exactly 132 listings with a text-cut literal, and they are the same 132. No other module changes, and no module is gained or lost.
  • All 29 FSPX listings with both trees: 29/29 translate and pass spirv-val. 1 changes, the one with the 6 instructions.
  • I did not check the new values one by one in the corpus modules. The 5 probes above check the values.

Safety

  • The new function looks only at the printed encoding. It acts only on a 12-byte VOP3P (enc[3] == 0xCC) v_fma_mix* whose literal has a non-zero high half, and only when a source in the text is an immediate equal to the low half.
  • restore_mix_literal changes only sources whose SRC field is 0xff. So an inline constant that happens to equal the low half (for example a 0 next to a literal whose low half is 0) is left alone.
  • An 8-byte print (LLVM 21's inline-constant form, mix_inline_literal) returns at once (len(enc) != 12). The later mix_literal and mix_inline_literal repairs still run after it and work from the blob, so they win when the bytes differ.
  • A text that already shows the full literal, or LLVM 23's lit(V), does not match the low-half test. Setting the same 32 bits again would be harmless anyway.
  • One literal per instruction on gfx10, so "several literals" cannot happen.

Suggestions (non-blocking)

  1. The new docs/design.md text (line 316) says "A third shape needs no blob". The function needs no blob, but it is only called from check_printed_encodings after its early returns (tools/rdna2spirv.py:18039, :18042, :18045). So a clean listing with no fssm/ or fspx/ blob next to it, or a header-only blob, still reads the high half as +0.0. I checked: listing(STORED) with no blob gives 6.0 on the PR. The re-anchoring path without a blob fails with FileNotFoundError on main too. Either move the call before those returns, or change the doc to say it needs the printed encoding but runs only where the blob check runs. The game's pipeline has the blobs, so this does not affect the counts above.
  2. No test pins the guards in restore_text_cut_mix_literal. Replacing each with True keeps 19/19 passing. One refused case would pin the text-match guard: a v_fma_mix whose text shows an immediate that is not the literal's low half. The name check may be unreachable with llvm-mc 21.1.8 output: it prints a v_pk_fma_f16 literal in full (0x40004200), so the low-half test already fails there.
  3. In the doc row, the new "third shape" sentence sits before "A second shape is repaired too", so the order reads first, third, second. Putting it after the second shape's paragraph would read better.

Not covered by this review

  • The game run (RTX 5080, white frames) and ecox982745's RTX 4090 run are the authors' numbers. I did not reproduce them.
  • No AMD hardware: the high-half reading of the literal through op_sel is still not measured on AMD, as the doc says.
  • The PR body says 2,479 of 2,479 library programs. My local library has 2,684 listings. I do not know how the 2,479 was counted. I translated only a 1-in-9 sample of mine, plus all 29 FSPX listings.
  • I did not run the whole translator suite or the hand-written GPU tests.
  • I did not check llvm-mc 23 listings with this change.

AI-assisted: yes (Claude Code, Claude Opus 5.5, ran the tests and wrote this; a second agent checked this text)

…n the low-half guard

Review of ProjectLeonida#2142: the repair needs only the printed encoding, so it now runs before
check_printed_encodings' early returns (no blob next to the listing, header-only blob).
New tests: a listing with no blob gives the full literal, and a text immediate that is
not the encoded literal's low half is left as the text says. design.md: the third shape
now follows the second.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwRYcpySx4P4ncQ3wD5
@DadaCap

DadaCap commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the review and for checking the fixture against llvm-mc 21.1.8 directly. All three suggestions are in abcccef (plain push on the same branch, no rebase; it still merges cleanly with current main):

  1. The call moved rather than the doc: check_printed_encodings now runs restore_text_cut_mix_literal over every instruction with a printed encoding before its early returns for a missing or header-only blob, so "needs no blob" holds. The old call in the blob loop is gone; the re-anchoring call is unchanged. test_text_cut_but_printed_encoding_whole now also checks listing(STORED) with no blob next to it (8.0).
  2. New test_text_immediate_other_than_the_low_half_is_left_alone: the text shows an immediate that is not the literal's low half, and the result is what the text says. Replacing the low-half guard with True now fails it (8.0 instead of 4.0); removing the moved call fails the test in 1. The literal >> 16 guard is still not pinned: with a high half of 0 the restore writes back the value the text already holds, so no result can tell it apart. It stays as a shortcut that skips work, not a correctness guard.
  3. The third-shape sentence now comes after the second shape's paragraph, and its "CPU-checked by" names the no-blob case and the new test.

Ran test_fma_mix_literal.py (20), test_printed_encoding.py, test_reanchor_llvm_offsets.py, tests/test_rdna2spirv_decode.py, the 13 tests/test_docs_*.py files, check_no_game_data.py on the changed files: all pass. Not run: the full semantic suite or a corpus slice with this commit.

(Reply written with Claude Code.)

@ecox982745

Copy link
Copy Markdown

I checked the new listing-only path at abcccefd7 against its parent a449806dc. The full CPU semantic suite ran 6,320 tests across 493 files with zero failing files on the unmodified head (Windows build 26300, Python 3.13.18, native llvm-mc 23.1.6).

RTX 4090, driver 616.64. I ran seven synthetic cases twice, once without an FSSM file and once with only its 256-byte header. The parent produced the expected pixels on 4/14 draws and the new head on 14/14. This covered positive and negative high halves, the low- and high-f16 destinations, an inline neighbor, genuinely truncated printed bytes, and the PR's text-mismatch control. Each SPIR-V module passed validation for Vulkan 1.2, and I checked every RGBA channel of each 64 × 64 draw.

I found one edge case where an unrelated inline source satisfies the low-half comparison. This is a hand-written inconsistent listing, in the spirit of the new negative test. Whether LLVM emits this input in practice is unknown. These lines reproduce it with the PR's test_fma_mix_literal.py helpers.

text = "v_fma_mix_f32 v1, v2, 1, lit(0x40000002) op_sel:[0,0,1] op_sel_hi:[0,0,1]"
data = listing(program(0x3c000001, "01 60 20 cc 02 03 fd 03", text))

Here src1 is the inline integer 1, while src2 is the only encoded literal (selector 0xff). Its text specifies a high half of 2.0. The parent rendered 2.0. The new head rendered 1.0 because src1 matched the low half and triggered replacement of src2. Both file layouts gave that result.

In a local control I required the same source to have selector 0xff and an immediate equal to the printed low half before replacing it. All 16 GPU draws then matched, including both edge-case draws, and all 20 existing test_fma_mix_literal.py cases passed.

@MoonEyes

Copy link
Copy Markdown
Collaborator

Independent confirmation, from another work copy of 1.0.2.1.

Setup: the pipeline library, 2,684 compute programs, the same translator (main a73a57e), only llvm-mc changes. 2,683 modules translate on both sides.

  • 383 modules differ between an llvm-mc 21 (noted 21.1.8; the version is not recorded in that folder) and 23.1.5. In a sample of 150 of the other 2,300, all are identical byte for byte.
  • The difference comes from 1,674 instructions (1,658 v_fma_mix_f32, 16 v_fma_mixlo_f16) whose literal is printed cut to its low 16 bits in the text while the printed encoding equals the stored bytes. Checked in the SPIR-V disassembly of 3 modules.
  • 776 are f32 sources, 898 are f16 low/high pairs. So with 21 the module gets, with no message, a constant that differs from the stored bytes. For the f16 pairs, what the hardware reads for the high half is still not measured (design.md), so I say "differs", not "wrong".

Not covered: no GPU run. My count differs from the one in this PR (47,910 in 1,218 listings); I do not know why.

AI-assisted: yes (Claude Code. Claude Fable 5.2 ran the comparison through sub-agents; a separate agent replayed the counts on other modules and wrote this wording.)

@DadaCap

DadaCap commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the independent check. On the count difference, here is exactly what the PR's figure counts, recounted over my local listings with the PR's own parser and the exact test restore_text_cut_mix_literal applies (counts only):

Pipeline library (2,684 listings)
all v_fma_mix* 236,467
with a 12-byte printed encoding carrying a literal 47,910
of those, literal's high half non-zero 47,910
of those, a text source equal to the literal's low half (cut) 47,910 (v_fma_mix_f32 47,849, v_fma_mixlo_f16 61)
listings with at least one 1,218

FSPX: 6 in 1 listing; the game's own 25,734 listings: 0. In these listings every literal-carrying v_fma_mix* is printed cut, and each of the 47,910 feeds a read that sees more than the low half (an f32 source, or an f16 source with op_sel set), so none of them is harmless.

So the two numbers measure different things: the PR counts instructions printed cut by llvm-mc 21, yours counts what differs between an llvm-mc 21 and an llvm-mc 23 translation. I can't reproduce your side here (no llvm-mc 23 on this machine), so I don't know why only 1,674 instructions / 383 modules differ in your comparison; one possibility, not checked, is that some of what 23 prints still translates to the same module. If you can share how many of your 383 modules fall within the 1,218 listings above, that would tell the two apart.

On @Zalthor's batch listing #2142 as no longer applying: at head abcccefd7 it merges with current main (a73a57e13) without conflict (git merge-tree clean; GitHub reports it mergeable), so a patch applied to docs/design.md may have hit moved context rather than a real conflict.

(Reply written with Claude Code.)

@Tauwni

Tauwni commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Tested abcccefd7 merged onto main a73a57e13 (it merges cleanly), on an Apple M3 (macOS 26.5.1). I ran the full scripts/run_gpu_tests.py in auto mode, with test programs built from main. I tested the merge rather than the branch alone because a branch from before 9f98dcc61 fails ps_tex against main's vk_bindless_test (the tagged descriptor table), which says nothing about this change.

Driver main main + this PR
MoltenVK 1.4.2 195 pass, 70 skip 195 pass, 70 skip
MoltenVK, LEONIDA_W64_EMULATE=1 229 pass, 36 skip 229 pass, 36 skip
KosmicKrisp (Mesa 26.3.1) 190 pass, 73 skip 190 pass, 73 skip

The counts are identical to main on all three. No new failure. The ones left are the same on both sides: cs_fma_fused (#1724), ps_ssharp (#1774) and cs_imgatomic_ext (#1518) on MoltenVK; KosmicKrisp's (NAN)-constant fixtures (#2229) and cs_imgatomic_ext there.

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

@aslan2142

Copy link
Copy Markdown
Collaborator

Merging.

  • Why: a v_fma_mix* literal that llvm-mc 21 cuts to its low half in the text is now taken from the printed encoding, and the docs/design.md row records the rule.
  • Tested on main 56bdd7e88 + this PR (abcccefd7):
    • The merge is clean, and check_no_game_data.py is clean on the changed files.
    • test_fma_mix_literal.py fails on main (1 of 20) and passes with the PR (20/20).
    • Translator suite: 500 files, 6,367 tests, 0 failures.
    • test_ci_registration.py, the docs tests, the line-ending test, compileall and make_game.py --help pass.
  • For llvm-mc 23 users nothing changes: with llvm-mc 23.1.5 our corpus has no cut literals.
  • Follow-up, not blocking: @ecox982745's point about tying the low-half guard to the literal's own source.

Thanks @DadaCap, @MoonEyes for the review, @JordanWebDev for the approval, and @ecox982745 and @Tauwni for the runs.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

@aslan2142
aslan2142 merged commit 2e2c51c into ProjectLeonida:main Nov 26, 2026
2 of 5 checks passed
@ecox982745

Copy link
Copy Markdown

I implemented the operand-specific follow-up from the merge review. An adjacent inline 1 could trigger a rewrite of a different operand, even when that operand printed a full literal. The patch checks the immediate and the encoded source selector on the same operand. Three-file patch against main 935a2cd5b6 · Coordination claim

On a physical RTX 4090 (Windows 11, NVIDIA 616.64), 14/18 synthetic draws matched the expected pixels on main and 18/18 matched with the patch. The inline-neighbor case changed from 1.0 to the expected 2.0. When two sources selected the literal and only one printed the cut value, the result changed from 8.0 to the expected 10.0. Both cases were run with the native shader file absent and with a header only. All 18 translated modules on each revision passed Vulkan 1.2 SPIR-V validation. Both new inputs are hand-written inconsistent listings. Their frequency in compiler output is unknown.

The full CPU translator suite passed all 6,369 tests across 500 files. The fork's branch is behind upstream workflow changes and GitHub rejected the branch sync with the current credentials. The linked patch is available for review or cherry-picking while the follow-up PR is pending.

AI-assisted with OpenAI Codex (GPT-6).

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.

6 participants