image_bvh_census: fix v_cmpx/v_rcp/SDWA/CFG bugs and harden JSON I/O (IMAGE-BVH-CENSUS-DATAFLOW-CFG-AND-IO) #1534

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

@⁠IlyassRS3 on GitHub · 2026-11-25 03:42 UTC · original
IlyassRS3:fix/image-bvh-census-dataflow-io → main · +279 −21 · 2 files · 1 commits · open

What changed

  • Fix v_cmpx_* operand handling (tools/image_bvh_census.py:sgpr_step, mask_writes, source_args):
    • v_cmpx_*_e32 takes 2 source operands with an implicit EXEC destination (a[0] is a source, not a destination). Previously, sgpr_step() and mask_writes() treated a[0] as an SGPR write, wiping literal descriptor words and saved EXEC masks whenever a 2-operand v_cmpx_* read an SGPR in a[0].
    • v_cmpx_*_e64 (len(a) > 2) puts its destination in a[0] and sources in a[1:]; source_args() now excludes a[0], so product_reader() reports v_cmpx_gt_i32 with 0 instead of with 2 other operands.
  • Require unmodified v_rcp_f32 before tagging R1 (tools/image_bvh_census.py:site_transfer): align site_transfer() with classify() by checking plain(a[1]), not ins.get("mods"), and no _sdwa/_dpp suffix so v_rcp_f32_e64 v20, |v5| / -v5 / clamp does not falsely classify downstream v_mul_f32 as ratio.
  • Require a single VGPR in high_half_operand() and handle dual high-half operands (tools/image_bvh_census.py:high_half_operand, site_transfer): prevent SGPR operands (e.g. sext(s5)) from reading VGPR v5, prevent immediate operands (sext(1)) from raising KeyError: 'r', and select the operand holding word 1 when both SDWA operands use WORD_1.
  • Copy & float-immediate edge cases (tools/image_bvh_census.py:is_copy, imm32): check single-VGPR operands and plain(a[0]) on v_swap_b32 in is_copy(), and catch OverflowError in imm32() for out-of-range float32 literals.
  • CFG gap splitting & bad encodings (tools/image_bvh_census.py:blocks_of, analyse, load_program): start a new block on non-contiguous PCs and require next_pc(last) == starts[n + 1] before adding a fall-through edge (avoiding phantom fall-through across embedded data gaps), handle empty insns cleanly, and convert invalid hex encodings (parse_file / bytes.fromhex ValueError) to Skip.
  • Harden --out overwrite guards, OSError handling, and atomic JSON output (tools/image_bvh_census.py:main): use report_paths.same_report_path (plus os.path.samestat for hardlinks) to refuse overwriting scanned listings, index.json, or fssm/*.fssm; catch OSError during directory scanning with exit code 2; write --out atomically through a sibling temp file with file_mode.published_mode, replace_retry.replace, allow_nan=False, and a trailing LF.
  • Tests (tests/test_image_bvh_census.py): added 6 synthetic unit/CLI regression tests covering every fix above.

Claimed in the coordination issue as:

IMAGE-BVH-CENSUS-DATAFLOW-CFG-AND-IO (#14)

How it was tested

  • python3 -I -W error tests/test_image_bvh_census.py: 21/21 passed (0 warnings).
  • python3 -I tests/test_text_write_line_endings.py: 3/3 passed.
  • python3 -I scripts/check_no_game_data.py tools/image_bvh_census.py tests/test_image_bvh_census.py: passed.
  • python3 -m compileall -q tools/image_bvh_census.py tests/test_image_bvh_census.py: passed.
  • Mutation check against unpatched main:tools/image_bvh_census.py: all 6 new tests fail (FAILED (failures=4, errors=2)), all 15 baseline tests pass.

Metadata

  • AI-assisted: yes
  • GPUs tested: none (CPU-only synthetic disassembly / CLI unit tests on macOS arm64; no game files used)
**@⁠IlyassRS3** on GitHub · 2026-11-25 03:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1534) `IlyassRS3:fix/image-bvh-census-dataflow-io` → `main` · +279 −21 · 2 files · 1 commits · open ## What changed - **Fix `v_cmpx_*` operand handling (`tools/image_bvh_census.py:sgpr_step`, `mask_writes`, `source_args`)**: - `v_cmpx_*_e32` takes 2 source operands with an implicit `EXEC` destination (`a[0]` is a source, not a destination). Previously, `sgpr_step()` and `mask_writes()` treated `a[0]` as an SGPR write, wiping literal descriptor words and saved EXEC masks whenever a 2-operand `v_cmpx_*` read an SGPR in `a[0]`. - `v_cmpx_*_e64` (`len(a) > 2`) puts its destination in `a[0]` and sources in `a[1:]`; `source_args()` now excludes `a[0]`, so `product_reader()` reports `v_cmpx_gt_i32 with 0` instead of `with 2 other operands`. - **Require unmodified `v_rcp_f32` before tagging `R1` (`tools/image_bvh_census.py:site_transfer`)**: align `site_transfer()` with `classify()` by checking `plain(a[1])`, `not ins.get("mods")`, and no `_sdwa`/`_dpp` suffix so `v_rcp_f32_e64 v20, |v5|` / `-v5` / `clamp` does not falsely classify downstream `v_mul_f32` as `ratio`. - **Require a single VGPR in `high_half_operand()` and handle dual high-half operands (`tools/image_bvh_census.py:high_half_operand`, `site_transfer`)**: prevent SGPR operands (e.g. `sext(s5)`) from reading VGPR `v5`, prevent immediate operands (`sext(1)`) from raising `KeyError: 'r'`, and select the operand holding word 1 when both SDWA operands use `WORD_1`. - **Copy & float-immediate edge cases (`tools/image_bvh_census.py:is_copy`, `imm32`)**: check single-VGPR operands and `plain(a[0])` on `v_swap_b32` in `is_copy()`, and catch `OverflowError` in `imm32()` for out-of-range float32 literals. - **CFG gap splitting & bad encodings (`tools/image_bvh_census.py:blocks_of`, `analyse`, `load_program`)**: start a new block on non-contiguous PCs and require `next_pc(last) == starts[n + 1]` before adding a fall-through edge (avoiding phantom fall-through across embedded data gaps), handle empty `insns` cleanly, and convert invalid hex encodings (`parse_file` / `bytes.fromhex` `ValueError`) to `Skip`. - **Harden `--out` overwrite guards, `OSError` handling, and atomic JSON output (`tools/image_bvh_census.py:main`)**: use `report_paths.same_report_path` (plus `os.path.samestat` for hardlinks) to refuse overwriting scanned listings, `index.json`, or `fssm/*.fssm`; catch `OSError` during directory scanning with exit code 2; write `--out` atomically through a sibling temp file with `file_mode.published_mode`, `replace_retry.replace`, `allow_nan=False`, and a trailing LF. - **Tests (`tests/test_image_bvh_census.py`)**: added 6 synthetic unit/CLI regression tests covering every fix above. ## Claimed in the coordination issue as: `IMAGE-BVH-CENSUS-DATAFLOW-CFG-AND-IO` (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ## How it was tested - `python3 -I -W error tests/test_image_bvh_census.py`: 21/21 passed (0 warnings). - `python3 -I tests/test_text_write_line_endings.py`: 3/3 passed. - `python3 -I scripts/check_no_game_data.py tools/image_bvh_census.py tests/test_image_bvh_census.py`: passed. - `python3 -m compileall -q tools/image_bvh_census.py tests/test_image_bvh_census.py`: passed. - Mutation check against unpatched `main:tools/image_bvh_census.py`: all 6 new tests fail (`FAILED (failures=4, errors=2)`), all 15 baseline tests pass. ## Metadata - AI-assisted: yes - GPUs tested: none (CPU-only synthetic disassembly / CLI unit tests on macOS arm64; no game files used)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:55:40 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 03:43 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 03:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1534#pullrequestreview-6051541297) · **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

@⁠klikso on GitHub · 2026-11-25 17:10 UTC · original

Ran on a real corpus, since the description's tests are synthetic: my own 1.0.2.1, the 26,503 listings that make_game.py disassembles (<work>/shaders/dxbc/disasm, llvm-mc 23.1.6, main 3d4f66123), Windows 11, Python 3.13.

python -I tools/image_bvh_census.py <work>/shaders/dxbc/disasm --out census.json, once with main fe1553456's tool and once with this branch's tools/image_bvh_census.py on the same tree. main has no commit to that file since this branch's base, so only the change differs.

  • Both runs give the same census. 716 image_bvh* sites in 64 compute shaders (638 image_bvh_intersect_ray, 78 image_bvh64_intersect_ray). The printed report is identical line for line, and the JSON is identical key for key (292 values). The only byte that differs is a final newline, which this branch now writes.
  • What that means for the change: none of the fixed cases changes a count on 1.0.2.1. The v_cmpx_* operands, a modified v_rcp_f32, an SGPR or immediate high-half operand, PC gaps and out-of-range float literals each either don't occur on these call paths or classify the same either way. For example, v_cmpx_gt_i32 reads of the high-half product are counted as with a constant 0/5 in both runs. The fixes guard edge cases the corpus doesn't have, and they don't move the published numbers.
  • Run time: 16 min 30 s on main, 15 min 24 s with the branch (one run each, single process).
  • python -I tests/test_image_bvh_census.py with the branch's file: OK.

Counts only; the listings and the JSON stay on my machine.

AI-assisted: yes (Claude Code; run on the machine above)

**@⁠klikso** on GitHub · 2026-11-25 17:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1534#issuecomment-7169119176) Ran on a real corpus, since the description's tests are synthetic: my own 1.0.2.1, the 26,503 listings that `make_game.py` disassembles (`<work>/shaders/dxbc/disasm`, llvm-mc 23.1.6, `main` 3d4f66123), Windows 11, Python 3.13. `python -I tools/image_bvh_census.py <work>/shaders/dxbc/disasm --out census.json`, once with `main` `fe1553456`'s tool and once with this branch's `tools/image_bvh_census.py` on the same tree. `main` has no commit to that file since this branch's base, so only the change differs. - **Both runs give the same census.** 716 `image_bvh*` sites in 64 compute shaders (638 `image_bvh_intersect_ray`, 78 `image_bvh64_intersect_ray`). The printed report is identical line for line, and the JSON is identical key for key (292 values). The only byte that differs is a final newline, which this branch now writes. - **What that means for the change:** none of the fixed cases changes a count on 1.0.2.1. The `v_cmpx_*` operands, a modified `v_rcp_f32`, an SGPR or immediate high-half operand, PC gaps and out-of-range float literals each either don't occur on these call paths or classify the same either way. For example, `v_cmpx_gt_i32` reads of the high-half product are counted as `with a constant 0/5` in both runs. The fixes guard edge cases the corpus doesn't have, and they don't move the published numbers. - Run time: 16 min 30 s on `main`, 15 min 24 s with the branch (one run each, single process). - `python -I tests/test_image_bvh_census.py` with the branch's file: OK. Counts only; the listings and the JSON stay on my machine. AI-assisted: yes (Claude Code; run on the machine above)
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/1534/head:github-bot/gh-1534'
git switch 'github-bot/gh-1534'

Merge

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