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

Closed
Moriz82 wants to merge 1 commit from upstream-pr/1283 into main
Moriz82 commented 2026-11-24 21:15:27 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Moriz82 on 2026-11-24T21:15:27Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5480461990 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1283). Originally posted by [Moriz82](https://github.com/Moriz82) on 2026-11-24T21:15:27Z. --- **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://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7144686585)). 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)
fin-github commented 2026-11-24 21:41:36 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fin-github on 2026-11-24T21:41:36Z.

Review: COMMENTED


  • 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).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6047867742 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1283#pullrequestreview-6047867742). Originally posted by [fin-github](https://github.com/fin-github) on 2026-11-24T21:41:36Z. **Review:** COMMENTED --- - `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).
Moriz82 commented 2026-11-25 03:41:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Moriz82 on 2026-11-25T03:41:07Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7152147230 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1283#issuecomment-7152147230). Originally posted by [Moriz82](https://github.com/Moriz82) on 2026-11-25T03:41:07Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!1283
No description provided.