2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 * Copyright (C) 2019 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include "midgard_ops.h"
27 #include "util/register_allocate.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
31 /* For work registers, we can subdivide in various ways. So we create
32 * classes for the various sizes and conflict accordingly, keeping in
33 * mind that physical registers are divided along 128-bit boundaries.
34 * The important part is that 128-bit boundaries are not crossed.
36 * For each 128-bit register, we can subdivide to 32-bits 10 ways
43 * For each 64-bit register, we can subdivide similarly to 16-bit
44 * (TODO: half-float RA, not that we support fp16 yet)
47 #define WORK_STRIDE 10
49 /* We have overlapping register classes for special registers, handled via
55 /* Prepacked masks/swizzles for virtual register types */
56 static unsigned reg_type_to_mask
[WORK_STRIDE
] = {
58 0x7, 0x7 << 1, /* xyz */
59 0x3, 0x3 << 1, 0x3 << 2, /* xy */
60 0x1, 0x1 << 1, 0x1 << 2, 0x1 << 3 /* x */
63 static unsigned reg_type_to_swizzle
[WORK_STRIDE
] = {
64 SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
66 SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
67 SWIZZLE(COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
, COMPONENT_W
),
69 SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
70 SWIZZLE(COMPONENT_Y
, COMPONENT_Z
, COMPONENT_Z
, COMPONENT_W
),
71 SWIZZLE(COMPONENT_Z
, COMPONENT_W
, COMPONENT_Z
, COMPONENT_W
),
73 SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
74 SWIZZLE(COMPONENT_Y
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
75 SWIZZLE(COMPONENT_Z
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
76 SWIZZLE(COMPONENT_W
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
85 /* Given the mask/swizzle of both the register and the original source,
86 * compose to find the actual mask/swizzle to give the hardware */
89 compose_writemask(unsigned mask
, struct phys_reg reg
)
91 /* Note: the reg mask is guaranteed to be contiguous. So we shift
92 * into the X place, compose via a simple AND, and shift back */
94 unsigned shift
= __builtin_ctz(reg
.mask
);
95 return ((reg
.mask
>> shift
) & mask
) << shift
;
99 compose_swizzle(unsigned swizzle
, unsigned mask
,
100 struct phys_reg reg
, struct phys_reg dst
)
102 unsigned out
= pan_compose_swizzle(swizzle
, reg
.swizzle
);
104 /* Based on the register mask, we need to adjust over. E.g if we're
105 * writing to yz, a base swizzle of xy__ becomes _xy_. Save the
106 * original first component (x). But to prevent duplicate shifting
107 * (only applies to ALU -- mask param is set to xyzw out on L/S to
108 * prevent changes), we have to account for the shift inherent to the
109 * original writemask */
111 unsigned rep
= out
& 0x3;
112 unsigned shift
= __builtin_ctz(dst
.mask
) - __builtin_ctz(mask
);
113 unsigned shifted
= out
<< (2*shift
);
115 /* ..but we fill in the gaps so it appears to replicate */
117 for (unsigned s
= 0; s
< shift
; ++s
)
118 shifted
|= rep
<< (2*s
);
123 /* Helper to return the default phys_reg for a given register */
125 static struct phys_reg
126 default_phys_reg(int reg
)
128 struct phys_reg r
= {
130 .mask
= 0xF, /* xyzw */
131 .swizzle
= 0xE4 /* xyzw */
137 /* Determine which physical register, swizzle, and mask a virtual
138 * register corresponds to */
140 static struct phys_reg
141 index_to_reg(compiler_context
*ctx
, struct ra_graph
*g
, unsigned reg
)
143 /* Check for special cases */
144 if ((reg
== ~0) && g
)
145 return default_phys_reg(REGISTER_UNUSED
);
146 else if (reg
>= SSA_FIXED_MINIMUM
)
147 return default_phys_reg(SSA_REG_FROM_FIXED(reg
));
149 return default_phys_reg(REGISTER_UNUSED
);
151 /* Special cases aside, we pick the underlying register */
152 int virt
= ra_get_node_reg(g
, reg
);
154 /* Divide out the register and classification */
155 int phys
= virt
/ WORK_STRIDE
;
156 int type
= virt
% WORK_STRIDE
;
158 /* Apply shadow registers */
160 if (phys
>= SHADOW_R28
&& phys
<= SHADOW_R29
)
161 phys
+= 28 - SHADOW_R28
;
163 struct phys_reg r
= {
165 .mask
= reg_type_to_mask
[type
],
166 .swizzle
= reg_type_to_swizzle
[type
]
169 /* Report that we actually use this register, and return it */
172 ctx
->work_registers
= MAX2(ctx
->work_registers
, phys
);
177 /* This routine creates a register set. Should be called infrequently since
178 * it's slow and can be cached. For legibility, variables are named in terms of
179 * work registers, although it is also used to create the register set for
180 * special register allocation */
183 add_shadow_conflicts (struct ra_regs
*regs
, unsigned base
, unsigned shadow
)
185 for (unsigned a
= 0; a
< WORK_STRIDE
; ++a
) {
186 unsigned reg_a
= (WORK_STRIDE
* base
) + a
;
188 for (unsigned b
= 0; b
< WORK_STRIDE
; ++b
) {
189 unsigned reg_b
= (WORK_STRIDE
* shadow
) + b
;
191 ra_add_reg_conflict(regs
, reg_a
, reg_b
);
192 ra_add_reg_conflict(regs
, reg_b
, reg_a
);
197 static struct ra_regs
*
198 create_register_set(unsigned work_count
, unsigned *classes
)
200 int virtual_count
= 32 * WORK_STRIDE
;
202 /* First, initialize the RA */
203 struct ra_regs
*regs
= ra_alloc_reg_set(NULL
, virtual_count
, true);
205 for (unsigned c
= 0; c
< NR_REG_CLASSES
; ++c
) {
206 int work_vec4
= ra_alloc_reg_class(regs
);
207 int work_vec3
= ra_alloc_reg_class(regs
);
208 int work_vec2
= ra_alloc_reg_class(regs
);
209 int work_vec1
= ra_alloc_reg_class(regs
);
211 classes
[4*c
+ 0] = work_vec1
;
212 classes
[4*c
+ 1] = work_vec2
;
213 classes
[4*c
+ 2] = work_vec3
;
214 classes
[4*c
+ 3] = work_vec4
;
216 /* Special register classes have other register counts */
218 (c
== REG_CLASS_WORK
) ? work_count
: 2;
221 (c
== REG_CLASS_LDST
) ? 26 :
222 (c
== REG_CLASS_TEXR
) ? 28 :
223 (c
== REG_CLASS_TEXW
) ? SHADOW_R28
:
226 /* Add the full set of work registers */
227 for (unsigned i
= first_reg
; i
< (first_reg
+ count
); ++i
) {
228 int base
= WORK_STRIDE
* i
;
230 /* Build a full set of subdivisions */
231 ra_class_add_reg(regs
, work_vec4
, base
);
232 ra_class_add_reg(regs
, work_vec3
, base
+ 1);
233 ra_class_add_reg(regs
, work_vec3
, base
+ 2);
234 ra_class_add_reg(regs
, work_vec2
, base
+ 3);
235 ra_class_add_reg(regs
, work_vec2
, base
+ 4);
236 ra_class_add_reg(regs
, work_vec2
, base
+ 5);
237 ra_class_add_reg(regs
, work_vec1
, base
+ 6);
238 ra_class_add_reg(regs
, work_vec1
, base
+ 7);
239 ra_class_add_reg(regs
, work_vec1
, base
+ 8);
240 ra_class_add_reg(regs
, work_vec1
, base
+ 9);
242 for (unsigned a
= 0; a
< 10; ++a
) {
243 unsigned mask1
= reg_type_to_mask
[a
];
245 for (unsigned b
= 0; b
< 10; ++b
) {
246 unsigned mask2
= reg_type_to_mask
[b
];
249 ra_add_reg_conflict(regs
,
257 /* We have duplicate classes */
258 add_shadow_conflicts(regs
, 28, SHADOW_R28
);
259 add_shadow_conflicts(regs
, 29, SHADOW_R29
);
261 /* We're done setting up */
262 ra_set_finalize(regs
, NULL
);
267 /* This routine gets a precomputed register set off the screen if it's able, or
268 * otherwise it computes one on the fly */
270 static struct ra_regs
*
271 get_register_set(struct midgard_screen
*screen
, unsigned work_count
, unsigned **classes
)
274 assert(work_count
>= 8);
275 assert(work_count
<= 16);
278 unsigned index
= work_count
- 8;
280 /* Find the reg set */
281 struct ra_regs
*cached
= screen
->regs
[index
];
284 assert(screen
->reg_classes
[index
]);
285 *classes
= screen
->reg_classes
[index
];
289 /* Otherwise, create one */
290 struct ra_regs
*created
= create_register_set(work_count
, screen
->reg_classes
[index
]);
292 /* Cache it and use it */
293 screen
->regs
[index
] = created
;
295 *classes
= screen
->reg_classes
[index
];
299 /* Assign a (special) class, ensuring that it is compatible with whatever class
303 set_class(unsigned *classes
, unsigned node
, unsigned class)
305 /* Check that we're even a node */
306 if (node
>= SSA_FIXED_MINIMUM
)
309 /* First 4 are work, next 4 are load/store.. */
310 unsigned current_class
= classes
[node
] >> 2;
313 if (class == current_class
)
316 /* If we're changing, we haven't assigned a special class */
317 assert(current_class
== REG_CLASS_WORK
);
319 classes
[node
] &= 0x3;
320 classes
[node
] |= (class << 2);
324 force_vec4(unsigned *classes
, unsigned node
)
326 if (node
>= SSA_FIXED_MINIMUM
)
330 classes
[node
] |= 0x3;
333 /* Special register classes impose special constraints on who can read their
334 * values, so check that */
337 check_read_class(unsigned *classes
, unsigned tag
, unsigned node
)
339 /* Non-nodes are implicitly ok */
340 if (node
>= SSA_FIXED_MINIMUM
)
343 unsigned current_class
= classes
[node
] >> 2;
345 switch (current_class
) {
347 return (tag
== TAG_LOAD_STORE_4
);
349 return (tag
== TAG_TEXTURE_4
);
351 return (tag
!= TAG_LOAD_STORE_4
);
353 return (tag
== TAG_ALU_4
);
355 unreachable("Invalid class");
360 check_write_class(unsigned *classes
, unsigned tag
, unsigned node
)
362 /* Non-nodes are implicitly ok */
363 if (node
>= SSA_FIXED_MINIMUM
)
366 unsigned current_class
= classes
[node
] >> 2;
368 switch (current_class
) {
372 return (tag
== TAG_TEXTURE_4
);
375 return (tag
== TAG_ALU_4
) || (tag
== TAG_LOAD_STORE_4
);
377 unreachable("Invalid class");
381 /* Prepass before RA to ensure special class restrictions are met. The idea is
382 * to create a bit field of types of instructions that read a particular index.
383 * Later, we'll add moves as appropriate and rewrite to specialize by type. */
386 mark_node_class (unsigned *bitfield
, unsigned node
)
388 if (node
< SSA_FIXED_MINIMUM
)
389 BITSET_SET(bitfield
, node
);
393 mir_lower_special_reads(compiler_context
*ctx
)
395 size_t sz
= BITSET_WORDS(ctx
->temp_count
) * sizeof(BITSET_WORD
);
397 /* Bitfields for the various types of registers we could have */
399 unsigned *alur
= calloc(sz
, 1);
400 unsigned *aluw
= calloc(sz
, 1);
401 unsigned *ldst
= calloc(sz
, 1);
402 unsigned *texr
= calloc(sz
, 1);
403 unsigned *texw
= calloc(sz
, 1);
405 /* Pass #1 is analysis, a linear scan to fill out the bitfields */
407 mir_foreach_instr_global(ctx
, ins
) {
410 mark_node_class(aluw
, ins
->dest
);
411 mark_node_class(alur
, ins
->src
[0]);
412 mark_node_class(alur
, ins
->src
[1]);
415 case TAG_LOAD_STORE_4
:
416 mark_node_class(ldst
, ins
->src
[0]);
417 mark_node_class(ldst
, ins
->src
[1]);
418 mark_node_class(ldst
, ins
->src
[2]);
422 mark_node_class(texr
, ins
->src
[0]);
423 mark_node_class(texr
, ins
->src
[1]);
424 mark_node_class(texr
, ins
->src
[2]);
425 mark_node_class(texw
, ins
->dest
);
430 /* Pass #2 is lowering now that we've analyzed all the classes.
431 * Conceptually, if an index is only marked for a single type of use,
432 * there is nothing to lower. If it is marked for different uses, we
433 * split up based on the number of types of uses. To do so, we divide
434 * into N distinct classes of use (where N>1 by definition), emit N-1
435 * moves from the index to copies of the index, and finally rewrite N-1
436 * of the types of uses to use the corresponding move */
438 unsigned spill_idx
= ctx
->temp_count
;
440 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
) {
441 bool is_alur
= BITSET_TEST(alur
, i
);
442 bool is_aluw
= BITSET_TEST(aluw
, i
);
443 bool is_ldst
= BITSET_TEST(ldst
, i
);
444 bool is_texr
= BITSET_TEST(texr
, i
);
445 bool is_texw
= BITSET_TEST(texw
, i
);
447 /* Analyse to check how many distinct uses there are. ALU ops
448 * (alur) can read the results of the texture pipeline (texw)
449 * but not ldst or texr. Load/store ops (ldst) cannot read
450 * anything but load/store inputs. Texture pipeline cannot read
451 * anything but texture inputs. TODO: Simplify. */
454 (is_alur
&& (is_ldst
|| is_texr
)) ||
455 (is_ldst
&& (is_alur
|| is_texr
|| is_texw
)) ||
456 (is_texr
&& (is_alur
|| is_ldst
|| is_texw
)) ||
457 (is_texw
&& (is_aluw
|| is_ldst
|| is_texr
));
462 /* Use the index as-is as the work copy. Emit copies for
465 unsigned classes
[] = { TAG_LOAD_STORE_4
, TAG_TEXTURE_4
, TAG_TEXTURE_4
};
466 bool collisions
[] = { is_ldst
, is_texr
, is_texw
&& is_aluw
};
468 for (unsigned j
= 0; j
< ARRAY_SIZE(collisions
); ++j
) {
469 if (!collisions
[j
]) continue;
471 /* When the hazard is from reading, we move and rewrite
472 * sources (typical case). When it's from writing, we
473 * flip the move and rewrite destinations (obscure,
474 * only from control flow -- impossible in SSA) */
476 bool hazard_write
= (j
== 2);
478 unsigned idx
= spill_idx
++;
480 midgard_instruction m
= hazard_write
?
481 v_mov(idx
, blank_alu_src
, i
) :
482 v_mov(i
, blank_alu_src
, idx
);
484 /* Insert move before each read/write, depending on the
485 * hazard we're trying to account for */
487 mir_foreach_instr_global_safe(ctx
, pre_use
) {
488 if (pre_use
->type
!= classes
[j
])
492 if (pre_use
->dest
!= i
)
495 if (!mir_has_arg(pre_use
, i
))
500 midgard_instruction
*use
= mir_next_op(pre_use
);
502 mir_insert_instruction_before(ctx
, use
, m
);
503 mir_rewrite_index_dst_single(pre_use
, i
, idx
);
506 m
= v_mov(i
, blank_alu_src
, idx
);
507 m
.mask
= mir_mask_of_read_components(pre_use
, i
);
508 mir_insert_instruction_before(ctx
, pre_use
, m
);
509 mir_rewrite_index_src_single(pre_use
, i
, idx
);
522 /* Routines for liveness analysis */
525 liveness_gen(uint8_t *live
, unsigned node
, unsigned max
, unsigned mask
)
534 liveness_kill(uint8_t *live
, unsigned node
, unsigned max
, unsigned mask
)
542 /* Updates live_in for a single instruction */
545 liveness_ins_update(uint8_t *live
, midgard_instruction
*ins
, unsigned max
)
547 /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
549 liveness_kill(live
, ins
->dest
, max
, ins
->mask
);
551 mir_foreach_src(ins
, src
) {
552 unsigned node
= ins
->src
[src
];
553 unsigned mask
= mir_mask_of_read_components(ins
, node
);
555 liveness_gen(live
, node
, max
, mask
);
559 /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
562 liveness_block_live_out(compiler_context
*ctx
, midgard_block
*blk
)
564 mir_foreach_successor(blk
, succ
) {
565 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
)
566 blk
->live_out
[i
] |= succ
->live_in
[i
];
570 /* Liveness analysis is a backwards-may dataflow analysis pass. Within a block,
571 * we compute live_out from live_in. The intrablock pass is linear-time. It
572 * returns whether progress was made. */
575 liveness_block_update(compiler_context
*ctx
, midgard_block
*blk
)
577 bool progress
= false;
579 liveness_block_live_out(ctx
, blk
);
581 uint8_t *live
= mem_dup(blk
->live_out
, ctx
->temp_count
);
583 mir_foreach_instr_in_block_rev(blk
, ins
)
584 liveness_ins_update(live
, ins
, ctx
->temp_count
);
586 /* To figure out progress, diff live_in */
588 for (unsigned i
= 0; (i
< ctx
->temp_count
) && !progress
; ++i
)
589 progress
|= (blk
->live_in
[i
] != live
[i
]);
597 /* Globally, liveness analysis uses a fixed-point algorithm based on a
598 * worklist. We initialize a work list with the exit block. We iterate the work
599 * list to compute live_in from live_out for each block on the work list,
600 * adding the predecessors of the block to the work list if we made progress.
604 mir_compute_liveness(
605 compiler_context
*ctx
,
608 /* List of midgard_block */
609 struct set
*work_list
;
611 work_list
= _mesa_set_create(ctx
,
613 _mesa_key_pointer_equal
);
617 mir_foreach_block(ctx
, block
) {
618 block
->live_in
= calloc(ctx
->temp_count
, 1);
619 block
->live_out
= calloc(ctx
->temp_count
, 1);
622 /* Initialize the work list with the exit block */
623 struct set_entry
*cur
;
625 midgard_block
*exit
= mir_exit_block(ctx
);
626 cur
= _mesa_set_add(work_list
, exit
);
628 /* Iterate the work list */
631 /* Pop off a block */
632 midgard_block
*blk
= (struct midgard_block
*) cur
->key
;
633 _mesa_set_remove(work_list
, cur
);
635 /* Update its liveness information */
636 bool progress
= liveness_block_update(ctx
, blk
);
638 /* If we made progress, we need to process the predecessors */
640 if (progress
|| (blk
== exit
)) {
641 mir_foreach_predecessor(blk
, pred
)
642 _mesa_set_add(work_list
, pred
);
644 } while((cur
= _mesa_set_next_entry(work_list
, NULL
)) != NULL
);
646 /* Now that every block has live_in/live_out computed, we can determine
647 * interference by walking each block linearly. Take live_out at the
648 * end of each block and walk the block backwards. */
650 mir_foreach_block(ctx
, blk
) {
651 uint8_t *live
= calloc(ctx
->temp_count
, 1);
653 mir_foreach_successor(blk
, succ
) {
654 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
)
655 live
[i
] |= succ
->live_in
[i
];
658 mir_foreach_instr_in_block_rev(blk
, ins
) {
659 /* Mark all registers live after the instruction as
660 * interfering with the destination */
662 unsigned dest
= ins
->dest
;
664 if (dest
< ctx
->temp_count
) {
665 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
)
667 ra_add_node_interference(g
, dest
, i
);
671 liveness_ins_update(live
, ins
, ctx
->temp_count
);
677 mir_foreach_block(ctx
, blk
) {
683 /* This routine performs the actual register allocation. It should be succeeded
684 * by install_registers */
687 allocate_registers(compiler_context
*ctx
, bool *spilled
)
689 /* The number of vec4 work registers available depends on when the
690 * uniforms start, so compute that first */
691 int work_count
= 16 - MAX2((ctx
->uniform_cutoff
- 8), 0);
692 unsigned *classes
= NULL
;
693 struct ra_regs
*regs
= get_register_set(ctx
->screen
, work_count
, &classes
);
695 assert(regs
!= NULL
);
696 assert(classes
!= NULL
);
698 /* No register allocation to do with no SSA */
700 if (!ctx
->temp_count
)
703 /* Let's actually do register allocation */
704 int nodes
= ctx
->temp_count
;
705 struct ra_graph
*g
= ra_alloc_interference_graph(regs
, nodes
);
707 /* Register class (as known to the Mesa register allocator) is actually
708 * the product of both semantic class (work, load/store, texture..) and
709 * size (vec2/vec3..). First, we'll go through and determine the
710 * minimum size needed to hold values */
712 unsigned *found_class
= calloc(sizeof(unsigned), ctx
->temp_count
);
714 mir_foreach_instr_global(ctx
, ins
) {
715 if (ins
->dest
>= SSA_FIXED_MINIMUM
) continue;
717 /* 0 for x, 1 for xy, 2 for xyz, 3 for xyzw */
718 int class = util_logbase2(ins
->mask
);
720 /* Use the largest class if there's ambiguity, this
721 * handles partial writes */
723 int dest
= ins
->dest
;
724 found_class
[dest
] = MAX2(found_class
[dest
], class);
727 /* Next, we'll determine semantic class. We default to zero (work).
728 * But, if we're used with a special operation, that will force us to a
729 * particular class. Each node must be assigned to exactly one class; a
730 * prepass before RA should have lowered what-would-have-been
731 * multiclass nodes into a series of moves to break it up into multiple
734 mir_foreach_instr_global(ctx
, ins
) {
735 /* Check if this operation imposes any classes */
737 if (ins
->type
== TAG_LOAD_STORE_4
) {
738 bool force_vec4_only
= OP_IS_VEC4_ONLY(ins
->load_store
.op
);
740 set_class(found_class
, ins
->src
[0], REG_CLASS_LDST
);
741 set_class(found_class
, ins
->src
[1], REG_CLASS_LDST
);
742 set_class(found_class
, ins
->src
[2], REG_CLASS_LDST
);
744 if (force_vec4_only
) {
745 force_vec4(found_class
, ins
->dest
);
746 force_vec4(found_class
, ins
->src
[0]);
747 force_vec4(found_class
, ins
->src
[1]);
748 force_vec4(found_class
, ins
->src
[2]);
750 } else if (ins
->type
== TAG_TEXTURE_4
) {
751 set_class(found_class
, ins
->dest
, REG_CLASS_TEXW
);
752 set_class(found_class
, ins
->src
[0], REG_CLASS_TEXR
);
753 set_class(found_class
, ins
->src
[1], REG_CLASS_TEXR
);
754 set_class(found_class
, ins
->src
[2], REG_CLASS_TEXR
);
758 /* Check that the semantics of the class are respected */
759 mir_foreach_instr_global(ctx
, ins
) {
760 assert(check_write_class(found_class
, ins
->type
, ins
->dest
));
761 assert(check_read_class(found_class
, ins
->type
, ins
->src
[0]));
762 assert(check_read_class(found_class
, ins
->type
, ins
->src
[1]));
763 assert(check_read_class(found_class
, ins
->type
, ins
->src
[2]));
766 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
) {
767 unsigned class = found_class
[i
];
768 ra_set_node_class(g
, i
, classes
[class]);
771 mir_compute_liveness(ctx
, g
);
773 if (!ra_allocate(g
)) {
779 /* Whether we were successful or not, report the graph so we can
780 * compute spill nodes */
785 /* Once registers have been decided via register allocation
786 * (allocate_registers), we need to rewrite the MIR to use registers instead of
790 install_registers_instr(
791 compiler_context
*ctx
,
793 midgard_instruction
*ins
)
797 struct phys_reg src1
= index_to_reg(ctx
, g
, ins
->src
[0]);
798 struct phys_reg src2
= index_to_reg(ctx
, g
, ins
->src
[1]);
799 struct phys_reg dest
= index_to_reg(ctx
, g
, ins
->dest
);
801 unsigned uncomposed_mask
= ins
->mask
;
802 ins
->mask
= compose_writemask(uncomposed_mask
, dest
);
804 /* Adjust the dest mask if necessary. Mostly this is a no-op
805 * but it matters for dot products */
806 dest
.mask
= effective_writemask(&ins
->alu
, ins
->mask
);
808 midgard_vector_alu_src mod1
=
809 vector_alu_from_unsigned(ins
->alu
.src1
);
810 mod1
.swizzle
= compose_swizzle(mod1
.swizzle
, uncomposed_mask
, src1
, dest
);
811 ins
->alu
.src1
= vector_alu_srco_unsigned(mod1
);
813 ins
->registers
.src1_reg
= src1
.reg
;
815 ins
->registers
.src2_imm
= ins
->has_inline_constant
;
817 if (ins
->has_inline_constant
) {
818 /* Encode inline 16-bit constant. See disassembler for
819 * where the algorithm is from */
821 ins
->registers
.src2_reg
= ins
->inline_constant
>> 11;
823 int lower_11
= ins
->inline_constant
& ((1 << 12) - 1);
824 uint16_t imm
= ((lower_11
>> 8) & 0x7) |
825 ((lower_11
& 0xFF) << 3);
827 ins
->alu
.src2
= imm
<< 2;
829 midgard_vector_alu_src mod2
=
830 vector_alu_from_unsigned(ins
->alu
.src2
);
831 mod2
.swizzle
= compose_swizzle(
832 mod2
.swizzle
, uncomposed_mask
, src2
, dest
);
833 ins
->alu
.src2
= vector_alu_srco_unsigned(mod2
);
835 ins
->registers
.src2_reg
= src2
.reg
;
838 ins
->registers
.out_reg
= dest
.reg
;
842 case TAG_LOAD_STORE_4
: {
843 /* Which physical register we read off depends on
844 * whether we are loading or storing -- think about the
845 * logical dataflow */
847 bool encodes_src
= OP_IS_STORE(ins
->load_store
.op
);
850 struct phys_reg src
= index_to_reg(ctx
, g
, ins
->src
[0]);
851 assert(src
.reg
== 26 || src
.reg
== 27);
853 ins
->load_store
.reg
= src
.reg
- 26;
855 unsigned shift
= __builtin_ctz(src
.mask
);
856 unsigned adjusted_mask
= src
.mask
>> shift
;
857 assert(((adjusted_mask
+ 1) & adjusted_mask
) == 0);
859 unsigned new_swizzle
= 0;
860 for (unsigned q
= 0; q
< 4; ++q
) {
861 unsigned c
= (ins
->load_store
.swizzle
>> (2*q
)) & 3;
862 new_swizzle
|= (c
+ shift
) << (2*q
);
865 ins
->load_store
.swizzle
= compose_swizzle(
866 new_swizzle
, src
.mask
,
867 default_phys_reg(0), src
);
869 struct phys_reg src
= index_to_reg(ctx
, g
, ins
->dest
);
871 ins
->load_store
.reg
= src
.reg
;
873 ins
->load_store
.swizzle
= compose_swizzle(
874 ins
->load_store
.swizzle
, 0xF,
875 default_phys_reg(0), src
);
877 ins
->mask
= compose_writemask(
881 /* We also follow up by actual arguments */
884 encodes_src
? ins
->src
[1] : ins
->src
[0];
887 encodes_src
? ins
->src
[2] : ins
->src
[1];
890 struct phys_reg src
= index_to_reg(ctx
, g
, src2
);
891 unsigned component
= __builtin_ctz(src
.mask
);
892 ins
->load_store
.arg_1
|= midgard_ldst_reg(src
.reg
, component
);
896 struct phys_reg src
= index_to_reg(ctx
, g
, src3
);
897 unsigned component
= __builtin_ctz(src
.mask
);
898 ins
->load_store
.arg_2
|= midgard_ldst_reg(src
.reg
, component
);
904 case TAG_TEXTURE_4
: {
905 /* Grab RA results */
906 struct phys_reg dest
= index_to_reg(ctx
, g
, ins
->dest
);
907 struct phys_reg coord
= index_to_reg(ctx
, g
, ins
->src
[0]);
908 struct phys_reg lod
= index_to_reg(ctx
, g
, ins
->src
[1]);
910 assert(dest
.reg
== 28 || dest
.reg
== 29);
911 assert(coord
.reg
== 28 || coord
.reg
== 29);
913 /* First, install the texture coordinate */
914 ins
->texture
.in_reg_full
= 1;
915 ins
->texture
.in_reg_upper
= 0;
916 ins
->texture
.in_reg_select
= coord
.reg
- 28;
917 ins
->texture
.in_reg_swizzle
=
918 compose_swizzle(ins
->texture
.in_reg_swizzle
, 0xF, coord
, dest
);
920 /* Next, install the destination */
921 ins
->texture
.out_full
= 1;
922 ins
->texture
.out_upper
= 0;
923 ins
->texture
.out_reg_select
= dest
.reg
- 28;
924 ins
->texture
.swizzle
=
925 compose_swizzle(ins
->texture
.swizzle
, dest
.mask
, dest
, dest
);
927 compose_writemask(ins
->mask
, dest
);
929 /* If there is a register LOD/bias, use it */
930 if (ins
->src
[1] != ~0) {
931 midgard_tex_register_select sel
= {
934 .component
= lod
.swizzle
& 3,
938 memcpy(&packed
, &sel
, sizeof(packed
));
939 ins
->texture
.bias
= packed
;
951 install_registers(compiler_context
*ctx
, struct ra_graph
*g
)
953 mir_foreach_block(ctx
, block
) {
954 mir_foreach_instr_in_block(block
, ins
) {
955 install_registers_instr(ctx
, g
, ins
);