cpu_ref: reject multi-dword VGPR sources, validate operands under EXEC=0, and report failing PC (CPU-REF-OPERAND-VALIDATION-AND-PC) #1457

Open
github-bot wants to merge 1 commit from github-bot/gh-1457 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 00:36 UTC · original
IlyassRS3:fix/cpu-ref-operand-validation-and-pc → main · +178 −33 · 2 files · 1 commits · open

Claimed in the coordination issue as: CPU-REF-OPERAND-VALIDATION-AND-PC

Summary

Fixes five operand-validation and state-reporting bugs in tools/cpu_ref.py (which is also the base Machine class for tools/cf_ref.py):

  1. Multi-dword VGPR sources silently accepted: _int_src and _float_src rejected multi-dword SGPR sources (n != 1) and _write_vgpr rejected multi-dword VGPR destinations, but multi-dword VGPR sources (v[1:2]) were silently narrowed to v1. Both helpers now raise Unsupported.
  2. Operand validation skipped when EXEC == 0: every vector instruction looped over self._active_lanes() without checking operands when no lanes were active, so unsupported destinations (v_mov_b32 s0, v1), sources (v_mov_b32 v0, m0, v_add_f32 v0, v[1:2], v3), or conditions (v_cndmask_b32_e64 v0, v1, v2, m0) silently passed under exec=0. Added _vector_lanes() / _validate_vector_operands() so operands are checked even when all lanes are masked off (zero extra work when lanes are active).
  3. Stale PC in Unsupported exceptions: Machine.run() updated self.pc only after self.step(ins), so operand errors raised by _int_src/_float_src/_write_vgpr/_write_scalar_dst/_mask_bit reported pc 0x0 (or the prior instruction's next_pc) instead of ins["pc"]. Both run() and step() now set self.pc = ins["pc"] before executing.
  4. Scalar scc clobbered on invalid destination: s_add_u32, s_lshl_b32, and s_lshr_b32 mutated self.scc before _write_scalar_dst() validated the destination; now _check_scalar_dst() runs first.
  5. CLI/input validation: --lane N passed together with --lanes 64 was silently ignored on the CLI (despite Machine.__init__ rejecting it), and negative/boolean register indices (--sgpr -1=0, {"sgpr": {-1: 0}}, {"lanes": True}) were accepted.

Testing

  • AI-assisted: yes
  • Host: macOS Apple M5 (arm64), Python 3.14 (CPU-only, hand-written .s fixtures only, no game data)
  • python3 -m compileall -q make_game.py tools tests -> clean
  • python3 -I scripts/check_no_game_data.py tools/cpu_ref.py tests/test_cpu_ref.py -> clean
  • python3 -I tests/test_cpu_ref.py -v -> 39/39 OK (5 new tests)
  • python3 -I tests/test_cf_ref.py -> 26/26 OK
  • python3 -I tests/test_cf_ref_edge_fixes.py -> 5/5 OK
  • python3 -I tests/test_wave_diff.py -> 12/12 OK
  • python3 -m unittest discover -s tests -p "test_*diff.py" -> 362/362 OK
  • python3 -I tests/test_missing_input_exit_codes.py -> 2/2 OK
  • python3 -I tests/test_text_write_line_endings.py -> 3/3 OK
  • python3 -I tests/test_ci_registration.py -> 16/16 OK
  • Mutation proof (running the new tests/test_cpu_ref.py on unpatched upstream/main b4b7756e8): FAILED (failures=25) across all 5 new tests and their subtests.
**@⁠IlyassRS3** on GitHub · 2026-11-25 00:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1457) `IlyassRS3:fix/cpu-ref-operand-validation-and-pc` → `main` · +178 −33 · 2 files · 1 commits · open Claimed in the coordination issue as: CPU-REF-OPERAND-VALIDATION-AND-PC ## Summary Fixes five operand-validation and state-reporting bugs in `tools/cpu_ref.py` (which is also the base `Machine` class for `tools/cf_ref.py`): 1. **Multi-dword VGPR sources silently accepted**: `_int_src` and `_float_src` rejected multi-dword SGPR sources (`n != 1`) and `_write_vgpr` rejected multi-dword VGPR destinations, but multi-dword VGPR sources (`v[1:2]`) were silently narrowed to `v1`. Both helpers now raise `Unsupported`. 2. **Operand validation skipped when `EXEC == 0`**: every vector instruction looped over `self._active_lanes()` without checking operands when no lanes were active, so unsupported destinations (`v_mov_b32 s0, v1`), sources (`v_mov_b32 v0, m0`, `v_add_f32 v0, v[1:2], v3`), or conditions (`v_cndmask_b32_e64 v0, v1, v2, m0`) silently passed under `exec=0`. Added `_vector_lanes()` / `_validate_vector_operands()` so operands are checked even when all lanes are masked off (zero extra work when lanes are active). 3. **Stale PC in `Unsupported` exceptions**: `Machine.run()` updated `self.pc` only after `self.step(ins)`, so operand errors raised by `_int_src`/`_float_src`/`_write_vgpr`/`_write_scalar_dst`/`_mask_bit` reported `pc 0x0` (or the prior instruction's `next_pc`) instead of `ins["pc"]`. Both `run()` and `step()` now set `self.pc = ins["pc"]` before executing. 4. **Scalar `scc` clobbered on invalid destination**: `s_add_u32`, `s_lshl_b32`, and `s_lshr_b32` mutated `self.scc` before `_write_scalar_dst()` validated the destination; now `_check_scalar_dst()` runs first. 5. **CLI/input validation**: `--lane N` passed together with `--lanes 64` was silently ignored on the CLI (despite `Machine.__init__` rejecting it), and negative/boolean register indices (`--sgpr -1=0`, `{"sgpr": {-1: 0}}`, `{"lanes": True}`) were accepted. ## Testing - **AI-assisted**: yes - **Host**: macOS Apple M5 (`arm64`), Python 3.14 (CPU-only, hand-written `.s` fixtures only, no game data) - `python3 -m compileall -q make_game.py tools tests` -> clean - `python3 -I scripts/check_no_game_data.py tools/cpu_ref.py tests/test_cpu_ref.py` -> clean - `python3 -I tests/test_cpu_ref.py -v` -> **39/39 OK** (5 new tests) - `python3 -I tests/test_cf_ref.py` -> **26/26 OK** - `python3 -I tests/test_cf_ref_edge_fixes.py` -> **5/5 OK** - `python3 -I tests/test_wave_diff.py` -> **12/12 OK** - `python3 -m unittest discover -s tests -p "test_*diff.py"` -> **362/362 OK** - `python3 -I tests/test_missing_input_exit_codes.py` -> **2/2 OK** - `python3 -I tests/test_text_write_line_endings.py` -> **3/3 OK** - `python3 -I tests/test_ci_registration.py` -> **16/16 OK** - **Mutation proof** (running the new `tests/test_cpu_ref.py` on unpatched `upstream/main` `b4b7756e8`): `FAILED (failures=25)` across all 5 new tests and their subtests.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:52:03 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 00:36 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 00:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1457#pullrequestreview-6049730415) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 07:34 UTC · original

Independent verification (CPU/tools-only, native Windows, Python 3.11.9 — stdlib-only tools so representative): diff reviewed line-by-line, all claimed fixes present and covered by the new tests; neighboring suites green. Details in the verification batch log. AI-assisted verification.

**@⁠whoalin1** on GitHub · 2026-11-25 07:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1457#issuecomment-7156018583) Independent verification (CPU/tools-only, native Windows, Python 3.11.9 — stdlib-only tools so representative): diff reviewed line-by-line, all claimed fixes present and covered by the new tests; neighboring suites green. Details in the verification batch log. AI-assisted verification.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1457/head:github-bot/gh-1457'
git switch 'github-bot/gh-1457'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1457'
git switch 'github-bot/gh-1457'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1457'
git switch 'github-bot/gh-1457'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1457'
git switch 'main'
git merge --squash 'github-bot/gh-1457'
git switch 'main'
git merge --ff-only 'github-bot/gh-1457'
git switch 'main'
git merge 'github-bot/gh-1457'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.