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