Skip to content

SHADER-507: v_fma_mix literal split into two f16 halves, taken from the blob (llvm-mc prints only the low half) - #2977

Open
omardev29 wants to merge 8 commits into
ItsNotRexed:mainfrom
omardev29:shader-fma-mix-literal
Open

omardev29 wants to merge 8 commits into
ItsNotRexed:mainfrom
omardev29:shader-fma-mix-literal

Conversation

@omardev29

@omardev29 omardev29 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-507 (#2716 (comment)).

What and why

One of the FidelityFX DLL's FSPX programs (unpacked with tools/fspx_unpack.py) has a v_fma_mix_f32 whose 32-bit literal feeds two sources: src0 its low half and src2 its high half (op_sel:[0,0,1] op_sel_hi:[1,0,1]). llvm-mc prints that literal as its low 16 bits and re-encodes it with a zero high half. So the printed-encoding check (#2026) refuses the program, and the text alone would read src2 as +0.0.

Change

  • mix_literal accepts only that difference. The 8 instruction bytes must match (same_encoding with all three sources counted as used, so since BUILD-840: reject mismatches in used VOP3 sources #2932 nothing is forgiven and a live s0 can't pass as an inline 0), and the printed literal must be the stored one's low half.
  • restore_mix_literal then gives every source that the bytes encode as the literal (SRC 0xff) the stored 32 bits, so f16_in takes the half op_sel names.
  • Both the clean-listing check and the re-anchoring path use it. Everything else is refused as before.

What is measured and what is not

  • Not measured: that the hardware gives a source with op_sel_hi and op_sel set the literal's high half.
  • Public evidence: Mesa's ACO relies on it. optimize_constants() in src/amd/compiler/aco_optimizer.cpp (Mesa main at bb986060) packs two constants as the two f16 halves of one literal, read through v_fma_mix's op_sel_hi, with the second source extracting the high half. The console compiler emitted the same here.
  • How to measure it: tools/fma_mix_literal_probe.py --oracle hw runs the instruction's exact bytes on an AMD GPU through tools/hw_oracle. My machine has none, so the hw side has not run anywhere yet. If someone with an RDNA2 GPU could run it, that would settle it (exit 0 means the hardware matches the translator).
  • The game's shaders are unaffected: the "Printed encodings" row counts no such difference in the game's clean listings.

Commits

Commit Change
rdna2spirv the repair; tests/semantic/test_fma_mix_literal.py
docs/design.md the repaired case in the "Printed encodings" row
docs/fspx.md translation status on current main: 27 of 29 (26 without this)
tools/fma_mix_literal_probe.py the hardware probe and its test
tests fixtures as llvm-mc 23.1.5 prints them (the words encoded s2 for v2)
rdna2spirv pins the live-source and inline-constant collision cases (docstring + 2 tests)
docs/design.md the byte check and the controls in the row
fma_mix_literal_probe each case's text and encoding as llvm-mc 23.1.5 prints them

Tested

  • tests/semantic/test_fma_mix_literal.py, 7 cases, with text and encodings from llvm-mc 23.1.5 (the collision case's text is hand-written, see the test):

    • the value through the interpreter, fma(3.0, 2.0, 2.0) = 8.0, on the clean-listing and the re-anchoring paths;
    • the printed text alone, 6.0;
    • an inline integer 1 next to a literal printed as 0x1 keeps its value;
    • three refused cases: another low half, another source register, and a live s0 printed as inline 0.

    Without the change, the two value tests fail. The s0 case is repaired if same_encoding is told the instruction has fewer sources, and the collision case fails if the literal's users are picked by printed value instead of the SRC field (both checked by breaking the code on purpose).

  • tests/test_fma_mix_literal_probe.py: 8 tests. The translator side follows the "halves" reading in all 3 discriminating cases and both controls. Each case's text and printed encoding equal llvm-mc 23.1.5's disassembly of its bytes, except case D's text (lit(0x40400000), which the translator does not parse).

  • The 31+ test files that import the translator, under pytest (Python 3.13): 5,065 passed, 12 skipped (first push).

  • After the review fixes, rebased on main aa2988cc: tests/semantic, test_fma_mix_literal_probe.py, test_docs_design_rows.py, test_rdna2spirv_decode.py, test_isa_coverage.py, test_rdna2spirv_cli.py under pytest (Python 3.13, Windows): 7,536 passed, 5 skipped, 9 failed. The 9 fail the same way on main aa2988cc without this branch (test_loop_ballot_barrier.py emulate modes, test_mutations.py v_mul_legacy_f32, test_cmp_class_f64.py rejected forms). Each new commit passes its own tests on its own.

  • On my own copy of the package: the program now translates and passes spirv-val. On main at aa2988cc, 27 of the DLL's 29 programs translate and pass spirv-val with this, 26 without.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. The tests and probe use made-up values.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed ("Printed encodings": public evidence and the probe, not yet measured).

AI-assisted.

@omardev29

Copy link
Copy Markdown
Contributor Author

Could someone with an RDNA2 GPU run python -I tools/fma_mix_literal_probe.py --oracle hw and post the output? It is the only part of this not measured: whether the hardware gives a source with op_sel_hi and op_sel set the high half of the literal. Exit 0 means it matches the translator.

@Marukooh

Copy link
Copy Markdown
Contributor

Tested 92a3b643 on CachyOS Linux, Python 3.14. tests/semantic/test_fma_mix_literal.py, tests/test_fma_mix_literal_probe.py: 13 passed, 5 subtests passed.

The f16 literal recovery tests cover a directly anchored and a re-anchored listing, and still reject an unrelated encoding mismatch. AMD hardware verification remains separate.

AI-assisted, synthetic inputs only.

@kacper-serewis

Copy link
Copy Markdown
Contributor

Related independent review of overlapping #3369 found a live-source negative control also relevant here: same_encoding on a three-source instruction prefix can accept stored SRC1 s0 (0x000) versus printed inline zero (0x080) while repairing a dropped literal high half. Please require exact instruction-prefix equality for this repair and retain an independent-inline-constant collision control. #3369's request-changes review contains the cases. Your existing tests cover re-anchoring and encoded literal users that the newer PR misses; please coordinate consolidation. AI-assisted synthetic CPU evidence.

@omardev29

Copy link
Copy Markdown
Contributor Author

@kacper-serewis thanks, point 2 is right: the repair will require the 8 instruction bytes to be identical, with a live s0 vs inline 0 control and an inline-constant collision control. Coming in the next push. On consolidating: #2977 came first (claimed 18:10 UTC in #2716, open since 19:00; #3369 was claimed at 20:04), so I've asked on #3369 to close it in favour of this one.

A v_fma_mix source read as f16 takes the half op_sel names, so one 32-bit
literal can give src0 its low half and src2 its high half (one program of
the FidelityFX DLL's FSPX code does: op_sel:[0,0,1] op_sel_hi:[1,0,1]).
llvm-mc prints that literal as its low 16 bits and re-encodes it with a
zero high half, so the printed-encoding check (ItsNotRexed#2026) refused the
instruction, and the text alone would read the high half as +0.0.

mix_literal accepts that one difference: the 8 instruction bytes match
(same_encoding) and the printed literal is the stored one's low half.
restore_mix_literal then gives every source the bytes encode as the
literal (SRC 0xff) the stored 32 bits. Both the clean-listing check and
the re-anchoring path use it. That the hardware hands op_sel's high half of
the literal to that source is not measured; Mesa's ACO relies on it
(optimize_constants() in aco_optimizer.cpp packs two constants as the
two f16 halves of one literal for v_fma_mix), and the console compiler
emitted it here.

tests/semantic/test_fma_mix_literal.py: the value through the interpreter
(fma(1.0, 2.0, 2.0) = 4.0) on both paths, the printed text alone (2.0), and
the refused cases. Without the change the two value tests fail.
The one difference the check now repairs instead of refusing, its
conditions, where it was seen, that the game's clean listings have none
(the row's own count), and that the hardware behaviour is inferred.
Measured on main aa2988c with the v_fma_mix literal repair: 27 of the 29 programs
translate and pass spirv-val (26 without it), and the other 2 stop at the
v_dot4_i32_i8 word llvm-mc does not decode.
…on an AMD GPU

Whether a v_fma_mix source with op_sel_hi and op_sel set reads the high
half of a 32-bit literal is what the previous commits rely on and no one
has measured. The probe tells the two readings apart (op_sel picks the
literal's half, or the literal is 16 bits with a zero high half) with
three cases where they differ and two controls, values made up and exact
in f32. The translator side runs the listing as llvm-mc prints it with the
real bytes next to it; the hw side puts the instruction's exact bytes
(.long, since LLVM's assembler can't encode the literal either) through
tools/hw_oracle on an AMD GPU. Exit codes as fma_mix_probe.py's. The hw
side has not run on any GPU yet: this machine has none.

tests/test_fma_mix_literal_probe.py: the hand-derived values, the
encodings, the translator side's reading, and exit 77 without hardware.
The v_fma_mix_f32 words encoded SRC1 as 0x002 (s2) while the text said v2 (0x102);
both copies had it, so the tests passed anyway. The low half 0x3c00 is the inline
f16 1.0, which llvm-mc 23.1.5 prints as lit(0x40003c00), not 0x3c00. Use a literal
with low half 0x4200 (3.0) and the text and encodings llvm-mc 23.1.5 gives for
these bytes.
…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.
…trols

Say that the repair's same_encoding counts all three sources as used, so a live s0
can't pass as an inline 0, and list the two new controls.
Checked case by case against llvm-mc 23.1.5's disassembly of each case's bytes:
- low halves 0x3c00 and 0x3800 are inline f16 constants, which it prints as
  lit(0x...); use 0x4200 and 0x4500 instead (same readings, new expected values);
- it re-encodes every v_fma_mix literal with its low 16 bits, one read as f32
  (case D) too, not all 32;
- it omits an all-zero op_sel.
D's text stays 0x40400000: llvm-mc prints lit(0x40400000), which the translator
does not parse.
@omardev29
omardev29 force-pushed the shader-fma-mix-literal branch from 0af975b to ef777d4 Compare November 22, 2026 22:17
@omardev29

Copy link
Copy Markdown
Contributor Author

Rebased on main and pushed. Since #2932, same_encoding with all three v_fma_mix sources counted as used forgives nothing, so it already compares the 8 bytes exactly; the repair keeps it, and the two new controls pin it (the live s0 one fails if the sources are under-counted, the collision one if the literal's users are picked by value). Also: the test words encoded s2 where the text said v2; fixtures and the probe now use llvm-mc 23.1.5's text and encodings. docs/fspx.md: 27 of 29 programs translate on current main (26 without this).

@Yourrespawn

Copy link
Copy Markdown
Contributor

Tested ef777d4b on NVIDIA GeForce RTX 4080 (Ada, subgroup 32..32), driver 617.14, Vulkan API 1.4.351, Windows 11 Pro 10.0.26200: pass — with one caveat that isn't this PR's.

  • run_gpu_tests.py: 164/210 passed, 0 failed, 46 skipped (native wave64, expected on this 32-lane GPU). No fma_mix GPU fixture in the PR; the reference is the author's llvm-mc-23 blob listings.
  • Semantic suite: 7463 tests, 7 failures + 2 errors — exactly the pre-existing red state of main aa2988cc (documented with the three causes on #166: the x_round OpFunctionCall outlining vs the mini-interpreter, the stale v_mul_legacy_f32 mutation anchor, and rdna2spirv: implement v_cmp_class_f64 and v_cmpx_class_f64 #2535's failing rejection forms). This branch adds no new failures — the count matches a clean main checkout test for test.

The literal-split rule (two f16 halves taken from the blob per llvm-mc 23.1.5 listings) holds on this machine alongside the existing v_fma_mix hardware rows.

AI-assisted (ZCode agent): builds and runs executed by a coding agent on this machine.

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.

4 participants