tools/shader_meta: fold signed min/max, s_bfe_i32, s_sext_i32, s_pack_* and guard SGPR width #1865

Merged
kirigayakazima merged 1 commit from upstream-pr/1865 into main 2026-11-28 17:57:52 +00:00
kirigayakazima commented 2026-11-25 13:39:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-25T13:39:05Z.


Claimed in the coordination issue as: SHADER-META-SCALAR-PACK-SEXT-SIGNED-FOLD-AND-WIDTH-GUARD (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164029615)

What and why

  • Add bit-exact constant folding matching tools/rdna2spirv.py in tools/shader_meta.py for signed scalar min/max (s_min_i32, s_max_i32), signed bitfield extraction (s_bfe_i32), 16-bit half packing (s_pack_ll_b32_b16, s_pack_lh_b32_b16, s_pack_hh_b32_b16), and unary sign extension (s_sext_i32_i8, s_sext_i32_i16).
  • Guard scalar write and stride propagation branches (s_add_*, s_sub_*, s_addc_u32, s_subb_u32, and binary ALU folding) to require single-SGPR (n == 1) destination and stride-source operands so malformed multi-SGPR operands clear prior state cleanly without attaching bogus constant or stride tags.
  • Add positive and negative unit tests across tests/test_shader_meta.py and tests/test_shader_meta_operands.py.

Tested

  • Patched: python -I tests/test_shader_meta.py -v -> Ran 30 tests in 0.200s (OK); python -I tests/test_shader_meta_operands.py -v -> Ran 6 tests in 0.000s (OK).
  • Baseline check (upstream/main 5e95fa2f with updated test files copied into detached worktree): test_shader_meta.py -> Ran 30 tests in 0.267s (FAILED (failures=17)); test_shader_meta_operands.py -> Ran 6 tests in 0.001s (FAILED (failures=3)).
  • Repo gates: python -I tests/test_ci_registration.py (Ran 16 tests in 5.029s, OK), python -I tests/test_text_write_line_endings.py (Ran 3 tests in 12.679s, OK), python -I scripts/check_no_game_data.py (exit 0), python -I -m compileall -q (exit 0), git diff --check (exit 0).

GPUs tested: n/a (static shader metadata constant folding and operand width validation tests)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5492427661 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1865). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-25T13:39:05Z. --- Claimed in the coordination issue as: SHADER-META-SCALAR-PACK-SEXT-SIGNED-FOLD-AND-WIDTH-GUARD (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164029615) ### What and why - Add bit-exact constant folding matching `tools/rdna2spirv.py` in `tools/shader_meta.py` for signed scalar min/max (`s_min_i32`, `s_max_i32`), signed bitfield extraction (`s_bfe_i32`), 16-bit half packing (`s_pack_ll_b32_b16`, `s_pack_lh_b32_b16`, `s_pack_hh_b32_b16`), and unary sign extension (`s_sext_i32_i8`, `s_sext_i32_i16`). - Guard scalar write and stride propagation branches (`s_add_*`, `s_sub_*`, `s_addc_u32`, `s_subb_u32`, and binary ALU folding) to require single-SGPR (`n == 1`) destination and stride-source operands so malformed multi-SGPR operands clear prior state cleanly without attaching bogus constant or stride tags. - Add positive and negative unit tests across `tests/test_shader_meta.py` and `tests/test_shader_meta_operands.py`. ### Tested - Patched: `python -I tests/test_shader_meta.py -v` -> `Ran 30 tests in 0.200s` (`OK`); `python -I tests/test_shader_meta_operands.py -v` -> `Ran 6 tests in 0.000s` (`OK`). - Baseline check (`upstream/main` `5e95fa2f` with updated test files copied into detached worktree): `test_shader_meta.py` -> `Ran 30 tests in 0.267s` (`FAILED (failures=17)`); `test_shader_meta_operands.py` -> `Ran 6 tests in 0.001s` (`FAILED (failures=3)`). - Repo gates: `python -I tests/test_ci_registration.py` (`Ran 16 tests in 5.029s`, `OK`), `python -I tests/test_text_write_line_endings.py` (`Ran 3 tests in 12.679s`, `OK`), `python -I scripts/check_no_game_data.py` (exit 0), `python -I -m compileall -q` (exit 0), `git diff --check` (exit 0). GPUs tested: n/a (static shader metadata constant folding and operand width validation tests) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
GeorgiyDemo commented 2026-11-25 14:03:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by GeorgiyDemo on 2026-11-25T14:03:40Z.


