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