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