Skip to content

startup: refuse a scalar instruction with the wrong operand count (33 handlers) - #2104

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
ChazY11:startup/scalar-compare-arity
Nov 25, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
ChazY11:startup/scalar-compare-arity

Conversation

@ChazY11

@ChazY11 ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

What

Thirty-three handlers in tools/startup/gfx2hlsl.py index ops[0]/ops[1]/ops[2] without checking how many operands they were given, so a
malformed instruction comes back as IndexError instead of Unsupported. In this translator that is not a cosmetic difference:

  • Stage.translate records Unsupported as a refusal and any other exception as handler_error(...), labelled internal error — its own marker
    for a translator defect, which #1723's census then counts as a gap nobody can 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.

Three guards, each in the one body several handlers share:

body handlers guard
the s_cmp/s_cmpk factory 24 len(ops) != 2
op_s_and_saveexec_b64 5 (s_and/s_or/s_andn2_saveexec_b32/b64) len(ops) != 2
op_s_andn2_wrexec_b64 2 len(ops) != 2
scalar_select 2 (s_cselect_b32/b64) len(ops) != 3

Measured

The sweep feeds every op_* handler operand shapes no decoder produces (none, one to four VGPRs, a truncated register, an attribute token):

before: 33 of the 33 escaped with IndexError
after:  none escape

Valid forms are unchanged, checked directly rather than by reading the diff:

s_cmp_lt_u32 s0, s1          -> scc = s0 < s1;
s_cmpk_gt_i32 s2, s3         -> scc = int(s2) > int(s3);
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;

The check this comes with

tests/startup_gfx2hlsl_handler_arity_check.py is the fixture that produced those numbers, and it is a ratchet rather than a snapshot:

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

PENDING holds the handlers that still do not refuse, and this PR takes it from 57 to 24. Registered as startup_gfx2hlsl_handler_arity in
CMakeLists.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:

s_cmp factory guard   exit 1   named 24: s_cmp_eq_i32 … s_cmpk_lt_u32
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.

Two notes on scope

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.

…med operands

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 R0Y00#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 R0Y00#2053's subject, not repeated here).
… handlers)

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 R0Y00#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.
@ChazY11
ChazY11 force-pushed the startup/scalar-compare-arity branch from e27a66f to d507055 Compare November 25, 2026 00:44
@R0Y00

R0Y00 commented Nov 25, 2026

Copy link
Copy Markdown
Owner

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.

@R0Y00
R0Y00 merged commit 3433da0 into R0Y00:main Nov 25, 2026
5 checks passed
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.

2 participants