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