2 * Copyright © 2018 Intel Corporation
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:
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
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
25 #include "nir_builder.h"
26 #include "nir_deref.h"
35 /* Block that has all the variable stores. All the blocks with reads
36 * should be dominated by this block.
40 /* If is_constant, hold the collected constant data for this var. */
41 uint32_t constant_data_size
;
46 var_info_cmp(const void *_a
, const void *_b
)
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
;
53 if (a_size
< b_size
) {
55 } else if (a_size
> b_size
) {
57 } else if (a_size
== 0) {
58 /* Don't call memcmp with invalid pointers. */
61 return memcmp(a
->constant_data
, b
->constant_data
, a_size
);
66 build_constant_load(nir_builder
*b
, nir_deref_instr
*deref
,
67 glsl_type_size_align_func size_align
)
69 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
71 const unsigned bit_size
= glsl_get_bit_size(deref
->type
);
72 const unsigned num_components
= glsl_get_vector_elements(deref
->type
);
74 UNUSED
unsigned var_size
, var_align
;
75 size_align(var
->type
, &var_size
, &var_align
);
76 assert(var
->data
.location
% var_align
== 0);
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
);
88 if (load
->dest
.ssa
.bit_size
< 8) {
89 /* Booleans are special-cased to be 32-bit */
90 assert(glsl_type_is_boolean(deref
->type
));
91 load
->dest
.ssa
.bit_size
= 32;
92 return nir_b2b1(b
, &load
->dest
.ssa
);
94 return &load
->dest
.ssa
;
99 handle_constant_store(void *mem_ctx
, struct var_info
*info
,
100 nir_deref_instr
*deref
, nir_const_value
*val
,
102 glsl_type_size_align_func size_align
)
104 assert(!nir_deref_instr_has_indirect(deref
));
105 const unsigned bit_size
= glsl_get_bit_size(deref
->type
);
106 const unsigned num_components
= glsl_get_vector_elements(deref
->type
);
108 if (info
->constant_data_size
== 0) {
109 unsigned var_size
, var_align
;
110 size_align(info
->var
->type
, &var_size
, &var_align
);
111 info
->constant_data_size
= var_size
;
112 info
->constant_data
= rzalloc_size(mem_ctx
, var_size
);
115 char *dst
= (char *)info
->constant_data
+
116 nir_deref_instr_get_const_offset(deref
, size_align
);
118 for (unsigned i
= 0; i
< num_components
; i
++) {
119 if (!(writemask
& (1 << i
)))
124 /* Booleans are special-cased to be 32-bit */
125 ((int32_t *)dst
)[i
] = -(int)val
[i
].b
;
129 ((uint8_t *)dst
)[i
] = val
[i
].u8
;
133 ((uint16_t *)dst
)[i
] = val
[i
].u16
;
137 ((uint32_t *)dst
)[i
] = val
[i
].u32
;
141 ((uint64_t *)dst
)[i
] = val
[i
].u64
;
145 unreachable("Invalid bit size");
150 /** Lower large constant variables to shader constant data
152 * This pass looks for large (type_size(var->type) > threshold) variables
153 * which are statically constant and moves them into shader constant data.
154 * This is especially useful when large tables are baked into the shader
155 * source code because they can be moved into a UBO by the driver to reduce
156 * register pressure and make indirect access cheaper.
159 nir_opt_large_constants(nir_shader
*shader
,
160 glsl_type_size_align_func size_align
,
163 /* Default to a natural alignment if none is provided */
164 if (size_align
== NULL
)
165 size_align
= glsl_get_natural_size_align_bytes
;
167 /* This only works with a single entrypoint */
168 nir_function_impl
*impl
= nir_shader_get_entrypoint(shader
);
170 /* This pass can only be run once */
171 assert(shader
->constant_data
== NULL
&& shader
->constant_data_size
== 0);
173 unsigned num_locals
= exec_list_length(&impl
->locals
);
174 nir_index_vars(shader
, impl
, nir_var_function_temp
);
179 struct var_info
*var_infos
= ralloc_array(NULL
, struct var_info
, num_locals
);
180 nir_foreach_variable(var
, &impl
->locals
) {
181 var_infos
[var
->index
] = (struct var_info
) {
188 nir_metadata_require(impl
, nir_metadata_dominance
);
190 /* First, walk through the shader and figure out what variables we can
191 * lower to the constant blob.
193 nir_foreach_block(block
, impl
) {
194 nir_foreach_instr(instr
, block
) {
195 if (instr
->type
!= nir_instr_type_intrinsic
)
198 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
200 bool src_is_const
= false;
201 nir_deref_instr
*src_deref
= NULL
, *dst_deref
= NULL
;
202 unsigned writemask
= 0;
203 switch (intrin
->intrinsic
) {
204 case nir_intrinsic_store_deref
:
205 dst_deref
= nir_src_as_deref(intrin
->src
[0]);
206 src_is_const
= nir_src_is_const(intrin
->src
[1]);
207 writemask
= nir_intrinsic_write_mask(intrin
);
210 case nir_intrinsic_load_deref
:
211 src_deref
= nir_src_as_deref(intrin
->src
[0]);
214 case nir_intrinsic_copy_deref
:
215 assert(!"Lowering of copy_deref with large constants is prohibited");
222 if (dst_deref
&& dst_deref
->mode
== nir_var_function_temp
) {
223 nir_variable
*var
= nir_deref_instr_get_variable(dst_deref
);
224 assert(var
->data
.mode
== nir_var_function_temp
);
226 struct var_info
*info
= &var_infos
[var
->index
];
227 if (!info
->is_constant
)
233 /* We only consider variables constant if they only have constant
234 * stores, all the stores come before any reads, and all stores
235 * come from the same block. We also can't handle indirect stores.
237 if (!src_is_const
|| info
->found_read
|| block
!= info
->block
||
238 nir_deref_instr_has_indirect(dst_deref
)) {
239 info
->is_constant
= false;
241 nir_const_value
*val
= nir_src_as_const_value(intrin
->src
[1]);
242 handle_constant_store(var_infos
, info
, dst_deref
, val
, writemask
,
247 if (src_deref
&& src_deref
->mode
== nir_var_function_temp
) {
248 nir_variable
*var
= nir_deref_instr_get_variable(src_deref
);
249 assert(var
->data
.mode
== nir_var_function_temp
);
251 /* We only consider variables constant if all the reads are
252 * dominated by the block that writes to it.
254 struct var_info
*info
= &var_infos
[var
->index
];
255 if (!info
->is_constant
)
258 if (!info
->block
|| !nir_block_dominates(info
->block
, block
))
259 info
->is_constant
= false;
261 info
->found_read
= true;
266 /* Allocate constant data space for each variable that just has constant
267 * data. We sort them by size and content so we can easily find
270 shader
->constant_data_size
= 0;
271 qsort(var_infos
, num_locals
, sizeof(struct var_info
), var_info_cmp
);
272 for (int i
= 0; i
< num_locals
; i
++) {
273 struct var_info
*info
= &var_infos
[i
];
275 /* Fix up indices after we sorted. */
276 info
->var
->index
= i
;
278 if (!info
->is_constant
)
281 unsigned var_size
, var_align
;
282 size_align(info
->var
->type
, &var_size
, &var_align
);
283 if (var_size
<= threshold
|| !info
->found_read
) {
284 /* Don't bother lowering small stuff or data that's never read */
285 info
->is_constant
= false;
289 if (i
> 0 && var_info_cmp(info
, &var_infos
[i
- 1]) == 0) {
290 info
->var
->data
.location
= var_infos
[i
- 1].var
->data
.location
;
291 info
->duplicate
= true;
293 info
->var
->data
.location
= ALIGN_POT(shader
->constant_data_size
, var_align
);
294 shader
->constant_data_size
= info
->var
->data
.location
+ var_size
;
298 if (shader
->constant_data_size
== 0) {
299 ralloc_free(var_infos
);
303 shader
->constant_data
= rzalloc_size(shader
, shader
->constant_data_size
);
304 for (int i
= 0; i
< num_locals
; i
++) {
305 struct var_info
*info
= &var_infos
[i
];
306 if (!info
->duplicate
&& info
->is_constant
) {
307 memcpy((char *)shader
->constant_data
+ info
->var
->data
.location
,
308 info
->constant_data
, info
->constant_data_size
);
313 nir_builder_init(&b
, impl
);
315 nir_foreach_block(block
, impl
) {
316 nir_foreach_instr_safe(instr
, block
) {
317 if (instr
->type
!= nir_instr_type_intrinsic
)
320 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
322 switch (intrin
->intrinsic
) {
323 case nir_intrinsic_load_deref
: {
324 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
325 if (deref
->mode
!= nir_var_function_temp
)
328 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
329 struct var_info
*info
= &var_infos
[var
->index
];
330 if (info
->is_constant
) {
331 b
.cursor
= nir_after_instr(&intrin
->instr
);
332 nir_ssa_def
*val
= build_constant_load(&b
, deref
, size_align
);
333 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
334 nir_src_for_ssa(val
));
335 nir_instr_remove(&intrin
->instr
);
336 nir_deref_instr_remove_if_unused(deref
);
341 case nir_intrinsic_store_deref
: {
342 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
343 if (deref
->mode
!= nir_var_function_temp
)
346 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
347 struct var_info
*info
= &var_infos
[var
->index
];
348 if (info
->is_constant
) {
349 nir_instr_remove(&intrin
->instr
);
350 nir_deref_instr_remove_if_unused(deref
);
354 case nir_intrinsic_copy_deref
:
361 /* Clean up the now unused variables */
362 for (int i
= 0; i
< num_locals
; i
++) {
363 struct var_info
*info
= &var_infos
[i
];
364 if (info
->is_constant
)
365 exec_node_remove(&info
->var
->node
);
368 ralloc_free(var_infos
);
370 nir_metadata_preserve(impl
, nir_metadata_block_index
|
371 nir_metadata_dominance
);