2 * Copyright © 2019 Google, Inc.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include "ir3_compiler.h"
26 #include "compiler/nir/nir.h"
27 #include "compiler/nir/nir_builder.h"
28 #include "util/u_math.h"
30 static inline struct ir3_ubo_range
31 get_ubo_load_range(nir_intrinsic_instr
*instr
)
33 struct ir3_ubo_range r
;
35 const int offset
= nir_src_as_uint(instr
->src
[1]);
36 const int bytes
= nir_intrinsic_dest_components(instr
) * 4;
38 r
.start
= ROUND_DOWN_TO(offset
, 16 * 4);
39 r
.end
= ALIGN(offset
+ bytes
, 16 * 4);
44 static struct ir3_ubo_range
*
45 get_existing_range(nir_intrinsic_instr
*instr
,
46 struct ir3_ubo_analysis_state
*state
,
49 unsigned block
, base
= 0;
51 if (nir_src_is_const(instr
->src
[0])) {
52 block
= nir_src_as_uint(instr
->src
[0]);
55 nir_intrinsic_instr
*rsrc
= ir3_bindless_resource(instr
->src
[0]);
56 if (rsrc
&& nir_src_is_const(rsrc
->src
[0])) {
57 block
= nir_src_as_uint(rsrc
->src
[0]);
58 base
= nir_intrinsic_desc_set(rsrc
);
64 for (int i
= 0; i
< IR3_MAX_UBO_PUSH_RANGES
; i
++) {
65 struct ir3_ubo_range
*range
= &state
->range
[i
];
66 if (range
->end
< range
->start
) {
67 /* We don't have a matching range, but there are more available.
71 range
->bindless_base
= base
;
72 range
->bindless
= bindless
;
77 } else if (range
->block
== block
&& range
->bindless_base
== base
&&
78 range
->bindless
== bindless
) {
87 gather_ubo_ranges(nir_shader
*nir
, nir_intrinsic_instr
*instr
,
88 struct ir3_ubo_analysis_state
*state
)
90 struct ir3_ubo_range
*old_r
= get_existing_range(instr
, state
, true);
94 if (!nir_src_is_const(instr
->src
[1])) {
95 if (!old_r
->bindless
&& old_r
->block
== 0) {
96 /* If this is an indirect on UBO 0, we'll still lower it back to
97 * load_uniform. Set the range to cover all of UBO 0.
99 state
->range
[0].start
= 0;
100 state
->range
[0].end
= ALIGN(nir
->num_uniforms
* 16, 16 * 4);
106 const struct ir3_ubo_range r
= get_ubo_load_range(instr
);
108 /* if UBO lowering is disabled, we still want to lower block 0
109 * (which is normal uniforms):
111 if ((old_r
->bindless
|| old_r
->block
!= 0) && (ir3_shader_debug
& IR3_DBG_NOUBOOPT
))
114 if (r
.start
< old_r
->start
)
115 old_r
->start
= r
.start
;
116 if (old_r
->end
< r
.end
)
120 /* For indirect offset, it is common to see a pattern of multiple
121 * loads with the same base, but different constant offset, ie:
123 * vec1 32 ssa_33 = iadd ssa_base, const_offset
124 * vec4 32 ssa_34 = intrinsic load_uniform (ssa_33) (base=N, 0, 0)
126 * Detect this, and peel out the const_offset part, to end up with:
128 * vec4 32 ssa_34 = intrinsic load_uniform (ssa_base) (base=N+const_offset, 0, 0)
132 * vec1 32 ssa_33 = imad24_ir3 a, b, const_offset
133 * vec4 32 ssa_34 = intrinsic load_uniform (ssa_33) (base=N, 0, 0)
135 * Can be converted to:
137 * vec1 32 ssa_base = imul24 a, b
138 * vec4 32 ssa_34 = intrinsic load_uniform (ssa_base) (base=N+const_offset, 0, 0)
140 * This gives the other opt passes something much easier to work
141 * with (ie. not requiring value range tracking)
144 handle_partial_const(nir_builder
*b
, nir_ssa_def
**srcp
, unsigned *offp
)
146 if ((*srcp
)->parent_instr
->type
!= nir_instr_type_alu
)
149 nir_alu_instr
*alu
= nir_instr_as_alu((*srcp
)->parent_instr
);
151 if (alu
->op
== nir_op_imad24_ir3
) {
152 /* This case is slightly more complicated as we need to
153 * replace the imad24_ir3 with an imul24:
155 if (!nir_src_is_const(alu
->src
[2].src
))
158 *offp
+= nir_src_as_uint(alu
->src
[2].src
);
159 *srcp
= nir_imul24(b
, nir_ssa_for_alu_src(b
, alu
, 0),
160 nir_ssa_for_alu_src(b
, alu
, 1));
165 if (alu
->op
!= nir_op_iadd
)
168 if (!(alu
->src
[0].src
.is_ssa
&& alu
->src
[1].src
.is_ssa
))
171 if (nir_src_is_const(alu
->src
[0].src
)) {
172 *offp
+= nir_src_as_uint(alu
->src
[0].src
);
173 *srcp
= alu
->src
[1].src
.ssa
;
174 } else if (nir_src_is_const(alu
->src
[1].src
)) {
175 *srcp
= alu
->src
[0].src
.ssa
;
176 *offp
+= nir_src_as_uint(alu
->src
[1].src
);
181 lower_ubo_load_to_uniform(nir_intrinsic_instr
*instr
, nir_builder
*b
,
182 struct ir3_ubo_analysis_state
*state
)
184 /* We don't lower dynamic block index UBO loads to load_uniform, but we
185 * could probably with some effort determine a block stride in number of
188 struct ir3_ubo_range
*range
= get_existing_range(instr
, state
, false);
192 if (range
->bindless
|| range
->block
> 0) {
193 /* We don't lower dynamic array indexing either, but we definitely should.
194 * We don't have a good way of determining the range of the dynamic
195 * access, so for now just fall back to pulling.
197 if (!nir_src_is_const(instr
->src
[1]))
200 /* After gathering the UBO access ranges, we limit the total
201 * upload. Reject if we're now outside the range.
203 const struct ir3_ubo_range r
= get_ubo_load_range(instr
);
204 if (!(range
->start
<= r
.start
&& r
.end
<= range
->end
))
208 b
->cursor
= nir_before_instr(&instr
->instr
);
210 nir_ssa_def
*ubo_offset
= nir_ssa_for_src(b
, instr
->src
[1], 1);
211 unsigned const_offset
= 0;
213 handle_partial_const(b
, &ubo_offset
, &const_offset
);
215 /* UBO offset is in bytes, but uniform offset is in units of
216 * dwords, so we need to divide by 4 (right-shift by 2). And
217 * also the same for the constant part of the offset:
219 nir_ssa_def
*new_offset
= ir3_nir_try_propagate_bit_shift(b
, ubo_offset
, -2);
220 nir_ssa_def
*uniform_offset
= NULL
;
222 uniform_offset
= new_offset
;
224 uniform_offset
= nir_ushr(b
, ubo_offset
, nir_imm_int(b
, 2));
227 debug_assert(!(const_offset
& 0x3));
230 const int range_offset
= (range
->offset
- range
->start
) / 4;
231 const_offset
+= range_offset
;
233 nir_intrinsic_instr
*uniform
=
234 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_load_uniform
);
235 uniform
->num_components
= instr
->num_components
;
236 uniform
->src
[0] = nir_src_for_ssa(uniform_offset
);
237 nir_intrinsic_set_base(uniform
, const_offset
);
238 nir_ssa_dest_init(&uniform
->instr
, &uniform
->dest
,
239 uniform
->num_components
, instr
->dest
.ssa
.bit_size
,
240 instr
->dest
.ssa
.name
);
241 nir_builder_instr_insert(b
, &uniform
->instr
);
242 nir_ssa_def_rewrite_uses(&instr
->dest
.ssa
,
243 nir_src_for_ssa(&uniform
->dest
.ssa
));
245 nir_instr_remove(&instr
->instr
);
247 state
->lower_count
++;
251 ir3_nir_analyze_ubo_ranges(nir_shader
*nir
, struct ir3_shader
*shader
)
253 struct ir3_ubo_analysis_state
*state
= &shader
->ubo_state
;
255 memset(state
, 0, sizeof(*state
));
256 for (int i
= 0; i
< IR3_MAX_UBO_PUSH_RANGES
; i
++) {
257 state
->range
[i
].start
= UINT32_MAX
;
260 nir_foreach_function (function
, nir
) {
261 if (function
->impl
) {
262 nir_foreach_block (block
, function
->impl
) {
263 nir_foreach_instr (instr
, block
) {
264 if (instr
->type
== nir_instr_type_intrinsic
&&
265 nir_instr_as_intrinsic(instr
)->intrinsic
== nir_intrinsic_load_ubo
)
266 gather_ubo_ranges(nir
, nir_instr_as_intrinsic(instr
), state
);
272 /* For now, everything we upload is accessed statically and thus will be
273 * used by the shader. Once we can upload dynamically indexed data, we may
274 * upload sparsely accessed arrays, at which point we probably want to
275 * give priority to smaller UBOs, on the assumption that big UBOs will be
276 * accessed dynamically. Alternatively, we can track statically and
277 * dynamically accessed ranges separately and upload static rangtes
280 const uint32_t max_upload
= 16 * 1024;
281 uint32_t offset
= shader
->const_state
.num_reserved_user_consts
* 16;
282 state
->num_enabled
= ARRAY_SIZE(state
->range
);
283 for (uint32_t i
= 0; i
< ARRAY_SIZE(state
->range
); i
++) {
284 if (state
->range
[i
].start
>= state
->range
[i
].end
) {
285 state
->num_enabled
= i
;
289 uint32_t range_size
= state
->range
[i
].end
- state
->range
[i
].start
;
291 debug_assert(offset
<= max_upload
);
292 state
->range
[i
].offset
= offset
;
293 if (offset
+ range_size
> max_upload
) {
294 range_size
= max_upload
- offset
;
295 state
->range
[i
].end
= state
->range
[i
].start
+ range_size
;
297 offset
+= range_size
;
300 state
->size
= offset
;
302 nir_foreach_function (function
, nir
) {
303 if (function
->impl
) {
305 nir_builder_init(&builder
, function
->impl
);
306 nir_foreach_block (block
, function
->impl
) {
307 nir_foreach_instr_safe (instr
, block
) {
308 if (instr
->type
== nir_instr_type_intrinsic
&&
309 nir_instr_as_intrinsic(instr
)->intrinsic
== nir_intrinsic_load_ubo
)
310 lower_ubo_load_to_uniform(nir_instr_as_intrinsic(instr
), &builder
, state
);
314 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
|
315 nir_metadata_dominance
);
319 return state
->lower_count
> 0;