Mesa (main): ir3: Add pass to remove unreachable blocks

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Aug 20 10:57:10 UTC 2021


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

Author: Connor Abbott <cwabbott0 at gmail.com>
Date:   Wed Jul 21 15:03:21 2021 +0200

ir3: Add pass to remove unreachable blocks

Rather than continue to add special cases for these, just clean them up.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12033>

---

 src/freedreno/ir3/ir3.c                    |  16 ++++
 src/freedreno/ir3/ir3.h                    |   5 ++
 src/freedreno/ir3/ir3_compiler_nir.c       |   2 +
 src/freedreno/ir3/ir3_remove_unreachable.c | 122 +++++++++++++++++++++++++++++
 src/freedreno/ir3/meson.build              |   1 +
 5 files changed, 146 insertions(+)

diff --git a/src/freedreno/ir3/ir3.c b/src/freedreno/ir3/ir3.c
index 1491429c14a..48f9f38cca4 100644
--- a/src/freedreno/ir3/ir3.c
+++ b/src/freedreno/ir3/ir3.c
@@ -393,6 +393,22 @@ ir3_block_remove_predecessor(struct ir3_block *block, struct ir3_block *pred)
    }
 }
 
+void
+ir3_block_remove_physical_predecessor(struct ir3_block *block, struct ir3_block *pred)
+{
+   for (unsigned i = 0; i < block->physical_predecessors_count; i++) {
+      if (block->physical_predecessors[i] == pred) {
+         if (i < block->predecessors_count - 1) {
+            block->physical_predecessors[i] =
+               block->physical_predecessors[block->predecessors_count - 1];
+         }
+
+         block->physical_predecessors_count--;
+         return;
+      }
+   }
+}
+
 unsigned
 ir3_block_get_pred_index(struct ir3_block *block, struct ir3_block *pred)
 {
diff --git a/src/freedreno/ir3/ir3.h b/src/freedreno/ir3/ir3.h
index 723eb6567ed..e0dd870971d 100644
--- a/src/freedreno/ir3/ir3.h
+++ b/src/freedreno/ir3/ir3.h
@@ -615,6 +615,8 @@ void ir3_block_add_physical_predecessor(struct ir3_block *block,
                                         struct ir3_block *pred);
 void ir3_block_remove_predecessor(struct ir3_block *block,
                                   struct ir3_block *pred);
+void ir3_block_remove_physical_predecessor(struct ir3_block *block,
+                                           struct ir3_block *pred);
 unsigned ir3_block_get_pred_index(struct ir3_block *block,
                                   struct ir3_block *pred);
 
@@ -1593,6 +1595,9 @@ unsigned ir3_delay_calc_exact(struct ir3_block *block,
                               struct ir3_instruction *instr, bool mergedregs);
 void ir3_remove_nops(struct ir3 *ir);
 
+/* unreachable block elimination: */
+bool ir3_remove_unreachable(struct ir3 *ir);
+
 /* dead code elimination: */
 struct ir3_shader_variant;
 bool ir3_dce(struct ir3 *ir, struct ir3_shader_variant *so);
diff --git a/src/freedreno/ir3/ir3_compiler_nir.c b/src/freedreno/ir3/ir3_compiler_nir.c
index b634eb50e96..16be6e1f810 100644
--- a/src/freedreno/ir3/ir3_compiler_nir.c
+++ b/src/freedreno/ir3/ir3_compiler_nir.c
@@ -4135,6 +4135,8 @@ ir3_compile_shader_nir(struct ir3_compiler *compiler,
    ir3_debug_print(ir, "AFTER: nir->ir3");
    ir3_validate(ir);
 
+   IR3_PASS(ir, ir3_remove_unreachable);
+
    IR3_PASS(ir, ir3_array_to_ssa);
 
    do {
diff --git a/src/freedreno/ir3/ir3_remove_unreachable.c b/src/freedreno/ir3/ir3_remove_unreachable.c
new file mode 100644
index 00000000000..c598d9f2e59
--- /dev/null
+++ b/src/freedreno/ir3/ir3_remove_unreachable.c
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2021 Valve Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "ir3.h"
+
+/* Sometimes we can get unreachable blocks from NIR. In particular this happens
+ * for blocks after an if where both sides end in a break/continue. These blocks
+ * are then reachable only via the physical CFG. This pass deletes these blocks
+ * and reroutes the physical edge past it.
+ */
+
+static void
+delete_block(struct ir3 *ir, struct ir3_block *block)
+{
+   struct ir3_instruction *end = NULL;
+   foreach_instr (instr, &block->instr_list) {
+      if (instr->opc == OPC_END) {
+         end = instr;
+         break;
+      }
+   }
+
+   /* The end block can be legitimately unreachable if the shader only exits via
+    * discarding. ir3_legalize will then insert a branch to the end. Keep the
+    * block around but delete all the other instructions and make the end not
+    * take any sources, so that we don't have any dangling references to other
+    * unreachable blocks.
+    */
+   if (end) {
+      foreach_instr_safe (instr, &block->instr_list) {
+         if (instr != end)
+            list_delinit(&instr->node);
+      }
+      end->srcs_count = 0;
+      return;
+   }
+
+   for (unsigned i = 0; i < 2; i++) {
+      struct ir3_block *succ = block->successors[i];
+      if (!succ)
+         continue;
+
+      unsigned pred_idx = ir3_block_get_pred_index(succ, block);
+
+      /* If this isn't the last predecessor, we swap it with the last before
+       * removing it.
+       */
+      bool swap_pred = pred_idx != succ->predecessors_count - 1;
+
+      foreach_instr (phi, &succ->instr_list) {
+         if (phi->opc != OPC_META_PHI)
+            break;
+
+         if (swap_pred)
+            phi->srcs[pred_idx] = phi->srcs[phi->srcs_count - 1];
+         phi->srcs_count--;
+      }
+      if (swap_pred) {
+         succ->predecessors[pred_idx] =
+            succ->predecessors[succ->predecessors_count - 1];
+      }
+      succ->predecessors_count--;
+   }
+
+   for (unsigned i = 0; i < 2; i++) {
+      struct ir3_block *succ = block->physical_successors[i];
+      if (!succ)
+         continue;
+
+      ir3_block_remove_physical_predecessor(succ, block);
+   }
+
+   if (block->physical_predecessors_count != 0) {
+      /* There should be only one physical predecessor, for the fallthrough
+       * edge.
+       */
+      assert(block->physical_predecessors_count == 1);
+      struct ir3_block *pred = block->physical_predecessors[0];
+      assert(block->node.next != &ir->block_list);
+      struct ir3_block *next = LIST_ENTRY(struct ir3_block, block->node.next, node);
+      if (pred->physical_successors[1] == block)
+         pred->physical_successors[1] = next;
+      else
+         pred->physical_successors[0] = next;
+      ir3_block_add_physical_predecessor(next, pred);
+   }
+}
+
+bool
+ir3_remove_unreachable(struct ir3 *ir)
+{
+   bool progress = false;
+   foreach_block_safe (block, &ir->block_list) {
+      if (block != ir3_start_block(ir) && block->predecessors_count == 0) {
+         delete_block(ir, block);
+         list_del(&block->node);
+         progress = true;
+      }
+   }
+
+   return progress;
+}
diff --git a/src/freedreno/ir3/meson.build b/src/freedreno/ir3/meson.build
index 1bf9847ad95..40bdb26194d 100644
--- a/src/freedreno/ir3/meson.build
+++ b/src/freedreno/ir3/meson.build
@@ -105,6 +105,7 @@ libfreedreno_ir3_files = files(
   'ir3_ra.c',
   'ir3_ra.h',
   'ir3_ra_validate.c',
+  'ir3_remove_unreachable.c',
   'ir3_sched.c',
   'ir3_shader.c',
   'ir3_shader.h',



More information about the mesa-commit mailing list