/* * Copyright © 2011 Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ #include "brw_vec4.h" extern "C" { #include "main/macros.h" #include "program/prog_parameter.h" } #define MAX_INSTRUCTION (1 << 30) namespace brw { void vec4_visitor::calculate_live_intervals() { int *def = ralloc_array(mem_ctx, int, virtual_grf_count); int *use = ralloc_array(mem_ctx, int, virtual_grf_count); int loop_depth = 0; int loop_start = 0; if (this->live_intervals_valid) return; for (int i = 0; i < virtual_grf_count; i++) { def[i] = MAX_INSTRUCTION; use[i] = -1; } int ip = 0; foreach_list(node, &this->instructions) { vec4_instruction *inst = (vec4_instruction *)node; if (inst->opcode == BRW_OPCODE_DO) { if (loop_depth++ == 0) loop_start = ip; } else if (inst->opcode == BRW_OPCODE_WHILE) { loop_depth--; if (loop_depth == 0) { /* Patches up the use of vars marked for being live across * the whole loop. */ for (int i = 0; i < virtual_grf_count; i++) { if (use[i] == loop_start) { use[i] = ip; } } } } else { for (unsigned int i = 0; i < 3; i++) { if (inst->src[i].file == GRF) { int reg = inst->src[i].reg; if (!loop_depth) { use[reg] = ip; } else { def[reg] = MIN2(loop_start, def[reg]); use[reg] = loop_start; /* Nobody else is going to go smash our start to * later in the loop now, because def[reg] now * points before the bb header. */ } } } if (inst->dst.file == GRF) { int reg = inst->dst.reg; if (!loop_depth) { def[reg] = MIN2(def[reg], ip); } else { def[reg] = MIN2(def[reg], loop_start); } } } ip++; } ralloc_free(this->virtual_grf_def); ralloc_free(this->virtual_grf_use); this->virtual_grf_def = def; this->virtual_grf_use = use; this->live_intervals_valid = true; } bool vec4_visitor::virtual_grf_interferes(int a, int b) { int start = MAX2(this->virtual_grf_def[a], this->virtual_grf_def[b]); int end = MIN2(this->virtual_grf_use[a], this->virtual_grf_use[b]); /* We can't handle dead register writes here, without iterating * over the whole instruction stream to find every single dead * write to that register to compare to the live interval of the * other register. Just assert that dead_code_eliminate() has been * called. */ assert((this->virtual_grf_use[a] != -1 || this->virtual_grf_def[a] == MAX_INSTRUCTION) && (this->virtual_grf_use[b] != -1 || this->virtual_grf_def[b] == MAX_INSTRUCTION)); return start < end; } /** * Must be called after calculate_live_intervales() to remove unused * writes to registers -- register allocation will fail otherwise * because something deffed but not used won't be considered to * interfere with other regs. */ bool vec4_visitor::dead_code_eliminate() { bool progress = false; int pc = 0; calculate_live_intervals(); foreach_list_safe(node, &this->instructions) { vec4_instruction *inst = (vec4_instruction *)node; if (inst->dst.file == GRF && this->virtual_grf_use[inst->dst.reg] <= pc) { inst->remove(); progress = true; } pc++; } if (progress) live_intervals_valid = false; return progress; } void vec4_visitor::split_uniform_registers() { /* Prior to this, uniforms have been in an array sized according to * the number of vector uniforms present, sparsely filled (so an * aggregate results in reg indices being skipped over). Now we're * going to cut those aggregates up so each .reg index is one * vector. The goal is to make elimination of unused uniform * components easier later. */ foreach_list(node, &this->instructions) { vec4_instruction *inst = (vec4_instruction *)node; for (int i = 0 ; i < 3; i++) { if (inst->src[i].file != UNIFORM) continue; assert(!inst->src[i].reladdr); inst->src[i].reg += inst->src[i].reg_offset; inst->src[i].reg_offset = 0; } } /* Update that everything is now vector-sized. */ for (int i = 0; i < this->uniforms; i++) { this->uniform_size[i] = 1; } } void vec4_visitor::pack_uniform_registers() { bool uniform_used[this->uniforms]; int new_loc[this->uniforms]; int new_chan[this->uniforms]; memset(uniform_used, 0, sizeof(uniform_used)); memset(new_loc, 0, sizeof(new_loc)); memset(new_chan, 0, sizeof(new_chan)); /* Find which uniform vectors are actually used by the program. We * expect unused vector elements when we've moved array access out * to pull constants, and from some GLSL code generators like wine. */ foreach_list(node, &this->instructions) { vec4_instruction *inst = (vec4_instruction *)node; for (int i = 0 ; i < 3; i++) { if (inst->src[i].file != UNIFORM) continue; uniform_used[inst->src[i].reg] = true; } } int new_uniform_count = 0; /* Now, figure out a packing of the live uniform vectors into our * push constants. */ for (int src = 0; src < uniforms; src++) { int size = this->uniform_vector_size[src]; if (!uniform_used[src]) { this->uniform_vector_size[src] = 0; continue; } int dst; /* Find the lowest place we can slot this uniform in. */ for (dst = 0; dst < src; dst++) { if (this->uniform_vector_size[dst] + size <= 4) break; } if (src == dst) { new_loc[src] = dst; new_chan[src] = 0; } else { new_loc[src] = dst; new_chan[src] = this->uniform_vector_size[dst]; /* Move the references to the data */ for (int j = 0; j < size; j++) { c->prog_data.param[dst * 4 + new_chan[src] + j] = c->prog_data.param[src * 4 + j]; } this->uniform_vector_size[dst] += size; this->uniform_vector_size[src] = 0; } new_uniform_count = MAX2(new_uniform_count, dst + 1); } this->uniforms = new_uniform_count; /* Now, update the instructions for our repacked uniforms. */ foreach_list(node, &this->instructions) { vec4_instruction *inst = (vec4_instruction *)node; for (int i = 0 ; i < 3; i++) { int src = inst->src[i].reg; if (inst->src[i].file != UNIFORM) continue; inst->src[i].reg = new_loc[src]; int sx = BRW_GET_SWZ(inst->src[i].swizzle, 0) + new_chan[src]; int sy = BRW_GET_SWZ(inst->src[i].swizzle, 1) + new_chan[src]; int sz = BRW_GET_SWZ(inst->src[i].swizzle, 2) + new_chan[src]; int sw = BRW_GET_SWZ(inst->src[i].swizzle, 3) + new_chan[src]; inst->src[i].swizzle = BRW_SWIZZLE4(sx, sy, sz, sw); } } } /** * Only a limited number of hardware registers may be used for push * constants, so this turns access to the overflowed constants into * pull constants. */ void vec4_visitor::move_push_constants_to_pull_constants() { int pull_constant_loc[this->uniforms]; /* Only allow 32 registers (256 uniform components) as push constants, * which is the limit on gen6. */ int max_uniform_components = 32 * 8; if (this->uniforms * 4 <= max_uniform_components) return; /* Make some sort of choice as to which uniforms get sent to pull * constants. We could potentially do something clever here like * look for the most infrequently used uniform vec4s, but leave * that for later. */ for (int i = 0; i < this->uniforms * 4; i += 4) { pull_constant_loc[i / 4] = -1; if (i >= max_uniform_components) { const float **values = &prog_data->param[i]; /* Try to find an existing copy of this uniform in the pull * constants if it was part of an array access already. */ for (unsigned int j = 0; j < prog_data->nr_pull_params; j += 4) { int matches; for (matches = 0; matches < 4; matches++) { if (prog_data->pull_param[j + matches] != values[matches]) break; } if (matches == 4) { pull_constant_loc[i / 4] = j / 4; break; } } if (pull_constant_loc[i / 4] == -1) { assert(prog_data->nr_pull_params % 4 == 0); pull_constant_loc[i / 4] = prog_data->nr_pull_params / 4; for (int j = 0; j < 4; j++) { prog_data->pull_param[prog_data->nr_pull_params++] = values[j]; } } } } /* Now actually rewrite usage of the things we've moved to pull * constants. */ foreach_list_safe(node, &this->instructions) { vec4_instruction *inst = (vec4_instruction *)node; for (int i = 0 ; i < 3; i++) { if (inst->src[i].file != UNIFORM || pull_constant_loc[inst->src[i].reg] == -1) continue; int uniform = inst->src[i].reg; dst_reg temp = dst_reg(this, glsl_type::vec4_type); emit_pull_constant_load(inst, temp, inst->src[i], pull_constant_loc[uniform]); inst->src[i].file = temp.file; inst->src[i].reg = temp.reg; inst->src[i].reg_offset = temp.reg_offset; inst->src[i].reladdr = NULL; } } /* Repack push constants to remove the now-unused ones. */ pack_uniform_registers(); } } /* namespace brw */