gfx2hlsl: prove a compute buffer load with a uniform address uniform for v_readfirstlane_b32 #2438
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2446"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T17:00:28Z.
What / Why
v_readfirstlane_b32is only expressible in the per-lane model when the vector source has the same value in every active lane. The proof (uniform_v) covered scalar-derived ALU results but never a loaded value, so the common compiler pattern "load a count/index from a buffer at a uniform address, then readfirstlane it into an SGPR" was refused.Change
Stage.onemarks the destination ofbuffer_load_dword/buffer_load_format_xuniform when all hold:off, or one VGPR (idxen or offen, not both) already inuniform_v, plus a uniform scalar/immediate soffset;Why sound: compute descriptors are symbolic table descriptors (uniform), and a descriptor used for a load and a store is already refused, so the buffer is read-only in the dispatch; same address means same dword in every lane. No
uniform_expris recorded (the emitted expression names scratcha/c0), so reads past the entry block still refuse. Assumption: a read-only SRV is not racing a UAV bound to the same memory.Tests
python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: 0 failed (3 positive, 7 negative controls + disabled-allowlist control added).tests/startup_gfx2hlsl_*_check.pyplustests/startup_translator_check.py: exit 0.startup_gfx2hlsl_lane_alu.pyalso ran, exit 0. Nothing skipped that needs LLVM.Measured (256 compute requests whose first refusal was this one)
Before: 241 first refusals
v_readfirstlane_b32 requires a proven uniform source.After: 115. 126 requests moved on; next first refusals: s_pack_ll_b32 41, v_mul_hi_u32 34, buffer store without offen form 12, src_scc in s_add_u32 10, formatted buffer load descriptor words not proven in entry block 9, scc from descriptor data 8, src_scc in s_mov_b32 7, ds_write 6, compute image_store 5, others 1-2.
Limits
Remaining 115 are mostly: loads past the entry block or with unknown exec (18),
v_lshrrev_b32 v, 6, v0wave index (31, needs guest wave size and group geometry), loads with no uniform_v proof for the address, image_load, v_cndmask, per-lane ALU. Not translated: none of the new passes proves anything about full-game execution.Relation to #2274
Refs #2274 (Part A,
v_readfirstlaneproof). That item is claimed for the loop-counter and one-path-register cases (proofs kept across joins). This change is a different rule (a loaded value in the entry block) and leaves joins alone, so the two do not overlap.🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T17:09:15Z.
Eight conditions dropped one at a time: two are equivalent, four I could not exercise, and that is the finding
Baseline on this branch:
Readfirstlane uniformity checks: 0 failed, exit 0.loadedconjunctgetattr(self, 'kind', None) == 'cs'self.uniform_source(ops[3])(soffset must be uniform)not getattr(self, 'past_entry', True)(entry block only)getattr(self, 'exec_known', False)self.tracks_uniformity()not ('idxen' in mods and 'offen' in mods)offset:/glc/slc/dlc/idxen/offenmodifier whitelistdst is None(do not claim a load that already has a destination)Two of the six survivors I can show are equivalent:
tracks_uniformity()—uniform_source()readsself.uniform_v/self.varying_sdirectly, so a Stage without those raisesAttributeErrorrather than returning False; the condition is a readable guard, not a behaviour.idxen and offen— the next clause requires(ops[1] == 'off') == (idxen not in mods and offen not in mods), which foridxen+offendemandsops[1] == 'off'; no encoding produces a MUBUF load with no vaddr, so the case cannot arise.The other four I could not distinguish from equivalent, and I would rather say that than call them gaps. What I tried, on the branch and then with each condition removed, translating
v_mov_b32_e32 v7, s2+v_readfirstlane_b32 s9, v7throughStage.translate():Identical in all three, so my inputs are not reaching the part of the state the conditions test —
exec_known=Falseis refused by the other clause (tracks_uniformity/uniform_sourcepath), and my branch line is not a shape the branch scan accepts.Two cases that would reach them, if you agree they are worth pinning:
past_entry— a program whosebuffer_load_dwordsits after a label the branch scan records as a target (self.past_entry = bool(self.blocks) or 0 in targets), withexecstill known. The uniform claim should be refused there because the loaded dword is not recomputable from the entry block; with the condition dropped it is accepted.dst is None— a load whose destination register already has something pending, so the uniform path would overwrite a symbol that another expression still refers to. If no such program exists, the condition is documentation and the comment says so.The whitelist and
dst is Nonemay well be unreachable-by-construction like the two above; my point is only that the fixture currently demonstrates the accept path and the two refusals beside it, not these four conjuncts.What is well pinned
Both
caughtrows matter:kind == 'cs'is the difference between a compute load (one dword per lane, uniform address) and a pixel-stage load (where the address can vary per quad), and dropping the soffset uniformity check is the classic way to accept a load that is uniform only by accident. Your two controls (chain with an in-place write refused without tracking,cross-register chain refused without tracking) also show the mechanism can fail closed, which is the part I usually find missing.AI-assisted; the eight mutations and the three-way comparison were run locally on Windows 11 build 26300.9974.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T18:26:02Z.
Review: COMMENTED
Review of head
8a69d81c. No blocking findings. Two non-blocking notes.What I checked:
Stage.one(tools/startup/gfx2hlsl.py, around line 1946) only fires forbuffer_load_dword/buffer_load_format_xin a compute stage, in the entry block, with known exec. Every address operand must be uniform:off, or exactly one proven-uniform VGPR (idxen or offen, never both), plus a uniform soffset. Same address in every lane means the same dword in every lane, so the proof holds.uniform_expris recorded for the loaded value (or loadedat the pop), so a later read past the entry block still refuses. That is the safe choice, and the test covers it ("load after the entry block refused").Commands run locally (Linux, clean worktree):
python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py-> 0 failedpython3 tests/docs_links_check.py-> okpython3 tests/bidi_controls_check.py-> okgit diff --check origin/main...HEAD-> cleanNotes (non-blocking):
v_readfirstlane_b32would be a stronger guard.I did not run any Windows tests and I did not see author-posted Windows results for this head SHA. The PR body lists only Linux-style test runs. The branch is 24 commits behind main.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T18:49:38Z.
Two open PRs, one containing the other — worth deciding before either merges
#2454is built directly on top of this branch:The two diffs against
mainshare 15 of 18 and 27 added lines respectively. So if#2454merges first, this PR becomes an empty diff; if this one merges first,#2454needs a rebase. Not a defect in either — just a sequencing decision that is cheaper to make now than to discover at merge time.Mutations on the part that is specific to this PR
Baseline:
Readfirstlane uniformity checks: 0 failed, exit 0 (and the fixture prints acontrol: cross-register chain refused without tracking: okline).loadedbypass dropped at the uniformity site (expr is None or loaded or any(...))if loaded:→if True:(everybuffer_loadresult treated as uniform)UNIFORM_LOADSwidened to includebuffer_load_dwordx2not past_entry)kind == 'cs')The second row is the one that matters most: the whole change is one boolean that says "a load through a read-only descriptor with a uniform address reads the same dword in every lane", and forcing it true is caught. So the fixture really does exercise the non-uniform path rather than only the accepted case.
The two survivors
UNIFORM_LOADSwidened: the fixture only buildsbuffer_load_dword(andbuffer_load_format_x), so adding a third element changes nothing observable. That is the same shape as#2454's modifier-whitelist survivor — a whitelist that is asserted only from the "what is accepted" side. A one-case fixture (buffer_load_dwordx2at the same site must be refused) would pin the boundary.past_entrynever becomes true for the fixture programs, sonot past_entryis a constant there. To pin it a program would have to reach a load after a branch join, and then the load must not be treated as uniform. That is a harder fixture, and I would not block on it — but it is the condition that makes the proof "entry block only" rather than "anywhere", which is the claim in the comment.I do not call either survivor a defect: neither changes behaviour for any input the fixture builds, and I could not construct a case where the mutation matters without also needing the deeper fixture above.
The condition itself
Read together with
#2454, the full gate is: compute stage, entry block, known EXEC, uniform-address, at most one ofidxen/offen, an allowed modifier set,len(ops) == 4, and a single-register destination. That is a lot of conjuncts for one expression, and each one is a way to accept a load that is not uniform — which is exactly the failure this PR is guarding against, because a wrongly-uniform load becomes areadfirstlanethat silently broadcasts one lane's value.Worth noting for the reviewer of
#2454: I found there that removinglen(ops) == 4turns a short-operandbuffer_load_dwordinto anIndexErrorinstead of the existing refusal. The same conjunction is present here, so the same pin would apply if this one merges first.AI-assisted; the ancestor check, the merge attempt and the five mutations were run locally on Windows 11 build 26300.9974.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T20:12:32Z.
Review: COMMENTED
Review of head
ceffc32a(main merged in; the PR's own diff is unchanged).Result: not mergeable by me yet. Two things need a decision or a test.
tools/startup/gfx2hlsl.py(whatv_readfirstlane_b32may read from). The new test,tests/startup_gfx2hlsl_readfirstlane_uniform_check.py, only checks accept/refuse decisions in Python. The soundness claim ("same address means same dword in every lane, so lane 0 is right") is a GPU-behavior claim. Please add a draw/dispatch where lanes would disagree if the proof were wrong, and compare GPU readback with a CPU reference, as the other lowering tests do.Also: the "Measured (256 compute requests ...)" numbers come from running the title's data, which I cannot reproduce. I reviewed code and fixtures only and make no claim about those numbers. Merge stays with @R0Y00 or MasterDomino.
Checked at
ceffc32a:python3 -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py-> 0 failedpython3 tests/docs_links_check.py,python3 tests/bidi_controls_check.py,git diff --check origin/main...HEAD-> passgfx2hlsl.py:2351-2385: the bypass is limited to compute, entry block, known EXEC, uniform address operands;UNIFORM_LOADSis onlybuffer_load_dword/buffer_load_format_x. The logic looks consistent with the stated assumption (a read-only SRV is not racing a UAV on the same memory); that assumption is worth a line in the docs.No Win2025 run was done because of the missing GPU test.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T02:00:48Z.
Pushed
d9fe245fdto this branch (fast-forward, maintainer edit): the missing D3D12 readback for the uniform-load case.What it adds (
tests/startup_gfx2hlsl_compute_check.py, plus one control name inCMakeLists.txt):v_readfirstlane_b32of abuffer_load_dwordat a uniform address, xored into each lane's own read-buffer word. Translated, dispatched on D3D12 and compared word by word with a CPU reference.v_readfirstlane_b32 requires a proven uniform source.n-dropped-broadcast: the same dispatch with the xor removed must mismatch.Verification:
python3 tests/startup_gfx2hlsl_compute_check.py,... binary llvm-mc-19(words match the assembler),python3 tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: pass.x2w_pipe.py win2025 2446 --patch <diff> --regex 'startup_gfx2hlsl_compute_(warp|n-dropped-broadcast_warp|controls)|startup_gfx2hlsl_readfirstlane_uniform': 4/4 passed.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T02:08:28Z.
Merged current
maininto this branch (merge commite88e79b92, no rebase, no force push) because the PR had gone into conflict.tests/startup_gfx2hlsl_compute_check.py, wheremainadded theaddress-carrycase next to this PR's caseN. Both blocks are kept as they were.mainis unchanged:CMakeLists.txt,tools/startup/gfx2hlsl.py, and the two test files.x2w_pipe.py win2025 2446 --patch <diff> --regex 'startup_gfx2hlsl_compute_(warp|n-dropped-broadcast_warp|controls)|startup_gfx2hlsl_readfirstlane_uniform': 4/4 passed.e88e79b92:python3 tests/startup_gfx2hlsl_compute_check.pyok,python3 tests/startup_gfx2hlsl_readfirstlane_uniform_check.pyok (0 failed),python3 tests/docs_links_check.pyok,python3 tests/bidi_controls_check.pyok,git diff --checkclean.Squash merge is queued for when this head's CI is green.
AI-assisted; commands listed were executed locally.