2 * Copyright © 2010 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
29 #include "glsl/glsl_types.h"
30 #include "glsl/ir_optimization.h"
31 #include "glsl/ir_print_visitor.h"
33 /** @file brw_fs_schedule_instructions.cpp
35 * List scheduling of FS instructions.
37 * The basic model of the list scheduler is to take a basic block,
38 * compute a DAG of the dependencies (RAW ordering with latency, WAW
39 * ordering, WAR ordering), and make a list of the DAG heads.
40 * Heuristically pick a DAG head, then put all the children that are
41 * now DAG heads into the list of things to schedule.
43 * The heuristic is the important part. We're trying to be cheap,
44 * since actually computing the optimal scheduling is NP complete.
45 * What we do is track a "current clock". When we schedule a node, we
46 * update the earliest-unblocked clock time of its children, and
47 * increment the clock. Then, when trying to schedule, we just pick
48 * the earliest-unblocked instruction to schedule.
50 * Note that often there will be many things which could execute
51 * immediately, and there are a range of heuristic options to choose
52 * from in picking among those.
55 class schedule_node
: public exec_node
58 schedule_node(fs_inst
*inst
)
61 this->child_array_size
= 0;
62 this->children
= NULL
;
63 this->child_latency
= NULL
;
64 this->child_count
= 0;
65 this->parent_count
= 0;
66 this->unblocked_time
= 0;
69 int math_latency
= 22;
71 switch (inst
->opcode
) {
72 case SHADER_OPCODE_RCP
:
73 this->latency
= 1 * chans
* math_latency
;
75 case SHADER_OPCODE_RSQ
:
76 this->latency
= 2 * chans
* math_latency
;
78 case SHADER_OPCODE_INT_QUOTIENT
:
79 case SHADER_OPCODE_SQRT
:
80 case SHADER_OPCODE_LOG2
:
81 /* full precision log. partial is 2. */
82 this->latency
= 3 * chans
* math_latency
;
84 case SHADER_OPCODE_INT_REMAINDER
:
85 case SHADER_OPCODE_EXP2
:
86 /* full precision. partial is 3, same throughput. */
87 this->latency
= 4 * chans
* math_latency
;
89 case SHADER_OPCODE_POW
:
90 this->latency
= 8 * chans
* math_latency
;
92 case SHADER_OPCODE_SIN
:
93 case SHADER_OPCODE_COS
:
94 /* minimum latency, max is 12 rounds. */
95 this->latency
= 5 * chans
* math_latency
;
104 schedule_node
**children
;
108 int child_array_size
;
113 class instruction_scheduler
{
115 instruction_scheduler(fs_visitor
*v
, void *mem_ctx
, int grf_count
,
119 this->mem_ctx
= ralloc_context(mem_ctx
);
120 this->grf_count
= grf_count
;
121 this->instructions
.make_empty();
122 this->instructions_to_schedule
= 0;
123 this->post_reg_alloc
= post_reg_alloc
;
126 ~instruction_scheduler()
128 ralloc_free(this->mem_ctx
);
130 void add_barrier_deps(schedule_node
*n
);
131 void add_dep(schedule_node
*before
, schedule_node
*after
, int latency
);
132 void add_dep(schedule_node
*before
, schedule_node
*after
);
134 void add_inst(fs_inst
*inst
);
135 void calculate_deps();
136 void schedule_instructions(fs_inst
*next_block_header
);
138 bool is_compressed(fs_inst
*inst
);
143 int instructions_to_schedule
;
145 exec_list instructions
;
150 instruction_scheduler::add_inst(fs_inst
*inst
)
152 schedule_node
*n
= new(mem_ctx
) schedule_node(inst
);
154 assert(!inst
->is_head_sentinel());
155 assert(!inst
->is_tail_sentinel());
157 this->instructions_to_schedule
++;
160 instructions
.push_tail(n
);
164 * Add a dependency between two instruction nodes.
166 * The @after node will be scheduled after @before. We will try to
167 * schedule it @latency cycles after @before, but no guarantees there.
170 instruction_scheduler::add_dep(schedule_node
*before
, schedule_node
*after
,
173 if (!before
|| !after
)
176 assert(before
!= after
);
178 for (int i
= 0; i
< before
->child_count
; i
++) {
179 if (before
->children
[i
] == after
) {
180 before
->child_latency
[i
] = MAX2(before
->child_latency
[i
], latency
);
185 if (before
->child_array_size
<= before
->child_count
) {
186 if (before
->child_array_size
< 16)
187 before
->child_array_size
= 16;
189 before
->child_array_size
*= 2;
191 before
->children
= reralloc(mem_ctx
, before
->children
,
193 before
->child_array_size
);
194 before
->child_latency
= reralloc(mem_ctx
, before
->child_latency
,
195 int, before
->child_array_size
);
198 before
->children
[before
->child_count
] = after
;
199 before
->child_latency
[before
->child_count
] = latency
;
200 before
->child_count
++;
201 after
->parent_count
++;
205 instruction_scheduler::add_dep(schedule_node
*before
, schedule_node
*after
)
210 add_dep(before
, after
, before
->latency
);
214 * Sometimes we really want this node to execute after everything that
215 * was before it and before everything that followed it. This adds
219 instruction_scheduler::add_barrier_deps(schedule_node
*n
)
221 schedule_node
*prev
= (schedule_node
*)n
->prev
;
222 schedule_node
*next
= (schedule_node
*)n
->next
;
225 while (!prev
->is_head_sentinel()) {
227 prev
= (schedule_node
*)prev
->prev
;
232 while (!next
->is_tail_sentinel()) {
234 next
= (schedule_node
*)next
->next
;
239 /* instruction scheduling needs to be aware of when an MRF write
240 * actually writes 2 MRFs.
243 instruction_scheduler::is_compressed(fs_inst
*inst
)
245 return (v
->dispatch_width
== 16 &&
246 !inst
->force_uncompressed
&&
247 !inst
->force_sechalf
);
251 instruction_scheduler::calculate_deps()
253 /* Pre-register-allocation, this tracks the last write per VGRF (so
254 * different reg_offsets within it can interfere when they shouldn't).
255 * After register allocation, reg_offsets are gone and we track individual
258 schedule_node
*last_grf_write
[grf_count
];
259 schedule_node
*last_mrf_write
[BRW_MAX_MRF
];
260 schedule_node
*last_conditional_mod
[2] = { NULL
, NULL
};
261 /* Fixed HW registers are assumed to be separate from the virtual
262 * GRFs, so they can be tracked separately. We don't really write
263 * to fixed GRFs much, so don't bother tracking them on a more
266 schedule_node
*last_fixed_grf_write
= NULL
;
267 int reg_width
= v
->dispatch_width
/ 8;
269 /* The last instruction always needs to still be the last
270 * instruction. Either it's flow control (IF, ELSE, ENDIF, DO,
271 * WHILE) and scheduling other things after it would disturb the
272 * basic block, or it's FB_WRITE and we should do a better job at
273 * dead code elimination anyway.
275 schedule_node
*last
= (schedule_node
*)instructions
.get_tail();
276 add_barrier_deps(last
);
278 memset(last_grf_write
, 0, sizeof(last_grf_write
));
279 memset(last_mrf_write
, 0, sizeof(last_mrf_write
));
281 /* top-to-bottom dependencies: RAW and WAW. */
282 foreach_list(node
, &instructions
) {
283 schedule_node
*n
= (schedule_node
*)node
;
284 fs_inst
*inst
= n
->inst
;
286 /* read-after-write deps. */
287 for (int i
= 0; i
< 3; i
++) {
288 if (inst
->src
[i
].file
== GRF
) {
289 if (post_reg_alloc
) {
290 for (int r
= 0; r
< reg_width
; r
++)
291 add_dep(last_grf_write
[inst
->src
[i
].reg
+ r
], n
);
293 add_dep(last_grf_write
[inst
->src
[i
].reg
], n
);
295 } else if (inst
->src
[i
].file
== FIXED_HW_REG
&&
296 (inst
->src
[i
].fixed_hw_reg
.file
==
297 BRW_GENERAL_REGISTER_FILE
)) {
298 if (post_reg_alloc
) {
299 for (int r
= 0; r
< reg_width
; r
++)
300 add_dep(last_grf_write
[inst
->src
[i
].fixed_hw_reg
.nr
+ r
], n
);
302 add_dep(last_fixed_grf_write
, n
);
304 } else if (inst
->src
[i
].file
!= BAD_FILE
&&
305 inst
->src
[i
].file
!= IMM
&&
306 inst
->src
[i
].file
!= UNIFORM
) {
307 assert(inst
->src
[i
].file
!= MRF
);
312 for (int i
= 0; i
< inst
->mlen
; i
++) {
313 /* It looks like the MRF regs are released in the send
314 * instruction once it's sent, not when the result comes
317 add_dep(last_mrf_write
[inst
->base_mrf
+ i
], n
);
320 if (inst
->predicate
) {
321 add_dep(last_conditional_mod
[inst
->flag_subreg
], n
);
324 /* write-after-write deps. */
325 if (inst
->dst
.file
== GRF
) {
326 if (post_reg_alloc
) {
327 for (int r
= 0; r
< inst
->regs_written() * reg_width
; r
++) {
328 add_dep(last_grf_write
[inst
->dst
.reg
+ r
], n
);
329 last_grf_write
[inst
->dst
.reg
+ r
] = n
;
332 add_dep(last_grf_write
[inst
->dst
.reg
], n
);
333 last_grf_write
[inst
->dst
.reg
] = n
;
335 } else if (inst
->dst
.file
== MRF
) {
336 int reg
= inst
->dst
.reg
& ~BRW_MRF_COMPR4
;
338 add_dep(last_mrf_write
[reg
], n
);
339 last_mrf_write
[reg
] = n
;
340 if (is_compressed(inst
)) {
341 if (inst
->dst
.reg
& BRW_MRF_COMPR4
)
345 add_dep(last_mrf_write
[reg
], n
);
346 last_mrf_write
[reg
] = n
;
348 } else if (inst
->dst
.file
== FIXED_HW_REG
&&
349 inst
->dst
.fixed_hw_reg
.file
== BRW_GENERAL_REGISTER_FILE
) {
350 if (post_reg_alloc
) {
351 for (int r
= 0; r
< reg_width
; r
++)
352 last_grf_write
[inst
->dst
.fixed_hw_reg
.nr
+ r
] = n
;
354 last_fixed_grf_write
= n
;
356 } else if (inst
->dst
.file
!= BAD_FILE
) {
360 if (inst
->mlen
> 0) {
361 for (int i
= 0; i
< v
->implied_mrf_writes(inst
); i
++) {
362 add_dep(last_mrf_write
[inst
->base_mrf
+ i
], n
);
363 last_mrf_write
[inst
->base_mrf
+ i
] = n
;
367 /* Treat FS_OPCODE_MOV_DISPATCH_TO_FLAGS as though it had a
368 * conditional_mod, because it sets the flag register.
370 if (inst
->conditional_mod
||
371 inst
->opcode
== FS_OPCODE_MOV_DISPATCH_TO_FLAGS
) {
372 add_dep(last_conditional_mod
[inst
->flag_subreg
], n
, 0);
373 last_conditional_mod
[inst
->flag_subreg
] = n
;
377 /* bottom-to-top dependencies: WAR */
378 memset(last_grf_write
, 0, sizeof(last_grf_write
));
379 memset(last_mrf_write
, 0, sizeof(last_mrf_write
));
380 memset(last_conditional_mod
, 0, sizeof(last_conditional_mod
));
381 last_fixed_grf_write
= NULL
;
385 for (node
= instructions
.get_tail(), prev
= node
->prev
;
386 !node
->is_head_sentinel();
387 node
= prev
, prev
= node
->prev
) {
388 schedule_node
*n
= (schedule_node
*)node
;
389 fs_inst
*inst
= n
->inst
;
391 /* write-after-read deps. */
392 for (int i
= 0; i
< 3; i
++) {
393 if (inst
->src
[i
].file
== GRF
) {
394 if (post_reg_alloc
) {
395 for (int r
= 0; r
< reg_width
; r
++)
396 add_dep(n
, last_grf_write
[inst
->src
[i
].reg
+ r
]);
398 add_dep(n
, last_grf_write
[inst
->src
[i
].reg
]);
400 } else if (inst
->src
[i
].file
== FIXED_HW_REG
&&
401 (inst
->src
[i
].fixed_hw_reg
.file
==
402 BRW_GENERAL_REGISTER_FILE
)) {
403 if (post_reg_alloc
) {
404 for (int r
= 0; r
< reg_width
; r
++)
405 add_dep(n
, last_grf_write
[inst
->src
[i
].fixed_hw_reg
.nr
+ r
]);
407 add_dep(n
, last_fixed_grf_write
);
409 } else if (inst
->src
[i
].file
!= BAD_FILE
&&
410 inst
->src
[i
].file
!= IMM
&&
411 inst
->src
[i
].file
!= UNIFORM
) {
412 assert(inst
->src
[i
].file
!= MRF
);
417 for (int i
= 0; i
< inst
->mlen
; i
++) {
418 /* It looks like the MRF regs are released in the send
419 * instruction once it's sent, not when the result comes
422 add_dep(n
, last_mrf_write
[inst
->base_mrf
+ i
], 2);
425 if (inst
->predicate
) {
426 add_dep(n
, last_conditional_mod
[inst
->flag_subreg
]);
429 /* Update the things this instruction wrote, so earlier reads
430 * can mark this as WAR dependency.
432 if (inst
->dst
.file
== GRF
) {
433 if (post_reg_alloc
) {
434 for (int r
= 0; r
< inst
->regs_written() * reg_width
; r
++)
435 last_grf_write
[inst
->dst
.reg
+ r
] = n
;
437 last_grf_write
[inst
->dst
.reg
] = n
;
439 } else if (inst
->dst
.file
== MRF
) {
440 int reg
= inst
->dst
.reg
& ~BRW_MRF_COMPR4
;
442 last_mrf_write
[reg
] = n
;
444 if (is_compressed(inst
)) {
445 if (inst
->dst
.reg
& BRW_MRF_COMPR4
)
450 last_mrf_write
[reg
] = n
;
452 } else if (inst
->dst
.file
== FIXED_HW_REG
&&
453 inst
->dst
.fixed_hw_reg
.file
== BRW_GENERAL_REGISTER_FILE
) {
454 if (post_reg_alloc
) {
455 for (int r
= 0; r
< reg_width
; r
++)
456 last_grf_write
[inst
->dst
.fixed_hw_reg
.nr
+ r
] = n
;
458 last_fixed_grf_write
= n
;
460 } else if (inst
->dst
.file
!= BAD_FILE
) {
464 if (inst
->mlen
> 0) {
465 for (int i
= 0; i
< v
->implied_mrf_writes(inst
); i
++) {
466 last_mrf_write
[inst
->base_mrf
+ i
] = n
;
470 /* Treat FS_OPCODE_MOV_DISPATCH_TO_FLAGS as though it had a
471 * conditional_mod, because it sets the flag register.
473 if (inst
->conditional_mod
||
474 inst
->opcode
== FS_OPCODE_MOV_DISPATCH_TO_FLAGS
) {
475 last_conditional_mod
[inst
->flag_subreg
] = n
;
481 instruction_scheduler::schedule_instructions(fs_inst
*next_block_header
)
485 /* Remove non-DAG heads from the list. */
486 foreach_list_safe(node
, &instructions
) {
487 schedule_node
*n
= (schedule_node
*)node
;
488 if (n
->parent_count
!= 0)
492 while (!instructions
.is_empty()) {
493 schedule_node
*chosen
= NULL
;
496 foreach_list(node
, &instructions
) {
497 schedule_node
*n
= (schedule_node
*)node
;
499 if (!chosen
|| n
->unblocked_time
< chosen_time
) {
501 chosen_time
= n
->unblocked_time
;
505 /* Schedule this instruction. */
508 next_block_header
->insert_before(chosen
->inst
);
509 instructions_to_schedule
--;
511 /* Bump the clock. If we expected a delay for scheduling, then
512 * bump the clock to reflect that.
514 time
= MAX2(time
+ 1, chosen_time
);
516 /* Now that we've scheduled a new instruction, some of its
517 * children can be promoted to the list of instructions ready to
518 * be scheduled. Update the children's unblocked time for this
519 * DAG edge as we do so.
521 for (int i
= 0; i
< chosen
->child_count
; i
++) {
522 schedule_node
*child
= chosen
->children
[i
];
524 child
->unblocked_time
= MAX2(child
->unblocked_time
,
525 time
+ chosen
->child_latency
[i
]);
527 child
->parent_count
--;
528 if (child
->parent_count
== 0) {
529 instructions
.push_tail(child
);
533 /* Shared resource: the mathbox. There's one per EU (on later
534 * generations, it's even more limited pre-gen6), so if we send
535 * something off to it then the next math isn't going to make
536 * progress until the first is done.
538 if (chosen
->inst
->is_math()) {
539 foreach_list(node
, &instructions
) {
540 schedule_node
*n
= (schedule_node
*)node
;
542 if (n
->inst
->is_math())
543 n
->unblocked_time
= MAX2(n
->unblocked_time
,
544 time
+ chosen
->latency
);
549 assert(instructions_to_schedule
== 0);
553 fs_visitor::schedule_instructions(bool post_reg_alloc
)
555 fs_inst
*next_block_header
= (fs_inst
*)instructions
.head
;
559 grf_count
= grf_used
;
561 grf_count
= virtual_grf_count
;
563 instruction_scheduler
sched(this, mem_ctx
, grf_count
, post_reg_alloc
);
565 while (!next_block_header
->is_tail_sentinel()) {
566 /* Add things to be scheduled until we get to a new BB. */
567 while (!next_block_header
->is_tail_sentinel()) {
568 fs_inst
*inst
= next_block_header
;
569 next_block_header
= (fs_inst
*)next_block_header
->next
;
571 sched
.add_inst(inst
);
572 if (inst
->opcode
== BRW_OPCODE_IF
||
573 inst
->opcode
== BRW_OPCODE_ELSE
||
574 inst
->opcode
== BRW_OPCODE_ENDIF
||
575 inst
->opcode
== BRW_OPCODE_DO
||
576 inst
->opcode
== BRW_OPCODE_WHILE
||
577 inst
->opcode
== BRW_OPCODE_BREAK
||
578 inst
->opcode
== BRW_OPCODE_CONTINUE
) {
582 sched
.calculate_deps();
583 sched
.schedule_instructions(next_block_header
);
586 this->live_intervals_valid
= false;