2 * Copyright (C) 2019 Google, Inc.
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
24 * Rob Clark <robclark@freedesktop.org>
29 #include "util/u_math.h"
32 #include "ir3_compiler.h"
33 #include "ir3_context.h"
36 #define SCHED_DEBUG (ir3_shader_debug & IR3_DBG_SCHEDMSGS)
40 #define d(fmt, ...) do { if (SCHED_DEBUG) { \
41 printf("PSCHED: "fmt"\n", ##__VA_ARGS__); \
44 #define di(instr, fmt, ...) do { if (SCHED_DEBUG) { \
45 printf("PSCHED: "fmt": ", ##__VA_ARGS__); \
46 ir3_print_instr(instr); \
50 * Post RA Instruction Scheduling
53 struct ir3_postsched_ctx
{
54 struct ir3_context
*ctx
;
57 struct ir3_block
*block
; /* the current block */
60 struct list_head unscheduled_list
; /* unscheduled instructions */
61 struct ir3_instruction
*scheduled
; /* last scheduled instr */
62 struct ir3_instruction
*pred
; /* current p0.x user, if any */
69 struct ir3_postsched_node
{
70 struct dag_node dag
; /* must be first for util_dynarray_foreach */
71 struct ir3_instruction
*instr
;
72 bool partially_evaluated_path
;
78 #define foreach_sched_node(__n, __list) \
79 list_for_each_entry(struct ir3_postsched_node, __n, __list, dag.link)
81 #define foreach_bit(b, mask) \
82 for (uint32_t _m = ({debug_assert((mask) >= 1); (mask);}); _m && ({(b) = u_bit_scan(&_m); 1;});)
85 schedule(struct ir3_postsched_ctx
*ctx
, struct ir3_instruction
*instr
)
87 debug_assert(ctx
->block
== instr
->block
);
89 /* remove from unscheduled_list:
91 list_delinit(&instr
->node
);
93 if (writes_pred(instr
)) {
97 di(instr
, "schedule");
99 list_addtail(&instr
->node
, &instr
->block
->instr_list
);
100 ctx
->scheduled
= instr
;
104 } else if (ctx
->sfu_delay
> 0) {
108 struct ir3_postsched_node
*n
= instr
->data
;
109 dag_prune_head(ctx
->dag
, &n
->dag
);
113 dump_state(struct ir3_postsched_ctx
*ctx
)
118 foreach_sched_node (n
, &ctx
->dag
->heads
) {
119 di(n
->instr
, "maxdel=%3d ", n
->max_delay
);
121 util_dynarray_foreach(&n
->dag
.edges
, struct dag_edge
, edge
) {
122 struct ir3_postsched_node
*child
=
123 (struct ir3_postsched_node
*)edge
->child
;
125 di(child
->instr
, " -> (%d parents) ", child
->dag
.parent_count
);
130 /* Determine if this is an instruction that we'd prefer not to schedule
131 * yet, in order to avoid an (ss) sync. This is limited by the sfu_delay
132 * counter, ie. the more cycles it has been since the last SFU, the less
133 * costly a sync would be.
136 would_sync(struct ir3_postsched_ctx
*ctx
, struct ir3_instruction
*instr
)
138 if (ctx
->sfu_delay
) {
139 struct ir3_register
*reg
;
140 foreach_src (reg
, instr
)
141 if (reg
->instr
&& is_sfu(reg
->instr
))
148 /* find instruction to schedule: */
149 static struct ir3_instruction
*
150 choose_instr(struct ir3_postsched_ctx
*ctx
)
152 struct ir3_postsched_node
*chosen
= NULL
;
156 foreach_sched_node (n
, &ctx
->dag
->heads
) {
157 if (!is_meta(n
->instr
))
160 if (!chosen
|| (chosen
->max_delay
< n
->max_delay
))
165 di(chosen
->instr
, "prio: chose (meta)");
166 return chosen
->instr
;
169 /* Try to schedule inputs with a higher priority, if possible, as
170 * the last bary.f unlocks varying storage to unblock more VS
173 foreach_sched_node (n
, &ctx
->dag
->heads
) {
174 if (!is_input(n
->instr
))
177 if (!chosen
|| (chosen
->max_delay
< n
->max_delay
))
182 di(chosen
->instr
, "prio: chose (input)");
183 return chosen
->instr
;
186 /* Next prioritize discards: */
187 foreach_sched_node (n
, &ctx
->dag
->heads
) {
188 unsigned d
= ir3_delay_calc(ctx
->block
, n
->instr
, false, false);
193 if (!is_kill(n
->instr
))
196 if (!chosen
|| (chosen
->max_delay
< n
->max_delay
))
201 di(chosen
->instr
, "csp: chose (kill, hard ready)");
202 return chosen
->instr
;
205 /* Next prioritize expensive instructions: */
206 foreach_sched_node (n
, &ctx
->dag
->heads
) {
207 unsigned d
= ir3_delay_calc(ctx
->block
, n
->instr
, false, false);
212 if (!(is_sfu(n
->instr
) || is_tex(n
->instr
)))
215 if (!chosen
|| (chosen
->max_delay
< n
->max_delay
))
220 di(chosen
->instr
, "csp: chose (sfu/tex, hard ready)");
221 return chosen
->instr
;
225 * Sometimes be better to take a nop, rather than scheduling an
226 * instruction that would require an (ss) shortly after another
227 * SFU.. ie. if last SFU was just one or two instr ago, and we
228 * could choose between taking a nop and then scheduling
229 * something else, vs scheduling the immed avail instruction that
230 * would require (ss), we are better with the nop.
232 for (unsigned delay
= 0; delay
< 4; delay
++) {
233 foreach_sched_node (n
, &ctx
->dag
->heads
) {
234 if (would_sync(ctx
, n
->instr
))
237 unsigned d
= ir3_delay_calc(ctx
->block
, n
->instr
, true, false);
242 if (!chosen
|| (chosen
->max_delay
< n
->max_delay
))
247 di(chosen
->instr
, "csp: chose (soft ready, delay=%u)", delay
);
248 return chosen
->instr
;
252 /* Next try to find a ready leader w/ soft delay (ie. including extra
253 * delay for things like tex fetch which can be synchronized w/ sync
254 * bit (but we probably do want to schedule some other instructions
257 foreach_sched_node (n
, &ctx
->dag
->heads
) {
258 unsigned d
= ir3_delay_calc(ctx
->block
, n
->instr
, true, false);
263 if (!chosen
|| (chosen
->max_delay
< n
->max_delay
))
268 di(chosen
->instr
, "csp: chose (soft ready)");
269 return chosen
->instr
;
272 /* Next try to find a ready leader that can be scheduled without nop's,
273 * which in the case of things that need (sy)/(ss) could result in
274 * stalls.. but we've already decided there is not a better option.
276 foreach_sched_node (n
, &ctx
->dag
->heads
) {
277 unsigned d
= ir3_delay_calc(ctx
->block
, n
->instr
, false, false);
282 if (!chosen
|| (chosen
->max_delay
< n
->max_delay
))
287 di(chosen
->instr
, "csp: chose (hard ready)");
288 return chosen
->instr
;
291 /* Otherwise choose leader with maximum cost:
293 * TODO should we try to balance cost and delays? I guess it is
294 * a balance between now-nop's and future-nop's?
296 foreach_sched_node (n
, &ctx
->dag
->heads
) {
297 if (!chosen
|| chosen
->max_delay
< n
->max_delay
)
302 di(chosen
->instr
, "csp: chose (leader)");
303 return chosen
->instr
;
309 struct ir3_postsched_deps_state
{
310 struct ir3_context
*ctx
;
312 enum { F
, R
} direction
;
316 /* Track the mapping between sched node (instruction) that last
317 * wrote a given register (in whichever direction we are iterating
320 * Note, this table is twice as big as the # of regs, to deal with
321 * half-precision regs. The approach differs depending on whether
322 * the half and full precision register files are "merged" (conflict,
323 * ie. a6xx+) in which case we consider each full precision dep
324 * as two half-precision dependencies, vs older separate (non-
325 * conflicting) in which case the first half of the table is used
326 * for full precision and 2nd half for half-precision.
328 struct ir3_postsched_node
*regs
[2 * 256];
331 /* bounds checking read/write accessors, since OoB access to stuff on
332 * the stack is gonna cause a bad day.
334 #define dep_reg(state, idx) *({ \
335 assert((idx) < ARRAY_SIZE((state)->regs)); \
336 &(state)->regs[(idx)]; \
340 add_dep(struct ir3_postsched_deps_state
*state
,
341 struct ir3_postsched_node
*before
,
342 struct ir3_postsched_node
*after
)
344 if (!before
|| !after
)
347 assert(before
!= after
);
349 if (state
->direction
== F
) {
350 dag_add_edge(&before
->dag
, &after
->dag
, NULL
);
352 dag_add_edge(&after
->dag
, &before
->dag
, NULL
);
357 add_single_reg_dep(struct ir3_postsched_deps_state
*state
,
358 struct ir3_postsched_node
*node
, unsigned num
, bool write
)
360 add_dep(state
, dep_reg(state
, num
), node
);
362 dep_reg(state
, num
) = node
;
366 /* This is where we handled full vs half-precision, and potential conflicts
367 * between half and full precision that result in additional dependencies.
368 * The 'reg' arg is really just to know half vs full precision.
371 add_reg_dep(struct ir3_postsched_deps_state
*state
,
372 struct ir3_postsched_node
*node
, const struct ir3_register
*reg
,
373 unsigned num
, bool write
)
376 if (reg
->flags
& IR3_REG_HALF
) {
377 /* single conflict in half-reg space: */
378 add_single_reg_dep(state
, node
, num
, write
);
380 /* two conflicts in half-reg space: */
381 add_single_reg_dep(state
, node
, 2 * num
+ 0, write
);
382 add_single_reg_dep(state
, node
, 2 * num
+ 1, write
);
385 if (reg
->flags
& IR3_REG_HALF
)
386 num
+= ARRAY_SIZE(state
->regs
) / 2;
387 add_single_reg_dep(state
, node
, num
, write
);
392 calculate_deps(struct ir3_postsched_deps_state
*state
,
393 struct ir3_postsched_node
*node
)
395 static const struct ir3_register half_reg
= { .flags
= IR3_REG_HALF
};
396 struct ir3_register
*reg
;
399 /* Add dependencies on instructions that previously (or next,
400 * in the reverse direction) wrote any of our src registers:
402 foreach_src_n (reg
, i
, node
->instr
) {
403 /* NOTE: relative access for a src can be either const or gpr: */
404 if (reg
->flags
& IR3_REG_RELATIV
) {
405 /* also reads a0.x: */
406 add_reg_dep(state
, node
, &half_reg
, regid(REG_A0
, 0), false);
409 if (reg
->flags
& (IR3_REG_CONST
| IR3_REG_IMMED
))
412 if (reg
->flags
& IR3_REG_RELATIV
) {
413 /* mark entire array as read: */
414 struct ir3_array
*arr
= ir3_lookup_array(state
->ctx
->ir
, reg
->array
.id
);
415 for (unsigned i
= 0; i
< arr
->length
; i
++) {
416 add_reg_dep(state
, node
, reg
, arr
->reg
+ i
, false);
419 foreach_bit (b
, reg
->wrmask
) {
420 add_reg_dep(state
, node
, reg
, reg
->num
+ b
, false);
422 struct ir3_postsched_node
*dep
= dep_reg(state
, reg
->num
+ b
);
423 if (dep
&& (state
->direction
== F
)) {
424 unsigned d
= ir3_delayslots(dep
->instr
, node
->instr
, i
, true);
425 node
->delay
= MAX2(node
->delay
, d
);
431 if (dest_regs(node
->instr
) == 0)
434 /* And then after we update the state for what this instruction
437 reg
= node
->instr
->regs
[0];
438 if (reg
->flags
& IR3_REG_RELATIV
) {
439 /* mark the entire array as written: */
440 struct ir3_array
*arr
= ir3_lookup_array(state
->ctx
->ir
, reg
->array
.id
);
441 for (unsigned i
= 0; i
< arr
->length
; i
++) {
442 add_reg_dep(state
, node
, reg
, arr
->reg
+ i
, true);
445 /* also reads a0.x: */
446 add_reg_dep(state
, node
, &half_reg
, regid(REG_A0
, 0), false);
448 foreach_bit (b
, reg
->wrmask
) {
449 add_reg_dep(state
, node
, reg
, reg
->num
+ b
, true);
455 calculate_forward_deps(struct ir3_postsched_ctx
*ctx
)
457 struct ir3_postsched_deps_state state
= {
460 .merged
= ctx
->ctx
->compiler
->gpu_id
>= 600,
463 foreach_instr (instr
, &ctx
->unscheduled_list
) {
464 calculate_deps(&state
, instr
->data
);
469 calculate_reverse_deps(struct ir3_postsched_ctx
*ctx
)
471 struct ir3_postsched_deps_state state
= {
474 .merged
= ctx
->ctx
->compiler
->gpu_id
>= 600,
477 foreach_instr_rev (instr
, &ctx
->unscheduled_list
) {
478 calculate_deps(&state
, instr
->data
);
483 sched_node_init(struct ir3_postsched_ctx
*ctx
, struct ir3_instruction
*instr
)
485 struct ir3_postsched_node
*n
= rzalloc(ctx
->mem_ctx
, struct ir3_postsched_node
);
487 dag_init_node(ctx
->dag
, &n
->dag
);
494 sched_dag_max_delay_cb(struct dag_node
*node
, void *state
)
496 struct ir3_postsched_node
*n
= (struct ir3_postsched_node
*)node
;
497 uint32_t max_delay
= 0;
499 util_dynarray_foreach(&n
->dag
.edges
, struct dag_edge
, edge
) {
500 struct ir3_postsched_node
*child
= (struct ir3_postsched_node
*)edge
->child
;
501 max_delay
= MAX2(child
->max_delay
, max_delay
);
504 n
->max_delay
= MAX2(n
->max_delay
, max_delay
+ n
->delay
);
508 sched_dag_init(struct ir3_postsched_ctx
*ctx
)
510 ctx
->mem_ctx
= ralloc_context(NULL
);
512 ctx
->dag
= dag_create(ctx
->mem_ctx
);
514 foreach_instr (instr
, &ctx
->unscheduled_list
)
515 sched_node_init(ctx
, instr
);
517 calculate_forward_deps(ctx
);
518 calculate_reverse_deps(ctx
);
521 * Normal srcs won't be in SSA at this point, those are dealt with in
522 * calculate_forward_deps() and calculate_reverse_deps(). But we still
523 * have the false-dep information in SSA form, so go ahead and add
524 * dependencies for that here:
526 foreach_instr (instr
, &ctx
->unscheduled_list
) {
527 struct ir3_postsched_node
*n
= instr
->data
;
528 struct ir3_instruction
*src
;
530 foreach_ssa_src_n (src
, i
, instr
) {
531 if (src
->block
!= instr
->block
)
534 /* we can end up with unused false-deps.. just skip them: */
535 if (src
->flags
& IR3_INSTR_UNUSED
)
538 struct ir3_postsched_node
*sn
= src
->data
;
540 /* don't consider dependencies in other blocks: */
541 if (src
->block
!= instr
->block
)
544 dag_add_edge(&sn
->dag
, &n
->dag
, NULL
);
548 // TODO do we want to do this after reverse-dependencies?
549 dag_traverse_bottom_up(ctx
->dag
, sched_dag_max_delay_cb
, NULL
);
553 sched_dag_destroy(struct ir3_postsched_ctx
*ctx
)
555 ralloc_free(ctx
->mem_ctx
);
561 sched_block(struct ir3_postsched_ctx
*ctx
, struct ir3_block
*block
)
564 ctx
->scheduled
= NULL
;
567 /* move all instructions to the unscheduled list, and
568 * empty the block's instruction list (to which we will
571 list_replace(&block
->instr_list
, &ctx
->unscheduled_list
);
572 list_inithead(&block
->instr_list
);
574 // TODO once we are using post-sched for everything we can
575 // just not stick in NOP's prior to post-sched, and drop this.
576 // for now keep this, since it makes post-sched optional:
577 foreach_instr_safe (instr
, &ctx
->unscheduled_list
) {
578 switch (instr
->opc
) {
582 list_delinit(&instr
->node
);
591 /* First schedule all meta:input instructions, followed by
592 * tex-prefetch. We want all of the instructions that load
593 * values into registers before the shader starts to go
594 * before any other instructions. But in particular we
595 * want inputs to come before prefetches. This is because
596 * a FS's bary_ij input may not actually be live in the
597 * shader, but it should not be scheduled on top of any
598 * other input (but can be overwritten by a tex prefetch)
600 foreach_instr_safe (instr
, &ctx
->unscheduled_list
)
601 if (instr
->opc
== OPC_META_INPUT
)
602 schedule(ctx
, instr
);
604 foreach_instr_safe (instr
, &ctx
->unscheduled_list
)
605 if (instr
->opc
== OPC_META_TEX_PREFETCH
)
606 schedule(ctx
, instr
);
608 while (!list_is_empty(&ctx
->unscheduled_list
)) {
609 struct ir3_instruction
*instr
;
611 instr
= choose_instr(ctx
);
613 /* this shouldn't happen: */
619 unsigned delay
= ir3_delay_calc(ctx
->block
, instr
, false, false);
620 d("delay=%u", delay
);
622 /* and if we run out of instructions that can be scheduled,
623 * then it is time for nop's:
625 debug_assert(delay
<= 6);
631 schedule(ctx
, instr
);
634 sched_dag_destroy(ctx
);
639 is_self_mov(struct ir3_instruction
*instr
)
641 if (!is_same_type_mov(instr
))
644 if (instr
->regs
[0]->num
!= instr
->regs
[1]->num
)
647 if (instr
->regs
[0]->flags
& IR3_REG_RELATIV
)
650 if (instr
->regs
[1]->flags
& (IR3_REG_CONST
| IR3_REG_IMMED
|
651 IR3_REG_RELATIV
| IR3_REG_FNEG
| IR3_REG_FABS
|
652 IR3_REG_SNEG
| IR3_REG_SABS
| IR3_REG_BNOT
|
653 IR3_REG_EVEN
| IR3_REG_POS_INF
))
659 /* sometimes we end up w/ in-place mov's, ie. mov.u32u32 r1.y, r1.y
660 * as a result of places were before RA we are not sure that it is
661 * safe to eliminate. We could eliminate these earlier, but sometimes
662 * they are tangled up in false-dep's, etc, so it is easier just to
663 * let them exist until after RA
666 cleanup_self_movs(struct ir3
*ir
)
668 foreach_block (block
, &ir
->block_list
) {
669 foreach_instr_safe (instr
, &block
->instr_list
) {
670 struct ir3_register
*reg
;
672 foreach_src (reg
, instr
) {
676 if (is_self_mov(reg
->instr
)) {
677 list_delinit(®
->instr
->node
);
678 reg
->instr
= reg
->instr
->regs
[1]->instr
;
682 for (unsigned i
= 0; i
< instr
->deps_count
; i
++) {
683 if (is_self_mov(instr
->deps
[i
])) {
684 list_delinit(&instr
->deps
[i
]->node
);
685 instr
->deps
[i
] = instr
->deps
[i
]->regs
[1]->instr
;
693 ir3_postsched(struct ir3_context
*cctx
)
695 struct ir3_postsched_ctx ctx
= {
699 ir3_remove_nops(cctx
->ir
);
700 cleanup_self_movs(cctx
->ir
);
702 foreach_block (block
, &cctx
->ir
->block_list
) {
703 sched_block(&ctx
, block
);