Skip to content

startup: refuse a lane mask read as numeric data by the remaining ALU forms in gfx2hlsl (Part of #1723) - #2702

Merged
MasterDomino merged 30 commits into
R0Y00:mainfrom
tedin7:x2w-lanemask-data
Nov 26, 2026
Merged

MasterDomino merged 30 commits into
R0Y00:mainfrom
tedin7:x2w-lanemask-data

Conversation

@tedin7

@tedin7 tedin7 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #1723

What this unblocks: stops silently wrong shader output when a wave lane mask is read as a number (the llvm.amdgcn.ballot word and anything derived from it).

What changed

gfx2hlsl.py keeps one lane's bit of every wave mask (vcc, exec, SGPRs that hold a compare or ballot result). On RDNA 2 a mask register is the whole wave's bit set, so any instruction that reads it as a number sees the other lanes' bits too. The one-lane model has only its own bit, so the lowered word was wrong and nothing refused it. #2623 covers v_mov_b32 and v_cndmask_b32 data sources; this PR covers the rest:

  • A vector ALU instruction with a lane-mask source (vcc, exec, or an SGPR tracked as a mask, including results of bitwise ops on a mask) is refused: <op> reads lane mask <reg> as numeric data. A vector compare's sources count as data, its destination does not.
  • A scalar ALU instruction outside the mask-aware set (mov/and/or/xor/andn2/orn2/nand/nor/xnor/not/cselect/wqm, the *saveexec forms, branches, ff1, bitset) that reads a mask is refused the same way (this commit is the scalar half).
  • The high word of a wave64 mask pair (for example s_xor_b64 s[0:1], vcc, s[0:1] then v_mov_b32 v0, s1) is modelled as 0 but is the other lanes' bits on hardware: a v_mov_b32 of it is refused.
  • v_shift64 with vcc_lo or exec_lo as the shift count is no longer accepted (a mask as a count).

Why it is correct

The hardware rule: a mask SGPR/vcc/exec holds one bit per lane of the wave (64 in wave64). A numeric read returns that word; the model returns 0 or 1. So the only reads the model can honour are the per-lane ones: the select of v_cndmask, the carry of v_addc/v_subb/*_co, v_readlane/v_writelane, bitwise mask algebra and saveexec. Those stay allowed.

What stays refused / translating

Mask-only uses (select, carry, bitwise mask algebra, saveexec, branches on vcc/scc) translate as before. v_mov_b32 of a non-mask SGPR translates as before.

Tests

tests/startup_gfx2hlsl_compute_check.py: check_lane_mask_data (vector ALU reading vcc_lo; select still translates), check_lane_mask_high_word (move of the high word refused; move of a non-mask SGPR translates), check_lane_mask_derived_word (s_and_b32 s6, vcc_lo, 0xff then a vector compare or add reading s6 is refused). Each fails on main without the lowering change and passes with it. Existing suites updated for the earlier, more specific refusal message: readfirstlane_uniform, readfirstlane_recompute, shift64.

Verification

  • python3 -B tests/startup_gfx2hlsl_compute_check.py: PASS (fails before the change, see above)
  • python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py, ..._readfirstlane_recompute_check.py, ..._shift64_check.py, ..._sdwa_check.py, ..._sdwa_cndmask_check.py, ..._mask_data_check.py, ..._float_alu_check.py: PASS
  • All 107 tests/startup_gfx2hlsl_*_check.py run with no arguments on this branch and on origin/main: identical result per script (105 PASS; export_null_draw and mul24_align_draw need a compiler and renderer and exit 1 on both).
  • Win2025 (Windows Server 2025, MSVC 14.44.35207, D3D12 WARP) on head bf138972a56b7ec0c40036206cbc30ec432592ad: x2w_pipe.py win2025 2702 --host aws-1 --no-skip-build --py-checks tests/startup_gfx2hlsl_compute_check.py --targets offscreen_check --regex 'startup_gfx2hlsl_(compute|mask|lane|vcc|cndmask|join|readfirstlane|cmp)': dev 56/56 passed, release 56/56 passed (includes startup_gfx2hlsl_compute_warp and _hardware). Build and run: cmake --build --preset dev --target offscreen_check then ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_(compute|mask|lane|vcc|cndmask|join|readfirstlane|cmp)' -V.
  • Linux, independent review: all 111 tests/startup_gfx2hlsl_* scripts give the same result on the merge of this branch with current main as on main (only export_null_draw and mul24_align_draw, which need a compiler and renderer, exit 1 on both). Every case of CASES in startup_gfx2hlsl_compute_check.py translates (23 of 23). The one-thread boundary was probed both ways: group (1,1,1) translates; (2,1,1), (1,2,1), (1,1,2), (3,1,1), (4,1,1), (32,1,1), (64,1,1) are refused. Three mutations of lane_mask_exact (always false, always true, at most two threads) and of the tag propagation each make startup_gfx2hlsl_compute_check.py fail.

AI-assisted; commands listed were executed locally.

tedin7 and others added 11 commits November 26, 2026 03:37
…ata in gfx2hlsl

(cherry picked from commit 6a91c26539918a6906104084ac638b05d8a267bb)
…hlsl

(cherry picked from commit 72945939f8e39c9948aa26fbbf1760f21832ba3c)
…a in gfx2hlsl

A scalar compare, shift or add on vcc/exec/a mask SGPR (s_cmpk_gt_u32 vcc_lo, s_add_u32 s6, vcc_lo, 1) reads the whole wave's word on hardware; the one-lane model has only the lane's own bit, so the lowering was silently wrong (the ballot word compared with a constant selected the wrong value on WARP). Mask-only scalar ops (mov, and/or/xor/andn2/orn2/not, cselect, wqm, saveexec, cbranch, s_ff1 which has its own checks) are unchanged. Test: check_lane_mask_scalar_data (fails before, passes after); three readfirstlane_uniform controls now name the earlier refusal.

(cherry picked from commit af4cf6e7e1423a84fe473fdeb5d1076290e74ec7)
…mask in gfx2hlsl

Only v_mov_b32 refused a read of s1 after a wave64 mask pair was written; v_add_nc_u32, v_cvt_f32_u32, v_and_b32, s_add_u32, s_cmp_eq_u32 and s_mov_b32 of it still lowered the high word as 0. Every vector or scalar op that takes the high word as a single-register operand now goes through the same refusal; the pair forms (select, saveexec) are unchanged. Test: check_lane_mask_high_word covers one form per class and keeps the non-mask SGPR control.
…a varying scalar case

After merging main (R0Y00#2455) that case meets the numeric-data refusal of v_lshl_add_u32 before the readfirstlane refusal; the expected text names it.
…ation in gfx2hlsl

The high word of a wave64 mask was tagged at five separate write sites, so a copy (s_mov_b64 s[8:9], s[0:1] or vcc) lost the tag and its numeric reads translated as 0. The tag now moves in one place, Stage.propagate_mask_tags, after every instruction: a mask-only scalar op (mov, bitwise, cselect, saveexec) gives its destination the tag of its sources, both words of a b64 pair; any other write clears it (set_s); every numeric read of a tagged word is refused in Stage.lower. Test: check_lane_mask_tag_propagation has one case per way a tagged word is made or copied plus a clearing control; disabling each rule fails it. Two readfirstlane_uniform controls (copied exec high word) now name the earlier refusal.
…e group

A group of one thread has one participating lane per wave, so the lane bit is the whole mask and its high word is 0: the one-lane model is exact and dispatch case E (saveexec and exec_lo copies read as numbers) translates again. With more than one thread, or in any other stage, the numeric read stays refused. Test: check_lane_mask_single_thread_group covers both sides of the boundary for the low word and the wave64 high word; forcing the boundary either way fails the compute check or the dispatch cases.
@tedin7
tedin7 marked this pull request as ready for review November 26, 2026 03:16
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

@MasterDomino this is ready for review at head d7eb2e311.

Independent gate review plus Windows run, four rounds. What it caught and the author fixed, so you know what was checked:

  • a numeric read of the high word of a wave64 mask pair was refused only for v_mov_b32; now every single-register read and every copy or combination of a mask pair carries the tag (one place, propagate_mask_tags), with a refusal test per kind;
  • merging current main made startup_gfx2hlsl_readfirstlane_uniform_check fail on a case whose expected message changed; updated;
  • the new refusal broke dispatch case E of startup_gfx2hlsl_compute_check (a saveexec result read as a number in a one-thread group, where the one-lane model is exact), visible only on Windows (compute_warp/compute_hardware); now a numeric mask read stays lowered in a compute group of one thread and is refused in every other group size.

Verification is in the PR body (commands, dev 36/36 and release 36/36 on WARP on this exact head, 111 Linux scripts identical to main, boundary probed both ways, mutations fail the tests).

AI-assisted; commands listed were executed locally.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T05:44:28.112543Z bf13897 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d7eb2e3118

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/startup/gfx2hlsl.py Resolved
Comment thread tools/startup/gfx2hlsl.py Resolved
Comment thread tools/startup/gfx2hlsl.py Resolved
…sets per pass

A copy of a wave64 mask's high word was tagged as a low word (numeric use got '& 1'); a wide write over a tagged pair left the high-word tag (false refusal); the high-word set was not reset when a translation pass restarts.
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Independent re-check of the fix pushed after Ready (f837dc2: high-word tag stays a high word, ends on overwrite, resets per pass), head 0700386.

  • Counter-shapes (compute, s_mov_b64 s[20:21], exec first): copy of the low word then a numeric use is refused in a 2-thread group and translates in a 1-thread group; copy of the high word is refused in a 2-thread group; a wide write (s_mov_b64 s[20:21], 0) ends the tag, so the next read of s21 is not falsely refused.
  • tests/startup_gfx2hlsl_compute_check.py fails on the parent of the fix and passes with it.
  • Win2025 (dev + release) was already run on this exact head (36/36); not repeated.

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 marked this pull request as draft November 26, 2026 04:13
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Moving this back to Draft for now: an extra round of generated test shapes found a few more ways a wave64 mask high word can still be read as a number (for example a copy to another register pair, then a scalar add). Fixing them with tests; it goes back to Ready once they pass.

AI-assisted; commands listed were executed locally.

A v_cmp or carry-out into an even SGPR pair is printed by its low word only, so a numeric read of the other 32 lanes' word
(s5 after s[4:5]) translated as a user constant. tag_compare_pair marks s(N+1) as the mask's high word, in the one place
the tags move; tests cover direct reads, pair and word copies, and a pre-compare read that stays translatable.
…carry-out destination

tag_compare_pair does not tag s(N+1) when it already holds its own mask (a wave32 compare or carry next to it); the
high-word read check skips the VOP3b carry-out operand, which is a destination. The mask-overwrite check uses s20 as the
OR's second operand (s7 is the high word of the s6 compare in wave64) and gains pixel and vertex stage refusals of a mask
read as a number.
A scalar mask-copy op whose sources carry no tag left the old tag on its destination when an earlier path or a loop back
edge had set it, so a later s_load through s[14:15] was refused as a read of a lane mask. The destination words now drop
the tag; a loop fixture checks the load translates and that a copy of the mask itself is still refused.
@tedin7
tedin7 marked this pull request as ready for review November 26, 2026 05:11
@tedin7

tedin7 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

@MasterDomino Ready for review on head bf13897 (re-gate after the CI fix and a review-thread fix).

History: marked Ready on ee0dead, put back to Draft because the Linux CI check startup_gfx2hlsl_bfm_b64 failed there; c7 fixed that test. While re-gating I also confirmed the open review thread (a 64-bit copy of plain data cleared the mask tag, so its truncated low word could be read as a number) and fixed it in 745d2c3 with a check that fails on the previous head; thread replied and resolved.

Re-gate on bf13897: fresh_gate GO (CI green, no open threads). Break-it sweep of 141 generated variants plus 173 corpus shapes: no FAIL, no unproven gain. The oracle result of the earlier run (32 translated, 32 MATCH, 0 MISMATCH, 346 refused) is unchanged for the translated HLSL. Sweep of all tests/startup_gfx2hlsl_*_check.py with no arguments: only the two draw-only checks that need adapter arguments exit 1.

Win2025 (dev + release) on the exact head: x2w_pipe.py win2025 2702 --host aws-1 --no-skip-build --py-checks tests/startup_gfx2hlsl_compute_check.py --targets offscreen_check --regex "startup_gfx2hlsl_(compute|mask|lane|vcc|cndmask|join|readfirstlane|cmp)": 56/56 dev, 56/56 release.

Open note, not new in this PR: a mask copy on one branch arm with a plain overwrite on the other, read as a number after the join, is not refused (the tag follows program order); llc does not emit it for an i1 value.

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 marked this pull request as draft November 26, 2026 05:11

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ee0deadadc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/startup/gfx2hlsl.py Resolved
scalar() lowers every b64 move as a mask copy (lane bit only), so clearing the tag on a pair copy of plain data let its truncated low word be read as a number (v_mov of s20 after s_mov_b64 s[20:21], s[4:5] stored 1, not the root constant). Only a 32-bit copy of plain data clears the tag now.
@tedin7
tedin7 marked this pull request as ready for review November 26, 2026 05:37

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bf138972a5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/startup/gfx2hlsl.py Resolved
tedin7 and others added 4 commits November 26, 2026 06:49

@MasterDomino MasterDomino 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.

Reviewed 7d3472d8. It fixes the WQM thread, and its merge with current main (e3cf6a91) passes the gfx2hlsl group here: 287/289 on WARP and an AMD Radeon RX 9070 XT (driver 32.0.32029.2010, Windows 11 build 26300). The two failures are already on main:

  • fma_mix_dispatch_hardware (#2746);
  • f16_alu_draw_hardware, where -0 products read back as +0.

Seven one-line mutations of the new tag and refusal logic each fail startup_gfx2hlsl_compute_check.py: the vector and scalar numeric checks off, no derived-word tag, no compare-pair high tag, the high tag not cleared on overwrite, the WQM fix reverted, and "exact" widened to 64 threads. Legitimate mask uses emit identical HLSL before and after: v_cndmask with VCC or an SGPR, s_and / s_or / s_andn2 on masks, saveexec, and carry-in.

One change is needed: a cache revision bump. main translates programs that this PR now refuses.

  • Examples: in a 64-thread compute group, v_cmp_lt_i32 vcc_lo, 5, v0 followed by any of v_cvt_f32_u32 v5, vcc_lo, s_add_u32 s6, vcc_lo, 1, or s_and_b32 s6, vcc_lo, 0xff; v_add_nc_u32 v5, s6, v5 translates on main and is refused here. About 15 more stage-level forms flip the same way.
  • Why it matters: their cached HLSL is wrong and would still be loaded under the same key.
  • What to change: advance x2w-startup-shader-19 / 19cs to 20 / 20cs, or one past whatever lands first; #2006 just took 20, so check main. That's pipeline_key / compute_key, d3d12_draw.inl, and the tests and docs that spell the revision.

Non-blocking, and also wrong on main:

  • Complemented masks: in a one-thread group, s_not_b32 s7, s6; s_add_i32 s1, s7, 1 is lowered as (~s6) & 1, while hardware holds 0xFFFFFFFE or 0xFFFFFFFF. The high word of s_not_b64 s[6:7], exec is modelled as 0 but is 0xFFFFFFFF. So the exactness argument holds only for masks that aren't complemented.
  • Carry forms: a mask as a data source of a _co_ form (v_add_co_u32_e64 v1, s8, s6, v2) is still accepted.
  • s_cselect_b32: a mask through it doesn't carry the tag; only the b64 form does.

Commands: ctest --preset dev --no-tests=error -j 8 -R '^(startup_gfx2hlsl|startup_compute_readfirstlane)' on the PR head and on its merge with main; python -B tests/startup_gfx2hlsl_{compute,mask_overwrite,readfirstlane_uniform,readfirstlane_recompute,shift64,bfm_b64,vcc_hi}_check.py.

AI-assisted; commands listed were executed locally.

c1 added 3 commits November 26, 2026 08:49
R0Y00#2702 now refuses programs that main translates (a lane mask read as a number), so translations cached under the old revision
would be reused with wrong HLSL. Runtime, offline keys, tests and docs spell the new revision; the pinned known-answer keys
are recomputed with the independent hash loops the tests already carry.
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the review and the mutation runs.

Cache revision: done. The startup shader cache revision advances from 22 / 22cs to 23 / 23cs: pipeline_key and compute_key in tools/startup/gfx2hlsl.py, revision / compute_revision in src/startup_stubs/d3d12_draw.inl, the two runtime tests that spell it (startup_indirect_draw_indexed.cpp, startup_resource_retire_title.cpp), docs/startup-runtime.md, and the Python checks that pin it. The two known-answer keys (startup_shader_cache_key_check.py, startup_compute_request_offline_check.py) are recomputed with the independent hash loops those tests already carry. Main moved to revision 22 while this was open, so this is one past it.

Also in this push: main merged again, and two controls that main added (#2769) now meet the lane-mask rule before the uniformity rule (predicate as OR source accepts either refusal).

Your non-blocking notes, left for a follow-up (all three are wrong on main today and none is made worse here; they widen the rule rather than fix this PR's):

  • complemented masks (s_not_b32 / s_not_b64 s[6:7], exec): the tag survives a complement, but the value modelled for it is not the hardware's;
  • a mask as a data source of a _co_ carry form (v_add_co_u32_e64 v1, s8, s6, v2);
  • s_cselect_b32 carrying the mask tag like the b64 form.

I can take them as a separate PR with their own WARP readback if you want them.

Commands run on the new head: the full Linux Python check list from the workflow (same 15 environment failures as origin/main, nothing new); python -B tests/startup_gfx2hlsl_{compute,mask_overwrite,readfirstlane_uniform,readfirstlane_recompute,shift64,bfm_b64}_check.py, startup_shader_cache_key_check.py, startup_compute_request_offline_check.py; the mask-tag fuzz variants over four llc seeds: 0 holes.

AI-assisted; commands listed were executed locally.

@MasterDomino MasterDomino 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.

Approving fcfd9f1c. The cache revision moves to x2w-startup-shader-23 / 23cs, one past current main (22).

Since 7d3472d8, which I reviewed with mutants:

  • Translator: apart from the revision line, the PR's own gfx2hlsl.py diff is identical.
  • Tests: the only test change I checked for semantics is narrow. predicate as OR source also accepts the mask-rule refusal (v_and_or_b32 reads lane mask s2 as numeric data), so the case stays refused under either rule.
  • Merge and CI: it merges cleanly onto main 624f5927, and CI is green at the head.

Commands, on Windows 11 build 26300, at fcfd9f1c; all exit 0:

  • python -B tests/startup_gfx2hlsl_{compute,mask_overwrite,readfirstlane_uniform,readfirstlane_recompute,readfirstlane_load_recompute,shift64,bfm_b64,vcc_hi,mask_data}_check.py;
  • startup_shader_cache_rekey_check.py, startup_shader_cache_key_check.py, startup_compute_request_offline_check.py;
  • startup_gfx2hlsl_compute_check.py binary <llvm-mc 15>: "fixture words match llvm-mc".

@tedin7, yes please: a separate PR for the three follow-ups (complemented masks, _co_ carry data sources, s_cselect_b32), each with its readback, would be welcome.

AI-assisted; commands listed were executed locally.

@MasterDomino
MasterDomino merged commit 83dcc92 into R0Y00:main Nov 26, 2026
5 checks passed
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.

2 participants