tools: add auto fallback to python disassembler in shader_disasm and shader_library #1712

Merged
Tortoche merged 2 commits from upstream-pr/1712 into main 2026-11-28 13:33:23 +00:00
Tortoche commented 2026-11-25 09:15:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Tortoche on 2026-11-25T09:15:53Z.


Claim: SHADER-DISASM-AUTO-FALLBACK (#1608#issuecomment-7158019367)
Findings: #1608 (#1608#issuecomment-7156808927, #1608#issuecomment-7157792566)
Related: #998
AI-assisted: yes

Summary

  • tools/shader_disasm.py:
    • Keep backend="llvm-mc" as the default in disasm() and --backend so FSSM callers and existing tests are unchanged.
    • Add backend="auto" (and --backend auto): runs llvm-mc first; if bad == 0 and output is non-empty, returns it immediately without invoking rdna2_decode. Otherwise runs the pure-Python decoder (rdna2_decode.disasm_bytes) and returns (py_asm, py_bad, "") iff py_asm.strip() and (not asm.strip() or py_bad < bad); otherwise returns the llvm-mc result.
    • Update main() to run probe(a.mcpu) when a.backend in ("llvm-mc", "auto").
  • tools/shader_library.py:
    • Add --backend (choices=("auto", "llvm-mc", "python"), default="auto"). make_game.py is intentionally untouched to stay conflict-free with #1592 and #1708.
    • Call disasm.probe(a.mcpu) upfront before ThreadPoolExecutor when work is non-empty and a.backend in ("llvm-mc", "auto").
    • Pass backend=a.backend to disasm.disasm(...) in one(job).
  • Tests (synthetic only, zero game data):
    • tests/test_shader_disasm.py: added DisasmAutoBackendTest (test_auto_backend_falls_back_to_python_on_invalid_words, test_auto_backend_preserves_clean_llvm_mc_output, test_auto_backend_keeps_llvm_mc_when_python_not_better).
    • tests/test_shader_library.py: added test_disasm_auto_falls_back_per_program_without_regressing_clean_programs.

Verification

  • python3 -I scripts/check_no_game_data.py tools/shader_disasm.py tools/shader_library.py tests/test_shader_disasm.py tests/test_shader_library.py -> exit 0
  • python3 -m compileall -q make_game.py tools tests -> exit 0
  • python3 -I tests/test_shader_disasm.py -> Ran 34 tests in 8.384s, OK
  • python3 -I tests/test_shader_library.py -> Ran 23 tests in 0.215s, OK
  • python3 -I tests/test_library_shaders_stage.py -> Ran 17 tests in 7.336s, OK
  • python3 -I tests/test_text_write_line_endings.py -> Ran 3 tests in 15.974s, OK
  • python3 -I tests/test_ci_registration.py -> Ran 16 tests in 9.878s, OK
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5488460527 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1712). Originally posted by [Tortoche](https://github.com/Tortoche) on 2026-11-25T09:15:53Z. --- Claim: `SHADER-DISASM-AUTO-FALLBACK` (`#1608#issuecomment-7158019367`) Findings: `#1608` (`#1608#issuecomment-7156808927`, `#1608#issuecomment-7157792566`) Related: `#998` AI-assisted: yes ### Summary - **`tools/shader_disasm.py`**: - Keep `backend="llvm-mc"` as the default in `disasm()` and `--backend` so FSSM callers and existing tests are unchanged. - Add `backend="auto"` (and `--backend auto`): runs `llvm-mc` first; if `bad == 0` and output is non-empty, returns it immediately without invoking `rdna2_decode`. Otherwise runs the pure-Python decoder (`rdna2_decode.disasm_bytes`) and returns `(py_asm, py_bad, "")` iff `py_asm.strip()` and (`not asm.strip()` or `py_bad < bad`); otherwise returns the `llvm-mc` result. - Update `main()` to run `probe(a.mcpu)` when `a.backend in ("llvm-mc", "auto")`. - **`tools/shader_library.py`**: - Add `--backend` (`choices=("auto", "llvm-mc", "python")`, `default="auto"`). `make_game.py` is intentionally untouched to stay conflict-free with `#1592` and `#1708`. - Call `disasm.probe(a.mcpu)` upfront before `ThreadPoolExecutor` when `work` is non-empty and `a.backend in ("llvm-mc", "auto")`. - Pass `backend=a.backend` to `disasm.disasm(...)` in `one(job)`. - **Tests (synthetic only, zero game data)**: - `tests/test_shader_disasm.py`: added `DisasmAutoBackendTest` (`test_auto_backend_falls_back_to_python_on_invalid_words`, `test_auto_backend_preserves_clean_llvm_mc_output`, `test_auto_backend_keeps_llvm_mc_when_python_not_better`). - `tests/test_shader_library.py`: added `test_disasm_auto_falls_back_per_program_without_regressing_clean_programs`. ### Verification - `python3 -I scripts/check_no_game_data.py tools/shader_disasm.py tools/shader_library.py tests/test_shader_disasm.py tests/test_shader_library.py` -> exit 0 - `python3 -m compileall -q make_game.py tools tests` -> exit 0 - `python3 -I tests/test_shader_disasm.py` -> Ran 34 tests in 8.384s, OK - `python3 -I tests/test_shader_library.py` -> Ran 23 tests in 0.215s, OK - `python3 -I tests/test_library_shaders_stage.py` -> Ran 17 tests in 7.336s, OK - `python3 -I tests/test_text_write_line_endings.py` -> Ran 3 tests in 15.974s, OK - `python3 -I tests/test_ci_registration.py` -> Ran 16 tests in 9.878s, OK
lixonik commented 2026-11-25 10:29:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-25T10:29:18Z.

Review: COMMENTED


Independent native Windows verification at 70315b5f: all 34 disassembler and 23 shader-library tests passed. A separate CLI run with synthetic input found one gap in auto mode when llvm-mc is unavailable. Details inline.

AI-assisted review. CPU only, synthetic inputs.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6055862800 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1712#pullrequestreview-6055862800). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-25T10:29:18Z. **Review:** COMMENTED --- Independent native Windows verification at `70315b5f`: all 34 disassembler and 23 shader-library tests passed. A separate CLI run with synthetic input found one gap in auto mode when llvm-mc is unavailable. Details inline. AI-assisted review. CPU only, synthetic inputs.
lixonik commented 2026-11-25 10:29:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-25T10:29:18Z.

Code review location: tools/shader_library.py; line 449

@@ -443,6 +446,8 @@ def one(job):
                 f.write("\n; --- llvm-mc messages ---\n; " + err.strip().replace("\n", "\n; ") + "\n")
 
     if work:
+        if a.backend in ("llvm-mc", "auto"):

This upfront probe can exit before auto reaches the Python decoder. I pointed LLVM_MC at a nonexistent executable and used the repository's synthetic FSSM and shader-library fixtures. Both CLIs exited 1 with zero listings in auto mode. Explicit --backend python with the same inputs exited 0 and wrote one and four listings, respectively. This also affects shader_library.py --disasm by default. Could auto switch to the Python backend when the probe fails, while explicit llvm-mc keeps the hard error? The same check applies to shader_disasm.py.

<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4693551210 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1712#discussion_r4693551210). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-25T10:29:18Z. **Code review location:** `tools/shader_library.py`; line 449 ~~~~diff @@ -443,6 +446,8 @@ def one(job): f.write("\n; --- llvm-mc messages ---\n; " + err.strip().replace("\n", "\n; ") + "\n") if work: + if a.backend in ("llvm-mc", "auto"): ~~~~ --- This upfront probe can exit before `auto` reaches the Python decoder. I pointed `LLVM_MC` at a nonexistent executable and used the repository's synthetic FSSM and shader-library fixtures. Both CLIs exited 1 with zero listings in `auto` mode. Explicit `--backend python` with the same inputs exited 0 and wrote one and four listings, respectively. This also affects `shader_library.py --disasm` by default. Could `auto` switch to the Python backend when the probe fails, while explicit `llvm-mc` keeps the hard error? The same check applies to `shader_disasm.py`.
GeorgiyDemo commented 2026-11-25 10:33:35 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by GeorgiyDemo on 2026-11-25T10:33:35Z.


Tested 70315b5fd on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 57ba40bd6 (Python tools; no GPU):

  • pytest 9.2.0 on tests/test_shader_disasm.py + tests/test_shader_library.py: 57 passed, 28 subtests passed at this head.
  • The same two files on main: 2 failed, 55 passed, 28 subtests — test_auto_backend_falls_back_to_python_on_invalid_words and the per-program fallback in shader_library.
  • Not run: an end-to-end --backend auto pass over a real listing set with llvm-mc rejecting words — this machine has the Homebrew llvm-mc, so the fallback itself is exercised through the tests' mocks; the default llvm-mc path is unchanged by design.

AI-assisted; synthetic fixtures only, no game data.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7159862007 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1712#issuecomment-7159862007). Originally posted by [GeorgiyDemo](https://github.com/GeorgiyDemo) on 2026-11-25T10:33:35Z. --- Tested `70315b5fd` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `57ba40bd6` (Python tools; no GPU): - pytest 9.2.0 on `tests/test_shader_disasm.py` + `tests/test_shader_library.py`: **57 passed, 28 subtests passed** at this head. - The same two files on `main`: **2 failed**, 55 passed, 28 subtests — `test_auto_backend_falls_back_to_python_on_invalid_words` and the per-program fallback in `shader_library`. - Not run: an end-to-end `--backend auto` pass over a real listing set with llvm-mc rejecting words — this machine has the Homebrew `llvm-mc`, so the fallback itself is exercised through the tests' mocks; the default `llvm-mc` path is unchanged by design. AI-assisted; synthetic fixtures only, no game data.
MoonEyes commented 2026-11-28 03:00:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T03:00:04Z.


Hi @Tortoche, thanks for this pull request. It currently conflicts with main (conflicting file(s): tools/shader_disasm.py, tools/shader_library.py), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7232728534 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1712#issuecomment-7232728534). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T03:00:04Z. --- Hi @Tortoche, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `tools/shader_disasm.py`, `tools/shader_library.py`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
zmasi commented 2026-11-28 12:04:37 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T12:04:37Z.


#1712 | head fef2f758be (merged cleanly with main d48a957bb) | python -I tests/test_shader_disasm.py & python -I tests/test_shader_library.py | red on main: FAILED (failures=1) (bad=2 != 0) and FAILED (failures=1, skipped=1) (probe called 0 times) | green on PR+main: 34 tests OK and 26 tests OK (skipped=1) | Windows 11, Python 3.12.10 | AI-assisted: yes

Audited disasm and main in tools/shader_disasm.py and tools/shader_library.py (adding --backend auto so clean llvm-mc disassemblies are preserved untouched while programs with invalid_words > 0 fall back per-program to rdna2_decode only when py_bad < bad, and falling back to "python" when probe() fails). Also verified tests/test_ci_registration.py (18 OK), tests/test_text_write_line_endings.py (3 OK), and scripts/check_no_game_data.py.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238976531 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1712#issuecomment-7238976531). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T12:04:37Z. --- #1712 | head fef2f758be7ee33b2326d4cc427088a2a88c7375 (merged cleanly with main d48a957bb) | `python -I tests/test_shader_disasm.py` & `python -I tests/test_shader_library.py` | red on main: `FAILED (failures=1)` (`bad=2 != 0`) and `FAILED (failures=1, skipped=1)` (`probe` called 0 times) | green on PR+main: `34 tests OK` and `26 tests OK (skipped=1)` | Windows 11, Python 3.12.10 | AI-assisted: yes Audited `disasm` and `main` in `tools/shader_disasm.py` and `tools/shader_library.py` (adding `--backend auto` so clean `llvm-mc` disassemblies are preserved untouched while programs with `invalid_words > 0` fall back per-program to `rdna2_decode` only when `py_bad < bad`, and falling back to `"python"` when `probe()` fails). Also verified `tests/test_ci_registration.py` (`18 OK`), `tests/test_text_write_line_endings.py` (`3 OK`), and `scripts/check_no_game_data.py`.
MoonEyes commented 2026-11-28 13:33:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T13:33:26Z.


Merged after local re-verification: this PR merged onto main e06955b7f without conflict, and its test file(s) (tests/test_shader_disasm.py, tests/test_shader_library.py) pass there. Earlier today the same tests failed on main without the PR (red/green) and a bundle with other open PRs showed no new failures. check_no_game_data.py is clean. Windows 11, Python 3.14; no GPU needed for these tests.

AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7240019768 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1712#issuecomment-7240019768). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T13:33:26Z. --- Merged after local re-verification: this PR merged onto `main` e06955b7f without conflict, and its test file(s) (`tests/test_shader_disasm.py`, `tests/test_shader_library.py`) pass there. Earlier today the same tests failed on `main` without the PR (red/green) and a bundle with other open PRs showed no new failures. `check_no_game_data.py` is clean. Windows 11, Python 3.14; no GPU needed for these tests. _AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes._
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!1712
No description provided.