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