Repository navigation
gfx2hlsl: prove a compute buffer load with a uniform address uniform for v_readfirstlane_b32 - #2446
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>
Eight conditions dropped one at a time: two are equivalent, four I could not exercise, and that is the findingBaseline on this branch:
Two of the six survivors I can show are equivalent:
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 Identical in all three, so my inputs are not reaching the part of the state the conditions test — Two cases that would reach them, if you agree they are worth pinning:
The whitelist and What is well pinnedBoth AI-assisted; the eight mutations and the three-way comparison were run locally on Windows 11 build 26300.9974. |
tedin7
left a comment
There was a problem hiding this comment.
Review of head 8a69d81c. No blocking findings. Two non-blocking notes.
What I checked:
- The new rule in
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. - It relies on the existing refusal at lines 5353/5358 (a descriptor used for both a load and a store). I confirmed both checks exist.
- No
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"). - Overlap: #2309 (merged) keeps proofs across joins and loop counters. This PR is a different rule. #2454 builds on this one for multi-register loads. I found no duplicate.
- Scope is small and additive, with no changes to CI, the launcher or hooks.
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-> clean- Mutation 1: dropped the compute-stage condition. The suite reports 2 failed.
- Mutation 2: dropped the uniform-soffset condition. The suite reports 1 failed.
- Both mutations reverted afterwards.
Notes (non-blocking):
- The tests compare emitted HLSL text. There is no D3D12 WARP readback here. The change only affects the uniformity proof, not a math lowering, but a readback with a uniform load feeding
v_readfirstlane_b32would be a stronger guard. - The description states the assumption that a read-only SRV does not race a UAV bound to the same memory. The descriptor check cannot see that aliasing. Worth a comment next to the rule.
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.
Two open PRs, one containing the other — worth deciding before either merges
The two diffs against Mutations on the part that is specific to this PRBaseline:
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
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 itselfRead together with Worth noting for the reviewer of AI-assisted; the ancestor check, the merge attempt and the five mutations were run locally on Windows 11 build 26300.9974. |
tedin7
left a comment
There was a problem hiding this comment.
Review of head ceffc32 (main merged in; the PR's own diff is unchanged).
Result: not mergeable by me yet. Two things need a decision or a test.
- No D3D12 WARP readback test. This changes a shader lowering in
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. - Overlap with #2454. #2454 is built on top of this branch (already noted in the thread). Whichever lands first changes the other. This is a sequencing choice; I leave it to @R0Y00.
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 ceffc32:
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-> pass- Code reading at
gfx2hlsl.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.
Compute case N: v_readfirstlane_b32 of a buffer load at a uniform address, xored into each lane's own word, dispatched on D3D12 and compared with a CPU reference. A per-lane address is refused. Control n-dropped-broadcast must mismatch.
|
Pushed d9fe245 to this branch (fast-forward, maintainer edit): the missing D3D12 readback for the uniform-load case. What it adds (
Verification:
AI-assisted; commands listed were executed locally. |
# Conflicts: # tests/startup_gfx2hlsl_compute_check.py
|
Merged current
Squash merge is queued for when this head's CI is green. AI-assisted; commands listed were executed locally. |
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