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
;
55 struct schedule_node_child
{
56 struct schedule_node
*node
;
57 bool write_after_read
;
60 /* When walking the instructions in reverse, we need to swap before/after in
63 enum direction
{ F
, R
};
65 struct schedule_state
{
66 struct schedule_node
*last_r
[6];
67 struct schedule_node
*last_ra
[32];
68 struct schedule_node
*last_rb
[32];
69 struct schedule_node
*last_sf
;
70 struct schedule_node
*last_vpm_read
;
71 struct schedule_node
*last_unif_read
;
72 struct schedule_node
*last_tmu_write
;
73 struct schedule_node
*last_tlb
;
74 struct schedule_node
*last_vpm
;
79 add_dep(struct schedule_state
*state
,
80 struct schedule_node
*before
,
81 struct schedule_node
*after
,
84 bool write_after_read
= !write
&& state
->dir
== R
;
86 if (!before
|| !after
)
89 assert(before
!= after
);
91 if (state
->dir
== R
) {
92 struct schedule_node
*t
= before
;
97 for (int i
= 0; i
< before
->child_count
; i
++) {
98 if (before
->children
[i
].node
== after
&&
99 (before
->children
[i
].write_after_read
== write_after_read
)) {
104 if (before
->child_array_size
<= before
->child_count
) {
105 before
->child_array_size
= MAX2(before
->child_array_size
* 2, 16);
106 before
->children
= reralloc(before
, before
->children
,
107 struct schedule_node_child
,
108 before
->child_array_size
);
111 before
->children
[before
->child_count
].node
= after
;
112 before
->children
[before
->child_count
].write_after_read
=
114 before
->child_count
++;
115 after
->parent_count
++;
119 add_read_dep(struct schedule_state
*state
,
120 struct schedule_node
*before
,
121 struct schedule_node
*after
)
123 add_dep(state
, before
, after
, false);
127 add_write_dep(struct schedule_state
*state
,
128 struct schedule_node
**before
,
129 struct schedule_node
*after
)
131 add_dep(state
, *before
, after
, true);
136 qpu_writes_r4(uint64_t inst
)
138 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
141 case QPU_SIG_COLOR_LOAD
:
142 case QPU_SIG_LOAD_TMU0
:
143 case QPU_SIG_LOAD_TMU1
:
144 case QPU_SIG_ALPHA_MASK_LOAD
:
152 process_raddr_deps(struct schedule_state
*state
, struct schedule_node
*n
,
153 uint32_t raddr
, bool is_a
)
157 add_write_dep(state
, &state
->last_r
[5], n
);
161 add_write_dep(state
, &state
->last_vpm_read
, n
);
165 add_write_dep(state
, &state
->last_unif_read
, n
);
170 case QPU_R_XY_PIXEL_COORD
:
171 case QPU_R_MS_REV_FLAGS
:
177 add_read_dep(state
, state
->last_ra
[raddr
], n
);
179 add_read_dep(state
, state
->last_rb
[raddr
], n
);
181 fprintf(stderr
, "unknown raddr %d\n", raddr
);
189 is_tmu_write(uint32_t waddr
)
207 process_mux_deps(struct schedule_state
*state
, struct schedule_node
*n
,
210 if (mux
!= QPU_MUX_A
&& mux
!= QPU_MUX_B
)
211 add_read_dep(state
, state
->last_r
[mux
], n
);
216 is_direct_tmu_read(uint64_t inst
)
218 /* If it's a direct read, we happen to structure the code such that
219 * there's an explicit uniform read in the instruction (for kernel
220 * texture reloc processing).
222 return (QPU_GET_FIELD(inst
, QPU_RADDR_A
) == QPU_R_UNIF
||
223 QPU_GET_FIELD(inst
, QPU_RADDR_B
) == QPU_R_UNIF
);
227 process_waddr_deps(struct schedule_state
*state
, struct schedule_node
*n
,
228 uint32_t waddr
, bool is_add
)
230 uint64_t inst
= n
->inst
->inst
;
231 bool is_a
= is_add
^ ((inst
& QPU_WS
) != 0);
235 add_write_dep(state
, &state
->last_ra
[waddr
], n
);
237 add_write_dep(state
, &state
->last_rb
[waddr
], n
);
239 } else if (is_tmu_write(waddr
)) {
240 add_write_dep(state
, &state
->last_tmu_write
, n
);
242 /* There is an implicit uniform read in texture ops in
243 * hardware, unless this is a direct-addressed uniform read,
244 * so we need to keep it in the same order as the other
247 if (!is_direct_tmu_read(n
->inst
->inst
))
248 add_write_dep(state
, &state
->last_unif_read
, n
);
249 } else if (qpu_waddr_is_tlb(waddr
)) {
250 add_write_dep(state
, &state
->last_tlb
, n
);
258 add_write_dep(state
, &state
->last_r
[waddr
- QPU_W_ACC0
],
263 case QPU_W_VPMVCD_SETUP
:
264 add_write_dep(state
, &state
->last_vpm
, n
);
267 case QPU_W_SFU_RECIP
:
268 case QPU_W_SFU_RECIPSQRT
:
271 add_write_dep(state
, &state
->last_r
[4], n
);
274 case QPU_W_TLB_STENCIL_SETUP
:
275 /* This isn't a TLB operation that does things like
276 * implicitly lock the scoreboard, but it does have to
277 * appear before TLB_Z, and each of the TLB_STENCILs
278 * have to schedule in the same order relative to each
281 add_write_dep(state
, &state
->last_tlb
, n
);
288 fprintf(stderr
, "Unknown waddr %d\n", waddr
);
295 process_cond_deps(struct schedule_state
*state
, struct schedule_node
*n
,
300 case QPU_COND_ALWAYS
:
303 add_read_dep(state
, state
->last_sf
, n
);
309 * Common code for dependencies that need to be tracked both forward and
312 * This is for things like "all reads of r4 have to happen between the r4
313 * writes that surround them".
316 calculate_deps(struct schedule_state
*state
, struct schedule_node
*n
)
318 uint64_t inst
= n
->inst
->inst
;
319 uint32_t add_op
= QPU_GET_FIELD(inst
, QPU_OP_ADD
);
320 uint32_t mul_op
= QPU_GET_FIELD(inst
, QPU_OP_MUL
);
321 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
322 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
323 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
324 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
325 uint32_t add_a
= QPU_GET_FIELD(inst
, QPU_ADD_A
);
326 uint32_t add_b
= QPU_GET_FIELD(inst
, QPU_ADD_B
);
327 uint32_t mul_a
= QPU_GET_FIELD(inst
, QPU_MUL_A
);
328 uint32_t mul_b
= QPU_GET_FIELD(inst
, QPU_MUL_B
);
329 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
331 process_raddr_deps(state
, n
, raddr_a
, true);
332 process_raddr_deps(state
, n
, raddr_b
, false);
333 if (add_op
!= QPU_A_NOP
) {
334 process_mux_deps(state
, n
, add_a
);
335 process_mux_deps(state
, n
, add_b
);
337 if (mul_op
!= QPU_M_NOP
) {
338 process_mux_deps(state
, n
, mul_a
);
339 process_mux_deps(state
, n
, mul_b
);
342 process_waddr_deps(state
, n
, waddr_add
, true);
343 process_waddr_deps(state
, n
, waddr_mul
, false);
344 if (qpu_writes_r4(inst
))
345 add_write_dep(state
, &state
->last_r
[4], n
);
348 case QPU_SIG_SW_BREAKPOINT
:
350 case QPU_SIG_THREAD_SWITCH
:
351 case QPU_SIG_LAST_THREAD_SWITCH
:
352 case QPU_SIG_SMALL_IMM
:
353 case QPU_SIG_LOAD_IMM
:
356 case QPU_SIG_LOAD_TMU0
:
357 case QPU_SIG_LOAD_TMU1
:
358 /* TMU loads are coming from a FIFO, so ordering is important.
360 add_write_dep(state
, &state
->last_tmu_write
, n
);
363 case QPU_SIG_COLOR_LOAD
:
364 add_read_dep(state
, state
->last_tlb
, n
);
367 case QPU_SIG_PROG_END
:
368 case QPU_SIG_WAIT_FOR_SCOREBOARD
:
369 case QPU_SIG_SCOREBOARD_UNLOCK
:
370 case QPU_SIG_COVERAGE_LOAD
:
371 case QPU_SIG_COLOR_LOAD_END
:
372 case QPU_SIG_ALPHA_MASK_LOAD
:
374 fprintf(stderr
, "Unhandled signal bits %d\n", sig
);
378 process_cond_deps(state
, n
, QPU_GET_FIELD(inst
, QPU_COND_ADD
));
379 process_cond_deps(state
, n
, QPU_GET_FIELD(inst
, QPU_COND_ADD
));
381 add_write_dep(state
, &state
->last_sf
, n
);
385 calculate_forward_deps(struct vc4_compile
*c
, struct simple_node
*schedule_list
)
387 struct simple_node
*node
;
388 struct schedule_state state
;
390 memset(&state
, 0, sizeof(state
));
393 foreach(node
, schedule_list
)
394 calculate_deps(&state
, (struct schedule_node
*)node
);
398 calculate_reverse_deps(struct vc4_compile
*c
, struct simple_node
*schedule_list
)
400 struct simple_node
*node
;
401 struct schedule_state state
;
403 memset(&state
, 0, sizeof(state
));
406 for (node
= schedule_list
->prev
; schedule_list
!= node
; node
= node
->prev
) {
407 calculate_deps(&state
, (struct schedule_node
*)node
);
411 struct choose_scoreboard
{
413 int last_sfu_write_tick
;
414 uint32_t last_waddr_a
, last_waddr_b
;
418 reads_too_soon_after_write(struct choose_scoreboard
*scoreboard
, uint64_t inst
)
420 uint32_t raddr_a
= QPU_GET_FIELD(inst
, QPU_RADDR_A
);
421 uint32_t raddr_b
= QPU_GET_FIELD(inst
, QPU_RADDR_B
);
422 uint32_t src_muxes
[] = {
423 QPU_GET_FIELD(inst
, QPU_ADD_A
),
424 QPU_GET_FIELD(inst
, QPU_ADD_B
),
425 QPU_GET_FIELD(inst
, QPU_MUL_A
),
426 QPU_GET_FIELD(inst
, QPU_MUL_B
),
428 for (int i
= 0; i
< ARRAY_SIZE(src_muxes
); i
++) {
429 if ((src_muxes
[i
] == QPU_MUX_A
&&
431 scoreboard
->last_waddr_a
== raddr_a
) ||
432 (src_muxes
[i
] == QPU_MUX_B
&&
434 scoreboard
->last_waddr_b
== raddr_b
)) {
438 if (src_muxes
[i
] == QPU_MUX_R4
) {
439 if (scoreboard
->tick
-
440 scoreboard
->last_sfu_write_tick
<= 2) {
450 pixel_scoreboard_too_soon(struct choose_scoreboard
*scoreboard
, uint64_t inst
)
452 return (scoreboard
->tick
< 2 && qpu_inst_is_tlb(inst
));
456 get_instruction_priority(uint64_t inst
)
458 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
459 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
460 uint32_t sig
= QPU_GET_FIELD(inst
, QPU_SIG
);
461 uint32_t baseline_score
;
462 uint32_t next_score
= 0;
464 /* Schedule TLB operations as late as possible, to get more
465 * parallelism between shaders.
467 if (qpu_inst_is_tlb(inst
))
471 /* Schedule texture read results collection late to hide latency. */
472 if (sig
== QPU_SIG_LOAD_TMU0
|| sig
== QPU_SIG_LOAD_TMU1
)
476 /* Default score for things that aren't otherwise special. */
477 baseline_score
= next_score
;
480 /* Schedule texture read setup early to hide their latency better. */
481 if (is_tmu_write(waddr_add
) || is_tmu_write(waddr_mul
))
485 return baseline_score
;
488 static struct schedule_node
*
489 choose_instruction_to_schedule(struct choose_scoreboard
*scoreboard
,
490 struct simple_node
*schedule_list
,
493 struct schedule_node
*chosen
= NULL
;
494 struct simple_node
*node
;
497 foreach(node
, schedule_list
) {
498 struct schedule_node
*n
= (struct schedule_node
*)node
;
499 uint64_t inst
= n
->inst
->inst
;
501 /* "An instruction must not read from a location in physical
502 * regfile A or B that was written to by the previous
505 if (reads_too_soon_after_write(scoreboard
, inst
))
508 /* "A scoreboard wait must not occur in the first two
509 * instructions of a fragment shader. This is either the
510 * explicit Wait for Scoreboard signal or an implicit wait
511 * with the first tile-buffer read or write instruction."
513 if (pixel_scoreboard_too_soon(scoreboard
, inst
))
516 /* If we're trying to pair with another instruction, check
517 * that they're compatible.
519 if (prev_inst
!= 0) {
520 inst
= qpu_merge_inst(prev_inst
, inst
);
525 int prio
= get_instruction_priority(inst
);
527 /* Found a valid instruction. If nothing better comes along,
536 if (prio
> chosen_prio
) {
539 } else if (prio
< chosen_prio
) {
548 update_scoreboard_for_chosen(struct choose_scoreboard
*scoreboard
,
551 uint32_t waddr_add
= QPU_GET_FIELD(inst
, QPU_WADDR_ADD
);
552 uint32_t waddr_mul
= QPU_GET_FIELD(inst
, QPU_WADDR_MUL
);
554 if (!(inst
& QPU_WS
)) {
555 scoreboard
->last_waddr_a
= waddr_add
;
556 scoreboard
->last_waddr_b
= waddr_mul
;
558 scoreboard
->last_waddr_b
= waddr_add
;
559 scoreboard
->last_waddr_a
= waddr_mul
;
562 if ((waddr_add
>= QPU_W_SFU_RECIP
&& waddr_add
<= QPU_W_SFU_LOG
) ||
563 (waddr_mul
>= QPU_W_SFU_RECIP
&& waddr_mul
<= QPU_W_SFU_LOG
)) {
564 scoreboard
->last_sfu_write_tick
= scoreboard
->tick
;
569 dump_state(struct simple_node
*schedule_list
)
571 struct simple_node
*node
;
574 foreach(node
, schedule_list
) {
575 struct schedule_node
*n
= (struct schedule_node
*)node
;
577 fprintf(stderr
, "%3d: ", i
++);
578 vc4_qpu_disasm(&n
->inst
->inst
, 1);
579 fprintf(stderr
, "\n");
581 for (int i
= 0; i
< n
->child_count
; i
++) {
582 struct schedule_node
*child
= n
->children
[i
].node
;
586 fprintf(stderr
, " - ");
587 vc4_qpu_disasm(&child
->inst
->inst
, 1);
588 fprintf(stderr
, " (%d parents, %c)\n",
590 n
->children
[i
].write_after_read
? 'w' : 'r');
595 /** Recursive computation of the delay member of a node. */
597 compute_delay(struct schedule_node
*n
)
599 if (!n
->child_count
) {
602 for (int i
= 0; i
< n
->child_count
; i
++) {
603 if (!n
->children
[i
].node
->delay
)
604 compute_delay(n
->children
[i
].node
);
605 n
->delay
= MAX2(n
->delay
,
606 n
->children
[i
].node
->delay
+ 1);
612 mark_instruction_scheduled(struct simple_node
*schedule_list
,
613 struct schedule_node
*node
,
619 for (int i
= node
->child_count
- 1; i
>= 0; i
--) {
620 struct schedule_node
*child
=
621 node
->children
[i
].node
;
626 if (war_only
&& !node
->children
[i
].write_after_read
)
629 child
->parent_count
--;
630 if (child
->parent_count
== 0)
631 insert_at_head(schedule_list
, &child
->link
);
633 node
->children
[i
].node
= NULL
;
638 schedule_instructions(struct vc4_compile
*c
, struct simple_node
*schedule_list
)
640 struct simple_node
*node
, *t
;
641 struct choose_scoreboard scoreboard
;
643 memset(&scoreboard
, 0, sizeof(scoreboard
));
644 scoreboard
.last_waddr_a
= ~0;
645 scoreboard
.last_waddr_b
= ~0;
646 scoreboard
.last_sfu_write_tick
= -10;
649 fprintf(stderr
, "initial deps:\n");
650 dump_state(schedule_list
);
651 fprintf(stderr
, "\n");
654 /* Remove non-DAG heads from the list. */
655 foreach_s(node
, t
, schedule_list
) {
656 struct schedule_node
*n
= (struct schedule_node
*)node
;
658 if (n
->parent_count
!= 0)
659 remove_from_list(&n
->link
);
662 while (!is_empty_list(schedule_list
)) {
663 struct schedule_node
*chosen
=
664 choose_instruction_to_schedule(&scoreboard
,
667 struct schedule_node
*merge
= NULL
;
669 /* If there are no valid instructions to schedule, drop a NOP
672 uint64_t inst
= chosen
? chosen
->inst
->inst
: qpu_NOP();
675 fprintf(stderr
, "current list:\n");
676 dump_state(schedule_list
);
677 fprintf(stderr
, "chose: ");
678 vc4_qpu_disasm(&inst
, 1);
679 fprintf(stderr
, "\n");
682 /* Schedule this instruction onto the QPU list. Also try to
683 * find an instruction to pair with it.
686 remove_from_list(&chosen
->link
);
687 mark_instruction_scheduled(schedule_list
, chosen
, true);
689 merge
= choose_instruction_to_schedule(&scoreboard
,
693 remove_from_list(&merge
->link
);
694 inst
= qpu_merge_inst(inst
, merge
->inst
->inst
);
698 fprintf(stderr
, "merging: ");
699 vc4_qpu_disasm(&merge
->inst
->inst
, 1);
700 fprintf(stderr
, "\n");
701 fprintf(stderr
, "resulting in: ");
702 vc4_qpu_disasm(&inst
, 1);
703 fprintf(stderr
, "\n");
709 fprintf(stderr
, "\n");
712 qpu_serialize_one_inst(c
, inst
);
714 update_scoreboard_for_chosen(&scoreboard
, inst
);
716 /* Now that we've scheduled a new instruction, some of its
717 * children can be promoted to the list of instructions ready to
718 * be scheduled. Update the children's unblocked time for this
719 * DAG edge as we do so.
721 mark_instruction_scheduled(schedule_list
, chosen
, false);
722 mark_instruction_scheduled(schedule_list
, merge
, false);
729 qpu_schedule_instructions(struct vc4_compile
*c
)
731 void *mem_ctx
= ralloc_context(NULL
);
732 struct simple_node schedule_list
;
733 struct simple_node
*node
;
735 make_empty_list(&schedule_list
);
738 fprintf(stderr
, "Pre-schedule instructions\n");
739 foreach(node
, &c
->qpu_inst_list
) {
740 struct queued_qpu_inst
*q
=
741 (struct queued_qpu_inst
*)node
;
742 vc4_qpu_disasm(&q
->inst
, 1);
743 fprintf(stderr
, "\n");
745 fprintf(stderr
, "\n");
748 /* Wrap each instruction in a scheduler structure. */
749 while (!is_empty_list(&c
->qpu_inst_list
)) {
750 struct queued_qpu_inst
*inst
=
751 (struct queued_qpu_inst
*)c
->qpu_inst_list
.next
;
752 struct schedule_node
*n
= rzalloc(mem_ctx
, struct schedule_node
);
755 remove_from_list(&inst
->link
);
756 insert_at_tail(&schedule_list
, &n
->link
);
759 calculate_forward_deps(c
, &schedule_list
);
760 calculate_reverse_deps(c
, &schedule_list
);
762 foreach(node
, &schedule_list
) {
763 struct schedule_node
*n
= (struct schedule_node
*)node
;
767 schedule_instructions(c
, &schedule_list
);
770 fprintf(stderr
, "Post-schedule instructions\n");
771 vc4_qpu_disasm(c
->qpu_insts
, c
->qpu_inst_count
);
772 fprintf(stderr
, "\n");
775 ralloc_free(mem_ctx
);