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