i965/vec4: split VEC4_OPCODE_FROM_DOUBLE into one opcode per destination's type
[mesa.git] / src / intel / compiler / 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_nir.h"
28 #include "brw_vec4_builder.h"
29 #include "brw_vec4_live_variables.h"
30 #include "brw_vec4_vs.h"
31 #include "brw_dead_control_flow.h"
32 #include "common/gen_debug.h"
33 #include "program/prog_parameter.h"
34
35 #define MAX_INSTRUCTION (1 << 30)
36
37 using namespace brw;
38
39 namespace brw {
40
41 void
42 src_reg::init()
43 {
44 memset(this, 0, sizeof(*this));
45
46 this->file = BAD_FILE;
47 }
48
49 src_reg::src_reg(enum brw_reg_file file, int nr, const glsl_type *type)
50 {
51 init();
52
53 this->file = file;
54 this->nr = nr;
55 if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
56 this->swizzle = brw_swizzle_for_size(type->vector_elements);
57 else
58 this->swizzle = BRW_SWIZZLE_XYZW;
59 if (type)
60 this->type = brw_type_for_base_type(type);
61 }
62
63 /** Generic unset register constructor. */
64 src_reg::src_reg()
65 {
66 init();
67 }
68
69 src_reg::src_reg(struct ::brw_reg reg) :
70 backend_reg(reg)
71 {
72 this->offset = 0;
73 this->reladdr = NULL;
74 }
75
76 src_reg::src_reg(const dst_reg &reg) :
77 backend_reg(reg)
78 {
79 this->reladdr = reg.reladdr;
80 this->swizzle = brw_swizzle_for_mask(reg.writemask);
81 }
82
83 void
84 dst_reg::init()
85 {
86 memset(this, 0, sizeof(*this));
87 this->file = BAD_FILE;
88 this->writemask = WRITEMASK_XYZW;
89 }
90
91 dst_reg::dst_reg()
92 {
93 init();
94 }
95
96 dst_reg::dst_reg(enum brw_reg_file file, int nr)
97 {
98 init();
99
100 this->file = file;
101 this->nr = nr;
102 }
103
104 dst_reg::dst_reg(enum brw_reg_file file, int nr, const glsl_type *type,
105 unsigned writemask)
106 {
107 init();
108
109 this->file = file;
110 this->nr = nr;
111 this->type = brw_type_for_base_type(type);
112 this->writemask = writemask;
113 }
114
115 dst_reg::dst_reg(enum brw_reg_file file, int nr, brw_reg_type type,
116 unsigned writemask)
117 {
118 init();
119
120 this->file = file;
121 this->nr = nr;
122 this->type = type;
123 this->writemask = writemask;
124 }
125
126 dst_reg::dst_reg(struct ::brw_reg reg) :
127 backend_reg(reg)
128 {
129 this->offset = 0;
130 this->reladdr = NULL;
131 }
132
133 dst_reg::dst_reg(const src_reg &reg) :
134 backend_reg(reg)
135 {
136 this->writemask = brw_mask_for_swizzle(reg.swizzle);
137 this->reladdr = reg.reladdr;
138 }
139
140 bool
141 dst_reg::equals(const dst_reg &r) const
142 {
143 return (this->backend_reg::equals(r) &&
144 (reladdr == r.reladdr ||
145 (reladdr && r.reladdr && reladdr->equals(*r.reladdr))));
146 }
147
148 bool
149 vec4_instruction::is_send_from_grf()
150 {
151 switch (opcode) {
152 case SHADER_OPCODE_SHADER_TIME_ADD:
153 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
154 case SHADER_OPCODE_UNTYPED_ATOMIC:
155 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
156 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
157 case SHADER_OPCODE_TYPED_ATOMIC:
158 case SHADER_OPCODE_TYPED_SURFACE_READ:
159 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
160 case VEC4_OPCODE_URB_READ:
161 case TCS_OPCODE_URB_WRITE:
162 case TCS_OPCODE_RELEASE_INPUT:
163 case SHADER_OPCODE_BARRIER:
164 return true;
165 default:
166 return false;
167 }
168 }
169
170 /**
171 * Returns true if this instruction's sources and destinations cannot
172 * safely be the same register.
173 *
174 * In most cases, a register can be written over safely by the same
175 * instruction that is its last use. For a single instruction, the
176 * sources are dereferenced before writing of the destination starts
177 * (naturally).
178 *
179 * However, there are a few cases where this can be problematic:
180 *
181 * - Virtual opcodes that translate to multiple instructions in the
182 * code generator: if src == dst and one instruction writes the
183 * destination before a later instruction reads the source, then
184 * src will have been clobbered.
185 *
186 * The register allocator uses this information to set up conflicts between
187 * GRF sources and the destination.
188 */
189 bool
190 vec4_instruction::has_source_and_destination_hazard() const
191 {
192 switch (opcode) {
193 case TCS_OPCODE_SET_INPUT_URB_OFFSETS:
194 case TCS_OPCODE_SET_OUTPUT_URB_OFFSETS:
195 case TES_OPCODE_ADD_INDIRECT_URB_OFFSET:
196 return true;
197 default:
198 /* 8-wide compressed DF operations are executed as two 4-wide operations,
199 * so we have a src/dst hazard if the first half of the instruction
200 * overwrites the source of the second half. Prevent this by marking
201 * compressed instructions as having src/dst hazards, so the register
202 * allocator assigns safe register regions for dst and srcs.
203 */
204 return size_written > REG_SIZE;
205 }
206 }
207
208 unsigned
209 vec4_instruction::size_read(unsigned arg) const
210 {
211 switch (opcode) {
212 case SHADER_OPCODE_SHADER_TIME_ADD:
213 case SHADER_OPCODE_UNTYPED_ATOMIC:
214 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
215 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE:
216 case SHADER_OPCODE_TYPED_ATOMIC:
217 case SHADER_OPCODE_TYPED_SURFACE_READ:
218 case SHADER_OPCODE_TYPED_SURFACE_WRITE:
219 case TCS_OPCODE_URB_WRITE:
220 if (arg == 0)
221 return mlen * REG_SIZE;
222 break;
223 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
224 if (arg == 1)
225 return mlen * REG_SIZE;
226 break;
227 default:
228 break;
229 }
230
231 switch (src[arg].file) {
232 case BAD_FILE:
233 return 0;
234 case IMM:
235 case UNIFORM:
236 return 4 * type_sz(src[arg].type);
237 default:
238 /* XXX - Represent actual vertical stride. */
239 return exec_size * type_sz(src[arg].type);
240 }
241 }
242
243 bool
244 vec4_instruction::can_do_source_mods(const struct gen_device_info *devinfo)
245 {
246 if (devinfo->gen == 6 && is_math())
247 return false;
248
249 if (is_send_from_grf())
250 return false;
251
252 if (!backend_instruction::can_do_source_mods())
253 return false;
254
255 return true;
256 }
257
258 bool
259 vec4_instruction::can_do_writemask(const struct gen_device_info *devinfo)
260 {
261 switch (opcode) {
262 case SHADER_OPCODE_GEN4_SCRATCH_READ:
263 case VEC4_OPCODE_DOUBLE_TO_F32:
264 case VEC4_OPCODE_DOUBLE_TO_D32:
265 case VEC4_OPCODE_DOUBLE_TO_U32:
266 case VEC4_OPCODE_TO_DOUBLE:
267 case VEC4_OPCODE_PICK_LOW_32BIT:
268 case VEC4_OPCODE_PICK_HIGH_32BIT:
269 case VEC4_OPCODE_SET_LOW_32BIT:
270 case VEC4_OPCODE_SET_HIGH_32BIT:
271 case VS_OPCODE_PULL_CONSTANT_LOAD:
272 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
273 case VS_OPCODE_SET_SIMD4X2_HEADER_GEN9:
274 case TCS_OPCODE_SET_INPUT_URB_OFFSETS:
275 case TCS_OPCODE_SET_OUTPUT_URB_OFFSETS:
276 case TES_OPCODE_CREATE_INPUT_READ_HEADER:
277 case TES_OPCODE_ADD_INDIRECT_URB_OFFSET:
278 case VEC4_OPCODE_URB_READ:
279 case SHADER_OPCODE_MOV_INDIRECT:
280 return false;
281 default:
282 /* The MATH instruction on Gen6 only executes in align1 mode, which does
283 * not support writemasking.
284 */
285 if (devinfo->gen == 6 && is_math())
286 return false;
287
288 if (is_tex())
289 return false;
290
291 return true;
292 }
293 }
294
295 bool
296 vec4_instruction::can_change_types() const
297 {
298 return dst.type == src[0].type &&
299 !src[0].abs && !src[0].negate && !saturate &&
300 (opcode == BRW_OPCODE_MOV ||
301 (opcode == BRW_OPCODE_SEL &&
302 dst.type == src[1].type &&
303 predicate != BRW_PREDICATE_NONE &&
304 !src[1].abs && !src[1].negate));
305 }
306
307 /**
308 * Returns how many MRFs an opcode will write over.
309 *
310 * Note that this is not the 0 or 1 implied writes in an actual gen
311 * instruction -- the generate_* functions generate additional MOVs
312 * for setup.
313 */
314 int
315 vec4_visitor::implied_mrf_writes(vec4_instruction *inst)
316 {
317 if (inst->mlen == 0 || inst->is_send_from_grf())
318 return 0;
319
320 switch (inst->opcode) {
321 case SHADER_OPCODE_RCP:
322 case SHADER_OPCODE_RSQ:
323 case SHADER_OPCODE_SQRT:
324 case SHADER_OPCODE_EXP2:
325 case SHADER_OPCODE_LOG2:
326 case SHADER_OPCODE_SIN:
327 case SHADER_OPCODE_COS:
328 return 1;
329 case SHADER_OPCODE_INT_QUOTIENT:
330 case SHADER_OPCODE_INT_REMAINDER:
331 case SHADER_OPCODE_POW:
332 case TCS_OPCODE_THREAD_END:
333 return 2;
334 case VS_OPCODE_URB_WRITE:
335 return 1;
336 case VS_OPCODE_PULL_CONSTANT_LOAD:
337 return 2;
338 case SHADER_OPCODE_GEN4_SCRATCH_READ:
339 return 2;
340 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
341 return 3;
342 case GS_OPCODE_URB_WRITE:
343 case GS_OPCODE_URB_WRITE_ALLOCATE:
344 case GS_OPCODE_THREAD_END:
345 return 0;
346 case GS_OPCODE_FF_SYNC:
347 return 1;
348 case TCS_OPCODE_URB_WRITE:
349 return 0;
350 case SHADER_OPCODE_SHADER_TIME_ADD:
351 return 0;
352 case SHADER_OPCODE_TEX:
353 case SHADER_OPCODE_TXL:
354 case SHADER_OPCODE_TXD:
355 case SHADER_OPCODE_TXF:
356 case SHADER_OPCODE_TXF_CMS:
357 case SHADER_OPCODE_TXF_CMS_W:
358 case SHADER_OPCODE_TXF_MCS:
359 case SHADER_OPCODE_TXS:
360 case SHADER_OPCODE_TG4:
361 case SHADER_OPCODE_TG4_OFFSET:
362 case SHADER_OPCODE_SAMPLEINFO:
363 case VS_OPCODE_GET_BUFFER_SIZE:
364 return inst->header_size;
365 default:
366 unreachable("not reached");
367 }
368 }
369
370 bool
371 src_reg::equals(const src_reg &r) const
372 {
373 return (this->backend_reg::equals(r) &&
374 !reladdr && !r.reladdr);
375 }
376
377 bool
378 vec4_visitor::opt_vector_float()
379 {
380 bool progress = false;
381
382 foreach_block(block, cfg) {
383 int last_reg = -1, last_offset = -1;
384 enum brw_reg_file last_reg_file = BAD_FILE;
385
386 uint8_t imm[4] = { 0 };
387 int inst_count = 0;
388 vec4_instruction *imm_inst[4];
389 unsigned writemask = 0;
390 enum brw_reg_type dest_type = BRW_REGISTER_TYPE_F;
391
392 foreach_inst_in_block_safe(vec4_instruction, inst, block) {
393 int vf = -1;
394 enum brw_reg_type need_type;
395
396 /* Look for unconditional MOVs from an immediate with a partial
397 * writemask. Skip type-conversion MOVs other than integer 0,
398 * where the type doesn't matter. See if the immediate can be
399 * represented as a VF.
400 */
401 if (inst->opcode == BRW_OPCODE_MOV &&
402 inst->src[0].file == IMM &&
403 inst->predicate == BRW_PREDICATE_NONE &&
404 inst->dst.writemask != WRITEMASK_XYZW &&
405 type_sz(inst->src[0].type) < 8 &&
406 (inst->src[0].type == inst->dst.type || inst->src[0].d == 0)) {
407
408 vf = brw_float_to_vf(inst->src[0].d);
409 need_type = BRW_REGISTER_TYPE_D;
410
411 if (vf == -1) {
412 vf = brw_float_to_vf(inst->src[0].f);
413 need_type = BRW_REGISTER_TYPE_F;
414 }
415 } else {
416 last_reg = -1;
417 }
418
419 /* If this wasn't a MOV, or the destination register doesn't match,
420 * or we have to switch destination types, then this breaks our
421 * sequence. Combine anything we've accumulated so far.
422 */
423 if (last_reg != inst->dst.nr ||
424 last_offset != inst->dst.offset ||
425 last_reg_file != inst->dst.file ||
426 (vf > 0 && dest_type != need_type)) {
427
428 if (inst_count > 1) {
429 unsigned vf;
430 memcpy(&vf, imm, sizeof(vf));
431 vec4_instruction *mov = MOV(imm_inst[0]->dst, brw_imm_vf(vf));
432 mov->dst.type = dest_type;
433 mov->dst.writemask = writemask;
434 inst->insert_before(block, mov);
435
436 for (int i = 0; i < inst_count; i++) {
437 imm_inst[i]->remove(block);
438 }
439
440 progress = true;
441 }
442
443 inst_count = 0;
444 last_reg = -1;
445 writemask = 0;
446 dest_type = BRW_REGISTER_TYPE_F;
447
448 for (int i = 0; i < 4; i++) {
449 imm[i] = 0;
450 }
451 }
452
453 /* Record this instruction's value (if it was representable). */
454 if (vf != -1) {
455 if ((inst->dst.writemask & WRITEMASK_X) != 0)
456 imm[0] = vf;
457 if ((inst->dst.writemask & WRITEMASK_Y) != 0)
458 imm[1] = vf;
459 if ((inst->dst.writemask & WRITEMASK_Z) != 0)
460 imm[2] = vf;
461 if ((inst->dst.writemask & WRITEMASK_W) != 0)
462 imm[3] = vf;
463
464 writemask |= inst->dst.writemask;
465 imm_inst[inst_count++] = inst;
466
467 last_reg = inst->dst.nr;
468 last_offset = inst->dst.offset;
469 last_reg_file = inst->dst.file;
470 if (vf > 0)
471 dest_type = need_type;
472 }
473 }
474 }
475
476 if (progress)
477 invalidate_live_intervals();
478
479 return progress;
480 }
481
482 /* Replaces unused channels of a swizzle with channels that are used.
483 *
484 * For instance, this pass transforms
485 *
486 * mov vgrf4.yz, vgrf5.wxzy
487 *
488 * into
489 *
490 * mov vgrf4.yz, vgrf5.xxzx
491 *
492 * This eliminates false uses of some channels, letting dead code elimination
493 * remove the instructions that wrote them.
494 */
495 bool
496 vec4_visitor::opt_reduce_swizzle()
497 {
498 bool progress = false;
499
500 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
501 if (inst->dst.file == BAD_FILE ||
502 inst->dst.file == ARF ||
503 inst->dst.file == FIXED_GRF ||
504 inst->is_send_from_grf())
505 continue;
506
507 unsigned swizzle;
508
509 /* Determine which channels of the sources are read. */
510 switch (inst->opcode) {
511 case VEC4_OPCODE_PACK_BYTES:
512 case BRW_OPCODE_DP4:
513 case BRW_OPCODE_DPH: /* FINISHME: DPH reads only three channels of src0,
514 * but all four of src1.
515 */
516 swizzle = brw_swizzle_for_size(4);
517 break;
518 case BRW_OPCODE_DP3:
519 swizzle = brw_swizzle_for_size(3);
520 break;
521 case BRW_OPCODE_DP2:
522 swizzle = brw_swizzle_for_size(2);
523 break;
524
525 case VEC4_OPCODE_TO_DOUBLE:
526 case VEC4_OPCODE_DOUBLE_TO_F32:
527 case VEC4_OPCODE_DOUBLE_TO_D32:
528 case VEC4_OPCODE_DOUBLE_TO_U32:
529 case VEC4_OPCODE_PICK_LOW_32BIT:
530 case VEC4_OPCODE_PICK_HIGH_32BIT:
531 case VEC4_OPCODE_SET_LOW_32BIT:
532 case VEC4_OPCODE_SET_HIGH_32BIT:
533 swizzle = brw_swizzle_for_size(4);
534 break;
535
536 default:
537 swizzle = brw_swizzle_for_mask(inst->dst.writemask);
538 break;
539 }
540
541 /* Update sources' swizzles. */
542 for (int i = 0; i < 3; i++) {
543 if (inst->src[i].file != VGRF &&
544 inst->src[i].file != ATTR &&
545 inst->src[i].file != UNIFORM)
546 continue;
547
548 const unsigned new_swizzle =
549 brw_compose_swizzle(swizzle, inst->src[i].swizzle);
550 if (inst->src[i].swizzle != new_swizzle) {
551 inst->src[i].swizzle = new_swizzle;
552 progress = true;
553 }
554 }
555 }
556
557 if (progress)
558 invalidate_live_intervals();
559
560 return progress;
561 }
562
563 void
564 vec4_visitor::split_uniform_registers()
565 {
566 /* Prior to this, uniforms have been in an array sized according to
567 * the number of vector uniforms present, sparsely filled (so an
568 * aggregate results in reg indices being skipped over). Now we're
569 * going to cut those aggregates up so each .nr index is one
570 * vector. The goal is to make elimination of unused uniform
571 * components easier later.
572 */
573 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
574 for (int i = 0 ; i < 3; i++) {
575 if (inst->src[i].file != UNIFORM)
576 continue;
577
578 assert(!inst->src[i].reladdr);
579
580 inst->src[i].nr += inst->src[i].offset / 16;
581 inst->src[i].offset %= 16;
582 }
583 }
584 }
585
586 void
587 vec4_visitor::pack_uniform_registers()
588 {
589 uint8_t chans_used[this->uniforms];
590 int new_loc[this->uniforms];
591 int new_chan[this->uniforms];
592
593 memset(chans_used, 0, sizeof(chans_used));
594 memset(new_loc, 0, sizeof(new_loc));
595 memset(new_chan, 0, sizeof(new_chan));
596
597 /* Find which uniform vectors are actually used by the program. We
598 * expect unused vector elements when we've moved array access out
599 * to pull constants, and from some GLSL code generators like wine.
600 */
601 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
602 unsigned readmask;
603 switch (inst->opcode) {
604 case VEC4_OPCODE_PACK_BYTES:
605 case BRW_OPCODE_DP4:
606 case BRW_OPCODE_DPH:
607 readmask = 0xf;
608 break;
609 case BRW_OPCODE_DP3:
610 readmask = 0x7;
611 break;
612 case BRW_OPCODE_DP2:
613 readmask = 0x3;
614 break;
615 default:
616 readmask = inst->dst.writemask;
617 break;
618 }
619
620 for (int i = 0 ; i < 3; i++) {
621 if (inst->src[i].file != UNIFORM)
622 continue;
623
624 assert(type_sz(inst->src[i].type) % 4 == 0);
625 unsigned channel_size = type_sz(inst->src[i].type) / 4;
626
627 int reg = inst->src[i].nr;
628 for (int c = 0; c < 4; c++) {
629 if (!(readmask & (1 << c)))
630 continue;
631
632 unsigned channel = BRW_GET_SWZ(inst->src[i].swizzle, c) + 1;
633 unsigned used = MAX2(chans_used[reg], channel * channel_size);
634 if (used <= 4)
635 chans_used[reg] = used;
636 else
637 chans_used[reg + 1] = used - 4;
638 }
639 }
640
641 if (inst->opcode == SHADER_OPCODE_MOV_INDIRECT &&
642 inst->src[0].file == UNIFORM) {
643 assert(inst->src[2].file == BRW_IMMEDIATE_VALUE);
644 assert(inst->src[0].subnr == 0);
645
646 unsigned bytes_read = inst->src[2].ud;
647 assert(bytes_read % 4 == 0);
648 unsigned vec4s_read = DIV_ROUND_UP(bytes_read, 16);
649
650 /* We just mark every register touched by a MOV_INDIRECT as being
651 * fully used. This ensures that it doesn't broken up piecewise by
652 * the next part of our packing algorithm.
653 */
654 int reg = inst->src[0].nr;
655 for (unsigned i = 0; i < vec4s_read; i++)
656 chans_used[reg + i] = 4;
657 }
658 }
659
660 int new_uniform_count = 0;
661
662 /* Now, figure out a packing of the live uniform vectors into our
663 * push constants.
664 */
665 for (int src = 0; src < uniforms; src++) {
666 int size = chans_used[src];
667
668 if (size == 0)
669 continue;
670
671 int dst;
672 /* Find the lowest place we can slot this uniform in. */
673 for (dst = 0; dst < src; dst++) {
674 if (chans_used[dst] + size <= 4)
675 break;
676 }
677
678 if (src == dst) {
679 new_loc[src] = dst;
680 new_chan[src] = 0;
681 } else {
682 new_loc[src] = dst;
683 new_chan[src] = chans_used[dst];
684
685 /* Move the references to the data */
686 for (int j = 0; j < size; j++) {
687 stage_prog_data->param[dst * 4 + new_chan[src] + j] =
688 stage_prog_data->param[src * 4 + j];
689 }
690
691 chans_used[dst] += size;
692 chans_used[src] = 0;
693 }
694
695 new_uniform_count = MAX2(new_uniform_count, dst + 1);
696 }
697
698 this->uniforms = new_uniform_count;
699
700 /* Now, update the instructions for our repacked uniforms. */
701 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
702 for (int i = 0 ; i < 3; i++) {
703 int src = inst->src[i].nr;
704
705 if (inst->src[i].file != UNIFORM)
706 continue;
707
708 inst->src[i].nr = new_loc[src];
709 inst->src[i].swizzle += BRW_SWIZZLE4(new_chan[src], new_chan[src],
710 new_chan[src], new_chan[src]);
711 }
712 }
713 }
714
715 /**
716 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
717 *
718 * While GLSL IR also performs this optimization, we end up with it in
719 * our instruction stream for a couple of reasons. One is that we
720 * sometimes generate silly instructions, for example in array access
721 * where we'll generate "ADD offset, index, base" even if base is 0.
722 * The other is that GLSL IR's constant propagation doesn't track the
723 * components of aggregates, so some VS patterns (initialize matrix to
724 * 0, accumulate in vertex blending factors) end up breaking down to
725 * instructions involving 0.
726 */
727 bool
728 vec4_visitor::opt_algebraic()
729 {
730 bool progress = false;
731
732 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
733 switch (inst->opcode) {
734 case BRW_OPCODE_MOV:
735 if (inst->src[0].file != IMM)
736 break;
737
738 if (inst->saturate) {
739 if (inst->dst.type != inst->src[0].type)
740 assert(!"unimplemented: saturate mixed types");
741
742 if (brw_saturate_immediate(inst->dst.type,
743 &inst->src[0].as_brw_reg())) {
744 inst->saturate = false;
745 progress = true;
746 }
747 }
748 break;
749
750 case VEC4_OPCODE_UNPACK_UNIFORM:
751 if (inst->src[0].file != UNIFORM) {
752 inst->opcode = BRW_OPCODE_MOV;
753 progress = true;
754 }
755 break;
756
757 case BRW_OPCODE_ADD:
758 if (inst->src[1].is_zero()) {
759 inst->opcode = BRW_OPCODE_MOV;
760 inst->src[1] = src_reg();
761 progress = true;
762 }
763 break;
764
765 case BRW_OPCODE_MUL:
766 if (inst->src[1].is_zero()) {
767 inst->opcode = BRW_OPCODE_MOV;
768 switch (inst->src[0].type) {
769 case BRW_REGISTER_TYPE_F:
770 inst->src[0] = brw_imm_f(0.0f);
771 break;
772 case BRW_REGISTER_TYPE_D:
773 inst->src[0] = brw_imm_d(0);
774 break;
775 case BRW_REGISTER_TYPE_UD:
776 inst->src[0] = brw_imm_ud(0u);
777 break;
778 default:
779 unreachable("not reached");
780 }
781 inst->src[1] = src_reg();
782 progress = true;
783 } else if (inst->src[1].is_one()) {
784 inst->opcode = BRW_OPCODE_MOV;
785 inst->src[1] = src_reg();
786 progress = true;
787 } else if (inst->src[1].is_negative_one()) {
788 inst->opcode = BRW_OPCODE_MOV;
789 inst->src[0].negate = !inst->src[0].negate;
790 inst->src[1] = src_reg();
791 progress = true;
792 }
793 break;
794 case BRW_OPCODE_CMP:
795 if (inst->conditional_mod == BRW_CONDITIONAL_GE &&
796 inst->src[0].abs &&
797 inst->src[0].negate &&
798 inst->src[1].is_zero()) {
799 inst->src[0].abs = false;
800 inst->src[0].negate = false;
801 inst->conditional_mod = BRW_CONDITIONAL_Z;
802 progress = true;
803 break;
804 }
805 break;
806 case SHADER_OPCODE_BROADCAST:
807 if (is_uniform(inst->src[0]) ||
808 inst->src[1].is_zero()) {
809 inst->opcode = BRW_OPCODE_MOV;
810 inst->src[1] = src_reg();
811 inst->force_writemask_all = true;
812 progress = true;
813 }
814 break;
815
816 default:
817 break;
818 }
819 }
820
821 if (progress)
822 invalidate_live_intervals();
823
824 return progress;
825 }
826
827 /**
828 * Only a limited number of hardware registers may be used for push
829 * constants, so this turns access to the overflowed constants into
830 * pull constants.
831 */
832 void
833 vec4_visitor::move_push_constants_to_pull_constants()
834 {
835 int pull_constant_loc[this->uniforms];
836
837 /* Only allow 32 registers (256 uniform components) as push constants,
838 * which is the limit on gen6.
839 *
840 * If changing this value, note the limitation about total_regs in
841 * brw_curbe.c.
842 */
843 int max_uniform_components = 32 * 8;
844 if (this->uniforms * 4 <= max_uniform_components)
845 return;
846
847 /* Make some sort of choice as to which uniforms get sent to pull
848 * constants. We could potentially do something clever here like
849 * look for the most infrequently used uniform vec4s, but leave
850 * that for later.
851 */
852 for (int i = 0; i < this->uniforms * 4; i += 4) {
853 pull_constant_loc[i / 4] = -1;
854
855 if (i >= max_uniform_components) {
856 const gl_constant_value **values = &stage_prog_data->param[i];
857
858 /* Try to find an existing copy of this uniform in the pull
859 * constants if it was part of an array access already.
860 */
861 for (unsigned int j = 0; j < stage_prog_data->nr_pull_params; j += 4) {
862 int matches;
863
864 for (matches = 0; matches < 4; matches++) {
865 if (stage_prog_data->pull_param[j + matches] != values[matches])
866 break;
867 }
868
869 if (matches == 4) {
870 pull_constant_loc[i / 4] = j / 4;
871 break;
872 }
873 }
874
875 if (pull_constant_loc[i / 4] == -1) {
876 assert(stage_prog_data->nr_pull_params % 4 == 0);
877 pull_constant_loc[i / 4] = stage_prog_data->nr_pull_params / 4;
878
879 for (int j = 0; j < 4; j++) {
880 stage_prog_data->pull_param[stage_prog_data->nr_pull_params++] =
881 values[j];
882 }
883 }
884 }
885 }
886
887 /* Now actually rewrite usage of the things we've moved to pull
888 * constants.
889 */
890 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
891 for (int i = 0 ; i < 3; i++) {
892 if (inst->src[i].file != UNIFORM ||
893 pull_constant_loc[inst->src[i].nr] == -1)
894 continue;
895
896 int uniform = inst->src[i].nr;
897
898 const glsl_type *temp_type = type_sz(inst->src[i].type) == 8 ?
899 glsl_type::dvec4_type : glsl_type::vec4_type;
900 dst_reg temp = dst_reg(this, temp_type);
901
902 emit_pull_constant_load(block, inst, temp, inst->src[i],
903 pull_constant_loc[uniform], src_reg());
904
905 inst->src[i].file = temp.file;
906 inst->src[i].nr = temp.nr;
907 inst->src[i].offset %= 16;
908 inst->src[i].reladdr = NULL;
909 }
910 }
911
912 /* Repack push constants to remove the now-unused ones. */
913 pack_uniform_registers();
914 }
915
916 /* Conditions for which we want to avoid setting the dependency control bits */
917 bool
918 vec4_visitor::is_dep_ctrl_unsafe(const vec4_instruction *inst)
919 {
920 #define IS_DWORD(reg) \
921 (reg.type == BRW_REGISTER_TYPE_UD || \
922 reg.type == BRW_REGISTER_TYPE_D)
923
924 #define IS_64BIT(reg) (reg.file != BAD_FILE && type_sz(reg.type) == 8)
925
926 /* From the Cherryview and Broadwell PRMs:
927 *
928 * "When source or destination datatype is 64b or operation is integer DWord
929 * multiply, DepCtrl must not be used."
930 *
931 * SKL PRMs don't include this restriction, however, gen7 seems to be
932 * affected, at least by the 64b restriction, since DepCtrl with double
933 * precision instructions seems to produce GPU hangs in some cases.
934 */
935 if (devinfo->gen == 8 || devinfo->is_broxton) {
936 if (inst->opcode == BRW_OPCODE_MUL &&
937 IS_DWORD(inst->src[0]) &&
938 IS_DWORD(inst->src[1]))
939 return true;
940 }
941
942 if (devinfo->gen >= 7 && devinfo->gen <= 8) {
943 if (IS_64BIT(inst->dst) || IS_64BIT(inst->src[0]) ||
944 IS_64BIT(inst->src[1]) || IS_64BIT(inst->src[2]))
945 return true;
946 }
947
948 #undef IS_64BIT
949 #undef IS_DWORD
950
951 if (devinfo->gen >= 8) {
952 if (inst->opcode == BRW_OPCODE_F32TO16)
953 return true;
954 }
955
956 /*
957 * mlen:
958 * In the presence of send messages, totally interrupt dependency
959 * control. They're long enough that the chance of dependency
960 * control around them just doesn't matter.
961 *
962 * predicate:
963 * From the Ivy Bridge PRM, volume 4 part 3.7, page 80:
964 * When a sequence of NoDDChk and NoDDClr are used, the last instruction that
965 * completes the scoreboard clear must have a non-zero execution mask. This
966 * means, if any kind of predication can change the execution mask or channel
967 * enable of the last instruction, the optimization must be avoided. This is
968 * to avoid instructions being shot down the pipeline when no writes are
969 * required.
970 *
971 * math:
972 * Dependency control does not work well over math instructions.
973 * NB: Discovered empirically
974 */
975 return (inst->mlen || inst->predicate || inst->is_math());
976 }
977
978 /**
979 * Sets the dependency control fields on instructions after register
980 * allocation and before the generator is run.
981 *
982 * When you have a sequence of instructions like:
983 *
984 * DP4 temp.x vertex uniform[0]
985 * DP4 temp.y vertex uniform[0]
986 * DP4 temp.z vertex uniform[0]
987 * DP4 temp.w vertex uniform[0]
988 *
989 * The hardware doesn't know that it can actually run the later instructions
990 * while the previous ones are in flight, producing stalls. However, we have
991 * manual fields we can set in the instructions that let it do so.
992 */
993 void
994 vec4_visitor::opt_set_dependency_control()
995 {
996 vec4_instruction *last_grf_write[BRW_MAX_GRF];
997 uint8_t grf_channels_written[BRW_MAX_GRF];
998 vec4_instruction *last_mrf_write[BRW_MAX_GRF];
999 uint8_t mrf_channels_written[BRW_MAX_GRF];
1000
1001 assert(prog_data->total_grf ||
1002 !"Must be called after register allocation");
1003
1004 foreach_block (block, cfg) {
1005 memset(last_grf_write, 0, sizeof(last_grf_write));
1006 memset(last_mrf_write, 0, sizeof(last_mrf_write));
1007
1008 foreach_inst_in_block (vec4_instruction, inst, block) {
1009 /* If we read from a register that we were doing dependency control
1010 * on, don't do dependency control across the read.
1011 */
1012 for (int i = 0; i < 3; i++) {
1013 int reg = inst->src[i].nr + inst->src[i].offset / REG_SIZE;
1014 if (inst->src[i].file == VGRF) {
1015 last_grf_write[reg] = NULL;
1016 } else if (inst->src[i].file == FIXED_GRF) {
1017 memset(last_grf_write, 0, sizeof(last_grf_write));
1018 break;
1019 }
1020 assert(inst->src[i].file != MRF);
1021 }
1022
1023 if (is_dep_ctrl_unsafe(inst)) {
1024 memset(last_grf_write, 0, sizeof(last_grf_write));
1025 memset(last_mrf_write, 0, sizeof(last_mrf_write));
1026 continue;
1027 }
1028
1029 /* Now, see if we can do dependency control for this instruction
1030 * against a previous one writing to its destination.
1031 */
1032 int reg = inst->dst.nr + inst->dst.offset / REG_SIZE;
1033 if (inst->dst.file == VGRF || inst->dst.file == FIXED_GRF) {
1034 if (last_grf_write[reg] &&
1035 last_grf_write[reg]->dst.offset == inst->dst.offset &&
1036 !(inst->dst.writemask & grf_channels_written[reg])) {
1037 last_grf_write[reg]->no_dd_clear = true;
1038 inst->no_dd_check = true;
1039 } else {
1040 grf_channels_written[reg] = 0;
1041 }
1042
1043 last_grf_write[reg] = inst;
1044 grf_channels_written[reg] |= inst->dst.writemask;
1045 } else if (inst->dst.file == MRF) {
1046 if (last_mrf_write[reg] &&
1047 last_mrf_write[reg]->dst.offset == inst->dst.offset &&
1048 !(inst->dst.writemask & mrf_channels_written[reg])) {
1049 last_mrf_write[reg]->no_dd_clear = true;
1050 inst->no_dd_check = true;
1051 } else {
1052 mrf_channels_written[reg] = 0;
1053 }
1054
1055 last_mrf_write[reg] = inst;
1056 mrf_channels_written[reg] |= inst->dst.writemask;
1057 }
1058 }
1059 }
1060 }
1061
1062 bool
1063 vec4_instruction::can_reswizzle(const struct gen_device_info *devinfo,
1064 int dst_writemask,
1065 int swizzle,
1066 int swizzle_mask)
1067 {
1068 /* Gen6 MATH instructions can not execute in align16 mode, so swizzles
1069 * are not allowed.
1070 */
1071 if (devinfo->gen == 6 && is_math() && swizzle != BRW_SWIZZLE_XYZW)
1072 return false;
1073
1074 if (!can_do_writemask(devinfo) && dst_writemask != WRITEMASK_XYZW)
1075 return false;
1076
1077 /* If this instruction sets anything not referenced by swizzle, then we'd
1078 * totally break it when we reswizzle.
1079 */
1080 if (dst.writemask & ~swizzle_mask)
1081 return false;
1082
1083 if (mlen > 0)
1084 return false;
1085
1086 for (int i = 0; i < 3; i++) {
1087 if (src[i].is_accumulator())
1088 return false;
1089 }
1090
1091 return true;
1092 }
1093
1094 /**
1095 * For any channels in the swizzle's source that were populated by this
1096 * instruction, rewrite the instruction to put the appropriate result directly
1097 * in those channels.
1098 *
1099 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
1100 */
1101 void
1102 vec4_instruction::reswizzle(int dst_writemask, int swizzle)
1103 {
1104 /* Destination write mask doesn't correspond to source swizzle for the dot
1105 * product and pack_bytes instructions.
1106 */
1107 if (opcode != BRW_OPCODE_DP4 && opcode != BRW_OPCODE_DPH &&
1108 opcode != BRW_OPCODE_DP3 && opcode != BRW_OPCODE_DP2 &&
1109 opcode != VEC4_OPCODE_PACK_BYTES) {
1110 for (int i = 0; i < 3; i++) {
1111 if (src[i].file == BAD_FILE || src[i].file == IMM)
1112 continue;
1113
1114 src[i].swizzle = brw_compose_swizzle(swizzle, src[i].swizzle);
1115 }
1116 }
1117
1118 /* Apply the specified swizzle and writemask to the original mask of
1119 * written components.
1120 */
1121 dst.writemask = dst_writemask &
1122 brw_apply_swizzle_to_mask(swizzle, dst.writemask);
1123 }
1124
1125 /*
1126 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
1127 * just written and then MOVed into another reg and making the original write
1128 * of the GRF write directly to the final destination instead.
1129 */
1130 bool
1131 vec4_visitor::opt_register_coalesce()
1132 {
1133 bool progress = false;
1134 int next_ip = 0;
1135
1136 calculate_live_intervals();
1137
1138 foreach_block_and_inst_safe (block, vec4_instruction, inst, cfg) {
1139 int ip = next_ip;
1140 next_ip++;
1141
1142 if (inst->opcode != BRW_OPCODE_MOV ||
1143 (inst->dst.file != VGRF && inst->dst.file != MRF) ||
1144 inst->predicate ||
1145 inst->src[0].file != VGRF ||
1146 inst->dst.type != inst->src[0].type ||
1147 inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
1148 continue;
1149
1150 /* Remove no-op MOVs */
1151 if (inst->dst.file == inst->src[0].file &&
1152 inst->dst.nr == inst->src[0].nr &&
1153 inst->dst.offset == inst->src[0].offset) {
1154 bool is_nop_mov = true;
1155
1156 for (unsigned c = 0; c < 4; c++) {
1157 if ((inst->dst.writemask & (1 << c)) == 0)
1158 continue;
1159
1160 if (BRW_GET_SWZ(inst->src[0].swizzle, c) != c) {
1161 is_nop_mov = false;
1162 break;
1163 }
1164 }
1165
1166 if (is_nop_mov) {
1167 inst->remove(block);
1168 progress = true;
1169 continue;
1170 }
1171 }
1172
1173 bool to_mrf = (inst->dst.file == MRF);
1174
1175 /* Can't coalesce this GRF if someone else was going to
1176 * read it later.
1177 */
1178 if (var_range_end(var_from_reg(alloc, dst_reg(inst->src[0])), 8) > ip)
1179 continue;
1180
1181 /* We need to check interference with the final destination between this
1182 * instruction and the earliest instruction involved in writing the GRF
1183 * we're eliminating. To do that, keep track of which of our source
1184 * channels we've seen initialized.
1185 */
1186 const unsigned chans_needed =
1187 brw_apply_inv_swizzle_to_mask(inst->src[0].swizzle,
1188 inst->dst.writemask);
1189 unsigned chans_remaining = chans_needed;
1190
1191 /* Now walk up the instruction stream trying to see if we can rewrite
1192 * everything writing to the temporary to write into the destination
1193 * instead.
1194 */
1195 vec4_instruction *_scan_inst = (vec4_instruction *)inst->prev;
1196 foreach_inst_in_block_reverse_starting_from(vec4_instruction, scan_inst,
1197 inst) {
1198 _scan_inst = scan_inst;
1199
1200 if (regions_overlap(inst->src[0], inst->size_read(0),
1201 scan_inst->dst, scan_inst->size_written)) {
1202 /* Found something writing to the reg we want to coalesce away. */
1203 if (to_mrf) {
1204 /* SEND instructions can't have MRF as a destination. */
1205 if (scan_inst->mlen)
1206 break;
1207
1208 if (devinfo->gen == 6) {
1209 /* gen6 math instructions must have the destination be
1210 * VGRF, so no compute-to-MRF for them.
1211 */
1212 if (scan_inst->is_math()) {
1213 break;
1214 }
1215 }
1216 }
1217
1218 /* This doesn't handle saturation on the instruction we
1219 * want to coalesce away if the register types do not match.
1220 * But if scan_inst is a non type-converting 'mov', we can fix
1221 * the types later.
1222 */
1223 if (inst->saturate &&
1224 inst->dst.type != scan_inst->dst.type &&
1225 !(scan_inst->opcode == BRW_OPCODE_MOV &&
1226 scan_inst->dst.type == scan_inst->src[0].type))
1227 break;
1228
1229 /* Only allow coalescing between registers of the same type size.
1230 * Otherwise we would need to make the pass aware of the fact that
1231 * channel sizes are different for single and double precision.
1232 */
1233 if (type_sz(inst->src[0].type) != type_sz(scan_inst->src[0].type))
1234 break;
1235
1236 /* Check that scan_inst writes the same amount of data as the
1237 * instruction, otherwise coalescing would lead to writing a
1238 * different (larger or smaller) region of the destination
1239 */
1240 if (scan_inst->size_written != inst->size_written)
1241 break;
1242
1243 /* If we can't handle the swizzle, bail. */
1244 if (!scan_inst->can_reswizzle(devinfo, inst->dst.writemask,
1245 inst->src[0].swizzle,
1246 chans_needed)) {
1247 break;
1248 }
1249
1250 /* This only handles coalescing writes of 8 channels (1 register
1251 * for single-precision and 2 registers for double-precision)
1252 * starting at the source offset of the copy instruction.
1253 */
1254 if (DIV_ROUND_UP(scan_inst->size_written,
1255 type_sz(scan_inst->dst.type)) > 8 ||
1256 scan_inst->dst.offset != inst->src[0].offset)
1257 break;
1258
1259 /* Mark which channels we found unconditional writes for. */
1260 if (!scan_inst->predicate)
1261 chans_remaining &= ~scan_inst->dst.writemask;
1262
1263 if (chans_remaining == 0)
1264 break;
1265 }
1266
1267 /* You can't read from an MRF, so if someone else reads our MRF's
1268 * source GRF that we wanted to rewrite, that stops us. If it's a
1269 * GRF we're trying to coalesce to, we don't actually handle
1270 * rewriting sources so bail in that case as well.
1271 */
1272 bool interfered = false;
1273 for (int i = 0; i < 3; i++) {
1274 if (regions_overlap(inst->src[0], inst->size_read(0),
1275 scan_inst->src[i], scan_inst->size_read(i)))
1276 interfered = true;
1277 }
1278 if (interfered)
1279 break;
1280
1281 /* If somebody else writes the same channels of our destination here,
1282 * we can't coalesce before that.
1283 */
1284 if (regions_overlap(inst->dst, inst->size_written,
1285 scan_inst->dst, scan_inst->size_written) &&
1286 (inst->dst.writemask & scan_inst->dst.writemask) != 0) {
1287 break;
1288 }
1289
1290 /* Check for reads of the register we're trying to coalesce into. We
1291 * can't go rewriting instructions above that to put some other value
1292 * in the register instead.
1293 */
1294 if (to_mrf && scan_inst->mlen > 0) {
1295 if (inst->dst.nr >= scan_inst->base_mrf &&
1296 inst->dst.nr < scan_inst->base_mrf + scan_inst->mlen) {
1297 break;
1298 }
1299 } else {
1300 for (int i = 0; i < 3; i++) {
1301 if (regions_overlap(inst->dst, inst->size_written,
1302 scan_inst->src[i], scan_inst->size_read(i)))
1303 interfered = true;
1304 }
1305 if (interfered)
1306 break;
1307 }
1308 }
1309
1310 if (chans_remaining == 0) {
1311 /* If we've made it here, we have an MOV we want to coalesce out, and
1312 * a scan_inst pointing to the earliest instruction involved in
1313 * computing the value. Now go rewrite the instruction stream
1314 * between the two.
1315 */
1316 vec4_instruction *scan_inst = _scan_inst;
1317 while (scan_inst != inst) {
1318 if (scan_inst->dst.file == VGRF &&
1319 scan_inst->dst.nr == inst->src[0].nr &&
1320 scan_inst->dst.offset == inst->src[0].offset) {
1321 scan_inst->reswizzle(inst->dst.writemask,
1322 inst->src[0].swizzle);
1323 scan_inst->dst.file = inst->dst.file;
1324 scan_inst->dst.nr = inst->dst.nr;
1325 scan_inst->dst.offset = inst->dst.offset;
1326 if (inst->saturate &&
1327 inst->dst.type != scan_inst->dst.type) {
1328 /* If we have reached this point, scan_inst is a non
1329 * type-converting 'mov' and we can modify its register types
1330 * to match the ones in inst. Otherwise, we could have an
1331 * incorrect saturation result.
1332 */
1333 scan_inst->dst.type = inst->dst.type;
1334 scan_inst->src[0].type = inst->src[0].type;
1335 }
1336 scan_inst->saturate |= inst->saturate;
1337 }
1338 scan_inst = (vec4_instruction *)scan_inst->next;
1339 }
1340 inst->remove(block);
1341 progress = true;
1342 }
1343 }
1344
1345 if (progress)
1346 invalidate_live_intervals();
1347
1348 return progress;
1349 }
1350
1351 /**
1352 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
1353 * flow. We could probably do better here with some form of divergence
1354 * analysis.
1355 */
1356 bool
1357 vec4_visitor::eliminate_find_live_channel()
1358 {
1359 bool progress = false;
1360 unsigned depth = 0;
1361
1362 if (!brw_stage_has_packed_dispatch(devinfo, stage, stage_prog_data)) {
1363 /* The optimization below assumes that channel zero is live on thread
1364 * dispatch, which may not be the case if the fixed function dispatches
1365 * threads sparsely.
1366 */
1367 return false;
1368 }
1369
1370 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
1371 switch (inst->opcode) {
1372 case BRW_OPCODE_IF:
1373 case BRW_OPCODE_DO:
1374 depth++;
1375 break;
1376
1377 case BRW_OPCODE_ENDIF:
1378 case BRW_OPCODE_WHILE:
1379 depth--;
1380 break;
1381
1382 case SHADER_OPCODE_FIND_LIVE_CHANNEL:
1383 if (depth == 0) {
1384 inst->opcode = BRW_OPCODE_MOV;
1385 inst->src[0] = brw_imm_d(0);
1386 inst->force_writemask_all = true;
1387 progress = true;
1388 }
1389 break;
1390
1391 default:
1392 break;
1393 }
1394 }
1395
1396 return progress;
1397 }
1398
1399 /**
1400 * Splits virtual GRFs requesting more than one contiguous physical register.
1401 *
1402 * We initially create large virtual GRFs for temporary structures, arrays,
1403 * and matrices, so that the visitor functions can add offsets to work their
1404 * way down to the actual member being accessed. But when it comes to
1405 * optimization, we'd like to treat each register as individual storage if
1406 * possible.
1407 *
1408 * So far, the only thing that might prevent splitting is a send message from
1409 * a GRF on IVB.
1410 */
1411 void
1412 vec4_visitor::split_virtual_grfs()
1413 {
1414 int num_vars = this->alloc.count;
1415 int new_virtual_grf[num_vars];
1416 bool split_grf[num_vars];
1417
1418 memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1419
1420 /* Try to split anything > 0 sized. */
1421 for (int i = 0; i < num_vars; i++) {
1422 split_grf[i] = this->alloc.sizes[i] != 1;
1423 }
1424
1425 /* Check that the instructions are compatible with the registers we're trying
1426 * to split.
1427 */
1428 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1429 if (inst->dst.file == VGRF && regs_written(inst) > 1)
1430 split_grf[inst->dst.nr] = false;
1431
1432 for (int i = 0; i < 3; i++) {
1433 if (inst->src[i].file == VGRF && regs_read(inst, i) > 1)
1434 split_grf[inst->src[i].nr] = false;
1435 }
1436 }
1437
1438 /* Allocate new space for split regs. Note that the virtual
1439 * numbers will be contiguous.
1440 */
1441 for (int i = 0; i < num_vars; i++) {
1442 if (!split_grf[i])
1443 continue;
1444
1445 new_virtual_grf[i] = alloc.allocate(1);
1446 for (unsigned j = 2; j < this->alloc.sizes[i]; j++) {
1447 unsigned reg = alloc.allocate(1);
1448 assert(reg == new_virtual_grf[i] + j - 1);
1449 (void) reg;
1450 }
1451 this->alloc.sizes[i] = 1;
1452 }
1453
1454 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1455 if (inst->dst.file == VGRF && split_grf[inst->dst.nr] &&
1456 inst->dst.offset / REG_SIZE != 0) {
1457 inst->dst.nr = (new_virtual_grf[inst->dst.nr] +
1458 inst->dst.offset / REG_SIZE - 1);
1459 inst->dst.offset %= REG_SIZE;
1460 }
1461 for (int i = 0; i < 3; i++) {
1462 if (inst->src[i].file == VGRF && split_grf[inst->src[i].nr] &&
1463 inst->src[i].offset / REG_SIZE != 0) {
1464 inst->src[i].nr = (new_virtual_grf[inst->src[i].nr] +
1465 inst->src[i].offset / REG_SIZE - 1);
1466 inst->src[i].offset %= REG_SIZE;
1467 }
1468 }
1469 }
1470 invalidate_live_intervals();
1471 }
1472
1473 void
1474 vec4_visitor::dump_instruction(backend_instruction *be_inst)
1475 {
1476 dump_instruction(be_inst, stderr);
1477 }
1478
1479 void
1480 vec4_visitor::dump_instruction(backend_instruction *be_inst, FILE *file)
1481 {
1482 vec4_instruction *inst = (vec4_instruction *)be_inst;
1483
1484 if (inst->predicate) {
1485 fprintf(file, "(%cf0.%d%s) ",
1486 inst->predicate_inverse ? '-' : '+',
1487 inst->flag_subreg,
1488 pred_ctrl_align16[inst->predicate]);
1489 }
1490
1491 fprintf(file, "%s(%d)", brw_instruction_name(devinfo, inst->opcode),
1492 inst->exec_size);
1493 if (inst->saturate)
1494 fprintf(file, ".sat");
1495 if (inst->conditional_mod) {
1496 fprintf(file, "%s", conditional_modifier[inst->conditional_mod]);
1497 if (!inst->predicate &&
1498 (devinfo->gen < 5 || (inst->opcode != BRW_OPCODE_SEL &&
1499 inst->opcode != BRW_OPCODE_IF &&
1500 inst->opcode != BRW_OPCODE_WHILE))) {
1501 fprintf(file, ".f0.%d", inst->flag_subreg);
1502 }
1503 }
1504 fprintf(file, " ");
1505
1506 switch (inst->dst.file) {
1507 case VGRF:
1508 fprintf(file, "vgrf%d", inst->dst.nr);
1509 break;
1510 case FIXED_GRF:
1511 fprintf(file, "g%d", inst->dst.nr);
1512 break;
1513 case MRF:
1514 fprintf(file, "m%d", inst->dst.nr);
1515 break;
1516 case ARF:
1517 switch (inst->dst.nr) {
1518 case BRW_ARF_NULL:
1519 fprintf(file, "null");
1520 break;
1521 case BRW_ARF_ADDRESS:
1522 fprintf(file, "a0.%d", inst->dst.subnr);
1523 break;
1524 case BRW_ARF_ACCUMULATOR:
1525 fprintf(file, "acc%d", inst->dst.subnr);
1526 break;
1527 case BRW_ARF_FLAG:
1528 fprintf(file, "f%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
1529 break;
1530 default:
1531 fprintf(file, "arf%d.%d", inst->dst.nr & 0xf, inst->dst.subnr);
1532 break;
1533 }
1534 break;
1535 case BAD_FILE:
1536 fprintf(file, "(null)");
1537 break;
1538 case IMM:
1539 case ATTR:
1540 case UNIFORM:
1541 unreachable("not reached");
1542 }
1543 if (inst->dst.offset ||
1544 (inst->dst.file == VGRF &&
1545 alloc.sizes[inst->dst.nr] * REG_SIZE != inst->size_written)) {
1546 const unsigned reg_size = (inst->dst.file == UNIFORM ? 16 : REG_SIZE);
1547 fprintf(file, "+%d.%d", inst->dst.offset / reg_size,
1548 inst->dst.offset % reg_size);
1549 }
1550 if (inst->dst.writemask != WRITEMASK_XYZW) {
1551 fprintf(file, ".");
1552 if (inst->dst.writemask & 1)
1553 fprintf(file, "x");
1554 if (inst->dst.writemask & 2)
1555 fprintf(file, "y");
1556 if (inst->dst.writemask & 4)
1557 fprintf(file, "z");
1558 if (inst->dst.writemask & 8)
1559 fprintf(file, "w");
1560 }
1561 fprintf(file, ":%s", brw_reg_type_letters(inst->dst.type));
1562
1563 if (inst->src[0].file != BAD_FILE)
1564 fprintf(file, ", ");
1565
1566 for (int i = 0; i < 3 && inst->src[i].file != BAD_FILE; i++) {
1567 if (inst->src[i].negate)
1568 fprintf(file, "-");
1569 if (inst->src[i].abs)
1570 fprintf(file, "|");
1571 switch (inst->src[i].file) {
1572 case VGRF:
1573 fprintf(file, "vgrf%d", inst->src[i].nr);
1574 break;
1575 case FIXED_GRF:
1576 fprintf(file, "g%d.%d", inst->src[i].nr, inst->src[i].subnr);
1577 break;
1578 case ATTR:
1579 fprintf(file, "attr%d", inst->src[i].nr);
1580 break;
1581 case UNIFORM:
1582 fprintf(file, "u%d", inst->src[i].nr);
1583 break;
1584 case IMM:
1585 switch (inst->src[i].type) {
1586 case BRW_REGISTER_TYPE_F:
1587 fprintf(file, "%fF", inst->src[i].f);
1588 break;
1589 case BRW_REGISTER_TYPE_DF:
1590 fprintf(file, "%fDF", inst->src[i].df);
1591 break;
1592 case BRW_REGISTER_TYPE_D:
1593 fprintf(file, "%dD", inst->src[i].d);
1594 break;
1595 case BRW_REGISTER_TYPE_UD:
1596 fprintf(file, "%uU", inst->src[i].ud);
1597 break;
1598 case BRW_REGISTER_TYPE_VF:
1599 fprintf(file, "[%-gF, %-gF, %-gF, %-gF]",
1600 brw_vf_to_float((inst->src[i].ud >> 0) & 0xff),
1601 brw_vf_to_float((inst->src[i].ud >> 8) & 0xff),
1602 brw_vf_to_float((inst->src[i].ud >> 16) & 0xff),
1603 brw_vf_to_float((inst->src[i].ud >> 24) & 0xff));
1604 break;
1605 default:
1606 fprintf(file, "???");
1607 break;
1608 }
1609 break;
1610 case ARF:
1611 switch (inst->src[i].nr) {
1612 case BRW_ARF_NULL:
1613 fprintf(file, "null");
1614 break;
1615 case BRW_ARF_ADDRESS:
1616 fprintf(file, "a0.%d", inst->src[i].subnr);
1617 break;
1618 case BRW_ARF_ACCUMULATOR:
1619 fprintf(file, "acc%d", inst->src[i].subnr);
1620 break;
1621 case BRW_ARF_FLAG:
1622 fprintf(file, "f%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
1623 break;
1624 default:
1625 fprintf(file, "arf%d.%d", inst->src[i].nr & 0xf, inst->src[i].subnr);
1626 break;
1627 }
1628 break;
1629 case BAD_FILE:
1630 fprintf(file, "(null)");
1631 break;
1632 case MRF:
1633 unreachable("not reached");
1634 }
1635
1636 if (inst->src[i].offset ||
1637 (inst->src[i].file == VGRF &&
1638 alloc.sizes[inst->src[i].nr] * REG_SIZE != inst->size_read(i))) {
1639 const unsigned reg_size = (inst->src[i].file == UNIFORM ? 16 : REG_SIZE);
1640 fprintf(file, "+%d.%d", inst->src[i].offset / reg_size,
1641 inst->src[i].offset % reg_size);
1642 }
1643
1644 if (inst->src[i].file != IMM) {
1645 static const char *chans[4] = {"x", "y", "z", "w"};
1646 fprintf(file, ".");
1647 for (int c = 0; c < 4; c++) {
1648 fprintf(file, "%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1649 }
1650 }
1651
1652 if (inst->src[i].abs)
1653 fprintf(file, "|");
1654
1655 if (inst->src[i].file != IMM) {
1656 fprintf(file, ":%s", brw_reg_type_letters(inst->src[i].type));
1657 }
1658
1659 if (i < 2 && inst->src[i + 1].file != BAD_FILE)
1660 fprintf(file, ", ");
1661 }
1662
1663 if (inst->force_writemask_all)
1664 fprintf(file, " NoMask");
1665
1666 if (inst->exec_size != 8)
1667 fprintf(file, " group%d", inst->group);
1668
1669 fprintf(file, "\n");
1670 }
1671
1672
1673 static inline struct brw_reg
1674 attribute_to_hw_reg(int attr, brw_reg_type type, bool interleaved)
1675 {
1676 struct brw_reg reg;
1677
1678 unsigned width = REG_SIZE / 2 / MAX2(4, type_sz(type));
1679 if (interleaved) {
1680 reg = stride(brw_vecn_grf(width, attr / 2, (attr % 2) * 4), 0, width, 1);
1681 } else {
1682 reg = brw_vecn_grf(width, attr, 0);
1683 }
1684
1685 reg.type = type;
1686 return reg;
1687 }
1688
1689
1690 /**
1691 * Replace each register of type ATTR in this->instructions with a reference
1692 * to a fixed HW register.
1693 *
1694 * If interleaved is true, then each attribute takes up half a register, with
1695 * register N containing attribute 2*N in its first half and attribute 2*N+1
1696 * in its second half (this corresponds to the payload setup used by geometry
1697 * shaders in "single" or "dual instanced" dispatch mode). If interleaved is
1698 * false, then each attribute takes up a whole register, with register N
1699 * containing attribute N (this corresponds to the payload setup used by
1700 * vertex shaders, and by geometry shaders in "dual object" dispatch mode).
1701 */
1702 void
1703 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map,
1704 bool interleaved)
1705 {
1706 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1707 for (int i = 0; i < 3; i++) {
1708 if (inst->src[i].file != ATTR)
1709 continue;
1710
1711 int grf = attribute_map[inst->src[i].nr +
1712 inst->src[i].offset / REG_SIZE];
1713 assert(inst->src[i].offset % REG_SIZE == 0);
1714
1715 /* All attributes used in the shader need to have been assigned a
1716 * hardware register by the caller
1717 */
1718 assert(grf != 0);
1719
1720 struct brw_reg reg =
1721 attribute_to_hw_reg(grf, inst->src[i].type, interleaved);
1722 reg.swizzle = inst->src[i].swizzle;
1723 if (inst->src[i].abs)
1724 reg = brw_abs(reg);
1725 if (inst->src[i].negate)
1726 reg = negate(reg);
1727
1728 inst->src[i] = reg;
1729 }
1730 }
1731 }
1732
1733 int
1734 vec4_vs_visitor::setup_attributes(int payload_reg)
1735 {
1736 int nr_attributes;
1737 int attribute_map[VERT_ATTRIB_MAX + 2];
1738 memset(attribute_map, 0, sizeof(attribute_map));
1739
1740 nr_attributes = 0;
1741 GLbitfield64 vs_inputs = vs_prog_data->inputs_read;
1742 while (vs_inputs) {
1743 GLuint first = ffsll(vs_inputs) - 1;
1744 int needed_slots =
1745 (vs_prog_data->double_inputs_read & BITFIELD64_BIT(first)) ? 2 : 1;
1746 for (int c = 0; c < needed_slots; c++) {
1747 attribute_map[first + c] = payload_reg + nr_attributes;
1748 nr_attributes++;
1749 vs_inputs &= ~BITFIELD64_BIT(first + c);
1750 }
1751 }
1752
1753 /* VertexID is stored by the VF as the last vertex element, but we
1754 * don't represent it with a flag in inputs_read, so we call it
1755 * VERT_ATTRIB_MAX.
1756 */
1757 if (vs_prog_data->uses_vertexid || vs_prog_data->uses_instanceid ||
1758 vs_prog_data->uses_basevertex || vs_prog_data->uses_baseinstance) {
1759 attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1760 nr_attributes++;
1761 }
1762
1763 if (vs_prog_data->uses_drawid) {
1764 attribute_map[VERT_ATTRIB_MAX + 1] = payload_reg + nr_attributes;
1765 nr_attributes++;
1766 }
1767
1768 lower_attributes_to_hw_regs(attribute_map, false /* interleaved */);
1769
1770 return payload_reg + vs_prog_data->nr_attribute_slots;
1771 }
1772
1773 int
1774 vec4_visitor::setup_uniforms(int reg)
1775 {
1776 prog_data->base.dispatch_grf_start_reg = reg;
1777
1778 /* The pre-gen6 VS requires that some push constants get loaded no
1779 * matter what, or the GPU would hang.
1780 */
1781 if (devinfo->gen < 6 && this->uniforms == 0) {
1782 stage_prog_data->param =
1783 reralloc(NULL, stage_prog_data->param, const gl_constant_value *, 4);
1784 for (unsigned int i = 0; i < 4; i++) {
1785 unsigned int slot = this->uniforms * 4 + i;
1786 static gl_constant_value zero = { 0.0 };
1787 stage_prog_data->param[slot] = &zero;
1788 }
1789
1790 this->uniforms++;
1791 reg++;
1792 } else {
1793 reg += ALIGN(uniforms, 2) / 2;
1794 }
1795
1796 stage_prog_data->nr_params = this->uniforms * 4;
1797
1798 prog_data->base.curb_read_length =
1799 reg - prog_data->base.dispatch_grf_start_reg;
1800
1801 return reg;
1802 }
1803
1804 void
1805 vec4_vs_visitor::setup_payload(void)
1806 {
1807 int reg = 0;
1808
1809 /* The payload always contains important data in g0, which contains
1810 * the URB handles that are passed on to the URB write at the end
1811 * of the thread. So, we always start push constants at g1.
1812 */
1813 reg++;
1814
1815 reg = setup_uniforms(reg);
1816
1817 reg = setup_attributes(reg);
1818
1819 this->first_non_payload_grf = reg;
1820 }
1821
1822 bool
1823 vec4_visitor::lower_minmax()
1824 {
1825 assert(devinfo->gen < 6);
1826
1827 bool progress = false;
1828
1829 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
1830 const vec4_builder ibld(this, block, inst);
1831
1832 if (inst->opcode == BRW_OPCODE_SEL &&
1833 inst->predicate == BRW_PREDICATE_NONE) {
1834 /* FIXME: Using CMP doesn't preserve the NaN propagation semantics of
1835 * the original SEL.L/GE instruction
1836 */
1837 ibld.CMP(ibld.null_reg_d(), inst->src[0], inst->src[1],
1838 inst->conditional_mod);
1839 inst->predicate = BRW_PREDICATE_NORMAL;
1840 inst->conditional_mod = BRW_CONDITIONAL_NONE;
1841
1842 progress = true;
1843 }
1844 }
1845
1846 if (progress)
1847 invalidate_live_intervals();
1848
1849 return progress;
1850 }
1851
1852 src_reg
1853 vec4_visitor::get_timestamp()
1854 {
1855 assert(devinfo->gen >= 7);
1856
1857 src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1858 BRW_ARF_TIMESTAMP,
1859 0,
1860 0,
1861 0,
1862 BRW_REGISTER_TYPE_UD,
1863 BRW_VERTICAL_STRIDE_0,
1864 BRW_WIDTH_4,
1865 BRW_HORIZONTAL_STRIDE_4,
1866 BRW_SWIZZLE_XYZW,
1867 WRITEMASK_XYZW));
1868
1869 dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1870
1871 vec4_instruction *mov = emit(MOV(dst, ts));
1872 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1873 * even if it's not enabled in the dispatch.
1874 */
1875 mov->force_writemask_all = true;
1876
1877 return src_reg(dst);
1878 }
1879
1880 void
1881 vec4_visitor::emit_shader_time_begin()
1882 {
1883 current_annotation = "shader time start";
1884 shader_start_time = get_timestamp();
1885 }
1886
1887 void
1888 vec4_visitor::emit_shader_time_end()
1889 {
1890 current_annotation = "shader time end";
1891 src_reg shader_end_time = get_timestamp();
1892
1893
1894 /* Check that there weren't any timestamp reset events (assuming these
1895 * were the only two timestamp reads that happened).
1896 */
1897 src_reg reset_end = shader_end_time;
1898 reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1899 vec4_instruction *test = emit(AND(dst_null_ud(), reset_end, brw_imm_ud(1u)));
1900 test->conditional_mod = BRW_CONDITIONAL_Z;
1901
1902 emit(IF(BRW_PREDICATE_NORMAL));
1903
1904 /* Take the current timestamp and get the delta. */
1905 shader_start_time.negate = true;
1906 dst_reg diff = dst_reg(this, glsl_type::uint_type);
1907 emit(ADD(diff, shader_start_time, shader_end_time));
1908
1909 /* If there were no instructions between the two timestamp gets, the diff
1910 * is 2 cycles. Remove that overhead, so I can forget about that when
1911 * trying to determine the time taken for single instructions.
1912 */
1913 emit(ADD(diff, src_reg(diff), brw_imm_ud(-2u)));
1914
1915 emit_shader_time_write(0, src_reg(diff));
1916 emit_shader_time_write(1, brw_imm_ud(1u));
1917 emit(BRW_OPCODE_ELSE);
1918 emit_shader_time_write(2, brw_imm_ud(1u));
1919 emit(BRW_OPCODE_ENDIF);
1920 }
1921
1922 void
1923 vec4_visitor::emit_shader_time_write(int shader_time_subindex, src_reg value)
1924 {
1925 dst_reg dst =
1926 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1927
1928 dst_reg offset = dst;
1929 dst_reg time = dst;
1930 time.offset += REG_SIZE;
1931
1932 offset.type = BRW_REGISTER_TYPE_UD;
1933 int index = shader_time_index * 3 + shader_time_subindex;
1934 emit(MOV(offset, brw_imm_d(index * BRW_SHADER_TIME_STRIDE)));
1935
1936 time.type = BRW_REGISTER_TYPE_UD;
1937 emit(MOV(time, value));
1938
1939 vec4_instruction *inst =
1940 emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1941 inst->mlen = 2;
1942 }
1943
1944 void
1945 vec4_visitor::convert_to_hw_regs()
1946 {
1947 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
1948 for (int i = 0; i < 3; i++) {
1949 struct src_reg &src = inst->src[i];
1950 struct brw_reg reg;
1951 switch (src.file) {
1952 case VGRF: {
1953 const unsigned type_size = type_sz(src.type);
1954 const unsigned width = REG_SIZE / 2 / MAX2(4, type_size);
1955 reg = byte_offset(brw_vecn_grf(width, src.nr, 0), src.offset);
1956 reg.type = src.type;
1957 reg.abs = src.abs;
1958 reg.negate = src.negate;
1959 break;
1960 }
1961
1962 case UNIFORM: {
1963 const unsigned width = REG_SIZE / 2 / MAX2(4, type_sz(src.type));
1964 reg = stride(byte_offset(brw_vec4_grf(
1965 prog_data->base.dispatch_grf_start_reg +
1966 src.nr / 2, src.nr % 2 * 4),
1967 src.offset),
1968 0, width, 1);
1969 reg.type = src.type;
1970 reg.abs = src.abs;
1971 reg.negate = src.negate;
1972
1973 /* This should have been moved to pull constants. */
1974 assert(!src.reladdr);
1975 break;
1976 }
1977
1978 case FIXED_GRF:
1979 if (type_sz(src.type) == 8) {
1980 reg = src.as_brw_reg();
1981 break;
1982 }
1983 /* fallthrough */
1984 case ARF:
1985 case IMM:
1986 continue;
1987
1988 case BAD_FILE:
1989 /* Probably unused. */
1990 reg = brw_null_reg();
1991 reg = retype(reg, src.type);
1992 break;
1993
1994 case MRF:
1995 case ATTR:
1996 unreachable("not reached");
1997 }
1998
1999 apply_logical_swizzle(&reg, inst, i);
2000 src = reg;
2001 }
2002
2003 if (inst->is_3src(devinfo)) {
2004 /* 3-src instructions with scalar sources support arbitrary subnr,
2005 * but don't actually use swizzles. Convert swizzle into subnr.
2006 * Skip this for double-precision instructions: RepCtrl=1 is not
2007 * allowed for them and needs special handling.
2008 */
2009 for (int i = 0; i < 3; i++) {
2010 if (inst->src[i].vstride == BRW_VERTICAL_STRIDE_0 &&
2011 type_sz(inst->src[i].type) < 8) {
2012 assert(brw_is_single_value_swizzle(inst->src[i].swizzle));
2013 inst->src[i].subnr += 4 * BRW_GET_SWZ(inst->src[i].swizzle, 0);
2014 }
2015 }
2016 }
2017
2018 dst_reg &dst = inst->dst;
2019 struct brw_reg reg;
2020
2021 switch (inst->dst.file) {
2022 case VGRF:
2023 reg = byte_offset(brw_vec8_grf(dst.nr, 0), dst.offset);
2024 reg.type = dst.type;
2025 reg.writemask = dst.writemask;
2026 break;
2027
2028 case MRF:
2029 reg = byte_offset(brw_message_reg(dst.nr), dst.offset);
2030 assert((reg.nr & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(devinfo->gen));
2031 reg.type = dst.type;
2032 reg.writemask = dst.writemask;
2033 break;
2034
2035 case ARF:
2036 case FIXED_GRF:
2037 reg = dst.as_brw_reg();
2038 break;
2039
2040 case BAD_FILE:
2041 reg = brw_null_reg();
2042 reg = retype(reg, dst.type);
2043 break;
2044
2045 case IMM:
2046 case ATTR:
2047 case UNIFORM:
2048 unreachable("not reached");
2049 }
2050
2051 dst = reg;
2052 }
2053 }
2054
2055 static bool
2056 stage_uses_interleaved_attributes(unsigned stage,
2057 enum shader_dispatch_mode dispatch_mode)
2058 {
2059 switch (stage) {
2060 case MESA_SHADER_TESS_EVAL:
2061 return true;
2062 case MESA_SHADER_GEOMETRY:
2063 return dispatch_mode != DISPATCH_MODE_4X2_DUAL_OBJECT;
2064 default:
2065 return false;
2066 }
2067 }
2068
2069 /**
2070 * Get the closest native SIMD width supported by the hardware for instruction
2071 * \p inst. The instruction will be left untouched by
2072 * vec4_visitor::lower_simd_width() if the returned value matches the
2073 * instruction's original execution size.
2074 */
2075 static unsigned
2076 get_lowered_simd_width(const struct gen_device_info *devinfo,
2077 enum shader_dispatch_mode dispatch_mode,
2078 unsigned stage, const vec4_instruction *inst)
2079 {
2080 /* Do not split some instructions that require special handling */
2081 switch (inst->opcode) {
2082 case SHADER_OPCODE_GEN4_SCRATCH_READ:
2083 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
2084 return inst->exec_size;
2085 default:
2086 break;
2087 }
2088
2089 unsigned lowered_width = MIN2(16, inst->exec_size);
2090
2091 /* We need to split some cases of double-precision instructions that write
2092 * 2 registers. We only need to care about this in gen7 because that is the
2093 * only hardware that implements fp64 in Align16.
2094 */
2095 if (devinfo->gen == 7 && inst->size_written > REG_SIZE) {
2096 /* Align16 8-wide double-precision SEL does not work well. Verified
2097 * empirically.
2098 */
2099 if (inst->opcode == BRW_OPCODE_SEL && type_sz(inst->dst.type) == 8)
2100 lowered_width = MIN2(lowered_width, 4);
2101
2102 /* HSW PRM, 3D Media GPGPU Engine, Region Alignment Rules for Direct
2103 * Register Addressing:
2104 *
2105 * "When destination spans two registers, the source MUST span two
2106 * registers."
2107 */
2108 for (unsigned i = 0; i < 3; i++) {
2109 if (inst->src[i].file == BAD_FILE)
2110 continue;
2111 if (inst->size_read(i) <= REG_SIZE)
2112 lowered_width = MIN2(lowered_width, 4);
2113
2114 /* Interleaved attribute setups use a vertical stride of 0, which
2115 * makes them hit the associated instruction decompression bug in gen7.
2116 * Split them to prevent this.
2117 */
2118 if (inst->src[i].file == ATTR &&
2119 stage_uses_interleaved_attributes(stage, dispatch_mode))
2120 lowered_width = MIN2(lowered_width, 4);
2121 }
2122 }
2123
2124 /* IvyBridge can manage a maximum of 4 DFs per SIMD4x2 instruction, since
2125 * it doesn't support compression in Align16 mode, no matter if it has
2126 * force_writemask_all enabled or disabled (the latter is affected by the
2127 * compressed instruction bug in gen7, which is another reason to enforce
2128 * this limit).
2129 */
2130 if (devinfo->gen == 7 && !devinfo->is_haswell &&
2131 (get_exec_type_size(inst) == 8 || type_sz(inst->dst.type) == 8))
2132 lowered_width = MIN2(lowered_width, 4);
2133
2134 return lowered_width;
2135 }
2136
2137 static bool
2138 dst_src_regions_overlap(vec4_instruction *inst)
2139 {
2140 if (inst->size_written == 0)
2141 return false;
2142
2143 unsigned dst_start = inst->dst.offset;
2144 unsigned dst_end = dst_start + inst->size_written - 1;
2145 for (int i = 0; i < 3; i++) {
2146 if (inst->src[i].file == BAD_FILE)
2147 continue;
2148
2149 if (inst->dst.file != inst->src[i].file ||
2150 inst->dst.nr != inst->src[i].nr)
2151 continue;
2152
2153 unsigned src_start = inst->src[i].offset;
2154 unsigned src_end = src_start + inst->size_read(i) - 1;
2155
2156 if ((dst_start >= src_start && dst_start <= src_end) ||
2157 (dst_end >= src_start && dst_end <= src_end) ||
2158 (dst_start <= src_start && dst_end >= src_end)) {
2159 return true;
2160 }
2161 }
2162
2163 return false;
2164 }
2165
2166 bool
2167 vec4_visitor::lower_simd_width()
2168 {
2169 bool progress = false;
2170
2171 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
2172 const unsigned lowered_width =
2173 get_lowered_simd_width(devinfo, prog_data->dispatch_mode, stage, inst);
2174 assert(lowered_width <= inst->exec_size);
2175 if (lowered_width == inst->exec_size)
2176 continue;
2177
2178 /* We need to deal with source / destination overlaps when splitting.
2179 * The hardware supports reading from and writing to the same register
2180 * in the same instruction, but we need to be careful that each split
2181 * instruction we produce does not corrupt the source of the next.
2182 *
2183 * The easiest way to handle this is to make the split instructions write
2184 * to temporaries if there is an src/dst overlap and then move from the
2185 * temporaries to the original destination. We also need to consider
2186 * instructions that do partial writes via align1 opcodes, in which case
2187 * we need to make sure that the we initialize the temporary with the
2188 * value of the instruction's dst.
2189 */
2190 bool needs_temp = dst_src_regions_overlap(inst);
2191 for (unsigned n = 0; n < inst->exec_size / lowered_width; n++) {
2192 unsigned channel_offset = lowered_width * n;
2193
2194 unsigned size_written = lowered_width * type_sz(inst->dst.type);
2195
2196 /* Create the split instruction from the original so that we copy all
2197 * relevant instruction fields, then set the width and calculate the
2198 * new dst/src regions.
2199 */
2200 vec4_instruction *linst = new(mem_ctx) vec4_instruction(*inst);
2201 linst->exec_size = lowered_width;
2202 linst->group = channel_offset;
2203 linst->size_written = size_written;
2204
2205 /* Compute split dst region */
2206 dst_reg dst;
2207 if (needs_temp) {
2208 unsigned num_regs = DIV_ROUND_UP(size_written, REG_SIZE);
2209 dst = retype(dst_reg(VGRF, alloc.allocate(num_regs)),
2210 inst->dst.type);
2211 if (inst->is_align1_partial_write()) {
2212 vec4_instruction *copy = MOV(dst, src_reg(inst->dst));
2213 copy->exec_size = lowered_width;
2214 copy->group = channel_offset;
2215 copy->size_written = size_written;
2216 inst->insert_before(block, copy);
2217 }
2218 } else {
2219 dst = horiz_offset(inst->dst, channel_offset);
2220 }
2221 linst->dst = dst;
2222
2223 /* Compute split source regions */
2224 for (int i = 0; i < 3; i++) {
2225 if (linst->src[i].file == BAD_FILE)
2226 continue;
2227
2228 if (!is_uniform(linst->src[i]))
2229 linst->src[i] = horiz_offset(linst->src[i], channel_offset);
2230 }
2231
2232 inst->insert_before(block, linst);
2233
2234 /* If we used a temporary to store the result of the split
2235 * instruction, copy the result to the original destination
2236 */
2237 if (needs_temp) {
2238 vec4_instruction *mov =
2239 MOV(offset(inst->dst, lowered_width, n), src_reg(dst));
2240 mov->exec_size = lowered_width;
2241 mov->group = channel_offset;
2242 mov->size_written = size_written;
2243 mov->predicate = inst->predicate;
2244 inst->insert_before(block, mov);
2245 }
2246 }
2247
2248 inst->remove(block);
2249 progress = true;
2250 }
2251
2252 if (progress)
2253 invalidate_live_intervals();
2254
2255 return progress;
2256 }
2257
2258 static bool
2259 is_align1_df(vec4_instruction *inst)
2260 {
2261 switch (inst->opcode) {
2262 case VEC4_OPCODE_DOUBLE_TO_F32:
2263 case VEC4_OPCODE_DOUBLE_TO_D32:
2264 case VEC4_OPCODE_DOUBLE_TO_U32:
2265 case VEC4_OPCODE_TO_DOUBLE:
2266 case VEC4_OPCODE_PICK_LOW_32BIT:
2267 case VEC4_OPCODE_PICK_HIGH_32BIT:
2268 case VEC4_OPCODE_SET_LOW_32BIT:
2269 case VEC4_OPCODE_SET_HIGH_32BIT:
2270 return true;
2271 default:
2272 return false;
2273 }
2274 }
2275
2276 static brw_predicate
2277 scalarize_predicate(brw_predicate predicate, unsigned writemask)
2278 {
2279 if (predicate != BRW_PREDICATE_NORMAL)
2280 return predicate;
2281
2282 switch (writemask) {
2283 case WRITEMASK_X:
2284 return BRW_PREDICATE_ALIGN16_REPLICATE_X;
2285 case WRITEMASK_Y:
2286 return BRW_PREDICATE_ALIGN16_REPLICATE_Y;
2287 case WRITEMASK_Z:
2288 return BRW_PREDICATE_ALIGN16_REPLICATE_Z;
2289 case WRITEMASK_W:
2290 return BRW_PREDICATE_ALIGN16_REPLICATE_W;
2291 default:
2292 unreachable("invalid writemask");
2293 }
2294 }
2295
2296 /* Gen7 has a hardware decompression bug that we can exploit to represent
2297 * handful of additional swizzles natively.
2298 */
2299 static bool
2300 is_gen7_supported_64bit_swizzle(vec4_instruction *inst, unsigned arg)
2301 {
2302 switch (inst->src[arg].swizzle) {
2303 case BRW_SWIZZLE_XXXX:
2304 case BRW_SWIZZLE_YYYY:
2305 case BRW_SWIZZLE_ZZZZ:
2306 case BRW_SWIZZLE_WWWW:
2307 case BRW_SWIZZLE_XYXY:
2308 case BRW_SWIZZLE_YXYX:
2309 case BRW_SWIZZLE_ZWZW:
2310 case BRW_SWIZZLE_WZWZ:
2311 return true;
2312 default:
2313 return false;
2314 }
2315 }
2316
2317 /* 64-bit sources use regions with a width of 2. These 2 elements in each row
2318 * can be addressed using 32-bit swizzles (which is what the hardware supports)
2319 * but it also means that the swizzle we apply on the first two components of a
2320 * dvec4 is coupled with the swizzle we use for the last 2. In other words,
2321 * only some specific swizzle combinations can be natively supported.
2322 *
2323 * FIXME: we can go an step further and implement even more swizzle
2324 * variations using only partial scalarization.
2325 *
2326 * For more details see:
2327 * https://bugs.freedesktop.org/show_bug.cgi?id=92760#c82
2328 */
2329 bool
2330 vec4_visitor::is_supported_64bit_region(vec4_instruction *inst, unsigned arg)
2331 {
2332 const src_reg &src = inst->src[arg];
2333 assert(type_sz(src.type) == 8);
2334
2335 /* Uniform regions have a vstride=0. Because we use 2-wide rows with
2336 * 64-bit regions it means that we cannot access components Z/W, so
2337 * return false for any such case. Interleaved attributes will also be
2338 * mapped to GRF registers with a vstride of 0, so apply the same
2339 * treatment.
2340 */
2341 if ((is_uniform(src) ||
2342 (stage_uses_interleaved_attributes(stage, prog_data->dispatch_mode) &&
2343 src.file == ATTR)) &&
2344 (brw_mask_for_swizzle(src.swizzle) & 12))
2345 return false;
2346
2347 switch (src.swizzle) {
2348 case BRW_SWIZZLE_XYZW:
2349 case BRW_SWIZZLE_XXZZ:
2350 case BRW_SWIZZLE_YYWW:
2351 case BRW_SWIZZLE_YXWZ:
2352 return true;
2353 default:
2354 return devinfo->gen == 7 && is_gen7_supported_64bit_swizzle(inst, arg);
2355 }
2356 }
2357
2358 bool
2359 vec4_visitor::scalarize_df()
2360 {
2361 bool progress = false;
2362
2363 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
2364 /* Skip DF instructions that operate in Align1 mode */
2365 if (is_align1_df(inst))
2366 continue;
2367
2368 /* Check if this is a double-precision instruction */
2369 bool is_double = type_sz(inst->dst.type) == 8;
2370 for (int arg = 0; !is_double && arg < 3; arg++) {
2371 is_double = inst->src[arg].file != BAD_FILE &&
2372 type_sz(inst->src[arg].type) == 8;
2373 }
2374
2375 if (!is_double)
2376 continue;
2377
2378 /* Skip the lowering for specific regioning scenarios that we can
2379 * support natively.
2380 */
2381 bool skip_lowering = true;
2382
2383 /* XY and ZW writemasks operate in 32-bit, which means that they don't
2384 * have a native 64-bit representation and they should always be split.
2385 */
2386 if (inst->dst.writemask == WRITEMASK_XY ||
2387 inst->dst.writemask == WRITEMASK_ZW) {
2388 skip_lowering = false;
2389 } else {
2390 for (unsigned i = 0; i < 3; i++) {
2391 if (inst->src[i].file == BAD_FILE || type_sz(inst->src[i].type) < 8)
2392 continue;
2393 skip_lowering = skip_lowering && is_supported_64bit_region(inst, i);
2394 }
2395 }
2396
2397 if (skip_lowering)
2398 continue;
2399
2400 /* Generate scalar instructions for each enabled channel */
2401 for (unsigned chan = 0; chan < 4; chan++) {
2402 unsigned chan_mask = 1 << chan;
2403 if (!(inst->dst.writemask & chan_mask))
2404 continue;
2405
2406 vec4_instruction *scalar_inst = new(mem_ctx) vec4_instruction(*inst);
2407
2408 for (unsigned i = 0; i < 3; i++) {
2409 unsigned swz = BRW_GET_SWZ(inst->src[i].swizzle, chan);
2410 scalar_inst->src[i].swizzle = BRW_SWIZZLE4(swz, swz, swz, swz);
2411 }
2412
2413 scalar_inst->dst.writemask = chan_mask;
2414
2415 if (inst->predicate != BRW_PREDICATE_NONE) {
2416 scalar_inst->predicate =
2417 scalarize_predicate(inst->predicate, chan_mask);
2418 }
2419
2420 inst->insert_before(block, scalar_inst);
2421 }
2422
2423 inst->remove(block);
2424 progress = true;
2425 }
2426
2427 if (progress)
2428 invalidate_live_intervals();
2429
2430 return progress;
2431 }
2432
2433 bool
2434 vec4_visitor::lower_64bit_mad_to_mul_add()
2435 {
2436 bool progress = false;
2437
2438 foreach_block_and_inst_safe(block, vec4_instruction, inst, cfg) {
2439 if (inst->opcode != BRW_OPCODE_MAD)
2440 continue;
2441
2442 if (type_sz(inst->dst.type) != 8)
2443 continue;
2444
2445 dst_reg mul_dst = dst_reg(this, glsl_type::dvec4_type);
2446
2447 /* Use the copy constructor so we copy all relevant instruction fields
2448 * from the original mad into the add and mul instructions
2449 */
2450 vec4_instruction *mul = new(mem_ctx) vec4_instruction(*inst);
2451 mul->opcode = BRW_OPCODE_MUL;
2452 mul->dst = mul_dst;
2453 mul->src[0] = inst->src[1];
2454 mul->src[1] = inst->src[2];
2455 mul->src[2].file = BAD_FILE;
2456
2457 vec4_instruction *add = new(mem_ctx) vec4_instruction(*inst);
2458 add->opcode = BRW_OPCODE_ADD;
2459 add->src[0] = src_reg(mul_dst);
2460 add->src[1] = inst->src[0];
2461 add->src[2].file = BAD_FILE;
2462
2463 inst->insert_before(block, mul);
2464 inst->insert_before(block, add);
2465 inst->remove(block);
2466
2467 progress = true;
2468 }
2469
2470 if (progress)
2471 invalidate_live_intervals();
2472
2473 return progress;
2474 }
2475
2476 /* The align16 hardware can only do 32-bit swizzle channels, so we need to
2477 * translate the logical 64-bit swizzle channels that we use in the Vec4 IR
2478 * to 32-bit swizzle channels in hardware registers.
2479 *
2480 * @inst and @arg identify the original vec4 IR source operand we need to
2481 * translate the swizzle for and @hw_reg is the hardware register where we
2482 * will write the hardware swizzle to use.
2483 *
2484 * This pass assumes that Align16/DF instructions have been fully scalarized
2485 * previously so there is just one 64-bit swizzle channel to deal with for any
2486 * given Vec4 IR source.
2487 */
2488 void
2489 vec4_visitor::apply_logical_swizzle(struct brw_reg *hw_reg,
2490 vec4_instruction *inst, int arg)
2491 {
2492 src_reg reg = inst->src[arg];
2493
2494 if (reg.file == BAD_FILE || reg.file == BRW_IMMEDIATE_VALUE)
2495 return;
2496
2497 /* If this is not a 64-bit operand or this is a scalar instruction we don't
2498 * need to do anything about the swizzles.
2499 */
2500 if(type_sz(reg.type) < 8 || is_align1_df(inst)) {
2501 hw_reg->swizzle = reg.swizzle;
2502 return;
2503 }
2504
2505 /* Take the 64-bit logical swizzle channel and translate it to 32-bit */
2506 assert(brw_is_single_value_swizzle(reg.swizzle) ||
2507 is_supported_64bit_region(inst, arg));
2508
2509 if (is_supported_64bit_region(inst, arg) &&
2510 !is_gen7_supported_64bit_swizzle(inst, arg)) {
2511 /* Supported 64-bit swizzles are those such that their first two
2512 * components, when expanded to 32-bit swizzles, match the semantics
2513 * of the original 64-bit swizzle with 2-wide row regioning.
2514 */
2515 unsigned swizzle0 = BRW_GET_SWZ(reg.swizzle, 0);
2516 unsigned swizzle1 = BRW_GET_SWZ(reg.swizzle, 1);
2517 hw_reg->swizzle = BRW_SWIZZLE4(swizzle0 * 2, swizzle0 * 2 + 1,
2518 swizzle1 * 2, swizzle1 * 2 + 1);
2519 } else {
2520 /* If we got here then we have one of the following:
2521 *
2522 * 1. An unsupported swizzle, which should be single-value thanks to the
2523 * scalarization pass.
2524 *
2525 * 2. A gen7 supported swizzle. These can be single-value or double-value
2526 * swizzles. If the latter, they are never cross-dvec2 channels. For
2527 * these we always need to activate the gen7 vstride=0 exploit.
2528 */
2529 unsigned swizzle0 = BRW_GET_SWZ(reg.swizzle, 0);
2530 unsigned swizzle1 = BRW_GET_SWZ(reg.swizzle, 1);
2531 assert((swizzle0 < 2) == (swizzle1 < 2));
2532
2533 /* To gain access to Z/W components we need to select the second half
2534 * of the register and then use a X/Y swizzle to select Z/W respectively.
2535 */
2536 if (swizzle0 >= 2) {
2537 *hw_reg = suboffset(*hw_reg, 2);
2538 swizzle0 -= 2;
2539 swizzle1 -= 2;
2540 }
2541
2542 /* All gen7-specific supported swizzles require the vstride=0 exploit */
2543 if (devinfo->gen == 7 && is_gen7_supported_64bit_swizzle(inst, arg))
2544 hw_reg->vstride = BRW_VERTICAL_STRIDE_0;
2545
2546 /* Any 64-bit source with an offset at 16B is intended to address the
2547 * second half of a register and needs a vertical stride of 0 so we:
2548 *
2549 * 1. Don't violate register region restrictions.
2550 * 2. Activate the gen7 instruction decompresion bug exploit when
2551 * execsize > 4
2552 */
2553 if (hw_reg->subnr % REG_SIZE == 16) {
2554 assert(devinfo->gen == 7);
2555 hw_reg->vstride = BRW_VERTICAL_STRIDE_0;
2556 }
2557
2558 hw_reg->swizzle = BRW_SWIZZLE4(swizzle0 * 2, swizzle0 * 2 + 1,
2559 swizzle1 * 2, swizzle1 * 2 + 1);
2560 }
2561 }
2562
2563 bool
2564 vec4_visitor::run()
2565 {
2566 if (shader_time_index >= 0)
2567 emit_shader_time_begin();
2568
2569 emit_prolog();
2570
2571 emit_nir_code();
2572 if (failed)
2573 return false;
2574 base_ir = NULL;
2575
2576 emit_thread_end();
2577
2578 calculate_cfg();
2579
2580 /* Before any optimization, push array accesses out to scratch
2581 * space where we need them to be. This pass may allocate new
2582 * virtual GRFs, so we want to do it early. It also makes sure
2583 * that we have reladdr computations available for CSE, since we'll
2584 * often do repeated subexpressions for those.
2585 */
2586 move_grf_array_access_to_scratch();
2587 move_uniform_array_access_to_pull_constants();
2588
2589 pack_uniform_registers();
2590 move_push_constants_to_pull_constants();
2591 split_virtual_grfs();
2592
2593 #define OPT(pass, args...) ({ \
2594 pass_num++; \
2595 bool this_progress = pass(args); \
2596 \
2597 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
2598 char filename[64]; \
2599 snprintf(filename, 64, "%s-%s-%02d-%02d-" #pass, \
2600 stage_abbrev, nir->info->name, iteration, pass_num); \
2601 \
2602 backend_shader::dump_instructions(filename); \
2603 } \
2604 \
2605 progress = progress || this_progress; \
2606 this_progress; \
2607 })
2608
2609
2610 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER)) {
2611 char filename[64];
2612 snprintf(filename, 64, "%s-%s-00-00-start",
2613 stage_abbrev, nir->info->name);
2614
2615 backend_shader::dump_instructions(filename);
2616 }
2617
2618 bool progress;
2619 int iteration = 0;
2620 int pass_num = 0;
2621 do {
2622 progress = false;
2623 pass_num = 0;
2624 iteration++;
2625
2626 OPT(opt_predicated_break, this);
2627 OPT(opt_reduce_swizzle);
2628 OPT(dead_code_eliminate);
2629 OPT(dead_control_flow_eliminate, this);
2630 OPT(opt_copy_propagation);
2631 OPT(opt_cmod_propagation);
2632 OPT(opt_cse);
2633 OPT(opt_algebraic);
2634 OPT(opt_register_coalesce);
2635 OPT(eliminate_find_live_channel);
2636 } while (progress);
2637
2638 pass_num = 0;
2639
2640 if (OPT(opt_vector_float)) {
2641 OPT(opt_cse);
2642 OPT(opt_copy_propagation, false);
2643 OPT(opt_copy_propagation, true);
2644 OPT(dead_code_eliminate);
2645 }
2646
2647 if (devinfo->gen <= 5 && OPT(lower_minmax)) {
2648 OPT(opt_cmod_propagation);
2649 OPT(opt_cse);
2650 OPT(opt_copy_propagation);
2651 OPT(dead_code_eliminate);
2652 }
2653
2654 if (OPT(lower_simd_width)) {
2655 OPT(opt_copy_propagation);
2656 OPT(dead_code_eliminate);
2657 }
2658
2659 if (failed)
2660 return false;
2661
2662 OPT(lower_64bit_mad_to_mul_add);
2663
2664 /* Run this before payload setup because tesselation shaders
2665 * rely on it to prevent cross dvec2 regioning on DF attributes
2666 * that are setup so that XY are on the second half of register and
2667 * ZW are in the first half of the next.
2668 */
2669 OPT(scalarize_df);
2670
2671 setup_payload();
2672
2673 if (unlikely(INTEL_DEBUG & DEBUG_SPILL_VEC4)) {
2674 /* Debug of register spilling: Go spill everything. */
2675 const int grf_count = alloc.count;
2676 float spill_costs[alloc.count];
2677 bool no_spill[alloc.count];
2678 evaluate_spill_costs(spill_costs, no_spill);
2679 for (int i = 0; i < grf_count; i++) {
2680 if (no_spill[i])
2681 continue;
2682 spill_reg(i);
2683 }
2684
2685 /* We want to run this after spilling because 64-bit (un)spills need to
2686 * emit code to shuffle 64-bit data for the 32-bit scratch read/write
2687 * messages that can produce unsupported 64-bit swizzle regions.
2688 */
2689 OPT(scalarize_df);
2690 }
2691
2692 bool allocated_without_spills = reg_allocate();
2693
2694 if (!allocated_without_spills) {
2695 compiler->shader_perf_log(log_data,
2696 "%s shader triggered register spilling. "
2697 "Try reducing the number of live vec4 values "
2698 "to improve performance.\n",
2699 stage_name);
2700
2701 while (!reg_allocate()) {
2702 if (failed)
2703 return false;
2704 }
2705
2706 /* We want to run this after spilling because 64-bit (un)spills need to
2707 * emit code to shuffle 64-bit data for the 32-bit scratch read/write
2708 * messages that can produce unsupported 64-bit swizzle regions.
2709 */
2710 OPT(scalarize_df);
2711 }
2712
2713 opt_schedule_instructions();
2714
2715 opt_set_dependency_control();
2716
2717 convert_to_hw_regs();
2718
2719 if (last_scratch > 0) {
2720 prog_data->base.total_scratch =
2721 brw_get_scratch_size(last_scratch * REG_SIZE);
2722 }
2723
2724 return !failed;
2725 }
2726
2727 } /* namespace brw */
2728
2729 extern "C" {
2730
2731 /**
2732 * Compile a vertex shader.
2733 *
2734 * Returns the final assembly and the program's size.
2735 */
2736 const unsigned *
2737 brw_compile_vs(const struct brw_compiler *compiler, void *log_data,
2738 void *mem_ctx,
2739 const struct brw_vs_prog_key *key,
2740 struct brw_vs_prog_data *prog_data,
2741 const nir_shader *src_shader,
2742 gl_clip_plane *clip_planes,
2743 bool use_legacy_snorm_formula,
2744 int shader_time_index,
2745 unsigned *final_assembly_size,
2746 char **error_str)
2747 {
2748 const bool is_scalar = compiler->scalar_stage[MESA_SHADER_VERTEX];
2749 nir_shader *shader = nir_shader_clone(mem_ctx, src_shader);
2750 shader = brw_nir_apply_sampler_key(shader, compiler, &key->tex, is_scalar);
2751 brw_nir_lower_vs_inputs(shader, is_scalar,
2752 use_legacy_snorm_formula, key->gl_attrib_wa_flags);
2753 brw_nir_lower_vue_outputs(shader, is_scalar);
2754 shader = brw_postprocess_nir(shader, compiler, is_scalar);
2755
2756 const unsigned *assembly = NULL;
2757
2758 prog_data->base.clip_distance_mask =
2759 ((1 << shader->info->clip_distance_array_size) - 1);
2760 prog_data->base.cull_distance_mask =
2761 ((1 << shader->info->cull_distance_array_size) - 1) <<
2762 shader->info->clip_distance_array_size;
2763
2764 unsigned nr_attribute_slots = _mesa_bitcount_64(prog_data->inputs_read);
2765
2766 /* gl_VertexID and gl_InstanceID are system values, but arrive via an
2767 * incoming vertex attribute. So, add an extra slot.
2768 */
2769 if (shader->info->system_values_read &
2770 (BITFIELD64_BIT(SYSTEM_VALUE_BASE_VERTEX) |
2771 BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE) |
2772 BITFIELD64_BIT(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) |
2773 BITFIELD64_BIT(SYSTEM_VALUE_INSTANCE_ID))) {
2774 nr_attribute_slots++;
2775 }
2776
2777 /* gl_DrawID has its very own vec4 */
2778 if (shader->info->system_values_read &
2779 BITFIELD64_BIT(SYSTEM_VALUE_DRAW_ID)) {
2780 nr_attribute_slots++;
2781 }
2782
2783 unsigned nr_attributes = nr_attribute_slots -
2784 DIV_ROUND_UP(_mesa_bitcount_64(shader->info->double_inputs_read), 2);
2785
2786 /* The 3DSTATE_VS documentation lists the lower bound on "Vertex URB Entry
2787 * Read Length" as 1 in vec4 mode, and 0 in SIMD8 mode. Empirically, in
2788 * vec4 mode, the hardware appears to wedge unless we read something.
2789 */
2790 if (is_scalar)
2791 prog_data->base.urb_read_length =
2792 DIV_ROUND_UP(nr_attribute_slots, 2);
2793 else
2794 prog_data->base.urb_read_length =
2795 DIV_ROUND_UP(MAX2(nr_attribute_slots, 1), 2);
2796
2797 prog_data->nr_attributes = nr_attributes;
2798 prog_data->nr_attribute_slots = nr_attribute_slots;
2799
2800 /* Since vertex shaders reuse the same VUE entry for inputs and outputs
2801 * (overwriting the original contents), we need to make sure the size is
2802 * the larger of the two.
2803 */
2804 const unsigned vue_entries =
2805 MAX2(nr_attribute_slots, (unsigned)prog_data->base.vue_map.num_slots);
2806
2807 if (compiler->devinfo->gen == 6)
2808 prog_data->base.urb_entry_size = DIV_ROUND_UP(vue_entries, 8);
2809 else
2810 prog_data->base.urb_entry_size = DIV_ROUND_UP(vue_entries, 4);
2811
2812 if (INTEL_DEBUG & DEBUG_VS) {
2813 fprintf(stderr, "VS Output ");
2814 brw_print_vue_map(stderr, &prog_data->base.vue_map);
2815 }
2816
2817 if (is_scalar) {
2818 prog_data->base.dispatch_mode = DISPATCH_MODE_SIMD8;
2819
2820 fs_visitor v(compiler, log_data, mem_ctx, key, &prog_data->base.base,
2821 NULL, /* prog; Only used for TEXTURE_RECTANGLE on gen < 8 */
2822 shader, 8, shader_time_index);
2823 if (!v.run_vs(clip_planes)) {
2824 if (error_str)
2825 *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
2826
2827 return NULL;
2828 }
2829
2830 prog_data->base.base.dispatch_grf_start_reg = v.payload.num_regs;
2831
2832 fs_generator g(compiler, log_data, mem_ctx, (void *) key,
2833 &prog_data->base.base, v.promoted_constants,
2834 v.runtime_check_aads_emit, MESA_SHADER_VERTEX);
2835 if (INTEL_DEBUG & DEBUG_VS) {
2836 const char *debug_name =
2837 ralloc_asprintf(mem_ctx, "%s vertex shader %s",
2838 shader->info->label ? shader->info->label :
2839 "unnamed",
2840 shader->info->name);
2841
2842 g.enable_debug(debug_name);
2843 }
2844 g.generate_code(v.cfg, 8);
2845 assembly = g.get_assembly(final_assembly_size);
2846 }
2847
2848 if (!assembly) {
2849 prog_data->base.dispatch_mode = DISPATCH_MODE_4X2_DUAL_OBJECT;
2850
2851 vec4_vs_visitor v(compiler, log_data, key, prog_data,
2852 shader, clip_planes, mem_ctx,
2853 shader_time_index, use_legacy_snorm_formula);
2854 if (!v.run()) {
2855 if (error_str)
2856 *error_str = ralloc_strdup(mem_ctx, v.fail_msg);
2857
2858 return NULL;
2859 }
2860
2861 assembly = brw_vec4_generate_assembly(compiler, log_data, mem_ctx,
2862 shader, &prog_data->base, v.cfg,
2863 final_assembly_size);
2864 }
2865
2866 return assembly;
2867 }
2868
2869 } /* extern "C" */