startup: refuse a scalar instruction with the wrong operand count (33 handlers) #2104
No reviewers
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows!2104
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2104/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@ChazY11 on GitHub · 2026-11-25 00:23 UTC · original
ChazY11:startup/scalar-compare-arity→main· +62 −1 · 5 files · 3 commits · merged 2026-11-25 00:53 UTCWhat
Thirty-three handlers in
tools/startup/gfx2hlsl.pyindexops[0]/ops[1]/ops[2]without checking how many operands they were given, so amalformed instruction comes back as
IndexErrorinstead ofUnsupported. In this translator that is not a cosmetic difference:Stage.translaterecordsUnsupportedas a refusal and any other exception ashandler_error(...), labelledinternal error— its own markerfor a translator defect, which
#1723's census then counts as a gap nobody can act on;refusals is None(single-shader translation) nothing is caught at all, so the same input exits with a traceback instead of a refusal line.Three guards, each in the one body several handlers share:
s_cmp/s_cmpkfactorylen(ops) != 2op_s_and_saveexec_b64s_and/s_or/s_andn2_saveexec_b32/b64)len(ops) != 2op_s_andn2_wrexec_b64len(ops) != 2scalar_selects_cselect_b32/b64)len(ops) != 3Measured
The sweep feeds every
op_*handler operand shapes no decoder produces (none, one to four VGPRs, a truncated register, an attribute token):Valid forms are unchanged, checked directly rather than by reading the diff:
The check this comes with
tests/startup_gfx2hlsl_handler_arity_check.pyis the fixture that produced those numbers, and it is a ratchet rather than a snapshot:PENDINGholds the handlers that still do not refuse, and this PR takes it from 57 to 24. Registered asstartup_gfx2hlsl_handler_arityinCMakeLists.txt, added to the platform-independent list in.github/workflows/windows.yml, and listed in AGENTS.md.Fail-first, one control per guard
Removing exactly one guard at a time and running the check names exactly the handlers that share that body:
All 28
tests/startup_gfx2hlsl_*_check.pyexit 0.Two notes on scope
with the behaviour; if you would rather have the test alone first, say so and I will split it back and reopen #2063 against
main.PENDINGentries are thebuffer_load_*/image_*/tbuffer_*/v_*handlers, which do not share bodies — each needs itsown line, so that is a separate slice and deliberately not here.
What this does not establish
That any of these shapes comes from a real shader. They cannot: the decoder always produces the operand form of the encoding. This is robustness of
the text entry point the unit fixtures use, not a live decode bug, and the check says so in its docstring so the next reader does not over-read the
number.
AI-assisted.
Every `Stage.op_*` handler is fed operand shapes the decoder cannot produce: none, one to four plain VGPRs, a truncated register and an attribute token. A handler must answer each one with `Unsupported`, because that is the only exception the translator treats as a refusal: * `Stage.translate` records `Unsupported` as a refusal and any other exception as `handler_error(...)`, which labels the entry `internal error` -- the project's marker for a translator defect, not a missing lowering, so #1723's census counts it as a gap 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. On main the sweep finds 260 escapes across 57 of 76 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 These shapes cannot come from `decode_instruction`, so this is not a live decode bug; it is a robustness invariant that was untested. Fixing all 57 handlers is not one reviewable change, so this pins the current state instead: `PENDING` lists the handlers that do not hold the invariant today. * a handler outside `PENDING` that starts escaping fails the check; * an entry in `PENDING` that stops escaping also fails, with "remove it from PENDING"; * a `PENDING` entry that is not a handler fails, so a rename shows up here. Verified with two controls against `tools/startup/gfx2hlsl.py`: # a compliant handler starts indexing an operand without a length check 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 Command and result: 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 Registered as `startup_gfx2hlsl_handler_arity` in CMakeLists.txt, added to the platform-independent run list in `.github/workflows/windows.yml`, and listed in the AGENTS.md Pure-Python section so the documented list stays in step (the twelve older omissions are #2053's subject, not repeated here).Nine handlers in `tools/startup/gfx2hlsl.py` index `ops[0]`/`ops[1]`/`ops[2]` without checking how many operands they were given, and they answer a malformed instruction with `IndexError` instead of `Unsupported`. The batch translator records that as `internal error` -- its own marker for a translator defect, not a missing lowering -- and the single-shader path does not catch it at all. Three guards, each in the one body that several handlers share, cover thirty-three handlers: op_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 scalar_select, 2 handlers len(ops) != 3 Measured with the sweep that no decoder output can produce (none, one to four VGPRs, a truncated register, an attribute token): before: 33 of the 33 escaped with IndexError after: none escape; PENDING 57 -> 24 Two- and three-operand lowers are unchanged, checked directly rather than by reading the diff: s_cmp_lt_u32 s0, s1 -> scc = s0 < s1; 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; Fail-first, one control per guard -- remove exactly that guard and the ratchet names exactly the handlers that share the body: 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 All 28 `tests/startup_gfx2hlsl_*_check.py` exit 0. This is a follow-up to #2063, which introduces `tests/startup_gfx2hlsl_handler_arity_check.py`; the branch is based on it so the list edit sits next to the guards that empty those entries.@R0Y00 on GitHub · 2026-11-25 00:48 UTC · original
I retained the 33 scalar dispatch guards. I replaced the generic handler probe because it skipped successful returns and assigned the same operand shapes to unrelated handlers. The revised test uses the real batch Stage path and checks 33 accepted operand lists and 99 distinct arity refusals. A control that silently drops the extra compare operand is detected.