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