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

Merged
Asimmimx merged 1 commit from upstream-pr/1716 into main 2026-11-24 17:47:39 +00:00
Asimmimx commented 2026-11-24 17:39:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Asimmimx on 2026-11-24T17:39:30Z.


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).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6587807724 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1716). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-24T17:39:30Z. --- 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).
upstream-sync closed this pull request 2026-11-24 18:02:45 +00:00
tedin7 commented 2026-11-24 18:02:45 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T17:46:19Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6044440065 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1716#pullrequestreview-6044440065). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T17:46:19Z. **Review:** COMMENTED --- 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.
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!1709
No description provided.