sampler_source_census: fix V#/pointer/SMEM/v_mov dataflow and harden JSON I/O (SAMPLER-SOURCE-CENSUS-DATAFLOW-AND-IO) #1530

Merged
IlyassRS3 merged 1 commit from upstream-pr/1530 into main 2026-11-29 18:37:02 +00:00
IlyassRS3 commented 2026-11-25 03:37:35 +00:00 (Migrated from github.com)

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


What changed

  • V# per-dword offset preservation (tools/sampler_source_census.py:one_load_source): collect [source(t, seen) for t in base] and check agreement on the source origin ({g[0] for g in got}) while combining any(g[1]) and max(g[2]), instead of requiring all four dwords to have identical (where, dyn, k) tuples. Previously, adding a static or dynamic offset to word 0 of a user V# dropped it to undetermined (V# from different sources).
  • 64-bit pointer high-word zero and offset tracking (tools/sampler_source_census.py:one_load_source): recognize 3-tuple unary-folded zero constants (("const", 0, op) from s_not_b32/s_brev_b32) as a zero high word, and propagate dyn/k from a user high word instead of ignoring them.
  • SMEM offset:IMM modifier tracking (tools/sampler_source_census.py:analyse, one_load_source, one_load): parse offset:N from ins["mods"] so combined sOFF offset:IMM loads count IMM in max_pointer_constant (dynamic) and max_static_offset (constant SGPR + offset:IMM).
  • Spill-copy soundness (tools/sampler_source_census.py:step): do not treat v_mov_b32_sdwa, v_mov_b32_dpp, instructions with modifiers (clamp, etc.), or register operands with neg/abs/sext as lossless spill copies.
  • Robustness (tools/sampler_source_census.py:anchored, blocks_of, analyse, census): convert invalid hex in anchored() into hic.Skip instead of crashing with ValueError, handle empty instruction lists cleanly in blocks_of([])/analyse([], ...), and validate positive non-bool integers for limit and jobs in census().
  • Pre-scan --json guards, atomic write, and OSError handling (tools/sampler_source_census.py:main): refuse --json targeting a folder, index.json, anything inside disasm, or any .dxbc / fssm/*.fssm / .s input (via report_paths.same_report_path and os.path.samestat for hardlinks); catch OSError from census() with exit code 2; write --json atomically through a sibling temp file with file_mode.published_mode, replace_retry.replace, allow_nan=False, and explicit newline="\n".
  • Tests (tests/test_sampler_source_census.py): added 7 synthetic unit/CLI regression tests covering every fix above.

Claimed in the coordination issue as:

SAMPLER-SOURCE-CENSUS-DATAFLOW-AND-IO (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7152027644)

How it was tested

  • python3 -I -W error tests/test_sampler_source_census.py: 37/37 passed (0 warnings).
  • python3 -I tests/test_text_write_line_endings.py: 3/3 passed.
  • python3 -I scripts/check_no_game_data.py tools/sampler_source_census.py tests/test_sampler_source_census.py: passed.
  • python3 -m compileall -q tools/sampler_source_census.py tests/test_sampler_source_census.py: passed.
  • Mutation check against unpatched main:tools/sampler_source_census.py: all 7 new tests fail (FAILED (failures=6, errors=1)), all 30 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:5484689555 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1530). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T03:37:35Z. --- ## What changed - **V# per-dword offset preservation (`tools/sampler_source_census.py:one_load_source`)**: collect `[source(t, seen) for t in base]` and check agreement on the source origin (`{g[0] for g in got}`) while combining `any(g[1])` and `max(g[2])`, instead of requiring all four dwords to have identical `(where, dyn, k)` tuples. Previously, adding a static or dynamic offset to word 0 of a user V# dropped it to `undetermined` (`V# from different sources`). - **64-bit pointer high-word zero and offset tracking (`tools/sampler_source_census.py:one_load_source`)**: recognize 3-tuple unary-folded zero constants (`("const", 0, op)` from `s_not_b32`/`s_brev_b32`) as a zero high word, and propagate `dyn`/`k` from a user high word instead of ignoring them. - **SMEM `offset:IMM` modifier tracking (`tools/sampler_source_census.py:analyse`, `one_load_source`, `one_load`)**: parse `offset:N` from `ins["mods"]` so combined `sOFF offset:IMM` loads count `IMM` in `max_pointer_constant` (dynamic) and `max_static_offset` (constant SGPR + `offset:IMM`). - **Spill-copy soundness (`tools/sampler_source_census.py:step`)**: do not treat `v_mov_b32_sdwa`, `v_mov_b32_dpp`, instructions with modifiers (`clamp`, etc.), or register operands with `neg`/`abs`/`sext` as lossless spill copies. - **Robustness (`tools/sampler_source_census.py:anchored`, `blocks_of`, `analyse`, `census`)**: convert invalid hex in `anchored()` into `hic.Skip` instead of crashing with `ValueError`, handle empty instruction lists cleanly in `blocks_of([])`/`analyse([], ...)`, and validate positive non-bool integers for `limit` and `jobs` in `census()`. - **Pre-scan `--json` guards, atomic write, and `OSError` handling (`tools/sampler_source_census.py:main`)**: refuse `--json` targeting a folder, `index.json`, anything inside `disasm`, or any `.dxbc` / `fssm/*.fssm` / `.s` input (via `report_paths.same_report_path` and `os.path.samestat` for hardlinks); catch `OSError` from `census()` with exit code 2; write `--json` atomically through a sibling temp file with `file_mode.published_mode`, `replace_retry.replace`, `allow_nan=False`, and explicit `newline="\n"`. - **Tests (`tests/test_sampler_source_census.py`)**: added 7 synthetic unit/CLI regression tests covering every fix above. ## Claimed in the coordination issue as: `SAMPLER-SOURCE-CENSUS-DATAFLOW-AND-IO` (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7152027644) ## How it was tested - `python3 -I -W error tests/test_sampler_source_census.py`: 37/37 passed (0 warnings). - `python3 -I tests/test_text_write_line_endings.py`: 3/3 passed. - `python3 -I scripts/check_no_game_data.py tools/sampler_source_census.py tests/test_sampler_source_census.py`: passed. - `python3 -m compileall -q tools/sampler_source_census.py tests/test_sampler_source_census.py`: passed. - Mutation check against unpatched `main:tools/sampler_source_census.py`: all 7 new tests fail (`FAILED (failures=6, errors=1)`), all 30 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:37:50 +00:00 (Migrated from github.com)

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

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:6051499247 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1530#pullrequestreview-6051499247). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T03:37:50Z. **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:40:32 +00:00 (Migrated from github.com)

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


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/sampler_source_census.py <work>/shaders/dxbc/disasm --json census.json --jobs 3, once with main fe1553456's tool and once with this branch's tools/sampler_source_census.py on the same tree. main has no commit to that file since this branch's base, so only the change differs.

  • The two runs are byte-identical, both the printed report and the JSON. 14,025 shaders sample, with 366,628 sampling instructions: 189,412 user_pointer, 174,796 literal, 0 user_sgprs, 0 other_base and 2,420 undetermined.
  • These are also the numbers in docs/d3d12_vulkan.md's SHADER-735 table, so this copy reproduces the published census as well.
  • What that means for the change: none of the dataflow fixes (V#/pointer, SMEM, v_mov) moves an instruction between classes on 1.0.2.1, and the 0 other_base conclusion the doc draws from this tool holds with the branch too. The fixes guard cases this corpus doesn't have.
  • Run time: 14 min 43 s on main, 14 min 47 s with the branch (one run each, --jobs 3).
  • python -I tests/test_sampler_source_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:7169840248 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1530#issuecomment-7169840248). Originally posted by [klikso](https://github.com/klikso) on 2026-11-25T17:40:32Z. --- 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/sampler_source_census.py <work>/shaders/dxbc/disasm --json census.json --jobs 3`, once with `main` `fe1553456`'s tool and once with this branch's `tools/sampler_source_census.py` on the same tree. `main` has no commit to that file since this branch's base, so only the change differs. - **The two runs are byte-identical,** both the printed report and the JSON. 14,025 shaders sample, with 366,628 sampling instructions: 189,412 `user_pointer`, 174,796 `literal`, 0 `user_sgprs`, 0 `other_base` and 2,420 `undetermined`. - **These are also the numbers in `docs/d3d12_vulkan.md`'s SHADER-735 table,** so this copy reproduces the published census as well. - **What that means for the change:** none of the dataflow fixes (V#/pointer, SMEM, `v_mov`) moves an instruction between classes on 1.0.2.1, and the 0 `other_base` conclusion the doc draws from this tool holds with the branch too. The fixes guard cases this corpus doesn't have. - Run time: 14 min 43 s on `main`, 14 min 47 s with the branch (one run each, `--jobs 3`). - `python -I tests/test_sampler_source_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:48 +00:00 (Migrated from github.com)

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


#1530 | head a9413ce4d64cbf438cb92e60ca45323141aa0f8e | python3 -I tests/test_sampler_source_census.py -v | red: 37 run, 7 failures, 1 error | green: 37 run, 1 failure | Still failing after merge (same assertion on untouched main)

One assertion remains red on the merged tree: SamplerSourceCensus.test_statistics_bound_what_the_classes_cannot_decide expects two user_pointer samples and gets one. The same assertion fails on untouched main (1 failure / 30 tests). The unmerged PR head passes 37/37. A repeat of the exact merge again fails 1/37. As a diagnostic control, restoring just tools/heap_index_census.py from the PR head on a disposable copy of the merge makes all 37 pass. Main commit 581283eed changed how s_lshlK_add treats its first source as a scaled index and the second as a base. The second synthetic shader in this test puts the user value in the shifted source. The official merged result above uses the unmodified merge.

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:7238556729 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1530#issuecomment-7238556729). Originally posted by [sodove](https://github.com/sodove) on 2026-11-28T11:26:48Z. --- #1530 | head `a9413ce4d64cbf438cb92e60ca45323141aa0f8e` | `python3 -I tests/test_sampler_source_census.py -v` | red: 37 run, 7 failures, 1 error | green: 37 run, 1 failure | **Still failing after merge (same assertion on untouched main)** One assertion remains red on the merged tree: SamplerSourceCensus.test_statistics_bound_what_the_classes_cannot_decide expects two user_pointer samples and gets one. The same assertion fails on untouched main (1 failure / 30 tests). The unmerged PR head passes 37/37. A repeat of the exact merge again fails 1/37. As a diagnostic control, restoring just tools/heap_index_census.py from the PR head on a disposable copy of the merge makes all 37 pass. Main commit 581283eed changed how s_lshlK_add treats its first source as a scaled index and the second as a base. The second synthetic shader in this test puts the user value in the shifted source. The official merged result above uses the unmodified merge. 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:43:00 +00:00 (Migrated from github.com)

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


Thanks @IlyassRS3! This PR passes on its own head, but after merging current main one assertion in tests/test_sampler_source_census.py still fails (one user-pointer sample where the test expects two), because main has since changed the heap-index census helper this census relies on (sodove's report above isolates it). Could you update it against current main? It will be merged once that test is green.

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238735289 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1530#issuecomment-7238735289). Originally posted by [DadaCap](https://github.com/DadaCap) on 2026-11-28T11:43:00Z. --- Thanks @IlyassRS3! This PR passes on its own head, but after merging current main one assertion in `tests/test_sampler_source_census.py` still fails (one user-pointer sample where the test expects two), because main has since changed the heap-index census helper this census relies on (sodove's report above isolates it). Could you update it against current main? It will be merged once that test is green. AI-assisted (Claude Code, Claude Opus 5.5), posted for the maintainers.
TheGeogeo commented 2026-11-29 14:17:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-29T14:17:49Z.


Ready for testing: merges cleanly with main 3535eb5c4. Not in main yet: git cherry finds 1 commit(s) not there, and 12 of the 193 lines it adds are already present. No duplicate found: its claim SAMPLER-SOURCE-CENSUS-DATAFLOW-AND-IO appears in no other open pull request and in no main commit.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7257192875 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1530#issuecomment-7257192875). Originally posted by [TheGeogeo](https://github.com/TheGeogeo) on 2026-11-29T14:17:49Z. --- Ready for testing: merges cleanly with `main` `3535eb5c4`. Not in `main` yet: `git cherry` finds 1 commit(s) not there, and 12 of the 193 lines it adds are already present. No duplicate found: its claim `SAMPLER-SOURCE-CENSUS-DATAFLOW-AND-IO` appears in no other open pull request and in no `main` commit. AI-assisted (Claude Code).
enlight3d commented 2026-11-29 15:40:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-11-29T15:40:59Z.


Independent run of #1530 (python -I, no build, no game; GPU not used).

  • PR head: a9413ce4d6
  • main: 91bab934d7 (PR merged into it locally with git merge --no-ff, clean; only tools/sampler_source_census.py and tests/test_sampler_source_census.py touched)
  • Machine: Windows 11 Pro 10.0.26300, CPython 3.13.12, standard library only, RTX 3090 / NVIDIA 621.40 (GPU not used)

Commands (one process each, from the repo root): python -I tests/test_sampler_source_census.py, plus test_ci_registration.py, test_check_no_game_data.py, test_text_write_line_endings.py, test_tool_stdout_encoding.py, test_docs_repo_paths.py.

  • Red: with tools/sampler_source_census.py reverted to the pre-PR version (the file is unchanged on main since the PR's base) and the PR's test file kept: FAILED (failures=6, errors=1, 37 tests). Behaviour red, not a build/API one: the fix tests fail on an assertion (pointer high word reported undetermined instead of user pointer; SMEM inst_offset not counted in bounds, 0 != 0x7f004000; v_mov with SDWA/DPP/neg modifier treated as a lossless spill; V# with per-dword offset reported as different sources; invalid limit/jobs not rejected with ValueError; JSON guard / atomic write / OSError exit code 0 instead of 2), and one IndexError on an anchored invalid-hex / empty-block input.
  • Green at head: 37/37 OK (15.5 s).
  • Green merged into main: 37/37 OK, ci_registration 18/18, check_no_game_data 21 (2 skipped), line endings 3/3, tool_stdout_encoding 4/4, docs_repo_paths 5/5.
  • Surprises: none. (main's test file carries one extra commit since the PR base, b1a3e93fd, which merges cleanly.)
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7258282687 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1530#issuecomment-7258282687). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-11-29T15:40:59Z. --- Independent run of #1530 (python -I, no build, no game; GPU not used). - PR head: a9413ce4d64cbf438cb92e60ca45323141aa0f8e - main: 91bab934d7ee43e23c582bd8a18baa2b09a0a558 (PR merged into it locally with `git merge --no-ff`, clean; only `tools/sampler_source_census.py` and `tests/test_sampler_source_census.py` touched) - Machine: Windows 11 Pro 10.0.26300, CPython 3.13.12, standard library only, RTX 3090 / NVIDIA 621.40 (GPU not used) Commands (one process each, from the repo root): `python -I tests/test_sampler_source_census.py`, plus `test_ci_registration.py`, `test_check_no_game_data.py`, `test_text_write_line_endings.py`, `test_tool_stdout_encoding.py`, `test_docs_repo_paths.py`. - Red: with `tools/sampler_source_census.py` reverted to the pre-PR version (the file is unchanged on main since the PR's base) and the PR's test file kept: FAILED (failures=6, errors=1, 37 tests). Behaviour red, not a build/API one: the fix tests fail on an assertion (pointer high word reported undetermined instead of user pointer; SMEM inst_offset not counted in bounds, 0 != 0x7f004000; v_mov with SDWA/DPP/neg modifier treated as a lossless spill; V# with per-dword offset reported as different sources; invalid limit/jobs not rejected with ValueError; JSON guard / atomic write / OSError exit code 0 instead of 2), and one IndexError on an anchored invalid-hex / empty-block input. - Green at head: 37/37 OK (15.5 s). - Green merged into main: 37/37 OK, ci_registration 18/18, check_no_game_data 21 (2 skipped), line endings 3/3, tool_stdout_encoding 4/4, docs_repo_paths 5/5. - Surprises: none. (main's test file carries one extra commit since the PR base, b1a3e93fd, which merges cleanly.)
MoonEyes commented 2026-11-29 18:31:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T18:31:12Z.


Tested a9413ce4d merged (no commit) with main 4755667f5: Python 3.12, Windows 11 10.0.26300, CPU only. Ran the PR's tests plus every test file that imports the changed tool.

  • Green: python -I tests/test_sampler_source_census.py gives 37 OK.
  • Red with tools/sampler_source_census.py back to main: FAILED (failures=6, errors=1).
  • Does not force a rebase of #2566 (merge-tree against 8db8335d2).

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7260547070 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1530#issuecomment-7260547070). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-29T18:31:12Z. --- Tested `a9413ce4d` merged (no commit) with `main` `4755667f5`: Python 3.12, Windows 11 10.0.26300, CPU only. Ran the PR's tests plus every test file that imports the changed tool. - Green: `python -I tests/test_sampler_source_census.py` gives 37 OK. - Red with `tools/sampler_source_census.py` back to `main`: `FAILED (failures=6, errors=1)`. - Does not force a rebase of #2566 (merge-tree against `8db8335d2`). AI-assisted (Claude Code).
MoonEyes commented 2026-11-29 18:36:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T18:36:59Z.


Merging. Red/green verified on a9413ce4d against current main by an agent other than the author (verdicts above), no conflict with main, does not force a rebase of #2566, claim ID present. Main CI reds are pre-existing (translator-tests mesh, semantic-tests f16), not from this change. Merged by MoonEyes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7260622562 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1530#issuecomment-7260622562). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-29T18:36:59Z. --- Merging. Red/green verified on `a9413ce4d` against current `main` by an agent other than the author (verdicts above), no conflict with `main`, does not force a rebase of #2566, claim ID present. Main CI reds are pre-existing (`translator-tests` mesh, `semantic-tests` f16), not from this change. Merged by MoonEyes. AI-assisted (Claude Code).
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!1530
No description provided.