Skip to content

startup: keep or refuse non-constant scalar symbols at joins in gfx2hlsl - #2292

Merged
R0Y00 merged 4 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-join-symbols
Nov 25, 2026
Merged

R0Y00 merged 4 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-join-symbols

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Change

Follow-up to #2279. That change drops scalar constants at a branch target that another path writes, over the
textual range between a branch and its target (proven s_setpc_b64 jumps included). It leaves every other symbol
unchanged, and two cases in the emitted HLSL stay wrong on main 342d3fa9:

  • Symbols that are not constants. A descriptor reloaded on one path binds the fallthrough path's descriptor; a
    descriptor word overwritten on one path is read as a register nothing assigned; an unplaced compute user SGPR
    written on one path is read as if placed; the high word of a 64-bit copy is never assigned, so a register
    written on only one path reads 0 on the other.
  • A path that leaves the loop's textual range and comes back writes a constant the textual range does not cover
    (s25 = s25 + 0x00000001u; on main, where the hardware adds 1, then 5 twice).

What changes (tools/startup/gfx2hlsl.py, upstream's join_writes is kept as it is):

  • Joined registers are worked out over the basic blocks and their dominators instead of a textual range. A
    proven s_setpc_b64 is an edge and ends its block, like s_branch; an unproven one adds no edge.
  • At a join, a joined register keeps its symbol when every path that wrote it brings an equal symbol that names
    one value (a descriptor reloaded from the same table slot, for example). Otherwise it is read at run time when it
    holds a constant, and refuses any read before its next write when a path brings a descriptor word, a pointer or
    an unplaced register. A path that never wrote the register brings nothing.
  • Back edges are known only once the loop body is translated. A back edge that brings another symbol makes the
    stage translate again with that register read at run time, or refused, at the header.
  • A 64-bit copy assigns its high word at run time.

Not covered: names_one_value treats equal ptr symbols as one value although s_add_u32 on a pointer keeps the
symbol (exists on main; open PR #2069 addresses it). A register written through s_movrel* counts as writing
every SGPR, which only costs folding.

Overlapping open PRs: #1631 (loop structure in gfx2hlsl) and #2069 touch the same Stage.translate area; the
exact-indented-line checks in the new fixture will need #1631's indentation if it lands first.

AI-assisted (Claude Code).

Verification

Verification (head aa17c95, rebased on origin/main 5fa712e; Windows, Python 3.13):

  • python -B tests/startup_gfx2hlsl_join_symbols_check.py passes. DESC_ONE_PATH (the branch skips the only
    s_load_dwordx8 that writes s[16:23]) and the new PTR_OFFSETS (two paths add 4 and 8 to the same pointer) are
    refusal controls; DESC_SAME is still accepted and kept.
  • Rule: a register with a symbol that has no run-time value on one edge and no definition on another is unproven at the
    join, and a later read is refused. An add on a pointer marks its symbol added, and only an untouched pointer names
    one value.
  • Mutation: disabling the missing-definition refusal makes DESC_ONE_PATH fail (accepted); dropping the added mark
    makes PTR_OFFSETS fail.
  • Every tests/startup_gfx2hlsl_*_check.py (53 scripts with startup_translator_check.py),
    startup_compute_request_offline_check.py (pinned key recomputed), docs_links_check.py, bidi_controls_check.py pass;
    git diff --check origin/main...HEAD is clean.
  • Cache revision moved to x2w-startup-shader-10 in a separate commit, in the files startup: move the shader cache revision to x2w-startup-shader-9 #2299 touched. The C++ change in
    tests/startup_indirect_draw_indexed.cpp is textual and was not built.
  • Not measured: the pixel dispatch tests (WARP, hardware) and the LLVM binary reassembly of the two new fixture words
    (no llvm-mc locally; PTR_OFFSETS words are hand-encoded).

Evidence and follow-up

  • Fixtures: source-built gfx1013 stages behind the fixed vertex stage of startup_flat_param_check.py; the
    compute stage uses the root of startup_gfx2hlsl_compute_check.py. No retail bytes.
  • Negative control: startup_gfx2hlsl_join_symbols_folded_* runs the dispatch with the old OUTSIDE
    statement and must give 3, not 11. With this branch's gfx2hlsl.py replaced by main's, the pure-Python check
    fails at DESC_CALC accepted, expected a refusal.
  • Not checked: the binary mode (fixture words against LLVM 15) was not run here, there is no llvm-mc on
    this machine; DESC_CALC's words are hand-encoded. The cache effect (graphics and compute requests) was not
    re-measured on this base.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

🤖 Generated with Claude Code

@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 5e6ec7abcef0b8ce5ddefaf3f2959b7d52492976 (comment only).

Scope / overlap. #2279 (scalar constants at joins) is already merged and this PR is a follow-up on top of it: it keeps join_writes as is and adds a basic-block/dominator joined_registers() plus a symbol meet at joins. No duplicate coverage with #2279. Its test (startup_gfx2hlsl_loop_constants_check.py) still passes on this head. Title is neutral, MIT/clean-room looks fine, mergeable. Branch is 2 commits behind main (25317c63); no conflict reported.

CI: Windows build+test, Linux Python checks, gfx1013 fixtures all pass; /O2 and chart jobs skipped.

Commands run locally on the head:

  • python3 tests/startup_gfx2hlsl_join_symbols_check.py -> PASS
  • python3 tests/startup_gfx2hlsl_loop_constants_check.py -> PASS
  • python3 tests/docs_links_check.py -> 18 files, 436 links OK
  • python3 tests/bidi_controls_check.py -> OK
  • git diff --check origin/main...HEAD -> clean

Mutations (both killed):

  1. names_one_value for desc changed to return True -> new fixture fails on the modified-desc / descnum assertion.
  2. Dropped the "later block branches back" term in joined_registers (b in reach and b >= t) -> new fixture fails (23 in joined_registers()[0xc]).

Findings

  • P1: none.
  • P2: none.
  • nit (tools/startup/gfx2hlsl.py, names_one_value): equal ptr symbols are treated as one value although s_add_u32 on a pointer keeps the symbol. The PR documents this as a known gap (exists on main, #2069). Fine to leave.
  • nit (Stage.__init__ retranslate loop): termination relies on the monotone kept -> run -> refuse order; the dict only grows, and refusals is truncated per pass. No change needed.
  • nit: the new fixture checks exact indented lines, so it will need a touch-up if #1631 lands first (already noted in the body).

Not run: Win2025 WARP. The change is scalar symbol tracking in the Python translator (no new math lowering needing a GPU-readback comparison), and the Windows CI job passed.

AI-assisted; commands listed were executed locally.

R0Y00 commented Nov 25, 2026

Copy link
Copy Markdown
Owner

I reproduced the host checks on 5e6ec7ab. One join case still needs a decision in the implementation before this lands: DESC_ONE_PATH sets s30 = 0 and takes the branch over the only s_load_dwordx8 that writes s[16:23], then consumes that descriptor at the target. The check currently requires translation into r0.Load, so the generated shader reads an apparently valid texture using provenance from the predecessor that never executed. Please treat a missing descriptor definition on a reaching edge as unproven, or prove the edge cannot reach the use. Turn this fixture into a refusal control and keep the existing DESC_SAME acceptance check. Also keep distinct pointer offsets separate if their symbolic tuples compare equal. Once the lowering is settled, the cache key needs to cover the resulting generated code.

@stan495 stan495 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verified head 5e6ec7abcef0b8ce5ddefaf3f2959b7d52492976 on Windows 11 (26200.9974).

Reviewed the tools/startup/gfx2hlsl.py diff (joined_registers(), names_one_value(), run_time_symbol(), scalar_destinations(), and the bounded Stage.__init__ re-translation loop). The dominance + back-reachability check soundly separates writes above the join from loop-body/back-edge writes, keeps only symbols proven identical across all incoming paths, and terminates cleanly.

Ran locally on 5e6ec7ab:

  • python -B tests/startup_gfx2hlsl_join_symbols_check.py -> passed (loop counters, forward joins, a root-constant loop, 64-bit copies and descriptor joins pass; equal symbols on every path are kept; straight-line code still folds)
  • python -B tests/startup_gfx2hlsl_loop_constants_check.py -> passed (PASS)
  • python -B tests/startup_translator_check.py -> passed (startup translator checks: 0 failed)
  • All tests/startup_gfx2hlsl_*_check.py checks -> passed
  • python -B tests/docs_links_check.py -> passed (18 files, 436 links)
  • python -B tests/bidi_controls_check.py -> passed (1585 tracked files)
  • git diff --check upstream/main...HEAD -> clean

No P1/P2 findings. Approve on 5e6ec7abcef0b8ce5ddefaf3f2959b7d52492976.

NiiRoZz and others added 3 commits November 25, 2026 12:22
Follow-up to R0Y00#2279. Work out the joined registers over the basic blocks and
dominators (a proven s_setpc_b64 is an edge). At a join, a register keeps a
symbol every path agrees on, reads a constant at run time, and refuses a read
when a path brings a descriptor word, pointer or unplaced register. A 64-bit
copy assigns its high word at run time. Adds a pure-Python and dispatch check.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…sets in gfx2hlsl

A register that holds a non-run-time symbol on one edge into a branch target and
has no definition on another is unproven there, so a later read is refused
instead of using the symbol of the path that never ran. An add on a pointer now
marks its symbol 'added', and only an untouched pointer names one value at a
join. DESC_ONE_PATH becomes a refusal control; PTR_OFFSETS is new.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The join-symbol lowering changes the generated HLSL for programs with joins, so
translations cached under revision 9 are not reused.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@NiiRoZz
NiiRoZz force-pushed the startup/gfx2hlsl-join-symbols branch from 5e6ec7a to aa17c95 Compare November 25, 2026 11:26

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

Re-review of head aa17c95646683d3895f417e00264758a071c6429 (previous review covered 5e6ec7a).

Result: no P1, no P2. Two optional nits.

What changed since 5e6ec7a

  • 75053419 (tools/startup/gfx2hlsl.py, join code): a register that holds a non-run-time symbol on one edge into a branch target and has no definition on another edge now gets ('joined at a branch target',). That means a later read is refused, and the symbol from the path that never ran is not used.
  • Same commit: s_add_u32 on a ptr/ptrhi symbol now marks it ('ptr', src, 'added'). names_one_value accepts ptr/ptrhi only when len(sym) == 2, so only an untouched pointer counts as one value at a join. This closes the old "equal ptr symbols" shortcut.
  • aa17c956: cache revision moved to x2w-startup-shader-10. I checked every site: d3d12_draw.inl:235,355, tests/startup_indirect_draw_indexed.cpp:235, gfx2hlsl.py:4300,4309, startup_compute_request_offline_check.py:33 and docs/startup-runtime.md:192 all agree.
  • Test and docs: DESC_ONE_PATH is now a refusal control, PTR_OFFSETS is new, and the experiments.md text matches the code.

Commands run locally on the PR head (Linux)

  • python3 tests/startup_gfx2hlsl_join_symbols_check.py: passes (equal symbols on every path are kept, straight-line code still folds).
  • Neighbours, all exit 0: startup_gfx2hlsl_{loop_constants,refusals,compute,scalar_m0,root}_check.py, mesh_join_descriptor_check.py, startup_compute_request_offline_check.py, startup_build_rev_check.py.
  • python3 tests/docs_links_check.py: 18 files, 446 links OK.
  • python3 tests/bidi_controls_check.py: OK.
  • git diff --check origin/main...HEAD: clean.
  • CI on this head: Linux Python checks, gfx1013 fixtures and Windows build/test pass. The /O2 and coverage jobs were skipped.

Mutations (each restored with git checkout -- .)

  1. names_one_value accepts ptr/ptrhi of any length: killed by PTR_OFFSETS (accepted, expected a refusal).
  2. The new "edge with no definition" branch disabled (if False:): killed by DESC_ONE_PATH.
  3. The 'added' mark removed from the add handler: killed by PTR_OFFSETS.

Nits (non-blocking)

  • The pointer-add handling relies on len(own) > 2 meaning "already marked". If a third tuple field is ever added to ptr symbols, this check breaks quietly. A named helper or constant would make that safer.
  • The "no definition at all" case only has coverage through DESC_ONE_PATH (descriptor words). A pointer-symbol variant would pin the other symbol kinds in that branch.

Windows: I did not queue a Win2025 run. The change is translator-side symbol tracking, and the Linux pure-Python tests plus the existing WARP dispatch in CI cover it. Clean-room and title-neutral checks pass, and the PR is mergeable.

AI-assisted; commands listed were executed locally.

@NiiRoZz

NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Done in aa17c95 (rebased on main 5fa712e). A register that holds a symbol with no run-time value on one edge and no definition on another is now unproven at the join, and a later read is refused. DESC_ONE_PATH is a refusal control; DESC_SAME is still accepted and kept. An add on a pointer now marks its symbol, and only an untouched pointer names one value at a join; PTR_OFFSETS (add 4 vs add 8 on the same base) is refused. Disabling either rule makes its control fail. The shader cache revision moved to x2w-startup-shader-10 in a separate commit. Not run: the llvm-mc binary check (the new fixture words are hand-encoded) and the WARP/hardware dispatches.

AI-assisted (Claude Code).

@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 78a1cd09347d89328e70f5710da55392fd736f2d (comment only).

Scope read: git diff --stat origin/main...HEAD (10 files, +598/-31). I read the gfx2hlsl.py join logic (joined_registers, names_one_value, run_time_symbol, scalar_destinations, the Stage.__init__ retranslation loop and the join handling in translate). The title is neutral, the PR is mergeable, and I saw nothing that breaks clean-room rules. At review time the CI checks were still pending.

Commands run locally on this head:

  • python3 tests/startup_gfx2hlsl_join_symbols_check.py: passes.
  • python3 tests/startup_compute_request_offline_check.py: passes.
  • python3 tests/docs_links_check.py: passes (18 files, 447 links).
  • python3 tests/bidi_controls_check.py: passes (1600 files).
  • git diff --check origin/main...HEAD: clean.

Mutations of the new logic (each reverted with git checkout -- .):

  1. names_one_value treats every ptr/ptrhi as one value (drops the len(sym) == 2 guard). Killed by the PTR_OFFSETS case.
  2. joined_registers: the above_dominators textual-order check is disabled (if False:). Survived: the join-symbols check still passes.
  3. joined_registers: the loop-body and later-block write set (b in reach and b >= t) is disabled. Killed.

Findings

  • P1: none.
  • P2 (1): the textual-order guard in joined_registers (the above_dominators / zip(above_dominators, above_dominators[1:]) check) has no test that fails when it is removed. This is the path where dominating blocks sit out of execution order. Please add a fixture where a dominator block is placed after its dominated block, or drop the guard if it cannot happen.
  • Nit: translate() reads joined_registers() through getattr(self, 'calculated_pc', {}) and getattr(self, 'force_joined', {}) for unit probes that skip __init__. This works, but it adds code that exists only for tests. A small Stage factory in the test would be cleaner.
  • Not verified: no Windows WARP run. The change is a Python-only translation-state change plus a shader cache revision bump, so I judged the Linux checks enough for this review. Final Windows proof is left to the CI and the gate.

AI-assisted; commands listed were executed locally.

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.

4 participants