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 "midgard_quirks.h"
27 #include "util/u_memory.h"
28 #include "util/u_math.h"
30 /* Scheduling for Midgard is complicated, to say the least. ALU instructions
31 * must be grouped into VLIW bundles according to following model:
34 * [VADD] [SMUL] [VLUT]
36 * A given instruction can execute on some subset of the units (or a few can
37 * execute on all). Instructions can be either vector or scalar; only scalar
38 * instructions can execute on SADD/SMUL units. Units on a given line execute
39 * in parallel. Subsequent lines execute separately and can pass results
40 * directly via pipeline registers r24/r25, bypassing the register file.
42 * A bundle can optionally have 128-bits of embedded constants, shared across
43 * all of the instructions within a bundle.
45 * Instructions consuming conditionals (branches and conditional selects)
46 * require their condition to be written into the conditional register (r31)
47 * within the same bundle they are consumed.
49 * Fragment writeout requires its argument to be written in full within the
50 * same bundle as the branch, with no hanging dependencies.
52 * Load/store instructions are also in bundles of simply two instructions, and
53 * texture instructions have no bundling.
55 * -------------------------------------------------------------------------
59 /* We create the dependency graph with per-byte granularity */
64 add_dependency(struct util_dynarray
*table
, unsigned index
, uint16_t mask
, midgard_instruction
**instructions
, unsigned child
)
66 for (unsigned i
= 0; i
< BYTE_COUNT
; ++i
) {
67 if (!(mask
& (1 << i
)))
70 struct util_dynarray
*parents
= &table
[(BYTE_COUNT
* index
) + i
];
72 util_dynarray_foreach(parents
, unsigned, parent
) {
73 BITSET_WORD
*dependents
= instructions
[*parent
]->dependents
;
75 /* Already have the dependency */
76 if (BITSET_TEST(dependents
, child
))
79 BITSET_SET(dependents
, child
);
80 instructions
[child
]->nr_dependencies
++;
86 mark_access(struct util_dynarray
*table
, unsigned index
, uint16_t mask
, unsigned parent
)
88 for (unsigned i
= 0; i
< BYTE_COUNT
; ++i
) {
89 if (!(mask
& (1 << i
)))
92 util_dynarray_append(&table
[(BYTE_COUNT
* index
) + i
], unsigned, parent
);
97 mir_create_dependency_graph(midgard_instruction
**instructions
, unsigned count
, unsigned node_count
)
99 size_t sz
= node_count
* BYTE_COUNT
;
101 struct util_dynarray
*last_read
= calloc(sizeof(struct util_dynarray
), sz
);
102 struct util_dynarray
*last_write
= calloc(sizeof(struct util_dynarray
), sz
);
104 for (unsigned i
= 0; i
< sz
; ++i
) {
105 util_dynarray_init(&last_read
[i
], NULL
);
106 util_dynarray_init(&last_write
[i
], NULL
);
109 /* Initialize dependency graph */
110 for (unsigned i
= 0; i
< count
; ++i
) {
111 instructions
[i
]->dependents
=
112 calloc(BITSET_WORDS(count
), sizeof(BITSET_WORD
));
114 instructions
[i
]->nr_dependencies
= 0;
117 /* Populate dependency graph */
118 for (signed i
= count
- 1; i
>= 0; --i
) {
119 if (instructions
[i
]->compact_branch
)
122 unsigned dest
= instructions
[i
]->dest
;
123 unsigned mask
= mir_bytemask(instructions
[i
]);
125 mir_foreach_src((*instructions
), s
) {
126 unsigned src
= instructions
[i
]->src
[s
];
128 if (src
< node_count
) {
129 unsigned readmask
= mir_bytemask_of_read_components(instructions
[i
], src
);
130 add_dependency(last_write
, src
, readmask
, instructions
, i
);
134 if (dest
< node_count
) {
135 add_dependency(last_read
, dest
, mask
, instructions
, i
);
136 add_dependency(last_write
, dest
, mask
, instructions
, i
);
137 mark_access(last_write
, dest
, mask
, i
);
140 mir_foreach_src((*instructions
), s
) {
141 unsigned src
= instructions
[i
]->src
[s
];
143 if (src
< node_count
) {
144 unsigned readmask
= mir_bytemask_of_read_components(instructions
[i
], src
);
145 mark_access(last_read
, src
, readmask
, i
);
150 /* If there is a branch, all instructions depend on it, as interblock
151 * execution must be purely in-order */
153 if (instructions
[count
- 1]->compact_branch
) {
154 BITSET_WORD
*dependents
= instructions
[count
- 1]->dependents
;
156 for (signed i
= count
- 2; i
>= 0; --i
) {
157 if (BITSET_TEST(dependents
, i
))
160 BITSET_SET(dependents
, i
);
161 instructions
[i
]->nr_dependencies
++;
165 /* Free the intermediate structures */
166 for (unsigned i
= 0; i
< sz
; ++i
) {
167 util_dynarray_fini(&last_read
[i
]);
168 util_dynarray_fini(&last_write
[i
]);
175 /* Does the mask cover more than a scalar? */
178 is_single_component_mask(unsigned mask
)
182 for (int c
= 0; c
< 8; ++c
) {
187 return components
== 1;
190 /* Helpers for scheudling */
193 mir_is_scalar(midgard_instruction
*ains
)
195 /* Do we try to use it as a vector op? */
196 if (!is_single_component_mask(ains
->mask
))
199 /* Otherwise, check mode hazards */
200 bool could_scalar
= true;
201 unsigned sz0
= nir_alu_type_get_type_size(ains
->src_types
[0]);
202 unsigned sz1
= nir_alu_type_get_type_size(ains
->src_types
[1]);
204 /* Only 16/32-bit can run on a scalar unit */
205 could_scalar
&= ains
->alu
.reg_mode
!= midgard_reg_mode_8
;
206 could_scalar
&= ains
->alu
.reg_mode
!= midgard_reg_mode_64
;
208 if (ains
->src
[0] != ~0)
209 could_scalar
&= (sz0
== 16) || (sz0
== 32);
211 if (ains
->src
[1] != ~0)
212 could_scalar
&= (sz1
== 16) || (sz1
== 32);
217 /* How many bytes does this ALU instruction add to the bundle? */
220 bytes_for_instruction(midgard_instruction
*ains
)
222 if (ains
->unit
& UNITS_ANY_VECTOR
)
223 return sizeof(midgard_reg_info
) + sizeof(midgard_vector_alu
);
224 else if (ains
->unit
== ALU_ENAB_BRANCH
)
225 return sizeof(midgard_branch_extended
);
226 else if (ains
->compact_branch
)
227 return sizeof(ains
->br_compact
);
229 return sizeof(midgard_reg_info
) + sizeof(midgard_scalar_alu
);
232 /* We would like to flatten the linked list of midgard_instructions in a bundle
233 * to an array of pointers on the heap for easy indexing */
235 static midgard_instruction
**
236 flatten_mir(midgard_block
*block
, unsigned *len
)
238 *len
= list_length(&block
->base
.instructions
);
243 midgard_instruction
**instructions
=
244 calloc(sizeof(midgard_instruction
*), *len
);
248 mir_foreach_instr_in_block(block
, ins
)
249 instructions
[i
++] = ins
;
254 /* The worklist is the set of instructions that can be scheduled now; that is,
255 * the set of instructions with no remaining dependencies */
258 mir_initialize_worklist(BITSET_WORD
*worklist
, midgard_instruction
**instructions
, unsigned count
)
260 for (unsigned i
= 0; i
< count
; ++i
) {
261 if (instructions
[i
]->nr_dependencies
== 0)
262 BITSET_SET(worklist
, i
);
266 /* Update the worklist after an instruction terminates. Remove its edges from
267 * the graph and if that causes any node to have no dependencies, add it to the
272 BITSET_WORD
*worklist
, unsigned count
,
273 midgard_instruction
**instructions
, midgard_instruction
*done
)
275 /* Sanity check: if no instruction terminated, there is nothing to do.
276 * If the instruction that terminated had dependencies, that makes no
277 * sense and means we messed up the worklist. Finally, as the purpose
278 * of this routine is to update dependents, we abort early if there are
279 * no dependents defined. */
284 assert(done
->nr_dependencies
== 0);
286 if (!done
->dependents
)
289 /* We have an instruction with dependents. Iterate each dependent to
290 * remove one dependency (`done`), adding dependents to the worklist
294 BITSET_FOREACH_SET(i
, done
->dependents
, count
) {
295 assert(instructions
[i
]->nr_dependencies
);
297 if (!(--instructions
[i
]->nr_dependencies
))
298 BITSET_SET(worklist
, i
);
301 free(done
->dependents
);
304 /* While scheduling, we need to choose instructions satisfying certain
305 * criteria. As we schedule backwards, we choose the *last* instruction in the
306 * worklist to simulate in-order scheduling. Chosen instructions must satisfy a
307 * given predicate. */
309 struct midgard_predicate
{
310 /* TAG or ~0 for dont-care */
313 /* True if we want to pop off the chosen instruction */
316 /* For ALU, choose only this unit */
319 /* State for bundle constants. constants is the actual constants
320 * for the bundle. constant_count is the number of bytes (up to
321 * 16) currently in use for constants. When picking in destructive
322 * mode, the constants array will be updated, and the instruction
323 * will be adjusted to index into the constants array */
325 midgard_constants
*constants
;
326 unsigned constant_mask
;
329 /* Exclude this destination (if not ~0) */
332 /* Don't schedule instructions consuming conditionals (since we already
333 * scheduled one). Excludes conditional branches and csel */
336 /* Require a minimal mask and (if nonzero) given destination. Used for
337 * writeout optimizations */
342 /* For load/store: how many pipeline registers are in use? The two
343 * scheduled instructions cannot use more than the 256-bits of pipeline
344 * space available or RA will fail (as it would run out of pipeline
345 * registers and fail to spill without breaking the schedule) */
347 unsigned pipeline_count
;
351 mir_adjust_constant(midgard_instruction
*ins
, unsigned src
,
352 unsigned *bundle_constant_mask
,
353 unsigned *comp_mapping
,
354 uint8_t *bundle_constants
,
357 unsigned type_size
= nir_alu_type_get_type_size(ins
->src_types
[src
]) / 8;
358 unsigned type_shift
= util_logbase2(type_size
);
359 unsigned max_comp
= mir_components_for_type(ins
->src_types
[src
]);
360 unsigned comp_mask
= mir_from_bytemask(mir_round_bytemask_up(
361 mir_bytemask_of_read_components_index(ins
, src
),
364 unsigned type_mask
= (1 << type_size
) - 1;
366 /* Upper only makes sense for 16-bit */
367 if (type_size
!= 16 && upper
)
370 /* For 16-bit, we need to stay on either upper or lower halves to avoid
371 * disrupting the swizzle */
372 unsigned start
= upper
? 8 : 0;
373 unsigned length
= (type_size
== 2) ? 8 : 16;
375 for (unsigned comp
= 0; comp
< max_comp
; comp
++) {
376 if (!(comp_mask
& (1 << comp
)))
379 uint8_t *constantp
= ins
->constants
.u8
+ (type_size
* comp
);
380 unsigned best_reuse_bytes
= 0;
381 signed best_place
= -1;
384 for (i
= start
; i
< (start
+ length
); i
+= type_size
) {
385 unsigned reuse_bytes
= 0;
387 for (j
= 0; j
< type_size
; j
++) {
388 if (!(*bundle_constant_mask
& (1 << (i
+ j
))))
390 if (constantp
[j
] != bundle_constants
[i
+ j
])
392 if ((i
+ j
) > (start
+ length
))
398 /* Select the place where existing bytes can be
399 * reused so we leave empty slots to others
401 if (j
== type_size
&&
402 (reuse_bytes
> best_reuse_bytes
|| best_place
< 0)) {
403 best_reuse_bytes
= reuse_bytes
;
409 /* This component couldn't fit in the remaining constant slot,
410 * no need check the remaining components, bail out now
415 memcpy(&bundle_constants
[i
], constantp
, type_size
);
416 *bundle_constant_mask
|= type_mask
<< best_place
;
417 comp_mapping
[comp
] = best_place
>> type_shift
;
423 /* For an instruction that can fit, adjust it to fit and update the constants
424 * array, in destructive mode. Returns whether the fitting was successful. */
427 mir_adjust_constants(midgard_instruction
*ins
,
428 struct midgard_predicate
*pred
,
431 /* Blend constants dominate */
432 if (ins
->has_blend_constant
) {
433 if (pred
->constant_mask
)
435 else if (destructive
) {
436 pred
->blend_constant
= true;
437 pred
->constant_mask
= 0xffff;
442 /* No constant, nothing to adjust */
443 if (!ins
->has_constants
)
446 unsigned r_constant
= SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
447 unsigned bundle_constant_mask
= pred
->constant_mask
;
448 unsigned comp_mapping
[2][16] = { };
449 uint8_t bundle_constants
[16];
451 memcpy(bundle_constants
, pred
->constants
, 16);
453 /* Let's try to find a place for each active component of the constant
456 for (unsigned src
= 0; src
< 2; ++src
) {
457 if (ins
->src
[src
] != SSA_FIXED_REGISTER(REGISTER_CONSTANT
))
460 /* First, try lower half (or whole for !16) */
461 if (mir_adjust_constant(ins
, src
, &bundle_constant_mask
,
462 comp_mapping
[src
], bundle_constants
, false))
465 /* Next, try upper half */
466 if (mir_adjust_constant(ins
, src
, &bundle_constant_mask
,
467 comp_mapping
[src
], bundle_constants
, true))
474 /* If non-destructive, we're done */
478 /* Otherwise update the constant_mask and constant values */
479 pred
->constant_mask
= bundle_constant_mask
;
480 memcpy(pred
->constants
, bundle_constants
, 16);
482 /* Use comp_mapping as a swizzle */
483 mir_foreach_src(ins
, s
) {
484 if (ins
->src
[s
] == r_constant
)
485 mir_compose_swizzle(ins
->swizzle
[s
], comp_mapping
[s
], ins
->swizzle
[s
]);
491 /* Conservative estimate of the pipeline registers required for load/store */
494 mir_pipeline_count(midgard_instruction
*ins
)
496 unsigned bytecount
= 0;
498 mir_foreach_src(ins
, i
) {
499 /* Skip empty source */
500 if (ins
->src
[i
] == ~0) continue;
502 unsigned bytemask
= mir_bytemask_of_read_components_index(ins
, i
);
504 unsigned max
= util_logbase2(bytemask
) + 1;
508 return DIV_ROUND_UP(bytecount
, 16);
511 static midgard_instruction
*
512 mir_choose_instruction(
513 midgard_instruction
**instructions
,
514 BITSET_WORD
*worklist
, unsigned count
,
515 struct midgard_predicate
*predicate
)
517 /* Parse the predicate */
518 unsigned tag
= predicate
->tag
;
519 bool alu
= tag
== TAG_ALU_4
;
520 bool ldst
= tag
== TAG_LOAD_STORE_4
;
521 unsigned unit
= predicate
->unit
;
522 bool branch
= alu
&& (unit
== ALU_ENAB_BR_COMPACT
);
523 bool scalar
= (unit
!= ~0) && (unit
& UNITS_SCALAR
);
524 bool no_cond
= predicate
->no_cond
;
526 unsigned mask
= predicate
->mask
;
527 unsigned dest
= predicate
->dest
;
528 bool needs_dest
= mask
& 0xF;
530 /* Iterate to find the best instruction satisfying the predicate */
533 signed best_index
= -1;
534 bool best_conditional
= false;
536 /* Enforce a simple metric limiting distance to keep down register
537 * pressure. TOOD: replace with liveness tracking for much better
540 unsigned max_active
= 0;
541 unsigned max_distance
= 6;
543 BITSET_FOREACH_SET(i
, worklist
, count
) {
544 max_active
= MAX2(max_active
, i
);
547 BITSET_FOREACH_SET(i
, worklist
, count
) {
548 if ((max_active
- i
) >= max_distance
)
551 if (tag
!= ~0 && instructions
[i
]->type
!= tag
)
554 if (predicate
->exclude
!= ~0 && instructions
[i
]->dest
== predicate
->exclude
)
557 if (alu
&& !branch
&& !(alu_opcode_props
[instructions
[i
]->alu
.op
].props
& unit
))
560 if (branch
&& !instructions
[i
]->compact_branch
)
563 if (alu
&& scalar
&& !mir_is_scalar(instructions
[i
]))
566 if (alu
&& !mir_adjust_constants(instructions
[i
], predicate
, false))
569 if (needs_dest
&& instructions
[i
]->dest
!= dest
)
572 if (mask
&& ((~instructions
[i
]->mask
) & mask
))
575 if (ldst
&& mir_pipeline_count(instructions
[i
]) + predicate
->pipeline_count
> 2)
578 bool conditional
= alu
&& !branch
&& OP_IS_CSEL(instructions
[i
]->alu
.op
);
579 conditional
|= (branch
&& instructions
[i
]->branch
.conditional
);
581 if (conditional
&& no_cond
)
584 /* Simulate in-order scheduling */
585 if ((signed) i
< best_index
)
589 best_conditional
= conditional
;
593 /* Did we find anything? */
598 /* If we found something, remove it from the worklist */
599 assert(best_index
< count
);
601 if (predicate
->destructive
) {
602 BITSET_CLEAR(worklist
, best_index
);
605 mir_adjust_constants(instructions
[best_index
], predicate
, true);
608 predicate
->pipeline_count
+= mir_pipeline_count(instructions
[best_index
]);
610 /* Once we schedule a conditional, we can't again */
611 predicate
->no_cond
|= best_conditional
;
614 return instructions
[best_index
];
617 /* Still, we don't choose instructions in a vacuum. We need a way to choose the
618 * best bundle type (ALU, load/store, texture). Nondestructive. */
622 midgard_instruction
**instructions
,
623 BITSET_WORD
*worklist
, unsigned count
)
625 /* At the moment, our algorithm is very simple - use the bundle of the
626 * best instruction, regardless of what else could be scheduled
627 * alongside it. This is not optimal but it works okay for in-order */
629 struct midgard_predicate predicate
= {
631 .destructive
= false,
635 midgard_instruction
*chosen
= mir_choose_instruction(instructions
, worklist
, count
, &predicate
);
643 /* We want to choose an ALU instruction filling a given unit */
645 mir_choose_alu(midgard_instruction
**slot
,
646 midgard_instruction
**instructions
,
647 BITSET_WORD
*worklist
, unsigned len
,
648 struct midgard_predicate
*predicate
,
651 /* Did we already schedule to this slot? */
655 /* Try to schedule something, if not */
656 predicate
->unit
= unit
;
657 *slot
= mir_choose_instruction(instructions
, worklist
, len
, predicate
);
659 /* Store unit upon scheduling */
660 if (*slot
&& !((*slot
)->compact_branch
))
661 (*slot
)->unit
= unit
;
664 /* When we are scheduling a branch/csel, we need the consumed condition in the
665 * same block as a pipeline register. There are two options to enable this:
667 * - Move the conditional into the bundle. Preferred, but only works if the
668 * conditional is used only once and is from this block.
669 * - Copy the conditional.
671 * We search for the conditional. If it's in this block, single-use, and
672 * without embedded constants, we schedule it immediately. Otherwise, we
673 * schedule a move for it.
675 * mir_comparison_mobile is a helper to find the moveable condition.
679 mir_comparison_mobile(
680 compiler_context
*ctx
,
681 midgard_instruction
**instructions
,
682 struct midgard_predicate
*predicate
,
686 if (!mir_single_use(ctx
, cond
))
691 for (unsigned i
= 0; i
< count
; ++i
) {
692 if (instructions
[i
]->dest
!= cond
)
695 /* Must fit in an ALU bundle */
696 if (instructions
[i
]->type
!= TAG_ALU_4
)
699 /* If it would itself require a condition, that's recursive */
700 if (OP_IS_CSEL(instructions
[i
]->alu
.op
))
703 /* We'll need to rewrite to .w but that doesn't work for vector
704 * ops that don't replicate (ball/bany), so bail there */
706 if (GET_CHANNEL_COUNT(alu_opcode_props
[instructions
[i
]->alu
.op
].props
))
709 /* Ensure it will fit with constants */
711 if (!mir_adjust_constants(instructions
[i
], predicate
, false))
714 /* Ensure it is written only once */
722 /* Inject constants now that we are sure we want to */
724 mir_adjust_constants(instructions
[ret
], predicate
, true);
729 /* Using the information about the moveable conditional itself, we either pop
730 * that condition off the worklist for use now, or create a move to
731 * artificially schedule instead as a fallback */
733 static midgard_instruction
*
734 mir_schedule_comparison(
735 compiler_context
*ctx
,
736 midgard_instruction
**instructions
,
737 struct midgard_predicate
*predicate
,
738 BITSET_WORD
*worklist
, unsigned count
,
739 unsigned cond
, bool vector
, unsigned *swizzle
,
740 midgard_instruction
*user
)
742 /* TODO: swizzle when scheduling */
744 (!vector
&& (swizzle
[0] == 0)) ?
745 mir_comparison_mobile(ctx
, instructions
, predicate
, count
, cond
) : ~0;
747 /* If we can, schedule the condition immediately */
748 if ((comp_i
!= ~0) && BITSET_TEST(worklist
, comp_i
)) {
749 assert(comp_i
< count
);
750 BITSET_CLEAR(worklist
, comp_i
);
751 return instructions
[comp_i
];
754 /* Otherwise, we insert a move */
756 midgard_instruction mov
= v_mov(cond
, cond
);
757 mov
.mask
= vector
? 0xF : 0x1;
758 memcpy(mov
.swizzle
[1], swizzle
, sizeof(mov
.swizzle
[1]));
760 return mir_insert_instruction_before(ctx
, user
, mov
);
763 /* Most generally, we need instructions writing to r31 in the appropriate
766 static midgard_instruction
*
767 mir_schedule_condition(compiler_context
*ctx
,
768 struct midgard_predicate
*predicate
,
769 BITSET_WORD
*worklist
, unsigned count
,
770 midgard_instruction
**instructions
,
771 midgard_instruction
*last
)
773 /* For a branch, the condition is the only argument; for csel, third */
774 bool branch
= last
->compact_branch
;
775 unsigned condition_index
= branch
? 0 : 2;
777 /* csel_v is vector; otherwise, conditions are scalar */
778 bool vector
= !branch
&& OP_IS_CSEL_V(last
->alu
.op
);
780 /* Grab the conditional instruction */
782 midgard_instruction
*cond
= mir_schedule_comparison(
783 ctx
, instructions
, predicate
, worklist
, count
, last
->src
[condition_index
],
784 vector
, last
->swizzle
[2], last
);
786 /* We have exclusive reign over this (possibly move) conditional
787 * instruction. We can rewrite into a pipeline conditional register */
789 predicate
->exclude
= cond
->dest
;
790 cond
->dest
= SSA_FIXED_REGISTER(31);
793 cond
->mask
= (1 << COMPONENT_W
);
795 mir_foreach_src(cond
, s
) {
796 if (cond
->src
[s
] == ~0)
799 for (unsigned q
= 0; q
< 4; ++q
)
800 cond
->swizzle
[s
][q
+ COMPONENT_W
] = cond
->swizzle
[s
][q
];
804 /* Schedule the unit: csel is always in the latter pipeline, so a csel
805 * condition must be in the former pipeline stage (vmul/sadd),
806 * depending on scalar/vector of the instruction itself. A branch must
807 * be written from the latter pipeline stage and a branch condition is
808 * always scalar, so it is always in smul (exception: ball/bany, which
812 cond
->unit
= UNIT_SMUL
;
814 cond
->unit
= vector
? UNIT_VMUL
: UNIT_SADD
;
819 /* Schedules a single bundle of the given type */
821 static midgard_bundle
822 mir_schedule_texture(
823 midgard_instruction
**instructions
,
824 BITSET_WORD
*worklist
, unsigned len
)
826 struct midgard_predicate predicate
= {
827 .tag
= TAG_TEXTURE_4
,
832 midgard_instruction
*ins
=
833 mir_choose_instruction(instructions
, worklist
, len
, &predicate
);
835 mir_update_worklist(worklist
, len
, instructions
, ins
);
837 struct midgard_bundle out
= {
838 .tag
= ins
->texture
.op
== TEXTURE_OP_BARRIER
?
839 TAG_TEXTURE_4_BARRIER
: TAG_TEXTURE_4
,
840 .instruction_count
= 1,
841 .instructions
= { ins
}
847 static midgard_bundle
849 midgard_instruction
**instructions
,
850 BITSET_WORD
*worklist
, unsigned len
)
852 struct midgard_predicate predicate
= {
853 .tag
= TAG_LOAD_STORE_4
,
858 /* Try to pick two load/store ops. Second not gauranteed to exist */
860 midgard_instruction
*ins
=
861 mir_choose_instruction(instructions
, worklist
, len
, &predicate
);
863 midgard_instruction
*pair
=
864 mir_choose_instruction(instructions
, worklist
, len
, &predicate
);
866 struct midgard_bundle out
= {
867 .tag
= TAG_LOAD_STORE_4
,
868 .instruction_count
= pair
? 2 : 1,
869 .instructions
= { ins
, pair
}
872 /* We have to update the worklist atomically, since the two
873 * instructions run concurrently (TODO: verify it's not pipelined) */
875 mir_update_worklist(worklist
, len
, instructions
, ins
);
876 mir_update_worklist(worklist
, len
, instructions
, pair
);
881 static midgard_bundle
883 compiler_context
*ctx
,
884 midgard_instruction
**instructions
,
885 BITSET_WORD
*worklist
, unsigned len
)
887 struct midgard_bundle bundle
= {};
889 unsigned bytes_emitted
= sizeof(bundle
.control
);
891 struct midgard_predicate predicate
= {
895 .constants
= &bundle
.constants
898 midgard_instruction
*vmul
= NULL
;
899 midgard_instruction
*vadd
= NULL
;
900 midgard_instruction
*vlut
= NULL
;
901 midgard_instruction
*smul
= NULL
;
902 midgard_instruction
*sadd
= NULL
;
903 midgard_instruction
*branch
= NULL
;
905 mir_choose_alu(&branch
, instructions
, worklist
, len
, &predicate
, ALU_ENAB_BR_COMPACT
);
906 mir_update_worklist(worklist
, len
, instructions
, branch
);
907 bool writeout
= branch
&& branch
->writeout
;
908 bool zs_writeout
= writeout
&& (branch
->writeout_depth
| branch
->writeout_stencil
);
910 if (branch
&& branch
->branch
.conditional
) {
911 midgard_instruction
*cond
= mir_schedule_condition(ctx
, &predicate
, worklist
, len
, instructions
, branch
);
913 if (cond
->unit
== UNIT_VADD
)
915 else if (cond
->unit
== UNIT_SMUL
)
918 unreachable("Bad condition");
921 /* If we have a render target reference, schedule a move for it. Since
922 * this will be in sadd, we boost this to prevent scheduling csel into
925 if (writeout
&& (branch
->constants
.u32
[0] || ctx
->is_blend
)) {
926 sadd
= ralloc(ctx
, midgard_instruction
);
927 *sadd
= v_mov(~0, make_compiler_temp(ctx
));
928 sadd
->unit
= UNIT_SADD
;
930 sadd
->has_inline_constant
= true;
931 sadd
->inline_constant
= branch
->constants
.u32
[0];
932 branch
->src
[1] = sadd
->dest
;
933 branch
->src_types
[1] = sadd
->dest_type
;
935 /* Mask off any conditionals. Could be optimized to just scalar
936 * conditionals TODO */
937 predicate
.no_cond
= true;
940 mir_choose_alu(&smul
, instructions
, worklist
, len
, &predicate
, UNIT_SMUL
);
943 mir_choose_alu(&vlut
, instructions
, worklist
, len
, &predicate
, UNIT_VLUT
);
946 bundle
.last_writeout
= branch
->last_writeout
;
949 if (writeout
&& !zs_writeout
) {
950 vadd
= ralloc(ctx
, midgard_instruction
);
951 *vadd
= v_mov(~0, make_compiler_temp(ctx
));
953 if (!ctx
->is_blend
) {
954 vadd
->alu
.op
= midgard_alu_op_iadd
;
955 vadd
->src
[0] = SSA_FIXED_REGISTER(31);
956 vadd
->src_types
[0] = nir_type_uint32
;
958 for (unsigned c
= 0; c
< 16; ++c
)
959 vadd
->swizzle
[0][c
] = COMPONENT_X
;
961 vadd
->has_inline_constant
= true;
962 vadd
->inline_constant
= 0;
964 vadd
->src
[1] = SSA_FIXED_REGISTER(1);
965 vadd
->src_types
[0] = nir_type_uint32
;
967 for (unsigned c
= 0; c
< 16; ++c
)
968 vadd
->swizzle
[1][c
] = COMPONENT_W
;
971 vadd
->unit
= UNIT_VADD
;
973 branch
->src
[2] = vadd
->dest
;
974 branch
->src_types
[2] = vadd
->dest_type
;
977 mir_choose_alu(&vadd
, instructions
, worklist
, len
, &predicate
, UNIT_VADD
);
979 mir_update_worklist(worklist
, len
, instructions
, vlut
);
980 mir_update_worklist(worklist
, len
, instructions
, vadd
);
981 mir_update_worklist(worklist
, len
, instructions
, smul
);
983 bool vadd_csel
= vadd
&& OP_IS_CSEL(vadd
->alu
.op
);
984 bool smul_csel
= smul
&& OP_IS_CSEL(smul
->alu
.op
);
986 if (vadd_csel
|| smul_csel
) {
987 midgard_instruction
*ins
= vadd_csel
? vadd
: smul
;
988 midgard_instruction
*cond
= mir_schedule_condition(ctx
, &predicate
, worklist
, len
, instructions
, ins
);
990 if (cond
->unit
== UNIT_VMUL
)
992 else if (cond
->unit
== UNIT_SADD
)
995 unreachable("Bad condition");
998 /* Stage 2, let's schedule sadd before vmul for writeout */
999 mir_choose_alu(&sadd
, instructions
, worklist
, len
, &predicate
, UNIT_SADD
);
1001 /* Check if writeout reads its own register */
1004 midgard_instruction
*stages
[] = { sadd
, vadd
, smul
};
1005 unsigned src
= (branch
->src
[0] == ~0) ? SSA_FIXED_REGISTER(zs_writeout
? 1 : 0) : branch
->src
[0];
1006 unsigned writeout_mask
= 0x0;
1007 bool bad_writeout
= false;
1009 for (unsigned i
= 0; i
< ARRAY_SIZE(stages
); ++i
) {
1013 if (stages
[i
]->dest
!= src
)
1016 writeout_mask
|= stages
[i
]->mask
;
1017 bad_writeout
|= mir_has_arg(stages
[i
], branch
->src
[0]);
1020 /* It's possible we'll be able to schedule something into vmul
1021 * to fill r0/r1. Let's peak into the future, trying to schedule
1022 * vmul specially that way. */
1024 unsigned full_mask
= zs_writeout
?
1025 (1 << (branch
->writeout_depth
+ branch
->writeout_stencil
)) - 1 :
1028 if (!bad_writeout
&& writeout_mask
!= full_mask
) {
1029 predicate
.unit
= UNIT_VMUL
;
1030 predicate
.dest
= src
;
1031 predicate
.mask
= writeout_mask
^ full_mask
;
1033 struct midgard_instruction
*peaked
=
1034 mir_choose_instruction(instructions
, worklist
, len
, &predicate
);
1038 vmul
->unit
= UNIT_VMUL
;
1039 writeout_mask
|= predicate
.mask
;
1040 assert(writeout_mask
== full_mask
);
1044 predicate
.dest
= predicate
.mask
= 0;
1047 /* Finally, add a move if necessary */
1048 if (bad_writeout
|| writeout_mask
!= full_mask
) {
1049 unsigned temp
= (branch
->src
[0] == ~0) ? SSA_FIXED_REGISTER(zs_writeout
? 1 : 0) : make_compiler_temp(ctx
);
1051 vmul
= ralloc(ctx
, midgard_instruction
);
1052 *vmul
= v_mov(src
, temp
);
1053 vmul
->unit
= UNIT_VMUL
;
1054 vmul
->mask
= full_mask
^ writeout_mask
;
1056 /* Rewrite to use our temp */
1058 for (unsigned i
= 0; i
< ARRAY_SIZE(stages
); ++i
) {
1060 mir_rewrite_index_dst_single(stages
[i
], src
, temp
);
1063 mir_rewrite_index_src_single(branch
, src
, temp
);
1067 mir_choose_alu(&vmul
, instructions
, worklist
, len
, &predicate
, UNIT_VMUL
);
1069 mir_update_worklist(worklist
, len
, instructions
, vmul
);
1070 mir_update_worklist(worklist
, len
, instructions
, sadd
);
1072 bundle
.has_blend_constant
= predicate
.blend_constant
;
1073 bundle
.has_embedded_constants
= predicate
.constant_mask
!= 0;
1075 unsigned padding
= 0;
1077 /* Now that we have finished scheduling, build up the bundle */
1078 midgard_instruction
*stages
[] = { vmul
, sadd
, vadd
, smul
, vlut
, branch
};
1080 for (unsigned i
= 0; i
< ARRAY_SIZE(stages
); ++i
) {
1082 bundle
.control
|= stages
[i
]->unit
;
1083 bytes_emitted
+= bytes_for_instruction(stages
[i
]);
1084 bundle
.instructions
[bundle
.instruction_count
++] = stages
[i
];
1086 /* If we branch, we can't spill to TLS since the store
1087 * instruction will never get executed. We could try to
1088 * break the bundle but this is probably easier for
1092 stages
[i
]->no_spill
|= (1 << REG_CLASS_WORK
);
1096 /* Pad ALU op to nearest word */
1098 if (bytes_emitted
& 15) {
1099 padding
= 16 - (bytes_emitted
& 15);
1100 bytes_emitted
+= padding
;
1103 /* Constants must always be quadwords */
1104 if (bundle
.has_embedded_constants
)
1105 bytes_emitted
+= 16;
1107 /* Size ALU instruction for tag */
1108 bundle
.tag
= (TAG_ALU_4
) + (bytes_emitted
/ 16) - 1;
1110 /* MRT capable GPUs use a special writeout procedure */
1111 if (writeout
&& !(ctx
->quirks
& MIDGARD_NO_UPPER_ALU
))
1114 bundle
.padding
= padding
;
1115 bundle
.control
|= bundle
.tag
;
1120 /* Schedule a single block by iterating its instruction to create bundles.
1121 * While we go, tally about the bundle sizes to compute the block size. */
1125 schedule_block(compiler_context
*ctx
, midgard_block
*block
)
1127 /* Copy list to dynamic array */
1129 midgard_instruction
**instructions
= flatten_mir(block
, &len
);
1134 /* Calculate dependencies and initial worklist */
1135 unsigned node_count
= ctx
->temp_count
+ 1;
1136 mir_create_dependency_graph(instructions
, len
, node_count
);
1138 /* Allocate the worklist */
1139 size_t sz
= BITSET_WORDS(len
) * sizeof(BITSET_WORD
);
1140 BITSET_WORD
*worklist
= calloc(sz
, 1);
1141 mir_initialize_worklist(worklist
, instructions
, len
);
1143 struct util_dynarray bundles
;
1144 util_dynarray_init(&bundles
, NULL
);
1146 block
->quadword_count
= 0;
1147 unsigned blend_offset
= 0;
1150 unsigned tag
= mir_choose_bundle(instructions
, worklist
, len
);
1151 midgard_bundle bundle
;
1153 if (tag
== TAG_TEXTURE_4
)
1154 bundle
= mir_schedule_texture(instructions
, worklist
, len
);
1155 else if (tag
== TAG_LOAD_STORE_4
)
1156 bundle
= mir_schedule_ldst(instructions
, worklist
, len
);
1157 else if (tag
== TAG_ALU_4
)
1158 bundle
= mir_schedule_alu(ctx
, instructions
, worklist
, len
);
1162 util_dynarray_append(&bundles
, midgard_bundle
, bundle
);
1164 if (bundle
.has_blend_constant
)
1165 blend_offset
= block
->quadword_count
;
1167 block
->quadword_count
+= midgard_tag_props
[bundle
.tag
].size
;
1170 /* We emitted bundles backwards; copy into the block in reverse-order */
1172 util_dynarray_init(&block
->bundles
, block
);
1173 util_dynarray_foreach_reverse(&bundles
, midgard_bundle
, bundle
) {
1174 util_dynarray_append(&block
->bundles
, midgard_bundle
, *bundle
);
1176 util_dynarray_fini(&bundles
);
1178 /* Blend constant was backwards as well. blend_offset if set is
1179 * strictly positive, as an offset of zero would imply constants before
1180 * any instructions which is invalid in Midgard. TODO: blend constants
1181 * are broken if you spill since then quadword_count becomes invalid
1185 ctx
->blend_constant_offset
= ((ctx
->quadword_count
+ block
->quadword_count
) - blend_offset
- 1) * 0x10;
1187 block
->scheduled
= true;
1188 ctx
->quadword_count
+= block
->quadword_count
;
1190 /* Reorder instructions to match bundled. First remove existing
1191 * instructions and then recreate the list */
1193 mir_foreach_instr_in_block_safe(block
, ins
) {
1194 list_del(&ins
->link
);
1197 mir_foreach_instr_in_block_scheduled_rev(block
, ins
) {
1198 list_add(&ins
->link
, &block
->base
.instructions
);
1201 free(instructions
); /* Allocated by flatten_mir() */
1206 midgard_schedule_program(compiler_context
*ctx
)
1208 midgard_promote_uniforms(ctx
);
1210 /* Must be lowered right before scheduling */
1211 mir_squeeze_index(ctx
);
1212 mir_lower_special_reads(ctx
);
1213 mir_squeeze_index(ctx
);
1215 /* Lowering can introduce some dead moves */
1217 mir_foreach_block(ctx
, _block
) {
1218 midgard_block
*block
= (midgard_block
*) _block
;
1219 midgard_opt_dead_move_eliminate(ctx
, block
);
1220 schedule_block(ctx
, block
);