2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2014 Broadcom
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 * @file vc4_qpu_schedule.c
28 * The basic model of the list scheduler is to take a basic block, compute a
29 * DAG of the dependencies, and make a list of the DAG heads. Heuristically
30 * pick a DAG head, then put all the children that are now DAG heads into the
31 * list of things to schedule.
33 * The goal of scheduling here is to pack pairs of operations together in a
34 * single QPU instruction.
39 #include "util/ralloc.h"
43 struct schedule_node_child
;
45 struct schedule_node
{
46 struct simple_node link
;
47 struct queued_qpu_inst
*inst
;
48 struct schedule_node_child
*children
;
50 uint32_t child_array_size
;
51 uint32_t parent_count
;
54 * Minimum number of cycles from scheduling this instruction until the
55 * end of the program, based on the slowest dependency chain through
61 * cycles between this instruction being scheduled and when its result
67 struct schedule_node_child
{
68 struct schedule_node
*node
;
69 bool write_after_read
;
72 /* When walking the instructions in reverse, we need to swap before/after in
75 enum direction
{ F
, R
};
77 struct schedule_state
{
78 struct schedule_node
*last_r
[6];
79 struct schedule_node
*last_ra
[32];
80 struct schedule_node
*last_rb
[32];
81 struct schedule_node
*last_sf
;
82 struct schedule_node
*last_vpm_read
;
83 struct schedule_node
*last_unif_read
;
84 struct schedule_node
*last_tmu_write
;
85 struct schedule_node
*last_tlb
;
86 struct schedule_node
*last_vpm
;
91 add_dep(struct schedule_state
*state
,
92 struct schedule_node
*before
,
93 struct schedule_node
*after
,
96 bool write_after_read
= !write
&& state
->dir
== R
;
98 if (!before
|| !after
)
101 assert(before
!= after
);
103 if (state
->dir
== R
) {
104 struct schedule_node
*t
= before
;
109 for (int i
= 0; i
< before
->child_count
; i
++) {
110 if (before
->children
[i
].node
== after
&&
111 (before
->children
[i
].write_after_read
== write_after_read
)) {
116 if (before
->child_array_size
<= before
->child_count
) {
117 before
->child_array_size
= MAX2(before
->child_array_size
* 2, 16);
118 before
->children
= reralloc(before
, before
->children
,
119 struct schedule_node_child
,
120 before
->child_array_size
);
123 before
->children
[before
->child_count
].node
= after
;
124 before
->children
[before
->child_count
].write_after_read
=
126 before
->child_count
++;
127 after
->parent_count
++;
131 add_read_dep(struct schedule_state
*state
,
132 struct schedule_node
*before
,
133 struct schedule_node
*after
)
135 add_dep(state
, before
, after
, false);
139 add_write_dep(struct schedule_state
*state
,
140 struct schedule_node
**before
,
141 struct schedule_node
*after
)
143 add_dep(state
, *before
, after
, true);
148 qpu_writes_r4(uint64_t inst
)
150 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
153 case QPU_SIG_COLOR_LOAD
:
154 case QPU_SIG_LOAD_TMU0
:
155 case QPU_SIG_LOAD_TMU1
:
156 case QPU_SIG_ALPHA_MASK_LOAD
:
164 process_raddr_deps(struct schedule_state
*state
, struct schedule_node
*n
,
165 uint32_t raddr
, bool is_a
)
169 add_write_dep(state
, &state
->last_r
[5], n
);
173 add_write_dep(state
, &state
->last_vpm_read
, n
);
177 add_write_dep(state
, &state
->last_unif_read
, n
);
182 case QPU_R_XY_PIXEL_COORD
:
183 case QPU_R_MS_REV_FLAGS
:
189 add_read_dep(state
, state
->last_ra
[raddr
], n
);
191 add_read_dep(state
, state
->last_rb
[raddr
], n
);
193 fprintf(stderr
, "unknown raddr %d\n", raddr
);
201 is_tmu_write(uint32_t waddr
)
219 process_mux_deps(struct schedule_state
*state
, struct schedule_node
*n
,
222 if (mux
!= QPU_MUX_A
&& mux
!= QPU_MUX_B
)
223 add_read_dep(state
, state
->last_r
[mux
], n
);
228 is_direct_tmu_read(uint64_t inst
)
230 /* If it's a direct read, we happen to structure the code such that
231 * there's an explicit uniform read in the instruction (for kernel
232 * texture reloc processing).
234 return (QPU_GET_FIELD(inst
, QPU_RADDR_A
) == QPU_R_UNIF
||
235 QPU_GET_FIELD(inst
, QPU_RADDR_B
) == QPU_R_UNIF
);
239 process_waddr_deps(struct schedule_state
*state
, struct schedule_node
*n
,
240 uint32_t waddr
, bool is_add
)
242 uint64_t inst
= n
->inst
->inst
;
243 bool is_a
= is_add
^ ((inst
& QPU_WS
) != 0);
247 add_write_dep(state
, &state
->last_ra
[waddr
], n
);
249 add_write_dep(state
, &state
->last_rb
[waddr
], n
);
251 } else if (is_tmu_write(waddr
)) {
252 add_write_dep(state
, &state
->last_tmu_write
, n
);
254 /* There is an implicit uniform read in texture ops in
255 * hardware, unless this is a direct-addressed uniform read,
256 * so we need to keep it in the same order as the other
259 if (!is_direct_tmu_read(n
->inst
->inst
))
260 add_write_dep(state
, &state
->last_unif_read
, n
);
261 } else if (qpu_waddr_is_tlb(waddr
)) {
262 add_write_dep(state
, &state
->last_tlb
, n
);
270 add_write_dep(state
, &state
->last_r
[waddr
- QPU_W_ACC0
],
275 add_write_dep(state
, &state
->last_vpm
, n
);
278 case QPU_W_VPMVCD_SETUP
:
280 add_write_dep(state
, &state
->last_vpm_read
, n
);
282 add_write_dep(state
, &state
->last_vpm
, n
);
285 case QPU_W_SFU_RECIP
:
286 case QPU_W_SFU_RECIPSQRT
:
289 add_write_dep(state
, &state
->last_r
[4], n
);
292 case QPU_W_TLB_STENCIL_SETUP
:
293 /* This isn't a TLB operation that does things like
294 * implicitly lock the scoreboard, but it does have to
295 * appear before TLB_Z, and each of the TLB_STENCILs
296 * have to schedule in the same order relative to each
299 add_write_dep(state
, &state
->last_tlb
, n
);
306 fprintf(stderr
, "Unknown waddr %d\n", waddr
);
313 process_cond_deps(struct schedule_state
*state
, struct schedule_node
*n
,
318 case QPU_COND_ALWAYS
:
321 add_read_dep(state
, state
->last_sf
, n
);
327 * Common code for dependencies that need to be tracked both forward and
330 * This is for things like "all reads of r4 have to happen between the r4
331 * writes that surround them".
334 calculate_deps(struct schedule_state
*state
, struct schedule_node
*n
)
336 uint64_t inst
= n
->inst
->inst
;
337 uint32_t add_op
= QPU_GET_FIELD(inst
, QPU_OP_ADD
);
338 uint32_t mul_op
= QPU_GET_FIELD(inst
, QPU_OP_MUL
);
339 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
340 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
341 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
342 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
343 uint32_t add_a
= QPU_GET_FIELD(inst
, QPU_ADD_A
);
344 uint32_t add_b
= QPU_GET_FIELD(inst
, QPU_ADD_B
);
345 uint32_t mul_a
= QPU_GET_FIELD(inst
, QPU_MUL_A
);
346 uint32_t mul_b
= QPU_GET_FIELD(inst
, QPU_MUL_B
);
347 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
349 if (sig
!= QPU_SIG_LOAD_IMM
) {
350 process_raddr_deps(state
, n
, raddr_a
, true);
351 process_raddr_deps(state
, n
, raddr_b
, false);
354 if (add_op
!= QPU_A_NOP
) {
355 process_mux_deps(state
, n
, add_a
);
356 process_mux_deps(state
, n
, add_b
);
358 if (mul_op
!= QPU_M_NOP
) {
359 process_mux_deps(state
, n
, mul_a
);
360 process_mux_deps(state
, n
, mul_b
);
363 process_waddr_deps(state
, n
, waddr_add
, true);
364 process_waddr_deps(state
, n
, waddr_mul
, false);
365 if (qpu_writes_r4(inst
))
366 add_write_dep(state
, &state
->last_r
[4], n
);
369 case QPU_SIG_SW_BREAKPOINT
:
371 case QPU_SIG_THREAD_SWITCH
:
372 case QPU_SIG_LAST_THREAD_SWITCH
:
373 case QPU_SIG_SMALL_IMM
:
374 case QPU_SIG_LOAD_IMM
:
377 case QPU_SIG_LOAD_TMU0
:
378 case QPU_SIG_LOAD_TMU1
:
379 /* TMU loads are coming from a FIFO, so ordering is important.
381 add_write_dep(state
, &state
->last_tmu_write
, n
);
384 case QPU_SIG_COLOR_LOAD
:
385 add_read_dep(state
, state
->last_tlb
, n
);
388 case QPU_SIG_PROG_END
:
389 case QPU_SIG_WAIT_FOR_SCOREBOARD
:
390 case QPU_SIG_SCOREBOARD_UNLOCK
:
391 case QPU_SIG_COVERAGE_LOAD
:
392 case QPU_SIG_COLOR_LOAD_END
:
393 case QPU_SIG_ALPHA_MASK_LOAD
:
395 fprintf(stderr
, "Unhandled signal bits %d\n", sig
);
399 process_cond_deps(state
, n
, QPU_GET_FIELD(inst
, QPU_COND_ADD
));
400 process_cond_deps(state
, n
, QPU_GET_FIELD(inst
, QPU_COND_ADD
));
402 add_write_dep(state
, &state
->last_sf
, n
);
406 calculate_forward_deps(struct vc4_compile
*c
, struct simple_node
*schedule_list
)
408 struct simple_node
*node
;
409 struct schedule_state state
;
411 memset(&state
, 0, sizeof(state
));
414 foreach(node
, schedule_list
)
415 calculate_deps(&state
, (struct schedule_node
*)node
);
419 calculate_reverse_deps(struct vc4_compile
*c
, struct simple_node
*schedule_list
)
421 struct simple_node
*node
;
422 struct schedule_state state
;
424 memset(&state
, 0, sizeof(state
));
427 for (node
= schedule_list
->prev
; schedule_list
!= node
; node
= node
->prev
) {
428 calculate_deps(&state
, (struct schedule_node
*)node
);
432 struct choose_scoreboard
{
434 int last_sfu_write_tick
;
435 uint32_t last_waddr_a
, last_waddr_b
;
439 reads_too_soon_after_write(struct choose_scoreboard
*scoreboard
, uint64_t inst
)
441 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
442 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
443 uint32_t src_muxes
[] = {
444 QPU_GET_FIELD(inst
, QPU_ADD_A
),
445 QPU_GET_FIELD(inst
, QPU_ADD_B
),
446 QPU_GET_FIELD(inst
, QPU_MUL_A
),
447 QPU_GET_FIELD(inst
, QPU_MUL_B
),
449 for (int i
= 0; i
< ARRAY_SIZE(src_muxes
); i
++) {
450 if ((src_muxes
[i
] == QPU_MUX_A
&&
452 scoreboard
->last_waddr_a
== raddr_a
) ||
453 (src_muxes
[i
] == QPU_MUX_B
&&
455 scoreboard
->last_waddr_b
== raddr_b
)) {
459 if (src_muxes
[i
] == QPU_MUX_R4
) {
460 if (scoreboard
->tick
-
461 scoreboard
->last_sfu_write_tick
<= 2) {
471 pixel_scoreboard_too_soon(struct choose_scoreboard
*scoreboard
, uint64_t inst
)
473 return (scoreboard
->tick
< 2 && qpu_inst_is_tlb(inst
));
477 get_instruction_priority(uint64_t inst
)
479 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
480 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
481 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
482 uint32_t baseline_score
;
483 uint32_t next_score
= 0;
485 /* Schedule TLB operations as late as possible, to get more
486 * parallelism between shaders.
488 if (qpu_inst_is_tlb(inst
))
492 /* Schedule texture read results collection late to hide latency. */
493 if (sig
== QPU_SIG_LOAD_TMU0
|| sig
== QPU_SIG_LOAD_TMU1
)
497 /* Default score for things that aren't otherwise special. */
498 baseline_score
= next_score
;
501 /* Schedule texture read setup early to hide their latency better. */
502 if (is_tmu_write(waddr_add
) || is_tmu_write(waddr_mul
))
506 return baseline_score
;
509 static struct schedule_node
*
510 choose_instruction_to_schedule(struct choose_scoreboard
*scoreboard
,
511 struct simple_node
*schedule_list
,
514 struct schedule_node
*chosen
= NULL
;
515 struct simple_node
*node
;
518 foreach(node
, schedule_list
) {
519 struct schedule_node
*n
= (struct schedule_node
*)node
;
520 uint64_t inst
= n
->inst
->inst
;
522 /* "An instruction must not read from a location in physical
523 * regfile A or B that was written to by the previous
526 if (reads_too_soon_after_write(scoreboard
, inst
))
529 /* "A scoreboard wait must not occur in the first two
530 * instructions of a fragment shader. This is either the
531 * explicit Wait for Scoreboard signal or an implicit wait
532 * with the first tile-buffer read or write instruction."
534 if (pixel_scoreboard_too_soon(scoreboard
, inst
))
537 /* If we're trying to pair with another instruction, check
538 * that they're compatible.
540 if (prev_inst
!= 0) {
541 inst
= qpu_merge_inst(prev_inst
, inst
);
546 int prio
= get_instruction_priority(inst
);
548 /* Found a valid instruction. If nothing better comes along,
557 if (prio
> chosen_prio
) {
560 } else if (prio
< chosen_prio
) {
564 if (n
->delay
> chosen
->delay
) {
567 } else if (n
->delay
< chosen
->delay
) {
576 update_scoreboard_for_chosen(struct choose_scoreboard
*scoreboard
,
579 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
580 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
582 if (!(inst
& QPU_WS
)) {
583 scoreboard
->last_waddr_a
= waddr_add
;
584 scoreboard
->last_waddr_b
= waddr_mul
;
586 scoreboard
->last_waddr_b
= waddr_add
;
587 scoreboard
->last_waddr_a
= waddr_mul
;
590 if ((waddr_add
>= QPU_W_SFU_RECIP
&& waddr_add
<= QPU_W_SFU_LOG
) ||
591 (waddr_mul
>= QPU_W_SFU_RECIP
&& waddr_mul
<= QPU_W_SFU_LOG
)) {
592 scoreboard
->last_sfu_write_tick
= scoreboard
->tick
;
597 dump_state(struct simple_node
*schedule_list
)
599 struct simple_node
*node
;
602 foreach(node
, schedule_list
) {
603 struct schedule_node
*n
= (struct schedule_node
*)node
;
605 fprintf(stderr
, "%3d: ", i
++);
606 vc4_qpu_disasm(&n
->inst
->inst
, 1);
607 fprintf(stderr
, "\n");
609 for (int i
= 0; i
< n
->child_count
; i
++) {
610 struct schedule_node
*child
= n
->children
[i
].node
;
614 fprintf(stderr
, " - ");
615 vc4_qpu_disasm(&child
->inst
->inst
, 1);
616 fprintf(stderr
, " (%d parents, %c)\n",
618 n
->children
[i
].write_after_read
? 'w' : 'r');
623 /** Recursive computation of the delay member of a node. */
625 compute_delay(struct schedule_node
*n
)
627 if (!n
->child_count
) {
630 for (int i
= 0; i
< n
->child_count
; i
++) {
631 if (!n
->children
[i
].node
->delay
)
632 compute_delay(n
->children
[i
].node
);
633 n
->delay
= MAX2(n
->delay
,
634 n
->children
[i
].node
->delay
+ n
->latency
);
640 mark_instruction_scheduled(struct simple_node
*schedule_list
,
641 struct schedule_node
*node
,
647 for (int i
= node
->child_count
- 1; i
>= 0; i
--) {
648 struct schedule_node
*child
=
649 node
->children
[i
].node
;
654 if (war_only
&& !node
->children
[i
].write_after_read
)
657 child
->parent_count
--;
658 if (child
->parent_count
== 0)
659 insert_at_head(schedule_list
, &child
->link
);
661 node
->children
[i
].node
= NULL
;
666 schedule_instructions(struct vc4_compile
*c
, struct simple_node
*schedule_list
)
668 struct simple_node
*node
, *t
;
669 struct choose_scoreboard scoreboard
;
671 memset(&scoreboard
, 0, sizeof(scoreboard
));
672 scoreboard
.last_waddr_a
= ~0;
673 scoreboard
.last_waddr_b
= ~0;
674 scoreboard
.last_sfu_write_tick
= -10;
677 fprintf(stderr
, "initial deps:\n");
678 dump_state(schedule_list
);
679 fprintf(stderr
, "\n");
682 /* Remove non-DAG heads from the list. */
683 foreach_s(node
, t
, schedule_list
) {
684 struct schedule_node
*n
= (struct schedule_node
*)node
;
686 if (n
->parent_count
!= 0)
687 remove_from_list(&n
->link
);
690 while (!is_empty_list(schedule_list
)) {
691 struct schedule_node
*chosen
=
692 choose_instruction_to_schedule(&scoreboard
,
695 struct schedule_node
*merge
= NULL
;
697 /* If there are no valid instructions to schedule, drop a NOP
700 uint64_t inst
= chosen
? chosen
->inst
->inst
: qpu_NOP();
703 fprintf(stderr
, "current list:\n");
704 dump_state(schedule_list
);
705 fprintf(stderr
, "chose: ");
706 vc4_qpu_disasm(&inst
, 1);
707 fprintf(stderr
, "\n");
710 /* Schedule this instruction onto the QPU list. Also try to
711 * find an instruction to pair with it.
714 remove_from_list(&chosen
->link
);
715 mark_instruction_scheduled(schedule_list
, chosen
, true);
717 merge
= choose_instruction_to_schedule(&scoreboard
,
721 remove_from_list(&merge
->link
);
722 inst
= qpu_merge_inst(inst
, merge
->inst
->inst
);
726 fprintf(stderr
, "merging: ");
727 vc4_qpu_disasm(&merge
->inst
->inst
, 1);
728 fprintf(stderr
, "\n");
729 fprintf(stderr
, "resulting in: ");
730 vc4_qpu_disasm(&inst
, 1);
731 fprintf(stderr
, "\n");
737 fprintf(stderr
, "\n");
740 qpu_serialize_one_inst(c
, inst
);
742 update_scoreboard_for_chosen(&scoreboard
, inst
);
744 /* Now that we've scheduled a new instruction, some of its
745 * children can be promoted to the list of instructions ready to
746 * be scheduled. Update the children's unblocked time for this
747 * DAG edge as we do so.
749 mark_instruction_scheduled(schedule_list
, chosen
, false);
750 mark_instruction_scheduled(schedule_list
, merge
, false);
756 static uint32_t waddr_latency(uint32_t waddr
)
761 /* Some huge number, really. */
762 if (waddr
>= QPU_W_TMU0_S
&& waddr
<= QPU_W_TMU1_B
)
766 case QPU_W_SFU_RECIP
:
767 case QPU_W_SFU_RECIPSQRT
:
777 instruction_latency(uint64_t inst
)
779 return MAX2(waddr_latency(QPU_GET_FIELD(inst
, QPU_WADDR_ADD
)),
780 waddr_latency(QPU_GET_FIELD(inst
, QPU_WADDR_MUL
)));
784 qpu_schedule_instructions(struct vc4_compile
*c
)
786 void *mem_ctx
= ralloc_context(NULL
);
787 struct simple_node schedule_list
;
788 struct simple_node
*node
;
790 make_empty_list(&schedule_list
);
793 fprintf(stderr
, "Pre-schedule instructions\n");
794 foreach(node
, &c
->qpu_inst_list
) {
795 struct queued_qpu_inst
*q
=
796 (struct queued_qpu_inst
*)node
;
797 vc4_qpu_disasm(&q
->inst
, 1);
798 fprintf(stderr
, "\n");
800 fprintf(stderr
, "\n");
803 /* Wrap each instruction in a scheduler structure. */
804 while (!is_empty_list(&c
->qpu_inst_list
)) {
805 struct queued_qpu_inst
*inst
=
806 (struct queued_qpu_inst
*)c
->qpu_inst_list
.next
;
807 struct schedule_node
*n
= rzalloc(mem_ctx
, struct schedule_node
);
810 n
->latency
= instruction_latency(inst
->inst
);
812 remove_from_list(&inst
->link
);
813 insert_at_tail(&schedule_list
, &n
->link
);
816 calculate_forward_deps(c
, &schedule_list
);
817 calculate_reverse_deps(c
, &schedule_list
);
819 foreach(node
, &schedule_list
) {
820 struct schedule_node
*n
= (struct schedule_node
*)node
;
824 schedule_instructions(c
, &schedule_list
);
827 fprintf(stderr
, "Post-schedule instructions\n");
828 vc4_qpu_disasm(c
->qpu_insts
, c
->qpu_inst_count
);
829 fprintf(stderr
, "\n");
832 ralloc_free(mem_ctx
);