gfx2hlsl: keep a scalar constant across a branch target when its write dominates #2452
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2460"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T17:21:14Z.
Depends on #2459 (this branch is stacked on it and reuses its dominance helper; the diff here includes that commit until it merges).
What / Why
At a branch target
Stage.translate()drops every scalar constant whose register is in the target's joined set.That set is built to be safe for symbols: besides the writes on paths into the target, it holds the writes of every
earlier block that is off the dominator chain. So a register that an earlier conditional block used for anything,
and that every path then sets to a constant, loses the constant at each later join, although no path into that join
writes it.
Measured effect: a constant sampler built with
s_movk_i32,s_bfm_b32ands_mov_b64a few dozen instructionsbefore the image instruction reached it as four plain words. The image instruction was refused as a descriptor
operand without a symbol, or as
image_gather4_lz requires a constant sampler. In every measured case theconstant's write dominated the join where it was dropped and no block on a path from the write to the join wrote
the register.
Change
last write (program order) must dominate the target, and no block on a path from that write to the target may
write the register again. A register without a write in the program (entry constant) counts as written in the
entry block.
forward-join controls are unchanged and pass.
Stage.unjoined_masks(target, last_write)from the parent branch becomesStage.unjoined(target, last_write, regs)and serves both the mask marks and the constants.
s_movreld(any destination) now counts as the last write ofevery SGPR.
Tests
tests/startup_gfx2hlsl_loop_constants_check.py(extended, pure Python), 5 new programs:The one file that did not pass,
tests/startup_image_get_lod_view_check.py, needs a compiler, renderer and adapterargument and stops at its argument list. Not run:
binary/decodemodes (llvm-mc, libLLVM) and device checks.Measured
First refusal per request over the same 124 locally captured vertex/pixel requests, on top of the parent branch and
a local merge of open translator PRs:
image_gather4_lzrequires a constant samplerv_cndmask_b32(open PR #2391)s_loadthrough a pointer joined at a branch targets_getpc_b64v_readfirstlane_b32requires a proven uniform sourceLimits
set_semits theassignment), so the change affects folding and constant-descriptor recognition, not which values exist.
covered by the measurement only, not by a fixture.
docs/statement described the old drop rule, so no doc text changes. The test docstring's claim about aloop-body mutation was already not red on main and is corrected.
joined_registers(calculateds_setpc_b64targets included).🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T18:27:34Z.
Review: COMMENTED
Review of head
661c9a1a(Linux checks only; no Windows run by me). This branch is stacked on #2459: its first commit (d94dae50) is that PR's head, so I reviewed only the second commit (git diff HEAD~1 HEAD: gfx2hlsl.py, loop_constants check, a 6-line tweak to the complement check).What I checked
Stage.unjoined(target, last_write, regs)generalizesunjoined_masks. For constants it keeps a scalar constant at a join only when the block of its last write dominates the target and no block on a path from that write to the target writes the register again. A loop counter set before a loop and bumped in the body is still dropped at the head (the body block is inafter(w) & into). This matches the PR description.last_writenow records every SGPR for ananydestination (s_movreld), so those writes also invalidate the proof. Good catch, consistent with the mask path.Commands run (all passed)
python3 -B tests/startup_gfx2hlsl_loop_constants_check.py-> PASSpython3 -B tests/startup_gfx2hlsl_sop2_complement_check.py-> PASSpython3 tests/docs_links_check.py-> OK;python3 tests/bidi_controls_check.py-> OKgit diff --check origin/main...HEAD-> cleanconstsunconditionally (old behavior) -> loop_constants check FAILS ("constant set after an earlier arm ...").0x0 << 3).git checkout -- .after each.Notes
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T18:56:29Z.
Five mutations on the join proof: four caught, and the survivor is a branch that no check in the family reaches
Baseline:
startup_gfx2hlsl_loop_constants_check.py→ PASS,startup_gfx2hlsl_sop2_complement_check.py→ PASS, both exit 0.w not in dom[t] or …→ the clobber test alone)set(regs)→set())… on a lane mask joined at a branch target)scc_effectsgate droppedThe first two are the ones worth having together:
unjoined()is the conjunction of "the last write dominates the target" and "nothing on a path from that write to the target writes the register again", and each half alone is caught, so the fixture exercises both a write that does not dominate and a write that is clobbered after dominating. A proof that kept only one half would be wrong in a different direction in each case, and both are visible.The survivor:
target not in indexis never reachedRather than guess, I counted the branch:
So no fixture in the family — not just this PR's two — ever takes that path. Two readings, and only you can say which:
succis built with[index[to]] if to in index else [], so a branch to an offset outside the program already contributes no successor edge; if such a target can never reachunjoined(), the guard is dead and could go with a one-line comment saying why.Either way the mutation surviving is not a defect I can demonstrate; it is a line whose behaviour no test constrains.
What the two caught refusal-side rows establish
The lane-mask refusal and the
scc_effectsgate are both about what may cross a join at all. Removing either changes accepted output, so the fixtures are asserting the refusal, not just the acceptance — which is the half that tends to go untested.AI-assisted; the five mutations and the 64-check reachability sweep were run locally on Windows 11 build 26300.9974.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T19:37:58Z.
Review: COMMENTED
Review of head
dfff36ea(Linux checks only; Windows run follows separately). #2459 is merged, so the dependency is satisfied. The change since the earlier review is main merges plus the removal of the unreachabletarget not in indexguard inStage.unjoined.What I checked
Stage.unjoined(target, last_write, regs)now serves both the lane-mask marks and the constant drop (tools/startup/gfx2hlsl.py, join code near theconstsset). The dominance plus no-rewrite-on-path rule is the same one the mask marks already used.s_movreldnow counts as the last write of every SGPR, which is the safe direction.tests/startup_gfx2hlsl_loop_constants_check.pyhas two folded cases and three negative controls.Commands run (worktree on the exact head)
python3 -B tests/startup_gfx2hlsl_loop_constants_check.py-> PASSpython3 -B tests/startup_gfx2hlsl_sop2_complement_check.py-> PASSpython3 -B tests/startup_gfx2hlsl_join_symbols_check.py-> PASSpython3 -B tests/startup_translator_check.py-> 0 failedpython3 tests/docs_links_check.py,python3 tests/bidi_controls_check.py,git diff --check origin/main...HEAD-> cleanif False:in the drop condition) -> FAIL, 5 cases MISMATCHFindings: none. This is a translator-only change and makes no claim about running the game.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T19:48:47Z.
Review: COMMENTED
Review of head
9038e453(merge of main into the PR branch; the PR's own diff is unchanged: gfx2hlsl.py plus two check files; #2459 is merged so the dependency has landed).Checked:
Stage.unjoined(target, last_write, regs)now serves both lane-mask marks and scalar constants. A constant is dropped at a join only if the block of its last write does not dominate the target, or a block on a path from that write to the target writes it again. Logic reads correct, including entry constants (no write counts as entry block) ands_movreld(anydestination now records every SGPR inlast_write).tests/startup_gfx2hlsl_loop_constants_check.pycovers constant set after an earlier arm (kept), the same constant written in a loop (dropped), loop counter, and loop offset from counter. The sop2 check was adapted to the renamed helper.constsinstead of only the unjoined ones. The loop-constants check FAILS (constant set after an earlier arm).Commands run:
python3 tests/startup_gfx2hlsl_loop_constants_check.pyPASSpython3 tests/startup_gfx2hlsl_sop2_complement_check.pyPASSpython3 tests/docs_links_check.pyOKpython3 tests/bidi_controls_check.pyOKgit diff --check origin/main...HEADcleanNot a lowering of a math op, so no new WARP readback is needed; Win2025 WARP dev+release receipt follows on this head.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T19:33:28Z.
Thanks for the mutation run. The
target not in indexreturn inStage.unjoinedcannot be reached, so I removed it instead of adding a case.Both callers (the join of
self.masksand the join of the dropped constants) are inside the instruction loop oftranslateand passoff, the offset of an instruction inself.ins. The flowindexis built from the sameself.ins, so every target passed is in it. A branch whose target lies past the last instruction never reaches the function: the loop only visits instruction offsets, and the successor edges are only built for targets inindex.Current main is merged into the branch (the base PR #2459 landed); head
dfff36ea. Run from the repo root with the Python 3.13 venv:python -B tests/startup_gfx2hlsl_loop_constants_check.py: PASSpython -B tests/startup_gfx2hlsl_sop2_complement_check.py: PASSpython -B tests/startup_translator_check.py: 0 failedpython -B tests/docs_links_check.py: passedtests/startup_gfx2hlsl_*_check.py(66 files): all exit 0No CMake build or ctest run; these are the pure-Python checks only.
git diff --check origin/main...HEADis clean.🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T19:40:29Z.
Win2025 WARP receipt for head
dfff36ea.Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit
dfff36eae2ae8bc20f25f53ccba338b0056a3750:--preset dev):cmake --build --preset dev --target d3d12_xs_device_obj d3d12_xs_shim -- -j 2):42.60s(configure3.16s)/O2(--preset release):cmake --build --preset release --target d3d12_xs_device_obj d3d12_xs_shim -- -j 2):2.39s(configure3.38s)tests/mesh_heap_sampler_check.pyok (1.2s),tests/startup_buffer_address_check.pyok (0.6s),tests/startup_compute_request_offline_check.pyok (0.6s),tests/startup_compute_translate_check.pyok (0.3s),tests/startup_flat_param_check.pyok (0.1s),tests/startup_gfx2hlsl_add_sub_co_check.pyok (0.2s),tests/startup_gfx2hlsl_add_sub_co_ci_check.pyok (0.2s),tests/startup_gfx2hlsl_bit_alu_check.pyok (0.8s),tests/startup_gfx2hlsl_capture_check.pyok (1.4s),tests/startup_gfx2hlsl_cmp16_check.pyok (0.4s),tests/startup_gfx2hlsl_compare_dst_check.pyok (0.2s),tests/startup_gfx2hlsl_compute_check.pyok (0.4s),tests/startup_gfx2hlsl_cselect_mask_check.pyok (0.1s),tests/startup_gfx2hlsl_cvt_alu_check.pyok (0.1s),tests/startup_gfx2hlsl_f32_lg_check.pyok (1.6s),tests/startup_gfx2hlsl_f32_nan_check.pyok (6.8s),tests/startup_gfx2hlsl_getpc_refusal_check.pyok (0.1s),tests/startup_gfx2hlsl_handler_arity_check.pyok (0.1s),tests/startup_gfx2hlsl_image_arity_check.pyok (0.1s),tests/startup_gfx2hlsl_image_get_lod_check.pyok (0.1s),tests/startup_gfx2hlsl_int_med3_check.pyok (0.1s),tests/startup_gfx2hlsl_join_symbols_check.pyok (0.3s),tests/startup_gfx2hlsl_loop_constants_check.pyok (0.1s),tests/startup_gfx2hlsl_mask_overwrite_check.pyok (0.1s),tests/startup_gfx2hlsl_operand_count_check.pyok (0.4s),tests/startup_gfx2hlsl_readfirstlane_recompute_check.pyok (0.1s),tests/startup_gfx2hlsl_readfirstlane_uniform_check.pyok (0.2s),tests/startup_gfx2hlsl_refusals_check.pyok (0.6s),tests/startup_gfx2hlsl_root_check.pyok (0.9s),tests/startup_gfx2hlsl_saveexec_same_reg_check.pyok (0.1s),tests/startup_gfx2hlsl_scalar_arity_check.pyok (0.2s),tests/startup_gfx2hlsl_scalar_brev_fold_check.pyok (0.2s),tests/startup_gfx2hlsl_scalar_m0_check.pyok (0.1s),tests/startup_gfx2hlsl_sdwa_cvt_check.pyok (0.4s),tests/startup_gfx2hlsl_sgpr_spill_check.pyok (0.2s),tests/startup_gfx2hlsl_shift64_check.pyok (0.2s),tests/startup_gfx2hlsl_smem_offset_check.pyok (0.1s),tests/startup_gfx2hlsl_sop2_complement_check.pyok (8.2s),tests/startup_gfx2hlsl_sopk_accum_check.pyok (1.2s),tests/startup_gfx2hlsl_sopk_signext_check.pyok (0.2s),tests/startup_gfx2hlsl_swap_check.pyok (0.4s),tests/startup_gfx2hlsl_vopc_sdwa_check.pyok (6.8s),tests/startup_gfx2hlsl_waitcnt_depctr_check.pyok (0.1s),tests/startup_gfx2hlsl_watch_check.pyok (2.7s),tests/startup_image_2d_array_check.pyok (0.3s),tests/startup_image_3d_check.pyok (0.1s),tests/startup_image_gather_lz_check.pyok (0.8s),tests/startup_image_load_d16_check.pyok (1.2s),tests/startup_image_sample_a16_check.pyok (0.2s),tests/startup_image_sample_b_check.pyok (0.2s),tests/startup_image_sample_c_check.pyok (0.3s),tests/startup_image_sample_l_check.pyok (0.2s),tests/startup_image_sample_lod_check.pyok (0.8s),tests/startup_inline_sampler_check.pyok (0.2s),tests/startup_mesh57_skeleton_check.pyok (0.2s),tests/startup_run_ps1_check.pyok (14.5s),tests/startup_run_sh_check.pyFAIL (0.1s),tests/startup_sampler_heap_check.pyok (0.1s),tests/startup_smem_address_check.pyok (0.1s),tests/startup_stream_fetch_check.pyok (0.3s),tests/startup_stream_interface_check.pyok (0.3s),tests/startup_translator_check.pyok (0.8s)sccache: 122/351 hits; wall122.9sAI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T20:00:39Z.
Win2025 WARP receipt for head
9038e453after updating with main.Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit
9038e45328200f654fb280df129f740c508eec2e:--preset dev):cmake --build --preset dev --target d3d12_xs_device_obj d3d12_xs_shim -- -j 2):45.52s(configure2.34s)/O2(--preset release):cmake --build --preset release --target d3d12_xs_device_obj d3d12_xs_shim -- -j 2):111.13s(configure2.55s)tests/mesh_heap_sampler_check.pyok (1.0s),tests/startup_buffer_address_check.pyok (0.6s),tests/startup_compute_request_offline_check.pyok (0.6s),tests/startup_compute_translate_check.pyok (0.4s),tests/startup_flat_param_check.pyok (0.2s),tests/startup_gfx2hlsl_add_sub_co_check.pyok (0.2s),tests/startup_gfx2hlsl_add_sub_co_ci_check.pyok (0.2s),tests/startup_gfx2hlsl_bit_alu_check.pyok (1.0s),tests/startup_gfx2hlsl_capture_check.pyok (1.3s),tests/startup_gfx2hlsl_cmp16_check.pyok (0.3s),tests/startup_gfx2hlsl_compare_dst_check.pyok (0.1s),tests/startup_gfx2hlsl_compute_check.pyok (0.6s),tests/startup_gfx2hlsl_cselect_mask_check.pyok (0.1s),tests/startup_gfx2hlsl_cvt_alu_check.pyok (0.2s),tests/startup_gfx2hlsl_f32_lg_check.pyok (2.0s),tests/startup_gfx2hlsl_f32_nan_check.pyok (6.3s),tests/startup_gfx2hlsl_getpc_refusal_check.pyok (0.1s),tests/startup_gfx2hlsl_handler_arity_check.pyok (0.1s),tests/startup_gfx2hlsl_image_arity_check.pyok (0.1s),tests/startup_gfx2hlsl_image_get_lod_check.pyok (0.1s),tests/startup_gfx2hlsl_int_med3_check.pyok (0.1s),tests/startup_gfx2hlsl_join_symbols_check.pyok (0.2s),tests/startup_gfx2hlsl_loop_constants_check.pyok (0.1s),tests/startup_gfx2hlsl_mask_overwrite_check.pyok (0.1s),tests/startup_gfx2hlsl_operand_count_check.pyok (0.3s),tests/startup_gfx2hlsl_pack_absdiff_check.pyok (3.2s),tests/startup_gfx2hlsl_readfirstlane_recompute_check.pyok (0.1s),tests/startup_gfx2hlsl_readfirstlane_uniform_check.pyok (0.1s),tests/startup_gfx2hlsl_refusals_check.pyok (0.5s),tests/startup_gfx2hlsl_root_check.pyok (0.8s),tests/startup_gfx2hlsl_sad_check.pyok (0.3s),tests/startup_gfx2hlsl_saveexec_same_reg_check.pyok (0.1s),tests/startup_gfx2hlsl_scalar_arity_check.pyok (0.2s),tests/startup_gfx2hlsl_scalar_brev_fold_check.pyok (0.1s),tests/startup_gfx2hlsl_scalar_m0_check.pyok (0.1s),tests/startup_gfx2hlsl_sdwa_cvt_check.pyok (0.3s),tests/startup_gfx2hlsl_sgpr_spill_check.pyok (0.1s),tests/startup_gfx2hlsl_shift64_check.pyok (0.1s),tests/startup_gfx2hlsl_smem_offset_check.pyok (0.1s),tests/startup_gfx2hlsl_sop2_complement_check.pyok (6.8s),tests/startup_gfx2hlsl_sopk_accum_check.pyok (0.9s),tests/startup_gfx2hlsl_sopk_signext_check.pyok (0.1s),tests/startup_gfx2hlsl_swap_check.pyok (0.3s),tests/startup_gfx2hlsl_vopc_sdwa_check.pyok (4.3s),tests/startup_gfx2hlsl_waitcnt_depctr_check.pyok (0.1s),tests/startup_gfx2hlsl_watch_check.pyok (2.5s),tests/startup_image_2d_array_check.pyok (0.2s),tests/startup_image_3d_check.pyok (0.1s),tests/startup_image_gather_lz_check.pyok (0.6s),tests/startup_image_load_d16_check.pyok (0.9s),tests/startup_image_sample_a16_check.pyok (0.2s),tests/startup_image_sample_b_check.pyok (0.1s),tests/startup_image_sample_c_check.pyok (0.2s),tests/startup_image_sample_l_check.pyok (0.2s),tests/startup_image_sample_lod_check.pyok (0.8s),tests/startup_inline_sampler_check.pyok (0.2s),tests/startup_mesh57_skeleton_check.pyok (0.2s),tests/startup_run_ps1_check.pyok (35.9s),tests/startup_run_sh_check.pyskipped (0.1s),tests/startup_sampler_heap_check.pyok (0.2s),tests/startup_shader_cache_rekey_check.pyok (2.7s),tests/startup_smem_address_check.pyok (0.1s),tests/startup_stream_fetch_check.pyok (0.2s),tests/startup_stream_interface_check.pyok (0.2s),tests/startup_translator_check.pyok (0.6s)sccache: 94/215 hits; wall260.3sAI-assisted; commands listed were executed locally.