<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, Aug 15, 2018 at 4:57 PM Caio Marcelo de Oliveira Filho <<a href="mailto:caio.oliveira@intel.com" target="_blank">caio.oliveira@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Instead of doing this as part of the existing (local) copy prop vars<br>
pass.  This is an intermediate step before changing both the dead<br>
write and the copy prop vars to act on the whole program instead of on<br>
local blocks.  The nature of data we store and the way we iterate is<br>
different enough that would be awkward keeping those together.<br>
---<br>
 src/compiler/Makefile.sources              |   1 +<br>
 src/compiler/nir/meson.build               |   1 +<br>
 src/compiler/nir/nir.h                     |   2 +<br>
 src/compiler/nir/nir_opt_dead_write_vars.c | 243 +++++++++++++++++++++<br>
 4 files changed, 247 insertions(+)<br>
 create mode 100644 src/compiler/nir/nir_opt_dead_write_vars.c<br>
<br>
diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources<br>
index 27a54e0be09..fa93ad08a16 100644<br>
--- a/src/compiler/Makefile.sources<br>
+++ b/src/compiler/Makefile.sources<br>
@@ -274,6 +274,7 @@ NIR_FILES = \<br>
        nir/nir_opt_cse.c \<br>
        nir/nir_opt_dce.c \<br>
        nir/nir_opt_dead_cf.c \<br>
+       nir/nir_opt_dead_write_vars.c \<br>
        nir/nir_opt_gcm.c \<br>
        nir/nir_opt_global_to_local.c \<br>
        nir/nir_opt_if.c \<br>
diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build<br>
index 8708f9b069c..1c164a548a7 100644<br>
--- a/src/compiler/nir/meson.build<br>
+++ b/src/compiler/nir/meson.build<br>
@@ -158,6 +158,7 @@ files_libnir = files(<br>
   'nir_opt_cse.c',<br>
   'nir_opt_dce.c',<br>
   'nir_opt_dead_cf.c',<br>
+  'nir_opt_dead_write_vars.c',<br>
   'nir_opt_gcm.c',<br>
   'nir_opt_global_to_local.c',<br>
   'nir_opt_if.c',<br>
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
index d0fa693884b..becf6e351c3 100644<br>
--- a/src/compiler/nir/nir.h<br>
+++ b/src/compiler/nir/nir.h<br>
@@ -2968,6 +2968,8 @@ bool nir_opt_dce(nir_shader *shader);<br>
<br>
 bool nir_opt_dead_cf(nir_shader *shader);<br>
<br>
+bool nir_opt_dead_write_vars(nir_shader *shader);<br>
+<br>
 bool nir_opt_gcm(nir_shader *shader, bool value_number);<br>
<br>
 bool nir_opt_if(nir_shader *shader);<br>
diff --git a/src/compiler/nir/nir_opt_dead_write_vars.c b/src/compiler/nir/nir_opt_dead_write_vars.c<br>
new file mode 100644<br>
index 00000000000..822bfa5595d<br>
--- /dev/null<br>
+++ b/src/compiler/nir/nir_opt_dead_write_vars.c<br>
@@ -0,0 +1,243 @@<br>
+/*<br>
+ * Copyright © 2018 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#include "nir.h"<br>
+#include "nir_builder.h"<br>
+#include "nir_deref.h"<br>
+<br>
+#include "util/u_dynarray.h"<br>
+<br>
+struct state {<br>
+   void *mem_ctx;<br>
+<br>
+   /* Maps nir_deref_instr to a corresponding nir_deref_path.  Avoids<br>
+    * rebuilding the paths for the same deref. */<br>
+   struct hash_table *paths;<br>
+   void *path_lin_ctx;<br>
+};<br>
+<br>
+static nir_deref_path *<br>
+get_path(struct state *state, nir_deref_instr *deref)<br>
+{<br>
+   struct hash_entry *entry = _mesa_hash_table_search(state->paths, deref);<br>
+   if (!entry) {<br>
+      nir_deref_path *path = linear_zalloc_child(state->path_lin_ctx, sizeof(nir_deref_path));<br>
+      nir_deref_path_init(path, deref, state->mem_ctx);<br>
+      _mesa_hash_table_insert(state->paths, deref, path);<br>
+      return path;<br>
+   } else {<br>
+      return entry->data;<br>
+   }<br>
+}<br></blockquote><div><br></div><div>Do you have any proof that this actually helps?  The deref_path stuff was designed to be put on the stack and absolutely as efficient as possible.  In the common case of a deref chain with only a couple of elements, I would expect to actually be more work to look it up in a hash table.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+/* Entry for unused_writes arrays. */<br>
+struct write_entry {<br>
+   /* If NULL indicates the entry is free to be reused. */<br>
+   nir_intrinsic_instr *intrin;<br>
+   uintptr_t mask;<br>
+   nir_deref_path *path;<br>
+};<br>
+<br>
+static void<br>
+clear_unused_for_modes(struct util_dynarray *unused_writes, nir_variable_mode modes)<br>
+{<br>
+   util_dynarray_foreach(unused_writes, struct write_entry, entry) {<br>
+      if (!entry->intrin)<br>
+         continue;<br>
+      nir_variable *var = entry->path->path[0]->var;<br>
+      if (var->data.mode & modes)<br>
+         entry->intrin = NULL;<br>
+   }<br>
+}<br>
+<br>
+static void<br>
+clear_unused_for_src(struct util_dynarray *unused_writes, nir_deref_path *src_path)<br>
+{<br>
+   util_dynarray_foreach(unused_writes, struct write_entry, entry) {<br>
+      if (!entry->intrin)<br>
+         continue;<br>
+      if (nir_compare_deref_paths(src_path, entry->path) & nir_derefs_may_alias_bit)<br>
+         entry->intrin = NULL;<br>
+   }<br>
+}<br>
+<br>
+static bool<br>
+update_unused_writes_with_dst(struct util_dynarray *unused_writes,<br>
+                              nir_intrinsic_instr *intrin,<br>
+                              nir_deref_path *dst_path, uintptr_t mask)<br>
+{<br>
+   /* If we see an empty entry, keep track of it for reuse later. */<br>
+   struct write_entry *empty_entry = NULL;<br>
+   bool progress = false;<br>
+<br>
+   /* Find writes that are unused and can be removed. */<br>
+   util_dynarray_foreach(unused_writes, struct write_entry, entry) {<br>
+      if (!entry->intrin) {<br>
+         empty_entry = entry;<br>
+         continue;<br>
+      }<br>
+      nir_deref_compare_result comp = nir_compare_deref_paths(dst_path, entry->path);<br>
+      if (comp & nir_derefs_a_contains_b_bit) {<br>
+         entry->mask &= ~mask;<br>
+         if (entry->mask == 0) {<br>
+            nir_instr_remove(&entry->intrin->instr);<br>
+            entry->intrin = NULL;<br>
+            empty_entry = entry;<br>
+            progress = true;<br>
+         }<br>
+      }<br>
+   }<br>
+<br>
+   /* Add the new write to the unused array. */<br>
+   struct write_entry new_entry = {<br>
+      .intrin = intrin,<br>
+      .mask = mask,<br>
+      .path = dst_path,<br>
+   };<br>
+<br>
+   if (empty_entry)<br>
+      *empty_entry = new_entry;<br>
+   else<br>
+      util_dynarray_append(unused_writes, struct write_entry, new_entry);<br>
+<br>
+   return progress;<br>
+}<br>
+<br>
+static bool<br>
+remove_dead_write_vars_local(struct state *state, nir_block *block)<br>
+{<br>
+   bool progress = false;<br>
+<br>
+   struct util_dynarray unused_writes;<br>
+   util_dynarray_init(&unused_writes, state->mem_ctx);<br>
+<br>
+   nir_foreach_instr_safe(instr, block) {<br></blockquote><div><br></div><div>It wouldn't hurt to add a case for call instructions which does a 
barrier on everything I mentioned below as well as globals and locals.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+      if (instr->type != nir_instr_type_intrinsic)<br>
+         continue;<br>
+<br>
+      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);<br>
+      switch (intrin->intrinsic) {<br>
+      case nir_intrinsic_barrier:<br>
+      case nir_intrinsic_memory_barrier: {<br>
+         nir_variable_mode modes = ~(nir_var_local | nir_var_global |<br>
+                                     nir_var_shader_in | nir_var_uniform);<br></blockquote><div><br></div><div>The only thing a barrier like this affects is shared, storage, and output.  Locals and globals can't cross between shader channels so there's no reason to do anything with them on a barrier.  For inputs and uniforms, they're never written anyway so there's no point in doing anything with them on a barrier.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+         clear_unused_for_modes(&unused_writes, modes); <br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+         break;<br>
+      }<br>
+<br>
+      case nir_intrinsic_emit_vertex:<br>
+      case nir_intrinsic_emit_vertex_with_counter: {<br>
+         nir_variable_mode modes = nir_var_shader_out;<br>
+         clear_unused_for_modes(&unused_writes, modes);<br></blockquote><div><br></div><div>Just pass nir_var_shader_out in here; no reason to make a temporary variable.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+         break;<br>
+      }<br>
+<br>
+      case nir_intrinsic_load_deref: {<br>
+         nir_deref_instr *src = nir_src_as_deref(intrin->src[0]);<br>
+         nir_deref_path *src_path = get_path(state, src);<br>
+<br>
+         clear_unused_for_src(&unused_writes, src_path);<br>
+         break;<br>
+      }<br>
+<br>
+      case nir_intrinsic_store_deref: {<br>
+         nir_deref_instr *dst = nir_src_as_deref(intrin->src[0]);<br>
+         nir_deref_path *dst_path = get_path(state, dst);<br>
+<br>
+         uintptr_t mask = nir_intrinsic_write_mask(intrin);<br>
+         progress |= update_unused_writes_with_dst(&unused_writes, intrin, dst_path, mask);<br>
+         break;<br>
+      }<br>
+<br>
+      case nir_intrinsic_copy_deref: {<br>
+         nir_deref_instr *src = nir_src_as_deref(intrin->src[1]);<br>
+         nir_deref_path *src_path = get_path(state, src);<br>
+<br>
+         nir_deref_instr *dst = nir_src_as_deref(intrin->src[0]);<br>
+         nir_deref_path *dst_path = get_path(state, dst);<br>
+<br>
+         /* Self-copy is removed. */<br>
+         if (src == dst || (nir_compare_deref_paths(src_path, dst_path) & nir_derefs_equal_bit)) { <br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+            nir_instr_remove(instr);<br>
+            progress = true;<br>
+            break;<br>
+         }<br>
+<br>
+         uintptr_t mask = ~(1 << NIR_MAX_VEC_COMPONENTS);<br>
+         clear_unused_for_src(&unused_writes, src_path);<br>
+         progress |= update_unused_writes_with_dst(&unused_writes, intrin, dst_path, mask);<br>
+         break;<br>
+      }<br>
+<br>
+      default:<br>
+         break;<br>
+      }<br>
+   }<br>
+<br>
+   /* All unused writes at the end of the block are kept, since we can't be<br>
+    * sure they'll be overwritten or not with local analysis only.<br>
+    */<br>
+<br>
+   return progress;<br>
+}<br>
+<br>
+static bool<br>
+remove_dead_write_vars_impl(void *mem_ctx, nir_function_impl *func)<br>
+{<br>
+   bool progress = false;<br>
+<br>
+   struct state state = {<br>
+      .mem_ctx = mem_ctx,<br>
+<br>
+      .path_lin_ctx = linear_alloc_parent(mem_ctx, 0),<br>
+      .paths = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer, _mesa_key_pointer_equal),<br>
+   };<br>
+<br>
+   nir_foreach_block(block, func)<br>
+      progress |= remove_dead_write_vars_local(&state, block);<br>
+<br>
+   return progress;<br>
+}<br>
+<br>
+bool<br>
+nir_opt_dead_write_vars(nir_shader *shader)<br>
+{<br>
+   void *mem_ctx = ralloc_context(NULL);<br>
+   bool progress = false;<br>
+<br>
+   nir_foreach_function(function, shader) {<br>
+      if (!function->impl)<br>
+         continue;<br>
+<br>
+      nir_metadata_require(function->impl, nir_metadata_block_index);<br>
+<br>
+      if (remove_dead_write_vars_impl(mem_ctx, function->impl)) {<br>
+         nir_metadata_preserve(function->impl, nir_metadata_block_index |<br>
+                                               nir_metadata_dominance);<br></blockquote><div><br></div><div>I'd put the metadata stuff in the _impl version in case we ever want to expose it for use directly.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+         progress = true;<br>
+      }<br>
+   }<br>
+<br>
+   ralloc_free(mem_ctx);<br>
+   return progress;<br>
+}<br>
-- <br>
2.18.0<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div></div>