From f5502513a77eaeac5372eba5927f0d471f622691 Mon Sep 17 00:00:00 2001 From: beamdotdev <56007474+beamdotdev@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:30:37 -0600 Subject: [PATCH] shader_disasm: hand llvm-mc its input in lines of 64 bytes, not one long line llvm-mc slows down sharply on one long input line. disasm() joined each program's bytes with spaces into a single line, so large programs took tens of seconds each: a 90 KB library program took 58.6 s that way and 0.2 s split into lines of 64 bytes, with byte-identical disassembly (the same .s text and the same count of undecodable words; only the line/column numbers in llvm-mc's diagnostics change). make_game.py's pipeline-library step (2,684 programs from shaders.rpf) went from about one program per minute to about 1.5 minutes for all of them. tests/test_shader_disasm_input_lines.py stubs subprocess.run and checks the text llvm-mc would receive: lines of at most 64 bytes, the same bytes in order, no empty or trailing lines, and inputs of up to 64 bytes (the probe included) unchanged. It needs no llvm-mc and no game data. AI-assisted. Co-Authored-By: Claude Opus 5.5 --- tests/test_shader_disasm_input_lines.py | 73 +++++++++++++++++++++++++ tools/shader_disasm.py | 4 +- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/test_shader_disasm_input_lines.py diff --git a/tests/test_shader_disasm_input_lines.py b/tests/test_shader_disasm_input_lines.py new file mode 100644 index 0000000000..36d4bce790 --- /dev/null +++ b/tests/test_shader_disasm_input_lines.py @@ -0,0 +1,73 @@ +"""shader_disasm.disasm() hands llvm-mc its input in lines of at most 64 bytes, never as one long line. + +llvm-mc slows down sharply on one long input line: a 90 KB program took 58.6 s as a single line and 0.2 s in lines +of 64 bytes, with byte-identical disassembly (only the line/column numbers in its diagnostics change). The pipeline +library step disassembles 2,684 programs this way, so one line per program made it take hours. + +subprocess.run is stubbed, so this needs no llvm-mc and no game data: it checks the text llvm-mc would receive. + + python -I tests/test_shader_disasm_input_lines.py +""" +import os +import subprocess +import sys +import unittest + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +sys.path.insert(0, os.path.join(ROOT, "tools")) +os.environ.setdefault("LLVM_MC", "llvm-mc") # only builds the command line; nothing is started +import shader_disasm # noqa: E402 + + +class InputLinesTest(unittest.TestCase): + def capture(self, code: bytes) -> str: + """The `input` text disasm() passes to llvm-mc for `code`.""" + seen = {} + + def fake_run(cmd, input=None, **kwargs): + seen["input"] = input + return subprocess.CompletedProcess(cmd, 0, stdout="\ts_endpgm\n", stderr="") + + real = shader_disasm.subprocess.run + shader_disasm.subprocess.run = fake_run + try: + asm, bad, err = shader_disasm.disasm(code, "gfx1013") + finally: + shader_disasm.subprocess.run = real + self.assertEqual((asm, bad, err), ("\ts_endpgm\n", 0, "")) + return seen["input"] + + @staticmethod + def tokens(text: str) -> list: + return text.split() + + def test_long_program_is_split_into_lines_of_64_bytes(self): + code = bytes(range(256)) * 40 # 10,240 bytes + text = self.capture(code) + lines = text.split("\n") + self.assertEqual(len(lines), 160) + self.assertTrue(all(len(line.split(" ")) == 64 for line in lines), "every line holds 64 bytes") + self.assertEqual(self.tokens(text), [f"0x{b:02x}" for b in code], "the same bytes, in order") + + def test_partial_last_line(self): + code = bytes(range(100)) + lines = self.capture(code).split("\n") + self.assertEqual([len(line.split(" ")) for line in lines], [64, 36]) + + def test_short_input_stays_one_line(self): + # The probe (s_endpgm) and any program of up to 64 bytes go on exactly as before. + self.assertEqual(self.capture(shader_disasm.ENDPGM), shader_disasm.PROBE) + code = bytes(range(64)) + self.assertEqual(self.capture(code), " ".join(f"0x{b:02x}" for b in code)) + + def test_no_empty_or_trailing_lines(self): + for size in (1, 63, 64, 65, 128, 129): + with self.subTest(size=size): + text = self.capture(bytes(size)) + self.assertFalse(text.endswith("\n")) + self.assertNotIn("\n\n", text) + self.assertEqual(len(self.tokens(text)), size) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/shader_disasm.py b/tools/shader_disasm.py index 373d36f144..3ec035e4ec 100644 --- a/tools/shader_disasm.py +++ b/tools/shader_disasm.py @@ -94,7 +94,9 @@ 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, "" - text = " ".join(f"0x{b:02x}" for b in code) + # 64 bytes per line: llvm-mc slows down sharply on one long input line (a 90 KB program took 58.6 s as one line + # and 0.2 s split like this, with byte-identical output); only its diagnostics' line/column numbers change. + text = "\n".join(" ".join(f"0x{b:02x}" for b in code[i:i + 64]) for i in range(0, len(code), 64)) try: p = subprocess.run(MC + ["--disassemble", f"-triple={TRIPLE}", f"-mcpu={mcpu}", "-show-encoding"], input=text, capture_output=True, text=True, errors="replace",