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