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>
30 #include <sys/types.h>
32 #include "main/macros.h"
33 #include "main/shaderobj.h"
34 #include "main/uniforms.h"
35 #include "program/prog_optimize.h"
36 #include "program/register_allocate.h"
37 #include "program/sampler.h"
38 #include "program/hash_table.h"
39 #include "brw_context.h"
44 #include "../glsl/glsl_types.h"
45 #include "../glsl/ir_optimization.h"
46 #include "../glsl/ir_print_visitor.h"
48 /** @file brw_fs_schedule_instructions.cpp
50 * List scheduling of FS instructions.
52 * The basic model of the list scheduler is to take a basic block,
53 * compute a DAG of the dependencies (RAW ordering with latency, WAW
54 * ordering, WAR ordering), and make a list of the DAG heads.
55 * Heuristically pick a DAG head, then put all the children that are
56 * now DAG heads into the list of things to schedule.
58 * The heuristic is the important part. We're trying to be cheap,
59 * since actually computing the optimal scheduling is NP complete.
60 * What we do is track a "current clock". When we schedule a node, we
61 * update the earliest-unblocked clock time of its children, and
62 * increment the clock. Then, when trying to schedule, we just pick
63 * the earliest-unblocked instruction to schedule.
65 * Note that often there will be many things which could execute
66 * immediately, and there are a range of heuristic options to choose
67 * from in picking among those.
70 class schedule_node
: public exec_node
73 schedule_node(fs_inst
*inst
)
76 this->child_array_size
= 0;
77 this->children
= NULL
;
78 this->child_latency
= NULL
;
79 this->child_count
= 0;
80 this->parent_count
= 0;
81 this->unblocked_time
= 0;
84 int math_latency
= 22;
86 switch (inst
->opcode
) {
88 this->latency
= 1 * chans
* math_latency
;
91 this->latency
= 2 * chans
* math_latency
;
95 /* full precision log. partial is 2. */
96 this->latency
= 3 * chans
* math_latency
;
99 /* full precision. partial is 3, same throughput. */
100 this->latency
= 4 * chans
* math_latency
;
103 this->latency
= 8 * chans
* math_latency
;
107 /* minimum latency, max is 12 rounds. */
108 this->latency
= 5 * chans
* math_latency
;
117 schedule_node
**children
;
121 int child_array_size
;
126 class instruction_scheduler
{
128 instruction_scheduler(fs_visitor
*v
, void *mem_ctx
, int virtual_grf_count
)
131 this->mem_ctx
= ralloc_context(mem_ctx
);
132 this->virtual_grf_count
= virtual_grf_count
;
133 this->instructions
.make_empty();
134 this->instructions_to_schedule
= 0;
137 ~instruction_scheduler()
139 ralloc_free(this->mem_ctx
);
141 void add_barrier_deps(schedule_node
*n
);
142 void add_dep(schedule_node
*before
, schedule_node
*after
, int latency
);
144 void add_inst(fs_inst
*inst
);
145 void calculate_deps();
146 void schedule_instructions(fs_inst
*next_block_header
);
150 int instructions_to_schedule
;
151 int virtual_grf_count
;
152 exec_list instructions
;
157 instruction_scheduler::add_inst(fs_inst
*inst
)
159 schedule_node
*n
= new(mem_ctx
) schedule_node(inst
);
161 assert(!inst
->is_head_sentinel());
162 assert(!inst
->is_tail_sentinel());
164 this->instructions_to_schedule
++;
167 instructions
.push_tail(n
);
171 * Add a dependency between two instruction nodes.
173 * The @after node will be scheduled after @before. We will try to
174 * schedule it @latency cycles after @before, but no guarantees there.
177 instruction_scheduler::add_dep(schedule_node
*before
, schedule_node
*after
,
180 if (!before
|| !after
)
183 assert(before
!= after
);
185 for (int i
= 0; i
< before
->child_count
; i
++) {
186 if (before
->children
[i
] == after
) {
187 before
->child_latency
[i
] = MAX2(before
->child_latency
[i
], latency
);
192 if (before
->child_array_size
<= before
->child_count
) {
193 if (before
->child_array_size
< 16)
194 before
->child_array_size
= 16;
196 before
->child_array_size
*= 2;
198 before
->children
= reralloc(mem_ctx
, before
->children
,
200 before
->child_array_size
);
201 before
->child_latency
= reralloc(mem_ctx
, before
->child_latency
,
202 int, before
->child_array_size
);
205 before
->children
[before
->child_count
] = after
;
206 before
->child_latency
[before
->child_count
] = latency
;
207 before
->child_count
++;
208 after
->parent_count
++;
212 * Sometimes we really want this node to execute after everything that
213 * was before it and before everything that followed it. This adds
217 instruction_scheduler::add_barrier_deps(schedule_node
*n
)
219 schedule_node
*prev
= (schedule_node
*)n
->prev
;
220 schedule_node
*next
= (schedule_node
*)n
->next
;
223 while (!prev
->is_head_sentinel()) {
225 prev
= (schedule_node
*)prev
->prev
;
230 while (!next
->is_tail_sentinel()) {
232 next
= (schedule_node
*)next
->next
;
238 instruction_scheduler::calculate_deps()
240 schedule_node
*last_grf_write
[virtual_grf_count
];
241 schedule_node
*last_mrf_write
[BRW_MAX_MRF
];
242 schedule_node
*last_conditional_mod
= NULL
;
244 /* The last instruction always needs to still be the last
245 * instruction. Either it's flow control (IF, ELSE, ENDIF, DO,
246 * WHILE) and scheduling other things after it would disturb the
247 * basic block, or it's FB_WRITE and we should do a better job at
248 * dead code elimination anyway.
250 schedule_node
*last
= (schedule_node
*)instructions
.get_tail();
251 add_barrier_deps(last
);
253 memset(last_grf_write
, 0, sizeof(last_grf_write
));
254 memset(last_mrf_write
, 0, sizeof(last_mrf_write
));
256 /* top-to-bottom dependencies: RAW and WAW. */
257 foreach_iter(exec_list_iterator
, iter
, instructions
) {
258 schedule_node
*n
= (schedule_node
*)iter
.get();
259 fs_inst
*inst
= n
->inst
;
261 /* read-after-write deps. */
262 for (int i
= 0; i
< 3; i
++) {
263 if (inst
->src
[i
].file
== GRF
) {
264 if (last_grf_write
[inst
->src
[i
].reg
]) {
265 add_dep(last_grf_write
[inst
->src
[i
].reg
], n
,
266 last_grf_write
[inst
->src
[i
].reg
]->latency
);
268 } else if (inst
->src
[i
].file
!= BAD_FILE
&&
269 inst
->src
[i
].file
!= IMM
&&
270 inst
->src
[i
].file
!= UNIFORM
) {
271 assert(inst
->src
[i
].file
!= MRF
);
276 for (int i
= 0; i
< inst
->mlen
; i
++) {
277 /* It looks like the MRF regs are released in the send
278 * instruction once it's sent, not when the result comes
281 if (last_mrf_write
[inst
->base_mrf
+ i
]) {
282 add_dep(last_mrf_write
[inst
->base_mrf
+ i
], n
,
283 last_mrf_write
[inst
->base_mrf
+ i
]->latency
);
287 if (inst
->predicated
) {
288 assert(last_conditional_mod
);
289 add_dep(last_conditional_mod
, n
, last_conditional_mod
->latency
);
292 /* write-after-write deps. */
293 if (inst
->dst
.file
== GRF
) {
294 if (last_grf_write
[inst
->dst
.reg
]) {
295 add_dep(last_grf_write
[inst
->dst
.reg
], n
,
296 last_grf_write
[inst
->dst
.reg
]->latency
);
298 last_grf_write
[inst
->dst
.reg
] = n
;
299 } else if (inst
->dst
.file
== MRF
) {
300 if (last_mrf_write
[inst
->dst
.hw_reg
]) {
301 add_dep(last_mrf_write
[inst
->dst
.hw_reg
], n
,
302 last_mrf_write
[inst
->dst
.hw_reg
]->latency
);
304 last_mrf_write
[inst
->dst
.hw_reg
] = n
;
305 } else if (inst
->dst
.file
!= BAD_FILE
) {
309 if (inst
->mlen
> 0) {
310 for (int i
= 0; i
< v
->implied_mrf_writes(inst
); i
++) {
311 if (last_mrf_write
[inst
->base_mrf
+ i
]) {
312 add_dep(last_mrf_write
[inst
->base_mrf
+ i
], n
,
313 last_mrf_write
[inst
->base_mrf
+ i
]->latency
);
315 last_mrf_write
[inst
->base_mrf
+ i
] = n
;
319 if (inst
->conditional_mod
) {
320 add_dep(last_conditional_mod
, n
, 0);
321 last_conditional_mod
= n
;
325 /* bottom-to-top dependencies: WAR */
326 memset(last_grf_write
, 0, sizeof(last_grf_write
));
327 memset(last_mrf_write
, 0, sizeof(last_mrf_write
));
328 last_conditional_mod
= NULL
;
332 for (node
= instructions
.get_tail(), prev
= node
->prev
;
333 !node
->is_head_sentinel();
334 node
= prev
, prev
= node
->prev
) {
335 schedule_node
*n
= (schedule_node
*)node
;
336 fs_inst
*inst
= n
->inst
;
338 /* write-after-read deps. */
339 for (int i
= 0; i
< 3; i
++) {
340 if (inst
->src
[i
].file
== GRF
) {
341 if (last_grf_write
[inst
->src
[i
].reg
]) {
342 add_dep(n
, last_grf_write
[inst
->src
[i
].reg
], n
->latency
);
344 } else if (inst
->src
[i
].file
!= BAD_FILE
&&
345 inst
->src
[i
].file
!= IMM
&&
346 inst
->src
[i
].file
!= UNIFORM
) {
347 assert(inst
->src
[i
].file
!= MRF
);
352 for (int i
= 0; i
< inst
->mlen
; i
++) {
353 /* It looks like the MRF regs are released in the send
354 * instruction once it's sent, not when the result comes
357 add_dep(n
, last_mrf_write
[inst
->base_mrf
+ i
], 2);
360 if (inst
->predicated
) {
361 if (last_conditional_mod
) {
362 add_dep(n
, last_conditional_mod
, n
->latency
);
366 /* Update the things this instruction wrote, so earlier reads
367 * can mark this as WAR dependency.
369 if (inst
->dst
.file
== GRF
) {
370 last_grf_write
[inst
->dst
.reg
] = n
;
371 } else if (inst
->dst
.file
== MRF
) {
372 last_mrf_write
[inst
->dst
.hw_reg
] = n
;
373 } else if (inst
->dst
.file
!= BAD_FILE
) {
377 if (inst
->mlen
> 0) {
378 for (int i
= 0; i
< v
->implied_mrf_writes(inst
); i
++) {
379 last_mrf_write
[inst
->base_mrf
+ i
] = n
;
383 if (inst
->conditional_mod
)
384 last_conditional_mod
= n
;
389 instruction_scheduler::schedule_instructions(fs_inst
*next_block_header
)
393 /* Remove non-DAG heads from the list. */
394 foreach_iter(exec_list_iterator
, iter
, instructions
) {
395 schedule_node
*n
= (schedule_node
*)iter
.get();
396 if (n
->parent_count
!= 0)
400 while (!instructions
.is_empty()) {
401 schedule_node
*chosen
= NULL
;
404 foreach_iter(exec_list_iterator
, iter
, instructions
) {
405 schedule_node
*n
= (schedule_node
*)iter
.get();
407 if (!chosen
|| n
->unblocked_time
< chosen_time
) {
409 chosen_time
= n
->unblocked_time
;
413 /* Schedule this instruction. */
416 next_block_header
->insert_before(chosen
->inst
);
417 instructions_to_schedule
--;
419 /* Bump the clock. If we expected a delay for scheduling, then
420 * bump the clock to reflect that.
422 time
= MAX2(time
+ 1, chosen_time
);
424 /* Now that we've scheduled a new instruction, some of its
425 * children can be promoted to the list of instructions ready to
426 * be scheduled. Update the children's unblocked time for this
427 * DAG edge as we do so.
429 for (int i
= 0; i
< chosen
->child_count
; i
++) {
430 schedule_node
*child
= chosen
->children
[i
];
432 child
->unblocked_time
= MAX2(child
->unblocked_time
,
433 time
+ chosen
->child_latency
[i
]);
435 child
->parent_count
--;
436 if (child
->parent_count
== 0) {
437 instructions
.push_tail(child
);
441 /* Shared resource: the mathbox. There's one per EU (on later
442 * generations, it's even more limited pre-gen6), so if we send
443 * something off to it then the next math isn't going to make
444 * progress until the first is done.
446 if (chosen
->inst
->is_math()) {
447 foreach_iter(exec_list_iterator
, iter
, instructions
) {
448 schedule_node
*n
= (schedule_node
*)iter
.get();
450 if (n
->inst
->is_math())
451 n
->unblocked_time
= MAX2(n
->unblocked_time
,
452 time
+ chosen
->latency
);
457 assert(instructions_to_schedule
== 0);
461 fs_visitor::schedule_instructions()
463 fs_inst
*next_block_header
= (fs_inst
*)instructions
.head
;
464 instruction_scheduler
sched(this, mem_ctx
, this->virtual_grf_next
);
466 while (!next_block_header
->is_tail_sentinel()) {
467 /* Add things to be scheduled until we get to a new BB. */
468 while (!next_block_header
->is_tail_sentinel()) {
469 fs_inst
*inst
= next_block_header
;
470 next_block_header
= (fs_inst
*)next_block_header
->next
;
472 sched
.add_inst(inst
);
473 if (inst
->opcode
== BRW_OPCODE_IF
||
474 inst
->opcode
== BRW_OPCODE_ELSE
||
475 inst
->opcode
== BRW_OPCODE_ENDIF
||
476 inst
->opcode
== BRW_OPCODE_DO
||
477 inst
->opcode
== BRW_OPCODE_WHILE
||
478 inst
->opcode
== BRW_OPCODE_BREAK
||
479 inst
->opcode
== BRW_OPCODE_CONTINUE
) {
483 sched
.calculate_deps();
484 sched
.schedule_instructions(next_block_header
);
487 this->live_intervals_valid
= false;