2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Jason Ekstrand (jason@jlekstrand.net)
29 * This lowering pass detects when a global variable is only being used by
30 * one function and makes it local to that function
36 mark_global_var_uses_block(nir_block
*block
, nir_function_impl
*impl
,
37 struct hash_table
*var_func_table
)
39 nir_foreach_instr(instr
, block
) {
40 if (instr
->type
!= nir_instr_type_intrinsic
)
43 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
44 unsigned num_vars
= nir_intrinsic_infos
[intrin
->intrinsic
].num_variables
;
46 for (unsigned i
= 0; i
< num_vars
; i
++) {
47 nir_variable
*var
= intrin
->variables
[i
]->var
;
48 if (var
->data
.mode
!= nir_var_global
)
51 struct hash_entry
*entry
=
52 _mesa_hash_table_search(var_func_table
, var
);
55 if (entry
->data
!= impl
)
58 _mesa_hash_table_insert(var_func_table
, var
, impl
);
67 nir_lower_global_vars_to_local(nir_shader
*shader
)
69 bool progress
= false;
71 /* A hash table keyed on variable pointers that stores the unique
72 * nir_function_impl that uses the given variable. If a variable is
73 * used in multiple functions, the data for the given key will be NULL.
75 struct hash_table
*var_func_table
=
76 _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
77 _mesa_key_pointer_equal
);
79 nir_assert_lowered_derefs(shader
, nir_lower_load_store_derefs
| nir_lower_interp_derefs
|
80 nir_lower_atomic_counter_derefs
| nir_lower_atomic_derefs
| nir_lower_image_derefs
);
82 nir_foreach_function(function
, shader
) {
84 nir_foreach_block(block
, function
->impl
)
85 mark_global_var_uses_block(block
, function
->impl
, var_func_table
);
89 struct hash_entry
*entry
;
90 hash_table_foreach(var_func_table
, entry
) {
91 nir_variable
*var
= (void *)entry
->key
;
92 nir_function_impl
*impl
= entry
->data
;
94 assert(var
->data
.mode
== nir_var_global
);
97 exec_node_remove(&var
->node
);
98 var
->data
.mode
= nir_var_local
;
99 exec_list_push_tail(&impl
->locals
, &var
->node
);
100 nir_metadata_preserve(impl
, nir_metadata_block_index
|
101 nir_metadata_dominance
|
102 nir_metadata_live_ssa_defs
);
107 _mesa_hash_table_destroy(var_func_table
, NULL
);