nir/scheduler: Move nir_scheduler to its own header
[mesa.git] / src / compiler / nir / nir_lower_variable_initializers.c
1 /*
2 * Copyright © 2016 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 #include "nir.h"
25 #include "nir_builder.h"
26
27 static void
28 build_constant_load(nir_builder *b, nir_deref_instr *deref, nir_constant *c)
29 {
30 if (glsl_type_is_vector_or_scalar(deref->type)) {
31 nir_load_const_instr *load =
32 nir_load_const_instr_create(b->shader,
33 glsl_get_vector_elements(deref->type),
34 glsl_get_bit_size(deref->type));
35 memcpy(load->value, c->values, sizeof(*load->value) * load->def.num_components);
36 nir_builder_instr_insert(b, &load->instr);
37 nir_store_deref(b, deref, &load->def, ~0);
38 } else if (glsl_type_is_struct_or_ifc(deref->type)) {
39 unsigned len = glsl_get_length(deref->type);
40 for (unsigned i = 0; i < len; i++) {
41 build_constant_load(b, nir_build_deref_struct(b, deref, i),
42 c->elements[i]);
43 }
44 } else {
45 assert(glsl_type_is_array(deref->type) ||
46 glsl_type_is_matrix(deref->type));
47 unsigned len = glsl_get_length(deref->type);
48 for (unsigned i = 0; i < len; i++) {
49 build_constant_load(b,
50 nir_build_deref_array_imm(b, deref, i),
51 c->elements[i]);
52 }
53 }
54 }
55
56 static bool
57 lower_const_initializer(struct nir_builder *b, struct exec_list *var_list)
58 {
59 bool progress = false;
60
61 b->cursor = nir_before_cf_list(&b->impl->body);
62
63 nir_foreach_variable(var, var_list) {
64 if (var->constant_initializer) {
65 build_constant_load(b, nir_build_deref_var(b, var),
66 var->constant_initializer);
67
68 progress = true;
69 var->constant_initializer = NULL;
70 } else if (var->pointer_initializer) {
71 nir_deref_instr *src_deref = nir_build_deref_var(b, var->pointer_initializer);
72 nir_deref_instr *dst_deref = nir_build_deref_var(b, var);
73
74 /* Note that this stores a pointer to src into dst */
75 nir_store_deref(b, dst_deref, &src_deref->dest.ssa, ~0);
76
77 progress = true;
78 var->pointer_initializer = NULL;
79 }
80
81 }
82
83 return progress;
84 }
85
86 bool
87 nir_lower_variable_initializers(nir_shader *shader, nir_variable_mode modes)
88 {
89 bool progress = false;
90
91 nir_foreach_function(function, shader) {
92 if (!function->impl)
93 continue;
94
95 bool impl_progress = false;
96
97 nir_builder builder;
98 nir_builder_init(&builder, function->impl);
99
100 if ((modes & nir_var_shader_out) && function->is_entrypoint)
101 impl_progress |= lower_const_initializer(&builder, &shader->outputs);
102
103 if ((modes & nir_var_shader_temp) && function->is_entrypoint)
104 impl_progress |= lower_const_initializer(&builder, &shader->globals);
105
106 if ((modes & nir_var_system_value) && function->is_entrypoint)
107 impl_progress |= lower_const_initializer(&builder, &shader->system_values);
108
109 if (modes & nir_var_function_temp)
110 impl_progress |= lower_const_initializer(&builder, &function->impl->locals);
111
112 if (impl_progress) {
113 progress = true;
114 nir_metadata_preserve(function->impl, nir_metadata_block_index |
115 nir_metadata_dominance |
116 nir_metadata_live_ssa_defs);
117 } else {
118 nir_metadata_preserve(function->impl, nir_metadata_all);
119 }
120 }
121
122 return progress;
123 }