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