Skip to content

startup: read a uniform compute buffer load past the entry block - #2607

Merged
R0Y00 merged 8 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-readfirstlane-join-uniform
Nov 26, 2026
Merged

R0Y00 merged 8 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-readfirstlane-join-uniform

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Depends on #2446, #2454

Refs #1723, #2274

What changes

v_readfirstlane_b32 of a VGPR loaded by a compute buffer load past the entry block was refused with
v_readfirstlane_b32 requires a proven uniform source. #2446/#2454 prove such a load uniform only in the entry block
with 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>, and
the read copies that temporary:

a = s3 + 0;
v7 = r0.Load(a + 0);
ulc_7 = r0.Load(a + 0);
s9 = ulc_7;
  • The load must satisfy the existing entry-block form: compute stage, a descriptor in UNIFORM_LOADS (read-only,
    never stored through), a uniform offset scalar and a uniform address.
  • The address is the uniform expression of the address VGPR (read before the load can overwrite its own address
    register), or the VGPR itself while exec is still the entry mask (every lane then ran its definition).
  • A formatted load reuses the stride/format selection the guarded write uses; only the final write is guarded, so
    every lane already computes the same component.
  • The temporary is recorded in the existing uniform_expr bookkeeping, so the read is accepted only under the existing
    past-entry rules: no branch target, exec write or overwrite of the loaded register between the load and the read.

Soundness

  • Same value in every active lane: every lane executes the extra unguarded load with the same descriptor and the same
    address, so all lanes hold the same words, independent of which lanes exec enabled.
  • Per-lane s_cbranch_execz and joins: a branch target clears the proof (existing rule), so a load inside a block one
    lane skips, or a join of two definitions, still refuses.
  • No back-edge shortcuts: a loop head between the load and the read clears the proof.
  • A later write of the offset scalar does not matter because the temporary already holds the words.
  • Assumption carried from gfx2hlsl: prove a compute buffer load with a uniform address uniform for v_readfirstlane_b32 #2446: a descriptor that is never stored through in the shader is not written by another
    lane or wave during the dispatch (no aliasing through another descriptor).

Tests

New tests/startup_gfx2hlsl_readfirstlane_load_recompute_check.py, registered in CMakeLists.txt, AGENTS.md and the
platform-independent Python job:

  • accepted, with exact HLSL: scalar offset load; offset scalar rewritten after the load; address from the expression
    under a narrowed exec (destination equal to the address register); address copy under the entry exec; second word
    of a two-dword load; a conversion of the loaded word; a formatted load; the temporary declaration;
  • refused: a join of a load and another definition; a per-lane address; a per-lane offset scalar; a back-edge target
    between the load and the read; a load inside a block one lane skips; exec narrowed 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;
  • negative control: with UNIFORM_LOADS emptied every accepted form refuses. Against the translator without this change
    the check reports 8 failures, each accepted form refused with the readfirstlane message.
python -B tests/startup_gfx2hlsl_readfirstlane_load_recompute_check.py   # Readfirstlane load recompute checks: 0 failed
python -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py        # 0 failed
python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py          # 0 failed
python -B tests/startup_compute_readfirstlane_check.py                   # passed (no llvm-mc run)
python -B tests/startup_translator_check.py                              # 0 failed
python -B tests/startup_gfx2hlsl_join_symbols_check.py                   # passed
python -B tests/startup_gfx2hlsl_loop_constants_check.py                 # PASS
python -B tests/startup_gfx2hlsl_compute_check.py                        # passed
python -B tests/docs_links_check.py                                      # 19 files, 495 links
python -B tests/bidi_controls_check.py                                   # no bidirectional controls
git show --check --format= HEAD                                          # clean

Every tests/startup_gfx2hlsl_*_check.py plus startup_buffer_address_check.py, startup_smem_address_check.py and
startup_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:

before after
v_readfirstlane_b32 requires a proven uniform source 113 95
refusal records (all causes) 470 467
programs translated and compiled with FXC 0 3

All 18 requests that changed were readfirstlane refusals before; no other request changed its first refusal. Where the
18 stop now:

next first refusal requests
translated fully (HLSL and FXC object written) 3
LDS exchange needs straight-line code and entry EXEC 6
LDS exchange needs a 1D group and a nonzero allocation of at most 32 KiB 2
s_load_dwordx8 inside a block s_cbranch_execz skips per lane (scc live at the join) 2
s_buffer_load_dwordx16 inside a block s_cbranch_execz skips per lane 1
LDS fill needs the unchanged entry local X ID 1
ds_bpermute_b32 1
buffer_store_format_xyzw 1
operand src_scc in s_mov_b32 1

An 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_b32 with another root. The
remaining 95, by the census's root classification (not addressed here):

root of the read source requests
several definitions reach across a loop back edge (waterfall-style loops) 33
entry-block formatted load under unknown exec 18
value operation outside the uniform allowlist (v_fmac, v_rndne, v_ceil, v_add, v_sub, v_cvt_f32_i32) 10
image_load source 9
thread-id chains 8
proven at the definition, no recompute expression past the entry block 8
definition before a join 3
two definitions at a join 2
v_cndmask_b32 from vcc 2
request unreadable by the census 2

Not verified

  • No GPU or WARP readback of a translated program using the temporary; the result is checked as emitted HLSL only.
  • The read-only, non-aliased descriptor assumption above.
  • The temporary is written for every qualifying load past the entry block, whether or not a v_readfirstlane_b32
    reads 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.
  • The remaining v_readfirstlane_b32 refusals need other proofs (waterfall loops, entry-block reads under unknown
    exec, thread-id chains, exec-masked joins); this change does not address them.
  • The 320 refusals of a vcc definition live past a per-lane s_cbranch_execz join hide later refusals in those
    programs; what they reach after that is not measured here.

🤖 Generated with Claude Code

NiiRoZz and others added 6 commits November 25, 2026 17:58
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 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_temps init): 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.

@R0Y00

R0Y00 commented Nov 26, 2026

Copy link
Copy Markdown
Owner

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.

AMD RDNA 2 ISA, V_READFIRSTLANE_B32

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@R0Y00
R0Y00 merged commit 1c061f8 into R0Y00:main Nov 26, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants