Recompiler: term() misclassifies S_SETPC_B64 as Term::Next, breaking multi-block ES+GS Mode::Geometry shaders #1360

Open
opened 2026-11-26 04:51:57 +00:00 by github-bot · 1 comment
Owner

@⁠microwave805 on GitHub · 2026-11-24 15:12 UTC · original

Checked on main (930e8b4).

Problem

In crates/xcl/src/gpu/rdna2_spec.rs:577, S_SETPC_B64 has flags: 5 (F_BRANCH | F_INDIRECT), not F_TERMINATOR (8). In Mode::Geometry, the ES→GS handoff S_SETPC_B64 has d.far_target == None, so rdna2::branch_target(d) returns None. term() (crates/xcl/src/gpu/recomp/mod.rs:564-574) therefore falls through to Term::Next:

fn term(c: &Ctx, start: u64, end: u64) -> Term {
    let Some(d) = insts_in(c, start, end).last() else { return Term::Next };
    match (rdna2::branch_target(d), d.inst.name) {
        (Some(t), n) if n.starts_with("S_CBRANCH") => Term::Cond(t),
        (Some(t), "S_BRANCH" | "S_SETPC_B64") => Term::Jump(t),
        (Some(_), _) => Term::Other,
        (None, "S_ENDPGM") => Term::End,
        (None, _) if d.inst.flags & rdna2::F_TERMINATOR != 0 => Term::Other,
        _ => Term::Next,
    }
}

In addition, edges (mod.rs:1446) only collects rdna2::branch_target(d), omitting the ES→GS handoff edge into gs_start. As a result, whenever either ES or GS has ≥ 2 basic blocks, structured_span(c, 0, hi) (mod.rs:1078-1101) treats the S_SETPC_B64 block as plain fallthrough, sees no outside edge entering gs_start, and collapses S_SETPC_B64 plus leading GS blocks into a single emit_structured region under if (pc == 0u). Inside emit_structured (mod.rs:1040-1043), the Flow::JumpExpr("select(xcl_es_phase, 0xFFFFFFFFu, {s}u)") / Flow::Jump(s) / Flow::End returned by emit.rs:864-872 for S_SETPC_B64 is silently discarded.

Minimal trigger

Any Mode::Geometry(g) shader with g.gs_start = Some(0x10) where ES or GS has ≥ 1 branch (e.g., ES = 0x00..0x10 ending in S_SETPC_B64; GS = 0x10..0x18 with S_CBRANCH_SCC0 -> 0x20, 0x18..0x20, 0x20..0x24 S_ENDPGM), translated with wave64 = true, split_wave64 = true (NVIDIA / wave32 GPUs):

  • structured_span(c, 0, u64::MAX) returns Some(0x20), emitting if (pc == 0u) { ... pc = 32u; } with no if (pc == 16u) block.
  • In split-wave64 Geometry mode, the ES phase (xcl_entry_pc = 0u, all 64 lanes) falls directly into GS code before GroupMemoryBarrierWithGroupSync(), racing on LDS on 64 lanes; then the GS phase (xcl_entry_pc = 16u, lower 32 lanes) has no matching if (pc == 16u) block and emits 0 vertices/primitives.
  • In native wave64 Geometry mode, an early S_SETPC_B64 or ES-only (gs_start = None) falls through instead of jumping/terminating.

Suggested fix

Return Term::Other in term() for unresolved indirect branches (d.inst.flags & (rdna2::F_TERMINATOR | rdna2::F_INDIRECT) != 0), and include geometry S_SETPC_B64 transitions in edges so structured_span never merges across ES/GS boundaries or swallows gs_start.


AI use: Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on 930e8b4 before opening.

