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