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