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 /* Scheduling for Midgard is complicated, to say the least. ALU instructions
30 * must be grouped into VLIW bundles according to following model:
33 * [VADD] [SMUL] [VLUT]
35 * A given instruction can execute on some subset of the units (or a few can
36 * execute on all). Instructions can be either vector or scalar; only scalar
37 * instructions can execute on SADD/SMUL units. Units on a given line execute
38 * in parallel. Subsequent lines execute separately and can pass results
39 * directly via pipeline registers r24/r25, bypassing the register file.
41 * A bundle can optionally have 128-bits of embedded constants, shared across
42 * all of the instructions within a bundle.
44 * Instructions consuming conditionals (branches and conditional selects)
45 * require their condition to be written into the conditional register (r31)
46 * within the same bundle they are consumed.
48 * Fragment writeout requires its argument to be written in full within the
49 * same bundle as the branch, with no hanging dependencies.
51 * Load/store instructions are also in bundles of simply two instructions, and
52 * texture instructions have no bundling.
54 * -------------------------------------------------------------------------
58 /* We create the dependency graph with per-component granularity */
60 #define COMPONENT_COUNT 8
63 add_dependency(struct util_dynarray
*table
, unsigned index
, unsigned mask
, midgard_instruction
**instructions
, unsigned child
)
65 for (unsigned i
= 0; i
< COMPONENT_COUNT
; ++i
) {
66 if (!(mask
& (1 << i
)))
69 struct util_dynarray
*parents
= &table
[(COMPONENT_COUNT
* index
) + i
];
71 util_dynarray_foreach(parents
, unsigned, parent
) {
72 BITSET_WORD
*dependents
= instructions
[*parent
]->dependents
;
74 /* Already have the dependency */
75 if (BITSET_TEST(dependents
, child
))
78 BITSET_SET(dependents
, child
);
79 instructions
[child
]->nr_dependencies
++;
85 mark_access(struct util_dynarray
*table
, unsigned index
, unsigned mask
, unsigned parent
)
87 for (unsigned i
= 0; i
< COMPONENT_COUNT
; ++i
) {
88 if (!(mask
& (1 << i
)))
91 util_dynarray_append(&table
[(COMPONENT_COUNT
* index
) + i
], unsigned, parent
);
96 mir_create_dependency_graph(midgard_instruction
**instructions
, unsigned count
, unsigned node_count
)
98 size_t sz
= node_count
* COMPONENT_COUNT
;
100 struct util_dynarray
*last_read
= calloc(sizeof(struct util_dynarray
), sz
);
101 struct util_dynarray
*last_write
= calloc(sizeof(struct util_dynarray
), sz
);
103 for (unsigned i
= 0; i
< sz
; ++i
) {
104 util_dynarray_init(&last_read
[i
], NULL
);
105 util_dynarray_init(&last_write
[i
], NULL
);
108 /* Initialize dependency graph */
109 for (unsigned i
= 0; i
< count
; ++i
) {
110 instructions
[i
]->dependents
=
111 calloc(BITSET_WORDS(count
), sizeof(BITSET_WORD
));
113 instructions
[i
]->nr_dependencies
= 0;
116 /* Populate dependency graph */
117 for (signed i
= count
- 1; i
>= 0; --i
) {
118 if (instructions
[i
]->compact_branch
)
121 unsigned dest
= instructions
[i
]->dest
;
122 unsigned mask
= instructions
[i
]->mask
;
124 mir_foreach_src((*instructions
), s
) {
125 unsigned src
= instructions
[i
]->src
[s
];
127 if (src
< node_count
) {
128 unsigned readmask
= mir_mask_of_read_components(instructions
[i
], src
);
129 add_dependency(last_write
, src
, readmask
, instructions
, i
);
133 if (dest
< node_count
) {
134 add_dependency(last_read
, dest
, mask
, instructions
, i
);
135 add_dependency(last_write
, dest
, mask
, instructions
, i
);
136 mark_access(last_write
, dest
, mask
, i
);
139 mir_foreach_src((*instructions
), s
) {
140 unsigned src
= instructions
[i
]->src
[s
];
142 if (src
< node_count
) {
143 unsigned readmask
= mir_mask_of_read_components(instructions
[i
], src
);
144 mark_access(last_read
, src
, readmask
, i
);
149 /* If there is a branch, all instructions depend on it, as interblock
150 * execution must be purely in-order */
152 if (instructions
[count
- 1]->compact_branch
) {
153 BITSET_WORD
*dependents
= instructions
[count
- 1]->dependents
;
155 for (signed i
= count
- 2; i
>= 0; --i
) {
156 if (BITSET_TEST(dependents
, i
))
159 BITSET_SET(dependents
, i
);
160 instructions
[i
]->nr_dependencies
++;
164 /* Free the intermediate structures */
165 for (unsigned i
= 0; i
< sz
; ++i
) {
166 util_dynarray_fini(&last_read
[i
]);
167 util_dynarray_fini(&last_write
[i
]);
171 /* Create a mask of accessed components from a swizzle to figure out vector
175 swizzle_to_access_mask(unsigned swizzle
)
177 unsigned component_mask
= 0;
179 for (int i
= 0; i
< 4; ++i
) {
180 unsigned c
= (swizzle
>> (2 * i
)) & 3;
181 component_mask
|= (1 << c
);
184 return component_mask
;
187 /* Does the mask cover more than a scalar? */
190 is_single_component_mask(unsigned mask
)
194 for (int c
= 0; c
< 8; ++c
) {
199 return components
== 1;
202 /* Checks for an SSA data hazard between two adjacent instructions, keeping in
203 * mind that we are a vector architecture and we can write to different
204 * components simultaneously */
207 can_run_concurrent_ssa(midgard_instruction
*first
, midgard_instruction
*second
)
209 /* Writeout has its own rules anyway */
210 if (first
->compact_branch
|| second
->compact_branch
)
213 /* Each instruction reads some registers and writes to a register. See
214 * where the first writes */
216 int source
= first
->dest
;
217 int source_mask
= first
->mask
;
219 /* As long as the second doesn't read from the first, we're okay */
220 for (unsigned i
= 0; i
< ARRAY_SIZE(second
->src
); ++i
) {
221 if (second
->src
[i
] != source
)
224 if (first
->type
!= TAG_ALU_4
)
227 /* Figure out which components we just read from */
229 int q
= (i
== 0) ? second
->alu
.src1
: second
->alu
.src2
;
230 midgard_vector_alu_src
*m
= (midgard_vector_alu_src
*) &q
;
232 /* Check if there are components in common, and fail if so */
233 if (swizzle_to_access_mask(m
->swizzle
) & source_mask
)
237 /* Otherwise, it's safe in that regard. Another data hazard is both
238 * writing to the same place, of course */
240 if (second
->dest
== source
) {
241 /* ...but only if the components overlap */
243 if (second
->mask
& source_mask
)
253 midgard_instruction
**segment
, unsigned segment_size
,
254 midgard_instruction
*ains
)
256 for (int s
= 0; s
< segment_size
; ++s
)
257 if (!can_run_concurrent_ssa(segment
[s
], ains
))
265 /* Fragment writeout (of r0) is allowed when:
267 * - All components of r0 are written in the bundle
268 * - No components of r0 are written in VLUT
269 * - Non-pipelined dependencies of r0 are not written in the bundle
271 * This function checks if these requirements are satisfied given the content
272 * of a scheduled bundle.
276 can_writeout_fragment(compiler_context
*ctx
, midgard_instruction
**bundle
, unsigned count
, unsigned node_count
, unsigned r0
)
278 /* First scan for which components of r0 are written out. Initially
279 * none are written */
281 uint8_t r0_written_mask
= 0x0;
283 /* Simultaneously we scan for the set of dependencies */
285 size_t sz
= sizeof(BITSET_WORD
) * BITSET_WORDS(node_count
);
286 BITSET_WORD
*dependencies
= calloc(1, sz
);
287 memset(dependencies
, 0, sz
);
289 bool success
= false;
291 for (unsigned i
= 0; i
< count
; ++i
) {
292 midgard_instruction
*ins
= bundle
[i
];
297 /* Record written out mask */
298 r0_written_mask
|= ins
->mask
;
300 /* Record dependencies, but only if they won't become pipeline
301 * registers. We know we can't be live after this, because
302 * we're writeout at the very end of the shader. So check if
303 * they were written before us. */
305 unsigned src0
= ins
->src
[0];
306 unsigned src1
= ins
->src
[1];
308 if (!mir_is_written_before(ctx
, bundle
[0], src0
))
311 if (!mir_is_written_before(ctx
, bundle
[0], src1
))
314 if (src0
< node_count
)
315 BITSET_SET(dependencies
, src0
);
317 if (src1
< node_count
)
318 BITSET_SET(dependencies
, src1
);
321 if (ins
->unit
== UNIT_VLUT
)
326 if ((r0_written_mask
& 0xF) != 0xF)
331 for (unsigned i
= 0; i
< count
; ++i
) {
332 unsigned dest
= bundle
[i
]->dest
;
334 if (dest
< node_count
&& BITSET_TEST(dependencies
, dest
))
338 /* Otherwise, we're good to go */
346 /* Helpers for scheudling */
349 mir_is_scalar(midgard_instruction
*ains
)
351 /* Does the op support scalar units? */
352 if (!(alu_opcode_props
[ains
->alu
.op
].props
& UNITS_SCALAR
))
355 /* Do we try to use it as a vector op? */
356 if (!is_single_component_mask(ains
->mask
))
359 /* Otherwise, check mode hazards */
360 bool could_scalar
= true;
362 /* Only 16/32-bit can run on a scalar unit */
363 could_scalar
&= ains
->alu
.reg_mode
!= midgard_reg_mode_8
;
364 could_scalar
&= ains
->alu
.reg_mode
!= midgard_reg_mode_64
;
365 could_scalar
&= ains
->alu
.dest_override
== midgard_dest_override_none
;
367 if (ains
->alu
.reg_mode
== midgard_reg_mode_16
) {
368 /* If we're running in 16-bit mode, we
369 * can't have any 8-bit sources on the
370 * scalar unit (since the scalar unit
371 * doesn't understand 8-bit) */
373 midgard_vector_alu_src s1
=
374 vector_alu_from_unsigned(ains
->alu
.src1
);
376 could_scalar
&= !s1
.half
;
378 midgard_vector_alu_src s2
=
379 vector_alu_from_unsigned(ains
->alu
.src2
);
381 could_scalar
&= !s2
.half
;
387 /* How many bytes does this ALU instruction add to the bundle? */
390 bytes_for_instruction(midgard_instruction
*ains
)
392 if (ains
->unit
& UNITS_ANY_VECTOR
)
393 return sizeof(midgard_reg_info
) + sizeof(midgard_vector_alu
);
394 else if (ains
->unit
== ALU_ENAB_BRANCH
)
395 return sizeof(midgard_branch_extended
);
396 else if (ains
->compact_branch
)
397 return sizeof(ains
->br_compact
);
399 return sizeof(midgard_reg_info
) + sizeof(midgard_scalar_alu
);
402 /* Schedules, but does not emit, a single basic block. After scheduling, the
403 * final tag and size of the block are known, which are necessary for branching
406 static midgard_bundle
407 schedule_bundle(compiler_context
*ctx
, midgard_block
*block
, midgard_instruction
*ins
, int *skip
)
409 int instructions_emitted
= 0, packed_idx
= 0;
410 midgard_bundle bundle
= { 0 };
412 midgard_instruction
*scheduled
[5] = { NULL
};
414 uint8_t tag
= ins
->type
;
416 /* Default to the instruction's tag */
421 uint32_t control
= 0;
422 size_t bytes_emitted
= sizeof(control
);
424 /* TODO: Constant combining */
425 int index
= 0, last_unit
= 0;
427 /* Previous instructions, for the purpose of parallelism */
428 midgard_instruction
*segment
[4] = {0};
429 int segment_size
= 0;
431 instructions_emitted
= -1;
432 midgard_instruction
*pins
= ins
;
434 unsigned constant_count
= 0;
437 midgard_instruction
*ains
= pins
;
439 /* Advance instruction pointer */
441 ains
= mir_next_op(pins
);
445 /* Out-of-work condition */
446 if ((struct list_head
*) ains
== &block
->instructions
)
449 /* Ensure that the chain can continue */
450 if (ains
->type
!= TAG_ALU_4
) break;
452 /* If there's already something in the bundle and we
453 * have weird scheduler constraints, break now */
454 if (ains
->precede_break
&& index
) break;
456 /* According to the presentation "The ARM
457 * Mali-T880 Mobile GPU" from HotChips 27,
458 * there are two pipeline stages. Branching
459 * position determined experimentally. Lines
460 * are executed in parallel:
463 * [ VADD ] [ SMUL ] [ LUT ] [ BRANCH ]
465 * Verify that there are no ordering dependencies here.
467 * TODO: Allow for parallelism!!!
470 /* Pick a unit for it if it doesn't force a particular unit */
472 int unit
= ains
->unit
;
475 int op
= ains
->alu
.op
;
476 int units
= alu_opcode_props
[op
].props
;
477 bool scalar
= mir_is_scalar(ains
);
480 if (last_unit
>= UNIT_VADD
) {
481 if (units
& UNIT_VLUT
)
486 if ((units
& UNIT_VMUL
) && last_unit
< UNIT_VMUL
)
488 else if ((units
& UNIT_VADD
) && !(control
& UNIT_VADD
))
490 else if (units
& UNIT_VLUT
)
496 if (last_unit
>= UNIT_VADD
) {
497 if ((units
& UNIT_SMUL
) && !(control
& UNIT_SMUL
))
499 else if (units
& UNIT_VLUT
)
504 if ((units
& UNIT_VMUL
) && (last_unit
< UNIT_VMUL
))
506 else if ((units
& UNIT_SADD
) && !(control
& UNIT_SADD
) && !midgard_has_hazard(segment
, segment_size
, ains
))
508 else if (units
& UNIT_VADD
)
510 else if (units
& UNIT_SMUL
)
512 else if (units
& UNIT_VLUT
)
519 assert(unit
& units
);
522 /* Late unit check, this time for encoding (not parallelism) */
523 if (unit
<= last_unit
) break;
525 /* Clear the segment */
526 if (last_unit
< UNIT_VADD
&& unit
>= UNIT_VADD
)
529 if (midgard_has_hazard(segment
, segment_size
, ains
))
532 /* We're good to go -- emit the instruction */
535 segment
[segment_size
++] = ains
;
537 /* We try to reuse constants if possible, by adjusting
540 if (ains
->has_blend_constant
) {
541 /* Everything conflicts with the blend constant */
542 if (bundle
.has_embedded_constants
)
545 bundle
.has_blend_constant
= 1;
546 bundle
.has_embedded_constants
= 1;
547 } else if (ains
->has_constants
&& ains
->alu
.reg_mode
== midgard_reg_mode_16
) {
548 /* TODO: DRY with the analysis pass */
550 if (bundle
.has_blend_constant
)
556 /* TODO: Fix packing XXX */
557 uint16_t *bundles
= (uint16_t *) bundle
.constants
;
558 uint32_t *constants
= (uint32_t *) ains
->constants
;
560 /* Copy them wholesale */
561 for (unsigned i
= 0; i
< 4; ++i
)
562 bundles
[i
] = constants
[i
];
564 bundle
.has_embedded_constants
= true;
566 } else if (ains
->has_constants
) {
567 /* By definition, blend constants conflict with
568 * everything, so if there are already
569 * constants we break the bundle *now* */
571 if (bundle
.has_blend_constant
)
574 /* For anything but blend constants, we can do
575 * proper analysis, however */
577 /* TODO: Mask by which are used */
578 uint32_t *constants
= (uint32_t *) ains
->constants
;
579 uint32_t *bundles
= (uint32_t *) bundle
.constants
;
581 uint32_t indices
[4] = { 0 };
582 bool break_bundle
= false;
584 for (unsigned i
= 0; i
< 4; ++i
) {
585 uint32_t cons
= constants
[i
];
586 bool constant_found
= false;
588 /* Search for the constant */
589 for (unsigned j
= 0; j
< constant_count
; ++j
) {
590 if (bundles
[j
] != cons
)
593 /* We found it, reuse */
595 constant_found
= true;
602 /* We didn't find it, so allocate it */
603 unsigned idx
= constant_count
++;
606 /* Uh-oh, out of space */
611 /* We have space, copy it in! */
619 /* Cool, we have it in. So use indices as a
622 unsigned swizzle
= SWIZZLE_FROM_ARRAY(indices
);
623 unsigned r_constant
= SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
625 if (ains
->src
[0] == r_constant
)
626 ains
->alu
.src1
= vector_alu_apply_swizzle(ains
->alu
.src1
, swizzle
);
628 if (ains
->src
[1] == r_constant
)
629 ains
->alu
.src2
= vector_alu_apply_swizzle(ains
->alu
.src2
, swizzle
);
631 bundle
.has_embedded_constants
= true;
634 if (ains
->compact_branch
) {
635 /* All of r0 has to be written out along with
636 * the branch writeout */
638 if (ains
->writeout
&& !can_writeout_fragment(ctx
, scheduled
, index
, ctx
->temp_count
, ains
->src
[0])) {
639 /* We only work on full moves
640 * at the beginning. We could
641 * probably do better */
646 midgard_instruction ins
= v_mov(0, blank_alu_src
, SSA_FIXED_REGISTER(0));
647 ins
.unit
= UNIT_VMUL
;
650 /* TODO don't leak */
651 midgard_instruction
*move
=
652 mem_dup(&ins
, sizeof(midgard_instruction
));
653 bytes_emitted
+= bytes_for_instruction(move
);
654 bundle
.instructions
[packed_idx
++] = move
;
658 bytes_emitted
+= bytes_for_instruction(ains
);
660 /* Defer marking until after writing to allow for break */
661 scheduled
[index
] = ains
;
662 control
|= ains
->unit
;
663 last_unit
= ains
->unit
;
664 ++instructions_emitted
;
670 /* Pad ALU op to nearest word */
672 if (bytes_emitted
& 15) {
673 padding
= 16 - (bytes_emitted
& 15);
674 bytes_emitted
+= padding
;
677 /* Constants must always be quadwords */
678 if (bundle
.has_embedded_constants
)
681 /* Size ALU instruction for tag */
682 bundle
.tag
= (TAG_ALU_4
) + (bytes_emitted
/ 16) - 1;
683 bundle
.padding
= padding
;
684 bundle
.control
= bundle
.tag
| control
;
689 case TAG_LOAD_STORE_4
: {
690 /* Load store instructions have two words at once. If
691 * we only have one queued up, we need to NOP pad.
692 * Otherwise, we store both in succession to save space
693 * and cycles -- letting them go in parallel -- skip
694 * the next. The usefulness of this optimisation is
695 * greatly dependent on the quality of the instruction
699 midgard_instruction
*next_op
= mir_next_op(ins
);
701 if ((struct list_head
*) next_op
!= &block
->instructions
&& next_op
->type
== TAG_LOAD_STORE_4
) {
702 /* TODO: Concurrency check */
703 instructions_emitted
++;
709 case TAG_TEXTURE_4
: {
710 /* Which tag we use depends on the shader stage */
711 bool in_frag
= ctx
->stage
== MESA_SHADER_FRAGMENT
;
712 bundle
.tag
= in_frag
? TAG_TEXTURE_4
: TAG_TEXTURE_4_VTX
;
717 unreachable("Unknown tag");
721 /* Copy the instructions into the bundle */
722 bundle
.instruction_count
= instructions_emitted
+ 1 + packed_idx
;
724 midgard_instruction
*uins
= ins
;
725 for (; packed_idx
< bundle
.instruction_count
; ++packed_idx
) {
726 assert(&uins
->link
!= &block
->instructions
);
727 bundle
.instructions
[packed_idx
] = uins
;
728 uins
= mir_next_op(uins
);
731 *skip
= instructions_emitted
;
736 /* We would like to flatten the linked list of midgard_instructions in a bundle
737 * to an array of pointers on the heap for easy indexing */
739 static midgard_instruction
**
740 flatten_mir(midgard_block
*block
, unsigned *len
)
742 *len
= list_length(&block
->instructions
);
747 midgard_instruction
**instructions
=
748 calloc(sizeof(midgard_instruction
*), *len
);
752 mir_foreach_instr_in_block(block
, ins
)
753 instructions
[i
++] = ins
;
758 /* The worklist is the set of instructions that can be scheduled now; that is,
759 * the set of instructions with no remaining dependencies */
762 mir_initialize_worklist(BITSET_WORD
*worklist
, midgard_instruction
**instructions
, unsigned count
)
764 for (unsigned i
= 0; i
< count
; ++i
) {
765 if (instructions
[i
]->nr_dependencies
== 0)
766 BITSET_SET(worklist
, i
);
770 /* While scheduling, we need to choose instructions satisfying certain
771 * criteria. As we schedule backwards, we choose the *last* instruction in the
772 * worklist to simulate in-order scheduling. Chosen instructions must satisfy a
773 * given predicate. */
775 struct midgard_predicate
{
776 /* TAG or ~0 for dont-care */
779 /* True if we want to pop off the chosen instruction */
783 static midgard_instruction
*
784 mir_choose_instruction(
785 midgard_instruction
**instructions
,
786 BITSET_WORD
*worklist
, unsigned count
,
787 struct midgard_predicate
*predicate
)
789 /* Parse the predicate */
790 unsigned tag
= predicate
->tag
;
792 /* Iterate to find the best instruction satisfying the predicate */
796 signed best_index
= -1;
798 BITSET_FOREACH_SET(i
, tmp
, worklist
, count
) {
799 if (tag
!= ~0 && instructions
[i
]->type
!= tag
)
802 /* Simulate in-order scheduling */
803 if ((signed) i
< best_index
)
810 /* Did we find anything? */
815 /* If we found something, remove it from the worklist */
816 assert(best_index
< count
);
818 if (predicate
->destructive
) {
819 BITSET_CLEAR(worklist
, best_index
);
822 return instructions
[best_index
];
825 /* Schedule a single block by iterating its instruction to create bundles.
826 * While we go, tally about the bundle sizes to compute the block size. */
829 schedule_block(compiler_context
*ctx
, midgard_block
*block
)
831 /* Copy list to dynamic array */
833 midgard_instruction
**instructions
= flatten_mir(block
, &len
);
835 /* Calculate dependencies and initial worklist */
836 unsigned node_count
= ctx
->temp_count
+ 1;
837 mir_create_dependency_graph(instructions
, len
, node_count
);
839 /* Allocate the worklist */
840 size_t sz
= BITSET_WORDS(len
) * sizeof(BITSET_WORD
);
841 BITSET_WORD
*worklist
= calloc(sz
, 1);
842 mir_initialize_worklist(worklist
, instructions
, len
);
844 util_dynarray_init(&block
->bundles
, NULL
);
846 block
->quadword_count
= 0;
849 mir_foreach_instr_in_block(block
, ins
) {
855 midgard_bundle bundle
= schedule_bundle(ctx
, block
, ins
, &skip
);
856 util_dynarray_append(&block
->bundles
, midgard_bundle
, bundle
);
858 if (bundle
.has_blend_constant
) {
859 unsigned offset
= ctx
->quadword_count
+ block
->quadword_count
+ quadword_size(bundle
.tag
) - 1;
860 ctx
->blend_constant_offset
= offset
* 0x10;
863 block
->quadword_count
+= quadword_size(bundle
.tag
);
866 block
->is_scheduled
= true;
867 ctx
->quadword_count
+= block
->quadword_count
;
870 /* The following passes reorder MIR instructions to enable better scheduling */
873 midgard_pair_load_store(compiler_context
*ctx
, midgard_block
*block
)
875 mir_foreach_instr_in_block_safe(block
, ins
) {
876 if (ins
->type
!= TAG_LOAD_STORE_4
) continue;
878 /* We've found a load/store op. Check if next is also load/store. */
879 midgard_instruction
*next_op
= mir_next_op(ins
);
880 if (&next_op
->link
!= &block
->instructions
) {
881 if (next_op
->type
== TAG_LOAD_STORE_4
) {
882 /* If so, we're done since we're a pair */
883 ins
= mir_next_op(ins
);
887 /* Maximum search distance to pair, to avoid register pressure disasters */
888 int search_distance
= 8;
890 /* Otherwise, we have an orphaned load/store -- search for another load */
891 mir_foreach_instr_in_block_from(block
, c
, mir_next_op(ins
)) {
892 /* Terminate search if necessary */
893 if (!(search_distance
--)) break;
895 if (c
->type
!= TAG_LOAD_STORE_4
) continue;
897 /* We can only reorder if there are no sources */
901 for (unsigned s
= 0; s
< ARRAY_SIZE(ins
->src
); ++s
)
902 deps
|= (c
->src
[s
] != ~0);
907 /* We found one! Move it up to pair and remove it from the old location */
909 mir_insert_instruction_before(ctx
, ins
, *c
);
910 mir_remove_instruction(c
);
918 /* When we're 'squeezing down' the values in the IR, we maintain a hash
922 find_or_allocate_temp(compiler_context
*ctx
, unsigned hash
)
924 if (hash
>= SSA_FIXED_MINIMUM
)
927 unsigned temp
= (uintptr_t) _mesa_hash_table_u64_search(
928 ctx
->hash_to_temp
, hash
+ 1);
933 /* If no temp is find, allocate one */
934 temp
= ctx
->temp_count
++;
935 ctx
->max_hash
= MAX2(ctx
->max_hash
, hash
);
937 _mesa_hash_table_u64_insert(ctx
->hash_to_temp
,
938 hash
+ 1, (void *) ((uintptr_t) temp
+ 1));
943 /* Reassigns numbering to get rid of gaps in the indices */
946 mir_squeeze_index(compiler_context
*ctx
)
950 /* TODO don't leak old hash_to_temp */
951 ctx
->hash_to_temp
= _mesa_hash_table_u64_create(NULL
);
953 mir_foreach_instr_global(ctx
, ins
) {
954 ins
->dest
= find_or_allocate_temp(ctx
, ins
->dest
);
956 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
->src
); ++i
)
957 ins
->src
[i
] = find_or_allocate_temp(ctx
, ins
->src
[i
]);
961 static midgard_instruction
962 v_load_store_scratch(
968 /* We index by 32-bit vec4s */
969 unsigned byte
= (index
* 4 * 4);
971 midgard_instruction ins
= {
972 .type
= TAG_LOAD_STORE_4
,
975 .src
= { ~0, ~0, ~0 },
977 .op
= is_store
? midgard_op_st_int4
: midgard_op_ld_int4
,
978 .swizzle
= SWIZZLE_XYZW
,
980 /* For register spilling - to thread local storage */
984 /* Splattered across, TODO combine logically */
985 .varying_parameters
= (byte
& 0x1FF) << 1,
986 .address
= (byte
>> 9)
989 /* If we spill an unspill, RA goes into an infinite loop */
994 /* r0 = r26, r1 = r27 */
995 assert(srcdest
== SSA_FIXED_REGISTER(26) || srcdest
== SSA_FIXED_REGISTER(27));
996 ins
.src
[0] = srcdest
;
1004 /* If register allocation fails, find the best spill node and spill it to fix
1005 * whatever the issue was. This spill node could be a work register (spilling
1006 * to thread local storage), but it could also simply be a special register
1007 * that needs to spill to become a work register. */
1009 static void mir_spill_register(
1010 compiler_context
*ctx
,
1012 unsigned *spill_count
)
1014 unsigned spill_index
= ctx
->temp_count
;
1016 /* Our first step is to calculate spill cost to figure out the best
1017 * spill node. All nodes are equal in spill cost, but we can't spill
1018 * nodes written to from an unspill */
1020 for (unsigned i
= 0; i
< ctx
->temp_count
; ++i
) {
1021 ra_set_node_spill_cost(g
, i
, 1.0);
1024 /* We can't spill any bundles that contain unspills. This could be
1025 * optimized to allow use of r27 to spill twice per bundle, but if
1026 * you're at the point of optimizing spilling, it's too late. */
1028 mir_foreach_block(ctx
, block
) {
1029 mir_foreach_bundle_in_block(block
, bun
) {
1030 bool no_spill
= false;
1032 for (unsigned i
= 0; i
< bun
->instruction_count
; ++i
)
1033 no_spill
|= bun
->instructions
[i
]->no_spill
;
1038 for (unsigned i
= 0; i
< bun
->instruction_count
; ++i
) {
1039 unsigned dest
= bun
->instructions
[i
]->dest
;
1040 if (dest
< ctx
->temp_count
)
1041 ra_set_node_spill_cost(g
, dest
, -1.0);
1046 int spill_node
= ra_get_best_spill_node(g
);
1048 if (spill_node
< 0) {
1049 mir_print_shader(ctx
);
1053 /* We have a spill node, so check the class. Work registers
1054 * legitimately spill to TLS, but special registers just spill to work
1057 unsigned class = ra_get_node_class(g
, spill_node
);
1058 bool is_special
= (class >> 2) != REG_CLASS_WORK
;
1059 bool is_special_w
= (class >> 2) == REG_CLASS_TEXW
;
1061 /* Allocate TLS slot (maybe) */
1062 unsigned spill_slot
= !is_special
? (*spill_count
)++ : 0;
1064 /* For TLS, replace all stores to the spilled node. For
1065 * special reads, just keep as-is; the class will be demoted
1066 * implicitly. For special writes, spill to a work register */
1068 if (!is_special
|| is_special_w
) {
1070 spill_slot
= spill_index
++;
1072 mir_foreach_block(ctx
, block
) {
1073 mir_foreach_instr_in_block_safe(block
, ins
) {
1074 if (ins
->dest
!= spill_node
) continue;
1076 midgard_instruction st
;
1079 st
= v_mov(spill_node
, blank_alu_src
, spill_slot
);
1082 ins
->dest
= SSA_FIXED_REGISTER(26);
1083 ins
->no_spill
= true;
1084 st
= v_load_store_scratch(ins
->dest
, spill_slot
, true, ins
->mask
);
1087 /* Hint: don't rewrite this node */
1090 mir_insert_instruction_after_scheduled(ctx
, block
, ins
, st
);
1098 /* For special reads, figure out how many components we need */
1099 unsigned read_mask
= 0;
1101 mir_foreach_instr_global_safe(ctx
, ins
) {
1102 read_mask
|= mir_mask_of_read_components(ins
, spill_node
);
1105 /* Insert a load from TLS before the first consecutive
1106 * use of the node, rewriting to use spilled indices to
1107 * break up the live range. Or, for special, insert a
1108 * move. Ironically the latter *increases* register
1109 * pressure, but the two uses of the spilling mechanism
1110 * are somewhat orthogonal. (special spilling is to use
1111 * work registers to back special registers; TLS
1112 * spilling is to use memory to back work registers) */
1114 mir_foreach_block(ctx
, block
) {
1115 bool consecutive_skip
= false;
1116 unsigned consecutive_index
= 0;
1118 mir_foreach_instr_in_block(block
, ins
) {
1119 /* We can't rewrite the moves used to spill in the
1120 * first place. These moves are hinted. */
1121 if (ins
->hint
) continue;
1123 if (!mir_has_arg(ins
, spill_node
)) {
1124 consecutive_skip
= false;
1128 if (consecutive_skip
) {
1130 mir_rewrite_index_src_single(ins
, spill_node
, consecutive_index
);
1134 if (!is_special_w
) {
1135 consecutive_index
= ++spill_index
;
1137 midgard_instruction
*before
= ins
;
1139 /* For a csel, go back one more not to break up the bundle */
1140 if (ins
->type
== TAG_ALU_4
&& OP_IS_CSEL(ins
->alu
.op
))
1141 before
= mir_prev_op(before
);
1143 midgard_instruction st
;
1147 st
= v_mov(spill_node
, blank_alu_src
, consecutive_index
);
1151 st
= v_load_store_scratch(consecutive_index
, spill_slot
, false, 0xF);
1154 /* Mask the load based on the component count
1155 * actually needed to prvent RA loops */
1157 st
.mask
= read_mask
;
1159 mir_insert_instruction_before_scheduled(ctx
, block
, before
, st
);
1160 // consecutive_skip = true;
1162 /* Special writes already have their move spilled in */
1163 consecutive_index
= spill_slot
;
1167 /* Rewrite to use */
1168 mir_rewrite_index_src_single(ins
, spill_node
, consecutive_index
);
1177 mir_foreach_instr_global(ctx
, ins
) {
1183 schedule_program(compiler_context
*ctx
)
1185 struct ra_graph
*g
= NULL
;
1186 bool spilled
= false;
1187 int iter_count
= 1000; /* max iterations */
1189 /* Number of 128-bit slots in memory we've spilled into */
1190 unsigned spill_count
= 0;
1192 midgard_promote_uniforms(ctx
, 16);
1194 mir_foreach_block(ctx
, block
) {
1195 midgard_pair_load_store(ctx
, block
);
1198 /* Must be lowered right before RA */
1199 mir_squeeze_index(ctx
);
1200 mir_lower_special_reads(ctx
);
1201 mir_squeeze_index(ctx
);
1203 /* Lowering can introduce some dead moves */
1205 mir_foreach_block(ctx
, block
) {
1206 midgard_opt_dead_move_eliminate(ctx
, block
);
1207 schedule_block(ctx
, block
);
1210 mir_create_pipeline_registers(ctx
);
1214 mir_spill_register(ctx
, g
, &spill_count
);
1216 mir_squeeze_index(ctx
);
1219 g
= allocate_registers(ctx
, &spilled
);
1220 } while(spilled
&& ((iter_count
--) > 0));
1222 if (iter_count
<= 0) {
1223 fprintf(stderr
, "panfrost: Gave up allocating registers, rendering will be incomplete\n");
1227 /* Report spilling information. spill_count is in 128-bit slots (vec4 x
1228 * fp32), but tls_size is in bytes, so multiply by 16 */
1230 ctx
->tls_size
= spill_count
* 16;
1232 install_registers(ctx
, g
);