Repository navigation
startup: read a uniform compute buffer load past the entry block - #2607
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>
The entry-block rule for a compute buffer load with a uniform address covered one destination register. Accept buffer_load_dwordx2/x3/x4 and buffer_load_format_xy/xyz/xyzw too: every destination register of such a load holds the same dword in every lane. The destination range must be exactly as long as the opcode writes, so no register the load leaves alone is marked uniform. None of them records a recomputable expression. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Past the entry block a lane the load skipped keeps an older VGPR, so every lane loads the same words again into a function-scope temporary from an address every lane computes alike, and v_readfirstlane_b32 copies that temporary. Branch targets, EXEC writes and overwrites between the load and the read still refuse, as do per-lane addresses and graphics stages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tedin7
left a comment
There was a problem hiding this comment.
Review of the merge of main into this branch (head d758f92). The cascade #2446/#2454/#2455 has landed, so the dependency is met.
What I did: the branch conflicted in tools/startup/gfx2hlsl.py and tests/startup_gfx2hlsl_readfirstlane_uniform_check.py. I merged origin/main (no rebase, no force) and kept both sides: the per-lane temporary (uniform_load_temps, uniform_vaddr, uniform_temps) next to main's shifted/tid_shift and heap_index tracking. loaded became loaded_regs in the uniform_expr pop, which is the same condition as before.
Checked locally:
- Python checks for readfirstlane (uniform, recompute, load_recompute), heap_record, sgpr_spill, join_symbols: pass.
python3 tests/docs_links_check.py,python3 tests/bidi_controls_check.py,git diff --check origin/main...HEAD: clean.- Mutation (drop the
uniform_tempsinit): load_recompute check fails. - Windows Server 2025 dev + release build of the affected targets and the Windows Python checks (the merged tree applied as a patch on the old head): all ok.
Open point, not a blocker for the logic but for merge: the new lowering emits extra HLSL (ul<pc>_<reg> = r0.Load(...)), and the only test is a string/refusal check on the emitted text. For a shader lowering, please add a D3D12 WARP readback (synthetic words) that runs a program with a uniform load past a branch and compares against a CPU reference, ideally extending the sibling startup_gfx2hlsl_* WARP checks. Until then I am not merging this.
AI-assisted; commands listed were executed locally.
|
I reproduced an empty-EXEC case on d758f92: lane zero has v7=17, the masked load does not write v7, yet the translated scalar read uses the unguarded reload temporary. The AMD RDNA 2 ISA specifies lane zero for V_READFIRSTLANE_B32 when EXEC is empty. In 931af7f the new load proof requires entry EXEC, and both the explicit empty-mask and potentially empty-mask cases refuse. The prior full-EXEC cases and six adjacent checks pass. A separate path proof can later accept a narrowed mask when the wave is known nonempty. |
R0Y00
left a comment
There was a problem hiding this comment.
The patched host suite keeps the existing full-entry-EXEC uniform loads and refuses both explicit empty EXEC and a narrowed mask without an active-lane proof. The empty mask reproducer emitted an unguarded reload on the previous head. Readfirstlane uniformity and recomputation, descriptor joins, compute translation and adjacent controls passed.
Depends on #2446, #2454
Refs #1723, #2274
What changes
v_readfirstlane_b32of a VGPR loaded by a compute buffer load past the entry block was refused withv_readfirstlane_b32 requires a proven uniform source. #2446/#2454 prove such a load uniform only in the entry blockwith known
exec, where every lane ran it.Past the entry block, the per-lane model keeps an older VGPR value in lanes the load skipped, so copying the VGPR is
not sound. This change has every lane load the same words again into a function-scope temporary
ul<pc>_<reg>, andthe read copies that temporary:
UNIFORM_LOADS(read-only,never stored through), a uniform offset scalar and a uniform address.
register), or the VGPR itself while
execis still the entry mask (every lane then ran its definition).every lane already computes the same component.
uniform_exprbookkeeping, so the read is accepted only under the existingpast-entry rules: no branch target,
execwrite or overwrite of the loaded register between the load and the read.Soundness
address, so all lanes hold the same words, independent of which lanes
execenabled.s_cbranch_execzand joins: a branch target clears the proof (existing rule), so a load inside a block onelane skips, or a join of two definitions, still refuses.
lane or wave during the dispatch (no aliasing through another descriptor).
Tests
New
tests/startup_gfx2hlsl_readfirstlane_load_recompute_check.py, registered inCMakeLists.txt,AGENTS.mdand theplatform-independent Python job:
under a narrowed
exec(destination equal to the address register); address copy under the entryexec; second wordof a two-dword load; a conversion of the loaded word; a formatted load; the temporary declaration;
between the load and the read; a load inside a block one lane skips;
execnarrowed between the load and the read;a stale address copy under a narrowed
exec; the loaded register overwritten; a two-dword load into three registers;a graphics stage;
UNIFORM_LOADSemptied every accepted form refuses. Against the translator without this changethe check reports 8 failures, each accepted form refused with the readfirstlane message.
Every
tests/startup_gfx2hlsl_*_check.pyplusstartup_buffer_address_check.py,startup_smem_address_check.pyandstartup_stream_fetch_check.py(82 scripts) exit 0.Measured
A local set of 470 refused shader requests (458 compute, 9 graphics and 3 RTV variants), collected with #2446 and #2454 applied and translated offline before and after this change:
v_readfirstlane_b32 requires a proven uniform sourceAll 18 requests that changed were readfirstlane refusals before; no other request changed its first refusal. Where the
18 stop now:
s_load_dwordx8inside a blocks_cbranch_execzskips per lane (scc live at the join)s_buffer_load_dwordx16inside a blocks_cbranch_execzskips per laneds_bpermute_b32buffer_store_format_xyzwsrc_sccins_mov_b32An offline census of the first refused read per request classified 22 refusals as a past-entry buffer load before the
change and none after; 4 of those 22 requests now refuse at a later
v_readfirstlane_b32with another root. Theremaining 95, by the census's root classification (not addressed here):
execv_fmac,v_rndne,v_ceil,v_add,v_sub,v_cvt_f32_i32)image_loadsourcev_cndmask_b32fromvccNot verified
v_readfirstlane_b32reads it. Programs that already translate can therefore gain an unused load and declaration; FXC should remove them,
but the output of already-translating programs was not diffed before and after.
v_readfirstlane_b32refusals need other proofs (waterfall loops, entry-block reads under unknownexec, thread-id chains, exec-masked joins); this change does not address them.vccdefinition live past a per-lanes_cbranch_execzjoin hide later refusals in thoseprograms; what they reach after that is not measured here.
🤖 Generated with Claude Code