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
35 struct global_to_local_state
{
36 nir_function_impl
*impl
;
37 /* A hash table keyed on variable pointers that stores the unique
38 * nir_function_impl that uses the given variable. If a variable is
39 * used in multiple functions, the data for the given key will be NULL.
41 struct hash_table
*var_func_table
;
45 mark_global_var_uses_block(nir_block
*block
, void *void_state
)
47 struct global_to_local_state
*state
= void_state
;
49 nir_foreach_instr(block
, instr
) {
50 if (instr
->type
!= nir_instr_type_intrinsic
)
53 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
54 unsigned num_vars
= nir_intrinsic_infos
[intrin
->intrinsic
].num_variables
;
56 for (unsigned i
= 0; i
< num_vars
; i
++) {
57 nir_variable
*var
= intrin
->variables
[i
]->var
;
58 if (var
->data
.mode
!= nir_var_global
)
61 struct hash_entry
*entry
=
62 _mesa_hash_table_search(state
->var_func_table
, var
);
65 if (entry
->data
!= state
->impl
)
68 _mesa_hash_table_insert(state
->var_func_table
, var
, state
->impl
);
77 nir_lower_global_vars_to_local(nir_shader
*shader
)
79 struct global_to_local_state state
;
81 state
.var_func_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
82 _mesa_key_pointer_equal
);
84 nir_foreach_overload(shader
, overload
) {
86 state
.impl
= overload
->impl
;
87 nir_foreach_block(overload
->impl
, mark_global_var_uses_block
, &state
);
91 struct hash_entry
*entry
;
92 hash_table_foreach(state
.var_func_table
, entry
) {
93 nir_variable
*var
= (void *)entry
->key
;
94 nir_function_impl
*impl
= entry
->data
;
96 assert(var
->data
.mode
== nir_var_global
);
99 exec_node_remove(&var
->node
);
100 var
->data
.mode
= nir_var_local
;
101 exec_list_push_tail(&impl
->locals
, &var
->node
);
105 _mesa_hash_table_destroy(state
.var_func_table
, NULL
);