Skip to content

rdna2spirv: read a v_fma_mix* literal's high half from the code blob - #3369

Closed
kapitche wants to merge 3 commits into
ItsNotRexed:mainfrom
kapitche:fma-mix-literal-high-half
Closed

kapitche wants to merge 3 commits into
ItsNotRexed:mainfrom
kapitche:fma-mix-literal-high-half

Conversation

@kapitche

Copy link
Copy Markdown
Contributor

Claims: #3232 (comment)

What broke. v_fma_mix_f32 v0, 0x3e00, 1.0, 0x3e00 op_sel:[0,0,1] op_sel_hi:[1,0,1] with the literal dword 0xc1003e00: llvm-mc prints the literal as an f16 (low half) and re-encodes the high half as 0, so check_printed_encodings refused it ("a field the disassembler dropped"). op_sel[2]=1 reads that high half (-2.5). Seen in 1 shader of my own 1.0.2.1 copy (counts only), 2 instructions.

Fix. For v_fma_mix* with a 12-byte encoding where only the literal's high half differs, take the literal dword from the code blob and use it for the printed literal operands. Any other difference is still refused. Row added to docs/design.md.

Tested. tests/semantic/test_valu_fma_mix_literal.py (synthetic, blob built in a temp dir): high half read gives -1.0, zero high half gives 1.5, a different low half is still refused. Linux (WSL2), Python 3.12.16: full tests/semantic 4145 tests OK (4 skipped); tests/test_rdna2spirv_decode.py, test_isa_coverage.py, test_docs_design_rows.py, test_rdna2spirv_cli.py OK. The real program now translates and passes spirv-val (1 of 1).

Negative control. Without the tools/rdna2spirv.py change the high-half test fails with the "field the disassembler dropped" Unsupported.

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 CPU review at f765e90 found four issues:

  1. Operand rewrite at tools/rdna2spirv.py:11574 uses low16 value equality. A shared literal 0x40000001 in SRC0/SRC2 with independent inline integer 1 in SRC1 overwrites that inline operand: expected export 0x40000000, observed 0x40000001. Select literal users by encoded source field 0xff.
  2. Prefix same_encoding at 11569 accepts stored SRC1 s0 versus printed inline zero when the literal high half was dropped. Parent rejects. Require exact equality of the eight instruction bytes for this repair.
  3. Re-anchoring at 11615 still rejects the intended repair. The shared-literal fixture exports 4.0 on the clean path and raises normalized-encoding Unsupported on the other path.
  4. Printed literal 0x12343c00 versus stored 0x40003c00 is accepted. Require the printed literal to be the zero-extended low half, and restrict to the supported mix opcodes.

Three local tests pass; four of five reused #2977 semantic tests pass, with re-anchoring failing. Even the wrong-value module passes Vulkan 1.2 spirv-val. Synthetic CPU only. Consolidate with existing #2977 (SHADER-507); it handles both paths, encoded literal users and zero-extension, but its prefix same_encoding also needs the live-source negative control.

@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested f765e90 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass.

  • python3 test_valu_fma_mix_literal.py (from tests/semantic) — 3/3 OK
  • python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build — 175/189 passed, 14 skipped, 0 failed (expected D3D12-owned skips + ps_w64x_readlane)

AI-assisted (OpenCode).

@evilclanker9000

Copy link
Copy Markdown
Contributor

Measured on gfx1036 (RDNA2, driver 26.10.1, game MODE: IEEE 0, DX10_CLAMP 1, f32 denormals flushed, f16/f64 kept): v_fma_mix_f32 v8, lit, 1.0, lit op_sel:[0,0,1] op_sel_hi:[1,0,1] with the 12-byte encoding of the PR's example (words 0xcc206008 0x0bfde4ff then the literal dword, written with .long in a raw hw_oracle kernel, since llvm-mc will not print or re-encode the high half), one lane, result stored from v8:

literal dword op_sel result
0xc1003e00 [0,0,1] 0xbf800000 = -1.0
0x00003e00 [0,0,1] 0x3fc00000 = 1.5
0xc100c100 [0,0,1] 0xc0a00000 = -5.0
0xc1003e00 [0,0,0] 0x40400000 = 3.0
0x00003e00 [0,0,0] 0x40400000 = 3.0
0xc100c100 [0,0,0] 0xc0a00000 = -5.0

