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/u_math.h"
28 #include "util/u_memory.h"
29 #include "midgard_quirks.h"
32 /* Physical register: 0-31 */
35 /* Byte offset into the physical register: 0-15 */
38 /* Number of bytes in a component of this register */
42 /* Shift up by reg_offset and horizontally by dst_offset. */
45 offset_swizzle(unsigned *swizzle
, unsigned reg_offset
, unsigned srcsize
, unsigned dstsize
, unsigned dst_offset
)
47 unsigned out
[MIR_VEC_COMPONENTS
];
49 signed reg_comp
= reg_offset
/ srcsize
;
50 signed dst_comp
= dst_offset
/ dstsize
;
52 unsigned max_component
= (16 / srcsize
) - 1;
54 assert(reg_comp
* srcsize
== reg_offset
);
55 assert(dst_comp
* dstsize
== dst_offset
);
57 for (signed c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
) {
58 signed comp
= MAX2(c
- dst_comp
, 0);
59 out
[c
] = MIN2(swizzle
[comp
] + reg_comp
, max_component
);
62 memcpy(swizzle
, out
, sizeof(out
));
65 /* Helper to return the default phys_reg for a given register */
67 static struct phys_reg
68 default_phys_reg(int reg
, unsigned size
)
79 /* Determine which physical register, swizzle, and mask a virtual
80 * register corresponds to */
82 static struct phys_reg
83 index_to_reg(compiler_context
*ctx
, struct lcra_state
*l
, unsigned reg
, unsigned size
)
85 /* Check for special cases */
87 return default_phys_reg(REGISTER_UNUSED
, size
);
88 else if (reg
>= SSA_FIXED_MINIMUM
)
89 return default_phys_reg(SSA_REG_FROM_FIXED(reg
), size
);
91 return default_phys_reg(REGISTER_UNUSED
, size
);
94 .reg
= l
->solutions
[reg
] / 16,
95 .offset
= l
->solutions
[reg
] & 0xF,
99 /* Report that we actually use this register, and return it */
102 ctx
->work_registers
= MAX2(ctx
->work_registers
, r
.reg
);
108 set_class(unsigned *classes
, unsigned node
, unsigned class)
110 if (node
< SSA_FIXED_MINIMUM
&& class != classes
[node
]) {
111 assert(classes
[node
] == REG_CLASS_WORK
);
112 classes
[node
] = class;
116 /* Special register classes impose special constraints on who can read their
117 * values, so check that */
120 check_read_class(unsigned *classes
, unsigned tag
, unsigned node
)
122 /* Non-nodes are implicitly ok */
123 if (node
>= SSA_FIXED_MINIMUM
)
126 switch (classes
[node
]) {
128 return (tag
== TAG_LOAD_STORE_4
);
130 return (tag
== TAG_TEXTURE_4
);
132 return (tag
!= TAG_LOAD_STORE_4
);
136 unreachable("Invalid class");
141 check_write_class(unsigned *classes
, unsigned tag
, unsigned node
)
143 /* Non-nodes are implicitly ok */
144 if (node
>= SSA_FIXED_MINIMUM
)
147 switch (classes
[node
]) {
151 return (tag
== TAG_TEXTURE_4
);
154 return IS_ALU(tag
) || (tag
== TAG_LOAD_STORE_4
);
156 unreachable("Invalid class");
160 /* Prepass before RA to ensure special class restrictions are met. The idea is
161 * to create a bit field of types of instructions that read a particular index.
162 * Later, we'll add moves as appropriate and rewrite to specialize by type. */
165 mark_node_class (unsigned *bitfield
, unsigned node
)
167 if (node
< SSA_FIXED_MINIMUM
)
168 BITSET_SET(bitfield
, node
);
172 mir_lower_special_reads(compiler_context
*ctx
)
174 size_t sz
= BITSET_WORDS(ctx
->temp_count
) * sizeof(BITSET_WORD
);
176 /* Bitfields for the various types of registers we could have. aluw can
177 * be written by either ALU or load/store */
179 unsigned *alur
= calloc(sz
, 1);
180 unsigned *aluw
= calloc(sz
, 1);
181 unsigned *brar
= calloc(sz
, 1);
182 unsigned *ldst
= calloc(sz
, 1);
183 unsigned *texr
= calloc(sz
, 1);
184 unsigned *texw
= calloc(sz
, 1);
186 /* Pass #1 is analysis, a linear scan to fill out the bitfields */
188 mir_foreach_instr_global(ctx
, ins
) {
191 mark_node_class(aluw
, ins
->dest
);
192 mark_node_class(alur
, ins
->src
[0]);
193 mark_node_class(alur
, ins
->src
[1]);
194 mark_node_class(alur
, ins
->src
[2]);
196 if (ins
->compact_branch
&& ins
->writeout
)
197 mark_node_class(brar
, ins
->src
[0]);
201 case TAG_LOAD_STORE_4
:
202 mark_node_class(aluw
, ins
->dest
);
203 mark_node_class(ldst
, ins
->src
[0]);
204 mark_node_class(ldst
, ins
->src
[1]);
205 mark_node_class(ldst
, ins
->src
[2]);
209 mark_node_class(texr
, ins
->src
[0]);
210 mark_node_class(texr
, ins
->src
[1]);
211 mark_node_class(texr
, ins
->src
[2]);
212 mark_node_class(texw
, ins
->dest
);
217 /* Pass #2 is lowering now that we've analyzed all the classes.
218 * Conceptually, if an index is only marked for a single type of use,
219 * there is nothing to lower. If it is marked for different uses, we
220 * split up based on the number of types of uses. To do so, we divide
221 * into N distinct classes of use (where N>1 by definition), emit N-1
222 * moves from the index to copies of the index, and finally rewrite N-1
223 * of the types of uses to use the corresponding move */
225 unsigned spill_idx
= ctx
->temp_count
;
227 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
) {
228 bool is_alur
= BITSET_TEST(alur
, i
);
229 bool is_aluw
= BITSET_TEST(aluw
, i
);
230 bool is_brar
= BITSET_TEST(brar
, i
);
231 bool is_ldst
= BITSET_TEST(ldst
, i
);
232 bool is_texr
= BITSET_TEST(texr
, i
);
233 bool is_texw
= BITSET_TEST(texw
, i
);
235 /* Analyse to check how many distinct uses there are. ALU ops
236 * (alur) can read the results of the texture pipeline (texw)
237 * but not ldst or texr. Load/store ops (ldst) cannot read
238 * anything but load/store inputs. Texture pipeline cannot read
239 * anything but texture inputs. TODO: Simplify. */
242 (is_alur
&& (is_ldst
|| is_texr
)) ||
243 (is_ldst
&& (is_alur
|| is_texr
|| is_texw
)) ||
244 (is_texr
&& (is_alur
|| is_ldst
|| is_texw
)) ||
245 (is_texw
&& (is_aluw
|| is_ldst
|| is_texr
)) ||
246 (is_brar
&& is_texw
);
251 /* Use the index as-is as the work copy. Emit copies for
254 unsigned classes
[] = { TAG_LOAD_STORE_4
, TAG_TEXTURE_4
, TAG_TEXTURE_4
, TAG_ALU_4
};
255 bool collisions
[] = { is_ldst
, is_texr
, is_texw
&& is_aluw
, is_brar
};
257 for (unsigned j
= 0; j
< ARRAY_SIZE(collisions
); ++j
) {
258 if (!collisions
[j
]) continue;
260 /* When the hazard is from reading, we move and rewrite
261 * sources (typical case). When it's from writing, we
262 * flip the move and rewrite destinations (obscure,
263 * only from control flow -- impossible in SSA) */
265 bool hazard_write
= (j
== 2);
267 unsigned idx
= spill_idx
++;
269 midgard_instruction m
= hazard_write
?
270 v_mov(idx
, i
) : v_mov(i
, idx
);
272 /* Insert move before each read/write, depending on the
273 * hazard we're trying to account for */
275 mir_foreach_instr_global_safe(ctx
, pre_use
) {
276 if (pre_use
->type
!= classes
[j
])
280 if (pre_use
->dest
!= i
)
283 if (!mir_has_arg(pre_use
, i
))
288 midgard_instruction
*use
= mir_next_op(pre_use
);
290 mir_insert_instruction_before(ctx
, use
, m
);
291 mir_rewrite_index_dst_single(pre_use
, i
, idx
);
295 m
.mask
= mir_from_bytemask(mir_round_bytemask_up(
296 mir_bytemask_of_read_components(pre_use
, i
), 32), 32);
297 mir_insert_instruction_before(ctx
, pre_use
, m
);
298 mir_rewrite_index_src_single(pre_use
, i
, idx
);
312 /* We register allocate after scheduling, so we need to ensure instructions
313 * executing in parallel within a segment of a bundle don't clobber each
314 * other's registers. This is mostly a non-issue thanks to scheduling, but
315 * there are edge cases. In particular, after a register is written in a
316 * segment, it interferes with anything reading. */
319 mir_compute_segment_interference(
320 compiler_context
*ctx
,
321 struct lcra_state
*l
,
326 for (unsigned j
= pivot
; j
< i
; ++j
) {
327 mir_foreach_src(bun
->instructions
[j
], s
) {
328 if (bun
->instructions
[j
]->src
[s
] >= ctx
->temp_count
)
331 for (unsigned q
= pivot
; q
< i
; ++q
) {
332 if (bun
->instructions
[q
]->dest
>= ctx
->temp_count
)
335 /* See dEQP-GLES2.functional.shaders.return.output_write_in_func_dynamic_fragment */
338 if (!(bun
->instructions
[j
]->unit
== UNIT_SMUL
&& bun
->instructions
[q
]->unit
== UNIT_VLUT
))
342 unsigned mask
= mir_bytemask(bun
->instructions
[q
]);
343 unsigned rmask
= mir_bytemask_of_read_components(bun
->instructions
[j
], bun
->instructions
[j
]->src
[s
]);
344 lcra_add_node_interference(l
, bun
->instructions
[q
]->dest
, mask
, bun
->instructions
[j
]->src
[s
], rmask
);
351 mir_compute_bundle_interference(
352 compiler_context
*ctx
,
353 struct lcra_state
*l
,
356 if (!IS_ALU(bun
->tag
))
359 bool old
= bun
->instructions
[0]->unit
>= UNIT_VADD
;
362 for (unsigned i
= 1; i
< bun
->instruction_count
; ++i
) {
363 bool new = bun
->instructions
[i
]->unit
>= UNIT_VADD
;
366 mir_compute_segment_interference(ctx
, l
, bun
, 0, i
);
372 mir_compute_segment_interference(ctx
, l
, bun
, pivot
, bun
->instruction_count
);
376 mir_compute_interference(
377 compiler_context
*ctx
,
378 struct lcra_state
*l
)
380 /* First, we need liveness information to be computed per block */
381 mir_compute_liveness(ctx
);
383 /* We need to force r1.w live throughout a blend shader */
388 mir_foreach_block(ctx
, _block
) {
389 midgard_block
*block
= (midgard_block
*) _block
;
390 mir_foreach_instr_in_block_rev(block
, ins
) {
399 mir_foreach_instr_global(ctx
, ins
) {
400 if (ins
->dest
< ctx
->temp_count
)
401 lcra_add_node_interference(l
, ins
->dest
, mir_bytemask(ins
), r1w
, 0xF);
405 /* Now that every block has live_in/live_out computed, we can determine
406 * interference by walking each block linearly. Take live_out at the
407 * end of each block and walk the block backwards. */
409 mir_foreach_block(ctx
, _blk
) {
410 midgard_block
*blk
= (midgard_block
*) _blk
;
411 uint16_t *live
= mem_dup(_blk
->live_out
, ctx
->temp_count
* sizeof(uint16_t));
413 mir_foreach_instr_in_block_rev(blk
, ins
) {
414 /* Mark all registers live after the instruction as
415 * interfering with the destination */
417 unsigned dest
= ins
->dest
;
419 if (dest
< ctx
->temp_count
) {
420 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
)
422 unsigned mask
= mir_bytemask(ins
);
423 lcra_add_node_interference(l
, dest
, mask
, i
, live
[i
]);
428 mir_liveness_ins_update(live
, ins
, ctx
->temp_count
);
431 mir_foreach_bundle_in_block(blk
, bun
)
432 mir_compute_bundle_interference(ctx
, l
, bun
);
438 /* This routine performs the actual register allocation. It should be succeeded
439 * by install_registers */
441 static struct lcra_state
*
442 allocate_registers(compiler_context
*ctx
, bool *spilled
)
444 /* The number of vec4 work registers available depends on when the
445 * uniforms start, so compute that first */
446 int work_count
= 16 - MAX2((ctx
->uniform_cutoff
- 8), 0);
448 /* No register allocation to do with no SSA */
450 if (!ctx
->temp_count
)
453 struct lcra_state
*l
= lcra_alloc_equations(ctx
->temp_count
, 5);
455 /* Starts of classes, in bytes */
456 l
->class_start
[REG_CLASS_WORK
] = 16 * 0;
457 l
->class_start
[REG_CLASS_LDST
] = 16 * 26;
458 l
->class_start
[REG_CLASS_TEXR
] = 16 * 28;
459 l
->class_start
[REG_CLASS_TEXW
] = 16 * 28;
461 l
->class_size
[REG_CLASS_WORK
] = 16 * work_count
;
462 l
->class_size
[REG_CLASS_LDST
] = 16 * 2;
463 l
->class_size
[REG_CLASS_TEXR
] = 16 * 2;
464 l
->class_size
[REG_CLASS_TEXW
] = 16 * 2;
466 lcra_set_disjoint_class(l
, REG_CLASS_TEXR
, REG_CLASS_TEXW
);
468 /* To save space on T*20, we don't have real texture registers.
469 * Instead, tex inputs reuse the load/store pipeline registers, and
470 * tex outputs use work r0/r1. Note we still use TEXR/TEXW classes,
471 * noting that this handles interferences and sizes correctly. */
473 if (ctx
->quirks
& MIDGARD_INTERPIPE_REG_ALIASING
) {
474 l
->class_start
[REG_CLASS_TEXR
] = l
->class_start
[REG_CLASS_LDST
];
475 l
->class_start
[REG_CLASS_TEXW
] = l
->class_start
[REG_CLASS_WORK
];
478 unsigned *found_class
= calloc(sizeof(unsigned), ctx
->temp_count
);
479 unsigned *min_alignment
= calloc(sizeof(unsigned), ctx
->temp_count
);
480 unsigned *min_bound
= calloc(sizeof(unsigned), ctx
->temp_count
);
482 mir_foreach_instr_global(ctx
, ins
) {
483 /* Swizzles of 32-bit sources on 64-bit instructions need to be
484 * aligned to either bottom (xy) or top (zw). More general
485 * swizzle lowering should happen prior to scheduling (TODO),
486 * but once we get RA we shouldn't disrupt this further. Align
487 * sources of 64-bit instructions. */
489 if (ins
->type
== TAG_ALU_4
&& ins
->alu
.reg_mode
== midgard_reg_mode_64
) {
490 mir_foreach_src(ins
, v
) {
491 unsigned s
= ins
->src
[v
];
493 if (s
< ctx
->temp_count
)
494 min_alignment
[s
] = 3;
498 if (ins
->type
== TAG_LOAD_STORE_4
&& OP_HAS_ADDRESS(ins
->load_store
.op
)) {
499 mir_foreach_src(ins
, v
) {
500 unsigned s
= ins
->src
[v
];
501 unsigned size
= nir_alu_type_get_type_size(ins
->src_types
[v
]);
503 if (s
< ctx
->temp_count
)
504 min_alignment
[s
] = (size
== 64) ? 3 : 2;
508 if (ins
->dest
>= SSA_FIXED_MINIMUM
) continue;
510 unsigned size
= nir_alu_type_get_type_size(ins
->dest_type
);
512 /* 0 for x, 1 for xy, 2 for xyz, 3 for xyzw */
513 int comps1
= util_logbase2(ins
->mask
);
515 int bytes
= (comps1
+ 1) * (size
/ 8);
517 /* Use the largest class if there's ambiguity, this
518 * handles partial writes */
520 int dest
= ins
->dest
;
521 found_class
[dest
] = MAX2(found_class
[dest
], bytes
);
523 min_alignment
[dest
] =
524 (size
== 16) ? 1 : /* (1 << 1) = 2-byte */
525 (size
== 32) ? 2 : /* (1 << 2) = 4-byte */
526 (size
== 64) ? 3 : /* (1 << 3) = 8-byte */
529 /* We can't cross xy/zw boundaries. TODO: vec8 can */
533 /* We don't have a swizzle for the conditional and we don't
534 * want to muck with the conditional itself, so just force
535 * alignment for now */
537 if (ins
->type
== TAG_ALU_4
&& OP_IS_CSEL_V(ins
->alu
.op
)) {
538 min_alignment
[dest
] = 4; /* 1 << 4= 16-byte = vec4 */
540 /* LCRA assumes bound >= alignment */
541 min_bound
[dest
] = 16;
544 /* Since ld/st swizzles and masks are 32-bit only, we need them
545 * aligned to enable final packing */
546 if (ins
->type
== TAG_LOAD_STORE_4
)
547 min_alignment
[dest
] = MAX2(min_alignment
[dest
], 2);
550 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
) {
551 lcra_set_alignment(l
, i
, min_alignment
[i
] ? min_alignment
[i
] : 2,
552 min_bound
[i
] ? min_bound
[i
] : 16);
553 lcra_restrict_range(l
, i
, found_class
[i
]);
560 /* Next, we'll determine semantic class. We default to zero (work).
561 * But, if we're used with a special operation, that will force us to a
562 * particular class. Each node must be assigned to exactly one class; a
563 * prepass before RA should have lowered what-would-have-been
564 * multiclass nodes into a series of moves to break it up into multiple
567 mir_foreach_instr_global(ctx
, ins
) {
568 /* Check if this operation imposes any classes */
570 if (ins
->type
== TAG_LOAD_STORE_4
) {
571 set_class(l
->class, ins
->src
[0], REG_CLASS_LDST
);
572 set_class(l
->class, ins
->src
[1], REG_CLASS_LDST
);
573 set_class(l
->class, ins
->src
[2], REG_CLASS_LDST
);
575 if (OP_IS_VEC4_ONLY(ins
->load_store
.op
)) {
576 lcra_restrict_range(l
, ins
->dest
, 16);
577 lcra_restrict_range(l
, ins
->src
[0], 16);
578 lcra_restrict_range(l
, ins
->src
[1], 16);
579 lcra_restrict_range(l
, ins
->src
[2], 16);
581 } else if (ins
->type
== TAG_TEXTURE_4
) {
582 set_class(l
->class, ins
->dest
, REG_CLASS_TEXW
);
583 set_class(l
->class, ins
->src
[0], REG_CLASS_TEXR
);
584 set_class(l
->class, ins
->src
[1], REG_CLASS_TEXR
);
585 set_class(l
->class, ins
->src
[2], REG_CLASS_TEXR
);
586 set_class(l
->class, ins
->src
[3], REG_CLASS_TEXR
);
590 /* Check that the semantics of the class are respected */
591 mir_foreach_instr_global(ctx
, ins
) {
592 assert(check_write_class(l
->class, ins
->type
, ins
->dest
));
593 assert(check_read_class(l
->class, ins
->type
, ins
->src
[0]));
594 assert(check_read_class(l
->class, ins
->type
, ins
->src
[1]));
595 assert(check_read_class(l
->class, ins
->type
, ins
->src
[2]));
598 /* Mark writeout to r0, render target to r1.z, unknown to r1.w */
599 mir_foreach_instr_global(ctx
, ins
) {
600 if (!(ins
->compact_branch
&& ins
->writeout
)) continue;
602 if (ins
->src
[0] < ctx
->temp_count
) {
603 if (ins
->writeout_depth
)
604 l
->solutions
[ins
->src
[0]] = (16 * 1) + COMPONENT_X
* 4;
605 else if (ins
->writeout_stencil
)
606 l
->solutions
[ins
->src
[0]] = (16 * 1) + COMPONENT_Y
* 4;
608 l
->solutions
[ins
->src
[0]] = 0;
611 if (ins
->src
[1] < ctx
->temp_count
)
612 l
->solutions
[ins
->src
[1]] = (16 * 1) + COMPONENT_Z
* 4;
614 if (ins
->src
[2] < ctx
->temp_count
)
615 l
->solutions
[ins
->src
[2]] = (16 * 1) + COMPONENT_W
* 4;
618 mir_compute_interference(ctx
, l
);
620 *spilled
= !lcra_solve(l
);
625 /* Once registers have been decided via register allocation
626 * (allocate_registers), we need to rewrite the MIR to use registers instead of
630 install_registers_instr(
631 compiler_context
*ctx
,
632 struct lcra_state
*l
,
633 midgard_instruction
*ins
)
635 unsigned src_size
[MIR_SRC_COUNT
];
637 for (unsigned i
= 0; i
< MIR_SRC_COUNT
; ++i
)
638 src_size
[i
] = MAX2(nir_alu_type_get_type_size(ins
->src_types
[i
]) / 8, 1);
640 unsigned dest_size
= MAX2(nir_alu_type_get_type_size(ins
->dest_type
) / 8, 1);
647 if (ins
->compact_branch
)
650 struct phys_reg src1
= index_to_reg(ctx
, l
, ins
->src
[0], src_size
[0]);
651 struct phys_reg src2
= index_to_reg(ctx
, l
, ins
->src
[1], src_size
[1]);
652 struct phys_reg dest
= index_to_reg(ctx
, l
, ins
->dest
, dest_size
);
654 mir_set_bytemask(ins
, mir_bytemask(ins
) << dest
.offset
);
656 unsigned dest_offset
=
657 GET_CHANNEL_COUNT(alu_opcode_props
[ins
->alu
.op
].props
) ? 0 :
660 offset_swizzle(ins
->swizzle
[0], src1
.offset
, src1
.size
, dest
.size
, dest_offset
);
662 ins
->registers
.src1_reg
= src1
.reg
;
664 ins
->registers
.src2_imm
= ins
->has_inline_constant
;
666 if (ins
->has_inline_constant
) {
667 /* Encode inline 16-bit constant. See disassembler for
668 * where the algorithm is from */
670 ins
->registers
.src2_reg
= ins
->inline_constant
>> 11;
672 int lower_11
= ins
->inline_constant
& ((1 << 12) - 1);
673 uint16_t imm
= ((lower_11
>> 8) & 0x7) |
674 ((lower_11
& 0xFF) << 3);
676 ins
->alu
.src2
= imm
<< 2;
678 offset_swizzle(ins
->swizzle
[1], src2
.offset
, src2
.size
, dest
.size
, dest_offset
);
680 ins
->registers
.src2_reg
= src2
.reg
;
683 ins
->registers
.out_reg
= dest
.reg
;
687 case TAG_LOAD_STORE_4
: {
688 /* Which physical register we read off depends on
689 * whether we are loading or storing -- think about the
690 * logical dataflow */
692 bool encodes_src
= OP_IS_STORE(ins
->load_store
.op
);
695 struct phys_reg src
= index_to_reg(ctx
, l
, ins
->src
[0], src_size
[0]);
696 assert(src
.reg
== 26 || src
.reg
== 27);
698 ins
->load_store
.reg
= src
.reg
- 26;
699 offset_swizzle(ins
->swizzle
[0], src
.offset
, src
.size
, 1, 0);
701 struct phys_reg dst
= index_to_reg(ctx
, l
, ins
->dest
, dest_size
);
703 ins
->load_store
.reg
= dst
.reg
;
704 offset_swizzle(ins
->swizzle
[0], 0, 4, 4, dst
.offset
);
705 mir_set_bytemask(ins
, mir_bytemask(ins
) << dst
.offset
);
708 /* We also follow up by actual arguments */
710 unsigned src2
= ins
->src
[1];
711 unsigned src3
= ins
->src
[2];
714 struct phys_reg src
= index_to_reg(ctx
, l
, src2
, 4);
715 unsigned component
= src
.offset
/ src
.size
;
716 assert(component
* src
.size
== src
.offset
);
717 ins
->load_store
.arg_1
|= midgard_ldst_reg(src
.reg
, component
);
721 struct phys_reg src
= index_to_reg(ctx
, l
, src3
, 4);
722 unsigned component
= src
.offset
/ src
.size
;
723 assert(component
* src
.size
== src
.offset
);
724 ins
->load_store
.arg_2
|= midgard_ldst_reg(src
.reg
, component
);
730 case TAG_TEXTURE_4
: {
731 if (ins
->texture
.op
== TEXTURE_OP_BARRIER
)
734 /* Grab RA results */
735 struct phys_reg dest
= index_to_reg(ctx
, l
, ins
->dest
, dest_size
);
736 struct phys_reg coord
= index_to_reg(ctx
, l
, ins
->src
[1], src_size
[1]);
737 struct phys_reg lod
= index_to_reg(ctx
, l
, ins
->src
[2], src_size
[2]);
738 struct phys_reg offset
= index_to_reg(ctx
, l
, ins
->src
[3], src_size
[3]);
740 /* First, install the texture coordinate */
741 ins
->texture
.in_reg_select
= coord
.reg
& 1;
742 offset_swizzle(ins
->swizzle
[1], coord
.offset
, coord
.size
, dest
.size
, 0);
744 /* Next, install the destination */
745 ins
->texture
.out_reg_select
= dest
.reg
& 1;
746 offset_swizzle(ins
->swizzle
[0], 0, 4, dest
.size
,
747 dest_size
== 2 ? dest
.offset
% 8 :
749 mir_set_bytemask(ins
, mir_bytemask(ins
) << dest
.offset
);
751 /* If there is a register LOD/bias, use it */
752 if (ins
->src
[2] != ~0) {
753 assert(!(lod
.offset
& 3));
754 midgard_tex_register_select sel
= {
755 .select
= lod
.reg
& 1,
757 .component
= lod
.offset
/ 4
761 memcpy(&packed
, &sel
, sizeof(packed
));
762 ins
->texture
.bias
= packed
;
765 /* If there is an offset register, install it */
766 if (ins
->src
[3] != ~0) {
767 unsigned x
= offset
.offset
/ 4;
771 /* Check range, TODO: half-registers */
774 ins
->texture
.offset
=
776 (offset
.reg
& 1) << 1 | /* select */
777 (0 << 2) | /* upper */
778 (x
<< 3) | /* swizzle */
779 (y
<< 5) | /* swizzle */
780 (z
<< 7); /* swizzle */
792 install_registers(compiler_context
*ctx
, struct lcra_state
*l
)
794 mir_foreach_instr_global(ctx
, ins
)
795 install_registers_instr(ctx
, l
, ins
);
799 /* If register allocation fails, find the best spill node */
802 mir_choose_spill_node(
803 compiler_context
*ctx
,
804 struct lcra_state
*l
)
806 /* We can't spill a previously spilled value or an unspill */
808 mir_foreach_instr_global(ctx
, ins
) {
809 if (ins
->no_spill
& (1 << l
->spill_class
)) {
810 lcra_set_node_spill_cost(l
, ins
->dest
, -1);
812 if (l
->spill_class
!= REG_CLASS_WORK
) {
813 mir_foreach_src(ins
, s
)
814 lcra_set_node_spill_cost(l
, ins
->src
[s
], -1);
819 return lcra_get_best_spill_node(l
);
822 /* Once we've chosen a spill node, spill it */
826 compiler_context
*ctx
,
828 unsigned spill_class
,
829 unsigned *spill_count
)
831 unsigned spill_index
= ctx
->temp_count
;
833 /* We have a spill node, so check the class. Work registers
834 * legitimately spill to TLS, but special registers just spill to work
837 bool is_special
= spill_class
!= REG_CLASS_WORK
;
838 bool is_special_w
= spill_class
== REG_CLASS_TEXW
;
840 /* Allocate TLS slot (maybe) */
841 unsigned spill_slot
= !is_special
? (*spill_count
)++ : 0;
843 /* For TLS, replace all stores to the spilled node. For
844 * special reads, just keep as-is; the class will be demoted
845 * implicitly. For special writes, spill to a work register */
847 if (!is_special
|| is_special_w
) {
849 spill_slot
= spill_index
++;
851 mir_foreach_block(ctx
, _block
) {
852 midgard_block
*block
= (midgard_block
*) _block
;
853 mir_foreach_instr_in_block_safe(block
, ins
) {
854 if (ins
->dest
!= spill_node
) continue;
856 midgard_instruction st
;
859 st
= v_mov(spill_node
, spill_slot
);
860 st
.no_spill
|= (1 << spill_class
);
862 ins
->dest
= spill_index
++;
863 ins
->no_spill
|= (1 << spill_class
);
864 st
= v_load_store_scratch(ins
->dest
, spill_slot
, true, ins
->mask
);
867 /* Hint: don't rewrite this node */
870 mir_insert_instruction_after_scheduled(ctx
, block
, ins
, st
);
878 /* For special reads, figure out how many bytes we need */
879 unsigned read_bytemask
= 0;
881 mir_foreach_instr_global_safe(ctx
, ins
) {
882 read_bytemask
|= mir_bytemask_of_read_components(ins
, spill_node
);
885 /* Insert a load from TLS before the first consecutive
886 * use of the node, rewriting to use spilled indices to
887 * break up the live range. Or, for special, insert a
888 * move. Ironically the latter *increases* register
889 * pressure, but the two uses of the spilling mechanism
890 * are somewhat orthogonal. (special spilling is to use
891 * work registers to back special registers; TLS
892 * spilling is to use memory to back work registers) */
894 mir_foreach_block(ctx
, _block
) {
895 midgard_block
*block
= (midgard_block
*) _block
;
896 mir_foreach_instr_in_block(block
, ins
) {
897 /* We can't rewrite the moves used to spill in the
898 * first place. These moves are hinted. */
899 if (ins
->hint
) continue;
901 /* If we don't use the spilled value, nothing to do */
902 if (!mir_has_arg(ins
, spill_node
)) continue;
907 index
= ++spill_index
;
909 midgard_instruction
*before
= ins
;
910 midgard_instruction st
;
914 st
= v_mov(spill_node
, index
);
915 st
.no_spill
|= (1 << spill_class
);
918 st
= v_load_store_scratch(index
, spill_slot
, false, 0xF);
921 /* Mask the load based on the component count
922 * actually needed to prevent RA loops */
924 st
.mask
= mir_from_bytemask(mir_round_bytemask_up(
925 read_bytemask
, 32), 32);
927 mir_insert_instruction_before_scheduled(ctx
, block
, before
, st
);
929 /* Special writes already have their move spilled in */
935 mir_rewrite_index_src_single(ins
, spill_node
, index
);
944 mir_foreach_instr_global(ctx
, ins
) {
949 /* Run register allocation in a loop, spilling until we succeed */
952 mir_ra(compiler_context
*ctx
)
954 struct lcra_state
*l
= NULL
;
955 bool spilled
= false;
956 int iter_count
= 1000; /* max iterations */
958 /* Number of 128-bit slots in memory we've spilled into */
959 unsigned spill_count
= 0;
962 mir_create_pipeline_registers(ctx
);
966 signed spill_node
= mir_choose_spill_node(ctx
, l
);
968 if (spill_node
== -1) {
969 fprintf(stderr
, "ERROR: Failed to choose spill node\n");
973 mir_spill_register(ctx
, spill_node
, l
->spill_class
, &spill_count
);
976 mir_squeeze_index(ctx
);
977 mir_invalidate_liveness(ctx
);
984 l
= allocate_registers(ctx
, &spilled
);
985 } while(spilled
&& ((iter_count
--) > 0));
987 if (iter_count
<= 0) {
988 fprintf(stderr
, "panfrost: Gave up allocating registers, rendering will be incomplete\n");
992 /* Report spilling information. spill_count is in 128-bit slots (vec4 x
993 * fp32), but tls_size is in bytes, so multiply by 16 */
995 ctx
->tls_size
= spill_count
* 16;
997 install_registers(ctx
, l
);