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:
262 case SHADER_OPCODE_TEX:
263 case SHADER_OPCODE_TXL:
264 case SHADER_OPCODE_TXD:
265 case SHADER_OPCODE_TXF:
266 case SHADER_OPCODE_TXF_MS:
267 case SHADER_OPCODE_TXS:
268 return inst->header_present ? 1 : 0;
270 assert(!"not reached");
276 src_reg::equals(src_reg *r)
278 return (file == r->file &&
280 reg_offset == r->reg_offset &&
282 negate == r->negate &&
284 swizzle == r->swizzle &&
285 !reladdr && !r->reladdr &&
286 memcmp(&fixed_hw_reg, &r->fixed_hw_reg,
287 sizeof(fixed_hw_reg)) == 0 &&
292 * Must be called after calculate_live_intervales() to remove unused
293 * writes to registers -- register allocation will fail otherwise
294 * because something deffed but not used won't be considered to
295 * interfere with other regs.
298 vec4_visitor::dead_code_eliminate()
300 bool progress = false;
303 calculate_live_intervals();
305 foreach_list_safe(node, &this->instructions) {
306 vec4_instruction *inst = (vec4_instruction *)node;
308 if (inst->dst.file == GRF && this->virtual_grf_use[inst->dst.reg] <= pc) {
317 live_intervals_valid = false;
323 vec4_visitor::split_uniform_registers()
325 /* Prior to this, uniforms have been in an array sized according to
326 * the number of vector uniforms present, sparsely filled (so an
327 * aggregate results in reg indices being skipped over). Now we're
328 * going to cut those aggregates up so each .reg index is one
329 * vector. The goal is to make elimination of unused uniform
330 * components easier later.
332 foreach_list(node, &this->instructions) {
333 vec4_instruction *inst = (vec4_instruction *)node;
335 for (int i = 0 ; i < 3; i++) {
336 if (inst->src[i].file != UNIFORM)
339 assert(!inst->src[i].reladdr);
341 inst->src[i].reg += inst->src[i].reg_offset;
342 inst->src[i].reg_offset = 0;
346 /* Update that everything is now vector-sized. */
347 for (int i = 0; i < this->uniforms; i++) {
348 this->uniform_size[i] = 1;
353 vec4_visitor::pack_uniform_registers()
355 bool uniform_used[this->uniforms];
356 int new_loc[this->uniforms];
357 int new_chan[this->uniforms];
359 memset(uniform_used, 0, sizeof(uniform_used));
360 memset(new_loc, 0, sizeof(new_loc));
361 memset(new_chan, 0, sizeof(new_chan));
363 /* Find which uniform vectors are actually used by the program. We
364 * expect unused vector elements when we've moved array access out
365 * to pull constants, and from some GLSL code generators like wine.
367 foreach_list(node, &this->instructions) {
368 vec4_instruction *inst = (vec4_instruction *)node;
370 for (int i = 0 ; i < 3; i++) {
371 if (inst->src[i].file != UNIFORM)
374 uniform_used[inst->src[i].reg] = true;
378 int new_uniform_count = 0;
380 /* Now, figure out a packing of the live uniform vectors into our
383 for (int src = 0; src < uniforms; src++) {
384 int size = this->uniform_vector_size[src];
386 if (!uniform_used[src]) {
387 this->uniform_vector_size[src] = 0;
392 /* Find the lowest place we can slot this uniform in. */
393 for (dst = 0; dst < src; dst++) {
394 if (this->uniform_vector_size[dst] + size <= 4)
403 new_chan[src] = this->uniform_vector_size[dst];
405 /* Move the references to the data */
406 for (int j = 0; j < size; j++) {
407 prog_data->param[dst * 4 + new_chan[src] + j] =
408 prog_data->param[src * 4 + j];
411 this->uniform_vector_size[dst] += size;
412 this->uniform_vector_size[src] = 0;
415 new_uniform_count = MAX2(new_uniform_count, dst + 1);
418 this->uniforms = new_uniform_count;
420 /* Now, update the instructions for our repacked uniforms. */
421 foreach_list(node, &this->instructions) {
422 vec4_instruction *inst = (vec4_instruction *)node;
424 for (int i = 0 ; i < 3; i++) {
425 int src = inst->src[i].reg;
427 if (inst->src[i].file != UNIFORM)
430 inst->src[i].reg = new_loc[src];
432 int sx = BRW_GET_SWZ(inst->src[i].swizzle, 0) + new_chan[src];
433 int sy = BRW_GET_SWZ(inst->src[i].swizzle, 1) + new_chan[src];
434 int sz = BRW_GET_SWZ(inst->src[i].swizzle, 2) + new_chan[src];
435 int sw = BRW_GET_SWZ(inst->src[i].swizzle, 3) + new_chan[src];
436 inst->src[i].swizzle = BRW_SWIZZLE4(sx, sy, sz, sw);
442 src_reg::is_zero() const
447 if (type == BRW_REGISTER_TYPE_F) {
455 src_reg::is_one() const
460 if (type == BRW_REGISTER_TYPE_F) {
468 * Does algebraic optimizations (0 * a = 0, 1 * a = a, a + 0 = a).
470 * While GLSL IR also performs this optimization, we end up with it in
471 * our instruction stream for a couple of reasons. One is that we
472 * sometimes generate silly instructions, for example in array access
473 * where we'll generate "ADD offset, index, base" even if base is 0.
474 * The other is that GLSL IR's constant propagation doesn't track the
475 * components of aggregates, so some VS patterns (initialize matrix to
476 * 0, accumulate in vertex blending factors) end up breaking down to
477 * instructions involving 0.
480 vec4_visitor::opt_algebraic()
482 bool progress = false;
484 foreach_list(node, &this->instructions) {
485 vec4_instruction *inst = (vec4_instruction *)node;
487 switch (inst->opcode) {
489 if (inst->src[1].is_zero()) {
490 inst->opcode = BRW_OPCODE_MOV;
491 inst->src[1] = src_reg();
497 if (inst->src[1].is_zero()) {
498 inst->opcode = BRW_OPCODE_MOV;
499 switch (inst->src[0].type) {
500 case BRW_REGISTER_TYPE_F:
501 inst->src[0] = src_reg(0.0f);
503 case BRW_REGISTER_TYPE_D:
504 inst->src[0] = src_reg(0);
506 case BRW_REGISTER_TYPE_UD:
507 inst->src[0] = src_reg(0u);
510 assert(!"not reached");
511 inst->src[0] = src_reg(0.0f);
514 inst->src[1] = src_reg();
516 } else if (inst->src[1].is_one()) {
517 inst->opcode = BRW_OPCODE_MOV;
518 inst->src[1] = src_reg();
528 this->live_intervals_valid = false;
534 * Only a limited number of hardware registers may be used for push
535 * constants, so this turns access to the overflowed constants into
539 vec4_visitor::move_push_constants_to_pull_constants()
541 int pull_constant_loc[this->uniforms];
543 /* Only allow 32 registers (256 uniform components) as push constants,
544 * which is the limit on gen6.
546 int max_uniform_components = 32 * 8;
547 if (this->uniforms * 4 <= max_uniform_components)
550 /* Make some sort of choice as to which uniforms get sent to pull
551 * constants. We could potentially do something clever here like
552 * look for the most infrequently used uniform vec4s, but leave
555 for (int i = 0; i < this->uniforms * 4; i += 4) {
556 pull_constant_loc[i / 4] = -1;
558 if (i >= max_uniform_components) {
559 const float **values = &prog_data->param[i];
561 /* Try to find an existing copy of this uniform in the pull
562 * constants if it was part of an array access already.
564 for (unsigned int j = 0; j < prog_data->nr_pull_params; j += 4) {
567 for (matches = 0; matches < 4; matches++) {
568 if (prog_data->pull_param[j + matches] != values[matches])
573 pull_constant_loc[i / 4] = j / 4;
578 if (pull_constant_loc[i / 4] == -1) {
579 assert(prog_data->nr_pull_params % 4 == 0);
580 pull_constant_loc[i / 4] = prog_data->nr_pull_params / 4;
582 for (int j = 0; j < 4; j++) {
583 prog_data->pull_param[prog_data->nr_pull_params++] = values[j];
589 /* Now actually rewrite usage of the things we've moved to pull
592 foreach_list_safe(node, &this->instructions) {
593 vec4_instruction *inst = (vec4_instruction *)node;
595 for (int i = 0 ; i < 3; i++) {
596 if (inst->src[i].file != UNIFORM ||
597 pull_constant_loc[inst->src[i].reg] == -1)
600 int uniform = inst->src[i].reg;
602 dst_reg temp = dst_reg(this, glsl_type::vec4_type);
604 emit_pull_constant_load(inst, temp, inst->src[i],
605 pull_constant_loc[uniform]);
607 inst->src[i].file = temp.file;
608 inst->src[i].reg = temp.reg;
609 inst->src[i].reg_offset = temp.reg_offset;
610 inst->src[i].reladdr = NULL;
614 /* Repack push constants to remove the now-unused ones. */
615 pack_uniform_registers();
619 * Sets the dependency control fields on instructions after register
620 * allocation and before the generator is run.
622 * When you have a sequence of instructions like:
624 * DP4 temp.x vertex uniform[0]
625 * DP4 temp.y vertex uniform[0]
626 * DP4 temp.z vertex uniform[0]
627 * DP4 temp.w vertex uniform[0]
629 * The hardware doesn't know that it can actually run the later instructions
630 * while the previous ones are in flight, producing stalls. However, we have
631 * manual fields we can set in the instructions that let it do so.
634 vec4_visitor::opt_set_dependency_control()
636 vec4_instruction *last_grf_write[BRW_MAX_GRF];
637 uint8_t grf_channels_written[BRW_MAX_GRF];
638 vec4_instruction *last_mrf_write[BRW_MAX_GRF];
639 uint8_t mrf_channels_written[BRW_MAX_GRF];
643 assert(prog_data->total_grf ||
644 !"Must be called after register allocation");
646 for (int i = 0; i < cfg.num_blocks; i++) {
647 bblock_t *bblock = cfg.blocks[i];
648 vec4_instruction *inst;
650 memset(last_grf_write, 0, sizeof(last_grf_write));
651 memset(last_mrf_write, 0, sizeof(last_mrf_write));
653 for (inst = (vec4_instruction *)bblock->start;
654 inst != (vec4_instruction *)bblock->end->next;
655 inst = (vec4_instruction *)inst->next) {
656 /* If we read from a register that we were doing dependency control
657 * on, don't do dependency control across the read.
659 for (int i = 0; i < 3; i++) {
660 int reg = inst->src[i].reg + inst->src[i].reg_offset;
661 if (inst->src[i].file == GRF) {
662 last_grf_write[reg] = NULL;
663 } else if (inst->src[i].file == HW_REG) {
664 memset(last_grf_write, 0, sizeof(last_grf_write));
667 assert(inst->src[i].file != MRF);
670 /* In the presence of send messages, totally interrupt dependency
671 * control. They're long enough that the chance of dependency
672 * control around them just doesn't matter.
675 memset(last_grf_write, 0, sizeof(last_grf_write));
676 memset(last_mrf_write, 0, sizeof(last_mrf_write));
680 /* It looks like setting dependency control on a predicated
681 * instruction hangs the GPU.
683 if (inst->predicate) {
684 memset(last_grf_write, 0, sizeof(last_grf_write));
685 memset(last_mrf_write, 0, sizeof(last_mrf_write));
689 /* Now, see if we can do dependency control for this instruction
690 * against a previous one writing to its destination.
692 int reg = inst->dst.reg + inst->dst.reg_offset;
693 if (inst->dst.file == GRF) {
694 if (last_grf_write[reg] &&
695 !(inst->dst.writemask & grf_channels_written[reg])) {
696 last_grf_write[reg]->no_dd_clear = true;
697 inst->no_dd_check = true;
699 grf_channels_written[reg] = 0;
702 last_grf_write[reg] = inst;
703 grf_channels_written[reg] |= inst->dst.writemask;
704 } else if (inst->dst.file == MRF) {
705 if (last_mrf_write[reg] &&
706 !(inst->dst.writemask & mrf_channels_written[reg])) {
707 last_mrf_write[reg]->no_dd_clear = true;
708 inst->no_dd_check = true;
710 mrf_channels_written[reg] = 0;
713 last_mrf_write[reg] = inst;
714 mrf_channels_written[reg] |= inst->dst.writemask;
715 } else if (inst->dst.reg == HW_REG) {
716 if (inst->dst.fixed_hw_reg.file == BRW_GENERAL_REGISTER_FILE)
717 memset(last_grf_write, 0, sizeof(last_grf_write));
718 if (inst->dst.fixed_hw_reg.file == BRW_MESSAGE_REGISTER_FILE)
719 memset(last_mrf_write, 0, sizeof(last_mrf_write));
726 vec4_instruction::can_reswizzle_dst(int dst_writemask,
730 /* If this instruction sets anything not referenced by swizzle, then we'd
731 * totally break it when we reswizzle.
733 if (dst.writemask & ~swizzle_mask)
742 /* Check if there happens to be no reswizzling required. */
743 for (int c = 0; c < 4; c++) {
744 int bit = 1 << BRW_GET_SWZ(swizzle, c);
745 /* Skip components of the swizzle not used by the dst. */
746 if (!(dst_writemask & (1 << c)))
749 /* We don't do the reswizzling yet, so just sanity check that we
760 * For any channels in the swizzle's source that were populated by this
761 * instruction, rewrite the instruction to put the appropriate result directly
764 * e.g. for swizzle=yywx, MUL a.xy b c -> MUL a.yy_x b.yy z.yy_x
767 vec4_instruction::reswizzle_dst(int dst_writemask, int swizzle)
769 int new_writemask = 0;
775 for (int c = 0; c < 4; c++) {
776 int bit = 1 << BRW_GET_SWZ(swizzle, c);
777 /* Skip components of the swizzle not used by the dst. */
778 if (!(dst_writemask & (1 << c)))
780 /* If we were populating this component, then populate the
781 * corresponding channel of the new dst.
783 if (dst.writemask & bit)
784 new_writemask |= (1 << c);
786 dst.writemask = new_writemask;
789 for (int c = 0; c < 4; c++) {
790 /* Skip components of the swizzle not used by the dst. */
791 if (!(dst_writemask & (1 << c)))
794 /* We don't do the reswizzling yet, so just sanity check that we
797 assert((1 << BRW_GET_SWZ(swizzle, c)) == (1 << c));
804 * Tries to reduce extra MOV instructions by taking temporary GRFs that get
805 * just written and then MOVed into another reg and making the original write
806 * of the GRF write directly to the final destination instead.
809 vec4_visitor::opt_register_coalesce()
811 bool progress = false;
814 calculate_live_intervals();
816 foreach_list_safe(node, &this->instructions) {
817 vec4_instruction *inst = (vec4_instruction *)node;
822 if (inst->opcode != BRW_OPCODE_MOV ||
823 (inst->dst.file != GRF && inst->dst.file != MRF) ||
825 inst->src[0].file != GRF ||
826 inst->dst.type != inst->src[0].type ||
827 inst->src[0].abs || inst->src[0].negate || inst->src[0].reladdr)
830 bool to_mrf = (inst->dst.file == MRF);
832 /* Can't coalesce this GRF if someone else was going to
835 if (this->virtual_grf_use[inst->src[0].reg] > ip)
838 /* We need to check interference with the final destination between this
839 * instruction and the earliest instruction involved in writing the GRF
840 * we're eliminating. To do that, keep track of which of our source
841 * channels we've seen initialized.
843 bool chans_needed[4] = {false, false, false, false};
844 int chans_remaining = 0;
845 int swizzle_mask = 0;
846 for (int i = 0; i < 4; i++) {
847 int chan = BRW_GET_SWZ(inst->src[0].swizzle, i);
849 if (!(inst->dst.writemask & (1 << i)))
852 swizzle_mask |= (1 << chan);
854 if (!chans_needed[chan]) {
855 chans_needed[chan] = true;
860 /* Now walk up the instruction stream trying to see if we can rewrite
861 * everything writing to the temporary to write into the destination
864 vec4_instruction *scan_inst;
865 for (scan_inst = (vec4_instruction *)inst->prev;
866 scan_inst->prev != NULL;
867 scan_inst = (vec4_instruction *)scan_inst->prev) {
868 if (scan_inst->dst.file == GRF &&
869 scan_inst->dst.reg == inst->src[0].reg &&
870 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
871 /* Found something writing to the reg we want to coalesce away. */
873 /* SEND instructions can't have MRF as a destination. */
877 if (intel->gen == 6) {
878 /* gen6 math instructions must have the destination be
879 * GRF, so no compute-to-MRF for them.
881 if (scan_inst->is_math()) {
887 /* If we can't handle the swizzle, bail. */
888 if (!scan_inst->can_reswizzle_dst(inst->dst.writemask,
889 inst->src[0].swizzle,
894 /* Mark which channels we found unconditional writes for. */
895 if (!scan_inst->predicate) {
896 for (int i = 0; i < 4; i++) {
897 if (scan_inst->dst.writemask & (1 << i) &&
899 chans_needed[i] = false;
905 if (chans_remaining == 0)
909 /* We don't handle flow control here. Most computation of values
910 * that could be coalesced happens just before their use.
912 if (scan_inst->opcode == BRW_OPCODE_DO ||
913 scan_inst->opcode == BRW_OPCODE_WHILE ||
914 scan_inst->opcode == BRW_OPCODE_ELSE ||
915 scan_inst->opcode == BRW_OPCODE_ENDIF) {
919 /* You can't read from an MRF, so if someone else reads our MRF's
920 * source GRF that we wanted to rewrite, that stops us. If it's a
921 * GRF we're trying to coalesce to, we don't actually handle
922 * rewriting sources so bail in that case as well.
924 bool interfered = false;
925 for (int i = 0; i < 3; i++) {
926 if (scan_inst->src[i].file == GRF &&
927 scan_inst->src[i].reg == inst->src[0].reg &&
928 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
935 /* If somebody else writes our destination here, we can't coalesce
938 if (scan_inst->dst.file == inst->dst.file &&
939 scan_inst->dst.reg == inst->dst.reg) {
943 /* Check for reads of the register we're trying to coalesce into. We
944 * can't go rewriting instructions above that to put some other value
945 * in the register instead.
947 if (to_mrf && scan_inst->mlen > 0) {
948 if (inst->dst.reg >= scan_inst->base_mrf &&
949 inst->dst.reg < scan_inst->base_mrf + scan_inst->mlen) {
953 for (int i = 0; i < 3; i++) {
954 if (scan_inst->src[i].file == inst->dst.file &&
955 scan_inst->src[i].reg == inst->dst.reg &&
956 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
965 if (chans_remaining == 0) {
966 /* If we've made it here, we have an MOV we want to coalesce out, and
967 * a scan_inst pointing to the earliest instruction involved in
968 * computing the value. Now go rewrite the instruction stream
972 while (scan_inst != inst) {
973 if (scan_inst->dst.file == GRF &&
974 scan_inst->dst.reg == inst->src[0].reg &&
975 scan_inst->dst.reg_offset == inst->src[0].reg_offset) {
976 scan_inst->reswizzle_dst(inst->dst.writemask,
977 inst->src[0].swizzle);
978 scan_inst->dst.file = inst->dst.file;
979 scan_inst->dst.reg = inst->dst.reg;
980 scan_inst->dst.reg_offset = inst->dst.reg_offset;
981 scan_inst->saturate |= inst->saturate;
983 scan_inst = (vec4_instruction *)scan_inst->next;
991 live_intervals_valid = false;
997 * Splits virtual GRFs requesting more than one contiguous physical register.
999 * We initially create large virtual GRFs for temporary structures, arrays,
1000 * and matrices, so that the dereference visitor functions can add reg_offsets
1001 * to work their way down to the actual member being accessed. But when it
1002 * comes to optimization, we'd like to treat each register as individual
1003 * storage if possible.
1005 * So far, the only thing that might prevent splitting is a send message from
1009 vec4_visitor::split_virtual_grfs()
1011 int num_vars = this->virtual_grf_count;
1012 int new_virtual_grf[num_vars];
1013 bool split_grf[num_vars];
1015 memset(new_virtual_grf, 0, sizeof(new_virtual_grf));
1017 /* Try to split anything > 0 sized. */
1018 for (int i = 0; i < num_vars; i++) {
1019 split_grf[i] = this->virtual_grf_sizes[i] != 1;
1022 /* Check that the instructions are compatible with the registers we're trying
1025 foreach_list(node, &this->instructions) {
1026 vec4_instruction *inst = (vec4_instruction *)node;
1028 /* If there's a SEND message loading from a GRF on gen7+, it needs to be
1029 * contiguous. Assume that the GRF for the SEND is always in src[0].
1031 if (inst->is_send_from_grf()) {
1032 split_grf[inst->src[0].reg] = false;
1036 /* Allocate new space for split regs. Note that the virtual
1037 * numbers will be contiguous.
1039 for (int i = 0; i < num_vars; i++) {
1043 new_virtual_grf[i] = virtual_grf_alloc(1);
1044 for (int j = 2; j < this->virtual_grf_sizes[i]; j++) {
1045 int reg = virtual_grf_alloc(1);
1046 assert(reg == new_virtual_grf[i] + j - 1);
1049 this->virtual_grf_sizes[i] = 1;
1052 foreach_list(node, &this->instructions) {
1053 vec4_instruction *inst = (vec4_instruction *)node;
1055 if (inst->dst.file == GRF && split_grf[inst->dst.reg] &&
1056 inst->dst.reg_offset != 0) {
1057 inst->dst.reg = (new_virtual_grf[inst->dst.reg] +
1058 inst->dst.reg_offset - 1);
1059 inst->dst.reg_offset = 0;
1061 for (int i = 0; i < 3; i++) {
1062 if (inst->src[i].file == GRF && split_grf[inst->src[i].reg] &&
1063 inst->src[i].reg_offset != 0) {
1064 inst->src[i].reg = (new_virtual_grf[inst->src[i].reg] +
1065 inst->src[i].reg_offset - 1);
1066 inst->src[i].reg_offset = 0;
1070 this->live_intervals_valid = false;
1074 vec4_visitor::dump_instruction(backend_instruction *be_inst)
1076 vec4_instruction *inst = (vec4_instruction *)be_inst;
1078 printf("%s ", brw_instruction_name(inst->opcode));
1080 switch (inst->dst.file) {
1082 printf("vgrf%d.%d", inst->dst.reg, inst->dst.reg_offset);
1085 printf("m%d", inst->dst.reg);
1094 if (inst->dst.writemask != WRITEMASK_XYZW) {
1096 if (inst->dst.writemask & 1)
1098 if (inst->dst.writemask & 2)
1100 if (inst->dst.writemask & 4)
1102 if (inst->dst.writemask & 8)
1107 for (int i = 0; i < 3; i++) {
1108 switch (inst->src[i].file) {
1110 printf("vgrf%d", inst->src[i].reg);
1113 printf("attr%d", inst->src[i].reg);
1116 printf("u%d", inst->src[i].reg);
1119 switch (inst->src[i].type) {
1120 case BRW_REGISTER_TYPE_F:
1121 printf("%fF", inst->src[i].imm.f);
1123 case BRW_REGISTER_TYPE_D:
1124 printf("%dD", inst->src[i].imm.i);
1126 case BRW_REGISTER_TYPE_UD:
1127 printf("%uU", inst->src[i].imm.u);
1142 if (inst->src[i].reg_offset)
1143 printf(".%d", inst->src[i].reg_offset);
1145 static const char *chans[4] = {"x", "y", "z", "w"};
1147 for (int c = 0; c < 4; c++) {
1148 printf("%s", chans[BRW_GET_SWZ(inst->src[i].swizzle, c)]);
1159 * Replace each register of type ATTR in this->instructions with a reference
1160 * to a fixed HW register.
1163 vec4_visitor::lower_attributes_to_hw_regs(const int *attribute_map)
1165 foreach_list(node, &this->instructions) {
1166 vec4_instruction *inst = (vec4_instruction *)node;
1168 /* We have to support ATTR as a destination for GL_FIXED fixup. */
1169 if (inst->dst.file == ATTR) {
1170 int grf = attribute_map[inst->dst.reg + inst->dst.reg_offset];
1172 /* All attributes used in the shader need to have been assigned a
1173 * hardware register by the caller
1177 struct brw_reg reg = brw_vec8_grf(grf, 0);
1178 reg.type = inst->dst.type;
1179 reg.dw1.bits.writemask = inst->dst.writemask;
1181 inst->dst.file = HW_REG;
1182 inst->dst.fixed_hw_reg = reg;
1185 for (int i = 0; i < 3; i++) {
1186 if (inst->src[i].file != ATTR)
1189 int grf = attribute_map[inst->src[i].reg + inst->src[i].reg_offset];
1191 /* All attributes used in the shader need to have been assigned a
1192 * hardware register by the caller
1196 struct brw_reg reg = brw_vec8_grf(grf, 0);
1197 reg.dw1.bits.swizzle = inst->src[i].swizzle;
1198 reg.type = inst->src[i].type;
1199 if (inst->src[i].abs)
1201 if (inst->src[i].negate)
1204 inst->src[i].file = HW_REG;
1205 inst->src[i].fixed_hw_reg = reg;
1211 vec4_vs_visitor::setup_attributes(int payload_reg)
1214 int attribute_map[VERT_ATTRIB_MAX + 1];
1215 memset(attribute_map, 0, sizeof(attribute_map));
1218 for (int i = 0; i < VERT_ATTRIB_MAX; i++) {
1219 if (vs_prog_data->inputs_read & BITFIELD64_BIT(i)) {
1220 attribute_map[i] = payload_reg + nr_attributes;
1225 /* VertexID is stored by the VF as the last vertex element, but we
1226 * don't represent it with a flag in inputs_read, so we call it
1229 if (vs_prog_data->uses_vertexid) {
1230 attribute_map[VERT_ATTRIB_MAX] = payload_reg + nr_attributes;
1234 lower_attributes_to_hw_regs(attribute_map);
1236 /* The BSpec says we always have to read at least one thing from
1237 * the VF, and it appears that the hardware wedges otherwise.
1239 if (nr_attributes == 0)
1242 prog_data->urb_read_length = (nr_attributes + 1) / 2;
1244 unsigned vue_entries =
1245 MAX2(nr_attributes, prog_data->vue_map.num_slots);
1247 if (intel->gen == 6)
1248 prog_data->urb_entry_size = ALIGN(vue_entries, 8) / 8;
1250 prog_data->urb_entry_size = ALIGN(vue_entries, 4) / 4;
1252 return payload_reg + nr_attributes;
1256 vec4_visitor::setup_uniforms(int reg)
1258 /* The pre-gen6 VS requires that some push constants get loaded no
1259 * matter what, or the GPU would hang.
1261 if (intel->gen < 6 && this->uniforms == 0) {
1262 this->uniform_vector_size[this->uniforms] = 1;
1264 for (unsigned int i = 0; i < 4; i++) {
1265 unsigned int slot = this->uniforms * 4 + i;
1266 static float zero = 0.0;
1267 prog_data->param[slot] = &zero;
1273 reg += ALIGN(uniforms, 2) / 2;
1276 prog_data->nr_params = this->uniforms * 4;
1278 prog_data->curb_read_length = reg - 1;
1284 vec4_visitor::setup_payload(void)
1288 /* The payload always contains important data in g0, which contains
1289 * the URB handles that are passed on to the URB write at the end
1290 * of the thread. So, we always start push constants at g1.
1294 reg = setup_uniforms(reg);
1296 reg = setup_attributes(reg);
1298 this->first_non_payload_grf = reg;
1302 vec4_visitor::get_timestamp()
1304 assert(intel->gen >= 7);
1306 src_reg ts = src_reg(brw_reg(BRW_ARCHITECTURE_REGISTER_FILE,
1309 BRW_REGISTER_TYPE_UD,
1310 BRW_VERTICAL_STRIDE_0,
1312 BRW_HORIZONTAL_STRIDE_4,
1316 dst_reg dst = dst_reg(this, glsl_type::uvec4_type);
1318 vec4_instruction *mov = emit(MOV(dst, ts));
1319 /* We want to read the 3 fields we care about (mostly field 0, but also 2)
1320 * even if it's not enabled in the dispatch.
1322 mov->force_writemask_all = true;
1324 return src_reg(dst);
1328 vec4_visitor::emit_shader_time_begin()
1330 current_annotation = "shader time start";
1331 shader_start_time = get_timestamp();
1335 vec4_visitor::emit_shader_time_end()
1337 current_annotation = "shader time end";
1338 src_reg shader_end_time = get_timestamp();
1341 /* Check that there weren't any timestamp reset events (assuming these
1342 * were the only two timestamp reads that happened).
1344 src_reg reset_end = shader_end_time;
1345 reset_end.swizzle = BRW_SWIZZLE_ZZZZ;
1346 vec4_instruction *test = emit(AND(dst_null_d(), reset_end, src_reg(1u)));
1347 test->conditional_mod = BRW_CONDITIONAL_Z;
1349 emit(IF(BRW_PREDICATE_NORMAL));
1351 /* Take the current timestamp and get the delta. */
1352 shader_start_time.negate = true;
1353 dst_reg diff = dst_reg(this, glsl_type::uint_type);
1354 emit(ADD(diff, shader_start_time, shader_end_time));
1356 /* If there were no instructions between the two timestamp gets, the diff
1357 * is 2 cycles. Remove that overhead, so I can forget about that when
1358 * trying to determine the time taken for single instructions.
1360 emit(ADD(diff, src_reg(diff), src_reg(-2u)));
1362 emit_shader_time_write(ST_VS, src_reg(diff));
1363 emit_shader_time_write(ST_VS_WRITTEN, src_reg(1u));
1364 emit(BRW_OPCODE_ELSE);
1365 emit_shader_time_write(ST_VS_RESET, src_reg(1u));
1366 emit(BRW_OPCODE_ENDIF);
1370 vec4_visitor::emit_shader_time_write(enum shader_time_shader_type type,
1373 int shader_time_index =
1374 brw_get_shader_time_index(brw, shader_prog, prog, type);
1377 dst_reg(this, glsl_type::get_array_instance(glsl_type::vec4_type, 2));
1379 dst_reg offset = dst;
1383 offset.type = BRW_REGISTER_TYPE_UD;
1384 emit(MOV(offset, src_reg(shader_time_index * SHADER_TIME_STRIDE)));
1386 time.type = BRW_REGISTER_TYPE_UD;
1387 emit(MOV(time, src_reg(value)));
1389 emit(SHADER_OPCODE_SHADER_TIME_ADD, dst_reg(), src_reg(dst));
1395 sanity_param_count = prog->Parameters->NumParameters;
1397 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
1398 emit_shader_time_begin();
1402 /* Generate VS IR for main(). (the visitor only descends into
1403 * functions called "main").
1406 visit_instructions(shader->ir);
1408 emit_program_code();
1412 if (key->userclip_active && !key->uses_clip_distance)
1413 setup_uniform_clipplane_values();
1417 /* Before any optimization, push array accesses out to scratch
1418 * space where we need them to be. This pass may allocate new
1419 * virtual GRFs, so we want to do it early. It also makes sure
1420 * that we have reladdr computations available for CSE, since we'll
1421 * often do repeated subexpressions for those.
1424 move_grf_array_access_to_scratch();
1425 move_uniform_array_access_to_pull_constants();
1427 /* The ARB_vertex_program frontend emits pull constant loads directly
1428 * rather than using reladdr, so we don't need to walk through all the
1429 * instructions looking for things to move. There isn't anything.
1431 * We do still need to split things to vec4 size.
1433 split_uniform_registers();
1435 pack_uniform_registers();
1436 move_push_constants_to_pull_constants();
1437 split_virtual_grfs();
1442 progress = dead_code_eliminate() || progress;
1443 progress = opt_copy_propagation() || progress;
1444 progress = opt_algebraic() || progress;
1445 progress = opt_register_coalesce() || progress;
1455 /* Debug of register spilling: Go spill everything. */
1456 const int grf_count = virtual_grf_count;
1457 float spill_costs[virtual_grf_count];
1458 bool no_spill[virtual_grf_count];
1459 evaluate_spill_costs(spill_costs, no_spill);
1460 for (int i = 0; i < grf_count; i++) {
1467 while (!reg_allocate()) {
1472 opt_schedule_instructions();
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;