Shaders: reuse lane guards for unchanged EXEC within a block

An instruction guard depends on its EXEC and the reachability of its block. Keep the most recent result, including an elided guard, rather than repeating the same BDD queries for consecutive vector instructions. Scalar instructions still bypass guard selection.

Cover EXEC changes across and within a branch with a synthetic shader. Add a CPU-only guard_runs benchmark for unmasked, short, and long masked runs.

(cherry picked from commit 81b81df351437c50f4321286c725de51664c7b1b)
This commit is contained in:
Devin authored and local committed 2026-11-25 01:02:58 -06:00
1 parent cd82c63c62
commit f084578994
3 files changed
+96 -2

No files matched your search

+44 -2
View File
@@ -792,6 +792,9 @@ impl<'a> Lanes<'a> {
.iter()
.enumerate()
.map(|(n, block)| {
// Reachability is fixed within the block. Vector instructions
// with the same EXEC therefore need the same guard.
let mut last_guard = None;
block
.insts
.iter()
@@ -801,8 +804,16 @@ impl<'a> Lanes<'a> {
| (_, Fields::Sop { .. } | Fields::Sopp { .. } | Fields::Smem { .. }) => {
None
}
(&Some(e), _) if b.implies(cond[n], e) => None,
(&Some(e), _) => Some(b.given(e, cond[n])),
(&Some(e), _) => {
if let Some((prev, guard)) = last_guard
&& prev == e
{
return guard;
}
let guard = (!b.implies(cond[n], e)).then(|| b.given(e, cond[n]));
last_guard = Some((e, guard));
guard
}
})
.collect()
})
@@ -1577,6 +1588,37 @@ mod tests {
assert!(matches!(s.bools.support(g)[..], [Atom::Lane(_)]));
}
#[test]
fn guards_follow_exec_and_block_reachability() {
let p = build(
&[
0x7DA4_0080, // v_cmpx_eq_u32 exec, 0, v0
0x7E02_0280, // v_mov_b32 v1, 0
0x7E04_0280, // v_mov_b32 v2, 0
0xBF88_0005, // s_cbranch_execz -> end
0x7E06_0280, // v_mov_b32 v3, 0
0x7DA4_0880, // v_cmpx_eq_u32 exec, 0, v4
0x7E06_0280, // v_mov_b32 v3, 0
0xBE82_0480, // s_mov_b64 s[2:3], 0
0x7E0A_0280, // v_mov_b32 v5, 0
S_ENDPGM,
],
true,
);
let s = structure(&p).unwrap();
let lane = |block: usize, i: usize| {
Atom::Lane(p.blocks[block].insts[i].write(Reg::S(EXEC)).unwrap())
};
let a = s.guards[0][1].unwrap();
let b = s.guards[1][2].unwrap();
assert_eq!(s.bools.node(a), Some((lane(0, 0), Bool::FALSE, Bool::TRUE)));
assert_eq!(s.guards[0], [None, Some(a), Some(a), None]);
// The fallthrough already implies the first compare. Only the second
// compare needs testing, and the scalar move never needs a guard.
assert_eq!(s.bools.node(b), Some((lane(1, 1), Bool::FALSE, Bool::TRUE)));
assert_eq!(s.guards[1], [None, None, Some(b), None, Some(b)]);
}
#[test]
fn divergent_loop_exits_per_lane() {
let p = build(