**@⁠microwave805** on GitHub · 2026-11-24 15:12 UTC · [original](https://github.com/xcl-dev/xcl/issues/1360) Checked on `main` (`930e8b4`). ### Problem In `crates/xcl/src/gpu/rdna2_spec.rs:577`, `S_SETPC_B64` has `flags: 5` (`F_BRANCH | F_INDIRECT`), not `F_TERMINATOR` (`8`). In `Mode::Geometry`, the ES→GS handoff `S_SETPC_B64` has `d.far_target == None`, so `rdna2::branch_target(d)` returns `None`. `term()` (`crates/xcl/src/gpu/recomp/mod.rs:564-574`) therefore falls through to `Term::Next`: ```rust fn term(c: &Ctx, start: u64, end: u64) -> Term { let Some(d) = insts_in(c, start, end).last() else { return Term::Next }; match (rdna2::branch_target(d), d.inst.name) { (Some(t), n) if n.starts_with("S_CBRANCH") => Term::Cond(t), (Some(t), "S_BRANCH" | "S_SETPC_B64") => Term::Jump(t), (Some(_), _) => Term::Other, (None, "S_ENDPGM") => Term::End, (None, _) if d.inst.flags & rdna2::F_TERMINATOR != 0 => Term::Other, _ => Term::Next, } } ``` In addition, `edges` (`mod.rs:1446`) only collects `rdna2::branch_target(d)`, omitting the ES→GS handoff edge into `gs_start`. As a result, whenever either ES or GS has ≥ 2 basic blocks, `structured_span(c, 0, hi)` (`mod.rs:1078-1101`) treats the `S_SETPC_B64` block as plain fallthrough, sees no outside edge entering `gs_start`, and collapses `S_SETPC_B64` plus leading GS blocks into a single `emit_structured` region under `if (pc == 0u)`. Inside `emit_structured` (`mod.rs:1040-1043`), the `Flow::JumpExpr("select(xcl_es_phase, 0xFFFFFFFFu, {s}u)")` / `Flow::Jump(s)` / `Flow::End` returned by `emit.rs:864-872` for `S_SETPC_B64` is silently discarded. ### Minimal trigger Any `Mode::Geometry(g)` shader with `g.gs_start = Some(0x10)` where ES or GS has ≥ 1 branch (e.g., ES = `0x00..0x10` ending in `S_SETPC_B64`; GS = `0x10..0x18` with `S_CBRANCH_SCC0 -> 0x20`, `0x18..0x20`, `0x20..0x24` `S_ENDPGM`), translated with `wave64 = true, split_wave64 = true` (NVIDIA / wave32 GPUs): - `structured_span(c, 0, u64::MAX)` returns `Some(0x20)`, emitting `if (pc == 0u) { ... pc = 32u; }` with no `if (pc == 16u)` block. - In split-wave64 Geometry mode, the ES phase (`xcl_entry_pc = 0u`, all 64 lanes) falls directly into GS code **before** `GroupMemoryBarrierWithGroupSync()`, racing on LDS on 64 lanes; then the GS phase (`xcl_entry_pc = 16u`, lower 32 lanes) has no matching `if (pc == 16u)` block and emits 0 vertices/primitives. - In native wave64 Geometry mode, an early `S_SETPC_B64` or ES-only (`gs_start = None`) falls through instead of jumping/terminating. ### Suggested fix Return `Term::Other` in `term()` for unresolved indirect branches (`d.inst.flags & (rdna2::F_TERMINATOR | rdna2::F_INDIRECT) != 0`), and include geometry `S_SETPC_B64` transitions in `edges` so `structured_span` never merges across ES/GS boundaries or swallows `gs_start`. --- **AI use:** Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on `930e8b4` before opening.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:51:57 +00:00
Author
Owner

@⁠tiagonogueiraa-bit on GitHub · 2026-11-24 15:17 UTC · original

claiming: I'll reproduce the issue's minimal ES+GS program on the CPU (translation only), then make the unresolved S_SETPC_B64 end its block for the structurer and add the ES→GS edge, so gs_start gets its own pc block. It comes with a CPU test on the HLSL that fails on main. PR within the hour or two.

Written with Claude Code (Claude Opus 5.5).

**@⁠tiagonogueiraa-bit** on GitHub · 2026-11-24 15:17 UTC · [original](https://github.com/xcl-dev/xcl/issues/1360#issuecomment-7135773821) claiming: I'll reproduce the issue's minimal ES+GS program on the CPU (translation only), then make the unresolved `S_SETPC_B64` end its block for the structurer and add the ES→GS edge, so `gs_start` gets its own `pc` block. It comes with a CPU test on the HLSL that fails on main. PR within the hour or two. Written with Claude Code (Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.