startup: add a read-only --refusals census of gfx2hlsl refusal sites #1971

Merged
Leyukaka merged 3 commits from upstream-pr/1978 into main 2026-11-24 22:42:09 +00:00
Leyukaka commented 2026-11-24 22:31:28 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-24T22:31:28Z.


Refs #1723.

Change

tools/startup/gfx2hlsl.py stops a pipeline at its first refused instruction. A cache of refused requests therefore shows one layer of gaps at a time: fixing a refusal reveals the next one behind it, and the census in #1723 notes that the gaps behind the first refusal are undercounted.

This adds a read-only diagnostic:

python -B tools/startup/gfx2hlsl.py --refusals <cache-dir> [--json]

It loads every request in the directory through the same loader as --pending, including RTV variants and requests that already have a .txt or .fail. Each request is translated in memory with a collector:

  • a refusal raised for one instruction, during decoding or lowering, is recorded and that instruction is skipped;
  • an undecodable word, or a refusal during request loading, pipeline construction or output generation, ends the pipeline. When it is the first site it is the reason the default path writes to .fail, after normalisation; file errors are reported as unreadable shader request because their text holds the path and key;
  • an unexpected handler exception is recorded as translator crash in <op>: <type> when nothing was recorded before it, a translator bug, or as internal error in <op>: <type> after an earlier refusal or crash, a possible follow-on effect.

The report has two views:

  • per request key: for each reason and scope (decode, vs, ps, pipeline), the requests that contain it, that reach it first, and that have it as their only distinct reason, plus a histogram of refusal sites per request and the count with no refusal recorded (not compiled);
  • per code pair, as #1723 moved to: base requests grouped by a hash of their .a and .b bytes, excluding RTV variants, with each reason counted under every observed root signature or under some roots only.

Register numbers and immediates are normalised (sN, s[N:N], vN), as are raw instruction words. Numbers that are the cause, such as a format, a width or a shader model, are kept. No request key or path is printed; refusal text is printed only after this normalisation. Counts after the first refusal are approximate, because a skipped instruction leaves later state unmodelled; the report and the docs say so.

