Mesa (main): nir_opt_dead_cf: Remove dead ifs

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Jan 7 05:38:04 UTC 2022


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

Author: Jesse Natalie <jenatali at microsoft.com>
Date:   Tue Jan  4 08:39:47 2022 -0800

nir_opt_dead_cf: Remove dead ifs

An if that looks like:
if (x) { } else { }
That has no phis following it is dead. Currently these are only
removed by peephole select, but that means that 'x' is considered
used until that pass is run, which can make it difficult to apply
sane lowering in the case where loading 'x' requires complex or
expensive transformations, but 'x' is *really* unused.

Reviewed-by: Jason Ekstrand <jason at jlekstrand.net>
Reviewed-by: Alyssa Rosenzweig <alyssa at rosenzweig.io>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14400>

---

 src/compiler/nir/nir_opt_dead_cf.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/compiler/nir/nir_opt_dead_cf.c b/src/compiler/nir/nir_opt_dead_cf.c
index 04009139007..adb713674aa 100644
--- a/src/compiler/nir/nir_opt_dead_cf.c
+++ b/src/compiler/nir/nir_opt_dead_cf.c
@@ -174,7 +174,7 @@ def_only_used_in_cf_node(nir_ssa_def *def, void *_node)
 }
 
 /*
- * Test if a loop node is dead. Such nodes are dead if:
+ * Test if a loop or if node is dead. Such nodes are dead if:
  *
  * 1) It has no side effects (i.e. intrinsics which could possibly affect the
  * state of the program aside from producing an SSA value, indicated by a lack
@@ -192,7 +192,7 @@ def_only_used_in_cf_node(nir_ssa_def *def, void *_node)
 static bool
 node_is_dead(nir_cf_node *node)
 {
-   assert(node->type == nir_cf_node_loop);
+   assert(node->type == nir_cf_node_loop || node->type == nir_cf_node_if);
 
    nir_block *after = nir_cf_node_as_block(nir_cf_node_next(node));
 
@@ -286,11 +286,15 @@ dead_cf_block(nir_block *block)
 {
    nir_if *following_if = nir_block_get_following_if(block);
    if (following_if) {
-      if (!nir_src_is_const(following_if->condition))
-         return false;
+      if (nir_src_is_const(following_if->condition)) {
+         opt_constant_if(following_if, nir_src_as_bool(following_if->condition));
+         return true;
+      }
 
-      opt_constant_if(following_if, nir_src_as_bool(following_if->condition));
-      return true;
+      if (node_is_dead(&following_if->cf_node)) {
+         nir_cf_node_remove(&following_if->cf_node);
+         return true;
+      }
    }
 
    nir_loop *following_loop = nir_block_get_following_loop(block);



More information about the mesa-commit mailing list