Files
mandy/tools/shader_disasm.py
Jan Trencansky 39879ce0fd Merge main into RT-PROTON-STORY-LOAD (#984)
Merges main at 1154976d4 into the integration branch at 273b3bdf9 (merge base ece4ba5b3). 42 files conflicted;
this commit is the resolution only. Build and test repairs follow in their own commits.

Rule of resolution: where both sides added different things, both stay; where both made the same change in two
forms, main's reviewed form stays and whatever #984's form did in addition is carried into it; where the two
changed the same code for different purposes, both purposes are kept.

Xbox proxy code (runtime/shims/d3d12_xs_device.cpp, d3d12_xs_device.h, d3d12_xs.cpp, d3d12_xs_alloc_size.h,
d3d12_xs_desc_clean.h, registry.cpp):
- d3d12_xs_device.cpp is #984's file with main's differing pieces ported in: env_slot and the configurable device
  routes, XboxQueueSlotRoutes, PresentationHandler / set_presentation_handler, the console caller rule for device
  slots 61 and 74 (#383, #385), route_xbox_device_slot, slot 81 as GetFrameStatisticsX, create_host_device, the
  console-form rule for slots 25 and 56, the console list IID of #359, and the forms of queue slot 18 (#403) and
  list slot 139 (#396).
- Kept from #984: the list proxy with handle translation, queue slots 16/17 as Signal/Wait, the fence's
  completed-value mirror, the shared FrameTokenSource, PresentX through queue_present_x when no handler is
  attached (slot 30, or 32 on a queue detected as the newer layout), slots 27/53/55/57/70/71/133, the wider
  pc_resource_desc, S_OK for queue slots past the PC table.
- Dropped: #984's GetFrameEventsX and LEONIDA_XS_FRAME_EVENTS_SLOT, its second-word rule for slot 61, its
  undetected-layout rule for queue slot 18, its list IID mapping to GraphicsCommandList6; main's copy of the base
  list proxy, its null-view slot 70 and its refusing slot 133.

Proxy tests (runtime/test/xs_proxy_mock_test.cpp, device_proxy_test.cpp, queue_proxy_test.cpp):
- Checks of both sides, each run once; main's forms of the render-target size and layout checks with #984's extra
  cases added; main's --env-slots-74 and #984's --env-slots-70.
- Adapted to the merged code: the queue tail answers S_OK, slot 73 answers an unknown IID with the list proxy,
  the queue slot 18 case installs a console caller. Dropped: #984's slot 81 GetFrameEventsX cases.

D3D12/Vulkan layer (runtime/shims/d3d12_com.cpp, d3d12_vulkan_device.cpp, runtime/src/command_list.h,
frame_pacer.h, shader_library.h, vulkan_backend.h, vulkan_replay.h, runtime/test/command_list_test.cpp,
d3d12_close_parity_test.cpp, d3d12_execute_indirect_validation_test.cpp, replay_texture_barrier_test.cpp,
vk_pipeline_check.cpp, vk_test_device.h):
- Resource::create_texture: #984's title images (deferred placed, pending streamed, title pool) and main's
  ordinary textures (#1413 shared blocks, #1364 staging fallback) are exclusive branches chosen by the texture's
  origin, so both pools run on disjoint sets of textures.
- frame_pacer.h is main's pacer (#402) with #984's FrameTokenSource as the token counter.
- main's forms kept for ExecuteIndirect (#1066, #377), the barrier into LAYOUT_UNDEFINED (#1055), the B4G4R4A4
  stand-in, per-face stencil masks, the unaligned tile staging (#528) and the device-lost flag (#980).
- shader_library.h: main's block inflate; #984's `bulk` parameter now defaults to true so main's callers keep it.
- Dropped: #984's B8G8R8A8 stand-in for B4G4R4A4 targets, its refusal of separate stencil masks, its zero-count
  refusal in ExecuteIndirect, its wider UNDEFINED -> UNDEFINED release and the test case for it.

Other shims (runtime/shims/dstorage.cpp, file_system.cpp, gameinput.cpp, xgameruntime_offline.cpp,
runtime/test/xgameruntime_offline_test.cpp, xmem_test.cpp):
- ReadFile and DirectStorage reads: #984's pinned kernel_writes read, then main's touch_pages retry on
  ERROR_NOACCESS. main's rt::fs::read_file is folded into #984's read_file.
- gameinput: main's strict LEONIDA_INPUT_SCRIPT parser with #984's rsup/rsdown entries.
- Speech synthesis: main's one silent voice and handle checks; #984's accept-anything form dropped.

Build, tools, docs (runtime/CMakeLists.txt, runtime/cmake/translator_deps.cmake, scripts/build_runtime.ps1,
make_game.py, tools/rdna2spirv.py, shader_disasm.py, shader_ir.py, shader_library.py,
runtime/test/shaders/cs_imgload_tfe.asm, cs_imgload_d16_tfe.asm, docs/README.md, boot.md, d3d12_vulkan.md,
runtime/README.md):
- Unions of the test, target and source lists; spirv_loop_guard.py added to translator_deps.cmake.
- rdna2spirv.py: main's DPP compare rule (#957), SMEM vcc base (#647) and widen_wave64_masks (#1021); #984's
  restore_skipped_instructions and restore_dot4_inline_sel. #984's duplicate widen_wave64_valu_masks dropped.
- shader_disasm.py and shader_ir.py: main's structure with #984's one-line-per-dword llvm-mc input and wave flags.
- shader_library.py: main's structure with #984's --compute-hashes and per-state compute variants.

One file that did not conflict is changed here: tools/rdna2_decode.py. Git kept both sides' decoders for a VOPC
compare with a DPP operand, and #984's narrower one shadowed main's; it is main's file again.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-24 19:38:04 -05:00

368 lines
18 KiB
Python

"""Disassemble the native RDNA2 code in unpacked FSSM shader blobs with LLVM's AMDGPU disassembler.
FSSM header (little-endian qwords at 0x00..): 'FSSM' 01 01, blob size, ..., then a table where the
code range appears as (offset, size) = qword[6], qword[7] in every blob checked so far (e.g. 0x2f8, 0x100).
The code ends with s_endpgm (0xBF810000). If the header guess doesn't hold for a blob, the tool falls back to
scanning back from the first s_endpgm to the start that decodes with no invalid instructions.
Usage:
python -I shader_disasm.py <fssm dir or file> [--unpack-index FILE] [--out DIR] [--mcpu gfx1013] [--limit N] [--skip-existing]
Writes <out>/<name>.s and prints a summary (decoded cleanly / with invalid words / failed).
Set LLVM_MC to llvm-mc's path, or to a whole command such as
'wsl -d Ubuntu -e /usr/lib/llvm-21/bin/llvm-mc' when the tool is not on PATH. Off Windows, a versioned install
(llvm-mc-21, /usr/lib/llvm-21/bin/llvm-mc) is found without it (tools/mc_command.py).
"""
import argparse
import os
import re
import struct
import subprocess
import sys
import tempfile
from concurrent.futures import ThreadPoolExecutor
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from mc_command import install_hint, mc_command, not_found # noqa: E402
from report_paths import path_key, same_report_path # noqa: E402
from shader_stage_manifest import shader_stems # noqa: E402
import fssm_header # noqa: E402
import rdna2_decode # noqa: E402
TRIPLE = "amdgcn--amdpal"
ENDPGM = b"\x00\x00\x81\xbf"
PROBE = "0x00 0x00 0x81 0xbf" # s_endpgm: the one instruction every shader ends with
FAILED = [] # (path, first stderr line) for shaders that produced no disassembly
MC = mc_command()
def fail(msg: str) -> "None":
print(f"ERROR: {msg}", file=sys.stderr)
sys.exit(1)
def output_path(path: str, out: str) -> str:
return os.path.join(out, os.path.splitext(os.path.basename(path))[0] + ".s")
def validate_output(out: str, files: list) -> None:
"""Refuse a planned <out>/<stem>.s that is a folder or names an input, before probing llvm-mc or creating --out.
A single `foo.s` given with --out at its own folder (or the default ../disasm of a file already in disasm/) was
replaced by its own listing; on Windows and macOS `FOO.S` is the same file as the `FOO.s` written for it."""
inputs = {path_key(p): p for p in files}
for src in files:
dst = output_path(src, out)
if os.path.isdir(dst):
fail(f"cannot write {dst}: destination is a folder, not a file")
hit = inputs.get(path_key(dst))
if hit is None and same_report_path(src, dst): # a hard link or another spelling the name rules missed
hit = src
if hit is not None:
fail(f"output {dst} would overwrite input {hit}")
def positive_int(text: str) -> int:
"""--jobs 0 made ThreadPoolExecutor raise, and --limit -1 silently dropped the last file."""
try:
value = int(text)
except ValueError:
raise argparse.ArgumentTypeError(f"invalid int value: {text!r}") from None
if value < 1:
raise argparse.ArgumentTypeError("must be at least 1")
return value
def probe(mcpu: str) -> None:
"""Disassemble one s_endpgm first, so a missing tool or AMDGPU target fails now and not per shader."""
try:
p = subprocess.run(MC + ["--disassemble", f"-triple={TRIPLE}", f"-mcpu={mcpu}"],
input=PROBE, capture_output=True, text=True, errors="replace",
creationflags=getattr(subprocess, "BELOW_NORMAL_PRIORITY_CLASS", 0))
except OSError as e:
fail(not_found(MC[0], e))
if p.returncode != 0 or "s_endpgm" not in p.stdout:
why = (p.stderr.strip().splitlines() or [f"exit code {p.returncode}"])[0]
fail(f"'{' '.join(MC)}' did not disassemble an s_endpgm: {why}\n"
f"The disassembler needs LLVM 21+ with the AMDGPU target (docs/design.md, 'Disassembling shaders').\n"
f"{install_hint()}")
def disasm(code: bytes, mcpu: str, backend: str = "llvm-mc", wave32: bool = True):
if backend == "python":
asm, bad = rdna2_decode.disasm_bytes(code, mcpu=mcpu, wave32=wave32)
return asm, bad, ""
asm, bad, err = run_mc(code, mcpu, wave32)
if bad and asm.strip():
fixed = with_vopc_dpp(code, mcpu, err, wave32)
if fixed:
return fixed
return asm, bad, err
def run_mc(code: bytes, mcpu: str, wave32: bool = True):
"""One llvm-mc pass over `code`: (listing, invalid word count, stderr), or ("", count, reason) when it failed."""
# llvm-mc echoes the entire input line for each bad word. Newlines are whitespace, even inside
# a multiword instruction. Bound each diagnostic to one dword instead of the whole shader.
text = "\n".join(" ".join(f"0x{b:02x}" for b in code[i:i + 4]) for i in range(0, len(code), 4))
try:
wave_flags = [] if wave32 else ["-mattr=-wavefrontsize32,+wavefrontsize64"]
p = subprocess.run(MC + ["--disassemble", f"-triple={TRIPLE}", f"-mcpu={mcpu}", *wave_flags, "-show-encoding"],
input=text, capture_output=True, text=True, errors="replace",
creationflags=getattr(subprocess, "BELOW_NORMAL_PRIORITY_CLASS", 0))
except OSError as e:
return "", 0, str(e)
bad = p.stderr.count("invalid instruction encoding")
if p.returncode != 0:
return "", bad, p.stderr.strip() or f"llvm-mc exited with code {p.returncode}"
return p.stdout, bad, p.stderr
ENCODING = re.compile(r"; encoding: \[([^\]]*)\]")
INVALID_AT = re.compile(r":(\d+):(\d+): warning: invalid instruction encoding")
VOPC = 0x3E # bits 31..25 of a VOPC word
DPP_SRC0 = (250, 233, 234) # the src0 field of a DPP16, DPP8 and DPP8-with-fi word
STAND_IN = 0x10 # VOP2 v_max_f32: takes the same DPP second word, and llvm-mc decodes it
STAND_IN_TEXT = "v_max_f32_dpp v0, "
def invalid_offsets(err: str) -> set:
"""Byte offsets llvm-mc rejected: its warning names the line and column of the word in the input of 5-character
"0xNN " tokens it was given, one line per dword (run_mc); a listing made from one line of the whole code has
every warning on line 1."""
return {(int(line) - 1) * 4 + (int(col) - 1) // 5 for line, col in INVALID_AT.findall(err)}
def vopc_dpp_words(code: bytes, offsets) -> dict:
"""{offset: (compare name, word0, word1)} of the rejected words at `offsets` that are a VOPC compare whose src0
field is a DPP marker. The name is the decoder's (rdna2_decode's VOPC table)."""
found = {}
for at in sorted(offsets):
if at % 4 or at + 8 > len(code):
continue
w0, w1 = struct.unpack_from("<2I", code, at)
entry = rdna2_decode.T.VOPC.get((w0 >> 17) & 0xFF)
if w0 >> 25 == VOPC and w0 & 0x1FF in DPP_SRC0 and entry:
found[at] = (entry[0], w0, w1)
return found
def with_vopc_dpp(code: bytes, mcpu: str, err: str, wave32: bool = True):
"""The listing of `code` with each rejected VOPC compare in DPP form printed as the instruction it is, or None
when there is none (docs/design.md, "DPP on a VOPC compare"). llvm-mc has no decoding for the pair on gfx10, so
it is decoded with the first word turned into a VOP2 that takes the same DPP second word (v_max_f32 into v0),
which gives llvm-mc's own text for the two sources and the DPP controls; each such line then gets the compare's
name, its VCC destination and the real encoding back, so the translator reads the instruction and checks its
encoding against the blob. A word that does not come out as that stand-in at an instruction boundary was not an
instruction (it sits in a literal or in data): it is left as it was and the pass runs again without it."""
words = vopc_dpp_words(code, invalid_offsets(err))
while words:
patched = bytearray(code)
for at, (_, w0, _) in words.items():
struct.pack_into("<I", patched, at, STAND_IN << 25 | (w0 & 0x1FFFF))
asm, bad, err2 = run_mc(bytes(patched), mcpu, wave32)
if not asm.strip():
return None
invalid, pos, hit, lines = invalid_offsets(err2), 0, set(), []
for line in asm.split("\n"):
enc = ENCODING.search(line)
if not enc:
lines.append(line)
continue
while pos in invalid:
pos += 4
text = line[:enc.start()].strip()
if pos in words and text.startswith(STAND_IN_TEXT):
hit.add(pos)
name, w0, w1 = words[pos]
operands = text[len(STAND_IN_TEXT):]
# v_cmpx writes EXEC and prints no destination; a compare prints VCC as llvm-mc does here, vcc_lo.
text = f"{name}_dpp {'' if name.startswith('v_cmpx_') else 'vcc_lo, '}{operands}"
original = ",".join(f"0x{b:02x}" for b in struct.pack("<2I", w0, w1))
line = f"\t{text:<40}; encoding: [{original}]"
lines.append(line)
pos += len(enc.group(1).split(","))
if hit == set(words):
return "\n".join(lines), bad, err2
words = {at: w for at, w in words.items() if at in hit}
return None
MAX_DATA_DWORDS = 1 << 16
def data_block_start(b: bytes, start: int, end: int) -> int:
"""Where a constant-data block begins at the end of the code range [start, end), or `end` if there is none.
Some blobs carry, after the last real s_endpgm, a block laid out as: s_endpgm, a dword count N, N dwords
of data, then one more s_endpgm (the one the range ends on) and s_code_end padding. The data is not code:
llvm-mc reports its words as invalid (and reads a 0xBF810000 inside it as a second program end). The
block is recognised only when the count N matches the distance to the closing s_endpgm and the dword
before the count is itself an s_endpgm, so ordinary code is not cut."""
last = end - 4
for n in range(min(MAX_DATA_DWORDS, (last - start) // 4 - 1) + 1):
count = last - 4 * (n + 1)
if count - 4 < start:
break
if struct.unpack_from("<I", b, count)[0] == n and b[count - 4:count] == ENDPGM:
return count
return end
def code_range(b: bytes):
"""The code section offset is the u32 at 0xC0; instructions start 0x28 bytes into it and run to the last
4-byte-aligned s_endpgm in the blob (some blobs hold several programs), minus a trailing constant-data
block (see data_block_start). Found by correlating against clean decodes of 154 random shaders."""
# A blob too short to hold the offset goes to the scan like any other header miss; reading it anyway raised
# struct.error, which stopped the whole run (and make_game.py at the same blob on every rerun).
off = struct.unpack_from("<I", b, 0xC0)[0] if len(b) >= 0xC4 else 0
start = off + 0x28
if 0 < off and start < len(b):
last = b.rfind(ENDPGM, start)
while last >= 0 and (last - start) % 4:
last = b.rfind(ENDPGM, start, last)
if last >= 0:
return start, data_block_start(b, start, last + 4), "header"
end = b.find(ENDPGM)
# Fallback instructions are dword-aligned in the blob. A matching byte sequence can straddle metadata words.
while end >= 0 and end % 4:
end = b.find(ENDPGM, end + 1)
return None, end + 4 if end >= 0 else None, "scan"
def one(path, out, mcpu, backend="llvm-mc"):
with open(path, "rb") as f:
b = f.read()
wave32 = fssm_header.wave_size(b) != 64
start, end, how = code_range(b)
if end is None:
return "no s_endpgm"
if start is None:
# Walk back in 4-byte steps from the end and keep the longest clean decode.
start, best = end - 4, None
for s in range(end - 4, max(0x100, end - 0x4000) - 1, -4):
_, bad, _ = disasm(b[s:end], mcpu, backend=backend, wave32=wave32)
if bad == 0:
best = s
if best is None:
return "scan failed"
start = best
asm, bad, err = disasm(b[start:end], mcpu, backend=backend, wave32=wave32)
if not asm.strip(): # a broken toolchain, not a shader: never report this as "clean"
reason = (err.strip().splitlines() or ["no output"])[0]
FAILED.append((path, reason))
return "failed"
name = os.path.splitext(os.path.basename(path))[0]
# Only a published listing can be reused after an interruption.
listing = tempfile.NamedTemporaryFile(mode="w", dir=out, prefix=".disasm-", suffix=".tmp",
encoding="utf-8", newline="\n", delete=False)
try:
with listing:
listing.write(f"; {name} code 0x{start:x}..0x{end:x} ({how}), {mcpu}, {bad} invalid words\n")
listing.write(asm)
if err.strip():
listing.write("\n; --- llvm-mc messages ---\n; " + err.strip().replace("\n", "\n; ") + "\n")
os.replace(listing.name, output_path(path, out))
finally:
if os.path.exists(listing.name):
os.unlink(listing.name)
return "clean" if bad == 0 else "invalid words"
def main():
ap = argparse.ArgumentParser()
ap.add_argument("src")
ap.add_argument("--unpack-index", help="disassemble only the FSSMs listed in this unpack index")
ap.add_argument("--out")
ap.add_argument("--skip-existing", action="store_true", help="reuse complete listings from a matching run")
ap.add_argument("--mcpu", default="gfx1013")
ap.add_argument("--backend", choices=("llvm-mc", "python"), default="llvm-mc",
help="disassembler backend: llvm-mc (default) or pure-Python RDNA2 decoder (#1901)")
ap.add_argument("--limit", type=positive_int)
ap.add_argument("--jobs", type=positive_int, default=max(1, (os.cpu_count() or 2) // 2))
a = ap.parse_args()
if os.path.isfile(a.src):
if a.unpack_index:
ap.error("--unpack-index requires an FSSM directory")
files = [a.src]
elif os.path.isdir(a.src):
try:
if a.unpack_index:
names = shader_stems(a.unpack_index, "name")
files = sorted(os.path.join(a.src, name + ".fssm") for name in names)
for path in files:
if not os.path.isfile(path):
raise ValueError(f"{a.unpack_index}: declared FSSM file is missing: {path}")
else:
with os.scandir(a.src) as entries:
files = sorted(entry.path for entry in entries
if entry.name.endswith(".fssm") and entry.is_file())
except (OSError, ValueError) as e:
fail(f"cannot list shaders in {a.src}: {e}")
elif not os.path.exists(a.src):
print(f"ERROR: {a.src}: does not exist", file=sys.stderr)
return 2
else:
fail(f"{a.src} is not a file or folder")
if not files:
fail(f"no .fssm files in {a.src}")
files = files[: a.limit] if a.limit else files
out = a.out or os.path.join(os.path.dirname(files[0]), "..", "disasm")
validate_output(out, files)
try:
os.makedirs(out, exist_ok=True)
except OSError as e:
fail(f"cannot create the output folder {out}: {e.strerror or e}")
from collections import Counter
res = Counter()
total = len(files)
every_stem = {os.path.splitext(os.path.basename(p))[0] for p in files} # before --skip-existing narrows files
if a.skip_existing:
pending = []
for path in files:
listing = output_path(path, out)
if not os.path.islink(listing) and os.path.isfile(listing) and os.path.getsize(listing) > 0:
res["reused"] += 1
else:
pending.append(path)
files = pending
if files:
if a.backend == "llvm-mc":
probe(a.mcpu)
with ThreadPoolExecutor(a.jobs) as ex:
res.update(ex.map(lambda p: one(p, out, a.mcpu, backend=a.backend), files))
print(f"{total} shaders -> {os.path.abspath(out)}: {dict(res)}")
if FAILED:
path, reason = FAILED[0]
fail(f"{len(FAILED)} shader(s) produced no disassembly, first {path}: {reason}")
if not (res["clean"] or res["invalid words"] or res["reused"]):
fail(f"no disassembly produced for {a.src}")
if os.path.isdir(a.src) and not a.limit:
# A shader dropped by a re-carve must not keep its old listing: the translator reads every .s in this folder,
# so a stale one would still be translated and indexed even though the corpus no longer has it. Prune only
# after a successful decode, so a missing llvm-mc or a failed shader never deletes a usable listing. --limit
# is a partial run on purpose and leaves the rest of the folder alone.
# Every input's stem, not just this run's `files`: --skip-existing leaves the reused listings out of `files`,
# and pruning against that list deleted every listing a resumed run had just reused.
stems = every_stem
stale = []
try:
with os.scandir(out) as entries:
stale = sorted(e.path for e in entries
if e.name.endswith(".s") and e.is_file() and e.name[:-2] not in stems)
for path in stale:
os.remove(path)
except OSError as e:
fail(f"cannot prune stale .s files in {out}: {e.strerror or e}")
if stale:
print(f"removed {len(stale)} stale .s file(s) that no .fssm in {a.src} names")
return 0
if __name__ == "__main__":
try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode
sys.stdout.reconfigure(errors="replace")
except (AttributeError, ValueError, OSError):
pass
sys.exit(main())