xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_global_vars_to_local.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 
24 /*
25  * This lowering pass detects when a global variable is only being used by
26  * one function and makes it local to that function
27  */
28 
29 #include "nir.h"
30 
31 static void
register_var_use(nir_variable * var,nir_function_impl * impl,struct hash_table * var_func_table)32 register_var_use(nir_variable *var, nir_function_impl *impl,
33                  struct hash_table *var_func_table)
34 {
35    if (var->data.mode != nir_var_shader_temp)
36       return;
37 
38    struct hash_entry *entry =
39       _mesa_hash_table_search(var_func_table, var);
40 
41    if (entry) {
42       if (entry->data != impl)
43          entry->data = NULL;
44    } else {
45       _mesa_hash_table_insert(var_func_table, var, impl);
46    }
47 }
48 
49 static bool
mark_global_var_uses_block(nir_block * block,nir_function_impl * impl,struct hash_table * var_func_table)50 mark_global_var_uses_block(nir_block *block, nir_function_impl *impl,
51                            struct hash_table *var_func_table)
52 {
53    nir_foreach_instr(instr, block) {
54       if (instr->type == nir_instr_type_deref) {
55          nir_deref_instr *deref = nir_instr_as_deref(instr);
56          if (deref->deref_type == nir_deref_type_var)
57             register_var_use(deref->var, impl, var_func_table);
58       }
59    }
60 
61    return true;
62 }
63 
64 bool
nir_lower_global_vars_to_local(nir_shader * shader)65 nir_lower_global_vars_to_local(nir_shader *shader)
66 {
67    bool progress = false;
68 
69    /* A hash table keyed on variable pointers that stores the unique
70     * nir_function_impl that uses the given variable.  If a variable is
71     * used in multiple functions, the data for the given key will be NULL.
72     */
73    struct hash_table *var_func_table = _mesa_pointer_hash_table_create(NULL);
74 
75    nir_foreach_function_impl(impl, shader) {
76       nir_foreach_block(block, impl)
77          mark_global_var_uses_block(block, impl, var_func_table);
78    }
79 
80    nir_foreach_variable_with_modes_safe(var, shader, nir_var_shader_temp) {
81       struct hash_entry *entry = _mesa_hash_table_search(var_func_table, var);
82       if (!entry)
83          continue;
84 
85       nir_function_impl *impl = entry->data;
86 
87       if (impl != NULL) {
88          exec_node_remove(&var->node);
89          var->data.mode = nir_var_function_temp;
90          exec_list_push_tail(&impl->locals, &var->node);
91          nir_metadata_preserve(impl, nir_metadata_control_flow |
92                                         nir_metadata_live_defs);
93          progress = true;
94       }
95    }
96 
97    _mesa_hash_table_destroy(var_func_table, NULL);
98 
99    if (progress)
100       nir_fixup_deref_modes(shader);
101 
102    nir_foreach_function_impl(impl, shader) {
103       nir_metadata_preserve(impl, nir_metadata_all);
104    }
105 
106    return progress;
107 }
108