startup: refuse a binary32 vector operation with the wrong operand count #1716

Manually merged
github merged 1 commit from gh-pr/1716/head into gh-pr/1716/base 2026-11-26 23:28:28 +00:00
Owner

@⁠Asimmimx on GitHub · 2026-11-24 17:39 UTC · original
Asimmimx:startup/gfx2hlsl-vec-f-operand-count → main · +102 −0 · 6 files · 1 commits · merged 2026-11-24 17:47 UTC

Refs #1149.

Change

tools/startup/gfx2hlsl.py checks the operand count of its integer vector table (VEC_U), but not of its binary32 table (VEC_F). On origin/main:

Input Result
v_add_f32 v1, v2 (one source short) IndexError: Replacement index 1 out of range from str.format, not Unsupported
v_med3_f32 v1, v2, v3, v4, v5 (one source too many) translates as med3(v2, v3, v4); the extra source is dropped

VEC_F now refuses any count other than its sources plus the destination, with the same message as VEC_U. This is a 3-line change in Stage.one.

I found this while drawing the VEC_F operations in #1712.

Test

tests/startup_gfx2hlsl_operand_count_check.py (startup_gfx2hlsl_operand_count) is pure Python. It checks:

  • every operation of both tables (32 VEC_F, 41 VEC_U) with one source fewer, the exact count and one source more, expecting refused, ok and refused;
  • that the constant and accumulator forms keep translating with their usual operands: v_fmac_f32, v_mac_f32_e64, v_fmamk_f32, v_fmaak_f32, v_madmk_f32, v_madak_f32, v_rcp_f32_e64 ... clamp, and v_med3_f32 with an inline constant;
  • a negative control that loads the translator with the new guard removed and requires the old IndexError and the accepted long form to come back.

It is added to the platform-independent Python job and the list in AGENTS.md. docs/experiments.md gets one paragraph under the per-lane bit operations section, which describes the VEC_U rule.

Results

Commit 17866fd4 on origin/main, Python 3.12, Windows 11 26300.9974:

python -B tests/startup_gfx2hlsl_operand_count_check.py
VEC_F: each operation translates with exactly its sources and refuses one fewer or one more (32 operations): ok
VEC_U: ... (41 operations): ok
...
control: without the guard v_add_f32 with one source raises IndexError: ok
control: without the guard v_med3_f32 with four sources translates: ok
operand count checks: 0 failed

Without the translator change, the same file reports 2 failed (the VEC_F table check and the guard control) and exits 1.

These also still pass: startup_gfx2hlsl_bit_alu_check.py, startup_translator_check.py, startup_gfx2hlsl_sdwa_check.py, startup_flat_param_check.py, startup_gfx2hlsl_smem_offset_check.py, startup_inline_sampler_check.py, startup_image_sample_c_check.py, docs_links_check.py and bidi_controls_check.py.

AI-assisted (Claude Code).

**@⁠Asimmimx** on GitHub · 2026-11-24 17:39 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1716) `Asimmimx:startup/gfx2hlsl-vec-f-operand-count` → `main` · +102 −0 · 6 files · 1 commits · merged 2026-11-24 17:47 UTC Refs #1149. ## Change `tools/startup/gfx2hlsl.py` checks the operand count of its integer vector table (`VEC_U`), but not of its binary32 table (`VEC_F`). On `origin/main`: | Input | Result | |---|---| | `v_add_f32 v1, v2` (one source short) | `IndexError: Replacement index 1 out of range` from `str.format`, not `Unsupported` | | `v_med3_f32 v1, v2, v3, v4, v5` (one source too many) | translates as `med3(v2, v3, v4)`; the extra source is dropped | `VEC_F` now refuses any count other than its sources plus the destination, with the same message as `VEC_U`. This is a 3-line change in `Stage.one`. I found this while drawing the `VEC_F` operations in #1712. ## Test `tests/startup_gfx2hlsl_operand_count_check.py` (`startup_gfx2hlsl_operand_count`) is pure Python. It checks: - every operation of both tables (32 `VEC_F`, 41 `VEC_U`) with one source fewer, the exact count and one source more, expecting `refused`, `ok` and `refused`; - that the constant and accumulator forms keep translating with their usual operands: `v_fmac_f32`, `v_mac_f32_e64`, `v_fmamk_f32`, `v_fmaak_f32`, `v_madmk_f32`, `v_madak_f32`, `v_rcp_f32_e64 ... clamp`, and `v_med3_f32` with an inline constant; - a negative control that loads the translator with the new guard removed and requires the old `IndexError` and the accepted long form to come back. It is added to the platform-independent Python job and the list in `AGENTS.md`. `docs/experiments.md` gets one paragraph under the per-lane bit operations section, which describes the `VEC_U` rule. ## Results Commit `17866fd4` on `origin/main`, Python 3.12, Windows 11 26300.9974: ``` python -B tests/startup_gfx2hlsl_operand_count_check.py VEC_F: each operation translates with exactly its sources and refuses one fewer or one more (32 operations): ok VEC_U: ... (41 operations): ok ... control: without the guard v_add_f32 with one source raises IndexError: ok control: without the guard v_med3_f32 with four sources translates: ok operand count checks: 0 failed ``` Without the translator change, the same file reports `2 failed` (the `VEC_F` table check and the guard control) and exits 1. These also still pass: `startup_gfx2hlsl_bit_alu_check.py`, `startup_translator_check.py`, `startup_gfx2hlsl_sdwa_check.py`, `startup_flat_param_check.py`, `startup_gfx2hlsl_smem_offset_check.py`, `startup_inline_sampler_check.py`, `startup_image_sample_c_check.py`, `docs_links_check.py` and `bidi_controls_check.py`. AI-assisted (Claude Code).
gfx2hlsl checked the operand count of its integer table (VEC_U) but not of
its binary32 table (VEC_F). A form one source short, such as
v_add_f32 v1, v2, raised IndexError from str.format instead of
Unsupported, and a form with one source too many, such as
v_med3_f32 v1, v2, v3, v4, v5, translated with the extra source dropped.
VEC_F now refuses any count other than its sources plus the destination.

