Skip to content

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

Open
kapitche wants to merge 1 commit into
ItsNotRexed:mainfrom
kapitche:fma-mix-literal-high-half
Open

kapitche wants to merge 1 commit 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).

@Vonsek

Vonsek commented Nov 22, 2026

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

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