Repository navigation
gfx2hlsl: prove a thread-id shift uniform when it clears every thread-id bit - #2455
Conversation
v_readfirstlane_b32 needs a vector source with the same value in every active lane. A buffer_load_dword or buffer_load_format_x in a compute program reads a read-only descriptor (a descriptor used for a load and a store is already refused), so with a uniform VGPR index or offset, a uniform scalar offset and exec known in the entry block, every lane reads the same dword. Mark the destination uniform there. The value has no recomputable expression, so reads past the entry block still refuse. Tests: positive cases plus negative controls for a varying VGPR offset, a varying scalar offset, a graphics stage, no stage kind, unknown exec, a read after the entry block and a varying overwrite. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…-id bit In a compute entry block, v_lshrrev_b32 by n of the X thread id (v0, each local id has its own VGPR) is 0 in every lane when the group's X size is at most 1 << n. The same holds for (uniform << K) + v0 built by v_lshl_add_u32 with n <= K and size <= 1 << K: the low K bits of the base are clear, the sum does not carry, and the shifted value is the uniform part. The nearest earlier writer is the definition (program order is execution order in the entry block), v0 must be unwritten at it and the scalar unwritten after it. The value records a recomputable expression, so a read past the entry block is accepted too, until the scalar changes. Everything else keeps refusing by name: a larger group, a shift larger than the scale, a varying or rewritten scalar, a vector base, a rewritten v0, another stage and any modifier. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ten mutations: six caught, four survive — two of them provably redundant, two genuinely unexercisedBaseline:
Two of the survivors are redundant, and I proved that rather than assuming itThe The two that are genuinely unexercised
What the caught rows say about the proofThe interesting one is that dropping AI-assisted; the ten mutations were run locally on Windows 11 build 26300.9974. |
tedin7
left a comment
There was a problem hiding this comment.
Review of head c30127d (comment only, no Windows run on my side; the PR body lists no Windows results, so nothing here covers any SHA on Windows).
Checked against origin/main:
Stage.tid_shift/vgpr_writerintools/startup/gfx2hlsl.py: the bound logic is sound. The X thread id is below the group X size (v0 holds SV_GroupThreadID per the compute prologue note near line 1153), so>> nwith size <= 1 << n clears it. For(sK << K) + v0the low K bits of the base are clear, so there is no carry. The nearest-writer search, the v0-unwritten check at the definition, and the scalar-unwritten check between definition and read all look right. Gating (compute, entry block, exec known, no modifiers) matches the sibling rules. I found no correctness problem.- Scope is small and additive: 2 files, no launcher/CI/hook changes. No other open or merged PR covers the same rule.
- Dependency: stacked on #2446, which is still OPEN. It should not merge before #2446; the diff against main includes that commit.
Commands run locally:
python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py-> 0 failedgit diff --check origin/main...HEAD-> cleanpython3 tests/docs_links_check.pyandpython3 tests/bidi_controls_check.py-> OK- Mutation 1: allow size <= 1 << (n+1) -> test fails (3 failed).
- Mutation 2: drop the
n <= Kcheck -> test fails (1 failed). - Mutation 3: drop the "v0 unwritten before the definition" check -> test fails (1 failed).
All three mutations are caught, so the tests guard the new rule. No blocking findings.
AI-assisted; commands listed were executed locally.
# Conflicts: # tests/startup_gfx2hlsl_readfirstlane_uniform_check.py
tedin7
left a comment
There was a problem hiding this comment.
Review of head c30127d (comment only). Not mergeable yet: the body says this branch is stacked on #2446, which is still open.
Code (tools/startup/gfx2hlsl.py, Stage.tid_shift and its call site):
- The rule is sound as written. A right shift by n of the X thread id is 0 when the group X size is at most
1 << n. For(sK << K) + v0with n <= K and size <=1 << K, the low K bits of the base are clear, so the sum does not carry and the shift givessK << (K - n)in every lane. - The guards cover what I could think of: v0 must be unwritten before the definition, the scalar must not be written between definition and read (including the 'any' destination case), no modifiers, compute entry block only, known exec.
Tests (tests/startup_gfx2hlsl_readfirstlane_uniform_check.py), run on the exact head:
python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: 0 failed.- Mutation 1, dropping the
size > 1 << nguard: 4 failed. - Mutation 2, dropping the
n > Kguard: 1 failed. git diff --check HEAD~1 HEADandtests/bidi_controls_check.py: clean.
Notes:
- This change only widens a uniformity proof. It emits no new lowering, so I did not ask for a D3D12 readback test.
- The proof depends on the group X size from compute state block A, which
compute_entrydocuments as an unconfirmed hypothesis. If that field is wrong, the proof is wrong with it, and the "88 first refusals" figure is a translator statistic, not an executable result. No game result is claimed here, and I cannot check one. - The branch is far behind main, so it needs an update, and the diff should shrink to one commit once #2446 lands. I will not merge until then.
AI-assisted; commands listed were executed locally.
|
Merged current main into this branch (the stacked uniform-load commit landed as a squash, so the old commit conflicted in Commands executed:
AI-assisted; commands listed were executed locally. |
|
Merged current main into this branch (no conflicts, Commands executed:
AI-assisted; commands listed were executed locally. |
…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.
… forms in gfx2hlsl (Part of #1723) (#2702) * startup: refuse a vector ALU form that reads a lane mask as numeric data in gfx2hlsl (cherry picked from commit 6a91c26539918a6906104084ac638b05d8a267bb) * startup: refuse a move of the high word of a wave64 lane mask in gfx2hlsl (cherry picked from commit 72945939f8e39c9948aa26fbbf1760f21832ba3c) * startup: keep the select, readlane and writelane forms of a lane-mask read allowed, SDWA spellings included * startup: refuse a scalar ALU op that reads a lane mask as numeric data 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) * startup: refuse a lane-mask word derived by a bitwise op when a vector compare reads it as a number * startup: refuse every numeric read of the high word of a wave64 lane 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. * tests: name the earlier lane-mask refusal in the thread id base from a varying scalar case After merging main (#2455) that case meets the numeric-data refusal of v_lshl_add_u32 before the readfirstlane refusal; the expected text names it. * startup: carry the lane-mask tag through every scalar copy and combination 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. * startup: keep a numeric lane-mask read lowered in a one-thread compute 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. * fix: lane-mask high word tag stays a high word, ends on overwrite, resets 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. * fix: tag the high word of a wave64 compare or carry-out pair 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. * fix: keep the high-word tag off a neighbouring wave32 mask and off a 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. * fix: a copy of plain data clears the lane-mask tag of its destination 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. * test: keep the bfm_b64 uniformity check independent of the wave64 mask-word rule * fix: a 64-bit copy of plain data keeps the lane-mask tag 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. * test: the lane-mask-in-chain readfirstlane control now meets the mask rule first * fix: s_wqm keeps the lane-mask tag of its destination 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. * chore: advance the startup shader cache revision to 23 / 23cs #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. * test: a lane-mask OR source meets the mask rule before the uniformity rule --------- Co-authored-by: tedin7 <tedin7@users.noreply.github.com> Co-authored-by: c1 <c1@x.invalid>
Depends on #2446: this branch is stacked on it, so the diff shows that commit too until it merges; this pull request then holds one commit. Refs #2274.
What / Why
Compute programs compute the group index as
v_lshl_add_u32 v0, sG, 6, v0(global id = group * 64 + local id) followed byv_lshrrev_b32 v1, 6, v0andv_readfirstlane_b32 s, v1. The shifted value is the group index, the same in every lane, but the proof had no rule for a thread-id source.Change (stacked on the uniform-load branch; independent of it)
Stage.tid_shiftprovesv_lshrrev_b32 vD, n, vSuniform in a compute entry block when:pipe.group[0]) is at most1 << n, andv_lshl_add_u32 vS, sK, K, v0with n <= K, size <=1 << K, v0 unwritten before that definition, sK uniform and not written between definition and read.The result is
(sK << K) >> n(a recompute expression, so a read past the entry block is accepted until sK changes). The low K bits ofsK << Kare clear and the thread id is below1 << K, so the sum does not carry. Every other form (larger group, shift larger than the scale, varying or rewritten scalar, vector base, rewritten or swapped v0, modifiers, other stages) stays refused.Tests
python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: 0 failed (positives: group sizes 1, 33, 64, global id shifted back, recompute past the entry block; 20 negative controls incl. group 65/128, no recorded size, shift larger than scale, varying/rewritten scalar, vector base, rewritten v0, swap, load into v0).tests/startup_gfx2hlsl_*.pyandtests/startup_translator_check.py: exit 0. No LLVM-dependent check run.Measured (256 compute requests)
First refusal
v_readfirstlane_b32 requires a proven uniform source: 241 baseline, 115 with the uniform-load rule, 88 with this commit on top of it (27 move on; next first refusals: s_load register offset through the resource heap 15, src_scc in s_mov_b32 13, buffer store without offen form 12, src_scc in s_add_u32 10).Limits
The group size comes from compute state block A, which
compute_entrydocuments as a hypothesis from a local observation, not a decoded field. The proof is as good as that field (the same value sizesnumthreads). Wave size is not needed: the value does not depend on how lanes are grouped into waves. A group whose X size is a multiple of 64 (a wave index) is not proven.🤖 Generated with Claude Code