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;
202 /* Only 16/32-bit can run on a scalar unit */
203 could_scalar
&= ains
->alu
.reg_mode
!= midgard_reg_mode_8
;
204 could_scalar
&= ains
->alu
.reg_mode
!= midgard_reg_mode_64
;
205 could_scalar
&= ains
->alu
.dest_override
== midgard_dest_override_none
;
207 if (ains
->alu
.reg_mode
== midgard_reg_mode_16
) {
208 /* If we're running in 16-bit mode, we
209 * can't have any 8-bit sources on the
210 * scalar unit (since the scalar unit
211 * doesn't understand 8-bit) */
213 midgard_vector_alu_src s1
=
214 vector_alu_from_unsigned(ains
->alu
.src1
);
216 could_scalar
&= !s1
.half
;
218 midgard_vector_alu_src s2
=
219 vector_alu_from_unsigned(ains
->alu
.src2
);
221 could_scalar
&= !s2
.half
;
227 /* How many bytes does this ALU instruction add to the bundle? */
230 bytes_for_instruction(midgard_instruction
*ains
)
232 if (ains
->unit
& UNITS_ANY_VECTOR
)
233 return sizeof(midgard_reg_info
) + sizeof(midgard_vector_alu
);
234 else if (ains
->unit
== ALU_ENAB_BRANCH
)
235 return sizeof(midgard_branch_extended
);
236 else if (ains
->compact_branch
)
237 return sizeof(ains
->br_compact
);
239 return sizeof(midgard_reg_info
) + sizeof(midgard_scalar_alu
);
242 /* We would like to flatten the linked list of midgard_instructions in a bundle
243 * to an array of pointers on the heap for easy indexing */
245 static midgard_instruction
**
246 flatten_mir(midgard_block
*block
, unsigned *len
)
248 *len
= list_length(&block
->base
.instructions
);
253 midgard_instruction
**instructions
=
254 calloc(sizeof(midgard_instruction
*), *len
);
258 mir_foreach_instr_in_block(block
, ins
)
259 instructions
[i
++] = ins
;
264 /* The worklist is the set of instructions that can be scheduled now; that is,
265 * the set of instructions with no remaining dependencies */
268 mir_initialize_worklist(BITSET_WORD
*worklist
, midgard_instruction
**instructions
, unsigned count
)
270 for (unsigned i
= 0; i
< count
; ++i
) {
271 if (instructions
[i
]->nr_dependencies
== 0)
272 BITSET_SET(worklist
, i
);
276 /* Update the worklist after an instruction terminates. Remove its edges from
277 * the graph and if that causes any node to have no dependencies, add it to the
282 BITSET_WORD
*worklist
, unsigned count
,
283 midgard_instruction
**instructions
, midgard_instruction
*done
)
285 /* Sanity check: if no instruction terminated, there is nothing to do.
286 * If the instruction that terminated had dependencies, that makes no
287 * sense and means we messed up the worklist. Finally, as the purpose
288 * of this routine is to update dependents, we abort early if there are
289 * no dependents defined. */
294 assert(done
->nr_dependencies
== 0);
296 if (!done
->dependents
)
299 /* We have an instruction with dependents. Iterate each dependent to
300 * remove one dependency (`done`), adding dependents to the worklist
304 BITSET_FOREACH_SET(i
, done
->dependents
, count
) {
305 assert(instructions
[i
]->nr_dependencies
);
307 if (!(--instructions
[i
]->nr_dependencies
))
308 BITSET_SET(worklist
, i
);
311 free(done
->dependents
);
314 /* While scheduling, we need to choose instructions satisfying certain
315 * criteria. As we schedule backwards, we choose the *last* instruction in the
316 * worklist to simulate in-order scheduling. Chosen instructions must satisfy a
317 * given predicate. */
319 struct midgard_predicate
{
320 /* TAG or ~0 for dont-care */
323 /* True if we want to pop off the chosen instruction */
326 /* For ALU, choose only this unit */
329 /* State for bundle constants. constants is the actual constants
330 * for the bundle. constant_count is the number of bytes (up to
331 * 16) currently in use for constants. When picking in destructive
332 * mode, the constants array will be updated, and the instruction
333 * will be adjusted to index into the constants array */
335 midgard_constants
*constants
;
336 unsigned constant_mask
;
339 /* Exclude this destination (if not ~0) */
342 /* Don't schedule instructions consuming conditionals (since we already
343 * scheduled one). Excludes conditional branches and csel */
346 /* Require a minimal mask and (if nonzero) given destination. Used for
347 * writeout optimizations */
352 /* For load/store: how many pipeline registers are in use? The two
353 * scheduled instructions cannot use more than the 256-bits of pipeline
354 * space available or RA will fail (as it would run out of pipeline
355 * registers and fail to spill without breaking the schedule) */
357 unsigned pipeline_count
;
360 /* For an instruction that can fit, adjust it to fit and update the constants
361 * array, in destructive mode. Returns whether the fitting was successful. */
364 mir_adjust_constants(midgard_instruction
*ins
,
365 struct midgard_predicate
*pred
,
368 /* Blend constants dominate */
369 if (ins
->has_blend_constant
) {
370 if (pred
->constant_mask
)
372 else if (destructive
) {
373 pred
->blend_constant
= true;
374 pred
->constant_mask
= 0xffff;
379 /* No constant, nothing to adjust */
380 if (!ins
->has_constants
)
383 unsigned r_constant
= SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
384 midgard_reg_mode dst_mode
= mir_typesize(ins
);
386 unsigned bundle_constant_mask
= pred
->constant_mask
;
387 unsigned comp_mapping
[2][16] = { };
388 uint8_t bundle_constants
[16];
390 memcpy(bundle_constants
, pred
->constants
, 16);
392 /* Let's try to find a place for each active component of the constant
395 for (unsigned src
= 0; src
< 2; ++src
) {
396 if (ins
->src
[src
] != SSA_FIXED_REGISTER(REGISTER_CONSTANT
))
399 midgard_reg_mode src_mode
= mir_srcsize(ins
, src
);
400 unsigned type_size
= mir_bytes_for_mode(src_mode
);
401 unsigned max_comp
= 16 / type_size
;
402 unsigned comp_mask
= mir_from_bytemask(mir_bytemask_of_read_components_index(ins
, src
),
404 unsigned type_mask
= (1 << type_size
) - 1;
406 for (unsigned comp
= 0; comp
< max_comp
; comp
++) {
407 if (!(comp_mask
& (1 << comp
)))
410 uint8_t *constantp
= ins
->constants
.u8
+ (type_size
* comp
);
411 unsigned best_reuse_bytes
= 0;
412 signed best_place
= -1;
415 for (i
= 0; i
< 16; i
+= type_size
) {
416 unsigned reuse_bytes
= 0;
418 for (j
= 0; j
< type_size
; j
++) {
419 if (!(bundle_constant_mask
& (1 << (i
+ j
))))
421 if (constantp
[j
] != bundle_constants
[i
+ j
])
427 /* Select the place where existing bytes can be
428 * reused so we leave empty slots to others
430 if (j
== type_size
&&
431 (reuse_bytes
> best_reuse_bytes
|| best_place
< 0)) {
432 best_reuse_bytes
= reuse_bytes
;
438 /* This component couldn't fit in the remaining constant slot,
439 * no need check the remaining components, bail out now
444 memcpy(&bundle_constants
[i
], constantp
, type_size
);
445 bundle_constant_mask
|= type_mask
<< best_place
;
446 comp_mapping
[src
][comp
] = best_place
/ type_size
;
450 /* If non-destructive, we're done */
454 /* Otherwise update the constant_mask and constant values */
455 pred
->constant_mask
= bundle_constant_mask
;
456 memcpy(pred
->constants
, bundle_constants
, 16);
458 /* Use comp_mapping as a swizzle */
459 mir_foreach_src(ins
, s
) {
460 if (ins
->src
[s
] == r_constant
)
461 mir_compose_swizzle(ins
->swizzle
[s
], comp_mapping
[s
], ins
->swizzle
[s
]);
467 /* Conservative estimate of the pipeline registers required for load/store */
470 mir_pipeline_count(midgard_instruction
*ins
)
472 unsigned bytecount
= 0;
474 mir_foreach_src(ins
, i
) {
475 /* Skip empty source */
476 if (ins
->src
[i
] == ~0) continue;
478 unsigned bytemask
= mir_bytemask_of_read_components_index(ins
, i
);
480 unsigned max
= util_logbase2(bytemask
) + 1;
484 return DIV_ROUND_UP(bytecount
, 16);
487 static midgard_instruction
*
488 mir_choose_instruction(
489 midgard_instruction
**instructions
,
490 BITSET_WORD
*worklist
, unsigned count
,
491 struct midgard_predicate
*predicate
)
493 /* Parse the predicate */
494 unsigned tag
= predicate
->tag
;
495 bool alu
= tag
== TAG_ALU_4
;
496 bool ldst
= tag
== TAG_LOAD_STORE_4
;
497 unsigned unit
= predicate
->unit
;
498 bool branch
= alu
&& (unit
== ALU_ENAB_BR_COMPACT
);
499 bool scalar
= (unit
!= ~0) && (unit
& UNITS_SCALAR
);
500 bool no_cond
= predicate
->no_cond
;
502 unsigned mask
= predicate
->mask
;
503 unsigned dest
= predicate
->dest
;
504 bool needs_dest
= mask
& 0xF;
506 /* Iterate to find the best instruction satisfying the predicate */
509 signed best_index
= -1;
510 bool best_conditional
= false;
512 /* Enforce a simple metric limiting distance to keep down register
513 * pressure. TOOD: replace with liveness tracking for much better
516 unsigned max_active
= 0;
517 unsigned max_distance
= 6;
519 BITSET_FOREACH_SET(i
, worklist
, count
) {
520 max_active
= MAX2(max_active
, i
);
523 BITSET_FOREACH_SET(i
, worklist
, count
) {
524 if ((max_active
- i
) >= max_distance
)
527 if (tag
!= ~0 && instructions
[i
]->type
!= tag
)
530 if (predicate
->exclude
!= ~0 && instructions
[i
]->dest
== predicate
->exclude
)
533 if (alu
&& !branch
&& !(alu_opcode_props
[instructions
[i
]->alu
.op
].props
& unit
))
536 if (branch
&& !instructions
[i
]->compact_branch
)
539 if (alu
&& scalar
&& !mir_is_scalar(instructions
[i
]))
542 if (alu
&& !mir_adjust_constants(instructions
[i
], predicate
, false))
545 if (needs_dest
&& instructions
[i
]->dest
!= dest
)
548 if (mask
&& ((~instructions
[i
]->mask
) & mask
))
551 if (ldst
&& mir_pipeline_count(instructions
[i
]) + predicate
->pipeline_count
> 2)
554 bool conditional
= alu
&& !branch
&& OP_IS_CSEL(instructions
[i
]->alu
.op
);
555 conditional
|= (branch
&& instructions
[i
]->branch
.conditional
);
557 if (conditional
&& no_cond
)
560 /* Simulate in-order scheduling */
561 if ((signed) i
< best_index
)
565 best_conditional
= conditional
;
569 /* Did we find anything? */
574 /* If we found something, remove it from the worklist */
575 assert(best_index
< count
);
577 if (predicate
->destructive
) {
578 BITSET_CLEAR(worklist
, best_index
);
581 mir_adjust_constants(instructions
[best_index
], predicate
, true);
584 predicate
->pipeline_count
+= mir_pipeline_count(instructions
[best_index
]);
586 /* Once we schedule a conditional, we can't again */
587 predicate
->no_cond
|= best_conditional
;
590 return instructions
[best_index
];
593 /* Still, we don't choose instructions in a vacuum. We need a way to choose the
594 * best bundle type (ALU, load/store, texture). Nondestructive. */
598 midgard_instruction
**instructions
,
599 BITSET_WORD
*worklist
, unsigned count
)
601 /* At the moment, our algorithm is very simple - use the bundle of the
602 * best instruction, regardless of what else could be scheduled
603 * alongside it. This is not optimal but it works okay for in-order */
605 struct midgard_predicate predicate
= {
607 .destructive
= false,
611 midgard_instruction
*chosen
= mir_choose_instruction(instructions
, worklist
, count
, &predicate
);
619 /* We want to choose an ALU instruction filling a given unit */
621 mir_choose_alu(midgard_instruction
**slot
,
622 midgard_instruction
**instructions
,
623 BITSET_WORD
*worklist
, unsigned len
,
624 struct midgard_predicate
*predicate
,
627 /* Did we already schedule to this slot? */
631 /* Try to schedule something, if not */
632 predicate
->unit
= unit
;
633 *slot
= mir_choose_instruction(instructions
, worklist
, len
, predicate
);
635 /* Store unit upon scheduling */
636 if (*slot
&& !((*slot
)->compact_branch
))
637 (*slot
)->unit
= unit
;
640 /* When we are scheduling a branch/csel, we need the consumed condition in the
641 * same block as a pipeline register. There are two options to enable this:
643 * - Move the conditional into the bundle. Preferred, but only works if the
644 * conditional is used only once and is from this block.
645 * - Copy the conditional.
647 * We search for the conditional. If it's in this block, single-use, and
648 * without embedded constants, we schedule it immediately. Otherwise, we
649 * schedule a move for it.
651 * mir_comparison_mobile is a helper to find the moveable condition.
655 mir_comparison_mobile(
656 compiler_context
*ctx
,
657 midgard_instruction
**instructions
,
658 struct midgard_predicate
*predicate
,
662 if (!mir_single_use(ctx
, cond
))
667 for (unsigned i
= 0; i
< count
; ++i
) {
668 if (instructions
[i
]->dest
!= cond
)
671 /* Must fit in an ALU bundle */
672 if (instructions
[i
]->type
!= TAG_ALU_4
)
675 /* If it would itself require a condition, that's recursive */
676 if (OP_IS_CSEL(instructions
[i
]->alu
.op
))
679 /* We'll need to rewrite to .w but that doesn't work for vector
680 * ops that don't replicate (ball/bany), so bail there */
682 if (GET_CHANNEL_COUNT(alu_opcode_props
[instructions
[i
]->alu
.op
].props
))
685 /* Ensure it will fit with constants */
687 if (!mir_adjust_constants(instructions
[i
], predicate
, false))
690 /* Ensure it is written only once */
698 /* Inject constants now that we are sure we want to */
700 mir_adjust_constants(instructions
[ret
], predicate
, true);
705 /* Using the information about the moveable conditional itself, we either pop
706 * that condition off the worklist for use now, or create a move to
707 * artificially schedule instead as a fallback */
709 static midgard_instruction
*
710 mir_schedule_comparison(
711 compiler_context
*ctx
,
712 midgard_instruction
**instructions
,
713 struct midgard_predicate
*predicate
,
714 BITSET_WORD
*worklist
, unsigned count
,
715 unsigned cond
, bool vector
, unsigned *swizzle
,
716 midgard_instruction
*user
)
718 /* TODO: swizzle when scheduling */
720 (!vector
&& (swizzle
[0] == 0)) ?
721 mir_comparison_mobile(ctx
, instructions
, predicate
, count
, cond
) : ~0;
723 /* If we can, schedule the condition immediately */
724 if ((comp_i
!= ~0) && BITSET_TEST(worklist
, comp_i
)) {
725 assert(comp_i
< count
);
726 BITSET_CLEAR(worklist
, comp_i
);
727 return instructions
[comp_i
];
730 /* Otherwise, we insert a move */
732 midgard_instruction mov
= v_mov(cond
, cond
);
733 mov
.mask
= vector
? 0xF : 0x1;
734 memcpy(mov
.swizzle
[1], swizzle
, sizeof(mov
.swizzle
[1]));
736 return mir_insert_instruction_before(ctx
, user
, mov
);
739 /* Most generally, we need instructions writing to r31 in the appropriate
742 static midgard_instruction
*
743 mir_schedule_condition(compiler_context
*ctx
,
744 struct midgard_predicate
*predicate
,
745 BITSET_WORD
*worklist
, unsigned count
,
746 midgard_instruction
**instructions
,
747 midgard_instruction
*last
)
749 /* For a branch, the condition is the only argument; for csel, third */
750 bool branch
= last
->compact_branch
;
751 unsigned condition_index
= branch
? 0 : 2;
753 /* csel_v is vector; otherwise, conditions are scalar */
754 bool vector
= !branch
&& OP_IS_CSEL_V(last
->alu
.op
);
756 /* Grab the conditional instruction */
758 midgard_instruction
*cond
= mir_schedule_comparison(
759 ctx
, instructions
, predicate
, worklist
, count
, last
->src
[condition_index
],
760 vector
, last
->swizzle
[2], last
);
762 /* We have exclusive reign over this (possibly move) conditional
763 * instruction. We can rewrite into a pipeline conditional register */
765 predicate
->exclude
= cond
->dest
;
766 cond
->dest
= SSA_FIXED_REGISTER(31);
769 cond
->mask
= (1 << COMPONENT_W
);
771 mir_foreach_src(cond
, s
) {
772 if (cond
->src
[s
] == ~0)
775 for (unsigned q
= 0; q
< 4; ++q
)
776 cond
->swizzle
[s
][q
+ COMPONENT_W
] = cond
->swizzle
[s
][q
];
780 /* Schedule the unit: csel is always in the latter pipeline, so a csel
781 * condition must be in the former pipeline stage (vmul/sadd),
782 * depending on scalar/vector of the instruction itself. A branch must
783 * be written from the latter pipeline stage and a branch condition is
784 * always scalar, so it is always in smul (exception: ball/bany, which
788 cond
->unit
= UNIT_SMUL
;
790 cond
->unit
= vector
? UNIT_VMUL
: UNIT_SADD
;
795 /* Schedules a single bundle of the given type */
797 static midgard_bundle
798 mir_schedule_texture(
799 midgard_instruction
**instructions
,
800 BITSET_WORD
*worklist
, unsigned len
)
802 struct midgard_predicate predicate
= {
803 .tag
= TAG_TEXTURE_4
,
808 midgard_instruction
*ins
=
809 mir_choose_instruction(instructions
, worklist
, len
, &predicate
);
811 mir_update_worklist(worklist
, len
, instructions
, ins
);
813 struct midgard_bundle out
= {
814 .tag
= ins
->texture
.op
== TEXTURE_OP_BARRIER
?
815 TAG_TEXTURE_4_BARRIER
: TAG_TEXTURE_4
,
816 .instruction_count
= 1,
817 .instructions
= { ins
}
823 static midgard_bundle
825 midgard_instruction
**instructions
,
826 BITSET_WORD
*worklist
, unsigned len
)
828 struct midgard_predicate predicate
= {
829 .tag
= TAG_LOAD_STORE_4
,
834 /* Try to pick two load/store ops. Second not gauranteed to exist */
836 midgard_instruction
*ins
=
837 mir_choose_instruction(instructions
, worklist
, len
, &predicate
);
839 midgard_instruction
*pair
=
840 mir_choose_instruction(instructions
, worklist
, len
, &predicate
);
842 struct midgard_bundle out
= {
843 .tag
= TAG_LOAD_STORE_4
,
844 .instruction_count
= pair
? 2 : 1,
845 .instructions
= { ins
, pair
}
848 /* We have to update the worklist atomically, since the two
849 * instructions run concurrently (TODO: verify it's not pipelined) */
851 mir_update_worklist(worklist
, len
, instructions
, ins
);
852 mir_update_worklist(worklist
, len
, instructions
, pair
);
857 static midgard_bundle
859 compiler_context
*ctx
,
860 midgard_instruction
**instructions
,
861 BITSET_WORD
*worklist
, unsigned len
)
863 struct midgard_bundle bundle
= {};
865 unsigned bytes_emitted
= sizeof(bundle
.control
);
867 struct midgard_predicate predicate
= {
871 .constants
= &bundle
.constants
874 midgard_instruction
*vmul
= NULL
;
875 midgard_instruction
*vadd
= NULL
;
876 midgard_instruction
*vlut
= NULL
;
877 midgard_instruction
*smul
= NULL
;
878 midgard_instruction
*sadd
= NULL
;
879 midgard_instruction
*branch
= NULL
;
881 mir_choose_alu(&branch
, instructions
, worklist
, len
, &predicate
, ALU_ENAB_BR_COMPACT
);
882 mir_update_worklist(worklist
, len
, instructions
, branch
);
883 bool writeout
= branch
&& branch
->writeout
;
884 bool zs_writeout
= writeout
&& (branch
->writeout_depth
| branch
->writeout_stencil
);
886 if (branch
&& branch
->branch
.conditional
) {
887 midgard_instruction
*cond
= mir_schedule_condition(ctx
, &predicate
, worklist
, len
, instructions
, branch
);
889 if (cond
->unit
== UNIT_VADD
)
891 else if (cond
->unit
== UNIT_SMUL
)
894 unreachable("Bad condition");
897 /* If we have a render target reference, schedule a move for it. Since
898 * this will be in sadd, we boost this to prevent scheduling csel into
901 if (writeout
&& (branch
->constants
.u32
[0] || ctx
->is_blend
)) {
902 sadd
= ralloc(ctx
, midgard_instruction
);
903 *sadd
= v_mov(~0, make_compiler_temp(ctx
));
904 sadd
->unit
= UNIT_SADD
;
906 sadd
->has_inline_constant
= true;
907 sadd
->inline_constant
= branch
->constants
.u32
[0];
908 branch
->src
[1] = sadd
->dest
;
910 /* Mask off any conditionals. Could be optimized to just scalar
911 * conditionals TODO */
912 predicate
.no_cond
= true;
915 mir_choose_alu(&smul
, instructions
, worklist
, len
, &predicate
, UNIT_SMUL
);
918 mir_choose_alu(&vlut
, instructions
, worklist
, len
, &predicate
, UNIT_VLUT
);
921 bundle
.last_writeout
= branch
->last_writeout
;
924 if (writeout
&& !zs_writeout
) {
925 vadd
= ralloc(ctx
, midgard_instruction
);
926 *vadd
= v_mov(~0, make_compiler_temp(ctx
));
928 if (!ctx
->is_blend
) {
929 vadd
->alu
.op
= midgard_alu_op_iadd
;
930 vadd
->src
[0] = SSA_FIXED_REGISTER(31);
932 for (unsigned c
= 0; c
< 16; ++c
)
933 vadd
->swizzle
[0][c
] = COMPONENT_X
;
935 vadd
->has_inline_constant
= true;
936 vadd
->inline_constant
= 0;
938 vadd
->src
[1] = SSA_FIXED_REGISTER(1);
940 for (unsigned c
= 0; c
< 16; ++c
)
941 vadd
->swizzle
[1][c
] = COMPONENT_W
;
944 vadd
->unit
= UNIT_VADD
;
946 branch
->src
[2] = vadd
->dest
;
949 mir_choose_alu(&vadd
, instructions
, worklist
, len
, &predicate
, UNIT_VADD
);
951 mir_update_worklist(worklist
, len
, instructions
, vlut
);
952 mir_update_worklist(worklist
, len
, instructions
, vadd
);
953 mir_update_worklist(worklist
, len
, instructions
, smul
);
955 bool vadd_csel
= vadd
&& OP_IS_CSEL(vadd
->alu
.op
);
956 bool smul_csel
= smul
&& OP_IS_CSEL(smul
->alu
.op
);
958 if (vadd_csel
|| smul_csel
) {
959 midgard_instruction
*ins
= vadd_csel
? vadd
: smul
;
960 midgard_instruction
*cond
= mir_schedule_condition(ctx
, &predicate
, worklist
, len
, instructions
, ins
);
962 if (cond
->unit
== UNIT_VMUL
)
964 else if (cond
->unit
== UNIT_SADD
)
967 unreachable("Bad condition");
970 /* Stage 2, let's schedule sadd before vmul for writeout */
971 mir_choose_alu(&sadd
, instructions
, worklist
, len
, &predicate
, UNIT_SADD
);
973 /* Check if writeout reads its own register */
976 midgard_instruction
*stages
[] = { sadd
, vadd
, smul
};
977 unsigned src
= (branch
->src
[0] == ~0) ? SSA_FIXED_REGISTER(zs_writeout
? 1 : 0) : branch
->src
[0];
978 unsigned writeout_mask
= 0x0;
979 bool bad_writeout
= false;
981 for (unsigned i
= 0; i
< ARRAY_SIZE(stages
); ++i
) {
985 if (stages
[i
]->dest
!= src
)
988 writeout_mask
|= stages
[i
]->mask
;
989 bad_writeout
|= mir_has_arg(stages
[i
], branch
->src
[0]);
992 /* It's possible we'll be able to schedule something into vmul
993 * to fill r0/r1. Let's peak into the future, trying to schedule
994 * vmul specially that way. */
996 unsigned full_mask
= zs_writeout
?
997 (1 << (branch
->writeout_depth
+ branch
->writeout_stencil
)) - 1 :
1000 if (!bad_writeout
&& writeout_mask
!= full_mask
) {
1001 predicate
.unit
= UNIT_VMUL
;
1002 predicate
.dest
= src
;
1003 predicate
.mask
= writeout_mask
^ full_mask
;
1005 struct midgard_instruction
*peaked
=
1006 mir_choose_instruction(instructions
, worklist
, len
, &predicate
);
1010 vmul
->unit
= UNIT_VMUL
;
1011 writeout_mask
|= predicate
.mask
;
1012 assert(writeout_mask
== full_mask
);
1016 predicate
.dest
= predicate
.mask
= 0;
1019 /* Finally, add a move if necessary */
1020 if (bad_writeout
|| writeout_mask
!= full_mask
) {
1021 unsigned temp
= (branch
->src
[0] == ~0) ? SSA_FIXED_REGISTER(zs_writeout
? 1 : 0) : make_compiler_temp(ctx
);
1023 vmul
= ralloc(ctx
, midgard_instruction
);
1024 *vmul
= v_mov(src
, temp
);
1025 vmul
->unit
= UNIT_VMUL
;
1026 vmul
->mask
= full_mask
^ writeout_mask
;
1028 /* Rewrite to use our temp */
1030 for (unsigned i
= 0; i
< ARRAY_SIZE(stages
); ++i
) {
1032 mir_rewrite_index_dst_single(stages
[i
], src
, temp
);
1035 mir_rewrite_index_src_single(branch
, src
, temp
);
1039 mir_choose_alu(&vmul
, instructions
, worklist
, len
, &predicate
, UNIT_VMUL
);
1041 mir_update_worklist(worklist
, len
, instructions
, vmul
);
1042 mir_update_worklist(worklist
, len
, instructions
, sadd
);
1044 bundle
.has_blend_constant
= predicate
.blend_constant
;
1045 bundle
.has_embedded_constants
= predicate
.constant_mask
!= 0;
1047 unsigned padding
= 0;
1049 /* Now that we have finished scheduling, build up the bundle */
1050 midgard_instruction
*stages
[] = { vmul
, sadd
, vadd
, smul
, vlut
, branch
};
1052 for (unsigned i
= 0; i
< ARRAY_SIZE(stages
); ++i
) {
1054 bundle
.control
|= stages
[i
]->unit
;
1055 bytes_emitted
+= bytes_for_instruction(stages
[i
]);
1056 bundle
.instructions
[bundle
.instruction_count
++] = stages
[i
];
1058 /* If we branch, we can't spill to TLS since the store
1059 * instruction will never get executed. We could try to
1060 * break the bundle but this is probably easier for
1064 stages
[i
]->no_spill
|= (1 << REG_CLASS_WORK
);
1068 /* Pad ALU op to nearest word */
1070 if (bytes_emitted
& 15) {
1071 padding
= 16 - (bytes_emitted
& 15);
1072 bytes_emitted
+= padding
;
1075 /* Constants must always be quadwords */
1076 if (bundle
.has_embedded_constants
)
1077 bytes_emitted
+= 16;
1079 /* Size ALU instruction for tag */
1080 bundle
.tag
= (TAG_ALU_4
) + (bytes_emitted
/ 16) - 1;
1082 /* MRT capable GPUs use a special writeout procedure */
1083 if (writeout
&& !(ctx
->quirks
& MIDGARD_NO_UPPER_ALU
))
1086 bundle
.padding
= padding
;
1087 bundle
.control
|= bundle
.tag
;
1092 /* Schedule a single block by iterating its instruction to create bundles.
1093 * While we go, tally about the bundle sizes to compute the block size. */
1097 schedule_block(compiler_context
*ctx
, midgard_block
*block
)
1099 /* Copy list to dynamic array */
1101 midgard_instruction
**instructions
= flatten_mir(block
, &len
);
1106 /* Calculate dependencies and initial worklist */
1107 unsigned node_count
= ctx
->temp_count
+ 1;
1108 mir_create_dependency_graph(instructions
, len
, node_count
);
1110 /* Allocate the worklist */
1111 size_t sz
= BITSET_WORDS(len
) * sizeof(BITSET_WORD
);
1112 BITSET_WORD
*worklist
= calloc(sz
, 1);
1113 mir_initialize_worklist(worklist
, instructions
, len
);
1115 struct util_dynarray bundles
;
1116 util_dynarray_init(&bundles
, NULL
);
1118 block
->quadword_count
= 0;
1119 unsigned blend_offset
= 0;
1122 unsigned tag
= mir_choose_bundle(instructions
, worklist
, len
);
1123 midgard_bundle bundle
;
1125 if (tag
== TAG_TEXTURE_4
)
1126 bundle
= mir_schedule_texture(instructions
, worklist
, len
);
1127 else if (tag
== TAG_LOAD_STORE_4
)
1128 bundle
= mir_schedule_ldst(instructions
, worklist
, len
);
1129 else if (tag
== TAG_ALU_4
)
1130 bundle
= mir_schedule_alu(ctx
, instructions
, worklist
, len
);
1134 util_dynarray_append(&bundles
, midgard_bundle
, bundle
);
1136 if (bundle
.has_blend_constant
)
1137 blend_offset
= block
->quadword_count
;
1139 block
->quadword_count
+= midgard_tag_props
[bundle
.tag
].size
;
1142 /* We emitted bundles backwards; copy into the block in reverse-order */
1144 util_dynarray_init(&block
->bundles
, block
);
1145 util_dynarray_foreach_reverse(&bundles
, midgard_bundle
, bundle
) {
1146 util_dynarray_append(&block
->bundles
, midgard_bundle
, *bundle
);
1148 util_dynarray_fini(&bundles
);
1150 /* Blend constant was backwards as well. blend_offset if set is
1151 * strictly positive, as an offset of zero would imply constants before
1152 * any instructions which is invalid in Midgard. TODO: blend constants
1153 * are broken if you spill since then quadword_count becomes invalid
1157 ctx
->blend_constant_offset
= ((ctx
->quadword_count
+ block
->quadword_count
) - blend_offset
- 1) * 0x10;
1159 block
->scheduled
= true;
1160 ctx
->quadword_count
+= block
->quadword_count
;
1162 /* Reorder instructions to match bundled. First remove existing
1163 * instructions and then recreate the list */
1165 mir_foreach_instr_in_block_safe(block
, ins
) {
1166 list_del(&ins
->link
);
1169 mir_foreach_instr_in_block_scheduled_rev(block
, ins
) {
1170 list_add(&ins
->link
, &block
->base
.instructions
);
1173 free(instructions
); /* Allocated by flatten_mir() */
1178 midgard_schedule_program(compiler_context
*ctx
)
1180 midgard_promote_uniforms(ctx
);
1182 /* Must be lowered right before scheduling */
1183 mir_squeeze_index(ctx
);
1184 mir_lower_special_reads(ctx
);
1185 mir_squeeze_index(ctx
);
1187 /* Lowering can introduce some dead moves */
1189 mir_foreach_block(ctx
, _block
) {
1190 midgard_block
*block
= (midgard_block
*) _block
;
1191 midgard_opt_dead_move_eliminate(ctx
, block
);
1192 schedule_block(ctx
, block
);