i965/vec4: Drop backend_reg::in_range() in favor of regions_overlap().
[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_reg_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_reg_offset != inst->dst.offset / REG_SIZE ||
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_reg_offset = inst->dst.offset / REG_SIZE;
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 !(inst->dst.writemask & grf_channels_written[reg])) {
984 last_grf_write[reg]->no_dd_clear = true;
985 inst->no_dd_check = true;
986 } else {
987 grf_channels_written[reg] = 0;
988 }
989
990 last_grf_write[reg] = inst;
991 grf_channels_written[reg] |= inst->dst.writemask;
992 } else if (inst->dst.file == MRF) {
993 if (last_mrf_write[reg] &&
994 !(inst->dst.writemask & mrf_channels_written[reg])) {
995 last_mrf_write[reg]->no_dd_clear = true;
996 inst->no_dd_check = true;
997 } else {
998 mrf_channels_written[reg] = 0;
999 }
1000
1001 last_mrf_write[reg] = inst;
1002 mrf_channels_written[reg] |= inst->dst.writemask;
1003 }
1004 }
1005 }
1006 }
1007
1008 bool
1009 vec4_instruction::can_reswizzle(const struct gen_device_info *devinfo,
1010 int dst_writemask,
1011 int swizzle,
1012 int swizzle_mask)
1013 {
1014 /* Gen6 MATH instructions can not execute in align16 mode, so swizzles
1015 * are not allowed.
1016 */
1017 if (devinfo->gen == 6 && is_math() && swizzle != BRW_SWIZZLE_XYZW)
1018 return false;
1019
1020 if (!can_do_writemask(devinfo) && dst_writemask != WRITEMASK_XYZW)
1021 return false;
1022
1023 /* If this instruction sets anything not referenced by swizzle, then we'd
1024 * totally break it when we reswizzle.
1025 */
1026 if (dst.writemask & ~swizzle_mask)
1027 return false;
1028
1029 if (mlen > 0)
1030 return false;
1031
1032 for (int i = 0; i < 3; i++) {
1033 if (src[i].is_accumulator())
1034 return false;
1035 }
1036
1037 return true;
1038 }
1039
1040 /**
1041 * For any channels in the swizzle's source that were populated by this
1042 * instruction, rewrite the instruction to put the appropriate result directly
1043 * in those channels.
1044 *
1045 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
1046 */
1047 void
1048 vec4_instruction::reswizzle(int dst_writemask, int swizzle)
1049 {
1050 /* Destination write mask doesn't correspond to source swizzle for the dot
1051 * product and pack_bytes instructions.
1052 */
1053 if (opcode != BRW_OPCODE_DP4 && opcode != BRW_OPCODE_DPH &&
1054 opcode != BRW_OPCODE_DP3 && opcode != BRW_OPCODE_DP2 &&
1055 opcode != VEC4_OPCODE_PACK_BYTES) {
1056 for (int i = 0; i < 3; i++) {
1057 if (src[i].file == BAD_FILE || src[i].file == IMM)
1058 continue;
1059
1060 src[i].swizzle = brw_compose_swizzle(swizzle, src[i].swizzle);
1061 }
1062 }
1063
1064 /* Apply the specified swizzle and writemask to the original mask of
1065 * written components.
1066 */
1067 dst.writemask = dst_writemask &
1068 brw_apply_swizzle_to_mask(swizzle, dst.writemask);
1069 }
1070
1071 /*
1072 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
1073 * just written and then MOVed into another reg and making the original write
1074 * of the GRF write directly to the final destination instead.
1075 */
1076 bool
1077 vec4_visitor::opt_register_coalesce()
1078 {
1079 bool progress = false;
1080 int next_ip = 0;
1081
1082 calculate_live_intervals();
1083
1084 foreach_block_and_inst_safe (block, vec4_instruction, inst, cfg) {
1085 int ip = next_ip;
1086 next_ip++;
1087
1088 if (inst->opcode != BRW_OPCODE_MOV ||
1089 (inst->dst.file != VGRF && inst->dst.file != MRF) ||
1090 inst->predicate ||
1091 inst->src[0].file != VGRF ||
1092 inst->dst.type != inst->src[0].type ||
1093 inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
1094 continue;
1095
1096 /* Remove no-op MOVs */
1097 if (inst->dst.file == inst->src[0].file &&
1098 inst->dst.nr == inst->src[0].nr &&
1099 inst->dst.offset / REG_SIZE == inst->src[0].offset / REG_SIZE) {
1100 bool is_nop_mov = true;
1101
1102 for (unsigned c = 0; c < 4; c++) {
1103 if ((inst->dst.writemask & (1 << c)) == 0)
1104 continue;
1105
1106 if (BRW_GET_SWZ(inst->src[0].swizzle, c) != c) {
1107 is_nop_mov = false;
1108 break;
1109 }
1110 }
1111
1112 if (is_nop_mov) {
1113 inst->remove(block);
1114 progress = true;
1115 continue;
1116 }
1117 }
1118
1119 bool to_mrf = (inst->dst.file == MRF);
1120
1121 /* Can't coalesce this GRF if someone else was going to
1122 * read it later.
1123 */
1124 if (var_range_end(var_from_reg(alloc, dst_reg(inst->src[0])), 4) > ip)
1125 continue;
1126
1127 /* We need to check interference with the final destination between this
1128 * instruction and the earliest instruction involved in writing the GRF
1129 * we're eliminating. To do that, keep track of which of our source
1130 * channels we've seen initialized.
1131 */
1132 const unsigned chans_needed =
1133 brw_apply_inv_swizzle_to_mask(inst->src[0].swizzle,
1134 inst->dst.writemask);
1135 unsigned chans_remaining = chans_needed;
1136
1137 /* Now walk up the instruction stream trying to see if we can rewrite
1138 * everything writing to the temporary to write into the destination
1139 * instead.
1140 */
1141 vec4_instruction *_scan_inst = (vec4_instruction *)inst->prev;
1142 foreach_inst_in_block_reverse_starting_from(vec4_instruction, scan_inst,
1143 inst) {
1144 _scan_inst = scan_inst;
1145
1146 if (regions_overlap(inst->src[0], inst->size_read(0),
1147 scan_inst->dst, scan_inst->size_written)) {
1148 /* Found something writing to the reg we want to coalesce away. */
1149 if (to_mrf) {
1150 /* SEND instructions can't have MRF as a destination. */
1151 if (scan_inst->mlen)
1152 break;
1153
1154 if (devinfo->gen == 6) {
1155 /* gen6 math instructions must have the destination be
1156 * VGRF, so no compute-to-MRF for them.
1157 */
1158 if (scan_inst->is_math()) {
1159 break;
1160 }
1161 }
1162 }
1163
1164 /* This doesn't handle saturation on the instruction we
1165 * want to coalesce away if the register types do not match.
1166 * But if scan_inst is a non type-converting 'mov', we can fix
1167 * the types later.
1168 */
1169 if (inst->saturate &&
1170 inst->dst.type != scan_inst->dst.type &&
1171 !(scan_inst->opcode == BRW_OPCODE_MOV &&
1172 scan_inst->dst.type == scan_inst->src[0].type))
1173 break;
1174
1175 /* If we can't handle the swizzle, bail. */
1176 if (!scan_inst->can_reswizzle(devinfo, inst->dst.writemask,
1177 inst->src[0].swizzle,
1178 chans_needed)) {
1179 break;
1180 }
1181
1182 /* This doesn't handle coalescing of multiple registers. */
1183 if (scan_inst->size_written > REG_SIZE)
1184 break;
1185
1186 /* Mark which channels we found unconditional writes for. */
1187 if (!scan_inst->predicate)
1188 chans_remaining &= ~scan_inst->dst.writemask;
1189
1190 if (chans_remaining == 0)
1191 break;
1192 }
1193
1194 /* You can't read from an MRF, so if someone else reads our MRF's
1195 * source GRF that we wanted to rewrite, that stops us. If it's a
1196 * GRF we're trying to coalesce to, we don't actually handle
1197 * rewriting sources so bail in that case as well.
1198 */
1199 bool interfered = false;
1200 for (int i = 0; i < 3; i++) {
1201 if (regions_overlap(inst->src[0], inst->size_read(0),
1202 scan_inst->src[i], scan_inst->size_read(i)))
1203 interfered = true;
1204 }
1205 if (interfered)
1206 break;
1207
1208 /* If somebody else writes the same channels of our destination here,
1209 * we can't coalesce before that.
1210 */
1211 if (regions_overlap(inst->dst, inst->size_written,
1212 scan_inst->dst, scan_inst->size_written) &&
1213 (inst->dst.writemask & scan_inst->dst.writemask) != 0) {
1214 break;
1215 }
1216
1217 /* Check for reads of the register we're trying to coalesce into. We
1218 * can't go rewriting instructions above that to put some other value
1219 * in the register instead.
1220 */
1221 if (to_mrf && scan_inst->mlen > 0) {
1222 if (inst->dst.nr >= scan_inst->base_mrf &&
1223 inst->dst.nr < scan_inst->base_mrf + scan_inst->mlen) {
1224 break;
1225 }
1226 } else {
1227 for (int i = 0; i < 3; i++) {
1228 if (regions_overlap(inst->dst, inst->size_written,
1229 scan_inst->src[i], scan_inst->size_read(i)))
1230 interfered = true;
1231 }
1232 if (interfered)
1233 break;
1234 }
1235 }
1236
1237 if (chans_remaining == 0) {
1238 /* If we've made it here, we have an MOV we want to coalesce out, and
1239 * a scan_inst pointing to the earliest instruction involved in
1240 * computing the value. Now go rewrite the instruction stream
1241 * between the two.
1242 */
1243 vec4_instruction *scan_inst = _scan_inst;
1244 while (scan_inst != inst) {
1245 if (scan_inst->dst.file == VGRF &&
1246 scan_inst->dst.nr == inst->src[0].nr &&
1247 scan_inst->dst.offset / REG_SIZE ==
1248 inst->src[0].offset / REG_SIZE) {
1249 scan_inst->reswizzle(inst->dst.writemask,
1250 inst->src[0].swizzle);
1251 scan_inst->dst.file = inst->dst.file;
1252 scan_inst->dst.nr = inst->dst.nr;
1253 scan_inst->dst.offset = scan_inst->dst.offset % REG_SIZE +
1254 ROUND_DOWN_TO(inst->dst.offset, REG_SIZE);
1255 if (inst->saturate &&
1256 inst->dst.type != scan_inst->dst.type) {
1257 /* If we have reached this point, scan_inst is a non
1258 * type-converting 'mov' and we can modify its register types
1259 * to match the ones in inst. Otherwise, we could have an
1260 * incorrect saturation result.
1261 */
1262 scan_inst->dst.type = inst->dst.type;
1263 scan_inst->src[0].type = inst->src[0].type;
1264 }
1265 scan_inst->saturate |= inst->saturate;
1266 }
1267 scan_inst = (vec4_instruction *)scan_inst->next;
1268 }
1269 inst->remove(block);
1270 progress = true;
1271 }
1272 }
1273
1274 if (progress)
1275 invalidate_live_intervals();
1276
1277 return progress;
1278 }
1279
1280 /**
1281 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
1282 * flow. We could probably do better here with some form of divergence
1283 * analysis.
1284 */
1285 bool
1286 vec4_visitor::eliminate_find_live_channel()
1287 {
1288 bool progress = false;
1289 unsigned depth = 0;
1290
1291 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
1292 switch (inst->opcode) {
1293 case BRW_OPCODE_IF:
1294 case BRW_OPCODE_DO:
1295 depth++;
1296 break;
1297
1298 case BRW_OPCODE_ENDIF:
1299 case BRW_OPCODE_WHILE:
1300 depth--;
1301 break;
1302
1303 case SHADER_OPCODE_FIND_LIVE_CHANNEL:
1304 if (depth == 0) {
1305 inst->opcode = BRW_OPCODE_MOV;
1306 inst->src[0] = brw_imm_d(0);
1307 inst->force_writemask_all = true;
1308 progress = true;
1309 }
1310 break;
1311
1312 default:
1313 break;
1314 }
1315 }
1316
1317 return progress;
1318 }
1319
1320 /**
1321 * Splits virtual GRFs requesting more than one contiguous physical register.
1322 *
1323 * We initially create large virtual GRFs for temporary structures, arrays,
1324 * and matrices, so that the dereference visitor functions can add reg_offsets
1325 * to work their way down to the actual member being accessed. But when it
1326 * comes to optimization, we'd like to treat each register as individual
1327 * storage if possible.
1328 *
1329 * So far, the only thing that might prevent splitting is a send message from
1330 * a GRF on IVB.
1331 */
1332 void
1333 vec4_visitor::split_virtual_grfs()
1334 {
1335 int num_vars = this->alloc.count;
1336 int new_virtual_grf[num_vars];
1337 bool split_grf[num_vars];
1338
1339 memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1340
1341 /* Try to split anything > 0 sized. */
1342 for (int i = 0; i < num_vars; i++) {
1343 split_grf[i] = this->alloc.sizes[i] != 1;
1344 }
1345
1346 /* Check that the instructions are compatible with the registers we're trying
1347 * to split.
1348 */
1349 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1350 if (inst->dst.file == VGRF && regs_written(inst) > 1)
1351 split_grf[inst->dst.nr] = false;
1352
1353 for (int i = 0; i < 3; i++) {
1354 if (inst->src[i].file == VGRF && regs_read(inst, i) > 1)
1355 split_grf[inst->src[i].nr] = false;
1356 }
1357 }
1358
1359 /* Allocate new space for split regs. Note that the virtual
1360 * numbers will be contiguous.
1361 */
1362 for (int i = 0; i < num_vars; i++) {
1363 if (!split_grf[i])
1364 continue;
1365
1366 new_virtual_grf[i] = alloc.allocate(1);
1367 for (unsigned j = 2; j < this->alloc.sizes[i]; j++) {
1368 unsigned reg = alloc.allocate(1);
1369 assert(reg == new_virtual_grf[i] + j - 1);
1370 (void) reg;
1371 }
1372 this->alloc.sizes[i] = 1;
1373 }
1374
1375 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1376 if (inst->dst.file == VGRF && split_grf[inst->dst.nr] &&
1377 inst->dst.offset / REG_SIZE != 0) {
1378 inst->dst.nr = (new_virtual_grf[inst->dst.nr] +
1379 inst->dst.offset / REG_SIZE - 1);
1380 inst->dst.offset %= REG_SIZE;
1381 }
1382 for (int i = 0; i < 3; i++) {
1383 if (inst->src[i].file == VGRF && split_grf[inst->src[i].nr] &&
1384 inst->src[i].offset / REG_SIZE != 0) {
1385 inst->src[i].nr = (new_virtual_grf[inst->src[i].nr] +
1386 inst->src[i].offset / REG_SIZE - 1);
1387 inst->src[i].offset %= REG_SIZE;
1388 }
1389 }
1390 }
1391 invalidate_live_intervals();
1392 }
1393
1394 void
1395 vec4_visitor::dump_instruction(backend_instruction *be_inst)
1396 {
1397 dump_instruction(be_inst, stderr);
1398 }
1399
1400 void
1401 vec4_visitor::dump_instruction(backend_instruction *be_inst, FILE *file)
1402 {
1403 vec4_instruction *inst = (vec4_instruction *)be_inst;
1404
1405 if (inst->predicate) {
1406 fprintf(file, "(%cf0.%d%s) ",
1407 inst->predicate_inverse ? '-' : '+',
1408 inst->flag_subreg,
1409 pred_ctrl_align16[inst->predicate]);
1410 }
1411
1412 fprintf(file, "%s", brw_instruction_name(devinfo, inst->opcode));
1413 if (inst->saturate)
1414 fprintf(file, ".sat");
1415 if (inst->conditional_mod) {
1416 fprintf(file, "%s", conditional_modifier[inst->conditional_mod]);
1417 if (!inst->predicate &&
1418 (devinfo->gen < 5 || (inst->opcode != BRW_OPCODE_SEL &&
1419 inst->opcode != BRW_OPCODE_IF &&
1420 inst->opcode != BRW_OPCODE_WHILE))) {
1421 fprintf(file, ".f0.%d", inst->flag_subreg);
1422 }
1423 }
1424 fprintf(file, " ");
1425
1426 switch (inst->dst.file) {
1427 case VGRF:
1428 fprintf(file, "vgrf%d.%d", inst->dst.nr, inst->dst.offset / REG_SIZE);
1429 break;
1430 case FIXED_GRF:
1431 fprintf(file, "g%d", inst->dst.nr);
1432 break;
1433 case MRF:
1434 fprintf(file, "m%d", inst->dst.nr);
1435 break;
1436 case ARF:
1437 switch (inst->dst.nr) {
1438 case BRW_ARF_NULL:
1439 fprintf(file, "null");
1440 break;
1441 case BRW_ARF_ADDRESS:
1442 fprintf(file, "a0.%d", inst->dst.subnr);
1443 break;
1444 case BRW_ARF_ACCUMULATOR:
1445 fprintf(file, "acc%d", inst->dst.subnr);
1446 break;
1447 case BRW_ARF_FLAG:
1448 fprintf(file, "f%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
1449 break;
1450 default:
1451 fprintf(file, "arf%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
1452 break;
1453 }
1454 if (inst->dst.subnr)
1455 fprintf(file, "+%d", inst->dst.subnr);
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.writemask != WRITEMASK_XYZW) {
1466 fprintf(file, ".");
1467 if (inst->dst.writemask & 1)
1468 fprintf(file, "x");
1469 if (inst->dst.writemask & 2)
1470 fprintf(file, "y");
1471 if (inst->dst.writemask & 4)
1472 fprintf(file, "z");
1473 if (inst->dst.writemask & 8)
1474 fprintf(file, "w");
1475 }
1476 fprintf(file, ":%s", brw_reg_type_letters(inst->dst.type));
1477
1478 if (inst->src[0].file != BAD_FILE)
1479 fprintf(file, ", ");
1480
1481 for (int i = 0; i < 3 && inst->src[i].file != BAD_FILE; i++) {
1482 if (inst->src[i].negate)
1483 fprintf(file, "-");
1484 if (inst->src[i].abs)
1485 fprintf(file, "|");
1486 switch (inst->src[i].file) {
1487 case VGRF:
1488 fprintf(file, "vgrf%d", inst->src[i].nr);
1489 break;
1490 case FIXED_GRF:
1491 fprintf(file, "g%d", inst->src[i].nr);
1492 break;
1493 case ATTR:
1494 fprintf(file, "attr%d", inst->src[i].nr);
1495 break;
1496 case UNIFORM:
1497 fprintf(file, "u%d", inst->src[i].nr);
1498 break;
1499 case IMM:
1500 switch (inst->src[i].type) {
1501 case BRW_REGISTER_TYPE_F:
1502 fprintf(file, "%fF", inst->src[i].f);
1503 break;
1504 case BRW_REGISTER_TYPE_D:
1505 fprintf(file, "%dD", inst->src[i].d);
1506 break;
1507 case BRW_REGISTER_TYPE_UD:
1508 fprintf(file, "%uU", inst->src[i].ud);
1509 break;
1510 case BRW_REGISTER_TYPE_VF:
1511 fprintf(file, "[%-gF, %-gF, %-gF, %-gF]",
1512 brw_vf_to_float((inst->src[i].ud >> 0) & 0xff),
1513 brw_vf_to_float((inst->src[i].ud >> 8) & 0xff),
1514 brw_vf_to_float((inst->src[i].ud >> 16) & 0xff),
1515 brw_vf_to_float((inst->src[i].ud >> 24) & 0xff));
1516 break;
1517 default:
1518 fprintf(file, "???");
1519 break;
1520 }
1521 break;
1522 case ARF:
1523 switch (inst->src[i].nr) {
1524 case BRW_ARF_NULL:
1525 fprintf(file, "null");
1526 break;
1527 case BRW_ARF_ADDRESS:
1528 fprintf(file, "a0.%d", inst->src[i].subnr);
1529 break;
1530 case BRW_ARF_ACCUMULATOR:
1531 fprintf(file, "acc%d", inst->src[i].subnr);
1532 break;
1533 case BRW_ARF_FLAG:
1534 fprintf(file, "f%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
1535 break;
1536 default:
1537 fprintf(file, "arf%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
1538 break;
1539 }
1540 if (inst->src[i].subnr)
1541 fprintf(file, "+%d", inst->src[i].subnr);
1542 break;
1543 case BAD_FILE:
1544 fprintf(file, "(null)");
1545 break;
1546 case MRF:
1547 unreachable("not reached");
1548 }
1549
1550 /* Don't print .0; and only VGRFs have reg_offsets and sizes */
1551 if (inst->src[i].offset / REG_SIZE != 0 &&
1552 inst->src[i].file == VGRF &&
1553 alloc.sizes[inst->src[i].nr] != 1)
1554 fprintf(file, ".%d", inst->src[i].offset / REG_SIZE);
1555
1556 if (inst->src[i].file != IMM) {
1557 static const char *chans[4] = {"x", "y", "z", "w"};
1558 fprintf(file, ".");
1559 for (int c = 0; c < 4; c++) {
1560 fprintf(file, "%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1561 }
1562 }
1563
1564 if (inst->src[i].abs)
1565 fprintf(file, "|");
1566
1567 if (inst->src[i].file != IMM) {
1568 fprintf(file, ":%s", brw_reg_type_letters(inst->src[i].type));
1569 }
1570
1571 if (i < 2 && inst->src[i + 1].file != BAD_FILE)
1572 fprintf(file, ", ");
1573 }
1574
1575 if (inst->force_writemask_all)
1576 fprintf(file, " NoMask");
1577
1578 fprintf(file, "\n");
1579 }
1580
1581
1582 static inline struct brw_reg
1583 attribute_to_hw_reg(int attr, bool interleaved)
1584 {
1585 if (interleaved)
1586 return stride(brw_vec4_grf(attr / 2, (attr % 2) * 4), 0, 4, 1);
1587 else
1588 return brw_vec8_grf(attr, 0);
1589 }
1590
1591
1592 /**
1593 * Replace each register of type ATTR in this->instructions with a reference
1594 * to a fixed HW register.
1595 *
1596 * If interleaved is true, then each attribute takes up half a register, with
1597 * register N containing attribute 2*N in its first half and attribute 2*N+1
1598 * in its second half (this corresponds to the payload setup used by geometry
1599 * shaders in "single" or "dual instanced" dispatch mode). If interleaved is
1600 * false, then each attribute takes up a whole register, with register N
1601 * containing attribute N (this corresponds to the payload setup used by
1602 * vertex shaders, and by geometry shaders in "dual object" dispatch mode).
1603 */
1604 void
1605 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map,
1606 bool interleaved)
1607 {
1608 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1609 for (int i = 0; i < 3; i++) {
1610 if (inst->src[i].file != ATTR)
1611 continue;
1612
1613 int grf = attribute_map[inst->src[i].nr +
1614 inst->src[i].offset / REG_SIZE];
1615
1616 /* All attributes used in the shader need to have been assigned a
1617 * hardware register by the caller
1618 */
1619 assert(grf != 0);
1620
1621 struct brw_reg reg = attribute_to_hw_reg(grf, interleaved);
1622 reg.swizzle = inst->src[i].swizzle;
1623 reg.type = inst->src[i].type;
1624 if (inst->src[i].abs)
1625 reg = brw_abs(reg);
1626 if (inst->src[i].negate)
1627 reg = negate(reg);
1628
1629 inst->src[i] = reg;
1630 }
1631 }
1632 }
1633
1634 int
1635 vec4_vs_visitor::setup_attributes(int payload_reg)
1636 {
1637 int nr_attributes;
1638 int attribute_map[VERT_ATTRIB_MAX + 2];
1639 memset(attribute_map, 0, sizeof(attribute_map));
1640
1641 nr_attributes = 0;
1642 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
1643 if (vs_prog_data->inputs_read & BITFIELD64_BIT(i)) {
1644 attribute_map[i] = payload_reg + nr_attributes;
1645 nr_attributes++;
1646 }
1647 }
1648
1649 /* VertexID is stored by the VF as the last vertex element, but we
1650 * don't represent it with a flag in inputs_read, so we call it
1651 * VERT_ATTRIB_MAX.
1652 */
1653 if (vs_prog_data->uses_vertexid || vs_prog_data->uses_instanceid ||
1654 vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance) {
1655 attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1656 nr_attributes++;
1657 }
1658
1659 if (vs_prog_data->uses_drawid) {
1660 attribute_map[VERT_ATTRIB_MAX + 1] = payload_reg + nr_attributes;
1661 nr_attributes++;
1662 }
1663
1664 lower_attributes_to_hw_regs(attribute_map, false /* interleaved */);
1665
1666 return payload_reg + vs_prog_data->nr_attributes;
1667 }
1668
1669 int
1670 vec4_visitor::setup_uniforms(int reg)
1671 {
1672 prog_data->base.dispatch_grf_start_reg = reg;
1673
1674 /* The pre-gen6 VS requires that some push constants get loaded no
1675 * matter what, or the GPU would hang.
1676 */
1677 if (devinfo->gen < 6 && this->uniforms == 0) {
1678 stage_prog_data->param =
1679 reralloc(NULL, stage_prog_data->param, const gl_constant_value *, 4);
1680 for (unsigned int i = 0; i < 4; i++) {
1681 unsigned int slot = this->uniforms * 4 + i;
1682 static gl_constant_value zero = { 0.0 };
1683 stage_prog_data->param[slot] = &zero;
1684 }
1685
1686 this->uniforms++;
1687 reg++;
1688 } else {
1689 reg += ALIGN(uniforms, 2) / 2;
1690 }
1691
1692 stage_prog_data->nr_params = this->uniforms * 4;
1693
1694 prog_data->base.curb_read_length =
1695 reg - prog_data->base.dispatch_grf_start_reg;
1696
1697 return reg;
1698 }
1699
1700 void
1701 vec4_vs_visitor::setup_payload(void)
1702 {
1703 int reg = 0;
1704
1705 /* The payload always contains important data in g0, which contains
1706 * the URB handles that are passed on to the URB write at the end
1707 * of the thread. So, we always start push constants at g1.
1708 */
1709 reg++;
1710
1711 reg = setup_uniforms(reg);
1712
1713 reg = setup_attributes(reg);
1714
1715 this->first_non_payload_grf = reg;
1716 }
1717
1718 bool
1719 vec4_visitor::lower_minmax()
1720 {
1721 assert(devinfo->gen < 6);
1722
1723 bool progress = false;
1724
1725 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
1726 const vec4_builder ibld(this, block, inst);
1727
1728 if (inst->opcode == BRW_OPCODE_SEL &&
1729 inst->predicate == BRW_PREDICATE_NONE) {
1730 /* FIXME: Using CMP doesn't preserve the NaN propagation semantics of
1731 * the original SEL.L/GE instruction
1732 */
1733 ibld.CMP(ibld.null_reg_d(), inst->src[0], inst->src[1],
1734 inst->conditional_mod);
1735 inst->predicate = BRW_PREDICATE_NORMAL;
1736 inst->conditional_mod = BRW_CONDITIONAL_NONE;
1737
1738 progress = true;
1739 }
1740 }
1741
1742 if (progress)
1743 invalidate_live_intervals();
1744
1745 return progress;
1746 }
1747
1748 src_reg
1749 vec4_visitor::get_timestamp()
1750 {
1751 assert(devinfo->gen >= 7);
1752
1753 src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1754 BRW_ARF_TIMESTAMP,
1755 0,
1756 0,
1757 0,
1758 BRW_REGISTER_TYPE_UD,
1759 BRW_VERTICAL_STRIDE_0,
1760 BRW_WIDTH_4,
1761 BRW_HORIZONTAL_STRIDE_4,
1762 BRW_SWIZZLE_XYZW,
1763 WRITEMASK_XYZW));
1764
1765 dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1766
1767 vec4_instruction *mov = emit(MOV(dst, ts));
1768 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1769 * even if it's not enabled in the dispatch.
1770 */
1771 mov->force_writemask_all = true;
1772
1773 return src_reg(dst);
1774 }
1775
1776 void
1777 vec4_visitor::emit_shader_time_begin()
1778 {
1779 current_annotation = "shader time start";
1780 shader_start_time = get_timestamp();
1781 }
1782
1783 void
1784 vec4_visitor::emit_shader_time_end()
1785 {
1786 current_annotation = "shader time end";
1787 src_reg shader_end_time = get_timestamp();
1788
1789
1790 /* Check that there weren't any timestamp reset events (assuming these
1791 * were the only two timestamp reads that happened).
1792 */
1793 src_reg reset_end = shader_end_time;
1794 reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1795 vec4_instruction *test = emit(AND(dst_null_ud(), reset_end, brw_imm_ud(1u)));
1796 test->conditional_mod = BRW_CONDITIONAL_Z;
1797
1798 emit(IF(BRW_PREDICATE_NORMAL));
1799
1800 /* Take the current timestamp and get the delta. */
1801 shader_start_time.negate = true;
1802 dst_reg diff = dst_reg(this, glsl_type::uint_type);
1803 emit(ADD(diff, shader_start_time, shader_end_time));
1804
1805 /* If there were no instructions between the two timestamp gets, the diff
1806 * is 2 cycles. Remove that overhead, so I can forget about that when
1807 * trying to determine the time taken for single instructions.
1808 */
1809 emit(ADD(diff, src_reg(diff), brw_imm_ud(-2u)));
1810
1811 emit_shader_time_write(0, src_reg(diff));
1812 emit_shader_time_write(1, brw_imm_ud(1u));
1813 emit(BRW_OPCODE_ELSE);
1814 emit_shader_time_write(2, brw_imm_ud(1u));
1815 emit(BRW_OPCODE_ENDIF);
1816 }
1817
1818 void
1819 vec4_visitor::emit_shader_time_write(int shader_time_subindex, src_reg value)
1820 {
1821 dst_reg dst =
1822 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1823
1824 dst_reg offset = dst;
1825 dst_reg time = dst;
1826 time.offset += REG_SIZE;
1827
1828 offset.type = BRW_REGISTER_TYPE_UD;
1829 int index = shader_time_index * 3 + shader_time_subindex;
1830 emit(MOV(offset, brw_imm_d(index * SHADER_TIME_STRIDE)));
1831
1832 time.type = BRW_REGISTER_TYPE_UD;
1833 emit(MOV(time, value));
1834
1835 vec4_instruction *inst =
1836 emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1837 inst->mlen = 2;
1838 }
1839
1840 void
1841 vec4_visitor::convert_to_hw_regs()
1842 {
1843 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1844 for (int i = 0; i < 3; i++) {
1845 struct src_reg &src = inst->src[i];
1846 struct brw_reg reg;
1847 switch (src.file) {
1848 case VGRF:
1849 reg = brw_vec8_grf(src.nr + src.offset / REG_SIZE, 0);
1850 reg.type = src.type;
1851 reg.swizzle = src.swizzle;
1852 reg.abs = src.abs;
1853 reg.negate = src.negate;
1854 break;
1855
1856 case UNIFORM:
1857 reg = stride(brw_vec4_grf(prog_data->base.dispatch_grf_start_reg +
1858 (src.nr + src.offset / 16) / 2,
1859 ((src.nr + src.offset / 16) % 2) * 4),
1860 0, 4, 1);
1861 reg.type = src.type;
1862 reg.swizzle = src.swizzle;
1863 reg.abs = src.abs;
1864 reg.negate = src.negate;
1865
1866 /* This should have been moved to pull constants. */
1867 assert(!src.reladdr);
1868 break;
1869
1870 case ARF:
1871 case FIXED_GRF:
1872 case IMM:
1873 continue;
1874
1875 case BAD_FILE:
1876 /* Probably unused. */
1877 reg = brw_null_reg();
1878 break;
1879
1880 case MRF:
1881 case ATTR:
1882 unreachable("not reached");
1883 }
1884
1885 src = reg;
1886 }
1887
1888 if (inst->is_3src(devinfo)) {
1889 /* 3-src instructions with scalar sources support arbitrary subnr,
1890 * but don't actually use swizzles. Convert swizzle into subnr.
1891 */
1892 for (int i = 0; i < 3; i++) {
1893 if (inst->src[i].vstride == BRW_VERTICAL_STRIDE_0) {
1894 assert(brw_is_single_value_swizzle(inst->src[i].swizzle));
1895 inst->src[i].subnr += 4 * BRW_GET_SWZ(inst->src[i].swizzle, 0);
1896 }
1897 }
1898 }
1899
1900 dst_reg &dst = inst->dst;
1901 struct brw_reg reg;
1902
1903 switch (inst->dst.file) {
1904 case VGRF:
1905 reg = brw_vec8_grf(dst.nr + dst.offset / REG_SIZE, 0);
1906 reg.type = dst.type;
1907 reg.writemask = dst.writemask;
1908 break;
1909
1910 case MRF:
1911 assert(((dst.nr + dst.offset / REG_SIZE) & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(devinfo->gen));
1912 reg = brw_message_reg(dst.nr + dst.offset / REG_SIZE);
1913 reg.type = dst.type;
1914 reg.writemask = dst.writemask;
1915 break;
1916
1917 case ARF:
1918 case FIXED_GRF:
1919 reg = dst.as_brw_reg();
1920 break;
1921
1922 case BAD_FILE:
1923 reg = brw_null_reg();
1924 break;
1925
1926 case IMM:
1927 case ATTR:
1928 case UNIFORM:
1929 unreachable("not reached");
1930 }
1931
1932 dst = reg;
1933 }
1934 }
1935
1936 bool
1937 vec4_visitor::run()
1938 {
1939 if (shader_time_index >= 0)
1940 emit_shader_time_begin();
1941
1942 emit_prolog();
1943
1944 emit_nir_code();
1945 if (failed)
1946 return false;
1947 base_ir = NULL;
1948
1949 emit_thread_end();
1950
1951 calculate_cfg();
1952
1953 /* Before any optimization, push array accesses out to scratch
1954 * space where we need them to be. This pass may allocate new
1955 * virtual GRFs, so we want to do it early. It also makes sure
1956 * that we have reladdr computations available for CSE, since we'll
1957 * often do repeated subexpressions for those.
1958 */
1959 move_grf_array_access_to_scratch();
1960 move_uniform_array_access_to_pull_constants();
1961
1962 pack_uniform_registers();
1963 move_push_constants_to_pull_constants();
1964 split_virtual_grfs();
1965
1966 #define OPT(pass, args...) ({ \
1967 pass_num++; \
1968 bool this_progress = pass(args); \
1969 \
1970 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
1971 char filename[64]; \
1972 snprintf(filename, 64, "%s-%s-%02d-%02d-" #pass, \
1973 stage_abbrev, nir->info.name, iteration, pass_num); \
1974 \
1975 backend_shader::dump_instructions(filename); \
1976 } \
1977 \
1978 progress = progress || this_progress; \
1979 this_progress; \
1980 })
1981
1982
1983 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER)) {
1984 char filename[64];
1985 snprintf(filename, 64, "%s-%s-00-00-start",
1986 stage_abbrev, nir->info.name);
1987
1988 backend_shader::dump_instructions(filename);
1989 }
1990
1991 bool progress;
1992 int iteration = 0;
1993 int pass_num = 0;
1994 do {
1995 progress = false;
1996 pass_num = 0;
1997 iteration++;
1998
1999 OPT(opt_predicated_break, this);
2000 OPT(opt_reduce_swizzle);
2001 OPT(dead_code_eliminate);
2002 OPT(dead_control_flow_eliminate, this);
2003 OPT(opt_copy_propagation);
2004 OPT(opt_cmod_propagation);
2005 OPT(opt_cse);
2006 OPT(opt_algebraic);
2007 OPT(opt_register_coalesce);
2008 OPT(eliminate_find_live_channel);
2009 } while (progress);
2010
2011 pass_num = 0;
2012
2013 if (OPT(opt_vector_float)) {
2014 OPT(opt_cse);
2015 OPT(opt_copy_propagation, false);
2016 OPT(opt_copy_propagation, true);
2017 OPT(dead_code_eliminate);
2018 }
2019
2020 if (devinfo->gen <= 5 && OPT(lower_minmax)) {
2021 OPT(opt_cmod_propagation);
2022 OPT(opt_cse);
2023 OPT(opt_copy_propagation);
2024 OPT(dead_code_eliminate);
2025 }
2026
2027 if (failed)
2028 return false;
2029
2030 setup_payload();
2031
2032 if (unlikely(INTEL_DEBUG & DEBUG_SPILL_VEC4)) {
2033 /* Debug of register spilling: Go spill everything. */
2034 const int grf_count = alloc.count;
2035 float spill_costs[alloc.count];
2036 bool no_spill[alloc.count];
2037 evaluate_spill_costs(spill_costs, no_spill);
2038 for (int i = 0; i < grf_count; i++) {
2039 if (no_spill[i])
2040 continue;
2041 spill_reg(i);
2042 }
2043 }
2044
2045 bool allocated_without_spills = reg_allocate();
2046
2047 if (!allocated_without_spills) {
2048 compiler->shader_perf_log(log_data,
2049 "%s shader triggered register spilling. "
2050 "Try reducing the number of live vec4 values "
2051 "to improve performance.\n",
2052 stage_name);
2053
2054 while (!reg_allocate()) {
2055 if (failed)
2056 return false;
2057 }
2058 }
2059
2060 opt_schedule_instructions();
2061
2062 opt_set_dependency_control();
2063
2064 convert_to_hw_regs();
2065
2066 if (last_scratch > 0) {
2067 prog_data->base.total_scratch =
2068 brw_get_scratch_size(last_scratch * REG_SIZE);
2069 }
2070
2071 return !failed;
2072 }
2073
2074 } /* namespace brw */
2075
2076 extern "C" {
2077
2078 /**
2079 * Compile a vertex shader.
2080 *
2081 * Returns the final assembly and the program's size.
2082 */
2083 const unsigned *
2084 brw_compile_vs(const struct brw_compiler *compiler, void *log_data,
2085 void *mem_ctx,
2086 const struct brw_vs_prog_key *key,
2087 struct brw_vs_prog_data *prog_data,
2088 const nir_shader *src_shader,
2089 gl_clip_plane *clip_planes,
2090 bool use_legacy_snorm_formula,
2091 int shader_time_index,
2092 unsigned *final_assembly_size,
2093 char **error_str)
2094 {
2095 const bool is_scalar = compiler->scalar_stage[MESA_SHADER_VERTEX];
2096 nir_shader *shader = nir_shader_clone(mem_ctx, src_shader);
2097 shader = brw_nir_apply_sampler_key(shader, compiler->devinfo, &key->tex,
2098 is_scalar);
2099 brw_nir_lower_vs_inputs(shader, compiler->devinfo, is_scalar,
2100 use_legacy_snorm_formula, key->gl_attrib_wa_flags);
2101 brw_nir_lower_vue_outputs(shader, is_scalar);
2102 shader = brw_postprocess_nir(shader, compiler->devinfo, is_scalar);
2103
2104 const unsigned *assembly = NULL;
2105
2106 unsigned nr_attributes = _mesa_bitcount_64(prog_data->inputs_read);
2107
2108 /* gl_VertexID and gl_InstanceID are system values, but arrive via an
2109 * incoming vertex attribute. So, add an extra slot.
2110 */
2111 if (shader->info.system_values_read &
2112 (BITFIELD64_BIT(SYSTEM_VALUE_BASE_VERTEX) |
2113 BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE) |
2114 BITFIELD64_BIT(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) |
2115 BITFIELD64_BIT(SYSTEM_VALUE_INSTANCE_ID))) {
2116 nr_attributes++;
2117 }
2118
2119 /* gl_DrawID has its very own vec4 */
2120 if (shader->info.system_values_read & BITFIELD64_BIT(SYSTEM_VALUE_DRAW_ID)) {
2121 nr_attributes++;
2122 }
2123
2124 unsigned nr_attribute_slots =
2125 nr_attributes +
2126 _mesa_bitcount_64(shader->info.double_inputs_read);
2127
2128 /* The 3DSTATE_VS documentation lists the lower bound on "Vertex URB Entry
2129 * Read Length" as 1 in vec4 mode, and 0 in SIMD8 mode. Empirically, in
2130 * vec4 mode, the hardware appears to wedge unless we read something.
2131 */
2132 if (is_scalar)
2133 prog_data->base.urb_read_length =
2134 DIV_ROUND_UP(nr_attribute_slots, 2);
2135 else
2136 prog_data->base.urb_read_length =
2137 DIV_ROUND_UP(MAX2(nr_attribute_slots, 1), 2);
2138
2139 prog_data->nr_attributes = nr_attributes;
2140 prog_data->nr_attribute_slots = nr_attribute_slots;
2141
2142 /* Since vertex shaders reuse the same VUE entry for inputs and outputs
2143 * (overwriting the original contents), we need to make sure the size is
2144 * the larger of the two.
2145 */
2146 const unsigned vue_entries =
2147 MAX2(nr_attribute_slots, (unsigned)prog_data->base.vue_map.num_slots);
2148
2149 if (compiler->devinfo->gen == 6)
2150 prog_data->base.urb_entry_size = DIV_ROUND_UP(vue_entries, 8);
2151 else
2152 prog_data->base.urb_entry_size = DIV_ROUND_UP(vue_entries, 4);
2153
2154 if (is_scalar) {
2155 prog_data->base.dispatch_mode = DISPATCH_MODE_SIMD8;
2156
2157 fs_visitor v(compiler, log_data, mem_ctx, key, &prog_data->base.base,
2158 NULL, /* prog; Only used for TEXTURE_RECTANGLE on gen < 8 */
2159 shader, 8, shader_time_index);
2160 if (!v.run_vs(clip_planes)) {
2161 if (error_str)
2162 *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
2163
2164 return NULL;
2165 }
2166
2167 prog_data->base.base.dispatch_grf_start_reg = v.payload.num_regs;
2168
2169 fs_generator g(compiler, log_data, mem_ctx, (void *) key,
2170 &prog_data->base.base, v.promoted_constants,
2171 v.runtime_check_aads_emit, MESA_SHADER_VERTEX);
2172 if (INTEL_DEBUG & DEBUG_VS) {
2173 const char *debug_name =
2174 ralloc_asprintf(mem_ctx, "%s vertex shader %s",
2175 shader->info.label ? shader->info.label : "unnamed",
2176 shader->info.name);
2177
2178 g.enable_debug(debug_name);
2179 }
2180 g.generate_code(v.cfg, 8);
2181 assembly = g.get_assembly(final_assembly_size);
2182 }
2183
2184 if (!assembly) {
2185 prog_data->base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT;
2186
2187 vec4_vs_visitor v(compiler, log_data, key, prog_data,
2188 shader, clip_planes, mem_ctx,
2189 shader_time_index, use_legacy_snorm_formula);
2190 if (!v.run()) {
2191 if (error_str)
2192 *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
2193
2194 return NULL;
2195 }
2196
2197 assembly = brw_vec4_generate_assembly(compiler, log_data, mem_ctx,
2198 shader, &prog_data->base, v.cfg,
2199 final_assembly_size);
2200 }
2201
2202 return assembly;
2203 }
2204
2205 } /* extern "C" */