Debian Codegraph @

src/panfrost/compiler/midgard/midgard_schedule.c

raw
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig
3 * Copyright (C) 2019-2020 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "util/half_float.h"
8 #include "util/u_math.h"
9 #include "util/u_memory.h"
10 #include "compiler.h"
11 #include "midgard_ops.h"
12 #include "midgard_quirks.h"
13
14 /* Scheduling for Midgard is complicated, to say the least. ALU instructions
15 * must be grouped into VLIW bundles according to following model:
16 *
17 * [VMUL] [SADD]
18 * [VADD] [SMUL] [VLUT]
19 *
20 * A given instruction can execute on some subset of the units (or a few can
21 * execute on all). Instructions can be either vector or scalar; only scalar
22 * instructions can execute on SADD/SMUL units. Units on a given line execute
23 * in parallel. Subsequent lines execute separately and can pass results
24 * directly via pipeline registers r24/r25, bypassing the register file.
25 *
26 * A bundle can optionally have 128-bits of embedded constants, shared across
27 * all of the instructions within a bundle.
28 *
29 * Instructions consuming conditionals (branches and conditional selects)
30 * require their condition to be written into the conditional register (r31)
31 * within the same bundle they are consumed.
32 *
33 * Fragment writeout requires its argument to be written in full within the
34 * same bundle as the branch, with no hanging dependencies.
35 *
36 * Load/store instructions are also in bundles of simply two instructions, and
37 * texture instructions have no bundling.
38 *
39 * -------------------------------------------------------------------------
40 *
41 */
42
43 /* We create the dependency graph with per-byte granularity */
44
45 #define BYTE_COUNT 16
46
47 static void
48 add_dependency(struct util_dynarray *table, unsigned index, uint16_t mask,
49 midgard_instruction **instructions, unsigned child)
50 {
51 for (unsigned i = 0; i < BYTE_COUNT; ++i) {
52 if (!(mask & (1 << i)))
53 continue;
54
55 struct util_dynarray *parents = &table[(BYTE_COUNT * index) + i];
56
57 util_dynarray_foreach(parents, unsigned, parent) {
58 BITSET_WORD *dependents = instructions[*parent]->dependents;
59
60 /* Already have the dependency */
61 if (BITSET_TEST(dependents, child))
62 continue;
63
64 BITSET_SET(dependents, child);
65 instructions[child]->nr_dependencies++;
66 }
67 }
68 }
69
70 static void
71 mark_access(struct util_dynarray *table, unsigned index, uint16_t mask,
72 unsigned parent)
73 {
74 for (unsigned i = 0; i < BYTE_COUNT; ++i) {
75 if (!(mask & (1 << i)))
76 continue;
77
78 util_dynarray_append(&table[(BYTE_COUNT * index) + i], parent);
79 }
80 }
81
82 static void
83 mir_create_dependency_graph(midgard_instruction **instructions, unsigned count,
84 unsigned node_count)
85 {
86 size_t sz = node_count * BYTE_COUNT;
87
88 struct util_dynarray *last_read = calloc(sizeof(struct util_dynarray), sz);
89 struct util_dynarray *last_write = calloc(sizeof(struct util_dynarray), sz);
90
91 for (unsigned i = 0; i < sz; ++i) {
92 last_read[i] = UTIL_DYNARRAY_INIT;
93 last_write[i] = UTIL_DYNARRAY_INIT;
94 }
95
96 /* Initialize dependency graph */
97 for (unsigned i = 0; i < count; ++i) {
98 instructions[i]->dependents = BITSET_CALLOC(count);
99
100 instructions[i]->nr_dependencies = 0;
101 }
102
103 unsigned prev_ldst[3] = {~0, ~0, ~0};
104
105 /* Populate dependency graph */
106 for (signed i = count - 1; i >= 0; --i) {
107 if (instructions[i]->compact_branch)
108 continue;
109
110 unsigned dest = instructions[i]->dest;
111 unsigned mask = mir_bytemask(instructions[i]);
112
113 mir_foreach_src((*instructions), s) {
114 unsigned src = instructions[i]->src[s];
115
116 if (src < node_count) {
117 unsigned readmask =
118 mir_bytemask_of_read_components(instructions[i], src);
119 add_dependency(last_write, src, readmask, instructions, i);
120 }
121 }
122
123 /* Create a list of dependencies for each type of load/store
124 * instruction to prevent reordering. */
125 if (instructions[i]->type == TAG_LOAD_STORE_4 &&
126 load_store_opcode_props[instructions[i]->op].props & LDST_ADDRESS) {
127
128 unsigned type = instructions[i]->load_store.arg_reg |
129 instructions[i]->load_store.arg_comp;
130
131 unsigned idx;
132 switch (type) {
133 case LDST_SHARED:
134 idx = 0;
135 break;
136 case LDST_SCRATCH:
137 idx = 1;
138 break;
139 default:
140 idx = 2;
141 break;
142 }
143
144 unsigned prev = prev_ldst[idx];
145
146 if (prev != ~0) {
147 BITSET_WORD *dependents = instructions[prev]->dependents;
148
149 /* Already have the dependency */
150 if (BITSET_TEST(dependents, i))
151 continue;
152
153 BITSET_SET(dependents, i);
154 instructions[i]->nr_dependencies++;
155 }
156
157 prev_ldst[idx] = i;
158 }
159
160 if (dest < node_count) {
161 add_dependency(last_read, dest, mask, instructions, i);
162 add_dependency(last_write, dest, mask, instructions, i);
163 mark_access(last_write, dest, mask, i);
164 }
165
166 mir_foreach_src((*instructions), s) {
167 unsigned src = instructions[i]->src[s];
168
169 if (src < node_count) {
170 unsigned readmask =
171 mir_bytemask_of_read_components(instructions[i], src);
172 mark_access(last_read, src, readmask, i);
173 }
174 }
175 }
176
177 /* If there is a branch, all instructions depend on it, as interblock
178 * execution must be purely in-order */
179
180 if (instructions[count - 1]->compact_branch) {
181 BITSET_WORD *dependents = instructions[count - 1]->dependents;
182
183 for (signed i = count - 2; i >= 0; --i) {
184 if (BITSET_TEST(dependents, i))
185 continue;
186
187 BITSET_SET(dependents, i);
188 instructions[i]->nr_dependencies++;
189 }
190 }
191
192 /* Free the intermediate structures */
193 for (unsigned i = 0; i < sz; ++i) {
194 util_dynarray_fini(&last_read[i]);
195 util_dynarray_fini(&last_write[i]);
196 }
197
198 free(last_read);
199 free(last_write);
200 }
201
202 /* Does the mask cover more than a scalar? */
203
204 static bool
205 is_single_component_mask(unsigned mask)
206 {
207 int components = 0;
208
209 for (int c = 0; c < 8; ++c) {
210 if (mask & (1 << c))
211 components++;
212 }
213
214 return components == 1;
215 }
216
217 /* Helpers for scheudling */
218
219 static bool
220 mir_is_scalar(midgard_instruction *ains)
221 {
222 /* Do we try to use it as a vector op? */
223 if (!is_single_component_mask(ains->mask))
224 return false;
225
226 /* Otherwise, check mode hazards */
227 bool could_scalar = true;
228 unsigned szd = nir_alu_type_get_type_size(ains->dest_type);
229 unsigned sz0 = nir_alu_type_get_type_size(ains->src_types[0]);
230 unsigned sz1 = nir_alu_type_get_type_size(ains->src_types[1]);
231
232 /* Only 16/32-bit can run on a scalar unit */
233 could_scalar &= (szd == 16) || (szd == 32);
234
235 if (ains->src[0] != ~0)
236 could_scalar &= (sz0 == 16) || (sz0 == 32);
237
238 if (ains->src[1] != ~0)
239 could_scalar &= (sz1 == 16) || (sz1 == 32);
240
241 if (midgard_is_integer_out_op(ains->op) &&
242 ains->outmod != midgard_outmod_keeplo)
243 return false;
244
245 return could_scalar;
246 }
247
248 /* How many bytes does this ALU instruction add to the bundle? */
249
250 static unsigned
251 bytes_for_instruction(midgard_instruction *ains)
252 {
253 if (ains->unit & UNITS_ANY_VECTOR)
254 return sizeof(midgard_reg_info) + sizeof(midgard_vector_alu);
255 else if (ains->unit == ALU_ENAB_BRANCH)
256 return sizeof(midgard_branch_extended);
257 else if (ains->compact_branch)
258 return sizeof(uint16_t);
259 else
260 return sizeof(midgard_reg_info) + sizeof(midgard_scalar_alu);
261 }
262
263 /* We would like to flatten the linked list of midgard_instructions in a bundle
264 * to an array of pointers on the heap for easy indexing */
265
266 static midgard_instruction **
267 flatten_mir(midgard_block *block, unsigned *len)
268 {
269 *len = list_length(&block->instructions);
270
271 if (!(*len))
272 return NULL;
273
274 midgard_instruction **instructions =
275 calloc(sizeof(midgard_instruction *), *len);
276
277 unsigned i = 0;
278
279 mir_foreach_instr_in_block(block, ins)
280 instructions[i++] = ins;
281
282 return instructions;
283 }
284
285 /* The worklist is the set of instructions that can be scheduled now; that is,
286 * the set of instructions with no remaining dependencies */
287
288 static void
289 mir_initialize_worklist(BITSET_WORD *worklist,
290 midgard_instruction **instructions, unsigned count)
291 {
292 for (unsigned i = 0; i < count; ++i) {
293 if (instructions[i]->nr_dependencies == 0)
294 BITSET_SET(worklist, i);
295 }
296 }
297
298 /* Update the worklist after an instruction terminates. Remove its edges from
299 * the graph and if that causes any node to have no dependencies, add it to the
300 * worklist */
301
302 static void
303 mir_update_worklist(BITSET_WORD *worklist, unsigned count,
304 midgard_instruction **instructions,
305 midgard_instruction *done)
306 {
307 /* Sanity check: if no instruction terminated, there is nothing to do.
308 * If the instruction that terminated had dependencies, that makes no
309 * sense and means we messed up the worklist. Finally, as the purpose
310 * of this routine is to update dependents, we abort early if there are
311 * no dependents defined. */
312
313 if (!done)
314 return;
315
316 assert(done->nr_dependencies == 0);
317
318 if (!done->dependents)
319 return;
320
321 /* We have an instruction with dependents. Iterate each dependent to
322 * remove one dependency (`done`), adding dependents to the worklist
323 * where possible. */
324
325 unsigned i;
326 BITSET_FOREACH_SET(i, done->dependents, count) {
327 assert(instructions[i]->nr_dependencies);
328
329 if (!(--instructions[i]->nr_dependencies))
330 BITSET_SET(worklist, i);
331 }
332
333 free(done->dependents);
334 }
335
336 /* While scheduling, we need to choose instructions satisfying certain
337 * criteria. As we schedule backwards, we choose the *last* instruction in the
338 * worklist to simulate in-order scheduling. Chosen instructions must satisfy a
339 * given predicate. */
340
341 struct midgard_predicate {
342 /* TAG or ~0 for dont-care */
343 unsigned tag;
344
345 /* True if we want to pop off the chosen instruction */
346 bool destructive;
347
348 /* For ALU, choose only this unit */
349 unsigned unit;
350
351 /* State for bundle constants. constants is the actual constants
352 * for the bundle. constant_count is the number of bytes (up to
353 * 16) currently in use for constants. When picking in destructive
354 * mode, the constants array will be updated, and the instruction
355 * will be adjusted to index into the constants array */
356
357 midgard_constants *constants;
358 unsigned constant_mask;
359
360 /* Exclude this destination (if not ~0) */
361 unsigned exclude;
362
363 /* Don't schedule instructions consuming conditionals (since we already
364 * scheduled one). Excludes conditional branches and csel */
365 bool no_cond;
366
367 /* Require (or reject) a minimal mask and (if nonzero) given
368 * destination. Used for writeout optimizations */
369
370 unsigned mask;
371 unsigned no_mask;
372 unsigned dest;
373
374 /* Whether to not-care/only/never schedule imov/fmov instructions This
375 * allows non-move instructions to get priority on each unit */
376 unsigned move_mode;
377
378 /* For load/store: how many pipeline registers are in use? The two
379 * scheduled instructions cannot use more than the 256-bits of pipeline
380 * space available or RA will fail (as it would run out of pipeline
381 * registers and fail to spill without breaking the schedule) */
382
383 unsigned pipeline_count;
384
385 /* For load/store: is a ST_VARY.a32 instruction scheduled into the
386 * bundle? is a non-ST_VARY.a32 instruction scheduled? Potential
387 * hardware issue, unknown cause.
388 */
389 bool any_st_vary_a32, any_non_st_vary_a32;
390 };
391
392 static bool
393 mir_adjust_constant(midgard_instruction *ins, unsigned src,
394 unsigned *bundle_constant_mask, unsigned *comp_mapping,
395 uint8_t *bundle_constants, bool upper)
396 {
397 unsigned type_size = nir_alu_type_get_type_size(ins->src_types[src]) / 8;
398 unsigned type_shift = util_logbase2(type_size);
399 unsigned max_comp = mir_components_for_type(ins->src_types[src]);
400 unsigned comp_mask = mir_from_bytemask(
401 mir_round_bytemask_up(mir_bytemask_of_read_components_index(ins, src),
402 type_size * 8),
403 type_size * 8);
404 unsigned type_mask = (1 << type_size) - 1;
405
406 /* Upper only makes sense for 16-bit */
407 if (type_size != 16 && upper)
408 return false;
409
410 /* For 16-bit, we need to stay on either upper or lower halves to avoid
411 * disrupting the swizzle */
412 unsigned start = upper ? 8 : 0;
413 unsigned length = (type_size == 2) ? 8 : 16;
414
415 for (unsigned comp = 0; comp < max_comp; comp++) {
416 if (!(comp_mask & (1 << comp)))
417 continue;
418
419 uint8_t *constantp = ins->constants.u8 + (type_size * comp);
420 unsigned best_reuse_bytes = 0;
421 signed best_place = -1;
422 unsigned i, j;
423
424 for (i = start; i < (start + length); i += type_size) {
425 unsigned reuse_bytes = 0;
426
427 for (j = 0; j < type_size; j++) {
428 if (!(*bundle_constant_mask & (1 << (i + j))))
429 continue;
430 if (constantp[j] != bundle_constants[i + j])
431 break;
432 if ((i + j) > (start + length))
433 break;
434
435 reuse_bytes++;
436 }
437
438 /* Select the place where existing bytes can be
439 * reused so we leave empty slots to others
440 */
441 if (j == type_size &&
442 (reuse_bytes > best_reuse_bytes || best_place < 0)) {
443 best_reuse_bytes = reuse_bytes;
444 best_place = i;
445 break;
446 }
447 }
448
449 /* This component couldn't fit in the remaining constant slot,
450 * no need check the remaining components, bail out now
451 */
452 if (best_place < 0)
453 return false;
454
455 memcpy(&bundle_constants[i], constantp, type_size);
456 *bundle_constant_mask |= type_mask << best_place;
457 comp_mapping[comp] = best_place >> type_shift;
458 }
459
460 return true;
461 }
462
463 /* For an instruction that can fit, adjust it to fit and update the constants
464 * array, in destructive mode. Returns whether the fitting was successful. */
465
466 static bool
467 mir_adjust_constants(midgard_instruction *ins, struct midgard_predicate *pred,
468 bool destructive)
469 {
470 /* No constant, nothing to adjust */
471 if (!ins->has_constants)
472 return true;
473
474 unsigned r_constant = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
475 unsigned bundle_constant_mask = pred->constant_mask;
476 unsigned comp_mapping[2][16] = {};
477 uint8_t bundle_constants[16];
478
479 memcpy(bundle_constants, pred->constants, 16);
480
481 /* Let's try to find a place for each active component of the constant
482 * register.
483 */
484 for (unsigned src = 0; src < 2; ++src) {
485 if (ins->src[src] != SSA_FIXED_REGISTER(REGISTER_CONSTANT))
486 continue;
487
488 /* First, try lower half (or whole for !16) */
489 if (mir_adjust_constant(ins, src, &bundle_constant_mask,
490 comp_mapping[src], bundle_constants, false))
491 continue;
492
493 /* Next, try upper half */
494 if (mir_adjust_constant(ins, src, &bundle_constant_mask,
495 comp_mapping[src], bundle_constants, true))
496 continue;
497
498 /* Otherwise bail */
499 return false;
500 }
501
502 /* If non-destructive, we're done */
503 if (!destructive)
504 return true;
505
506 /* Otherwise update the constant_mask and constant values */
507 pred->constant_mask = bundle_constant_mask;
508 memcpy(pred->constants, bundle_constants, 16);
509
510 /* Use comp_mapping as a swizzle */
511 mir_foreach_src(ins, s) {
512 if (ins->src[s] == r_constant)
513 mir_compose_swizzle(ins->swizzle[s], comp_mapping[s], ins->swizzle[s]);
514 }
515
516 return true;
517 }
518
519 /* Conservative estimate of the pipeline registers required for load/store */
520
521 static unsigned
522 mir_pipeline_count(midgard_instruction *ins)
523 {
524 unsigned bytecount = 0;
525
526 mir_foreach_src(ins, i) {
527 /* Skip empty source */
528 if (ins->src[i] == ~0)
529 continue;
530
531 if (i == 0) {
532 /* First source is a vector, worst-case the mask */
533 unsigned bytemask = mir_bytemask_of_read_components_index(ins, i);
534 unsigned max = util_logbase2(bytemask) + 1;
535 bytecount += max;
536 } else {
537 /* Sources 1 on are scalars */
538 bytecount += 4;
539 }
540 }
541
542 unsigned dwords = DIV_ROUND_UP(bytecount, 16);
543 assert(dwords <= 2);
544
545 return dwords;
546 }
547
548 /* Matches FADD x, x with modifiers compatible. Since x + x = x * 2, for
549 * any x including of the form f(y) for some swizzle/abs/neg function f */
550
551 static bool
552 mir_is_add_2(midgard_instruction *ins)
553 {
554 if (ins->op != midgard_alu_op_fadd)
555 return false;
556
557 if (ins->src[0] != ins->src[1])
558 return false;
559
560 if (ins->src_types[0] != ins->src_types[1])
561 return false;
562
563 for (unsigned i = 0; i < MIR_VEC_COMPONENTS; ++i) {
564 if (ins->swizzle[0][i] != ins->swizzle[1][i])
565 return false;
566 }
567
568 if (ins->src_abs[0] != ins->src_abs[1])
569 return false;
570
571 if (ins->src_neg[0] != ins->src_neg[1])
572 return false;
573
574 return true;
575 }
576
577 static void
578 mir_adjust_unit(midgard_instruction *ins, unsigned unit)
579 {
580 /* FADD x, x = FMUL x, #2 */
581 if (mir_is_add_2(ins) && (unit & (UNITS_MUL | UNIT_VLUT))) {
582 ins->op = midgard_alu_op_fmul;
583
584 ins->src[1] = ~0;
585 ins->src_abs[1] = false;
586 ins->src_neg[1] = false;
587
588 ins->has_inline_constant = true;
589 ins->inline_constant = _mesa_float_to_half(2.0);
590 }
591 }
592
593 static unsigned
594 mir_has_unit(midgard_instruction *ins, unsigned unit)
595 {
596 if (alu_opcode_props[ins->op].props & unit)
597 return true;
598
599 /* FADD x, x can run on any adder or any multiplier */
600 if (mir_is_add_2(ins))
601 return true;
602
603 return false;
604 }
605
606 /* Net change in liveness if an instruction were scheduled. Loosely based on
607 * ir3's scheduler. */
608
609 static int
610 mir_live_effect(uint16_t *liveness, midgard_instruction *ins, bool destructive)
611 {
612 /* TODO: what if dest is used multiple times? */
613 int free_live = 0;
614
615 if (ins->dest < SSA_FIXED_MINIMUM) {
616 unsigned bytemask = mir_bytemask(ins);
617 bytemask = util_next_power_of_two(bytemask + 1) - 1;
618 free_live += util_bitcount(liveness[ins->dest] & bytemask);
619
620 if (destructive)
621 liveness[ins->dest] &= ~bytemask;
622 }
623
624 int new_live = 0;
625
626 mir_foreach_src(ins, s) {
627 unsigned S = ins->src[s];
628
629 bool dupe = false;
630
631 for (unsigned q = 0; q < s; ++q)
632 dupe |= (ins->src[q] == S);
633
634 if (dupe)
635 continue;
636
637 if (S < SSA_FIXED_MINIMUM) {
638 unsigned bytemask = mir_bytemask_of_read_components(ins, S);
639 bytemask = util_next_power_of_two(bytemask + 1) - 1;
640
641 /* Count only the new components */
642 new_live += util_bitcount(bytemask & ~(liveness[S]));
643
644 if (destructive)
645 liveness[S] |= bytemask;
646 }
647 }
648
649 return new_live - free_live;
650 }
651
652 static midgard_instruction *
653 mir_choose_instruction(midgard_instruction **instructions, uint16_t *liveness,
654 BITSET_WORD *worklist, unsigned count,
655 struct midgard_predicate *predicate)
656 {
657 /* Parse the predicate */
658 unsigned tag = predicate->tag;
659 unsigned unit = predicate->unit;
660 bool scalar = (unit != ~0) && (unit & UNITS_SCALAR);
661 bool no_cond = predicate->no_cond;
662
663 unsigned mask = predicate->mask;
664 unsigned dest = predicate->dest;
665 bool needs_dest = mask & 0xF;
666
667 /* Iterate to find the best instruction satisfying the predicate */
668 unsigned i;
669
670 signed best_index = -1;
671 signed best_effect = INT_MAX;
672 bool best_conditional = false;
673
674 /* Enforce a simple metric limiting distance to keep down register
675 * pressure. TOOD: replace with liveness tracking for much better
676 * results */
677
678 unsigned max_active = 0;
679 unsigned max_distance = 36;
680
681 #ifndef NDEBUG
682 /* Force in-order scheduling */
683 if (midgard_debug & MIDGARD_DBG_INORDER)
684 max_distance = 1;
685 #endif
686
687 BITSET_FOREACH_SET(i, worklist, count) {
688 max_active = MAX2(max_active, i);
689 }
690
691 BITSET_FOREACH_SET(i, worklist, count) {
692 if ((max_active - i) >= max_distance)
693 continue;
694
695 if (tag != ~0 && instructions[i]->type != tag)
696 continue;
697
698 bool alu = (instructions[i]->type == TAG_ALU_4);
699 bool ldst = (instructions[i]->type == TAG_LOAD_STORE_4);
700
701 bool branch = alu && (unit == ALU_ENAB_BR_COMPACT);
702 bool is_move = alu && (instructions[i]->op == midgard_alu_op_imov ||
703 instructions[i]->op == midgard_alu_op_fmov);
704
705 if (predicate->exclude != ~0 &&
706 instructions[i]->dest == predicate->exclude)
707 continue;
708
709 if (alu && !branch && unit != ~0 &&
710 !(mir_has_unit(instructions[i], unit)))
711 continue;
712
713 /* 0: don't care, 1: no moves, 2: only moves */
714 if (predicate->move_mode && ((predicate->move_mode - 1) != is_move))
715 continue;
716
717 if (branch && !instructions[i]->compact_branch)
718 continue;
719
720 if (alu && scalar && !mir_is_scalar(instructions[i]))
721 continue;
722
723 if (alu && predicate->constants &&
724 !mir_adjust_constants(instructions[i], predicate, false))
725 continue;
726
727 if (needs_dest && instructions[i]->dest != dest)
728 continue;
729
730 if (mask && ((~instructions[i]->mask) & mask))
731 continue;
732
733 if (instructions[i]->mask & predicate->no_mask)
734 continue;
735
736 if (ldst &&
737 mir_pipeline_count(instructions[i]) + predicate->pipeline_count > 2)
738 continue;
739
740 bool st_vary_a32 = (instructions[i]->op == midgard_op_st_vary_32);
741
742 if (ldst && predicate->any_non_st_vary_a32 && st_vary_a32)
743 continue;
744
745 if (ldst && predicate->any_st_vary_a32 && !st_vary_a32)
746 continue;
747
748 bool conditional = alu && !branch && OP_IS_CSEL(instructions[i]->op);
749 conditional |= (branch && instructions[i]->branch.conditional);
750
751 if (conditional && no_cond)
752 continue;
753
754 int effect = mir_live_effect(liveness, instructions[i], false);
755
756 if (effect > best_effect)
757 continue;
758
759 if (effect == best_effect && (signed)i < best_index)
760 continue;
761
762 best_effect = effect;
763 best_index = i;
764 best_conditional = conditional;
765 }
766
767 /* Did we find anything? */
768
769 if (best_index < 0)
770 return NULL;
771
772 /* If we found something, remove it from the worklist */
773 assert(best_index < count);
774 midgard_instruction *I = instructions[best_index];
775
776 if (predicate->destructive) {
777 BITSET_CLEAR(worklist, best_index);
778
779 if (I->type == TAG_ALU_4)
780 mir_adjust_constants(instructions[best_index], predicate, true);
781
782 if (I->type == TAG_LOAD_STORE_4) {
783 predicate->pipeline_count +=
784 mir_pipeline_count(instructions[best_index]);
785
786 if (instructions[best_index]->op == midgard_op_st_vary_32)
787 predicate->any_st_vary_a32 = true;
788 else
789 predicate->any_non_st_vary_a32 = true;
790 }
791
792 if (I->type == TAG_ALU_4)
793 mir_adjust_unit(instructions[best_index], unit);
794
795 /* Once we schedule a conditional, we can't again */
796 predicate->no_cond |= best_conditional;
797 mir_live_effect(liveness, instructions[best_index], true);
798 }
799
800 return I;
801 }
802
803 /* Still, we don't choose instructions in a vacuum. We need a way to choose the
804 * best bundle type (ALU, load/store, texture). Nondestructive. */
805
806 static unsigned
807 mir_choose_bundle(midgard_instruction **instructions, uint16_t *liveness,
808 BITSET_WORD *worklist, unsigned count, unsigned num_ldst)
809 {
810 /* At the moment, our algorithm is very simple - use the bundle of the
811 * best instruction, regardless of what else could be scheduled
812 * alongside it. This is not optimal but it works okay for in-order */
813
814 struct midgard_predicate predicate = {
815 .tag = ~0,
816 .unit = ~0,
817 .destructive = false,
818 .exclude = ~0,
819 };
820
821 midgard_instruction *chosen = mir_choose_instruction(
822 instructions, liveness, worklist, count, &predicate);
823
824 if (chosen && chosen->type == TAG_LOAD_STORE_4 && !(num_ldst % 2)) {
825 /* Try to schedule load/store ops in pairs */
826
827 predicate.exclude = chosen->dest;
828 predicate.tag = TAG_LOAD_STORE_4;
829
830 chosen = mir_choose_instruction(instructions, liveness, worklist, count,
831 &predicate);
832 if (chosen)
833 return TAG_LOAD_STORE_4;
834
835 predicate.tag = ~0;
836
837 chosen = mir_choose_instruction(instructions, liveness, worklist, count,
838 &predicate);
839 assert(chosen == NULL || chosen->type != TAG_LOAD_STORE_4);
840
841 if (chosen)
842 return chosen->type;
843 else
844 return TAG_LOAD_STORE_4;
845 }
846
847 if (chosen)
848 return chosen->type;
849 else
850 return ~0;
851 }
852
853 /* We want to choose an ALU instruction filling a given unit */
854 static void
855 mir_choose_alu(midgard_instruction **slot, midgard_instruction **instructions,
856 uint16_t *liveness, BITSET_WORD *worklist, unsigned len,
857 struct midgard_predicate *predicate, unsigned unit)
858 {
859 /* Did we already schedule to this slot? */
860 if ((*slot) != NULL)
861 return;
862
863 /* Try to schedule something, if not */
864 predicate->unit = unit;
865 *slot =
866 mir_choose_instruction(instructions, liveness, worklist, len, predicate);
867
868 /* Store unit upon scheduling */
869 if (*slot && !((*slot)->compact_branch))
870 (*slot)->unit = unit;
871 }
872
873 /* When we are scheduling a branch/csel, we need the consumed condition in the
874 * same block as a pipeline register. There are two options to enable this:
875 *
876 * - Move the conditional into the bundle. Preferred, but only works if the
877 * conditional is used only once and is from this block.
878 * - Copy the conditional.
879 *
880 * We search for the conditional. If it's in this block, single-use, and
881 * without embedded constants, we schedule it immediately. Otherwise, we
882 * schedule a move for it.
883 *
884 * mir_comparison_mobile is a helper to find the moveable condition.
885 */
886
887 static unsigned
888 mir_comparison_mobile(compiler_context *ctx, midgard_instruction **instructions,
889 struct midgard_predicate *predicate, unsigned count,
890 unsigned cond)
891 {
892 if (!mir_single_use(ctx, cond))
893 return ~0;
894
895 unsigned ret = ~0;
896
897 for (unsigned i = 0; i < count; ++i) {
898 if (instructions[i]->dest != cond)
899 continue;
900
901 /* Must fit in an ALU bundle */
902 if (instructions[i]->type != TAG_ALU_4)
903 return ~0;
904
905 /* If it would itself require a condition, that's recursive */
906 if (OP_IS_CSEL(instructions[i]->op))
907 return ~0;
908
909 /* We'll need to rewrite to .w but that doesn't work for vector
910 * ops that don't replicate (ball/bany), so bail there */
911
912 if (GET_CHANNEL_COUNT(alu_opcode_props[instructions[i]->op].props))
913 return ~0;
914
915 /* Ensure it will fit with constants */
916
917 if (!mir_adjust_constants(instructions[i], predicate, false))
918 return ~0;
919
920 /* Ensure it is written only once */
921
922 if (ret != ~0)
923 return ~0;
924 else
925 ret = i;
926 }
927
928 /* Inject constants now that we are sure we want to */
929 if (ret != ~0)
930 mir_adjust_constants(instructions[ret], predicate, true);
931
932 return ret;
933 }
934
935 /* Using the information about the moveable conditional itself, we either pop
936 * that condition off the worklist for use now, or create a move to
937 * artificially schedule instead as a fallback */
938
939 static midgard_instruction *
940 mir_schedule_comparison(compiler_context *ctx,
941 midgard_instruction **instructions,
942 struct midgard_predicate *predicate,
943 BITSET_WORD *worklist, unsigned count, unsigned cond,
944 bool vector, unsigned *swizzle,
945 midgard_instruction *user)
946 {
947 /* TODO: swizzle when scheduling */
948 unsigned comp_i =
949 (!vector && (swizzle[0] == 0))
950 ? mir_comparison_mobile(ctx, instructions, predicate, count, cond)
951 : ~0;
952
953 /* If we can, schedule the condition immediately */
954 if ((comp_i != ~0) && BITSET_TEST(worklist, comp_i)) {
955 assert(comp_i < count);
956 BITSET_CLEAR(worklist, comp_i);
957 return instructions[comp_i];
958 }
959
960 /* Otherwise, we insert a move */
961
962 midgard_instruction mov = v_mov(cond, cond);
963 mov.mask = vector ? 0xF : 0x1;
964 memcpy(mov.swizzle[1], swizzle, sizeof(mov.swizzle[1]));
965
966 return mir_insert_instruction_before(ctx, user, &mov);
967 }
968
969 /* Most generally, we need instructions writing to r31 in the appropriate
970 * components */
971
972 static midgard_instruction *
973 mir_schedule_condition(compiler_context *ctx,
974 struct midgard_predicate *predicate,
975 BITSET_WORD *worklist, unsigned count,
976 midgard_instruction **instructions,
977 midgard_instruction *last)
978 {
979 /* For a branch, the condition is the only argument; for csel, third */
980 bool branch = last->compact_branch;
981 unsigned condition_index = branch ? 0 : 2;
982
983 /* csel_v is vector; otherwise, conditions are scalar */
984 bool vector = !branch && OP_IS_CSEL_V(last->op);
985
986 /* Grab the conditional instruction */
987
988 midgard_instruction *cond = mir_schedule_comparison(
989 ctx, instructions, predicate, worklist, count, last->src[condition_index],
990 vector, last->swizzle[condition_index], last);
991
992 /* We have exclusive reign over this (possibly move) conditional
993 * instruction. We can rewrite into a pipeline conditional register */
994
995 predicate->exclude = cond->dest;
996 cond->dest = SSA_FIXED_REGISTER(31);
997 last->src[condition_index] = cond->dest;
998
999 if (!vector) {
1000 cond->mask = (1 << COMPONENT_W);
1001
1002 mir_foreach_src(cond, s) {
1003 if (cond->src[s] == ~0)
1004 continue;
1005
1006 for (unsigned q = 0; q < 4; ++q)
1007 cond->swizzle[s][q + COMPONENT_W] = cond->swizzle[s][q];
1008 }
1009
1010 last->swizzle[condition_index][0] = COMPONENT_W;
1011 }
1012
1013 /* Schedule the unit: csel is always in the latter pipeline, so a csel
1014 * condition must be in the former pipeline stage (vmul/sadd),
1015 * depending on scalar/vector of the instruction itself. A branch must
1016 * be written from the latter pipeline stage and a branch condition is
1017 * always scalar, so it is always in smul (exception: ball/bany, which
1018 * will be vadd) */
1019
1020 if (branch)
1021 cond->unit = UNIT_SMUL;
1022 else
1023 cond->unit = vector ? UNIT_VMUL : UNIT_SADD;
1024
1025 return cond;
1026 }
1027
1028 /* Schedules a single bundle of the given type */
1029
1030 static midgard_bundle
1031 mir_schedule_texture(midgard_instruction **instructions, uint16_t *liveness,
1032 BITSET_WORD *worklist, unsigned len, bool is_vertex)
1033 {
1034 struct midgard_predicate predicate = {
1035 .tag = TAG_TEXTURE_4,
1036 .destructive = true,
1037 .exclude = ~0,
1038 };
1039
1040 midgard_instruction *ins =
1041 mir_choose_instruction(instructions, liveness, worklist, len, &predicate);
1042
1043 mir_update_worklist(worklist, len, instructions, ins);
1044
1045 struct midgard_bundle out = {
1046 .tag = ins->op == midgard_tex_op_barrier ? TAG_TEXTURE_4_BARRIER
1047 : (ins->op == midgard_tex_op_fetch) || is_vertex
1048 ? TAG_TEXTURE_4_VTX
1049 : TAG_TEXTURE_4,
1050 .instruction_count = 1,
1051 .instructions = {ins},
1052 };
1053
1054 return out;
1055 }
1056
1057 static midgard_bundle
1058 mir_schedule_ldst(midgard_instruction **instructions, uint16_t *liveness,
1059 BITSET_WORD *worklist, unsigned len, unsigned *num_ldst)
1060 {
1061 struct midgard_predicate predicate = {
1062 .tag = TAG_LOAD_STORE_4,
1063 .destructive = true,
1064 .exclude = ~0,
1065 };
1066
1067 /* Try to pick two load/store ops. Second not gauranteed to exist */
1068
1069 midgard_instruction *ins =
1070 mir_choose_instruction(instructions, liveness, worklist, len, &predicate);
1071
1072 midgard_instruction *pair =
1073 mir_choose_instruction(instructions, liveness, worklist, len, &predicate);
1074
1075 assert(ins != NULL);
1076
1077 struct midgard_bundle out = {
1078 .tag = TAG_LOAD_STORE_4,
1079 .instruction_count = pair ? 2 : 1,
1080 .instructions = {ins, pair},
1081 };
1082
1083 *num_ldst -= out.instruction_count;
1084
1085 /* We have to update the worklist atomically, since the two
1086 * instructions run concurrently (TODO: verify it's not pipelined) */
1087
1088 mir_update_worklist(worklist, len, instructions, ins);
1089 mir_update_worklist(worklist, len, instructions, pair);
1090
1091 return out;
1092 }
1093
1094 static void
1095 mir_schedule_zs_write(compiler_context *ctx,
1096 struct midgard_predicate *predicate,
1097 midgard_instruction **instructions, uint16_t *liveness,
1098 BITSET_WORD *worklist, unsigned len,
1099 midgard_instruction *branch, midgard_instruction **smul,
1100 midgard_instruction **vadd, midgard_instruction **vlut,
1101 bool stencil)
1102 {
1103 bool success = false;
1104 unsigned idx = stencil ? 3 : 2;
1105 unsigned src =
1106 (branch->src[0] == ~0) ? SSA_FIXED_REGISTER(1) : branch->src[idx];
1107
1108 predicate->dest = src;
1109 predicate->mask = 0x1;
1110
1111 midgard_instruction **units[] = {smul, vadd, vlut};
1112 unsigned unit_names[] = {UNIT_SMUL, UNIT_VADD, UNIT_VLUT};
1113
1114 for (unsigned i = 0; i < 3; ++i) {
1115 if (*(units[i]))
1116 continue;
1117
1118 predicate->unit = unit_names[i];
1119 midgard_instruction *ins = mir_choose_instruction(
1120 instructions, liveness, worklist, len, predicate);
1121
1122 if (ins) {
1123 ins->unit = unit_names[i];
1124 *(units[i]) = ins;
1125 success |= true;
1126 break;
1127 }
1128 }
1129
1130 predicate->dest = predicate->mask = 0;
1131
1132 if (success)
1133 return;
1134
1135 midgard_instruction *mov = ralloc(ctx, midgard_instruction);
1136 *mov = v_mov(src, make_compiler_temp(ctx));
1137 mov->mask = 0x1;
1138
1139 branch->src[idx] = mov->dest;
1140
1141 if (stencil) {
1142 unsigned swizzle = (branch->src[0] == ~0) ? COMPONENT_Y : COMPONENT_X;
1143
1144 for (unsigned c = 0; c < 16; ++c)
1145 mov->swizzle[1][c] = swizzle;
1146 }
1147
1148 for (unsigned i = 0; i < 3; ++i) {
1149 if (!(*(units[i]))) {
1150 *(units[i]) = mov;
1151 mov->unit = unit_names[i];
1152 return;
1153 }
1154 }
1155
1156 UNREACHABLE("Could not schedule Z/S move to any unit");
1157 }
1158
1159 static midgard_bundle
1160 mir_schedule_alu(compiler_context *ctx, midgard_instruction **instructions,
1161 uint16_t *liveness, BITSET_WORD *worklist, unsigned len)
1162 {
1163 struct midgard_bundle bundle = {};
1164
1165 unsigned bytes_emitted = sizeof(bundle.control);
1166
1167 struct midgard_predicate predicate = {
1168 .tag = TAG_ALU_4,
1169 .destructive = true,
1170 .exclude = ~0,
1171 .constants = &bundle.constants,
1172 };
1173
1174 midgard_instruction *vmul = NULL;
1175 midgard_instruction *vadd = NULL;
1176 midgard_instruction *vlut = NULL;
1177 midgard_instruction *smul = NULL;
1178 midgard_instruction *sadd = NULL;
1179 midgard_instruction *branch = NULL;
1180
1181 mir_choose_alu(&branch, instructions, liveness, worklist, len, &predicate,
1182 ALU_ENAB_BR_COMPACT);
1183 mir_update_worklist(worklist, len, instructions, branch);
1184 unsigned writeout = branch ? branch->writeout : 0;
1185
1186 if (branch && branch->branch.conditional) {
1187 midgard_instruction *cond = mir_schedule_condition(
1188 ctx, &predicate, worklist, len, instructions, branch);
1189
1190 if (cond->unit == UNIT_VADD)
1191 vadd = cond;
1192 else if (cond->unit == UNIT_SMUL)
1193 smul = cond;
1194 else
1195 UNREACHABLE("Bad condition");
1196 }
1197
1198 /* If we have a render target reference, schedule a move for it. Since
1199 * this will be in sadd, we boost this to prevent scheduling csel into
1200 * smul */
1201
1202 if (writeout && (branch->constants.u32[0] || ctx->inputs->is_blend)) {
1203 sadd = ralloc(ctx, midgard_instruction);
1204 *sadd = v_mov(~0, make_compiler_temp(ctx));
1205 sadd->unit = UNIT_SADD;
1206 sadd->mask = 0x1;
1207 sadd->has_inline_constant = true;
1208 sadd->inline_constant = branch->constants.u32[0];
1209 branch->src[1] = sadd->dest;
1210 branch->src_types[1] = sadd->dest_type;
1211 }
1212
1213 if (writeout) {
1214 /* Propagate up */
1215 bundle.last_writeout = branch->last_writeout;
1216
1217 /* Mask off any conditionals.
1218 * This prevents csel and csel_v being scheduled into smul
1219 * since we might not have room for a conditional in vmul/sadd.
1220 * This is important because both writeout and csel have same-bundle
1221 * requirements on their dependencies. */
1222 predicate.no_cond = true;
1223 }
1224
1225 /* Set r1.w to the return address so we can return from blend shaders */
1226 if (writeout) {
1227 vadd = ralloc(ctx, midgard_instruction);
1228 *vadd = v_mov(~0, make_compiler_temp(ctx));
1229
1230 if (!ctx->inputs->is_blend) {
1231 vadd->op = midgard_alu_op_iadd;
1232 vadd->src[0] = SSA_FIXED_REGISTER(31);
1233 vadd->src_types[0] = nir_type_uint32;
1234
1235 for (unsigned c = 0; c < 16; ++c)
1236 vadd->swizzle[0][c] = COMPONENT_X;
1237
1238 vadd->has_inline_constant = true;
1239 vadd->inline_constant = 0;
1240 } else {
1241 vadd->src[1] = SSA_FIXED_REGISTER(1);
1242 vadd->src_types[0] = nir_type_uint32;
1243
1244 for (unsigned c = 0; c < 16; ++c)
1245 vadd->swizzle[1][c] = COMPONENT_W;
1246 }
1247
1248 vadd->unit = UNIT_VADD;
1249 vadd->mask = 0x1;
1250 branch->dest = vadd->dest;
1251 branch->dest_type = vadd->dest_type;
1252 }
1253
1254 if (writeout & MIR_WRITEOUT_Z)
1255 mir_schedule_zs_write(ctx, &predicate, instructions, liveness, worklist,
1256 len, branch, &smul, &vadd, &vlut, false);
1257
1258 if (writeout & MIR_WRITEOUT_S)
1259 mir_schedule_zs_write(ctx, &predicate, instructions, liveness, worklist,
1260 len, branch, &smul, &vadd, &vlut, true);
1261
1262 mir_choose_alu(&smul, instructions, liveness, worklist, len, &predicate,
1263 UNIT_SMUL);
1264
1265 for (unsigned mode = 1; mode < 3; ++mode) {
1266 predicate.move_mode = mode;
1267 predicate.no_mask = writeout ? (1 << 3) : 0;
1268 mir_choose_alu(&vlut, instructions, liveness, worklist, len, &predicate,
1269 UNIT_VLUT);
1270 predicate.no_mask = 0;
1271 mir_choose_alu(&vadd, instructions, liveness, worklist, len, &predicate,
1272 UNIT_VADD);
1273 }
1274
1275 /* Reset */
1276 predicate.move_mode = 0;
1277 predicate.exclude = ~0;
1278
1279 mir_update_worklist(worklist, len, instructions, vlut);
1280 mir_update_worklist(worklist, len, instructions, vadd);
1281 mir_update_worklist(worklist, len, instructions, smul);
1282
1283 bool vadd_csel = vadd && OP_IS_CSEL(vadd->op);
1284 bool smul_csel = smul && OP_IS_CSEL(smul->op);
1285
1286 if (vadd_csel || smul_csel) {
1287 midgard_instruction *ins = vadd_csel ? vadd : smul;
1288 midgard_instruction *cond = mir_schedule_condition(
1289 ctx, &predicate, worklist, len, instructions, ins);
1290
1291 if (cond->unit == UNIT_VMUL)
1292 vmul = cond;
1293 else if (cond->unit == UNIT_SADD)
1294 sadd = cond;
1295 else
1296 UNREACHABLE("Bad condition");
1297 }
1298
1299 /* Stage 2, let's schedule sadd before vmul for writeout */
1300 mir_choose_alu(&sadd, instructions, liveness, worklist, len, &predicate,
1301 UNIT_SADD);
1302
1303 /* Check if writeout reads its own register */
1304
1305 if (writeout) {
1306 midgard_instruction *stages[] = {sadd, vadd, smul, vlut};
1307 unsigned src =
1308 (branch->src[0] == ~0) ? SSA_FIXED_REGISTER(0) : branch->src[0];
1309 unsigned writeout_mask = 0x0;
1310 bool bad_writeout = false;
1311
1312 for (unsigned i = 0; i < ARRAY_SIZE(stages); ++i) {
1313 if (!stages[i])
1314 continue;
1315
1316 if (stages[i]->dest != src)
1317 continue;
1318
1319 writeout_mask |= stages[i]->mask;
1320 bad_writeout |= mir_has_arg(stages[i], branch->src[0]);
1321 }
1322
1323 /* It's possible we'll be able to schedule something into vmul
1324 * to fill r0. Let's peak into the future, trying to schedule
1325 * vmul specially that way. */
1326
1327 unsigned full_mask = 0xF;
1328
1329 if (!bad_writeout && writeout_mask != full_mask) {
1330 predicate.unit = UNIT_VMUL;
1331 predicate.dest = src;
1332 predicate.mask = writeout_mask ^ full_mask;
1333
1334 struct midgard_instruction *peaked = mir_choose_instruction(
1335 instructions, liveness, worklist, len, &predicate);
1336
1337 if (peaked) {
1338 vmul = peaked;
1339 vmul->unit = UNIT_VMUL;
1340 writeout_mask |= predicate.mask;
1341 assert(writeout_mask == full_mask);
1342 }
1343
1344 /* Cleanup */
1345 predicate.dest = predicate.mask = 0;
1346 }
1347
1348 /* Finally, add a move if necessary */
1349 if (bad_writeout || writeout_mask != full_mask) {
1350 unsigned temp = (branch->src[0] == ~0) ? SSA_FIXED_REGISTER(0)
1351 : make_compiler_temp(ctx);
1352
1353 vmul = ralloc(ctx, midgard_instruction);
1354 *vmul = v_mov(src, temp);
1355 vmul->unit = UNIT_VMUL;
1356 vmul->mask = full_mask ^ writeout_mask;
1357
1358 /* Rewrite to use our temp */
1359
1360 for (unsigned i = 0; i < ARRAY_SIZE(stages); ++i) {
1361 if (stages[i]) {
1362 mir_rewrite_index_dst_single(stages[i], src, temp);
1363 mir_rewrite_index_src_single(stages[i], src, temp);
1364 }
1365 }
1366
1367 mir_rewrite_index_src_single(branch, src, temp);
1368 }
1369 }
1370
1371 mir_choose_alu(&vmul, instructions, liveness, worklist, len, &predicate,
1372 UNIT_VMUL);
1373
1374 mir_update_worklist(worklist, len, instructions, vmul);
1375 mir_update_worklist(worklist, len, instructions, sadd);
1376
1377 bundle.has_embedded_constants = predicate.constant_mask != 0;
1378
1379 unsigned padding = 0;
1380
1381 /* Now that we have finished scheduling, build up the bundle */
1382 midgard_instruction *stages[] = {vmul, sadd, vadd, smul, vlut, branch};
1383
1384 for (unsigned i = 0; i < ARRAY_SIZE(stages); ++i) {
1385 if (stages[i]) {
1386 bundle.control |= stages[i]->unit;
1387 bytes_emitted += bytes_for_instruction(stages[i]);
1388 bundle.instructions[bundle.instruction_count++] = stages[i];
1389
1390 /* If we branch, we can't spill to TLS since the store
1391 * instruction will never get executed. We could try to
1392 * break the bundle but this is probably easier for
1393 * now. */
1394
1395 if (branch)
1396 stages[i]->no_spill |= (1 << REG_CLASS_WORK);
1397 }
1398 }
1399
1400 /* Pad ALU op to nearest word */
1401
1402 if (bytes_emitted & 15) {
1403 padding = 16 - (bytes_emitted & 15);
1404 bytes_emitted += padding;
1405 }
1406
1407 /* Constants must always be quadwords */
1408 if (bundle.has_embedded_constants)
1409 bytes_emitted += 16;
1410
1411 /* Size ALU instruction for tag */
1412 bundle.tag = (TAG_ALU_4) + (bytes_emitted / 16) - 1;
1413
1414 bool tilebuf_wait = branch && branch->compact_branch &&
1415 branch->branch.target_type == TARGET_TILEBUF_WAIT;
1416
1417 /* MRT capable GPUs use a special writeout procedure */
1418 if ((writeout || tilebuf_wait) && !(ctx->quirks & MIDGARD_NO_UPPER_ALU))
1419 bundle.tag += 4;
1420
1421 bundle.padding = padding;
1422 bundle.control |= bundle.tag;
1423
1424 return bundle;
1425 }
1426
1427 /* Schedule a single block by iterating its instruction to create bundles.
1428 * While we go, tally about the bundle sizes to compute the block size. */
1429
1430 static void
1431 schedule_block(compiler_context *ctx, midgard_block *block)
1432 {
1433 /* Copy list to dynamic array */
1434 unsigned len = 0;
1435 midgard_instruction **instructions = flatten_mir(block, &len);
1436
1437 if (!len)
1438 return;
1439
1440 /* Calculate dependencies and initial worklist */
1441 unsigned node_count = ctx->temp_count + 1;
1442 mir_create_dependency_graph(instructions, len, node_count);
1443
1444 /* Allocate the worklist */
1445 size_t sz = BITSET_BYTES(len);
1446 BITSET_WORD *worklist = calloc(sz, 1);
1447 uint16_t *liveness = calloc(node_count, 2);
1448 mir_initialize_worklist(worklist, instructions, len);
1449
1450 /* Count the number of load/store instructions so we know when it's
1451 * worth trying to schedule them in pairs. */
1452 unsigned num_ldst = 0;
1453 for (unsigned i = 0; i < len; ++i) {
1454 if (instructions[i]->type == TAG_LOAD_STORE_4)
1455 ++num_ldst;
1456 }
1457
1458 struct util_dynarray bundles = UTIL_DYNARRAY_INIT;
1459
1460 block->quadword_count = 0;
1461
1462 for (;;) {
1463 unsigned tag =
1464 mir_choose_bundle(instructions, liveness, worklist, len, num_ldst);
1465 midgard_bundle bundle;
1466
1467 if (tag == TAG_TEXTURE_4)
1468 bundle = mir_schedule_texture(instructions, liveness, worklist, len,
1469 ctx->stage != MESA_SHADER_FRAGMENT);
1470 else if (tag == TAG_LOAD_STORE_4)
1471 bundle =
1472 mir_schedule_ldst(instructions, liveness, worklist, len, &num_ldst);
1473 else if (tag == TAG_ALU_4)
1474 bundle = mir_schedule_alu(ctx, instructions, liveness, worklist, len);
1475 else
1476 break;
1477
1478 for (unsigned i = 0; i < bundle.instruction_count; ++i)
1479 bundle.instructions[i]->bundle_id =
1480 ctx->quadword_count + block->quadword_count;
1481
1482 util_dynarray_append(&bundles, bundle);
1483 block->quadword_count += midgard_tag_props[bundle.tag].size;
1484 }
1485
1486 assert(num_ldst == 0);
1487
1488 /* We emitted bundles backwards; copy into the block in reverse-order */
1489
1490 util_dynarray_init(&block->bundles, block);
1491 util_dynarray_foreach_reverse(&bundles, midgard_bundle, bundle) {
1492 util_dynarray_append(&block->bundles, *bundle);
1493 }
1494 util_dynarray_fini(&bundles);
1495
1496 block->scheduled = true;
1497 ctx->quadword_count += block->quadword_count;
1498
1499 /* Reorder instructions to match bundled. First remove existing
1500 * instructions and then recreate the list */
1501
1502 mir_foreach_instr_in_block_safe(block, ins) {
1503 list_del(&ins->link);
1504 }
1505
1506 mir_foreach_instr_in_block_scheduled_rev(block, ins) {
1507 list_add(&ins->link, &block->instructions);
1508 }
1509
1510 free(instructions); /* Allocated by flatten_mir() */
1511 free(worklist);
1512 free(liveness);
1513 }
1514
1515 /* Insert moves to ensure we can register allocate load/store registers */
1516 static void
1517 mir_lower_ldst(compiler_context *ctx)
1518 {
1519 mir_foreach_instr_global_safe(ctx, I) {
1520 if (I->type != TAG_LOAD_STORE_4)
1521 continue;
1522
1523 mir_foreach_src(I, s) {
1524 if (s == 0)
1525 continue;
1526 if (I->src[s] == ~0)
1527 continue;
1528 if (I->swizzle[s][0] == 0)
1529 continue;
1530
1531 unsigned temp = make_compiler_temp(ctx);
1532 midgard_instruction mov = v_mov(I->src[s], temp);
1533 mov.mask = 0x1;
1534 mov.dest_type = I->src_types[s];
1535 for (unsigned c = 0; c < NIR_MAX_VEC_COMPONENTS; ++c)
1536 mov.swizzle[1][c] = I->swizzle[s][0];
1537
1538 mir_insert_instruction_before(ctx, I, &mov);
1539 I->src[s] = mov.dest;
1540 I->swizzle[s][0] = 0;
1541 }
1542 }
1543 }
1544
1545 /* Insert moves to ensure we can register allocate blend writeout */
1546 static void
1547 mir_lower_blend_input(compiler_context *ctx)
1548 {
1549 mir_foreach_block(ctx, _blk) {
1550 midgard_block *blk = (midgard_block *)_blk;
1551
1552 if (list_is_empty(&blk->instructions))
1553 continue;
1554
1555 midgard_instruction *I = mir_last_in_block(blk);
1556
1557 if (!I || I->type != TAG_ALU_4 || !I->writeout)
1558 continue;
1559
1560 mir_foreach_src(I, s) {
1561 unsigned src = I->src[s];
1562
1563 if (src >= ctx->temp_count)
1564 continue;
1565
1566 if (!_blk->live_out[src])
1567 continue;
1568
1569 unsigned temp = make_compiler_temp(ctx);
1570 midgard_instruction mov = v_mov(src, temp);
1571 mov.mask = 0xF;
1572 mov.dest_type = nir_type_uint32;
1573 mir_insert_instruction_before(ctx, I, &mov);
1574 I->src[s] = mov.dest;
1575 }
1576 }
1577 }
1578
1579 void
1580 midgard_schedule_program(compiler_context *ctx)
1581 {
1582 mir_lower_ldst(ctx);
1583 midgard_promote_uniforms(ctx);
1584
1585 /* Must be lowered right before scheduling */
1586 mir_lower_special_reads(ctx);
1587 mir_squeeze_index(ctx);
1588
1589 if (ctx->stage == MESA_SHADER_FRAGMENT) {
1590 mir_invalidate_liveness(ctx);
1591 mir_compute_liveness(ctx);
1592 mir_lower_blend_input(ctx);
1593 }
1594
1595 mir_squeeze_index(ctx);
1596
1597 /* Lowering can introduce some dead moves */
1598
1599 mir_foreach_block(ctx, _block) {
1600 midgard_block *block = (midgard_block *)_block;
1601 midgard_opt_dead_move_eliminate(ctx, block);
1602 schedule_block(ctx, block);
1603 }
1604 }