i965: Refactor debug dumping of GLSL IR.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4.cpp
1 /*
2 * Copyright © 2011 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
24 #include "brw_vec4.h"
25 #include "brw_cfg.h"
26 #include "brw_vs.h"
27 #include "brw_dead_control_flow.h"
28
29 extern "C" {
30 #include "main/macros.h"
31 #include "main/shaderobj.h"
32 #include "program/prog_print.h"
33 #include "program/prog_parameter.h"
34 }
35
36 #define MAX_INSTRUCTION (1 << 30)
37
38 using namespace brw;
39
40 namespace brw {
41
42 /**
43 * Common helper for constructing swizzles. When only a subset of
44 * channels of a vec4 are used, we don't want to reference the other
45 * channels, as that will tell optimization passes that those other
46 * channels are used.
47 */
48 unsigned
49 swizzle_for_size(int size)
50 {
51 static const unsigned size_swizzles[4] = {
52 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
53 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
54 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
55 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
56 };
57
58 assert((size >= 1) && (size <= 4));
59 return size_swizzles[size - 1];
60 }
61
62 void
63 src_reg::init()
64 {
65 memset(this, 0, sizeof(*this));
66
67 this->file = BAD_FILE;
68 }
69
70 src_reg::src_reg(register_file file, int reg, const glsl_type *type)
71 {
72 init();
73
74 this->file = file;
75 this->reg = reg;
76 if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
77 this->swizzle = swizzle_for_size(type->vector_elements);
78 else
79 this->swizzle = BRW_SWIZZLE_XYZW;
80 }
81
82 /** Generic unset register constructor. */
83 src_reg::src_reg()
84 {
85 init();
86 }
87
88 src_reg::src_reg(float f)
89 {
90 init();
91
92 this->file = IMM;
93 this->type = BRW_REGISTER_TYPE_F;
94 this->imm.f = f;
95 }
96
97 src_reg::src_reg(uint32_t u)
98 {
99 init();
100
101 this->file = IMM;
102 this->type = BRW_REGISTER_TYPE_UD;
103 this->imm.u = u;
104 }
105
106 src_reg::src_reg(int32_t i)
107 {
108 init();
109
110 this->file = IMM;
111 this->type = BRW_REGISTER_TYPE_D;
112 this->imm.i = i;
113 }
114
115 src_reg::src_reg(struct brw_reg reg)
116 {
117 init();
118
119 this->file = HW_REG;
120 this->fixed_hw_reg = reg;
121 this->type = reg.type;
122 }
123
124 src_reg::src_reg(dst_reg reg)
125 {
126 init();
127
128 this->file = reg.file;
129 this->reg = reg.reg;
130 this->reg_offset = reg.reg_offset;
131 this->type = reg.type;
132 this->reladdr = reg.reladdr;
133 this->fixed_hw_reg = reg.fixed_hw_reg;
134
135 int swizzles[4];
136 int next_chan = 0;
137 int last = 0;
138
139 for (int i = 0; i < 4; i++) {
140 if (!(reg.writemask & (1 << i)))
141 continue;
142
143 swizzles[next_chan++] = last = i;
144 }
145
146 for (; next_chan < 4; next_chan++) {
147 swizzles[next_chan] = last;
148 }
149
150 this->swizzle = BRW_SWIZZLE4(swizzles[0], swizzles[1],
151 swizzles[2], swizzles[3]);
152 }
153
154 void
155 dst_reg::init()
156 {
157 memset(this, 0, sizeof(*this));
158 this->file = BAD_FILE;
159 this->writemask = WRITEMASK_XYZW;
160 }
161
162 dst_reg::dst_reg()
163 {
164 init();
165 }
166
167 dst_reg::dst_reg(register_file file, int reg)
168 {
169 init();
170
171 this->file = file;
172 this->reg = reg;
173 }
174
175 dst_reg::dst_reg(register_file file, int reg, const glsl_type *type,
176 int writemask)
177 {
178 init();
179
180 this->file = file;
181 this->reg = reg;
182 this->type = brw_type_for_base_type(type);
183 this->writemask = writemask;
184 }
185
186 dst_reg::dst_reg(struct brw_reg reg)
187 {
188 init();
189
190 this->file = HW_REG;
191 this->fixed_hw_reg = reg;
192 this->type = reg.type;
193 }
194
195 dst_reg::dst_reg(src_reg reg)
196 {
197 init();
198
199 this->file = reg.file;
200 this->reg = reg.reg;
201 this->reg_offset = reg.reg_offset;
202 this->type = reg.type;
203 /* How should we do writemasking when converting from a src_reg? It seems
204 * pretty obvious that for src.xxxx the caller wants to write to src.x, but
205 * what about for src.wx? Just special-case src.xxxx for now.
206 */
207 if (reg.swizzle == BRW_SWIZZLE_XXXX)
208 this->writemask = WRITEMASK_X;
209 else
210 this->writemask = WRITEMASK_XYZW;
211 this->reladdr = reg.reladdr;
212 this->fixed_hw_reg = reg.fixed_hw_reg;
213 }
214
215 bool
216 vec4_instruction::is_send_from_grf()
217 {
218 switch (opcode) {
219 case SHADER_OPCODE_SHADER_TIME_ADD:
220 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
221 return true;
222 default:
223 return false;
224 }
225 }
226
227 bool
228 vec4_visitor::can_do_source_mods(vec4_instruction *inst)
229 {
230 if (brw->gen == 6 && inst->is_math())
231 return false;
232
233 if (inst->is_send_from_grf())
234 return false;
235
236 if (!inst->can_do_source_mods())
237 return false;
238
239 return true;
240 }
241
242 /**
243 * Returns how many MRFs an opcode will write over.
244 *
245 * Note that this is not the 0 or 1 implied writes in an actual gen
246 * instruction -- the generate_* functions generate additional MOVs
247 * for setup.
248 */
249 int
250 vec4_visitor::implied_mrf_writes(vec4_instruction *inst)
251 {
252 if (inst->mlen == 0)
253 return 0;
254
255 switch (inst->opcode) {
256 case SHADER_OPCODE_RCP:
257 case SHADER_OPCODE_RSQ:
258 case SHADER_OPCODE_SQRT:
259 case SHADER_OPCODE_EXP2:
260 case SHADER_OPCODE_LOG2:
261 case SHADER_OPCODE_SIN:
262 case SHADER_OPCODE_COS:
263 return 1;
264 case SHADER_OPCODE_INT_QUOTIENT:
265 case SHADER_OPCODE_INT_REMAINDER:
266 case SHADER_OPCODE_POW:
267 return 2;
268 case VS_OPCODE_URB_WRITE:
269 return 1;
270 case VS_OPCODE_PULL_CONSTANT_LOAD:
271 return 2;
272 case SHADER_OPCODE_GEN4_SCRATCH_READ:
273 return 2;
274 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
275 return 3;
276 case GS_OPCODE_URB_WRITE:
277 case GS_OPCODE_THREAD_END:
278 return 0;
279 case SHADER_OPCODE_SHADER_TIME_ADD:
280 return 0;
281 case SHADER_OPCODE_TEX:
282 case SHADER_OPCODE_TXL:
283 case SHADER_OPCODE_TXD:
284 case SHADER_OPCODE_TXF:
285 case SHADER_OPCODE_TXF_CMS:
286 case SHADER_OPCODE_TXF_MCS:
287 case SHADER_OPCODE_TXS:
288 case SHADER_OPCODE_TG4:
289 case SHADER_OPCODE_TG4_OFFSET:
290 return inst->header_present ? 1 : 0;
291 case SHADER_OPCODE_UNTYPED_ATOMIC:
292 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
293 return 0;
294 default:
295 assert(!"not reached");
296 return inst->mlen;
297 }
298 }
299
300 bool
301 src_reg::equals(src_reg *r)
302 {
303 return (file == r->file &&
304 reg == r->reg &&
305 reg_offset == r->reg_offset &&
306 type == r->type &&
307 negate == r->negate &&
308 abs == r->abs &&
309 swizzle == r->swizzle &&
310 !reladdr && !r->reladdr &&
311 memcmp(&fixed_hw_reg, &r->fixed_hw_reg,
312 sizeof(fixed_hw_reg)) == 0 &&
313 imm.u == r->imm.u);
314 }
315
316 /**
317 * Must be called after calculate_live_intervales() to remove unused
318 * writes to registers -- register allocation will fail otherwise
319 * because something deffed but not used won't be considered to
320 * interfere with other regs.
321 */
322 bool
323 vec4_visitor::dead_code_eliminate()
324 {
325 bool progress = false;
326 int pc = 0;
327
328 calculate_live_intervals();
329
330 foreach_list_safe(node, &this->instructions) {
331 vec4_instruction *inst = (vec4_instruction *)node;
332
333 if (inst->dst.file == GRF && !inst->has_side_effects()) {
334 assert(this->virtual_grf_end[inst->dst.reg] >= pc);
335 if (this->virtual_grf_end[inst->dst.reg] == pc) {
336 /* Don't dead code eliminate instructions that write to the
337 * accumulator as a side-effect. Instead just set the destination
338 * to the null register to free it.
339 */
340 switch (inst->opcode) {
341 case BRW_OPCODE_ADDC:
342 case BRW_OPCODE_SUBB:
343 case BRW_OPCODE_MACH:
344 inst->dst = dst_reg(retype(brw_null_reg(), inst->dst.type));
345 break;
346 default:
347 inst->remove();
348 break;
349 }
350 progress = true;
351 }
352 }
353
354 pc++;
355 }
356
357 if (progress)
358 invalidate_live_intervals();
359
360 return progress;
361 }
362
363 void
364 vec4_visitor::split_uniform_registers()
365 {
366 /* Prior to this, uniforms have been in an array sized according to
367 * the number of vector uniforms present, sparsely filled (so an
368 * aggregate results in reg indices being skipped over). Now we're
369 * going to cut those aggregates up so each .reg index is one
370 * vector. The goal is to make elimination of unused uniform
371 * components easier later.
372 */
373 foreach_list(node, &this->instructions) {
374 vec4_instruction *inst = (vec4_instruction *)node;
375
376 for (int i = 0 ; i < 3; i++) {
377 if (inst->src[i].file != UNIFORM)
378 continue;
379
380 assert(!inst->src[i].reladdr);
381
382 inst->src[i].reg += inst->src[i].reg_offset;
383 inst->src[i].reg_offset = 0;
384 }
385 }
386
387 /* Update that everything is now vector-sized. */
388 for (int i = 0; i < this->uniforms; i++) {
389 this->uniform_size[i] = 1;
390 }
391 }
392
393 void
394 vec4_visitor::pack_uniform_registers()
395 {
396 bool uniform_used[this->uniforms];
397 int new_loc[this->uniforms];
398 int new_chan[this->uniforms];
399
400 memset(uniform_used, 0, sizeof(uniform_used));
401 memset(new_loc, 0, sizeof(new_loc));
402 memset(new_chan, 0, sizeof(new_chan));
403
404 /* Find which uniform vectors are actually used by the program. We
405 * expect unused vector elements when we've moved array access out
406 * to pull constants, and from some GLSL code generators like wine.
407 */
408 foreach_list(node, &this->instructions) {
409 vec4_instruction *inst = (vec4_instruction *)node;
410
411 for (int i = 0 ; i < 3; i++) {
412 if (inst->src[i].file != UNIFORM)
413 continue;
414
415 uniform_used[inst->src[i].reg] = true;
416 }
417 }
418
419 int new_uniform_count = 0;
420
421 /* Now, figure out a packing of the live uniform vectors into our
422 * push constants.
423 */
424 for (int src = 0; src < uniforms; src++) {
425 int size = this->uniform_vector_size[src];
426
427 if (!uniform_used[src]) {
428 this->uniform_vector_size[src] = 0;
429 continue;
430 }
431
432 int dst;
433 /* Find the lowest place we can slot this uniform in. */
434 for (dst = 0; dst < src; dst++) {
435 if (this->uniform_vector_size[dst] + size <= 4)
436 break;
437 }
438
439 if (src == dst) {
440 new_loc[src] = dst;
441 new_chan[src] = 0;
442 } else {
443 new_loc[src] = dst;
444 new_chan[src] = this->uniform_vector_size[dst];
445
446 /* Move the references to the data */
447 for (int j = 0; j < size; j++) {
448 stage_prog_data->param[dst * 4 + new_chan[src] + j] =
449 stage_prog_data->param[src * 4 + j];
450 }
451
452 this->uniform_vector_size[dst] += size;
453 this->uniform_vector_size[src] = 0;
454 }
455
456 new_uniform_count = MAX2(new_uniform_count, dst + 1);
457 }
458
459 this->uniforms = new_uniform_count;
460
461 /* Now, update the instructions for our repacked uniforms. */
462 foreach_list(node, &this->instructions) {
463 vec4_instruction *inst = (vec4_instruction *)node;
464
465 for (int i = 0 ; i < 3; i++) {
466 int src = inst->src[i].reg;
467
468 if (inst->src[i].file != UNIFORM)
469 continue;
470
471 inst->src[i].reg = new_loc[src];
472
473 int sx = BRW_GET_SWZ(inst->src[i].swizzle, 0) + new_chan[src];
474 int sy = BRW_GET_SWZ(inst->src[i].swizzle, 1) + new_chan[src];
475 int sz = BRW_GET_SWZ(inst->src[i].swizzle, 2) + new_chan[src];
476 int sw = BRW_GET_SWZ(inst->src[i].swizzle, 3) + new_chan[src];
477 inst->src[i].swizzle = BRW_SWIZZLE4(sx, sy, sz, sw);
478 }
479 }
480 }
481
482 bool
483 src_reg::is_zero() const
484 {
485 if (file != IMM)
486 return false;
487
488 if (type == BRW_REGISTER_TYPE_F) {
489 return imm.f == 0.0;
490 } else {
491 return imm.i == 0;
492 }
493 }
494
495 bool
496 src_reg::is_one() const
497 {
498 if (file != IMM)
499 return false;
500
501 if (type == BRW_REGISTER_TYPE_F) {
502 return imm.f == 1.0;
503 } else {
504 return imm.i == 1;
505 }
506 }
507
508 /**
509 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
510 *
511 * While GLSL IR also performs this optimization, we end up with it in
512 * our instruction stream for a couple of reasons. One is that we
513 * sometimes generate silly instructions, for example in array access
514 * where we'll generate "ADD offset, index, base" even if base is 0.
515 * The other is that GLSL IR's constant propagation doesn't track the
516 * components of aggregates, so some VS patterns (initialize matrix to
517 * 0, accumulate in vertex blending factors) end up breaking down to
518 * instructions involving 0.
519 */
520 bool
521 vec4_visitor::opt_algebraic()
522 {
523 bool progress = false;
524
525 foreach_list(node, &this->instructions) {
526 vec4_instruction *inst = (vec4_instruction *)node;
527
528 switch (inst->opcode) {
529 case BRW_OPCODE_ADD:
530 if (inst->src[1].is_zero()) {
531 inst->opcode = BRW_OPCODE_MOV;
532 inst->src[1] = src_reg();
533 progress = true;
534 }
535 break;
536
537 case BRW_OPCODE_MUL:
538 if (inst->src[1].is_zero()) {
539 inst->opcode = BRW_OPCODE_MOV;
540 switch (inst->src[0].type) {
541 case BRW_REGISTER_TYPE_F:
542 inst->src[0] = src_reg(0.0f);
543 break;
544 case BRW_REGISTER_TYPE_D:
545 inst->src[0] = src_reg(0);
546 break;
547 case BRW_REGISTER_TYPE_UD:
548 inst->src[0] = src_reg(0u);
549 break;
550 default:
551 assert(!"not reached");
552 inst->src[0] = src_reg(0.0f);
553 break;
554 }
555 inst->src[1] = src_reg();
556 progress = true;
557 } else if (inst->src[1].is_one()) {
558 inst->opcode = BRW_OPCODE_MOV;
559 inst->src[1] = src_reg();
560 progress = true;
561 }
562 break;
563 default:
564 break;
565 }
566 }
567
568 if (progress)
569 invalidate_live_intervals();
570
571 return progress;
572 }
573
574 /**
575 * Only a limited number of hardware registers may be used for push
576 * constants, so this turns access to the overflowed constants into
577 * pull constants.
578 */
579 void
580 vec4_visitor::move_push_constants_to_pull_constants()
581 {
582 int pull_constant_loc[this->uniforms];
583
584 /* Only allow 32 registers (256 uniform components) as push constants,
585 * which is the limit on gen6.
586 */
587 int max_uniform_components = 32 * 8;
588 if (this->uniforms * 4 <= max_uniform_components)
589 return;
590
591 /* Make some sort of choice as to which uniforms get sent to pull
592 * constants. We could potentially do something clever here like
593 * look for the most infrequently used uniform vec4s, but leave
594 * that for later.
595 */
596 for (int i = 0; i < this->uniforms * 4; i += 4) {
597 pull_constant_loc[i / 4] = -1;
598
599 if (i >= max_uniform_components) {
600 const float **values = &stage_prog_data->param[i];
601
602 /* Try to find an existing copy of this uniform in the pull
603 * constants if it was part of an array access already.
604 */
605 for (unsigned int j = 0; j < stage_prog_data->nr_pull_params; j += 4) {
606 int matches;
607
608 for (matches = 0; matches < 4; matches++) {
609 if (stage_prog_data->pull_param[j + matches] != values[matches])
610 break;
611 }
612
613 if (matches == 4) {
614 pull_constant_loc[i / 4] = j / 4;
615 break;
616 }
617 }
618
619 if (pull_constant_loc[i / 4] == -1) {
620 assert(stage_prog_data->nr_pull_params % 4 == 0);
621 pull_constant_loc[i / 4] = stage_prog_data->nr_pull_params / 4;
622
623 for (int j = 0; j < 4; j++) {
624 stage_prog_data->pull_param[stage_prog_data->nr_pull_params++] =
625 values[j];
626 }
627 }
628 }
629 }
630
631 /* Now actually rewrite usage of the things we've moved to pull
632 * constants.
633 */
634 foreach_list_safe(node, &this->instructions) {
635 vec4_instruction *inst = (vec4_instruction *)node;
636
637 for (int i = 0 ; i < 3; i++) {
638 if (inst->src[i].file != UNIFORM ||
639 pull_constant_loc[inst->src[i].reg] == -1)
640 continue;
641
642 int uniform = inst->src[i].reg;
643
644 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
645
646 emit_pull_constant_load(inst, temp, inst->src[i],
647 pull_constant_loc[uniform]);
648
649 inst->src[i].file = temp.file;
650 inst->src[i].reg = temp.reg;
651 inst->src[i].reg_offset = temp.reg_offset;
652 inst->src[i].reladdr = NULL;
653 }
654 }
655
656 /* Repack push constants to remove the now-unused ones. */
657 pack_uniform_registers();
658 }
659
660 /**
661 * Sets the dependency control fields on instructions after register
662 * allocation and before the generator is run.
663 *
664 * When you have a sequence of instructions like:
665 *
666 * DP4 temp.x vertex uniform[0]
667 * DP4 temp.y vertex uniform[0]
668 * DP4 temp.z vertex uniform[0]
669 * DP4 temp.w vertex uniform[0]
670 *
671 * The hardware doesn't know that it can actually run the later instructions
672 * while the previous ones are in flight, producing stalls. However, we have
673 * manual fields we can set in the instructions that let it do so.
674 */
675 void
676 vec4_visitor::opt_set_dependency_control()
677 {
678 vec4_instruction *last_grf_write[BRW_MAX_GRF];
679 uint8_t grf_channels_written[BRW_MAX_GRF];
680 vec4_instruction *last_mrf_write[BRW_MAX_GRF];
681 uint8_t mrf_channels_written[BRW_MAX_GRF];
682
683 cfg_t cfg(&instructions);
684
685 assert(prog_data->total_grf ||
686 !"Must be called after register allocation");
687
688 for (int i = 0; i < cfg.num_blocks; i++) {
689 bblock_t *bblock = cfg.blocks[i];
690 vec4_instruction *inst;
691
692 memset(last_grf_write, 0, sizeof(last_grf_write));
693 memset(last_mrf_write, 0, sizeof(last_mrf_write));
694
695 for (inst = (vec4_instruction *)bblock->start;
696 inst != (vec4_instruction *)bblock->end->next;
697 inst = (vec4_instruction *)inst->next) {
698 /* If we read from a register that we were doing dependency control
699 * on, don't do dependency control across the read.
700 */
701 for (int i = 0; i < 3; i++) {
702 int reg = inst->src[i].reg + inst->src[i].reg_offset;
703 if (inst->src[i].file == GRF) {
704 last_grf_write[reg] = NULL;
705 } else if (inst->src[i].file == HW_REG) {
706 memset(last_grf_write, 0, sizeof(last_grf_write));
707 break;
708 }
709 assert(inst->src[i].file != MRF);
710 }
711
712 /* In the presence of send messages, totally interrupt dependency
713 * control. They're long enough that the chance of dependency
714 * control around them just doesn't matter.
715 */
716 if (inst->mlen) {
717 memset(last_grf_write, 0, sizeof(last_grf_write));
718 memset(last_mrf_write, 0, sizeof(last_mrf_write));
719 continue;
720 }
721
722 /* It looks like setting dependency control on a predicated
723 * instruction hangs the GPU.
724 */
725 if (inst->predicate) {
726 memset(last_grf_write, 0, sizeof(last_grf_write));
727 memset(last_mrf_write, 0, sizeof(last_mrf_write));
728 continue;
729 }
730
731 /* Now, see if we can do dependency control for this instruction
732 * against a previous one writing to its destination.
733 */
734 int reg = inst->dst.reg + inst->dst.reg_offset;
735 if (inst->dst.file == GRF) {
736 if (last_grf_write[reg] &&
737 !(inst->dst.writemask & grf_channels_written[reg])) {
738 last_grf_write[reg]->no_dd_clear = true;
739 inst->no_dd_check = true;
740 } else {
741 grf_channels_written[reg] = 0;
742 }
743
744 last_grf_write[reg] = inst;
745 grf_channels_written[reg] |= inst->dst.writemask;
746 } else if (inst->dst.file == MRF) {
747 if (last_mrf_write[reg] &&
748 !(inst->dst.writemask & mrf_channels_written[reg])) {
749 last_mrf_write[reg]->no_dd_clear = true;
750 inst->no_dd_check = true;
751 } else {
752 mrf_channels_written[reg] = 0;
753 }
754
755 last_mrf_write[reg] = inst;
756 mrf_channels_written[reg] |= inst->dst.writemask;
757 } else if (inst->dst.reg == HW_REG) {
758 if (inst->dst.fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE)
759 memset(last_grf_write, 0, sizeof(last_grf_write));
760 if (inst->dst.fixed_hw_reg.file == BRW_MESSAGE_REGISTER_FILE)
761 memset(last_mrf_write, 0, sizeof(last_mrf_write));
762 }
763 }
764 }
765 }
766
767 bool
768 vec4_instruction::can_reswizzle_dst(int dst_writemask,
769 int swizzle,
770 int swizzle_mask)
771 {
772 /* If this instruction sets anything not referenced by swizzle, then we'd
773 * totally break it when we reswizzle.
774 */
775 if (dst.writemask & ~swizzle_mask)
776 return false;
777
778 switch (opcode) {
779 case BRW_OPCODE_DP4:
780 case BRW_OPCODE_DP3:
781 case BRW_OPCODE_DP2:
782 return true;
783 default:
784 /* Check if there happens to be no reswizzling required. */
785 for (int c = 0; c < 4; c++) {
786 int bit = 1 << BRW_GET_SWZ(swizzle, c);
787 /* Skip components of the swizzle not used by the dst. */
788 if (!(dst_writemask & (1 << c)))
789 continue;
790
791 /* We don't do the reswizzling yet, so just sanity check that we
792 * don't have to.
793 */
794 if (bit != (1 << c))
795 return false;
796 }
797 return true;
798 }
799 }
800
801 /**
802 * For any channels in the swizzle's source that were populated by this
803 * instruction, rewrite the instruction to put the appropriate result directly
804 * in those channels.
805 *
806 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
807 */
808 void
809 vec4_instruction::reswizzle_dst(int dst_writemask, int swizzle)
810 {
811 int new_writemask = 0;
812
813 switch (opcode) {
814 case BRW_OPCODE_DP4:
815 case BRW_OPCODE_DP3:
816 case BRW_OPCODE_DP2:
817 for (int c = 0; c < 4; c++) {
818 int bit = 1 << BRW_GET_SWZ(swizzle, c);
819 /* Skip components of the swizzle not used by the dst. */
820 if (!(dst_writemask & (1 << c)))
821 continue;
822 /* If we were populating this component, then populate the
823 * corresponding channel of the new dst.
824 */
825 if (dst.writemask & bit)
826 new_writemask |= (1 << c);
827 }
828 dst.writemask = new_writemask;
829 break;
830 default:
831 for (int c = 0; c < 4; c++) {
832 /* Skip components of the swizzle not used by the dst. */
833 if (!(dst_writemask & (1 << c)))
834 continue;
835
836 /* We don't do the reswizzling yet, so just sanity check that we
837 * don't have to.
838 */
839 assert((1 << BRW_GET_SWZ(swizzle, c)) == (1 << c));
840 }
841 break;
842 }
843 }
844
845 /*
846 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
847 * just written and then MOVed into another reg and making the original write
848 * of the GRF write directly to the final destination instead.
849 */
850 bool
851 vec4_visitor::opt_register_coalesce()
852 {
853 bool progress = false;
854 int next_ip = 0;
855
856 calculate_live_intervals();
857
858 foreach_list_safe(node, &this->instructions) {
859 vec4_instruction *inst = (vec4_instruction *)node;
860
861 int ip = next_ip;
862 next_ip++;
863
864 if (inst->opcode != BRW_OPCODE_MOV ||
865 (inst->dst.file != GRF && inst->dst.file != MRF) ||
866 inst->predicate ||
867 inst->src[0].file != GRF ||
868 inst->dst.type != inst->src[0].type ||
869 inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
870 continue;
871
872 bool to_mrf = (inst->dst.file == MRF);
873
874 /* Can't coalesce this GRF if someone else was going to
875 * read it later.
876 */
877 if (this->virtual_grf_end[inst->src[0].reg] > ip)
878 continue;
879
880 /* We need to check interference with the final destination between this
881 * instruction and the earliest instruction involved in writing the GRF
882 * we're eliminating. To do that, keep track of which of our source
883 * channels we've seen initialized.
884 */
885 bool chans_needed[4] = {false, false, false, false};
886 int chans_remaining = 0;
887 int swizzle_mask = 0;
888 for (int i = 0; i < 4; i++) {
889 int chan = BRW_GET_SWZ(inst->src[0].swizzle, i);
890
891 if (!(inst->dst.writemask & (1 << i)))
892 continue;
893
894 swizzle_mask |= (1 << chan);
895
896 if (!chans_needed[chan]) {
897 chans_needed[chan] = true;
898 chans_remaining++;
899 }
900 }
901
902 /* Now walk up the instruction stream trying to see if we can rewrite
903 * everything writing to the temporary to write into the destination
904 * instead.
905 */
906 vec4_instruction *scan_inst;
907 for (scan_inst = (vec4_instruction *)inst->prev;
908 scan_inst->prev != NULL;
909 scan_inst = (vec4_instruction *)scan_inst->prev) {
910 if (scan_inst->dst.file == GRF &&
911 scan_inst->dst.reg == inst->src[0].reg &&
912 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
913 /* Found something writing to the reg we want to coalesce away. */
914 if (to_mrf) {
915 /* SEND instructions can't have MRF as a destination. */
916 if (scan_inst->mlen)
917 break;
918
919 if (brw->gen == 6) {
920 /* gen6 math instructions must have the destination be
921 * GRF, so no compute-to-MRF for them.
922 */
923 if (scan_inst->is_math()) {
924 break;
925 }
926 }
927 }
928
929 /* If we can't handle the swizzle, bail. */
930 if (!scan_inst->can_reswizzle_dst(inst->dst.writemask,
931 inst->src[0].swizzle,
932 swizzle_mask)) {
933 break;
934 }
935
936 /* Mark which channels we found unconditional writes for. */
937 if (!scan_inst->predicate) {
938 for (int i = 0; i < 4; i++) {
939 if (scan_inst->dst.writemask & (1 << i) &&
940 chans_needed[i]) {
941 chans_needed[i] = false;
942 chans_remaining--;
943 }
944 }
945 }
946
947 if (chans_remaining == 0)
948 break;
949 }
950
951 /* We don't handle flow control here. Most computation of values
952 * that could be coalesced happens just before their use.
953 */
954 if (scan_inst->opcode == BRW_OPCODE_DO ||
955 scan_inst->opcode == BRW_OPCODE_WHILE ||
956 scan_inst->opcode == BRW_OPCODE_ELSE ||
957 scan_inst->opcode == BRW_OPCODE_ENDIF) {
958 break;
959 }
960
961 /* You can't read from an MRF, so if someone else reads our MRF's
962 * source GRF that we wanted to rewrite, that stops us. If it's a
963 * GRF we're trying to coalesce to, we don't actually handle
964 * rewriting sources so bail in that case as well.
965 */
966 bool interfered = false;
967 for (int i = 0; i < 3; i++) {
968 if (scan_inst->src[i].file == GRF &&
969 scan_inst->src[i].reg == inst->src[0].reg &&
970 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
971 interfered = true;
972 }
973 }
974 if (interfered)
975 break;
976
977 /* If somebody else writes our destination here, we can't coalesce
978 * before that.
979 */
980 if (scan_inst->dst.file == inst->dst.file &&
981 scan_inst->dst.reg == inst->dst.reg) {
982 break;
983 }
984
985 /* Check for reads of the register we're trying to coalesce into. We
986 * can't go rewriting instructions above that to put some other value
987 * in the register instead.
988 */
989 if (to_mrf && scan_inst->mlen > 0) {
990 if (inst->dst.reg >= scan_inst->base_mrf &&
991 inst->dst.reg < scan_inst->base_mrf + scan_inst->mlen) {
992 break;
993 }
994 } else {
995 for (int i = 0; i < 3; i++) {
996 if (scan_inst->src[i].file == inst->dst.file &&
997 scan_inst->src[i].reg == inst->dst.reg &&
998 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
999 interfered = true;
1000 }
1001 }
1002 if (interfered)
1003 break;
1004 }
1005 }
1006
1007 if (chans_remaining == 0) {
1008 /* If we've made it here, we have an MOV we want to coalesce out, and
1009 * a scan_inst pointing to the earliest instruction involved in
1010 * computing the value. Now go rewrite the instruction stream
1011 * between the two.
1012 */
1013
1014 while (scan_inst != inst) {
1015 if (scan_inst->dst.file == GRF &&
1016 scan_inst->dst.reg == inst->src[0].reg &&
1017 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
1018 scan_inst->reswizzle_dst(inst->dst.writemask,
1019 inst->src[0].swizzle);
1020 scan_inst->dst.file = inst->dst.file;
1021 scan_inst->dst.reg = inst->dst.reg;
1022 scan_inst->dst.reg_offset = inst->dst.reg_offset;
1023 scan_inst->saturate |= inst->saturate;
1024 }
1025 scan_inst = (vec4_instruction *)scan_inst->next;
1026 }
1027 inst->remove();
1028 progress = true;
1029 }
1030 }
1031
1032 if (progress)
1033 invalidate_live_intervals();
1034
1035 return progress;
1036 }
1037
1038 /**
1039 * Splits virtual GRFs requesting more than one contiguous physical register.
1040 *
1041 * We initially create large virtual GRFs for temporary structures, arrays,
1042 * and matrices, so that the dereference visitor functions can add reg_offsets
1043 * to work their way down to the actual member being accessed. But when it
1044 * comes to optimization, we'd like to treat each register as individual
1045 * storage if possible.
1046 *
1047 * So far, the only thing that might prevent splitting is a send message from
1048 * a GRF on IVB.
1049 */
1050 void
1051 vec4_visitor::split_virtual_grfs()
1052 {
1053 int num_vars = this->virtual_grf_count;
1054 int new_virtual_grf[num_vars];
1055 bool split_grf[num_vars];
1056
1057 memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1058
1059 /* Try to split anything > 0 sized. */
1060 for (int i = 0; i < num_vars; i++) {
1061 split_grf[i] = this->virtual_grf_sizes[i] != 1;
1062 }
1063
1064 /* Check that the instructions are compatible with the registers we're trying
1065 * to split.
1066 */
1067 foreach_list(node, &this->instructions) {
1068 vec4_instruction *inst = (vec4_instruction *)node;
1069
1070 /* If there's a SEND message loading from a GRF on gen7+, it needs to be
1071 * contiguous.
1072 */
1073 if (inst->is_send_from_grf()) {
1074 for (int i = 0; i < 3; i++) {
1075 if (inst->src[i].file == GRF) {
1076 split_grf[inst->src[i].reg] = false;
1077 }
1078 }
1079 }
1080 }
1081
1082 /* Allocate new space for split regs. Note that the virtual
1083 * numbers will be contiguous.
1084 */
1085 for (int i = 0; i < num_vars; i++) {
1086 if (!split_grf[i])
1087 continue;
1088
1089 new_virtual_grf[i] = virtual_grf_alloc(1);
1090 for (int j = 2; j < this->virtual_grf_sizes[i]; j++) {
1091 int reg = virtual_grf_alloc(1);
1092 assert(reg == new_virtual_grf[i] + j - 1);
1093 (void) reg;
1094 }
1095 this->virtual_grf_sizes[i] = 1;
1096 }
1097
1098 foreach_list(node, &this->instructions) {
1099 vec4_instruction *inst = (vec4_instruction *)node;
1100
1101 if (inst->dst.file == GRF && split_grf[inst->dst.reg] &&
1102 inst->dst.reg_offset != 0) {
1103 inst->dst.reg = (new_virtual_grf[inst->dst.reg] +
1104 inst->dst.reg_offset - 1);
1105 inst->dst.reg_offset = 0;
1106 }
1107 for (int i = 0; i < 3; i++) {
1108 if (inst->src[i].file == GRF && split_grf[inst->src[i].reg] &&
1109 inst->src[i].reg_offset != 0) {
1110 inst->src[i].reg = (new_virtual_grf[inst->src[i].reg] +
1111 inst->src[i].reg_offset - 1);
1112 inst->src[i].reg_offset = 0;
1113 }
1114 }
1115 }
1116 invalidate_live_intervals();
1117 }
1118
1119 void
1120 vec4_visitor::dump_instruction(backend_instruction *be_inst)
1121 {
1122 vec4_instruction *inst = (vec4_instruction *)be_inst;
1123
1124 printf("%s", brw_instruction_name(inst->opcode));
1125 if (inst->conditional_mod) {
1126 printf("%s", conditional_modifier[inst->conditional_mod]);
1127 }
1128 printf(" ");
1129
1130 switch (inst->dst.file) {
1131 case GRF:
1132 printf("vgrf%d.%d", inst->dst.reg, inst->dst.reg_offset);
1133 break;
1134 case MRF:
1135 printf("m%d", inst->dst.reg);
1136 break;
1137 case HW_REG:
1138 if (inst->dst.fixed_hw_reg.file == BRW_ARCHITECTURE_REGISTER_FILE) {
1139 switch (inst->dst.fixed_hw_reg.nr) {
1140 case BRW_ARF_NULL:
1141 printf("null");
1142 break;
1143 case BRW_ARF_ADDRESS:
1144 printf("a0.%d", inst->dst.fixed_hw_reg.subnr);
1145 break;
1146 case BRW_ARF_ACCUMULATOR:
1147 printf("acc%d", inst->dst.fixed_hw_reg.subnr);
1148 break;
1149 case BRW_ARF_FLAG:
1150 printf("f%d.%d", inst->dst.fixed_hw_reg.nr & 0xf,
1151 inst->dst.fixed_hw_reg.subnr);
1152 break;
1153 default:
1154 printf("arf%d.%d", inst->dst.fixed_hw_reg.nr & 0xf,
1155 inst->dst.fixed_hw_reg.subnr);
1156 break;
1157 }
1158 } else {
1159 printf("hw_reg%d", inst->dst.fixed_hw_reg.nr);
1160 }
1161 if (inst->dst.fixed_hw_reg.subnr)
1162 printf("+%d", inst->dst.fixed_hw_reg.subnr);
1163 break;
1164 case BAD_FILE:
1165 printf("(null)");
1166 break;
1167 default:
1168 printf("???");
1169 break;
1170 }
1171 if (inst->dst.writemask != WRITEMASK_XYZW) {
1172 printf(".");
1173 if (inst->dst.writemask & 1)
1174 printf("x");
1175 if (inst->dst.writemask & 2)
1176 printf("y");
1177 if (inst->dst.writemask & 4)
1178 printf("z");
1179 if (inst->dst.writemask & 8)
1180 printf("w");
1181 }
1182 printf(":%s, ", brw_reg_type_letters(inst->dst.type));
1183
1184 for (int i = 0; i < 3 && inst->src[i].file != BAD_FILE; i++) {
1185 if (inst->src[i].negate)
1186 printf("-");
1187 if (inst->src[i].abs)
1188 printf("|");
1189 switch (inst->src[i].file) {
1190 case GRF:
1191 printf("vgrf%d", inst->src[i].reg);
1192 break;
1193 case ATTR:
1194 printf("attr%d", inst->src[i].reg);
1195 break;
1196 case UNIFORM:
1197 printf("u%d", inst->src[i].reg);
1198 break;
1199 case IMM:
1200 switch (inst->src[i].type) {
1201 case BRW_REGISTER_TYPE_F:
1202 printf("%fF", inst->src[i].imm.f);
1203 break;
1204 case BRW_REGISTER_TYPE_D:
1205 printf("%dD", inst->src[i].imm.i);
1206 break;
1207 case BRW_REGISTER_TYPE_UD:
1208 printf("%uU", inst->src[i].imm.u);
1209 break;
1210 default:
1211 printf("???");
1212 break;
1213 }
1214 break;
1215 case HW_REG:
1216 if (inst->src[i].fixed_hw_reg.negate)
1217 printf("-");
1218 if (inst->src[i].fixed_hw_reg.abs)
1219 printf("|");
1220 if (inst->src[i].fixed_hw_reg.file == BRW_ARCHITECTURE_REGISTER_FILE) {
1221 switch (inst->src[i].fixed_hw_reg.nr) {
1222 case BRW_ARF_NULL:
1223 printf("null");
1224 break;
1225 case BRW_ARF_ADDRESS:
1226 printf("a0.%d", inst->src[i].fixed_hw_reg.subnr);
1227 break;
1228 case BRW_ARF_ACCUMULATOR:
1229 printf("acc%d", inst->src[i].fixed_hw_reg.subnr);
1230 break;
1231 case BRW_ARF_FLAG:
1232 printf("f%d.%d", inst->src[i].fixed_hw_reg.nr & 0xf,
1233 inst->src[i].fixed_hw_reg.subnr);
1234 break;
1235 default:
1236 printf("arf%d.%d", inst->src[i].fixed_hw_reg.nr & 0xf,
1237 inst->src[i].fixed_hw_reg.subnr);
1238 break;
1239 }
1240 } else {
1241 printf("hw_reg%d", inst->src[i].fixed_hw_reg.nr);
1242 }
1243 if (inst->src[i].fixed_hw_reg.subnr)
1244 printf("+%d", inst->src[i].fixed_hw_reg.subnr);
1245 if (inst->src[i].fixed_hw_reg.abs)
1246 printf("|");
1247 break;
1248 case BAD_FILE:
1249 printf("(null)");
1250 break;
1251 default:
1252 printf("???");
1253 break;
1254 }
1255
1256 if (virtual_grf_sizes[inst->src[i].reg] != 1)
1257 printf(".%d", inst->src[i].reg_offset);
1258
1259 if (inst->src[i].file != IMM) {
1260 static const char *chans[4] = {"x", "y", "z", "w"};
1261 printf(".");
1262 for (int c = 0; c < 4; c++) {
1263 printf("%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1264 }
1265 }
1266
1267 if (inst->src[i].abs)
1268 printf("|");
1269
1270 if (inst->src[i].file != IMM) {
1271 printf(":%s", reg_encoding[inst->src[i].type]);
1272 }
1273
1274 if (i < 2 && inst->src[i + 1].file != BAD_FILE)
1275 printf(", ");
1276 }
1277
1278 printf("\n");
1279 }
1280
1281
1282 static inline struct brw_reg
1283 attribute_to_hw_reg(int attr, bool interleaved)
1284 {
1285 if (interleaved)
1286 return stride(brw_vec4_grf(attr / 2, (attr % 2) * 4), 0, 4, 1);
1287 else
1288 return brw_vec8_grf(attr, 0);
1289 }
1290
1291
1292 /**
1293 * Replace each register of type ATTR in this->instructions with a reference
1294 * to a fixed HW register.
1295 *
1296 * If interleaved is true, then each attribute takes up half a register, with
1297 * register N containing attribute 2*N in its first half and attribute 2*N+1
1298 * in its second half (this corresponds to the payload setup used by geometry
1299 * shaders in "single" or "dual instanced" dispatch mode). If interleaved is
1300 * false, then each attribute takes up a whole register, with register N
1301 * containing attribute N (this corresponds to the payload setup used by
1302 * vertex shaders, and by geometry shaders in "dual object" dispatch mode).
1303 */
1304 void
1305 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map,
1306 bool interleaved)
1307 {
1308 foreach_list(node, &this->instructions) {
1309 vec4_instruction *inst = (vec4_instruction *)node;
1310
1311 /* We have to support ATTR as a destination for GL_FIXED fixup. */
1312 if (inst->dst.file == ATTR) {
1313 int grf = attribute_map[inst->dst.reg + inst->dst.reg_offset];
1314
1315 /* All attributes used in the shader need to have been assigned a
1316 * hardware register by the caller
1317 */
1318 assert(grf != 0);
1319
1320 struct brw_reg reg = attribute_to_hw_reg(grf, interleaved);
1321 reg.type = inst->dst.type;
1322 reg.dw1.bits.writemask = inst->dst.writemask;
1323
1324 inst->dst.file = HW_REG;
1325 inst->dst.fixed_hw_reg = reg;
1326 }
1327
1328 for (int i = 0; i < 3; i++) {
1329 if (inst->src[i].file != ATTR)
1330 continue;
1331
1332 int grf = attribute_map[inst->src[i].reg + inst->src[i].reg_offset];
1333
1334 /* All attributes used in the shader need to have been assigned a
1335 * hardware register by the caller
1336 */
1337 assert(grf != 0);
1338
1339 struct brw_reg reg = attribute_to_hw_reg(grf, interleaved);
1340 reg.dw1.bits.swizzle = inst->src[i].swizzle;
1341 reg.type = inst->src[i].type;
1342 if (inst->src[i].abs)
1343 reg = brw_abs(reg);
1344 if (inst->src[i].negate)
1345 reg = negate(reg);
1346
1347 inst->src[i].file = HW_REG;
1348 inst->src[i].fixed_hw_reg = reg;
1349 }
1350 }
1351 }
1352
1353 int
1354 vec4_vs_visitor::setup_attributes(int payload_reg)
1355 {
1356 int nr_attributes;
1357 int attribute_map[VERT_ATTRIB_MAX + 1];
1358 memset(attribute_map, 0, sizeof(attribute_map));
1359
1360 nr_attributes = 0;
1361 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
1362 if (vs_prog_data->inputs_read & BITFIELD64_BIT(i)) {
1363 attribute_map[i] = payload_reg + nr_attributes;
1364 nr_attributes++;
1365 }
1366 }
1367
1368 /* VertexID is stored by the VF as the last vertex element, but we
1369 * don't represent it with a flag in inputs_read, so we call it
1370 * VERT_ATTRIB_MAX.
1371 */
1372 if (vs_prog_data->uses_vertexid) {
1373 attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1374 nr_attributes++;
1375 }
1376
1377 lower_attributes_to_hw_regs(attribute_map, false /* interleaved */);
1378
1379 /* The BSpec says we always have to read at least one thing from
1380 * the VF, and it appears that the hardware wedges otherwise.
1381 */
1382 if (nr_attributes == 0)
1383 nr_attributes = 1;
1384
1385 prog_data->urb_read_length = (nr_attributes + 1) / 2;
1386
1387 unsigned vue_entries =
1388 MAX2(nr_attributes, prog_data->vue_map.num_slots);
1389
1390 if (brw->gen == 6)
1391 prog_data->urb_entry_size = ALIGN(vue_entries, 8) / 8;
1392 else
1393 prog_data->urb_entry_size = ALIGN(vue_entries, 4) / 4;
1394
1395 return payload_reg + nr_attributes;
1396 }
1397
1398 int
1399 vec4_visitor::setup_uniforms(int reg)
1400 {
1401 prog_data->dispatch_grf_start_reg = reg;
1402
1403 /* The pre-gen6 VS requires that some push constants get loaded no
1404 * matter what, or the GPU would hang.
1405 */
1406 if (brw->gen < 6 && this->uniforms == 0) {
1407 this->uniform_vector_size[this->uniforms] = 1;
1408
1409 stage_prog_data->param =
1410 reralloc(NULL, stage_prog_data->param, const float *, 4);
1411 for (unsigned int i = 0; i < 4; i++) {
1412 unsigned int slot = this->uniforms * 4 + i;
1413 static float zero = 0.0;
1414 stage_prog_data->param[slot] = &zero;
1415 }
1416
1417 this->uniforms++;
1418 reg++;
1419 } else {
1420 reg += ALIGN(uniforms, 2) / 2;
1421 }
1422
1423 stage_prog_data->nr_params = this->uniforms * 4;
1424
1425 prog_data->curb_read_length = reg - prog_data->dispatch_grf_start_reg;
1426
1427 return reg;
1428 }
1429
1430 void
1431 vec4_vs_visitor::setup_payload(void)
1432 {
1433 int reg = 0;
1434
1435 /* The payload always contains important data in g0, which contains
1436 * the URB handles that are passed on to the URB write at the end
1437 * of the thread. So, we always start push constants at g1.
1438 */
1439 reg++;
1440
1441 reg = setup_uniforms(reg);
1442
1443 reg = setup_attributes(reg);
1444
1445 this->first_non_payload_grf = reg;
1446 }
1447
1448 src_reg
1449 vec4_visitor::get_timestamp()
1450 {
1451 assert(brw->gen >= 7);
1452
1453 src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1454 BRW_ARF_TIMESTAMP,
1455 0,
1456 BRW_REGISTER_TYPE_UD,
1457 BRW_VERTICAL_STRIDE_0,
1458 BRW_WIDTH_4,
1459 BRW_HORIZONTAL_STRIDE_4,
1460 BRW_SWIZZLE_XYZW,
1461 WRITEMASK_XYZW));
1462
1463 dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1464
1465 vec4_instruction *mov = emit(MOV(dst, ts));
1466 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1467 * even if it's not enabled in the dispatch.
1468 */
1469 mov->force_writemask_all = true;
1470
1471 return src_reg(dst);
1472 }
1473
1474 void
1475 vec4_visitor::emit_shader_time_begin()
1476 {
1477 current_annotation = "shader time start";
1478 shader_start_time = get_timestamp();
1479 }
1480
1481 void
1482 vec4_visitor::emit_shader_time_end()
1483 {
1484 current_annotation = "shader time end";
1485 src_reg shader_end_time = get_timestamp();
1486
1487
1488 /* Check that there weren't any timestamp reset events (assuming these
1489 * were the only two timestamp reads that happened).
1490 */
1491 src_reg reset_end = shader_end_time;
1492 reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1493 vec4_instruction *test = emit(AND(dst_null_d(), reset_end, src_reg(1u)));
1494 test->conditional_mod = BRW_CONDITIONAL_Z;
1495
1496 emit(IF(BRW_PREDICATE_NORMAL));
1497
1498 /* Take the current timestamp and get the delta. */
1499 shader_start_time.negate = true;
1500 dst_reg diff = dst_reg(this, glsl_type::uint_type);
1501 emit(ADD(diff, shader_start_time, shader_end_time));
1502
1503 /* If there were no instructions between the two timestamp gets, the diff
1504 * is 2 cycles. Remove that overhead, so I can forget about that when
1505 * trying to determine the time taken for single instructions.
1506 */
1507 emit(ADD(diff, src_reg(diff), src_reg(-2u)));
1508
1509 emit_shader_time_write(st_base, src_reg(diff));
1510 emit_shader_time_write(st_written, src_reg(1u));
1511 emit(BRW_OPCODE_ELSE);
1512 emit_shader_time_write(st_reset, src_reg(1u));
1513 emit(BRW_OPCODE_ENDIF);
1514 }
1515
1516 void
1517 vec4_visitor::emit_shader_time_write(enum shader_time_shader_type type,
1518 src_reg value)
1519 {
1520 int shader_time_index =
1521 brw_get_shader_time_index(brw, shader_prog, prog, type);
1522
1523 dst_reg dst =
1524 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1525
1526 dst_reg offset = dst;
1527 dst_reg time = dst;
1528 time.reg_offset++;
1529
1530 offset.type = BRW_REGISTER_TYPE_UD;
1531 emit(MOV(offset, src_reg(shader_time_index * SHADER_TIME_STRIDE)));
1532
1533 time.type = BRW_REGISTER_TYPE_UD;
1534 emit(MOV(time, src_reg(value)));
1535
1536 emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1537 }
1538
1539 bool
1540 vec4_visitor::run()
1541 {
1542 sanity_param_count = prog->Parameters->NumParameters;
1543
1544 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
1545 emit_shader_time_begin();
1546
1547 assign_common_binding_table_offsets(0);
1548
1549 emit_prolog();
1550
1551 /* Generate VS IR for main(). (the visitor only descends into
1552 * functions called "main").
1553 */
1554 if (shader) {
1555 visit_instructions(shader->base.ir);
1556 } else {
1557 emit_program_code();
1558 }
1559 base_ir = NULL;
1560
1561 if (key->userclip_active && !prog->UsesClipDistanceOut)
1562 setup_uniform_clipplane_values();
1563
1564 emit_thread_end();
1565
1566 /* Before any optimization, push array accesses out to scratch
1567 * space where we need them to be. This pass may allocate new
1568 * virtual GRFs, so we want to do it early. It also makes sure
1569 * that we have reladdr computations available for CSE, since we'll
1570 * often do repeated subexpressions for those.
1571 */
1572 if (shader) {
1573 move_grf_array_access_to_scratch();
1574 move_uniform_array_access_to_pull_constants();
1575 } else {
1576 /* The ARB_vertex_program frontend emits pull constant loads directly
1577 * rather than using reladdr, so we don't need to walk through all the
1578 * instructions looking for things to move. There isn't anything.
1579 *
1580 * We do still need to split things to vec4 size.
1581 */
1582 split_uniform_registers();
1583 }
1584 pack_uniform_registers();
1585 move_push_constants_to_pull_constants();
1586 split_virtual_grfs();
1587
1588 bool progress;
1589 do {
1590 progress = false;
1591 progress = dead_code_eliminate() || progress;
1592 progress = dead_control_flow_eliminate(this) || progress;
1593 progress = opt_copy_propagation() || progress;
1594 progress = opt_algebraic() || progress;
1595 progress = opt_register_coalesce() || progress;
1596 } while (progress);
1597
1598
1599 if (failed)
1600 return false;
1601
1602 setup_payload();
1603
1604 if (false) {
1605 /* Debug of register spilling: Go spill everything. */
1606 const int grf_count = virtual_grf_count;
1607 float spill_costs[virtual_grf_count];
1608 bool no_spill[virtual_grf_count];
1609 evaluate_spill_costs(spill_costs, no_spill);
1610 for (int i = 0; i < grf_count; i++) {
1611 if (no_spill[i])
1612 continue;
1613 spill_reg(i);
1614 }
1615 }
1616
1617 while (!reg_allocate()) {
1618 if (failed)
1619 return false;
1620 }
1621
1622 opt_schedule_instructions();
1623
1624 opt_set_dependency_control();
1625
1626 /* If any state parameters were appended, then ParameterValues could have
1627 * been realloced, in which case the driver uniform storage set up by
1628 * _mesa_associate_uniform_storage() would point to freed memory. Make
1629 * sure that didn't happen.
1630 */
1631 assert(sanity_param_count == prog->Parameters->NumParameters);
1632
1633 return !failed;
1634 }
1635
1636 } /* namespace brw */
1637
1638 extern "C" {
1639
1640 /**
1641 * Compile a vertex shader.
1642 *
1643 * Returns the final assembly and the program's size.
1644 */
1645 const unsigned *
1646 brw_vs_emit(struct brw_context *brw,
1647 struct gl_shader_program *prog,
1648 struct brw_vs_compile *c,
1649 struct brw_vs_prog_data *prog_data,
1650 void *mem_ctx,
1651 unsigned *final_assembly_size)
1652 {
1653 bool start_busy = false;
1654 double start_time = 0;
1655
1656 if (unlikely(brw->perf_debug)) {
1657 start_busy = (brw->batch.last_bo &&
1658 drm_intel_bo_busy(brw->batch.last_bo));
1659 start_time = get_time();
1660 }
1661
1662 struct brw_shader *shader = NULL;
1663 if (prog)
1664 shader = (brw_shader *) prog->_LinkedShaders[MESA_SHADER_VERTEX];
1665
1666 if (unlikely(INTEL_DEBUG & DEBUG_VS))
1667 brw_dump_ir(brw, "vertex", prog, &shader->base, &c->vp->program.Base);
1668
1669 vec4_vs_visitor v(brw, c, prog_data, prog, shader, mem_ctx);
1670 if (!v.run()) {
1671 if (prog) {
1672 prog->LinkStatus = false;
1673 ralloc_strcat(&prog->InfoLog, v.fail_msg);
1674 }
1675
1676 _mesa_problem(NULL, "Failed to compile vertex shader: %s\n",
1677 v.fail_msg);
1678
1679 return NULL;
1680 }
1681
1682 const unsigned *assembly = NULL;
1683 if (brw->gen >= 8) {
1684 gen8_vec4_generator g(brw, prog, &c->vp->program.Base, &prog_data->base,
1685 mem_ctx, INTEL_DEBUG & DEBUG_VS);
1686 assembly = g.generate_assembly(&v.instructions, final_assembly_size);
1687 } else {
1688 vec4_generator g(brw, prog, &c->vp->program.Base, &prog_data->base,
1689 mem_ctx, INTEL_DEBUG & DEBUG_VS);
1690 assembly = g.generate_assembly(&v.instructions, final_assembly_size);
1691 }
1692
1693 if (unlikely(brw->perf_debug) && shader) {
1694 if (shader->compiled_once) {
1695 brw_vs_debug_recompile(brw, prog, &c->key);
1696 }
1697 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
1698 perf_debug("VS compile took %.03f ms and stalled the GPU\n",
1699 (get_time() - start_time) * 1000);
1700 }
1701 shader->compiled_once = true;
1702 }
1703
1704 return assembly;
1705 }
1706
1707
1708 void
1709 brw_vec4_setup_prog_key_for_precompile(struct gl_context *ctx,
1710 struct brw_vec4_prog_key *key,
1711 GLuint id, struct gl_program *prog)
1712 {
1713 key->program_string_id = id;
1714 key->clamp_vertex_color = ctx->API == API_OPENGL_COMPAT;
1715
1716 unsigned sampler_count = _mesa_fls(prog->SamplersUsed);
1717 for (unsigned i = 0; i < sampler_count; i++) {
1718 if (prog->ShadowSamplers & (1 << i)) {
1719 /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
1720 key->tex.swizzles[i] =
1721 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
1722 } else {
1723 /* Color sampler: assume no swizzling. */
1724 key->tex.swizzles[i] = SWIZZLE_XYZW;
1725 }
1726 }
1727 }
1728
1729 } /* extern "C" */