Skip to content

startup: fold s_mov_b32 copies of a constant SGPR so inline samplers translate (Part of #1723) - #2708

Merged
MasterDomino merged 6 commits into
R0Y00:mainfrom
tedin7:x2w-const-copy-fold
Nov 26, 2026
Merged

MasterDomino merged 6 commits into
R0Y00:mainfrom
tedin7:x2w-const-copy-fold

Conversation

@tedin7

@tedin7 tedin7 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #1723

What this unblocks: an inline sampler whose words are built by copying a zero register (the way LLVM builds <0, N, M, 0>) now translates to a static sampler instead of being refused as a descriptor held in an unknown register.

Small follow-up to #2319, which folds s_brev_b32 of a known constant; the copy form was still missing.

What changed

s_mov_b32 sD, sS where sS holds a plain known constant now makes sD that constant (same word, same symbolic value), so const_words() can read the four sampler words. Example: s_mov_b32 s15, 0 then s_mov_b32 s12, s15 for sampler word 0.

The second commit narrows the fold to a plain constant. A register with an unassigned placeholder, a run-time value or a descriptor symbol is not copied as a constant, so a sampler built from run-time words stays refused.

What stays refused

A sampler with any word from a run-time register, a table or the sampler heap; vector or 64-bit moves; copies of non-constant registers.

Tests

tests/startup_inline_sampler_check.py (already registered in CMakeLists.txt): check_copied_constant_sampler (a copied-zero sampler folds to the expected smp line), check_copy_only_folds_plain_constants (copies of non-constant symbols are not folded), and check_copied_sampler_after_overwrite (a run-time source is refused, a new constant wins). On origin/main the check fails (descriptor operand s[24:27] holds ('const', 0)); with the change it passes.

Verification

  • python3 -B tests/startup_inline_sampler_check.py: PASS (fails on origin/main)
  • python3 -B tests/startup_inline_sampler_check.py binary llvm-mc-19: PASS (fixture words assemble to the recorded words)
  • All tests/startup_gfx2hlsl_*_check.py run with no arguments on this branch and on origin/main: identical result per script (108 scripts, export_null_draw and mul24_align_draw need a compiler and renderer and exit 1 on both).
  • Win2025 (dev + release, head a52f1d7): x2w_pipe.py win2025 2708 --regex startup_inline_sampler: 3/3 passed (controls, warp, hardware). The draw now includes a shader whose sampler word 0 is built in s29 and copied into s24 (it must read the clamped texel with its own sampler and mismatch with the wrap sampler), so a GPU readback depends on the copy fold.
  • Build and run: cmake --build --preset dev --target offscreen_check then ctest --preset dev --no-tests=error -R '^startup_inline_sampler' -V.
  • Branch merged with current main (no rebase) so it builds on Windows after startup: remove duplicate Pass render target bindings breaking Windows builds #2706.

AI-assisted; commands listed were executed locally.

…plers translate

llc builds a zero sampler as s_mov_b32 s15, 0 then s_mov_b32 s12, s15. The copy kept no symbol, so the sampler operand held None and the shader was refused. The copy now keeps the constant. Extends startup_inline_sampler_check with a copied-constant sampler control.

(cherry picked from commit da03456187fe80e5bf53c800f32766df5cb02504)
…ed samplers

Do not fold a copy of the tagged unassigned placeholder. Tests: placeholder copy, run-time source and a copy after the source was rewritten must not fold.

(cherry picked from commit cbd9717507808d3110e71c3e5e3cd735b23d27e8)
…trol

check_draw now also draws a shader whose sampler word 0 is built in s29 and copied into s24; it must read the clamped texel with its own sampler and mismatch with the wrap sampler.
@tedin7
tedin7 marked this pull request as ready for review November 26, 2026 03:40
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T03:46:36.169545Z a52f1d7 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@tedin7

tedin7 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

@MasterDomino Ready for review on head a52f1d7.

Independent gate review of the fold (s_mov_b32 copy of a plain constant SGPR): tried a branch-arm sampler word, copies of a run-time/placeholder/descriptor source, a 64-bit pair, vcc and m0 sources, and two one-line mutations of the fix (drop the plain-constant guard; copy a wrong value). The first set stays refused or unfolded, both mutations fail startup_inline_sampler_check.py.

Gap found and closed: only translation controls covered the copy form. check_draw now also draws a shader whose sampler word 0 is built in s29 and copied into s24; it must read the clamped texel with its own sampler and mismatch with the wrap sampler (wrong-value control). The branch was also merged with current main (no rebase, no force) so it builds on Windows after #2706.

Verification:

  • Linux: python3 -B tests/startup_inline_sampler_check.py and ... binary llvm-mc-19 pass; on origin/main's translator the check fails.
  • Win2025 (dev + release): x2w_pipe.py win2025 2708 --patch <diff> --targets offscreen_check --regex startup_inline_sampler: 3/3 passed (controls, warp, hardware).

AI-assisted; commands listed were executed locally.

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

Reviewed head a52f1d75 (21 commits behind main, merges cleanly).

The fold itself looks right:

  • Only a plain ('const', v) source is copied. The tagged unassigned placeholder, ud, descriptor and unknown sources are not.
  • The value is copied at that point, so a later write to the source can't reach the copy.
  • set_s still assigns the register at run time.
  • At a join, the copy's constant is dropped exactly as a literal move's would be.

Blocking: this needs a shader-cache revision bump. A register copied from a constant now folds at every later read, so programs that already translated emit different HLSL. For example, s_mov_b32 s15, 5; s_mov_b32 s12, s15; s_add_u32 s13, s12, 1 lowers to s13 = s12 + 0x00000001u; on main and to s13 = 0x00000005u + 0x00000001u; here. #2319 bumped the revision for the same kind of change (the s_brev_b32 fold).

Please move to x2w-startup-shader-19 / 19cs (main is at 18 / 18cs) in:

  • gfx2hlsl.py (pipeline_key, compute_key);
  • src/startup_stubs/d3d12_draw.inl (revision, compute_revision);
  • the tests that pin the revision;
  • the revision note in docs/startup-runtime.md.

Moving the slot-57 mesh key is your call.

Non-blocking:

  • Untested run-time write: a one-line mutant that keeps the constant symbol but drops the run-time sD = … write passes every inline-sampler and join check. A case where a join drops the copy's constant and the copy is read afterwards would pin it.
  • Misleading test comment: in check_copy_only_folds_plain_constants, the case commented "A copy after the source was overwritten takes the new value" doesn't overwrite anything.

Environment: Windows 11 build 26300; AMD Radeon RX 9070 XT (driver 32.0.32029.2010); WARP 10.0.26100.9818; debug layer on, 0 errors; dev preset.

Runs and mutants:

  • ctest --preset dev --no-tests=error -R '^startup_inline_sampler|^startup_gfx2hlsl|^startup_image_3d': 256 passed, 0 skipped, 1 failed. The failure is startup_gfx2hlsl_fma_mix_dispatch_hardware, which fails the same way on main on this adapter (#2746), so it's unrelated.
  • A trial merge with main passed the 114 non-GPU checks.
  • Mutants caught by startup_inline_sampler_controls, _warp and _hardware: dropping the plain-constant guard, copying a wrong value, and disabling the fold.

AI-assisted; commands listed were executed locally.

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

Approving 2381d5c6; the change request is resolved.

  • Revision bump: x2w-startup-shader-19 / 19cs now appear in gfx2hlsl.py (pipeline_key, compute_key), in d3d12_draw.inl (revision, compute_revision), in the tests that pin the key, and in the revision note in docs/startup-runtime.md.
  • New check: startup_gfx2hlsl_const_copy_join_check pins the run-time write. I replayed the mutant that survived my earlier review, keeping the copy's constant symbol but dropping its run-time sD = … write: it now fails with "FAIL: straight line: the copy is still assigned at run time" (exit 1), and passes again once restored.

Pure-Python checks at this head, all passing (Windows 11 build 26300):

  • startup_gfx2hlsl_const_copy_join_check
  • startup_inline_sampler_check
  • startup_compute_request_offline_check
  • startup_gfx2hlsl_fract_check
  • startup_gfx2hlsl_buffer_store_check
  • startup_shader_cache_key_check

The GPU results from my earlier review (inline sampler WARP and RX 9070 XT) were at a52f1d75; the fold itself is unchanged since then. CI is green at this head.

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.

2 participants