nak: Optimize jumps to fall-through if possible

This saves 15 instructions on the compute shader in Sascha Willems'
computecloth example.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26473>
This commit is contained in:
M Henning 2023-12-02 15:12:08 -05:00 committed by Marge Bot
parent b2420fae4b
commit 586c34b19c

View file

@ -118,9 +118,28 @@ fn rewrite_cfg(func: &mut Function) {
let _ = std::mem::replace(&mut func.blocks, builder.as_cfg());
}
/// Replace jumps to the following block with fall-through
fn opt_fall_through(func: &mut Function) {
for i in 0..func.blocks.len() - 1 {
let remove_last_instr = match func.blocks[i].branch() {
Some(b) => match b.op {
Op::Bra(OpBra { target }) => target == func.blocks[i + 1].label,
_ => false,
},
None => false,
};
if remove_last_instr {
func.blocks[i].instrs.pop();
}
}
}
impl Function {
pub fn opt_jump_thread(&mut self) {
jump_thread(self);
if jump_thread(self) {
opt_fall_through(self);
}
}
}