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