shader_ir: read a listing a line at a time (TOOLS-DISASM-BOUNDED-LISTING) #1283

Closed
github-bot wants to merge 1 commit from gh-pr/1283/head into gh-pr/1283/base
Owner

@⁠Moriz82 on GitHub · 2026-11-24 21:15 UTC · original
split/tools-disasm-bounded-listing → main · +35 −0 · 1 files · 1 commits · closed without merging

Rebased onto bfef489c at 47d9c3b8. Main already streams listings and retains the diagnostic offsets. This diff adds the large-tail and empty-file checks, which pass with the current parser. The shader IR suite passed 41 tests.

Claimed in the coordination issue as: TOOLS-DISASM-BOUNDED-LISTING (claim).

Part of the split of #984 into small pull requests.

Depends on: none
Overlaps: #984 is the parent integration. #623 (open, OLD-10020) hands llvm-mc its input in lines of 64 bytes in tools/shader_disasm.py; that also bounds what llvm-mc repeats under each message, so the integration's own change to that line (one dword per line) is left to #623 and is not repeated here. This pull request is the reader's side, tools/shader_ir.py, which #623 does not touch. #365 (open) rewords the docstring of the same file, 230 lines above this hunk. Both merge cleanly with this branch (each applied to the base and merged three-way).

What and why

shader_ir.parse_file read the whole listing into one string, split it into lines and, after the instructions, joined the lines again to count the invalid instruction encoding messages. The messages can be far larger than the instructions: llvm-mc prints its whole input line under every word it cannot decode, and shader_disasm.py gives it the program as one line and writes its messages to the end of the listing. A program of N bytes with B undecodable words leaves about 5·N·B bytes of messages (plus the caret lines), and parse_file held about twice the file in memory to read the few instructions at its start. Every tool that parses listings goes through it, the translator included.

parse_file now reads one line at a time. The messages after ; --- are counted and dropped. Nothing else changes:

  • lines are split as str.splitlines() split the whole text (each line of the file is passed through it), so a listing with a form feed or another of its separators parses as before;
  • the first line is still the header, and still an instruction when it starts with a tab;
  • the result dictionary has the same keys in the same order.

With #623 new listings get short message lines. Listings written before it, and any program with very many undecodable words, still have long tails, and those are the ones this is for.

Tested

Linux (CachyOS, kernel 7.3.5), Python 3.14. One commit on 6293c1c4f.

  • python3 -I tests/test_shader_ir.py: 41 tests pass (39 before, 2 new). The new test_long_message_tail_is_counted_a_line_at_a_time writes a synthetic listing of two instructions and 400 messages that each repeat a 2,048-byte input line (4,519,727 bytes), and requires tracemalloc's peak during parse_file to stay under a quarter of the file size. Peak with this change: 172,394 bytes (Python 3.14 reads through a 128 KiB buffer). The other new test covers an empty file and a last line without a newline.
  • The same test file with the base commit's tools/shader_ir.py (an export of 6293c1c4f with only the test file replaced): 1 failure, that test, 9229703 not less than 1129931. The other 40 pass there.
  • With 1,600 messages (22,879,127 bytes, not part of the test): base peak 46,124,671 bytes, this change 178,412 bytes.
  • A differential run that is not part of the change: 20,000 random listings built from 23 synthetic lines (headers, instructions with and without encodings, directives, ; ---, message lines, lines with form feed, NEL, U+2028 and U+001E, joined with LF, CRLF or CR, with and without a final newline) give equal dictionaries, key order included, from the base's parse_file and this one.
  • Files that call parse_file: tests/test_cf_ref.py (26), tests/test_cf_ref_edge_fixes.py (5), tests/test_wave_diff.py (12), tests/test_sampler_source_census.py (30), tests/test_wave32_pixel_scan.py (13), tests/test_rdna2spirv_decode.py (25), tests/semantic/test_missing_encoding.py (3) and tests/test_shader_ir_parse.py (3): all pass.
  • The CI loop as ci.yml runs it (compileall, every tests/test_*.py outside tests/semantic, the semantic suite, the tracked-file guard), on the committed tree: one failing file, tests/test_imhex_patterns.py, which fails on the base commit too and is the only one there. Semantic suite: 470 files, 6,168 tests, 0 files failed, the same counts as the base.
  • python3 -I scripts/check_no_game_data.py: passes.

