2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2014-2017 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
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.
37 #include "qpu/qpu_disasm.h"
38 #include "v3d_compiler.h"
39 #include "util/ralloc.h"
44 struct schedule_node_child
;
46 struct schedule_node
{
48 struct list_head link
;
51 /* Longest cycles + instruction_latency() of any parent of this node. */
52 uint32_t unblocked_time
;
55 * Minimum number of cycles from scheduling this instruction until the
56 * end of the program, based on the slowest dependency chain through
62 * cycles between this instruction being scheduled and when its result
68 /* When walking the instructions in reverse, we need to swap before/after in
71 enum direction
{ F
, R
};
73 struct schedule_state
{
74 const struct v3d_device_info
*devinfo
;
76 struct schedule_node
*last_r
[6];
77 struct schedule_node
*last_rf
[64];
78 struct schedule_node
*last_sf
;
79 struct schedule_node
*last_vpm_read
;
80 struct schedule_node
*last_tmu_write
;
81 struct schedule_node
*last_tmu_config
;
82 struct schedule_node
*last_tlb
;
83 struct schedule_node
*last_vpm
;
84 struct schedule_node
*last_unif
;
85 struct schedule_node
*last_rtop
;
87 /* Estimated cycle when the current instruction would start. */
92 add_dep(struct schedule_state
*state
,
93 struct schedule_node
*before
,
94 struct schedule_node
*after
,
97 bool write_after_read
= !write
&& state
->dir
== R
;
98 void *edge_data
= (void *)(uintptr_t)write_after_read
;
100 if (!before
|| !after
)
103 assert(before
!= after
);
106 dag_add_edge(&before
->dag
, &after
->dag
, edge_data
);
108 dag_add_edge(&after
->dag
, &before
->dag
, edge_data
);
112 add_read_dep(struct schedule_state
*state
,
113 struct schedule_node
*before
,
114 struct schedule_node
*after
)
116 add_dep(state
, before
, after
, false);
120 add_write_dep(struct schedule_state
*state
,
121 struct schedule_node
**before
,
122 struct schedule_node
*after
)
124 add_dep(state
, *before
, after
, true);
129 qpu_inst_is_tlb(const struct v3d_qpu_instr
*inst
)
131 if (inst
->type
!= V3D_QPU_INSTR_TYPE_ALU
)
134 if (inst
->alu
.add
.magic_write
&&
135 (inst
->alu
.add
.waddr
== V3D_QPU_WADDR_TLB
||
136 inst
->alu
.add
.waddr
== V3D_QPU_WADDR_TLBU
))
139 if (inst
->alu
.mul
.magic_write
&&
140 (inst
->alu
.mul
.waddr
== V3D_QPU_WADDR_TLB
||
141 inst
->alu
.mul
.waddr
== V3D_QPU_WADDR_TLBU
))
148 process_mux_deps(struct schedule_state
*state
, struct schedule_node
*n
,
149 enum v3d_qpu_mux mux
)
153 add_read_dep(state
, state
->last_rf
[n
->inst
->qpu
.raddr_a
], n
);
156 if (!n
->inst
->qpu
.sig
.small_imm
) {
158 state
->last_rf
[n
->inst
->qpu
.raddr_b
], n
);
162 add_read_dep(state
, state
->last_r
[mux
- V3D_QPU_MUX_R0
], n
);
169 process_waddr_deps(struct schedule_state
*state
, struct schedule_node
*n
,
170 uint32_t waddr
, bool magic
)
173 add_write_dep(state
, &state
->last_rf
[waddr
], n
);
174 } else if (v3d_qpu_magic_waddr_is_tmu(waddr
)) {
175 /* XXX perf: For V3D 4.x, we could reorder TMU writes other
176 * than the TMUS/TMUD/TMUA to improve scheduling flexibility.
178 add_write_dep(state
, &state
->last_tmu_write
, n
);
180 case V3D_QPU_WADDR_TMUS
:
181 case V3D_QPU_WADDR_TMUSCM
:
182 case V3D_QPU_WADDR_TMUSF
:
183 case V3D_QPU_WADDR_TMUSLOD
:
184 add_write_dep(state
, &state
->last_tmu_config
, n
);
189 } else if (v3d_qpu_magic_waddr_is_sfu(waddr
)) {
190 /* Handled by v3d_qpu_writes_r4() check. */
193 case V3D_QPU_WADDR_R0
:
194 case V3D_QPU_WADDR_R1
:
195 case V3D_QPU_WADDR_R2
:
197 &state
->last_r
[waddr
- V3D_QPU_WADDR_R0
],
200 case V3D_QPU_WADDR_R3
:
201 case V3D_QPU_WADDR_R4
:
202 case V3D_QPU_WADDR_R5
:
203 /* Handled by v3d_qpu_writes_r*() checks below. */
206 case V3D_QPU_WADDR_VPM
:
207 case V3D_QPU_WADDR_VPMU
:
208 add_write_dep(state
, &state
->last_vpm
, n
);
211 case V3D_QPU_WADDR_TLB
:
212 case V3D_QPU_WADDR_TLBU
:
213 add_write_dep(state
, &state
->last_tlb
, n
);
216 case V3D_QPU_WADDR_SYNC
:
217 case V3D_QPU_WADDR_SYNCB
:
218 case V3D_QPU_WADDR_SYNCU
:
219 /* For CS barrier(): Sync against any other memory
220 * accesses. There doesn't appear to be any need for
221 * barriers to affect ALU operations.
223 add_write_dep(state
, &state
->last_tmu_write
, n
);
226 case V3D_QPU_WADDR_NOP
:
230 fprintf(stderr
, "Unknown waddr %d\n", waddr
);
237 * Common code for dependencies that need to be tracked both forward and
240 * This is for things like "all reads of r4 have to happen between the r4
241 * writes that surround them".
244 calculate_deps(struct schedule_state
*state
, struct schedule_node
*n
)
246 const struct v3d_device_info
*devinfo
= state
->devinfo
;
247 struct qinst
*qinst
= n
->inst
;
248 struct v3d_qpu_instr
*inst
= &qinst
->qpu
;
249 /* If the input and output segments are shared, then all VPM reads to
250 * a location need to happen before all writes. We handle this by
251 * serializing all VPM operations for now.
253 bool separate_vpm_segment
= false;
255 if (inst
->type
== V3D_QPU_INSTR_TYPE_BRANCH
) {
256 if (inst
->branch
.cond
!= V3D_QPU_BRANCH_COND_ALWAYS
)
257 add_read_dep(state
, state
->last_sf
, n
);
264 add_write_dep(state
, &state
->last_unif
, n
);
268 assert(inst
->type
== V3D_QPU_INSTR_TYPE_ALU
);
272 if (v3d_qpu_add_op_num_src(inst
->alu
.add
.op
) > 0)
273 process_mux_deps(state
, n
, inst
->alu
.add
.a
);
274 if (v3d_qpu_add_op_num_src(inst
->alu
.add
.op
) > 1)
275 process_mux_deps(state
, n
, inst
->alu
.add
.b
);
277 if (v3d_qpu_mul_op_num_src(inst
->alu
.mul
.op
) > 0)
278 process_mux_deps(state
, n
, inst
->alu
.mul
.a
);
279 if (v3d_qpu_mul_op_num_src(inst
->alu
.mul
.op
) > 1)
280 process_mux_deps(state
, n
, inst
->alu
.mul
.b
);
282 switch (inst
->alu
.add
.op
) {
283 case V3D_QPU_A_VPMSETUP
:
284 /* Could distinguish read/write by unpacking the uniform. */
285 add_write_dep(state
, &state
->last_vpm
, n
);
286 add_write_dep(state
, &state
->last_vpm_read
, n
);
289 case V3D_QPU_A_STVPMV
:
290 case V3D_QPU_A_STVPMD
:
291 case V3D_QPU_A_STVPMP
:
292 add_write_dep(state
, &state
->last_vpm
, n
);
295 case V3D_QPU_A_LDVPMV_IN
:
296 case V3D_QPU_A_LDVPMD_IN
:
297 case V3D_QPU_A_LDVPMG_IN
:
298 case V3D_QPU_A_LDVPMP
:
299 if (!separate_vpm_segment
)
300 add_write_dep(state
, &state
->last_vpm
, n
);
303 case V3D_QPU_A_VPMWT
:
304 add_read_dep(state
, state
->last_vpm
, n
);
308 add_read_dep(state
, state
->last_tlb
, n
);
311 case V3D_QPU_A_SETMSF
:
312 case V3D_QPU_A_SETREVF
:
313 add_write_dep(state
, &state
->last_tlb
, n
);
320 switch (inst
->alu
.mul
.op
) {
321 case V3D_QPU_M_MULTOP
:
322 case V3D_QPU_M_UMUL24
:
323 /* MULTOP sets rtop, and UMUL24 implicitly reads rtop and
324 * resets it to 0. We could possibly reorder umul24s relative
325 * to each other, but for now just keep all the MUL parts in
328 add_write_dep(state
, &state
->last_rtop
, n
);
334 if (inst
->alu
.add
.op
!= V3D_QPU_A_NOP
) {
335 process_waddr_deps(state
, n
, inst
->alu
.add
.waddr
,
336 inst
->alu
.add
.magic_write
);
338 if (inst
->alu
.mul
.op
!= V3D_QPU_M_NOP
) {
339 process_waddr_deps(state
, n
, inst
->alu
.mul
.waddr
,
340 inst
->alu
.mul
.magic_write
);
342 if (v3d_qpu_sig_writes_address(devinfo
, &inst
->sig
)) {
343 process_waddr_deps(state
, n
, inst
->sig_addr
,
347 if (v3d_qpu_writes_r3(devinfo
, inst
))
348 add_write_dep(state
, &state
->last_r
[3], n
);
349 if (v3d_qpu_writes_r4(devinfo
, inst
))
350 add_write_dep(state
, &state
->last_r
[4], n
);
351 if (v3d_qpu_writes_r5(devinfo
, inst
))
352 add_write_dep(state
, &state
->last_r
[5], n
);
354 if (inst
->sig
.thrsw
) {
355 /* All accumulator contents and flags are undefined after the
358 for (int i
= 0; i
< ARRAY_SIZE(state
->last_r
); i
++)
359 add_write_dep(state
, &state
->last_r
[i
], n
);
360 add_write_dep(state
, &state
->last_sf
, n
);
361 add_write_dep(state
, &state
->last_rtop
, n
);
363 /* Scoreboard-locking operations have to stay after the last
366 add_write_dep(state
, &state
->last_tlb
, n
);
368 add_write_dep(state
, &state
->last_tmu_write
, n
);
369 add_write_dep(state
, &state
->last_tmu_config
, n
);
372 if (v3d_qpu_waits_on_tmu(inst
)) {
373 /* TMU loads are coming from a FIFO, so ordering is important.
375 add_write_dep(state
, &state
->last_tmu_write
, n
);
378 if (inst
->sig
.wrtmuc
)
379 add_write_dep(state
, &state
->last_tmu_config
, n
);
381 if (inst
->sig
.ldtlb
| inst
->sig
.ldtlbu
)
382 add_read_dep(state
, state
->last_tlb
, n
);
384 if (inst
->sig
.ldvpm
) {
385 add_write_dep(state
, &state
->last_vpm_read
, n
);
387 /* At least for now, we're doing shared I/O segments, so queue
388 * all writes after all reads.
390 if (!separate_vpm_segment
)
391 add_write_dep(state
, &state
->last_vpm
, n
);
394 /* inst->sig.ldunif or sideband uniform read */
395 if (vir_has_uniform(qinst
))
396 add_write_dep(state
, &state
->last_unif
, n
);
398 if (v3d_qpu_reads_flags(inst
))
399 add_read_dep(state
, state
->last_sf
, n
);
400 if (v3d_qpu_writes_flags(inst
))
401 add_write_dep(state
, &state
->last_sf
, n
);
405 calculate_forward_deps(struct v3d_compile
*c
, struct dag
*dag
,
406 struct list_head
*schedule_list
)
408 struct schedule_state state
;
410 memset(&state
, 0, sizeof(state
));
412 state
.devinfo
= c
->devinfo
;
415 list_for_each_entry(struct schedule_node
, node
, schedule_list
, link
)
416 calculate_deps(&state
, node
);
420 calculate_reverse_deps(struct v3d_compile
*c
, struct dag
*dag
,
421 struct list_head
*schedule_list
)
423 struct schedule_state state
;
425 memset(&state
, 0, sizeof(state
));
427 state
.devinfo
= c
->devinfo
;
430 list_for_each_entry_rev(struct schedule_node
, node
, schedule_list
,
432 calculate_deps(&state
, (struct schedule_node
*)node
);
436 struct choose_scoreboard
{
439 int last_magic_sfu_write_tick
;
440 int last_ldvary_tick
;
441 int last_uniforms_reset_tick
;
447 mux_reads_too_soon(struct choose_scoreboard
*scoreboard
,
448 const struct v3d_qpu_instr
*inst
, enum v3d_qpu_mux mux
)
452 if (scoreboard
->tick
- scoreboard
->last_magic_sfu_write_tick
<= 2)
457 if (scoreboard
->tick
- scoreboard
->last_ldvary_tick
<= 1)
468 reads_too_soon_after_write(struct choose_scoreboard
*scoreboard
,
471 const struct v3d_qpu_instr
*inst
= &qinst
->qpu
;
473 /* XXX: Branching off of raddr. */
474 if (inst
->type
== V3D_QPU_INSTR_TYPE_BRANCH
)
477 assert(inst
->type
== V3D_QPU_INSTR_TYPE_ALU
);
479 if (inst
->alu
.add
.op
!= V3D_QPU_A_NOP
) {
480 if (v3d_qpu_add_op_num_src(inst
->alu
.add
.op
) > 0 &&
481 mux_reads_too_soon(scoreboard
, inst
, inst
->alu
.add
.a
)) {
484 if (v3d_qpu_add_op_num_src(inst
->alu
.add
.op
) > 1 &&
485 mux_reads_too_soon(scoreboard
, inst
, inst
->alu
.add
.b
)) {
490 if (inst
->alu
.mul
.op
!= V3D_QPU_M_NOP
) {
491 if (v3d_qpu_mul_op_num_src(inst
->alu
.mul
.op
) > 0 &&
492 mux_reads_too_soon(scoreboard
, inst
, inst
->alu
.mul
.a
)) {
495 if (v3d_qpu_mul_op_num_src(inst
->alu
.mul
.op
) > 1 &&
496 mux_reads_too_soon(scoreboard
, inst
, inst
->alu
.mul
.b
)) {
507 writes_too_soon_after_write(const struct v3d_device_info
*devinfo
,
508 struct choose_scoreboard
*scoreboard
,
511 const struct v3d_qpu_instr
*inst
= &qinst
->qpu
;
513 /* Don't schedule any other r4 write too soon after an SFU write.
514 * This would normally be prevented by dependency tracking, but might
515 * occur if a dead SFU computation makes it to scheduling.
517 if (scoreboard
->tick
- scoreboard
->last_magic_sfu_write_tick
< 2 &&
518 v3d_qpu_writes_r4(devinfo
, inst
))
525 pixel_scoreboard_too_soon(struct choose_scoreboard
*scoreboard
,
526 const struct v3d_qpu_instr
*inst
)
528 return (scoreboard
->tick
== 0 && qpu_inst_is_tlb(inst
));
532 get_instruction_priority(const struct v3d_qpu_instr
*inst
)
534 uint32_t baseline_score
;
535 uint32_t next_score
= 0;
537 /* Schedule TLB operations as late as possible, to get more
538 * parallelism between shaders.
540 if (qpu_inst_is_tlb(inst
))
544 /* Schedule texture read results collection late to hide latency. */
545 if (v3d_qpu_waits_on_tmu(inst
))
549 /* XXX perf: We should schedule SFU ALU ops so that the reader is 2
550 * instructions after the producer if possible, not just 1.
553 /* Default score for things that aren't otherwise special. */
554 baseline_score
= next_score
;
557 /* Schedule texture read setup early to hide their latency better. */
558 if (v3d_qpu_writes_tmu(inst
))
562 return baseline_score
;
566 qpu_magic_waddr_is_periph(enum v3d_qpu_waddr waddr
)
568 return (v3d_qpu_magic_waddr_is_tmu(waddr
) ||
569 v3d_qpu_magic_waddr_is_sfu(waddr
) ||
570 v3d_qpu_magic_waddr_is_tlb(waddr
) ||
571 v3d_qpu_magic_waddr_is_vpm(waddr
) ||
572 v3d_qpu_magic_waddr_is_tsy(waddr
));
576 qpu_accesses_peripheral(const struct v3d_qpu_instr
*inst
)
578 if (v3d_qpu_uses_vpm(inst
))
580 if (v3d_qpu_uses_sfu(inst
))
583 if (inst
->type
== V3D_QPU_INSTR_TYPE_ALU
) {
584 if (inst
->alu
.add
.op
!= V3D_QPU_A_NOP
&&
585 inst
->alu
.add
.magic_write
&&
586 qpu_magic_waddr_is_periph(inst
->alu
.add
.waddr
)) {
590 if (inst
->alu
.add
.op
== V3D_QPU_A_TMUWT
)
593 if (inst
->alu
.mul
.op
!= V3D_QPU_M_NOP
&&
594 inst
->alu
.mul
.magic_write
&&
595 qpu_magic_waddr_is_periph(inst
->alu
.mul
.waddr
)) {
600 return (inst
->sig
.ldvpm
||
608 qpu_merge_inst(const struct v3d_device_info
*devinfo
,
609 struct v3d_qpu_instr
*result
,
610 const struct v3d_qpu_instr
*a
,
611 const struct v3d_qpu_instr
*b
)
613 if (a
->type
!= V3D_QPU_INSTR_TYPE_ALU
||
614 b
->type
!= V3D_QPU_INSTR_TYPE_ALU
) {
618 /* Can't do more than one peripheral access in an instruction.
620 * XXX: V3D 4.1 allows TMU read along with a VPM read or write, and
621 * WRTMUC with a TMU magic register write (other than tmuc).
623 if (qpu_accesses_peripheral(a
) && qpu_accesses_peripheral(b
))
626 struct v3d_qpu_instr merge
= *a
;
628 if (b
->alu
.add
.op
!= V3D_QPU_A_NOP
) {
629 if (a
->alu
.add
.op
!= V3D_QPU_A_NOP
)
631 merge
.alu
.add
= b
->alu
.add
;
633 merge
.flags
.ac
= b
->flags
.ac
;
634 merge
.flags
.apf
= b
->flags
.apf
;
635 merge
.flags
.auf
= b
->flags
.auf
;
638 if (b
->alu
.mul
.op
!= V3D_QPU_M_NOP
) {
639 if (a
->alu
.mul
.op
!= V3D_QPU_M_NOP
)
641 merge
.alu
.mul
= b
->alu
.mul
;
643 merge
.flags
.mc
= b
->flags
.mc
;
644 merge
.flags
.mpf
= b
->flags
.mpf
;
645 merge
.flags
.muf
= b
->flags
.muf
;
648 if (v3d_qpu_uses_mux(b
, V3D_QPU_MUX_A
)) {
649 if (v3d_qpu_uses_mux(a
, V3D_QPU_MUX_A
) &&
650 a
->raddr_a
!= b
->raddr_a
) {
653 merge
.raddr_a
= b
->raddr_a
;
656 if (v3d_qpu_uses_mux(b
, V3D_QPU_MUX_B
)) {
657 if (v3d_qpu_uses_mux(a
, V3D_QPU_MUX_B
) &&
658 (a
->raddr_b
!= b
->raddr_b
||
659 a
->sig
.small_imm
!= b
->sig
.small_imm
)) {
662 merge
.raddr_b
= b
->raddr_b
;
665 merge
.sig
.thrsw
|= b
->sig
.thrsw
;
666 merge
.sig
.ldunif
|= b
->sig
.ldunif
;
667 merge
.sig
.ldunifrf
|= b
->sig
.ldunifrf
;
668 merge
.sig
.ldunifa
|= b
->sig
.ldunifa
;
669 merge
.sig
.ldunifarf
|= b
->sig
.ldunifarf
;
670 merge
.sig
.ldtmu
|= b
->sig
.ldtmu
;
671 merge
.sig
.ldvary
|= b
->sig
.ldvary
;
672 merge
.sig
.ldvpm
|= b
->sig
.ldvpm
;
673 merge
.sig
.small_imm
|= b
->sig
.small_imm
;
674 merge
.sig
.ldtlb
|= b
->sig
.ldtlb
;
675 merge
.sig
.ldtlbu
|= b
->sig
.ldtlbu
;
676 merge
.sig
.ucb
|= b
->sig
.ucb
;
677 merge
.sig
.rotate
|= b
->sig
.rotate
;
678 merge
.sig
.wrtmuc
|= b
->sig
.wrtmuc
;
680 if (v3d_qpu_sig_writes_address(devinfo
, &a
->sig
) &&
681 v3d_qpu_sig_writes_address(devinfo
, &b
->sig
))
683 merge
.sig_addr
|= b
->sig_addr
;
684 merge
.sig_magic
|= b
->sig_magic
;
687 bool ok
= v3d_qpu_instr_pack(devinfo
, &merge
, &packed
);
690 /* No modifying the real instructions on failure. */
691 assert(ok
|| (a
!= result
&& b
!= result
));
696 static struct schedule_node
*
697 choose_instruction_to_schedule(const struct v3d_device_info
*devinfo
,
698 struct choose_scoreboard
*scoreboard
,
699 struct schedule_node
*prev_inst
)
701 struct schedule_node
*chosen
= NULL
;
704 /* Don't pair up anything with a thread switch signal -- emit_thrsw()
705 * will handle pairing it along with filling the delay slots.
708 if (prev_inst
->inst
->qpu
.sig
.thrsw
)
712 list_for_each_entry(struct schedule_node
, n
, &scoreboard
->dag
->heads
,
714 const struct v3d_qpu_instr
*inst
= &n
->inst
->qpu
;
716 /* Don't choose the branch instruction until it's the last one
717 * left. We'll move it up to fit its delay slots after we
720 if (inst
->type
== V3D_QPU_INSTR_TYPE_BRANCH
&&
721 !list_is_singular(&scoreboard
->dag
->heads
)) {
725 /* "An instruction must not read from a location in physical
726 * regfile A or B that was written to by the previous
729 if (reads_too_soon_after_write(scoreboard
, n
->inst
))
732 if (writes_too_soon_after_write(devinfo
, scoreboard
, n
->inst
))
735 /* "A scoreboard wait must not occur in the first two
736 * instructions of a fragment shader. This is either the
737 * explicit Wait for Scoreboard signal or an implicit wait
738 * with the first tile-buffer read or write instruction."
740 if (pixel_scoreboard_too_soon(scoreboard
, inst
))
743 /* ldunif and ldvary both write r5, but ldunif does so a tick
744 * sooner. If the ldvary's r5 wasn't used, then ldunif might
745 * otherwise get scheduled so ldunif and ldvary try to update
746 * r5 in the same tick.
748 * XXX perf: To get good pipelining of a sequence of varying
749 * loads, we need to figure out how to pair the ldvary signal
750 * up to the instruction before the last r5 user in the
751 * previous ldvary sequence. Currently, it usually pairs with
754 if ((inst
->sig
.ldunif
|| inst
->sig
.ldunifa
) &&
755 scoreboard
->tick
== scoreboard
->last_ldvary_tick
+ 1) {
759 /* If we're trying to pair with another instruction, check
760 * that they're compatible.
763 /* Don't pair up a thread switch signal -- we'll
764 * handle pairing it when we pick it on its own.
769 if (prev_inst
->inst
->uniform
!= -1 &&
770 n
->inst
->uniform
!= -1)
773 /* Don't merge in something that will lock the TLB.
774 * Hopwefully what we have in inst will release some
775 * other instructions, allowing us to delay the
776 * TLB-locking instruction until later.
778 if (!scoreboard
->tlb_locked
&& qpu_inst_is_tlb(inst
))
781 struct v3d_qpu_instr merged_inst
;
782 if (!qpu_merge_inst(devinfo
, &merged_inst
,
783 &prev_inst
->inst
->qpu
, inst
)) {
788 int prio
= get_instruction_priority(inst
);
790 /* Found a valid instruction. If nothing better comes along,
799 if (prio
> chosen_prio
) {
802 } else if (prio
< chosen_prio
) {
806 if (n
->delay
> chosen
->delay
) {
809 } else if (n
->delay
< chosen
->delay
) {
818 update_scoreboard_for_magic_waddr(struct choose_scoreboard
*scoreboard
,
819 enum v3d_qpu_waddr waddr
)
821 if (v3d_qpu_magic_waddr_is_sfu(waddr
))
822 scoreboard
->last_magic_sfu_write_tick
= scoreboard
->tick
;
826 update_scoreboard_for_chosen(struct choose_scoreboard
*scoreboard
,
827 const struct v3d_qpu_instr
*inst
)
829 if (inst
->type
== V3D_QPU_INSTR_TYPE_BRANCH
)
832 assert(inst
->type
== V3D_QPU_INSTR_TYPE_ALU
);
834 if (inst
->alu
.add
.op
!= V3D_QPU_A_NOP
) {
835 if (inst
->alu
.add
.magic_write
) {
836 update_scoreboard_for_magic_waddr(scoreboard
,
837 inst
->alu
.add
.waddr
);
841 if (inst
->alu
.mul
.op
!= V3D_QPU_M_NOP
) {
842 if (inst
->alu
.mul
.magic_write
) {
843 update_scoreboard_for_magic_waddr(scoreboard
,
844 inst
->alu
.mul
.waddr
);
848 if (inst
->sig
.ldvary
)
849 scoreboard
->last_ldvary_tick
= scoreboard
->tick
;
851 if (qpu_inst_is_tlb(inst
))
852 scoreboard
->tlb_locked
= true;
856 dump_state(const struct v3d_device_info
*devinfo
, struct dag
*dag
)
858 list_for_each_entry(struct schedule_node
, n
, &dag
->heads
, dag
.link
) {
859 fprintf(stderr
, " t=%4d: ", n
->unblocked_time
);
860 v3d_qpu_dump(devinfo
, &n
->inst
->qpu
);
861 fprintf(stderr
, "\n");
863 util_dynarray_foreach(&n
->dag
.edges
, struct dag_edge
, edge
) {
864 struct schedule_node
*child
=
865 (struct schedule_node
*)edge
->child
;
869 fprintf(stderr
, " - ");
870 v3d_qpu_dump(devinfo
, &child
->inst
->qpu
);
871 fprintf(stderr
, " (%d parents, %c)\n",
872 child
->dag
.parent_count
,
873 edge
->data
? 'w' : 'r');
878 static uint32_t magic_waddr_latency(enum v3d_qpu_waddr waddr
,
879 const struct v3d_qpu_instr
*after
)
881 /* Apply some huge latency between texture fetch requests and getting
882 * their results back.
884 * FIXME: This is actually pretty bogus. If we do:
893 * we count that as worse than
902 * because we associate the first load_tmu0 with the *second* tmu0_s.
904 if (v3d_qpu_magic_waddr_is_tmu(waddr
) && v3d_qpu_waits_on_tmu(after
))
907 /* Assume that anything depending on us is consuming the SFU result. */
908 if (v3d_qpu_magic_waddr_is_sfu(waddr
))
915 instruction_latency(struct schedule_node
*before
, struct schedule_node
*after
)
917 const struct v3d_qpu_instr
*before_inst
= &before
->inst
->qpu
;
918 const struct v3d_qpu_instr
*after_inst
= &after
->inst
->qpu
;
919 uint32_t latency
= 1;
921 if (before_inst
->type
!= V3D_QPU_INSTR_TYPE_ALU
||
922 after_inst
->type
!= V3D_QPU_INSTR_TYPE_ALU
)
925 if (before_inst
->alu
.add
.magic_write
) {
926 latency
= MAX2(latency
,
927 magic_waddr_latency(before_inst
->alu
.add
.waddr
,
931 if (before_inst
->alu
.mul
.magic_write
) {
932 latency
= MAX2(latency
,
933 magic_waddr_latency(before_inst
->alu
.mul
.waddr
,
940 /** Recursive computation of the delay member of a node. */
942 compute_delay(struct dag_node
*node
, void *state
)
944 struct schedule_node
*n
= (struct schedule_node
*)node
;
948 util_dynarray_foreach(&n
->dag
.edges
, struct dag_edge
, edge
) {
949 struct schedule_node
*child
=
950 (struct schedule_node
*)edge
->child
;
952 n
->delay
= MAX2(n
->delay
, (child
->delay
+
953 instruction_latency(n
, child
)));
957 /* Removes a DAG head, but removing only the WAR edges. (dag_prune_head()
958 * should be called on it later to finish pruning the other edges).
961 pre_remove_head(struct dag
*dag
, struct schedule_node
*n
)
963 list_delinit(&n
->dag
.link
);
965 util_dynarray_foreach(&n
->dag
.edges
, struct dag_edge
, edge
) {
967 dag_remove_edge(dag
, edge
);
972 mark_instruction_scheduled(struct dag
*dag
,
974 struct schedule_node
*node
)
979 util_dynarray_foreach(&node
->dag
.edges
, struct dag_edge
, edge
) {
980 struct schedule_node
*child
=
981 (struct schedule_node
*)edge
->child
;
986 uint32_t latency
= instruction_latency(node
, child
);
988 child
->unblocked_time
= MAX2(child
->unblocked_time
,
991 dag_prune_head(dag
, &node
->dag
);
995 insert_scheduled_instruction(struct v3d_compile
*c
,
996 struct qblock
*block
,
997 struct choose_scoreboard
*scoreboard
,
1000 list_addtail(&inst
->link
, &block
->instructions
);
1002 update_scoreboard_for_chosen(scoreboard
, &inst
->qpu
);
1003 c
->qpu_inst_count
++;
1007 static struct qinst
*
1010 struct qreg undef
= vir_nop_reg();
1011 struct qinst
*qinst
= vir_add_inst(V3D_QPU_A_NOP
, undef
, undef
, undef
);
1017 emit_nop(struct v3d_compile
*c
, struct qblock
*block
,
1018 struct choose_scoreboard
*scoreboard
)
1020 insert_scheduled_instruction(c
, block
, scoreboard
, vir_nop());
1024 qpu_instruction_valid_in_thrend_slot(struct v3d_compile
*c
,
1025 const struct qinst
*qinst
, int slot
)
1027 const struct v3d_qpu_instr
*inst
= &qinst
->qpu
;
1029 /* Only TLB Z writes are prohibited in the last slot, but we don't
1030 * have those flagged so prohibit all TLB ops for now.
1032 if (slot
== 2 && qpu_inst_is_tlb(inst
))
1035 if (slot
> 0 && qinst
->uniform
!= ~0)
1038 if (v3d_qpu_uses_vpm(inst
))
1041 if (inst
->sig
.ldvary
)
1044 if (inst
->type
== V3D_QPU_INSTR_TYPE_ALU
) {
1045 /* GFXH-1625: TMUWT not allowed in the final instruction. */
1046 if (slot
== 2 && inst
->alu
.add
.op
== V3D_QPU_A_TMUWT
)
1049 /* No writing physical registers at the end. */
1050 if (!inst
->alu
.add
.magic_write
||
1051 !inst
->alu
.mul
.magic_write
) {
1055 if (c
->devinfo
->ver
< 40 && inst
->alu
.add
.op
== V3D_QPU_A_SETMSF
)
1058 /* RF0-2 might be overwritten during the delay slots by
1059 * fragment shader setup.
1061 if (inst
->raddr_a
< 3 &&
1062 (inst
->alu
.add
.a
== V3D_QPU_MUX_A
||
1063 inst
->alu
.add
.b
== V3D_QPU_MUX_A
||
1064 inst
->alu
.mul
.a
== V3D_QPU_MUX_A
||
1065 inst
->alu
.mul
.b
== V3D_QPU_MUX_A
)) {
1069 if (inst
->raddr_b
< 3 &&
1070 !inst
->sig
.small_imm
&&
1071 (inst
->alu
.add
.a
== V3D_QPU_MUX_B
||
1072 inst
->alu
.add
.b
== V3D_QPU_MUX_B
||
1073 inst
->alu
.mul
.a
== V3D_QPU_MUX_B
||
1074 inst
->alu
.mul
.b
== V3D_QPU_MUX_B
)) {
1083 valid_thrsw_sequence(struct v3d_compile
*c
, struct choose_scoreboard
*scoreboard
,
1084 struct qinst
*qinst
, int instructions_in_sequence
,
1087 /* No emitting our thrsw while the previous thrsw hasn't happened yet. */
1088 if (scoreboard
->last_thrsw_tick
+ 3 >
1089 scoreboard
->tick
- instructions_in_sequence
) {
1093 for (int slot
= 0; slot
< instructions_in_sequence
; slot
++) {
1094 /* No scheduling SFU when the result would land in the other
1095 * thread. The simulator complains for safety, though it
1096 * would only occur for dead code in our case.
1099 qinst
->qpu
.type
== V3D_QPU_INSTR_TYPE_ALU
&&
1100 (v3d_qpu_magic_waddr_is_sfu(qinst
->qpu
.alu
.add
.waddr
) ||
1101 v3d_qpu_magic_waddr_is_sfu(qinst
->qpu
.alu
.mul
.waddr
))) {
1105 if (slot
> 0 && qinst
->qpu
.sig
.ldvary
)
1109 !qpu_instruction_valid_in_thrend_slot(c
, qinst
, slot
)) {
1113 /* Note that the list is circular, so we can only do this up
1114 * to instructions_in_sequence.
1116 qinst
= (struct qinst
*)qinst
->link
.next
;
1123 * Emits a THRSW signal in the stream, trying to move it up to pair with
1124 * another instruction.
1127 emit_thrsw(struct v3d_compile
*c
,
1128 struct qblock
*block
,
1129 struct choose_scoreboard
*scoreboard
,
1135 /* There should be nothing in a thrsw inst being scheduled other than
1138 assert(inst
->qpu
.type
== V3D_QPU_INSTR_TYPE_ALU
);
1139 assert(inst
->qpu
.alu
.add
.op
== V3D_QPU_A_NOP
);
1140 assert(inst
->qpu
.alu
.mul
.op
== V3D_QPU_M_NOP
);
1142 /* Find how far back into previous instructions we can put the THRSW. */
1143 int slots_filled
= 0;
1144 struct qinst
*merge_inst
= NULL
;
1145 vir_for_each_inst_rev(prev_inst
, block
) {
1146 struct v3d_qpu_sig sig
= prev_inst
->qpu
.sig
;
1148 uint32_t packed_sig
;
1150 if (!v3d_qpu_sig_pack(c
->devinfo
, &sig
, &packed_sig
))
1153 if (!valid_thrsw_sequence(c
, scoreboard
,
1154 prev_inst
, slots_filled
+ 1,
1159 merge_inst
= prev_inst
;
1160 if (++slots_filled
== 3)
1164 bool needs_free
= false;
1166 merge_inst
->qpu
.sig
.thrsw
= true;
1168 scoreboard
->last_thrsw_tick
= scoreboard
->tick
- slots_filled
;
1170 scoreboard
->last_thrsw_tick
= scoreboard
->tick
;
1171 insert_scheduled_instruction(c
, block
, scoreboard
, inst
);
1177 /* Insert any extra delay slot NOPs we need. */
1178 for (int i
= 0; i
< 3 - slots_filled
; i
++) {
1179 emit_nop(c
, block
, scoreboard
);
1183 /* If we're emitting the last THRSW (other than program end), then
1184 * signal that to the HW by emitting two THRSWs in a row.
1186 if (inst
->is_last_thrsw
) {
1187 struct qinst
*second_inst
=
1188 (struct qinst
*)merge_inst
->link
.next
;
1189 second_inst
->qpu
.sig
.thrsw
= true;
1192 /* If we put our THRSW into another instruction, free up the
1193 * instruction that didn't end up scheduled into the list.
1202 schedule_instructions(struct v3d_compile
*c
,
1203 struct choose_scoreboard
*scoreboard
,
1204 struct qblock
*block
,
1205 enum quniform_contents
*orig_uniform_contents
,
1206 uint32_t *orig_uniform_data
,
1207 uint32_t *next_uniform
)
1209 const struct v3d_device_info
*devinfo
= c
->devinfo
;
1212 while (!list_empty(&scoreboard
->dag
->heads
)) {
1213 struct schedule_node
*chosen
=
1214 choose_instruction_to_schedule(devinfo
,
1217 struct schedule_node
*merge
= NULL
;
1219 /* If there are no valid instructions to schedule, drop a NOP
1222 struct qinst
*qinst
= chosen
? chosen
->inst
: vir_nop();
1223 struct v3d_qpu_instr
*inst
= &qinst
->qpu
;
1226 fprintf(stderr
, "t=%4d: current list:\n",
1228 dump_state(devinfo
, scoreboard
->dag
);
1229 fprintf(stderr
, "t=%4d: chose: ", time
);
1230 v3d_qpu_dump(devinfo
, inst
);
1231 fprintf(stderr
, "\n");
1234 /* We can't mark_instruction_scheduled() the chosen inst until
1235 * we're done identifying instructions to merge, so put the
1236 * merged instructions on a list for a moment.
1238 struct list_head merged_list
;
1239 list_inithead(&merged_list
);
1241 /* Schedule this instruction onto the QPU list. Also try to
1242 * find an instruction to pair with it.
1245 time
= MAX2(chosen
->unblocked_time
, time
);
1246 pre_remove_head(scoreboard
->dag
, chosen
);
1249 choose_instruction_to_schedule(devinfo
,
1252 time
= MAX2(merge
->unblocked_time
, time
);
1253 pre_remove_head(scoreboard
->dag
, chosen
);
1254 list_addtail(&merge
->link
, &merged_list
);
1255 (void)qpu_merge_inst(devinfo
, inst
,
1256 inst
, &merge
->inst
->qpu
);
1257 if (merge
->inst
->uniform
!= -1) {
1258 chosen
->inst
->uniform
=
1259 merge
->inst
->uniform
;
1263 fprintf(stderr
, "t=%4d: merging: ",
1265 v3d_qpu_dump(devinfo
, &merge
->inst
->qpu
);
1266 fprintf(stderr
, "\n");
1267 fprintf(stderr
, " result: ");
1268 v3d_qpu_dump(devinfo
, inst
);
1269 fprintf(stderr
, "\n");
1274 /* Update the uniform index for the rewritten location --
1275 * branch target updating will still need to change
1276 * c->uniform_data[] using this index.
1278 if (qinst
->uniform
!= -1) {
1279 if (inst
->type
== V3D_QPU_INSTR_TYPE_BRANCH
)
1280 block
->branch_uniform
= *next_uniform
;
1282 c
->uniform_data
[*next_uniform
] =
1283 orig_uniform_data
[qinst
->uniform
];
1284 c
->uniform_contents
[*next_uniform
] =
1285 orig_uniform_contents
[qinst
->uniform
];
1286 qinst
->uniform
= *next_uniform
;
1291 fprintf(stderr
, "\n");
1294 /* Now that we've scheduled a new instruction, some of its
1295 * children can be promoted to the list of instructions ready to
1296 * be scheduled. Update the children's unblocked time for this
1297 * DAG edge as we do so.
1299 mark_instruction_scheduled(scoreboard
->dag
, time
, chosen
);
1300 list_for_each_entry(struct schedule_node
, merge
, &merged_list
,
1302 mark_instruction_scheduled(scoreboard
->dag
, time
, merge
);
1304 /* The merged VIR instruction doesn't get re-added to the
1305 * block, so free it now.
1310 if (inst
->sig
.thrsw
) {
1311 time
+= emit_thrsw(c
, block
, scoreboard
, qinst
, false);
1313 insert_scheduled_instruction(c
, block
,
1316 if (inst
->type
== V3D_QPU_INSTR_TYPE_BRANCH
) {
1317 block
->branch_qpu_ip
= c
->qpu_inst_count
- 1;
1318 /* Fill the delay slots.
1320 * We should fill these with actual instructions,
1321 * instead, but that will probably need to be done
1322 * after this, once we know what the leading
1323 * instructions of the successors are (so we can
1324 * handle A/B register file write latency)
1326 for (int i
= 0; i
< 3; i
++)
1327 emit_nop(c
, block
, scoreboard
);
1336 qpu_schedule_instructions_block(struct v3d_compile
*c
,
1337 struct choose_scoreboard
*scoreboard
,
1338 struct qblock
*block
,
1339 enum quniform_contents
*orig_uniform_contents
,
1340 uint32_t *orig_uniform_data
,
1341 uint32_t *next_uniform
)
1343 void *mem_ctx
= ralloc_context(NULL
);
1344 scoreboard
->dag
= dag_create(mem_ctx
);
1345 struct list_head setup_list
;
1347 list_inithead(&setup_list
);
1349 /* Wrap each instruction in a scheduler structure. */
1350 while (!list_empty(&block
->instructions
)) {
1351 struct qinst
*qinst
= (struct qinst
*)block
->instructions
.next
;
1352 struct schedule_node
*n
=
1353 rzalloc(mem_ctx
, struct schedule_node
);
1355 dag_init_node(scoreboard
->dag
, &n
->dag
);
1358 list_del(&qinst
->link
);
1359 list_addtail(&n
->link
, &setup_list
);
1362 calculate_forward_deps(c
, scoreboard
->dag
, &setup_list
);
1363 calculate_reverse_deps(c
, scoreboard
->dag
, &setup_list
);
1365 dag_traverse_bottom_up(scoreboard
->dag
, compute_delay
, NULL
);
1367 uint32_t cycles
= schedule_instructions(c
, scoreboard
, block
,
1368 orig_uniform_contents
,
1372 ralloc_free(mem_ctx
);
1373 scoreboard
->dag
= NULL
;
1379 qpu_set_branch_targets(struct v3d_compile
*c
)
1381 vir_for_each_block(block
, c
) {
1382 /* The end block of the program has no branch. */
1383 if (!block
->successors
[0])
1386 /* If there was no branch instruction, then the successor
1387 * block must follow immediately after this one.
1389 if (block
->branch_qpu_ip
== ~0) {
1390 assert(block
->end_qpu_ip
+ 1 ==
1391 block
->successors
[0]->start_qpu_ip
);
1395 /* Walk back through the delay slots to find the branch
1398 struct list_head
*entry
= block
->instructions
.prev
;
1399 for (int i
= 0; i
< 3; i
++)
1400 entry
= entry
->prev
;
1401 struct qinst
*branch
= container_of(entry
, branch
, link
);
1402 assert(branch
->qpu
.type
== V3D_QPU_INSTR_TYPE_BRANCH
);
1404 /* Make sure that the if-we-don't-jump
1405 * successor was scheduled just after the
1408 assert(!block
->successors
[1] ||
1409 block
->successors
[1]->start_qpu_ip
==
1410 block
->branch_qpu_ip
+ 4);
1412 branch
->qpu
.branch
.offset
=
1413 ((block
->successors
[0]->start_qpu_ip
-
1414 (block
->branch_qpu_ip
+ 4)) *
1417 /* Set up the relative offset to jump in the
1420 * Use a temporary here, because
1421 * uniform_data[inst->uniform] may be shared
1422 * between multiple instructions.
1424 assert(c
->uniform_contents
[branch
->uniform
] == QUNIFORM_CONSTANT
);
1425 c
->uniform_data
[branch
->uniform
] =
1426 (block
->successors
[0]->start_uniform
-
1427 (block
->branch_uniform
+ 1)) * 4;
1432 v3d_qpu_schedule_instructions(struct v3d_compile
*c
)
1434 const struct v3d_device_info
*devinfo
= c
->devinfo
;
1435 struct qblock
*end_block
= list_last_entry(&c
->blocks
,
1436 struct qblock
, link
);
1438 /* We reorder the uniforms as we schedule instructions, so save the
1439 * old data off and replace it.
1441 uint32_t *uniform_data
= c
->uniform_data
;
1442 enum quniform_contents
*uniform_contents
= c
->uniform_contents
;
1443 c
->uniform_contents
= ralloc_array(c
, enum quniform_contents
,
1445 c
->uniform_data
= ralloc_array(c
, uint32_t, c
->num_uniforms
);
1446 c
->uniform_array_size
= c
->num_uniforms
;
1447 uint32_t next_uniform
= 0;
1449 struct choose_scoreboard scoreboard
;
1450 memset(&scoreboard
, 0, sizeof(scoreboard
));
1451 scoreboard
.last_ldvary_tick
= -10;
1452 scoreboard
.last_magic_sfu_write_tick
= -10;
1453 scoreboard
.last_uniforms_reset_tick
= -10;
1454 scoreboard
.last_thrsw_tick
= -10;
1457 fprintf(stderr
, "Pre-schedule instructions\n");
1458 vir_for_each_block(block
, c
) {
1459 fprintf(stderr
, "BLOCK %d\n", block
->index
);
1460 list_for_each_entry(struct qinst
, qinst
,
1461 &block
->instructions
, link
) {
1462 v3d_qpu_dump(devinfo
, &qinst
->qpu
);
1463 fprintf(stderr
, "\n");
1466 fprintf(stderr
, "\n");
1469 uint32_t cycles
= 0;
1470 vir_for_each_block(block
, c
) {
1471 block
->start_qpu_ip
= c
->qpu_inst_count
;
1472 block
->branch_qpu_ip
= ~0;
1473 block
->start_uniform
= next_uniform
;
1475 cycles
+= qpu_schedule_instructions_block(c
,
1482 block
->end_qpu_ip
= c
->qpu_inst_count
- 1;
1485 /* Emit the program-end THRSW instruction. */;
1486 struct qinst
*thrsw
= vir_nop();
1487 thrsw
->qpu
.sig
.thrsw
= true;
1488 emit_thrsw(c
, end_block
, &scoreboard
, thrsw
, true);
1490 qpu_set_branch_targets(c
);
1492 assert(next_uniform
== c
->num_uniforms
);