nir/opt_large_constants: Fix a type/deref_type typo
[mesa.git] / src / compiler / nir / nir_opt_large_constants.c
1 /*
2 * Copyright © 2018 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 #include "nir_deref.h"
27
28 struct var_info {
29 nir_variable *var;
30
31 bool is_constant;
32 bool found_read;
33 bool duplicate;
34
35 /* Block that has all the variable stores. All the blocks with reads
36 * should be dominated by this block.
37 */
38 nir_block *block;
39
40 /* If is_constant, hold the collected constant data for this var. */
41 uint32_t constant_data_size;
42 void *constant_data;
43 };
44
45 static int
46 var_info_cmp(const void *_a, const void *_b)
47 {
48 const struct var_info *a = _a;
49 const struct var_info *b = _b;
50 uint32_t a_size = a->constant_data_size;
51 uint32_t b_size = b->constant_data_size;
52
53 if (a_size < b_size) {
54 return -1;
55 } else if (a_size > b_size) {
56 return 1;
57 } else if (a_size == 0) {
58 /* Don't call memcmp with invalid pointers. */
59 return 0;
60 } else {
61 return memcmp(a->constant_data, b->constant_data, a_size);
62 }
63 }
64
65 static nir_ssa_def *
66 build_constant_load(nir_builder *b, nir_deref_instr *deref,
67 glsl_type_size_align_func size_align)
68 {
69 nir_variable *var = nir_deref_instr_get_variable(deref);
70
71 const unsigned bit_size = glsl_get_bit_size(deref->type);
72 const unsigned num_components = glsl_get_vector_elements(deref->type);
73
74 UNUSED unsigned var_size, var_align;
75 size_align(var->type, &var_size, &var_align);
76 assert(var->data.location % var_align == 0);
77
78 UNUSED unsigned deref_size, deref_align;
79 size_align(deref->type, &deref_size, &deref_align);
80
81 nir_intrinsic_instr *load =
82 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_constant);
83 load->num_components = num_components;
84 nir_intrinsic_set_base(load, var->data.location);
85 nir_intrinsic_set_range(load, var_size);
86 nir_intrinsic_set_align(load, deref_align, 0);
87 load->src[0] = nir_src_for_ssa(nir_build_deref_offset(b, deref, size_align));
88 nir_ssa_dest_init(&load->instr, &load->dest,
89 num_components, bit_size, NULL);
90 nir_builder_instr_insert(b, &load->instr);
91
92 if (load->dest.ssa.bit_size < 8) {
93 /* Booleans are special-cased to be 32-bit */
94 assert(glsl_type_is_boolean(deref->type));
95 assert(deref_size == num_components * 4);
96 load->dest.ssa.bit_size = 32;
97 return nir_b2b1(b, &load->dest.ssa);
98 } else {
99 assert(deref_size == num_components * bit_size / 8);
100 return &load->dest.ssa;
101 }
102 }
103
104 static void
105 handle_constant_store(void *mem_ctx, struct var_info *info,
106 nir_deref_instr *deref, nir_const_value *val,
107 unsigned writemask,
108 glsl_type_size_align_func size_align)
109 {
110 assert(!nir_deref_instr_has_indirect(deref));
111 const unsigned bit_size = glsl_get_bit_size(deref->type);
112 const unsigned num_components = glsl_get_vector_elements(deref->type);
113
114 if (info->constant_data_size == 0) {
115 unsigned var_size, var_align;
116 size_align(info->var->type, &var_size, &var_align);
117 info->constant_data_size = var_size;
118 info->constant_data = rzalloc_size(mem_ctx, var_size);
119 }
120
121 char *dst = (char *)info->constant_data +
122 nir_deref_instr_get_const_offset(deref, size_align);
123
124 for (unsigned i = 0; i < num_components; i++) {
125 if (!(writemask & (1 << i)))
126 continue;
127
128 switch (bit_size) {
129 case 1:
130 /* Booleans are special-cased to be 32-bit */
131 ((int32_t *)dst)[i] = -(int)val[i].b;
132 break;
133
134 case 8:
135 ((uint8_t *)dst)[i] = val[i].u8;
136 break;
137
138 case 16:
139 ((uint16_t *)dst)[i] = val[i].u16;
140 break;
141
142 case 32:
143 ((uint32_t *)dst)[i] = val[i].u32;
144 break;
145
146 case 64:
147 ((uint64_t *)dst)[i] = val[i].u64;
148 break;
149
150 default:
151 unreachable("Invalid bit size");
152 }
153 }
154 }
155
156 /** Lower large constant variables to shader constant data
157 *
158 * This pass looks for large (type_size(var->type) > threshold) variables
159 * which are statically constant and moves them into shader constant data.
160 * This is especially useful when large tables are baked into the shader
161 * source code because they can be moved into a UBO by the driver to reduce
162 * register pressure and make indirect access cheaper.
163 */
164 bool
165 nir_opt_large_constants(nir_shader *shader,
166 glsl_type_size_align_func size_align,
167 unsigned threshold)
168 {
169 /* Default to a natural alignment if none is provided */
170 if (size_align == NULL)
171 size_align = glsl_get_natural_size_align_bytes;
172
173 /* This only works with a single entrypoint */
174 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
175
176 /* This pass can only be run once */
177 assert(shader->constant_data == NULL && shader->constant_data_size == 0);
178
179 unsigned num_locals = nir_function_impl_index_vars(impl);
180
181 if (num_locals == 0) {
182 nir_shader_preserve_all_metadata(shader);
183 return false;
184 }
185
186 struct var_info *var_infos = ralloc_array(NULL, struct var_info, num_locals);
187 nir_foreach_function_temp_variable(var, impl) {
188 var_infos[var->index] = (struct var_info) {
189 .var = var,
190 .is_constant = true,
191 .found_read = false,
192 };
193 }
194
195 nir_metadata_require(impl, nir_metadata_dominance);
196
197 /* First, walk through the shader and figure out what variables we can
198 * lower to the constant blob.
199 */
200 nir_foreach_block(block, impl) {
201 nir_foreach_instr(instr, block) {
202 if (instr->type == nir_instr_type_deref) {
203 /* If we ever see a complex use of a deref_var, we have to assume
204 * that variable is non-constant because we can't guarantee we
205 * will find all of the writers of that variable.
206 */
207 nir_deref_instr *deref = nir_instr_as_deref(instr);
208 if (deref->deref_type == nir_deref_type_var &&
209 deref->mode == nir_var_function_temp &&
210 nir_deref_instr_has_complex_use(deref))
211 var_infos[deref->var->index].is_constant = false;
212 continue;
213 }
214
215 if (instr->type != nir_instr_type_intrinsic)
216 continue;
217
218 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
219
220 bool src_is_const = false;
221 nir_deref_instr *src_deref = NULL, *dst_deref = NULL;
222 unsigned writemask = 0;
223 switch (intrin->intrinsic) {
224 case nir_intrinsic_store_deref:
225 dst_deref = nir_src_as_deref(intrin->src[0]);
226 src_is_const = nir_src_is_const(intrin->src[1]);
227 writemask = nir_intrinsic_write_mask(intrin);
228 break;
229
230 case nir_intrinsic_load_deref:
231 src_deref = nir_src_as_deref(intrin->src[0]);
232 break;
233
234 case nir_intrinsic_copy_deref:
235 assert(!"Lowering of copy_deref with large constants is prohibited");
236 break;
237
238 default:
239 continue;
240 }
241
242 if (dst_deref && dst_deref->mode == nir_var_function_temp) {
243 nir_variable *var = nir_deref_instr_get_variable(dst_deref);
244 if (var == NULL)
245 continue;
246
247 assert(var->data.mode == nir_var_function_temp);
248
249 struct var_info *info = &var_infos[var->index];
250 if (!info->is_constant)
251 continue;
252
253 if (!info->block)
254 info->block = block;
255
256 /* We only consider variables constant if they only have constant
257 * stores, all the stores come before any reads, and all stores
258 * come from the same block. We also can't handle indirect stores.
259 */
260 if (!src_is_const || info->found_read || block != info->block ||
261 nir_deref_instr_has_indirect(dst_deref)) {
262 info->is_constant = false;
263 } else {
264 nir_const_value *val = nir_src_as_const_value(intrin->src[1]);
265 handle_constant_store(var_infos, info, dst_deref, val, writemask,
266 size_align);
267 }
268 }
269
270 if (src_deref && src_deref->mode == nir_var_function_temp) {
271 nir_variable *var = nir_deref_instr_get_variable(src_deref);
272 if (var == NULL)
273 continue;
274
275 assert(var->data.mode == nir_var_function_temp);
276
277 /* We only consider variables constant if all the reads are
278 * dominated by the block that writes to it.
279 */
280 struct var_info *info = &var_infos[var->index];
281 if (!info->is_constant)
282 continue;
283
284 if (!info->block || !nir_block_dominates(info->block, block))
285 info->is_constant = false;
286
287 info->found_read = true;
288 }
289 }
290 }
291
292 /* Allocate constant data space for each variable that just has constant
293 * data. We sort them by size and content so we can easily find
294 * duplicates.
295 */
296 shader->constant_data_size = 0;
297 qsort(var_infos, num_locals, sizeof(struct var_info), var_info_cmp);
298 for (int i = 0; i < num_locals; i++) {
299 struct var_info *info = &var_infos[i];
300
301 /* Fix up indices after we sorted. */
302 info->var->index = i;
303
304 if (!info->is_constant)
305 continue;
306
307 unsigned var_size, var_align;
308 size_align(info->var->type, &var_size, &var_align);
309 if (var_size <= threshold || !info->found_read) {
310 /* Don't bother lowering small stuff or data that's never read */
311 info->is_constant = false;
312 continue;
313 }
314
315 if (i > 0 && var_info_cmp(info, &var_infos[i - 1]) == 0) {
316 info->var->data.location = var_infos[i - 1].var->data.location;
317 info->duplicate = true;
318 } else {
319 info->var->data.location = ALIGN_POT(shader->constant_data_size, var_align);
320 shader->constant_data_size = info->var->data.location + var_size;
321 }
322 }
323
324 if (shader->constant_data_size == 0) {
325 nir_shader_preserve_all_metadata(shader);
326 ralloc_free(var_infos);
327 return false;
328 }
329
330 shader->constant_data = rzalloc_size(shader, shader->constant_data_size);
331 for (int i = 0; i < num_locals; i++) {
332 struct var_info *info = &var_infos[i];
333 if (!info->duplicate && info->is_constant) {
334 memcpy((char *)shader->constant_data + info->var->data.location,
335 info->constant_data, info->constant_data_size);
336 }
337 }
338
339 nir_builder b;
340 nir_builder_init(&b, impl);
341
342 nir_foreach_block(block, impl) {
343 nir_foreach_instr_safe(instr, block) {
344 if (instr->type != nir_instr_type_intrinsic)
345 continue;
346
347 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
348
349 switch (intrin->intrinsic) {
350 case nir_intrinsic_load_deref: {
351 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
352 if (deref->mode != nir_var_function_temp)
353 continue;
354
355 nir_variable *var = nir_deref_instr_get_variable(deref);
356 if (var == NULL)
357 continue;
358
359 struct var_info *info = &var_infos[var->index];
360 if (info->is_constant) {
361 b.cursor = nir_after_instr(&intrin->instr);
362 nir_ssa_def *val = build_constant_load(&b, deref, size_align);
363 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
364 nir_src_for_ssa(val));
365 nir_instr_remove(&intrin->instr);
366 nir_deref_instr_remove_if_unused(deref);
367 }
368 break;
369 }
370
371 case nir_intrinsic_store_deref: {
372 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
373 if (deref->mode != nir_var_function_temp)
374 continue;
375
376 nir_variable *var = nir_deref_instr_get_variable(deref);
377 if (var == NULL)
378 continue;
379
380 struct var_info *info = &var_infos[var->index];
381 if (info->is_constant) {
382 nir_instr_remove(&intrin->instr);
383 nir_deref_instr_remove_if_unused(deref);
384 }
385 break;
386 }
387 case nir_intrinsic_copy_deref:
388 default:
389 continue;
390 }
391 }
392 }
393
394 /* Clean up the now unused variables */
395 for (int i = 0; i < num_locals; i++) {
396 struct var_info *info = &var_infos[i];
397 if (info->is_constant)
398 exec_node_remove(&info->var->node);
399 }
400
401 ralloc_free(var_infos);
402
403 nir_metadata_preserve(impl, nir_metadata_block_index |
404 nir_metadata_dominance);
405 return true;
406 }