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