Debian Codegraph @

src/panfrost/compiler/bifrost/bi_opt_copy_prop.c

raw
1 /*
2 * Copyright (C) 2018 Alyssa Rosenzweig
3 * Copyright (C) 2019-2020 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "bi_builder.h"
8 #include "compiler.h"
9 #include "util/hash_table.h"
10
11 /* SSA copy propagation */
12
13 static bool
14 bi_reads_fau(bi_instr *ins)
15 {
16 bi_foreach_src(ins, s) {
17 if (ins->src[s].type == BI_INDEX_FAU)
18 return true;
19 }
20
21 return false;
22 }
23
24 static void
25 bi_opt_word_prop(bi_context *ctx)
26 {
27 /* Chase SPLIT of COLLECT. Instruction selection usually avoids this
28 * pattern (due to the split cache), but it is inevitably generated by
29 * the UBO pushing pass.
30 */
31 bi_instr **collects = calloc(sizeof(bi_instr *), ctx->ssa_alloc);
32 bi_foreach_instr_global_safe(ctx, I) {
33 if (I->op == BI_OPCODE_COLLECT_I32) {
34 /* Rewrite trivial collects while we're at it */
35 if (I->nr_srcs == 1)
36 bi_set_opcode(I, BI_OPCODE_MOV_I32);
37
38 collects[I->dest[0].value] = I;
39 } else if (I->op == BI_OPCODE_SPLIT_I32) {
40 /* Rewrite trivial splits while we're at it */
41 if (I->nr_dests == 1)
42 bi_set_opcode(I, BI_OPCODE_MOV_I32);
43
44 bi_instr *collect = collects[I->src[0].value];
45 if (!collect)
46 continue;
47
48 /* Lower the split to moves, copyprop cleans up */
49 bi_builder b = bi_init_builder(ctx, bi_before_instr(I));
50
51 bi_foreach_dest(I, d)
52 bi_mov_i32_to(&b, I->dest[d], collect->src[d]);
53
54 bi_remove_instruction(I);
55 }
56 }
57
58 free(collects);
59
60 bi_index *replacement = calloc(sizeof(bi_index), ctx->ssa_alloc);
61
62 bi_foreach_instr_global_safe(ctx, ins) {
63 if (ins->op == BI_OPCODE_MOV_I32 &&
64 ins->src[0].type != BI_INDEX_REGISTER) {
65 bi_index replace = ins->src[0];
66
67 /* Peek through one layer so copyprop converges in one
68 * iteration for chained moves */
69 if (bi_is_ssa(replace)) {
70 bi_index chained = replacement[replace.value];
71
72 if (!bi_is_null(chained))
73 replace = chained;
74 }
75
76 assert(ins->nr_dests == 1);
77 replacement[ins->dest[0].value] = replace;
78 }
79
80 bi_foreach_src(ins, s) {
81 bi_index use = ins->src[s];
82
83 if (use.type != BI_INDEX_NORMAL)
84 continue;
85 if (bi_is_staging_src(ins, s))
86 continue;
87
88 bi_index repl = replacement[use.value];
89
90 if (repl.type == BI_INDEX_CONSTANT && bi_reads_fau(ins))
91 continue;
92
93 if (!bi_is_null(repl))
94 bi_replace_src(ins, s, repl);
95 }
96 }
97
98 free(replacement);
99 }
100
101 static bi_index
102 select_byte(bi_index idx, unsigned byte)
103 {
104 unsigned bytes[4] = {0, 0, 0, 0};
105 bi_swizzle_to_byte_channels(idx.swizzle, bytes);
106 idx.swizzle = BI_SWIZZLE_B0 + bytes[byte];
107 return idx;
108 }
109
110 struct byte_prop_entry {
111 bi_index word;
112 bi_index bytes[4];
113 };
114
115 static void *
116 byte_prop_ht_key(bi_index idx)
117 {
118 assert(bi_is_ssa(idx));
119
120 assert(idx.value < (1 << 26) - 1);
121 assert(idx.offset < (1 << 6));
122
123 /* Add 1 to value because zero keys are not allowed */
124 uint32_t key = (idx.value + 1) | ((uint32_t)idx.offset << 26);
125
126 return (void *)(uintptr_t)key;
127 }
128
129 static struct byte_prop_entry *
130 add_byte_prop_entry(struct hash_table *ht, bi_index idx)
131 {
132 assert(!_mesa_hash_table_search(ht, byte_prop_ht_key(idx)));
133 struct byte_prop_entry *entry = rzalloc(ht, struct byte_prop_entry);
134 _mesa_hash_table_insert(ht, byte_prop_ht_key(idx), entry);
135 return entry;
136 }
137
138 static struct byte_prop_entry *
139 get_byte_prop_entry(struct hash_table *ht, bi_index idx)
140 {
141 if (!bi_is_ssa(idx))
142 return NULL;
143
144 struct hash_entry *ht_entry =
145 _mesa_hash_table_search(ht, byte_prop_ht_key(idx));
146 if (ht_entry == NULL)
147 return NULL;
148
149 return ht_entry->data;
150 }
151
152 static bool
153 chase_bytes(struct byte_prop_entry *dst_entry, struct hash_table *ht)
154 {
155 bool progress = false;
156 for (unsigned i = 0; i < 4; i++) {
157 /* Chase as far as we can, updating dst_entry->src[i] as we go */
158 while (true) {
159 assert(bi_swizzle_replicates_8(dst_entry->bytes[i].swizzle));
160 unsigned byte = dst_entry->bytes[i].swizzle - BI_SWIZZLE_B0;
161
162 struct byte_prop_entry *src_entry =
163 get_byte_prop_entry(ht, dst_entry->bytes[i]);
164 if (src_entry == NULL || bi_is_null(src_entry->bytes[byte]))
165 break;
166
167 progress = true;
168 dst_entry->bytes[i] = src_entry->bytes[byte];
169 }
170 }
171
172 unsigned swizzle_bytes[4];
173 bool is_swizzle = true, is_const = true;
174 for (unsigned i = 0; i < 4; i++) {
175 swizzle_bytes[i] = dst_entry->bytes[i].swizzle - BI_SWIZZLE_B0;
176 if (i > 0 && !bi_is_word_equiv(dst_entry->bytes[i], dst_entry->bytes[0]))
177 is_swizzle = false;
178
179 if (dst_entry->bytes[i].type != BI_INDEX_CONSTANT)
180 is_const = false;
181 }
182
183 enum bi_swizzle swizzle = BI_SWIZZLE_H01;
184 if (is_const) {
185 uint32_t value = 0;
186 for (unsigned i = 0; i < 4; i++) {
187 uint8_t byte = bi_apply_swizzle(dst_entry->bytes[i].value,
188 dst_entry->bytes[i].swizzle);
189 value |= (uint32_t)byte << (i * 8);
190 }
191 dst_entry->word = bi_imm_u32(value);
192 } else if (is_swizzle && bi_swizzle_from_byte_channels(swizzle_bytes,
193 &swizzle)) {
194 dst_entry->word = dst_entry->bytes[0];
195 dst_entry->word.swizzle = swizzle;
196 }
197
198 return progress;
199 }
200
201 static bool
202 chase_word(struct byte_prop_entry *dst_entry, struct hash_table *ht)
203 {
204 /* Chase the swizzle, updaing dst_entry->word whenever we find a new valid
205 * swizzled word. For the purposes of this function, src will only be used
206 * to index into the prop entry table. The actual swizzle is represented
207 * by src_bytes[], which may or may not be a valid bi_swizzle.
208 */
209 bi_index src = dst_entry->word;
210 unsigned src_bytes[4];
211 bi_swizzle_to_byte_channels(src.swizzle, src_bytes);
212
213 bool progress = false;
214 while (true) {
215 struct byte_prop_entry *src_entry = get_byte_prop_entry(ht, src);
216 if (src_entry == NULL || bi_is_null(src_entry->word))
217 return progress;
218
219 if (src_entry->word.type == BI_INDEX_CONSTANT) {
220 assert(src_entry->word.swizzle == BI_SWIZZLE_H01);
221 const uint32_t src_value = src_entry->word.value;
222
223 uint32_t value = 0;
224 for (unsigned i = 0; i < 4; i++)
225 value |= ((src_value >> (src_bytes[i] * 8)) & 0xff) << (i * 8);
226
227 dst_entry->word = bi_imm_u32(value);
228 return true;
229 }
230
231 unsigned entry_bytes[4];
232 bi_swizzle_to_byte_channels(src_entry->word.swizzle, entry_bytes);
233
234 for (unsigned i = 0; i < 4; i++)
235 src_bytes[i] = entry_bytes[src_bytes[i]];
236
237 enum bi_swizzle swizzle = BI_SWIZZLE_H01;
238 if (bi_swizzle_from_byte_channels(src_bytes, &swizzle)) {
239 progress = true;
240 dst_entry->word = src_entry->word;
241 dst_entry->word.swizzle = swizzle;
242 }
243
244 src = src_entry->word;
245 }
246 }
247
248 static void
249 byte_chase_instr_srcs(bi_context *ctx, bi_instr *I, struct hash_table *ht)
250 {
251 bi_foreach_src(I, s) {
252 if (bi_count_read_registers(I, s) != 1)
253 continue;
254
255 struct byte_prop_entry *src_entry = get_byte_prop_entry(ht, I->src[s]);
256 if (src_entry == NULL)
257 continue;
258
259 bi_index repl;
260 if (bi_swizzle_replicates_8(I->src[s].swizzle)) {
261 unsigned byte = I->src[s].swizzle - BI_SWIZZLE_B0;
262 repl = src_entry->bytes[byte];
263 } else {
264 if (bi_is_null(src_entry->word))
265 continue;
266
267 enum bi_swizzle swizzle = BI_SWIZZLE_H01;
268 if (!bi_try_compose_swizzles(&swizzle, I->src[s].swizzle,
269 src_entry->word.swizzle))
270 continue;
271
272 repl = src_entry->word;
273 repl.swizzle = swizzle;
274 }
275
276 if (!bi_op_supports_swizzle(I->op, s, repl.swizzle, ctx->arch))
277 continue;
278
279 if (bi_is_staging_src(I, s) && repl.type != BI_INDEX_NORMAL)
280 continue;
281
282 if (repl.type == BI_INDEX_CONSTANT && bi_reads_fau(I))
283 continue;
284
285 /* bi_replace_src uses the swizzle and other modifiers from the original
286 * and stops the replacement.
287 */
288 I->src[s].swizzle = repl.swizzle;
289 repl.swizzle = BI_SWIZZLE_H01;
290 bi_replace_src(I, s, repl);
291 }
292 }
293
294 static bool
295 byte_chase_add_swz_instr(bi_context *ctx, bi_instr *I, struct hash_table *ht)
296 {
297 assert(I->op == BI_OPCODE_MOV_I32 ||
298 I->op == BI_OPCODE_SWZ_V2I16 ||
299 I->op == BI_OPCODE_SWZ_V4I8);
300
301 /* Don't try to propagate registers */
302 if (I->src[0].type == BI_INDEX_REGISTER)
303 return false;
304
305 struct byte_prop_entry *dst_entry = add_byte_prop_entry(ht, I->dest[0]);
306 for (unsigned i = 0; i < 4; i++)
307 dst_entry->bytes[i] = select_byte(I->src[0], i);
308
309 bool progress = chase_bytes(dst_entry, ht);
310 if (bi_is_null(dst_entry->word)) {
311 /* If chasing bytes wasn't able to produce a word, just chase words and
312 * see if we can do better.
313 */
314 dst_entry->word = I->src[0];
315 progress = chase_word(dst_entry, ht);
316 }
317
318 if (!progress || bi_is_null(dst_entry->word))
319 return false;
320
321 I->src[0] = dst_entry->word;
322 if (dst_entry->word.swizzle == BI_SWIZZLE_H01)
323 I->op = BI_OPCODE_MOV_I32;
324 else if (dst_entry->word.swizzle == BI_SWIZZLE_H00 ||
325 dst_entry->word.swizzle == BI_SWIZZLE_H10 ||
326 dst_entry->word.swizzle == BI_SWIZZLE_H11)
327 I->op = BI_OPCODE_SWZ_V2I16;
328 else
329 I->op = BI_OPCODE_SWZ_V4I8;
330
331 return true;
332 }
333
334 static bool
335 byte_chase_add_vec_instr(bi_context *ctx, bi_instr *I, struct hash_table *ht)
336 {
337 bi_index bytes[4];
338 switch (I->op) {
339 case BI_OPCODE_MKVEC_V2I16:
340 for (unsigned i = 0; i < 2; i++) {
341 assert(I->src[i].swizzle == BI_SWIZZLE_H00 ||
342 I->src[i].swizzle == BI_SWIZZLE_H11);
343 bytes[i * 2 + 0] = select_byte(I->src[i], 0);
344 bytes[i * 2 + 1] = select_byte(I->src[i], 1);
345 }
346 break;
347
348 case BI_OPCODE_MKVEC_V2I8:
349 bytes[0] = I->src[0];
350 bytes[1] = I->src[1];
351 bytes[2] = select_byte(I->src[2], 0);
352 bytes[3] = select_byte(I->src[2], 1);
353 break;
354
355 case BI_OPCODE_MKVEC_V4I8:
356 for (unsigned i = 0; i < 4; i++)
357 bytes[i] = I->src[i];
358 break;
359
360 case BI_OPCODE_U8_TO_U32:
361 bytes[0] = I->src[0];
362 bytes[1] = bi_imm_u8(0);
363 bytes[2] = bi_imm_u8(0);
364 bytes[3] = bi_imm_u8(0);
365 break;
366
367 case BI_OPCODE_U16_TO_U32:
368 bytes[0] = select_byte(I->src[0], 0);
369 bytes[1] = select_byte(I->src[0], 1);
370 bytes[2] = bi_imm_u8(0);
371 bytes[3] = bi_imm_u8(0);
372 break;
373
374 case BI_OPCODE_V2U8_TO_V2U16:
375 bytes[0] = select_byte(I->src[0], 0);
376 bytes[1] = bi_imm_u8(0);
377 bytes[2] = select_byte(I->src[0], 2);
378 bytes[3] = bi_imm_u8(0);
379 break;
380
381 default:
382 UNREACHABLE("Unhanded vec instruction");
383 }
384
385 struct byte_prop_entry *dst_entry = add_byte_prop_entry(ht, I->dest[0]);
386 for (unsigned i = 0; i < 4; i++) {
387 /* Don't try to propagate registers */
388 if (bytes[i].type != BI_INDEX_REGISTER)
389 dst_entry->bytes[i] = bytes[i];
390 }
391
392 if (!chase_bytes(dst_entry, ht))
393 return false;
394
395 bi_builder b = bi_init_builder(ctx, bi_before_instr(I));
396 bi_mkvec_v4i8_to(&b, I->dest[0],
397 dst_entry->bytes[0], dst_entry->bytes[1],
398 dst_entry->bytes[2], dst_entry->bytes[3]);
399 bi_remove_instruction(I);
400
401 return true;
402 }
403
404 /** Byte propagation
405 *
406 * This is actually two passes in one: Byte propagation and swizzle
407 * propagation. Any time we see a MKVEC, we look at its sources only as bytes
408 * and chase individual bytes back, through other MKVEC and SWZ, to their
409 * generating instruction and make the MKVEC only consume the original bytes.
410 * If the MKVEC happens to construct something that's just a swizzle of
411 * another def (this is fairly common), we record that as well. The idea here
412 * is that a lot of MKVEC just consume other MKVEC and we can get rid of the
413 * intermediate ones or even the whole chain if it just ends up being a
414 * swizzle in the end.
415 *
416 * For SWZ instructions, we first look at them like a MKVEC of the individual
417 * bytes they consume. If that doesn't yield a single swizzled word, we then
418 * crawl through the words table, just accumulating swizzles. This gives us
419 * the best (closest to the generating instructions) coherent word. We could
420 * also replace SWZ with MKVEC and just do byte propagation but MKVEC is often
421 * 2 instructions whereas SWZ is often one (or folded into a source) so this
422 * is probably the better balance.
423 *
424 * Finally, we not only replace the MKVEC and SWZ instructions but we also
425 * attempt to propagate swizzles into individual ALU op sources.
426 */
427 static void
428 bi_opt_byte_prop(bi_context *ctx)
429 {
430 struct hash_table *ht = _mesa_hash_table_create_u32_keys(NULL);
431
432 bi_foreach_instr_global_safe(ctx, I) {
433 switch (I->op) {
434 case BI_OPCODE_MKVEC_V2I16:
435 case BI_OPCODE_MKVEC_V2I8:
436 case BI_OPCODE_MKVEC_V4I8:
437 case BI_OPCODE_U8_TO_U32:
438 case BI_OPCODE_U16_TO_U32:
439 case BI_OPCODE_V2U8_TO_V2U16:
440 byte_chase_add_vec_instr(ctx, I, ht);
441 break;
442
443 case BI_OPCODE_MOV_I32:
444 case BI_OPCODE_SWZ_V2I16:
445 case BI_OPCODE_SWZ_V4I8:
446 byte_chase_add_swz_instr(ctx, I, ht);
447 break;
448
449 default:
450 byte_chase_instr_srcs(ctx, I, ht);
451 break;
452 }
453 }
454
455 _mesa_hash_table_destroy(ht, NULL);
456 }
457
458 void
459 bi_opt_copy_prop(bi_context *ctx)
460 {
461 bi_opt_word_prop(ctx);
462 bi_opt_byte_prop(ctx);
463 }