Repository navigation
startup: refuse a binary32 vector operation with the wrong operand count - #1716
Merged
Merged
Conversation
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
reviewed
Nov 24, 2026
tedin7
left a comment
Collaborator
There was a problem hiding this comment.
Peer review at head_sha: 17866fd4f5d1119fb6bb3b980590766f6a286f1f
Verdict: 0 P1, 0 P2.
Scope (full diff read)
tools/startup/gfx2hlsl.py(+3): inStage.one, theVEC_Fbranch now refuses a form whose operand count is notarity + 1withUnsupported('<op> with N operands, expected M'), the same ruleVEC_Ualready has.- New
tests/startup_gfx2hlsl_operand_count_check.py, registered inCMakeLists.txt(startup_gfx2hlsl_operand_count, inside theINTAKE_PYTHONblock next to its neighbours), listed inAGENTS.mdand in the Linux checks of.github/workflows/windows.yml(one line, betweenstartup_image_load_mip_check.pyandstartup_image_2d_array_check.py; no other workflow change).docs/experiments.mdgets one paragraph that matches the code. git merge-treeagainst currentmain(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, andv_rcp_f32_e64 ... clampandv_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_checkandstartup_gfx2hlsl_bit_alu_checkall still exit 0 on this head. llvm-mc-19 -mcpu=gfx1030agrees 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, 41VEC_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 oldIndexErrorand the silently dropped extra source come back.
Findings
- nit: the positive example
v_mac_f32_e64 v1, v2, v3is not a gfx1030 instruction (llvm-mc-19says "instruction not supported on this GPU"). It is only a translator-level example, so nothing breaks, butv_fmac_f32_e64would 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^ HEADclean.- Mutation 1: the
VEC_Fguard condition!=-><(only a short form refused): exit 1,VEC_F: ... MISMATCH [('v_add_f32', ['refused', 'ok', 'ok']), ...]. - Mutation 2: the
VEC_Fguard replaced byif False:: exit 1,VEC_F: ... MISMATCH [('v_add_f32', ['IndexError', 'ok', 'ok']), ...]. - Earlier I also changed both guards (
VEC_FandVEC_U) to>,<and!= arity + 2: each exits 1 with the table mismatches for both tables. - All reverted with
git checkout -- .;git status --shortclean.
AI-assisted; commands listed were executed locally.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #1149.
Change
tools/startup/gfx2hlsl.pychecks the operand count of its integer vector table (VEC_U), but not of its binary32 table (VEC_F). Onorigin/main:v_add_f32 v1, v2(one source short)IndexError: Replacement index 1 out of rangefromstr.format, notUnsupportedv_med3_f32 v1, v2, v3, v4, v5(one source too many)med3(v2, v3, v4); the extra source is droppedVEC_Fnow refuses any count other than its sources plus the destination, with the same message asVEC_U. This is a 3-line change inStage.one.I found this while drawing the
VEC_Foperations in #1712.Test
tests/startup_gfx2hlsl_operand_count_check.py(startup_gfx2hlsl_operand_count) is pure Python. It checks:VEC_F, 41VEC_U) with one source fewer, the exact count and one source more, expectingrefused,okandrefused;v_fmac_f32,v_mac_f32_e64,v_fmamk_f32,v_fmaak_f32,v_madmk_f32,v_madak_f32,v_rcp_f32_e64 ... clamp, andv_med3_f32with an inline constant;IndexErrorand the accepted long form to come back.It is added to the platform-independent Python job and the list in
AGENTS.md.docs/experiments.mdgets one paragraph under the per-lane bit operations section, which describes theVEC_Urule.Results
Commit
17866fd4onorigin/main, Python 3.12, Windows 11 26300.9974:Without the translator change, the same file reports
2 failed(theVEC_Ftable 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.pyandbidi_controls_check.py.AI-assisted (Claude Code).