tests/startup_gfx2hlsl_operand_count_check.py checks every operation of
both tables with one source fewer, the exact count and one more, keeps the
constant and accumulator forms (fmac, mac, fmamk, fmaak, madmk, madak)
translating, and loads the translator with the new guard removed to
require the old IndexError and the silently accepted long form to come
back. Registered as startup_gfx2hlsl_operand_count and added to the
platform-independent Python job.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:28:26 +00:00
github manually merged commit 17866fd4f5 into gh-pr/1716/base 2026-11-26 23:28:28 +00:00
Author
Owner

@⁠tedin7 on GitHub · 2026-11-24 17:46 UTC · original · reviewed

Peer review at head_sha: 17866fd4f5d1119fb6bb3b980590766f6a286f1f

Verdict: 0 P1, 0 P2.

Scope (full diff read)

  • tools/startup/gfx2hlsl.py (+3): in Stage.one, the VEC_F branch now refuses a form whose operand count is not arity + 1 with Unsupported('<op> with N operands, expected M'), the same rule VEC_U already has.
  • New tests/startup_gfx2hlsl_operand_count_check.py, registered in CMakeLists.txt (startup_gfx2hlsl_operand_count, inside the INTAKE_PYTHON block next to its neighbours), listed in AGENTS.md and in the Linux checks of .github/workflows/windows.yml (one line, between startup_image_load_mip_check.py and startup_image_2d_array_check.py; no other workflow change). docs/experiments.md gets one paragraph that matches the code.
  • git merge-tree against current main (eaa2024b): no conflicts. It also merges cleanly with #1712.

