shader: recompute uniform compute loads under a proven nonempty EXEC #3692

Merged
Not-Saii merged 1 commit from upstream-pr/3692 into main 2026-12-01 03:13:16 +00:00
Not-Saii commented 2026-12-01 02:18:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Not-Saii on 2026-12-01T02:18:40Z.


Change

Refs #2644.

v_readfirstlane_b32 of a uniform compute buffer load past the entry block is lowered by reloading the words into a temporary in every lane (the recompute path). Until now that path required the entry EXEC mask at the load. On RDNA 2, READFIRSTLANE with an empty EXEC returns the old lane-zero VGPR, and an unguarded reload cannot reproduce that. The existing comment already allowed this "until the path proves a nonempty wave". This change adds that proof.

Stage.nonempty_exec_loads() is a forward must-dataflow over the block graph (spill_cfg). It applies to compute stages only.

  • Nonempty:
    • at entry, only when the stage starts with the entry mask;
    • on the fallthrough of s_cbranch_execz;
    • on the taken edge of s_cbranch_execnz.
  • Killed: by any instruction that writes_exec accepts, including v_cmpx, saveexec, wrexec and s_movreld.
  • Joins: a join keeps the fact only when every incoming edge has it. A block with no predecessor proves nothing.
  • Unresolved branch targets: if any branch target is unresolved, nothing in the program is proven.

A uniform load at a proven offset is recomputed even when exec_known is false. Under a narrowed EXEC, a lane that skipped the address definition holds an older copy. So a VGPR address must still have its recomputable uniform expression; the copy is used only under the entry mask, as before.

Everything else in the read is unchanged. A branch target, an EXEC write, or an overwrite between the load and the read still refuses. Image loads keep their current gate.

