Repository navigation
startup: refuse a lane mask read as numeric data by the remaining ALU forms in gfx2hlsl (Part of #1723) - #2702
Conversation
…ata in gfx2hlsl (cherry picked from commit 6a91c26539918a6906104084ac638b05d8a267bb)
…hlsl (cherry picked from commit 72945939f8e39c9948aa26fbbf1760f21832ba3c)
… read allowed, SDWA spellings included
…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)
…r compare reads it as a number
…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.
|
@MasterDomino this is ready for review at head Independent gate review plus Windows run, four rounds. What it caught and the author fixed, so you know what was checked:
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 AI-assisted; commands listed were executed locally. |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
…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.
|
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.
AI-assisted; commands listed were executed locally. |
|
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.
|
@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 Win2025 (dev + release) on the exact head: 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. |
There was a problem hiding this comment.
💡 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".
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.
There was a problem hiding this comment.
💡 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".
s_wqm is a no-op in the lowering, so the plain-data cleanup must not clear the tag of a mask it nominally rewrites.
MasterDomino
left a comment
There was a problem hiding this comment.
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, v0followed by any ofv_cvt_f32_u32 v5, vcc_lo,s_add_u32 s6, vcc_lo, 1, ors_and_b32 s6, vcc_lo, 0xff; v_add_nc_u32 v5, s6, v5translates onmainand 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/19csto20/20cs, or one past whatever lands first; #2006 just took20, so checkmain. That'spipeline_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, 1is lowered as(~s6) & 1, while hardware holds 0xFFFFFFFE or 0xFFFFFFFF. The high word ofs_not_b64 s[6:7], execis 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.
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.
|
Thanks for the review and the mutation runs. Cache revision: done. The startup shader cache revision advances from 22 / 22cs to 23 / 23cs: Also in this push: main merged again, and two controls that main added (#2769) now meet the lane-mask rule before the uniformity rule ( Your non-blocking notes, left for a follow-up (all three are wrong on
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 AI-assisted; commands listed were executed locally. |
MasterDomino
left a comment
There was a problem hiding this comment.
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.pydiff is identical. - Tests: the only test change I checked for semantics is narrow.
predicate as OR sourcealso 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
main624f5927, 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.
Part of #1723
What this unblocks: stops silently wrong shader output when a wave lane mask is read as a number (the
llvm.amdgcn.ballotword and anything derived from it).What changed
gfx2hlsl.pykeeps 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 coversv_mov_b32andv_cndmask_b32data sources; this PR covers the rest: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.mov/and/or/xor/andn2/orn2/nand/nor/xnor/not/cselect/wqm, the*saveexecforms, branches,ff1,bitset) that reads a mask is refused the same way (this commit is the scalar half).s_xor_b64 s[0:1], vcc, s[0:1]thenv_mov_b32 v0, s1) is modelled as 0 but is the other lanes' bits on hardware: av_mov_b32of it is refused.v_shift64withvcc_loorexec_loas the shift count is no longer accepted (a mask as a count).Why it is correct
The hardware rule: a mask SGPR/
vcc/execholds 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 ofv_cndmask, the carry ofv_addc/v_subb/*_co,v_readlane/v_writelane, bitwise mask algebra andsaveexec. Those stay allowed.What stays refused / translating
Mask-only uses (select, carry, bitwise mask algebra,
saveexec, branches onvcc/scc) translate as before.v_mov_b32of a non-mask SGPR translates as before.Tests
tests/startup_gfx2hlsl_compute_check.py:check_lane_mask_data(vector ALU readingvcc_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, 0xffthen a vector compare or add readings6is 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: PASStests/startup_gfx2hlsl_*_check.pyrun with no arguments on this branch and onorigin/main: identical result per script (105 PASS;export_null_drawandmul24_align_drawneed a compiler and renderer and exit 1 on both).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 (includesstartup_gfx2hlsl_compute_warpand_hardware). Build and run:cmake --build --preset dev --target offscreen_checkthenctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_(compute|mask|lane|vcc|cndmask|join|readfirstlane|cmp)' -V.tests/startup_gfx2hlsl_*scripts give the same result on the merge of this branch with currentmainas onmain(onlyexport_null_drawandmul24_align_draw, which need a compiler and renderer, exit 1 on both). Every case ofCASESinstartup_gfx2hlsl_compute_check.pytranslates (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 oflane_mask_exact(always false, always true, at most two threads) and of the tag propagation each makestartup_gfx2hlsl_compute_check.pyfail.AI-assisted; commands listed were executed locally.