Repository navigation
cfg_structure / rdna2spirv: unwrap tautological %pc guards in structured CFGs (ms_wave_info_s3 AMD crash, SHADER-STRUCTURED-PC-UNWRAP) - #808
Conversation
qhukz13
left a comment
There was a problem hiding this comment.
Checked 185220e (Linux, CPU only, no game files). Merges cleanly with current main.
The moved logic holds up:
tests/test_cfg_structure.py: OK (18,228 random graphs).- Under
--wave64-emulatethe pass now runs twice (incapped_tree, then inwave64_emulate_optat line 1501). On 19,689 random structured graphs a secondunwrap_known_pc_guardsnever changed the tree, so that is safe. - Five hand-written mutations of
unwrap_known_pc_guards, run againsttest_cfg_structure.py: four are caught (unwrapping whengis only one of the possible values, keepinghpossible after a loop, dropping the skip path at a guard exit, ignoringskip_pcin the sibling-guard merge). One survives: starting a loop body fromcur_pcinstead ofcur_pc | {h}. That looks equivalent if a structured loop is always entered with%pc == h; a line saying so, or a graph showing otherwise, would settle it.
The thing I would ask for: until now this pass only ran under --wave64-emulate; with the capped_tree call it rewrites the tree of every structured shader on native wave32/wave64 too, and capped_tree also feeds needs_barrier_rounds and the MAX_STRUCTURED_DEPTH fallback. The verification covers ms_wave_info_s3 and the three cs_barrier_* fixtures on hardware plus the unit tests, but not the corpus. Per AGENTS.md ("count over the whole corpus"), could you add before/after numbers over the game's listings: translated and spirv-val-valid counts, how many modules' SPIR-V changes, and how many shaders change their barrier-rounds or depth-fallback decision? A shader that leaves barrier rounds because its barrier is now top-level is the intended effect, but the count would show it is only that.
|
Thanks for the thorough review and mutation testing, @qhukz13!
|
6c52f6c to
cc7b947
Compare
|
checked:
AI-assisted (Claude Code). |
|
Follow-up to my check above: I built AI-assisted (Claude Code). |
|
Tested cc7b947 on NVIDIA GeForce RTX 3080 Ti Laptop (Ampere, subgroup 32..32), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 Pro (build 26300): pass, the 32-wide side of @towade's RDNA4 regression check Runtime built at this head with
|
|
Reviewed cc7b947 applied onto main 66d4f5a, and ran it on an AMD Radeon RX 7900 XT. The GPU results are below. Before that, a CPU-only repro of a miscompile the move to the native path introduces. The unwrap drops a guard when the last block ends in a conditional branch
Hand-written wave32 compute shader (no game data).
How exposed this is: all 392 listings in the repository end in GPU, RX 7900 XTAMD Radeon RX 7900 XT (RDNA3, gfx1100), AMD driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5, test programs from a
On this card and driver, No game files. |
|
Great catch, @adrianmjim — thank you both for the RX 7900 XT verification and for spotting that! Fixed in
Verified locally on
|
|
checked: the corpus before/after count asked for above, native path, no
So nothing is lost or newly rejected. The change touches most of the structured modules. The 36 modules that move from What the bytes change is (opcode counts, not a GPU check):
The falloff case from the review above. I checked the corpus for it: all 26,503 listings end in Not checked: the corpus with AI-assisted (Claude Code). |
b630a9b to
f4d3338
Compare
…red CFGs (ms_wave_info_s3 AMD LLPC crash)
cfg_structure.structure() wraps post-dominating join blocks after a diamond or loop in a ('guard', g, ...) node even when %pc == g is tautologically true on every incoming path. wave64_emulate_opt._unwrap_known_pc_guards already unwraps these guards for --wave64-emulate, which is why ms_wave_info_s3 passed on 32-wide NVIDIA GPUs while crashing with 0xC0000005 on native wave64 AMD RDNA2 Windows (driver 24.10.24, LLPC) where blk2's OpControlBarrier stayed trapped inside a conditional OpBranchConditional.
Move unwrap_known_pc_guards into tools/cfg_structure.py (reused by wave64_emulate_opt with zero duplication) and apply it in rdna2spirv.capped_tree() so both assemble() and needs_barrier_rounds() see post-dominating join blocks at top level.
SHADER-STRUCTURED-PC-UNWRAP
Co-Authored-By: Gemini 4 Neon <noreply@google.com>
…nown_pc_guards Co-Authored-By: Gemini 4 Neon <noreply@google.com>
…ches A conditional branch in the last basic block (no fallthrough block, so len(raw_edges[b]) == 1) emits OpSelect(%cond, tgt, len(blocks)) and can leave %pc at either tgt or cfg_structure.END. Compute exact per-block out_pcs in rdna2spirv._block_out_pcs (matching wave64_emulate_opt), pass it through capped_tree() into both cfg_structure.structure() (so single-target fall-off-the-end blocks also keep their dominated target guard) and unwrap_known_pc_guards(), and pass compact_out_pcs into cfg_structure.structure() in wave64_emulate_opt. Add unit and semantic regression tests covering both the loop back-edge repro reported in review and the non-loop dominated-target variant. Co-Authored-By: Gemini 4 Neon <noreply@google.com>
f4d3338 to
8e5cf5a
Compare
|
Rechecked at The fix works. My earlier repro now matches the ISA in all three cases, structured and The With One path the fix does not reach yet: exec-fold regionsWith exec folds on, as they are by default, the same shader still stores 0x3333 and 0x4444 with
This is the same missing
Passing The repro drives GPU, RX 7900 XTAMD Radeon RX 7900 XT (RDNA3, gfx1100), AMD driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5. Test programs come from a
The one failure is No game files. |
…ng conditional branch A block with len(raw_edges[u]) == 1 ending in a conditional branch (no fallthrough block, so falling through writes %pc = len(blocks) = END) cannot safely sit inside a folded s_cbranch_execz/nz region whose exit unconditionally sets %pc = skip. Reject such blocks during the region walk in find_local_exec_folds (matching the existing ot succ[u] check) and add semantic regression tests covering both s_cbranch_scc1 X and s_cbranch_scc1 skip in structured and --dispatch-loop --wave64-emulate modes. Co-Authored-By: Gemini 4 Neon <noreply@google.com>
|
Thanks again for the thorough recheck and full RX 7900 XT pass, @adrianmjim! Since the exec-fold sibling fix was only two lines, I folded it into this PR as
Verified locally on
|
|
Rechecked at Both wave64 shaders from my last comment, the "dominated X" form and the "to skip" form, under
That holds in the structured and dispatch emulate forms, and for the "to skip" variant: Thanks for picking that up. |
|
Merged. Thank you, @whoalin1, and thanks to @adrianmjim for the fall-off bug, and to @qhukz13 and @towade for the corpus counts.
AI-assisted (Claude Code, Claude Opus 5.5). |
Claim: #14 (comment) (
SHADER-STRUCTURED-PC-UNWRAP)Problem
On AMD's proprietary Windows Vulkan driver (
24.10.24, LLPC, AMD Ryzen 5 7600 RDNA2 iGPU, native wave64),ms_wave_info_s3segfaults (0xC0000005) duringvkCreateGraphicsPipelines.In
ms_wave_info_s3.asm,blk0conditionally skipsblk1(MSG_GS_ALLOC_REQ), and bothblk0andblk1converge atblk2(s_barrier).cfg_structure.structure()wrapsblk2in("guard", 2, [("block", 2)]), emittingOpBranchConditionalaround%blk2'sOpControlBarriereven though%pc == 2is tautologically true on every incoming path.--wave64-emulate(e.g., 32-wide NVIDIA GPUs),wave64_emulate_opt._unwrap_known_pc_guardsalready unwrapped that guard, soms_wave_info_s3passed there.rdna2spirv.capped_tree()usedcfg_structure.structure()directly without unwrapping tautological%pcguards, leavingOpControlBarrierinside conditional control flow and crashing AMD Windows LLPC (0xC0000005).Fix
tools/cfg_structure.py: addunwrap_known_pc_guards(tree, out_pcs, entry_pc=0)(moved fromtools/wave64_emulate_opt.py, keepingcfg_structure.structure()unchanged for callers like_drop_sink_block).tools/wave64_emulate_opt.py: delegate_unwrap_known_pc_guardsdirectly tocfg_structure.unwrap_known_pc_guards.tools/rdna2spirv.py(capped_tree): applycfg_structure.unwrap_known_pc_guardsto structured trees before computing nesting depth, so bothassemble()andneeds_barrier_rounds()see post-dominating merge/barrier blocks at top level.tests/test_cfg_structure.py: addtest_unwrap_known_pc_guards(single-arm if-then, diamond, loop exit, early-exit guard preservation) and differentially checkunwrap_known_pc_guardsacross all 18,228 random reducible graphs × 6 seeds.tests/test_translate.py: assert%blk2inms_wave_info_s3is emitted without a conditional%pcguard before itsOpControlBarrier.docs/design.md: document the pass in the Control flow contract row.Verification (before/after on
main47c651174,LEONIDA_VK_VALIDATION=1)LEONIDA_PC_TAIL_UBO=1):main):ms_wave_info_s3FAIL (vk_harnesscrash0xC0000005)ms_wave_info_s3PASS (4096/4096 pixels exact, 0 validation errors);cs_barrier_divergent,cs_barrier_early_end,cs_barrier_loop3/3 PASSms_wave_info_s3PASS (4096/4096 pixels exact, 0 validation errors);cs_barrier_*3/3 PASSpython -I tests/test_cfg_structure.py— 18,228 random reducible graphs × 6 seeds (both structured and unwrapped trees matchwalk()), plus shape & deep-chain testspython -I tests/test_runner_calls_all_tests.py— 3/3 OKpython -I tests/test_cfg_run_limit.py— 4/4 OKpytest tests/test_translate.py— 669 passedunittest discover -s tests/semantic -p test_wave64_emulate_opt.py— 31/31 OKunittest discover -s tests/semantic -p "test_*barrier*.py"— 53/53 OKunittest discover -s tests/semantic -p "test_exec*.py"— 2/2 OKunittest discover -s tests/semantic -p "test_ms_*.py"— 6/6 OKpython -I tests/test_text_write_line_endings.py— 3/3 OKpython -I scripts/check_no_game_data.py ...— clean (rc=0)AI-assisted (Gemini 4 Neon).