Skip to content

rdna2_decode: name VOP2 0x06 / VOP3 0x106 / 0x140 legacy MAC/MAD ops for the shader target (BUILD-806) - #4928

Open
bobcatchris wants to merge 4 commits into
ItsNotRexed:mainfrom
bobcatchris:fix/decoder-vop3-140-target
Open

bobcatchris wants to merge 4 commits into
ItsNotRexed:mainfrom
bobcatchris:fix/decoder-vop3-140-target

Conversation

@bobcatchris

@bobcatchris bobcatchris commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

BUILD-806: the pure-Python decoder (#2038) names the legacy MAC/MAD ops with the GFX10.3 mnemonic whatever target the listing is for, and had no target parameter at all. For the console's gfx1013 code that is the wrong name: those opcodes are the ones GFX10.1 spells without the fused prefix.

Repro on main (3faac557), one encoding each

$ python -I -c "import sys; sys.path.insert(0,'tools'); import rdna2_decode as D; \
   [print(D.disasm_bytes(bytes.fromhex(c), wave32=True)[0]) for c in ('0105000c','008006d501050200','000040d501050e04')]"
v_fmac_legacy_f32_e32 v0, v1, v2        ; encoding: [0x01,0x05,0x00,0x0c]
v_fmac_legacy_f32_e64 v0, v1, v2 clamp  ; encoding: [0x00,0x80,0x06,0xd5,0x01,0x05,0x02,0x00]
v_fma_legacy_f32 v0, v1, v2, v3         ; encoding: [0x00,0x00,0x40,0xd5,0x01,0x05,0x0e,0x04]

llvm-mc (ROCm 5.7's, LLVM 15) on the same bytes, per target:

bytes -mcpu=gfx1013 (the console) -mcpu=gfx1030
01 05 00 0c (VOP2 0x06) v_mac_legacy_f32_e32 v0, v1, v2 v_fmac_legacy_f32_e32 v0, v1, v2
00 80 06 d5 01 05 02 00 (VOP3 0x106, clamp) v_mac_legacy_f32 v0, v1, v2 clamp v_fmac_legacy_f32 v0, v1, v2 clamp
00 00 40 d5 01 05 0e 04 (VOP3 0x140) v_mad_legacy_f32 v0, v1, v2, v3 v_fma_legacy_f32 v0, v1, v2, v3

This is the GFX10.1/10.3 opcode row in docs/design.md: the tables in tools/rdna2_isa_tables.py carry only the GFX10.3 spelling, so console code was printed as fused MAC/MAD.

Change

  • _decode_one, decode_bytes() and disasm_bytes() take the target (mcpu, default gfx1013, since the console is the project's target).
  • One TARGET_DEPENDENT_NAMES map plus target_name() names the three affected opcodes for the target: VOP2 0x06 and VOP3 0x106 → v_mac_legacy_f32, VOP3 0x140 → v_mad_legacy_f32 on gfx101*. Applying it before the VOP2 path appends _e32/_dpp/_sdwa and the VOP3 path appends _e64 keeps every form in step.
  • shader_disasm.disasm() passes its existing --mcpu to the Python backend instead of dropping it (the llvm-mc backend already used it).

After the change: gfx1013/gfx1010 → v_mac_legacy_f32_e32, v_mac_legacy_f32_e64 … clamp, v_mad_legacy_f32; gfx1030 → v_fmac_legacy_f32_e32, v_fmac_legacy_f32_e64 … clamp, v_fma_legacy_f32; default (gfx1013) → the GFX10.1 names.

The target is also validated

A target parameter that accepts values the tables do not describe is the same defect in another form: the Python backend needs no llvm-mc, so nothing rejected a target at all. On main, shader_disasm.py <fssm> --backend python --mcpu gfx1100 exits 0 and reports {'clean': 1} — RDNA2 tables, GFX11 target, GFX10.3 names — and disasm(raw, "gfx1100", backend="python") returns v_fma_legacy_f32.

decode_bytes() now raises for any target outside an explicit list of the parts the tables describe — gfx1010-gfx1013 (GFX10.1; the console is gfx1013) and gfx1030-gfx1036 (GFX10.3) — and the CLI stops with exit 2 before it reads a shader. The list is explicit rather than a gfx101/gfx103 prefix because a prefix lets near-misses through: llvm-mc accepts gfx1037 and gfx1014 as CPU names without complaint, and main also accepts both with the Python backend. --backend llvm-mc is unchanged, since there llvm-mc itself decides which targets exist.

Tests

tests/test_rdna2_decode.py gains test_legacy_mac_mad_mnemonics_follow_the_target: for all three encodings above it decodes as gfx1013, gfx1010 and gfx1030, asserting the expected mnemonic and that the other target's one is absent, then checks the default and that shader_disasm.disasm(..., "gfx1030", backend="python") passes its target through.

tests/test_shader_disasm.py gains test_python_backend_rejects_a_target_its_tables_do_not_cover: the CLI with --backend python exits 2 for gfx1100, gfx9999, rdna2, the bare prefixes gfx101/gfx103 and the near-misses gfx1014/gfx1037, each message naming the rejected value and the accepted list, and a valid gfx1013 run still writes its listing. The decoder test checks the same near-misses (plus gfx1030x, GFX1013, " gfx1013") through the API, then asserts every listed target is accepted and gets the right GFX10.1/GFX10.3 mnemonic.

--skip-existing keeps a listing only for its own target

Same defect, third form: the reuse check was "non-empty file", so the target recorded in the listing was ignored. On main, disassembling with --mcpu gfx1013 and then running --mcpu gfx1030 --skip-existing reports {'reused': 1} and leaves the gfx1013 header — while those two targets disagree on VOP3 0x140 (v_mad_legacy_f32 vs v_fma_legacy_f32), so the kept listing is wrong for the run it was reused by. The check now parses the target from the listing's own header (; name code …, <mcpu>, N invalid words) and reuses only a match; a listing without a header is rebuilt.

test_skip_existing_only_reuses_listings_for_the_same_target covers it end to end: gfx1013 listing → gfx1030 --skip-existing re-disassembles (clean: 1, header now gfx1030) → the same command again reuses (reused: 1). The older test_skip_existing_only_reuses_published_nonempty_listings now writes the header a real listing has.

python -I tests/test_rdna2_decode.py: 11 tests, 1 failure — the failure is test_parity_with_llvm_mc_on_shader_fixtures, which fails identically on unmodified main in this checkout: ROCm 5.7's llvm-mc is LLVM 15 and encodes one SDWA fixture differently (8 vs 12 bytes for ps_sdwa_f16_const.asm), which the repo's CI builds with LLVM 21. The two new tests pass on their own (Ran 2 tests … OK).

python -I tests/test_shader_disasm.py: 22 tests OK. test_rdna2spirv_decode.py and test_rdna2spirv_merge.py: OK (they consume the module's default target).

Control on main's decoder: its disasm_bytes() has no target parameter at all (TypeError with mcpu=), and its default output for these bytes is the GFX10.3 name for all three — the value the test asserts against.

Scope

tools/rdna2_decode.py, tools/shader_disasm.py and the decoder's test file. Hand-built instruction words; the test needs no LLVM, no GPU and no game files.

AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 17dc102 on Windows 11 with CPython 3.14.7, CPU only. The Python CLI emits the target-specific instruction name.

I put 000040d501050e04000081bf at offset 0x128 in a synthetic FSSM fixture. I ran py -3.14 -I tools/shader_disasm.py probe.fssm --backend python --mcpu gfx1013 --jobs 1 --out gfx1013, then repeated with gfx1030 and a separate output folder. Both runs report one clean shader. The first listing has v_mad_legacy_f32. The second has v_fma_legacy_f32. The operand registers and encoded bytes match.

py -3.14 -I tests/test_rdna2_decode.py reports 9 passes and one skip (llvm-mc parity). All 20 tests in py -3.14 -I tests/test_shader_disasm.py pass. On merge base d974c88, the new decoder test fails because the function rejects the mcpu argument.

The existing listing allocation cost is tracked separately in #4932.

AI-assisted.

The decoder's tables carry the GFX10.3 names, but one encoding has two names
depending on the target: GFX10.1 (the console, gfx1013) spells the legacy
multiply-accumulate ops without the fused prefix. llvm-mc prints
v_mac_legacy_f32 where the tables say v_fmac_legacy_f32 (VOP2 0x06, VOP3 0x106)
and v_mad_legacy_f32 where they say v_fma_legacy_f32 (VOP3 0x140). Thread the
target through decode_bytes() and disasm_bytes() (default gfx1013), name those
three opcodes per target, and pass shader_disasm's --mcpu to its Python backend.
@bobcatchris
bobcatchris force-pushed the fix/decoder-vop3-140-target branch from 17dc102 to 36a94fa Compare November 23, 2026 06:33
@bobcatchris bobcatchris changed the title rdna2_decode: name VOP3 0x140 for the shader target (BUILD-806) rdna2_decode: name VOP2 0x06 / VOP3 0x106 / 0x140 legacy MAC/MAD ops for the shader target (BUILD-806) Nov 23, 2026

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

Independent Windows CPU follow-up on the updated head, AI-assisted, with uv CPython 3.12.13 and WSL LLVM 21.1.8. For VOP2 0x06 and VOP3 0x106, the Python backend matched LLVM's target-specific names, operands, suffixes and the VOP3 clamp. Default gfx1013 matched explicit gfx1013.

Both unclamped gfx1013 MAC forms and 0x140 lowered to separate multiply and add with NoContraction and returned zero on the rounding canary. The VOP3 clamp returned 1.0 for 2×3+4 and 0.75 for 0.5×0.5+0.5, confirming that the old destination serves as the addend. With gfx1030 both backends decode the fused names, and the translator still reports the known unsupported fused lowering (see #3188). The ordinary f32 add control matched on both targets. Synthetic inputs only.

@rhtnxe

rhtnxe commented Nov 23, 2026

Copy link
Copy Markdown

Tested 36a94faa merged into main 3faac557 (clean merge) on macOS 27.2.1 arm64 (Apple M4), CPython 3.15.2, with llvm-mc 23.1.6 (Homebrew LLVM): pass. An independent check of the LLVM 15 table in the description on a current LLVM.

  • python -I tests/test_rdna2_decode.py: 10 tests OK (1 skipped), including test_legacy_mac_mad_mnemonics_follow_the_target; main: 9 OK (1 skipped). python -I tests/test_shader_disasm.py: 20 OK on both.
  • The three encodings through llvm-mc -triple=amdgcn--amdpal -mattr=+wavefrontsize32 -disassemble and through this PR's disasm_bytes(..., mcpu=...), for gfx1010, gfx1013, gfx1030 and gfx1036: all 12 lines match, operands included.
bytes gfx1010 / gfx1013 gfx1030 / gfx1036
01 05 00 0c v_mac_legacy_f32_e32 v0, v1, v2 v_fmac_legacy_f32_e32 v0, v1, v2
00 80 06 d5 01 05 02 00 v_mac_legacy_f32_e64 v0, v1, v2 clamp v_fmac_legacy_f32_e64 v0, v1, v2 clamp
00 00 40 d5 01 05 0e 04 v_mad_legacy_f32 v0, v1, v2, v3 v_fma_legacy_f32 v0, v1, v2, v3
  • With no mcpu, disasm_bytes gives v_mad_legacy_f32 v0, v1, v2, v3 (the gfx1013 default).

Synthetic encodings only, no game data. AI-assisted (Claude Code).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 36a94fa on Ubuntu 24.04 (linux-dev-01; Python decoder tests, no GPU used) with LLVM_MC=llvm-mc-21, so the llvm-mc parity test ran instead of skipping.

  • test_legacy_mac_mad_mnemonics_follow_the_target (new): ok.
  • With mcpu=gfx1030 the three encodings decode as: 0105000c -> v_fmac_legacy_f32_e32 v0, v1, v2; 008006d501050200 -> v_fmac_legacy_f32_e64 v0, v1, v2 clamp; 000040d501050e04 -> v_fma_legacy_f32 v0, v1, v2, v3.
  • test_parity_with_llvm_mc_on_shader_fixtures fails on runtime/test/shaders/ps_sdwa_f16_const.asm: 'f9a00a7ef2148500' != 'f9a00a7eff148500003c0000'. Same failure with the same bytes on main 3faac55 on the same machine (Ran 9 tests, FAILED (failures=1)), so it is not from this PR. Since llvm-mc-21 shows it too, the parity failure does not come only from an older LLVM.
  • Without LLVM_MC: Ran 10 tests, OK (skipped=1).

AI-assisted.

The new target parameter accepted anything: `--backend python --mcpu gfx1100`
(the Python tools need no llvm-mc, so nothing else rejected it) decoded RDNA2
tables and reported the shader "clean", with the GFX10.3 opcode names for a
GFX11 target. decode_bytes() now raises for a target outside gfx101*/gfx103*,
and the CLI stops with exit 2 before it reads a shader.
@memorypressure

Copy link
Copy Markdown

Verified merged with main 3faac55: the new target-sensitive mnemonic test passes and test_shader_disasm stays green. The single failure in tests/test_rdna2_decode.py is the known exp-null parity red that reproduces identically on plain main (re-confirmed at the same ps_w64_a_discard.asm case; fix pending in #4697, see #4421) — unrelated to this change. mcpu threading is backward compatible and shader_disasm's python backend now matches its llvm-mc backend per target. CI pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

`gfx101`/`gfx103` prefixes let near-misses through, and llvm-mc accepts
gfx1014 and gfx1037 as CPU names without complaint, so nothing else rejects
them either: `--backend python --mcpu gfx1037` exited 0 on main and would still
have been accepted with a prefix. The guard now lists the parts the tables
describe (gfx1010-1013, gfx1030-1036) and names them in the error.
A listing is only reusable for the target it was written for, but the check
was "non-empty file", so `--mcpu gfx1030 --skip-existing` kept a listing
disassembled as gfx1013 and reported it as reused, even though opcode 0x140 has
a different mnemonic there. Match the target recorded in the listing header.
@kapitche

Copy link
Copy Markdown
Contributor

Tested 200a4c3 merged with main 3faac55 (clean merge) on Ubuntu (WSL2) / CPython 3.12.16 and Windows 11 / CPython 3.13.1 with llvm-mc 23.1.6: pass.

  • Linux (no llvm-mc, so the parity test skips): tests/test_rdna2_decode.py 11 run, 10 pass, 0 fail, 1 skipped; tests/test_shader_disasm.py 22 run, 22 pass.
  • Windows (LLVM_MC set, parity test runs): tests/test_rdna2_decode.py 11 run, 10 pass, 1 fail; tests/test_shader_disasm.py 22 run, 22 pass. The one failure is test_parity_with_llvm_mc_on_shader_fixtures, an exp-null operand mismatch (target vs special); I ran that test alone on plain main 3faac55 on the same machine and it fails identically, so it is the known red on main and not from this PR.

Negative control: with tools/rdna2_decode.py and tools/shader_disasm.py taken from main and the PR's tests kept, 4 new tests fail (2 in test_rdna2_decode.py, errors since mcpu is not accepted; 2 in test_shader_disasm.py); restored, they pass.

Read the diff: no verified issue.

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.

7 participants