Module: Mesa
Branch: main
Commit: 586c34b19c2e40cc5ddd43302fa202a4aa94a5c5
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=586c34b19c2e40cc5ddd43302fa202a4aa94a5c5

Author: M Henning <[email protected]>
Date:   Sat Dec  2 15:12:08 2023 -0500

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>

---

 src/nouveau/compiler/nak/opt_jump_thread.rs | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/nouveau/compiler/nak/opt_jump_thread.rs 
b/src/nouveau/compiler/nak/opt_jump_thread.rs
index a85eefd66ad..bc0a6025afe 100644
--- a/src/nouveau/compiler/nak/opt_jump_thread.rs
+++ b/src/nouveau/compiler/nak/opt_jump_thread.rs
@@ -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);
+        }
     }
 }
 

Reply via email to