intel/fs: Add a generic SEND opcode
[mesa.git] / src / intel / compiler / brw_schedule_instructions.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_fs.h"
29 #include "brw_fs_live_variables.h"
30 #include "brw_vec4.h"
31 #include "brw_cfg.h"
32 #include "brw_shader.h"
33
34 using namespace brw;
35
36 /** @file brw_fs_schedule_instructions.cpp
37 *
38 * List scheduling of FS instructions.
39 *
40 * The basic model of the list scheduler is to take a basic block,
41 * compute a DAG of the dependencies (RAW ordering with latency, WAW
42 * ordering with latency, WAR ordering), and make a list of the DAG heads.
43 * Heuristically pick a DAG head, then put all the children that are
44 * now DAG heads into the list of things to schedule.
45 *
46 * The heuristic is the important part. We're trying to be cheap,
47 * since actually computing the optimal scheduling is NP complete.
48 * What we do is track a "current clock". When we schedule a node, we
49 * update the earliest-unblocked clock time of its children, and
50 * increment the clock. Then, when trying to schedule, we just pick
51 * the earliest-unblocked instruction to schedule.
52 *
53 * Note that often there will be many things which could execute
54 * immediately, and there are a range of heuristic options to choose
55 * from in picking among those.
56 */
57
58 static bool debug = false;
59
60 class instruction_scheduler;
61
62 class schedule_node : public exec_node
63 {
64 public:
65 schedule_node(backend_instruction *inst, instruction_scheduler *sched);
66 void set_latency_gen4();
67 void set_latency_gen7(bool is_haswell);
68
69 backend_instruction *inst;
70 schedule_node **children;
71 int *child_latency;
72 int child_count;
73 int parent_count;
74 int child_array_size;
75 int unblocked_time;
76 int latency;
77
78 /**
79 * Which iteration of pushing groups of children onto the candidates list
80 * this node was a part of.
81 */
82 unsigned cand_generation;
83
84 /**
85 * This is the sum of the instruction's latency plus the maximum delay of
86 * its children, or just the issue_time if it's a leaf node.
87 */
88 int delay;
89
90 /**
91 * Preferred exit node among the (direct or indirect) successors of this
92 * node. Among the scheduler nodes blocked by this node, this will be the
93 * one that may cause earliest program termination, or NULL if none of the
94 * successors is an exit node.
95 */
96 schedule_node *exit;
97 };
98
99 /**
100 * Lower bound of the scheduling time after which one of the instructions
101 * blocked by this node may lead to program termination.
102 *
103 * exit_unblocked_time() determines a strict partial ordering relation '«' on
104 * the set of scheduler nodes as follows:
105 *
106 * n « m <-> exit_unblocked_time(n) < exit_unblocked_time(m)
107 *
108 * which can be used to heuristically order nodes according to how early they
109 * can unblock an exit node and lead to program termination.
110 */
111 static inline int
112 exit_unblocked_time(const schedule_node *n)
113 {
114 return n->exit ? n->exit->unblocked_time : INT_MAX;
115 }
116
117 void
118 schedule_node::set_latency_gen4()
119 {
120 int chans = 8;
121 int math_latency = 22;
122
123 switch (inst->opcode) {
124 case SHADER_OPCODE_RCP:
125 this->latency = 1 * chans * math_latency;
126 break;
127 case SHADER_OPCODE_RSQ:
128 this->latency = 2 * chans * math_latency;
129 break;
130 case SHADER_OPCODE_INT_QUOTIENT:
131 case SHADER_OPCODE_SQRT:
132 case SHADER_OPCODE_LOG2:
133 /* full precision log. partial is 2. */
134 this->latency = 3 * chans * math_latency;
135 break;
136 case SHADER_OPCODE_INT_REMAINDER:
137 case SHADER_OPCODE_EXP2:
138 /* full precision. partial is 3, same throughput. */
139 this->latency = 4 * chans * math_latency;
140 break;
141 case SHADER_OPCODE_POW:
142 this->latency = 8 * chans * math_latency;
143 break;
144 case SHADER_OPCODE_SIN:
145 case SHADER_OPCODE_COS:
146 /* minimum latency, max is 12 rounds. */
147 this->latency = 5 * chans * math_latency;
148 break;
149 default:
150 this->latency = 2;
151 break;
152 }
153 }
154
155 void
156 schedule_node::set_latency_gen7(bool is_haswell)
157 {
158 switch (inst->opcode) {
159 case BRW_OPCODE_MAD:
160 /* 2 cycles
161 * (since the last two src operands are in different register banks):
162 * mad(8) g4<1>F g2.2<4,4,1>F.x g2<4,4,1>F.x g3.1<4,4,1>F.x { align16 WE_normal 1Q };
163 *
164 * 3 cycles on IVB, 4 on HSW
165 * (since the last two src operands are in the same register bank):
166 * mad(8) g4<1>F g2.2<4,4,1>F.x g2<4,4,1>F.x g2.1<4,4,1>F.x { align16 WE_normal 1Q };
167 *
168 * 18 cycles on IVB, 16 on HSW
169 * (since the last two src operands are in different register banks):
170 * mad(8) g4<1>F g2.2<4,4,1>F.x g2<4,4,1>F.x g3.1<4,4,1>F.x { align16 WE_normal 1Q };
171 * mov(8) null g4<4,5,1>F { align16 WE_normal 1Q };
172 *
173 * 20 cycles on IVB, 18 on HSW
174 * (since the last two src operands are in the same register bank):
175 * mad(8) g4<1>F g2.2<4,4,1>F.x g2<4,4,1>F.x g2.1<4,4,1>F.x { align16 WE_normal 1Q };
176 * mov(8) null g4<4,4,1>F { align16 WE_normal 1Q };
177 */
178
179 /* Our register allocator doesn't know about register banks, so use the
180 * higher latency.
181 */
182 latency = is_haswell ? 16 : 18;
183 break;
184
185 case BRW_OPCODE_LRP:
186 /* 2 cycles
187 * (since the last two src operands are in different register banks):
188 * lrp(8) g4<1>F g2.2<4,4,1>F.x g2<4,4,1>F.x g3.1<4,4,1>F.x { align16 WE_normal 1Q };
189 *
190 * 3 cycles on IVB, 4 on HSW
191 * (since the last two src operands are in the same register bank):
192 * lrp(8) g4<1>F g2.2<4,4,1>F.x g2<4,4,1>F.x g2.1<4,4,1>F.x { align16 WE_normal 1Q };
193 *
194 * 16 cycles on IVB, 14 on HSW
195 * (since the last two src operands are in different register banks):
196 * lrp(8) g4<1>F g2.2<4,4,1>F.x g2<4,4,1>F.x g3.1<4,4,1>F.x { align16 WE_normal 1Q };
197 * mov(8) null g4<4,4,1>F { align16 WE_normal 1Q };
198 *
199 * 16 cycles
200 * (since the last two src operands are in the same register bank):
201 * lrp(8) g4<1>F g2.2<4,4,1>F.x g2<4,4,1>F.x g2.1<4,4,1>F.x { align16 WE_normal 1Q };
202 * mov(8) null g4<4,4,1>F { align16 WE_normal 1Q };
203 */
204
205 /* Our register allocator doesn't know about register banks, so use the
206 * higher latency.
207 */
208 latency = 14;
209 break;
210
211 case SHADER_OPCODE_RCP:
212 case SHADER_OPCODE_RSQ:
213 case SHADER_OPCODE_SQRT:
214 case SHADER_OPCODE_LOG2:
215 case SHADER_OPCODE_EXP2:
216 case SHADER_OPCODE_SIN:
217 case SHADER_OPCODE_COS:
218 /* 2 cycles:
219 * math inv(8) g4<1>F g2<0,1,0>F null { align1 WE_normal 1Q };
220 *
221 * 18 cycles:
222 * math inv(8) g4<1>F g2<0,1,0>F null { align1 WE_normal 1Q };
223 * mov(8) null g4<8,8,1>F { align1 WE_normal 1Q };
224 *
225 * Same for exp2, log2, rsq, sqrt, sin, cos.
226 */
227 latency = is_haswell ? 14 : 16;
228 break;
229
230 case SHADER_OPCODE_POW:
231 /* 2 cycles:
232 * math pow(8) g4<1>F g2<0,1,0>F g2.1<0,1,0>F { align1 WE_normal 1Q };
233 *
234 * 26 cycles:
235 * math pow(8) g4<1>F g2<0,1,0>F g2.1<0,1,0>F { align1 WE_normal 1Q };
236 * mov(8) null g4<8,8,1>F { align1 WE_normal 1Q };
237 */
238 latency = is_haswell ? 22 : 24;
239 break;
240
241 case SHADER_OPCODE_TEX:
242 case SHADER_OPCODE_TXD:
243 case SHADER_OPCODE_TXF:
244 case SHADER_OPCODE_TXF_LZ:
245 case SHADER_OPCODE_TXL:
246 case SHADER_OPCODE_TXL_LZ:
247 /* 18 cycles:
248 * mov(8) g115<1>F 0F { align1 WE_normal 1Q };
249 * mov(8) g114<1>F 0F { align1 WE_normal 1Q };
250 * send(8) g4<1>UW g114<8,8,1>F
251 * sampler (10, 0, 0, 1) mlen 2 rlen 4 { align1 WE_normal 1Q };
252 *
253 * 697 +/-49 cycles (min 610, n=26):
254 * mov(8) g115<1>F 0F { align1 WE_normal 1Q };
255 * mov(8) g114<1>F 0F { align1 WE_normal 1Q };
256 * send(8) g4<1>UW g114<8,8,1>F
257 * sampler (10, 0, 0, 1) mlen 2 rlen 4 { align1 WE_normal 1Q };
258 * mov(8) null g4<8,8,1>F { align1 WE_normal 1Q };
259 *
260 * So the latency on our first texture load of the batchbuffer takes
261 * ~700 cycles, since the caches are cold at that point.
262 *
263 * 840 +/- 92 cycles (min 720, n=25):
264 * mov(8) g115<1>F 0F { align1 WE_normal 1Q };
265 * mov(8) g114<1>F 0F { align1 WE_normal 1Q };
266 * send(8) g4<1>UW g114<8,8,1>F
267 * sampler (10, 0, 0, 1) mlen 2 rlen 4 { align1 WE_normal 1Q };
268 * mov(8) null g4<8,8,1>F { align1 WE_normal 1Q };
269 * send(8) g4<1>UW g114<8,8,1>F
270 * sampler (10, 0, 0, 1) mlen 2 rlen 4 { align1 WE_normal 1Q };
271 * mov(8) null g4<8,8,1>F { align1 WE_normal 1Q };
272 *
273 * On the second load, it takes just an extra ~140 cycles, and after
274 * accounting for the 14 cycles of the MOV's latency, that makes ~130.
275 *
276 * 683 +/- 49 cycles (min = 602, n=47):
277 * mov(8) g115<1>F 0F { align1 WE_normal 1Q };
278 * mov(8) g114<1>F 0F { align1 WE_normal 1Q };
279 * send(8) g4<1>UW g114<8,8,1>F
280 * sampler (10, 0, 0, 1) mlen 2 rlen 4 { align1 WE_normal 1Q };
281 * send(8) g50<1>UW g114<8,8,1>F
282 * sampler (10, 0, 0, 1) mlen 2 rlen 4 { align1 WE_normal 1Q };
283 * mov(8) null g4<8,8,1>F { align1 WE_normal 1Q };
284 *
285 * The unit appears to be pipelined, since this matches up with the
286 * cache-cold case, despite there being two loads here. If you replace
287 * the g4 in the MOV to null with g50, it's still 693 +/- 52 (n=39).
288 *
289 * So, take some number between the cache-hot 140 cycles and the
290 * cache-cold 700 cycles. No particular tuning was done on this.
291 *
292 * I haven't done significant testing of the non-TEX opcodes. TXL at
293 * least looked about the same as TEX.
294 */
295 latency = 200;
296 break;
297
298 case SHADER_OPCODE_TXS:
299 /* Testing textureSize(sampler2D, 0), one load was 420 +/- 41
300 * cycles (n=15):
301 * mov(8) g114<1>UD 0D { align1 WE_normal 1Q };
302 * send(8) g6<1>UW g114<8,8,1>F
303 * sampler (10, 0, 10, 1) mlen 1 rlen 4 { align1 WE_normal 1Q };
304 * mov(16) g6<1>F g6<8,8,1>D { align1 WE_normal 1Q };
305 *
306 *
307 * Two loads was 535 +/- 30 cycles (n=19):
308 * mov(16) g114<1>UD 0D { align1 WE_normal 1H };
309 * send(16) g6<1>UW g114<8,8,1>F
310 * sampler (10, 0, 10, 2) mlen 2 rlen 8 { align1 WE_normal 1H };
311 * mov(16) g114<1>UD 0D { align1 WE_normal 1H };
312 * mov(16) g6<1>F g6<8,8,1>D { align1 WE_normal 1H };
313 * send(16) g8<1>UW g114<8,8,1>F
314 * sampler (10, 0, 10, 2) mlen 2 rlen 8 { align1 WE_normal 1H };
315 * mov(16) g8<1>F g8<8,8,1>D { align1 WE_normal 1H };
316 * add(16) g6<1>F g6<8,8,1>F g8<8,8,1>F { align1 WE_normal 1H };
317 *
318 * Since the only caches that should matter are just the
319 * instruction/state cache containing the surface state, assume that we
320 * always have hot caches.
321 */
322 latency = 100;
323 break;
324
325 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4:
326 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
327 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
328 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7:
329 case VS_OPCODE_PULL_CONSTANT_LOAD:
330 /* testing using varying-index pull constants:
331 *
332 * 16 cycles:
333 * mov(8) g4<1>D g2.1<0,1,0>F { align1 WE_normal 1Q };
334 * send(8) g4<1>F g4<8,8,1>D
335 * data (9, 2, 3) mlen 1 rlen 1 { align1 WE_normal 1Q };
336 *
337 * ~480 cycles:
338 * mov(8) g4<1>D g2.1<0,1,0>F { align1 WE_normal 1Q };
339 * send(8) g4<1>F g4<8,8,1>D
340 * data (9, 2, 3) mlen 1 rlen 1 { align1 WE_normal 1Q };
341 * mov(8) null g4<8,8,1>F { align1 WE_normal 1Q };
342 *
343 * ~620 cycles:
344 * mov(8) g4<1>D g2.1<0,1,0>F { align1 WE_normal 1Q };
345 * send(8) g4<1>F g4<8,8,1>D
346 * data (9, 2, 3) mlen 1 rlen 1 { align1 WE_normal 1Q };
347 * mov(8) null g4<8,8,1>F { align1 WE_normal 1Q };
348 * send(8) g4<1>F g4<8,8,1>D
349 * data (9, 2, 3) mlen 1 rlen 1 { align1 WE_normal 1Q };
350 * mov(8) null g4<8,8,1>F { align1 WE_normal 1Q };
351 *
352 * So, if it's cache-hot, it's about 140. If it's cache cold, it's
353 * about 460. We expect to mostly be cache hot, so pick something more
354 * in that direction.
355 */
356 latency = 200;
357 break;
358
359 case SHADER_OPCODE_GEN7_SCRATCH_READ:
360 /* Testing a load from offset 0, that had been previously written:
361 *
362 * send(8) g114<1>UW g0<8,8,1>F data (0, 0, 0) mlen 1 rlen 1 { align1 WE_normal 1Q };
363 * mov(8) null g114<8,8,1>F { align1 WE_normal 1Q };
364 *
365 * The cycles spent seemed to be grouped around 40-50 (as low as 38),
366 * then around 140. Presumably this is cache hit vs miss.
367 */
368 latency = 50;
369 break;
370
371 case SHADER_OPCODE_UNTYPED_ATOMIC:
372 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT:
373 case SHADER_OPCODE_TYPED_ATOMIC:
374 /* Test code:
375 * mov(8) g112<1>ud 0x00000000ud { align1 WE_all 1Q };
376 * mov(1) g112.7<1>ud g1.7<0,1,0>ud { align1 WE_all };
377 * mov(8) g113<1>ud 0x00000000ud { align1 WE_normal 1Q };
378 * send(8) g4<1>ud g112<8,8,1>ud
379 * data (38, 5, 6) mlen 2 rlen 1 { align1 WE_normal 1Q };
380 *
381 * Running it 100 times as fragment shader on a 128x128 quad
382 * gives an average latency of 13867 cycles per atomic op,
383 * standard deviation 3%. Note that this is a rather
384 * pessimistic estimate, the actual latency in cases with few
385 * collisions between threads and favorable pipelining has been
386 * seen to be reduced by a factor of 100.
387 */
388 latency = 14000;
389 break;
390
391 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
392 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
393 case SHADER_OPCODE_TYPED_SURFACE_READ:
394 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
395 /* Test code:
396 * mov(8) g112<1>UD 0x00000000UD { align1 WE_all 1Q };
397 * mov(1) g112.7<1>UD g1.7<0,1,0>UD { align1 WE_all };
398 * mov(8) g113<1>UD 0x00000000UD { align1 WE_normal 1Q };
399 * send(8) g4<1>UD g112<8,8,1>UD
400 * data (38, 6, 5) mlen 2 rlen 1 { align1 WE_normal 1Q };
401 * .
402 * . [repeats 8 times]
403 * .
404 * mov(8) g112<1>UD 0x00000000UD { align1 WE_all 1Q };
405 * mov(1) g112.7<1>UD g1.7<0,1,0>UD { align1 WE_all };
406 * mov(8) g113<1>UD 0x00000000UD { align1 WE_normal 1Q };
407 * send(8) g4<1>UD g112<8,8,1>UD
408 * data (38, 6, 5) mlen 2 rlen 1 { align1 WE_normal 1Q };
409 *
410 * Running it 100 times as fragment shader on a 128x128 quad
411 * gives an average latency of 583 cycles per surface read,
412 * standard deviation 0.9%.
413 */
414 latency = is_haswell ? 300 : 600;
415 break;
416
417 case SHADER_OPCODE_SEND:
418 switch (inst->sfid) {
419 default:
420 unreachable("Unknown SFID");
421 }
422 break;
423
424 default:
425 /* 2 cycles:
426 * mul(8) g4<1>F g2<0,1,0>F 0.5F { align1 WE_normal 1Q };
427 *
428 * 16 cycles:
429 * mul(8) g4<1>F g2<0,1,0>F 0.5F { align1 WE_normal 1Q };
430 * mov(8) null g4<8,8,1>F { align1 WE_normal 1Q };
431 */
432 latency = 14;
433 break;
434 }
435 }
436
437 class instruction_scheduler {
438 public:
439 instruction_scheduler(backend_shader *s, int grf_count,
440 unsigned hw_reg_count, int block_count,
441 instruction_scheduler_mode mode)
442 {
443 this->bs = s;
444 this->mem_ctx = ralloc_context(NULL);
445 this->grf_count = grf_count;
446 this->hw_reg_count = hw_reg_count;
447 this->instructions.make_empty();
448 this->instructions_to_schedule = 0;
449 this->post_reg_alloc = (mode == SCHEDULE_POST);
450 this->mode = mode;
451 if (!post_reg_alloc) {
452 this->reg_pressure_in = rzalloc_array(mem_ctx, int, block_count);
453
454 this->livein = ralloc_array(mem_ctx, BITSET_WORD *, block_count);
455 for (int i = 0; i < block_count; i++)
456 this->livein[i] = rzalloc_array(mem_ctx, BITSET_WORD,
457 BITSET_WORDS(grf_count));
458
459 this->liveout = ralloc_array(mem_ctx, BITSET_WORD *, block_count);
460 for (int i = 0; i < block_count; i++)
461 this->liveout[i] = rzalloc_array(mem_ctx, BITSET_WORD,
462 BITSET_WORDS(grf_count));
463
464 this->hw_liveout = ralloc_array(mem_ctx, BITSET_WORD *, block_count);
465 for (int i = 0; i < block_count; i++)
466 this->hw_liveout[i] = rzalloc_array(mem_ctx, BITSET_WORD,
467 BITSET_WORDS(hw_reg_count));
468
469 this->written = rzalloc_array(mem_ctx, bool, grf_count);
470
471 this->reads_remaining = rzalloc_array(mem_ctx, int, grf_count);
472
473 this->hw_reads_remaining = rzalloc_array(mem_ctx, int, hw_reg_count);
474 } else {
475 this->reg_pressure_in = NULL;
476 this->livein = NULL;
477 this->liveout = NULL;
478 this->hw_liveout = NULL;
479 this->written = NULL;
480 this->reads_remaining = NULL;
481 this->hw_reads_remaining = NULL;
482 }
483 }
484
485 ~instruction_scheduler()
486 {
487 ralloc_free(this->mem_ctx);
488 }
489 void add_barrier_deps(schedule_node *n);
490 void add_dep(schedule_node *before, schedule_node *after, int latency);
491 void add_dep(schedule_node *before, schedule_node *after);
492
493 void run(cfg_t *cfg);
494 void add_insts_from_block(bblock_t *block);
495 void compute_delays();
496 void compute_exits();
497 virtual void calculate_deps() = 0;
498 virtual schedule_node *choose_instruction_to_schedule() = 0;
499
500 /**
501 * Returns how many cycles it takes the instruction to issue.
502 *
503 * Instructions in gen hardware are handled one simd4 vector at a time,
504 * with 1 cycle per vector dispatched. Thus SIMD8 pixel shaders take 2
505 * cycles to dispatch and SIMD16 (compressed) instructions take 4.
506 */
507 virtual int issue_time(backend_instruction *inst) = 0;
508
509 virtual void count_reads_remaining(backend_instruction *inst) = 0;
510 virtual void setup_liveness(cfg_t *cfg) = 0;
511 virtual void update_register_pressure(backend_instruction *inst) = 0;
512 virtual int get_register_pressure_benefit(backend_instruction *inst) = 0;
513
514 void schedule_instructions(bblock_t *block);
515
516 void *mem_ctx;
517
518 bool post_reg_alloc;
519 int instructions_to_schedule;
520 int grf_count;
521 unsigned hw_reg_count;
522 int reg_pressure;
523 int block_idx;
524 exec_list instructions;
525 backend_shader *bs;
526
527 instruction_scheduler_mode mode;
528
529 /*
530 * The register pressure at the beginning of each basic block.
531 */
532
533 int *reg_pressure_in;
534
535 /*
536 * The virtual GRF's whose range overlaps the beginning of each basic block.
537 */
538
539 BITSET_WORD **livein;
540
541 /*
542 * The virtual GRF's whose range overlaps the end of each basic block.
543 */
544
545 BITSET_WORD **liveout;
546
547 /*
548 * The hardware GRF's whose range overlaps the end of each basic block.
549 */
550
551 BITSET_WORD **hw_liveout;
552
553 /*
554 * Whether we've scheduled a write for this virtual GRF yet.
555 */
556
557 bool *written;
558
559 /*
560 * How many reads we haven't scheduled for this virtual GRF yet.
561 */
562
563 int *reads_remaining;
564
565 /*
566 * How many reads we haven't scheduled for this hardware GRF yet.
567 */
568
569 int *hw_reads_remaining;
570 };
571
572 class fs_instruction_scheduler : public instruction_scheduler
573 {
574 public:
575 fs_instruction_scheduler(fs_visitor *v, int grf_count, int hw_reg_count,
576 int block_count,
577 instruction_scheduler_mode mode);
578 void calculate_deps();
579 bool is_compressed(fs_inst *inst);
580 schedule_node *choose_instruction_to_schedule();
581 int issue_time(backend_instruction *inst);
582 fs_visitor *v;
583
584 void count_reads_remaining(backend_instruction *inst);
585 void setup_liveness(cfg_t *cfg);
586 void update_register_pressure(backend_instruction *inst);
587 int get_register_pressure_benefit(backend_instruction *inst);
588 };
589
590 fs_instruction_scheduler::fs_instruction_scheduler(fs_visitor *v,
591 int grf_count, int hw_reg_count,
592 int block_count,
593 instruction_scheduler_mode mode)
594 : instruction_scheduler(v, grf_count, hw_reg_count, block_count, mode),
595 v(v)
596 {
597 }
598
599 static bool
600 is_src_duplicate(fs_inst *inst, int src)
601 {
602 for (int i = 0; i < src; i++)
603 if (inst->src[i].equals(inst->src[src]))
604 return true;
605
606 return false;
607 }
608
609 void
610 fs_instruction_scheduler::count_reads_remaining(backend_instruction *be)
611 {
612 fs_inst *inst = (fs_inst *)be;
613
614 if (!reads_remaining)
615 return;
616
617 for (int i = 0; i < inst->sources; i++) {
618 if (is_src_duplicate(inst, i))
619 continue;
620
621 if (inst->src[i].file == VGRF) {
622 reads_remaining[inst->src[i].nr]++;
623 } else if (inst->src[i].file == FIXED_GRF) {
624 if (inst->src[i].nr >= hw_reg_count)
625 continue;
626
627 for (unsigned j = 0; j < regs_read(inst, i); j++)
628 hw_reads_remaining[inst->src[i].nr + j]++;
629 }
630 }
631 }
632
633 void
634 fs_instruction_scheduler::setup_liveness(cfg_t *cfg)
635 {
636 /* First, compute liveness on a per-GRF level using the in/out sets from
637 * liveness calculation.
638 */
639 for (int block = 0; block < cfg->num_blocks; block++) {
640 for (int i = 0; i < v->live_intervals->num_vars; i++) {
641 if (BITSET_TEST(v->live_intervals->block_data[block].livein, i)) {
642 int vgrf = v->live_intervals->vgrf_from_var[i];
643 if (!BITSET_TEST(livein[block], vgrf)) {
644 reg_pressure_in[block] += v->alloc.sizes[vgrf];
645 BITSET_SET(livein[block], vgrf);
646 }
647 }
648
649 if (BITSET_TEST(v->live_intervals->block_data[block].liveout, i))
650 BITSET_SET(liveout[block], v->live_intervals->vgrf_from_var[i]);
651 }
652 }
653
654 /* Now, extend the live in/live out sets for when a range crosses a block
655 * boundary, which matches what our register allocator/interference code
656 * does to account for force_writemask_all and incompatible exec_mask's.
657 */
658 for (int block = 0; block < cfg->num_blocks - 1; block++) {
659 for (int i = 0; i < grf_count; i++) {
660 if (v->virtual_grf_start[i] <= cfg->blocks[block]->end_ip &&
661 v->virtual_grf_end[i] >= cfg->blocks[block + 1]->start_ip) {
662 if (!BITSET_TEST(livein[block + 1], i)) {
663 reg_pressure_in[block + 1] += v->alloc.sizes[i];
664 BITSET_SET(livein[block + 1], i);
665 }
666
667 BITSET_SET(liveout[block], i);
668 }
669 }
670 }
671
672 int payload_last_use_ip[hw_reg_count];
673 v->calculate_payload_ranges(hw_reg_count, payload_last_use_ip);
674
675 for (unsigned i = 0; i < hw_reg_count; i++) {
676 if (payload_last_use_ip[i] == -1)
677 continue;
678
679 for (int block = 0; block < cfg->num_blocks; block++) {
680 if (cfg->blocks[block]->start_ip <= payload_last_use_ip[i])
681 reg_pressure_in[block]++;
682
683 if (cfg->blocks[block]->end_ip <= payload_last_use_ip[i])
684 BITSET_SET(hw_liveout[block], i);
685 }
686 }
687 }
688
689 void
690 fs_instruction_scheduler::update_register_pressure(backend_instruction *be)
691 {
692 fs_inst *inst = (fs_inst *)be;
693
694 if (!reads_remaining)
695 return;
696
697 if (inst->dst.file == VGRF) {
698 written[inst->dst.nr] = true;
699 }
700
701 for (int i = 0; i < inst->sources; i++) {
702 if (is_src_duplicate(inst, i))
703 continue;
704
705 if (inst->src[i].file == VGRF) {
706 reads_remaining[inst->src[i].nr]--;
707 } else if (inst->src[i].file == FIXED_GRF &&
708 inst->src[i].nr < hw_reg_count) {
709 for (unsigned off = 0; off < regs_read(inst, i); off++)
710 hw_reads_remaining[inst->src[i].nr + off]--;
711 }
712 }
713 }
714
715 int
716 fs_instruction_scheduler::get_register_pressure_benefit(backend_instruction *be)
717 {
718 fs_inst *inst = (fs_inst *)be;
719 int benefit = 0;
720
721 if (inst->dst.file == VGRF) {
722 if (!BITSET_TEST(livein[block_idx], inst->dst.nr) &&
723 !written[inst->dst.nr])
724 benefit -= v->alloc.sizes[inst->dst.nr];
725 }
726
727 for (int i = 0; i < inst->sources; i++) {
728 if (is_src_duplicate(inst, i))
729 continue;
730
731 if (inst->src[i].file == VGRF &&
732 !BITSET_TEST(liveout[block_idx], inst->src[i].nr) &&
733 reads_remaining[inst->src[i].nr] == 1)
734 benefit += v->alloc.sizes[inst->src[i].nr];
735
736 if (inst->src[i].file == FIXED_GRF &&
737 inst->src[i].nr < hw_reg_count) {
738 for (unsigned off = 0; off < regs_read(inst, i); off++) {
739 int reg = inst->src[i].nr + off;
740 if (!BITSET_TEST(hw_liveout[block_idx], reg) &&
741 hw_reads_remaining[reg] == 1) {
742 benefit++;
743 }
744 }
745 }
746 }
747
748 return benefit;
749 }
750
751 class vec4_instruction_scheduler : public instruction_scheduler
752 {
753 public:
754 vec4_instruction_scheduler(vec4_visitor *v, int grf_count);
755 void calculate_deps();
756 schedule_node *choose_instruction_to_schedule();
757 int issue_time(backend_instruction *inst);
758 vec4_visitor *v;
759
760 void count_reads_remaining(backend_instruction *inst);
761 void setup_liveness(cfg_t *cfg);
762 void update_register_pressure(backend_instruction *inst);
763 int get_register_pressure_benefit(backend_instruction *inst);
764 };
765
766 vec4_instruction_scheduler::vec4_instruction_scheduler(vec4_visitor *v,
767 int grf_count)
768 : instruction_scheduler(v, grf_count, 0, 0, SCHEDULE_POST),
769 v(v)
770 {
771 }
772
773 void
774 vec4_instruction_scheduler::count_reads_remaining(backend_instruction *)
775 {
776 }
777
778 void
779 vec4_instruction_scheduler::setup_liveness(cfg_t *)
780 {
781 }
782
783 void
784 vec4_instruction_scheduler::update_register_pressure(backend_instruction *)
785 {
786 }
787
788 int
789 vec4_instruction_scheduler::get_register_pressure_benefit(backend_instruction *)
790 {
791 return 0;
792 }
793
794 schedule_node::schedule_node(backend_instruction *inst,
795 instruction_scheduler *sched)
796 {
797 const struct gen_device_info *devinfo = sched->bs->devinfo;
798
799 this->inst = inst;
800 this->child_array_size = 0;
801 this->children = NULL;
802 this->child_latency = NULL;
803 this->child_count = 0;
804 this->parent_count = 0;
805 this->unblocked_time = 0;
806 this->cand_generation = 0;
807 this->delay = 0;
808 this->exit = NULL;
809
810 /* We can't measure Gen6 timings directly but expect them to be much
811 * closer to Gen7 than Gen4.
812 */
813 if (!sched->post_reg_alloc)
814 this->latency = 1;
815 else if (devinfo->gen >= 6)
816 set_latency_gen7(devinfo->is_haswell);
817 else
818 set_latency_gen4();
819 }
820
821 void
822 instruction_scheduler::add_insts_from_block(bblock_t *block)
823 {
824 foreach_inst_in_block(backend_instruction, inst, block) {
825 schedule_node *n = new(mem_ctx) schedule_node(inst, this);
826
827 instructions.push_tail(n);
828 }
829
830 this->instructions_to_schedule = block->end_ip - block->start_ip + 1;
831 }
832
833 /** Computation of the delay member of each node. */
834 void
835 instruction_scheduler::compute_delays()
836 {
837 foreach_in_list_reverse(schedule_node, n, &instructions) {
838 if (!n->child_count) {
839 n->delay = issue_time(n->inst);
840 } else {
841 for (int i = 0; i < n->child_count; i++) {
842 assert(n->children[i]->delay);
843 n->delay = MAX2(n->delay, n->latency + n->children[i]->delay);
844 }
845 }
846 }
847 }
848
849 void
850 instruction_scheduler::compute_exits()
851 {
852 /* Calculate a lower bound of the scheduling time of each node in the
853 * graph. This is analogous to the node's critical path but calculated
854 * from the top instead of from the bottom of the block.
855 */
856 foreach_in_list(schedule_node, n, &instructions) {
857 for (int i = 0; i < n->child_count; i++) {
858 n->children[i]->unblocked_time =
859 MAX2(n->children[i]->unblocked_time,
860 n->unblocked_time + issue_time(n->inst) + n->child_latency[i]);
861 }
862 }
863
864 /* Calculate the exit of each node by induction based on the exit nodes of
865 * its children. The preferred exit of a node is the one among the exit
866 * nodes of its children which can be unblocked first according to the
867 * optimistic unblocked time estimate calculated above.
868 */
869 foreach_in_list_reverse(schedule_node, n, &instructions) {
870 n->exit = (n->inst->opcode == FS_OPCODE_DISCARD_JUMP ? n : NULL);
871
872 for (int i = 0; i < n->child_count; i++) {
873 if (exit_unblocked_time(n->children[i]) < exit_unblocked_time(n))
874 n->exit = n->children[i]->exit;
875 }
876 }
877 }
878
879 /**
880 * Add a dependency between two instruction nodes.
881 *
882 * The @after node will be scheduled after @before. We will try to
883 * schedule it @latency cycles after @before, but no guarantees there.
884 */
885 void
886 instruction_scheduler::add_dep(schedule_node *before, schedule_node *after,
887 int latency)
888 {
889 if (!before || !after)
890 return;
891
892 assert(before != after);
893
894 for (int i = 0; i < before->child_count; i++) {
895 if (before->children[i] == after) {
896 before->child_latency[i] = MAX2(before->child_latency[i], latency);
897 return;
898 }
899 }
900
901 if (before->child_array_size <= before->child_count) {
902 if (before->child_array_size < 16)
903 before->child_array_size = 16;
904 else
905 before->child_array_size *= 2;
906
907 before->children = reralloc(mem_ctx, before->children,
908 schedule_node *,
909 before->child_array_size);
910 before->child_latency = reralloc(mem_ctx, before->child_latency,
911 int, before->child_array_size);
912 }
913
914 before->children[before->child_count] = after;
915 before->child_latency[before->child_count] = latency;
916 before->child_count++;
917 after->parent_count++;
918 }
919
920 void
921 instruction_scheduler::add_dep(schedule_node *before, schedule_node *after)
922 {
923 if (!before)
924 return;
925
926 add_dep(before, after, before->latency);
927 }
928
929 static bool
930 is_scheduling_barrier(const backend_instruction *inst)
931 {
932 return inst->opcode == FS_OPCODE_PLACEHOLDER_HALT ||
933 inst->is_control_flow() ||
934 inst->has_side_effects();
935 }
936
937 /**
938 * Sometimes we really want this node to execute after everything that
939 * was before it and before everything that followed it. This adds
940 * the deps to do so.
941 */
942 void
943 instruction_scheduler::add_barrier_deps(schedule_node *n)
944 {
945 schedule_node *prev = (schedule_node *)n->prev;
946 schedule_node *next = (schedule_node *)n->next;
947
948 if (prev) {
949 while (!prev->is_head_sentinel()) {
950 add_dep(prev, n, 0);
951 if (is_scheduling_barrier(prev->inst))
952 break;
953 prev = (schedule_node *)prev->prev;
954 }
955 }
956
957 if (next) {
958 while (!next->is_tail_sentinel()) {
959 add_dep(n, next, 0);
960 if (is_scheduling_barrier(next->inst))
961 break;
962 next = (schedule_node *)next->next;
963 }
964 }
965 }
966
967 /* instruction scheduling needs to be aware of when an MRF write
968 * actually writes 2 MRFs.
969 */
970 bool
971 fs_instruction_scheduler::is_compressed(fs_inst *inst)
972 {
973 return inst->exec_size == 16;
974 }
975
976 void
977 fs_instruction_scheduler::calculate_deps()
978 {
979 /* Pre-register-allocation, this tracks the last write per VGRF offset.
980 * After register allocation, reg_offsets are gone and we track individual
981 * GRF registers.
982 */
983 schedule_node **last_grf_write;
984 schedule_node *last_mrf_write[BRW_MAX_MRF(v->devinfo->gen)];
985 schedule_node *last_conditional_mod[8] = {};
986 schedule_node *last_accumulator_write = NULL;
987 /* Fixed HW registers are assumed to be separate from the virtual
988 * GRFs, so they can be tracked separately. We don't really write
989 * to fixed GRFs much, so don't bother tracking them on a more
990 * granular level.
991 */
992 schedule_node *last_fixed_grf_write = NULL;
993
994 last_grf_write = (schedule_node **)calloc(sizeof(schedule_node *), grf_count * 16);
995 memset(last_mrf_write, 0, sizeof(last_mrf_write));
996
997 /* top-to-bottom dependencies: RAW and WAW. */
998 foreach_in_list(schedule_node, n, &instructions) {
999 fs_inst *inst = (fs_inst *)n->inst;
1000
1001 if (is_scheduling_barrier(inst))
1002 add_barrier_deps(n);
1003
1004 /* read-after-write deps. */
1005 for (int i = 0; i < inst->sources; i++) {
1006 if (inst->src[i].file == VGRF) {
1007 if (post_reg_alloc) {
1008 for (unsigned r = 0; r < regs_read(inst, i); r++)
1009 add_dep(last_grf_write[inst->src[i].nr + r], n);
1010 } else {
1011 for (unsigned r = 0; r < regs_read(inst, i); r++) {
1012 add_dep(last_grf_write[inst->src[i].nr * 16 +
1013 inst->src[i].offset / REG_SIZE + r], n);
1014 }
1015 }
1016 } else if (inst->src[i].file == FIXED_GRF) {
1017 if (post_reg_alloc) {
1018 for (unsigned r = 0; r < regs_read(inst, i); r++)
1019 add_dep(last_grf_write[inst->src[i].nr + r], n);
1020 } else {
1021 add_dep(last_fixed_grf_write, n);
1022 }
1023 } else if (inst->src[i].is_accumulator()) {
1024 add_dep(last_accumulator_write, n);
1025 } else if (inst->src[i].file == ARF) {
1026 add_barrier_deps(n);
1027 }
1028 }
1029
1030 if (inst->base_mrf != -1) {
1031 for (int i = 0; i < inst->mlen; i++) {
1032 /* It looks like the MRF regs are released in the send
1033 * instruction once it's sent, not when the result comes
1034 * back.
1035 */
1036 add_dep(last_mrf_write[inst->base_mrf + i], n);
1037 }
1038 }
1039
1040 if (const unsigned mask = inst->flags_read(v->devinfo)) {
1041 assert(mask < (1 << ARRAY_SIZE(last_conditional_mod)));
1042
1043 for (unsigned i = 0; i < ARRAY_SIZE(last_conditional_mod); i++) {
1044 if (mask & (1 << i))
1045 add_dep(last_conditional_mod[i], n);
1046 }
1047 }
1048
1049 if (inst->reads_accumulator_implicitly()) {
1050 add_dep(last_accumulator_write, n);
1051 }
1052
1053 /* write-after-write deps. */
1054 if (inst->dst.file == VGRF) {
1055 if (post_reg_alloc) {
1056 for (unsigned r = 0; r < regs_written(inst); r++) {
1057 add_dep(last_grf_write[inst->dst.nr + r], n);
1058 last_grf_write[inst->dst.nr + r] = n;
1059 }
1060 } else {
1061 for (unsigned r = 0; r < regs_written(inst); r++) {
1062 add_dep(last_grf_write[inst->dst.nr * 16 +
1063 inst->dst.offset / REG_SIZE + r], n);
1064 last_grf_write[inst->dst.nr * 16 +
1065 inst->dst.offset / REG_SIZE + r] = n;
1066 }
1067 }
1068 } else if (inst->dst.file == MRF) {
1069 int reg = inst->dst.nr & ~BRW_MRF_COMPR4;
1070
1071 add_dep(last_mrf_write[reg], n);
1072 last_mrf_write[reg] = n;
1073 if (is_compressed(inst)) {
1074 if (inst->dst.nr & BRW_MRF_COMPR4)
1075 reg += 4;
1076 else
1077 reg++;
1078 add_dep(last_mrf_write[reg], n);
1079 last_mrf_write[reg] = n;
1080 }
1081 } else if (inst->dst.file == FIXED_GRF) {
1082 if (post_reg_alloc) {
1083 for (unsigned r = 0; r < regs_written(inst); r++)
1084 last_grf_write[inst->dst.nr + r] = n;
1085 } else {
1086 last_fixed_grf_write = n;
1087 }
1088 } else if (inst->dst.is_accumulator()) {
1089 add_dep(last_accumulator_write, n);
1090 last_accumulator_write = n;
1091 } else if (inst->dst.file == ARF && !inst->dst.is_null()) {
1092 add_barrier_deps(n);
1093 }
1094
1095 if (inst->mlen > 0 && inst->base_mrf != -1) {
1096 for (int i = 0; i < v->implied_mrf_writes(inst); i++) {
1097 add_dep(last_mrf_write[inst->base_mrf + i], n);
1098 last_mrf_write[inst->base_mrf + i] = n;
1099 }
1100 }
1101
1102 if (const unsigned mask = inst->flags_written()) {
1103 assert(mask < (1 << ARRAY_SIZE(last_conditional_mod)));
1104
1105 for (unsigned i = 0; i < ARRAY_SIZE(last_conditional_mod); i++) {
1106 if (mask & (1 << i)) {
1107 add_dep(last_conditional_mod[i], n, 0);
1108 last_conditional_mod[i] = n;
1109 }
1110 }
1111 }
1112
1113 if (inst->writes_accumulator_implicitly(v->devinfo) &&
1114 !inst->dst.is_accumulator()) {
1115 add_dep(last_accumulator_write, n);
1116 last_accumulator_write = n;
1117 }
1118 }
1119
1120 /* bottom-to-top dependencies: WAR */
1121 memset(last_grf_write, 0, sizeof(schedule_node *) * grf_count * 16);
1122 memset(last_mrf_write, 0, sizeof(last_mrf_write));
1123 memset(last_conditional_mod, 0, sizeof(last_conditional_mod));
1124 last_accumulator_write = NULL;
1125 last_fixed_grf_write = NULL;
1126
1127 foreach_in_list_reverse_safe(schedule_node, n, &instructions) {
1128 fs_inst *inst = (fs_inst *)n->inst;
1129
1130 /* write-after-read deps. */
1131 for (int i = 0; i < inst->sources; i++) {
1132 if (inst->src[i].file == VGRF) {
1133 if (post_reg_alloc) {
1134 for (unsigned r = 0; r < regs_read(inst, i); r++)
1135 add_dep(n, last_grf_write[inst->src[i].nr + r], 0);
1136 } else {
1137 for (unsigned r = 0; r < regs_read(inst, i); r++) {
1138 add_dep(n, last_grf_write[inst->src[i].nr * 16 +
1139 inst->src[i].offset / REG_SIZE + r], 0);
1140 }
1141 }
1142 } else if (inst->src[i].file == FIXED_GRF) {
1143 if (post_reg_alloc) {
1144 for (unsigned r = 0; r < regs_read(inst, i); r++)
1145 add_dep(n, last_grf_write[inst->src[i].nr + r], 0);
1146 } else {
1147 add_dep(n, last_fixed_grf_write, 0);
1148 }
1149 } else if (inst->src[i].is_accumulator()) {
1150 add_dep(n, last_accumulator_write, 0);
1151 } else if (inst->src[i].file == ARF) {
1152 add_barrier_deps(n);
1153 }
1154 }
1155
1156 if (inst->base_mrf != -1) {
1157 for (int i = 0; i < inst->mlen; i++) {
1158 /* It looks like the MRF regs are released in the send
1159 * instruction once it's sent, not when the result comes
1160 * back.
1161 */
1162 add_dep(n, last_mrf_write[inst->base_mrf + i], 2);
1163 }
1164 }
1165
1166 if (const unsigned mask = inst->flags_read(v->devinfo)) {
1167 assert(mask < (1 << ARRAY_SIZE(last_conditional_mod)));
1168
1169 for (unsigned i = 0; i < ARRAY_SIZE(last_conditional_mod); i++) {
1170 if (mask & (1 << i))
1171 add_dep(n, last_conditional_mod[i]);
1172 }
1173 }
1174
1175 if (inst->reads_accumulator_implicitly()) {
1176 add_dep(n, last_accumulator_write);
1177 }
1178
1179 /* Update the things this instruction wrote, so earlier reads
1180 * can mark this as WAR dependency.
1181 */
1182 if (inst->dst.file == VGRF) {
1183 if (post_reg_alloc) {
1184 for (unsigned r = 0; r < regs_written(inst); r++)
1185 last_grf_write[inst->dst.nr + r] = n;
1186 } else {
1187 for (unsigned r = 0; r < regs_written(inst); r++) {
1188 last_grf_write[inst->dst.nr * 16 +
1189 inst->dst.offset / REG_SIZE + r] = n;
1190 }
1191 }
1192 } else if (inst->dst.file == MRF) {
1193 int reg = inst->dst.nr & ~BRW_MRF_COMPR4;
1194
1195 last_mrf_write[reg] = n;
1196
1197 if (is_compressed(inst)) {
1198 if (inst->dst.nr & BRW_MRF_COMPR4)
1199 reg += 4;
1200 else
1201 reg++;
1202
1203 last_mrf_write[reg] = n;
1204 }
1205 } else if (inst->dst.file == FIXED_GRF) {
1206 if (post_reg_alloc) {
1207 for (unsigned r = 0; r < regs_written(inst); r++)
1208 last_grf_write[inst->dst.nr + r] = n;
1209 } else {
1210 last_fixed_grf_write = n;
1211 }
1212 } else if (inst->dst.is_accumulator()) {
1213 last_accumulator_write = n;
1214 } else if (inst->dst.file == ARF && !inst->dst.is_null()) {
1215 add_barrier_deps(n);
1216 }
1217
1218 if (inst->mlen > 0 && inst->base_mrf != -1) {
1219 for (int i = 0; i < v->implied_mrf_writes(inst); i++) {
1220 last_mrf_write[inst->base_mrf + i] = n;
1221 }
1222 }
1223
1224 if (const unsigned mask = inst->flags_written()) {
1225 assert(mask < (1 << ARRAY_SIZE(last_conditional_mod)));
1226
1227 for (unsigned i = 0; i < ARRAY_SIZE(last_conditional_mod); i++) {
1228 if (mask & (1 << i))
1229 last_conditional_mod[i] = n;
1230 }
1231 }
1232
1233 if (inst->writes_accumulator_implicitly(v->devinfo)) {
1234 last_accumulator_write = n;
1235 }
1236 }
1237
1238 free(last_grf_write);
1239 }
1240
1241 void
1242 vec4_instruction_scheduler::calculate_deps()
1243 {
1244 schedule_node *last_grf_write[grf_count];
1245 schedule_node *last_mrf_write[BRW_MAX_MRF(v->devinfo->gen)];
1246 schedule_node *last_conditional_mod = NULL;
1247 schedule_node *last_accumulator_write = NULL;
1248 /* Fixed HW registers are assumed to be separate from the virtual
1249 * GRFs, so they can be tracked separately. We don't really write
1250 * to fixed GRFs much, so don't bother tracking them on a more
1251 * granular level.
1252 */
1253 schedule_node *last_fixed_grf_write = NULL;
1254
1255 memset(last_grf_write, 0, sizeof(last_grf_write));
1256 memset(last_mrf_write, 0, sizeof(last_mrf_write));
1257
1258 /* top-to-bottom dependencies: RAW and WAW. */
1259 foreach_in_list(schedule_node, n, &instructions) {
1260 vec4_instruction *inst = (vec4_instruction *)n->inst;
1261
1262 if (is_scheduling_barrier(inst))
1263 add_barrier_deps(n);
1264
1265 /* read-after-write deps. */
1266 for (int i = 0; i < 3; i++) {
1267 if (inst->src[i].file == VGRF) {
1268 for (unsigned j = 0; j < regs_read(inst, i); ++j)
1269 add_dep(last_grf_write[inst->src[i].nr + j], n);
1270 } else if (inst->src[i].file == FIXED_GRF) {
1271 add_dep(last_fixed_grf_write, n);
1272 } else if (inst->src[i].is_accumulator()) {
1273 assert(last_accumulator_write);
1274 add_dep(last_accumulator_write, n);
1275 } else if (inst->src[i].file == ARF) {
1276 add_barrier_deps(n);
1277 }
1278 }
1279
1280 if (inst->reads_g0_implicitly())
1281 add_dep(last_fixed_grf_write, n);
1282
1283 if (!inst->is_send_from_grf()) {
1284 for (int i = 0; i < inst->mlen; i++) {
1285 /* It looks like the MRF regs are released in the send
1286 * instruction once it's sent, not when the result comes
1287 * back.
1288 */
1289 add_dep(last_mrf_write[inst->base_mrf + i], n);
1290 }
1291 }
1292
1293 if (inst->reads_flag()) {
1294 assert(last_conditional_mod);
1295 add_dep(last_conditional_mod, n);
1296 }
1297
1298 if (inst->reads_accumulator_implicitly()) {
1299 assert(last_accumulator_write);
1300 add_dep(last_accumulator_write, n);
1301 }
1302
1303 /* write-after-write deps. */
1304 if (inst->dst.file == VGRF) {
1305 for (unsigned j = 0; j < regs_written(inst); ++j) {
1306 add_dep(last_grf_write[inst->dst.nr + j], n);
1307 last_grf_write[inst->dst.nr + j] = n;
1308 }
1309 } else if (inst->dst.file == MRF) {
1310 add_dep(last_mrf_write[inst->dst.nr], n);
1311 last_mrf_write[inst->dst.nr] = n;
1312 } else if (inst->dst.file == FIXED_GRF) {
1313 last_fixed_grf_write = n;
1314 } else if (inst->dst.is_accumulator()) {
1315 add_dep(last_accumulator_write, n);
1316 last_accumulator_write = n;
1317 } else if (inst->dst.file == ARF && !inst->dst.is_null()) {
1318 add_barrier_deps(n);
1319 }
1320
1321 if (inst->mlen > 0 && !inst->is_send_from_grf()) {
1322 for (int i = 0; i < v->implied_mrf_writes(inst); i++) {
1323 add_dep(last_mrf_write[inst->base_mrf + i], n);
1324 last_mrf_write[inst->base_mrf + i] = n;
1325 }
1326 }
1327
1328 if (inst->writes_flag()) {
1329 add_dep(last_conditional_mod, n, 0);
1330 last_conditional_mod = n;
1331 }
1332
1333 if (inst->writes_accumulator_implicitly(v->devinfo) &&
1334 !inst->dst.is_accumulator()) {
1335 add_dep(last_accumulator_write, n);
1336 last_accumulator_write = n;
1337 }
1338 }
1339
1340 /* bottom-to-top dependencies: WAR */
1341 memset(last_grf_write, 0, sizeof(last_grf_write));
1342 memset(last_mrf_write, 0, sizeof(last_mrf_write));
1343 last_conditional_mod = NULL;
1344 last_accumulator_write = NULL;
1345 last_fixed_grf_write = NULL;
1346
1347 foreach_in_list_reverse_safe(schedule_node, n, &instructions) {
1348 vec4_instruction *inst = (vec4_instruction *)n->inst;
1349
1350 /* write-after-read deps. */
1351 for (int i = 0; i < 3; i++) {
1352 if (inst->src[i].file == VGRF) {
1353 for (unsigned j = 0; j < regs_read(inst, i); ++j)
1354 add_dep(n, last_grf_write[inst->src[i].nr + j]);
1355 } else if (inst->src[i].file == FIXED_GRF) {
1356 add_dep(n, last_fixed_grf_write);
1357 } else if (inst->src[i].is_accumulator()) {
1358 add_dep(n, last_accumulator_write);
1359 } else if (inst->src[i].file == ARF) {
1360 add_barrier_deps(n);
1361 }
1362 }
1363
1364 if (!inst->is_send_from_grf()) {
1365 for (int i = 0; i < inst->mlen; i++) {
1366 /* It looks like the MRF regs are released in the send
1367 * instruction once it's sent, not when the result comes
1368 * back.
1369 */
1370 add_dep(n, last_mrf_write[inst->base_mrf + i], 2);
1371 }
1372 }
1373
1374 if (inst->reads_flag()) {
1375 add_dep(n, last_conditional_mod);
1376 }
1377
1378 if (inst->reads_accumulator_implicitly()) {
1379 add_dep(n, last_accumulator_write);
1380 }
1381
1382 /* Update the things this instruction wrote, so earlier reads
1383 * can mark this as WAR dependency.
1384 */
1385 if (inst->dst.file == VGRF) {
1386 for (unsigned j = 0; j < regs_written(inst); ++j)
1387 last_grf_write[inst->dst.nr + j] = n;
1388 } else if (inst->dst.file == MRF) {
1389 last_mrf_write[inst->dst.nr] = n;
1390 } else if (inst->dst.file == FIXED_GRF) {
1391 last_fixed_grf_write = n;
1392 } else if (inst->dst.is_accumulator()) {
1393 last_accumulator_write = n;
1394 } else if (inst->dst.file == ARF && !inst->dst.is_null()) {
1395 add_barrier_deps(n);
1396 }
1397
1398 if (inst->mlen > 0 && !inst->is_send_from_grf()) {
1399 for (int i = 0; i < v->implied_mrf_writes(inst); i++) {
1400 last_mrf_write[inst->base_mrf + i] = n;
1401 }
1402 }
1403
1404 if (inst->writes_flag()) {
1405 last_conditional_mod = n;
1406 }
1407
1408 if (inst->writes_accumulator_implicitly(v->devinfo)) {
1409 last_accumulator_write = n;
1410 }
1411 }
1412 }
1413
1414 schedule_node *
1415 fs_instruction_scheduler::choose_instruction_to_schedule()
1416 {
1417 schedule_node *chosen = NULL;
1418
1419 if (mode == SCHEDULE_PRE || mode == SCHEDULE_POST) {
1420 int chosen_time = 0;
1421
1422 /* Of the instructions ready to execute or the closest to being ready,
1423 * choose the one most likely to unblock an early program exit, or
1424 * otherwise the oldest one.
1425 */
1426 foreach_in_list(schedule_node, n, &instructions) {
1427 if (!chosen ||
1428 exit_unblocked_time(n) < exit_unblocked_time(chosen) ||
1429 (exit_unblocked_time(n) == exit_unblocked_time(chosen) &&
1430 n->unblocked_time < chosen_time)) {
1431 chosen = n;
1432 chosen_time = n->unblocked_time;
1433 }
1434 }
1435 } else {
1436 /* Before register allocation, we don't care about the latencies of
1437 * instructions. All we care about is reducing live intervals of
1438 * variables so that we can avoid register spilling, or get SIMD16
1439 * shaders which naturally do a better job of hiding instruction
1440 * latency.
1441 */
1442 foreach_in_list(schedule_node, n, &instructions) {
1443 fs_inst *inst = (fs_inst *)n->inst;
1444
1445 if (!chosen) {
1446 chosen = n;
1447 continue;
1448 }
1449
1450 /* Most important: If we can definitely reduce register pressure, do
1451 * so immediately.
1452 */
1453 int register_pressure_benefit = get_register_pressure_benefit(n->inst);
1454 int chosen_register_pressure_benefit =
1455 get_register_pressure_benefit(chosen->inst);
1456
1457 if (register_pressure_benefit > 0 &&
1458 register_pressure_benefit > chosen_register_pressure_benefit) {
1459 chosen = n;
1460 continue;
1461 } else if (chosen_register_pressure_benefit > 0 &&
1462 (register_pressure_benefit <
1463 chosen_register_pressure_benefit)) {
1464 continue;
1465 }
1466
1467 if (mode == SCHEDULE_PRE_LIFO) {
1468 /* Prefer instructions that recently became available for
1469 * scheduling. These are the things that are most likely to
1470 * (eventually) make a variable dead and reduce register pressure.
1471 * Typical register pressure estimates don't work for us because
1472 * most of our pressure comes from texturing, where no single
1473 * instruction to schedule will make a vec4 value dead.
1474 */
1475 if (n->cand_generation > chosen->cand_generation) {
1476 chosen = n;
1477 continue;
1478 } else if (n->cand_generation < chosen->cand_generation) {
1479 continue;
1480 }
1481
1482 /* On MRF-using chips, prefer non-SEND instructions. If we don't
1483 * do this, then because we prefer instructions that just became
1484 * candidates, we'll end up in a pattern of scheduling a SEND,
1485 * then the MRFs for the next SEND, then the next SEND, then the
1486 * MRFs, etc., without ever consuming the results of a send.
1487 */
1488 if (v->devinfo->gen < 7) {
1489 fs_inst *chosen_inst = (fs_inst *)chosen->inst;
1490
1491 /* We use size_written > 4 * exec_size as our test for the kind
1492 * of send instruction to avoid -- only sends generate many
1493 * regs, and a single-result send is probably actually reducing
1494 * register pressure.
1495 */
1496 if (inst->size_written <= 4 * inst->exec_size &&
1497 chosen_inst->size_written > 4 * chosen_inst->exec_size) {
1498 chosen = n;
1499 continue;
1500 } else if (inst->size_written > chosen_inst->size_written) {
1501 continue;
1502 }
1503 }
1504 }
1505
1506 /* For instructions pushed on the cands list at the same time, prefer
1507 * the one with the highest delay to the end of the program. This is
1508 * most likely to have its values able to be consumed first (such as
1509 * for a large tree of lowered ubo loads, which appear reversed in
1510 * the instruction stream with respect to when they can be consumed).
1511 */
1512 if (n->delay > chosen->delay) {
1513 chosen = n;
1514 continue;
1515 } else if (n->delay < chosen->delay) {
1516 continue;
1517 }
1518
1519 /* Prefer the node most likely to unblock an early program exit.
1520 */
1521 if (exit_unblocked_time(n) < exit_unblocked_time(chosen)) {
1522 chosen = n;
1523 continue;
1524 } else if (exit_unblocked_time(n) > exit_unblocked_time(chosen)) {
1525 continue;
1526 }
1527
1528 /* If all other metrics are equal, we prefer the first instruction in
1529 * the list (program execution).
1530 */
1531 }
1532 }
1533
1534 return chosen;
1535 }
1536
1537 schedule_node *
1538 vec4_instruction_scheduler::choose_instruction_to_schedule()
1539 {
1540 schedule_node *chosen = NULL;
1541 int chosen_time = 0;
1542
1543 /* Of the instructions ready to execute or the closest to being ready,
1544 * choose the oldest one.
1545 */
1546 foreach_in_list(schedule_node, n, &instructions) {
1547 if (!chosen || n->unblocked_time < chosen_time) {
1548 chosen = n;
1549 chosen_time = n->unblocked_time;
1550 }
1551 }
1552
1553 return chosen;
1554 }
1555
1556 int
1557 fs_instruction_scheduler::issue_time(backend_instruction *inst)
1558 {
1559 const unsigned overhead = v->bank_conflict_cycles((fs_inst *)inst);
1560 if (is_compressed((fs_inst *)inst))
1561 return 4 + overhead;
1562 else
1563 return 2 + overhead;
1564 }
1565
1566 int
1567 vec4_instruction_scheduler::issue_time(backend_instruction *)
1568 {
1569 /* We always execute as two vec4s in parallel. */
1570 return 2;
1571 }
1572
1573 void
1574 instruction_scheduler::schedule_instructions(bblock_t *block)
1575 {
1576 const struct gen_device_info *devinfo = bs->devinfo;
1577 int time = 0;
1578 if (!post_reg_alloc)
1579 reg_pressure = reg_pressure_in[block->num];
1580 block_idx = block->num;
1581
1582 /* Remove non-DAG heads from the list. */
1583 foreach_in_list_safe(schedule_node, n, &instructions) {
1584 if (n->parent_count != 0)
1585 n->remove();
1586 }
1587
1588 unsigned cand_generation = 1;
1589 while (!instructions.is_empty()) {
1590 schedule_node *chosen = choose_instruction_to_schedule();
1591
1592 /* Schedule this instruction. */
1593 assert(chosen);
1594 chosen->remove();
1595 chosen->inst->exec_node::remove();
1596 block->instructions.push_tail(chosen->inst);
1597 instructions_to_schedule--;
1598
1599 if (!post_reg_alloc) {
1600 reg_pressure -= get_register_pressure_benefit(chosen->inst);
1601 update_register_pressure(chosen->inst);
1602 }
1603
1604 /* If we expected a delay for scheduling, then bump the clock to reflect
1605 * that. In reality, the hardware will switch to another hyperthread
1606 * and may not return to dispatching our thread for a while even after
1607 * we're unblocked. After this, we have the time when the chosen
1608 * instruction will start executing.
1609 */
1610 time = MAX2(time, chosen->unblocked_time);
1611
1612 /* Update the clock for how soon an instruction could start after the
1613 * chosen one.
1614 */
1615 time += issue_time(chosen->inst);
1616
1617 if (debug) {
1618 fprintf(stderr, "clock %4d, scheduled: ", time);
1619 bs->dump_instruction(chosen->inst);
1620 if (!post_reg_alloc)
1621 fprintf(stderr, "(register pressure %d)\n", reg_pressure);
1622 }
1623
1624 /* Now that we've scheduled a new instruction, some of its
1625 * children can be promoted to the list of instructions ready to
1626 * be scheduled. Update the children's unblocked time for this
1627 * DAG edge as we do so.
1628 */
1629 for (int i = chosen->child_count - 1; i >= 0; i--) {
1630 schedule_node *child = chosen->children[i];
1631
1632 child->unblocked_time = MAX2(child->unblocked_time,
1633 time + chosen->child_latency[i]);
1634
1635 if (debug) {
1636 fprintf(stderr, "\tchild %d, %d parents: ", i, child->parent_count);
1637 bs->dump_instruction(child->inst);
1638 }
1639
1640 child->cand_generation = cand_generation;
1641 child->parent_count--;
1642 if (child->parent_count == 0) {
1643 if (debug) {
1644 fprintf(stderr, "\t\tnow available\n");
1645 }
1646 instructions.push_head(child);
1647 }
1648 }
1649 cand_generation++;
1650
1651 /* Shared resource: the mathbox. There's one mathbox per EU on Gen6+
1652 * but it's more limited pre-gen6, so if we send something off to it then
1653 * the next math instruction isn't going to make progress until the first
1654 * is done.
1655 */
1656 if (devinfo->gen < 6 && chosen->inst->is_math()) {
1657 foreach_in_list(schedule_node, n, &instructions) {
1658 if (n->inst->is_math())
1659 n->unblocked_time = MAX2(n->unblocked_time,
1660 time + chosen->latency);
1661 }
1662 }
1663 }
1664
1665 assert(instructions_to_schedule == 0);
1666
1667 block->cycle_count = time;
1668 }
1669
1670 static unsigned get_cycle_count(cfg_t *cfg)
1671 {
1672 unsigned count = 0, multiplier = 1;
1673 foreach_block(block, cfg) {
1674 if (block->start()->opcode == BRW_OPCODE_DO)
1675 multiplier *= 10; /* assume that loops execute ~10 times */
1676
1677 count += block->cycle_count * multiplier;
1678
1679 if (block->end()->opcode == BRW_OPCODE_WHILE)
1680 multiplier /= 10;
1681 }
1682
1683 return count;
1684 }
1685
1686 void
1687 instruction_scheduler::run(cfg_t *cfg)
1688 {
1689 if (debug && !post_reg_alloc) {
1690 fprintf(stderr, "\nInstructions before scheduling (reg_alloc %d)\n",
1691 post_reg_alloc);
1692 bs->dump_instructions();
1693 }
1694
1695 if (!post_reg_alloc)
1696 setup_liveness(cfg);
1697
1698 foreach_block(block, cfg) {
1699 if (reads_remaining) {
1700 memset(reads_remaining, 0,
1701 grf_count * sizeof(*reads_remaining));
1702 memset(hw_reads_remaining, 0,
1703 hw_reg_count * sizeof(*hw_reads_remaining));
1704 memset(written, 0, grf_count * sizeof(*written));
1705
1706 foreach_inst_in_block(fs_inst, inst, block)
1707 count_reads_remaining(inst);
1708 }
1709
1710 add_insts_from_block(block);
1711
1712 calculate_deps();
1713
1714 compute_delays();
1715 compute_exits();
1716
1717 schedule_instructions(block);
1718 }
1719
1720 if (debug && !post_reg_alloc) {
1721 fprintf(stderr, "\nInstructions after scheduling (reg_alloc %d)\n",
1722 post_reg_alloc);
1723 bs->dump_instructions();
1724 }
1725
1726 cfg->cycle_count = get_cycle_count(cfg);
1727 }
1728
1729 void
1730 fs_visitor::schedule_instructions(instruction_scheduler_mode mode)
1731 {
1732 if (mode != SCHEDULE_POST)
1733 calculate_live_intervals();
1734
1735 int grf_count;
1736 if (mode == SCHEDULE_POST)
1737 grf_count = grf_used;
1738 else
1739 grf_count = alloc.count;
1740
1741 fs_instruction_scheduler sched(this, grf_count, first_non_payload_grf,
1742 cfg->num_blocks, mode);
1743 sched.run(cfg);
1744
1745 invalidate_live_intervals();
1746 }
1747
1748 void
1749 vec4_visitor::opt_schedule_instructions()
1750 {
1751 vec4_instruction_scheduler sched(this, prog_data->total_grf);
1752 sched.run(cfg);
1753
1754 invalidate_live_intervals();
1755 }