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