1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
30 #include "util/u_math.h"
40 * Instruction Scheduling:
42 * Using the depth sorted list from depth pass, attempt to recursively
43 * schedule deepest unscheduled path. The first instruction that cannot
44 * be scheduled, returns the required delay slots it needs, at which
45 * point we return back up to the top and attempt to schedule by next
46 * highest depth. After a sufficient number of instructions have been
47 * scheduled, return back to beginning of list and start again. If you
48 * reach the end of depth sorted list without being able to insert any
49 * instruction, insert nop's. Repeat until no more unscheduled
52 * There are a few special cases that need to be handled, since sched
53 * is currently independent of register allocation. Usages of address
54 * register (a0.x) or predicate register (p0.x) must be serialized. Ie.
55 * if you have two pairs of instructions that write the same special
56 * register and then read it, then those pairs cannot be interleaved.
57 * To solve this, when we are in such a scheduling "critical section",
58 * and we encounter a conflicting write to a special register, we try
59 * to schedule any remaining instructions that use that value first.
62 struct ir3_sched_ctx
{
63 struct ir3_instruction
*scheduled
; /* last scheduled instr */
64 struct ir3_instruction
*addr
; /* current a0.x user, if any */
65 struct ir3_instruction
*pred
; /* current p0.x user, if any */
70 static struct ir3_instruction
*
71 deepest(struct ir3_instruction
**srcs
, unsigned nsrcs
)
73 struct ir3_instruction
*d
= NULL
;
74 unsigned i
= 0, id
= 0;
76 while ((i
< nsrcs
) && !(d
= srcs
[id
= i
]))
82 for (; i
< nsrcs
; i
++)
83 if (srcs
[i
] && (srcs
[i
]->depth
> d
->depth
))
91 static unsigned distance(struct ir3_sched_ctx
*ctx
,
92 struct ir3_instruction
*instr
, unsigned maxd
)
94 struct ir3_instruction
*n
= ctx
->scheduled
;
96 while (n
&& (n
!= instr
) && (d
< maxd
)) {
97 if (is_alu(n
) || is_flow(n
))
104 /* TODO maybe we want double linked list? */
105 static struct ir3_instruction
* prev(struct ir3_instruction
*instr
)
107 struct ir3_instruction
*p
= instr
->block
->head
;
108 while (p
&& (p
->next
!= instr
))
113 static void schedule(struct ir3_sched_ctx
*ctx
,
114 struct ir3_instruction
*instr
, bool remove
)
116 struct ir3_block
*block
= instr
->block
;
118 /* maybe there is a better way to handle this than just stuffing
119 * a nop.. ideally we'd know about this constraint in the
120 * scheduling and depth calculation..
122 if (ctx
->scheduled
&& is_sfu(ctx
->scheduled
) && is_sfu(instr
))
123 schedule(ctx
, ir3_instr_create(block
, 0, OPC_NOP
), false);
125 /* remove from depth list:
128 struct ir3_instruction
*p
= prev(instr
);
130 /* NOTE: this can happen for inputs which are not
131 * read.. in that case there is no need to schedule
132 * the input, so just bail:
134 if (instr
!= (p
? p
->next
: block
->head
))
138 p
->next
= instr
->next
;
140 block
->head
= instr
->next
;
143 if (writes_addr(instr
)) {
144 assert(ctx
->addr
== NULL
);
148 if (writes_pred(instr
)) {
149 assert(ctx
->pred
== NULL
);
153 instr
->flags
|= IR3_INSTR_MARK
;
155 instr
->next
= ctx
->scheduled
;
156 ctx
->scheduled
= instr
;
162 * Delay-slot calculation. Follows fanin/fanout.
165 /* calculate delay for specified src: */
166 static unsigned delay_calc_srcn(struct ir3_sched_ctx
*ctx
,
167 struct ir3_instruction
*assigner
,
168 struct ir3_instruction
*consumer
, unsigned srcn
)
172 if (is_meta(assigner
)) {
174 for (i
= 1; i
< assigner
->regs_count
; i
++) {
175 struct ir3_register
*reg
= assigner
->regs
[i
];
176 if (reg
->flags
& IR3_REG_SSA
) {
177 unsigned d
= delay_calc_srcn(ctx
, reg
->instr
,
179 delay
= MAX2(delay
, d
);
183 delay
= ir3_delayslots(assigner
, consumer
, srcn
);
184 delay
-= distance(ctx
, assigner
, delay
);
190 /* calculate delay for instruction (maximum of delay for all srcs): */
191 static unsigned delay_calc(struct ir3_sched_ctx
*ctx
,
192 struct ir3_instruction
*instr
)
194 unsigned i
, delay
= 0;
196 for (i
= 1; i
< instr
->regs_count
; i
++) {
197 struct ir3_register
*reg
= instr
->regs
[i
];
198 if (reg
->flags
& IR3_REG_SSA
) {
199 unsigned d
= delay_calc_srcn(ctx
, reg
->instr
,
201 delay
= MAX2(delay
, d
);
208 /* A negative return value signals that an instruction has been newly
209 * scheduled, return back up to the top of the stack (to block_sched())
211 static int trysched(struct ir3_sched_ctx
*ctx
,
212 struct ir3_instruction
*instr
)
214 struct ir3_instruction
*srcs
[64];
215 struct ir3_instruction
*src
;
216 unsigned i
, delay
, nsrcs
= 0;
218 /* if already scheduled: */
219 if (instr
->flags
& IR3_INSTR_MARK
)
222 debug_assert(instr
->regs_count
< ARRAY_SIZE(srcs
));
224 /* figure out our src's: */
225 for (i
= 1; i
< instr
->regs_count
; i
++) {
226 struct ir3_register
*reg
= instr
->regs
[i
];
227 if (reg
->flags
& IR3_REG_SSA
)
228 srcs
[nsrcs
++] = reg
->instr
;
231 /* for each src register in sorted order:
234 while ((src
= deepest(srcs
, nsrcs
))) {
235 delay
= trysched(ctx
, src
);
240 /* all our dependents are scheduled, figure out if
241 * we have enough delay slots to schedule ourself:
243 delay
= delay_calc(ctx
, instr
);
247 /* if the instruction is a kill, we need to ensure *every*
248 * bary.f is scheduled. The hw seems unhappy if the thread
249 * gets killed before the end-input (ei) flag is hit.
251 * We could do this by adding each bary.f instruction as
252 * virtual ssa src for the kill instruction. But we have
253 * fixed length instr->regs[].
255 * TODO this wouldn't be quite right if we had multiple
256 * basic blocks, if any block was conditional. We'd need
257 * to schedule the bary.f's outside of any block which
258 * was conditional that contained a kill.. I think..
260 if (is_kill(instr
)) {
261 struct ir3
*ir
= instr
->block
->shader
;
264 for (i
= 0; i
< ir
->baryfs_count
; i
++) {
265 if (ir
->baryfs
[i
]->depth
== DEPTH_UNUSED
)
267 delay
= trysched(ctx
, ir
->baryfs
[i
]);
273 /* if this is a write to address/predicate register, and that
274 * register is currently in use, we need to defer until it is
277 if (writes_addr(instr
) && ctx
->addr
) {
278 assert(ctx
->addr
!= instr
);
281 if (writes_pred(instr
) && ctx
->pred
) {
282 assert(ctx
->pred
!= instr
);
286 schedule(ctx
, instr
, true);
290 static struct ir3_instruction
* reverse(struct ir3_instruction
*instr
)
292 struct ir3_instruction
*reversed
= NULL
;
294 struct ir3_instruction
*next
= instr
->next
;
295 instr
->next
= reversed
;
302 static bool uses_current_addr(struct ir3_sched_ctx
*ctx
,
303 struct ir3_instruction
*instr
)
306 for (i
= 1; i
< instr
->regs_count
; i
++) {
307 struct ir3_register
*reg
= instr
->regs
[i
];
308 if (reg
->flags
& IR3_REG_SSA
) {
309 if (is_addr(reg
->instr
)) {
310 struct ir3_instruction
*addr
;
311 addr
= reg
->instr
->regs
[1]->instr
; /* the mova */
312 if (ctx
->addr
== addr
)
320 static bool uses_current_pred(struct ir3_sched_ctx
*ctx
,
321 struct ir3_instruction
*instr
)
324 for (i
= 1; i
< instr
->regs_count
; i
++) {
325 struct ir3_register
*reg
= instr
->regs
[i
];
326 if ((reg
->flags
& IR3_REG_SSA
) && (ctx
->pred
== reg
->instr
))
332 /* when we encounter an instruction that writes to the address register
333 * when it is in use, we delay that instruction and try to schedule all
334 * other instructions using the current address register:
336 static int block_sched_undelayed(struct ir3_sched_ctx
*ctx
,
337 struct ir3_block
*block
)
339 struct ir3_instruction
*instr
= block
->head
;
340 bool addr_in_use
= false;
341 bool pred_in_use
= false;
342 bool all_delayed
= true;
343 unsigned cnt
= ~0, attempted
= 0;
346 struct ir3_instruction
*next
= instr
->next
;
347 bool addr
= uses_current_addr(ctx
, instr
);
348 bool pred
= uses_current_pred(ctx
, instr
);
351 int ret
= trysched(ctx
, instr
);
356 if (ret
== SCHEDULED
)
359 cnt
= MIN2(cnt
, ret
);
377 /* detect if we've gotten ourselves into an impossible situation
380 if (all_delayed
&& (attempted
> 0))
386 static void block_sched(struct ir3_sched_ctx
*ctx
, struct ir3_block
*block
)
388 struct ir3_instruction
*instr
;
390 /* schedule all the shader input's (meta-instr) first so that
391 * the RA step sees that the input registers contain a value
392 * from the start of the shader:
394 if (!block
->parent
) {
396 for (i
= 0; i
< block
->ninputs
; i
++) {
397 struct ir3_instruction
*in
= block
->inputs
[i
];
399 schedule(ctx
, in
, true);
403 while ((instr
= block
->head
) && !ctx
->error
) {
404 /* NOTE: always grab next *before* trysched(), in case the
405 * instruction is actually scheduled (and therefore moved
406 * from depth list into scheduled list)
408 struct ir3_instruction
*next
= instr
->next
;
409 int cnt
= trysched(ctx
, instr
);
412 cnt
= block_sched_undelayed(ctx
, block
);
414 /* -1 is signal to return up stack, but to us means same as 0: */
419 /* if deepest remaining instruction cannot be scheduled, try
420 * the increasingly more shallow instructions until needed
421 * number of delay slots is filled:
423 while (instr
&& (cnt
> ctx
->cnt
)) {
425 trysched(ctx
, instr
);
429 /* and if we run out of instructions that can be scheduled,
430 * then it is time for nop's:
432 while (cnt
> ctx
->cnt
)
433 schedule(ctx
, ir3_instr_create(block
, 0, OPC_NOP
), false);
436 /* at this point, scheduled list is in reverse order, so fix that: */
437 block
->head
= reverse(ctx
->scheduled
);
440 int ir3_block_sched(struct ir3_block
*block
)
442 struct ir3_sched_ctx ctx
= {0};
443 ir3_clear_mark(block
->shader
);
444 block_sched(&ctx
, block
);