gfx2hlsl: refuse a scalar ALU source list shorter than the table template uses #2507

Merged
ChazY11 merged 1 commit from upstream-pr/2515 into main 2026-11-25 19:49:37 +00:00
ChazY11 commented 2026-11-25 19:24:51 +00:00 (Migrated from github.com)

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


What

Stage.scalar builds a scalar ALU expression with text.format(*values), where the SCA entry spells its sources
as {0}, {1}, … A source list shorter than the template uses leaves values too short and str.format raises
IndexError, which the single-shader path records as translator crash in <op>: IndexError instead of a refusal
naming the instruction — the class #2463 catalogues.

The arity guards merged in #2469 fire earlier in lower() for their own subsets; this path reaches scalar()
without passing one, so the short form of 36 two-source templates crashed. One guard before text.format covers
all of them, and it returns the same instruction-specific Unsupported shape the rest of the table uses.

# The table templates name their sources as {0}, {1}, ...; a short operand list would make
# str.format raise IndexError, which the single-shader path records as a translator crash
# rather than a refusal.
needed = max((int(i) for i in re.findall(r'\{(\d+)\}', text)), default=-1) + 1
if len(srcs) < needed:
    raise Unsupported('%s with %d operands, expected %d' % (base, len(ops), needed + 1))

Refs #2463

Reproduction

Before the change, on 6d2d3bed:

Probe().one('s_or_b32', 's_or_b32 s1, s2')
File "tools/startup/gfx2hlsl.py", line 1953, in one
    term = self.lower(base, asm, ops, mods)
File "tools/startup/gfx2hlsl.py", line 2198, in lower
    return self.scalar(base, ops)
File "tools/startup/gfx2hlsl.py", line 2473, in scalar
    expr = text.format(*values)
IndexError: Replacement index 1 out of range for positional args tuple

Fuzzing every two-source template with one source fewer gives 35 IndexErrors (s_add_i32 s_add_u32 s_and_b32 s_and_b64 s_andn2_b32 s_andn2_b64 s_ashr_i32 s_bfe_u32 s_lshl[1234]_add_u32 s_lshl_b32 s_lshr_b32 s_max_i32 s_max_u32 s_min_i32 s_min_u32 s_mul_hi_u32 s_mul_i32 s_nand_b32 s_nand_b64 s_nor_b32 s_nor_b64 s_or_b32 s_or_b64 s_orn2_b32 s_orn2_b64 s_pack_hh_b32_b16 s_sub_i32 s_sub_u32 s_xnor_b32 s_xnor_b64 s_xor_b32 s_xor_b64). The
three-operand form is unaffected: every extra source is already refused with a well-formed message.

Checks

python -B tests/startup_gfx2hlsl_scalar_table_arity_check.py
    the SCA table has two-source templates: ok
    no two-source template crashes on a short source list: ok
    every short form is refused with the operand count it needs: ok
    a complete form is never refused for its operand count: ok
    control: without the guard the short form raises IndexError: ok
    the one template with its own arity check still refuses without the guard: ok
    scalar table arity checks: 0 failed

The whole startup_gfx2hlsl_* family on this branch: 67 checks, 0 red.

The two diagnostic paths, as asked

  • Complete form — asserted never to be refused for its operand count, so the guard cannot turn a working
    instruction into a refusal.
  • s_bfm_b32 — it carries its own expected 3 message, so it refuses with or without this guard. The control
    excludes it by name and asserts separately that it still refuses by its own message, rather than counting it as a
    survivor and loosening the threshold.

Negative control

Changing if len(srcs) < needed: to if False: makes the check exit 1; restoring it exits 0. That is the same
in-place mutation the review reported, now part of the fixture.

Registration

CMakeLists.txt, AGENTS.md and the Linux CI list each gain one entry next to
startup_gfx2hlsl_scalar_arity_check.py. No other behaviour changes: accepted instructions translate exactly as
before, and only the refusing path is new.

Scope

One reviewable behaviour: a short source list is refused instead of crashing. The pointer-join and lane-mask claims
mentioned in #2463 stay with their authors; this branch does not touch them.