So src0 (op_sel[0] = 0) reads the low half 0x3e00 = 1.5 and src2 with op_sel[2] = 1 reads the literal's high half: 0xc100 = -2.5, giving 1.5 * 1.0 - 2.5 = -1.0, and a zero high half gives 1.5 + 0 = 1.5. That is what the PR says (-1.0 with the high half, 1.5 with a zero one, "op_sel[2]=1 reads that high half (-2.5)"). The extra rows show the other cases it implies: with op_sel[2] = 0 the low half is read whatever the high half holds (3.0, and -5.0 when both halves are 0xc100), so taking the literal from the blob only matters when op_sel selects the high half, as the PR limits it to. I measured only; I did not run the PR's tests in a scratch worktree for this comment. The kernel is local, not pushed. AI-assisted.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested f765e90 on NVIDIA GeForce RTX 4090 (Ada, subgroup 32..32), 621.14, Windows 11 (26200): pass, no change against main 1628210 (the branch's base).

Test programs: MSVC 19.44 RelWithDebInfo build of main 1628210 (the PR changes nothing under runtime/). llvm-mc 21.1.8, Vulkan SDK 1.4.363.0, Python 3.13.7.

main 1628210 f765e90
python -I scripts/run_gpu_tests.py 143/189 passed, 46 skipped, 0 failed 143/189 passed, 46 skipped, 0 failed
the same with LEONIDA_W64_EMULATE=1 174/189 passed, 15 skipped 174/189 passed, 15 skipped
python -I tools/rdna2spirv.py <work>/shaders/dxbc/disasm --limit 200 --no-asm (my own work folder; counts only) 200 translated, 200 pass spirv-val 200 translated, 200 pass spirv-val; all 218 .spv files byte-identical to main's

On the branch:

  • python -I tests/semantic/test_valu_fma_mix_literal.py: 3 tests OK
  • python -m unittest discover -s tests/semantic -p "test_valu_fma*.py": 18 OK; -p "test_*mix*.py": 60 OK
  • python -I tests/test_rdna2spirv_decode.py 23 OK, test_isa_coverage.py 13 OK (3 skipped), test_docs_design_rows.py 2 OK, test_rdna2spirv_cli.py 18 OK
  • python -I -m compileall -q make_game.py tools tests: exit 0

Not run: the full tests/semantic suite, and the one retail shader the PR is about (the 200-shader slice does not contain it, so this is a no-regression check). AI-assisted check (Claude Code).

@chpeu

chpeu commented Nov 22, 2026

Copy link
Copy Markdown

Tested f765e90a on two NVIDIA cards, Linux, no game files. main f847748c is not in this branch's
history, so I ran it twice: as it stands, and re-applied on top of current main.

As it stands (f765e90a)

  • LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build runtime/build/linux --llvm-mc /usr/lib/llvm-20/bin/llvm-mc
    → RTX 3080 174/189 passed, 15 skipped, 0 failed; RTX 5080 (LEONIDA_VK_DEVICE="RTX 5080") the same.
  • Your new tests/semantic/test_valu_fma_mix_literal.py: 3 tests OK under python -m unittest discover -s tests/semantic, the way CI runs that folder.

Re-applied on current main (git merge --no-ff --no-commit of your head into f847748c: clean, no
conflicts):

  • GPU suite → 184/199 passed, 15 skipped, 0 failed on the RTX 3080 and on the RTX 5080 with the wave64
    emulation, 153/199 passed, 46 skipped without it — the same numbers main gives alone here, so the
    high-half literal read changes nothing on NVIDIA hardware with today's fixtures.
  • Build clean, python -m compileall -q make_game.py tools tests clean.

One limit worth stating: these two cards are 32-wide, so the v_fma_mix* paths are checked through the wave64
emulation of the compute tests, not on a real 64-wide subgroup — the semantic tests cover the translation side,
and a wave64 card would still add a second opinion.

Machine: Linux Mint 22 (Ubuntu 24.04 base), NVIDIA driver 615.71.09, Vulkan 1.4.351, llvm-mc 20.1.2,
SPIRV-Tools 2025.1~rc1 (Ubuntu packages), GCC 13.3, Python 3.12.3, built with
cmake -S runtime -B runtime/build/linux -DLEONIDA_VULKAN_HEADERS_FETCH=ON. AI-assisted testing, counts read
through by hand.

@omardev29

Copy link
Copy Markdown
Contributor

This overlaps #2977 (SHADER-507), which came first: claimed at 18:10 UTC in #2716 and open since 19:00, before this claim (20:04 in #3232). It takes the same literal from the blob for the same instruction, and already covers points 1, 3 and 4 of the review above; point 2 (identical instruction bytes) is being added there now. Unless you have a case #2977 misses, could you close this one in favour of it?

omardev29 added a commit to omardev29/Leonida that referenced this pull request Nov 22, 2026
…bytes

mix_literal compared the 8 instruction bytes with same_encoding, whose
unused-SRC1/SRC2 allowance (stored 0, printed 0x80) would accept a live s0
source printed as inline 0 and then repair the literal. Every source of a
three-source v_fma_mix* is live, so require the bytes to be identical.

Tests: that case is refused (it is repaired without this change), and an inline
constant whose value equals the printed literal keeps its value, since the
literal's users are picked by the SRC field (0xff). Found by kacper-serewis's
review of ItsNotRexed#3369.
@kapitche
kapitche force-pushed the fma-mix-literal-high-half branch from f765e90 to c680d44 Compare November 22, 2026 22:20
@kapitche

Copy link
Copy Markdown
Contributor Author

Rebased onto main aa2988c; conflicts in tools/rdna2spirv.py and docs/design.md (main changed same_encoding to take the operand count, and added a v_fmamk/fmaak_f16 row), kept both and passed the operand count in our branch; tests: tests/semantic 7459 run (only the 10 failures that also fail on main), test_valu_fma_mix_literal 3 OK, test_docs_* and test_fma_mix_probe OK.

@oVolkra

oVolkra commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested c680d44 (merged onto main aa2988c) on RTX 3060 (Ampere), driver 617.14, Windows 11, MSVC 14.44, RelWithDebInfo: pass

  • ctest: 153/153 pass, also with LEONIDA_VK_VALIDATION=1 (153/153).
  • scripts/run_gpu_tests.py: native 164/210 (46 skipped), with LEONIDA_W64_EMULATE=1 195/210 (15 skipped). Same as main at aa2988c.
  • Eight *_test.exe programs that ctest does not register (d3d12_clear, tiny_game_exit_code, vk_bindless, vk_compute, vk_sampler, vk_store, vk_texture, vk_xdxr_build) were run by hand, with and without validation: same results as on main.
  • No game files used.

AI-assisted: this check was run and written with Claude Code (Anthropic) on my machine; the numbers above are its unedited output.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested c680d44 merged onto main aa2988c on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed. main aa2988c gives 164/210, 46 skipped, 0 failed. All 214 test shaders translate and pass spirv-val.
  • tests/semantic/test_valu_fma_mix_literal.py: ran 3 tests in 0.060s; OK.

AI-assisted (Claude Code).

@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Combined data point for #3369 and #3372, from a scratch merge of both onto main 98fa503f (they merge cleanly). The FidelityFX upscaler's FSPX programs, from my own 1.0.2.1 copy (counts only), go from 26/29 to 29/29 translated, and 29/29 pass spirv-val. That holds natively, with --wave64-emulate, and with --wave64-emulate --opt (#4760).

Caveat: as @aslan2142 found on #3372, some programs fall through into dot4 words llvm-mc can't decode at all. Those translate but may lose stores until SHADER-813 lands, so 29/29 means 'translated and valid', not yet 'computes everything'. 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.

SHADER-507 independent CPU comparison at c680d44 and the sibling proposal against shared parent aa2988c, all 29 cached inputs. Both proposals improved LLVM 22 from 21 to 22 valid and LLVM 23 from 26 to 27 valid. LLVM 21 remained 22 valid, but #2977 kept the residual shader’s two truncated literals while #3369 restored the full literals. Conversely, #3369’s low-half matching changed an unrelated inline constant in a synthetic control and did not accept the reanchored control. Source-field targeting, reanchoring and restoration on the LLVM 21 path need coverage together. No GPU semantics inferred. AI-assisted.

…r SRC fields encoded 0xff, with exact instruction bytes, a zero-extended printed literal, on the mix opcodes, and on the re-anchor path too
@kapitche

kapitche commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the controls. I reproduced them on c680d44 and pushed 293ba66 (two commits on top).

Measured on c680d44 (Python 3.12.16, Linux, synthetic blob and listing):

  • Shared literal 0x40000001 in SRC0/SRC2 plus the inline integer 1 in SRC1: the export was 0x40000001 where 0x40000000 (2.0) is right, because the inline operand was overwritten by value match. Real.
  • Printed high half not zero (printed literal 0x12343e00, stored 0x40003e00): accepted. Real.
  • Listing with undecodable words (the re-anchor path): the repair was not applied there, so the instruction was refused. Real, within the supported shape.
  • Stored SRC1 = s0 against printed inline 0: refused already (same_encoding applies no SRC1/SRC2 leniency with three sources). Not a bug; the test is now there to pin it.
  • v_fma_f32 with the same bytes is refused already (the repair was gated on the v_fma_mix prefix); pinned as well.

Changes: the dword goes only to sources whose encoded SRC field is 0xff; the eight instruction bytes must be equal, the literal's low half equal and the printed high half zero; only v_fma_mix_f32/mixlo_f16/mixhi_f16; the same match is used when re-anchoring. Tests: tests/semantic/test_valu_fma_mix_literal.py is 8 tests now. With tools/rdna2spirv.py put back to c680d44, the shared-literal control, the zero-extension control and the re-anchor control fail (2 failures, 1 error) and the other five pass; with the change all 8 pass. Also green: test_valu_fma_mix.py (15), test_printed_encoding.py (7), test_valu_fma*.py (128), test_semantic_translator_use.py, the 7 test_docs_*.py.

On #2977: it covers the same ground; I have not measured it. Either one can go in, and this PR's tests are meant to be reusable against it. I did not run the 29 FSPX programs again.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 293ba660c80c1747fd4ab8edb0be67ec5e16690a on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

I rendered the SPIR-V produced from four of the synthetic listing/blob pairs in test_valu_fma_mix_literal.py with the Vulkan harness built from this head. Each draw matched the expected binary32 word in all four channels at all 4,096 pixels.

  • Shared literal 0x40000001 in SRC0/SRC2 with inline integer 1 in SRC1: exactly 0x40000000 (2.0). This distinguishes the corrected result from the reported parent result of 0x40000001.
  • Stored literal 0xc1003e00: 0xbf800000 (−1.0), including when the listing required re-anchoring.
  • Zero high-half control 0x00003e00: 0x3fc00000 (1.5).

All four pixel shaders passed Vulkan 1.2 SPIR-V validation, and the eight focused semantic tests passed separately.

AI-assisted verification.

@ItsNotRexed

Copy link
Copy Markdown
Owner

Closing as a duplicate of #2977, merged this round, which reads the v_fma_mix literal by its SRC field. This one was merged and then reverted on main: it widened every immediate whose value matched the literal's low half, so an inline constant printed the same way took the literal (test_fma_mix_literal).

ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
… literal's low half

#2977 (merged this round) reads the literal by the SRC field; #3369 replaced any operand whose value matched
the literal's low half, so an inline constant printed as the same value took the literal
(test_fma_mix_literal's inline-constant case). The two PRs fix the same thing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
evilclanker9000 pushed a commit to evilclanker9000/Leonida that referenced this pull request Nov 23, 2026
…ion cases

mix_literal compares the 8 instruction bytes with same_encoding counting all
three sources as used, so since ItsNotRexed#2932 nothing is forgiven there and a live s0
can't pass as an inline 0. Say so in the docstring and pin it:

- a stored SRC1 s0 against a printed inline 0 is refused (repaired if the
  sources are under-counted);
- an inline constant whose value equals the printed literal keeps its value,
  since the literal's users are picked by the SRC field (0xff).

Asked for by kacper-serewis's review of ItsNotRexed#3369.
@chpeu

chpeu commented Nov 23, 2026

Copy link
Copy Markdown

Re-tested the new head 293ba660 here (my earlier note was against f765e90a). Your change is Python + docs only,
so no GPU run was needed.

  • tests/semantic/test_valu_fma_mix_literal.py → OK.
  • Your branch as it stands: 1592 passed, 1 failed in the pytest-marked translator files — but that single failure
    is the known test_u16_high_half one that came from aa2988cc and was fixed by Translator: constants assemble asks for late are defined too #3902, which your branch predates;
    it is not from your change.
  • Merged onto current main 3faac557 (clean, no conflicts) and measured there: 1637 passed, 37 skipped,
    108 subtests, 0 failed
    , which is exactly what main gives on this box in the same session.

So on the state people will actually merge, this head is green here. Machine: Linux Mint 22 (Ubuntu 24.04 base),
llvm-mc 20.1.2, SPIRV-Tools 2025.1~rc1, Python 3.12.3 with pytest 9.2.0 and numpy 2.5.5. AI-assisted testing,
counts read through by hand.

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.