nir/opt_peephole_select: respect selection_control when collapsing ifs
authorDaniel Schürmann <daniel@schuermann.dev>
Fri, 6 Nov 2020 22:44:06 +0000 (23:44 +0100)
committerMarge Bot <eric+marge@anholt.net>
Tue, 24 Nov 2020 08:39:35 +0000 (08:39 +0000)
Totals from 34 (0.02% of 138013) affected shaders (RAVEN):
CodeSize: 625888 -> 626336 (+0.07%); split: -0.00%, +0.08%
Instrs: 124121 -> 124229 (+0.09%); split: -0.00%, +0.09%
Cycles: 1403072 -> 1403588 (+0.04%); split: -0.01%, +0.04%
VMEM: 5308 -> 5364 (+1.06%); split: +1.07%, -0.02%
Copies: 12773 -> 12838 (+0.51%); split: -0.08%, +0.59%
Branches: 5758 -> 5801 (+0.75%); split: -0.21%, +0.96%

Reviewed-by: Rhys Perry <pendingchaos02@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7478>

src/compiler/nir/nir_opt_peephole_select.c

index 840d0fe..01efb84 100644 (file)
@@ -242,6 +242,10 @@ nir_opt_collapse_if(nir_if *if_stmt, nir_shader *shader, unsigned limit,
    if (if_stmt->cf_node.parent->type != nir_cf_node_if)
       return false;
 
+   nir_if *parent_if = nir_cf_node_as_if(if_stmt->cf_node.parent);
+   if (parent_if->control == nir_selection_control_dont_flatten)
+      return false;
+
    /* check if the else block is empty */
    if (!nir_cf_list_is_empty_block(&if_stmt->else_list))
       return false;
@@ -252,7 +256,6 @@ nir_opt_collapse_if(nir_if *if_stmt, nir_shader *shader, unsigned limit,
 
    /* the nested if has to be the only cf_node:
     * i.e. <block> <if_stmt> <block> */
-   nir_if *parent_if = nir_cf_node_as_if(if_stmt->cf_node.parent);
    if (exec_list_length(&parent_if->then_list) != 3)
       return false;
 
@@ -268,6 +271,9 @@ nir_opt_collapse_if(nir_if *if_stmt, nir_shader *shader, unsigned limit,
 
    /* check if all outer phis become trivial after merging the ifs */
    nir_foreach_instr(instr, last) {
+      if (parent_if->control == nir_selection_control_flatten)
+         break;
+
       nir_phi_instr *phi = nir_instr_as_phi(instr);
       nir_phi_src *else_src =
          nir_phi_get_src_from_block(phi, nir_if_first_else_block(if_stmt));
@@ -282,6 +288,12 @@ nir_opt_collapse_if(nir_if *if_stmt, nir_shader *shader, unsigned limit,
       }
    }
 
+   if (parent_if->control == nir_selection_control_flatten) {
+      /* Override driver defaults */
+      indirect_load_ok = true;
+      expensive_alu_ok = true;
+   }
+
    /* check if the block before the nested if matches the requirements */
    nir_block *first = nir_if_first_then_block(parent_if);
    unsigned count = 0;
@@ -289,18 +301,21 @@ nir_opt_collapse_if(nir_if *if_stmt, nir_shader *shader, unsigned limit,
                                        indirect_load_ok, expensive_alu_ok))
       return false;
 
-   if (count > limit)
+   if (count > limit && parent_if->control != nir_selection_control_flatten)
       return false;
 
    /* trivialize succeeding phis */
    nir_foreach_instr(instr, last) {
       nir_phi_instr *phi = nir_instr_as_phi(instr);
+      nir_phi_src *else_src =
+         nir_phi_get_src_from_block(phi, nir_if_first_else_block(if_stmt));
       nir_foreach_use_safe(src, &phi->dest.ssa) {
          nir_phi_src *phi_src =
             nir_phi_get_src_from_block(nir_instr_as_phi(src->parent_instr),
                                        nir_if_first_else_block(parent_if));
-         nir_instr_rewrite_src(src->parent_instr, &phi_src->src,
-                               nir_src_for_ssa(&phi->dest.ssa));
+         if (phi_src->src.ssa == else_src->src.ssa)
+            nir_instr_rewrite_src(src->parent_instr, &phi_src->src,
+                                  nir_src_for_ssa(&phi->dest.ssa));
       }
    }