Per-lane: bridged non-CMPX V_CMP_* ignore EXEC (corrupting inactive lane bits) and writing to NULL (SDST = 125) clobbers EXEC #2458

Open
opened 2026-11-26 05:31:21 +00:00 by github-bot · 0 comments
Owner

@⁠microwave805 on GitHub · 2026-11-25 19:30 UTC · original

Static audit on main @ c7e1799 found two critical correctness bugs in the per-lane direct DXIL lowering (XCL_PER_LANE=1) following #1876 and #1864. Part of #1269.

1. [Critical] Bridged non-CMPX V_CMP_* instructions ignore EXEC and corrupt inactive lane bits

  • Where: crates/xcl/src/gpu/recomp/lower.rs:1782-1784, 1817-1823, crates/xcl/src/gpu/recomp/lane_masks.rs:295-298
  • Root cause:
    • lane_masks::is_vector (lane_masks.rs:295-298) excludes all V_CMP* (!n.starts_with("V_CMP")), so V_CMP_* PCs are never added to self.guarded.
    • Native valu() (lower.rs:825-854) handles only plain U32/I32 V_CMP* (ANDing with EXEC at lines 849-850); every float (F32/F16/F64), 64-bit (I64/U64), 16-bit (I16/U16), V_CMP_CLASS_*, and VOP3-modified/SDWA compare falls through to bridge().
    • Commit fe78c81 (#1876) only set old_exec for cmpx (V_CMPX*) at lower.rs:1782. For non-CMPX bridged V_CMP_*, old_exec is None, !self.guarded.contains(&d.pc) is true, so guard is constant true (Lw::write_mask sees act() == true) and lines 1819-1823 write raw cond unmasked by EXEC.
  • Trigger: Under narrowed EXEC (e.g. compute wave_branches == true from #2203, or any guarded region / PS helper lanes):
    v_cmp_lt_u32 vcc, v0, 16       ; lanes 0..15 true
    s_and_saveexec_b32 s0, vcc     ; EXEC = 0x0000_FFFF
    v_cmp_gt_f32 vcc, v1, 0.0      ; bridged: v1 = 0.0 in lanes 0..15, v1 = 1.0 in lanes 16..31
    s_cbranch_vccz L_skip
    
    Hardware sets vcc = 0x0000_0000 (inactive lanes forced to 0) and branches to L_skip; per-lane lowering produces vcc = 0xFFFF_0000 and takes the wrong branch.
  • Fix: Mask every bridged V_CMP* (not only V_CMPX*) with EXEC when !in_guarded_run:
    let old_exec = if name.starts_with("V_CMP") && !in_guarded_run { Some(self.read_bit(b, EXEC)?) } else { None };
    

2. [Critical] Writing to NULL (SDST = 125) clobbers EXEC (126) in per-lane lowering and corrupts lane_masks

  • Where: crates/xcl/src/gpu/recomp/lower.rs:403-424 (write_num, write_bit), 577-578 (S_GETPC_B64), 669-670 (S_MOV_B64), 691-692 (S_CSELECT_B64), 973; crates/xcl/src/gpu/recomp/lane_masks.rs:492-496, 551, 581
  • Root cause: On RDNA2, SGPR 125 is NULL (reads 0, discards writes; standard idiom for s_and_b64 null, vcc, exec to set SCC without clobbering any SGPR, or VOP3 v_cmp_* null, ...). Commit 15fff3b (#1864) added 125 reads and saveexec guards sd != 125, but write_num and write_bit do not ignore r == 125, and 64-bit writes unconditionally write sd + 1 = 126 (EXEC):
    • Wave64: write_bit(b, 125, bit) (e.g. s_and_b64 null, vcc, exec or VOP3 v_cmp_eq_u32 null, ...) writes Var::S(126) = num_marker; Var::M(126) = bit_marker;, destroying EXEC and failing with "s126: a mask read as a number".
    • Wave32 (silent miscompilation): s_mov_b64 null, s[0:1] or s_cselect_b64 null, ... calls write_num(b, 126, hi), silently overwriting EXEC with hi without any error!
    • lane_masks.rs:496, 551, 581: 125 < 128 stores st.regs[125] = Some(v) and a subsequent mask_src(125) reads back st.regs[125] instead of Some(FALSE).
  • Fix:
    • Return immediately when r == 125 in write_num and write_bit; guard sd != 125 around (sd, sd + 1) writes at lower.rs:577-578, 669-670, 691-692, 973.
    • In lane_masks.rs, map 125 => Some(FALSE) in mask_src and require r != 125 before writing st.regs[r].

AI use

  • Antigravity (Gemini 4 Neon): static code audit on main @ c7e1799 and issue drafting.
**@⁠microwave805** on GitHub · 2026-11-25 19:30 UTC · [original](https://github.com/xcl-dev/xcl/issues/2458) Static audit on `main @ c7e1799` found two critical correctness bugs in the per-lane direct DXIL lowering (`XCL_PER_LANE=1`) following #1876 and #1864. Part of #1269. ### 1. [Critical] Bridged non-`CMPX` `V_CMP_*` instructions ignore `EXEC` and corrupt inactive lane bits - **Where:** `crates/xcl/src/gpu/recomp/lower.rs:1782-1784, 1817-1823`, `crates/xcl/src/gpu/recomp/lane_masks.rs:295-298` - **Root cause:** - `lane_masks::is_vector` (`lane_masks.rs:295-298`) excludes all `V_CMP*` (`!n.starts_with("V_CMP")`), so `V_CMP_*` PCs are never added to `self.guarded`. - Native `valu()` (`lower.rs:825-854`) handles only plain `U32`/`I32` `V_CMP*` (ANDing with `EXEC` at lines 849-850); every float (`F32`/`F16`/`F64`), 64-bit (`I64`/`U64`), 16-bit (`I16`/`U16`), `V_CMP_CLASS_*`, and VOP3-modified/SDWA compare falls through to `bridge()`. - Commit `fe78c81` (#1876) only set `old_exec` for `cmpx` (`V_CMPX*`) at `lower.rs:1782`. For non-`CMPX` bridged `V_CMP_*`, `old_exec` is `None`, `!self.guarded.contains(&d.pc)` is `true`, so `guard` is constant `true` (`Lw::write_mask` sees `act() == true`) and lines 1819-1823 write raw `cond` unmasked by `EXEC`. - **Trigger:** Under narrowed `EXEC` (e.g. compute `wave_branches == true` from #2203, or any guarded region / PS helper lanes): ```asm v_cmp_lt_u32 vcc, v0, 16 ; lanes 0..15 true s_and_saveexec_b32 s0, vcc ; EXEC = 0x0000_FFFF v_cmp_gt_f32 vcc, v1, 0.0 ; bridged: v1 = 0.0 in lanes 0..15, v1 = 1.0 in lanes 16..31 s_cbranch_vccz L_skip ``` Hardware sets `vcc = 0x0000_0000` (inactive lanes forced to 0) and branches to `L_skip`; per-lane lowering produces `vcc = 0xFFFF_0000` and takes the wrong branch. - **Fix:** Mask every bridged `V_CMP*` (not only `V_CMPX*`) with `EXEC` when `!in_guarded_run`: ```rust let old_exec = if name.starts_with("V_CMP") && !in_guarded_run { Some(self.read_bit(b, EXEC)?) } else { None }; ``` ### 2. [Critical] Writing to `NULL` (`SDST = 125`) clobbers `EXEC` (`126`) in per-lane lowering and corrupts `lane_masks` - **Where:** `crates/xcl/src/gpu/recomp/lower.rs:403-424` (`write_num`, `write_bit`), `577-578` (`S_GETPC_B64`), `669-670` (`S_MOV_B64`), `691-692` (`S_CSELECT_B64`), `973`; `crates/xcl/src/gpu/recomp/lane_masks.rs:492-496, 551, 581` - **Root cause:** On RDNA2, SGPR `125` is `NULL` (reads `0`, discards writes; standard idiom for `s_and_b64 null, vcc, exec` to set `SCC` without clobbering any SGPR, or VOP3 `v_cmp_* null, ...`). Commit `15fff3b` (#1864) added `125` reads and `saveexec` guards `sd != 125`, but `write_num` and `write_bit` do not ignore `r == 125`, and 64-bit writes unconditionally write `sd + 1 = 126` (`EXEC`): - **Wave64:** `write_bit(b, 125, bit)` (e.g. `s_and_b64 null, vcc, exec` or VOP3 `v_cmp_eq_u32 null, ...`) writes `Var::S(126) = num_marker; Var::M(126) = bit_marker;`, destroying `EXEC` and failing with `"s126: a mask read as a number"`. - **Wave32 (silent miscompilation):** `s_mov_b64 null, s[0:1]` or `s_cselect_b64 null, ...` calls `write_num(b, 126, hi)`, silently overwriting `EXEC` with `hi` without any error! - **`lane_masks.rs:496, 551, 581`:** `125 < 128` stores `st.regs[125] = Some(v)` and a subsequent `mask_src(125)` reads back `st.regs[125]` instead of `Some(FALSE)`. - **Fix:** - Return immediately when `r == 125` in `write_num` and `write_bit`; guard `sd != 125` around `(sd, sd + 1)` writes at `lower.rs:577-578, 669-670, 691-692, 973`. - In `lane_masks.rs`, map `125 => Some(FALSE)` in `mask_src` and require `r != 125` before writing `st.regs[r]`. ### AI use - Antigravity (Gemini 4 Neon): static code audit on `main @ c7e1799` and issue drafting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:31:21 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.