2 * Copyright © 2016 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
25 #include "nir_builder.h"
28 build_constant_load(nir_builder
*b
, nir_deref_instr
*deref
, nir_constant
*c
)
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 load
->value
= c
->values
[0];
36 nir_builder_instr_insert(b
, &load
->instr
);
37 nir_store_deref(b
, deref
, &load
->def
, ~0);
38 } else if (glsl_type_is_matrix(deref
->type
)) {
39 unsigned cols
= glsl_get_matrix_columns(deref
->type
);
40 unsigned rows
= glsl_get_vector_elements(deref
->type
);
41 unsigned bit_size
= glsl_get_bit_size(deref
->type
);
42 for (unsigned i
= 0; i
< cols
; i
++) {
43 nir_load_const_instr
*load
=
44 nir_load_const_instr_create(b
->shader
, rows
, bit_size
);
45 load
->value
= c
->values
[i
];
46 nir_builder_instr_insert(b
, &load
->instr
);
47 nir_store_deref(b
, nir_build_deref_array(b
, deref
, nir_imm_int(b
, i
)),
50 } else if (glsl_type_is_struct(deref
->type
)) {
51 unsigned len
= glsl_get_length(deref
->type
);
52 for (unsigned i
= 0; i
< len
; i
++) {
53 build_constant_load(b
, nir_build_deref_struct(b
, deref
, i
),
57 assert(glsl_type_is_array(deref
->type
));
58 unsigned len
= glsl_get_length(deref
->type
);
59 for (unsigned i
= 0; i
< len
; i
++) {
60 build_constant_load(b
,
61 nir_build_deref_array(b
, deref
, nir_imm_int(b
, i
)),
68 lower_const_initializer(struct nir_builder
*b
, struct exec_list
*var_list
)
70 bool progress
= false;
72 b
->cursor
= nir_before_cf_list(&b
->impl
->body
);
74 nir_foreach_variable(var
, var_list
) {
75 if (!var
->constant_initializer
)
80 build_constant_load(b
, nir_build_deref_var(b
, var
),
81 var
->constant_initializer
);
83 var
->constant_initializer
= NULL
;
90 nir_lower_constant_initializers(nir_shader
*shader
, nir_variable_mode modes
)
92 bool progress
= false;
94 nir_foreach_function(function
, shader
) {
98 bool impl_progress
= false;
101 nir_builder_init(&builder
, function
->impl
);
103 if ((modes
& nir_var_shader_out
) && function
->is_entrypoint
)
104 impl_progress
|= lower_const_initializer(&builder
, &shader
->outputs
);
106 if ((modes
& nir_var_shader_temp
) && function
->is_entrypoint
)
107 impl_progress
|= lower_const_initializer(&builder
, &shader
->globals
);
109 if ((modes
& nir_var_system_value
) && function
->is_entrypoint
)
110 impl_progress
|= lower_const_initializer(&builder
, &shader
->system_values
);
112 if (modes
& nir_var_function_temp
)
113 impl_progress
|= lower_const_initializer(&builder
, &function
->impl
->locals
);
117 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
118 nir_metadata_dominance
|
119 nir_metadata_live_ssa_defs
);
122 function
->impl
->valid_metadata
&= ~nir_metadata_not_properly_reset
;