Tested e0a8694be on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 71a769d7d (Python shader-metadata tool; no GPU):

  • pytest 9.2.0 on tests/test_shader_meta.py + tests/test_shader_meta_operands.py: 36 passed, 86 subtests passed at this head.
  • The same two files on main: 20 failed, 36 passed, 66 subtests — the signed min/max, s_bfe_i32, s_sext_i32, s_pack_* folds and the wide-scalar-destination width guard are new.
  • Not run: real shader metadata dumps; the tests build the instructions synthetically.

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7164696093 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1865#issuecomment-7164696093). Originally posted by [GeorgiyDemo](https://github.com/GeorgiyDemo) on 2026-11-25T14:03:40Z. --- Tested `e0a8694be` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `71a769d7d` (Python shader-metadata tool; no GPU): - pytest 9.2.0 on `tests/test_shader_meta.py` + `tests/test_shader_meta_operands.py`: **36 passed, 86 subtests passed** at this head. - The same two files on `main`: **20 failed**, 36 passed, 66 subtests — the signed min/max, `s_bfe_i32`, `s_sext_i32`, `s_pack_*` folds and the wide-scalar-destination width guard are new. - Not run: real shader metadata dumps; the tests build the instructions synthetically. AI-assisted; synthetic fixtures only, no game data.
DeadlyChirp commented 2026-11-25 15:32:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DeadlyChirp on 2026-11-25T15:32:11Z.


Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

python -m pytest -q tests/test_shader_meta.py tests/test_shader_meta_operands.py:

  • PR: 36 passed, 86 subtests passed. main: 33 passed, 56 subtests passed. The new tests pass on Windows.

(AI-assisted test run; output checked by hand.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7166994644 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1865#issuecomment-7166994644). Originally posted by [DeadlyChirp](https://github.com/DeadlyChirp) on 2026-11-25T15:32:11Z. --- Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. `python -m pytest -q tests/test_shader_meta.py tests/test_shader_meta_operands.py`: - PR: 36 passed, 86 subtests passed. main: 33 passed, 56 subtests passed. The new tests pass on Windows. (AI-assisted test run; output checked by hand.)
MSedgeMC commented 2026-11-25 19:28:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MSedgeMC on 2026-11-25T19:28:52Z.

Review: APPROVED


Verified on the PR branch (clean, 1 commit ahead of main): python3 -m unittest tests.test_shader_meta -> 30 tests OK, including the new cases for signed s_min/s_max, s_bfe_i32 width clamping/sign extension, s_sext_i32_i8/i16, and s_pack_ll/lh/hh constant folding into literal samplers, plus negative cases where an unknown source must not fold. Genuine translator improvement with comprehensive 32-bit edge cases. Python-only, no game data.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6063726467 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1865#pullrequestreview-6063726467). Originally posted by [MSedgeMC](https://github.com/MSedgeMC) on 2026-11-25T19:28:52Z. **Review:** APPROVED --- Verified on the PR branch (clean, 1 commit ahead of main): python3 -m unittest tests.test_shader_meta -> 30 tests OK, including the new cases for signed s_min/s_max, s_bfe_i32 width clamping/sign extension, s_sext_i32_i8/i16, and s_pack_ll/lh/hh constant folding into literal samplers, plus negative cases where an unknown source must not fold. Genuine translator improvement with comprehensive 32-bit edge cases. Python-only, no game data.
MoonEyes commented 2026-11-28 17:57:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:57:49Z.


Red/green: tests/test_shader_meta.py and tests/test_shader_meta_operands.py fail 17 and 3 assertions on main (signed min/max, s_bfe_i32, s_sext_i32, s_pack_* folds, wide SGPR destination guard) and pass with the PR.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243422405 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1865#issuecomment-7243422405). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:57:49Z. --- Red/green: tests/test_shader_meta.py and tests/test_shader_meta_operands.py fail 17 and 3 assertions on main (signed min/max, s_bfe_i32, s_sext_i32, s_pack_* folds, wide SGPR destination guard) and pass with the PR. Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
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!1865
No description provided.