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

Open
opened 2026-11-24 15:12:18 +00:00 by microwave805 · 1 comment
microwave805 commented 2026-11-24 15:12:18 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by microwave805 on 2026-11-24T15:12:18Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6584982125 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/1360). Originally posted by [microwave805](https://github.com/microwave805) on 2026-11-24T15:12:18Z. --- 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.
tiagonogueiraa-bit commented 2026-11-24 16:06:59 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by tiagonogueiraa-bit on 2026-11-24T15:17:22Z.


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).

<!-- greyfinch-sync:ivall/xcl:comment:7135773821 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/1360#issuecomment-7135773821). Originally posted by [tiagonogueiraa-bit](https://github.com/tiagonogueiraa-bit) on 2026-11-24T15:17:22Z. --- 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).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl#1350
No description provided.