AI-assisted. Pure Python; no build or GPU needed. Verified on Windows 11 build 26300.9974.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6612011680 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2515). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T19:24:51Z. --- ## What `Stage.scalar` builds a scalar ALU expression with `text.format(*values)`, where the `SCA` entry spells its sources as `{0}`, `{1}`, … A source list shorter than the template uses leaves `values` too short and `str.format` raises `IndexError`, which the single-shader path records as `translator crash in <op>: IndexError` instead of a refusal naming the instruction — the class #2463 catalogues. The arity guards merged in #2469 fire earlier in `lower()` for their own subsets; this path reaches `scalar()` without passing one, so the short form of **36 two-source templates** crashed. One guard before `text.format` covers all of them, and it returns the same instruction-specific `Unsupported` shape the rest of the table uses. ```python # The table templates name their sources as {0}, {1}, ...; a short operand list would make # str.format raise IndexError, which the single-shader path records as a translator crash # rather than a refusal. needed = max((int(i) for i in re.findall(r'\{(\d+)\}', text)), default=-1) + 1 if len(srcs) < needed: raise Unsupported('%s with %d operands, expected %d' % (base, len(ops), needed + 1)) ``` Refs #2463 ## Reproduction Before the change, on `6d2d3bed`: ```python Probe().one('s_or_b32', 's_or_b32 s1, s2') ``` ```text File "tools/startup/gfx2hlsl.py", line 1953, in one term = self.lower(base, asm, ops, mods) File "tools/startup/gfx2hlsl.py", line 2198, in lower return self.scalar(base, ops) File "tools/startup/gfx2hlsl.py", line 2473, in scalar expr = text.format(*values) IndexError: Replacement index 1 out of range for positional args tuple ``` Fuzzing every two-source template with one source fewer gives 35 `IndexError`s (`s_add_i32 s_add_u32 s_and_b32 s_and_b64 s_andn2_b32 s_andn2_b64 s_ashr_i32 s_bfe_u32 s_lshl[1234]_add_u32 s_lshl_b32 s_lshr_b32 s_max_i32 s_max_u32 s_min_i32 s_min_u32 s_mul_hi_u32 s_mul_i32 s_nand_b32 s_nand_b64 s_nor_b32 s_nor_b64 s_or_b32 s_or_b64 s_orn2_b32 s_orn2_b64 s_pack_hh_b32_b16 s_sub_i32 s_sub_u32 s_xnor_b32 s_xnor_b64 s_xor_b32 s_xor_b64`). The three-operand form is unaffected: every extra source is already refused with a well-formed message. ## Checks ```text python -B tests/startup_gfx2hlsl_scalar_table_arity_check.py the SCA table has two-source templates: ok no two-source template crashes on a short source list: ok every short form is refused with the operand count it needs: ok a complete form is never refused for its operand count: ok control: without the guard the short form raises IndexError: ok the one template with its own arity check still refuses without the guard: ok scalar table arity checks: 0 failed ``` The whole `startup_gfx2hlsl_*` family on this branch: **67 checks, 0 red**. ### The two diagnostic paths, as asked - **Complete form** — asserted never to be refused for its operand count, so the guard cannot turn a working instruction into a refusal. - **`s_bfm_b32`** — it carries its own `expected 3` message, so it refuses with or without this guard. The control excludes it by name and asserts separately that it still refuses by its own message, rather than counting it as a survivor and loosening the threshold. ### Negative control Changing `if len(srcs) < needed:` to `if False:` makes the check exit 1; restoring it exits 0. That is the same in-place mutation the review reported, now part of the fixture. ## Registration `CMakeLists.txt`, `AGENTS.md` and the Linux CI list each gain one entry next to `startup_gfx2hlsl_scalar_arity_check.py`. No other behaviour changes: accepted instructions translate exactly as before, and only the refusing path is new. ## Scope One reviewable behaviour: a short source list is refused instead of crashing. The pointer-join and lane-mask claims mentioned in #2463 stay with their authors; this branch does not touch them. AI-assisted. Pure Python; no build or GPU needed. Verified on Windows 11 build 26300.9974.
upstream-sync closed this pull request 2026-11-25 20:04:35 +00:00
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!2507
No description provided.