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

Merged
IlyassRS3 merged 1 commit from upstream-pr/1534 into main 2026-11-28 11:42:17 +00:00
IlyassRS3 commented 2026-11-25 03:42:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T03:42:58Z.


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://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7152125566)

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)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5484750002 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1534). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T03:42:58Z. --- ## 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://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7152125566) ## 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)
copilot-pull-request-reviewer[bot] commented 2026-11-25 03:43:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T03:43:10Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6051541297 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1534#pullrequestreview-6051541297). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T03:43:10Z. **Review:** COMMENTED --- 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.
klikso commented 2026-11-25 17:10:37 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by klikso on 2026-11-25T17:10:37Z.


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)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7169119176 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1534#issuecomment-7169119176). Originally posted by [klikso](https://github.com/klikso) on 2026-11-25T17:10:37Z. --- 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)
sodove commented 2026-11-28 11:26:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by sodove on 2026-11-28T11:26:52Z.


#1534 | head c2d32249e6845782f96495606652b346e851cfe0 | python3 -I tests/test_image_bvh_census.py -v | red: 21 run, 4 failures, 2 errors | green: 21 passed | Red → green

The baseline failed synthetic dataflow, CFG and input/output cases. All 21 methods passed with main merged.

Copied the PR tests onto clean main 4a54aedcb6b66525e0c9f72a6ea10263337c9141 for the baseline, then merged that main into the recorded PR head and reran those files. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Unittest failure/error counts include subtests where applicable. AI-assisted with OpenAI Codex. Batch claim.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238557531 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1534#issuecomment-7238557531). Originally posted by [sodove](https://github.com/sodove) on 2026-11-28T11:26:52Z. --- #1534 | head `c2d32249e6845782f96495606652b346e851cfe0` | `python3 -I tests/test_image_bvh_census.py -v` | red: 21 run, 4 failures, 2 errors | green: 21 passed | **Red → green** The baseline failed synthetic dataflow, CFG and input/output cases. All 21 methods passed with main merged. Copied the PR tests onto clean main `4a54aedcb6b66525e0c9f72a6ea10263337c9141` for the baseline, then merged that main into the recorded PR head and reran those files. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Unittest failure/error counts include subtests where applicable. AI-assisted with OpenAI Codex. [Batch claim](https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7238450840).
DadaCap commented 2026-11-28 11:42:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DadaCap on 2026-11-28T11:42:19Z.


Merged, thank you @IlyassRS3! This PR (image_bvh_census: fix v_cmpx/v_rcp/SDWA/CFG bugs and harden JSON I/O (IMAGE-BVH-CENSUS-DATAFLOW-CFG-AND-IO)) passed its own tests with main merged in during sodove's verification of this shader batch, so it went in with the batch.

AI-assisted (Claude Code, Claude Opus 5.5), posted for the maintainers.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238727564 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1534#issuecomment-7238727564). Originally posted by [DadaCap](https://github.com/DadaCap) on 2026-11-28T11:42:19Z. --- Merged, thank you @IlyassRS3! This PR (image_bvh_census: fix v_cmpx/v_rcp/SDWA/CFG bugs and harden JSON I/O (IMAGE-BVH-CENSUS-DATAFLOW-CFG-AND-IO)) passed its own tests with main merged in during sodove's verification of this shader batch, so it went in with the batch. AI-assisted (Claude Code, Claude Opus 5.5), posted for the maintainers.
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!1534
No description provided.