2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
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:
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
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
25 #include "midgard_ops.h"
26 #include "util/u_memory.h"
27 #include "util/register_allocate.h"
29 /* Create a mask of accessed components from a swizzle to figure out vector
33 swizzle_to_access_mask(unsigned swizzle
)
35 unsigned component_mask
= 0;
37 for (int i
= 0; i
< 4; ++i
) {
38 unsigned c
= (swizzle
>> (2 * i
)) & 3;
39 component_mask
|= (1 << c
);
42 return component_mask
;
45 /* Does the mask cover more than a scalar? */
48 is_single_component_mask(unsigned mask
)
52 for (int c
= 0; c
< 8; ++c
) {
57 return components
== 1;
60 /* Checks for an SSA data hazard between two adjacent instructions, keeping in
61 * mind that we are a vector architecture and we can write to different
62 * components simultaneously */
65 can_run_concurrent_ssa(midgard_instruction
*first
, midgard_instruction
*second
)
67 /* Writeout has its own rules anyway */
68 if (first
->compact_branch
|| second
->compact_branch
)
71 /* Each instruction reads some registers and writes to a register. See
72 * where the first writes */
74 int source
= first
->dest
;
75 int source_mask
= first
->mask
;
77 /* As long as the second doesn't read from the first, we're okay */
78 for (unsigned i
= 0; i
< ARRAY_SIZE(second
->src
); ++i
) {
79 if (second
->src
[i
] != source
)
82 if (first
->type
!= TAG_ALU_4
)
85 /* Figure out which components we just read from */
87 int q
= (i
== 0) ? second
->alu
.src1
: second
->alu
.src2
;
88 midgard_vector_alu_src
*m
= (midgard_vector_alu_src
*) &q
;
90 /* Check if there are components in common, and fail if so */
91 if (swizzle_to_access_mask(m
->swizzle
) & source_mask
)
95 /* Otherwise, it's safe in that regard. Another data hazard is both
96 * writing to the same place, of course */
98 if (second
->dest
== source
) {
99 /* ...but only if the components overlap */
101 if (second
->mask
& source_mask
)
111 midgard_instruction
**segment
, unsigned segment_size
,
112 midgard_instruction
*ains
)
114 for (int s
= 0; s
< segment_size
; ++s
)
115 if (!can_run_concurrent_ssa(segment
[s
], ains
))
123 /* Fragment writeout (of r0) is allowed when:
125 * - All components of r0 are written in the bundle
126 * - No components of r0 are written in VLUT
127 * - Non-pipelined dependencies of r0 are not written in the bundle
129 * This function checks if these requirements are satisfied given the content
130 * of a scheduled bundle.
134 can_writeout_fragment(compiler_context
*ctx
, midgard_instruction
**bundle
, unsigned count
, unsigned node_count
)
136 /* First scan for which components of r0 are written out. Initially
137 * none are written */
139 uint8_t r0_written_mask
= 0x0;
141 /* Simultaneously we scan for the set of dependencies */
143 size_t sz
= sizeof(BITSET_WORD
) * BITSET_WORDS(node_count
);
144 BITSET_WORD
*dependencies
= alloca(sz
);
145 memset(dependencies
, 0, sz
);
147 for (unsigned i
= 0; i
< count
; ++i
) {
148 midgard_instruction
*ins
= bundle
[i
];
150 if (ins
->dest
!= SSA_FIXED_REGISTER(0))
153 /* Record written out mask */
154 r0_written_mask
|= ins
->mask
;
156 /* Record dependencies, but only if they won't become pipeline
157 * registers. We know we can't be live after this, because
158 * we're writeout at the very end of the shader. So check if
159 * they were written before us. */
161 unsigned src0
= ins
->src
[0];
162 unsigned src1
= ins
->src
[1];
164 if (!mir_is_written_before(ctx
, bundle
[0], src0
))
167 if (!mir_is_written_before(ctx
, bundle
[0], src1
))
170 if (src0
< node_count
)
171 BITSET_SET(dependencies
, src0
);
173 if (src1
< node_count
)
174 BITSET_SET(dependencies
, src1
);
177 if (ins
->unit
== UNIT_VLUT
)
182 if ((r0_written_mask
& 0xF) != 0xF)
187 for (unsigned i
= 0; i
< count
; ++i
) {
188 unsigned dest
= bundle
[i
]->dest
;
190 if (dest
< node_count
&& BITSET_TEST(dependencies
, dest
))
194 /* Otherwise, we're good to go */
198 /* Helpers for scheudling */
201 mir_is_scalar(midgard_instruction
*ains
)
203 /* Does the op support scalar units? */
204 if (!(alu_opcode_props
[ains
->alu
.op
].props
& UNITS_SCALAR
))
207 /* Do we try to use it as a vector op? */
208 if (!is_single_component_mask(ains
->mask
))
211 /* Otherwise, check mode hazards */
212 bool could_scalar
= true;
214 /* Only 16/32-bit can run on a scalar unit */
215 could_scalar
&= ains
->alu
.reg_mode
!= midgard_reg_mode_8
;
216 could_scalar
&= ains
->alu
.reg_mode
!= midgard_reg_mode_64
;
217 could_scalar
&= ains
->alu
.dest_override
== midgard_dest_override_none
;
219 if (ains
->alu
.reg_mode
== midgard_reg_mode_16
) {
220 /* If we're running in 16-bit mode, we
221 * can't have any 8-bit sources on the
222 * scalar unit (since the scalar unit
223 * doesn't understand 8-bit) */
225 midgard_vector_alu_src s1
=
226 vector_alu_from_unsigned(ains
->alu
.src1
);
228 could_scalar
&= !s1
.half
;
230 midgard_vector_alu_src s2
=
231 vector_alu_from_unsigned(ains
->alu
.src2
);
233 could_scalar
&= !s2
.half
;
239 /* How many bytes does this ALU instruction add to the bundle? */
242 bytes_for_instruction(midgard_instruction
*ains
)
244 if (ains
->unit
& UNITS_ANY_VECTOR
)
245 return sizeof(midgard_reg_info
) + sizeof(midgard_vector_alu
);
246 else if (ains
->unit
== ALU_ENAB_BRANCH
)
247 return sizeof(midgard_branch_extended
);
248 else if (ains
->compact_branch
)
249 return sizeof(ains
->br_compact
);
251 return sizeof(midgard_reg_info
) + sizeof(midgard_scalar_alu
);
254 /* Schedules, but does not emit, a single basic block. After scheduling, the
255 * final tag and size of the block are known, which are necessary for branching
258 static midgard_bundle
259 schedule_bundle(compiler_context
*ctx
, midgard_block
*block
, midgard_instruction
*ins
, int *skip
)
261 int instructions_emitted
= 0, packed_idx
= 0;
262 midgard_bundle bundle
= { 0 };
264 midgard_instruction
*scheduled
[5] = { NULL
};
266 uint8_t tag
= ins
->type
;
268 /* Default to the instruction's tag */
273 uint32_t control
= 0;
274 size_t bytes_emitted
= sizeof(control
);
276 /* TODO: Constant combining */
277 int index
= 0, last_unit
= 0;
279 /* Previous instructions, for the purpose of parallelism */
280 midgard_instruction
*segment
[4] = {0};
281 int segment_size
= 0;
283 instructions_emitted
= -1;
284 midgard_instruction
*pins
= ins
;
286 unsigned constant_count
= 0;
289 midgard_instruction
*ains
= pins
;
291 /* Advance instruction pointer */
293 ains
= mir_next_op(pins
);
297 /* Out-of-work condition */
298 if ((struct list_head
*) ains
== &block
->instructions
)
301 /* Ensure that the chain can continue */
302 if (ains
->type
!= TAG_ALU_4
) break;
304 /* If there's already something in the bundle and we
305 * have weird scheduler constraints, break now */
306 if (ains
->precede_break
&& index
) break;
308 /* According to the presentation "The ARM
309 * Mali-T880 Mobile GPU" from HotChips 27,
310 * there are two pipeline stages. Branching
311 * position determined experimentally. Lines
312 * are executed in parallel:
315 * [ VADD ] [ SMUL ] [ LUT ] [ BRANCH ]
317 * Verify that there are no ordering dependencies here.
319 * TODO: Allow for parallelism!!!
322 /* Pick a unit for it if it doesn't force a particular unit */
324 int unit
= ains
->unit
;
327 int op
= ains
->alu
.op
;
328 int units
= alu_opcode_props
[op
].props
;
329 bool scalar
= mir_is_scalar(ains
);
332 if (last_unit
>= UNIT_VADD
) {
333 if (units
& UNIT_VLUT
)
338 if ((units
& UNIT_VMUL
) && last_unit
< UNIT_VMUL
)
340 else if ((units
& UNIT_VADD
) && !(control
& UNIT_VADD
))
342 else if (units
& UNIT_VLUT
)
348 if (last_unit
>= UNIT_VADD
) {
349 if ((units
& UNIT_SMUL
) && !(control
& UNIT_SMUL
))
351 else if (units
& UNIT_VLUT
)
356 if ((units
& UNIT_VMUL
) && (last_unit
< UNIT_VMUL
))
358 else if ((units
& UNIT_SADD
) && !(control
& UNIT_SADD
) && !midgard_has_hazard(segment
, segment_size
, ains
))
360 else if (units
& UNIT_VADD
)
362 else if (units
& UNIT_SMUL
)
364 else if (units
& UNIT_VLUT
)
371 assert(unit
& units
);
374 /* Late unit check, this time for encoding (not parallelism) */
375 if (unit
<= last_unit
) break;
377 /* Clear the segment */
378 if (last_unit
< UNIT_VADD
&& unit
>= UNIT_VADD
)
381 if (midgard_has_hazard(segment
, segment_size
, ains
))
384 /* We're good to go -- emit the instruction */
387 segment
[segment_size
++] = ains
;
389 /* We try to reuse constants if possible, by adjusting
392 if (ains
->has_blend_constant
) {
393 /* Everything conflicts with the blend constant */
394 if (bundle
.has_embedded_constants
)
397 bundle
.has_blend_constant
= 1;
398 bundle
.has_embedded_constants
= 1;
399 } else if (ains
->has_constants
&& ains
->alu
.reg_mode
== midgard_reg_mode_16
) {
400 /* TODO: DRY with the analysis pass */
402 if (bundle
.has_blend_constant
)
408 /* TODO: Fix packing XXX */
409 uint16_t *bundles
= (uint16_t *) bundle
.constants
;
410 uint32_t *constants
= (uint32_t *) ains
->constants
;
412 /* Copy them wholesale */
413 for (unsigned i
= 0; i
< 4; ++i
)
414 bundles
[i
] = constants
[i
];
416 bundle
.has_embedded_constants
= true;
418 } else if (ains
->has_constants
) {
419 /* By definition, blend constants conflict with
420 * everything, so if there are already
421 * constants we break the bundle *now* */
423 if (bundle
.has_blend_constant
)
426 /* For anything but blend constants, we can do
427 * proper analysis, however */
429 /* TODO: Mask by which are used */
430 uint32_t *constants
= (uint32_t *) ains
->constants
;
431 uint32_t *bundles
= (uint32_t *) bundle
.constants
;
433 uint32_t indices
[4] = { 0 };
434 bool break_bundle
= false;
436 for (unsigned i
= 0; i
< 4; ++i
) {
437 uint32_t cons
= constants
[i
];
438 bool constant_found
= false;
440 /* Search for the constant */
441 for (unsigned j
= 0; j
< constant_count
; ++j
) {
442 if (bundles
[j
] != cons
)
445 /* We found it, reuse */
447 constant_found
= true;
454 /* We didn't find it, so allocate it */
455 unsigned idx
= constant_count
++;
458 /* Uh-oh, out of space */
463 /* We have space, copy it in! */
471 /* Cool, we have it in. So use indices as a
474 unsigned swizzle
= SWIZZLE_FROM_ARRAY(indices
);
475 unsigned r_constant
= SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
477 if (ains
->src
[0] == r_constant
)
478 ains
->alu
.src1
= vector_alu_apply_swizzle(ains
->alu
.src1
, swizzle
);
480 if (ains
->src
[1] == r_constant
)
481 ains
->alu
.src2
= vector_alu_apply_swizzle(ains
->alu
.src2
, swizzle
);
483 bundle
.has_embedded_constants
= true;
486 if (ains
->compact_branch
) {
487 /* All of r0 has to be written out along with
488 * the branch writeout */
490 if (ains
->writeout
&& !can_writeout_fragment(ctx
, scheduled
, index
, ctx
->temp_count
)) {
491 /* We only work on full moves
492 * at the beginning. We could
493 * probably do better */
498 midgard_instruction ins
= v_mov(0, blank_alu_src
, SSA_FIXED_REGISTER(0));
499 ins
.unit
= UNIT_VMUL
;
502 /* TODO don't leak */
503 midgard_instruction
*move
=
504 mem_dup(&ins
, sizeof(midgard_instruction
));
505 bytes_emitted
+= bytes_for_instruction(move
);
506 bundle
.instructions
[packed_idx
++] = move
;
510 bytes_emitted
+= bytes_for_instruction(ains
);
512 /* Defer marking until after writing to allow for break */
513 scheduled
[index
] = ains
;
514 control
|= ains
->unit
;
515 last_unit
= ains
->unit
;
516 ++instructions_emitted
;
522 /* Pad ALU op to nearest word */
524 if (bytes_emitted
& 15) {
525 padding
= 16 - (bytes_emitted
& 15);
526 bytes_emitted
+= padding
;
529 /* Constants must always be quadwords */
530 if (bundle
.has_embedded_constants
)
533 /* Size ALU instruction for tag */
534 bundle
.tag
= (TAG_ALU_4
) + (bytes_emitted
/ 16) - 1;
535 bundle
.padding
= padding
;
536 bundle
.control
= bundle
.tag
| control
;
541 case TAG_LOAD_STORE_4
: {
542 /* Load store instructions have two words at once. If
543 * we only have one queued up, we need to NOP pad.
544 * Otherwise, we store both in succession to save space
545 * and cycles -- letting them go in parallel -- skip
546 * the next. The usefulness of this optimisation is
547 * greatly dependent on the quality of the instruction
551 midgard_instruction
*next_op
= mir_next_op(ins
);
553 if ((struct list_head
*) next_op
!= &block
->instructions
&& next_op
->type
== TAG_LOAD_STORE_4
) {
554 /* TODO: Concurrency check */
555 instructions_emitted
++;
561 case TAG_TEXTURE_4
: {
562 /* Which tag we use depends on the shader stage */
563 bool in_frag
= ctx
->stage
== MESA_SHADER_FRAGMENT
;
564 bundle
.tag
= in_frag
? TAG_TEXTURE_4
: TAG_TEXTURE_4_VTX
;
569 unreachable("Unknown tag");
573 /* Copy the instructions into the bundle */
574 bundle
.instruction_count
= instructions_emitted
+ 1 + packed_idx
;
576 midgard_instruction
*uins
= ins
;
577 for (; packed_idx
< bundle
.instruction_count
; ++packed_idx
) {
578 assert(&uins
->link
!= &block
->instructions
);
579 bundle
.instructions
[packed_idx
] = uins
;
580 uins
= mir_next_op(uins
);
583 *skip
= instructions_emitted
;
588 /* Schedule a single block by iterating its instruction to create bundles.
589 * While we go, tally about the bundle sizes to compute the block size. */
592 schedule_block(compiler_context
*ctx
, midgard_block
*block
)
594 util_dynarray_init(&block
->bundles
, NULL
);
596 block
->quadword_count
= 0;
598 mir_foreach_instr_in_block(block
, ins
) {
600 midgard_bundle bundle
= schedule_bundle(ctx
, block
, ins
, &skip
);
601 util_dynarray_append(&block
->bundles
, midgard_bundle
, bundle
);
603 if (bundle
.has_blend_constant
) {
604 /* TODO: Multiblock? */
605 int quadwords_within_block
= block
->quadword_count
+ quadword_size(bundle
.tag
) - 1;
606 ctx
->blend_constant_offset
= quadwords_within_block
* 0x10;
610 ins
= mir_next_op(ins
);
612 block
->quadword_count
+= quadword_size(bundle
.tag
);
615 block
->is_scheduled
= true;
618 /* The following passes reorder MIR instructions to enable better scheduling */
621 midgard_pair_load_store(compiler_context
*ctx
, midgard_block
*block
)
623 mir_foreach_instr_in_block_safe(block
, ins
) {
624 if (ins
->type
!= TAG_LOAD_STORE_4
) continue;
626 /* We've found a load/store op. Check if next is also load/store. */
627 midgard_instruction
*next_op
= mir_next_op(ins
);
628 if (&next_op
->link
!= &block
->instructions
) {
629 if (next_op
->type
== TAG_LOAD_STORE_4
) {
630 /* If so, we're done since we're a pair */
631 ins
= mir_next_op(ins
);
635 /* Maximum search distance to pair, to avoid register pressure disasters */
636 int search_distance
= 8;
638 /* Otherwise, we have an orphaned load/store -- search for another load */
639 mir_foreach_instr_in_block_from(block
, c
, mir_next_op(ins
)) {
640 /* Terminate search if necessary */
641 if (!(search_distance
--)) break;
643 if (c
->type
!= TAG_LOAD_STORE_4
) continue;
645 /* We can only reorder if there are no sources */
649 for (unsigned s
= 0; s
< ARRAY_SIZE(ins
->src
); ++s
)
650 deps
|= (c
->src
[s
] != ~0);
655 /* We found one! Move it up to pair and remove it from the old location */
657 mir_insert_instruction_before(ctx
, ins
, *c
);
658 mir_remove_instruction(c
);
666 /* When we're 'squeezing down' the values in the IR, we maintain a hash
670 find_or_allocate_temp(compiler_context
*ctx
, unsigned hash
)
672 if (hash
>= SSA_FIXED_MINIMUM
)
675 unsigned temp
= (uintptr_t) _mesa_hash_table_u64_search(
676 ctx
->hash_to_temp
, hash
+ 1);
681 /* If no temp is find, allocate one */
682 temp
= ctx
->temp_count
++;
683 ctx
->max_hash
= MAX2(ctx
->max_hash
, hash
);
685 _mesa_hash_table_u64_insert(ctx
->hash_to_temp
,
686 hash
+ 1, (void *) ((uintptr_t) temp
+ 1));
691 /* Reassigns numbering to get rid of gaps in the indices */
694 mir_squeeze_index(compiler_context
*ctx
)
698 /* TODO don't leak old hash_to_temp */
699 ctx
->hash_to_temp
= _mesa_hash_table_u64_create(NULL
);
701 mir_foreach_instr_global(ctx
, ins
) {
702 ins
->dest
= find_or_allocate_temp(ctx
, ins
->dest
);
704 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
->src
); ++i
)
705 ins
->src
[i
] = find_or_allocate_temp(ctx
, ins
->src
[i
]);
709 static midgard_instruction
710 v_load_store_scratch(
716 /* We index by 32-bit vec4s */
717 unsigned byte
= (index
* 4 * 4);
719 midgard_instruction ins
= {
720 .type
= TAG_LOAD_STORE_4
,
723 .src
= { ~0, ~0, ~0 },
725 .op
= is_store
? midgard_op_st_int4
: midgard_op_ld_int4
,
726 .swizzle
= SWIZZLE_XYZW
,
728 /* For register spilling - to thread local storage */
732 /* Splattered across, TODO combine logically */
733 .varying_parameters
= (byte
& 0x1FF) << 1,
734 .address
= (byte
>> 9)
737 /* If we spill an unspill, RA goes into an infinite loop */
742 /* r0 = r26, r1 = r27 */
743 assert(srcdest
== SSA_FIXED_REGISTER(26) || srcdest
== SSA_FIXED_REGISTER(27));
744 ins
.src
[0] = srcdest
;
752 /* If register allocation fails, find the best spill node and spill it to fix
753 * whatever the issue was. This spill node could be a work register (spilling
754 * to thread local storage), but it could also simply be a special register
755 * that needs to spill to become a work register. */
757 static void mir_spill_register(
758 compiler_context
*ctx
,
760 unsigned *spill_count
)
762 unsigned spill_index
= ctx
->temp_count
;
764 /* Our first step is to calculate spill cost to figure out the best
765 * spill node. All nodes are equal in spill cost, but we can't spill
766 * nodes written to from an unspill */
768 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
) {
769 ra_set_node_spill_cost(g
, i
, 1.0);
772 mir_foreach_instr_global(ctx
, ins
) {
775 ins
->dest
< ctx
->temp_count
)
776 ra_set_node_spill_cost(g
, ins
->dest
, -1.0);
779 int spill_node
= ra_get_best_spill_node(g
);
781 if (spill_node
< 0) {
782 mir_print_shader(ctx
);
786 /* We have a spill node, so check the class. Work registers
787 * legitimately spill to TLS, but special registers just spill to work
790 unsigned class = ra_get_node_class(g
, spill_node
);
791 bool is_special
= (class >> 2) != REG_CLASS_WORK
;
792 bool is_special_w
= (class >> 2) == REG_CLASS_TEXW
;
794 /* Allocate TLS slot (maybe) */
795 unsigned spill_slot
= !is_special
? (*spill_count
)++ : 0;
797 /* For TLS, replace all stores to the spilled node. For
798 * special reads, just keep as-is; the class will be demoted
799 * implicitly. For special writes, spill to a work register */
801 if (!is_special
|| is_special_w
) {
803 spill_slot
= spill_index
++;
805 mir_foreach_instr_global_safe(ctx
, ins
) {
806 if (ins
->dest
!= spill_node
) continue;
808 midgard_instruction st
;
811 st
= v_mov(spill_node
, blank_alu_src
, spill_slot
);
814 ins
->dest
= SSA_FIXED_REGISTER(26);
815 st
= v_load_store_scratch(ins
->dest
, spill_slot
, true, ins
->mask
);
818 /* Hint: don't rewrite this node */
821 mir_insert_instruction_before(ctx
, mir_next_op(ins
), st
);
828 /* For special reads, figure out how many components we need */
829 unsigned read_mask
= 0;
831 mir_foreach_instr_global_safe(ctx
, ins
) {
832 read_mask
|= mir_mask_of_read_components(ins
, spill_node
);
835 /* Insert a load from TLS before the first consecutive
836 * use of the node, rewriting to use spilled indices to
837 * break up the live range. Or, for special, insert a
838 * move. Ironically the latter *increases* register
839 * pressure, but the two uses of the spilling mechanism
840 * are somewhat orthogonal. (special spilling is to use
841 * work registers to back special registers; TLS
842 * spilling is to use memory to back work registers) */
844 mir_foreach_block(ctx
, block
) {
845 bool consecutive_skip
= false;
846 unsigned consecutive_index
= 0;
848 mir_foreach_instr_in_block(block
, ins
) {
849 /* We can't rewrite the moves used to spill in the
850 * first place. These moves are hinted. */
851 if (ins
->hint
) continue;
853 if (!mir_has_arg(ins
, spill_node
)) {
854 consecutive_skip
= false;
858 if (consecutive_skip
) {
860 mir_rewrite_index_src_single(ins
, spill_node
, consecutive_index
);
865 consecutive_index
= ++spill_index
;
867 midgard_instruction
*before
= ins
;
869 /* For a csel, go back one more not to break up the bundle */
870 if (ins
->type
== TAG_ALU_4
&& OP_IS_CSEL(ins
->alu
.op
))
871 before
= mir_prev_op(before
);
873 midgard_instruction st
;
877 st
= v_mov(spill_node
, blank_alu_src
, consecutive_index
);
881 st
= v_load_store_scratch(consecutive_index
, spill_slot
, false, 0xF);
884 /* Mask the load based on the component count
885 * actually needed to prvent RA loops */
889 mir_insert_instruction_before(ctx
, before
, st
);
890 // consecutive_skip = true;
892 /* Special writes already have their move spilled in */
893 consecutive_index
= spill_slot
;
898 mir_rewrite_index_src_single(ins
, spill_node
, consecutive_index
);
907 mir_foreach_instr_global(ctx
, ins
) {
913 schedule_program(compiler_context
*ctx
)
915 struct ra_graph
*g
= NULL
;
916 bool spilled
= false;
917 int iter_count
= 1000; /* max iterations */
919 /* Number of 128-bit slots in memory we've spilled into */
920 unsigned spill_count
= 0;
922 midgard_promote_uniforms(ctx
, 16);
924 mir_foreach_block(ctx
, block
) {
925 midgard_pair_load_store(ctx
, block
);
928 /* Must be lowered right before RA */
929 mir_squeeze_index(ctx
);
930 mir_lower_special_reads(ctx
);
932 /* Lowering can introduce some dead moves */
934 mir_foreach_block(ctx
, block
) {
935 midgard_opt_dead_move_eliminate(ctx
, block
);
940 mir_spill_register(ctx
, g
, &spill_count
);
942 mir_squeeze_index(ctx
);
945 g
= allocate_registers(ctx
, &spilled
);
946 } while(spilled
&& ((iter_count
--) > 0));
948 /* We can simplify a bit after RA */
950 mir_foreach_block(ctx
, block
) {
951 midgard_opt_post_move_eliminate(ctx
, block
, g
);
954 /* After RA finishes, we schedule all at once */
956 mir_foreach_block(ctx
, block
) {
957 schedule_block(ctx
, block
);
960 /* Finally, we create pipeline registers as a peephole pass after
961 * scheduling. This isn't totally optimal, since there are cases where
962 * the usage of pipeline registers can eliminate spills, but it does
965 mir_create_pipeline_registers(ctx
);
967 if (iter_count
<= 0) {
968 fprintf(stderr
, "panfrost: Gave up allocating registers, rendering will be incomplete\n");
972 /* Report spilling information. spill_count is in 128-bit slots (vec4 x
973 * fp32), but tls_size is in bytes, so multiply by 16 */
975 ctx
->tls_size
= spill_count
* 16;
977 install_registers(ctx
, g
);