Per-lane: a mask last written in a per-lane loop leaves the code behind it unguarded (lane_masks) #3440

Open
opened 2026-11-29 17:20:57 +00:00 by throny · 0 comments
throny commented 2026-11-29 17:20:57 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by throny on 2026-11-29T17:20:57Z.


Found while reviewing local work on the per-lane analysis (C56 on #3011). On main 0fe06eb0, gpu/recomp/lane_masks.rs leaves an instruction without its EXEC guard when the mask that narrows EXEC was last written inside a per-lane loop.

Program (wave64, the encodings the module's tests use):

     s_mov_b64   s[0:1], exec
L:   v_cmp ...   vcc                 ; per lane
     v_add_nc_u32 v4, v4, v5
     s_andn2_b64 exec, exec, vcc     ; lanes whose bit is set leave the loop
     s_cbranch_execnz L
     s_or_b64    exec, exec, s[0:1]  ; restore
     s_and_saveexec_b64 s[2:3], vcc
     v_mov_b32   ...                 ; pc 28
     s_endpgm

Console: after the loop VCC holds the compare of the last round only, so it has bits just for the lanes that were still in the loop in that round. s_and_saveexec narrows EXEC to those lanes, and the v_mov runs only there.

Per-lane form: every lane keeps its own last compare bit, and every lane left the loop with that bit set. The summary lists no guard for pc 28 (guarded is empty), so the v_mov runs in every lane, also in those the console leaves out.

Test that shows it (add to lane_masks.rs mod tests; fails on main with left: []):

#[test]
fn a_mask_from_a_per_lane_loop_still_guards_what_follows() {
    let r = run(&[
        ("S_MOV_B64", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 126)]),
        CMP,
        ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 4), ("SRC0", V + 4), ("VSRC1", 5)]),
        ("S_ANDN2_B64", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 126), ("SSRC1", 106)]),
        ("S_CBRANCH_EXECNZ", "ENC_SOPP", &[("SIMM16", 0xFFFC)]),
        RESTORE,
        ("S_AND_SAVEEXEC_B64", "ENC_SOP1", &[("SDST", 2), ("SSRC0", 106)]),
        MOV3,
        ENDPGM,
    ]);
    assert_eq!(r.unwrap().guarded, vec![28], "the v_mov at pc 28 needs its guard");
}

Run here: cargo test -p xcl --lib -- lane_masks, RTX 4080 16 GB, Windows 11: the assertion fails with left: [].

Effect: only with XCL_PER_LANE=1 (off by default). A program of this shape would write a register in lanes the console skips. Whether the retail title has such a program I have not checked.

What I think the cause is (reading, not verified further): a mask written in a loop body where lanes leave one by one is treated after the loop as each lane's own bit, although on the console the register is one wave-wide value from the last round. Such a mask would have to be refused, or guarded as unknown, when it is read after the loop.

Not tested: a GPU run of this program in both forms; other shapes (a mask from one side of an if read after the join may have the same problem).

AI use: Claude Code; a Claude Opus 5.5 review found the shape, a Claude Fable 5.2 agent wrote and ran the test, Claude Fable 5.2 wrote this report.

<!-- greyfinch-sync:ivall/xcl:issue:6681747869 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/3440). Originally posted by [throny](https://github.com/throny) on 2026-11-29T17:20:57Z. --- Found while reviewing local work on the per-lane analysis (C56 on #3011). On `main` `0fe06eb0`, `gpu/recomp/lane_masks.rs` leaves an instruction without its EXEC guard when the mask that narrows EXEC was last written inside a per-lane loop. **Program** (wave64, the encodings the module's tests use): ``` s_mov_b64 s[0:1], exec L: v_cmp ... vcc ; per lane v_add_nc_u32 v4, v4, v5 s_andn2_b64 exec, exec, vcc ; lanes whose bit is set leave the loop s_cbranch_execnz L s_or_b64 exec, exec, s[0:1] ; restore s_and_saveexec_b64 s[2:3], vcc v_mov_b32 ... ; pc 28 s_endpgm ``` **Console:** after the loop VCC holds the compare of the last round only, so it has bits just for the lanes that were still in the loop in that round. `s_and_saveexec` narrows EXEC to those lanes, and the `v_mov` runs only there. **Per-lane form:** every lane keeps its own last compare bit, and every lane left the loop with that bit set. The summary lists no guard for pc 28 (`guarded` is empty), so the `v_mov` runs in every lane, also in those the console leaves out. **Test that shows it** (add to `lane_masks.rs` `mod tests`; fails on `main` with `left: []`): ```rust #[test] fn a_mask_from_a_per_lane_loop_still_guards_what_follows() { let r = run(&[ ("S_MOV_B64", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 126)]), CMP, ("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 4), ("SRC0", V + 4), ("VSRC1", 5)]), ("S_ANDN2_B64", "ENC_SOP2", &[("SDST", 126), ("SSRC0", 126), ("SSRC1", 106)]), ("S_CBRANCH_EXECNZ", "ENC_SOPP", &[("SIMM16", 0xFFFC)]), RESTORE, ("S_AND_SAVEEXEC_B64", "ENC_SOP1", &[("SDST", 2), ("SSRC0", 106)]), MOV3, ENDPGM, ]); assert_eq!(r.unwrap().guarded, vec![28], "the v_mov at pc 28 needs its guard"); } ``` Run here: `cargo test -p xcl --lib -- lane_masks`, RTX 4080 16 GB, Windows 11: the assertion fails with `left: []`. **Effect:** only with `XCL_PER_LANE=1` (off by default). A program of this shape would write a register in lanes the console skips. Whether the retail title has such a program I have not checked. **What I think the cause is (reading, not verified further):** a mask written in a loop body where lanes leave one by one is treated after the loop as each lane's own bit, although on the console the register is one wave-wide value from the last round. Such a mask would have to be refused, or guarded as unknown, when it is read after the loop. Not tested: a GPU run of this program in both forms; other shapes (a mask from one side of an if read after the join may have the same problem). AI use: Claude Code; a Claude Opus 5.5 review found the shape, a Claude Fable 5.2 agent wrote and ran the test, Claude Fable 5.2 wrote this report.
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#3440
No description provided.