Path: blob/21.2-virgl/src/intel/compiler/brw_fs_reg_allocate.cpp
4550 views
/*1* Copyright © 2010 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22* Authors:23* Eric Anholt <[email protected]>24*25*/2627#include "brw_eu.h"28#include "brw_fs.h"29#include "brw_cfg.h"30#include "util/set.h"31#include "util/register_allocate.h"3233using namespace brw;3435static void36assign_reg(unsigned *reg_hw_locations, fs_reg *reg)37{38if (reg->file == VGRF) {39reg->nr = reg_hw_locations[reg->nr] + reg->offset / REG_SIZE;40reg->offset %= REG_SIZE;41}42}4344void45fs_visitor::assign_regs_trivial()46{47unsigned hw_reg_mapping[this->alloc.count + 1];48unsigned i;49int reg_width = dispatch_width / 8;5051/* Note that compressed instructions require alignment to 2 registers. */52hw_reg_mapping[0] = ALIGN(this->first_non_payload_grf, reg_width);53for (i = 1; i <= this->alloc.count; i++) {54hw_reg_mapping[i] = (hw_reg_mapping[i - 1] +55this->alloc.sizes[i - 1]);56}57this->grf_used = hw_reg_mapping[this->alloc.count];5859foreach_block_and_inst(block, fs_inst, inst, cfg) {60assign_reg(hw_reg_mapping, &inst->dst);61for (i = 0; i < inst->sources; i++) {62assign_reg(hw_reg_mapping, &inst->src[i]);63}64}6566if (this->grf_used >= max_grf) {67fail("Ran out of regs on trivial allocator (%d/%d)\n",68this->grf_used, max_grf);69} else {70this->alloc.count = this->grf_used;71}7273}7475/**76* Size of a register from the aligned_bary_class register class.77*/78static unsigned79aligned_bary_size(unsigned dispatch_width)80{81return (dispatch_width == 8 ? 2 : 4);82}8384static void85brw_alloc_reg_set(struct brw_compiler *compiler, int dispatch_width)86{87const struct intel_device_info *devinfo = compiler->devinfo;88int base_reg_count = BRW_MAX_GRF;89const int index = util_logbase2(dispatch_width / 8);9091if (dispatch_width > 8 && devinfo->ver >= 7) {92/* For IVB+, we don't need the PLN hacks or the even-reg alignment in93* SIMD16. Therefore, we can use the exact same register sets for94* SIMD16 as we do for SIMD8 and we don't need to recalculate them.95*/96compiler->fs_reg_sets[index] = compiler->fs_reg_sets[0];97return;98}99100/* The registers used to make up almost all values handled in the compiler101* are a scalar value occupying a single register (or 2 registers in the102* case of SIMD16, which is handled by dividing base_reg_count by 2 and103* multiplying allocated register numbers by 2). Things that were104* aggregates of scalar values at the GLSL level were split to scalar105* values by split_virtual_grfs().106*107* However, texture SEND messages return a series of contiguous registers108* to write into. We currently always ask for 4 registers, but we may109* convert that to use less some day.110*111* Additionally, on gfx5 we need aligned pairs of registers for the PLN112* instruction, and on gfx4 we need 8 contiguous regs for workaround simd16113* texturing.114*/115const int class_count = MAX_VGRF_SIZE;116int class_sizes[MAX_VGRF_SIZE];117for (unsigned i = 0; i < MAX_VGRF_SIZE; i++)118class_sizes[i] = i + 1;119120struct ra_regs *regs = ra_alloc_reg_set(compiler, BRW_MAX_GRF, false);121if (devinfo->ver >= 6)122ra_set_allocate_round_robin(regs);123struct ra_class **classes = ralloc_array(compiler, struct ra_class *, class_count);124struct ra_class *aligned_bary_class = NULL;125126/* Now, make the register classes for each size of contiguous register127* allocation we might need to make.128*/129for (int i = 0; i < class_count; i++) {130classes[i] = ra_alloc_contig_reg_class(regs, class_sizes[i]);131132if (devinfo->ver <= 5 && dispatch_width >= 16) {133/* From the G45 PRM:134*135* In order to reduce the hardware complexity, the following136* rules and restrictions apply to the compressed instruction:137* ...138* * Operand Alignment Rule: With the exceptions listed below, a139* source/destination operand in general should be aligned to140* even 256-bit physical register with a region size equal to141* two 256-bit physical register142*/143for (int reg = 0; reg <= base_reg_count - class_sizes[i]; reg += 2)144ra_class_add_reg(classes[i], reg);145} else {146for (int reg = 0; reg <= base_reg_count - class_sizes[i]; reg++)147ra_class_add_reg(classes[i], reg);148}149}150151/* Add a special class for aligned barycentrics, which we'll put the152* first source of LINTERP on so that we can do PLN on Gen <= 6.153*/154if (devinfo->has_pln && (devinfo->ver == 6 ||155(dispatch_width == 8 && devinfo->ver <= 5))) {156int contig_len = aligned_bary_size(dispatch_width);157aligned_bary_class = ra_alloc_contig_reg_class(regs, contig_len);158159for (int i = 0; i <= base_reg_count - contig_len; i += 2)160ra_class_add_reg(aligned_bary_class, i);161}162163ra_set_finalize(regs, NULL);164165compiler->fs_reg_sets[index].regs = regs;166for (unsigned i = 0; i < ARRAY_SIZE(compiler->fs_reg_sets[index].classes); i++)167compiler->fs_reg_sets[index].classes[i] = NULL;168for (int i = 0; i < class_count; i++)169compiler->fs_reg_sets[index].classes[class_sizes[i] - 1] = classes[i];170compiler->fs_reg_sets[index].aligned_bary_class = aligned_bary_class;171}172173void174brw_fs_alloc_reg_sets(struct brw_compiler *compiler)175{176brw_alloc_reg_set(compiler, 8);177brw_alloc_reg_set(compiler, 16);178brw_alloc_reg_set(compiler, 32);179}180181static int182count_to_loop_end(const bblock_t *block)183{184if (block->end()->opcode == BRW_OPCODE_WHILE)185return block->end_ip;186187int depth = 1;188/* Skip the first block, since we don't want to count the do the calling189* function found.190*/191for (block = block->next();192depth > 0;193block = block->next()) {194if (block->start()->opcode == BRW_OPCODE_DO)195depth++;196if (block->end()->opcode == BRW_OPCODE_WHILE) {197depth--;198if (depth == 0)199return block->end_ip;200}201}202unreachable("not reached");203}204205void fs_visitor::calculate_payload_ranges(int payload_node_count,206int *payload_last_use_ip) const207{208int loop_depth = 0;209int loop_end_ip = 0;210211for (int i = 0; i < payload_node_count; i++)212payload_last_use_ip[i] = -1;213214int ip = 0;215foreach_block_and_inst(block, fs_inst, inst, cfg) {216switch (inst->opcode) {217case BRW_OPCODE_DO:218loop_depth++;219220/* Since payload regs are deffed only at the start of the shader221* execution, any uses of the payload within a loop mean the live222* interval extends to the end of the outermost loop. Find the ip of223* the end now.224*/225if (loop_depth == 1)226loop_end_ip = count_to_loop_end(block);227break;228case BRW_OPCODE_WHILE:229loop_depth--;230break;231default:232break;233}234235int use_ip;236if (loop_depth > 0)237use_ip = loop_end_ip;238else239use_ip = ip;240241/* Note that UNIFORM args have been turned into FIXED_GRF by242* assign_curbe_setup(), and interpolation uses fixed hardware regs from243* the start (see interp_reg()).244*/245for (int i = 0; i < inst->sources; i++) {246if (inst->src[i].file == FIXED_GRF) {247int node_nr = inst->src[i].nr;248if (node_nr >= payload_node_count)249continue;250251for (unsigned j = 0; j < regs_read(inst, i); j++) {252payload_last_use_ip[node_nr + j] = use_ip;253assert(node_nr + j < unsigned(payload_node_count));254}255}256}257258if (inst->dst.file == FIXED_GRF) {259int node_nr = inst->dst.nr;260if (node_nr < payload_node_count) {261for (unsigned j = 0; j < regs_written(inst); j++) {262payload_last_use_ip[node_nr + j] = use_ip;263assert(node_nr + j < unsigned(payload_node_count));264}265}266}267268/* Special case instructions which have extra implied registers used. */269switch (inst->opcode) {270case CS_OPCODE_CS_TERMINATE:271payload_last_use_ip[0] = use_ip;272break;273274default:275if (inst->eot) {276/* We could omit this for the !inst->header_present case, except277* that the simulator apparently incorrectly reads from g0/g1278* instead of sideband. It also really freaks out driver279* developers to see g0 used in unusual places, so just always280* reserve it.281*/282payload_last_use_ip[0] = use_ip;283payload_last_use_ip[1] = use_ip;284}285break;286}287288ip++;289}290}291292class fs_reg_alloc {293public:294fs_reg_alloc(fs_visitor *fs):295fs(fs), devinfo(fs->devinfo), compiler(fs->compiler),296live(fs->live_analysis.require()), g(NULL),297have_spill_costs(false)298{299mem_ctx = ralloc_context(NULL);300301/* Stash the number of instructions so we can sanity check that our302* counts still match liveness.303*/304live_instr_count = fs->cfg->last_block()->end_ip + 1;305306spill_insts = _mesa_pointer_set_create(mem_ctx);307308/* Most of this allocation was written for a reg_width of 1309* (dispatch_width == 8). In extending to SIMD16, the code was310* left in place and it was converted to have the hardware311* registers it's allocating be contiguous physical pairs of regs312* for reg_width == 2.313*/314int reg_width = fs->dispatch_width / 8;315rsi = util_logbase2(reg_width);316payload_node_count = ALIGN(fs->first_non_payload_grf, reg_width);317318/* Get payload IP information */319payload_last_use_ip = ralloc_array(mem_ctx, int, payload_node_count);320321node_count = 0;322first_payload_node = 0;323first_mrf_hack_node = 0;324scratch_header_node = 0;325grf127_send_hack_node = 0;326first_vgrf_node = 0;327last_vgrf_node = 0;328first_spill_node = 0;329330spill_vgrf_ip = NULL;331spill_vgrf_ip_alloc = 0;332spill_node_count = 0;333}334335~fs_reg_alloc()336{337ralloc_free(mem_ctx);338}339340bool assign_regs(bool allow_spilling, bool spill_all);341342private:343void setup_live_interference(unsigned node,344int node_start_ip, int node_end_ip);345void setup_inst_interference(const fs_inst *inst);346347void build_interference_graph(bool allow_spilling);348void discard_interference_graph();349350void emit_unspill(const fs_builder &bld, fs_reg dst,351uint32_t spill_offset, unsigned count);352void emit_spill(const fs_builder &bld, fs_reg src,353uint32_t spill_offset, unsigned count);354355void set_spill_costs();356int choose_spill_reg();357fs_reg alloc_scratch_header();358fs_reg alloc_spill_reg(unsigned size, int ip);359void spill_reg(unsigned spill_reg);360361void *mem_ctx;362fs_visitor *fs;363const intel_device_info *devinfo;364const brw_compiler *compiler;365const fs_live_variables &live;366int live_instr_count;367368set *spill_insts;369370/* Which compiler->fs_reg_sets[] to use */371int rsi;372373ra_graph *g;374bool have_spill_costs;375376int payload_node_count;377int *payload_last_use_ip;378379int node_count;380int first_payload_node;381int first_mrf_hack_node;382int scratch_header_node;383int grf127_send_hack_node;384int first_vgrf_node;385int last_vgrf_node;386int first_spill_node;387388int *spill_vgrf_ip;389int spill_vgrf_ip_alloc;390int spill_node_count;391392fs_reg scratch_header;393};394395/**396* Sets the mrf_used array to indicate which MRFs are used by the shader IR397*398* This is used in assign_regs() to decide which of the GRFs that we use as399* MRFs on gfx7 get normally register allocated, and in register spilling to400* see if we can actually use MRFs to do spills without overwriting normal MRF401* contents.402*/403static void404get_used_mrfs(const fs_visitor *v, bool *mrf_used)405{406int reg_width = v->dispatch_width / 8;407408memset(mrf_used, 0, BRW_MAX_MRF(v->devinfo->ver) * sizeof(bool));409410foreach_block_and_inst(block, fs_inst, inst, v->cfg) {411if (inst->dst.file == MRF) {412int reg = inst->dst.nr & ~BRW_MRF_COMPR4;413mrf_used[reg] = true;414if (reg_width == 2) {415if (inst->dst.nr & BRW_MRF_COMPR4) {416mrf_used[reg + 4] = true;417} else {418mrf_used[reg + 1] = true;419}420}421}422423if (inst->mlen > 0) {424for (unsigned i = 0; i < inst->implied_mrf_writes(); i++) {425mrf_used[inst->base_mrf + i] = true;426}427}428}429}430431namespace {432/**433* Maximum spill block size we expect to encounter in 32B units.434*435* This is somewhat arbitrary and doesn't necessarily limit the maximum436* variable size that can be spilled -- A higher value will allow a437* variable of a given size to be spilled more efficiently with a smaller438* number of scratch messages, but will increase the likelihood of a439* collision between the MRFs reserved for spilling and other MRFs used by440* the program (and possibly increase GRF register pressure on platforms441* without hardware MRFs), what could cause register allocation to fail.442*443* For the moment reserve just enough space so a register of 32 bit444* component type and natural region width can be spilled without splitting445* into multiple (force_writemask_all) scratch messages.446*/447unsigned448spill_max_size(const backend_shader *s)449{450/* FINISHME - On Gfx7+ it should be possible to avoid this limit451* altogether by spilling directly from the temporary GRF452* allocated to hold the result of the instruction (and the453* scratch write header).454*/455/* FINISHME - The shader's dispatch width probably belongs in456* backend_shader (or some nonexistent fs_shader class?)457* rather than in the visitor class.458*/459return static_cast<const fs_visitor *>(s)->dispatch_width / 8;460}461462/**463* First MRF register available for spilling.464*/465unsigned466spill_base_mrf(const backend_shader *s)467{468/* We don't use the MRF hack on Gfx9+ */469assert(s->devinfo->ver < 9);470return BRW_MAX_MRF(s->devinfo->ver) - spill_max_size(s) - 1;471}472}473474void475fs_reg_alloc::setup_live_interference(unsigned node,476int node_start_ip, int node_end_ip)477{478/* Mark any virtual grf that is live between the start of the program and479* the last use of a payload node interfering with that payload node.480*/481for (int i = 0; i < payload_node_count; i++) {482if (payload_last_use_ip[i] == -1)483continue;484485/* Note that we use a <= comparison, unlike vgrfs_interfere(),486* in order to not have to worry about the uniform issue described in487* calculate_live_intervals().488*/489if (node_start_ip <= payload_last_use_ip[i])490ra_add_node_interference(g, node, first_payload_node + i);491}492493/* If we have the MRF hack enabled, mark this node as interfering with all494* MRF registers.495*/496if (first_mrf_hack_node >= 0) {497for (int i = spill_base_mrf(fs); i < BRW_MAX_MRF(devinfo->ver); i++)498ra_add_node_interference(g, node, first_mrf_hack_node + i);499}500501/* Everything interferes with the scratch header */502if (scratch_header_node >= 0)503ra_add_node_interference(g, node, scratch_header_node);504505/* Add interference with every vgrf whose live range intersects this506* node's. We only need to look at nodes below this one as the reflexivity507* of interference will take care of the rest.508*/509for (unsigned n2 = first_vgrf_node;510n2 <= (unsigned)last_vgrf_node && n2 < node; n2++) {511unsigned vgrf = n2 - first_vgrf_node;512if (!(node_end_ip <= live.vgrf_start[vgrf] ||513live.vgrf_end[vgrf] <= node_start_ip))514ra_add_node_interference(g, node, n2);515}516}517518void519fs_reg_alloc::setup_inst_interference(const fs_inst *inst)520{521/* Certain instructions can't safely use the same register for their522* sources and destination. Add interference.523*/524if (inst->dst.file == VGRF && inst->has_source_and_destination_hazard()) {525for (unsigned i = 0; i < inst->sources; i++) {526if (inst->src[i].file == VGRF) {527ra_add_node_interference(g, first_vgrf_node + inst->dst.nr,528first_vgrf_node + inst->src[i].nr);529}530}531}532533/* In 16-wide instructions we have an issue where a compressed534* instruction is actually two instructions executed simultaneously.535* It's actually ok to have the source and destination registers be536* the same. In this case, each instruction over-writes its own537* source and there's no problem. The real problem here is if the538* source and destination registers are off by one. Then you can end539* up in a scenario where the first instruction over-writes the540* source of the second instruction. Since the compiler doesn't know541* about this level of granularity, we simply make the source and542* destination interfere.543*/544if (inst->exec_size >= 16 && inst->dst.file == VGRF) {545for (int i = 0; i < inst->sources; ++i) {546if (inst->src[i].file == VGRF) {547ra_add_node_interference(g, first_vgrf_node + inst->dst.nr,548first_vgrf_node + inst->src[i].nr);549}550}551}552553if (grf127_send_hack_node >= 0) {554/* At Intel Broadwell PRM, vol 07, section "Instruction Set Reference",555* subsection "EUISA Instructions", Send Message (page 990):556*557* "r127 must not be used for return address when there is a src and558* dest overlap in send instruction."559*560* We are avoiding using grf127 as part of the destination of send561* messages adding a node interference to the grf127_send_hack_node.562* This node has a fixed asignment to grf127.563*564* We don't apply it to SIMD16 instructions because previous code avoids565* any register overlap between sources and destination.566*/567if (inst->exec_size < 16 && inst->is_send_from_grf() &&568inst->dst.file == VGRF)569ra_add_node_interference(g, first_vgrf_node + inst->dst.nr,570grf127_send_hack_node);571572/* Spilling instruction are genereated as SEND messages from MRF but as573* Gfx7+ supports sending from GRF the driver will maps assingn these574* MRF registers to a GRF. Implementations reuses the dest of the send575* message as source. So as we will have an overlap for sure, we create576* an interference between destination and grf127.577*/578if ((inst->opcode == SHADER_OPCODE_GFX7_SCRATCH_READ ||579inst->opcode == SHADER_OPCODE_GFX4_SCRATCH_READ) &&580inst->dst.file == VGRF)581ra_add_node_interference(g, first_vgrf_node + inst->dst.nr,582grf127_send_hack_node);583}584585/* From the Skylake PRM Vol. 2a docs for sends:586*587* "It is required that the second block of GRFs does not overlap with588* the first block."589*590* Normally, this is taken care of by fixup_sends_duplicate_payload() but591* in the case where one of the registers is an undefined value, the592* register allocator may decide that they don't interfere even though593* they're used as sources in the same instruction. We also need to add594* interference here.595*/596if (devinfo->ver >= 9) {597if (inst->opcode == SHADER_OPCODE_SEND && inst->ex_mlen > 0 &&598inst->src[2].file == VGRF && inst->src[3].file == VGRF &&599inst->src[2].nr != inst->src[3].nr)600ra_add_node_interference(g, first_vgrf_node + inst->src[2].nr,601first_vgrf_node + inst->src[3].nr);602}603604/* When we do send-from-GRF for FB writes, we need to ensure that the last605* write instruction sends from a high register. This is because the606* vertex fetcher wants to start filling the low payload registers while607* the pixel data port is still working on writing out the memory. If we608* don't do this, we get rendering artifacts.609*610* We could just do "something high". Instead, we just pick the highest611* register that works.612*/613if (inst->eot) {614const int vgrf = inst->opcode == SHADER_OPCODE_SEND ?615inst->src[2].nr : inst->src[0].nr;616int size = fs->alloc.sizes[vgrf];617int reg = BRW_MAX_GRF - size;618619if (first_mrf_hack_node >= 0) {620/* If something happened to spill, we want to push the EOT send621* register early enough in the register file that we don't622* conflict with any used MRF hack registers.623*/624reg -= BRW_MAX_MRF(devinfo->ver) - spill_base_mrf(fs);625} else if (grf127_send_hack_node >= 0) {626/* Avoid r127 which might be unusable if the node was previously627* written by a SIMD8 SEND message with source/destination overlap.628*/629reg--;630}631632ra_set_node_reg(g, first_vgrf_node + vgrf, reg);633}634}635636void637fs_reg_alloc::build_interference_graph(bool allow_spilling)638{639/* Compute the RA node layout */640node_count = 0;641first_payload_node = node_count;642node_count += payload_node_count;643if (devinfo->ver >= 7 && devinfo->ver < 9 && allow_spilling) {644first_mrf_hack_node = node_count;645node_count += BRW_MAX_GRF - GFX7_MRF_HACK_START;646} else {647first_mrf_hack_node = -1;648}649if (devinfo->ver >= 8) {650grf127_send_hack_node = node_count;651node_count ++;652} else {653grf127_send_hack_node = -1;654}655first_vgrf_node = node_count;656node_count += fs->alloc.count;657last_vgrf_node = node_count - 1;658if (devinfo->ver >= 9 && allow_spilling) {659scratch_header_node = node_count++;660} else {661scratch_header_node = -1;662}663first_spill_node = node_count;664665fs->calculate_payload_ranges(payload_node_count,666payload_last_use_ip);667668assert(g == NULL);669g = ra_alloc_interference_graph(compiler->fs_reg_sets[rsi].regs, node_count);670ralloc_steal(mem_ctx, g);671672/* Set up the payload nodes */673for (int i = 0; i < payload_node_count; i++)674ra_set_node_reg(g, first_payload_node + i, i);675676if (first_mrf_hack_node >= 0) {677/* Mark each MRF reg node as being allocated to its physical678* register.679*680* The alternative would be to have per-physical-register classes,681* which would just be silly.682*/683for (int i = 0; i < BRW_MAX_MRF(devinfo->ver); i++) {684ra_set_node_reg(g, first_mrf_hack_node + i,685GFX7_MRF_HACK_START + i);686}687}688689if (grf127_send_hack_node >= 0)690ra_set_node_reg(g, grf127_send_hack_node, 127);691692/* Specify the classes of each virtual register. */693for (unsigned i = 0; i < fs->alloc.count; i++) {694unsigned size = fs->alloc.sizes[i];695696assert(size <= ARRAY_SIZE(compiler->fs_reg_sets[rsi].classes) &&697"Register allocation relies on split_virtual_grfs()");698699ra_set_node_class(g, first_vgrf_node + i,700compiler->fs_reg_sets[rsi].classes[size - 1]);701}702703/* Special case: on pre-Gfx7 hardware that supports PLN, the second operand704* of a PLN instruction needs to be an even-numbered register, so we have a705* special register class aligned_bary_class to handle this case.706*/707if (compiler->fs_reg_sets[rsi].aligned_bary_class) {708foreach_block_and_inst(block, fs_inst, inst, fs->cfg) {709if (inst->opcode == FS_OPCODE_LINTERP && inst->src[0].file == VGRF &&710fs->alloc.sizes[inst->src[0].nr] ==711aligned_bary_size(fs->dispatch_width)) {712ra_set_node_class(g, first_vgrf_node + inst->src[0].nr,713compiler->fs_reg_sets[rsi].aligned_bary_class);714}715}716}717718/* Add interference based on the live range of the register */719for (unsigned i = 0; i < fs->alloc.count; i++) {720setup_live_interference(first_vgrf_node + i,721live.vgrf_start[i],722live.vgrf_end[i]);723}724725/* Add interference based on the instructions in which a register is used.726*/727foreach_block_and_inst(block, fs_inst, inst, fs->cfg)728setup_inst_interference(inst);729}730731void732fs_reg_alloc::discard_interference_graph()733{734ralloc_free(g);735g = NULL;736have_spill_costs = false;737}738739void740fs_reg_alloc::emit_unspill(const fs_builder &bld, fs_reg dst,741uint32_t spill_offset, unsigned count)742{743const intel_device_info *devinfo = bld.shader->devinfo;744const unsigned reg_size = dst.component_size(bld.dispatch_width()) /745REG_SIZE;746assert(count % reg_size == 0);747748for (unsigned i = 0; i < count / reg_size; i++) {749fs_inst *unspill_inst;750if (devinfo->ver >= 9) {751fs_reg header = this->scratch_header;752fs_builder ubld = bld.exec_all().group(1, 0);753assert(spill_offset % 16 == 0);754unspill_inst = ubld.MOV(component(header, 2),755brw_imm_ud(spill_offset / 16));756_mesa_set_add(spill_insts, unspill_inst);757758unsigned bti;759fs_reg ex_desc;760if (devinfo->verx10 >= 125) {761bti = GFX9_BTI_BINDLESS;762ex_desc = component(this->scratch_header, 0);763} else {764bti = GFX8_BTI_STATELESS_NON_COHERENT;765ex_desc = brw_imm_ud(0);766}767768fs_reg srcs[] = { brw_imm_ud(0), ex_desc, header };769unspill_inst = bld.emit(SHADER_OPCODE_SEND, dst,770srcs, ARRAY_SIZE(srcs));771unspill_inst->mlen = 1;772unspill_inst->header_size = 1;773unspill_inst->size_written = reg_size * REG_SIZE;774unspill_inst->send_has_side_effects = false;775unspill_inst->send_is_volatile = true;776unspill_inst->sfid = GFX7_SFID_DATAPORT_DATA_CACHE;777unspill_inst->desc =778brw_dp_desc(devinfo, bti,779BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ,780BRW_DATAPORT_OWORD_BLOCK_DWORDS(reg_size * 8));781} else if (devinfo->ver >= 7 && spill_offset < (1 << 12) * REG_SIZE) {782/* The Gfx7 descriptor-based offset is 12 bits of HWORD units.783* Because the Gfx7-style scratch block read is hardwired to BTI 255,784* on Gfx9+ it would cause the DC to do an IA-coherent read, what785* largely outweighs the slight advantage from not having to provide786* the address as part of the message header, so we're better off787* using plain old oword block reads.788*/789unspill_inst = bld.emit(SHADER_OPCODE_GFX7_SCRATCH_READ, dst);790unspill_inst->offset = spill_offset;791} else {792unspill_inst = bld.emit(SHADER_OPCODE_GFX4_SCRATCH_READ, dst);793unspill_inst->offset = spill_offset;794unspill_inst->base_mrf = spill_base_mrf(bld.shader);795unspill_inst->mlen = 1; /* header contains offset */796}797_mesa_set_add(spill_insts, unspill_inst);798799dst.offset += reg_size * REG_SIZE;800spill_offset += reg_size * REG_SIZE;801}802}803804void805fs_reg_alloc::emit_spill(const fs_builder &bld, fs_reg src,806uint32_t spill_offset, unsigned count)807{808const intel_device_info *devinfo = bld.shader->devinfo;809const unsigned reg_size = src.component_size(bld.dispatch_width()) /810REG_SIZE;811assert(count % reg_size == 0);812813for (unsigned i = 0; i < count / reg_size; i++) {814fs_inst *spill_inst;815if (devinfo->ver >= 9) {816fs_reg header = this->scratch_header;817fs_builder ubld = bld.exec_all().group(1, 0);818assert(spill_offset % 16 == 0);819spill_inst = ubld.MOV(component(header, 2),820brw_imm_ud(spill_offset / 16));821_mesa_set_add(spill_insts, spill_inst);822823unsigned bti;824fs_reg ex_desc;825if (devinfo->verx10 >= 125) {826bti = GFX9_BTI_BINDLESS;827ex_desc = component(this->scratch_header, 0);828} else {829bti = GFX8_BTI_STATELESS_NON_COHERENT;830ex_desc = brw_imm_ud(0);831}832833fs_reg srcs[] = { brw_imm_ud(0), ex_desc, header, src };834spill_inst = bld.emit(SHADER_OPCODE_SEND, bld.null_reg_f(),835srcs, ARRAY_SIZE(srcs));836spill_inst->mlen = 1;837spill_inst->ex_mlen = reg_size;838spill_inst->size_written = 0;839spill_inst->header_size = 1;840spill_inst->send_has_side_effects = true;841spill_inst->send_is_volatile = false;842spill_inst->sfid = GFX7_SFID_DATAPORT_DATA_CACHE;843spill_inst->desc =844brw_dp_desc(devinfo, bti,845GFX6_DATAPORT_WRITE_MESSAGE_OWORD_BLOCK_WRITE,846BRW_DATAPORT_OWORD_BLOCK_DWORDS(reg_size * 8));847} else {848spill_inst = bld.emit(SHADER_OPCODE_GFX4_SCRATCH_WRITE,849bld.null_reg_f(), src);850spill_inst->offset = spill_offset;851spill_inst->mlen = 1 + reg_size; /* header, value */852spill_inst->base_mrf = spill_base_mrf(bld.shader);853}854_mesa_set_add(spill_insts, spill_inst);855856src.offset += reg_size * REG_SIZE;857spill_offset += reg_size * REG_SIZE;858}859}860861void862fs_reg_alloc::set_spill_costs()863{864float block_scale = 1.0;865float spill_costs[fs->alloc.count];866bool no_spill[fs->alloc.count];867868for (unsigned i = 0; i < fs->alloc.count; i++) {869spill_costs[i] = 0.0;870no_spill[i] = false;871}872873/* Calculate costs for spilling nodes. Call it a cost of 1 per874* spill/unspill we'll have to do, and guess that the insides of875* loops run 10 times.876*/877foreach_block_and_inst(block, fs_inst, inst, fs->cfg) {878for (unsigned int i = 0; i < inst->sources; i++) {879if (inst->src[i].file == VGRF)880spill_costs[inst->src[i].nr] += regs_read(inst, i) * block_scale;881}882883if (inst->dst.file == VGRF)884spill_costs[inst->dst.nr] += regs_written(inst) * block_scale;885886/* Don't spill anything we generated while spilling */887if (_mesa_set_search(spill_insts, inst)) {888for (unsigned int i = 0; i < inst->sources; i++) {889if (inst->src[i].file == VGRF)890no_spill[inst->src[i].nr] = true;891}892if (inst->dst.file == VGRF)893no_spill[inst->dst.nr] = true;894}895896switch (inst->opcode) {897898case BRW_OPCODE_DO:899block_scale *= 10;900break;901902case BRW_OPCODE_WHILE:903block_scale /= 10;904break;905906case BRW_OPCODE_IF:907case BRW_OPCODE_IFF:908block_scale *= 0.5;909break;910911case BRW_OPCODE_ENDIF:912block_scale /= 0.5;913break;914915default:916break;917}918}919920for (unsigned i = 0; i < fs->alloc.count; i++) {921/* Do the no_spill check first. Registers that are used as spill922* temporaries may have been allocated after we calculated liveness so923* we shouldn't look their liveness up. Fortunately, they're always924* used in SCRATCH_READ/WRITE instructions so they'll always be flagged925* no_spill.926*/927if (no_spill[i])928continue;929930int live_length = live.vgrf_end[i] - live.vgrf_start[i];931if (live_length <= 0)932continue;933934/* Divide the cost (in number of spills/fills) by the log of the length935* of the live range of the register. This will encourage spill logic936* to spill long-living things before spilling short-lived things where937* spilling is less likely to actually do us any good. We use the log938* of the length because it will fall off very quickly and not cause us939* to spill medium length registers with more uses.940*/941float adjusted_cost = spill_costs[i] / logf(live_length);942ra_set_node_spill_cost(g, first_vgrf_node + i, adjusted_cost);943}944945have_spill_costs = true;946}947948int949fs_reg_alloc::choose_spill_reg()950{951if (!have_spill_costs)952set_spill_costs();953954int node = ra_get_best_spill_node(g);955if (node < 0)956return -1;957958assert(node >= first_vgrf_node);959return node - first_vgrf_node;960}961962fs_reg963fs_reg_alloc::alloc_scratch_header()964{965int vgrf = fs->alloc.allocate(1);966assert(first_vgrf_node + vgrf == scratch_header_node);967ra_set_node_class(g, scratch_header_node,968compiler->fs_reg_sets[rsi].classes[0]);969970setup_live_interference(scratch_header_node, 0, INT_MAX);971972return fs_reg(VGRF, vgrf, BRW_REGISTER_TYPE_UD);973}974975fs_reg976fs_reg_alloc::alloc_spill_reg(unsigned size, int ip)977{978int vgrf = fs->alloc.allocate(size);979int n = ra_add_node(g, compiler->fs_reg_sets[rsi].classes[size - 1]);980assert(n == first_vgrf_node + vgrf);981assert(n == first_spill_node + spill_node_count);982983setup_live_interference(n, ip - 1, ip + 1);984985/* Add interference between this spill node and any other spill nodes for986* the same instruction.987*/988for (int s = 0; s < spill_node_count; s++) {989if (spill_vgrf_ip[s] == ip)990ra_add_node_interference(g, n, first_spill_node + s);991}992993/* Add this spill node to the list for next time */994if (spill_node_count >= spill_vgrf_ip_alloc) {995if (spill_vgrf_ip_alloc == 0)996spill_vgrf_ip_alloc = 16;997else998spill_vgrf_ip_alloc *= 2;999spill_vgrf_ip = reralloc(mem_ctx, spill_vgrf_ip, int,1000spill_vgrf_ip_alloc);1001}1002spill_vgrf_ip[spill_node_count++] = ip;10031004return fs_reg(VGRF, vgrf);1005}10061007void1008fs_reg_alloc::spill_reg(unsigned spill_reg)1009{1010int size = fs->alloc.sizes[spill_reg];1011unsigned int spill_offset = fs->last_scratch;1012assert(ALIGN(spill_offset, 16) == spill_offset); /* oword read/write req. */10131014/* Spills may use MRFs 13-15 in the SIMD16 case. Our texturing is done1015* using up to 11 MRFs starting from either m1 or m2, and fb writes can use1016* up to m13 (gfx6+ simd16: 2 header + 8 color + 2 src0alpha + 2 omask) or1017* m15 (gfx4-5 simd16: 2 header + 8 color + 1 aads + 2 src depth + 2 dst1018* depth), starting from m1. In summary: We may not be able to spill in1019* SIMD16 mode, because we'd stomp the FB writes.1020*/1021if (!fs->spilled_any_registers) {1022if (devinfo->ver >= 9) {1023this->scratch_header = alloc_scratch_header();1024fs_builder ubld = fs->bld.exec_all().group(8, 0).at(1025fs->cfg->first_block(), fs->cfg->first_block()->start());10261027fs_inst *inst;1028if (devinfo->verx10 >= 125) {1029inst = ubld.MOV(this->scratch_header, brw_imm_ud(0));1030_mesa_set_add(spill_insts, inst);1031inst = ubld.group(1, 0).AND(component(this->scratch_header, 0),1032retype(brw_vec1_grf(0, 5),1033BRW_REGISTER_TYPE_UD),1034brw_imm_ud(INTEL_MASK(31, 10)));1035_mesa_set_add(spill_insts, inst);1036} else {1037inst = ubld.emit(SHADER_OPCODE_SCRATCH_HEADER,1038this->scratch_header);1039_mesa_set_add(spill_insts, inst);1040}1041} else {1042bool mrf_used[BRW_MAX_MRF(devinfo->ver)];1043get_used_mrfs(fs, mrf_used);10441045for (int i = spill_base_mrf(fs); i < BRW_MAX_MRF(devinfo->ver); i++) {1046if (mrf_used[i]) {1047fs->fail("Register spilling not supported with m%d used", i);1048return;1049}1050}1051}10521053fs->spilled_any_registers = true;1054}10551056fs->last_scratch += size * REG_SIZE;10571058/* We're about to replace all uses of this register. It no longer1059* conflicts with anything so we can get rid of its interference.1060*/1061ra_set_node_spill_cost(g, first_vgrf_node + spill_reg, 0);1062ra_reset_node_interference(g, first_vgrf_node + spill_reg);10631064/* Generate spill/unspill instructions for the objects being1065* spilled. Right now, we spill or unspill the whole thing to a1066* virtual grf of the same size. For most instructions, though, we1067* could just spill/unspill the GRF being accessed.1068*/1069int ip = 0;1070foreach_block_and_inst (block, fs_inst, inst, fs->cfg) {1071const fs_builder ibld = fs_builder(fs, block, inst);1072exec_node *before = inst->prev;1073exec_node *after = inst->next;10741075for (unsigned int i = 0; i < inst->sources; i++) {1076if (inst->src[i].file == VGRF &&1077inst->src[i].nr == spill_reg) {1078int count = regs_read(inst, i);1079int subset_spill_offset = spill_offset +1080ROUND_DOWN_TO(inst->src[i].offset, REG_SIZE);1081fs_reg unspill_dst = alloc_spill_reg(count, ip);10821083inst->src[i].nr = unspill_dst.nr;1084inst->src[i].offset %= REG_SIZE;10851086/* We read the largest power-of-two divisor of the register count1087* (because only POT scratch read blocks are allowed by the1088* hardware) up to the maximum supported block size.1089*/1090const unsigned width =1091MIN2(32, 1u << (ffs(MAX2(1, count) * 8) - 1));10921093/* Set exec_all() on unspill messages under the (rather1094* pessimistic) assumption that there is no one-to-one1095* correspondence between channels of the spilled variable in1096* scratch space and the scratch read message, which operates on1097* 32 bit channels. It shouldn't hurt in any case because the1098* unspill destination is a block-local temporary.1099*/1100emit_unspill(ibld.exec_all().group(width, 0), unspill_dst,1101subset_spill_offset, count);1102}1103}11041105if (inst->dst.file == VGRF &&1106inst->dst.nr == spill_reg &&1107inst->opcode != SHADER_OPCODE_UNDEF) {1108int subset_spill_offset = spill_offset +1109ROUND_DOWN_TO(inst->dst.offset, REG_SIZE);1110fs_reg spill_src = alloc_spill_reg(regs_written(inst), ip);11111112inst->dst.nr = spill_src.nr;1113inst->dst.offset %= REG_SIZE;11141115/* If we're immediately spilling the register, we should not use1116* destination dependency hints. Doing so will cause the GPU do1117* try to read and write the register at the same time and may1118* hang the GPU.1119*/1120inst->no_dd_clear = false;1121inst->no_dd_check = false;11221123/* Calculate the execution width of the scratch messages (which work1124* in terms of 32 bit components so we have a fixed number of eight1125* channels per spilled register). We attempt to write one1126* exec_size-wide component of the variable at a time without1127* exceeding the maximum number of (fake) MRF registers reserved for1128* spills.1129*/1130const unsigned width = 8 * MIN2(1131DIV_ROUND_UP(inst->dst.component_size(inst->exec_size), REG_SIZE),1132spill_max_size(fs));11331134/* Spills should only write data initialized by the instruction for1135* whichever channels are enabled in the excution mask. If that's1136* not possible we'll have to emit a matching unspill before the1137* instruction and set force_writemask_all on the spill.1138*/1139const bool per_channel =1140inst->dst.is_contiguous() && type_sz(inst->dst.type) == 4 &&1141inst->exec_size == width;11421143/* Builder used to emit the scratch messages. */1144const fs_builder ubld = ibld.exec_all(!per_channel).group(width, 0);11451146/* If our write is going to affect just part of the1147* regs_written(inst), then we need to unspill the destination since1148* we write back out all of the regs_written(). If the original1149* instruction had force_writemask_all set and is not a partial1150* write, there should be no need for the unspill since the1151* instruction will be overwriting the whole destination in any case.1152*/1153if (inst->is_partial_write() ||1154(!inst->force_writemask_all && !per_channel))1155emit_unspill(ubld, spill_src, subset_spill_offset,1156regs_written(inst));11571158emit_spill(ubld.at(block, inst->next), spill_src,1159subset_spill_offset, regs_written(inst));1160}11611162for (fs_inst *inst = (fs_inst *)before->next;1163inst != after; inst = (fs_inst *)inst->next)1164setup_inst_interference(inst);11651166/* We don't advance the ip for scratch read/write instructions1167* because we consider them to have the same ip as instruction we're1168* spilling around for the purposes of interference. Also, we're1169* inserting spill instructions without re-running liveness analysis1170* and we don't want to mess up our IPs.1171*/1172if (!_mesa_set_search(spill_insts, inst))1173ip++;1174}11751176assert(ip == live_instr_count);1177}11781179bool1180fs_reg_alloc::assign_regs(bool allow_spilling, bool spill_all)1181{1182build_interference_graph(fs->spilled_any_registers || spill_all);11831184bool spilled = false;1185while (1) {1186/* Debug of register spilling: Go spill everything. */1187if (unlikely(spill_all)) {1188int reg = choose_spill_reg();1189if (reg != -1) {1190spill_reg(reg);1191continue;1192}1193}11941195if (ra_allocate(g))1196break;11971198if (!allow_spilling)1199return false;12001201/* Failed to allocate registers. Spill a reg, and the caller will1202* loop back into here to try again.1203*/1204int reg = choose_spill_reg();1205if (reg == -1)1206return false;12071208/* If we're going to spill but we've never spilled before, we need to1209* re-build the interference graph with MRFs enabled to allow spilling.1210*/1211if (!fs->spilled_any_registers) {1212discard_interference_graph();1213build_interference_graph(true);1214}12151216spilled = true;12171218spill_reg(reg);1219}12201221if (spilled)1222fs->invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);12231224/* Get the chosen virtual registers for each node, and map virtual1225* regs in the register classes back down to real hardware reg1226* numbers.1227*/1228unsigned hw_reg_mapping[fs->alloc.count];1229fs->grf_used = fs->first_non_payload_grf;1230for (unsigned i = 0; i < fs->alloc.count; i++) {1231int reg = ra_get_node_reg(g, first_vgrf_node + i);12321233hw_reg_mapping[i] = reg;1234fs->grf_used = MAX2(fs->grf_used,1235hw_reg_mapping[i] + fs->alloc.sizes[i]);1236}12371238foreach_block_and_inst(block, fs_inst, inst, fs->cfg) {1239assign_reg(hw_reg_mapping, &inst->dst);1240for (int i = 0; i < inst->sources; i++) {1241assign_reg(hw_reg_mapping, &inst->src[i]);1242}1243}12441245fs->alloc.count = fs->grf_used;12461247return true;1248}12491250bool1251fs_visitor::assign_regs(bool allow_spilling, bool spill_all)1252{1253fs_reg_alloc alloc(this);1254bool success = alloc.assign_regs(allow_spilling, spill_all);1255if (!success && allow_spilling) {1256fail("no register to spill:\n");1257dump_instructions(NULL);1258}1259return success;1260}126112621263