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