bfb0fba39871f7e6effe03a73d6cf4df92920b2f
[mesa.git] / src / mesa / drivers / dri / i965 / 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_cfg.h"
26 #include "brw_vs.h"
27 #include "brw_dead_control_flow.h"
28
29 extern "C" {
30 #include "main/macros.h"
31 #include "main/shaderobj.h"
32 #include "program/prog_print.h"
33 #include "program/prog_parameter.h"
34 }
35
36 #define MAX_INSTRUCTION (1 << 30)
37
38 using namespace brw;
39
40 namespace brw {
41
42 /**
43 * Common helper for constructing swizzles. When only a subset of
44 * channels of a vec4 are used, we don't want to reference the other
45 * channels, as that will tell optimization passes that those other
46 * channels are used.
47 */
48 unsigned
49 swizzle_for_size(int size)
50 {
51 static const unsigned size_swizzles[4] = {
52 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X),
53 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Y, SWIZZLE_Y),
54 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z),
55 BRW_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W),
56 };
57
58 assert((size >= 1) && (size <= 4));
59 return size_swizzles[size - 1];
60 }
61
62 void
63 src_reg::init()
64 {
65 memset(this, 0, sizeof(*this));
66
67 this->file = BAD_FILE;
68 }
69
70 src_reg::src_reg(register_file file, int reg, const glsl_type *type)
71 {
72 init();
73
74 this->file = file;
75 this->reg = reg;
76 if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
77 this->swizzle = swizzle_for_size(type->vector_elements);
78 else
79 this->swizzle = BRW_SWIZZLE_XYZW;
80 }
81
82 /** Generic unset register constructor. */
83 src_reg::src_reg()
84 {
85 init();
86 }
87
88 src_reg::src_reg(float f)
89 {
90 init();
91
92 this->file = IMM;
93 this->type = BRW_REGISTER_TYPE_F;
94 this->fixed_hw_reg.dw1.f = f;
95 }
96
97 src_reg::src_reg(uint32_t u)
98 {
99 init();
100
101 this->file = IMM;
102 this->type = BRW_REGISTER_TYPE_UD;
103 this->fixed_hw_reg.dw1.ud = u;
104 }
105
106 src_reg::src_reg(int32_t i)
107 {
108 init();
109
110 this->file = IMM;
111 this->type = BRW_REGISTER_TYPE_D;
112 this->fixed_hw_reg.dw1.d = i;
113 }
114
115 src_reg::src_reg(struct brw_reg reg)
116 {
117 init();
118
119 this->file = HW_REG;
120 this->fixed_hw_reg = reg;
121 this->type = reg.type;
122 }
123
124 src_reg::src_reg(dst_reg reg)
125 {
126 init();
127
128 this->file = reg.file;
129 this->reg = reg.reg;
130 this->reg_offset = reg.reg_offset;
131 this->type = reg.type;
132 this->reladdr = reg.reladdr;
133 this->fixed_hw_reg = reg.fixed_hw_reg;
134
135 int swizzles[4];
136 int next_chan = 0;
137 int last = 0;
138
139 for (int i = 0; i < 4; i++) {
140 if (!(reg.writemask & (1 << i)))
141 continue;
142
143 swizzles[next_chan++] = last = i;
144 }
145
146 for (; next_chan < 4; next_chan++) {
147 swizzles[next_chan] = last;
148 }
149
150 this->swizzle = BRW_SWIZZLE4(swizzles[0], swizzles[1],
151 swizzles[2], swizzles[3]);
152 }
153
154 void
155 dst_reg::init()
156 {
157 memset(this, 0, sizeof(*this));
158 this->file = BAD_FILE;
159 this->writemask = WRITEMASK_XYZW;
160 }
161
162 dst_reg::dst_reg()
163 {
164 init();
165 }
166
167 dst_reg::dst_reg(register_file file, int reg)
168 {
169 init();
170
171 this->file = file;
172 this->reg = reg;
173 }
174
175 dst_reg::dst_reg(register_file file, int reg, const glsl_type *type,
176 int writemask)
177 {
178 init();
179
180 this->file = file;
181 this->reg = reg;
182 this->type = brw_type_for_base_type(type);
183 this->writemask = writemask;
184 }
185
186 dst_reg::dst_reg(struct brw_reg reg)
187 {
188 init();
189
190 this->file = HW_REG;
191 this->fixed_hw_reg = reg;
192 this->type = reg.type;
193 }
194
195 dst_reg::dst_reg(src_reg reg)
196 {
197 init();
198
199 this->file = reg.file;
200 this->reg = reg.reg;
201 this->reg_offset = reg.reg_offset;
202 this->type = reg.type;
203 /* How should we do writemasking when converting from a src_reg? It seems
204 * pretty obvious that for src.xxxx the caller wants to write to src.x, but
205 * what about for src.wx? Just special-case src.xxxx for now.
206 */
207 if (reg.swizzle == BRW_SWIZZLE_XXXX)
208 this->writemask = WRITEMASK_X;
209 else
210 this->writemask = WRITEMASK_XYZW;
211 this->reladdr = reg.reladdr;
212 this->fixed_hw_reg = reg.fixed_hw_reg;
213 }
214
215 bool
216 vec4_instruction::is_send_from_grf()
217 {
218 switch (opcode) {
219 case SHADER_OPCODE_SHADER_TIME_ADD:
220 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
221 return true;
222 default:
223 return false;
224 }
225 }
226
227 bool
228 vec4_instruction::can_do_source_mods(struct brw_context *brw)
229 {
230 if (brw->gen == 6 && is_math())
231 return false;
232
233 if (is_send_from_grf())
234 return false;
235
236 if (!backend_instruction::can_do_source_mods())
237 return false;
238
239 return true;
240 }
241
242 /**
243 * Returns how many MRFs an opcode will write over.
244 *
245 * Note that this is not the 0 or 1 implied writes in an actual gen
246 * instruction -- the generate_* functions generate additional MOVs
247 * for setup.
248 */
249 int
250 vec4_visitor::implied_mrf_writes(vec4_instruction *inst)
251 {
252 if (inst->mlen == 0)
253 return 0;
254
255 switch (inst->opcode) {
256 case SHADER_OPCODE_RCP:
257 case SHADER_OPCODE_RSQ:
258 case SHADER_OPCODE_SQRT:
259 case SHADER_OPCODE_EXP2:
260 case SHADER_OPCODE_LOG2:
261 case SHADER_OPCODE_SIN:
262 case SHADER_OPCODE_COS:
263 return 1;
264 case SHADER_OPCODE_INT_QUOTIENT:
265 case SHADER_OPCODE_INT_REMAINDER:
266 case SHADER_OPCODE_POW:
267 return 2;
268 case VS_OPCODE_URB_WRITE:
269 return 1;
270 case VS_OPCODE_PULL_CONSTANT_LOAD:
271 return 2;
272 case SHADER_OPCODE_GEN4_SCRATCH_READ:
273 return 2;
274 case SHADER_OPCODE_GEN4_SCRATCH_WRITE:
275 return 3;
276 case GS_OPCODE_URB_WRITE:
277 case GS_OPCODE_THREAD_END:
278 return 0;
279 case SHADER_OPCODE_SHADER_TIME_ADD:
280 return 0;
281 case SHADER_OPCODE_TEX:
282 case SHADER_OPCODE_TXL:
283 case SHADER_OPCODE_TXD:
284 case SHADER_OPCODE_TXF:
285 case SHADER_OPCODE_TXF_CMS:
286 case SHADER_OPCODE_TXF_MCS:
287 case SHADER_OPCODE_TXS:
288 case SHADER_OPCODE_TG4:
289 case SHADER_OPCODE_TG4_OFFSET:
290 return inst->header_present ? 1 : 0;
291 case SHADER_OPCODE_UNTYPED_ATOMIC:
292 case SHADER_OPCODE_UNTYPED_SURFACE_READ:
293 return 0;
294 default:
295 unreachable("not reached");
296 }
297 }
298
299 bool
300 src_reg::equals(const src_reg &r) const
301 {
302 return (file == r.file &&
303 reg == r.reg &&
304 reg_offset == r.reg_offset &&
305 type == r.type &&
306 negate == r.negate &&
307 abs == r.abs &&
308 swizzle == r.swizzle &&
309 !reladdr && !r.reladdr &&
310 memcmp(&fixed_hw_reg, &r.fixed_hw_reg,
311 sizeof(fixed_hw_reg)) == 0);
312 }
313
314 static bool
315 try_eliminate_instruction(vec4_instruction *inst, int new_writemask,
316 const struct brw_context *brw)
317 {
318 if (inst->has_side_effects())
319 return false;
320
321 if (new_writemask == 0) {
322 /* Don't dead code eliminate instructions that write to the
323 * accumulator as a side-effect. Instead just set the destination
324 * to the null register to free it.
325 */
326 if (inst->writes_accumulator || inst->writes_flag()) {
327 inst->dst = dst_reg(retype(brw_null_reg(), inst->dst.type));
328 } else {
329 inst->remove();
330 }
331
332 return true;
333 } else if (inst->dst.writemask != new_writemask) {
334 switch (inst->opcode) {
335 case SHADER_OPCODE_TXF_CMS:
336 case SHADER_OPCODE_GEN4_SCRATCH_READ:
337 case VS_OPCODE_PULL_CONSTANT_LOAD:
338 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
339 break;
340 default:
341 /* Do not set a writemask on Gen6 for math instructions, those are
342 * executed using align1 mode that does not support a destination mask.
343 */
344 if (!(brw->gen == 6 && inst->is_math()) && !inst->is_tex()) {
345 inst->dst.writemask = new_writemask;
346 return true;
347 }
348 }
349 }
350
351 return false;
352 }
353
354 /**
355 * Must be called after calculate_live_intervals() to remove unused
356 * writes to registers -- register allocation will fail otherwise
357 * because something deffed but not used won't be considered to
358 * interfere with other regs.
359 */
360 bool
361 vec4_visitor::dead_code_eliminate()
362 {
363 bool progress = false;
364 int pc = -1;
365
366 calculate_live_intervals();
367
368 foreach_in_list_safe(vec4_instruction, inst, &instructions) {
369 pc++;
370
371 bool inst_writes_flag = false;
372 if (inst->dst.file != GRF) {
373 if (inst->dst.is_null() && inst->writes_flag()) {
374 inst_writes_flag = true;
375 } else {
376 continue;
377 }
378 }
379
380 if (inst->dst.file == GRF) {
381 int write_mask = inst->dst.writemask;
382
383 for (int c = 0; c < 4; c++) {
384 if (write_mask & (1 << c)) {
385 assert(this->virtual_grf_end[inst->dst.reg * 4 + c] >= pc);
386 if (this->virtual_grf_end[inst->dst.reg * 4 + c] == pc) {
387 write_mask &= ~(1 << c);
388 }
389 }
390 }
391
392 progress = try_eliminate_instruction(inst, write_mask, brw) ||
393 progress;
394 }
395
396 if (inst->predicate || inst->prev == NULL)
397 continue;
398
399 int dead_channels;
400 if (inst_writes_flag) {
401 /* Arbitrarily chosen, other than not being an xyzw writemask. */
402 #define FLAG_WRITEMASK (1 << 5)
403 dead_channels = inst->reads_flag() ? 0 : FLAG_WRITEMASK;
404 } else {
405 dead_channels = inst->dst.writemask;
406
407 for (int i = 0; i < 3; i++) {
408 if (inst->src[i].file != GRF ||
409 inst->src[i].reg != inst->dst.reg)
410 continue;
411
412 for (int j = 0; j < 4; j++) {
413 int swiz = BRW_GET_SWZ(inst->src[i].swizzle, j);
414 dead_channels &= ~(1 << swiz);
415 }
416 }
417 }
418
419 for (exec_node *node = inst->prev, *prev = node->prev;
420 prev != NULL && dead_channels != 0;
421 node = prev, prev = prev->prev) {
422 vec4_instruction *scan_inst = (vec4_instruction *)node;
423
424 if (scan_inst->is_control_flow())
425 break;
426
427 if (inst_writes_flag) {
428 if (scan_inst->dst.is_null() && scan_inst->writes_flag()) {
429 scan_inst->remove();
430 progress = true;
431 continue;
432 } else if (scan_inst->reads_flag()) {
433 break;
434 }
435 }
436
437 if (inst->dst.file == scan_inst->dst.file &&
438 inst->dst.reg == scan_inst->dst.reg &&
439 inst->dst.reg_offset == scan_inst->dst.reg_offset) {
440 int new_writemask = scan_inst->dst.writemask & ~dead_channels;
441
442 progress = try_eliminate_instruction(scan_inst, new_writemask, brw) ||
443 progress;
444 }
445
446 for (int i = 0; i < 3; i++) {
447 if (scan_inst->src[i].file != inst->dst.file ||
448 scan_inst->src[i].reg != inst->dst.reg)
449 continue;
450
451 for (int j = 0; j < 4; j++) {
452 int swiz = BRW_GET_SWZ(scan_inst->src[i].swizzle, j);
453 dead_channels &= ~(1 << swiz);
454 }
455 }
456 }
457 }
458
459 if (progress)
460 invalidate_live_intervals();
461
462 return progress;
463 }
464
465 void
466 vec4_visitor::split_uniform_registers()
467 {
468 /* Prior to this, uniforms have been in an array sized according to
469 * the number of vector uniforms present, sparsely filled (so an
470 * aggregate results in reg indices being skipped over). Now we're
471 * going to cut those aggregates up so each .reg index is one
472 * vector. The goal is to make elimination of unused uniform
473 * components easier later.
474 */
475 foreach_in_list(vec4_instruction, inst, &instructions) {
476 for (int i = 0 ; i < 3; i++) {
477 if (inst->src[i].file != UNIFORM)
478 continue;
479
480 assert(!inst->src[i].reladdr);
481
482 inst->src[i].reg += inst->src[i].reg_offset;
483 inst->src[i].reg_offset = 0;
484 }
485 }
486
487 /* Update that everything is now vector-sized. */
488 for (int i = 0; i < this->uniforms; i++) {
489 this->uniform_size[i] = 1;
490 }
491 }
492
493 void
494 vec4_visitor::pack_uniform_registers()
495 {
496 bool uniform_used[this->uniforms];
497 int new_loc[this->uniforms];
498 int new_chan[this->uniforms];
499
500 memset(uniform_used, 0, sizeof(uniform_used));
501 memset(new_loc, 0, sizeof(new_loc));
502 memset(new_chan, 0, sizeof(new_chan));
503
504 /* Find which uniform vectors are actually used by the program. We
505 * expect unused vector elements when we've moved array access out
506 * to pull constants, and from some GLSL code generators like wine.
507 */
508 foreach_in_list(vec4_instruction, inst, &instructions) {
509 for (int i = 0 ; i < 3; i++) {
510 if (inst->src[i].file != UNIFORM)
511 continue;
512
513 uniform_used[inst->src[i].reg] = true;
514 }
515 }
516
517 int new_uniform_count = 0;
518
519 /* Now, figure out a packing of the live uniform vectors into our
520 * push constants.
521 */
522 for (int src = 0; src < uniforms; src++) {
523 assert(src < uniform_array_size);
524 int size = this->uniform_vector_size[src];
525
526 if (!uniform_used[src]) {
527 this->uniform_vector_size[src] = 0;
528 continue;
529 }
530
531 int dst;
532 /* Find the lowest place we can slot this uniform in. */
533 for (dst = 0; dst < src; dst++) {
534 if (this->uniform_vector_size[dst] + size <= 4)
535 break;
536 }
537
538 if (src == dst) {
539 new_loc[src] = dst;
540 new_chan[src] = 0;
541 } else {
542 new_loc[src] = dst;
543 new_chan[src] = this->uniform_vector_size[dst];
544
545 /* Move the references to the data */
546 for (int j = 0; j < size; j++) {
547 stage_prog_data->param[dst * 4 + new_chan[src] + j] =
548 stage_prog_data->param[src * 4 + j];
549 }
550
551 this->uniform_vector_size[dst] += size;
552 this->uniform_vector_size[src] = 0;
553 }
554
555 new_uniform_count = MAX2(new_uniform_count, dst + 1);
556 }
557
558 this->uniforms = new_uniform_count;
559
560 /* Now, update the instructions for our repacked uniforms. */
561 foreach_in_list(vec4_instruction, inst, &instructions) {
562 for (int i = 0 ; i < 3; i++) {
563 int src = inst->src[i].reg;
564
565 if (inst->src[i].file != UNIFORM)
566 continue;
567
568 inst->src[i].reg = new_loc[src];
569
570 int sx = BRW_GET_SWZ(inst->src[i].swizzle, 0) + new_chan[src];
571 int sy = BRW_GET_SWZ(inst->src[i].swizzle, 1) + new_chan[src];
572 int sz = BRW_GET_SWZ(inst->src[i].swizzle, 2) + new_chan[src];
573 int sw = BRW_GET_SWZ(inst->src[i].swizzle, 3) + new_chan[src];
574 inst->src[i].swizzle = BRW_SWIZZLE4(sx, sy, sz, sw);
575 }
576 }
577 }
578
579 /**
580 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
581 *
582 * While GLSL IR also performs this optimization, we end up with it in
583 * our instruction stream for a couple of reasons. One is that we
584 * sometimes generate silly instructions, for example in array access
585 * where we'll generate "ADD offset, index, base" even if base is 0.
586 * The other is that GLSL IR's constant propagation doesn't track the
587 * components of aggregates, so some VS patterns (initialize matrix to
588 * 0, accumulate in vertex blending factors) end up breaking down to
589 * instructions involving 0.
590 */
591 bool
592 vec4_visitor::opt_algebraic()
593 {
594 bool progress = false;
595
596 foreach_in_list(vec4_instruction, inst, &instructions) {
597 switch (inst->opcode) {
598 case BRW_OPCODE_ADD:
599 if (inst->src[1].is_zero()) {
600 inst->opcode = BRW_OPCODE_MOV;
601 inst->src[1] = src_reg();
602 progress = true;
603 }
604 break;
605
606 case BRW_OPCODE_MUL:
607 if (inst->src[1].is_zero()) {
608 inst->opcode = BRW_OPCODE_MOV;
609 switch (inst->src[0].type) {
610 case BRW_REGISTER_TYPE_F:
611 inst->src[0] = src_reg(0.0f);
612 break;
613 case BRW_REGISTER_TYPE_D:
614 inst->src[0] = src_reg(0);
615 break;
616 case BRW_REGISTER_TYPE_UD:
617 inst->src[0] = src_reg(0u);
618 break;
619 default:
620 unreachable("not reached");
621 }
622 inst->src[1] = src_reg();
623 progress = true;
624 } else if (inst->src[1].is_one()) {
625 inst->opcode = BRW_OPCODE_MOV;
626 inst->src[1] = src_reg();
627 progress = true;
628 }
629 break;
630 default:
631 break;
632 }
633 }
634
635 if (progress)
636 invalidate_live_intervals();
637
638 return progress;
639 }
640
641 /**
642 * Only a limited number of hardware registers may be used for push
643 * constants, so this turns access to the overflowed constants into
644 * pull constants.
645 */
646 void
647 vec4_visitor::move_push_constants_to_pull_constants()
648 {
649 int pull_constant_loc[this->uniforms];
650
651 /* Only allow 32 registers (256 uniform components) as push constants,
652 * which is the limit on gen6.
653 *
654 * If changing this value, note the limitation about total_regs in
655 * brw_curbe.c.
656 */
657 int max_uniform_components = 32 * 8;
658 if (this->uniforms * 4 <= max_uniform_components)
659 return;
660
661 /* Make some sort of choice as to which uniforms get sent to pull
662 * constants. We could potentially do something clever here like
663 * look for the most infrequently used uniform vec4s, but leave
664 * that for later.
665 */
666 for (int i = 0; i < this->uniforms * 4; i += 4) {
667 pull_constant_loc[i / 4] = -1;
668
669 if (i >= max_uniform_components) {
670 const gl_constant_value **values = &stage_prog_data->param[i];
671
672 /* Try to find an existing copy of this uniform in the pull
673 * constants if it was part of an array access already.
674 */
675 for (unsigned int j = 0; j < stage_prog_data->nr_pull_params; j += 4) {
676 int matches;
677
678 for (matches = 0; matches < 4; matches++) {
679 if (stage_prog_data->pull_param[j + matches] != values[matches])
680 break;
681 }
682
683 if (matches == 4) {
684 pull_constant_loc[i / 4] = j / 4;
685 break;
686 }
687 }
688
689 if (pull_constant_loc[i / 4] == -1) {
690 assert(stage_prog_data->nr_pull_params % 4 == 0);
691 pull_constant_loc[i / 4] = stage_prog_data->nr_pull_params / 4;
692
693 for (int j = 0; j < 4; j++) {
694 stage_prog_data->pull_param[stage_prog_data->nr_pull_params++] =
695 values[j];
696 }
697 }
698 }
699 }
700
701 /* Now actually rewrite usage of the things we've moved to pull
702 * constants.
703 */
704 foreach_in_list_safe(vec4_instruction, inst, &instructions) {
705 for (int i = 0 ; i < 3; i++) {
706 if (inst->src[i].file != UNIFORM ||
707 pull_constant_loc[inst->src[i].reg] == -1)
708 continue;
709
710 int uniform = inst->src[i].reg;
711
712 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
713
714 emit_pull_constant_load(inst, temp, inst->src[i],
715 pull_constant_loc[uniform]);
716
717 inst->src[i].file = temp.file;
718 inst->src[i].reg = temp.reg;
719 inst->src[i].reg_offset = temp.reg_offset;
720 inst->src[i].reladdr = NULL;
721 }
722 }
723
724 /* Repack push constants to remove the now-unused ones. */
725 pack_uniform_registers();
726 }
727
728 /**
729 * Sets the dependency control fields on instructions after register
730 * allocation and before the generator is run.
731 *
732 * When you have a sequence of instructions like:
733 *
734 * DP4 temp.x vertex uniform[0]
735 * DP4 temp.y vertex uniform[0]
736 * DP4 temp.z vertex uniform[0]
737 * DP4 temp.w vertex uniform[0]
738 *
739 * The hardware doesn't know that it can actually run the later instructions
740 * while the previous ones are in flight, producing stalls. However, we have
741 * manual fields we can set in the instructions that let it do so.
742 */
743 void
744 vec4_visitor::opt_set_dependency_control()
745 {
746 vec4_instruction *last_grf_write[BRW_MAX_GRF];
747 uint8_t grf_channels_written[BRW_MAX_GRF];
748 vec4_instruction *last_mrf_write[BRW_MAX_GRF];
749 uint8_t mrf_channels_written[BRW_MAX_GRF];
750
751 calculate_cfg();
752
753 assert(prog_data->total_grf ||
754 !"Must be called after register allocation");
755
756 foreach_block (block, cfg) {
757 memset(last_grf_write, 0, sizeof(last_grf_write));
758 memset(last_mrf_write, 0, sizeof(last_mrf_write));
759
760 foreach_inst_in_block (vec4_instruction, inst, block) {
761 /* If we read from a register that we were doing dependency control
762 * on, don't do dependency control across the read.
763 */
764 for (int i = 0; i < 3; i++) {
765 int reg = inst->src[i].reg + inst->src[i].reg_offset;
766 if (inst->src[i].file == GRF) {
767 last_grf_write[reg] = NULL;
768 } else if (inst->src[i].file == HW_REG) {
769 memset(last_grf_write, 0, sizeof(last_grf_write));
770 break;
771 }
772 assert(inst->src[i].file != MRF);
773 }
774
775 /* In the presence of send messages, totally interrupt dependency
776 * control. They're long enough that the chance of dependency
777 * control around them just doesn't matter.
778 */
779 if (inst->mlen) {
780 memset(last_grf_write, 0, sizeof(last_grf_write));
781 memset(last_mrf_write, 0, sizeof(last_mrf_write));
782 continue;
783 }
784
785 /* It looks like setting dependency control on a predicated
786 * instruction hangs the GPU.
787 */
788 if (inst->predicate) {
789 memset(last_grf_write, 0, sizeof(last_grf_write));
790 memset(last_mrf_write, 0, sizeof(last_mrf_write));
791 continue;
792 }
793
794 /* Dependency control does not work well over math instructions.
795 */
796 if (inst->is_math()) {
797 memset(last_grf_write, 0, sizeof(last_grf_write));
798 memset(last_mrf_write, 0, sizeof(last_mrf_write));
799 continue;
800 }
801
802 /* Now, see if we can do dependency control for this instruction
803 * against a previous one writing to its destination.
804 */
805 int reg = inst->dst.reg + inst->dst.reg_offset;
806 if (inst->dst.file == GRF) {
807 if (last_grf_write[reg] &&
808 !(inst->dst.writemask & grf_channels_written[reg])) {
809 last_grf_write[reg]->no_dd_clear = true;
810 inst->no_dd_check = true;
811 } else {
812 grf_channels_written[reg] = 0;
813 }
814
815 last_grf_write[reg] = inst;
816 grf_channels_written[reg] |= inst->dst.writemask;
817 } else if (inst->dst.file == MRF) {
818 if (last_mrf_write[reg] &&
819 !(inst->dst.writemask & mrf_channels_written[reg])) {
820 last_mrf_write[reg]->no_dd_clear = true;
821 inst->no_dd_check = true;
822 } else {
823 mrf_channels_written[reg] = 0;
824 }
825
826 last_mrf_write[reg] = inst;
827 mrf_channels_written[reg] |= inst->dst.writemask;
828 } else if (inst->dst.reg == HW_REG) {
829 if (inst->dst.fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE)
830 memset(last_grf_write, 0, sizeof(last_grf_write));
831 if (inst->dst.fixed_hw_reg.file == BRW_MESSAGE_REGISTER_FILE)
832 memset(last_mrf_write, 0, sizeof(last_mrf_write));
833 }
834 }
835 }
836 }
837
838 bool
839 vec4_instruction::can_reswizzle_dst(int dst_writemask,
840 int swizzle,
841 int swizzle_mask)
842 {
843 /* If this instruction sets anything not referenced by swizzle, then we'd
844 * totally break it when we reswizzle.
845 */
846 if (dst.writemask & ~swizzle_mask)
847 return false;
848
849 switch (opcode) {
850 case BRW_OPCODE_DP4:
851 case BRW_OPCODE_DP3:
852 case BRW_OPCODE_DP2:
853 return true;
854 default:
855 /* Check if there happens to be no reswizzling required. */
856 for (int c = 0; c < 4; c++) {
857 int bit = 1 << BRW_GET_SWZ(swizzle, c);
858 /* Skip components of the swizzle not used by the dst. */
859 if (!(dst_writemask & (1 << c)))
860 continue;
861
862 /* We don't do the reswizzling yet, so just sanity check that we
863 * don't have to.
864 */
865 if (bit != (1 << c))
866 return false;
867 }
868 return true;
869 }
870 }
871
872 /**
873 * For any channels in the swizzle's source that were populated by this
874 * instruction, rewrite the instruction to put the appropriate result directly
875 * in those channels.
876 *
877 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
878 */
879 void
880 vec4_instruction::reswizzle_dst(int dst_writemask, int swizzle)
881 {
882 int new_writemask = 0;
883
884 switch (opcode) {
885 case BRW_OPCODE_DP4:
886 case BRW_OPCODE_DP3:
887 case BRW_OPCODE_DP2:
888 for (int c = 0; c < 4; c++) {
889 int bit = 1 << BRW_GET_SWZ(swizzle, c);
890 /* Skip components of the swizzle not used by the dst. */
891 if (!(dst_writemask & (1 << c)))
892 continue;
893 /* If we were populating this component, then populate the
894 * corresponding channel of the new dst.
895 */
896 if (dst.writemask & bit)
897 new_writemask |= (1 << c);
898 }
899 dst.writemask = new_writemask;
900 break;
901 default:
902 for (int c = 0; c < 4; c++) {
903 /* Skip components of the swizzle not used by the dst. */
904 if (!(dst_writemask & (1 << c)))
905 continue;
906
907 /* We don't do the reswizzling yet, so just sanity check that we
908 * don't have to.
909 */
910 assert((1 << BRW_GET_SWZ(swizzle, c)) == (1 << c));
911 }
912 break;
913 }
914 }
915
916 /*
917 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
918 * just written and then MOVed into another reg and making the original write
919 * of the GRF write directly to the final destination instead.
920 */
921 bool
922 vec4_visitor::opt_register_coalesce()
923 {
924 bool progress = false;
925 int next_ip = 0;
926
927 calculate_live_intervals();
928
929 foreach_in_list_safe(vec4_instruction, inst, &instructions) {
930 int ip = next_ip;
931 next_ip++;
932
933 if (inst->opcode != BRW_OPCODE_MOV ||
934 (inst->dst.file != GRF && inst->dst.file != MRF) ||
935 inst->predicate ||
936 inst->src[0].file != GRF ||
937 inst->dst.type != inst->src[0].type ||
938 inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
939 continue;
940
941 bool to_mrf = (inst->dst.file == MRF);
942
943 /* Can't coalesce this GRF if someone else was going to
944 * read it later.
945 */
946 if (this->virtual_grf_end[inst->src[0].reg * 4 + 0] > ip ||
947 this->virtual_grf_end[inst->src[0].reg * 4 + 1] > ip ||
948 this->virtual_grf_end[inst->src[0].reg * 4 + 2] > ip ||
949 this->virtual_grf_end[inst->src[0].reg * 4 + 3] > ip)
950 continue;
951
952 /* We need to check interference with the final destination between this
953 * instruction and the earliest instruction involved in writing the GRF
954 * we're eliminating. To do that, keep track of which of our source
955 * channels we've seen initialized.
956 */
957 bool chans_needed[4] = {false, false, false, false};
958 int chans_remaining = 0;
959 int swizzle_mask = 0;
960 for (int i = 0; i < 4; i++) {
961 int chan = BRW_GET_SWZ(inst->src[0].swizzle, i);
962
963 if (!(inst->dst.writemask & (1 << i)))
964 continue;
965
966 swizzle_mask |= (1 << chan);
967
968 if (!chans_needed[chan]) {
969 chans_needed[chan] = true;
970 chans_remaining++;
971 }
972 }
973
974 /* Now walk up the instruction stream trying to see if we can rewrite
975 * everything writing to the temporary to write into the destination
976 * instead.
977 */
978 vec4_instruction *scan_inst;
979 for (scan_inst = (vec4_instruction *)inst->prev;
980 scan_inst->prev != NULL;
981 scan_inst = (vec4_instruction *)scan_inst->prev) {
982 if (scan_inst->dst.file == GRF &&
983 scan_inst->dst.reg == inst->src[0].reg &&
984 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
985 /* Found something writing to the reg we want to coalesce away. */
986 if (to_mrf) {
987 /* SEND instructions can't have MRF as a destination. */
988 if (scan_inst->mlen)
989 break;
990
991 if (brw->gen == 6) {
992 /* gen6 math instructions must have the destination be
993 * GRF, so no compute-to-MRF for them.
994 */
995 if (scan_inst->is_math()) {
996 break;
997 }
998 }
999 }
1000
1001 /* If we can't handle the swizzle, bail. */
1002 if (!scan_inst->can_reswizzle_dst(inst->dst.writemask,
1003 inst->src[0].swizzle,
1004 swizzle_mask)) {
1005 break;
1006 }
1007
1008 /* Mark which channels we found unconditional writes for. */
1009 if (!scan_inst->predicate) {
1010 for (int i = 0; i < 4; i++) {
1011 if (scan_inst->dst.writemask & (1 << i) &&
1012 chans_needed[i]) {
1013 chans_needed[i] = false;
1014 chans_remaining--;
1015 }
1016 }
1017 }
1018
1019 if (chans_remaining == 0)
1020 break;
1021 }
1022
1023 /* We don't handle flow control here. Most computation of values
1024 * that could be coalesced happens just before their use.
1025 */
1026 if (scan_inst->opcode == BRW_OPCODE_DO ||
1027 scan_inst->opcode == BRW_OPCODE_WHILE ||
1028 scan_inst->opcode == BRW_OPCODE_ELSE ||
1029 scan_inst->opcode == BRW_OPCODE_ENDIF) {
1030 break;
1031 }
1032
1033 /* You can't read from an MRF, so if someone else reads our MRF's
1034 * source GRF that we wanted to rewrite, that stops us. If it's a
1035 * GRF we're trying to coalesce to, we don't actually handle
1036 * rewriting sources so bail in that case as well.
1037 */
1038 bool interfered = false;
1039 for (int i = 0; i < 3; i++) {
1040 if (scan_inst->src[i].file == GRF &&
1041 scan_inst->src[i].reg == inst->src[0].reg &&
1042 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
1043 interfered = true;
1044 }
1045 }
1046 if (interfered)
1047 break;
1048
1049 /* If somebody else writes our destination here, we can't coalesce
1050 * before that.
1051 */
1052 if (scan_inst->dst.file == inst->dst.file &&
1053 scan_inst->dst.reg == inst->dst.reg) {
1054 break;
1055 }
1056
1057 /* Check for reads of the register we're trying to coalesce into. We
1058 * can't go rewriting instructions above that to put some other value
1059 * in the register instead.
1060 */
1061 if (to_mrf && scan_inst->mlen > 0) {
1062 if (inst->dst.reg >= scan_inst->base_mrf &&
1063 inst->dst.reg < scan_inst->base_mrf + scan_inst->mlen) {
1064 break;
1065 }
1066 } else {
1067 for (int i = 0; i < 3; i++) {
1068 if (scan_inst->src[i].file == inst->dst.file &&
1069 scan_inst->src[i].reg == inst->dst.reg &&
1070 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
1071 interfered = true;
1072 }
1073 }
1074 if (interfered)
1075 break;
1076 }
1077 }
1078
1079 if (chans_remaining == 0) {
1080 /* If we've made it here, we have an MOV we want to coalesce out, and
1081 * a scan_inst pointing to the earliest instruction involved in
1082 * computing the value. Now go rewrite the instruction stream
1083 * between the two.
1084 */
1085
1086 while (scan_inst != inst) {
1087 if (scan_inst->dst.file == GRF &&
1088 scan_inst->dst.reg == inst->src[0].reg &&
1089 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
1090 scan_inst->reswizzle_dst(inst->dst.writemask,
1091 inst->src[0].swizzle);
1092 scan_inst->dst.file = inst->dst.file;
1093 scan_inst->dst.reg = inst->dst.reg;
1094 scan_inst->dst.reg_offset = inst->dst.reg_offset;
1095 scan_inst->saturate |= inst->saturate;
1096 }
1097 scan_inst = (vec4_instruction *)scan_inst->next;
1098 }
1099 inst->remove();
1100 progress = true;
1101 }
1102 }
1103
1104 if (progress)
1105 invalidate_live_intervals();
1106
1107 return progress;
1108 }
1109
1110 /**
1111 * Splits virtual GRFs requesting more than one contiguous physical register.
1112 *
1113 * We initially create large virtual GRFs for temporary structures, arrays,
1114 * and matrices, so that the dereference visitor functions can add reg_offsets
1115 * to work their way down to the actual member being accessed. But when it
1116 * comes to optimization, we'd like to treat each register as individual
1117 * storage if possible.
1118 *
1119 * So far, the only thing that might prevent splitting is a send message from
1120 * a GRF on IVB.
1121 */
1122 void
1123 vec4_visitor::split_virtual_grfs()
1124 {
1125 int num_vars = this->virtual_grf_count;
1126 int new_virtual_grf[num_vars];
1127 bool split_grf[num_vars];
1128
1129 memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1130
1131 /* Try to split anything > 0 sized. */
1132 for (int i = 0; i < num_vars; i++) {
1133 split_grf[i] = this->virtual_grf_sizes[i] != 1;
1134 }
1135
1136 /* Check that the instructions are compatible with the registers we're trying
1137 * to split.
1138 */
1139 foreach_in_list(vec4_instruction, inst, &instructions) {
1140 /* If there's a SEND message loading from a GRF on gen7+, it needs to be
1141 * contiguous.
1142 */
1143 if (inst->is_send_from_grf()) {
1144 for (int i = 0; i < 3; i++) {
1145 if (inst->src[i].file == GRF) {
1146 split_grf[inst->src[i].reg] = false;
1147 }
1148 }
1149 }
1150 }
1151
1152 /* Allocate new space for split regs. Note that the virtual
1153 * numbers will be contiguous.
1154 */
1155 for (int i = 0; i < num_vars; i++) {
1156 if (!split_grf[i])
1157 continue;
1158
1159 new_virtual_grf[i] = virtual_grf_alloc(1);
1160 for (int j = 2; j < this->virtual_grf_sizes[i]; j++) {
1161 int reg = virtual_grf_alloc(1);
1162 assert(reg == new_virtual_grf[i] + j - 1);
1163 (void) reg;
1164 }
1165 this->virtual_grf_sizes[i] = 1;
1166 }
1167
1168 foreach_in_list(vec4_instruction, inst, &instructions) {
1169 if (inst->dst.file == GRF && split_grf[inst->dst.reg] &&
1170 inst->dst.reg_offset != 0) {
1171 inst->dst.reg = (new_virtual_grf[inst->dst.reg] +
1172 inst->dst.reg_offset - 1);
1173 inst->dst.reg_offset = 0;
1174 }
1175 for (int i = 0; i < 3; i++) {
1176 if (inst->src[i].file == GRF && split_grf[inst->src[i].reg] &&
1177 inst->src[i].reg_offset != 0) {
1178 inst->src[i].reg = (new_virtual_grf[inst->src[i].reg] +
1179 inst->src[i].reg_offset - 1);
1180 inst->src[i].reg_offset = 0;
1181 }
1182 }
1183 }
1184 invalidate_live_intervals();
1185 }
1186
1187 void
1188 vec4_visitor::dump_instruction(backend_instruction *be_inst)
1189 {
1190 dump_instruction(be_inst, stderr);
1191 }
1192
1193 void
1194 vec4_visitor::dump_instruction(backend_instruction *be_inst, FILE *file)
1195 {
1196 vec4_instruction *inst = (vec4_instruction *)be_inst;
1197
1198 if (inst->predicate) {
1199 fprintf(file, "(%cf0) ",
1200 inst->predicate_inverse ? '-' : '+');
1201 }
1202
1203 fprintf(file, "%s", brw_instruction_name(inst->opcode));
1204 if (inst->conditional_mod) {
1205 fprintf(file, "%s", conditional_modifier[inst->conditional_mod]);
1206 }
1207 fprintf(file, " ");
1208
1209 switch (inst->dst.file) {
1210 case GRF:
1211 fprintf(file, "vgrf%d.%d", inst->dst.reg, inst->dst.reg_offset);
1212 break;
1213 case MRF:
1214 fprintf(file, "m%d", inst->dst.reg);
1215 break;
1216 case HW_REG:
1217 if (inst->dst.fixed_hw_reg.file == BRW_ARCHITECTURE_REGISTER_FILE) {
1218 switch (inst->dst.fixed_hw_reg.nr) {
1219 case BRW_ARF_NULL:
1220 fprintf(file, "null");
1221 break;
1222 case BRW_ARF_ADDRESS:
1223 fprintf(file, "a0.%d", inst->dst.fixed_hw_reg.subnr);
1224 break;
1225 case BRW_ARF_ACCUMULATOR:
1226 fprintf(file, "acc%d", inst->dst.fixed_hw_reg.subnr);
1227 break;
1228 case BRW_ARF_FLAG:
1229 fprintf(file, "f%d.%d", inst->dst.fixed_hw_reg.nr & 0xf,
1230 inst->dst.fixed_hw_reg.subnr);
1231 break;
1232 default:
1233 fprintf(file, "arf%d.%d", inst->dst.fixed_hw_reg.nr & 0xf,
1234 inst->dst.fixed_hw_reg.subnr);
1235 break;
1236 }
1237 } else {
1238 fprintf(file, "hw_reg%d", inst->dst.fixed_hw_reg.nr);
1239 }
1240 if (inst->dst.fixed_hw_reg.subnr)
1241 fprintf(file, "+%d", inst->dst.fixed_hw_reg.subnr);
1242 break;
1243 case BAD_FILE:
1244 fprintf(file, "(null)");
1245 break;
1246 default:
1247 fprintf(file, "???");
1248 break;
1249 }
1250 if (inst->dst.writemask != WRITEMASK_XYZW) {
1251 fprintf(file, ".");
1252 if (inst->dst.writemask & 1)
1253 fprintf(file, "x");
1254 if (inst->dst.writemask & 2)
1255 fprintf(file, "y");
1256 if (inst->dst.writemask & 4)
1257 fprintf(file, "z");
1258 if (inst->dst.writemask & 8)
1259 fprintf(file, "w");
1260 }
1261 fprintf(file, ":%s, ", brw_reg_type_letters(inst->dst.type));
1262
1263 for (int i = 0; i < 3 && inst->src[i].file != BAD_FILE; i++) {
1264 if (inst->src[i].negate)
1265 fprintf(file, "-");
1266 if (inst->src[i].abs)
1267 fprintf(file, "|");
1268 switch (inst->src[i].file) {
1269 case GRF:
1270 fprintf(file, "vgrf%d", inst->src[i].reg);
1271 break;
1272 case ATTR:
1273 fprintf(file, "attr%d", inst->src[i].reg);
1274 break;
1275 case UNIFORM:
1276 fprintf(file, "u%d", inst->src[i].reg);
1277 break;
1278 case IMM:
1279 switch (inst->src[i].type) {
1280 case BRW_REGISTER_TYPE_F:
1281 fprintf(file, "%fF", inst->src[i].fixed_hw_reg.dw1.f);
1282 break;
1283 case BRW_REGISTER_TYPE_D:
1284 fprintf(file, "%dD", inst->src[i].fixed_hw_reg.dw1.d);
1285 break;
1286 case BRW_REGISTER_TYPE_UD:
1287 fprintf(file, "%uU", inst->src[i].fixed_hw_reg.dw1.ud);
1288 break;
1289 default:
1290 fprintf(file, "???");
1291 break;
1292 }
1293 break;
1294 case HW_REG:
1295 if (inst->src[i].fixed_hw_reg.negate)
1296 fprintf(file, "-");
1297 if (inst->src[i].fixed_hw_reg.abs)
1298 fprintf(file, "|");
1299 if (inst->src[i].fixed_hw_reg.file == BRW_ARCHITECTURE_REGISTER_FILE) {
1300 switch (inst->src[i].fixed_hw_reg.nr) {
1301 case BRW_ARF_NULL:
1302 fprintf(file, "null");
1303 break;
1304 case BRW_ARF_ADDRESS:
1305 fprintf(file, "a0.%d", inst->src[i].fixed_hw_reg.subnr);
1306 break;
1307 case BRW_ARF_ACCUMULATOR:
1308 fprintf(file, "acc%d", inst->src[i].fixed_hw_reg.subnr);
1309 break;
1310 case BRW_ARF_FLAG:
1311 fprintf(file, "f%d.%d", inst->src[i].fixed_hw_reg.nr & 0xf,
1312 inst->src[i].fixed_hw_reg.subnr);
1313 break;
1314 default:
1315 fprintf(file, "arf%d.%d", inst->src[i].fixed_hw_reg.nr & 0xf,
1316 inst->src[i].fixed_hw_reg.subnr);
1317 break;
1318 }
1319 } else {
1320 fprintf(file, "hw_reg%d", inst->src[i].fixed_hw_reg.nr);
1321 }
1322 if (inst->src[i].fixed_hw_reg.subnr)
1323 fprintf(file, "+%d", inst->src[i].fixed_hw_reg.subnr);
1324 if (inst->src[i].fixed_hw_reg.abs)
1325 fprintf(file, "|");
1326 break;
1327 case BAD_FILE:
1328 fprintf(file, "(null)");
1329 break;
1330 default:
1331 fprintf(file, "???");
1332 break;
1333 }
1334
1335 /* Don't print .0; and only VGRFs have reg_offsets and sizes */
1336 if (inst->src[i].reg_offset != 0 &&
1337 inst->src[i].file == GRF &&
1338 virtual_grf_sizes[inst->src[i].reg] != 1)
1339 fprintf(file, ".%d", inst->src[i].reg_offset);
1340
1341 if (inst->src[i].file != IMM) {
1342 static const char *chans[4] = {"x", "y", "z", "w"};
1343 fprintf(file, ".");
1344 for (int c = 0; c < 4; c++) {
1345 fprintf(file, "%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1346 }
1347 }
1348
1349 if (inst->src[i].abs)
1350 fprintf(file, "|");
1351
1352 if (inst->src[i].file != IMM) {
1353 fprintf(file, ":%s", brw_reg_type_letters(inst->src[i].type));
1354 }
1355
1356 if (i < 2 && inst->src[i + 1].file != BAD_FILE)
1357 fprintf(file, ", ");
1358 }
1359
1360 fprintf(file, "\n");
1361 }
1362
1363
1364 static inline struct brw_reg
1365 attribute_to_hw_reg(int attr, bool interleaved)
1366 {
1367 if (interleaved)
1368 return stride(brw_vec4_grf(attr / 2, (attr % 2) * 4), 0, 4, 1);
1369 else
1370 return brw_vec8_grf(attr, 0);
1371 }
1372
1373
1374 /**
1375 * Replace each register of type ATTR in this->instructions with a reference
1376 * to a fixed HW register.
1377 *
1378 * If interleaved is true, then each attribute takes up half a register, with
1379 * register N containing attribute 2*N in its first half and attribute 2*N+1
1380 * in its second half (this corresponds to the payload setup used by geometry
1381 * shaders in "single" or "dual instanced" dispatch mode). If interleaved is
1382 * false, then each attribute takes up a whole register, with register N
1383 * containing attribute N (this corresponds to the payload setup used by
1384 * vertex shaders, and by geometry shaders in "dual object" dispatch mode).
1385 */
1386 void
1387 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map,
1388 bool interleaved)
1389 {
1390 foreach_in_list(vec4_instruction, inst, &instructions) {
1391 /* We have to support ATTR as a destination for GL_FIXED fixup. */
1392 if (inst->dst.file == ATTR) {
1393 int grf = attribute_map[inst->dst.reg + inst->dst.reg_offset];
1394
1395 /* All attributes used in the shader need to have been assigned a
1396 * hardware register by the caller
1397 */
1398 assert(grf != 0);
1399
1400 struct brw_reg reg = attribute_to_hw_reg(grf, interleaved);
1401 reg.type = inst->dst.type;
1402 reg.dw1.bits.writemask = inst->dst.writemask;
1403
1404 inst->dst.file = HW_REG;
1405 inst->dst.fixed_hw_reg = reg;
1406 }
1407
1408 for (int i = 0; i < 3; i++) {
1409 if (inst->src[i].file != ATTR)
1410 continue;
1411
1412 int grf = attribute_map[inst->src[i].reg + inst->src[i].reg_offset];
1413
1414 /* All attributes used in the shader need to have been assigned a
1415 * hardware register by the caller
1416 */
1417 assert(grf != 0);
1418
1419 struct brw_reg reg = attribute_to_hw_reg(grf, interleaved);
1420 reg.dw1.bits.swizzle = inst->src[i].swizzle;
1421 reg.type = inst->src[i].type;
1422 if (inst->src[i].abs)
1423 reg = brw_abs(reg);
1424 if (inst->src[i].negate)
1425 reg = negate(reg);
1426
1427 inst->src[i].file = HW_REG;
1428 inst->src[i].fixed_hw_reg = reg;
1429 }
1430 }
1431 }
1432
1433 int
1434 vec4_vs_visitor::setup_attributes(int payload_reg)
1435 {
1436 int nr_attributes;
1437 int attribute_map[VERT_ATTRIB_MAX + 1];
1438 memset(attribute_map, 0, sizeof(attribute_map));
1439
1440 nr_attributes = 0;
1441 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
1442 if (vs_prog_data->inputs_read & BITFIELD64_BIT(i)) {
1443 attribute_map[i] = payload_reg + nr_attributes;
1444 nr_attributes++;
1445 }
1446 }
1447
1448 /* VertexID is stored by the VF as the last vertex element, but we
1449 * don't represent it with a flag in inputs_read, so we call it
1450 * VERT_ATTRIB_MAX.
1451 */
1452 if (vs_prog_data->uses_vertexid) {
1453 attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1454 nr_attributes++;
1455 }
1456
1457 lower_attributes_to_hw_regs(attribute_map, false /* interleaved */);
1458
1459 /* The BSpec says we always have to read at least one thing from
1460 * the VF, and it appears that the hardware wedges otherwise.
1461 */
1462 if (nr_attributes == 0)
1463 nr_attributes = 1;
1464
1465 prog_data->urb_read_length = (nr_attributes + 1) / 2;
1466
1467 unsigned vue_entries =
1468 MAX2(nr_attributes, prog_data->vue_map.num_slots);
1469
1470 if (brw->gen == 6)
1471 prog_data->urb_entry_size = ALIGN(vue_entries, 8) / 8;
1472 else
1473 prog_data->urb_entry_size = ALIGN(vue_entries, 4) / 4;
1474
1475 return payload_reg + nr_attributes;
1476 }
1477
1478 int
1479 vec4_visitor::setup_uniforms(int reg)
1480 {
1481 prog_data->base.dispatch_grf_start_reg = reg;
1482
1483 /* The pre-gen6 VS requires that some push constants get loaded no
1484 * matter what, or the GPU would hang.
1485 */
1486 if (brw->gen < 6 && this->uniforms == 0) {
1487 assert(this->uniforms < this->uniform_array_size);
1488 this->uniform_vector_size[this->uniforms] = 1;
1489
1490 stage_prog_data->param =
1491 reralloc(NULL, stage_prog_data->param, const gl_constant_value *, 4);
1492 for (unsigned int i = 0; i < 4; i++) {
1493 unsigned int slot = this->uniforms * 4 + i;
1494 static gl_constant_value zero = { 0.0 };
1495 stage_prog_data->param[slot] = &zero;
1496 }
1497
1498 this->uniforms++;
1499 reg++;
1500 } else {
1501 reg += ALIGN(uniforms, 2) / 2;
1502 }
1503
1504 stage_prog_data->nr_params = this->uniforms * 4;
1505
1506 prog_data->curb_read_length = reg - prog_data->base.dispatch_grf_start_reg;
1507
1508 return reg;
1509 }
1510
1511 void
1512 vec4_vs_visitor::setup_payload(void)
1513 {
1514 int reg = 0;
1515
1516 /* The payload always contains important data in g0, which contains
1517 * the URB handles that are passed on to the URB write at the end
1518 * of the thread. So, we always start push constants at g1.
1519 */
1520 reg++;
1521
1522 reg = setup_uniforms(reg);
1523
1524 reg = setup_attributes(reg);
1525
1526 this->first_non_payload_grf = reg;
1527 }
1528
1529 src_reg
1530 vec4_visitor::get_timestamp()
1531 {
1532 assert(brw->gen >= 7);
1533
1534 src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1535 BRW_ARF_TIMESTAMP,
1536 0,
1537 BRW_REGISTER_TYPE_UD,
1538 BRW_VERTICAL_STRIDE_0,
1539 BRW_WIDTH_4,
1540 BRW_HORIZONTAL_STRIDE_4,
1541 BRW_SWIZZLE_XYZW,
1542 WRITEMASK_XYZW));
1543
1544 dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1545
1546 vec4_instruction *mov = emit(MOV(dst, ts));
1547 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1548 * even if it's not enabled in the dispatch.
1549 */
1550 mov->force_writemask_all = true;
1551
1552 return src_reg(dst);
1553 }
1554
1555 void
1556 vec4_visitor::emit_shader_time_begin()
1557 {
1558 current_annotation = "shader time start";
1559 shader_start_time = get_timestamp();
1560 }
1561
1562 void
1563 vec4_visitor::emit_shader_time_end()
1564 {
1565 current_annotation = "shader time end";
1566 src_reg shader_end_time = get_timestamp();
1567
1568
1569 /* Check that there weren't any timestamp reset events (assuming these
1570 * were the only two timestamp reads that happened).
1571 */
1572 src_reg reset_end = shader_end_time;
1573 reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1574 vec4_instruction *test = emit(AND(dst_null_d(), reset_end, src_reg(1u)));
1575 test->conditional_mod = BRW_CONDITIONAL_Z;
1576
1577 emit(IF(BRW_PREDICATE_NORMAL));
1578
1579 /* Take the current timestamp and get the delta. */
1580 shader_start_time.negate = true;
1581 dst_reg diff = dst_reg(this, glsl_type::uint_type);
1582 emit(ADD(diff, shader_start_time, shader_end_time));
1583
1584 /* If there were no instructions between the two timestamp gets, the diff
1585 * is 2 cycles. Remove that overhead, so I can forget about that when
1586 * trying to determine the time taken for single instructions.
1587 */
1588 emit(ADD(diff, src_reg(diff), src_reg(-2u)));
1589
1590 emit_shader_time_write(st_base, src_reg(diff));
1591 emit_shader_time_write(st_written, src_reg(1u));
1592 emit(BRW_OPCODE_ELSE);
1593 emit_shader_time_write(st_reset, src_reg(1u));
1594 emit(BRW_OPCODE_ENDIF);
1595 }
1596
1597 void
1598 vec4_visitor::emit_shader_time_write(enum shader_time_shader_type type,
1599 src_reg value)
1600 {
1601 int shader_time_index =
1602 brw_get_shader_time_index(brw, shader_prog, prog, type);
1603
1604 dst_reg dst =
1605 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1606
1607 dst_reg offset = dst;
1608 dst_reg time = dst;
1609 time.reg_offset++;
1610
1611 offset.type = BRW_REGISTER_TYPE_UD;
1612 emit(MOV(offset, src_reg(shader_time_index * SHADER_TIME_STRIDE)));
1613
1614 time.type = BRW_REGISTER_TYPE_UD;
1615 emit(MOV(time, src_reg(value)));
1616
1617 emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1618 }
1619
1620 bool
1621 vec4_visitor::run()
1622 {
1623 sanity_param_count = prog->Parameters->NumParameters;
1624
1625 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
1626 emit_shader_time_begin();
1627
1628 assign_common_binding_table_offsets(0);
1629
1630 emit_prolog();
1631
1632 /* Generate VS IR for main(). (the visitor only descends into
1633 * functions called "main").
1634 */
1635 if (shader) {
1636 visit_instructions(shader->base.ir);
1637 } else {
1638 emit_program_code();
1639 }
1640 base_ir = NULL;
1641
1642 if (key->userclip_active && !prog->UsesClipDistanceOut)
1643 setup_uniform_clipplane_values();
1644
1645 emit_thread_end();
1646
1647 /* Before any optimization, push array accesses out to scratch
1648 * space where we need them to be. This pass may allocate new
1649 * virtual GRFs, so we want to do it early. It also makes sure
1650 * that we have reladdr computations available for CSE, since we'll
1651 * often do repeated subexpressions for those.
1652 */
1653 if (shader) {
1654 move_grf_array_access_to_scratch();
1655 move_uniform_array_access_to_pull_constants();
1656 } else {
1657 /* The ARB_vertex_program frontend emits pull constant loads directly
1658 * rather than using reladdr, so we don't need to walk through all the
1659 * instructions looking for things to move. There isn't anything.
1660 *
1661 * We do still need to split things to vec4 size.
1662 */
1663 split_uniform_registers();
1664 }
1665 pack_uniform_registers();
1666 move_push_constants_to_pull_constants();
1667 split_virtual_grfs();
1668
1669 const char *stage_name = stage == MESA_SHADER_GEOMETRY ? "gs" : "vs";
1670
1671 #define OPT(pass, args...) do { \
1672 pass_num++; \
1673 bool this_progress = pass(args); \
1674 \
1675 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
1676 char filename[64]; \
1677 snprintf(filename, 64, "%s-%04d-%02d-%02d-" #pass, \
1678 stage_name, shader_prog->Name, iteration, pass_num); \
1679 \
1680 backend_visitor::dump_instructions(filename); \
1681 } \
1682 \
1683 progress = progress || this_progress; \
1684 } while (false)
1685
1686
1687 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER)) {
1688 char filename[64];
1689 snprintf(filename, 64, "%s-%04d-00-start",
1690 stage_name, shader_prog->Name);
1691
1692 backend_visitor::dump_instructions(filename);
1693 }
1694
1695 bool progress;
1696 int iteration = 0;
1697 do {
1698 progress = false;
1699 iteration++;
1700 int pass_num = 0;
1701
1702 OPT(dead_code_eliminate);
1703 OPT(dead_control_flow_eliminate, this);
1704 OPT(opt_copy_propagation);
1705 OPT(opt_algebraic);
1706 OPT(opt_cse);
1707 OPT(opt_register_coalesce);
1708 } while (progress);
1709
1710
1711 if (failed)
1712 return false;
1713
1714 setup_payload();
1715
1716 if (false) {
1717 /* Debug of register spilling: Go spill everything. */
1718 const int grf_count = virtual_grf_count;
1719 float spill_costs[virtual_grf_count];
1720 bool no_spill[virtual_grf_count];
1721 evaluate_spill_costs(spill_costs, no_spill);
1722 for (int i = 0; i < grf_count; i++) {
1723 if (no_spill[i])
1724 continue;
1725 spill_reg(i);
1726 }
1727 }
1728
1729 while (!reg_allocate()) {
1730 if (failed)
1731 return false;
1732 }
1733
1734 opt_schedule_instructions();
1735
1736 opt_set_dependency_control();
1737
1738 /* If any state parameters were appended, then ParameterValues could have
1739 * been realloced, in which case the driver uniform storage set up by
1740 * _mesa_associate_uniform_storage() would point to freed memory. Make
1741 * sure that didn't happen.
1742 */
1743 assert(sanity_param_count == prog->Parameters->NumParameters);
1744
1745 calculate_cfg();
1746
1747 return !failed;
1748 }
1749
1750 } /* namespace brw */
1751
1752 extern "C" {
1753
1754 /**
1755 * Compile a vertex shader.
1756 *
1757 * Returns the final assembly and the program's size.
1758 */
1759 const unsigned *
1760 brw_vs_emit(struct brw_context *brw,
1761 struct gl_shader_program *prog,
1762 struct brw_vs_compile *c,
1763 struct brw_vs_prog_data *prog_data,
1764 void *mem_ctx,
1765 unsigned *final_assembly_size)
1766 {
1767 bool start_busy = false;
1768 double start_time = 0;
1769
1770 if (unlikely(brw->perf_debug)) {
1771 start_busy = (brw->batch.last_bo &&
1772 drm_intel_bo_busy(brw->batch.last_bo));
1773 start_time = get_time();
1774 }
1775
1776 struct brw_shader *shader = NULL;
1777 if (prog)
1778 shader = (brw_shader *) prog->_LinkedShaders[MESA_SHADER_VERTEX];
1779
1780 if (unlikely(INTEL_DEBUG & DEBUG_VS))
1781 brw_dump_ir(brw, "vertex", prog, &shader->base, &c->vp->program.Base);
1782
1783 vec4_vs_visitor v(brw, c, prog_data, prog, mem_ctx);
1784 if (!v.run()) {
1785 if (prog) {
1786 prog->LinkStatus = false;
1787 ralloc_strcat(&prog->InfoLog, v.fail_msg);
1788 }
1789
1790 _mesa_problem(NULL, "Failed to compile vertex shader: %s\n",
1791 v.fail_msg);
1792
1793 return NULL;
1794 }
1795
1796 const unsigned *assembly = NULL;
1797 vec4_generator g(brw, prog, &c->vp->program.Base, &prog_data->base,
1798 mem_ctx, INTEL_DEBUG & DEBUG_VS);
1799 assembly = g.generate_assembly(v.cfg, final_assembly_size);
1800
1801 if (unlikely(brw->perf_debug) && shader) {
1802 if (shader->compiled_once) {
1803 brw_vs_debug_recompile(brw, prog, &c->key);
1804 }
1805 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
1806 perf_debug("VS compile took %.03f ms and stalled the GPU\n",
1807 (get_time() - start_time) * 1000);
1808 }
1809 shader->compiled_once = true;
1810 }
1811
1812 return assembly;
1813 }
1814
1815
1816 void
1817 brw_vec4_setup_prog_key_for_precompile(struct gl_context *ctx,
1818 struct brw_vec4_prog_key *key,
1819 GLuint id, struct gl_program *prog)
1820 {
1821 key->program_string_id = id;
1822 key->clamp_vertex_color = ctx->API == API_OPENGL_COMPAT;
1823
1824 unsigned sampler_count = _mesa_fls(prog->SamplersUsed);
1825 for (unsigned i = 0; i < sampler_count; i++) {
1826 if (prog->ShadowSamplers & (1 << i)) {
1827 /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
1828 key->tex.swizzles[i] =
1829 MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
1830 } else {
1831 /* Color sampler: assume no swizzling. */
1832 key->tex.swizzles[i] = SWIZZLE_XYZW;
1833 }
1834 }
1835 }
1836
1837 } /* extern "C" */