pan/midgard: Don't double check SCALAR units
[mesa.git] / src / panfrost / midgard / midgard_schedule.c
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
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 "compiler.h"
25 #include "midgard_ops.h"
26 #include "util/u_memory.h"
27 #include "util/register_allocate.h"
28
29 /* Scheduling for Midgard is complicated, to say the least. ALU instructions
30 * must be grouped into VLIW bundles according to following model:
31 *
32 * [VMUL] [SADD]
33 * [VADD] [SMUL] [VLUT]
34 *
35 * A given instruction can execute on some subset of the units (or a few can
36 * execute on all). Instructions can be either vector or scalar; only scalar
37 * instructions can execute on SADD/SMUL units. Units on a given line execute
38 * in parallel. Subsequent lines execute separately and can pass results
39 * directly via pipeline registers r24/r25, bypassing the register file.
40 *
41 * A bundle can optionally have 128-bits of embedded constants, shared across
42 * all of the instructions within a bundle.
43 *
44 * Instructions consuming conditionals (branches and conditional selects)
45 * require their condition to be written into the conditional register (r31)
46 * within the same bundle they are consumed.
47 *
48 * Fragment writeout requires its argument to be written in full within the
49 * same bundle as the branch, with no hanging dependencies.
50 *
51 * Load/store instructions are also in bundles of simply two instructions, and
52 * texture instructions have no bundling.
53 *
54 * -------------------------------------------------------------------------
55 *
56 */
57
58 /* We create the dependency graph with per-component granularity */
59
60 #define COMPONENT_COUNT 8
61
62 static void
63 add_dependency(struct util_dynarray *table, unsigned index, unsigned mask, midgard_instruction **instructions, unsigned child)
64 {
65 for (unsigned i = 0; i < COMPONENT_COUNT; ++i) {
66 if (!(mask & (1 << i)))
67 continue;
68
69 struct util_dynarray *parents = &table[(COMPONENT_COUNT * index) + i];
70
71 util_dynarray_foreach(parents, unsigned, parent) {
72 BITSET_WORD *dependents = instructions[*parent]->dependents;
73
74 /* Already have the dependency */
75 if (BITSET_TEST(dependents, child))
76 continue;
77
78 BITSET_SET(dependents, child);
79 instructions[child]->nr_dependencies++;
80 }
81 }
82 }
83
84 static void
85 mark_access(struct util_dynarray *table, unsigned index, unsigned mask, unsigned parent)
86 {
87 for (unsigned i = 0; i < COMPONENT_COUNT; ++i) {
88 if (!(mask & (1 << i)))
89 continue;
90
91 util_dynarray_append(&table[(COMPONENT_COUNT * index) + i], unsigned, parent);
92 }
93 }
94
95 static void
96 mir_create_dependency_graph(midgard_instruction **instructions, unsigned count, unsigned node_count)
97 {
98 size_t sz = node_count * COMPONENT_COUNT;
99
100 struct util_dynarray *last_read = calloc(sizeof(struct util_dynarray), sz);
101 struct util_dynarray *last_write = calloc(sizeof(struct util_dynarray), sz);
102
103 for (unsigned i = 0; i < sz; ++i) {
104 util_dynarray_init(&last_read[i], NULL);
105 util_dynarray_init(&last_write[i], NULL);
106 }
107
108 /* Initialize dependency graph */
109 for (unsigned i = 0; i < count; ++i) {
110 instructions[i]->dependents =
111 calloc(BITSET_WORDS(count), sizeof(BITSET_WORD));
112
113 instructions[i]->nr_dependencies = 0;
114 }
115
116 /* Populate dependency graph */
117 for (signed i = count - 1; i >= 0; --i) {
118 if (instructions[i]->compact_branch)
119 continue;
120
121 unsigned dest = instructions[i]->dest;
122 unsigned mask = instructions[i]->mask;
123
124 mir_foreach_src((*instructions), s) {
125 unsigned src = instructions[i]->src[s];
126
127 if (src < node_count) {
128 unsigned readmask = mir_mask_of_read_components(instructions[i], src);
129 add_dependency(last_write, src, readmask, instructions, i);
130 }
131 }
132
133 if (dest < node_count) {
134 add_dependency(last_read, dest, mask, instructions, i);
135 add_dependency(last_write, dest, mask, instructions, i);
136 mark_access(last_write, dest, mask, i);
137 }
138
139 mir_foreach_src((*instructions), s) {
140 unsigned src = instructions[i]->src[s];
141
142 if (src < node_count) {
143 unsigned readmask = mir_mask_of_read_components(instructions[i], src);
144 mark_access(last_read, src, readmask, i);
145 }
146 }
147 }
148
149 /* If there is a branch, all instructions depend on it, as interblock
150 * execution must be purely in-order */
151
152 if (instructions[count - 1]->compact_branch) {
153 BITSET_WORD *dependents = instructions[count - 1]->dependents;
154
155 for (signed i = count - 2; i >= 0; --i) {
156 if (BITSET_TEST(dependents, i))
157 continue;
158
159 BITSET_SET(dependents, i);
160 instructions[i]->nr_dependencies++;
161 }
162 }
163
164 /* Free the intermediate structures */
165 for (unsigned i = 0; i < sz; ++i) {
166 util_dynarray_fini(&last_read[i]);
167 util_dynarray_fini(&last_write[i]);
168 }
169 }
170
171 /* Does the mask cover more than a scalar? */
172
173 static bool
174 is_single_component_mask(unsigned mask)
175 {
176 int components = 0;
177
178 for (int c = 0; c < 8; ++c) {
179 if (mask & (1 << c))
180 components++;
181 }
182
183 return components == 1;
184 }
185
186 /* Helpers for scheudling */
187
188 static bool
189 mir_is_scalar(midgard_instruction *ains)
190 {
191 /* Do we try to use it as a vector op? */
192 if (!is_single_component_mask(ains->mask))
193 return false;
194
195 /* Otherwise, check mode hazards */
196 bool could_scalar = true;
197
198 /* Only 16/32-bit can run on a scalar unit */
199 could_scalar &= ains->alu.reg_mode != midgard_reg_mode_8;
200 could_scalar &= ains->alu.reg_mode != midgard_reg_mode_64;
201 could_scalar &= ains->alu.dest_override == midgard_dest_override_none;
202
203 if (ains->alu.reg_mode == midgard_reg_mode_16) {
204 /* If we're running in 16-bit mode, we
205 * can't have any 8-bit sources on the
206 * scalar unit (since the scalar unit
207 * doesn't understand 8-bit) */
208
209 midgard_vector_alu_src s1 =
210 vector_alu_from_unsigned(ains->alu.src1);
211
212 could_scalar &= !s1.half;
213
214 midgard_vector_alu_src s2 =
215 vector_alu_from_unsigned(ains->alu.src2);
216
217 could_scalar &= !s2.half;
218 }
219
220 return could_scalar;
221 }
222
223 /* How many bytes does this ALU instruction add to the bundle? */
224
225 static unsigned
226 bytes_for_instruction(midgard_instruction *ains)
227 {
228 if (ains->unit & UNITS_ANY_VECTOR)
229 return sizeof(midgard_reg_info) + sizeof(midgard_vector_alu);
230 else if (ains->unit == ALU_ENAB_BRANCH)
231 return sizeof(midgard_branch_extended);
232 else if (ains->compact_branch)
233 return sizeof(ains->br_compact);
234 else
235 return sizeof(midgard_reg_info) + sizeof(midgard_scalar_alu);
236 }
237
238 /* We would like to flatten the linked list of midgard_instructions in a bundle
239 * to an array of pointers on the heap for easy indexing */
240
241 static midgard_instruction **
242 flatten_mir(midgard_block *block, unsigned *len)
243 {
244 *len = list_length(&block->instructions);
245
246 if (!(*len))
247 return NULL;
248
249 midgard_instruction **instructions =
250 calloc(sizeof(midgard_instruction *), *len);
251
252 unsigned i = 0;
253
254 mir_foreach_instr_in_block(block, ins)
255 instructions[i++] = ins;
256
257 return instructions;
258 }
259
260 /* The worklist is the set of instructions that can be scheduled now; that is,
261 * the set of instructions with no remaining dependencies */
262
263 static void
264 mir_initialize_worklist(BITSET_WORD *worklist, midgard_instruction **instructions, unsigned count)
265 {
266 for (unsigned i = 0; i < count; ++i) {
267 if (instructions[i]->nr_dependencies == 0)
268 BITSET_SET(worklist, i);
269 }
270 }
271
272 /* Update the worklist after an instruction terminates. Remove its edges from
273 * the graph and if that causes any node to have no dependencies, add it to the
274 * worklist */
275
276 static void
277 mir_update_worklist(
278 BITSET_WORD *worklist, unsigned count,
279 midgard_instruction **instructions, midgard_instruction *done)
280 {
281 /* Sanity check: if no instruction terminated, there is nothing to do.
282 * If the instruction that terminated had dependencies, that makes no
283 * sense and means we messed up the worklist. Finally, as the purpose
284 * of this routine is to update dependents, we abort early if there are
285 * no dependents defined. */
286
287 if (!done)
288 return;
289
290 assert(done->nr_dependencies == 0);
291
292 if (!done->dependents)
293 return;
294
295 /* We have an instruction with dependents. Iterate each dependent to
296 * remove one dependency (`done`), adding dependents to the worklist
297 * where possible. */
298
299 unsigned i;
300 BITSET_WORD tmp;
301 BITSET_FOREACH_SET(i, tmp, done->dependents, count) {
302 assert(instructions[i]->nr_dependencies);
303
304 if (!(--instructions[i]->nr_dependencies))
305 BITSET_SET(worklist, i);
306 }
307
308 free(done->dependents);
309 }
310
311 /* While scheduling, we need to choose instructions satisfying certain
312 * criteria. As we schedule backwards, we choose the *last* instruction in the
313 * worklist to simulate in-order scheduling. Chosen instructions must satisfy a
314 * given predicate. */
315
316 struct midgard_predicate {
317 /* TAG or ~0 for dont-care */
318 unsigned tag;
319
320 /* True if we want to pop off the chosen instruction */
321 bool destructive;
322
323 /* For ALU, choose only this unit */
324 unsigned unit;
325
326 /* State for bundle constants. constants is the actual constants
327 * for the bundle. constant_count is the number of bytes (up to
328 * 16) currently in use for constants. When picking in destructive
329 * mode, the constants array will be updated, and the instruction
330 * will be adjusted to index into the constants array */
331
332 uint8_t *constants;
333 unsigned constant_count;
334 bool blend_constant;
335
336 /* Exclude this destination (if not ~0) */
337 unsigned exclude;
338 };
339
340 /* For an instruction that can fit, adjust it to fit and update the constants
341 * array, in destructive mode. Returns whether the fitting was successful. */
342
343 static bool
344 mir_adjust_constants(midgard_instruction *ins,
345 struct midgard_predicate *pred,
346 bool destructive)
347 {
348 /* Blend constants dominate */
349 if (ins->has_blend_constant) {
350 if (pred->constant_count)
351 return false;
352 else if (destructive) {
353 pred->blend_constant = true;
354 pred->constant_count = 16;
355 return true;
356 }
357 }
358
359 /* No constant, nothing to adjust */
360 if (!ins->has_constants)
361 return true;
362
363 /* TODO: Deduplicate; permit multiple constants within a bundle */
364
365 if (destructive && !pred->constant_count) {
366 if (ins->alu.reg_mode == midgard_reg_mode_16) {
367 /* TODO: Fix packing XXX */
368 uint16_t *bundles = (uint16_t *) pred->constants;
369 uint32_t *constants = (uint32_t *) ins->constants;
370
371 /* Copy them wholesale */
372 for (unsigned i = 0; i < 4; ++i)
373 bundles[i] = constants[i];
374 } else {
375 memcpy(pred->constants, ins->constants, 16);
376 }
377
378 pred->constant_count = 16;
379 return true;
380 }
381
382 return !pred->constant_count;
383 }
384
385 static midgard_instruction *
386 mir_choose_instruction(
387 midgard_instruction **instructions,
388 BITSET_WORD *worklist, unsigned count,
389 struct midgard_predicate *predicate)
390 {
391 /* Parse the predicate */
392 unsigned tag = predicate->tag;
393 bool alu = tag == TAG_ALU_4;
394 unsigned unit = predicate->unit;
395 bool branch = alu && (unit == ALU_ENAB_BR_COMPACT);
396
397 /* Iterate to find the best instruction satisfying the predicate */
398 unsigned i;
399 BITSET_WORD tmp;
400
401 signed best_index = -1;
402
403 /* Enforce a simple metric limiting distance to keep down register
404 * pressure. TOOD: replace with liveness tracking for much better
405 * results */
406
407 unsigned max_active = 0;
408 unsigned max_distance = 6;
409
410 BITSET_FOREACH_SET(i, tmp, worklist, count) {
411 max_active = MAX2(max_active, i);
412 }
413
414 BITSET_FOREACH_SET(i, tmp, worklist, count) {
415 if ((max_active - i) >= max_distance)
416 continue;
417
418 if (tag != ~0 && instructions[i]->type != tag)
419 continue;
420
421 if (predicate->exclude != ~0 && instructions[i]->dest == predicate->exclude)
422 continue;
423
424 if (alu && !branch && !(alu_opcode_props[instructions[i]->alu.op].props & unit))
425 continue;
426
427 if (branch && !instructions[i]->compact_branch)
428 continue;
429
430 if (alu && !mir_adjust_constants(instructions[i], predicate, false))
431 continue;
432
433 /* Simulate in-order scheduling */
434 if ((signed) i < best_index)
435 continue;
436
437 best_index = i;
438 }
439
440
441 /* Did we find anything? */
442
443 if (best_index < 0)
444 return NULL;
445
446 /* If we found something, remove it from the worklist */
447 assert(best_index < count);
448
449 if (predicate->destructive) {
450 BITSET_CLEAR(worklist, best_index);
451
452 if (alu)
453 mir_adjust_constants(instructions[best_index], predicate, true);
454 }
455
456 return instructions[best_index];
457 }
458
459 /* Still, we don't choose instructions in a vacuum. We need a way to choose the
460 * best bundle type (ALU, load/store, texture). Nondestructive. */
461
462 static unsigned
463 mir_choose_bundle(
464 midgard_instruction **instructions,
465 BITSET_WORD *worklist, unsigned count)
466 {
467 /* At the moment, our algorithm is very simple - use the bundle of the
468 * best instruction, regardless of what else could be scheduled
469 * alongside it. This is not optimal but it works okay for in-order */
470
471 struct midgard_predicate predicate = {
472 .tag = ~0,
473 .destructive = false,
474 .exclude = ~0
475 };
476
477 midgard_instruction *chosen = mir_choose_instruction(instructions, worklist, count, &predicate);
478
479 if (chosen)
480 return chosen->type;
481 else
482 return ~0;
483 }
484
485 /* We want to choose an ALU instruction filling a given unit */
486 static void
487 mir_choose_alu(midgard_instruction **slot,
488 midgard_instruction **instructions,
489 BITSET_WORD *worklist, unsigned len,
490 struct midgard_predicate *predicate,
491 unsigned unit)
492 {
493 /* Did we already schedule to this slot? */
494 if ((*slot) != NULL)
495 return;
496
497 /* Try to schedule something, if not */
498 predicate->unit = unit;
499 *slot = mir_choose_instruction(instructions, worklist, len, predicate);
500
501 /* Store unit upon scheduling */
502 if (*slot && !((*slot)->compact_branch))
503 (*slot)->unit = unit;
504 }
505
506 /* When we are scheduling a branch/csel, we need the consumed condition in the
507 * same block as a pipeline register. There are two options to enable this:
508 *
509 * - Move the conditional into the bundle. Preferred, but only works if the
510 * conditional is used only once and is from this block.
511 * - Copy the conditional.
512 *
513 * We search for the conditional. If it's in this block, single-use, and
514 * without embedded constants, we schedule it immediately. Otherwise, we
515 * schedule a move for it.
516 *
517 * mir_comparison_mobile is a helper to find the moveable condition.
518 */
519
520 static unsigned
521 mir_comparison_mobile(
522 compiler_context *ctx,
523 midgard_instruction **instructions,
524 unsigned count,
525 unsigned cond)
526 {
527 if (!mir_single_use(ctx, cond))
528 return ~0;
529
530 unsigned ret = ~0;
531
532 for (unsigned i = 0; i < count; ++i) {
533 if (instructions[i]->dest != cond)
534 continue;
535
536 /* Must fit in an ALU bundle */
537 if (instructions[i]->type != TAG_ALU_4)
538 return ~0;
539
540 /* We'll need to rewrite to .w but that doesn't work for vector
541 * ops that don't replicate (ball/bany), so bail there */
542
543 if (GET_CHANNEL_COUNT(alu_opcode_props[instructions[i]->alu.op].props))
544 return ~0;
545
546 /* TODO: moving conditionals with constants */
547
548 if (instructions[i]->has_constants)
549 return ~0;
550
551 /* Ensure it is written only once */
552
553 if (ret != ~0)
554 return ~0;
555 else
556 ret = i;
557 }
558
559 return ret;
560 }
561
562 /* Using the information about the moveable conditional itself, we either pop
563 * that condition off the worklist for use now, or create a move to
564 * artificially schedule instead as a fallback */
565
566 static midgard_instruction *
567 mir_schedule_comparison(
568 compiler_context *ctx,
569 midgard_instruction **instructions,
570 BITSET_WORD *worklist, unsigned count,
571 unsigned cond, bool vector, unsigned swizzle,
572 midgard_instruction *user)
573 {
574 /* TODO: swizzle when scheduling */
575 unsigned comp_i =
576 (!vector && (swizzle == 0)) ?
577 mir_comparison_mobile(ctx, instructions, count, cond) : ~0;
578
579 /* If we can, schedule the condition immediately */
580 if ((comp_i != ~0) && BITSET_TEST(worklist, comp_i)) {
581 assert(comp_i < count);
582 BITSET_CLEAR(worklist, comp_i);
583 return instructions[comp_i];
584 }
585
586 /* Otherwise, we insert a move */
587 midgard_vector_alu_src csel = {
588 .swizzle = swizzle
589 };
590
591 midgard_instruction mov = v_mov(cond, csel, cond);
592 mov.mask = vector ? 0xF : 0x1;
593
594 return mir_insert_instruction_before(ctx, user, mov);
595 }
596
597 /* Most generally, we need instructions writing to r31 in the appropriate
598 * components */
599
600 static midgard_instruction *
601 mir_schedule_condition(compiler_context *ctx,
602 struct midgard_predicate *predicate,
603 BITSET_WORD *worklist, unsigned count,
604 midgard_instruction **instructions,
605 midgard_instruction *last)
606 {
607 /* For a branch, the condition is the only argument; for csel, third */
608 bool branch = last->compact_branch;
609 unsigned condition_index = branch ? 0 : 2;
610
611 /* csel_v is vector; otherwise, conditions are scalar */
612 bool vector = !branch && OP_IS_CSEL_V(last->alu.op);
613
614 /* Grab the conditional instruction */
615
616 midgard_instruction *cond = mir_schedule_comparison(
617 ctx, instructions, worklist, count, last->src[condition_index],
618 vector, last->cond_swizzle, last);
619
620 /* We have exclusive reign over this (possibly move) conditional
621 * instruction. We can rewrite into a pipeline conditional register */
622
623 predicate->exclude = cond->dest;
624 cond->dest = SSA_FIXED_REGISTER(31);
625
626 if (!vector) {
627 cond->mask = (1 << COMPONENT_W);
628
629 mir_foreach_src(cond, s) {
630 if (cond->src[s] == ~0)
631 continue;
632
633 mir_set_swizzle(cond, s, (mir_get_swizzle(cond, s) << (2*3)) & 0xFF);
634 }
635 }
636
637 /* Schedule the unit: csel is always in the latter pipeline, so a csel
638 * condition must be in the former pipeline stage (vmul/sadd),
639 * depending on scalar/vector of the instruction itself. A branch must
640 * be written from the latter pipeline stage and a branch condition is
641 * always scalar, so it is always in smul (exception: ball/bany, which
642 * will be vadd) */
643
644 if (branch)
645 cond->unit = UNIT_SMUL;
646 else
647 cond->unit = vector ? UNIT_VMUL : UNIT_SADD;
648
649 return cond;
650 }
651
652 /* Schedules a single bundle of the given type */
653
654 static midgard_bundle
655 mir_schedule_texture(
656 midgard_instruction **instructions,
657 BITSET_WORD *worklist, unsigned len)
658 {
659 struct midgard_predicate predicate = {
660 .tag = TAG_TEXTURE_4,
661 .destructive = true,
662 .exclude = ~0
663 };
664
665 midgard_instruction *ins =
666 mir_choose_instruction(instructions, worklist, len, &predicate);
667
668 mir_update_worklist(worklist, len, instructions, ins);
669
670 struct midgard_bundle out = {
671 .tag = TAG_TEXTURE_4,
672 .instruction_count = 1,
673 .instructions = { ins }
674 };
675
676 return out;
677 }
678
679 static midgard_bundle
680 mir_schedule_ldst(
681 midgard_instruction **instructions,
682 BITSET_WORD *worklist, unsigned len)
683 {
684 struct midgard_predicate predicate = {
685 .tag = TAG_LOAD_STORE_4,
686 .destructive = true,
687 .exclude = ~0
688 };
689
690 /* Try to pick two load/store ops. Second not gauranteed to exist */
691
692 midgard_instruction *ins =
693 mir_choose_instruction(instructions, worklist, len, &predicate);
694
695 midgard_instruction *pair =
696 mir_choose_instruction(instructions, worklist, len, &predicate);
697
698 struct midgard_bundle out = {
699 .tag = TAG_LOAD_STORE_4,
700 .instruction_count = pair ? 2 : 1,
701 .instructions = { ins, pair }
702 };
703
704 /* We have to update the worklist atomically, since the two
705 * instructions run concurrently (TODO: verify it's not pipelined) */
706
707 mir_update_worklist(worklist, len, instructions, ins);
708 mir_update_worklist(worklist, len, instructions, pair);
709
710 return out;
711 }
712
713 static midgard_bundle
714 mir_schedule_alu(
715 compiler_context *ctx,
716 midgard_instruction **instructions,
717 BITSET_WORD *worklist, unsigned len)
718 {
719 struct midgard_bundle bundle = {};
720
721 unsigned bytes_emitted = sizeof(bundle.control);
722
723 struct midgard_predicate predicate = {
724 .tag = TAG_ALU_4,
725 .destructive = true,
726 .exclude = ~0,
727 .constants = (uint8_t *) bundle.constants
728 };
729
730 midgard_instruction *vmul = NULL;
731 midgard_instruction *vadd = NULL;
732 midgard_instruction *vlut = NULL;
733 midgard_instruction *smul = NULL;
734 midgard_instruction *sadd = NULL;
735 midgard_instruction *branch = NULL;
736
737 mir_choose_alu(&branch, instructions, worklist, len, &predicate, ALU_ENAB_BR_COMPACT);
738 mir_update_worklist(worklist, len, instructions, branch);
739 bool writeout = branch && branch->writeout;
740
741 if (branch && !branch->prepacked_branch && branch->branch.conditional) {
742 midgard_instruction *cond = mir_schedule_condition(ctx, &predicate, worklist, len, instructions, branch);
743
744 if (cond->unit == UNIT_VADD)
745 vadd = cond;
746 else if (cond->unit == UNIT_SMUL)
747 smul = cond;
748 else
749 unreachable("Bad condition");
750 }
751
752 if (!writeout)
753 mir_choose_alu(&vlut, instructions, worklist, len, &predicate, UNIT_VLUT);
754
755 mir_choose_alu(&vadd, instructions, worklist, len, &predicate, UNIT_VADD);
756
757 mir_update_worklist(worklist, len, instructions, vlut);
758 mir_update_worklist(worklist, len, instructions, vadd);
759 mir_update_worklist(worklist, len, instructions, smul);
760
761 bool vadd_csel = vadd && OP_IS_CSEL(vadd->alu.op);
762 bool smul_csel = smul && OP_IS_CSEL(smul->alu.op);
763
764 if (vadd_csel || smul_csel) {
765 midgard_instruction *ins = vadd_csel ? vadd : smul;
766 midgard_instruction *cond = mir_schedule_condition(ctx, &predicate, worklist, len, instructions, ins);
767
768 if (cond->unit == UNIT_VMUL)
769 vmul = cond;
770 else if (cond->unit == UNIT_SADD)
771 sadd = cond;
772 else
773 unreachable("Bad condition");
774 }
775
776 /* Check if writeout reads its own register */
777 bool bad_writeout = false;
778
779 if (branch && branch->writeout) {
780 midgard_instruction *stages[] = { sadd, vadd, smul };
781 unsigned src = (branch->src[0] == ~0) ? SSA_FIXED_REGISTER(0) : branch->src[0];
782 unsigned writeout_mask = 0x0;
783
784 for (unsigned i = 0; i < ARRAY_SIZE(stages); ++i) {
785 if (!stages[i])
786 continue;
787
788 if (stages[i]->dest != src)
789 continue;
790
791 writeout_mask |= stages[i]->mask;
792 bad_writeout |= mir_has_arg(stages[i], branch->src[0]);
793 }
794
795 /* Add a move if necessary */
796 if (bad_writeout || writeout_mask != 0xF) {
797 unsigned temp = (branch->src[0] == ~0) ? SSA_FIXED_REGISTER(0) : make_compiler_temp(ctx);
798 midgard_instruction mov = v_mov(src, blank_alu_src, temp);
799 vmul = mem_dup(&mov, sizeof(midgard_instruction));
800 vmul->unit = UNIT_VMUL;
801 vmul->mask = 0xF ^ writeout_mask;
802 /* TODO: Don't leak */
803
804 /* Rewrite to use our temp */
805
806 for (unsigned i = 0; i < ARRAY_SIZE(stages); ++i) {
807 if (stages[i])
808 mir_rewrite_index_dst_single(stages[i], src, temp);
809 }
810
811 mir_rewrite_index_src_single(branch, src, temp);
812 }
813 }
814
815 mir_choose_alu(&vmul, instructions, worklist, len, &predicate, UNIT_VMUL);
816
817 mir_update_worklist(worklist, len, instructions, vmul);
818 mir_update_worklist(worklist, len, instructions, sadd);
819
820 bundle.has_blend_constant = predicate.blend_constant;
821 bundle.has_embedded_constants = predicate.constant_count > 0;
822
823 unsigned padding = 0;
824
825 /* Now that we have finished scheduling, build up the bundle */
826 midgard_instruction *stages[] = { vmul, sadd, vadd, smul, vlut, branch };
827
828 for (unsigned i = 0; i < ARRAY_SIZE(stages); ++i) {
829 if (stages[i]) {
830 bundle.control |= stages[i]->unit;
831 bytes_emitted += bytes_for_instruction(stages[i]);
832 bundle.instructions[bundle.instruction_count++] = stages[i];
833 }
834 }
835
836 /* Pad ALU op to nearest word */
837
838 if (bytes_emitted & 15) {
839 padding = 16 - (bytes_emitted & 15);
840 bytes_emitted += padding;
841 }
842
843 /* Constants must always be quadwords */
844 if (bundle.has_embedded_constants)
845 bytes_emitted += 16;
846
847 /* Size ALU instruction for tag */
848 bundle.tag = (TAG_ALU_4) + (bytes_emitted / 16) - 1;
849 bundle.padding = padding;
850 bundle.control |= bundle.tag;
851
852 return bundle;
853 }
854
855 /* Schedule a single block by iterating its instruction to create bundles.
856 * While we go, tally about the bundle sizes to compute the block size. */
857
858
859 static void
860 schedule_block(compiler_context *ctx, midgard_block *block)
861 {
862 /* Copy list to dynamic array */
863 unsigned len = 0;
864 midgard_instruction **instructions = flatten_mir(block, &len);
865
866 if (!len)
867 return;
868
869 /* Calculate dependencies and initial worklist */
870 unsigned node_count = ctx->temp_count + 1;
871 mir_create_dependency_graph(instructions, len, node_count);
872
873 /* Allocate the worklist */
874 size_t sz = BITSET_WORDS(len) * sizeof(BITSET_WORD);
875 BITSET_WORD *worklist = calloc(sz, 1);
876 mir_initialize_worklist(worklist, instructions, len);
877
878 struct util_dynarray bundles;
879 util_dynarray_init(&bundles, NULL);
880
881 block->quadword_count = 0;
882 unsigned blend_offset = 0;
883
884 for (;;) {
885 unsigned tag = mir_choose_bundle(instructions, worklist, len);
886 midgard_bundle bundle;
887
888 if (tag == TAG_TEXTURE_4)
889 bundle = mir_schedule_texture(instructions, worklist, len);
890 else if (tag == TAG_LOAD_STORE_4)
891 bundle = mir_schedule_ldst(instructions, worklist, len);
892 else if (tag == TAG_ALU_4)
893 bundle = mir_schedule_alu(ctx, instructions, worklist, len);
894 else
895 break;
896
897 util_dynarray_append(&bundles, midgard_bundle, bundle);
898
899 if (bundle.has_blend_constant)
900 blend_offset = block->quadword_count;
901
902 block->quadword_count += quadword_size(bundle.tag);
903 }
904
905 /* We emitted bundles backwards; copy into the block in reverse-order */
906
907 util_dynarray_init(&block->bundles, NULL);
908 util_dynarray_foreach_reverse(&bundles, midgard_bundle, bundle) {
909 util_dynarray_append(&block->bundles, midgard_bundle, *bundle);
910 }
911
912 /* Blend constant was backwards as well. blend_offset if set is
913 * strictly positive, as an offset of zero would imply constants before
914 * any instructions which is invalid in Midgard */
915
916 if (blend_offset)
917 ctx->blend_constant_offset = ((ctx->quadword_count + block->quadword_count) - blend_offset - 1) * 0x10;
918
919 block->is_scheduled = true;
920 ctx->quadword_count += block->quadword_count;
921
922 /* Reorder instructions to match bundled. First remove existing
923 * instructions and then recreate the list */
924
925 mir_foreach_instr_in_block_safe(block, ins) {
926 list_del(&ins->link);
927 }
928
929 mir_foreach_instr_in_block_scheduled_rev(block, ins) {
930 list_add(&ins->link, &block->instructions);
931 }
932 }
933
934 /* When we're 'squeezing down' the values in the IR, we maintain a hash
935 * as such */
936
937 static unsigned
938 find_or_allocate_temp(compiler_context *ctx, unsigned hash)
939 {
940 if (hash >= SSA_FIXED_MINIMUM)
941 return hash;
942
943 unsigned temp = (uintptr_t) _mesa_hash_table_u64_search(
944 ctx->hash_to_temp, hash + 1);
945
946 if (temp)
947 return temp - 1;
948
949 /* If no temp is find, allocate one */
950 temp = ctx->temp_count++;
951 ctx->max_hash = MAX2(ctx->max_hash, hash);
952
953 _mesa_hash_table_u64_insert(ctx->hash_to_temp,
954 hash + 1, (void *) ((uintptr_t) temp + 1));
955
956 return temp;
957 }
958
959 /* Reassigns numbering to get rid of gaps in the indices */
960
961 static void
962 mir_squeeze_index(compiler_context *ctx)
963 {
964 /* Reset */
965 ctx->temp_count = 0;
966 /* TODO don't leak old hash_to_temp */
967 ctx->hash_to_temp = _mesa_hash_table_u64_create(NULL);
968
969 mir_foreach_instr_global(ctx, ins) {
970 ins->dest = find_or_allocate_temp(ctx, ins->dest);
971
972 for (unsigned i = 0; i < ARRAY_SIZE(ins->src); ++i)
973 ins->src[i] = find_or_allocate_temp(ctx, ins->src[i]);
974 }
975 }
976
977 static midgard_instruction
978 v_load_store_scratch(
979 unsigned srcdest,
980 unsigned index,
981 bool is_store,
982 unsigned mask)
983 {
984 /* We index by 32-bit vec4s */
985 unsigned byte = (index * 4 * 4);
986
987 midgard_instruction ins = {
988 .type = TAG_LOAD_STORE_4,
989 .mask = mask,
990 .dest = ~0,
991 .src = { ~0, ~0, ~0 },
992 .load_store = {
993 .op = is_store ? midgard_op_st_int4 : midgard_op_ld_int4,
994 .swizzle = SWIZZLE_XYZW,
995
996 /* For register spilling - to thread local storage */
997 .arg_1 = 0xEA,
998 .arg_2 = 0x1E,
999
1000 /* Splattered across, TODO combine logically */
1001 .varying_parameters = (byte & 0x1FF) << 1,
1002 .address = (byte >> 9)
1003 },
1004
1005 /* If we spill an unspill, RA goes into an infinite loop */
1006 .no_spill = true
1007 };
1008
1009 if (is_store) {
1010 /* r0 = r26, r1 = r27 */
1011 assert(srcdest == SSA_FIXED_REGISTER(26) || srcdest == SSA_FIXED_REGISTER(27));
1012 ins.src[0] = srcdest;
1013 } else {
1014 ins.dest = srcdest;
1015 }
1016
1017 return ins;
1018 }
1019
1020 /* If register allocation fails, find the best spill node and spill it to fix
1021 * whatever the issue was. This spill node could be a work register (spilling
1022 * to thread local storage), but it could also simply be a special register
1023 * that needs to spill to become a work register. */
1024
1025 static void mir_spill_register(
1026 compiler_context *ctx,
1027 struct ra_graph *g,
1028 unsigned *spill_count)
1029 {
1030 unsigned spill_index = ctx->temp_count;
1031
1032 /* Our first step is to calculate spill cost to figure out the best
1033 * spill node. All nodes are equal in spill cost, but we can't spill
1034 * nodes written to from an unspill */
1035
1036 for (unsigned i = 0; i < ctx->temp_count; ++i) {
1037 ra_set_node_spill_cost(g, i, 1.0);
1038 }
1039
1040 /* We can't spill any bundles that contain unspills. This could be
1041 * optimized to allow use of r27 to spill twice per bundle, but if
1042 * you're at the point of optimizing spilling, it's too late. */
1043
1044 mir_foreach_block(ctx, block) {
1045 mir_foreach_bundle_in_block(block, bun) {
1046 bool no_spill = false;
1047
1048 for (unsigned i = 0; i < bun->instruction_count; ++i)
1049 no_spill |= bun->instructions[i]->no_spill;
1050
1051 if (!no_spill)
1052 continue;
1053
1054 for (unsigned i = 0; i < bun->instruction_count; ++i) {
1055 unsigned dest = bun->instructions[i]->dest;
1056 if (dest < ctx->temp_count)
1057 ra_set_node_spill_cost(g, dest, -1.0);
1058 }
1059 }
1060 }
1061
1062 int spill_node = ra_get_best_spill_node(g);
1063
1064 if (spill_node < 0) {
1065 mir_print_shader(ctx);
1066 assert(0);
1067 }
1068
1069 /* We have a spill node, so check the class. Work registers
1070 * legitimately spill to TLS, but special registers just spill to work
1071 * registers */
1072
1073 unsigned class = ra_get_node_class(g, spill_node);
1074 bool is_special = (class >> 2) != REG_CLASS_WORK;
1075 bool is_special_w = (class >> 2) == REG_CLASS_TEXW;
1076
1077 /* Allocate TLS slot (maybe) */
1078 unsigned spill_slot = !is_special ? (*spill_count)++ : 0;
1079
1080 /* For TLS, replace all stores to the spilled node. For
1081 * special reads, just keep as-is; the class will be demoted
1082 * implicitly. For special writes, spill to a work register */
1083
1084 if (!is_special || is_special_w) {
1085 if (is_special_w)
1086 spill_slot = spill_index++;
1087
1088 mir_foreach_block(ctx, block) {
1089 mir_foreach_instr_in_block_safe(block, ins) {
1090 if (ins->dest != spill_node) continue;
1091
1092 midgard_instruction st;
1093
1094 if (is_special_w) {
1095 st = v_mov(spill_node, blank_alu_src, spill_slot);
1096 st.no_spill = true;
1097 } else {
1098 ins->dest = SSA_FIXED_REGISTER(26);
1099 ins->no_spill = true;
1100 st = v_load_store_scratch(ins->dest, spill_slot, true, ins->mask);
1101 }
1102
1103 /* Hint: don't rewrite this node */
1104 st.hint = true;
1105
1106 mir_insert_instruction_after_scheduled(ctx, block, ins, st);
1107
1108 if (!is_special)
1109 ctx->spills++;
1110 }
1111 }
1112 }
1113
1114 /* For special reads, figure out how many components we need */
1115 unsigned read_mask = 0;
1116
1117 mir_foreach_instr_global_safe(ctx, ins) {
1118 read_mask |= mir_mask_of_read_components(ins, spill_node);
1119 }
1120
1121 /* Insert a load from TLS before the first consecutive
1122 * use of the node, rewriting to use spilled indices to
1123 * break up the live range. Or, for special, insert a
1124 * move. Ironically the latter *increases* register
1125 * pressure, but the two uses of the spilling mechanism
1126 * are somewhat orthogonal. (special spilling is to use
1127 * work registers to back special registers; TLS
1128 * spilling is to use memory to back work registers) */
1129
1130 mir_foreach_block(ctx, block) {
1131 bool consecutive_skip = false;
1132 unsigned consecutive_index = 0;
1133
1134 mir_foreach_instr_in_block(block, ins) {
1135 /* We can't rewrite the moves used to spill in the
1136 * first place. These moves are hinted. */
1137 if (ins->hint) continue;
1138
1139 if (!mir_has_arg(ins, spill_node)) {
1140 consecutive_skip = false;
1141 continue;
1142 }
1143
1144 if (consecutive_skip) {
1145 /* Rewrite */
1146 mir_rewrite_index_src_single(ins, spill_node, consecutive_index);
1147 continue;
1148 }
1149
1150 if (!is_special_w) {
1151 consecutive_index = ++spill_index;
1152
1153 midgard_instruction *before = ins;
1154
1155 /* For a csel, go back one more not to break up the bundle */
1156 if (ins->type == TAG_ALU_4 && OP_IS_CSEL(ins->alu.op))
1157 before = mir_prev_op(before);
1158
1159 midgard_instruction st;
1160
1161 if (is_special) {
1162 /* Move */
1163 st = v_mov(spill_node, blank_alu_src, consecutive_index);
1164 st.no_spill = true;
1165 } else {
1166 /* TLS load */
1167 st = v_load_store_scratch(consecutive_index, spill_slot, false, 0xF);
1168 }
1169
1170 /* Mask the load based on the component count
1171 * actually needed to prvent RA loops */
1172
1173 st.mask = read_mask;
1174
1175 mir_insert_instruction_before_scheduled(ctx, block, before, st);
1176 // consecutive_skip = true;
1177 } else {
1178 /* Special writes already have their move spilled in */
1179 consecutive_index = spill_slot;
1180 }
1181
1182
1183 /* Rewrite to use */
1184 mir_rewrite_index_src_single(ins, spill_node, consecutive_index);
1185
1186 if (!is_special)
1187 ctx->fills++;
1188 }
1189 }
1190
1191 /* Reset hints */
1192
1193 mir_foreach_instr_global(ctx, ins) {
1194 ins->hint = false;
1195 }
1196 }
1197
1198 void
1199 schedule_program(compiler_context *ctx)
1200 {
1201 struct ra_graph *g = NULL;
1202 bool spilled = false;
1203 int iter_count = 1000; /* max iterations */
1204
1205 /* Number of 128-bit slots in memory we've spilled into */
1206 unsigned spill_count = 0;
1207
1208 midgard_promote_uniforms(ctx, 16);
1209
1210 /* Must be lowered right before RA */
1211 mir_squeeze_index(ctx);
1212 mir_lower_special_reads(ctx);
1213 mir_squeeze_index(ctx);
1214
1215 /* Lowering can introduce some dead moves */
1216
1217 mir_foreach_block(ctx, block) {
1218 midgard_opt_dead_move_eliminate(ctx, block);
1219 schedule_block(ctx, block);
1220 }
1221
1222 mir_create_pipeline_registers(ctx);
1223
1224 do {
1225 if (spilled)
1226 mir_spill_register(ctx, g, &spill_count);
1227
1228 mir_squeeze_index(ctx);
1229
1230 g = NULL;
1231 g = allocate_registers(ctx, &spilled);
1232 } while(spilled && ((iter_count--) > 0));
1233
1234 if (iter_count <= 0) {
1235 fprintf(stderr, "panfrost: Gave up allocating registers, rendering will be incomplete\n");
1236 assert(0);
1237 }
1238
1239 /* Report spilling information. spill_count is in 128-bit slots (vec4 x
1240 * fp32), but tls_size is in bytes, so multiply by 16 */
1241
1242 ctx->tls_size = spill_count * 16;
1243
1244 install_registers(ctx, g);
1245 }