Skip to content

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

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
Leyukaka:startup/gfx2hlsl-refusals
Nov 24, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
Leyukaka:startup/gfx2hlsl-refusals

Conversation

@Leyukaka

Copy link
Copy Markdown
Contributor

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:

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 startup: lower NEG on an immediate source of v_cndmask_b32_e64 from its encoding #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

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.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants