Skip to content

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

Backlog

Description

@microwave805

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 (Recompiler: a float v_cmpx through #1426's lowering keeps lanes outside EXEC out #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 recomp: enable whole-wave branches and dead-SCC elision in split wave64 #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 (Recompiler: allow inline-constant and NULL mask operands in the per-lane form #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.

Activity

  1. throny commented on Nov 29, 2026

    @throny
    Collaborator

    Status for triage: the V_CMP_* / EXEC half of this issue is fixed on main by #2503 (merged today): bridge() in gpu/recomp/lower.rs now reads EXEC for every bridged V_CMP* and masks the result with it, and a_bridged_v_cmp_clears_inactive_lanes_outside_exec covers it (1 passed on main 24d21a41, RTX 4080 16 GB, 621.40). What is still open here is the NULL-destination half, which is #2117; its bridge() hunk was written against an older base and may need a rebase.

    AI use: Claude Code (Claude Fable 5.2) checked the code, ran the test and wrote this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions