• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Jason Ekstrand (jason@jlekstrand.net)
25  *
26  */
27 
28 /*
29  * This lowering pass detects when a global variable is only being used by
30  * one function and makes it local to that function
31  */
32 
33 #include "nir.h"
34 
35 static void
register_var_use(nir_variable * var,nir_function_impl * impl,struct hash_table * var_func_table)36 register_var_use(nir_variable *var, nir_function_impl *impl,
37                  struct hash_table *var_func_table)
38 {
39    if (var->data.mode != nir_var_shader_temp)
40       return;
41 
42    struct hash_entry *entry =
43       _mesa_hash_table_search(var_func_table, var);
44 
45    if (entry) {
46       if (entry->data != impl)
47          entry->data = NULL;
48    } else {
49       _mesa_hash_table_insert(var_func_table, var, impl);
50    }
51 }
52 
53 static bool
mark_global_var_uses_block(nir_block * block,nir_function_impl * impl,struct hash_table * var_func_table)54 mark_global_var_uses_block(nir_block *block, nir_function_impl *impl,
55                            struct hash_table *var_func_table)
56 {
57    nir_foreach_instr(instr, block) {
58       if (instr->type ==  nir_instr_type_deref) {
59          nir_deref_instr *deref = nir_instr_as_deref(instr);
60          if (deref->deref_type == nir_deref_type_var)
61             register_var_use(deref->var, impl, var_func_table);
62       }
63    }
64 
65    return true;
66 }
67 
68 bool
nir_lower_global_vars_to_local(nir_shader * shader)69 nir_lower_global_vars_to_local(nir_shader *shader)
70 {
71    bool progress = false;
72 
73    /* A hash table keyed on variable pointers that stores the unique
74     * nir_function_impl that uses the given variable.  If a variable is
75     * used in multiple functions, the data for the given key will be NULL.
76     */
77    struct hash_table *var_func_table = _mesa_pointer_hash_table_create(NULL);
78 
79    nir_foreach_function(function, shader) {
80       if (function->impl) {
81          nir_foreach_block(block, function->impl)
82             mark_global_var_uses_block(block, function->impl, var_func_table);
83       }
84    }
85 
86    nir_foreach_variable_with_modes_safe(var, shader, nir_var_shader_temp) {
87       struct hash_entry *entry = _mesa_hash_table_search(var_func_table, var);
88       if (!entry)
89          continue;
90 
91       nir_function_impl *impl = entry->data;
92 
93       if (impl != NULL) {
94          exec_node_remove(&var->node);
95          var->data.mode = nir_var_function_temp;
96          exec_list_push_tail(&impl->locals, &var->node);
97          nir_metadata_preserve(impl, nir_metadata_block_index |
98                                      nir_metadata_dominance |
99                                      nir_metadata_live_ssa_defs);
100          progress = true;
101       }
102    }
103 
104    _mesa_hash_table_destroy(var_func_table, NULL);
105 
106    if (progress)
107       nir_fixup_deref_modes(shader);
108 
109    nir_foreach_function(function, shader) {
110       if (function->impl)
111          nir_metadata_preserve(function->impl, nir_metadata_all);
112    }
113 
114    return progress;
115 }
116