freedreno/ir3: use uniform base
[mesa.git] / src / freedreno / ir3 / ir3_nir_analyze_ubo_ranges.c
1 /*
2 * Copyright © 2019 Google, Inc.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "ir3_nir.h"
25 #include "ir3_compiler.h"
26 #include "compiler/nir/nir.h"
27 #include "compiler/nir/nir_builder.h"
28 #include "mesa/main/macros.h"
29
30 static inline struct ir3_ubo_range
31 get_ubo_load_range(nir_intrinsic_instr *instr)
32 {
33 struct ir3_ubo_range r;
34
35 const int offset = nir_src_as_uint(instr->src[1]);
36 const int bytes = nir_intrinsic_dest_components(instr) * 4;
37
38 r.start = ROUND_DOWN_TO(offset, 16 * 4);
39 r.end = ALIGN(offset + bytes, 16 * 4);
40
41 return r;
42 }
43
44 static void
45 gather_ubo_ranges(nir_shader *nir, nir_intrinsic_instr *instr,
46 struct ir3_ubo_analysis_state *state)
47 {
48 if (!nir_src_is_const(instr->src[0]))
49 return;
50
51 if (!nir_src_is_const(instr->src[1])) {
52 if (nir_src_as_uint(instr->src[0]) == 0) {
53 /* If this is an indirect on UBO 0, we'll still lower it back to
54 * load_uniform. Set the range to cover all of UBO 0.
55 */
56 state->range[0].end = align(nir->num_uniforms * 16, 16 * 4);
57 }
58
59 return;
60 }
61
62 const struct ir3_ubo_range r = get_ubo_load_range(instr);
63 const uint32_t block = nir_src_as_uint(instr->src[0]);
64
65 /* if UBO lowering is disabled, we still want to lower block 0
66 * (which is normal uniforms):
67 */
68 if ((block > 0) && (ir3_shader_debug & IR3_DBG_NOUBOOPT))
69 return;
70
71 if (r.start < state->range[block].start)
72 state->range[block].start = r.start;
73 if (state->range[block].end < r.end)
74 state->range[block].end = r.end;
75 }
76
77 static void
78 lower_ubo_load_to_uniform(nir_intrinsic_instr *instr, nir_builder *b,
79 struct ir3_ubo_analysis_state *state)
80 {
81 /* We don't lower dynamic block index UBO loads to load_uniform, but we
82 * could probably with some effort determine a block stride in number of
83 * registers.
84 */
85 if (!nir_src_is_const(instr->src[0]))
86 return;
87
88 const uint32_t block = nir_src_as_uint(instr->src[0]);
89
90 if (block > 0) {
91 /* We don't lower dynamic array indexing either, but we definitely should.
92 * We don't have a good way of determining the range of the dynamic
93 * access, so for now just fall back to pulling.
94 */
95 if (!nir_src_is_const(instr->src[1]))
96 return;
97
98 /* After gathering the UBO access ranges, we limit the total
99 * upload. Reject if we're now outside the range.
100 */
101 const struct ir3_ubo_range r = get_ubo_load_range(instr);
102 if (!(state->range[block].start <= r.start &&
103 r.end <= state->range[block].end))
104 return;
105 }
106
107 b->cursor = nir_before_instr(&instr->instr);
108
109 nir_ssa_def *ubo_offset = nir_ssa_for_src(b, instr->src[1], 1);
110 nir_ssa_def *new_offset = ir3_nir_try_propagate_bit_shift(b, ubo_offset, -2);
111 nir_ssa_def *uniform_offset;
112 if (new_offset)
113 uniform_offset = new_offset;
114 else
115 uniform_offset = nir_ushr(b, ubo_offset, nir_imm_int(b, 2));
116
117 const int range_offset =
118 (state->range[block].offset - state->range[block].start) / 4;
119
120 nir_intrinsic_instr *uniform =
121 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_uniform);
122 uniform->num_components = instr->num_components;
123 uniform->src[0] = nir_src_for_ssa(uniform_offset);
124 nir_intrinsic_set_base(uniform, range_offset);
125 nir_ssa_dest_init(&uniform->instr, &uniform->dest,
126 uniform->num_components, instr->dest.ssa.bit_size,
127 instr->dest.ssa.name);
128 nir_builder_instr_insert(b, &uniform->instr);
129 nir_ssa_def_rewrite_uses(&instr->dest.ssa,
130 nir_src_for_ssa(&uniform->dest.ssa));
131
132 nir_instr_remove(&instr->instr);
133
134 state->lower_count++;
135 }
136
137 bool
138 ir3_nir_analyze_ubo_ranges(nir_shader *nir, struct ir3_shader *shader)
139 {
140 struct ir3_ubo_analysis_state *state = &shader->ubo_state;
141
142 memset(state, 0, sizeof(*state));
143
144 nir_foreach_function(function, nir) {
145 if (function->impl) {
146 nir_foreach_block(block, function->impl) {
147 nir_foreach_instr(instr, block) {
148 if (instr->type == nir_instr_type_intrinsic &&
149 nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_load_ubo)
150 gather_ubo_ranges(nir, nir_instr_as_intrinsic(instr), state);
151 }
152 }
153 }
154 }
155
156 /* For now, everything we upload is accessed statically and thus will be
157 * used by the shader. Once we can upload dynamically indexed data, we may
158 * upload sparsely accessed arrays, at which point we probably want to
159 * give priority to smaller UBOs, on the assumption that big UBOs will be
160 * accessed dynamically. Alternatively, we can track statically and
161 * dynamically accessed ranges separately and upload static rangtes
162 * first.
163 */
164 const uint32_t max_upload = 16 * 1024;
165 uint32_t offset = 0;
166 for (uint32_t i = 0; i < ARRAY_SIZE(state->range); i++) {
167 uint32_t range_size = state->range[i].end - state->range[i].start;
168
169 debug_assert(offset <= max_upload);
170 state->range[i].offset = offset;
171 if (offset + range_size > max_upload) {
172 range_size = max_upload - offset;
173 state->range[i].end = state->range[i].start + range_size;
174 }
175 offset += range_size;
176 }
177 state->size = offset;
178
179 nir_foreach_function(function, nir) {
180 if (function->impl) {
181 nir_builder builder;
182 nir_builder_init(&builder, function->impl);
183 nir_foreach_block(block, function->impl) {
184 nir_foreach_instr_safe(instr, block) {
185 if (instr->type == nir_instr_type_intrinsic &&
186 nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_load_ubo)
187 lower_ubo_load_to_uniform(nir_instr_as_intrinsic(instr), &builder, state);
188 }
189 }
190
191 nir_metadata_preserve(function->impl, nir_metadata_block_index |
192 nir_metadata_dominance);
193 }
194 }
195
196 return state->lower_count > 0;
197 }