Debian Codegraph @

src/freedreno/ir3/ir3_merge_regs.c

raw symbols SCIP
1 /*
2 * Copyright © 2021 Valve Corporation
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "ir3_compiler.h"
7 #include "ir3_ra.h"
8 #include "util/ralloc.h"
9
10 /* This pass "merges" compatible phi-web SSA values. First, we insert a bunch
11 * of parallelcopy's to trivially turn the program into CSSA form. Then we
12 * try to "merge" SSA def's into "merge sets" which could be allocated to a
13 * single register in order to eliminate copies. First we merge phi nodes,
14 * which should always succeed because of the parallelcopy's we inserted, and
15 * then we try to coalesce the copies we introduced.
16 *
17 * The merged registers are used for three purposes:
18 *
19 * 1. We always use the same pvtmem slot for spilling all SSA defs in each
20 * merge set. This prevents us from having to insert memory-to-memory copies
21 * in the spiller and makes sure we don't insert unecessary copies.
22 * 2. When two values are live at the same time, part of the same merge
23 * set, and they overlap each other in the merge set, they always occupy
24 * overlapping physical registers in RA. This reduces register pressure and
25 * copies in several important scenarios:
26 * - When sources of a collect are used later by something else, we don't
27 * have to introduce copies.
28 * - We can handle sequences of extracts that "explode" a vector into its
29 * components without any additional copying.
30 * 3. We use the merge sets for affinities in register allocation: That is, we
31 * try to allocate all the definitions in the same merge set to the
32 * same/compatible registers. This helps us e.g. allocate sources of a collect
33 * to contiguous registers without too much special code in RA.
34 *
35 * In a "normal" register allocator, or when spilling, we'd just merge
36 * registers in the same merge set to the same register, but with SSA-based
37 * register allocation we may have to split the live interval.
38 *
39 * The implementation is based on "Revisiting Out-of-SSA Translation for
40 * Correctness, CodeQuality, and Efficiency," and is broadly similar to the
41 * implementation in nir_from_ssa, with the twist that we also try to coalesce
42 * META_SPLIT and META_COLLECT. This makes this pass more complicated but
43 * prevents us from needing to handle these specially in RA and the spiller,
44 * which are already complicated enough. This also forces us to implement that
45 * value-comparison optimization they explain, as without it we wouldn't be
46 * able to coalesce META_SPLIT even in the simplest of cases.
47 */
48
49 /* In order to dynamically reconstruct the dominance forest, we need the
50 * instructions ordered by a preorder traversal of the dominance tree:
51 */
52
53 static unsigned
54 index_instrs(struct ir3_block *block, unsigned index)
55 {
56 foreach_instr (instr, &block->instr_list)
57 instr->ip = index++;
58
59 for (unsigned i = 0; i < block->dom_children_count; i++)
60 index = index_instrs(block->dom_children[i], index);
61
62 return index;
63 }
64
65 void
66 ir3_index_instrs_for_merge_sets(struct ir3 *ir)
67 {
68 index_instrs(ir3_start_block(ir), 0);
69 }
70
71 /* Definitions within a merge set are ordered by instr->ip as set above: */
72
73 static bool
74 def_after(struct ir3_register *a, struct ir3_register *b)
75 {
76 return a->instr->ip > b->instr->ip;
77 }
78
79 static bool
80 def_dominates(struct ir3_register *a, struct ir3_register *b)
81 {
82 if (def_after(a, b)) {
83 return false;
84 } else if (a->instr->block == b->instr->block) {
85 return def_after(b, a);
86 } else {
87 return ir3_block_dominates(a->instr->block, b->instr->block);
88 }
89 }
90
91 /* This represents a region inside a register. The offset is relative to the
92 * start of the register, and offset + size <= size(reg).
93 */
94 struct def_value {
95 struct ir3_register *reg;
96 unsigned offset, size;
97 };
98
99 /* Chase any copies to get the source of a region inside a register. This is
100 * Value(a) in the paper.
101 */
102 static struct def_value
103 chase_copies(struct def_value value)
104 {
105 while (true) {
106 struct ir3_instruction *instr = value.reg->instr;
107 if (instr->opc == OPC_META_SPLIT) {
108 value.offset += instr->split.off * reg_elem_size(value.reg);
109 value.reg = instr->srcs[0]->def;
110 } else if (instr->opc == OPC_META_COLLECT) {
111 if (value.offset % reg_elem_size(value.reg) != 0 ||
112 value.size > reg_elem_size(value.reg) ||
113 value.offset + value.size > reg_size(value.reg))
114 break;
115 struct ir3_register *src =
116 instr->srcs[value.offset / reg_elem_size(value.reg)];
117 if (!src->def)
118 break;
119 value.offset = 0;
120 value.reg = src->def;
121 } else {
122 /* TODO: parallelcopy */
123 break;
124 }
125 }
126
127 return value;
128 }
129
130 /* This represents an entry in the merge set, and consists of a register +
131 * offset from the merge set base.
132 */
133 struct merge_def {
134 struct ir3_register *reg;
135 unsigned offset;
136 };
137
138 static bool
139 can_skip_interference(const struct merge_def *a, const struct merge_def *b)
140 {
141 unsigned a_start = a->offset;
142 unsigned b_start = b->offset;
143 unsigned a_end = a_start + reg_size(a->reg);
144 unsigned b_end = b_start + reg_size(b->reg);
145
146 /* Registers that don't overlap never interfere */
147 if (a_end <= b_start || b_end <= a_start)
148 return true;
149
150 /* Disallow skipping interference unless one definition contains the
151 * other. This restriction is important for register allocation, because
152 * it means that at any given point in the program, the live values in a
153 * given merge set will form a tree. If they didn't, then one live value
154 * would partially overlap another, and they would have overlapping live
155 * ranges because they're live at the same point. This simplifies register
156 * allocation and spilling.
157 */
158 if (!((a_start <= b_start && a_end >= b_end) ||
159 (b_start <= a_start && b_end >= a_end)))
160 return false;
161
162 /* For each register, chase the intersection of a and b to find the
163 * ultimate source.
164 */
165 unsigned start = MAX2(a_start, b_start);
166 unsigned end = MIN2(a_end, b_end);
167 struct def_value a_value = chase_copies((struct def_value){
168 .reg = a->reg,
169 .offset = start - a_start,
170 .size = end - start,
171 });
172 struct def_value b_value = chase_copies((struct def_value){
173 .reg = b->reg,
174 .offset = start - b_start,
175 .size = end - start,
176 });
177 return a_value.reg == b_value.reg && a_value.offset == b_value.offset;
178 }
179
180 static struct ir3_merge_set *
181 get_merge_set(struct ir3_register *def)
182 {
183 if (def->merge_set)
184 return def->merge_set;
185
186 struct ir3_merge_set *set =
187 linear_alloc(def->instr->block->shader->lin_ctx, struct ir3_merge_set);
188 set->preferred_reg = ~0;
189 set->interval_start = ~0;
190 set->spill_slot = ~0;
191 set->size = reg_size(def);
192 set->alignment = (def->flags & IR3_REG_HALF) ? 1 : 2;
193 set->regs_count = 1;
194 set->regs = linear_alloc(def->instr->block->shader->lin_ctx, struct ir3_register *);
195 set->regs[0] = def;
196
197 return set;
198 }
199
200 /* Merges b into a */
201 static struct ir3_merge_set *
202 merge_merge_sets(struct ir3_merge_set *a, struct ir3_merge_set *b, int b_offset)
203 {
204 if (b_offset < 0)
205 return merge_merge_sets(b, a, -b_offset);
206
207 struct ir3_register **new_regs =
208 linear_zalloc_array(a->regs[0]->instr->block->shader->lin_ctx,
209 struct ir3_register *, a->regs_count + b->regs_count);
210
211 unsigned a_index = 0, b_index = 0, new_index = 0;
212 for (; a_index < a->regs_count || b_index < b->regs_count; new_index++) {
213 if (b_index < b->regs_count &&
214 (a_index == a->regs_count ||
215 def_after(a->regs[a_index], b->regs[b_index]))) {
216 new_regs[new_index] = b->regs[b_index++];
217 new_regs[new_index]->merge_set_offset += b_offset;
218 } else {
219 new_regs[new_index] = a->regs[a_index++];
220 }
221 new_regs[new_index]->merge_set = a;
222 }
223
224 assert(new_index == a->regs_count + b->regs_count);
225
226 /* Technically this should be the lcm, but because alignment is only 1 or
227 * 2 so far this should be ok.
228 */
229 a->alignment = MAX2(a->alignment, b->alignment);
230 a->regs_count += b->regs_count;
231 a->regs = new_regs;
232 a->size = MAX2(a->size, b->size + b_offset);
233
234 return a;
235 }
236
237 static bool
238 merge_sets_interfere(struct ir3_liveness *live, struct ir3_merge_set *a,
239 struct ir3_merge_set *b, int b_offset)
240 {
241 if (b_offset < 0)
242 return merge_sets_interfere(live, b, a, -b_offset);
243
244 struct merge_def dom[a->regs_count + b->regs_count];
245 unsigned a_index = 0, b_index = 0;
246 int dom_index = -1;
247
248 /* Reject trying to merge the sets if the alignment doesn't work out */
249 if ((a->alignment + b_offset) % b->alignment != 0)
250 return true;
251
252 while (a_index < a->regs_count || b_index < b->regs_count) {
253 struct merge_def current;
254 if (a_index == a->regs_count) {
255 current.reg = b->regs[b_index];
256 current.offset = current.reg->merge_set_offset + b_offset;
257 b_index++;
258 } else if (b_index == b->regs_count) {
259 current.reg = a->regs[a_index];
260 current.offset = current.reg->merge_set_offset;
261 a_index++;
262 } else {
263 if (def_after(b->regs[b_index], a->regs[a_index])) {
264 current.reg = a->regs[a_index];
265 current.offset = current.reg->merge_set_offset;
266 a_index++;
267 } else {
268 current.reg = b->regs[b_index];
269 current.offset = current.reg->merge_set_offset + b_offset;
270 b_index++;
271 }
272 }
273
274 while (dom_index >= 0 &&
275 !def_dominates(dom[dom_index].reg, current.reg)) {
276 dom_index--;
277 }
278
279 /* TODO: in the original paper, just dom[dom_index] needs to be
280 * checked for interference. We implement the value-chasing extension
281 * as well as support for sub-registers, which complicates this
282 * significantly because it's no longer the case that if a dominates b
283 * dominates c and a and b don't interfere then we only need to check
284 * interference between b and c to be sure a and c don't interfere --
285 * this means we may have to check for interference against values
286 * higher in the stack then dom[dom_index]. In the paper there's a
287 * description of a way to do less interference tests with the
288 * value-chasing extension, but we'd have to come up with something
289 * ourselves for handling the similar problems that come up with
290 * allowing values to contain subregisters. For now we just test
291 * everything in the stack.
292 */
293 for (int i = 0; i <= dom_index; i++) {
294 if (can_skip_interference(&current, &dom[i]))
295 continue;
296
297 /* Ok, now we actually have to check interference. Since we know
298 * that dom[i] dominates current, this boils down to checking
299 * whether dom[i] is live after current.
300 */
301 if (ir3_def_live_after(live, dom[i].reg, current.reg->instr))
302 return true;
303 }
304
305 dom[++dom_index] = current;
306 }
307
308 return false;
309 }
310
311 static void
312 try_merge_defs(struct ir3_liveness *live, struct ir3_register *a,
313 struct ir3_register *b, unsigned b_offset)
314 {
315 struct ir3_merge_set *a_set = get_merge_set(a);
316 struct ir3_merge_set *b_set = get_merge_set(b);
317
318 if (a_set == b_set) {
319 /* Note: Even in this case we may not always successfully be able to
320 * coalesce this copy, if the offsets don't line up. But in any
321 * case, we can't do anything.
322 */
323 return;
324 }
325
326 int b_set_offset = a->merge_set_offset + b_offset - b->merge_set_offset;
327
328 if (!merge_sets_interfere(live, a_set, b_set, b_set_offset))
329 merge_merge_sets(a_set, b_set, b_set_offset);
330 }
331
332 void
333 ir3_force_merge(struct ir3_register *a, struct ir3_register *b, int b_offset)
334 {
335 struct ir3_merge_set *a_set = get_merge_set(a);
336 struct ir3_merge_set *b_set = get_merge_set(b);
337
338 if (a_set == b_set)
339 return;
340
341 int b_set_offset = a->merge_set_offset + b_offset - b->merge_set_offset;
342 merge_merge_sets(a_set, b_set, b_set_offset);
343 }
344
345 static void
346 coalesce_phi(struct ir3_liveness *live, struct ir3_instruction *phi)
347 {
348 for (unsigned i = 0; i < phi->srcs_count; i++) {
349 if (phi->srcs[i]->def)
350 try_merge_defs(live, phi->dsts[0], phi->srcs[i]->def, 0);
351 }
352 }
353
354 static void
355 aggressive_coalesce_parallel_copy(struct ir3_liveness *live,
356 struct ir3_instruction *pcopy)
357 {
358 for (unsigned i = 0; i < pcopy->dsts_count; i++) {
359 if (!(pcopy->srcs[i]->flags & IR3_REG_SSA))
360 continue;
361 try_merge_defs(live, pcopy->dsts[i], pcopy->srcs[i]->def, 0);
362 }
363 }
364
365 static void
366 aggressive_coalesce_split(struct ir3_liveness *live,
367 struct ir3_instruction *split)
368 {
369 if (!(split->dsts[0]->flags & IR3_REG_SSA))
370 return;
371 try_merge_defs(live, split->srcs[0]->def, split->dsts[0],
372 split->split.off * reg_elem_size(split->dsts[0]));
373 }
374
375 static void
376 aggressive_coalesce_collect(struct ir3_liveness *live,
377 struct ir3_instruction *collect)
378 {
379 for (unsigned i = 0, offset = 0; i < collect->srcs_count;
380 offset += reg_elem_size(collect->srcs[i]), i++) {
381 if (!(collect->srcs[i]->flags & IR3_REG_SSA) ||
382 !collect->srcs[i]->def)
383 continue;
384 try_merge_defs(live, collect->dsts[0], collect->srcs[i]->def, offset);
385 }
386 }
387
388 static void
389 aggressive_coalesce_rpt(struct ir3_liveness *live,
390 struct ir3_instruction *instr)
391 {
392 if (!ir3_instr_is_first_rpt(instr))
393 return;
394
395 struct ir3_register *def = instr->dsts[0];
396 unsigned def_offset = 0;
397 unsigned src_offsets[instr->srcs_count];
398 memset(src_offsets, 0, sizeof(unsigned) * instr->srcs_count);
399
400 foreach_instr_rpt_excl (rpt, instr) {
401 if (!(rpt->dsts[0]->flags & IR3_REG_SSA))
402 continue;
403
404 def_offset += reg_elem_size(def);
405 try_merge_defs(live, def, rpt->dsts[0], def_offset);
406
407 foreach_src_n (src, src_n, instr) {
408 struct ir3_register *rpt_src = rpt->srcs[src_n];
409
410 if (!(src->flags & IR3_REG_SSA) || !(rpt_src->flags & IR3_REG_SSA))
411 continue;
412 if (src->def == rpt_src->def)
413 continue;
414
415 src_offsets[src_n] += reg_elem_size(src->def);
416 try_merge_defs(live, src->def, rpt_src->def, src_offsets[src_n]);
417 }
418 }
419 }
420
421 static void
422 create_parallel_copy(struct ir3_block *block)
423 {
424 for (unsigned i = 0; i < 2; i++) {
425 if (!block->successors[i])
426 continue;
427
428 struct ir3_block *succ = block->successors[i];
429
430 unsigned pred_idx = ir3_block_get_pred_index(succ, block);
431
432 unsigned phi_count = 0;
433 foreach_instr (phi, &succ->instr_list) {
434 if (phi->opc != OPC_META_PHI)
435 break;
436
437 /* Avoid phis we've already colored */
438 if (!(phi->dsts[0]->flags & IR3_REG_SSA))
439 continue;
440
441 /* Avoid undef */
442 if ((phi->srcs[pred_idx]->flags & IR3_REG_SSA) &&
443 !phi->srcs[pred_idx]->def)
444 continue;
445
446 /* We don't support critical edges. If we were to support them,
447 * we'd need to insert parallel copies after the phi node to solve
448 * the lost-copy problem.
449 */
450 assert(i == 0 && !block->successors[1]);
451 phi_count++;
452 }
453
454 if (phi_count == 0)
455 continue;
456
457 struct ir3_register *src[phi_count];
458 unsigned j = 0;
459 foreach_instr (phi, &succ->instr_list) {
460 if (phi->opc != OPC_META_PHI)
461 break;
462 if (!(phi->dsts[0]->flags & IR3_REG_SSA))
463 continue;
464 if ((phi->srcs[pred_idx]->flags & IR3_REG_SSA) &&
465 !phi->srcs[pred_idx]->def)
466 continue;
467 src[j++] = phi->srcs[pred_idx];
468 }
469 assert(j == phi_count);
470
471 struct ir3_instruction *pcopy =
472 ir3_instr_create_at(ir3_before_terminator(block),
473 OPC_META_PARALLEL_COPY, phi_count, phi_count);
474
475 for (j = 0; j < phi_count; j++) {
476 struct ir3_register *reg = __ssa_dst(pcopy);
477 reg->flags |= src[j]->flags & (IR3_REG_HALF | IR3_REG_ARRAY);
478 reg->size = src[j]->size;
479 reg->wrmask = src[j]->wrmask;
480 }
481
482 for (j = 0; j < phi_count; j++) {
483 pcopy->srcs[pcopy->srcs_count++] =
484 ir3_reg_clone(block->shader, src[j]);
485 }
486
487 j = 0;
488 foreach_instr (phi, &succ->instr_list) {
489 if (phi->opc != OPC_META_PHI)
490 break;
491 if (!(phi->dsts[0]->flags & IR3_REG_SSA))
492 continue;
493 if ((phi->srcs[pred_idx]->flags & IR3_REG_SSA) &&
494 !phi->srcs[pred_idx]->def)
495 continue;
496 phi->srcs[pred_idx]->def = pcopy->dsts[j];
497 pcopy->dsts[j]->flags |= phi->dsts[0]->flags & IR3_REG_SHARED;
498 phi->srcs[pred_idx]->flags = pcopy->dsts[j]->flags;
499 phi->srcs[pred_idx]->num = INVALID_REG;
500 j++;
501 }
502 assert(j == phi_count);
503 }
504 }
505
506 void
507 ir3_create_parallel_copies(struct ir3 *ir)
508 {
509 foreach_block (block, &ir->block_list) {
510 create_parallel_copy(block);
511 }
512 }
513
514 static void
515 index_merge_sets(struct ir3_liveness *live, struct ir3 *ir, bool update)
516 {
517 unsigned offset = update ? live->interval_offset : 0;
518 foreach_block (block, &ir->block_list) {
519 foreach_instr (instr, &block->instr_list) {
520 for (unsigned i = 0; i < instr->dsts_count; i++) {
521 struct ir3_register *dst = instr->dsts[i];
522
523 if (update &&
524 (dst->interval_start != 0 || dst->interval_end != 0)) {
525 continue;
526 }
527
528 unsigned dst_offset;
529 struct ir3_merge_set *merge_set = dst->merge_set;
530 unsigned size = reg_size(dst);
531 if (merge_set) {
532 if (merge_set->interval_start == ~0) {
533 merge_set->interval_start = offset;
534 offset += merge_set->size;
535 }
536 dst_offset = merge_set->interval_start + dst->merge_set_offset;
537 } else {
538 dst_offset = offset;
539 offset += size;
540 }
541
542 dst->interval_start = dst_offset;
543 dst->interval_end = dst_offset + size;
544 }
545 }
546 }
547
548 live->interval_offset = offset;
549 }
550
551 void
552 ir3_update_merge_sets_index(struct ir3_liveness *live, struct ir3 *ir)
553 {
554 index_merge_sets(live, ir, true);
555 }
556
557 #define RESET "\x1b[0m"
558 #define BLUE "\x1b[0;34m"
559 #define SYN_SSA(x) BLUE x RESET
560
561 static void
562 dump_merge_sets(struct ir3 *ir)
563 {
564 d("merge sets:");
565 struct set *merge_sets = _mesa_pointer_set_create(NULL);
566 foreach_block (block, &ir->block_list) {
567 foreach_instr (instr, &block->instr_list) {
568 for (unsigned i = 0; i < instr->dsts_count; i++) {
569 struct ir3_register *dst = instr->dsts[i];
570
571 struct ir3_merge_set *merge_set = dst->merge_set;
572 if (!merge_set || _mesa_set_search(merge_sets, merge_set))
573 continue;
574
575 d("merge set, size %u, align %u, interval start %u:",
576 merge_set->size, merge_set->alignment, merge_set->interval_start);
577 for (unsigned j = 0; j < merge_set->regs_count; j++) {
578 struct ir3_register *reg = merge_set->regs[j];
579 const char *s = (reg->flags & IR3_REG_SHARED) ? "s" : "";
580 const char *h = (reg->flags & IR3_REG_HALF) ? "h" : "";
581 d("\t%s%s" SYN_SSA("ssa_%u") ":%u, offset %u, interval: %u-%u",
582 s, h, reg->instr->serialno, reg->name, reg->merge_set_offset,
583 reg->interval_start, reg->interval_end);
584 }
585
586 _mesa_set_add(merge_sets, merge_set);
587 }
588 }
589 }
590
591 ralloc_free(merge_sets);
592 }
593
594 void
595 ir3_aggressive_coalesce(struct ir3_liveness *live,
596 struct ir3_instruction *instr)
597 {
598 switch (instr->opc) {
599 case OPC_META_SPLIT:
600 aggressive_coalesce_split(live, instr);
601 break;
602 case OPC_META_COLLECT:
603 aggressive_coalesce_collect(live, instr);
604 break;
605 case OPC_META_PARALLEL_COPY:
606 aggressive_coalesce_parallel_copy(live, instr);
607 break;
608 default:
609 break;
610 }
611 }
612
613 void
614 ir3_merge_regs(struct ir3_liveness *live, struct ir3 *ir)
615 {
616 /* First pass: coalesce phis, which must be together. */
617 foreach_block (block, &ir->block_list) {
618 foreach_instr (instr, &block->instr_list) {
619 if (instr->opc != OPC_META_PHI)
620 break;
621
622 coalesce_phi(live, instr);
623 }
624 }
625
626 /* Second pass: aggressively coalesce parallelcopy, split, collect */
627 foreach_block (block, &ir->block_list) {
628 foreach_instr (instr, &block->instr_list) {
629 ir3_aggressive_coalesce(live, instr);
630 }
631 }
632
633 foreach_block (block, &ir->block_list) {
634 foreach_instr (instr, &block->instr_list) {
635 aggressive_coalesce_rpt(live, instr);
636 }
637 }
638
639 index_merge_sets(live, ir, false);
640
641 if (ir3_shader_debug & IR3_DBG_RAMSGS)
642 dump_merge_sets(ir);
643 }