2 * Copyright © 2011 Intel Corporation
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:
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
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
26 #include "glsl/ir_print_visitor.h"
29 #include "main/macros.h"
30 #include "main/shaderobj.h"
31 #include "program/prog_print.h"
32 #include "program/prog_parameter.h"
35 #define MAX_INSTRUCTION (1 << 30)
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
48 swizzle_for_size(int size)
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),
57 assert((size >= 1) && (size <= 4));
58 return size_swizzles[size - 1];
64 memset(this, 0, sizeof(*this));
66 this->file = BAD_FILE;
69 src_reg::src_reg(register_file file, int reg, const glsl_type *type)
75 if (type && (type->is_scalar() || type->is_vector() || type->is_matrix()))
76 this->swizzle = swizzle_for_size(type->vector_elements);
78 this->swizzle = SWIZZLE_XYZW;
81 /** Generic unset register constructor. */
87 src_reg::src_reg(float f)
92 this->type = BRW_REGISTER_TYPE_F;
96 src_reg::src_reg(uint32_t u)
101 this->type = BRW_REGISTER_TYPE_UD;
105 src_reg::src_reg(int32_t i)
110 this->type = BRW_REGISTER_TYPE_D;
114 src_reg::src_reg(dst_reg reg)
118 this->file = reg.file;
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;
129 for (int i = 0; i < 4; i++) {
130 if (!(reg.writemask & (1 << i)))
133 swizzles[next_chan++] = last = i;
136 for (; next_chan < 4; next_chan++) {
137 swizzles[next_chan] = last;
140 this->swizzle = BRW_SWIZZLE4(swizzles[0], swizzles[1],
141 swizzles[2], swizzles[3]);
147 memset(this, 0, sizeof(*this));
148 this->file = BAD_FILE;
149 this->writemask = WRITEMASK_XYZW;
157 dst_reg::dst_reg(register_file file, int reg)
165 dst_reg::dst_reg(register_file file, int reg, const glsl_type *type,
172 this->type = brw_type_for_base_type(type);
173 this->writemask = writemask;
176 dst_reg::dst_reg(struct brw_reg reg)
181 this->fixed_hw_reg = reg;
184 dst_reg::dst_reg(src_reg reg)
188 this->file = reg.file;
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.
196 if (reg.swizzle == BRW_SWIZZLE_XXXX)
197 this->writemask = WRITEMASK_X;
199 this->writemask = WRITEMASK_XYZW;
200 this->reladdr = reg.reladdr;
201 this->fixed_hw_reg = reg.fixed_hw_reg;
205 vec4_instruction::is_send_from_grf()
208 case SHADER_OPCODE_SHADER_TIME_ADD:
209 case VS_OPCODE_PULL_CONSTANT_LOAD_GEN7:
217 vec4_visitor::can_do_source_mods(vec4_instruction *inst)
219 if (intel->gen == 6 && inst->is_math())
222 if (inst->is_send_from_grf())
229 * Returns how many MRFs an opcode will write over.
231 * Note that this is not the 0 or 1 implied writes in an actual gen
232 * instruction -- the generate_* functions generate additional MOVs
236 vec4_visitor::implied_mrf_writes(vec4_instruction *inst)
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:
250 case SHADER_OPCODE_POW:
252 case VS_OPCODE_URB_WRITE:
254 case VS_OPCODE_PULL_CONSTANT_LOAD:
256 case VS_OPCODE_SCRATCH_READ:
258 case VS_OPCODE_SCRATCH_WRITE:
260 case SHADER_OPCODE_SHADER_TIME_ADD:
263 assert(!"not reached");
269 src_reg::equals(src_reg *r)
271 return (file == r->file &&
273 reg_offset == r->reg_offset &&
275 negate == r->negate &&
277 swizzle == r->swizzle &&
278 !reladdr && !r->reladdr &&
279 memcmp(&fixed_hw_reg, &r->fixed_hw_reg,
280 sizeof(fixed_hw_reg)) == 0 &&
285 * Must be called after calculate_live_intervales() to remove unused
286 * writes to registers -- register allocation will fail otherwise
287 * because something deffed but not used won't be considered to
288 * interfere with other regs.
291 vec4_visitor::dead_code_eliminate()
293 bool progress = false;
296 calculate_live_intervals();
298 foreach_list_safe(node, &this->instructions) {
299 vec4_instruction *inst = (vec4_instruction *)node;
301 if (inst->dst.file == GRF && this->virtual_grf_use[inst->dst.reg] <= pc) {
310 live_intervals_valid = false;
316 vec4_visitor::split_uniform_registers()
318 /* Prior to this, uniforms have been in an array sized according to
319 * the number of vector uniforms present, sparsely filled (so an
320 * aggregate results in reg indices being skipped over). Now we're
321 * going to cut those aggregates up so each .reg index is one
322 * vector. The goal is to make elimination of unused uniform
323 * components easier later.
325 foreach_list(node, &this->instructions) {
326 vec4_instruction *inst = (vec4_instruction *)node;
328 for (int i = 0 ; i < 3; i++) {
329 if (inst->src[i].file != UNIFORM)
332 assert(!inst->src[i].reladdr);
334 inst->src[i].reg += inst->src[i].reg_offset;
335 inst->src[i].reg_offset = 0;
339 /* Update that everything is now vector-sized. */
340 for (int i = 0; i < this->uniforms; i++) {
341 this->uniform_size[i] = 1;
346 vec4_visitor::pack_uniform_registers()
348 bool uniform_used[this->uniforms];
349 int new_loc[this->uniforms];
350 int new_chan[this->uniforms];
352 memset(uniform_used, 0, sizeof(uniform_used));
353 memset(new_loc, 0, sizeof(new_loc));
354 memset(new_chan, 0, sizeof(new_chan));
356 /* Find which uniform vectors are actually used by the program. We
357 * expect unused vector elements when we've moved array access out
358 * to pull constants, and from some GLSL code generators like wine.
360 foreach_list(node, &this->instructions) {
361 vec4_instruction *inst = (vec4_instruction *)node;
363 for (int i = 0 ; i < 3; i++) {
364 if (inst->src[i].file != UNIFORM)
367 uniform_used[inst->src[i].reg] = true;
371 int new_uniform_count = 0;
373 /* Now, figure out a packing of the live uniform vectors into our
376 for (int src = 0; src < uniforms; src++) {
377 int size = this->uniform_vector_size[src];
379 if (!uniform_used[src]) {
380 this->uniform_vector_size[src] = 0;
385 /* Find the lowest place we can slot this uniform in. */
386 for (dst = 0; dst < src; dst++) {
387 if (this->uniform_vector_size[dst] + size <= 4)
396 new_chan[src] = this->uniform_vector_size[dst];
398 /* Move the references to the data */
399 for (int j = 0; j < size; j++) {
400 prog_data->param[dst * 4 + new_chan[src] + j] =
401 prog_data->param[src * 4 + j];
404 this->uniform_vector_size[dst] += size;
405 this->uniform_vector_size[src] = 0;
408 new_uniform_count = MAX2(new_uniform_count, dst + 1);
411 this->uniforms = new_uniform_count;
413 /* Now, update the instructions for our repacked uniforms. */
414 foreach_list(node, &this->instructions) {
415 vec4_instruction *inst = (vec4_instruction *)node;
417 for (int i = 0 ; i < 3; i++) {
418 int src = inst->src[i].reg;
420 if (inst->src[i].file != UNIFORM)
423 inst->src[i].reg = new_loc[src];
425 int sx = BRW_GET_SWZ(inst->src[i].swizzle, 0) + new_chan[src];
426 int sy = BRW_GET_SWZ(inst->src[i].swizzle, 1) + new_chan[src];
427 int sz = BRW_GET_SWZ(inst->src[i].swizzle, 2) + new_chan[src];
428 int sw = BRW_GET_SWZ(inst->src[i].swizzle, 3) + new_chan[src];
429 inst->src[i].swizzle = BRW_SWIZZLE4(sx, sy, sz, sw);
435 src_reg::is_zero() const
440 if (type == BRW_REGISTER_TYPE_F) {
448 src_reg::is_one() const
453 if (type == BRW_REGISTER_TYPE_F) {
461 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
463 * While GLSL IR also performs this optimization, we end up with it in
464 * our instruction stream for a couple of reasons. One is that we
465 * sometimes generate silly instructions, for example in array access
466 * where we'll generate "ADD offset, index, base" even if base is 0.
467 * The other is that GLSL IR's constant propagation doesn't track the
468 * components of aggregates, so some VS patterns (initialize matrix to
469 * 0, accumulate in vertex blending factors) end up breaking down to
470 * instructions involving 0.
473 vec4_visitor::opt_algebraic()
475 bool progress = false;
477 foreach_list(node, &this->instructions) {
478 vec4_instruction *inst = (vec4_instruction *)node;
480 switch (inst->opcode) {
482 if (inst->src[1].is_zero()) {
483 inst->opcode = BRW_OPCODE_MOV;
484 inst->src[1] = src_reg();
490 if (inst->src[1].is_zero()) {
491 inst->opcode = BRW_OPCODE_MOV;
492 switch (inst->src[0].type) {
493 case BRW_REGISTER_TYPE_F:
494 inst->src[0] = src_reg(0.0f);
496 case BRW_REGISTER_TYPE_D:
497 inst->src[0] = src_reg(0);
499 case BRW_REGISTER_TYPE_UD:
500 inst->src[0] = src_reg(0u);
503 assert(!"not reached");
504 inst->src[0] = src_reg(0.0f);
507 inst->src[1] = src_reg();
509 } else if (inst->src[1].is_one()) {
510 inst->opcode = BRW_OPCODE_MOV;
511 inst->src[1] = src_reg();
521 this->live_intervals_valid = false;
527 * Only a limited number of hardware registers may be used for push
528 * constants, so this turns access to the overflowed constants into
532 vec4_visitor::move_push_constants_to_pull_constants()
534 int pull_constant_loc[this->uniforms];
536 /* Only allow 32 registers (256 uniform components) as push constants,
537 * which is the limit on gen6.
539 int max_uniform_components = 32 * 8;
540 if (this->uniforms * 4 <= max_uniform_components)
543 /* Make some sort of choice as to which uniforms get sent to pull
544 * constants. We could potentially do something clever here like
545 * look for the most infrequently used uniform vec4s, but leave
548 for (int i = 0; i < this->uniforms * 4; i += 4) {
549 pull_constant_loc[i / 4] = -1;
551 if (i >= max_uniform_components) {
552 const float **values = &prog_data->param[i];
554 /* Try to find an existing copy of this uniform in the pull
555 * constants if it was part of an array access already.
557 for (unsigned int j = 0; j < prog_data->nr_pull_params; j += 4) {
560 for (matches = 0; matches < 4; matches++) {
561 if (prog_data->pull_param[j + matches] != values[matches])
566 pull_constant_loc[i / 4] = j / 4;
571 if (pull_constant_loc[i / 4] == -1) {
572 assert(prog_data->nr_pull_params % 4 == 0);
573 pull_constant_loc[i / 4] = prog_data->nr_pull_params / 4;
575 for (int j = 0; j < 4; j++) {
576 prog_data->pull_param[prog_data->nr_pull_params++] = values[j];
582 /* Now actually rewrite usage of the things we've moved to pull
585 foreach_list_safe(node, &this->instructions) {
586 vec4_instruction *inst = (vec4_instruction *)node;
588 for (int i = 0 ; i < 3; i++) {
589 if (inst->src[i].file != UNIFORM ||
590 pull_constant_loc[inst->src[i].reg] == -1)
593 int uniform = inst->src[i].reg;
595 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
597 emit_pull_constant_load(inst, temp, inst->src[i],
598 pull_constant_loc[uniform]);
600 inst->src[i].file = temp.file;
601 inst->src[i].reg = temp.reg;
602 inst->src[i].reg_offset = temp.reg_offset;
603 inst->src[i].reladdr = NULL;
607 /* Repack push constants to remove the now-unused ones. */
608 pack_uniform_registers();
612 * Sets the dependency control fields on instructions after register
613 * allocation and before the generator is run.
615 * When you have a sequence of instructions like:
617 * DP4 temp.x vertex uniform[0]
618 * DP4 temp.y vertex uniform[0]
619 * DP4 temp.z vertex uniform[0]
620 * DP4 temp.w vertex uniform[0]
622 * The hardware doesn't know that it can actually run the later instructions
623 * while the previous ones are in flight, producing stalls. However, we have
624 * manual fields we can set in the instructions that let it do so.
627 vec4_visitor::opt_set_dependency_control()
629 vec4_instruction *last_grf_write[BRW_MAX_GRF];
630 uint8_t grf_channels_written[BRW_MAX_GRF];
631 vec4_instruction *last_mrf_write[BRW_MAX_GRF];
632 uint8_t mrf_channels_written[BRW_MAX_GRF];
636 assert(prog_data->total_grf ||
637 !"Must be called after register allocation");
639 for (int i = 0; i < cfg.num_blocks; i++) {
640 bblock_t *bblock = cfg.blocks[i];
641 vec4_instruction *inst;
643 memset(last_grf_write, 0, sizeof(last_grf_write));
644 memset(last_mrf_write, 0, sizeof(last_mrf_write));
646 for (inst = (vec4_instruction *)bblock->start;
647 inst != (vec4_instruction *)bblock->end->next;
648 inst = (vec4_instruction *)inst->next) {
649 /* If we read from a register that we were doing dependency control
650 * on, don't do dependency control across the read.
652 for (int i = 0; i < 3; i++) {
653 int reg = inst->src[i].reg + inst->src[i].reg_offset;
654 if (inst->src[i].file == GRF) {
655 last_grf_write[reg] = NULL;
656 } else if (inst->src[i].file == HW_REG) {
657 memset(last_grf_write, 0, sizeof(last_grf_write));
660 assert(inst->src[i].file != MRF);
663 /* In the presence of send messages, totally interrupt dependency
664 * control. They're long enough that the chance of dependency
665 * control around them just doesn't matter.
668 memset(last_grf_write, 0, sizeof(last_grf_write));
669 memset(last_mrf_write, 0, sizeof(last_mrf_write));
673 /* It looks like setting dependency control on a predicated
674 * instruction hangs the GPU.
676 if (inst->predicate) {
677 memset(last_grf_write, 0, sizeof(last_grf_write));
678 memset(last_mrf_write, 0, sizeof(last_mrf_write));
682 /* Now, see if we can do dependency control for this instruction
683 * against a previous one writing to its destination.
685 int reg = inst->dst.reg + inst->dst.reg_offset;
686 if (inst->dst.file == GRF) {
687 if (last_grf_write[reg] &&
688 !(inst->dst.writemask & grf_channels_written[reg])) {
689 last_grf_write[reg]->no_dd_clear = true;
690 inst->no_dd_check = true;
692 grf_channels_written[reg] = 0;
695 last_grf_write[reg] = inst;
696 grf_channels_written[reg] |= inst->dst.writemask;
697 } else if (inst->dst.file == MRF) {
698 if (last_mrf_write[reg] &&
699 !(inst->dst.writemask & mrf_channels_written[reg])) {
700 last_mrf_write[reg]->no_dd_clear = true;
701 inst->no_dd_check = true;
703 mrf_channels_written[reg] = 0;
706 last_mrf_write[reg] = inst;
707 mrf_channels_written[reg] |= inst->dst.writemask;
708 } else if (inst->dst.reg == HW_REG) {
709 if (inst->dst.fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE)
710 memset(last_grf_write, 0, sizeof(last_grf_write));
711 if (inst->dst.fixed_hw_reg.file == BRW_MESSAGE_REGISTER_FILE)
712 memset(last_mrf_write, 0, sizeof(last_mrf_write));
719 vec4_instruction::can_reswizzle_dst(int dst_writemask,
723 /* If this instruction sets anything not referenced by swizzle, then we'd
724 * totally break it when we reswizzle.
726 if (dst.writemask & ~swizzle_mask)
735 /* Check if there happens to be no reswizzling required. */
736 for (int c = 0; c < 4; c++) {
737 int bit = 1 << BRW_GET_SWZ(swizzle, c);
738 /* Skip components of the swizzle not used by the dst. */
739 if (!(dst_writemask & (1 << c)))
742 /* We don't do the reswizzling yet, so just sanity check that we
753 * For any channels in the swizzle's source that were populated by this
754 * instruction, rewrite the instruction to put the appropriate result directly
757 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
760 vec4_instruction::reswizzle_dst(int dst_writemask, int swizzle)
762 int new_writemask = 0;
768 for (int c = 0; c < 4; c++) {
769 int bit = 1 << BRW_GET_SWZ(swizzle, c);
770 /* Skip components of the swizzle not used by the dst. */
771 if (!(dst_writemask & (1 << c)))
773 /* If we were populating this component, then populate the
774 * corresponding channel of the new dst.
776 if (dst.writemask & bit)
777 new_writemask |= (1 << c);
779 dst.writemask = new_writemask;
782 for (int c = 0; c < 4; c++) {
783 /* Skip components of the swizzle not used by the dst. */
784 if (!(dst_writemask & (1 << c)))
787 /* We don't do the reswizzling yet, so just sanity check that we
790 assert((1 << BRW_GET_SWZ(swizzle, c)) == (1 << c));
797 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
798 * just written and then MOVed into another reg and making the original write
799 * of the GRF write directly to the final destination instead.
802 vec4_visitor::opt_register_coalesce()
804 bool progress = false;
807 calculate_live_intervals();
809 foreach_list_safe(node, &this->instructions) {
810 vec4_instruction *inst = (vec4_instruction *)node;
815 if (inst->opcode != BRW_OPCODE_MOV ||
816 (inst->dst.file != GRF && inst->dst.file != MRF) ||
818 inst->src[0].file != GRF ||
819 inst->dst.type != inst->src[0].type ||
820 inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
823 bool to_mrf = (inst->dst.file == MRF);
825 /* Can't coalesce this GRF if someone else was going to
828 if (this->virtual_grf_use[inst->src[0].reg] > ip)
831 /* We need to check interference with the final destination between this
832 * instruction and the earliest instruction involved in writing the GRF
833 * we're eliminating. To do that, keep track of which of our source
834 * channels we've seen initialized.
836 bool chans_needed[4] = {false, false, false, false};
837 int chans_remaining = 0;
838 int swizzle_mask = 0;
839 for (int i = 0; i < 4; i++) {
840 int chan = BRW_GET_SWZ(inst->src[0].swizzle, i);
842 if (!(inst->dst.writemask & (1 << i)))
845 swizzle_mask |= (1 << chan);
847 if (!chans_needed[chan]) {
848 chans_needed[chan] = true;
853 /* Now walk up the instruction stream trying to see if we can rewrite
854 * everything writing to the temporary to write into the destination
857 vec4_instruction *scan_inst;
858 for (scan_inst = (vec4_instruction *)inst->prev;
859 scan_inst->prev != NULL;
860 scan_inst = (vec4_instruction *)scan_inst->prev) {
861 if (scan_inst->dst.file == GRF &&
862 scan_inst->dst.reg == inst->src[0].reg &&
863 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
864 /* Found something writing to the reg we want to coalesce away. */
866 /* SEND instructions can't have MRF as a destination. */
870 if (intel->gen == 6) {
871 /* gen6 math instructions must have the destination be
872 * GRF, so no compute-to-MRF for them.
874 if (scan_inst->is_math()) {
880 /* If we can't handle the swizzle, bail. */
881 if (!scan_inst->can_reswizzle_dst(inst->dst.writemask,
882 inst->src[0].swizzle,
887 /* Mark which channels we found unconditional writes for. */
888 if (!scan_inst->predicate) {
889 for (int i = 0; i < 4; i++) {
890 if (scan_inst->dst.writemask & (1 << i) &&
892 chans_needed[i] = false;
898 if (chans_remaining == 0)
902 /* We don't handle flow control here. Most computation of values
903 * that could be coalesced happens just before their use.
905 if (scan_inst->opcode == BRW_OPCODE_DO ||
906 scan_inst->opcode == BRW_OPCODE_WHILE ||
907 scan_inst->opcode == BRW_OPCODE_ELSE ||
908 scan_inst->opcode == BRW_OPCODE_ENDIF) {
912 /* You can't read from an MRF, so if someone else reads our MRF's
913 * source GRF that we wanted to rewrite, that stops us. If it's a
914 * GRF we're trying to coalesce to, we don't actually handle
915 * rewriting sources so bail in that case as well.
917 bool interfered = false;
918 for (int i = 0; i < 3; i++) {
919 if (scan_inst->src[i].file == GRF &&
920 scan_inst->src[i].reg == inst->src[0].reg &&
921 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
928 /* If somebody else writes our destination here, we can't coalesce
931 if (scan_inst->dst.file == inst->dst.file &&
932 scan_inst->dst.reg == inst->dst.reg) {
936 /* Check for reads of the register we're trying to coalesce into. We
937 * can't go rewriting instructions above that to put some other value
938 * in the register instead.
940 if (to_mrf && scan_inst->mlen > 0) {
941 if (inst->dst.reg >= scan_inst->base_mrf &&
942 inst->dst.reg < scan_inst->base_mrf + scan_inst->mlen) {
946 for (int i = 0; i < 3; i++) {
947 if (scan_inst->src[i].file == inst->dst.file &&
948 scan_inst->src[i].reg == inst->dst.reg &&
949 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
958 if (chans_remaining == 0) {
959 /* If we've made it here, we have an MOV we want to coalesce out, and
960 * a scan_inst pointing to the earliest instruction involved in
961 * computing the value. Now go rewrite the instruction stream
965 while (scan_inst != inst) {
966 if (scan_inst->dst.file == GRF &&
967 scan_inst->dst.reg == inst->src[0].reg &&
968 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
969 scan_inst->reswizzle_dst(inst->dst.writemask,
970 inst->src[0].swizzle);
971 scan_inst->dst.file = inst->dst.file;
972 scan_inst->dst.reg = inst->dst.reg;
973 scan_inst->dst.reg_offset = inst->dst.reg_offset;
974 scan_inst->saturate |= inst->saturate;
976 scan_inst = (vec4_instruction *)scan_inst->next;
984 live_intervals_valid = false;
990 * Splits virtual GRFs requesting more than one contiguous physical register.
992 * We initially create large virtual GRFs for temporary structures, arrays,
993 * and matrices, so that the dereference visitor functions can add reg_offsets
994 * to work their way down to the actual member being accessed. But when it
995 * comes to optimization, we'd like to treat each register as individual
996 * storage if possible.
998 * So far, the only thing that might prevent splitting is a send message from
1002 vec4_visitor::split_virtual_grfs()
1004 int num_vars = this->virtual_grf_count;
1005 int new_virtual_grf[num_vars];
1006 bool split_grf[num_vars];
1008 memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1010 /* Try to split anything > 0 sized. */
1011 for (int i = 0; i < num_vars; i++) {
1012 split_grf[i] = this->virtual_grf_sizes[i] != 1;
1015 /* Check that the instructions are compatible with the registers we're trying
1018 foreach_list(node, &this->instructions) {
1019 vec4_instruction *inst = (vec4_instruction *)node;
1021 /* If there's a SEND message loading from a GRF on gen7+, it needs to be
1022 * contiguous. Assume that the GRF for the SEND is always in src[0].
1024 if (inst->is_send_from_grf()) {
1025 split_grf[inst->src[0].reg] = false;
1029 /* Allocate new space for split regs. Note that the virtual
1030 * numbers will be contiguous.
1032 for (int i = 0; i < num_vars; i++) {
1036 new_virtual_grf[i] = virtual_grf_alloc(1);
1037 for (int j = 2; j < this->virtual_grf_sizes[i]; j++) {
1038 int reg = virtual_grf_alloc(1);
1039 assert(reg == new_virtual_grf[i] + j - 1);
1042 this->virtual_grf_sizes[i] = 1;
1045 foreach_list(node, &this->instructions) {
1046 vec4_instruction *inst = (vec4_instruction *)node;
1048 if (inst->dst.file == GRF && split_grf[inst->dst.reg] &&
1049 inst->dst.reg_offset != 0) {
1050 inst->dst.reg = (new_virtual_grf[inst->dst.reg] +
1051 inst->dst.reg_offset - 1);
1052 inst->dst.reg_offset = 0;
1054 for (int i = 0; i < 3; i++) {
1055 if (inst->src[i].file == GRF && split_grf[inst->src[i].reg] &&
1056 inst->src[i].reg_offset != 0) {
1057 inst->src[i].reg = (new_virtual_grf[inst->src[i].reg] +
1058 inst->src[i].reg_offset - 1);
1059 inst->src[i].reg_offset = 0;
1063 this->live_intervals_valid = false;
1067 vec4_visitor::dump_instruction(vec4_instruction *inst)
1069 printf("%s ", brw_instruction_name(inst->opcode));
1071 switch (inst->dst.file) {
1073 printf("vgrf%d.%d", inst->dst.reg, inst->dst.reg_offset);
1076 printf("m%d", inst->dst.reg);
1085 if (inst->dst.writemask != WRITEMASK_XYZW) {
1087 if (inst->dst.writemask & 1)
1089 if (inst->dst.writemask & 2)
1091 if (inst->dst.writemask & 4)
1093 if (inst->dst.writemask & 8)
1098 for (int i = 0; i < 3; i++) {
1099 switch (inst->src[i].file) {
1101 printf("vgrf%d", inst->src[i].reg);
1104 printf("attr%d", inst->src[i].reg);
1107 printf("u%d", inst->src[i].reg);
1110 switch (inst->src[i].type) {
1111 case BRW_REGISTER_TYPE_F:
1112 printf("%fF", inst->src[i].imm.f);
1114 case BRW_REGISTER_TYPE_D:
1115 printf("%dD", inst->src[i].imm.i);
1117 case BRW_REGISTER_TYPE_UD:
1118 printf("%uU", inst->src[i].imm.u);
1133 if (inst->src[i].reg_offset)
1134 printf(".%d", inst->src[i].reg_offset);
1136 static const char *chans[4] = {"x", "y", "z", "w"};
1138 for (int c = 0; c < 4; c++) {
1139 printf("%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1150 vec4_visitor::dump_instructions()
1153 foreach_list_safe(node, &this->instructions) {
1154 vec4_instruction *inst = (vec4_instruction *)node;
1155 printf("%d: ", ip++);
1156 dump_instruction(inst);
1161 * Replace each register of type ATTR in this->instructions with a reference
1162 * to a fixed HW register.
1165 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map)
1167 foreach_list(node, &this->instructions) {
1168 vec4_instruction *inst = (vec4_instruction *)node;
1170 /* We have to support ATTR as a destination for GL_FIXED fixup. */
1171 if (inst->dst.file == ATTR) {
1172 int grf = attribute_map[inst->dst.reg + inst->dst.reg_offset];
1174 /* All attributes used in the shader need to have been assigned a
1175 * hardware register by the caller
1179 struct brw_reg reg = brw_vec8_grf(grf, 0);
1180 reg.type = inst->dst.type;
1181 reg.dw1.bits.writemask = inst->dst.writemask;
1183 inst->dst.file = HW_REG;
1184 inst->dst.fixed_hw_reg = reg;
1187 for (int i = 0; i < 3; i++) {
1188 if (inst->src[i].file != ATTR)
1191 int grf = attribute_map[inst->src[i].reg + inst->src[i].reg_offset];
1193 /* All attributes used in the shader need to have been assigned a
1194 * hardware register by the caller
1198 struct brw_reg reg = brw_vec8_grf(grf, 0);
1199 reg.dw1.bits.swizzle = inst->src[i].swizzle;
1200 reg.type = inst->src[i].type;
1201 if (inst->src[i].abs)
1203 if (inst->src[i].negate)
1206 inst->src[i].file = HW_REG;
1207 inst->src[i].fixed_hw_reg = reg;
1213 vec4_vs_visitor::setup_attributes(int payload_reg)
1216 int attribute_map[VERT_ATTRIB_MAX + 1];
1217 memset(attribute_map, 0, sizeof(attribute_map));
1220 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
1221 if (vs_prog_data->inputs_read & BITFIELD64_BIT(i)) {
1222 attribute_map[i] = payload_reg + nr_attributes;
1227 /* VertexID is stored by the VF as the last vertex element, but we
1228 * don't represent it with a flag in inputs_read, so we call it
1231 if (vs_prog_data->uses_vertexid) {
1232 attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1236 lower_attributes_to_hw_regs(attribute_map);
1238 /* The BSpec says we always have to read at least one thing from
1239 * the VF, and it appears that the hardware wedges otherwise.
1241 if (nr_attributes == 0)
1244 prog_data->urb_read_length = (nr_attributes + 1) / 2;
1246 unsigned vue_entries =
1247 MAX2(nr_attributes, prog_data->vue_map.num_slots);
1249 if (intel->gen == 6)
1250 prog_data->urb_entry_size = ALIGN(vue_entries, 8) / 8;
1252 prog_data->urb_entry_size = ALIGN(vue_entries, 4) / 4;
1254 return payload_reg + nr_attributes;
1258 vec4_visitor::setup_uniforms(int reg)
1260 /* The pre-gen6 VS requires that some push constants get loaded no
1261 * matter what, or the GPU would hang.
1263 if (intel->gen < 6 && this->uniforms == 0) {
1264 this->uniform_vector_size[this->uniforms] = 1;
1266 for (unsigned int i = 0; i < 4; i++) {
1267 unsigned int slot = this->uniforms * 4 + i;
1268 static float zero = 0.0;
1269 prog_data->param[slot] = &zero;
1275 reg += ALIGN(uniforms, 2) / 2;
1278 prog_data->nr_params = this->uniforms * 4;
1280 prog_data->curb_read_length = reg - 1;
1286 vec4_visitor::setup_payload(void)
1290 /* The payload always contains important data in g0, which contains
1291 * the URB handles that are passed on to the URB write at the end
1292 * of the thread. So, we always start push constants at g1.
1296 reg = setup_uniforms(reg);
1298 reg = setup_attributes(reg);
1300 this->first_non_payload_grf = reg;
1304 vec4_visitor::get_timestamp()
1306 assert(intel->gen >= 7);
1308 src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1311 BRW_REGISTER_TYPE_UD,
1312 BRW_VERTICAL_STRIDE_0,
1314 BRW_HORIZONTAL_STRIDE_4,
1318 dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1320 vec4_instruction *mov = emit(MOV(dst, ts));
1321 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1322 * even if it's not enabled in the dispatch.
1324 mov->force_writemask_all = true;
1326 return src_reg(dst);
1330 vec4_visitor::emit_shader_time_begin()
1332 current_annotation = "shader time start";
1333 shader_start_time = get_timestamp();
1337 vec4_visitor::emit_shader_time_end()
1339 current_annotation = "shader time end";
1340 src_reg shader_end_time = get_timestamp();
1343 /* Check that there weren't any timestamp reset events (assuming these
1344 * were the only two timestamp reads that happened).
1346 src_reg reset_end = shader_end_time;
1347 reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1348 vec4_instruction *test = emit(AND(dst_null_d(), reset_end, src_reg(1u)));
1349 test->conditional_mod = BRW_CONDITIONAL_Z;
1351 emit(IF(BRW_PREDICATE_NORMAL));
1353 /* Take the current timestamp and get the delta. */
1354 shader_start_time.negate = true;
1355 dst_reg diff = dst_reg(this, glsl_type::uint_type);
1356 emit(ADD(diff, shader_start_time, shader_end_time));
1358 /* If there were no instructions between the two timestamp gets, the diff
1359 * is 2 cycles. Remove that overhead, so I can forget about that when
1360 * trying to determine the time taken for single instructions.
1362 emit(ADD(diff, src_reg(diff), src_reg(-2u)));
1364 emit_shader_time_write(ST_VS, src_reg(diff));
1365 emit_shader_time_write(ST_VS_WRITTEN, src_reg(1u));
1366 emit(BRW_OPCODE_ELSE);
1367 emit_shader_time_write(ST_VS_RESET, src_reg(1u));
1368 emit(BRW_OPCODE_ENDIF);
1372 vec4_visitor::emit_shader_time_write(enum shader_time_shader_type type,
1375 int shader_time_index =
1376 brw_get_shader_time_index(brw, shader_prog, prog, type);
1379 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1381 dst_reg offset = dst;
1385 offset.type = BRW_REGISTER_TYPE_UD;
1386 emit(MOV(offset, src_reg(shader_time_index * SHADER_TIME_STRIDE)));
1388 time.type = BRW_REGISTER_TYPE_UD;
1389 emit(MOV(time, src_reg(value)));
1391 emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1397 sanity_param_count = prog->Parameters->NumParameters;
1399 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
1400 emit_shader_time_begin();
1404 /* Generate VS IR for main(). (the visitor only descends into
1405 * functions called "main").
1408 visit_instructions(shader->ir);
1410 emit_program_code();
1414 if (key->userclip_active && !key->uses_clip_distance)
1415 setup_uniform_clipplane_values();
1419 /* Before any optimization, push array accesses out to scratch
1420 * space where we need them to be. This pass may allocate new
1421 * virtual GRFs, so we want to do it early. It also makes sure
1422 * that we have reladdr computations available for CSE, since we'll
1423 * often do repeated subexpressions for those.
1426 move_grf_array_access_to_scratch();
1427 move_uniform_array_access_to_pull_constants();
1429 /* The ARB_vertex_program frontend emits pull constant loads directly
1430 * rather than using reladdr, so we don't need to walk through all the
1431 * instructions looking for things to move. There isn't anything.
1433 * We do still need to split things to vec4 size.
1435 split_uniform_registers();
1437 pack_uniform_registers();
1438 move_push_constants_to_pull_constants();
1439 split_virtual_grfs();
1444 progress = dead_code_eliminate() || progress;
1445 progress = opt_copy_propagation() || progress;
1446 progress = opt_algebraic() || progress;
1447 progress = opt_register_coalesce() || progress;
1457 /* Debug of register spilling: Go spill everything. */
1458 const int grf_count = virtual_grf_count;
1459 float spill_costs[virtual_grf_count];
1460 bool no_spill[virtual_grf_count];
1461 evaluate_spill_costs(spill_costs, no_spill);
1462 for (int i = 0; i < grf_count; i++) {
1469 while (!reg_allocate()) {
1474 opt_set_dependency_control();
1476 /* If any state parameters were appended, then ParameterValues could have
1477 * been realloced, in which case the driver uniform storage set up by
1478 * _mesa_associate_uniform_storage() would point to freed memory. Make
1479 * sure that didn't happen.
1481 assert(sanity_param_count == prog->Parameters->NumParameters);
1486 } /* namespace brw */
1491 * Compile a vertex shader.
1493 * Returns the final assembly and the program's size.
1496 brw_vs_emit(struct brw_context *brw,
1497 struct gl_shader_program *prog,
1498 struct brw_vs_compile *c,
1499 struct brw_vs_prog_data *prog_data,
1501 unsigned *final_assembly_size)
1503 struct intel_context *intel = &brw->intel;
1504 bool start_busy = false;
1505 float start_time = 0;
1507 if (unlikely(intel->perf_debug)) {
1508 start_busy = (intel->batch.last_bo &&
1509 drm_intel_bo_busy(intel->batch.last_bo));
1510 start_time = get_time();
1513 struct brw_shader *shader = NULL;
1515 shader = (brw_shader *) prog->_LinkedShaders[MESA_SHADER_VERTEX];
1517 if (unlikely(INTEL_DEBUG & DEBUG_VS)) {
1519 printf("GLSL IR for native vertex shader %d:\n", prog->Name);
1520 _mesa_print_ir(shader->ir, NULL);
1523 printf("ARB_vertex_program %d for native vertex shader\n",
1524 c->vp->program.Base.Id);
1525 _mesa_print_program(&c->vp->program.Base);
1529 vec4_vs_visitor v(brw, c, prog_data, prog, shader, mem_ctx);
1532 prog->LinkStatus = false;
1533 ralloc_strcat(&prog->InfoLog, v.fail_msg);
1536 _mesa_problem(NULL, "Failed to compile vertex shader: %s\n",
1542 vec4_generator g(brw, prog, &c->vp->program.Base, mem_ctx,
1543 INTEL_DEBUG & DEBUG_VS);
1544 const unsigned *generated =g.generate_assembly(&v.instructions,
1545 final_assembly_size);
1547 if (unlikely(intel->perf_debug) && shader) {
1548 if (shader->compiled_once) {
1549 brw_vs_debug_recompile(brw, prog, &c->key);
1551 if (start_busy && !drm_intel_bo_busy(intel->batch.last_bo)) {
1552 perf_debug("VS compile took %.03f ms and stalled the GPU\n",
1553 (get_time() - start_time) * 1000);
1555 shader->compiled_once = true;