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