Not run: a real corpus (no listing of a game shader was read for this), native Windows or macOS, the runtime build (no C++ or build file changes).

GPUs tested: none (a text parser).

AI-assisted: yes (Claude Code). The numbers above come from runs on the machine named.

Checklist

  • Based on upstream main 6293c1c4f, one commit.
  • One topic: one function and its tests.
  • No game data, keys or decryption; the listings in the tests are hand-written.
  • No translator rule changed, so no row in docs/design.md.

🤖 Generated with Claude Code

**@⁠Moriz82** on GitHub · 2026-11-24 21:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1283) `split/tools-disasm-bounded-listing` → `main` · +35 −0 · 1 files · 1 commits · closed without merging **Rebased onto `bfef489c` at `47d9c3b8`.** Main already streams listings and retains the diagnostic offsets. This diff adds the large-tail and empty-file checks, which pass with the current parser. The shader IR suite passed 41 tests. Claimed in the coordination issue as: TOOLS-DISASM-BOUNDED-LISTING ([claim](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14)). Part of the split of #984 into small pull requests. Depends on: none Overlaps: #984 is the parent integration. #623 (open, OLD-10020) hands llvm-mc its input in lines of 64 bytes in `tools/shader_disasm.py`; that also bounds what llvm-mc repeats under each message, so the integration's own change to that line (one dword per line) is left to #623 and is not repeated here. This pull request is the reader's side, `tools/shader_ir.py`, which #623 does not touch. #365 (open) rewords the docstring of the same file, 230 lines above this hunk. Both merge cleanly with this branch (each applied to the base and merged three-way). ### What and why `shader_ir.parse_file` read the whole listing into one string, split it into lines and, after the instructions, joined the lines again to count the `invalid instruction encoding` messages. The messages can be far larger than the instructions: llvm-mc prints its whole input line under every word it cannot decode, and `shader_disasm.py` gives it the program as one line and writes its messages to the end of the listing. A program of N bytes with B undecodable words leaves about 5·N·B bytes of messages (plus the caret lines), and `parse_file` held about twice the file in memory to read the few instructions at its start. Every tool that parses listings goes through it, the translator included. `parse_file` now reads one line at a time. The messages after `; ---` are counted and dropped. Nothing else changes: - lines are split as `str.splitlines()` split the whole text (each line of the file is passed through it), so a listing with a form feed or another of its separators parses as before; - the first line is still the header, and still an instruction when it starts with a tab; - the result dictionary has the same keys in the same order. With #623 new listings get short message lines. Listings written before it, and any program with very many undecodable words, still have long tails, and those are the ones this is for. ### Tested Linux (CachyOS, kernel 7.3.5), Python 3.14. One commit on `6293c1c4f`. - `python3 -I tests/test_shader_ir.py`: 41 tests pass (39 before, 2 new). The new `test_long_message_tail_is_counted_a_line_at_a_time` writes a synthetic listing of two instructions and 400 messages that each repeat a 2,048-byte input line (4,519,727 bytes), and requires `tracemalloc`'s peak during `parse_file` to stay under a quarter of the file size. Peak with this change: 172,394 bytes (Python 3.14 reads through a 128 KiB buffer). The other new test covers an empty file and a last line without a newline. - The same test file with the base commit's `tools/shader_ir.py` (an export of `6293c1c4f` with only the test file replaced): **1 failure**, that test, `9229703 not less than 1129931`. The other 40 pass there. - With 1,600 messages (22,879,127 bytes, not part of the test): base peak 46,124,671 bytes, this change 178,412 bytes. - A differential run that is not part of the change: 20,000 random listings built from 23 synthetic lines (headers, instructions with and without encodings, directives, `; ---`, message lines, lines with form feed, NEL, U+2028 and U+001E, joined with LF, CRLF or CR, with and without a final newline) give equal dictionaries, key order included, from the base's `parse_file` and this one. - Files that call `parse_file`: `tests/test_cf_ref.py` (26), `tests/test_cf_ref_edge_fixes.py` (5), `tests/test_wave_diff.py` (12), `tests/test_sampler_source_census.py` (30), `tests/test_wave32_pixel_scan.py` (13), `tests/test_rdna2spirv_decode.py` (25), `tests/semantic/test_missing_encoding.py` (3) and `tests/test_shader_ir_parse.py` (3): all pass. - The CI loop as `ci.yml` runs it (compileall, every `tests/test_*.py` outside `tests/semantic`, the semantic suite, the tracked-file guard), on the committed tree: one failing file, `tests/test_imhex_patterns.py`, which fails on the base commit too and is the only one there. Semantic suite: 470 files, 6,168 tests, 0 files failed, the same counts as the base. - `python3 -I scripts/check_no_game_data.py`: passes. Not run: a real corpus (no listing of a game shader was read for this), native Windows or macOS, the runtime build (no C++ or build file changes). GPUs tested: none (a text parser). AI-assisted: yes (Claude Code). The numbers above come from runs on the machine named. ### Checklist - [x] Based on upstream main `6293c1c4f`, one commit. - [x] One topic: one function and its tests. - [x] No game data, keys or decryption; the listings in the tests are hand-written. - [x] No translator rule changed, so no row in `docs/design.md`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:45:51 +00:00
github-bot closed this pull request 2026-11-26 09:45:52 +00:00
Author
Owner

