Debian Codegraph @

src/panfrost/compiler/midgard/mir.c

raw
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2019-2020 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "compiler.h"
8 #include "midgard_ops.h"
9
10 void
11 mir_block_add_successor(midgard_block *block, midgard_block *successor)
12 {
13 assert(block);
14 assert(successor);
15
16 /* Cull impossible edges */
17 if (block->unconditional_jumps)
18 return;
19
20 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) {
21 if (block->successors[i]) {
22 if (block->successors[i] == successor)
23 return;
24 else
25 continue;
26 }
27
28 block->successors[i] = successor;
29 _mesa_set_add(successor->predecessors, block);
30 return;
31 }
32
33 UNREACHABLE("Too many successors");
34 }
35
36 void
37 mir_rewrite_index_src_single(midgard_instruction *ins, unsigned old,
38 unsigned new)
39 {
40 mir_foreach_src(ins, i) {
41 if (ins->src[i] == old)
42 ins->src[i] = new;
43 }
44 }
45
46 void
47 mir_rewrite_index_dst_single(midgard_instruction *ins, unsigned old,
48 unsigned new)
49 {
50 if (ins->dest == old)
51 ins->dest = new;
52 }
53
54 static void
55 mir_rewrite_index_src_single_swizzle(midgard_instruction *ins, unsigned old,
56 unsigned new, unsigned *swizzle)
57 {
58 for (unsigned i = 0; i < ARRAY_SIZE(ins->src); ++i) {
59 if (ins->src[i] != old)
60 continue;
61
62 ins->src[i] = new;
63 mir_compose_swizzle(ins->swizzle[i], swizzle, ins->swizzle[i]);
64 }
65 }
66
67 void
68 mir_rewrite_index_src(compiler_context *ctx, unsigned old, unsigned new)
69 {
70 mir_foreach_instr_global(ctx, ins) {
71 mir_rewrite_index_src_single(ins, old, new);
72 }
73 }
74
75 void
76 mir_rewrite_index_src_swizzle(compiler_context *ctx, unsigned old, unsigned new,
77 unsigned *swizzle)
78 {
79 mir_foreach_instr_global(ctx, ins) {
80 mir_rewrite_index_src_single_swizzle(ins, old, new, swizzle);
81 }
82 }
83
84 void
85 mir_rewrite_index_dst(compiler_context *ctx, unsigned old, unsigned new)
86 {
87 mir_foreach_instr_global(ctx, ins) {
88 mir_rewrite_index_dst_single(ins, old, new);
89 }
90
91 /* Implicitly written before the shader */
92 if (ctx->blend_input == old)
93 ctx->blend_input = new;
94
95 if (ctx->blend_src1 == old)
96 ctx->blend_src1 = new;
97 }
98
99 void
100 mir_rewrite_index(compiler_context *ctx, unsigned old, unsigned new)
101 {
102 mir_rewrite_index_src(ctx, old, new);
103 mir_rewrite_index_dst(ctx, old, new);
104 }
105
106 unsigned
107 mir_use_count(compiler_context *ctx, unsigned value)
108 {
109 unsigned used_count = 0;
110
111 mir_foreach_instr_global(ctx, ins) {
112 if (mir_has_arg(ins, value))
113 ++used_count;
114 }
115
116 if (ctx->blend_input == value)
117 ++used_count;
118
119 if (ctx->blend_src1 == value)
120 ++used_count;
121
122 return used_count;
123 }
124
125 /* Checks if a value is used only once (or totally dead), which is an important
126 * heuristic to figure out if certain optimizations are Worth It (TM) */
127
128 bool
129 mir_single_use(compiler_context *ctx, unsigned value)
130 {
131 /* We can replicate constants in places so who cares */
132 if (value == SSA_FIXED_REGISTER(REGISTER_CONSTANT))
133 return true;
134
135 return mir_use_count(ctx, value) <= 1;
136 }
137
138 bool
139 mir_nontrivial_mod(const midgard_instruction *ins, unsigned i,
140 bool check_swizzle)
141 {
142 bool is_int = midgard_is_integer_op(ins->op);
143
144 if (is_int) {
145 if (ins->src_shift[i])
146 return true;
147 } else {
148 if (ins->src_neg[i])
149 return true;
150 if (ins->src_abs[i])
151 return true;
152 }
153
154 if (ins->dest_type != ins->src_types[i])
155 return true;
156
157 if (check_swizzle) {
158 for (unsigned c = 0; c < 16; ++c) {
159 if (!(ins->mask & (1 << c)))
160 continue;
161 if (ins->swizzle[i][c] != c)
162 return true;
163 }
164 }
165
166 return false;
167 }
168
169 bool
170 mir_nontrivial_outmod(const midgard_instruction *ins)
171 {
172 bool is_int = midgard_is_integer_op(ins->op);
173 unsigned mod = ins->outmod;
174
175 if (ins->dest_type != ins->src_types[1])
176 return true;
177
178 if (is_int)
179 return mod != midgard_outmod_keeplo;
180 else
181 return mod != midgard_outmod_none;
182 }
183
184 /* 128 / sz = exp2(log2(128 / sz))
185 * = exp2(log2(128) - log2(sz))
186 * = exp2(7 - log2(sz))
187 * = 1 << (7 - log2(sz))
188 */
189
190 static unsigned
191 mir_components_for_bits(unsigned bits)
192 {
193 return 1 << (7 - util_logbase2(bits));
194 }
195
196 unsigned
197 mir_components_for_type(nir_alu_type T)
198 {
199 unsigned sz = nir_alu_type_get_type_size(T);
200 return mir_components_for_bits(sz);
201 }
202
203 uint16_t
204 mir_from_bytemask(uint16_t bytemask, unsigned bits)
205 {
206 unsigned value = 0;
207 unsigned count = bits / 8;
208
209 for (unsigned c = 0, d = 0; c < 16; c += count, ++d) {
210 bool a = (bytemask & (1 << c)) != 0;
211
212 for (unsigned q = c; q < count; ++q)
213 assert(((bytemask & (1 << q)) != 0) == a);
214
215 value |= (a << d);
216 }
217
218 return value;
219 }
220
221 /* Rounds up a bytemask to fill a given component count. Iterate each
222 * component, and check if any bytes in the component are masked on */
223
224 uint16_t
225 mir_round_bytemask_up(uint16_t mask, unsigned bits)
226 {
227 unsigned bytes = bits / 8;
228 unsigned maxmask = mask_of(bytes);
229 unsigned channels = mir_components_for_bits(bits);
230
231 for (unsigned c = 0; c < channels; ++c) {
232 unsigned submask = maxmask << (c * bytes);
233
234 if (mask & submask)
235 mask |= submask;
236 }
237
238 return mask;
239 }
240
241 /* Grabs the per-byte mask of an instruction (as opposed to per-component) */
242
243 uint16_t
244 mir_bytemask(const midgard_instruction *ins)
245 {
246 unsigned type_size = nir_alu_type_get_type_size(ins->dest_type);
247 return pan_to_bytemask(type_size, ins->mask);
248 }
249
250 void
251 mir_set_bytemask(midgard_instruction *ins, uint16_t bytemask)
252 {
253 unsigned type_size = nir_alu_type_get_type_size(ins->dest_type);
254 ins->mask = mir_from_bytemask(bytemask, type_size);
255 }
256
257 /*
258 * Checks if we should use an upper destination override, rather than the lower
259 * one in the IR. If yes, returns the bytes to shift by. If no, returns zero
260 * for a lower override and negative for no override.
261 */
262 signed
263 mir_upper_override(const midgard_instruction *ins, unsigned inst_size)
264 {
265 unsigned type_size = nir_alu_type_get_type_size(ins->dest_type);
266
267 /* If the sizes are the same, there's nothing to override */
268 if (type_size == inst_size)
269 return -1;
270
271 /* There are 16 bytes per vector, so there are (16/bytes)
272 * components per vector. So the magic half is half of
273 * (16/bytes), which simplifies to 8/bytes = 8 / (bits / 8) = 64 / bits
274 * */
275
276 unsigned threshold = mir_components_for_bits(type_size) >> 1;
277
278 /* How many components did we shift over? */
279 unsigned zeroes = __builtin_ctz(ins->mask);
280
281 /* Did we hit the threshold? */
282 return (zeroes >= threshold) ? threshold : 0;
283 }
284
285 /* Creates a mask of the components of a node read by an instruction, by
286 * analyzing the swizzle with respect to the instruction's mask. E.g.:
287 *
288 * fadd r0.xz, r1.yyyy, r2.zwyx
289 *
290 * will return a mask of Z/Y for r2
291 */
292
293 static uint16_t
294 mir_bytemask_of_read_components_single(const unsigned *swizzle, unsigned inmask,
295 unsigned bits)
296 {
297 unsigned cmask = 0;
298
299 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c) {
300 if (!(inmask & (1 << c)))
301 continue;
302 cmask |= (1 << swizzle[c]);
303 }
304
305 return pan_to_bytemask(bits, cmask);
306 }
307
308 uint16_t
309 mir_bytemask_of_read_components_index(const midgard_instruction *ins,
310 unsigned i)
311 {
312 /* Conditional branches read one 32-bit component = 4 bytes (TODO: multi
313 * branch??) */
314 if (ins->compact_branch && ins->branch.conditional && (i == 0))
315 return 0xF;
316
317 /* ALU ops act componentwise so we need to pay attention to
318 * their mask. Texture/ldst does not so we don't clamp source
319 * readmasks based on the writemask */
320 unsigned qmask = ~0;
321
322 /* Handle dot products and things */
323 if (ins->type == TAG_ALU_4 && !ins->compact_branch) {
324 unsigned props = alu_opcode_props[ins->op].props;
325
326 unsigned channel_override = GET_CHANNEL_COUNT(props);
327
328 if (channel_override)
329 qmask = mask_of(channel_override);
330 else
331 qmask = ins->mask;
332 }
333
334 return mir_bytemask_of_read_components_single(
335 ins->swizzle[i], qmask, nir_alu_type_get_type_size(ins->src_types[i]));
336 }
337
338 uint16_t
339 mir_bytemask_of_read_components(const midgard_instruction *ins, unsigned node)
340 {
341 uint16_t mask = 0;
342
343 if (node == ~0)
344 return 0;
345
346 mir_foreach_src(ins, i) {
347 if (ins->src[i] != node)
348 continue;
349 mask |= mir_bytemask_of_read_components_index(ins, i);
350 }
351
352 return mask;
353 }
354
355 /* Register allocation occurs after instruction scheduling, which is fine until
356 * we start needing to spill registers and therefore insert instructions into
357 * an already-scheduled program. We don't have to be terribly efficient about
358 * this, since spilling is already slow. So just semantically we need to insert
359 * the instruction into a new bundle before/after the bundle of the instruction
360 * in question */
361
362 static midgard_bundle
363 mir_bundle_for_op(compiler_context *ctx, const midgard_instruction *ins)
364 {
365 midgard_instruction *u = mir_upload_ins(ctx, ins);
366
367 midgard_bundle bundle = {
368 .tag = ins->type,
369 .instruction_count = 1,
370 .instructions = {u},
371 };
372
373 if (bundle.tag == TAG_ALU_4) {
374 assert(OP_IS_MOVE(u->op));
375 u->unit = UNIT_VMUL;
376
377 size_t bytes_emitted = sizeof(uint32_t) + sizeof(midgard_reg_info) +
378 sizeof(midgard_vector_alu);
379 bundle.padding = ~(bytes_emitted - 1) & 0xF;
380 bundle.control = ins->type | u->unit;
381 }
382
383 return bundle;
384 }
385
386 static unsigned
387 mir_bundle_idx_for_ins(const midgard_instruction *tag, midgard_block *block)
388 {
389 midgard_bundle *bundles = (midgard_bundle *)block->bundles.data;
390
391 size_t count = (block->bundles.size / sizeof(midgard_bundle));
392
393 for (unsigned i = 0; i < count; ++i) {
394 for (unsigned j = 0; j < bundles[i].instruction_count; ++j) {
395 if (bundles[i].instructions[j] == tag)
396 return i;
397 }
398 }
399
400 mir_print_instruction(tag);
401 UNREACHABLE("Instruction not scheduled in block");
402 }
403
404 midgard_instruction *
405 mir_insert_instruction_before_scheduled(compiler_context *ctx,
406 midgard_block *block,
407 const midgard_instruction *tag,
408 const midgard_instruction *ins)
409 {
410 unsigned before = mir_bundle_idx_for_ins(tag, block);
411 size_t count = util_dynarray_num_elements(&block->bundles, midgard_bundle);
412 UNUSED void *unused = util_dynarray_grow(&block->bundles, midgard_bundle, 1);
413
414 midgard_bundle *bundles = (midgard_bundle *)block->bundles.data;
415 memmove(bundles + before + 1, bundles + before,
416 (count - before) * sizeof(midgard_bundle));
417 midgard_bundle *before_bundle = bundles + before + 1;
418
419 midgard_bundle new = mir_bundle_for_op(ctx, ins);
420 memcpy(bundles + before, &new, sizeof(new));
421
422 list_addtail(&new.instructions[0]->link,
423 &before_bundle->instructions[0]->link);
424 block->quadword_count += midgard_tag_props[new.tag].size;
425
426 return new.instructions[0];
427 }
428
429 midgard_instruction *
430 mir_insert_instruction_after_scheduled(compiler_context *ctx,
431 midgard_block *block,
432 const midgard_instruction *tag,
433 const midgard_instruction *ins)
434 {
435 /* We need to grow the bundles array to add our new bundle */
436 size_t count = util_dynarray_num_elements(&block->bundles, midgard_bundle);
437 UNUSED void *unused = util_dynarray_grow(&block->bundles, midgard_bundle, 1);
438
439 /* Find the bundle that we want to insert after */
440 unsigned after = mir_bundle_idx_for_ins(tag, block);
441
442 /* All the bundles after that one, we move ahead by one */
443 midgard_bundle *bundles = (midgard_bundle *)block->bundles.data;
444 memmove(bundles + after + 2, bundles + after + 1,
445 (count - after - 1) * sizeof(midgard_bundle));
446 midgard_bundle *after_bundle = bundles + after;
447
448 midgard_bundle new = mir_bundle_for_op(ctx, ins);
449 memcpy(bundles + after + 1, &new, sizeof(new));
450 list_add(
451 &new.instructions[0]->link,
452 &after_bundle->instructions[after_bundle->instruction_count - 1]->link);
453 block->quadword_count += midgard_tag_props[new.tag].size;
454
455 return new.instructions[0];
456 }
457
458 /* Flip the first-two arguments of a (binary) op. Currently ALU
459 * only, no known uses for ldst/tex */
460
461 void
462 mir_flip(midgard_instruction *ins)
463 {
464 unsigned temp = ins->src[0];
465 ins->src[0] = ins->src[1];
466 ins->src[1] = temp;
467
468 assert(ins->type == TAG_ALU_4);
469
470 temp = ins->src_types[0];
471 ins->src_types[0] = ins->src_types[1];
472 ins->src_types[1] = temp;
473
474 temp = ins->src_abs[0];
475 ins->src_abs[0] = ins->src_abs[1];
476 ins->src_abs[1] = temp;
477
478 temp = ins->src_neg[0];
479 ins->src_neg[0] = ins->src_neg[1];
480 ins->src_neg[1] = temp;
481
482 temp = ins->src_invert[0];
483 ins->src_invert[0] = ins->src_invert[1];
484 ins->src_invert[1] = temp;
485
486 unsigned temp_swizzle[16];
487 memcpy(temp_swizzle, ins->swizzle[0], sizeof(ins->swizzle[0]));
488 memcpy(ins->swizzle[0], ins->swizzle[1], sizeof(ins->swizzle[0]));
489 memcpy(ins->swizzle[1], temp_swizzle, sizeof(ins->swizzle[0]));
490 }
491
492 /* Before squashing, calculate ctx->temp_count just by observing the MIR */
493
494 void
495 mir_compute_temp_count(compiler_context *ctx)
496 {
497 unsigned max_index = 0;
498
499 mir_foreach_instr_global(ctx, ins) {
500 if (ins->dest < SSA_FIXED_MINIMUM)
501 max_index = MAX2(max_index, ins->dest + 1);
502 }
503
504 if (ctx->blend_input != ~0)
505 max_index = MAX2(max_index, ctx->blend_input + 1);
506
507 if (ctx->blend_src1 != ~0)
508 max_index = MAX2(max_index, ctx->blend_src1 + 1);
509
510 ctx->temp_count = max_index;
511 }