What I checked

  • The guard sits after the SDWA/DPP dispatch and before the arguments are built, so only plain and VOP3 spellings reach it. The accumulator and constant forms keep their arity: v_fmac_f32/v_mac_f32 (2 sources + the destination as accumulator), v_fmamk/fmaak/madmk/madak (3) all still translate, and v_rcp_f32_e64 ... clamp and v_med3_f32 ... 1.0 ... too (the PR's own check asserts this).
  • Regression: startup_gfx2hlsl_capture_check, startup_gfx2hlsl_sdwa_check, startup_gfx2hlsl_smem_offset_check, startup_translator_check and startup_gfx2hlsl_bit_alu_check all still exit 0 on this head.
  • llvm-mc-19 -mcpu=gfx1030 agrees with the rule: v_add_f32 v1, v2 -> "too few operands for instruction", v_med3_f32 v1, v2, v3, v4, v5 -> "invalid operand for instruction"; the exact-count forms (v_add_f32, v_med3_f32, v_fmamk_f32, v_fmaak_f32, v_rcp_f32_e64 ... clamp) assemble.
  • The check covers every operation of both tables (32 VEC_F, 41 VEC_U) with one source fewer, the exact count and one more, and it loads a copy of the translator with the guard removed to show that the old IndexError and the silently dropped extra source come back.

Findings

  • nit: the positive example v_mac_f32_e64 v1, v2, v3 is not a gfx1030 instruction (llvm-mc-19 says "instruction not supported on this GPU"). It is only a translator-level example, so nothing breaks, but v_fmac_f32_e64 would be the form real code contains.

Verified

Limits: Linux x86-64 only. No Windows run, no D3D12.

  • python3 -B tests/startup_gfx2hlsl_operand_count_check.py -> "operand count checks: 0 failed", exit 0. python3 -B tests/docs_links_check.py -> 17 files, 364 links OK. git diff --check HEAD^ HEAD clean.
  • Mutation 1: the VEC_F guard condition != -> < (only a short form refused): exit 1, VEC_F: ... MISMATCH [('v_add_f32', ['refused', 'ok', 'ok']), ...].
  • Mutation 2: the VEC_F guard replaced by if False:: exit 1, VEC_F: ... MISMATCH [('v_add_f32', ['IndexError', 'ok', 'ok']), ...].
  • Earlier I also changed both guards (VEC_F and VEC_U) to >, < and != arity + 2: each exits 1 with the table mismatches for both tables.
  • All reverted with git checkout -- .; git status --short clean.

AI-assisted; commands listed were executed locally.

**@⁠tedin7** on GitHub · 2026-11-24 17:46 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1716#pullrequestreview-6044440065) · **reviewed** Peer review at `head_sha`: `17866fd4f5d1119fb6bb3b980590766f6a286f1f` **Verdict: 0 P1, 0 P2.** ### Scope (full diff read) - `tools/startup/gfx2hlsl.py` (+3): in `Stage.one`, the `VEC_F` branch now refuses a form whose operand count is not `arity + 1` with `Unsupported('<op> with N operands, expected M')`, the same rule `VEC_U` already has. - New `tests/startup_gfx2hlsl_operand_count_check.py`, registered in `CMakeLists.txt` (`startup_gfx2hlsl_operand_count`, inside the `INTAKE_PYTHON` block next to its neighbours), listed in `AGENTS.md` and in the Linux checks of `.github/workflows/windows.yml` (one line, between `startup_image_load_mip_check.py` and `startup_image_2d_array_check.py`; no other workflow change). `docs/experiments.md` gets one paragraph that matches the code. - `git merge-tree` against current `main` (`eaa2024b`): no conflicts. It also merges cleanly with #1712. ### What I checked - The guard sits after the SDWA/DPP dispatch and before the arguments are built, so only plain and VOP3 spellings reach it. The accumulator and constant forms keep their arity: `v_fmac_f32`/`v_mac_f32` (2 sources + the destination as accumulator), `v_fmamk/fmaak/madmk/madak` (3) all still translate, and `v_rcp_f32_e64 ... clamp` and `v_med3_f32 ... 1.0 ...` too (the PR's own check asserts this). - Regression: `startup_gfx2hlsl_capture_check`, `startup_gfx2hlsl_sdwa_check`, `startup_gfx2hlsl_smem_offset_check`, `startup_translator_check` and `startup_gfx2hlsl_bit_alu_check` all still exit 0 on this head. - `llvm-mc-19 -mcpu=gfx1030` agrees with the rule: `v_add_f32 v1, v2` -> "too few operands for instruction", `v_med3_f32 v1, v2, v3, v4, v5` -> "invalid operand for instruction"; the exact-count forms (`v_add_f32`, `v_med3_f32`, `v_fmamk_f32`, `v_fmaak_f32`, `v_rcp_f32_e64 ... clamp`) assemble. - The check covers every operation of both tables (32 `VEC_F`, 41 `VEC_U`) with one source fewer, the exact count and one more, and it loads a copy of the translator with the guard removed to show that the old `IndexError` and the silently dropped extra source come back. ### Findings - **nit:** the positive example `v_mac_f32_e64 v1, v2, v3` is not a gfx1030 instruction (`llvm-mc-19` says "instruction not supported on this GPU"). It is only a translator-level example, so nothing breaks, but `v_fmac_f32_e64` would be the form real code contains. ### Verified Limits: Linux x86-64 only. No Windows run, no D3D12. - `python3 -B tests/startup_gfx2hlsl_operand_count_check.py` -> "operand count checks: 0 failed", exit 0. `python3 -B tests/docs_links_check.py` -> 17 files, 364 links OK. `git diff --check HEAD^ HEAD` clean. - **Mutation 1:** the `VEC_F` guard condition `!=` -> `<` (only a short form refused): exit 1, `VEC_F: ... MISMATCH [('v_add_f32', ['refused', 'ok', 'ok']), ...]`. - **Mutation 2:** the `VEC_F` guard replaced by `if False:`: exit 1, `VEC_F: ... MISMATCH [('v_add_f32', ['IndexError', 'ok', 'ok']), ...]`. - Earlier I also changed both guards (`VEC_F` and `VEC_U`) to `>`, `<` and `!= arity + 2`: each exits 1 with the table mismatches for both tables. - All reverted with `git checkout -- .`; `git status --short` clean. AI-assisted; commands listed were executed locally.
This discussion has been locked. Commenting is limited to contributors.
No description provided.