The mode writes nothing: refusals_main never reaches translate_to, opens no file for writing, never starts fxc, and exits before main()'s os.makedirs. It is selected by --refusals DIR or --refusals=DIR anywhere on the command line. With the collector unset, the default path is unchanged. The decode() encoding checks, #1932's NEG rewrite included, move verbatim into check_encoding(). Request loading is split out of translate_request() into load_request(). The end of main() (the pending loop that #1749 rewrites) is untouched. On a translator without this change, --refusals DIR is treated as --pending DIR and writes into the directory; the docs say so.

Registered as CTest startup_gfx2hlsl_refusals, in the python-linux CI list and in AGENTS.md. There is a section in docs/experiments.md and a short note in docs/startup-runtime.md.

Verification

On 425cd53b (based on main at 889cd3f4), Windows 11 build 26300, Python 3.14.10:

python -B tests/startup_gfx2hlsl_refusals_check.py

47 checks pass, without game data or LLVM, and with X2W_FXC empty so fxc is never started. The disassembler is stubbed with synthetic words, except the NEG check, which decodes one hand-assembled v_cndmask_b32_e64 word with the in-tree decoder. They cover:

  • stage-level skip and resume;
  • decode-level skip on a synthetic encoding refusal, and #1932's NEG rewrite kept;
  • handler errors as crash or follow-on;
  • request failures (dangling RTV, undecodable word, bad stage set, loader and pipeline exceptions), with the default path still writing the same .fail text;
  • per-request and per-pair counts, including a root-dependent reason and distinct-reason only;
  • normalisation, including word_0x…;
  • both flag orders and --refusals=DIR, and a missing directory that is never created;
  • a cache directory byte-identical afterwards, with translate_to patched to raise a BaseException subclass.

A control stops at the first refusal and requires the second reason to disappear from the report. On main the script ends at its first collector call with an uncaught gfx2hlsl.Unsupported: unsupported_first.

Local data, not in the repository. On a local cache of 575 recorded requests (the synthetic check above covers the same properties without it: unchanged default output, an unchanged directory, no key in the report):

  • X2W_FXC= python -B tools/startup/gfx2hlsl.py --pending <copy> with this branch and with main at 889cd3f4 gives byte-identical directories (897 files) and identical logs;
  • --refusals <copy> leaves every file byte-identical and prints no 16-hex-digit key or raw word_0x name.

Also run on this branch, each python -B tests/<name> exiting 0:

  • startup_translator_check.py, startup_gfx2hlsl_capture_check.py, startup_ngg_slice_check.py, startup_gfx2hlsl_f32_lg_check.py, startup_gfx2hlsl_f32_nan_check.py;
  • startup_gfx2hlsl_cndmask_neg_check.py, startup_gfx2hlsl_op_refusals_check.py, startup_uniform_lane_check.py, startup_gfx2hlsl_scalar_m0_check.py, startup_gfx2hlsl_sop2_complement_check.py, startup_gfx2hlsl_smem_offset_check.py;
  • docs_links_check.py, bidi_controls_check.py.

CTest was not run: no build tree was configured for this change. The registration is one add_test beside its neighbours, and CI runs the script directly.

Evidence and follow-up

  • Source-built fixture (MIT), with no title or package needed.
  • #1750 (open) writes requests from the shader archive without starting the title. Run on such a cache, this mode would give a per-pair census of the remaining gaps, approximate behind each first refusal.
  • Overlaps in tools/startup/gfx2hlsl.py: none textual with #1749 (it rewrites the end of main()) or #1931 (translate() hunks separated by unchanged lines). #1931 and this change both add an add_test after startup_gfx2hlsl_smem_offset in CMakeLists.txt; whichever lands second needs a one-line rebase. #1631 edits the same lines of Pipeline.__init__, Stage.__init__ and the top of Stage.translate(), so the second to land needs a manual rebase; its new Stage argument is positional and this change passes its own by keyword, so the two compose.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6592721700 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1978). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-24T22:31:28Z. --- Refs #1723. ## Change `tools/startup/gfx2hlsl.py` stops a pipeline at its first refused instruction. A cache of refused requests therefore shows one layer of gaps at a time: fixing a refusal reveals the next one behind it, and the census in #1723 notes that the gaps behind the first refusal are undercounted. This adds a read-only diagnostic: ```text python -B tools/startup/gfx2hlsl.py --refusals <cache-dir> [--json] ``` It loads every request in the directory through the same loader as `--pending`, including RTV variants and requests that already have a `.txt` or `.fail`. Each request is translated in memory with a collector: - a refusal raised for one instruction, during decoding or lowering, is recorded and that instruction is skipped; - an undecodable word, or a refusal during request loading, pipeline construction or output generation, ends the pipeline. When it is the first site it is the reason the default path writes to `.fail`, after normalisation; file errors are reported as `unreadable shader request` because their text holds the path and key; - an unexpected handler exception is recorded as `translator crash in <op>: <type>` when nothing was recorded before it, a translator bug, or as `internal error in <op>: <type>` after an earlier refusal or crash, a possible follow-on effect. The report has two views: - **per request key:** for each reason and scope (`decode`, `vs`, `ps`, `pipeline`), the requests that contain it, that reach it first, and that have it as their only distinct reason, plus a histogram of refusal sites per request and the count with no refusal recorded (not compiled); - **per code pair**, as #1723 moved to: base requests grouped by a hash of their `.a` and `.b` bytes, excluding RTV variants, with each reason counted under every observed root signature or under some roots only. Register numbers and immediates are normalised (`sN`, `s[N:N]`, `vN`), as are raw instruction words. Numbers that are the cause, such as a format, a width or a shader model, are kept. No request key or path is printed; refusal text is printed only after this normalisation. Counts after the first refusal are approximate, because a skipped instruction leaves later state unmodelled; the report and the docs say so. The mode writes nothing: `refusals_main` never reaches `translate_to`, opens no file for writing, never starts fxc, and exits before `main()`'s `os.makedirs`. It is selected by `--refusals DIR` or `--refusals=DIR` anywhere on the command line. With the collector unset, the default path is unchanged. The `decode()` encoding checks, #1932's NEG rewrite included, move verbatim into `check_encoding()`. Request loading is split out of `translate_request()` into `load_request()`. The end of `main()` (the pending loop that #1749 rewrites) is untouched. On a translator without this change, `--refusals DIR` is treated as `--pending DIR` and writes into the directory; the docs say so. Registered as CTest `startup_gfx2hlsl_refusals`, in the `python-linux` CI list and in `AGENTS.md`. There is a section in `docs/experiments.md` and a short note in `docs/startup-runtime.md`. ## Verification On `425cd53b` (based on `main` at `889cd3f4`), Windows 11 build 26300, Python 3.14.10: ```text python -B tests/startup_gfx2hlsl_refusals_check.py ``` 47 checks pass, without game data or LLVM, and with `X2W_FXC` empty so fxc is never started. The disassembler is stubbed with synthetic words, except the NEG check, which decodes one hand-assembled `v_cndmask_b32_e64` word with the in-tree decoder. They cover: - stage-level skip and resume; - decode-level skip on a synthetic encoding refusal, and #1932's NEG rewrite kept; - handler errors as crash or follow-on; - request failures (dangling RTV, undecodable word, bad stage set, loader and pipeline exceptions), with the default path still writing the same `.fail` text; - per-request and per-pair counts, including a root-dependent reason and distinct-reason `only`; - normalisation, including `word_0x…`; - both flag orders and `--refusals=DIR`, and a missing directory that is never created; - a cache directory byte-identical afterwards, with `translate_to` patched to raise a `BaseException` subclass. A control stops at the first refusal and requires the second reason to disappear from the report. On `main` the script ends at its first collector call with an uncaught `gfx2hlsl.Unsupported: unsupported_first`. **Local data, not in the repository.** On a local cache of 575 recorded requests (the synthetic check above covers the same properties without it: unchanged default output, an unchanged directory, no key in the report): - `X2W_FXC= python -B tools/startup/gfx2hlsl.py --pending <copy>` with this branch and with `main` at `889cd3f4` gives byte-identical directories (897 files) and identical logs; - `--refusals <copy>` leaves every file byte-identical and prints no 16-hex-digit key or raw `word_0x` name. Also run on this branch, each `python -B tests/<name>` exiting 0: - `startup_translator_check.py`, `startup_gfx2hlsl_capture_check.py`, `startup_ngg_slice_check.py`, `startup_gfx2hlsl_f32_lg_check.py`, `startup_gfx2hlsl_f32_nan_check.py`; - `startup_gfx2hlsl_cndmask_neg_check.py`, `startup_gfx2hlsl_op_refusals_check.py`, `startup_uniform_lane_check.py`, `startup_gfx2hlsl_scalar_m0_check.py`, `startup_gfx2hlsl_sop2_complement_check.py`, `startup_gfx2hlsl_smem_offset_check.py`; - `docs_links_check.py`, `bidi_controls_check.py`. CTest was not run: no build tree was configured for this change. The registration is one `add_test` beside its neighbours, and CI runs the script directly. ## Evidence and follow-up - Source-built fixture (MIT), with no title or package needed. - #1750 (open) writes requests from the shader archive without starting the title. Run on such a cache, this mode would give a per-pair census of the remaining gaps, approximate behind each first refusal. - Overlaps in `tools/startup/gfx2hlsl.py`: none textual with #1749 (it rewrites the end of `main()`) or #1931 (`translate()` hunks separated by unchanged lines). #1931 and this change both add an `add_test` after `startup_gfx2hlsl_smem_offset` in `CMakeLists.txt`; whichever lands second needs a one-line rebase. #1631 edits the same lines of `Pipeline.__init__`, `Stage.__init__` and the top of `Stage.translate()`, so the second to land needs a manual rebase; its new `Stage` argument is positional and this change passes its own by keyword, so the two compose. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
upstream-sync closed this pull request 2026-11-24 23:03:23 +00:00
ChazY11 commented 2026-11-24 23:03:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T22:37:59Z.


Ran it, broke the normaliser five ways: four caught, the hex redaction is unpinned

Windows 11 build 26300.9974, device-free:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_refusals_check.py
exit 0
mutation in normalize_refusal result
s[16:23] range normalisation removed caught
single-register normalisation removed (s16 → sN) caught
word_0x… normalisation removed caught
the startswith label short-circuit removed caught
the hex-operand redaction line removed not caught

What the surviving line does, and why its loss matters

Calling normalize_refusal directly, before and after removing line 2130:

                                          intact                      line removed
v_mov_b32 with literal 0x3f800000      -> v_mov_b32 with literal N      0x3f800000
unexpected value 0x7fc00000 in source  -> unexpected value N in source  0x7fc00000
image_sample_lz dmask:0xf              -> dmask:N                       dmask:N      (other rule)
buffer_load offset 0x10                -> offset N                     offset N     (other rule)

So the operand-position rule at 2132–2133 covers offset 0x10, which is why the fixture still passes — but a bare literal in the
reason text
keeps its exact bits. That is the case the function's own comment is about ("redact hex operands and offsets"), and it
has two consequences for a census: an input-derived value lands in a report meant to be safe to paste into an issue, and two
refusals that differ only in a literal are counted as two different sites — the overcount this diagnostic exists to reduce.

Suggested case, one line in the existing shape:

assert normalize_refusal('v_mov_b32 with literal 0x3f800000') == 'v_mov_b32 with literal N'
assert normalize_refusal('unexpected value 0x7fc00000 in source') == 'unexpected value N in source'

One process note, not a finding about the PR

Two neighbouring checks (startup_translator_check.py, startup_gfx2hlsl_capture_check.py) reported exit 2 in my first batch run
and exit 0 when run individually — that was my own loop, not your change. Both are green on this branch; I re-ran them directly
before writing anything down.

What I like about the design

  • The census is read-only and reuses load_request() with a refusals list instead of a second decode path, so a new refusal site
    is picked up without touching the census.
  • handler_error distinguishes internal error from translator crash depending on whether refusals were being collected — the
    difference between "this form is not lowered yet" and "the translator broke"; getting it backwards would hide crashes.
  • Keeping cause numbers while redacting operands is the right asymmetry: a count is a property of the site, a literal is a property
    of the input.

What this does not establish

Nothing about a full cache: the fixture builds its own requests. The undercount behind the first refusal (#1723's motivation) is a
claim about real caches, not something this check measures.

Verdict

Reviewed. The normaliser is well covered except its hex-redaction line, which the fixture never exercises.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7146462356 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1978#issuecomment-7146462356). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T22:37:59Z. --- ## Ran it, broke the normaliser five ways: four caught, the hex redaction is unpinned Windows 11 build 26300.9974, device-free: ```powershell out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_refusals_check.py exit 0 ``` | mutation in `normalize_refusal` | result | |---|---| | `s[16:23]` range normalisation removed | **caught** | | single-register normalisation removed (`s16` → `sN`) | **caught** | | `word_0x…` normalisation removed | **caught** | | the startswith label short-circuit removed | **caught** | | **the hex-operand redaction line removed** | **not caught** | ## What the surviving line does, and why its loss matters Calling `normalize_refusal` directly, before and after removing line 2130: ```text intact line removed v_mov_b32 with literal 0x3f800000 -> v_mov_b32 with literal N 0x3f800000 unexpected value 0x7fc00000 in source -> unexpected value N in source 0x7fc00000 image_sample_lz dmask:0xf -> dmask:N dmask:N (other rule) buffer_load offset 0x10 -> offset N offset N (other rule) ``` So the operand-position rule at 2132–2133 covers `offset 0x10`, which is why the fixture still passes — but a **bare literal in the reason text** keeps its exact bits. That is the case the function's own comment is about ("redact hex operands and offsets"), and it has two consequences for a census: an input-derived value lands in a report meant to be safe to paste into an issue, and two refusals that differ only in a literal are counted as two different sites — the overcount this diagnostic exists to reduce. Suggested case, one line in the existing shape: ```python assert normalize_refusal('v_mov_b32 with literal 0x3f800000') == 'v_mov_b32 with literal N' assert normalize_refusal('unexpected value 0x7fc00000 in source') == 'unexpected value N in source' ``` ## One process note, not a finding about the PR Two neighbouring checks (`startup_translator_check.py`, `startup_gfx2hlsl_capture_check.py`) reported exit 2 in my first batch run and exit 0 when run individually — that was my own loop, not your change. Both are green on this branch; I re-ran them directly before writing anything down. ## What I like about the design - The census is read-only and reuses `load_request()` with a `refusals` list instead of a second decode path, so a new refusal site is picked up without touching the census. - `handler_error` distinguishes `internal error` from `translator crash` depending on whether refusals were being collected — the difference between "this form is not lowered yet" and "the translator broke"; getting it backwards would hide crashes. - Keeping cause numbers while redacting operands is the right asymmetry: a count is a property of the site, a literal is a property of the input. ## What this does not establish Nothing about a full cache: the fixture builds its own requests. The undercount behind the first refusal (#1723's motivation) is a claim about real caches, not something this check measures. ## Verdict Reviewed. The normaliser is well covered except its hex-redaction line, which the fixture never exercises. ## Notes - AI-assisted review.
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
R0Y00/Xbox2Windows!1971
No description provided.