Shaders: fold empty loop latches containing only dead phis

(cherry picked from commit be3202de393b71c9385f74b587256b01b48f7f63)
This commit is contained in:
Tex authored and local committed 2026-11-25 01:03:02 -06:00
1 parent 482ee84e2c
commit dc155d2fb9
1 file changed
+40 -1
+40 -1
View File
@@ -727,7 +727,7 @@ impl Module {
let blk = &self.blocks[b];
if let (Some(Term::Br(h)), true, [p]) = (
blk.term,
blk.phis.is_empty() && blk.body.is_empty(),
blk.phis.iter().all(|&i| !live[i as usize]) && blk.body.is_empty(),
&preds[b][..],
) && pos[h] < pos[b]
&& !succs(*p).contains(&h)
@@ -1196,6 +1196,45 @@ mod tests {
assert_eq!(live, live_by_passes(&m));
}
#[test]
fn empty_latch_with_dead_phis_is_folded() {
// Two identical loops except one has an unread phi in its empty latch;
// both must fold the latch and emit identical bitcode.
let build = |with_dead_phi: bool| {
let mut m = Module::default();
let i32t = m.int(32);
let void = m.void();
let fty = m.func_ty(void, vec![i32t]);
let f = m.function("sink", fty, Some(Attr::NoUnwind), false);
let main_ty = m.func_ty(void, vec![]);
m.function("main", main_ty, None, true);
let (entry, head, body, latch, exit) = (
m.new_block(),
m.new_block(),
m.new_block(),
m.new_block(),
m.new_block(),
);
let (z, one, ten) = (m.i32(0), m.i32(1), m.i32(10));
m.terminate(entry, Term::Br(head));
let i = m.phi(head, i32t);
m.terminate(head, Term::Br(body));
let next = m.bin(body, Bin::Add, i, one);
let cond = m.cmp(body, pred::IULT, next, ten);
m.terminate(body, Term::CondBr(cond, latch, exit));
if with_dead_phi {
let dead = m.phi(latch, i32t);
m.set_phi(dead, vec![(next, body)]);
}
m.terminate(latch, Term::Br(head));
m.set_phi(i, vec![(z, entry), (next, latch)]);
let _ = m.call(exit, f, vec![next]);
m.terminate(exit, Term::Ret);
m.write()
};
assert_eq!(build(false), build(true));
}
#[test]
fn successors_of_each_terminator() {
assert!(successors(None).is_empty());