nir: copy intrinsic type when lowering load input/uniform and store output
[mesa.git] / src / compiler / nir / nir_lower_global_vars_to_local.c
index 7b4cd4ee8dc8d77ea0fceeb4f57f5f3e5389fddb..4df87aba366fdd93aceed69cca826b0e2fe7b9a0 100644 (file)
 
 #include "nir.h"
 
-struct global_to_local_state {
-   nir_function_impl *impl;
-   /* A hash table keyed on variable pointers that stores the unique
-    * nir_function_impl that uses the given variable.  If a variable is
-    * used in multiple functions, the data for the given key will be NULL.
-    */
-   struct hash_table *var_func_table;
-};
-
-static bool
-mark_global_var_uses_block(nir_block *block, void *void_state)
+static void
+register_var_use(nir_variable *var, nir_function_impl *impl,
+                 struct hash_table *var_func_table)
 {
-   struct global_to_local_state *state = void_state;
+   if (var->data.mode != nir_var_shader_temp)
+      return;
 
-   nir_foreach_instr(block, instr) {
-      if (instr->type != nir_instr_type_intrinsic)
-         continue;
+   struct hash_entry *entry =
+      _mesa_hash_table_search(var_func_table, var);
 
-      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
-      unsigned num_vars = nir_intrinsic_infos[intrin->intrinsic].num_variables;
-
-      for (unsigned i = 0; i < num_vars; i++) {
-         nir_variable *var = intrin->variables[i]->var;
-         if (var->data.mode != nir_var_global)
-            continue;
-
-         struct hash_entry *entry =
-            _mesa_hash_table_search(state->var_func_table, var);
+   if (entry) {
+      if (entry->data != impl)
+         entry->data = NULL;
+   } else {
+      _mesa_hash_table_insert(var_func_table, var, impl);
+   }
+}
 
-         if (entry) {
-            if (entry->data != state->impl)
-               entry->data = NULL;
-         } else {
-            _mesa_hash_table_insert(state->var_func_table, var, state->impl);
-         }
+static bool
+mark_global_var_uses_block(nir_block *block, nir_function_impl *impl,
+                           struct hash_table *var_func_table)
+{
+   nir_foreach_instr(instr, block) {
+      if (instr->type ==  nir_instr_type_deref) {
+         nir_deref_instr *deref = nir_instr_as_deref(instr);
+         if (deref->deref_type == nir_deref_type_var)
+            register_var_use(deref->var, impl, var_func_table);
       }
    }
 
@@ -76,29 +68,30 @@ mark_global_var_uses_block(nir_block *block, void *void_state)
 bool
 nir_lower_global_vars_to_local(nir_shader *shader)
 {
-   struct global_to_local_state state;
    bool progress = false;
 
-   state.var_func_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
-                                                  _mesa_key_pointer_equal);
+   /* A hash table keyed on variable pointers that stores the unique
+    * nir_function_impl that uses the given variable.  If a variable is
+    * used in multiple functions, the data for the given key will be NULL.
+    */
+   struct hash_table *var_func_table = _mesa_pointer_hash_table_create(NULL);
 
-   nir_foreach_function(shader, function) {
+   nir_foreach_function(function, shader) {
       if (function->impl) {
-         state.impl = function->impl;
-         nir_foreach_block(function->impl, mark_global_var_uses_block, &state);
+         nir_foreach_block(block, function->impl)
+            mark_global_var_uses_block(block, function->impl, var_func_table);
       }
    }
 
-   struct hash_entry *entry;
-   hash_table_foreach(state.var_func_table, entry) {
+   hash_table_foreach(var_func_table, entry) {
       nir_variable *var = (void *)entry->key;
       nir_function_impl *impl = entry->data;
 
-      assert(var->data.mode == nir_var_global);
+      assert(var->data.mode == nir_var_shader_temp);
 
       if (impl != NULL) {
          exec_node_remove(&var->node);
-         var->data.mode = nir_var_local;
+         var->data.mode = nir_var_function_temp;
          exec_list_push_tail(&impl->locals, &var->node);
          nir_metadata_preserve(impl, nir_metadata_block_index |
                                      nir_metadata_dominance |
@@ -107,7 +100,18 @@ nir_lower_global_vars_to_local(nir_shader *shader)
       }
    }
 
-   _mesa_hash_table_destroy(state.var_func_table, NULL);
+   _mesa_hash_table_destroy(var_func_table, NULL);
+
+   if (progress)
+      nir_fixup_deref_modes(shader);
+
+#ifndef NDEBUG
+   nir_foreach_function(function, shader) {
+      if (function->impl) {
+         function->impl->valid_metadata &= ~nir_metadata_not_properly_reset;
+      }
+   }
+#endif
 
    return progress;
 }