@⁠fin-github on GitHub · 2026-11-24 21:41 UTC · original · reviewed

  • python -I tests/test_shader_ir.py on the PR: 41 tests OK.
  • The PR's test file against main (4a50cafc7): test_long_message_tail_is_counted_a_line_at_a_time fails (peak
    memory over a quarter of the file), so the test catches the old whole-file read.

I checked the rewrite against the old function by hand: the header is still taken only from line 0, a line 0 that
starts with a tab is still parsed as an instruction, instruction parsing still stops at ; ---, and
invalid instruction encoding is still counted over the whole file, both before and after that marker. Passing each
line of the file through splitlines() also keeps a blank line ("\n".splitlines() == [""]). Looks good to me.

Tested on Windows 11 Pro 26200, CPython 3.14.4, applied on main 4a50cafc7. No GPU or game files used. AI-assisted (Claude Code).

**@⁠fin-github** on GitHub · 2026-11-24 21:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1283#pullrequestreview-6047867742) · **reviewed** - `python -I tests/test_shader_ir.py` on the PR: 41 tests OK. - The PR's test file against `main` (4a50cafc7): `test_long_message_tail_is_counted_a_line_at_a_time` fails (peak memory over a quarter of the file), so the test catches the old whole-file read. I checked the rewrite against the old function by hand: the header is still taken only from line 0, a line 0 that starts with a tab is still parsed as an instruction, instruction parsing still stops at `; ---`, and `invalid instruction encoding` is still counted over the whole file, both before and after that marker. Passing each line of the file through `splitlines()` also keeps a blank line (`"\n".splitlines() == [""]`). Looks good to me. Tested on Windows 11 Pro 26200, CPython 3.14.4, applied on main 4a50cafc7. No GPU or game files used. AI-assisted (Claude Code).
Author
Owner

@⁠Moriz82 on GitHub · 2026-11-25 03:41 UTC · original

Thanks for checking the memory regression. #984 brought streaming reads into main along with diagnostic offsets. The rebase keeps that parser and adds the large-tail and empty-file checks from this PR. All 41 shader IR tests passed on 47d9c3b8.

AI-assisted.

**@⁠Moriz82** on GitHub · 2026-11-25 03:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1283#issuecomment-7152147230) Thanks for checking the memory regression. #984 brought streaming reads into main along with diagnostic offsets. The rebase keeps that parser and adds the large-tail and empty-file checks from this PR. All 41 shader IR tests passed on `47d9c3b8`. AI-assisted.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.