Skip to content

gfx2hlsl: prove a compute buffer load with a uniform address uniform for v_readfirstlane_b32 - #2446

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

tedin7 merged 4 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-readfirstlane-uniform

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

What / Why

v_readfirstlane_b32 is 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.one marks the destination of buffer_load_dword / buffer_load_format_x uniform when all hold:

  • compute stage; entry block (not past_entry); exec known;
  • address operands uniform: off, or one VGPR (idxen or offen, not both) already in uniform_v, plus a uniform scalar/immediate soffset;
  • only idxen/offen/glc/slc/dlc/offset modifiers (lds, tfe etc. stay refused by the lowering).
    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_expr is recorded (the emitted expression names scratch a/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).
  • All no-argument tests/startup_gfx2hlsl_*_check.py plus tests/startup_translator_check.py: exit 0. startup_gfx2hlsl_lane_alu.py also 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, v0 wave 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_readfirstlane proof). 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

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>
@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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.

dropped condition in the new loaded conjunct result
getattr(self, 'kind', None) == 'cs' caught
self.uniform_source(ops[3]) (soffset must be uniform) caught
not getattr(self, 'past_entry', True) (entry block only) survived
getattr(self, 'exec_known', False) survived
self.tracks_uniformity() survived
not ('idxen' in mods and 'offen' in mods) survived
the offset:/glc/slc/dlc/idxen/offen modifier whitelist survived
dst is None (do not claim a load that already has a destination) survived

Two of the six survivors I can show are equivalent:

  • tracks_uniformity() — uniform_source() reads self.uniform_v/self.varying_s directly, so a Stage without those raises AttributeError rather 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 for idxen+offen demands ops[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, v7 through Stage.translate():

                          baseline      past_entry dropped   exec_known dropped
base                      ok: s9 = v7   ok: s9 = v7          ok: s9 = v7
exec_known=False          refused       refused              refused
after s_cbranch_scc1      IndexError    IndexError           IndexError

Identical in all three, so my inputs are not reaching the part of the state the conditions test — exec_known=False is refused by the other clause (tracks_uniformity/uniform_source path), 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:

  1. past_entry — a program whose buffer_load_dword sits after a label the branch scan records as a target (self.past_entry = bool(self.blocks) or 0 in targets), with exec still 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.
  2. 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 None may 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 caught rows 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.

@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 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 for buffer_load_dword / buffer_load_format_x in 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_expr is recorded for the loaded value (or loaded at 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 failed
  • python3 tests/docs_links_check.py -> ok
  • python3 tests/bidi_controls_check.py -> ok
  • git 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_b32 would 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.

@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Two open PRs, one containing the other — worth deciding before either merges

#2454 is built directly on top of this branch:

git merge-base --is-ancestor pr-2446 pr-2454   -> True
git merge-base --is-ancestor pr-2454 pr-2446   -> False
git merge --no-commit --no-ff pr-2446 (from pr-2454) -> "Already up to date."

The two diffs against main share 15 of 18 and 27 added lines respectively. So if #2454 merges first, this PR becomes an empty diff; if this one merges first, #2454 needs 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 a control: cross-register chain refused without tracking: ok line).

mutation result
the loaded bypass dropped at the uniformity site (expr is None or loaded or any(...)) caught
if loaded: → if True: (every buffer_load result treated as uniform) caught
UNIFORM_LOADS widened to include buffer_load_dwordx2 survived
the entry-block requirement dropped (not past_entry) survived
the compute-stage requirement dropped (kind == 'cs') caught

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_LOADS widened: the fixture only builds buffer_load_dword (and buffer_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_dwordx2 at the same site must be refused) would pin the boundary.
  • entry-block requirement dropped: past_entry never becomes true for the fixture programs, so not past_entry is 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 of idxen/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 a readfirstlane that silently broadcasts one lane's value.

Worth noting for the reviewer of #2454: I found there that removing len(ops) == 4 turns a short-operand buffer_load_dword into an IndexError instead 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.

legacy-code-archaeologist added a commit to legacy-code-archaeologist/Xbox2Windows that referenced this pull request Nov 25, 2026

@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 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.

  1. No D3D12 WARP readback test. This changes a shader lowering in tools/startup/gfx2hlsl.py (what v_readfirstlane_b32 may 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.
  2. 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 failed
  • python3 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_LOADS is only buffer_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.
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Pushed d9fe245 to 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 in CMakeLists.txt):

  • Case N: v_readfirstlane_b32 of a buffer_load_dword at 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.
  • A variant whose load address is per-lane is refused with v_readfirstlane_b32 requires a proven uniform source.
  • Control n-dropped-broadcast: the same dispatch with the xor removed must mismatch.

Verification:

  • Linux: python3 tests/startup_gfx2hlsl_compute_check.py, ... binary llvm-mc-19 (words match the assembler), python3 tests/startup_gfx2hlsl_readfirstlane_uniform_check.py: pass.
  • Win2025 (dev + release): 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.

# Conflicts:
#	tests/startup_gfx2hlsl_compute_check.py
@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Merged current main into this branch (merge commit e88e79b92, no rebase, no force push) because the PR had gone into conflict.

  • Only conflict: tests/startup_gfx2hlsl_compute_check.py, where main added the address-carry case next to this PR's case N. Both blocks are kept as they were.
  • The PR's own diff against main is unchanged: CMakeLists.txt, tools/startup/gfx2hlsl.py, and the two test files.
  • Win2025 dev + release on the content (before this merge): x2w_pipe.py win2025 2446 --patch <diff> --regex 'startup_gfx2hlsl_compute_(warp|n-dropped-broadcast_warp|controls)|startup_gfx2hlsl_readfirstlane_uniform': 4/4 passed.
  • Linux, on e88e79b92: python3 tests/startup_gfx2hlsl_compute_check.py ok, python3 tests/startup_gfx2hlsl_readfirstlane_uniform_check.py ok (0 failed), python3 tests/docs_links_check.py ok, python3 tests/bidi_controls_check.py ok, git diff --check clean.

Squash merge is queued for when this head's CI is green.

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 enabled auto-merge (squash) November 26, 2026 02:08
@tedin7
tedin7 merged commit f213cf6 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