Not covered

  • The largest group of this refusal (see the last census comment on #2644) reads a per-lane record word as a constant-buffer index. It is not uniform and stays refused.
  • An entry-block load after s_mov_b64 exec, 1 uses the VGPR-copy path, not the recompute path, and stays refused.

Verification

Base: main 1a9ccbc4. Python 3.14.8, no GPU.

tests/startup_gfx2hlsl_readfirstlane_load_recompute_check.py adds these cases:

  • Accepted:
    • a scalar-offset load on the s_cbranch_execz fallthrough;
    • an address-expression load on the same fallthrough;
    • a formatted idxen load on the same fallthrough;
    • a load on the s_cbranch_execnz taken edge;
    • the entry mask carried through a scalar branch taken before a s_and_saveexec.
  • Refused:
    • a lane-varying address on the fallthrough (negative control);
    • an EXEC write after the fallthrough;
    • a join with the execz taken edge;
    • EXEC narrowed between the load and the read;
    • a join with a narrowed path not proven nonempty;
    • a back edge from a block that narrows EXEC;
    • a stale address copy on the fallthrough;
    • a loop head between the load and the read.
  • Control: with nonempty_exec_loads returning nothing, every accepted case refuses again.

Mutation checks:

  • If the analysis returns every offset, the refusal controls for the EXEC write after the fallthrough, the join with the execz taken edge, the unproven narrowed path and the back edge are accepted. These four controls test the dataflow itself.
  • If the address-expression guard is removed, stale address copy on the s_cbranch_execz fallthrough fails.
python -B tests/startup_gfx2hlsl_readfirstlane_load_recompute_check.py   # 0 failed
python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py          # 0 failed
python -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py        # 0 failed
python -B tests/startup_compute_readfirstlane_check.py                   # pass (pure-Python mode)
python -B tests/startup_gfx2hlsl_compute_check.py                        # pass
python -B tests/startup_compute_request_offline_check.py                 # pass
python -B tests/startup_gfx2hlsl_compute_spill_check.py                  # PASS
python -B tests/startup_gfx2hlsl_compute_mask_spill_check.py             # PASS
python -B tests/startup_tools_lint_check.py                              # pass

Local census (counts only, inputs not attached): 512 compute requests stopped first at v_readfirstlane_b32 requires a proven uniform source on main.

  • Moved past this refusal: 15 requests.
  • Translated, DXC cs_6_0 compiles: 4 of them.
  • Next first refusals of the rest: LDS exchange or fill (9), ds_bpermute_b32 (1), and a scalar live out of an execz block (1).

Regression replay: I retranslated the 223 compute requests of the same set that already translated, on main and on this branch.

  • All 223 still translate, and DXC compiles all of them.
  • All manifests are identical.
  • The HLSL is identical for 222. The other one only gains three unused reload temporaries.

Not measured: GPU readback (WARP or hardware) of the new path.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5577614794 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3692). Originally posted by [Not-Saii](https://github.com/Not-Saii) on 2026-12-01T02:18:40Z. --- ## Change Refs #2644. `v_readfirstlane_b32` of a uniform compute buffer load past the entry block is lowered by reloading the words into a temporary in every lane (the recompute path). Until now that path required the entry EXEC mask at the load. On RDNA 2, READFIRSTLANE with an empty EXEC returns the old lane-zero VGPR, and an unguarded reload cannot reproduce that. The existing comment already allowed this "until the path proves a nonempty wave". This change adds that proof. `Stage.nonempty_exec_loads()` is a forward must-dataflow over the block graph (`spill_cfg`). It applies to compute stages only. - **Nonempty:** - at entry, only when the stage starts with the entry mask; - on the fallthrough of `s_cbranch_execz`; - on the taken edge of `s_cbranch_execnz`. - **Killed:** by any instruction that `writes_exec` accepts, including `v_cmpx`, saveexec, wrexec and `s_movreld`. - **Joins:** a join keeps the fact only when every incoming edge has it. A block with no predecessor proves nothing. - **Unresolved branch targets:** if any branch target is unresolved, nothing in the program is proven. A uniform load at a proven offset is recomputed even when `exec_known` is false. Under a narrowed EXEC, a lane that skipped the address definition holds an older copy. So a VGPR address must still have its recomputable uniform expression; the copy is used only under the entry mask, as before. Everything else in the read is unchanged. A branch target, an EXEC write, or an overwrite between the load and the read still refuses. Image loads keep their current gate. ## Not covered - The largest group of this refusal (see the last census comment on #2644) reads a per-lane record word as a constant-buffer index. It is not uniform and stays refused. - An entry-block load after `s_mov_b64 exec, 1` uses the VGPR-copy path, not the recompute path, and stays refused. ## Verification Base: main 1a9ccbc4. Python 3.14.8, no GPU. `tests/startup_gfx2hlsl_readfirstlane_load_recompute_check.py` adds these cases: - **Accepted:** - a scalar-offset load on the `s_cbranch_execz` fallthrough; - an address-expression load on the same fallthrough; - a formatted `idxen` load on the same fallthrough; - a load on the `s_cbranch_execnz` taken edge; - the entry mask carried through a scalar branch taken before a `s_and_saveexec`. - **Refused:** - a lane-varying address on the fallthrough (negative control); - an EXEC write after the fallthrough; - a join with the execz taken edge; - EXEC narrowed between the load and the read; - a join with a narrowed path not proven nonempty; - a back edge from a block that narrows EXEC; - a stale address copy on the fallthrough; - a loop head between the load and the read. - **Control:** with `nonempty_exec_loads` returning nothing, every accepted case refuses again. Mutation checks: - If the analysis returns every offset, the refusal controls for the EXEC write after the fallthrough, the join with the execz taken edge, the unproven narrowed path and the back edge are accepted. These four controls test the dataflow itself. - If the address-expression guard is removed, `stale address copy on the s_cbranch_execz fallthrough` fails. ``` python -B tests/startup_gfx2hlsl_readfirstlane_load_recompute_check.py # 0 failed python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py # 0 failed python -B tests/startup_gfx2hlsl_readfirstlane_recompute_check.py # 0 failed python -B tests/startup_compute_readfirstlane_check.py # pass (pure-Python mode) python -B tests/startup_gfx2hlsl_compute_check.py # pass python -B tests/startup_compute_request_offline_check.py # pass python -B tests/startup_gfx2hlsl_compute_spill_check.py # PASS python -B tests/startup_gfx2hlsl_compute_mask_spill_check.py # PASS python -B tests/startup_tools_lint_check.py # pass ``` **Local census (counts only, inputs not attached):** 512 compute requests stopped first at `v_readfirstlane_b32 requires a proven uniform source` on main. - **Moved past this refusal:** 15 requests. - **Translated, DXC `cs_6_0` compiles:** 4 of them. - **Next first refusals of the rest:** LDS exchange or fill (9), `ds_bpermute_b32` (1), and a scalar live out of an execz block (1). **Regression replay:** I retranslated the 223 compute requests of the same set that already translated, on main and on this branch. - All 223 still translate, and DXC compiles all of them. - All manifests are identical. - The HLSL is identical for 222. The other one only gains three unused reload temporaries. Not measured: GPU readback (WARP or hardware) of the new path. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
lukaslw-lab commented 2026-12-01 02:32:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-12-01T02:32:37Z.

Review: APPROVED


Reviewed 273a534d by reading the diff; not built or run.

Verdict: approve. nonempty_exec_loads is a sound must-analysis, and the new recompute gate keeps the address guard that a narrowed EXEC needs. No MUST.

  • NIT No case covers the rule that an unresolved branch target proves nothing (tools/startup/gfx2hlsl.py:4557, return set()). An s_setpc_b64 without a calculated target in front of an s_cbranch_execz fallthrough load, expected to refuse, would pin it.
  • Checked, no finding:
    • The state starts optimistic and only drops (new and has, :4579), so the fixpoint is the greatest one. The execz fallthrough and execnz taken edges assert nonempty, and the opposite edges assert empty. An execz that branches to its own fallthrough gets both edges, so the AND drops the fact.
    • The address guard (vaddr[1] in exprs, :4922) relies on forget_writes clearing every uniform_expr on any EXEC write (:5129-5131). An address that still has an expression was therefore written after the last EXEC write, by the same lanes that are active at the load. That is why recomputing it matches the first active lane. The stale address copy case covers the scalar-rewrite side.
    • The proof is computed only for cs (:4062), and the entry-mask path is unchanged.
    • The control that stubs nonempty_exec_loads to set() makes every accepted case refuse again, so the accepted cases depend on the new proof. The back-edge, narrowed-join and taken-edge-join refusals exercise the dataflow itself.

Tests: startup_gfx2hlsl_readfirstlane_load_recompute_check.py adds 5 accepted cases, 8 refused cases and the control. They reach the new edges, the joins and the address guard. CI ran the test at this head (the Windows Python checks step, windows.yml:322).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6124142415 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3692#pullrequestreview-6124142415). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-12-01T02:32:37Z. **Review:** APPROVED --- Reviewed `273a534d` by reading the diff; not built or run. **Verdict: approve.** `nonempty_exec_loads` is a sound must-analysis, and the new recompute gate keeps the address guard that a narrowed EXEC needs. No MUST. - **NIT** No case covers the rule that an unresolved branch target proves nothing (`tools/startup/gfx2hlsl.py:4557`, `return set()`). An `s_setpc_b64` without a calculated target in front of an `s_cbranch_execz` fallthrough load, expected to refuse, would pin it. - Checked, no finding: - The state starts optimistic and only drops (`new and has`, `:4579`), so the fixpoint is the greatest one. The `execz` fallthrough and `execnz` taken edges assert nonempty, and the opposite edges assert empty. An `execz` that branches to its own fallthrough gets both edges, so the AND drops the fact. - The address guard (`vaddr[1] in exprs`, `:4922`) relies on `forget_writes` clearing every `uniform_expr` on any EXEC write (`:5129-5131`). An address that still has an expression was therefore written after the last EXEC write, by the same lanes that are active at the load. That is why recomputing it matches the first active lane. The `stale address copy` case covers the scalar-rewrite side. - The proof is computed only for `cs` (`:4062`), and the entry-mask path is unchanged. - The control that stubs `nonempty_exec_loads` to `set()` makes every accepted case refuse again, so the accepted cases depend on the new proof. The back-edge, narrowed-join and taken-edge-join refusals exercise the dataflow itself. Tests: `startup_gfx2hlsl_readfirstlane_load_recompute_check.py` adds 5 accepted cases, 8 refused cases and the control. They reach the new edges, the joins and the address guard. CI ran the test at this head (the Windows Python checks step, `windows.yml:322`).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!3692
No description provided.