From 47d9c3b8fb57e54798efe7ea6902a0500961d977 Mon Sep 17 00:00:00 2001 From: Moriz82 <72836413+Moriz82@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:15:06 -0600 Subject: [PATCH] shader_ir: read a listing a line at a time (TOOLS-DISASM-BOUNDED-LISTING) parse_file read the whole listing into memory, split it into lines and joined them again to count the "invalid instruction encoding" messages. llvm-mc prints its whole input line under each of those messages, so the messages of a program with many undecodable words are many times the size of its instructions, and parse_file needed about twice the file size in memory to find the instructions at its start. Read the file one line at a time. Lines are split as str.splitlines() split the whole text, the first line is still the header unless it is an instruction, and the messages after "; ---" are counted without being kept. The result is the same for every input. tests/test_shader_ir.py gets a synthetic listing with 400 messages (4.5 MB). The base peaks at 9.2 MB for it and fails the new check; this change peaks at 0.17 MB. 20,000 random synthetic listings parse to the same result before and after. The standard-library tests and the translator suite add no failing file. AI-assisted (Claude Code). Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 --- tests/test_shader_ir.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/test_shader_ir.py b/tests/test_shader_ir.py index 0a68023666..0b728dcce3 100644 --- a/tests/test_shader_ir.py +++ b/tests/test_shader_ir.py @@ -10,6 +10,7 @@ import subprocess import sys import tempfile +import tracemalloc import unittest from unittest import mock @@ -329,6 +330,40 @@ def test_header_only_file_has_no_instructions(self): self.assertEqual(info["insns"], []) self.assertEqual(info["invalid"], 0) + def test_empty_file_and_last_line_without_a_newline(self): + info = shader_ir.parse_file(self.write("")) + self.assertEqual((info["code_start"], info["code_end"], info["insns"], info["invalid"]), (0, None, [], 0)) + info = shader_ir.parse_file(self.write("; tail code 0x20..0x28\n\ts_endpgm ; encoding: [0x00,0x00,0x81,0xbf]")) + self.assertEqual((info["code_start"], info["code_end"]), (0x20, 0x28)) + self.assertEqual([(i["op"], i["pc"], i["enc"]) for i in info["insns"]], [("s_endpgm", 0x20, "000081bf")]) + + def test_long_message_tail_is_counted_a_line_at_a_time(self): + # llvm-mc prints its whole input line under every invalid word. The messages of a program given as one line + # are then far larger than its instructions: 400 bad words in 2,048 synthetic bytes leave about 6 MB here. + # They are counted, but the listing is not held in memory for it (the read buffer is 128 KiB at most). + echoed = " ".join(["0xff"] * 2048) + bad = 400 + path = self.write( + "; big code 0x00000000..0x00000800\n" + "\ts_nop 0 ; encoding: [0x00,0x00,0x80,0xbf]\n" + "\ts_endpgm ; encoding: [0x00,0x00,0x81,0xbf]\n" + "\n; --- llvm-mc messages ---\n" + + "".join(f"; :1:{1 + 5 * k}: warning: invalid instruction encoding\n; {echoed}\n" + f"; {' ' * (5 * k)}^\n" for k in range(bad)) + ) + size = os.path.getsize(path) + self.assertGreater(size, 4 << 20) + tracemalloc.start() + try: + info = shader_ir.parse_file(path) + peak = tracemalloc.get_traced_memory()[1] + finally: + tracemalloc.stop() + self.assertEqual(info["invalid"], bad) + self.assertEqual([(i["op"], i["pc"]) for i in info["insns"]], [("s_nop", 0), ("s_endpgm", 4)]) + self.assertEqual(info["code_end"], 0x800) + self.assertLess(peak, size // 4, "parse_file held the message tail in memory") + class CliInputTest(unittest.TestCase): """The tool read sys.argv by hand: no arguments, a bad --limit or a missing folder ended in a traceback."""