Per-lane: DxilIr::ballot and DxilIr::read_lane do not update self.tails after spin_exchange, emitting invalid DXIL PHI predecessors #1889

Open
opened 2026-11-26 05:08:37 +00:00 by github-bot · 2 comments
Owner

@⁠microwave805 on GitHub · 2026-11-24 22:37 UTC · original

Static audit on main @ 689f2db found that DxilIr::ballot and DxilIr::read_lane do not update self.tails[self.cur] after calling Lw split-wave64 helpers that create basic blocks via spin_exchange.

Where

  • crates/xcl/src/gpu/recomp/lower_dxil.rs:229-237 (DxilIr::ballot)
  • crates/xcl/src/gpu/recomp/lower_dxil.rs:281-288 (DxilIr::read_lane)
  • Compare crates/xcl/src/gpu/recomp/lower_dxil.rs:277-278 (DxilIr::foreign, which does self.tails[self.cur] = self.lw.cur();)
  • crates/xcl/src/gpu/dxil/lanes.rs:271-368 (Lw::spin_exchange)

Root cause

DxilIr tracks each Ir block's starting DXIL block (self.blocks[b]) and current tail DXIL block (self.tails[b]) so DxilIr::incoming() (lower_dxil.rs:138) wires PHI predecessors to self.tails[pred] and DxilIr::at() (lower_dxil.rs:161) resumes emission at self.tails[block].

In split wave64 compute shaders with more than 64 threads per group (Partner::Spin), self.lw.ballot(c) and self.lw.read_lane_uniform(v, lane) both call self.lw.exchange(m) → self.lw.spin_exchange(...), which creates multiple new DXIL basic blocks (guarded(go, ...) → guarded(first, ...) → spin loop lp / ex / joins) and advances self.lw.cur() to the final join block.

Because neither DxilIr::ballot nor DxilIr::read_lane updates self.tails[self.cur] = self.lw.cur():

  1. Outgoing br / br_if terminates the post-spin_exchange join block, while DxilIr::incoming() records the pre-exchange self.tails[pred] as the PHI predecessor in successor blocks, producing malformed DXIL (PHI node entries do not match predecessors!).
  2. Any subsequent DxilIr::at(cur) would position the builder back in the already-terminated pre-exchange block.

Trigger

Any wave64 compute shader lowered via lower_dxil::compute_split (XCL_DIRECT_DXIL=1 XCL_PER_LANE=1 on a wave32 GPU) with threads > 64 (e.g. [128, 1, 1]) that performs a whole-wave ballot (e.g. s_bcnt1_i32_b64, s_ff1_i32_b64, mask-to-number read) or v_readlane_b32 / v_readfirstlane_b32 in a block feeding a PHI at a successor block.

Fix direction

Update self.tails[self.cur] = self.lw.cur(); in both DxilIr::ballot and DxilIr::read_lane whenever self.lw.split is true (matching DxilIr::foreign), and add a unit test compiling a [128, 1, 1] split wave64 compute shader with v_readlane_b32 / s_bcnt1_i32_b64 feeding a successor PHI through DXIL validation.

AI use

  • Antigravity (Gemini 4 Neon): static code audit on main @ 689f2db and issue drafting.
**@⁠microwave805** on GitHub · 2026-11-24 22:37 UTC · [original](https://github.com/xcl-dev/xcl/issues/1889) Static audit on `main @ 689f2db` found that `DxilIr::ballot` and `DxilIr::read_lane` do not update `self.tails[self.cur]` after calling `Lw` split-wave64 helpers that create basic blocks via `spin_exchange`. ### Where - `crates/xcl/src/gpu/recomp/lower_dxil.rs:229-237` (`DxilIr::ballot`) - `crates/xcl/src/gpu/recomp/lower_dxil.rs:281-288` (`DxilIr::read_lane`) - Compare `crates/xcl/src/gpu/recomp/lower_dxil.rs:277-278` (`DxilIr::foreign`, which does `self.tails[self.cur] = self.lw.cur();`) - `crates/xcl/src/gpu/dxil/lanes.rs:271-368` (`Lw::spin_exchange`) ### Root cause `DxilIr` tracks each `Ir` block's starting DXIL block (`self.blocks[b]`) and current tail DXIL block (`self.tails[b]`) so `DxilIr::incoming()` (`lower_dxil.rs:138`) wires PHI predecessors to `self.tails[pred]` and `DxilIr::at()` (`lower_dxil.rs:161`) resumes emission at `self.tails[block]`. In split wave64 compute shaders with more than 64 threads per group (`Partner::Spin`), `self.lw.ballot(c)` and `self.lw.read_lane_uniform(v, lane)` both call `self.lw.exchange(m)` → `self.lw.spin_exchange(...)`, which creates multiple new DXIL basic blocks (`guarded(go, ...)` → `guarded(first, ...)` → spin loop `lp` / `ex` / joins) and advances `self.lw.cur()` to the final join block. Because neither `DxilIr::ballot` nor `DxilIr::read_lane` updates `self.tails[self.cur] = self.lw.cur()`: 1. Outgoing `br` / `br_if` terminates the post-`spin_exchange` join block, while `DxilIr::incoming()` records the **pre-exchange** `self.tails[pred]` as the PHI predecessor in successor blocks, producing malformed DXIL (`PHI node entries do not match predecessors!`). 2. Any subsequent `DxilIr::at(cur)` would position the builder back in the already-terminated pre-exchange block. ### Trigger Any wave64 compute shader lowered via `lower_dxil::compute_split` (`XCL_DIRECT_DXIL=1 XCL_PER_LANE=1` on a wave32 GPU) with `threads > 64` (e.g. `[128, 1, 1]`) that performs a whole-wave ballot (e.g. `s_bcnt1_i32_b64`, `s_ff1_i32_b64`, mask-to-number read) or `v_readlane_b32` / `v_readfirstlane_b32` in a block feeding a PHI at a successor block. ### Fix direction Update `self.tails[self.cur] = self.lw.cur();` in both `DxilIr::ballot` and `DxilIr::read_lane` whenever `self.lw.split` is true (matching `DxilIr::foreign`), and add a unit test compiling a `[128, 1, 1]` split wave64 compute shader with `v_readlane_b32` / `s_bcnt1_i32_b64` feeding a successor PHI through DXIL validation. ### AI use - Antigravity (Gemini 4 Neon): static code audit on `main @ 689f2db` and issue drafting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:08:37 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 11:15 UTC · original

Taking #1889: update self.tails after spin_exchange in DxilIr::ballot and DxilIr::read_lane and add regression coverage proving valid DXIL PHI predecessors.

**@⁠oni-swr** on GitHub · 2026-11-25 11:15 UTC · [original](https://github.com/xcl-dev/xcl/issues/1889#issuecomment-7160807459) Taking #1889: update self.tails after spin_exchange in DxilIr::ballot and DxilIr::read_lane and add regression coverage proving valid DXIL PHI predecessors.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 12:02 UTC · original

Releasing our claim on #1889: open PR #1928 already covers this.

**@⁠oni-swr** on GitHub · 2026-11-25 12:02 UTC · [original](https://github.com/xcl-dev/xcl/issues/1889#issuecomment-7161836414) Releasing our claim on #1889: open PR #1928 already covers this.
This discussion has been locked. Commenting is limited to contributors.
No description provided.