Per-lane: unsound whole-wave predicate checks (uniform_over ignores inactive lanes, S_CBRANCH_VCCZ/NZ ANDs with EXEC, SRC_SCC 253 unchecked) #1888

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

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

Static audit on main @ 689f2db found three soundness gaps in the per-lane (XCL_PER_LANE=1) whole-wave mask predicate analysis (crates/xcl/src/gpu/recomp/lane_masks.rs and lower.rs) that cause silent control-flow inversion or per-lane divergence from RDNA2 hardware semantics. (Distinct from #1866 / Pipodi's point 2 on #1745.)

1. uniform_over ignores inactive lanes (EXEC == 0)

  • Where: crates/xcl/src/gpu/recomp/lane_masks.rs:553-562, 605-614, 829-853
  • Root cause: uniform_over(reach, e, c) checks whether re = reach & e implies c or !c, completely ignoring bits outside EXEC. Hardware scalar bitwise ops and S_CMP_EQ/LG_U32/U64 set SCC from all 32/64 bits of the mask register, including inactive lanes:
    • s_cmp_eq_u32 exec, -1; s_cbranch_scc1 (with partial EXEC, e.g. 0x0000FFFF): lane_masks.rs:558-559 sets scc = !(exec ^ TRUE) = exec. Since re = reach & exec trivially implies exec, uniform_over accepts the branch. On RDNA2 hardware, EXEC != 0xFFFFFFFF so SCC = 0 for the whole wave (branch not taken); in the per-lane lowering, active lanes see exec_bit == true (SCC = 1) and take the branch (100% inverted).
    • s_not_b32 s1, exec; s_cbranch_scc0 (or s_xor_b32 s1, exec, -1; s_cbranch_scc1): lane_masks.rs:531 sets v = !exec. Since re = reach & exec implies !(!exec), uniform_over accepts. Hardware sees s1 = 0xFFFF0000 != 0 (SCC = 1); active per-lane threads see !exec_bit == false (SCC = 0).
    • s_cmp_eq_u32 vcc, 0; s_cbranch_scc1 when vcc has bits set in inactive lanes: re => !vcc passes uniform_over, so active lanes see SCC = 1 where hardware sees vcc != 0 (SCC = 0).
  • Fix direction: Distinguish any-lane-nonzero (res != 0 / S_CMP_LG) vs all-lanes-equal (S_CMP_EQ):
    • For res != 0: per-lane SCC matches hardware SCC only if re => c (so at least one lane is 1 and every active lane is 1, provided re is satisfiable) OR !c holds wave-wide (not just over re, i.e., c == FALSE including inactive lanes).
    • For S_CMP_EQ: per-lane SCC matches hardware SCC only if c == TRUE wave-wide (all lanes including inactive ones agree) OR re => !c.

2. S_CBRANCH_VCCZ / S_CBRANCH_VCCNZ wrongly AND VCC with EXEC

  • Where: crates/xcl/src/gpu/recomp/lane_masks.rs:628-634, crates/xcl/src/gpu/recomp/lower.rs:970-978
  • Root cause: Both Lanes::cond (self.bdd.and(v, e)) and Lower::cond (self.ir.bin(Bin::And, v, e)) mask VCC with EXEC. On RDNA2 hardware, S_CBRANCH_VCCZ tests VCC == 0 across all 32/64 bits, independent of EXEC. If VCC retains set bits outside a narrowed EXEC (e.g., v_cmp before s_and_saveexec, or s_mov/s_or/s_not into VCC), VCC & EXEC == 0 makes XCL_PER_LANE take S_CBRANCH_VCCZ while hardware falls through.
  • Fix direction: Test v directly (without ANDing with e) and apply the wave-wide zero/nonzero check above.

3. Direct SRC_SCC (encoding 253) reads bypass lane_masks validation

  • Where: crates/xcl/src/gpu/recomp/lane_masks.rs:829-836, crates/xcl/src/gpu/recomp/lower.rs:433-436
  • Root cause: lane_masks.rs:829 only checks ["S_CSELECT", "S_CMOV", "S_ADDC", "S_SUBB"] for reads of a per-lane mask-op SCC. However, Lower::src() handles source encoding 253 (SRC_SCC) for any SALU/VALU instruction (s_mov_b32 s0, scc, s_add_u32 s0, s1, scc, v_mov_b32 v0, scc, v_add_nc_u32 v0, v1, scc, etc.). When SCC holds a per-lane mask bit, those instructions silently read per-lane 0/1 values instead of hardware's wave-uniform SCC — and if copied into an SGPR (s_mov_b32 s0, scc), subsequent branches on s0 are misclassified as uniform.
  • Fix direction: Check all explicit operand fields (SSRC0, SSRC1, SRC0, SRC1, SRC2, SOFFSET) for code 253 (as well as implicit OPR_SSRC_SPECIAL_SCC inputs) in lane_masks.rs:829-836.

(Note: separately, lane_masks::is_vector at lane_masks.rs:271 also excludes !n.starts_with("V_CMP"), so bridged non-X V_CMP* instructions — e.g. V_CMP_LT_F32 — pass guard = true at lower.rs:1647 just like V_CMPX* noted by @Pipodi in #1745 point 3, allowing inactive lanes to set bits in the destination mask.)

AI use

  • Antigravity (Gemini 4 Neon): static code audit on main @ 689f2db and issue drafting. Not built or run on retail game data (no game files on hand).
**@⁠microwave805** on GitHub · 2026-11-24 22:37 UTC · [original](https://github.com/xcl-dev/xcl/issues/1888) Static audit on `main @ 689f2db` found three soundness gaps in the per-lane (`XCL_PER_LANE=1`) whole-wave mask predicate analysis (`crates/xcl/src/gpu/recomp/lane_masks.rs` and `lower.rs`) that cause silent control-flow inversion or per-lane divergence from RDNA2 hardware semantics. *(Distinct from `#1866` / Pipodi's point 2 on `#1745`.)* ### 1. `uniform_over` ignores inactive lanes (`EXEC == 0`) - **Where:** `crates/xcl/src/gpu/recomp/lane_masks.rs:553-562, 605-614, 829-853` - **Root cause:** `uniform_over(reach, e, c)` checks whether `re = reach & e` implies `c` or `!c`, completely ignoring bits outside `EXEC`. Hardware scalar bitwise ops and `S_CMP_EQ/LG_U32/U64` set `SCC` from **all** 32/64 bits of the mask register, including inactive lanes: - **`s_cmp_eq_u32 exec, -1; s_cbranch_scc1` (with partial `EXEC`, e.g. `0x0000FFFF`):** `lane_masks.rs:558-559` sets `scc = !(exec ^ TRUE) = exec`. Since `re = reach & exec` trivially implies `exec`, `uniform_over` accepts the branch. On RDNA2 hardware, `EXEC != 0xFFFFFFFF` so `SCC = 0` for the whole wave (branch not taken); in the per-lane lowering, active lanes see `exec_bit == true` (`SCC = 1`) and take the branch (100% inverted). - **`s_not_b32 s1, exec; s_cbranch_scc0` (or `s_xor_b32 s1, exec, -1; s_cbranch_scc1`):** `lane_masks.rs:531` sets `v = !exec`. Since `re = reach & exec` implies `!(!exec)`, `uniform_over` accepts. Hardware sees `s1 = 0xFFFF0000 != 0` (`SCC = 1`); active per-lane threads see `!exec_bit == false` (`SCC = 0`). - **`s_cmp_eq_u32 vcc, 0; s_cbranch_scc1` when `vcc` has bits set in inactive lanes:** `re => !vcc` passes `uniform_over`, so active lanes see `SCC = 1` where hardware sees `vcc != 0` (`SCC = 0`). - **Fix direction:** Distinguish `any-lane-nonzero` (`res != 0` / `S_CMP_LG`) vs `all-lanes-equal` (`S_CMP_EQ`): - For `res != 0`: per-lane `SCC` matches hardware `SCC` only if `re => c` (so at least one lane is 1 and every active lane is 1, provided `re` is satisfiable) OR `!c` holds wave-wide (not just over `re`, i.e., `c == FALSE` including inactive lanes). - For `S_CMP_EQ`: per-lane `SCC` matches hardware `SCC` only if `c == TRUE` wave-wide (all lanes including inactive ones agree) OR `re => !c`. ### 2. `S_CBRANCH_VCCZ` / `S_CBRANCH_VCCNZ` wrongly AND `VCC` with `EXEC` - **Where:** `crates/xcl/src/gpu/recomp/lane_masks.rs:628-634`, `crates/xcl/src/gpu/recomp/lower.rs:970-978` - **Root cause:** Both `Lanes::cond` (`self.bdd.and(v, e)`) and `Lower::cond` (`self.ir.bin(Bin::And, v, e)`) mask `VCC` with `EXEC`. On RDNA2 hardware, `S_CBRANCH_VCCZ` tests `VCC == 0` across all 32/64 bits, independent of `EXEC`. If `VCC` retains set bits outside a narrowed `EXEC` (e.g., `v_cmp` before `s_and_saveexec`, or `s_mov/s_or/s_not` into `VCC`), `VCC & EXEC == 0` makes `XCL_PER_LANE` take `S_CBRANCH_VCCZ` while hardware falls through. - **Fix direction:** Test `v` directly (without ANDing with `e`) and apply the wave-wide zero/nonzero check above. ### 3. Direct `SRC_SCC` (encoding `253`) reads bypass `lane_masks` validation - **Where:** `crates/xcl/src/gpu/recomp/lane_masks.rs:829-836`, `crates/xcl/src/gpu/recomp/lower.rs:433-436` - **Root cause:** `lane_masks.rs:829` only checks `["S_CSELECT", "S_CMOV", "S_ADDC", "S_SUBB"]` for reads of a per-lane mask-op `SCC`. However, `Lower::src()` handles source encoding `253` (`SRC_SCC`) for **any** SALU/VALU instruction (`s_mov_b32 s0, scc`, `s_add_u32 s0, s1, scc`, `v_mov_b32 v0, scc`, `v_add_nc_u32 v0, v1, scc`, etc.). When `SCC` holds a per-lane mask bit, those instructions silently read per-lane `0/1` values instead of hardware's wave-uniform `SCC` — and if copied into an SGPR (`s_mov_b32 s0, scc`), subsequent branches on `s0` are misclassified as uniform. - **Fix direction:** Check all explicit operand fields (`SSRC0`, `SSRC1`, `SRC0`, `SRC1`, `SRC2`, `SOFFSET`) for code `253` (as well as implicit `OPR_SSRC_SPECIAL_SCC` inputs) in `lane_masks.rs:829-836`. *(Note: separately, `lane_masks::is_vector` at `lane_masks.rs:271` also excludes `!n.starts_with("V_CMP")`, so bridged non-`X` `V_CMP*` instructions — e.g. `V_CMP_LT_F32` — pass `guard = true` at `lower.rs:1647` just like `V_CMPX*` noted by `@Pipodi` in `#1745` point 3, allowing inactive lanes to set bits in the destination mask.)* ### AI use - Antigravity (Gemini 4 Neon): static code audit on `main @ 689f2db` and issue drafting. Not built or run on retail game data (no game files on hand).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:08:37 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.