Skip to content

Recompiler: basic blocks, post-dominators and control dependence (#336) - #341

Open
emirduran707 wants to merge 1 commit into
ivall:mainfrom
emirduran707:wave64-cfg
Open

emirduran707 wants to merge 1 commit into
ivall:mainfrom
emirduran707:wave64-cfg

Conversation

@emirduran707

@emirduran707 emirduran707 commented Nov 23, 2026 •

Copy link
Copy Markdown

Part of #336. Preparation for classifying wave64 VS / PS by whether their result depends on how a 32-lane GPU cuts their waves: that needs to know which blocks run because of a wave-wide vote. No behaviour change: nothing outside the tests calls it yet.

What is in it

  • gpu/recomp/cfg.rs (new): Cfg – a program's basic blocks built from upper_lanes::successors, each block's immediate post-dominator (Cooper, Harvey and Kennedy's iteration on the reversed graph, one exit after every block that ends the program) and the branches it is control-dependent on (Ferrante, Ottenstein and Warren). A successor from which no path ends the program counts as the exit (so a branch into a loop with no way out decides both sides), and every block of such a region depends on the branch that leads into it. A program with an indirect jump gives no graph; a block from which no path ends the program has no post-dominator.
  • gpu/recomp/upper_lanes.rs: successors is pub(super), unchanged otherwise.
  • Tests in cfg.rs: an if, an if / else, a loop (control-dependent on its own branch), a loop with no way out, an indirect jump, and the two branches into a region with no way out from the review below (both fail without the fix).

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.31044.16; Ryzen 5 5600, 32 GB; Windows 11 Pro 26300; rustc 1.100.0 x86_64-pc-windows-msvc; DXC 1.9.2602.17; debug for the suite, release for the runs.
  • cargo test --workspace -- --test-threads=4 at f7fd232: 683 passed, 0 failed, 41 ignored; no SKIPPED lines.
  • Over all 26 698 programs of a retail shader pack (a local test, not committed; release, one thread; at 4e6156c, before the review fix): no panic; 195 programs with an indirect jump (no graph); at most 15 995 blocks in one program; at most 15 ms for one program's graph, post-dominators and control dependence.

Regression run

The gpu: line names a virtual display adapter of this machine (what #281 / #282 / #285 fix); XCL's log names backend 'real' – AMD Radeon RX 9060 XT (16188 MiB VRAM). Nothing in the runtime calls the new code, so this only shows the build is unchanged in use.

main:

xcl-smoke v1
commit: a6f5f7a74cc620c1f88a9e3669a633d10c3c8212
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1382 (PresentX 1382)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14751 MB

PR:

xcl-smoke v1
commit: f7fd232a3bd4bfa35313cb124124727a12816008
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1408 (PresentX 1409)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14739 MB

Not tested

AI use

  • Claude Code with Claude Opus 5.5: wrote the code, the tests and this description; the runs above were made on my machine.

@matheusfermiano-source

Copy link
Copy Markdown

Tested

Arch Linux x86-64, rustc 1.97.1, head 4e6156c9f9af533010a10956f6b5f6b49bff82a8. I used a standalone rustc --edition 2021 --test harness with the PR's cfg.rs, upper_lanes.rs, and RDNA decoder modules, plus the unchanged ext_encoding() helper extracted from emit.rs. This exercises the portable analysis in isolation.

  • The PR's 5 CFG tests passed. A separate path-set oracle agreed with the immediate post-dominators and dependence sets on all 1,024 forward-edge graphs with five nodes.
  • A second synthetic test encoded and decoded a real EXECZ branch with successors 2 (a closed loop) and 1 (exit). The loop edges are 2 → 4 → 2, and block 3 is an unreachable exit. Computed post-dominators were [Block(1), Exit, None, Exit, None], and control dependence was [[], [], [0], [], []]. Asserting dependence on branch 0 for both loop blocks fails at block 4 with the empty set. Both blocks execute only on the loop arm in this fixture. This is the case where the walk stops at the first PostDom::None, despite the comment about blocks on paths that never exit.
  • Total for the standalone CFG filter, including the two additional cases: 6 passed, 1 failed, 18 unrelated tests filtered out.

Not tested

Windows workspace build, DXC, GPU execution and retail titles. The shader-like input was hand-constructed for static analysis and was never executed.

AI use

Codex (GPT-6; exact variant unavailable) wrote the standalone test harness and this report.

@0xKaji

0xKaji commented Nov 24, 2026

Copy link
Copy Markdown

Reviewed commit 4e6156c statically on Linux (not built or executed on Windows; assisted by T3 Code + Gemini 4 Neon).

Clean preparation PR overall, and the CHK intersect termination invariant holds on the exit-reachable subgraph. However, there are two concrete bugs when a branch has a successor that cannot reach any program exit (PostDom::None):

  1. post_dominators (crates/xcl/src/gpu/recomp/cfg.rs:142-147) skips non-exiting successors and produces false post-dominators (dropping real control dependences):

    • if idom[p] == usize::MAX { continue; } conflates two cases: a reverse-reachable node not yet processed on the first pass (seen[p] == true, which must be skipped), and a node that can never reach exit (!seen[p]).
    • Skipping !seen[p] causes a branch whose other arm exits to get that exiting successor as its ipdom, falsely implying every path from the branch goes through it.
    • Concrete 3-block repro:
      • Block 0: S_CMP_EQ_U32 s0, s1; S_CBRANCH_SCC0 +2 → succ[0] = [2, 1]
      • Block 1: S_ADD_U32 s0, s0, 1; S_BRANCH -2 (0xFFFE) → succ[1] = [1] (infinite loop)
      • Block 2: S_ENDPGM → succ[2] = []
    • Current result: post_dominators() returns [PostDom::Block(2), PostDom::None, PostDom::Exit], so control_dependence breaks immediately on first = 2 (PostDom::Block(2) == stop) and returns cd[2] == [] — even though branch 0 decides whether block 2 executes at all.
    • Fix: in the rpred(v) loop, treat !seen[p] as exit (i.e. let p = if !seen[p] { exit } else { p };) before checking idom[p] == usize::MAX. That yields ipdom[0] == PostDom::Exit and cd[2] == [0] while leaving normal loops (seen[p] == true) and purely non-exiting regions (!seen[v]) unchanged.
  2. control_dependence (crates/xcl/src/gpu/recomp/cfg.rs:165-166, 185-188) stops at the first PostDom::None block, contradicting its doc comment:

    • The doc comment states: "A block on a path that never ends the program counts as dependent on every branch that leads to it."
    • Because match ipdom[run] breaks immediately on PostDom::None, only the immediate successor first of branch a is recorded; blocks downstream of first in the non-terminating region are never visited.
    • Concrete 4-block repro:
      • Block 0: S_CMP_EQ_U32 s0, s1; S_CBRANCH_SCC0 +3 → succ[0] = [3, 1]
      • Block 1: S_MOV_B32 s2, 1 → succ[1] = [2]
      • Block 2: S_ADD_U32 s0, s0, 1; S_BRANCH -2 (0xFFFE) → succ[2] = [2]
      • Block 3: S_ENDPGM → succ[3] = []
    • Even with (1) fixed (ipdom = [Exit, None, None, Exit]), control_dependence sets cd[1] = [0] and breaks, leaving cd[2] == [] instead of [0].
    • Fix: since every block reachable from a PostDom::None block is itself PostDom::None, when ipdom[run] == PostDom::None a forward DFS/worklist over self.succ (skipping blocks that already contain a in cd) will mark all downstream non-terminating blocks as documented.

@PHeitor1

PHeitor1 commented Nov 24, 2026 •

Copy link
Copy Markdown

Tested 4e6156c merged onto main 3516080.

RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native, Graphics Tools + XGameRuntime installed, so the debug-layer tests run), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*.

Pass: 675 passed, 0 failed, 41 ignored. SKIPPED: only the 2 no wave64 tests (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware). Build clean.

Run and written with Claude Code (Claude Opus 5.5).

…ll#336)

Preparation for classifying wave64 VS / PS by whether their result depends
on how a 32-lane GPU cuts their waves: that needs to know which blocks run
because of a wave-wide vote. No behaviour change: nothing calls it yet.

cfg::Cfg builds a program's basic blocks from upper_lanes::successors (now
shared), and gives each block's immediate post-dominator (Cooper, Harvey
and Kennedy's iteration on the reversed graph, with one exit after every
block that ends the program) and the branches it is control-dependent on
(Ferrante, Ottenstein and Warren). A program with an indirect jump has no
graph; a block from which no path ends the program has no post-dominator.

