5c64a864f290ec710c7d4b6619634cb36b0e53b6
[mesa.git] / src / freedreno / ir3 / ir3_nir_lower_io_offsets.c
1 /*
2 * Copyright © 2018-2019 Igalia S.L.
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 "ir3_nir.h"
25 #include "compiler/nir/nir_builder.h"
26
27 /**
28 * This pass moves to NIR certain offset computations for different I/O
29 * ops that are currently implemented on the IR3 backend compiler, to
30 * give NIR a chance to optimize them:
31 *
32 * - Dword-offset for SSBO load, store and atomics: A new, similar intrinsic
33 * is emitted that replaces the original one, adding a new source that
34 * holds the result of the original byte-offset source divided by 4.
35 */
36
37
38 /* Returns the ir3-specific intrinsic opcode corresponding to an SSBO
39 * instruction that is handled by this pass. It also conveniently returns
40 * the offset source index in @offset_src_idx.
41 *
42 * If @intrinsic is not SSBO, or it is not handled by the pass, -1 is
43 * returned.
44 */
45 static int
46 get_ir3_intrinsic_for_ssbo_intrinsic(unsigned intrinsic,
47 uint8_t *offset_src_idx)
48 {
49 debug_assert(offset_src_idx);
50
51 *offset_src_idx = 1;
52
53 switch (intrinsic) {
54 case nir_intrinsic_store_ssbo:
55 *offset_src_idx = 2;
56 return nir_intrinsic_store_ssbo_ir3;
57 case nir_intrinsic_load_ssbo:
58 return nir_intrinsic_load_ssbo_ir3;
59 case nir_intrinsic_ssbo_atomic_add:
60 return nir_intrinsic_ssbo_atomic_add_ir3;
61 case nir_intrinsic_ssbo_atomic_imin:
62 return nir_intrinsic_ssbo_atomic_imin_ir3;
63 case nir_intrinsic_ssbo_atomic_umin:
64 return nir_intrinsic_ssbo_atomic_umin_ir3;
65 case nir_intrinsic_ssbo_atomic_imax:
66 return nir_intrinsic_ssbo_atomic_imax_ir3;
67 case nir_intrinsic_ssbo_atomic_umax:
68 return nir_intrinsic_ssbo_atomic_umax_ir3;
69 case nir_intrinsic_ssbo_atomic_and:
70 return nir_intrinsic_ssbo_atomic_and_ir3;
71 case nir_intrinsic_ssbo_atomic_or:
72 return nir_intrinsic_ssbo_atomic_or_ir3;
73 case nir_intrinsic_ssbo_atomic_xor:
74 return nir_intrinsic_ssbo_atomic_xor_ir3;
75 case nir_intrinsic_ssbo_atomic_exchange:
76 return nir_intrinsic_ssbo_atomic_exchange_ir3;
77 case nir_intrinsic_ssbo_atomic_comp_swap:
78 return nir_intrinsic_ssbo_atomic_comp_swap_ir3;
79 default:
80 break;
81 }
82
83 return -1;
84 }
85
86 static nir_ssa_def *
87 check_and_propagate_bit_shift32(nir_builder *b, nir_alu_instr *alu_instr,
88 int32_t direction, int32_t shift)
89 {
90 debug_assert(alu_instr->src[1].src.is_ssa);
91 nir_ssa_def *shift_ssa = alu_instr->src[1].src.ssa;
92
93 /* Only propagate if the shift is a const value so we can check value range
94 * statically.
95 */
96 nir_const_value *const_val = nir_src_as_const_value(alu_instr->src[1].src);
97 if (!const_val)
98 return NULL;
99
100 int32_t current_shift = const_val[0].i32 * direction;
101 int32_t new_shift = current_shift + shift;
102
103 /* If the merge would reverse the direction, bail out.
104 * e.g, 'x << 2' then 'x >> 4' is not 'x >> 2'.
105 */
106 if (current_shift * new_shift < 0)
107 return NULL;
108
109 /* If the propagation would overflow an int32_t, bail out too to be on the
110 * safe side.
111 */
112 if (new_shift < -31 || new_shift > 31)
113 return NULL;
114
115 b->cursor = nir_before_instr(&alu_instr->instr);
116
117 /* Add or substract shift depending on the final direction (SHR vs. SHL). */
118 if (shift * direction < 0)
119 shift_ssa = nir_isub(b, shift_ssa, nir_imm_int(b, abs(shift)));
120 else
121 shift_ssa = nir_iadd(b, shift_ssa, nir_imm_int(b, abs(shift)));
122
123 return shift_ssa;
124 }
125
126 nir_ssa_def *
127 ir3_nir_try_propagate_bit_shift(nir_builder *b, nir_ssa_def *offset, int32_t shift)
128 {
129 nir_instr *offset_instr = offset->parent_instr;
130 if (offset_instr->type != nir_instr_type_alu)
131 return NULL;
132
133 nir_alu_instr *alu = nir_instr_as_alu(offset_instr);
134 nir_ssa_def *shift_ssa;
135 nir_ssa_def *new_offset = NULL;
136
137 switch (alu->op) {
138 case nir_op_ishl:
139 shift_ssa = check_and_propagate_bit_shift32(b, alu, 1, shift);
140 if (shift_ssa)
141 new_offset = nir_ishl(b, alu->src[0].src.ssa, shift_ssa);
142 break;
143 case nir_op_ishr:
144 shift_ssa = check_and_propagate_bit_shift32(b, alu, -1, shift);
145 if (shift_ssa)
146 new_offset = nir_ishr(b, alu->src[0].src.ssa, shift_ssa);
147 break;
148 case nir_op_ushr:
149 shift_ssa = check_and_propagate_bit_shift32(b, alu, -1, shift);
150 if (shift_ssa)
151 new_offset = nir_ushr(b, alu->src[0].src.ssa, shift_ssa);
152 break;
153 default:
154 return NULL;
155 }
156
157 return new_offset;
158 }
159
160 static bool
161 lower_offset_for_ssbo(nir_intrinsic_instr *intrinsic, nir_builder *b,
162 unsigned ir3_ssbo_opcode, uint8_t offset_src_idx)
163 {
164 unsigned num_srcs = nir_intrinsic_infos[intrinsic->intrinsic].num_srcs;
165
166 bool has_dest = nir_intrinsic_infos[intrinsic->intrinsic].has_dest;
167 nir_ssa_def *new_dest = NULL;
168
169 /* Here we create a new intrinsic and copy over all contents from the old one. */
170
171 nir_intrinsic_instr *new_intrinsic;
172 nir_src *target_src;
173
174 /* 'offset_src_idx' holds the index of the source that represent the offset. */
175 new_intrinsic =
176 nir_intrinsic_instr_create(b->shader, ir3_ssbo_opcode);
177
178 debug_assert(intrinsic->src[offset_src_idx].is_ssa);
179 nir_ssa_def *offset = intrinsic->src[offset_src_idx].ssa;
180
181 /* Since we don't have value range checking, we first try to propagate
182 * the division by 4 ('offset >> 2') into another bit-shift instruction that
183 * possibly defines the offset. If that's the case, we emit a similar
184 * instructions adjusting (merging) the shift value.
185 *
186 * Here we use the convention that shifting right is negative while shifting
187 * left is positive. So 'x / 4' ~ 'x >> 2' or 'x << -2'.
188 */
189 nir_ssa_def *new_offset = ir3_nir_try_propagate_bit_shift(b, offset, -2);
190
191 /* The new source that will hold the dword-offset is always the last
192 * one for every intrinsic.
193 */
194 target_src = &new_intrinsic->src[num_srcs];
195 *target_src = nir_src_for_ssa(offset);
196
197 if (has_dest) {
198 debug_assert(intrinsic->dest.is_ssa);
199 nir_ssa_def *dest = &intrinsic->dest.ssa;
200 nir_ssa_dest_init(&new_intrinsic->instr, &new_intrinsic->dest,
201 dest->num_components, dest->bit_size, NULL);
202 new_dest = &new_intrinsic->dest.ssa;
203 }
204
205 for (unsigned i = 0; i < num_srcs; i++)
206 new_intrinsic->src[i] = nir_src_for_ssa(intrinsic->src[i].ssa);
207
208 for (unsigned i = 0; i < NIR_INTRINSIC_MAX_CONST_INDEX; i++)
209 new_intrinsic->const_index[i] = intrinsic->const_index[i];
210
211 new_intrinsic->num_components = intrinsic->num_components;
212
213 b->cursor = nir_before_instr(&intrinsic->instr);
214
215 /* If we managed to propagate the division by 4, just use the new offset
216 * register and don't emit the SHR.
217 */
218 if (new_offset)
219 offset = new_offset;
220 else
221 offset = nir_ushr(b, offset, nir_imm_int(b, 2));
222
223 /* Insert the new intrinsic right before the old one. */
224 nir_builder_instr_insert(b, &new_intrinsic->instr);
225
226 /* Replace the last source of the new intrinsic by the result of
227 * the offset divided by 4.
228 */
229 nir_instr_rewrite_src(&new_intrinsic->instr,
230 target_src,
231 nir_src_for_ssa(offset));
232
233 if (has_dest) {
234 /* Replace the uses of the original destination by that
235 * of the new intrinsic.
236 */
237 nir_ssa_def_rewrite_uses(&intrinsic->dest.ssa,
238 nir_src_for_ssa(new_dest));
239 }
240
241 /* Finally remove the original intrinsic. */
242 nir_instr_remove(&intrinsic->instr);
243
244 return true;
245 }
246
247 static bool
248 lower_io_offsets_block(nir_block *block, nir_builder *b, void *mem_ctx)
249 {
250 bool progress = false;
251
252 nir_foreach_instr_safe(instr, block) {
253 if (instr->type != nir_instr_type_intrinsic)
254 continue;
255
256 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
257
258 /* SSBO */
259 int ir3_intrinsic;
260 uint8_t offset_src_idx;
261 ir3_intrinsic = get_ir3_intrinsic_for_ssbo_intrinsic(intr->intrinsic,
262 &offset_src_idx);
263 if (ir3_intrinsic != -1) {
264 progress |= lower_offset_for_ssbo(intr, b, (unsigned) ir3_intrinsic,
265 offset_src_idx);
266 }
267 }
268
269 return progress;
270 }
271
272 static bool
273 lower_io_offsets_func(nir_function_impl *impl)
274 {
275 void *mem_ctx = ralloc_parent(impl);
276 nir_builder b;
277 nir_builder_init(&b, impl);
278
279 bool progress = false;
280 nir_foreach_block_safe(block, impl) {
281 progress |= lower_io_offsets_block(block, &b, mem_ctx);
282 }
283
284 if (progress) {
285 nir_metadata_preserve(impl, nir_metadata_block_index |
286 nir_metadata_dominance);
287 }
288
289 return progress;
290 }
291
292 bool
293 ir3_nir_lower_io_offsets(nir_shader *shader)
294 {
295 bool progress = false;
296
297 nir_foreach_function(function, shader) {
298 if (function->impl)
299 progress |= lower_io_offsets_func(function->impl);
300 }
301
302 return progress;
303 }