i965/vs: Move lazy NIR creation to codegen_vs_prog
[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 bool uniform_used[this->uniforms];
522 int new_loc[this->uniforms];
523 int new_chan[this->uniforms];
524
525 memset(uniform_used, 0, sizeof(uniform_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 for (int i = 0 ; i < 3; i++) {
535 if (inst->src[i].file != UNIFORM)
536 continue;
537
538 uniform_used[inst->src[i].reg] = true;
539 }
540 }
541
542 int new_uniform_count = 0;
543
544 /* Now, figure out a packing of the live uniform vectors into our
545 * push constants.
546 */
547 for (int src = 0; src < uniforms; src++) {
548 assert(src < uniform_array_size);
549 int size = this->uniform_vector_size[src];
550
551 if (!uniform_used[src]) {
552 this->uniform_vector_size[src] = 0;
553 continue;
554 }
555
556 int dst;
557 /* Find the lowest place we can slot this uniform in. */
558 for (dst = 0; dst < src; dst++) {
559 if (this->uniform_vector_size[dst] + size <= 4)
560 break;
561 }
562
563 if (src == dst) {
564 new_loc[src] = dst;
565 new_chan[src] = 0;
566 } else {
567 new_loc[src] = dst;
568 new_chan[src] = this->uniform_vector_size[dst];
569
570 /* Move the references to the data */
571 for (int j = 0; j < size; j++) {
572 stage_prog_data->param[dst * 4 + new_chan[src] + j] =
573 stage_prog_data->param[src * 4 + j];
574 }
575
576 this->uniform_vector_size[dst] += size;
577 this->uniform_vector_size[src] = 0;
578 }
579
580 new_uniform_count = MAX2(new_uniform_count, dst + 1);
581 }
582
583 this->uniforms = new_uniform_count;
584
585 /* Now, update the instructions for our repacked uniforms. */
586 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
587 for (int i = 0 ; i < 3; i++) {
588 int src = inst->src[i].reg;
589
590 if (inst->src[i].file != UNIFORM)
591 continue;
592
593 inst->src[i].reg = new_loc[src];
594 inst->src[i].swizzle += BRW_SWIZZLE4(new_chan[src], new_chan[src],
595 new_chan[src], new_chan[src]);
596 }
597 }
598 }
599
600 /**
601 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
602 *
603 * While GLSL IR also performs this optimization, we end up with it in
604 * our instruction stream for a couple of reasons. One is that we
605 * sometimes generate silly instructions, for example in array access
606 * where we'll generate "ADD offset, index, base" even if base is 0.
607 * The other is that GLSL IR's constant propagation doesn't track the
608 * components of aggregates, so some VS patterns (initialize matrix to
609 * 0, accumulate in vertex blending factors) end up breaking down to
610 * instructions involving 0.
611 */
612 bool
613 vec4_visitor::opt_algebraic()
614 {
615 bool progress = false;
616
617 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
618 switch (inst->opcode) {
619 case BRW_OPCODE_MOV:
620 if (inst->src[0].file != IMM)
621 break;
622
623 if (inst->saturate) {
624 if (inst->dst.type != inst->src[0].type)
625 assert(!"unimplemented: saturate mixed types");
626
627 if (brw_saturate_immediate(inst->dst.type,
628 &inst->src[0].fixed_hw_reg)) {
629 inst->saturate = false;
630 progress = true;
631 }
632 }
633 break;
634
635 case VEC4_OPCODE_UNPACK_UNIFORM:
636 if (inst->src[0].file != UNIFORM) {
637 inst->opcode = BRW_OPCODE_MOV;
638 progress = true;
639 }
640 break;
641
642 case BRW_OPCODE_ADD:
643 if (inst->src[1].is_zero()) {
644 inst->opcode = BRW_OPCODE_MOV;
645 inst->src[1] = src_reg();
646 progress = true;
647 }
648 break;
649
650 case BRW_OPCODE_MUL:
651 if (inst->src[1].is_zero()) {
652 inst->opcode = BRW_OPCODE_MOV;
653 switch (inst->src[0].type) {
654 case BRW_REGISTER_TYPE_F:
655 inst->src[0] = src_reg(0.0f);
656 break;
657 case BRW_REGISTER_TYPE_D:
658 inst->src[0] = src_reg(0);
659 break;
660 case BRW_REGISTER_TYPE_UD:
661 inst->src[0] = src_reg(0u);
662 break;
663 default:
664 unreachable("not reached");
665 }
666 inst->src[1] = src_reg();
667 progress = true;
668 } else if (inst->src[1].is_one()) {
669 inst->opcode = BRW_OPCODE_MOV;
670 inst->src[1] = src_reg();
671 progress = true;
672 } else if (inst->src[1].is_negative_one()) {
673 inst->opcode = BRW_OPCODE_MOV;
674 inst->src[0].negate = !inst->src[0].negate;
675 inst->src[1] = src_reg();
676 progress = true;
677 }
678 break;
679 case BRW_OPCODE_CMP:
680 if (inst->conditional_mod == BRW_CONDITIONAL_GE &&
681 inst->src[0].abs &&
682 inst->src[0].negate &&
683 inst->src[1].is_zero()) {
684 inst->src[0].abs = false;
685 inst->src[0].negate = false;
686 inst->conditional_mod = BRW_CONDITIONAL_Z;
687 progress = true;
688 break;
689 }
690 break;
691 case SHADER_OPCODE_RCP: {
692 vec4_instruction *prev = (vec4_instruction *)inst->prev;
693 if (prev->opcode == SHADER_OPCODE_SQRT) {
694 if (inst->src[0].equals(src_reg(prev->dst))) {
695 inst->opcode = SHADER_OPCODE_RSQ;
696 inst->src[0] = prev->src[0];
697 progress = true;
698 }
699 }
700 break;
701 }
702 case SHADER_OPCODE_BROADCAST:
703 if (is_uniform(inst->src[0]) ||
704 inst->src[1].is_zero()) {
705 inst->opcode = BRW_OPCODE_MOV;
706 inst->src[1] = src_reg();
707 inst->force_writemask_all = true;
708 progress = true;
709 }
710 break;
711
712 default:
713 break;
714 }
715 }
716
717 if (progress)
718 invalidate_live_intervals();
719
720 return progress;
721 }
722
723 /**
724 * Only a limited number of hardware registers may be used for push
725 * constants, so this turns access to the overflowed constants into
726 * pull constants.
727 */
728 void
729 vec4_visitor::move_push_constants_to_pull_constants()
730 {
731 int pull_constant_loc[this->uniforms];
732
733 /* Only allow 32 registers (256 uniform components) as push constants,
734 * which is the limit on gen6.
735 *
736 * If changing this value, note the limitation about total_regs in
737 * brw_curbe.c.
738 */
739 int max_uniform_components = 32 * 8;
740 if (this->uniforms * 4 <= max_uniform_components)
741 return;
742
743 /* Make some sort of choice as to which uniforms get sent to pull
744 * constants. We could potentially do something clever here like
745 * look for the most infrequently used uniform vec4s, but leave
746 * that for later.
747 */
748 for (int i = 0; i < this->uniforms * 4; i += 4) {
749 pull_constant_loc[i / 4] = -1;
750
751 if (i >= max_uniform_components) {
752 const gl_constant_value **values = &stage_prog_data->param[i];
753
754 /* Try to find an existing copy of this uniform in the pull
755 * constants if it was part of an array access already.
756 */
757 for (unsigned int j = 0; j < stage_prog_data->nr_pull_params; j += 4) {
758 int matches;
759
760 for (matches = 0; matches < 4; matches++) {
761 if (stage_prog_data->pull_param[j + matches] != values[matches])
762 break;
763 }
764
765 if (matches == 4) {
766 pull_constant_loc[i / 4] = j / 4;
767 break;
768 }
769 }
770
771 if (pull_constant_loc[i / 4] == -1) {
772 assert(stage_prog_data->nr_pull_params % 4 == 0);
773 pull_constant_loc[i / 4] = stage_prog_data->nr_pull_params / 4;
774
775 for (int j = 0; j < 4; j++) {
776 stage_prog_data->pull_param[stage_prog_data->nr_pull_params++] =
777 values[j];
778 }
779 }
780 }
781 }
782
783 /* Now actually rewrite usage of the things we've moved to pull
784 * constants.
785 */
786 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
787 for (int i = 0 ; i < 3; i++) {
788 if (inst->src[i].file != UNIFORM ||
789 pull_constant_loc[inst->src[i].reg] == -1)
790 continue;
791
792 int uniform = inst->src[i].reg;
793
794 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
795
796 emit_pull_constant_load(block, inst, temp, inst->src[i],
797 pull_constant_loc[uniform]);
798
799 inst->src[i].file = temp.file;
800 inst->src[i].reg = temp.reg;
801 inst->src[i].reg_offset = temp.reg_offset;
802 inst->src[i].reladdr = NULL;
803 }
804 }
805
806 /* Repack push constants to remove the now-unused ones. */
807 pack_uniform_registers();
808 }
809
810 /* Conditions for which we want to avoid setting the dependency control bits */
811 bool
812 vec4_visitor::is_dep_ctrl_unsafe(const vec4_instruction *inst)
813 {
814 #define IS_DWORD(reg) \
815 (reg.type == BRW_REGISTER_TYPE_UD || \
816 reg.type == BRW_REGISTER_TYPE_D)
817
818 /* "When source or destination datatype is 64b or operation is integer DWord
819 * multiply, DepCtrl must not be used."
820 * May apply to future SoCs as well.
821 */
822 if (devinfo->is_cherryview) {
823 if (inst->opcode == BRW_OPCODE_MUL &&
824 IS_DWORD(inst->src[0]) &&
825 IS_DWORD(inst->src[1]))
826 return true;
827 }
828 #undef IS_DWORD
829
830 if (devinfo->gen >= 8) {
831 if (inst->opcode == BRW_OPCODE_F32TO16)
832 return true;
833 }
834
835 /*
836 * mlen:
837 * In the presence of send messages, totally interrupt dependency
838 * control. They're long enough that the chance of dependency
839 * control around them just doesn't matter.
840 *
841 * predicate:
842 * From the Ivy Bridge PRM, volume 4 part 3.7, page 80:
843 * When a sequence of NoDDChk and NoDDClr are used, the last instruction that
844 * completes the scoreboard clear must have a non-zero execution mask. This
845 * means, if any kind of predication can change the execution mask or channel
846 * enable of the last instruction, the optimization must be avoided. This is
847 * to avoid instructions being shot down the pipeline when no writes are
848 * required.
849 *
850 * math:
851 * Dependency control does not work well over math instructions.
852 * NB: Discovered empirically
853 */
854 return (inst->mlen || inst->predicate || inst->is_math());
855 }
856
857 /**
858 * Sets the dependency control fields on instructions after register
859 * allocation and before the generator is run.
860 *
861 * When you have a sequence of instructions like:
862 *
863 * DP4 temp.x vertex uniform[0]
864 * DP4 temp.y vertex uniform[0]
865 * DP4 temp.z vertex uniform[0]
866 * DP4 temp.w vertex uniform[0]
867 *
868 * The hardware doesn't know that it can actually run the later instructions
869 * while the previous ones are in flight, producing stalls. However, we have
870 * manual fields we can set in the instructions that let it do so.
871 */
872 void
873 vec4_visitor::opt_set_dependency_control()
874 {
875 vec4_instruction *last_grf_write[BRW_MAX_GRF];
876 uint8_t grf_channels_written[BRW_MAX_GRF];
877 vec4_instruction *last_mrf_write[BRW_MAX_GRF];
878 uint8_t mrf_channels_written[BRW_MAX_GRF];
879
880 assert(prog_data->total_grf ||
881 !"Must be called after register allocation");
882
883 foreach_block (block, cfg) {
884 memset(last_grf_write, 0, sizeof(last_grf_write));
885 memset(last_mrf_write, 0, sizeof(last_mrf_write));
886
887 foreach_inst_in_block (vec4_instruction, inst, block) {
888 /* If we read from a register that we were doing dependency control
889 * on, don't do dependency control across the read.
890 */
891 for (int i = 0; i < 3; i++) {
892 int reg = inst->src[i].reg + inst->src[i].reg_offset;
893 if (inst->src[i].file == GRF) {
894 last_grf_write[reg] = NULL;
895 } else if (inst->src[i].file == HW_REG) {
896 memset(last_grf_write, 0, sizeof(last_grf_write));
897 break;
898 }
899 assert(inst->src[i].file != MRF);
900 }
901
902 if (is_dep_ctrl_unsafe(inst)) {
903 memset(last_grf_write, 0, sizeof(last_grf_write));
904 memset(last_mrf_write, 0, sizeof(last_mrf_write));
905 continue;
906 }
907
908 /* Now, see if we can do dependency control for this instruction
909 * against a previous one writing to its destination.
910 */
911 int reg = inst->dst.reg + inst->dst.reg_offset;
912 if (inst->dst.file == GRF) {
913 if (last_grf_write[reg] &&
914 !(inst->dst.writemask & grf_channels_written[reg])) {
915 last_grf_write[reg]->no_dd_clear = true;
916 inst->no_dd_check = true;
917 } else {
918 grf_channels_written[reg] = 0;
919 }
920
921 last_grf_write[reg] = inst;
922 grf_channels_written[reg] |= inst->dst.writemask;
923 } else if (inst->dst.file == MRF) {
924 if (last_mrf_write[reg] &&
925 !(inst->dst.writemask & mrf_channels_written[reg])) {
926 last_mrf_write[reg]->no_dd_clear = true;
927 inst->no_dd_check = true;
928 } else {
929 mrf_channels_written[reg] = 0;
930 }
931
932 last_mrf_write[reg] = inst;
933 mrf_channels_written[reg] |= inst->dst.writemask;
934 } else if (inst->dst.reg == HW_REG) {
935 if (inst->dst.fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE)
936 memset(last_grf_write, 0, sizeof(last_grf_write));
937 if (inst->dst.fixed_hw_reg.file == BRW_MESSAGE_REGISTER_FILE)
938 memset(last_mrf_write, 0, sizeof(last_mrf_write));
939 }
940 }
941 }
942 }
943
944 bool
945 vec4_instruction::can_reswizzle(const struct brw_device_info *devinfo,
946 int dst_writemask,
947 int swizzle,
948 int swizzle_mask)
949 {
950 /* Gen6 MATH instructions can not execute in align16 mode, so swizzles
951 * or writemasking are not allowed.
952 */
953 if (devinfo->gen == 6 && is_math() &&
954 (swizzle != BRW_SWIZZLE_XYZW || dst_writemask != WRITEMASK_XYZW))
955 return false;
956
957 /* If this instruction sets anything not referenced by swizzle, then we'd
958 * totally break it when we reswizzle.
959 */
960 if (dst.writemask & ~swizzle_mask)
961 return false;
962
963 if (mlen > 0)
964 return false;
965
966 /* We can't use swizzles on the accumulator and that's really the only
967 * HW_REG we would care to reswizzle so just disallow them all.
968 */
969 for (int i = 0; i < 3; i++) {
970 if (src[i].file == HW_REG)
971 return false;
972 }
973
974 return true;
975 }
976
977 /**
978 * For any channels in the swizzle's source that were populated by this
979 * instruction, rewrite the instruction to put the appropriate result directly
980 * in those channels.
981 *
982 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
983 */
984 void
985 vec4_instruction::reswizzle(int dst_writemask, int swizzle)
986 {
987 /* Destination write mask doesn't correspond to source swizzle for the dot
988 * product and pack_bytes instructions.
989 */
990 if (opcode != BRW_OPCODE_DP4 && opcode != BRW_OPCODE_DPH &&
991 opcode != BRW_OPCODE_DP3 && opcode != BRW_OPCODE_DP2 &&
992 opcode != VEC4_OPCODE_PACK_BYTES) {
993 for (int i = 0; i < 3; i++) {
994 if (src[i].file == BAD_FILE || src[i].file == IMM)
995 continue;
996
997 src[i].swizzle = brw_compose_swizzle(swizzle, src[i].swizzle);
998 }
999 }
1000
1001 /* Apply the specified swizzle and writemask to the original mask of
1002 * written components.
1003 */
1004 dst.writemask = dst_writemask &
1005 brw_apply_swizzle_to_mask(swizzle, dst.writemask);
1006 }
1007
1008 /*
1009 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
1010 * just written and then MOVed into another reg and making the original write
1011 * of the GRF write directly to the final destination instead.
1012 */
1013 bool
1014 vec4_visitor::opt_register_coalesce()
1015 {
1016 bool progress = false;
1017 int next_ip = 0;
1018
1019 calculate_live_intervals();
1020
1021 foreach_block_and_inst_safe (block, vec4_instruction, inst, cfg) {
1022 int ip = next_ip;
1023 next_ip++;
1024
1025 if (inst->opcode != BRW_OPCODE_MOV ||
1026 (inst->dst.file != GRF && inst->dst.file != MRF) ||
1027 inst->predicate ||
1028 inst->src[0].file != GRF ||
1029 inst->dst.type != inst->src[0].type ||
1030 inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
1031 continue;
1032
1033 /* Remove no-op MOVs */
1034 if (inst->dst.file == inst->src[0].file &&
1035 inst->dst.reg == inst->src[0].reg &&
1036 inst->dst.reg_offset == inst->src[0].reg_offset) {
1037 bool is_nop_mov = true;
1038
1039 for (unsigned c = 0; c < 4; c++) {
1040 if ((inst->dst.writemask & (1 << c)) == 0)
1041 continue;
1042
1043 if (BRW_GET_SWZ(inst->src[0].swizzle, c) != c) {
1044 is_nop_mov = false;
1045 break;
1046 }
1047 }
1048
1049 if (is_nop_mov) {
1050 inst->remove(block);
1051 continue;
1052 }
1053 }
1054
1055 bool to_mrf = (inst->dst.file == MRF);
1056
1057 /* Can't coalesce this GRF if someone else was going to
1058 * read it later.
1059 */
1060 if (var_range_end(var_from_reg(alloc, inst->src[0]), 4) > ip)
1061 continue;
1062
1063 /* We need to check interference with the final destination between this
1064 * instruction and the earliest instruction involved in writing the GRF
1065 * we're eliminating. To do that, keep track of which of our source
1066 * channels we've seen initialized.
1067 */
1068 const unsigned chans_needed =
1069 brw_apply_inv_swizzle_to_mask(inst->src[0].swizzle,
1070 inst->dst.writemask);
1071 unsigned chans_remaining = chans_needed;
1072
1073 /* Now walk up the instruction stream trying to see if we can rewrite
1074 * everything writing to the temporary to write into the destination
1075 * instead.
1076 */
1077 vec4_instruction *_scan_inst = (vec4_instruction *)inst->prev;
1078 foreach_inst_in_block_reverse_starting_from(vec4_instruction, scan_inst,
1079 inst, block) {
1080 _scan_inst = scan_inst;
1081
1082 if (inst->src[0].in_range(scan_inst->dst, scan_inst->regs_written)) {
1083 /* Found something writing to the reg we want to coalesce away. */
1084 if (to_mrf) {
1085 /* SEND instructions can't have MRF as a destination. */
1086 if (scan_inst->mlen)
1087 break;
1088
1089 if (devinfo->gen == 6) {
1090 /* gen6 math instructions must have the destination be
1091 * GRF, so no compute-to-MRF for them.
1092 */
1093 if (scan_inst->is_math()) {
1094 break;
1095 }
1096 }
1097 }
1098
1099 /* This doesn't handle saturation on the instruction we
1100 * want to coalesce away if the register types do not match.
1101 * But if scan_inst is a non type-converting 'mov', we can fix
1102 * the types later.
1103 */
1104 if (inst->saturate &&
1105 inst->dst.type != scan_inst->dst.type &&
1106 !(scan_inst->opcode == BRW_OPCODE_MOV &&
1107 scan_inst->dst.type == scan_inst->src[0].type))
1108 break;
1109
1110 /* If we can't handle the swizzle, bail. */
1111 if (!scan_inst->can_reswizzle(devinfo, inst->dst.writemask,
1112 inst->src[0].swizzle,
1113 chans_needed)) {
1114 break;
1115 }
1116
1117 /* This doesn't handle coalescing of multiple registers. */
1118 if (scan_inst->regs_written > 1)
1119 break;
1120
1121 /* Mark which channels we found unconditional writes for. */
1122 if (!scan_inst->predicate)
1123 chans_remaining &= ~scan_inst->dst.writemask;
1124
1125 if (chans_remaining == 0)
1126 break;
1127 }
1128
1129 /* You can't read from an MRF, so if someone else reads our MRF's
1130 * source GRF that we wanted to rewrite, that stops us. If it's a
1131 * GRF we're trying to coalesce to, we don't actually handle
1132 * rewriting sources so bail in that case as well.
1133 */
1134 bool interfered = false;
1135 for (int i = 0; i < 3; i++) {
1136 if (inst->src[0].in_range(scan_inst->src[i],
1137 scan_inst->regs_read(i)))
1138 interfered = true;
1139 }
1140 if (interfered)
1141 break;
1142
1143 /* If somebody else writes the same channels of our destination here,
1144 * we can't coalesce before that.
1145 */
1146 if (inst->dst.in_range(scan_inst->dst, scan_inst->regs_written) &&
1147 (inst->dst.writemask & scan_inst->dst.writemask) != 0) {
1148 break;
1149 }
1150
1151 /* Check for reads of the register we're trying to coalesce into. We
1152 * can't go rewriting instructions above that to put some other value
1153 * in the register instead.
1154 */
1155 if (to_mrf && scan_inst->mlen > 0) {
1156 if (inst->dst.reg >= scan_inst->base_mrf &&
1157 inst->dst.reg < scan_inst->base_mrf + scan_inst->mlen) {
1158 break;
1159 }
1160 } else {
1161 for (int i = 0; i < 3; i++) {
1162 if (inst->dst.in_range(scan_inst->src[i],
1163 scan_inst->regs_read(i)))
1164 interfered = true;
1165 }
1166 if (interfered)
1167 break;
1168 }
1169 }
1170
1171 if (chans_remaining == 0) {
1172 /* If we've made it here, we have an MOV we want to coalesce out, and
1173 * a scan_inst pointing to the earliest instruction involved in
1174 * computing the value. Now go rewrite the instruction stream
1175 * between the two.
1176 */
1177 vec4_instruction *scan_inst = _scan_inst;
1178 while (scan_inst != inst) {
1179 if (scan_inst->dst.file == GRF &&
1180 scan_inst->dst.reg == inst->src[0].reg &&
1181 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
1182 scan_inst->reswizzle(inst->dst.writemask,
1183 inst->src[0].swizzle);
1184 scan_inst->dst.file = inst->dst.file;
1185 scan_inst->dst.reg = inst->dst.reg;
1186 scan_inst->dst.reg_offset = inst->dst.reg_offset;
1187 if (inst->saturate &&
1188 inst->dst.type != scan_inst->dst.type) {
1189 /* If we have reached this point, scan_inst is a non
1190 * type-converting 'mov' and we can modify its register types
1191 * to match the ones in inst. Otherwise, we could have an
1192 * incorrect saturation result.
1193 */
1194 scan_inst->dst.type = inst->dst.type;
1195 scan_inst->src[0].type = inst->src[0].type;
1196 }
1197 scan_inst->saturate |= inst->saturate;
1198 }
1199 scan_inst = (vec4_instruction *)scan_inst->next;
1200 }
1201 inst->remove(block);
1202 progress = true;
1203 }
1204 }
1205
1206 if (progress)
1207 invalidate_live_intervals();
1208
1209 return progress;
1210 }
1211
1212 /**
1213 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
1214 * flow. We could probably do better here with some form of divergence
1215 * analysis.
1216 */
1217 bool
1218 vec4_visitor::eliminate_find_live_channel()
1219 {
1220 bool progress = false;
1221 unsigned depth = 0;
1222
1223 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
1224 switch (inst->opcode) {
1225 case BRW_OPCODE_IF:
1226 case BRW_OPCODE_DO:
1227 depth++;
1228 break;
1229
1230 case BRW_OPCODE_ENDIF:
1231 case BRW_OPCODE_WHILE:
1232 depth--;
1233 break;
1234
1235 case SHADER_OPCODE_FIND_LIVE_CHANNEL:
1236 if (depth == 0) {
1237 inst->opcode = BRW_OPCODE_MOV;
1238 inst->src[0] = src_reg(0);
1239 inst->force_writemask_all = true;
1240 progress = true;
1241 }
1242 break;
1243
1244 default:
1245 break;
1246 }
1247 }
1248
1249 return progress;
1250 }
1251
1252 /**
1253 * Splits virtual GRFs requesting more than one contiguous physical register.
1254 *
1255 * We initially create large virtual GRFs for temporary structures, arrays,
1256 * and matrices, so that the dereference visitor functions can add reg_offsets
1257 * to work their way down to the actual member being accessed. But when it
1258 * comes to optimization, we'd like to treat each register as individual
1259 * storage if possible.
1260 *
1261 * So far, the only thing that might prevent splitting is a send message from
1262 * a GRF on IVB.
1263 */
1264 void
1265 vec4_visitor::split_virtual_grfs()
1266 {
1267 int num_vars = this->alloc.count;
1268 int new_virtual_grf[num_vars];
1269 bool split_grf[num_vars];
1270
1271 memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1272
1273 /* Try to split anything > 0 sized. */
1274 for (int i = 0; i < num_vars; i++) {
1275 split_grf[i] = this->alloc.sizes[i] != 1;
1276 }
1277
1278 /* Check that the instructions are compatible with the registers we're trying
1279 * to split.
1280 */
1281 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1282 if (inst->dst.file == GRF && inst->regs_written > 1)
1283 split_grf[inst->dst.reg] = false;
1284
1285 for (int i = 0; i < 3; i++) {
1286 if (inst->src[i].file == GRF && inst->regs_read(i) > 1)
1287 split_grf[inst->src[i].reg] = false;
1288 }
1289 }
1290
1291 /* Allocate new space for split regs. Note that the virtual
1292 * numbers will be contiguous.
1293 */
1294 for (int i = 0; i < num_vars; i++) {
1295 if (!split_grf[i])
1296 continue;
1297
1298 new_virtual_grf[i] = alloc.allocate(1);
1299 for (unsigned j = 2; j < this->alloc.sizes[i]; j++) {
1300 unsigned reg = alloc.allocate(1);
1301 assert(reg == new_virtual_grf[i] + j - 1);
1302 (void) reg;
1303 }
1304 this->alloc.sizes[i] = 1;
1305 }
1306
1307 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1308 if (inst->dst.file == GRF && split_grf[inst->dst.reg] &&
1309 inst->dst.reg_offset != 0) {
1310 inst->dst.reg = (new_virtual_grf[inst->dst.reg] +
1311 inst->dst.reg_offset - 1);
1312 inst->dst.reg_offset = 0;
1313 }
1314 for (int i = 0; i < 3; i++) {
1315 if (inst->src[i].file == GRF && split_grf[inst->src[i].reg] &&
1316 inst->src[i].reg_offset != 0) {
1317 inst->src[i].reg = (new_virtual_grf[inst->src[i].reg] +
1318 inst->src[i].reg_offset - 1);
1319 inst->src[i].reg_offset = 0;
1320 }
1321 }
1322 }
1323 invalidate_live_intervals();
1324 }
1325
1326 void
1327 vec4_visitor::dump_instruction(backend_instruction *be_inst)
1328 {
1329 dump_instruction(be_inst, stderr);
1330 }
1331
1332 void
1333 vec4_visitor::dump_instruction(backend_instruction *be_inst, FILE *file)
1334 {
1335 vec4_instruction *inst = (vec4_instruction *)be_inst;
1336
1337 if (inst->predicate) {
1338 fprintf(file, "(%cf0.%d) ",
1339 inst->predicate_inverse ? '-' : '+',
1340 inst->flag_subreg);
1341 }
1342
1343 fprintf(file, "%s", brw_instruction_name(inst->opcode));
1344 if (inst->saturate)
1345 fprintf(file, ".sat");
1346 if (inst->conditional_mod) {
1347 fprintf(file, "%s", conditional_modifier[inst->conditional_mod]);
1348 if (!inst->predicate &&
1349 (devinfo->gen < 5 || (inst->opcode != BRW_OPCODE_SEL &&
1350 inst->opcode != BRW_OPCODE_IF &&
1351 inst->opcode != BRW_OPCODE_WHILE))) {
1352 fprintf(file, ".f0.%d", inst->flag_subreg);
1353 }
1354 }
1355 fprintf(file, " ");
1356
1357 switch (inst->dst.file) {
1358 case GRF:
1359 fprintf(file, "vgrf%d.%d", inst->dst.reg, inst->dst.reg_offset);
1360 break;
1361 case MRF:
1362 fprintf(file, "m%d", inst->dst.reg);
1363 break;
1364 case HW_REG:
1365 if (inst->dst.fixed_hw_reg.file == BRW_ARCHITECTURE_REGISTER_FILE) {
1366 switch (inst->dst.fixed_hw_reg.nr) {
1367 case BRW_ARF_NULL:
1368 fprintf(file, "null");
1369 break;
1370 case BRW_ARF_ADDRESS:
1371 fprintf(file, "a0.%d", inst->dst.fixed_hw_reg.subnr);
1372 break;
1373 case BRW_ARF_ACCUMULATOR:
1374 fprintf(file, "acc%d", inst->dst.fixed_hw_reg.subnr);
1375 break;
1376 case BRW_ARF_FLAG:
1377 fprintf(file, "f%d.%d", inst->dst.fixed_hw_reg.nr & 0xf,
1378 inst->dst.fixed_hw_reg.subnr);
1379 break;
1380 default:
1381 fprintf(file, "arf%d.%d", inst->dst.fixed_hw_reg.nr & 0xf,
1382 inst->dst.fixed_hw_reg.subnr);
1383 break;
1384 }
1385 } else {
1386 fprintf(file, "hw_reg%d", inst->dst.fixed_hw_reg.nr);
1387 }
1388 if (inst->dst.fixed_hw_reg.subnr)
1389 fprintf(file, "+%d", inst->dst.fixed_hw_reg.subnr);
1390 break;
1391 case BAD_FILE:
1392 fprintf(file, "(null)");
1393 break;
1394 default:
1395 fprintf(file, "???");
1396 break;
1397 }
1398 if (inst->dst.writemask != WRITEMASK_XYZW) {
1399 fprintf(file, ".");
1400 if (inst->dst.writemask & 1)
1401 fprintf(file, "x");
1402 if (inst->dst.writemask & 2)
1403 fprintf(file, "y");
1404 if (inst->dst.writemask & 4)
1405 fprintf(file, "z");
1406 if (inst->dst.writemask & 8)
1407 fprintf(file, "w");
1408 }
1409 fprintf(file, ":%s", brw_reg_type_letters(inst->dst.type));
1410
1411 if (inst->src[0].file != BAD_FILE)
1412 fprintf(file, ", ");
1413
1414 for (int i = 0; i < 3 && inst->src[i].file != BAD_FILE; i++) {
1415 if (inst->src[i].negate)
1416 fprintf(file, "-");
1417 if (inst->src[i].abs)
1418 fprintf(file, "|");
1419 switch (inst->src[i].file) {
1420 case GRF:
1421 fprintf(file, "vgrf%d", inst->src[i].reg);
1422 break;
1423 case ATTR:
1424 fprintf(file, "attr%d", inst->src[i].reg);
1425 break;
1426 case UNIFORM:
1427 fprintf(file, "u%d", inst->src[i].reg);
1428 break;
1429 case IMM:
1430 switch (inst->src[i].type) {
1431 case BRW_REGISTER_TYPE_F:
1432 fprintf(file, "%fF", inst->src[i].fixed_hw_reg.dw1.f);
1433 break;
1434 case BRW_REGISTER_TYPE_D:
1435 fprintf(file, "%dD", inst->src[i].fixed_hw_reg.dw1.d);
1436 break;
1437 case BRW_REGISTER_TYPE_UD:
1438 fprintf(file, "%uU", inst->src[i].fixed_hw_reg.dw1.ud);
1439 break;
1440 case BRW_REGISTER_TYPE_VF:
1441 fprintf(file, "[%-gF, %-gF, %-gF, %-gF]",
1442 brw_vf_to_float((inst->src[i].fixed_hw_reg.dw1.ud >> 0) & 0xff),
1443 brw_vf_to_float((inst->src[i].fixed_hw_reg.dw1.ud >> 8) & 0xff),
1444 brw_vf_to_float((inst->src[i].fixed_hw_reg.dw1.ud >> 16) & 0xff),
1445 brw_vf_to_float((inst->src[i].fixed_hw_reg.dw1.ud >> 24) & 0xff));
1446 break;
1447 default:
1448 fprintf(file, "???");
1449 break;
1450 }
1451 break;
1452 case HW_REG:
1453 if (inst->src[i].fixed_hw_reg.negate)
1454 fprintf(file, "-");
1455 if (inst->src[i].fixed_hw_reg.abs)
1456 fprintf(file, "|");
1457 if (inst->src[i].fixed_hw_reg.file == BRW_ARCHITECTURE_REGISTER_FILE) {
1458 switch (inst->src[i].fixed_hw_reg.nr) {
1459 case BRW_ARF_NULL:
1460 fprintf(file, "null");
1461 break;
1462 case BRW_ARF_ADDRESS:
1463 fprintf(file, "a0.%d", inst->src[i].fixed_hw_reg.subnr);
1464 break;
1465 case BRW_ARF_ACCUMULATOR:
1466 fprintf(file, "acc%d", inst->src[i].fixed_hw_reg.subnr);
1467 break;
1468 case BRW_ARF_FLAG:
1469 fprintf(file, "f%d.%d", inst->src[i].fixed_hw_reg.nr & 0xf,
1470 inst->src[i].fixed_hw_reg.subnr);
1471 break;
1472 default:
1473 fprintf(file, "arf%d.%d", inst->src[i].fixed_hw_reg.nr & 0xf,
1474 inst->src[i].fixed_hw_reg.subnr);
1475 break;
1476 }
1477 } else {
1478 fprintf(file, "hw_reg%d", inst->src[i].fixed_hw_reg.nr);
1479 }
1480 if (inst->src[i].fixed_hw_reg.subnr)
1481 fprintf(file, "+%d", inst->src[i].fixed_hw_reg.subnr);
1482 if (inst->src[i].fixed_hw_reg.abs)
1483 fprintf(file, "|");
1484 break;
1485 case BAD_FILE:
1486 fprintf(file, "(null)");
1487 break;
1488 default:
1489 fprintf(file, "???");
1490 break;
1491 }
1492
1493 /* Don't print .0; and only VGRFs have reg_offsets and sizes */
1494 if (inst->src[i].reg_offset != 0 &&
1495 inst->src[i].file == GRF &&
1496 alloc.sizes[inst->src[i].reg] != 1)
1497 fprintf(file, ".%d", inst->src[i].reg_offset);
1498
1499 if (inst->src[i].file != IMM) {
1500 static const char *chans[4] = {"x", "y", "z", "w"};
1501 fprintf(file, ".");
1502 for (int c = 0; c < 4; c++) {
1503 fprintf(file, "%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1504 }
1505 }
1506
1507 if (inst->src[i].abs)
1508 fprintf(file, "|");
1509
1510 if (inst->src[i].file != IMM) {
1511 fprintf(file, ":%s", brw_reg_type_letters(inst->src[i].type));
1512 }
1513
1514 if (i < 2 && inst->src[i + 1].file != BAD_FILE)
1515 fprintf(file, ", ");
1516 }
1517
1518 fprintf(file, "\n");
1519 }
1520
1521
1522 static inline struct brw_reg
1523 attribute_to_hw_reg(int attr, bool interleaved)
1524 {
1525 if (interleaved)
1526 return stride(brw_vec4_grf(attr / 2, (attr % 2) * 4), 0, 4, 1);
1527 else
1528 return brw_vec8_grf(attr, 0);
1529 }
1530
1531
1532 /**
1533 * Replace each register of type ATTR in this->instructions with a reference
1534 * to a fixed HW register.
1535 *
1536 * If interleaved is true, then each attribute takes up half a register, with
1537 * register N containing attribute 2*N in its first half and attribute 2*N+1
1538 * in its second half (this corresponds to the payload setup used by geometry
1539 * shaders in "single" or "dual instanced" dispatch mode). If interleaved is
1540 * false, then each attribute takes up a whole register, with register N
1541 * containing attribute N (this corresponds to the payload setup used by
1542 * vertex shaders, and by geometry shaders in "dual object" dispatch mode).
1543 */
1544 void
1545 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map,
1546 bool interleaved)
1547 {
1548 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1549 /* We have to support ATTR as a destination for GL_FIXED fixup. */
1550 if (inst->dst.file == ATTR) {
1551 int grf = attribute_map[inst->dst.reg + inst->dst.reg_offset];
1552
1553 /* All attributes used in the shader need to have been assigned a
1554 * hardware register by the caller
1555 */
1556 assert(grf != 0);
1557
1558 struct brw_reg reg = attribute_to_hw_reg(grf, interleaved);
1559 reg.type = inst->dst.type;
1560 reg.dw1.bits.writemask = inst->dst.writemask;
1561
1562 inst->dst.file = HW_REG;
1563 inst->dst.fixed_hw_reg = reg;
1564 }
1565
1566 for (int i = 0; i < 3; i++) {
1567 if (inst->src[i].file != ATTR)
1568 continue;
1569
1570 int grf = attribute_map[inst->src[i].reg + inst->src[i].reg_offset];
1571
1572 /* All attributes used in the shader need to have been assigned a
1573 * hardware register by the caller
1574 */
1575 assert(grf != 0);
1576
1577 struct brw_reg reg = attribute_to_hw_reg(grf, interleaved);
1578 reg.dw1.bits.swizzle = inst->src[i].swizzle;
1579 reg.type = inst->src[i].type;
1580 if (inst->src[i].abs)
1581 reg = brw_abs(reg);
1582 if (inst->src[i].negate)
1583 reg = negate(reg);
1584
1585 inst->src[i].file = HW_REG;
1586 inst->src[i].fixed_hw_reg = reg;
1587 }
1588 }
1589 }
1590
1591 int
1592 vec4_vs_visitor::setup_attributes(int payload_reg)
1593 {
1594 int nr_attributes;
1595 int attribute_map[VERT_ATTRIB_MAX + 1];
1596 memset(attribute_map, 0, sizeof(attribute_map));
1597
1598 nr_attributes = 0;
1599 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
1600 if (vs_prog_data->inputs_read & BITFIELD64_BIT(i)) {
1601 attribute_map[i] = payload_reg + nr_attributes;
1602 nr_attributes++;
1603 }
1604 }
1605
1606 /* VertexID is stored by the VF as the last vertex element, but we
1607 * don't represent it with a flag in inputs_read, so we call it
1608 * VERT_ATTRIB_MAX.
1609 */
1610 if (vs_prog_data->uses_vertexid || vs_prog_data->uses_instanceid) {
1611 attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1612 nr_attributes++;
1613 }
1614
1615 lower_attributes_to_hw_regs(attribute_map, false /* interleaved */);
1616
1617 /* The BSpec says we always have to read at least one thing from
1618 * the VF, and it appears that the hardware wedges otherwise.
1619 */
1620 if (nr_attributes == 0)
1621 nr_attributes = 1;
1622
1623 prog_data->urb_read_length = (nr_attributes + 1) / 2;
1624
1625 unsigned vue_entries =
1626 MAX2(nr_attributes, prog_data->vue_map.num_slots);
1627
1628 if (devinfo->gen == 6)
1629 prog_data->urb_entry_size = ALIGN(vue_entries, 8) / 8;
1630 else
1631 prog_data->urb_entry_size = ALIGN(vue_entries, 4) / 4;
1632
1633 return payload_reg + nr_attributes;
1634 }
1635
1636 int
1637 vec4_visitor::setup_uniforms(int reg)
1638 {
1639 prog_data->base.dispatch_grf_start_reg = reg;
1640
1641 /* The pre-gen6 VS requires that some push constants get loaded no
1642 * matter what, or the GPU would hang.
1643 */
1644 if (devinfo->gen < 6 && this->uniforms == 0) {
1645 assert(this->uniforms < this->uniform_array_size);
1646 this->uniform_vector_size[this->uniforms] = 1;
1647
1648 stage_prog_data->param =
1649 reralloc(NULL, stage_prog_data->param, const gl_constant_value *, 4);
1650 for (unsigned int i = 0; i < 4; i++) {
1651 unsigned int slot = this->uniforms * 4 + i;
1652 static gl_constant_value zero = { 0.0 };
1653 stage_prog_data->param[slot] = &zero;
1654 }
1655
1656 this->uniforms++;
1657 reg++;
1658 } else {
1659 reg += ALIGN(uniforms, 2) / 2;
1660 }
1661
1662 stage_prog_data->nr_params = this->uniforms * 4;
1663
1664 prog_data->base.curb_read_length =
1665 reg - prog_data->base.dispatch_grf_start_reg;
1666
1667 return reg;
1668 }
1669
1670 void
1671 vec4_vs_visitor::setup_payload(void)
1672 {
1673 int reg = 0;
1674
1675 /* The payload always contains important data in g0, which contains
1676 * the URB handles that are passed on to the URB write at the end
1677 * of the thread. So, we always start push constants at g1.
1678 */
1679 reg++;
1680
1681 reg = setup_uniforms(reg);
1682
1683 reg = setup_attributes(reg);
1684
1685 this->first_non_payload_grf = reg;
1686 }
1687
1688 void
1689 vec4_visitor::assign_binding_table_offsets()
1690 {
1691 assign_common_binding_table_offsets(0);
1692 }
1693
1694 src_reg
1695 vec4_visitor::get_timestamp()
1696 {
1697 assert(devinfo->gen >= 7);
1698
1699 src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1700 BRW_ARF_TIMESTAMP,
1701 0,
1702 0,
1703 0,
1704 BRW_REGISTER_TYPE_UD,
1705 BRW_VERTICAL_STRIDE_0,
1706 BRW_WIDTH_4,
1707 BRW_HORIZONTAL_STRIDE_4,
1708 BRW_SWIZZLE_XYZW,
1709 WRITEMASK_XYZW));
1710
1711 dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1712
1713 vec4_instruction *mov = emit(MOV(dst, ts));
1714 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1715 * even if it's not enabled in the dispatch.
1716 */
1717 mov->force_writemask_all = true;
1718
1719 return src_reg(dst);
1720 }
1721
1722 void
1723 vec4_visitor::emit_shader_time_begin()
1724 {
1725 current_annotation = "shader time start";
1726 shader_start_time = get_timestamp();
1727 }
1728
1729 void
1730 vec4_visitor::emit_shader_time_end()
1731 {
1732 current_annotation = "shader time end";
1733 src_reg shader_end_time = get_timestamp();
1734
1735
1736 /* Check that there weren't any timestamp reset events (assuming these
1737 * were the only two timestamp reads that happened).
1738 */
1739 src_reg reset_end = shader_end_time;
1740 reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1741 vec4_instruction *test = emit(AND(dst_null_d(), reset_end, src_reg(1u)));
1742 test->conditional_mod = BRW_CONDITIONAL_Z;
1743
1744 emit(IF(BRW_PREDICATE_NORMAL));
1745
1746 /* Take the current timestamp and get the delta. */
1747 shader_start_time.negate = true;
1748 dst_reg diff = dst_reg(this, glsl_type::uint_type);
1749 emit(ADD(diff, shader_start_time, shader_end_time));
1750
1751 /* If there were no instructions between the two timestamp gets, the diff
1752 * is 2 cycles. Remove that overhead, so I can forget about that when
1753 * trying to determine the time taken for single instructions.
1754 */
1755 emit(ADD(diff, src_reg(diff), src_reg(-2u)));
1756
1757 emit_shader_time_write(0, src_reg(diff));
1758 emit_shader_time_write(1, src_reg(1u));
1759 emit(BRW_OPCODE_ELSE);
1760 emit_shader_time_write(2, src_reg(1u));
1761 emit(BRW_OPCODE_ENDIF);
1762 }
1763
1764 void
1765 vec4_visitor::emit_shader_time_write(int shader_time_subindex, src_reg value)
1766 {
1767 dst_reg dst =
1768 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1769
1770 dst_reg offset = dst;
1771 dst_reg time = dst;
1772 time.reg_offset++;
1773
1774 offset.type = BRW_REGISTER_TYPE_UD;
1775 int index = shader_time_index * 3 + shader_time_subindex;
1776 emit(MOV(offset, src_reg(index * SHADER_TIME_STRIDE)));
1777
1778 time.type = BRW_REGISTER_TYPE_UD;
1779 emit(MOV(time, src_reg(value)));
1780
1781 vec4_instruction *inst =
1782 emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1783 inst->mlen = 2;
1784 }
1785
1786 bool
1787 vec4_visitor::run()
1788 {
1789 sanity_param_count = prog->Parameters->NumParameters;
1790
1791 if (shader_time_index >= 0)
1792 emit_shader_time_begin();
1793
1794 assign_binding_table_offsets();
1795
1796 emit_prolog();
1797
1798 assert(prog->nir != NULL);
1799 emit_nir_code();
1800 if (failed)
1801 return false;
1802 base_ir = NULL;
1803
1804 emit_thread_end();
1805
1806 calculate_cfg();
1807
1808 /* Before any optimization, push array accesses out to scratch
1809 * space where we need them to be. This pass may allocate new
1810 * virtual GRFs, so we want to do it early. It also makes sure
1811 * that we have reladdr computations available for CSE, since we'll
1812 * often do repeated subexpressions for those.
1813 */
1814 move_grf_array_access_to_scratch();
1815 move_uniform_array_access_to_pull_constants();
1816
1817 pack_uniform_registers();
1818 move_push_constants_to_pull_constants();
1819 split_virtual_grfs();
1820
1821 #define OPT(pass, args...) ({ \
1822 pass_num++; \
1823 bool this_progress = pass(args); \
1824 \
1825 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
1826 char filename[64]; \
1827 snprintf(filename, 64, "%s-%04d-%02d-%02d-" #pass, \
1828 stage_abbrev, shader_prog ? shader_prog->Name : 0, iteration, pass_num); \
1829 \
1830 backend_shader::dump_instructions(filename); \
1831 } \
1832 \
1833 progress = progress || this_progress; \
1834 this_progress; \
1835 })
1836
1837
1838 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER)) {
1839 char filename[64];
1840 snprintf(filename, 64, "%s-%04d-00-start",
1841 stage_abbrev, shader_prog ? shader_prog->Name : 0);
1842
1843 backend_shader::dump_instructions(filename);
1844 }
1845
1846 bool progress;
1847 int iteration = 0;
1848 int pass_num = 0;
1849 do {
1850 progress = false;
1851 pass_num = 0;
1852 iteration++;
1853
1854 OPT(opt_reduce_swizzle);
1855 OPT(dead_code_eliminate);
1856 OPT(dead_control_flow_eliminate, this);
1857 OPT(opt_copy_propagation);
1858 OPT(opt_cse);
1859 OPT(opt_algebraic);
1860 OPT(opt_register_coalesce);
1861 OPT(eliminate_find_live_channel);
1862 } while (progress);
1863
1864 pass_num = 0;
1865
1866 if (OPT(opt_vector_float)) {
1867 OPT(opt_cse);
1868 OPT(opt_copy_propagation, false);
1869 OPT(opt_copy_propagation, true);
1870 OPT(dead_code_eliminate);
1871 }
1872
1873 if (failed)
1874 return false;
1875
1876 setup_payload();
1877
1878 if (unlikely(INTEL_DEBUG & DEBUG_SPILL_VEC4)) {
1879 /* Debug of register spilling: Go spill everything. */
1880 const int grf_count = alloc.count;
1881 float spill_costs[alloc.count];
1882 bool no_spill[alloc.count];
1883 evaluate_spill_costs(spill_costs, no_spill);
1884 for (int i = 0; i < grf_count; i++) {
1885 if (no_spill[i])
1886 continue;
1887 spill_reg(i);
1888 }
1889 }
1890
1891 bool allocated_without_spills = reg_allocate();
1892
1893 if (!allocated_without_spills) {
1894 compiler->shader_perf_log(log_data,
1895 "%s shader triggered register spilling. "
1896 "Try reducing the number of live vec4 values "
1897 "to improve performance.\n",
1898 stage_name);
1899
1900 while (!reg_allocate()) {
1901 if (failed)
1902 return false;
1903 }
1904 }
1905
1906 opt_schedule_instructions();
1907
1908 opt_set_dependency_control();
1909
1910 if (last_scratch > 0) {
1911 prog_data->base.total_scratch =
1912 brw_get_scratch_size(last_scratch * REG_SIZE);
1913 }
1914
1915 /* If any state parameters were appended, then ParameterValues could have
1916 * been realloced, in which case the driver uniform storage set up by
1917 * _mesa_associate_uniform_storage() would point to freed memory. Make
1918 * sure that didn't happen.
1919 */
1920 assert(sanity_param_count == prog->Parameters->NumParameters);
1921
1922 return !failed;
1923 }
1924
1925 } /* namespace brw */
1926
1927 extern "C" {
1928
1929 /**
1930 * Compile a vertex shader.
1931 *
1932 * Returns the final assembly and the program's size.
1933 */
1934 const unsigned *
1935 brw_vs_emit(struct brw_context *brw,
1936 void *mem_ctx,
1937 const struct brw_vs_prog_key *key,
1938 struct brw_vs_prog_data *prog_data,
1939 struct gl_vertex_program *vp,
1940 struct gl_shader_program *prog,
1941 unsigned *final_assembly_size)
1942 {
1943 const unsigned *assembly = NULL;
1944
1945 struct brw_shader *shader = NULL;
1946 if (prog)
1947 shader = (brw_shader *) prog->_LinkedShaders[MESA_SHADER_VERTEX];
1948
1949 int st_index = -1;
1950 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
1951 st_index = brw_get_shader_time_index(brw, prog, &vp->Base, ST_VS);
1952
1953 if (unlikely(INTEL_DEBUG & DEBUG_VS))
1954 brw_dump_ir("vertex", prog, &shader->base, &vp->Base);
1955
1956 if (brw->intelScreen->compiler->scalar_vs) {
1957 prog_data->base.dispatch_mode = DISPATCH_MODE_SIMD8;
1958
1959 fs_visitor v(brw->intelScreen->compiler, brw,
1960 mem_ctx, MESA_SHADER_VERTEX, key,
1961 &prog_data->base.base, prog, &vp->Base,
1962 8, st_index);
1963 if (!v.run_vs(brw_select_clip_planes(&brw->ctx))) {
1964 if (prog) {
1965 prog->LinkStatus = false;
1966 ralloc_strcat(&prog->InfoLog, v.fail_msg);
1967 }
1968
1969 _mesa_problem(NULL, "Failed to compile vertex shader: %s\n",
1970 v.fail_msg);
1971
1972 return NULL;
1973 }
1974
1975 fs_generator g(brw->intelScreen->compiler, brw,
1976 mem_ctx, (void *) key, &prog_data->base.base,
1977 &vp->Base, v.promoted_constants,
1978 v.runtime_check_aads_emit, "VS");
1979 if (INTEL_DEBUG & DEBUG_VS) {
1980 char *name;
1981 if (prog) {
1982 name = ralloc_asprintf(mem_ctx, "%s vertex shader %d",
1983 prog->Label ? prog->Label : "unnamed",
1984 prog->Name);
1985 } else {
1986 name = ralloc_asprintf(mem_ctx, "vertex program %d",
1987 vp->Base.Id);
1988 }
1989 g.enable_debug(name);
1990 }
1991 g.generate_code(v.cfg, 8);
1992 assembly = g.get_assembly(final_assembly_size);
1993 }
1994
1995 if (!assembly) {
1996 prog_data->base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT;
1997
1998 vec4_vs_visitor v(brw->intelScreen->compiler, brw, key, prog_data,
1999 vp, prog, brw_select_clip_planes(&brw->ctx),
2000 mem_ctx, st_index,
2001 !_mesa_is_gles3(&brw->ctx));
2002 if (!v.run()) {
2003 if (prog) {
2004 prog->LinkStatus = false;
2005 ralloc_strcat(&prog->InfoLog, v.fail_msg);
2006 }
2007
2008 _mesa_problem(NULL, "Failed to compile vertex shader: %s\n",
2009 v.fail_msg);
2010
2011 return NULL;
2012 }
2013
2014 vec4_generator g(brw->intelScreen->compiler, brw,
2015 prog, &vp->Base, &prog_data->base,
2016 mem_ctx, INTEL_DEBUG & DEBUG_VS, "vertex", "VS");
2017 assembly = g.generate_assembly(v.cfg, final_assembly_size);
2018 }
2019
2020 return assembly;
2021 }
2022
2023 } /* extern "C" */