Measured over all 26 698 programs of a retail shader pack (release, one
thread): no panic, 195 with an indirect jump, at most 15 995 blocks in one
program, at most 15 ms for one program's graph, post-dominators and
control dependence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@emirduran707

Copy link
Copy Markdown
Author

Thanks both – you were right, and it was the same gap: a successor from which no path ends the program. Fixed in f7fd232 (squashed, rebased on a6f5f7a):

  • post_dominators: in the intersection, a successor that cannot reach the exit (!seen) counts as the exit, as @0xKaji suggested. A branch into a loop with no way out now gets PostDom::Exit instead of its other side.
  • control_dependence: on reaching a PostDom::None block, every block reachable from it (none of them can end the program either) is marked as depending on the branch, as the doc comment said.
  • Your two repros are tests now (a_branch_into_a_loop_with_no_way_out_decides_both_sides: [Exit, None, Exit] / [[], [0], [0]]; every_block_of_a_side_with_no_way_out_depends_on_the_branch: [Exit, None, None, Exit] / [[], [0], [0], [0]]). Both fail with the old code (checked) and pass now. @matheusfermiano-source, your EXECZ loop fixture is the second shape; its loop blocks now get the dependence on branch 0.

Suite at f7fd232 on an RX 9060 XT: 683 passed, 0 failed, 41 ignored, no SKIPPED; new smoke blocks are in the description. Thanks @PHeitor1 for the Blackwell run.

AI use: Claude Code with Claude Opus 5.5 made the fix, ran these tests and wrote this reply.

@PHeitor1

Copy link
Copy Markdown

Retested the new head f7fd232 merged onto main 2997c9a.

RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native, Graphics Tools + XGameRuntime installed), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*.

Pass: 683 passed, 0 failed, 41 ignored (675 on 4e6156c earlier; the difference is the new tests here and on main). SKIPPED: only the 2 no wave64 tests. Build clean.

Run and written with Claude Code (Claude Opus 5.5).

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