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