Skip to content

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

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
Asimmimx:startup/gfx2hlsl-vec-f-operand-count
Nov 24, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
Asimmimx:startup/gfx2hlsl-vec-f-operand-count

Conversation

@Asimmimx

Copy link
Copy Markdown
Contributor

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.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@R0Y00
R0Y00 merged commit a1f2625 into R0Y00:main Nov 24, 2026
5 checks passed
denn0x added a commit to denn0x/Xbox2Windows that referenced this pull request Nov 25, 2026
Brings R0Y00#1707 (output modifiers on float conversions, VEC_U_TO_F), R0Y00#1716 (operand-count guard),
R0Y00#1697 (triage slot-133 counts) and test/tool PRs.

Conflict: gfx2hlsl.py set_vf, took upstream's combined scale-then-clamp (same result as the
local comment described). Semantic fixes: the local op_v_cvt_f16_f32 handler bypasses VEC_U, so it
now applies R0Y00#1716's operand-count rule itself; the local coverage check expects R0Y00#1707's
U((float(int(v0))) * 2.0) form for v_cvt_f32_i32 mul:2.
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.

3 participants