nir: make nir_const_value scalar
[mesa.git] / src / compiler / nir / nir_opt_constant_folding.c
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 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir_constant_expressions.h"
29 #include <math.h>
30
31 /*
32 * Implements SSA-based constant folding.
33 */
34
35 struct constant_fold_state {
36 void *mem_ctx;
37 nir_function_impl *impl;
38 bool progress;
39 };
40
41 static bool
42 constant_fold_alu_instr(nir_alu_instr *instr, void *mem_ctx)
43 {
44 nir_const_value src[NIR_MAX_VEC_COMPONENTS][NIR_MAX_VEC_COMPONENTS];
45
46 if (!instr->dest.dest.is_ssa)
47 return false;
48
49 /* In the case that any outputs/inputs have unsized types, then we need to
50 * guess the bit-size. In this case, the validator ensures that all
51 * bit-sizes match so we can just take the bit-size from first
52 * output/input with an unsized type. If all the outputs/inputs are sized
53 * then we don't need to guess the bit-size at all because the code we
54 * generate for constant opcodes in this case already knows the sizes of
55 * the types involved and does not need the provided bit-size for anything
56 * (although it still requires to receive a valid bit-size).
57 */
58 unsigned bit_size = 0;
59 if (!nir_alu_type_get_type_size(nir_op_infos[instr->op].output_type))
60 bit_size = instr->dest.dest.ssa.bit_size;
61
62 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
63 if (!instr->src[i].src.is_ssa)
64 return false;
65
66 if (bit_size == 0 &&
67 !nir_alu_type_get_type_size(nir_op_infos[instr->op].input_types[i]))
68 bit_size = instr->src[i].src.ssa->bit_size;
69
70 nir_instr *src_instr = instr->src[i].src.ssa->parent_instr;
71
72 if (src_instr->type != nir_instr_type_load_const)
73 return false;
74 nir_load_const_instr* load_const = nir_instr_as_load_const(src_instr);
75
76 for (unsigned j = 0; j < nir_ssa_alu_instr_src_components(instr, i);
77 j++) {
78 switch(load_const->def.bit_size) {
79 case 64:
80 src[i][j].u64 = load_const->value[instr->src[i].swizzle[j]].u64;
81 break;
82 case 32:
83 src[i][j].u32 = load_const->value[instr->src[i].swizzle[j]].u32;
84 break;
85 case 16:
86 src[i][j].u16 = load_const->value[instr->src[i].swizzle[j]].u16;
87 break;
88 case 8:
89 src[i][j].u8 = load_const->value[instr->src[i].swizzle[j]].u8;
90 break;
91 case 1:
92 src[i][j].b = load_const->value[instr->src[i].swizzle[j]].b;
93 break;
94 default:
95 unreachable("Invalid bit size");
96 }
97 }
98
99 /* We shouldn't have any source modifiers in the optimization loop. */
100 assert(!instr->src[i].abs && !instr->src[i].negate);
101 }
102
103 if (bit_size == 0)
104 bit_size = 32;
105
106 /* We shouldn't have any saturate modifiers in the optimization loop. */
107 assert(!instr->dest.saturate);
108
109 nir_const_value dest[NIR_MAX_VEC_COMPONENTS];
110 nir_const_value *srcs[NIR_MAX_VEC_COMPONENTS];
111 memset(dest, 0, sizeof(dest));
112 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; ++i)
113 srcs[i] = src[i];
114 nir_eval_const_opcode(instr->op, dest, instr->dest.dest.ssa.num_components,
115 bit_size, srcs);
116
117 nir_load_const_instr *new_instr =
118 nir_load_const_instr_create(mem_ctx,
119 instr->dest.dest.ssa.num_components,
120 instr->dest.dest.ssa.bit_size);
121
122 memcpy(new_instr->value, dest, sizeof(*new_instr->value) * new_instr->def.num_components);
123
124 nir_instr_insert_before(&instr->instr, &new_instr->instr);
125
126 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa,
127 nir_src_for_ssa(&new_instr->def));
128
129 nir_instr_remove(&instr->instr);
130 ralloc_free(instr);
131
132 return true;
133 }
134
135 static bool
136 constant_fold_intrinsic_instr(nir_intrinsic_instr *instr)
137 {
138 bool progress = false;
139
140 if (instr->intrinsic == nir_intrinsic_discard_if &&
141 nir_src_is_const(instr->src[0])) {
142 if (nir_src_as_bool(instr->src[0])) {
143 /* This method of getting a nir_shader * from a nir_instr is
144 * admittedly gross, but given the rarity of hitting this case I think
145 * it's preferable to plumbing an otherwise unused nir_shader *
146 * parameter through four functions to get here.
147 */
148 nir_cf_node *cf_node = &instr->instr.block->cf_node;
149 nir_function_impl *impl = nir_cf_node_get_function(cf_node);
150 nir_shader *shader = impl->function->shader;
151
152 nir_intrinsic_instr *discard =
153 nir_intrinsic_instr_create(shader, nir_intrinsic_discard);
154 nir_instr_insert_before(&instr->instr, &discard->instr);
155 nir_instr_remove(&instr->instr);
156 progress = true;
157 } else {
158 /* We're not discarding, just delete the instruction */
159 nir_instr_remove(&instr->instr);
160 progress = true;
161 }
162 }
163
164 return progress;
165 }
166
167 static bool
168 constant_fold_block(nir_block *block, void *mem_ctx)
169 {
170 bool progress = false;
171
172 nir_foreach_instr_safe(instr, block) {
173 switch (instr->type) {
174 case nir_instr_type_alu:
175 progress |= constant_fold_alu_instr(nir_instr_as_alu(instr), mem_ctx);
176 break;
177 case nir_instr_type_intrinsic:
178 progress |=
179 constant_fold_intrinsic_instr(nir_instr_as_intrinsic(instr));
180 break;
181 default:
182 /* Don't know how to constant fold */
183 break;
184 }
185 }
186
187 return progress;
188 }
189
190 static bool
191 nir_opt_constant_folding_impl(nir_function_impl *impl)
192 {
193 void *mem_ctx = ralloc_parent(impl);
194 bool progress = false;
195
196 nir_foreach_block(block, impl) {
197 progress |= constant_fold_block(block, mem_ctx);
198 }
199
200 if (progress) {
201 nir_metadata_preserve(impl, nir_metadata_block_index |
202 nir_metadata_dominance);
203 } else {
204 #ifndef NDEBUG
205 impl->valid_metadata &= ~nir_metadata_not_properly_reset;
206 #endif
207 }
208
209 return progress;
210 }
211
212 bool
213 nir_opt_constant_folding(nir_shader *shader)
214 {
215 bool progress = false;
216
217 nir_foreach_function(function, shader) {
218 if (function->impl)
219 progress |= nir_opt_constant_folding_impl(function->impl);
220 }
221
222 return progress;
223 }