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