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