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