nir/large_constants: De-duplicate constants
[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 nir_intrinsic_instr *load =
79 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_constant);
80 load->num_components = num_components;
81 nir_intrinsic_set_base(load, var->data.location);
82 nir_intrinsic_set_range(load, var_size);
83 load->src[0] = nir_src_for_ssa(nir_build_deref_offset(b, deref, size_align));
84 nir_ssa_dest_init(&load->instr, &load->dest,
85 num_components, bit_size, NULL);
86 nir_builder_instr_insert(b, &load->instr);
87
88 if (load->dest.ssa.bit_size < 8) {
89 /* Booleans are special-cased to be 32-bit
90 *
91 * Ideally, for drivers that can handle 32-bit booleans, we wouldn't
92 * emit the i2b here. However, at this point, the driver is likely to
93 * still have 1-bit booleans so we need to at least convert bit sizes.
94 * Unfortunately, we don't have a good way to annotate the load as
95 * loading a known boolean value so the optimizer isn't going to be
96 * able to get rid of the conversion. Some day, we may solve that
97 * problem but not today.
98 */
99 assert(glsl_type_is_boolean(deref->type));
100 load->dest.ssa.bit_size = 32;
101 return nir_i2b(b, &load->dest.ssa);
102 } else {
103 return &load->dest.ssa;
104 }
105 }
106
107 static void
108 handle_constant_store(void *mem_ctx, struct var_info *info,
109 nir_deref_instr *deref, nir_const_value *val,
110 glsl_type_size_align_func size_align)
111 {
112 assert(!nir_deref_instr_has_indirect(deref));
113 const unsigned bit_size = glsl_get_bit_size(deref->type);
114 const unsigned num_components = glsl_get_vector_elements(deref->type);
115
116 if (info->constant_data_size == 0) {
117 unsigned var_size, var_align;
118 size_align(info->var->type, &var_size, &var_align);
119 info->constant_data_size = var_size;
120 info->constant_data = rzalloc_size(mem_ctx, var_size);
121 }
122
123 char *dst = (char *)info->constant_data +
124 nir_deref_instr_get_const_offset(deref, size_align);
125
126 switch (bit_size) {
127 case 1:
128 /* Booleans are special-cased to be 32-bit */
129 for (unsigned i = 0; i < num_components; i++)
130 ((int32_t *)dst)[i] = -(int)val[i].b;
131 break;
132
133 case 8:
134 for (unsigned i = 0; i < num_components; i++)
135 ((uint8_t *)dst)[i] = val[i].u8;
136 break;
137
138 case 16:
139 for (unsigned i = 0; i < num_components; i++)
140 ((uint16_t *)dst)[i] = val[i].u16;
141 break;
142
143 case 32:
144 for (unsigned i = 0; i < num_components; i++)
145 ((uint32_t *)dst)[i] = val[i].u32;
146 break;
147
148 case 64:
149 for (unsigned i = 0; i < num_components; i++)
150 ((uint64_t *)dst)[i] = val[i].u64;
151 break;
152
153 default:
154 unreachable("Invalid bit size");
155 }
156 }
157
158 /** Lower large constant variables to shader constant data
159 *
160 * This pass looks for large (type_size(var->type) > threshold) variables
161 * which are statically constant and moves them into shader constant data.
162 * This is especially useful when large tables are baked into the shader
163 * source code because they can be moved into a UBO by the driver to reduce
164 * register pressure and make indirect access cheaper.
165 */
166 bool
167 nir_opt_large_constants(nir_shader *shader,
168 glsl_type_size_align_func size_align,
169 unsigned threshold)
170 {
171 /* Default to a natural alignment if none is provided */
172 if (size_align == NULL)
173 size_align = glsl_get_natural_size_align_bytes;
174
175 /* This only works with a single entrypoint */
176 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
177
178 /* This pass can only be run once */
179 assert(shader->constant_data == NULL && shader->constant_data_size == 0);
180
181 /* The index parameter is unused for local variables so we'll use it for
182 * indexing into our array of variable metadata.
183 */
184 unsigned num_locals = 0;
185 nir_foreach_variable(var, &impl->locals)
186 var->data.index = num_locals++;
187
188 if (num_locals == 0)
189 return false;
190
191 struct var_info *var_infos = ralloc_array(NULL, struct var_info, num_locals);
192 nir_foreach_variable(var, &impl->locals) {
193 var_infos[var->data.index] = (struct var_info) {
194 .var = var,
195 .is_constant = true,
196 .found_read = false,
197 };
198 }
199
200 nir_metadata_require(impl, nir_metadata_dominance);
201
202 /* First, walk through the shader and figure out what variables we can
203 * lower to the constant blob.
204 */
205 nir_foreach_block(block, impl) {
206 nir_foreach_instr(instr, block) {
207 if (instr->type != nir_instr_type_intrinsic)
208 continue;
209
210 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
211
212 bool src_is_const = false;
213 nir_deref_instr *src_deref = NULL, *dst_deref = NULL;
214 switch (intrin->intrinsic) {
215 case nir_intrinsic_store_deref:
216 dst_deref = nir_src_as_deref(intrin->src[0]);
217 src_is_const = nir_src_is_const(intrin->src[1]);
218 break;
219
220 case nir_intrinsic_load_deref:
221 src_deref = nir_src_as_deref(intrin->src[0]);
222 break;
223
224 case nir_intrinsic_copy_deref:
225 /* We always assume the src and therefore the dst are not
226 * constants here. Copy and constant propagation passes should
227 * have taken care of this in most cases anyway.
228 */
229 dst_deref = nir_src_as_deref(intrin->src[0]);
230 src_deref = nir_src_as_deref(intrin->src[1]);
231 src_is_const = false;
232 break;
233
234 default:
235 continue;
236 }
237
238 if (dst_deref && dst_deref->mode == nir_var_function_temp) {
239 nir_variable *var = nir_deref_instr_get_variable(dst_deref);
240 assert(var->data.mode == nir_var_function_temp);
241
242 struct var_info *info = &var_infos[var->data.index];
243 if (!info->is_constant)
244 continue;
245
246 if (!info->block)
247 info->block = block;
248
249 /* We only consider variables constant if they only have constant
250 * stores, all the stores come before any reads, and all stores
251 * come from the same block. We also can't handle indirect stores.
252 */
253 if (!src_is_const || info->found_read || block != info->block ||
254 nir_deref_instr_has_indirect(dst_deref)) {
255 info->is_constant = false;
256 } else {
257 nir_const_value *val = nir_src_as_const_value(intrin->src[1]);
258 handle_constant_store(var_infos, info, dst_deref, val, size_align);
259 }
260 }
261
262 if (src_deref && src_deref->mode == nir_var_function_temp) {
263 nir_variable *var = nir_deref_instr_get_variable(src_deref);
264 assert(var->data.mode == nir_var_function_temp);
265
266 /* We only consider variables constant if all the reads are
267 * dominated by the block that writes to it.
268 */
269 struct var_info *info = &var_infos[var->data.index];
270 if (!info->is_constant)
271 continue;
272
273 if (!info->block || !nir_block_dominates(info->block, block))
274 info->is_constant = false;
275
276 info->found_read = true;
277 }
278 }
279 }
280
281 /* Allocate constant data space for each variable that just has constant
282 * data. We sort them by size and content so we can easily find
283 * duplicates.
284 */
285 shader->constant_data_size = 0;
286 qsort(var_infos, num_locals, sizeof(struct var_info), var_info_cmp);
287 for (int i = 0; i < num_locals; i++) {
288 struct var_info *info = &var_infos[i];
289
290 /* Fix up indices after we sorted. */
291 info->var->data.index = i;
292
293 if (!info->is_constant)
294 continue;
295
296 unsigned var_size, var_align;
297 size_align(info->var->type, &var_size, &var_align);
298 if (var_size <= threshold || !info->found_read) {
299 /* Don't bother lowering small stuff or data that's never read */
300 info->is_constant = false;
301 continue;
302 }
303
304 if (i > 0 && var_info_cmp(info, &var_infos[i - 1]) == 0) {
305 info->var->data.location = var_infos[i - 1].var->data.location;
306 info->duplicate = true;
307 } else {
308 info->var->data.location = ALIGN_POT(shader->constant_data_size, var_align);
309 shader->constant_data_size = info->var->data.location + var_size;
310 }
311 }
312
313 if (shader->constant_data_size == 0) {
314 ralloc_free(var_infos);
315 return false;
316 }
317
318 shader->constant_data = rzalloc_size(shader, shader->constant_data_size);
319 for (int i = 0; i < num_locals; i++) {
320 struct var_info *info = &var_infos[i];
321 if (!info->duplicate) {
322 memcpy((char *)shader->constant_data + info->var->data.location,
323 info->constant_data, info->constant_data_size);
324 }
325 }
326
327 nir_builder b;
328 nir_builder_init(&b, impl);
329
330 nir_foreach_block(block, impl) {
331 nir_foreach_instr_safe(instr, block) {
332 if (instr->type != nir_instr_type_intrinsic)
333 continue;
334
335 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
336
337 switch (intrin->intrinsic) {
338 case nir_intrinsic_load_deref: {
339 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
340 if (deref->mode != nir_var_function_temp)
341 continue;
342
343 nir_variable *var = nir_deref_instr_get_variable(deref);
344 struct var_info *info = &var_infos[var->data.index];
345 if (info->is_constant) {
346 b.cursor = nir_after_instr(&intrin->instr);
347 nir_ssa_def *val = build_constant_load(&b, deref, size_align);
348 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
349 nir_src_for_ssa(val));
350 nir_instr_remove(&intrin->instr);
351 nir_deref_instr_remove_if_unused(deref);
352 }
353 break;
354 }
355
356 case nir_intrinsic_store_deref: {
357 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
358 if (deref->mode != nir_var_function_temp)
359 continue;
360
361 nir_variable *var = nir_deref_instr_get_variable(deref);
362 struct var_info *info = &var_infos[var->data.index];
363 if (info->is_constant) {
364 nir_instr_remove(&intrin->instr);
365 nir_deref_instr_remove_if_unused(deref);
366 }
367 break;
368 }
369
370 case nir_intrinsic_copy_deref: {
371 nir_deref_instr *deref = nir_src_as_deref(intrin->src[1]);
372 if (deref->mode != nir_var_function_temp)
373 continue;
374
375 nir_variable *var = nir_deref_instr_get_variable(deref);
376 struct var_info *info = &var_infos[var->data.index];
377 if (info->is_constant) {
378 b.cursor = nir_after_instr(&intrin->instr);
379 nir_ssa_def *val = build_constant_load(&b, deref, size_align);
380 nir_store_deref(&b, nir_src_as_deref(intrin->src[0]), val, ~0);
381 nir_instr_remove(&intrin->instr);
382 nir_deref_instr_remove_if_unused(deref);
383 }
384 break;
385 }
386
387 default:
388 continue;
389 }
390 }
391 }
392
393 /* Clean up the now unused variables */
394 for (int i = 0; i < num_locals; i++) {
395 struct var_info *info = &var_infos[i];
396 if (info->is_constant)
397 exec_node_remove(&info->var->node);
398 }
399
400 ralloc_free(var_infos);
401
402 nir_metadata_preserve(impl, nir_metadata_block_index |
403 nir_metadata_dominance);
404 return true;
405 }