<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jan 4, 2015 at 7:24 PM, Connor Abbott <span dir="ltr"><<a href="mailto:cwabbott0@gmail.com" target="_blank">cwabbott0@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Tue, Dec 16, 2014 at 1:11 AM, Jason Ekstrand <span dir="ltr"><<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">---<br>
 src/glsl/Makefile.sources                     |   1 +<br>
 src/glsl/nir/nir.h                            |   2 +<br>
 src/glsl/nir/nir_lower_global_vars_to_local.c | 107 ++++++++++++++++++++++++++<br>
 3 files changed, 110 insertions(+)<br>
 create mode 100644 src/glsl/nir/nir_lower_global_vars_to_local.c<br>
<br>
diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources<br>
index 53c3e98..3f5c0bd 100644<br>
--- a/src/glsl/Makefile.sources<br>
+++ b/src/glsl/Makefile.sources<br>
@@ -22,6 +22,7 @@ NIR_FILES = \<br>
        $(GLSL_SRCDIR)/nir/nir_intrinsics.h \<br>
        $(GLSL_SRCDIR)/nir/nir_live_variables.c \<br>
        $(GLSL_SRCDIR)/nir/nir_lower_atomics.c \<br>
+       $(GLSL_SRCDIR)/nir/nir_lower_global_vars_to_local.c \<br>
        $(GLSL_SRCDIR)/nir/nir_lower_locals_to_regs.c \<br>
        $(GLSL_SRCDIR)/nir/nir_lower_io.c \<br>
        $(GLSL_SRCDIR)/nir/nir_lower_samplers.cpp \<br>
diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h<br>
index ec9ce07..30146d6 100644<br>
--- a/src/glsl/nir/nir.h<br>
+++ b/src/glsl/nir/nir.h<br>
@@ -1358,6 +1358,8 @@ void nir_dump_cfg(nir_shader *shader, FILE *fp);<br>
<br>
 void nir_split_var_copies(nir_shader *shader);<br>
<br>
+void nir_lower_global_vars_to_local(nir_shader *shader);<br>
+<br>
 void nir_lower_locals_to_regs(nir_shader *shader);<br>
<br>
 void nir_lower_io(nir_shader *shader);<br>
diff --git a/src/glsl/nir/nir_lower_global_vars_to_local.c b/src/glsl/nir/nir_lower_global_vars_to_local.c<br>
new file mode 100644<br>
index 0000000..ec23a0a<br>
--- /dev/null<br>
+++ b/src/glsl/nir/nir_lower_global_vars_to_local.c<br>
@@ -0,0 +1,107 @@<br>
+/*<br>
+ * Copyright © 2014 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>
+ * Authors:<br>
+ *    Jason Ekstrand (<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>)<br>
+ *<br>
+ */<br>
+<br>
+/*<br>
+ * This lowering pass detects when a global variable is only being used by<br>
+ * one function and makes it local to that function<br>
+ */<br>
+<br>
+#include "nir.h"<br>
+<br>
+struct global_to_local_state {<br>
+   nir_function_impl *impl;<br>
+   /* A hash table keyed on variable pointers that stores the unique<br>
+    * nir_function_impl that uses the given variable.  If a variable is<br>
+    * used in multiple functions, the data for the given key will be NULL.<br>
+    */<br>
+   struct hash_table *var_func_table;<br>
+};<br>
+<br>
+static bool<br>
+mark_global_var_uses_block(nir_block *block, void *void_state)<br>
+{<br>
+   struct global_to_local_state *state = void_state;<br>
+<br>
+   nir_foreach_instr(block, instr) {<br>
+      if (instr->type != nir_instr_type_intrinsic)<br>
+         continue;<br>
+<br>
+      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);<br>
+      unsigned num_vars = nir_intrinsic_infos[intrin->intrinsic].num_variables;<br>
+<br>
+      for (unsigned i = 0; i < num_vars; i++) {<br>
+         nir_variable *var = intrin->variables[i]->var;<br>
+         if (var->data.mode != nir_var_global)<br>
+            continue;<br>
+<br>
+         uint32_t hash = _mesa_hash_pointer(var);<br>
+         struct hash_entry *entry =<br>
+            _mesa_hash_table_search(state->var_func_table, hash, var);<br>
+<br>
+         if (entry) {<br>
+            if (entry->data != state->impl)<br>
+               entry->data = NULL;<br>
+         }<br>
+<br>
+         _mesa_hash_table_insert(state->var_func_table, hash, var, state->impl);<br></blockquote><div><br></div></div></div><div>I think you should be doing:</div><div><br></div><div>if (entry) {</div><div>   ...</div><div>} else {</div><div><div><div>   _mesa_hash_table_insert(state->var_func_table, hash, var, state->impl);</div><div>}</div></div></div></div></div></div></blockquote><div><br></div><div>Yup.  Fixed.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div><div> </div></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div>
+      }<br>
+   }<br>
+<br>
+   return true;<br>
+}<br>
+<br>
+void<br>
+nir_lower_global_vars_to_local(nir_shader *shader)<br>
+{<br>
+   struct global_to_local_state state;<br>
+<br>
+   state.var_func_table = _mesa_hash_table_create(NULL,<br>
+                                                  _mesa_key_pointer_equal);<br>
+<br>
+   nir_foreach_overload(shader, overload) {<br>
+      if (overload->impl) {<br>
+         state.impl = overload->impl;<br>
+         nir_foreach_block(overload->impl, mark_global_var_uses_block, &state);<br>
+      }<br>
+   }<br>
+<br>
+   struct hash_entry *entry;<br>
+   hash_table_foreach(state.var_func_table, entry) {<br>
+      nir_variable *var = (void *)entry->key;<br>
+      nir_function_impl *impl = entry->data;<br>
+<br>
+      assert(var->data.mode == nir_var_global);<br>
+<br>
+      if (impl != NULL) {<br>
+         exec_node_remove(&var->node);<br>
+         var->data.mode = nir_var_local;<br>
+         exec_list_push_tail(&impl->locals, &var->node);<br>
+      }<br>
+   }<br>
+<br>
+   _mesa_hash_table_destroy(state.var_func_table, NULL);<br>
+}<br>
</div></div><span><font color="#888888"><span><font color="#888888">--<br>
2.2.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="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></font></span></blockquote></div><br></div></div>
</blockquote></div><br></div></div>