tests: ratchet the set of gfx2hlsl handlers that do not refuse malformed operands #2056

Closed
ChazY11 wants to merge 1 commit from upstream-pr/2063 into main
ChazY11 commented 2026-11-24 23:50:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T23:50:55Z.


What

A ratchet check for an invariant that was untested: every Stage.op_* handler must answer malformed operand shapes with Unsupported, never anything else.

Why it matters, in this repository's own terms:

  • Stage.translate records Unsupported as a refusal and any other exception as handler_error(...), labelled internal error — the marker for a translator defect. #1723's census reads those as gaps it cannot act on.
  • With refusals is None (single-shader translation) nothing is caught at all, so the same input exits with a traceback instead of a refusal line.

Measured on main

76 handlers × 7 shapes (none, one to four VGPRs, a truncated register, an attribute token):

260 escapes across 57 handlers
  IndexError: 236    TypeError: 24

IndexError  buffer_load_dword      (no operands)
IndexError  image_load v0 / v0 v1 / v0 v1 v2
TypeError   s_bfm_b32 v[0 v1      'NoneType' object is not subscriptable
TypeError   v_fma_mixhi_f16       not enough arguments for format string

Reachability, stated plainly: these shapes cannot come from decode_instruction, which always produces the operand form of the encoding. So this is not a live decode bug — it is robustness for the text entry point the unit fixtures use. Fixing all 57 handlers is not one reviewable change, so this PR pins the current state instead.

The ratchet

a handler outside PENDING that starts escaping     -> fails   (the regression)
a PENDING entry that stops escaping                -> fails, "remove it from PENDING"
a PENDING entry that is not a handler              -> fails   (a rename shows up)

Two controls, run by hand against tools/startup/gfx2hlsl.py:

# a compliant handler gets an unguarded operand index
handler s_load_dword answers () with IndexError; it must raise Unsupported      -> 1 failed

# a pending handler gains a length check
handler v_mov_b32 now refuses every shape; remove it from PENDING               -> 1 failed

Verification

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_handler_arity_check.py
startup gfx2hlsl handler arity checks: 76 handlers, 57 pending      exit 0

Neighbours still green: startup_gfx2hlsl_swap_check.py, startup_gfx2hlsl_mad_check.py, startup_gfx2hlsl_operand_count_check.py.

Registered as startup_gfx2hlsl_handler_arity in CMakeLists.txt, added to the platform-independent list in .github/workflows/windows.yml, and listed in the AGENTS.md section so the documented list stays in step — the twelve older omissions there are #2053's subject and are not repeated in this PR.

Scope

Test only; no handler is changed. The 57 entries are the honest current state, and the list is meant to shrink.

Notes

  • AI-assisted.

Rebased after #2024, and the ratchet already earned its keep

main added two handlers while this PR was open (s_bitcmp0_b32, s_bitcmp1_b32 from #2024). The rebase into CMakeLists.txt was a two-sided
addition and both registrations are kept. Re-run after the rebase:

startup gfx2hlsl handler arity checks: 78 handlers, 57 pending      exit 0
handlers: 78   pending: 57   compliant: 21
s_bitcmp 系列是否合规: ['s_bitcmp0_b32', 's_bitcmp1_b32']

Both new handlers are already compliant, so the check passes without touching them — and if either had indexed an operand before checking its count,
it would have failed here rather than silently adding a 58th entry later. That is the behaviour this PR exists for.

Neighbours after the rebase: startup_gfx2hlsl_s_bitcmp_check.py, startup_gfx2hlsl_swap_check.py, startup_triage_run_check.py — all exit 0.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6593818412 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2063). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T23:50:55Z. --- ## What A ratchet check for an invariant that was untested: every `Stage.op_*` handler must answer malformed operand shapes with `Unsupported`, never anything else. Why it matters, in this repository's own terms: - `Stage.translate` records `Unsupported` as a refusal and **any other exception** as `handler_error(...)`, labelled `internal error` — the marker for a translator defect. `#1723`'s census reads those as gaps it cannot act on. - With `refusals is None` (single-shader translation) nothing is caught at all, so the same input exits with a traceback instead of a refusal line. ## Measured on `main` 76 handlers × 7 shapes (none, one to four VGPRs, a truncated register, an attribute token): ```text 260 escapes across 57 handlers IndexError: 236 TypeError: 24 IndexError buffer_load_dword (no operands) IndexError image_load v0 / v0 v1 / v0 v1 v2 TypeError s_bfm_b32 v[0 v1 'NoneType' object is not subscriptable TypeError v_fma_mixhi_f16 not enough arguments for format string ``` **Reachability, stated plainly:** these shapes cannot come from `decode_instruction`, which always produces the operand form of the encoding. So this is not a live decode bug — it is robustness for the text entry point the unit fixtures use. Fixing all 57 handlers is not one reviewable change, so this PR pins the current state instead. ## The ratchet ```text a handler outside PENDING that starts escaping -> fails (the regression) a PENDING entry that stops escaping -> fails, "remove it from PENDING" a PENDING entry that is not a handler -> fails (a rename shows up) ``` Two controls, run by hand against `tools/startup/gfx2hlsl.py`: ```text # a compliant handler gets an unguarded operand index handler s_load_dword answers () with IndexError; it must raise Unsupported -> 1 failed # a pending handler gains a length check handler v_mov_b32 now refuses every shape; remove it from PENDING -> 1 failed ``` ## Verification ```powershell out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_handler_arity_check.py startup gfx2hlsl handler arity checks: 76 handlers, 57 pending exit 0 ``` Neighbours still green: `startup_gfx2hlsl_swap_check.py`, `startup_gfx2hlsl_mad_check.py`, `startup_gfx2hlsl_operand_count_check.py`. Registered as `startup_gfx2hlsl_handler_arity` in `CMakeLists.txt`, added to the platform-independent list in `.github/workflows/windows.yml`, and listed in the AGENTS.md section so the documented list stays in step — the twelve older omissions there are #2053's subject and are not repeated in this PR. ## Scope Test only; no handler is changed. The 57 entries are the honest current state, and the list is meant to shrink. ## Notes - AI-assisted. ## Rebased after #2024, and the ratchet already earned its keep `main` added two handlers while this PR was open (`s_bitcmp0_b32`, `s_bitcmp1_b32` from #2024). The rebase into `CMakeLists.txt` was a two-sided addition and both registrations are kept. Re-run after the rebase: ```text startup gfx2hlsl handler arity checks: 78 handlers, 57 pending exit 0 handlers: 78 pending: 57 compliant: 21 s_bitcmp 系列是否合规: ['s_bitcmp0_b32', 's_bitcmp1_b32'] ``` Both new handlers are already compliant, so the check passes without touching them — and if either had indexed an operand before checking its count, it would have failed here rather than silently adding a 58th entry later. That is the behaviour this PR exists for. Neighbours after the rebase: `startup_gfx2hlsl_s_bitcmp_check.py`, `startup_gfx2hlsl_swap_check.py`, `startup_triage_run_check.py` — all exit 0.
upstream-sync closed this pull request 2026-11-25 01:01:24 +00:00
ChazY11 commented 2026-11-25 01:02:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:14:59Z.


Ready behind this PR: one guard that empties twenty-four of the entries

Since the list in this check only shrinks when a handler actually stops escaping, the natural follow-up is to fix a slice. The cheapest slice is the
twenty-four s_cmp_* / s_cmpk_* handlers: they are generated by one factory, and that factory indexes ops[0]/ops[1] without checking the
count, so a single guard covers all of them.

Measured on a branch stacked on this one (startup/scalar-compare-arity, pushed to my fork):

before: 24 of 24 handlers escaped with IndexError, on 5 malformed shapes each
after:  none escape;  PENDING 57 -> 33

with the fail-first control being "remove exactly that guard":

handler s_cmp_eq_i32 answers () with IndexError; it must raise Unsupported
... (24 lines)
startup gfx2hlsl handler arity checks: 24 failed        exit 1

and two-operand lowers unchanged:

s_cmp_lt_u32 s0, s1   -> scc = s0 < s1;
s_cmpk_gt_i32 s2, s3  -> scc = int(s2) > int(s3);
s_cmp_eq_u32 s4, 5    -> scc = s4 == 0x00000005u;

All 28 tests/startup_gfx2hlsl_*_check.py exit 0 there, so the guard does not disturb the shared scalar path.

Why I am not opening it as a second PR yet

gh pr create --base cannot target a branch that exists only in a fork, and opening it against main would duplicate this PR's test file. So the
branch is pushed and waiting rather than racing this one:

  • if this PR lands, the follow-up is a one-file change in gfx2hlsl.py plus the list edit, and I will open it immediately;
  • if you would rather review them together, say so and I will fold the guard into this branch — but that mixes a test-only change with a production
    one, which is why I did not do it by default.

If the slice is not the one you would choose, the sweep is 40 lines and reruns in a second, so any group of the remaining 33 is a candidate — the
s_cmp family is simply the one where a shared factory makes the fix a single line.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148309778 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2063#issuecomment-7148309778). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T00:14:59Z. --- ## Ready behind this PR: one guard that empties twenty-four of the entries Since the list in this check only shrinks when a handler actually stops escaping, the natural follow-up is to fix a slice. The cheapest slice is the twenty-four `s_cmp_*` / `s_cmpk_*` handlers: they are generated by **one factory**, and that factory indexes `ops[0]`/`ops[1]` without checking the count, so a single guard covers all of them. Measured on a branch stacked on this one (`startup/scalar-compare-arity`, pushed to my fork): ```text before: 24 of 24 handlers escaped with IndexError, on 5 malformed shapes each after: none escape; PENDING 57 -> 33 ``` with the fail-first control being "remove exactly that guard": ```text handler s_cmp_eq_i32 answers () with IndexError; it must raise Unsupported ... (24 lines) startup gfx2hlsl handler arity checks: 24 failed exit 1 ``` and two-operand lowers unchanged: ```text s_cmp_lt_u32 s0, s1 -> scc = s0 < s1; s_cmpk_gt_i32 s2, s3 -> scc = int(s2) > int(s3); s_cmp_eq_u32 s4, 5 -> scc = s4 == 0x00000005u; ``` All 28 `tests/startup_gfx2hlsl_*_check.py` exit 0 there, so the guard does not disturb the shared scalar path. ## Why I am not opening it as a second PR yet `gh pr create --base` cannot target a branch that exists only in a fork, and opening it against `main` would duplicate this PR's test file. So the branch is pushed and waiting rather than racing this one: - if this PR lands, the follow-up is a one-file change in `gfx2hlsl.py` plus the list edit, and I will open it immediately; - if you would rather review them together, say so and I will fold the guard into this branch — but that mixes a test-only change with a production one, which is why I did not do it by default. If the slice is not the one you would choose, the sweep is 40 lines and reruns in a second, so any group of the remaining 33 is a candidate — the `s_cmp` family is simply the one where a shared factory makes the fix a single line. AI-assisted.
ChazY11 commented 2026-11-25 01:02:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:16:53Z.


Update: the slice is now thirty-three handlers, in three guards

Both PRs are green here, so I extended the branch that is waiting behind this one. Instead of only s_cmp*, it now covers every handler that
indexes an operand without checking the count and shares a body with its siblings:

s_cmp_*_i32/u32, s_cmpk_*_i32/u32      one factory, 24 handlers   len(ops) != 2
s_and/or/andn2_saveexec_b32/b64        one body,     5 handlers   len(ops) != 2
s_andn2_wrexec_b32/b64                 one body,     2 handlers   len(ops) != 2
s_cselect_b32/b64                      one body,     2 handlers   len(ops) != 3

Measured on startup/scalar-compare-arity (rebased on this PR):

before: 33 of the 33 escaped with IndexError on the malformed shapes
after:  none escape;  PENDING 57 -> 24

One control per guard, removing exactly that guard:

saveexec guard   exit 1   named 5: s_and_saveexec_b32, s_and_saveexec_b64, s_andn2_saveexec_b64,
                                  s_or_saveexec_b32, s_or_saveexec_b64
wrexec guard     exit 1   named 2: s_andn2_wrexec_b32, s_andn2_wrexec_b64
cselect guard    exit 1   named 2: s_cselect_b32, s_cselect_b64
restored         exit 0   78 handlers, 24 pending

Valid forms unchanged, checked directly:

s_and_saveexec_b32 s0, s1   -> s0 = exec; exec = (exec & s1) & 1; scc = exec != 0;
s_andn2_wrexec_b32 s2, s3   -> exec = (s3 & ~exec) & 1; scc = exec != 0; s2 = exec;
s_cselect_b32 s4, s5, s6    -> s4 = scc ? s5 : s6;

All 28 tests/startup_gfx2hlsl_*_check.py exit 0 there. The remaining 24 entries in PENDING are the
buffer_load_* / image_* / tbuffer_* / v_* handlers, which do not share bodies, so each needs its own line — a separate slice, not this one.

Still waiting on this PR rather than racing it: gh pr create --base cannot target a branch that exists only in a fork. Say the word and I will
either open it against main as a two-commit PR or fold these three guards in here.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148344522 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2063#issuecomment-7148344522). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T00:16:53Z. --- ## Update: the slice is now thirty-three handlers, in three guards Both PRs are green here, so I extended the branch that is waiting behind this one. Instead of only `s_cmp*`, it now covers every handler that indexes an operand without checking the count and shares a body with its siblings: ```text s_cmp_*_i32/u32, s_cmpk_*_i32/u32 one factory, 24 handlers len(ops) != 2 s_and/or/andn2_saveexec_b32/b64 one body, 5 handlers len(ops) != 2 s_andn2_wrexec_b32/b64 one body, 2 handlers len(ops) != 2 s_cselect_b32/b64 one body, 2 handlers len(ops) != 3 ``` Measured on `startup/scalar-compare-arity` (rebased on this PR): ```text before: 33 of the 33 escaped with IndexError on the malformed shapes after: none escape; PENDING 57 -> 24 ``` One control per guard, removing exactly that guard: ```text saveexec guard exit 1 named 5: s_and_saveexec_b32, s_and_saveexec_b64, s_andn2_saveexec_b64, s_or_saveexec_b32, s_or_saveexec_b64 wrexec guard exit 1 named 2: s_andn2_wrexec_b32, s_andn2_wrexec_b64 cselect guard exit 1 named 2: s_cselect_b32, s_cselect_b64 restored exit 0 78 handlers, 24 pending ``` Valid forms unchanged, checked directly: ```text s_and_saveexec_b32 s0, s1 -> s0 = exec; exec = (exec & s1) & 1; scc = exec != 0; s_andn2_wrexec_b32 s2, s3 -> exec = (s3 & ~exec) & 1; scc = exec != 0; s2 = exec; s_cselect_b32 s4, s5, s6 -> s4 = scc ? s5 : s6; ``` All 28 `tests/startup_gfx2hlsl_*_check.py` exit 0 there. The remaining 24 entries in `PENDING` are the `buffer_load_*` / `image_*` / `tbuffer_*` / `v_*` handlers, which do not share bodies, so each needs its own line — a separate slice, not this one. Still waiting on this PR rather than racing it: `gh pr create --base` cannot target a branch that exists only in a fork. Say the word and I will either open it against `main` as a two-commit PR or fold these three guards in here. AI-assisted.
ChazY11 commented 2026-11-25 01:02:04 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:24:08Z.


Superseded by #2104

I opened #2104, which is this check together with the guards that empty 33 of its PENDING entries (57 → 24). I combined them because every
other change in this tree ships its check with the behaviour, and because a test-only PR has to wait for a reviewer twice — once for the fixture, once
for the fix.

Nothing about this PR's content changes: tests/startup_gfx2hlsl_handler_arity_check.py is identical to the first commit of #2104. If you would rather
review the fixture alone first, close #2104 and merge this one, then I will reopen the guards as a one-file change against main.

Sorry for the duplicate in the queue — my fault for splitting it this way to begin with.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148471552 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2063#issuecomment-7148471552). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T00:24:08Z. --- ## Superseded by #2104 I opened #2104, which is this check **together with** the guards that empty 33 of its `PENDING` entries (57 → 24). I combined them because every other change in this tree ships its check with the behaviour, and because a test-only PR has to wait for a reviewer twice — once for the fixture, once for the fix. Nothing about this PR's content changes: `tests/startup_gfx2hlsl_handler_arity_check.py` is identical to the first commit of #2104. If you would rather review the fixture alone first, close #2104 and merge this one, then I will reopen the guards as a one-file change against `main`. Sorry for the duplicate in the queue — my fault for splitting it this way to begin with. AI-assisted.
ChazY11 commented 2026-11-25 01:02:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:33:03Z.


Closing this one myself: #2104 carries the same check verbatim except that its PENDING list is 33 entries shorter, because the guards in that PR make those handlers refuse.

Verified rather than assumed:

tests/startup_gfx2hlsl_handler_arity_check.py
  #2063   5781 bytes, 76 quoted entries
  #2104   5162 bytes, 43 quoted entries   (subset of the above)

So nothing in this PR is lost. Keeping both in the queue asked a reviewer to compare two versions of the same file for no benefit, and the decision I offered
("close one or the other") costs someone else time that I can spend instead.

What changed my mind about which to keep: #2092 has just merged, and it is the same shape as #2104 — a production change that ships its check. That is the
shape this tree actually merges, so #2104 is the right vehicle and this one is the redundant half.

If you would rather review the fixture alone first, closing this does not block that: the fixture is the first commit of #2104 and its behaviour is identical
to the version here, apart from the list. Say so there and I will open it as a one-file PR against main.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148625045 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2063#issuecomment-7148625045). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T00:33:03Z. --- Closing this one myself: #2104 carries the same check verbatim except that its `PENDING` list is 33 entries shorter, because the guards in that PR make those handlers refuse. Verified rather than assumed: ```text tests/startup_gfx2hlsl_handler_arity_check.py #2063 5781 bytes, 76 quoted entries #2104 5162 bytes, 43 quoted entries (subset of the above) ``` So nothing in this PR is lost. Keeping both in the queue asked a reviewer to compare two versions of the same file for no benefit, and the decision I offered ("close one or the other") costs someone else time that I can spend instead. What changed my mind about which to keep: #2092 has just merged, and it is the same shape as #2104 — a production change that ships its check. That is the shape this tree actually merges, so #2104 is the right vehicle and this one is the redundant half. If you would rather review the fixture alone first, closing this does not block that: the fixture is the first commit of #2104 and its behaviour is identical to the version here, apart from the list. Say so there and I will open it as a one-file PR against `main`. AI-assisted.

Pull request closed

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!2056
No description provided.