[Mesa-dev] [PATCH] nir: evaluate if condition uses inside the if branches

Timothy Arceri timothy.arceri at collabora.com
Tue Jan 17 06:06:08 UTC 2017


All helped shaders are fromm Unreal Engine 4 besides one shader from
Dirt Showdown.

shader-db results BDW:

total instructions in shared programs: 12985186 -> 12985112 (-0.00%)
instructions in affected programs: 26523 -> 26449 (-0.28%)
helped: 43
HURT: 0

total cycles in shared programs: 246851202 -> 246850312 (-0.00%)
cycles in affected programs: 252032 -> 251142 (-0.35%)
helped: 38
HURT: 9
---
 src/compiler/nir/nir_opt_if.c | 46 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 41 insertions(+), 5 deletions(-)

diff --git a/src/compiler/nir/nir_opt_if.c b/src/compiler/nir/nir_opt_if.c
index 68dacea..57f5c86 100644
--- a/src/compiler/nir/nir_opt_if.c
+++ b/src/compiler/nir/nir_opt_if.c
@@ -200,8 +200,22 @@ opt_peel_loop_initial_if(nir_loop *loop)
    return true;
 }
 
+static void
+replace_if_condition_use_with_const(nir_src *use, nir_instr*instr,
+                                    unsigned nir_boolean, void *mem_ctx)
+{
+   /* Create const */
+   nir_load_const_instr *load = nir_load_const_instr_create(mem_ctx, 1, 32);
+   load->value.u32[0] = NIR_TRUE;
+   nir_instr_insert_before(instr,  &load->instr);
+
+   /* Rewrite use to use const */
+   nir_src new_src = nir_src_for_ssa(&load->def);
+   nir_instr_rewrite_src(use->parent_instr, use, new_src);
+}
+
 static bool
-opt_if_cf_list(struct exec_list *cf_list)
+opt_if_cf_list(struct exec_list *cf_list, void *mem_ctx)
 {
    bool progress = false;
    foreach_list_typed(nir_cf_node, cf_node, node, cf_list) {
@@ -211,14 +225,33 @@ opt_if_cf_list(struct exec_list *cf_list)
 
       case nir_cf_node_if: {
          nir_if *nif = nir_cf_node_as_if(cf_node);
-         progress |= opt_if_cf_list(&nif->then_list);
-         progress |= opt_if_cf_list(&nif->else_list);
+         progress |= opt_if_cf_list(&nif->then_list, mem_ctx);
+         progress |= opt_if_cf_list(&nif->else_list, mem_ctx);
+
+         /* Evaluate any uses of the if condition inside the if branches */
+         nir_foreach_use_safe(use_src, nif->condition.ssa) {
+            nir_block *first_then = nir_if_first_then_block(nif);
+            if (use_src->parent_instr->block->index > first_then->index) {
+               nir_block *first_else = nir_if_first_else_block(nif);
+               if (use_src->parent_instr->block->index < first_else->index) {
+                  nir_instr *instr = nir_block_first_instr(first_then);
+                  replace_if_condition_use_with_const(use_src, instr,
+                                                      NIR_TRUE, mem_ctx);
+               } else if (use_src->parent_instr->block->index <
+                          nir_if_last_else_block(nif)->index) {
+                  nir_instr *instr = nir_block_first_instr(first_then);
+                  replace_if_condition_use_with_const(use_src, instr,
+                                                      NIR_FALSE, mem_ctx);
+               }
+            }
+
+         }
          break;
       }
 
       case nir_cf_node_loop: {
          nir_loop *loop = nir_cf_node_as_loop(cf_node);
-         progress |= opt_if_cf_list(&loop->body);
+         progress |= opt_if_cf_list(&loop->body, mem_ctx);
          progress |= opt_peel_loop_initial_if(loop);
          break;
       }
@@ -240,7 +273,10 @@ nir_opt_if(nir_shader *shader)
       if (function->impl == NULL)
          continue;
 
-      if (opt_if_cf_list(&function->impl->body)) {
+      nir_metadata_require(function->impl, nir_metadata_block_index);
+
+      void *mem_ctx = ralloc_parent(function->impl);
+      if (opt_if_cf_list(&function->impl->body, mem_ctx)) {
          nir_metadata_preserve(function->impl, nir_metadata_none);
 
          /* If that made progress, we're no longer really in SSA form.  We
-- 
2.9.3



More information about the mesa-dev mailing list