shader: preserve one-path SGPRs and uniform loop counters for v_readfirstlane_b32 #2301
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2309"
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 stan495 on 2026-11-25T11:39:41Z.
Refs #2274 (Part A:
v_readfirstlaneproof across joins and loops).Summary
Preserve proven uniform SGPRs across branch joins for
v_readfirstlane_b32in two CFG-verified cases:tthat are not written on any non-dominating path reachingtor any path reachingtat/aftert.SCCbranch conditions (cond_uniform = cond in ('scc0', 'scc1') and self.scc_uniform), validated via a fixed-point pass inStage.translate()against non-uniform scalar writes (bad_sgprs) and divergents_cbranch_*regions (div_targetsover dominators and post-dominators). Constants (const) are still cleared at clobbered joins so loop counters emit dynamic HLSL scalar reads.What remains refused (
v_readfirstlane_b32 requires a proven uniform source):s_cbranch_*back-edges (e.g.s_cbranch_vccnz), divergent forward branch arms writing the candidate scalar before reconvergence (e.g.else-arm write unders_cbranch_vccz), lane-mask writes (s_mov_b32 sN, vcc_lo), unmodelled SGPR destinations (v_add_co_u32_e64,s_movreld,s_cselecton stalescc), and joins overwriting the scalar insidetentative_keptwhen later invalidated.Also fixes
Stage.one()/Stage.forget_writes()ordering so in-place uniform scalar updates (e.g.s_add_u32 s3, s3, 1) inspects3's prior uniformity mark before unmodelled-write invalidation.Sources and semantics
v_readfirstlane_b32,SOPPs_cbranch_scc0/s_cbranch_scc1/s_cbranch_vccz/s_cbranch_vccnz,SOPC,SOP1,SOP2,VOP1,VOP2) and LLVM 15 AMDGPU gfx1013 wave64 semantics (all lanes executeUniformscalar/branch decisions identically, while divergentvcc/execbranches split active lane masks until immediate post-dominator reconvergence).Verification
Branch commit:
b0c83b06(rebased ontod8b345cb).Environment: Windows 11 25H2 (build
26200.9974), MSVC 19.44.35228.0, D3D12 debug layer enabled (debug_errors=0 debug_warnings=0).CTest (
devpreset)Result: 6 / 6 passed (
100% tests passed, 0 tests failed out of 6):startup_compute_readfirstlane(controls, machine-code refusals, lowering check): passedstartup_gfx2hlsl_loop_constants: passed (PASS)startup_gfx2hlsl_readfirstlane_uniform: passed (0 failed)startup_gfx2hlsl_readfirstlane_recompute: passed (0 failed; covers one-path scalars, uniform loop counters inside/after loops, and divergent/mask refusals)startup_compute_readfirstlane_hardware(adapter=AMD Radeon RX 7900 XTX,vendor=1002 device=744c,driver=32.0.32029.2010,debug_layer=enabled): passed (compared_words=320 mismatches=0 debug_errors=0 debug_warnings=0)dropped-one-path:word=0 expected=0000008b actual=00000019dropped-loop-read:word=0 expected=0000008b actual=0000007fdropped-post-loop:word=0 expected=0000008b actual=00000087startup_compute_readfirstlane_warp(adapter=Microsoft Basic Render Driver,vendor=1414 device=008c,driver=10.0.26100.9818,debug_layer=enabled): passed (compared_words=320 mismatches=0 debug_errors=0 debug_warnings=0, same 3 pinned negative controls rejected)Pure-Python & repo checks
tests/startup_gfx2hlsl_compute_check.py,tests/startup_translator_check.py,tests/docs_links_check.py(18 files, 448 links),tests/bidi_controls_check.py(1600 files),tests/form_coverage_check.py,git diff --check: all passed.python3 -B tests/startup_compute_readfirstlane_check.py --llvm-mc llvm-mc-15(and all other gfx1013 checks).Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T11:42:58Z.
Review: COMMENTED
Review of head
b0c83b064dcf0c401abc065e1155e108388d98b6(Linux-only; no Windows run, CI for Windows was still pending).Commands run locally in a worktree of this head:
git diff --stat origin/main...HEAD: 6 files, +589/-69python3 tests/startup_compute_readfirstlane_check.py: passed ("one-path, loop-counter and refusal checks passed")python3 tests/startup_gfx2hlsl_readfirstlane_recompute_check.py: "0 failed"python3 tests/docs_links_check.py: passed (448 links)python3 tests/bidi_controls_check.py: passedgit diff --check origin/main...HEAD: cleangfx2hlsl.pyreplacedforce_unproven.update(new_bad)withpass(the feedback of loop-written SGPRs into the next pass). The new check did not report a failure: the fixpoint loop never converged and the run hung until I stopped it. So the mutation is detected, but only as a hang, not as an assertion. Reverted afterwards.Findings
tools/startup/gfx2hlsl.py:1120-1121):join_writes = self.join_writes(branches)is assigned twice in a row. Drop one.tools/startup/gfx2hlsl.py, thewhile Truepass loop that ends near line 1257): it ends becauseforce_unprovenandforce_atonly grow, and the mutation above shows a hang if that growth is lost. A small pass cap that raisesUnsupportedwould turn a future regression into a clear refusal rather than a stuck translation.hasattr(self, 'defined_uniform_s')/'_uniform_written'/'bad_sgprs'guards inset_sand the write path suggest these fields are only sometimes present. Initializing them inStage.__init__would remove the guards.Clean-room, neutral title, MIT-only content, mergeable: all fine. The new test is wired into CMake and the Windows workflow. Windows WARP/D3D12 coverage comes from the added
--dispatchCTest entries; I did not run those here.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-25T11:46:19Z.
Addressed all three nits in
0395dd92:join_writes = self.join_writes(branches)assignment inStage.translate().while True:with a boundedfor _ in range(max(4, len(self.ins) + 2)):loop that refuses viaUnsupported('v_readfirstlane_b32 uniformity proof exceeded pass limit')(or records inrefusals) on exhaustion instead of hanging. Re-tested theforce_unproven.update(new_bad)->passmutation in a temporary copy; it now terminates immediately withUnsupported: v_readfirstlane_b32 uniformity proof exceeded pass limit.Noneclass defaults onStage(defined_uniform_s = None,bad_sgprs = None,_uniform_written = None) and initialized freshset()s inStage.__init__, replacinghasattr(...)checks inset_sandonewithis not None(soStage.__new__(Stage)unit probes continue working cleanly without sharing mutable defaults).Re-ran locally after
0395dd92:ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_(readfirstlane_(uniform|recompute)|loop_constants)|startup_compute_readfirstlane(_(warp|hardware))?)$' -V: 6 / 6 passed (startup_compute_readfirstlane_hardwareonAMD Radeon RX 7900 XTXdriver32.0.32029.2010andstartup_compute_readfirstlane_warponMicrosoft Basic Render Driver10.0.26100.9818,debug_layer=enabled,0mismatches/errors/warnings, all 3 GPU negative controls rejected at expected word 0).tests/startup_translator_check.py,tests/docs_links_check.py(448 links),tests/bidi_controls_check.py(1600 files),git diff --check: all passed.Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-25T11:58:17Z.
Rebased onto current
main(2f5c54c0, including #2292 and #2310) -> headbeba8498(mergeable: MERGEABLE):Stage.joined_registers(with_cfg=True)(dropping the duplicatecfg_join_writes()helper) and separated forward split clobbers (forward_splits) from loop back-edges soJOIN_RFLintests/startup_gfx2hlsl_join_symbols_check.pystays refused while uniform loop counters (LOOP_RFL) lower dynamically (s23 = s23 + 0x00000001u;->s24 = s23;) with no constant folding.Stage.translate()'s bounded loop strictly on(bad_sgprs, bad_at_target)withStage.__init__'sforce_joinedhandling remaining outside sodescriptor_scc_readsruns beforeback_forceoverrides.Re-verified locally at
beba8498:ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_(readfirstlane_(uniform|recompute)|loop_constants|join_symbols(_(warp|hardware))?|compare_dst)|startup_compute_readfirstlane(_(warp|hardware))?)$' -V: 8 / 8 passed (includingstartup_gfx2hlsl_join_symbols_{warp,hardware}andstartup_compute_readfirstlane_{warp,hardware}onAMD Radeon RX 7900 XTX32.0.32029.2010and WARP10.0.26100.9818,debug_layer=enabled,0errors/warnings).readfirstlane_uniform,readfirstlane_recompute,loop_constants,join_symbols,compare_dst,compute_readfirstlane,compute,translator,docs_links448 links,bidi_controls1607 files,form_coverage,git diff --check): all passed.