Path: blob/21.2-virgl/src/intel/compiler/brw_fs_generator.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*/2223/** @file brw_fs_generator.cpp24*25* This file supports generating code from the FS LIR to the actual26* native instructions.27*/2829#include "brw_eu.h"30#include "brw_fs.h"31#include "brw_cfg.h"32#include "util/mesa-sha1.h"3334static enum brw_reg_file35brw_file_from_reg(fs_reg *reg)36{37switch (reg->file) {38case ARF:39return BRW_ARCHITECTURE_REGISTER_FILE;40case FIXED_GRF:41case VGRF:42return BRW_GENERAL_REGISTER_FILE;43case MRF:44return BRW_MESSAGE_REGISTER_FILE;45case IMM:46return BRW_IMMEDIATE_VALUE;47case BAD_FILE:48case ATTR:49case UNIFORM:50unreachable("not reached");51}52return BRW_ARCHITECTURE_REGISTER_FILE;53}5455static struct brw_reg56brw_reg_from_fs_reg(const struct intel_device_info *devinfo, fs_inst *inst,57fs_reg *reg, bool compressed)58{59struct brw_reg brw_reg;6061switch (reg->file) {62case MRF:63assert((reg->nr & ~BRW_MRF_COMPR4) < BRW_MAX_MRF(devinfo->ver));64FALLTHROUGH;65case VGRF:66if (reg->stride == 0) {67brw_reg = brw_vec1_reg(brw_file_from_reg(reg), reg->nr, 0);68} else {69/* From the Haswell PRM:70*71* "VertStride must be used to cross GRF register boundaries. This72* rule implies that elements within a 'Width' cannot cross GRF73* boundaries."74*75* The maximum width value that could satisfy this restriction is:76*/77const unsigned reg_width = REG_SIZE / (reg->stride * type_sz(reg->type));7879/* Because the hardware can only split source regions at a whole80* multiple of width during decompression (i.e. vertically), clamp81* the value obtained above to the physical execution size of a82* single decompressed chunk of the instruction:83*/84const unsigned phys_width = compressed ? inst->exec_size / 2 :85inst->exec_size;8687const unsigned max_hw_width = 16;8889/* XXX - The equation above is strictly speaking not correct on90* hardware that supports unbalanced GRF writes -- On Gfx9+91* each decompressed chunk of the instruction may have a92* different execution size when the number of components93* written to each destination GRF is not the same.94*/95if (reg->stride > 4) {96assert(reg != &inst->dst);97assert(reg->stride * type_sz(reg->type) <= REG_SIZE);98brw_reg = brw_vecn_reg(1, brw_file_from_reg(reg), reg->nr, 0);99brw_reg = stride(brw_reg, reg->stride, 1, 0);100} else {101const unsigned width = MIN3(reg_width, phys_width, max_hw_width);102brw_reg = brw_vecn_reg(width, brw_file_from_reg(reg), reg->nr, 0);103brw_reg = stride(brw_reg, width * reg->stride, width, reg->stride);104}105106if (devinfo->verx10 == 70) {107/* From the IvyBridge PRM (EU Changes by Processor Generation, page 13):108* "Each DF (Double Float) operand uses an element size of 4 rather109* than 8 and all regioning parameters are twice what the values110* would be based on the true element size: ExecSize, Width,111* HorzStride, and VertStride. Each DF operand uses a pair of112* channels and all masking and swizzing should be adjusted113* appropriately."114*115* From the IvyBridge PRM (Special Requirements for Handling Double116* Precision Data Types, page 71):117* "In Align1 mode, all regioning parameters like stride, execution118* size, and width must use the syntax of a pair of packed119* floats. The offsets for these data types must be 64-bit120* aligned. The execution size and regioning parameters are in terms121* of floats."122*123* Summarized: when handling DF-typed arguments, ExecSize,124* VertStride, and Width must be doubled.125*126* It applies to BayTrail too.127*/128if (type_sz(reg->type) == 8) {129brw_reg.width++;130if (brw_reg.vstride > 0)131brw_reg.vstride++;132assert(brw_reg.hstride == BRW_HORIZONTAL_STRIDE_1);133}134135/* When converting from DF->F, we set the destination stride to 2136* because each d2f conversion implicitly writes 2 floats, being137* the first one the converted value. IVB/BYT actually writes two138* F components per SIMD channel, and every other component is139* filled with garbage.140*/141if (reg == &inst->dst && get_exec_type_size(inst) == 8 &&142type_sz(inst->dst.type) < 8) {143assert(brw_reg.hstride > BRW_HORIZONTAL_STRIDE_1);144brw_reg.hstride--;145}146}147}148149brw_reg = retype(brw_reg, reg->type);150brw_reg = byte_offset(brw_reg, reg->offset);151brw_reg.abs = reg->abs;152brw_reg.negate = reg->negate;153break;154case ARF:155case FIXED_GRF:156case IMM:157assert(reg->offset == 0);158brw_reg = reg->as_brw_reg();159break;160case BAD_FILE:161/* Probably unused. */162brw_reg = brw_null_reg();163break;164case ATTR:165case UNIFORM:166unreachable("not reached");167}168169/* On HSW+, scalar DF sources can be accessed using the normal <0,1,0>170* region, but on IVB and BYT DF regions must be programmed in terms of171* floats. A <0,2,1> region accomplishes this.172*/173if (devinfo->verx10 == 70 &&174type_sz(reg->type) == 8 &&175brw_reg.vstride == BRW_VERTICAL_STRIDE_0 &&176brw_reg.width == BRW_WIDTH_1 &&177brw_reg.hstride == BRW_HORIZONTAL_STRIDE_0) {178brw_reg.width = BRW_WIDTH_2;179brw_reg.hstride = BRW_HORIZONTAL_STRIDE_1;180}181182return brw_reg;183}184185fs_generator::fs_generator(const struct brw_compiler *compiler, void *log_data,186void *mem_ctx,187struct brw_stage_prog_data *prog_data,188bool runtime_check_aads_emit,189gl_shader_stage stage)190191: compiler(compiler), log_data(log_data),192devinfo(compiler->devinfo),193prog_data(prog_data), dispatch_width(0),194runtime_check_aads_emit(runtime_check_aads_emit), debug_flag(false),195shader_name(NULL), stage(stage), mem_ctx(mem_ctx)196{197p = rzalloc(mem_ctx, struct brw_codegen);198brw_init_codegen(devinfo, p, mem_ctx);199200/* In the FS code generator, we are very careful to ensure that we always201* set the right execution size so we don't need the EU code to "help" us202* by trying to infer it. Sometimes, it infers the wrong thing.203*/204p->automatic_exec_sizes = false;205}206207fs_generator::~fs_generator()208{209}210211class ip_record : public exec_node {212public:213DECLARE_RALLOC_CXX_OPERATORS(ip_record)214215ip_record(int ip)216{217this->ip = ip;218}219220int ip;221};222223bool224fs_generator::patch_halt_jumps()225{226if (this->discard_halt_patches.is_empty())227return false;228229int scale = brw_jump_scale(p->devinfo);230231if (devinfo->ver >= 6) {232/* There is a somewhat strange undocumented requirement of using233* HALT, according to the simulator. If some channel has HALTed to234* a particular UIP, then by the end of the program, every channel235* must have HALTed to that UIP. Furthermore, the tracking is a236* stack, so you can't do the final halt of a UIP after starting237* halting to a new UIP.238*239* Symptoms of not emitting this instruction on actual hardware240* included GPU hangs and sparkly rendering on the piglit discard241* tests.242*/243brw_inst *last_halt = brw_HALT(p);244brw_inst_set_uip(p->devinfo, last_halt, 1 * scale);245brw_inst_set_jip(p->devinfo, last_halt, 1 * scale);246}247248int ip = p->nr_insn;249250foreach_in_list(ip_record, patch_ip, &discard_halt_patches) {251brw_inst *patch = &p->store[patch_ip->ip];252253assert(brw_inst_opcode(p->devinfo, patch) == BRW_OPCODE_HALT);254if (devinfo->ver >= 6) {255/* HALT takes a half-instruction distance from the pre-incremented IP. */256brw_inst_set_uip(p->devinfo, patch, (ip - patch_ip->ip) * scale);257} else {258brw_set_src1(p, patch, brw_imm_d((ip - patch_ip->ip) * scale));259}260}261262this->discard_halt_patches.make_empty();263264if (devinfo->ver < 6) {265/* From the g965 PRM:266*267* "As DMask is not automatically reloaded into AMask upon completion268* of this instruction, software has to manually restore AMask upon269* completion."270*271* DMask lives in the bottom 16 bits of sr0.1.272*/273brw_inst *reset = brw_MOV(p, brw_mask_reg(BRW_AMASK),274retype(brw_sr0_reg(1), BRW_REGISTER_TYPE_UW));275brw_inst_set_exec_size(devinfo, reset, BRW_EXECUTE_1);276brw_inst_set_mask_control(devinfo, reset, BRW_MASK_DISABLE);277brw_inst_set_qtr_control(devinfo, reset, BRW_COMPRESSION_NONE);278brw_inst_set_thread_control(devinfo, reset, BRW_THREAD_SWITCH);279}280281if (devinfo->ver == 4 && !devinfo->is_g4x) {282/* From the g965 PRM:283*284* "[DevBW, DevCL] Erratum: The subfields in mask stack register are285* reset to zero during graphics reset, however, they are not286* initialized at thread dispatch. These subfields will retain the287* values from the previous thread. Software should make sure the288* mask stack is empty (reset to zero) before terminating the thread.289* In case that this is not practical, software may have to reset the290* mask stack at the beginning of each kernel, which will impact the291* performance."292*293* Luckily we can rely on:294*295* "[DevBW, DevCL] This register access restriction is not296* applicable, hardware does ensure execution pipeline coherency,297* when a mask stack register is used as an explicit source and/or298* destination."299*/300brw_push_insn_state(p);301brw_set_default_mask_control(p, BRW_MASK_DISABLE);302brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);303304brw_set_default_exec_size(p, BRW_EXECUTE_2);305brw_MOV(p, vec2(brw_mask_stack_depth_reg(0)), brw_imm_uw(0));306307brw_set_default_exec_size(p, BRW_EXECUTE_16);308/* Reset the if stack. */309brw_MOV(p, retype(brw_mask_stack_reg(0), BRW_REGISTER_TYPE_UW),310brw_imm_uw(0));311312brw_pop_insn_state(p);313}314315return true;316}317318void319fs_generator::generate_send(fs_inst *inst,320struct brw_reg dst,321struct brw_reg desc,322struct brw_reg ex_desc,323struct brw_reg payload,324struct brw_reg payload2)325{326const bool dst_is_null = dst.file == BRW_ARCHITECTURE_REGISTER_FILE &&327dst.nr == BRW_ARF_NULL;328const unsigned rlen = dst_is_null ? 0 : inst->size_written / REG_SIZE;329330uint32_t desc_imm = inst->desc |331brw_message_desc(devinfo, inst->mlen, rlen, inst->header_size);332333uint32_t ex_desc_imm = inst->ex_desc |334brw_message_ex_desc(devinfo, inst->ex_mlen);335336if (ex_desc.file != BRW_IMMEDIATE_VALUE || ex_desc.ud || ex_desc_imm) {337/* If we have any sort of extended descriptor, then we need SENDS. This338* also covers the dual-payload case because ex_mlen goes in ex_desc.339*/340brw_send_indirect_split_message(p, inst->sfid, dst, payload, payload2,341desc, desc_imm, ex_desc, ex_desc_imm,342inst->eot);343if (inst->check_tdr)344brw_inst_set_opcode(p->devinfo, brw_last_inst,345devinfo->ver >= 12 ? BRW_OPCODE_SENDC : BRW_OPCODE_SENDSC);346} else {347brw_send_indirect_message(p, inst->sfid, dst, payload, desc, desc_imm,348inst->eot);349if (inst->check_tdr)350brw_inst_set_opcode(p->devinfo, brw_last_inst, BRW_OPCODE_SENDC);351}352}353354void355fs_generator::fire_fb_write(fs_inst *inst,356struct brw_reg payload,357struct brw_reg implied_header,358GLuint nr)359{360struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);361362if (devinfo->ver < 6) {363brw_push_insn_state(p);364brw_set_default_exec_size(p, BRW_EXECUTE_8);365brw_set_default_mask_control(p, BRW_MASK_DISABLE);366brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);367brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);368brw_MOV(p, offset(retype(payload, BRW_REGISTER_TYPE_UD), 1),369offset(retype(implied_header, BRW_REGISTER_TYPE_UD), 1));370brw_pop_insn_state(p);371}372373uint32_t msg_control = brw_fb_write_msg_control(inst, prog_data);374375/* We assume render targets start at 0, because headerless FB write376* messages set "Render Target Index" to 0. Using a different binding377* table index would make it impossible to use headerless messages.378*/379const uint32_t surf_index = inst->target;380381brw_inst *insn = brw_fb_WRITE(p,382payload,383retype(implied_header, BRW_REGISTER_TYPE_UW),384msg_control,385surf_index,386nr,3870,388inst->eot,389inst->last_rt,390inst->header_size != 0);391392if (devinfo->ver >= 6)393brw_inst_set_rt_slot_group(devinfo, insn, inst->group / 16);394}395396void397fs_generator::generate_fb_write(fs_inst *inst, struct brw_reg payload)398{399if (devinfo->verx10 <= 70) {400brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);401brw_set_default_flag_reg(p, 0, 0);402}403404const struct brw_reg implied_header =405devinfo->ver < 6 ? payload : brw_null_reg();406407if (inst->base_mrf >= 0)408payload = brw_message_reg(inst->base_mrf);409410if (!runtime_check_aads_emit) {411fire_fb_write(inst, payload, implied_header, inst->mlen);412} else {413/* This can only happen in gen < 6 */414assert(devinfo->ver < 6);415416struct brw_reg v1_null_ud = vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD));417418/* Check runtime bit to detect if we have to send AA data or not */419brw_push_insn_state(p);420brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);421brw_set_default_exec_size(p, BRW_EXECUTE_1);422brw_AND(p,423v1_null_ud,424retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_UD),425brw_imm_ud(1<<26));426brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_NZ);427428int jmp = brw_JMPI(p, brw_imm_ud(0), BRW_PREDICATE_NORMAL) - p->store;429brw_pop_insn_state(p);430{431/* Don't send AA data */432fire_fb_write(inst, offset(payload, 1), implied_header, inst->mlen-1);433}434brw_land_fwd_jump(p, jmp);435fire_fb_write(inst, payload, implied_header, inst->mlen);436}437}438439void440fs_generator::generate_fb_read(fs_inst *inst, struct brw_reg dst,441struct brw_reg payload)442{443assert(inst->size_written % REG_SIZE == 0);444struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);445/* We assume that render targets start at binding table index 0. */446const unsigned surf_index = inst->target;447448gfx9_fb_READ(p, dst, payload, surf_index,449inst->header_size, inst->size_written / REG_SIZE,450prog_data->persample_dispatch);451}452453void454fs_generator::generate_mov_indirect(fs_inst *inst,455struct brw_reg dst,456struct brw_reg reg,457struct brw_reg indirect_byte_offset)458{459assert(indirect_byte_offset.type == BRW_REGISTER_TYPE_UD);460assert(indirect_byte_offset.file == BRW_GENERAL_REGISTER_FILE);461assert(!reg.abs && !reg.negate);462assert(reg.type == dst.type);463464unsigned imm_byte_offset = reg.nr * REG_SIZE + reg.subnr;465466if (indirect_byte_offset.file == BRW_IMMEDIATE_VALUE) {467imm_byte_offset += indirect_byte_offset.ud;468469reg.nr = imm_byte_offset / REG_SIZE;470reg.subnr = imm_byte_offset % REG_SIZE;471if (type_sz(reg.type) > 4 && !devinfo->has_64bit_float) {472brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 0),473subscript(reg, BRW_REGISTER_TYPE_D, 0));474brw_set_default_swsb(p, tgl_swsb_null());475brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 1),476subscript(reg, BRW_REGISTER_TYPE_D, 1));477} else {478brw_MOV(p, dst, reg);479}480} else {481/* Prior to Broadwell, there are only 8 address registers. */482assert(inst->exec_size <= 8 || devinfo->ver >= 8);483484/* We use VxH indirect addressing, clobbering a0.0 through a0.7. */485struct brw_reg addr = vec8(brw_address_reg(0));486487/* Whether we can use destination dependency control without running the488* risk of a hang if an instruction gets shot down.489*/490const bool use_dep_ctrl = !inst->predicate &&491inst->exec_size == dispatch_width;492brw_inst *insn;493494/* The destination stride of an instruction (in bytes) must be greater495* than or equal to the size of the rest of the instruction. Since the496* address register is of type UW, we can't use a D-type instruction.497* In order to get around this, re retype to UW and use a stride.498*/499indirect_byte_offset =500retype(spread(indirect_byte_offset, 2), BRW_REGISTER_TYPE_UW);501502/* There are a number of reasons why we don't use the base offset here.503* One reason is that the field is only 9 bits which means we can only504* use it to access the first 16 GRFs. Also, from the Haswell PRM505* section "Register Region Restrictions":506*507* "The lower bits of the AddressImmediate must not overflow to508* change the register address. The lower 5 bits of Address509* Immediate when added to lower 5 bits of address register gives510* the sub-register offset. The upper bits of Address Immediate511* when added to upper bits of address register gives the register512* address. Any overflow from sub-register offset is dropped."513*514* Since the indirect may cause us to cross a register boundary, this515* makes the base offset almost useless. We could try and do something516* clever where we use a actual base offset if base_offset % 32 == 0 but517* that would mean we were generating different code depending on the518* base offset. Instead, for the sake of consistency, we'll just do the519* add ourselves. This restriction is only listed in the Haswell PRM520* but empirical testing indicates that it applies on all older521* generations and is lifted on Broadwell.522*523* In the end, while base_offset is nice to look at in the generated524* code, using it saves us 0 instructions and would require quite a bit525* of case-by-case work. It's just not worth it.526*527* Due to a hardware bug some platforms (particularly Gfx11+) seem to528* require the address components of all channels to be valid whether or529* not they're active, which causes issues if we use VxH addressing530* under non-uniform control-flow. We can easily work around that by531* initializing the whole address register with a pipelined NoMask MOV532* instruction.533*/534if (devinfo->ver >= 7) {535insn = brw_MOV(p, addr, brw_imm_uw(imm_byte_offset));536brw_inst_set_mask_control(devinfo, insn, BRW_MASK_DISABLE);537brw_inst_set_pred_control(devinfo, insn, BRW_PREDICATE_NONE);538if (devinfo->ver >= 12)539brw_set_default_swsb(p, tgl_swsb_null());540else541brw_inst_set_no_dd_clear(devinfo, insn, use_dep_ctrl);542}543544insn = brw_ADD(p, addr, indirect_byte_offset, brw_imm_uw(imm_byte_offset));545if (devinfo->ver >= 12)546brw_set_default_swsb(p, tgl_swsb_regdist(1));547else if (devinfo->ver >= 7)548brw_inst_set_no_dd_check(devinfo, insn, use_dep_ctrl);549550if (type_sz(reg.type) > 4 &&551((devinfo->verx10 == 70) ||552devinfo->is_cherryview || intel_device_info_is_9lp(devinfo) ||553!devinfo->has_64bit_float || devinfo->verx10 >= 125)) {554/* IVB has an issue (which we found empirically) where it reads two555* address register components per channel for indirectly addressed556* 64-bit sources.557*558* From the Cherryview PRM Vol 7. "Register Region Restrictions":559*560* "When source or destination datatype is 64b or operation is561* integer DWord multiply, indirect addressing must not be used."562*563* To work around both of these, we do two integer MOVs insead of one564* 64-bit MOV. Because no double value should ever cross a register565* boundary, it's safe to use the immediate offset in the indirect566* here to handle adding 4 bytes to the offset and avoid the extra567* ADD to the register file.568*/569brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 0),570retype(brw_VxH_indirect(0, 0), BRW_REGISTER_TYPE_D));571brw_set_default_swsb(p, tgl_swsb_null());572brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 1),573retype(brw_VxH_indirect(0, 4), BRW_REGISTER_TYPE_D));574} else {575struct brw_reg ind_src = brw_VxH_indirect(0, 0);576577brw_inst *mov = brw_MOV(p, dst, retype(ind_src, reg.type));578579if (devinfo->ver == 6 && dst.file == BRW_MESSAGE_REGISTER_FILE &&580!inst->get_next()->is_tail_sentinel() &&581((fs_inst *)inst->get_next())->mlen > 0) {582/* From the Sandybridge PRM:583*584* "[Errata: DevSNB(SNB)] If MRF register is updated by any585* instruction that “indexed/indirect” source AND is followed586* by a send, the instruction requires a “Switch”. This is to587* avoid race condition where send may dispatch before MRF is588* updated."589*/590brw_inst_set_thread_control(devinfo, mov, BRW_THREAD_SWITCH);591}592}593}594}595596void597fs_generator::generate_shuffle(fs_inst *inst,598struct brw_reg dst,599struct brw_reg src,600struct brw_reg idx)601{602assert(src.file == BRW_GENERAL_REGISTER_FILE);603assert(!src.abs && !src.negate);604605/* Ivy bridge has some strange behavior that makes this a real pain to606* implement for 64-bit values so we just don't bother.607*/608assert(devinfo->verx10 >= 75 || type_sz(src.type) <= 4);609610/* Because we're using the address register, we're limited to 8-wide611* execution on gfx7. On gfx8, we're limited to 16-wide by the address612* register file and 8-wide for 64-bit types. We could try and make this613* instruction splittable higher up in the compiler but that gets weird614* because it reads all of the channels regardless of execution size. It's615* easier just to split it here.616*/617const unsigned lower_width =618(devinfo->ver <= 7 || type_sz(src.type) > 4) ?6198 : MIN2(16, inst->exec_size);620621brw_set_default_exec_size(p, cvt(lower_width) - 1);622for (unsigned group = 0; group < inst->exec_size; group += lower_width) {623brw_set_default_group(p, group);624625if ((src.vstride == 0 && src.hstride == 0) ||626idx.file == BRW_IMMEDIATE_VALUE) {627/* Trivial, the source is already uniform or the index is a constant.628* We will typically not get here if the optimizer is doing its job,629* but asserting would be mean.630*/631const unsigned i = idx.file == BRW_IMMEDIATE_VALUE ? idx.ud : 0;632struct brw_reg group_src = stride(suboffset(src, i), 0, 1, 0);633struct brw_reg group_dst = suboffset(dst, group);634if (type_sz(src.type) > 4 && !devinfo->has_64bit_float) {635brw_MOV(p, subscript(group_dst, BRW_REGISTER_TYPE_UD, 0),636subscript(group_src, BRW_REGISTER_TYPE_UD, 0));637brw_set_default_swsb(p, tgl_swsb_null());638brw_MOV(p, subscript(group_dst, BRW_REGISTER_TYPE_UD, 1),639subscript(group_src, BRW_REGISTER_TYPE_UD, 1));640} else {641brw_MOV(p, group_dst, group_src);642}643} else {644/* We use VxH indirect addressing, clobbering a0.0 through a0.7. */645struct brw_reg addr = vec8(brw_address_reg(0));646647struct brw_reg group_idx = suboffset(idx, group);648649if (lower_width == 8 && group_idx.width == BRW_WIDTH_16) {650/* Things get grumpy if the register is too wide. */651group_idx.width--;652group_idx.vstride--;653}654655assert(type_sz(group_idx.type) <= 4);656if (type_sz(group_idx.type) == 4) {657/* The destination stride of an instruction (in bytes) must be658* greater than or equal to the size of the rest of the659* instruction. Since the address register is of type UW, we660* can't use a D-type instruction. In order to get around this,661* re retype to UW and use a stride.662*/663group_idx = retype(spread(group_idx, 2), BRW_REGISTER_TYPE_W);664}665666uint32_t src_start_offset = src.nr * REG_SIZE + src.subnr;667668/* From the Haswell PRM:669*670* "When a sequence of NoDDChk and NoDDClr are used, the last671* instruction that completes the scoreboard clear must have a672* non-zero execution mask. This means, if any kind of predication673* can change the execution mask or channel enable of the last674* instruction, the optimization must be avoided. This is to675* avoid instructions being shot down the pipeline when no writes676* are required."677*678* Whenever predication is enabled or the instructions being emitted679* aren't the full width, it's possible that it will be run with zero680* channels enabled so we can't use dependency control without681* running the risk of a hang if an instruction gets shot down.682*/683const bool use_dep_ctrl = !inst->predicate &&684lower_width == dispatch_width;685brw_inst *insn;686687/* Due to a hardware bug some platforms (particularly Gfx11+) seem688* to require the address components of all channels to be valid689* whether or not they're active, which causes issues if we use VxH690* addressing under non-uniform control-flow. We can easily work691* around that by initializing the whole address register with a692* pipelined NoMask MOV instruction.693*/694insn = brw_MOV(p, addr, brw_imm_uw(src_start_offset));695brw_inst_set_mask_control(devinfo, insn, BRW_MASK_DISABLE);696brw_inst_set_pred_control(devinfo, insn, BRW_PREDICATE_NONE);697if (devinfo->ver >= 12)698brw_set_default_swsb(p, tgl_swsb_null());699else700brw_inst_set_no_dd_clear(devinfo, insn, use_dep_ctrl);701702/* Take into account the component size and horizontal stride. */703assert(src.vstride == src.hstride + src.width);704insn = brw_SHL(p, addr, group_idx,705brw_imm_uw(util_logbase2(type_sz(src.type)) +706src.hstride - 1));707if (devinfo->ver >= 12)708brw_set_default_swsb(p, tgl_swsb_regdist(1));709else710brw_inst_set_no_dd_check(devinfo, insn, use_dep_ctrl);711712/* Add on the register start offset */713brw_ADD(p, addr, addr, brw_imm_uw(src_start_offset));714715if (type_sz(src.type) > 4 &&716((devinfo->verx10 == 70) ||717devinfo->is_cherryview || intel_device_info_is_9lp(devinfo) ||718!devinfo->has_64bit_float)) {719/* IVB has an issue (which we found empirically) where it reads720* two address register components per channel for indirectly721* addressed 64-bit sources.722*723* From the Cherryview PRM Vol 7. "Register Region Restrictions":724*725* "When source or destination datatype is 64b or operation is726* integer DWord multiply, indirect addressing must not be727* used."728*729* To work around both of these, we do two integer MOVs insead of730* one 64-bit MOV. Because no double value should ever cross a731* register boundary, it's safe to use the immediate offset in the732* indirect here to handle adding 4 bytes to the offset and avoid733* the extra ADD to the register file.734*/735struct brw_reg gdst = suboffset(dst, group);736struct brw_reg dst_d = retype(spread(gdst, 2),737BRW_REGISTER_TYPE_D);738assert(dst.hstride == 1);739brw_MOV(p, dst_d,740retype(brw_VxH_indirect(0, 0), BRW_REGISTER_TYPE_D));741brw_set_default_swsb(p, tgl_swsb_null());742brw_MOV(p, byte_offset(dst_d, 4),743retype(brw_VxH_indirect(0, 4), BRW_REGISTER_TYPE_D));744} else {745brw_MOV(p, suboffset(dst, group * dst.hstride),746retype(brw_VxH_indirect(0, 0), src.type));747}748}749750brw_set_default_swsb(p, tgl_swsb_null());751}752}753754void755fs_generator::generate_quad_swizzle(const fs_inst *inst,756struct brw_reg dst, struct brw_reg src,757unsigned swiz)758{759/* Requires a quad. */760assert(inst->exec_size >= 4);761762if (src.file == BRW_IMMEDIATE_VALUE ||763has_scalar_region(src)) {764/* The value is uniform across all channels */765brw_MOV(p, dst, src);766767} else if (devinfo->ver < 11 && type_sz(src.type) == 4) {768/* This only works on 8-wide 32-bit values */769assert(inst->exec_size == 8);770assert(src.hstride == BRW_HORIZONTAL_STRIDE_1);771assert(src.vstride == src.width + 1);772brw_set_default_access_mode(p, BRW_ALIGN_16);773struct brw_reg swiz_src = stride(src, 4, 4, 1);774swiz_src.swizzle = swiz;775brw_MOV(p, dst, swiz_src);776777} else {778assert(src.hstride == BRW_HORIZONTAL_STRIDE_1);779assert(src.vstride == src.width + 1);780const struct brw_reg src_0 = suboffset(src, BRW_GET_SWZ(swiz, 0));781782switch (swiz) {783case BRW_SWIZZLE_XXXX:784case BRW_SWIZZLE_YYYY:785case BRW_SWIZZLE_ZZZZ:786case BRW_SWIZZLE_WWWW:787brw_MOV(p, dst, stride(src_0, 4, 4, 0));788break;789790case BRW_SWIZZLE_XXZZ:791case BRW_SWIZZLE_YYWW:792brw_MOV(p, dst, stride(src_0, 2, 2, 0));793break;794795case BRW_SWIZZLE_XYXY:796case BRW_SWIZZLE_ZWZW:797assert(inst->exec_size == 4);798brw_MOV(p, dst, stride(src_0, 0, 2, 1));799break;800801default:802assert(inst->force_writemask_all);803brw_set_default_exec_size(p, cvt(inst->exec_size / 4) - 1);804805for (unsigned c = 0; c < 4; c++) {806brw_inst *insn = brw_MOV(807p, stride(suboffset(dst, c),8084 * inst->dst.stride, 1, 4 * inst->dst.stride),809stride(suboffset(src, BRW_GET_SWZ(swiz, c)), 4, 1, 0));810811if (devinfo->ver < 12) {812brw_inst_set_no_dd_clear(devinfo, insn, c < 3);813brw_inst_set_no_dd_check(devinfo, insn, c > 0);814}815816brw_set_default_swsb(p, tgl_swsb_null());817}818819break;820}821}822}823824void825fs_generator::generate_urb_read(fs_inst *inst,826struct brw_reg dst,827struct brw_reg header)828{829assert(inst->size_written % REG_SIZE == 0);830assert(header.file == BRW_GENERAL_REGISTER_FILE);831assert(header.type == BRW_REGISTER_TYPE_UD);832833brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);834brw_set_dest(p, send, retype(dst, BRW_REGISTER_TYPE_UD));835brw_set_src0(p, send, header);836if (devinfo->ver < 12)837brw_set_src1(p, send, brw_imm_ud(0u));838839brw_inst_set_sfid(p->devinfo, send, BRW_SFID_URB);840brw_inst_set_urb_opcode(p->devinfo, send, GFX8_URB_OPCODE_SIMD8_READ);841842if (inst->opcode == SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT)843brw_inst_set_urb_per_slot_offset(p->devinfo, send, true);844845brw_inst_set_mlen(p->devinfo, send, inst->mlen);846brw_inst_set_rlen(p->devinfo, send, inst->size_written / REG_SIZE);847brw_inst_set_header_present(p->devinfo, send, true);848brw_inst_set_urb_global_offset(p->devinfo, send, inst->offset);849}850851void852fs_generator::generate_urb_write(fs_inst *inst, struct brw_reg payload)853{854brw_inst *insn = brw_next_insn(p, BRW_OPCODE_SEND);855856brw_set_dest(p, insn, brw_null_reg());857brw_set_src0(p, insn, payload);858if (devinfo->ver < 12)859brw_set_src1(p, insn, brw_imm_ud(0u));860861brw_inst_set_sfid(p->devinfo, insn, BRW_SFID_URB);862brw_inst_set_urb_opcode(p->devinfo, insn, GFX8_URB_OPCODE_SIMD8_WRITE);863864if (inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT ||865inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT)866brw_inst_set_urb_per_slot_offset(p->devinfo, insn, true);867868if (inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED ||869inst->opcode == SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT)870brw_inst_set_urb_channel_mask_present(p->devinfo, insn, true);871872brw_inst_set_mlen(p->devinfo, insn, inst->mlen);873brw_inst_set_rlen(p->devinfo, insn, 0);874brw_inst_set_eot(p->devinfo, insn, inst->eot);875brw_inst_set_header_present(p->devinfo, insn, true);876brw_inst_set_urb_global_offset(p->devinfo, insn, inst->offset);877}878879void880fs_generator::generate_cs_terminate(fs_inst *inst, struct brw_reg payload)881{882struct brw_inst *insn;883884insn = brw_next_insn(p, BRW_OPCODE_SEND);885886brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_UW));887brw_set_src0(p, insn, retype(payload, BRW_REGISTER_TYPE_UW));888if (devinfo->ver < 12)889brw_set_src1(p, insn, brw_imm_ud(0u));890891/* For XeHP and newer send a message to the message gateway to terminate a892* compute shader. For older devices, a message is sent to the thread893* spawner.894*/895if (devinfo->verx10 >= 125)896brw_inst_set_sfid(devinfo, insn, BRW_SFID_MESSAGE_GATEWAY);897else898brw_inst_set_sfid(devinfo, insn, BRW_SFID_THREAD_SPAWNER);899brw_inst_set_mlen(devinfo, insn, 1);900brw_inst_set_rlen(devinfo, insn, 0);901brw_inst_set_eot(devinfo, insn, inst->eot);902brw_inst_set_header_present(devinfo, insn, false);903904brw_inst_set_ts_opcode(devinfo, insn, 0); /* Dereference resource */905906if (devinfo->ver < 11) {907brw_inst_set_ts_request_type(devinfo, insn, 0); /* Root thread */908909/* Note that even though the thread has a URB resource associated with it,910* we set the "do not dereference URB" bit, because the URB resource is911* managed by the fixed-function unit, so it will free it automatically.912*/913brw_inst_set_ts_resource_select(devinfo, insn, 1); /* Do not dereference URB */914}915916brw_inst_set_mask_control(devinfo, insn, BRW_MASK_DISABLE);917}918919void920fs_generator::generate_barrier(fs_inst *, struct brw_reg src)921{922brw_barrier(p, src);923if (devinfo->ver >= 12) {924brw_set_default_swsb(p, tgl_swsb_null());925brw_SYNC(p, TGL_SYNC_BAR);926} else {927brw_WAIT(p);928}929}930931bool932fs_generator::generate_linterp(fs_inst *inst,933struct brw_reg dst, struct brw_reg *src)934{935/* PLN reads:936* / in SIMD16 \937* -----------------------------------938* | src1+0 | src1+1 | src1+2 | src1+3 |939* |-----------------------------------|940* |(x0, x1)|(y0, y1)|(x2, x3)|(y2, y3)|941* -----------------------------------942*943* but for the LINE/MAC pair, the LINE reads Xs and the MAC reads Ys:944*945* -----------------------------------946* | src1+0 | src1+1 | src1+2 | src1+3 |947* |-----------------------------------|948* |(x0, x1)|(y0, y1)| | | in SIMD8949* |-----------------------------------|950* |(x0, x1)|(x2, x3)|(y0, y1)|(y2, y3)| in SIMD16951* -----------------------------------952*953* See also: emit_interpolation_setup_gfx4().954*/955struct brw_reg delta_x = src[0];956struct brw_reg delta_y = offset(src[0], inst->exec_size / 8);957struct brw_reg interp = src[1];958brw_inst *i[2];959960/* nir_lower_interpolation() will do the lowering to MAD instructions for961* us on gfx11+962*/963assert(devinfo->ver < 11);964965if (devinfo->has_pln) {966if (devinfo->ver <= 6 && (delta_x.nr & 1) != 0) {967/* From the Sandy Bridge PRM Vol. 4, Pt. 2, Section 8.3.53, "Plane":968*969* "[DevSNB]:<src1> must be even register aligned.970*971* This restriction is lifted on Ivy Bridge.972*973* This means that we need to split PLN into LINE+MAC on-the-fly.974* Unfortunately, the inputs are laid out for PLN and not LINE+MAC so975* we have to split into SIMD8 pieces. For gfx4 (!has_pln), the976* coordinate registers are laid out differently so we leave it as a977* SIMD16 instruction.978*/979assert(inst->exec_size == 8 || inst->exec_size == 16);980assert(inst->group % 16 == 0);981982brw_push_insn_state(p);983brw_set_default_exec_size(p, BRW_EXECUTE_8);984985/* Thanks to two accumulators, we can emit all the LINEs and then all986* the MACs. This improves parallelism a bit.987*/988for (unsigned g = 0; g < inst->exec_size / 8; g++) {989brw_inst *line = brw_LINE(p, brw_null_reg(), interp,990offset(delta_x, g * 2));991brw_inst_set_group(devinfo, line, inst->group + g * 8);992993/* LINE writes the accumulator automatically on gfx4-5. On Sandy994* Bridge and later, we have to explicitly enable it.995*/996if (devinfo->ver >= 6)997brw_inst_set_acc_wr_control(p->devinfo, line, true);998999/* brw_set_default_saturate() is called before emitting1000* instructions, so the saturate bit is set in each instruction,1001* so we need to unset it on the LINE instructions.1002*/1003brw_inst_set_saturate(p->devinfo, line, false);1004}10051006for (unsigned g = 0; g < inst->exec_size / 8; g++) {1007brw_inst *mac = brw_MAC(p, offset(dst, g), suboffset(interp, 1),1008offset(delta_x, g * 2 + 1));1009brw_inst_set_group(devinfo, mac, inst->group + g * 8);1010brw_inst_set_cond_modifier(p->devinfo, mac, inst->conditional_mod);1011}10121013brw_pop_insn_state(p);10141015return true;1016} else {1017brw_PLN(p, dst, interp, delta_x);10181019return false;1020}1021} else {1022i[0] = brw_LINE(p, brw_null_reg(), interp, delta_x);1023i[1] = brw_MAC(p, dst, suboffset(interp, 1), delta_y);10241025brw_inst_set_cond_modifier(p->devinfo, i[1], inst->conditional_mod);10261027/* brw_set_default_saturate() is called before emitting instructions, so1028* the saturate bit is set in each instruction, so we need to unset it on1029* the first instruction.1030*/1031brw_inst_set_saturate(p->devinfo, i[0], false);10321033return true;1034}1035}10361037void1038fs_generator::generate_get_buffer_size(fs_inst *inst,1039struct brw_reg dst,1040struct brw_reg src,1041struct brw_reg surf_index)1042{1043assert(devinfo->ver >= 7);1044assert(surf_index.file == BRW_IMMEDIATE_VALUE);10451046uint32_t simd_mode;1047int rlen = 4;10481049switch (inst->exec_size) {1050case 8:1051simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;1052break;1053case 16:1054simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;1055break;1056default:1057unreachable("Invalid width for texture instruction");1058}10591060if (simd_mode == BRW_SAMPLER_SIMD_MODE_SIMD16) {1061rlen = 8;1062dst = vec16(dst);1063}10641065brw_SAMPLE(p,1066retype(dst, BRW_REGISTER_TYPE_UW),1067inst->base_mrf,1068src,1069surf_index.ud,10700,1071GFX5_SAMPLER_MESSAGE_SAMPLE_RESINFO,1072rlen, /* response length */1073inst->mlen,1074inst->header_size > 0,1075simd_mode,1076BRW_SAMPLER_RETURN_FORMAT_SINT32);1077}10781079void1080fs_generator::generate_tex(fs_inst *inst, struct brw_reg dst,1081struct brw_reg surface_index,1082struct brw_reg sampler_index)1083{1084assert(devinfo->ver < 7);1085assert(inst->size_written % REG_SIZE == 0);1086int msg_type = -1;1087uint32_t simd_mode;1088uint32_t return_format;10891090/* Sampler EOT message of less than the dispatch width would kill the1091* thread prematurely.1092*/1093assert(!inst->eot || inst->exec_size == dispatch_width);10941095switch (dst.type) {1096case BRW_REGISTER_TYPE_D:1097return_format = BRW_SAMPLER_RETURN_FORMAT_SINT32;1098break;1099case BRW_REGISTER_TYPE_UD:1100return_format = BRW_SAMPLER_RETURN_FORMAT_UINT32;1101break;1102default:1103return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;1104break;1105}11061107/* Stomp the resinfo output type to UINT32. On gens 4-5, the output type1108* is set as part of the message descriptor. On gfx4, the PRM seems to1109* allow UINT32 and FLOAT32 (i965 PRM, Vol. 4 Section 4.8.1.1), but on1110* later gens UINT32 is required. Once you hit Sandy Bridge, the bit is1111* gone from the message descriptor entirely and you just get UINT32 all1112* the time regasrdless. Since we can really only do non-UINT32 on gfx4,1113* just stomp it to UINT32 all the time.1114*/1115if (inst->opcode == SHADER_OPCODE_TXS)1116return_format = BRW_SAMPLER_RETURN_FORMAT_UINT32;11171118switch (inst->exec_size) {1119case 8:1120simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;1121break;1122case 16:1123simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;1124break;1125default:1126unreachable("Invalid width for texture instruction");1127}11281129if (devinfo->ver >= 5) {1130switch (inst->opcode) {1131case SHADER_OPCODE_TEX:1132if (inst->shadow_compare) {1133msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE_COMPARE;1134} else {1135msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE;1136}1137break;1138case FS_OPCODE_TXB:1139if (inst->shadow_compare) {1140msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE;1141} else {1142msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE_BIAS;1143}1144break;1145case SHADER_OPCODE_TXL:1146if (inst->shadow_compare) {1147msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE;1148} else {1149msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE_LOD;1150}1151break;1152case SHADER_OPCODE_TXS:1153msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE_RESINFO;1154break;1155case SHADER_OPCODE_TXD:1156assert(!inst->shadow_compare);1157msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE_DERIVS;1158break;1159case SHADER_OPCODE_TXF:1160msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE_LD;1161break;1162case SHADER_OPCODE_TXF_CMS:1163msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE_LD;1164break;1165case SHADER_OPCODE_LOD:1166msg_type = GFX5_SAMPLER_MESSAGE_LOD;1167break;1168case SHADER_OPCODE_TG4:1169assert(devinfo->ver == 6);1170assert(!inst->shadow_compare);1171msg_type = GFX7_SAMPLER_MESSAGE_SAMPLE_GATHER4;1172break;1173case SHADER_OPCODE_SAMPLEINFO:1174msg_type = GFX6_SAMPLER_MESSAGE_SAMPLE_SAMPLEINFO;1175break;1176default:1177unreachable("not reached");1178}1179} else {1180switch (inst->opcode) {1181case SHADER_OPCODE_TEX:1182/* Note that G45 and older determines shadow compare and dispatch width1183* from message length for most messages.1184*/1185if (inst->exec_size == 8) {1186msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE;1187if (inst->shadow_compare) {1188assert(inst->mlen == 6);1189} else {1190assert(inst->mlen <= 4);1191}1192} else {1193if (inst->shadow_compare) {1194msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_COMPARE;1195assert(inst->mlen == 9);1196} else {1197msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE;1198assert(inst->mlen <= 7 && inst->mlen % 2 == 1);1199}1200}1201break;1202case FS_OPCODE_TXB:1203if (inst->shadow_compare) {1204assert(inst->exec_size == 8);1205assert(inst->mlen == 6);1206msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_BIAS_COMPARE;1207} else {1208assert(inst->mlen == 9);1209msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS;1210simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;1211}1212break;1213case SHADER_OPCODE_TXL:1214if (inst->shadow_compare) {1215assert(inst->exec_size == 8);1216assert(inst->mlen == 6);1217msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_LOD_COMPARE;1218} else {1219assert(inst->mlen == 9);1220msg_type = BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_LOD;1221simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;1222}1223break;1224case SHADER_OPCODE_TXD:1225/* There is no sample_d_c message; comparisons are done manually */1226assert(inst->exec_size == 8);1227assert(inst->mlen == 7 || inst->mlen == 10);1228msg_type = BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_GRADIENTS;1229break;1230case SHADER_OPCODE_TXF:1231assert(inst->mlen <= 9 && inst->mlen % 2 == 1);1232msg_type = BRW_SAMPLER_MESSAGE_SIMD16_LD;1233simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;1234break;1235case SHADER_OPCODE_TXS:1236assert(inst->mlen == 3);1237msg_type = BRW_SAMPLER_MESSAGE_SIMD16_RESINFO;1238simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;1239break;1240default:1241unreachable("not reached");1242}1243}1244assert(msg_type != -1);12451246if (simd_mode == BRW_SAMPLER_SIMD_MODE_SIMD16) {1247dst = vec16(dst);1248}12491250assert(sampler_index.type == BRW_REGISTER_TYPE_UD);12511252/* Load the message header if present. If there's a texture offset,1253* we need to set it up explicitly and load the offset bitfield.1254* Otherwise, we can use an implied move from g0 to the first message reg.1255*/1256struct brw_reg src = brw_null_reg();1257if (inst->header_size != 0) {1258if (devinfo->ver < 6 && !inst->offset) {1259/* Set up an implied move from g0 to the MRF. */1260src = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW);1261} else {1262const tgl_swsb swsb = brw_get_default_swsb(p);1263assert(inst->base_mrf != -1);1264struct brw_reg header_reg = brw_message_reg(inst->base_mrf);12651266brw_push_insn_state(p);1267brw_set_default_swsb(p, tgl_swsb_src_dep(swsb));1268brw_set_default_exec_size(p, BRW_EXECUTE_8);1269brw_set_default_mask_control(p, BRW_MASK_DISABLE);1270brw_set_default_compression_control(p, BRW_COMPRESSION_NONE);1271/* Explicitly set up the message header by copying g0 to the MRF. */1272brw_MOV(p, header_reg, brw_vec8_grf(0, 0));1273brw_set_default_swsb(p, tgl_swsb_regdist(1));12741275brw_set_default_exec_size(p, BRW_EXECUTE_1);1276if (inst->offset) {1277/* Set the offset bits in DWord 2. */1278brw_MOV(p, get_element_ud(header_reg, 2),1279brw_imm_ud(inst->offset));1280}12811282brw_pop_insn_state(p);1283brw_set_default_swsb(p, tgl_swsb_dst_dep(swsb, 1));1284}1285}12861287uint32_t base_binding_table_index;1288switch (inst->opcode) {1289case SHADER_OPCODE_TG4:1290base_binding_table_index = prog_data->binding_table.gather_texture_start;1291break;1292default:1293base_binding_table_index = prog_data->binding_table.texture_start;1294break;1295}12961297assert(surface_index.file == BRW_IMMEDIATE_VALUE);1298assert(sampler_index.file == BRW_IMMEDIATE_VALUE);12991300brw_SAMPLE(p,1301retype(dst, BRW_REGISTER_TYPE_UW),1302inst->base_mrf,1303src,1304surface_index.ud + base_binding_table_index,1305sampler_index.ud % 16,1306msg_type,1307inst->size_written / REG_SIZE,1308inst->mlen,1309inst->header_size != 0,1310simd_mode,1311return_format);1312}131313141315/* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input1316* looking like:1317*1318* arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br1319*1320* Ideally, we want to produce:1321*1322* DDX DDY1323* dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)1324* (ss0.tr - ss0.tl) (ss0.tr - ss0.br)1325* (ss0.br - ss0.bl) (ss0.tl - ss0.bl)1326* (ss0.br - ss0.bl) (ss0.tr - ss0.br)1327* (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)1328* (ss1.tr - ss1.tl) (ss1.tr - ss1.br)1329* (ss1.br - ss1.bl) (ss1.tl - ss1.bl)1330* (ss1.br - ss1.bl) (ss1.tr - ss1.br)1331*1332* and add another set of two more subspans if in 16-pixel dispatch mode.1333*1334* For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result1335* for each pair, and vertstride = 2 jumps us 2 elements after processing a1336* pair. But the ideal approximation may impose a huge performance cost on1337* sample_d. On at least Haswell, sample_d instruction does some1338* optimizations if the same LOD is used for all pixels in the subspan.1339*1340* For DDY, we need to use ALIGN16 mode since it's capable of doing the1341* appropriate swizzling.1342*/1343void1344fs_generator::generate_ddx(const fs_inst *inst,1345struct brw_reg dst, struct brw_reg src)1346{1347unsigned vstride, width;13481349if (devinfo->ver >= 8) {1350if (inst->opcode == FS_OPCODE_DDX_FINE) {1351/* produce accurate derivatives */1352vstride = BRW_VERTICAL_STRIDE_2;1353width = BRW_WIDTH_2;1354} else {1355/* replicate the derivative at the top-left pixel to other pixels */1356vstride = BRW_VERTICAL_STRIDE_4;1357width = BRW_WIDTH_4;1358}13591360struct brw_reg src0 = byte_offset(src, type_sz(src.type));;1361struct brw_reg src1 = src;13621363src0.vstride = vstride;1364src0.width = width;1365src0.hstride = BRW_HORIZONTAL_STRIDE_0;1366src1.vstride = vstride;1367src1.width = width;1368src1.hstride = BRW_HORIZONTAL_STRIDE_0;13691370brw_ADD(p, dst, src0, negate(src1));1371} else {1372/* On Haswell and earlier, the region used above appears to not work1373* correctly for compressed instructions. At least on Haswell and1374* Iron Lake, compressed ALIGN16 instructions do work. Since we1375* would have to split to SIMD8 no matter which method we choose, we1376* may as well use ALIGN16 on all platforms gfx7 and earlier.1377*/1378struct brw_reg src0 = stride(src, 4, 4, 1);1379struct brw_reg src1 = stride(src, 4, 4, 1);1380if (inst->opcode == FS_OPCODE_DDX_FINE) {1381src0.swizzle = BRW_SWIZZLE_XXZZ;1382src1.swizzle = BRW_SWIZZLE_YYWW;1383} else {1384src0.swizzle = BRW_SWIZZLE_XXXX;1385src1.swizzle = BRW_SWIZZLE_YYYY;1386}13871388brw_push_insn_state(p);1389brw_set_default_access_mode(p, BRW_ALIGN_16);1390brw_ADD(p, dst, negate(src0), src1);1391brw_pop_insn_state(p);1392}1393}13941395/* The negate_value boolean is used to negate the derivative computation for1396* FBOs, since they place the origin at the upper left instead of the lower1397* left.1398*/1399void1400fs_generator::generate_ddy(const fs_inst *inst,1401struct brw_reg dst, struct brw_reg src)1402{1403const uint32_t type_size = type_sz(src.type);14041405if (inst->opcode == FS_OPCODE_DDY_FINE) {1406/* produce accurate derivatives.1407*1408* From the Broadwell PRM, Volume 7 (3D-Media-GPGPU)1409* "Register Region Restrictions", Section "1. Special Restrictions":1410*1411* "In Align16 mode, the channel selects and channel enables apply to1412* a pair of half-floats, because these parameters are defined for1413* DWord elements ONLY. This is applicable when both source and1414* destination are half-floats."1415*1416* So for half-float operations we use the Gfx11+ Align1 path. CHV1417* inherits its FP16 hardware from SKL, so it is not affected.1418*/1419if (devinfo->ver >= 11 ||1420(devinfo->is_broadwell && src.type == BRW_REGISTER_TYPE_HF)) {1421src = stride(src, 0, 2, 1);14221423brw_push_insn_state(p);1424brw_set_default_exec_size(p, BRW_EXECUTE_4);1425for (uint32_t g = 0; g < inst->exec_size; g += 4) {1426brw_set_default_group(p, inst->group + g);1427brw_ADD(p, byte_offset(dst, g * type_size),1428negate(byte_offset(src, g * type_size)),1429byte_offset(src, (g + 2) * type_size));1430brw_set_default_swsb(p, tgl_swsb_null());1431}1432brw_pop_insn_state(p);1433} else {1434struct brw_reg src0 = stride(src, 4, 4, 1);1435struct brw_reg src1 = stride(src, 4, 4, 1);1436src0.swizzle = BRW_SWIZZLE_XYXY;1437src1.swizzle = BRW_SWIZZLE_ZWZW;14381439brw_push_insn_state(p);1440brw_set_default_access_mode(p, BRW_ALIGN_16);1441brw_ADD(p, dst, negate(src0), src1);1442brw_pop_insn_state(p);1443}1444} else {1445/* replicate the derivative at the top-left pixel to other pixels */1446if (devinfo->ver >= 8) {1447struct brw_reg src0 = byte_offset(stride(src, 4, 4, 0), 0 * type_size);1448struct brw_reg src1 = byte_offset(stride(src, 4, 4, 0), 2 * type_size);14491450brw_ADD(p, dst, negate(src0), src1);1451} else {1452/* On Haswell and earlier, the region used above appears to not work1453* correctly for compressed instructions. At least on Haswell and1454* Iron Lake, compressed ALIGN16 instructions do work. Since we1455* would have to split to SIMD8 no matter which method we choose, we1456* may as well use ALIGN16 on all platforms gfx7 and earlier.1457*/1458struct brw_reg src0 = stride(src, 4, 4, 1);1459struct brw_reg src1 = stride(src, 4, 4, 1);1460src0.swizzle = BRW_SWIZZLE_XXXX;1461src1.swizzle = BRW_SWIZZLE_ZZZZ;14621463brw_push_insn_state(p);1464brw_set_default_access_mode(p, BRW_ALIGN_16);1465brw_ADD(p, dst, negate(src0), src1);1466brw_pop_insn_state(p);1467}1468}1469}14701471void1472fs_generator::generate_halt(fs_inst *)1473{1474/* This HALT will be patched up at FB write time to point UIP at the end of1475* the program, and at brw_uip_jip() JIP will be set to the end of the1476* current block (or the program).1477*/1478this->discard_halt_patches.push_tail(new(mem_ctx) ip_record(p->nr_insn));1479brw_HALT(p);1480}14811482void1483fs_generator::generate_scratch_write(fs_inst *inst, struct brw_reg src)1484{1485/* The 32-wide messages only respect the first 16-wide half of the channel1486* enable signals which are replicated identically for the second group of1487* 16 channels, so we cannot use them unless the write is marked1488* force_writemask_all.1489*/1490const unsigned lower_size = inst->force_writemask_all ? inst->exec_size :1491MIN2(16, inst->exec_size);1492const unsigned block_size = 4 * lower_size / REG_SIZE;1493const tgl_swsb swsb = brw_get_default_swsb(p);1494assert(inst->mlen != 0);14951496brw_push_insn_state(p);1497brw_set_default_exec_size(p, cvt(lower_size) - 1);1498brw_set_default_compression(p, lower_size > 8);14991500for (unsigned i = 0; i < inst->exec_size / lower_size; i++) {1501brw_set_default_group(p, inst->group + lower_size * i);15021503if (i > 0) {1504assert(swsb.mode & TGL_SBID_SET);1505brw_set_default_swsb(p, tgl_swsb_sbid(TGL_SBID_SRC, swsb.sbid));1506} else {1507brw_set_default_swsb(p, tgl_swsb_src_dep(swsb));1508}15091510brw_MOV(p, brw_uvec_mrf(lower_size, inst->base_mrf + 1, 0),1511retype(offset(src, block_size * i), BRW_REGISTER_TYPE_UD));15121513brw_set_default_swsb(p, tgl_swsb_dst_dep(swsb, 1));1514brw_oword_block_write_scratch(p, brw_message_reg(inst->base_mrf),1515block_size,1516inst->offset + block_size * REG_SIZE * i);1517}15181519brw_pop_insn_state(p);1520}15211522void1523fs_generator::generate_scratch_read(fs_inst *inst, struct brw_reg dst)1524{1525assert(inst->exec_size <= 16 || inst->force_writemask_all);1526assert(inst->mlen != 0);15271528brw_oword_block_read_scratch(p, dst, brw_message_reg(inst->base_mrf),1529inst->exec_size / 8, inst->offset);1530}15311532void1533fs_generator::generate_scratch_read_gfx7(fs_inst *inst, struct brw_reg dst)1534{1535assert(inst->exec_size <= 16 || inst->force_writemask_all);15361537gfx7_block_read_scratch(p, dst, inst->exec_size / 8, inst->offset);1538}15391540/* The A32 messages take a buffer base address in header.5:[31:0] (See1541* MH1_A32_PSM for typed messages or MH_A32_GO for byte/dword scattered1542* and OWord block messages in the SKL PRM Vol. 2d for more details.)1543* Unfortunately, there are a number of subtle differences:1544*1545* For the block read/write messages:1546*1547* - We always stomp header.2 to fill in the actual scratch address (in1548* units of OWORDs) so we don't care what's in there.1549*1550* - They rely on per-thread scratch space value in header.3[3:0] to do1551* bounds checking so that needs to be valid. The upper bits of1552* header.3 are ignored, though, so we can copy all of g0.3.1553*1554* - They ignore header.5[9:0] and assumes the address is 1KB aligned.1555*1556*1557* For the byte/dword scattered read/write messages:1558*1559* - We want header.2 to be zero because that gets added to the per-channel1560* offset in the non-header portion of the message.1561*1562* - Contrary to what the docs claim, they don't do any bounds checking so1563* the value of header.3[3:0] doesn't matter.1564*1565* - They consider all of header.5 for the base address and header.5[9:0]1566* are not ignored. This means that we can't copy g0.5 verbatim because1567* g0.5[9:0] contains the FFTID on most platforms. Instead, we have to1568* use an AND to mask off the bottom 10 bits.1569*1570*1571* For block messages, just copying g0 gives a valid header because all the1572* garbage gets ignored except for header.2 which we stomp as part of message1573* setup. For byte/dword scattered messages, we can just zero out the header1574* and copy over the bits we need from g0.5. This opcode, however, tries to1575* satisfy the requirements of both by starting with 0 and filling out the1576* information required by either set of opcodes.1577*/1578void1579fs_generator::generate_scratch_header(fs_inst *inst, struct brw_reg dst)1580{1581assert(inst->exec_size == 8 && inst->force_writemask_all);1582assert(dst.file == BRW_GENERAL_REGISTER_FILE);15831584dst.type = BRW_REGISTER_TYPE_UD;15851586brw_inst *insn = brw_MOV(p, dst, brw_imm_ud(0));1587if (devinfo->ver >= 12)1588brw_set_default_swsb(p, tgl_swsb_null());1589else1590brw_inst_set_no_dd_clear(p->devinfo, insn, true);15911592/* Copy the per-thread scratch space size from g0.3[3:0] */1593brw_set_default_exec_size(p, BRW_EXECUTE_1);1594insn = brw_AND(p, suboffset(dst, 3),1595retype(brw_vec1_grf(0, 3), BRW_REGISTER_TYPE_UD),1596brw_imm_ud(INTEL_MASK(3, 0)));1597if (devinfo->ver < 12) {1598brw_inst_set_no_dd_clear(p->devinfo, insn, true);1599brw_inst_set_no_dd_check(p->devinfo, insn, true);1600}16011602/* Copy the scratch base address from g0.5[31:10] */1603insn = brw_AND(p, suboffset(dst, 5),1604retype(brw_vec1_grf(0, 5), BRW_REGISTER_TYPE_UD),1605brw_imm_ud(INTEL_MASK(31, 10)));1606if (devinfo->ver < 12)1607brw_inst_set_no_dd_check(p->devinfo, insn, true);1608}16091610void1611fs_generator::generate_uniform_pull_constant_load(fs_inst *inst,1612struct brw_reg dst,1613struct brw_reg index,1614struct brw_reg offset)1615{1616assert(type_sz(dst.type) == 4);1617assert(inst->mlen != 0);16181619assert(index.file == BRW_IMMEDIATE_VALUE &&1620index.type == BRW_REGISTER_TYPE_UD);1621uint32_t surf_index = index.ud;16221623assert(offset.file == BRW_IMMEDIATE_VALUE &&1624offset.type == BRW_REGISTER_TYPE_UD);1625uint32_t read_offset = offset.ud;16261627brw_oword_block_read(p, dst, brw_message_reg(inst->base_mrf),1628read_offset, surf_index);1629}16301631void1632fs_generator::generate_uniform_pull_constant_load_gfx7(fs_inst *inst,1633struct brw_reg dst,1634struct brw_reg index,1635struct brw_reg payload)1636{1637assert(index.type == BRW_REGISTER_TYPE_UD);1638assert(payload.file == BRW_GENERAL_REGISTER_FILE);1639assert(type_sz(dst.type) == 4);16401641if (index.file == BRW_IMMEDIATE_VALUE) {1642const uint32_t surf_index = index.ud;16431644brw_push_insn_state(p);1645brw_set_default_mask_control(p, BRW_MASK_DISABLE);1646brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);1647brw_pop_insn_state(p);16481649brw_inst_set_sfid(devinfo, send, GFX6_SFID_DATAPORT_CONSTANT_CACHE);1650brw_set_dest(p, send, retype(dst, BRW_REGISTER_TYPE_UD));1651brw_set_src0(p, send, retype(payload, BRW_REGISTER_TYPE_UD));1652brw_set_desc(p, send,1653brw_message_desc(devinfo, 1, DIV_ROUND_UP(inst->size_written,1654REG_SIZE), true) |1655brw_dp_desc(devinfo, surf_index,1656GFX7_DATAPORT_DC_OWORD_BLOCK_READ,1657BRW_DATAPORT_OWORD_BLOCK_DWORDS(inst->exec_size)));16581659} else {1660const tgl_swsb swsb = brw_get_default_swsb(p);1661struct brw_reg addr = vec1(retype(brw_address_reg(0), BRW_REGISTER_TYPE_UD));16621663brw_push_insn_state(p);1664brw_set_default_mask_control(p, BRW_MASK_DISABLE);16651666/* a0.0 = surf_index & 0xff */1667brw_set_default_swsb(p, tgl_swsb_src_dep(swsb));1668brw_inst *insn_and = brw_next_insn(p, BRW_OPCODE_AND);1669brw_inst_set_exec_size(p->devinfo, insn_and, BRW_EXECUTE_1);1670brw_set_dest(p, insn_and, addr);1671brw_set_src0(p, insn_and, vec1(retype(index, BRW_REGISTER_TYPE_UD)));1672brw_set_src1(p, insn_and, brw_imm_ud(0x0ff));16731674/* dst = send(payload, a0.0 | <descriptor>) */1675brw_set_default_swsb(p, tgl_swsb_dst_dep(swsb, 1));1676brw_send_indirect_message(1677p, GFX6_SFID_DATAPORT_CONSTANT_CACHE,1678retype(dst, BRW_REGISTER_TYPE_UD),1679retype(payload, BRW_REGISTER_TYPE_UD), addr,1680brw_message_desc(devinfo, 1,1681DIV_ROUND_UP(inst->size_written, REG_SIZE), true) |1682brw_dp_desc(devinfo, 0 /* surface */,1683GFX7_DATAPORT_DC_OWORD_BLOCK_READ,1684BRW_DATAPORT_OWORD_BLOCK_DWORDS(inst->exec_size)),1685false /* EOT */);16861687brw_pop_insn_state(p);1688}1689}16901691void1692fs_generator::generate_varying_pull_constant_load_gfx4(fs_inst *inst,1693struct brw_reg dst,1694struct brw_reg index)1695{1696assert(devinfo->ver < 7); /* Should use the gfx7 variant. */1697assert(inst->header_size != 0);1698assert(inst->mlen);16991700assert(index.file == BRW_IMMEDIATE_VALUE &&1701index.type == BRW_REGISTER_TYPE_UD);1702uint32_t surf_index = index.ud;17031704uint32_t simd_mode, rlen, msg_type;1705if (inst->exec_size == 16) {1706simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;1707rlen = 8;1708} else {1709assert(inst->exec_size == 8);1710simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD8;1711rlen = 4;1712}17131714if (devinfo->ver >= 5)1715msg_type = GFX5_SAMPLER_MESSAGE_SAMPLE_LD;1716else {1717/* We always use the SIMD16 message so that we only have to load U, and1718* not V or R.1719*/1720msg_type = BRW_SAMPLER_MESSAGE_SIMD16_LD;1721assert(inst->mlen == 3);1722assert(inst->size_written == 8 * REG_SIZE);1723rlen = 8;1724simd_mode = BRW_SAMPLER_SIMD_MODE_SIMD16;1725}17261727struct brw_reg header = brw_vec8_grf(0, 0);1728gfx6_resolve_implied_move(p, &header, inst->base_mrf);17291730brw_inst *send = brw_next_insn(p, BRW_OPCODE_SEND);1731brw_inst_set_compression(devinfo, send, false);1732brw_inst_set_sfid(devinfo, send, BRW_SFID_SAMPLER);1733brw_set_dest(p, send, retype(dst, BRW_REGISTER_TYPE_UW));1734brw_set_src0(p, send, header);1735if (devinfo->ver < 6)1736brw_inst_set_base_mrf(p->devinfo, send, inst->base_mrf);17371738/* Our surface is set up as floats, regardless of what actual data is1739* stored in it.1740*/1741uint32_t return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;1742brw_set_desc(p, send,1743brw_message_desc(devinfo, inst->mlen, rlen, inst->header_size) |1744brw_sampler_desc(devinfo, surf_index,17450, /* sampler (unused) */1746msg_type, simd_mode, return_format));1747}17481749void1750fs_generator::generate_pixel_interpolator_query(fs_inst *inst,1751struct brw_reg dst,1752struct brw_reg src,1753struct brw_reg msg_data,1754unsigned msg_type)1755{1756const bool has_payload = inst->src[0].file != BAD_FILE;1757assert(msg_data.type == BRW_REGISTER_TYPE_UD);1758assert(inst->size_written % REG_SIZE == 0);17591760struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);17611762brw_pixel_interpolator_query(p,1763retype(dst, BRW_REGISTER_TYPE_UW),1764/* If we don't have a payload, what we send doesn't matter */1765has_payload ? src : brw_vec8_grf(0, 0),1766inst->pi_noperspective,1767prog_data->per_coarse_pixel_dispatch,1768msg_type,1769msg_data,1770has_payload ? 2 * inst->exec_size / 8 : 1,1771inst->size_written / REG_SIZE);1772}17731774/* Sets vstride=1, width=4, hstride=0 of register src1 during1775* the ADD instruction.1776*/1777void1778fs_generator::generate_set_sample_id(fs_inst *inst,1779struct brw_reg dst,1780struct brw_reg src0,1781struct brw_reg src1)1782{1783assert(dst.type == BRW_REGISTER_TYPE_D ||1784dst.type == BRW_REGISTER_TYPE_UD);1785assert(src0.type == BRW_REGISTER_TYPE_D ||1786src0.type == BRW_REGISTER_TYPE_UD);17871788const struct brw_reg reg = stride(src1, 1, 4, 0);1789const unsigned lower_size = MIN2(inst->exec_size,1790devinfo->ver >= 8 ? 16 : 8);17911792for (unsigned i = 0; i < inst->exec_size / lower_size; i++) {1793brw_inst *insn = brw_ADD(p, offset(dst, i * lower_size / 8),1794offset(src0, (src0.vstride == 0 ? 0 : (1 << (src0.vstride - 1)) *1795(i * lower_size / (1 << src0.width))) *1796type_sz(src0.type) / REG_SIZE),1797suboffset(reg, i * lower_size / 4));1798brw_inst_set_exec_size(devinfo, insn, cvt(lower_size) - 1);1799brw_inst_set_group(devinfo, insn, inst->group + lower_size * i);1800brw_inst_set_compression(devinfo, insn, lower_size > 8);1801brw_set_default_swsb(p, tgl_swsb_null());1802}1803}18041805void1806fs_generator::generate_pack_half_2x16_split(fs_inst *,1807struct brw_reg dst,1808struct brw_reg x,1809struct brw_reg y)1810{1811assert(devinfo->ver >= 7);1812assert(dst.type == BRW_REGISTER_TYPE_UD);1813assert(x.type == BRW_REGISTER_TYPE_F);1814assert(y.type == BRW_REGISTER_TYPE_F);18151816/* From the Ivybridge PRM, Vol4, Part3, Section 6.27 f32to16:1817*1818* Because this instruction does not have a 16-bit floating-point type,1819* the destination data type must be Word (W).1820*1821* The destination must be DWord-aligned and specify a horizontal stride1822* (HorzStride) of 2. The 16-bit result is stored in the lower word of1823* each destination channel and the upper word is not modified.1824*/1825struct brw_reg dst_w = spread(retype(dst, BRW_REGISTER_TYPE_W), 2);18261827/* Give each 32-bit channel of dst the form below, where "." means1828* unchanged.1829* 0x....hhhh1830*/1831brw_F32TO16(p, dst_w, y);18321833/* Now the form:1834* 0xhhhh00001835*/1836brw_set_default_swsb(p, tgl_swsb_regdist(1));1837brw_SHL(p, dst, dst, brw_imm_ud(16u));18381839/* And, finally the form of packHalf2x16's output:1840* 0xhhhhllll1841*/1842brw_F32TO16(p, dst_w, x);1843}18441845void1846fs_generator::generate_shader_time_add(fs_inst *,1847struct brw_reg payload,1848struct brw_reg offset,1849struct brw_reg value)1850{1851const tgl_swsb swsb = brw_get_default_swsb(p);18521853assert(devinfo->ver >= 7);1854brw_push_insn_state(p);1855brw_set_default_mask_control(p, true);1856brw_set_default_swsb(p, tgl_swsb_src_dep(swsb));18571858assert(payload.file == BRW_GENERAL_REGISTER_FILE);1859struct brw_reg payload_offset = retype(brw_vec1_grf(payload.nr, 0),1860offset.type);1861struct brw_reg payload_value = retype(brw_vec1_grf(payload.nr + 1, 0),1862value.type);18631864assert(offset.file == BRW_IMMEDIATE_VALUE);1865if (value.file == BRW_GENERAL_REGISTER_FILE) {1866value.width = BRW_WIDTH_1;1867value.hstride = BRW_HORIZONTAL_STRIDE_0;1868value.vstride = BRW_VERTICAL_STRIDE_0;1869} else {1870assert(value.file == BRW_IMMEDIATE_VALUE);1871}18721873/* Trying to deal with setup of the params from the IR is crazy in the FS81874* case, and we don't really care about squeezing every bit of performance1875* out of this path, so we just emit the MOVs from here.1876*/1877brw_MOV(p, payload_offset, offset);1878brw_set_default_swsb(p, tgl_swsb_null());1879brw_MOV(p, payload_value, value);1880brw_set_default_swsb(p, tgl_swsb_dst_dep(swsb, 1));1881brw_shader_time_add(p, payload,1882prog_data->binding_table.shader_time_start);1883brw_pop_insn_state(p);1884}18851886void1887fs_generator::enable_debug(const char *shader_name)1888{1889debug_flag = true;1890this->shader_name = shader_name;1891}18921893int1894fs_generator::generate_code(const cfg_t *cfg, int dispatch_width,1895struct shader_stats shader_stats,1896const brw::performance &perf,1897struct brw_compile_stats *stats)1898{1899/* align to 64 byte boundary. */1900brw_realign(p, 64);19011902this->dispatch_width = dispatch_width;19031904int start_offset = p->next_insn_offset;19051906/* `send_count` explicitly does not include spills or fills, as we'd1907* like to use it as a metric for intentional memory access or other1908* shared function use. Otherwise, subtle changes to scheduling or1909* register allocation could cause it to fluctuate wildly - and that1910* effect is already counted in spill/fill counts.1911*/1912int spill_count = 0, fill_count = 0;1913int loop_count = 0, send_count = 0, nop_count = 0;1914bool is_accum_used = false;19151916struct disasm_info *disasm_info = disasm_initialize(devinfo, cfg);19171918foreach_block_and_inst (block, fs_inst, inst, cfg) {1919if (inst->opcode == SHADER_OPCODE_UNDEF)1920continue;19211922struct brw_reg src[4], dst;1923unsigned int last_insn_offset = p->next_insn_offset;1924bool multiple_instructions_emitted = false;1925tgl_swsb swsb = inst->sched;19261927/* From the Broadwell PRM, Volume 7, "3D-Media-GPGPU", in the1928* "Register Region Restrictions" section: for BDW, SKL:1929*1930* "A POW/FDIV operation must not be followed by an instruction1931* that requires two destination registers."1932*1933* The documentation is often lacking annotations for Atom parts,1934* and empirically this affects CHV as well.1935*/1936if (devinfo->ver >= 8 &&1937devinfo->ver <= 9 &&1938p->nr_insn > 1 &&1939brw_inst_opcode(devinfo, brw_last_inst) == BRW_OPCODE_MATH &&1940brw_inst_math_function(devinfo, brw_last_inst) == BRW_MATH_FUNCTION_POW &&1941inst->dst.component_size(inst->exec_size) > REG_SIZE) {1942brw_NOP(p);1943last_insn_offset = p->next_insn_offset;19441945/* In order to avoid spurious instruction count differences when the1946* instruction schedule changes, keep track of the number of inserted1947* NOPs.1948*/1949nop_count++;1950}19511952/* Wa_14010017096:1953*1954* Clear accumulator register before end of thread.1955*/1956if (inst->eot && is_accum_used && devinfo->ver >= 12) {1957brw_set_default_exec_size(p, BRW_EXECUTE_16);1958brw_set_default_mask_control(p, BRW_MASK_DISABLE);1959brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);1960brw_set_default_swsb(p, tgl_swsb_src_dep(swsb));1961brw_MOV(p, brw_acc_reg(8), brw_imm_f(0.0f));1962last_insn_offset = p->next_insn_offset;1963swsb = tgl_swsb_dst_dep(swsb, 1);1964}19651966if (!is_accum_used && !inst->eot) {1967is_accum_used = inst->writes_accumulator_implicitly(devinfo) ||1968inst->dst.is_accumulator();1969}19701971/* Wa_14013745556:1972*1973* Always use @1 SWSB for EOT.1974*/1975if (inst->eot && devinfo->ver >= 12) {1976if (tgl_swsb_src_dep(swsb).mode) {1977brw_set_default_exec_size(p, BRW_EXECUTE_1);1978brw_set_default_mask_control(p, BRW_MASK_DISABLE);1979brw_set_default_predicate_control(p, BRW_PREDICATE_NONE);1980brw_set_default_swsb(p, tgl_swsb_src_dep(swsb));1981brw_SYNC(p, TGL_SYNC_NOP);1982last_insn_offset = p->next_insn_offset;1983}19841985swsb = tgl_swsb_dst_dep(swsb, 1);1986}19871988if (unlikely(debug_flag))1989disasm_annotate(disasm_info, inst, p->next_insn_offset);19901991/* If the instruction writes to more than one register, it needs to be1992* explicitly marked as compressed on Gen <= 5. On Gen >= 6 the1993* hardware figures out by itself what the right compression mode is,1994* but we still need to know whether the instruction is compressed to1995* set up the source register regions appropriately.1996*1997* XXX - This is wrong for instructions that write a single register but1998* read more than one which should strictly speaking be treated as1999* compressed. For instructions that don't write any registers it2000* relies on the destination being a null register of the correct2001* type and regioning so the instruction is considered compressed2002* or not accordingly.2003*/2004const bool compressed =2005inst->dst.component_size(inst->exec_size) > REG_SIZE;2006brw_set_default_compression(p, compressed);2007brw_set_default_group(p, inst->group);20082009for (unsigned int i = 0; i < inst->sources; i++) {2010src[i] = brw_reg_from_fs_reg(devinfo, inst,2011&inst->src[i], compressed);2012/* The accumulator result appears to get used for the2013* conditional modifier generation. When negating a UD2014* value, there is a 33rd bit generated for the sign in the2015* accumulator value, so now you can't check, for example,2016* equality with a 32-bit value. See piglit fs-op-neg-uvec4.2017*/2018assert(!inst->conditional_mod ||2019inst->src[i].type != BRW_REGISTER_TYPE_UD ||2020!inst->src[i].negate);2021}2022dst = brw_reg_from_fs_reg(devinfo, inst,2023&inst->dst, compressed);20242025brw_set_default_access_mode(p, BRW_ALIGN_1);2026brw_set_default_predicate_control(p, inst->predicate);2027brw_set_default_predicate_inverse(p, inst->predicate_inverse);2028/* On gfx7 and above, hardware automatically adds the group onto the2029* flag subregister number. On Sandy Bridge and older, we have to do it2030* ourselves.2031*/2032const unsigned flag_subreg = inst->flag_subreg +2033(devinfo->ver >= 7 ? 0 : inst->group / 16);2034brw_set_default_flag_reg(p, flag_subreg / 2, flag_subreg % 2);2035brw_set_default_saturate(p, inst->saturate);2036brw_set_default_mask_control(p, inst->force_writemask_all);2037brw_set_default_acc_write_control(p, inst->writes_accumulator);2038brw_set_default_swsb(p, swsb);20392040unsigned exec_size = inst->exec_size;2041if (devinfo->verx10 == 70 &&2042(get_exec_type_size(inst) == 8 || type_sz(inst->dst.type) == 8)) {2043exec_size *= 2;2044}20452046brw_set_default_exec_size(p, cvt(exec_size) - 1);20472048assert(inst->force_writemask_all || inst->exec_size >= 4);2049assert(inst->force_writemask_all || inst->group % inst->exec_size == 0);2050assert(inst->base_mrf + inst->mlen <= BRW_MAX_MRF(devinfo->ver));2051assert(inst->mlen <= BRW_MAX_MSG_LENGTH);20522053switch (inst->opcode) {2054case BRW_OPCODE_SYNC:2055assert(src[0].file == BRW_IMMEDIATE_VALUE);2056brw_SYNC(p, tgl_sync_function(src[0].ud));2057break;2058case BRW_OPCODE_MOV:2059brw_MOV(p, dst, src[0]);2060break;2061case BRW_OPCODE_ADD:2062brw_ADD(p, dst, src[0], src[1]);2063break;2064case BRW_OPCODE_MUL:2065brw_MUL(p, dst, src[0], src[1]);2066break;2067case BRW_OPCODE_AVG:2068brw_AVG(p, dst, src[0], src[1]);2069break;2070case BRW_OPCODE_MACH:2071brw_MACH(p, dst, src[0], src[1]);2072break;20732074case BRW_OPCODE_LINE:2075brw_LINE(p, dst, src[0], src[1]);2076break;20772078case BRW_OPCODE_MAD:2079assert(devinfo->ver >= 6);2080if (devinfo->ver < 10)2081brw_set_default_access_mode(p, BRW_ALIGN_16);2082brw_MAD(p, dst, src[0], src[1], src[2]);2083break;20842085case BRW_OPCODE_LRP:2086assert(devinfo->ver >= 6 && devinfo->ver <= 10);2087if (devinfo->ver < 10)2088brw_set_default_access_mode(p, BRW_ALIGN_16);2089brw_LRP(p, dst, src[0], src[1], src[2]);2090break;20912092case BRW_OPCODE_FRC:2093brw_FRC(p, dst, src[0]);2094break;2095case BRW_OPCODE_RNDD:2096brw_RNDD(p, dst, src[0]);2097break;2098case BRW_OPCODE_RNDE:2099brw_RNDE(p, dst, src[0]);2100break;2101case BRW_OPCODE_RNDZ:2102brw_RNDZ(p, dst, src[0]);2103break;21042105case BRW_OPCODE_AND:2106brw_AND(p, dst, src[0], src[1]);2107break;2108case BRW_OPCODE_OR:2109brw_OR(p, dst, src[0], src[1]);2110break;2111case BRW_OPCODE_XOR:2112brw_XOR(p, dst, src[0], src[1]);2113break;2114case BRW_OPCODE_NOT:2115brw_NOT(p, dst, src[0]);2116break;2117case BRW_OPCODE_ASR:2118brw_ASR(p, dst, src[0], src[1]);2119break;2120case BRW_OPCODE_SHR:2121brw_SHR(p, dst, src[0], src[1]);2122break;2123case BRW_OPCODE_SHL:2124brw_SHL(p, dst, src[0], src[1]);2125break;2126case BRW_OPCODE_ROL:2127assert(devinfo->ver >= 11);2128assert(src[0].type == dst.type);2129brw_ROL(p, dst, src[0], src[1]);2130break;2131case BRW_OPCODE_ROR:2132assert(devinfo->ver >= 11);2133assert(src[0].type == dst.type);2134brw_ROR(p, dst, src[0], src[1]);2135break;2136case BRW_OPCODE_F32TO16:2137assert(devinfo->ver >= 7);2138brw_F32TO16(p, dst, src[0]);2139break;2140case BRW_OPCODE_F16TO32:2141assert(devinfo->ver >= 7);2142brw_F16TO32(p, dst, src[0]);2143break;2144case BRW_OPCODE_CMP:2145if (inst->exec_size >= 16 && devinfo->verx10 == 70 &&2146dst.file == BRW_ARCHITECTURE_REGISTER_FILE) {2147/* For unknown reasons the WaCMPInstFlagDepClearedEarly workaround2148* implemented in the compiler is not sufficient. Overriding the2149* type when the destination is the null register is necessary but2150* not sufficient by itself.2151*/2152dst.type = BRW_REGISTER_TYPE_D;2153}2154brw_CMP(p, dst, inst->conditional_mod, src[0], src[1]);2155break;2156case BRW_OPCODE_CMPN:2157if (inst->exec_size >= 16 && devinfo->verx10 == 70 &&2158dst.file == BRW_ARCHITECTURE_REGISTER_FILE) {2159/* For unknown reasons the WaCMPInstFlagDepClearedEarly workaround2160* implemented in the compiler is not sufficient. Overriding the2161* type when the destination is the null register is necessary but2162* not sufficient by itself.2163*/2164dst.type = BRW_REGISTER_TYPE_D;2165}2166brw_CMPN(p, dst, inst->conditional_mod, src[0], src[1]);2167break;2168case BRW_OPCODE_SEL:2169brw_SEL(p, dst, src[0], src[1]);2170break;2171case BRW_OPCODE_CSEL:2172assert(devinfo->ver >= 8);2173if (devinfo->ver < 10)2174brw_set_default_access_mode(p, BRW_ALIGN_16);2175brw_CSEL(p, dst, src[0], src[1], src[2]);2176break;2177case BRW_OPCODE_BFREV:2178assert(devinfo->ver >= 7);2179brw_BFREV(p, retype(dst, BRW_REGISTER_TYPE_UD),2180retype(src[0], BRW_REGISTER_TYPE_UD));2181break;2182case BRW_OPCODE_FBH:2183assert(devinfo->ver >= 7);2184brw_FBH(p, retype(dst, src[0].type), src[0]);2185break;2186case BRW_OPCODE_FBL:2187assert(devinfo->ver >= 7);2188brw_FBL(p, retype(dst, BRW_REGISTER_TYPE_UD),2189retype(src[0], BRW_REGISTER_TYPE_UD));2190break;2191case BRW_OPCODE_LZD:2192brw_LZD(p, dst, src[0]);2193break;2194case BRW_OPCODE_CBIT:2195assert(devinfo->ver >= 7);2196brw_CBIT(p, retype(dst, BRW_REGISTER_TYPE_UD),2197retype(src[0], BRW_REGISTER_TYPE_UD));2198break;2199case BRW_OPCODE_ADDC:2200assert(devinfo->ver >= 7);2201brw_ADDC(p, dst, src[0], src[1]);2202break;2203case BRW_OPCODE_SUBB:2204assert(devinfo->ver >= 7);2205brw_SUBB(p, dst, src[0], src[1]);2206break;2207case BRW_OPCODE_MAC:2208brw_MAC(p, dst, src[0], src[1]);2209break;22102211case BRW_OPCODE_BFE:2212assert(devinfo->ver >= 7);2213if (devinfo->ver < 10)2214brw_set_default_access_mode(p, BRW_ALIGN_16);2215brw_BFE(p, dst, src[0], src[1], src[2]);2216break;22172218case BRW_OPCODE_BFI1:2219assert(devinfo->ver >= 7);2220brw_BFI1(p, dst, src[0], src[1]);2221break;2222case BRW_OPCODE_BFI2:2223assert(devinfo->ver >= 7);2224if (devinfo->ver < 10)2225brw_set_default_access_mode(p, BRW_ALIGN_16);2226brw_BFI2(p, dst, src[0], src[1], src[2]);2227break;22282229case BRW_OPCODE_IF:2230if (inst->src[0].file != BAD_FILE) {2231/* The instruction has an embedded compare (only allowed on gfx6) */2232assert(devinfo->ver == 6);2233gfx6_IF(p, inst->conditional_mod, src[0], src[1]);2234} else {2235brw_IF(p, brw_get_default_exec_size(p));2236}2237break;22382239case BRW_OPCODE_ELSE:2240brw_ELSE(p);2241break;2242case BRW_OPCODE_ENDIF:2243brw_ENDIF(p);2244break;22452246case BRW_OPCODE_DO:2247brw_DO(p, brw_get_default_exec_size(p));2248break;22492250case BRW_OPCODE_BREAK:2251brw_BREAK(p);2252break;2253case BRW_OPCODE_CONTINUE:2254brw_CONT(p);2255break;22562257case BRW_OPCODE_WHILE:2258brw_WHILE(p);2259loop_count++;2260break;22612262case SHADER_OPCODE_RCP:2263case SHADER_OPCODE_RSQ:2264case SHADER_OPCODE_SQRT:2265case SHADER_OPCODE_EXP2:2266case SHADER_OPCODE_LOG2:2267case SHADER_OPCODE_SIN:2268case SHADER_OPCODE_COS:2269assert(inst->conditional_mod == BRW_CONDITIONAL_NONE);2270if (devinfo->ver >= 6) {2271assert(inst->mlen == 0);2272assert(devinfo->ver >= 7 || inst->exec_size == 8);2273gfx6_math(p, dst, brw_math_function(inst->opcode),2274src[0], brw_null_reg());2275} else {2276assert(inst->mlen >= 1);2277assert(devinfo->ver == 5 || devinfo->is_g4x || inst->exec_size == 8);2278gfx4_math(p, dst,2279brw_math_function(inst->opcode),2280inst->base_mrf, src[0],2281BRW_MATH_PRECISION_FULL);2282send_count++;2283}2284break;2285case SHADER_OPCODE_INT_QUOTIENT:2286case SHADER_OPCODE_INT_REMAINDER:2287case SHADER_OPCODE_POW:2288assert(devinfo->verx10 < 125);2289assert(inst->conditional_mod == BRW_CONDITIONAL_NONE);2290if (devinfo->ver >= 6) {2291assert(inst->mlen == 0);2292assert((devinfo->ver >= 7 && inst->opcode == SHADER_OPCODE_POW) ||2293inst->exec_size == 8);2294gfx6_math(p, dst, brw_math_function(inst->opcode), src[0], src[1]);2295} else {2296assert(inst->mlen >= 1);2297assert(inst->exec_size == 8);2298gfx4_math(p, dst, brw_math_function(inst->opcode),2299inst->base_mrf, src[0],2300BRW_MATH_PRECISION_FULL);2301send_count++;2302}2303break;2304case FS_OPCODE_LINTERP:2305multiple_instructions_emitted = generate_linterp(inst, dst, src);2306break;2307case FS_OPCODE_PIXEL_X:2308assert(src[0].type == BRW_REGISTER_TYPE_UW);2309assert(src[1].type == BRW_REGISTER_TYPE_UW);2310src[0].subnr = 0 * type_sz(src[0].type);2311if (src[1].file == BRW_IMMEDIATE_VALUE) {2312assert(src[1].ud == 0);2313brw_MOV(p, dst, stride(src[0], 8, 4, 1));2314} else {2315/* Coarse pixel case */2316brw_ADD(p, dst, stride(src[0], 8, 4, 1), src[1]);2317}2318break;2319case FS_OPCODE_PIXEL_Y:2320assert(src[0].type == BRW_REGISTER_TYPE_UW);2321assert(src[1].type == BRW_REGISTER_TYPE_UW);2322src[0].subnr = 4 * type_sz(src[0].type);2323if (src[1].file == BRW_IMMEDIATE_VALUE) {2324assert(src[1].ud == 0);2325brw_MOV(p, dst, stride(src[0], 8, 4, 1));2326} else {2327/* Coarse pixel case */2328brw_ADD(p, dst, stride(src[0], 8, 4, 1), src[1]);2329}2330break;23312332case SHADER_OPCODE_SEND:2333generate_send(inst, dst, src[0], src[1], src[2],2334inst->ex_mlen > 0 ? src[3] : brw_null_reg());2335if ((inst->desc & 0xff) == BRW_BTI_STATELESS ||2336(inst->desc & 0xff) == GFX8_BTI_STATELESS_NON_COHERENT) {2337if (inst->size_written)2338fill_count++;2339else2340spill_count++;2341} else {2342send_count++;2343}2344break;23452346case SHADER_OPCODE_GET_BUFFER_SIZE:2347generate_get_buffer_size(inst, dst, src[0], src[1]);2348send_count++;2349break;2350case SHADER_OPCODE_TEX:2351case FS_OPCODE_TXB:2352case SHADER_OPCODE_TXD:2353case SHADER_OPCODE_TXF:2354case SHADER_OPCODE_TXF_CMS:2355case SHADER_OPCODE_TXL:2356case SHADER_OPCODE_TXS:2357case SHADER_OPCODE_LOD:2358case SHADER_OPCODE_TG4:2359case SHADER_OPCODE_SAMPLEINFO:2360assert(inst->src[0].file == BAD_FILE);2361generate_tex(inst, dst, src[1], src[2]);2362send_count++;2363break;23642365case FS_OPCODE_DDX_COARSE:2366case FS_OPCODE_DDX_FINE:2367generate_ddx(inst, dst, src[0]);2368break;2369case FS_OPCODE_DDY_COARSE:2370case FS_OPCODE_DDY_FINE:2371generate_ddy(inst, dst, src[0]);2372break;23732374case SHADER_OPCODE_GFX4_SCRATCH_WRITE:2375generate_scratch_write(inst, src[0]);2376spill_count++;2377break;23782379case SHADER_OPCODE_GFX4_SCRATCH_READ:2380generate_scratch_read(inst, dst);2381fill_count++;2382break;23832384case SHADER_OPCODE_GFX7_SCRATCH_READ:2385generate_scratch_read_gfx7(inst, dst);2386fill_count++;2387break;23882389case SHADER_OPCODE_SCRATCH_HEADER:2390generate_scratch_header(inst, dst);2391break;23922393case SHADER_OPCODE_MOV_INDIRECT:2394generate_mov_indirect(inst, dst, src[0], src[1]);2395break;23962397case SHADER_OPCODE_MOV_RELOC_IMM:2398assert(src[0].file == BRW_IMMEDIATE_VALUE);2399brw_MOV_reloc_imm(p, dst, dst.type, src[0].ud);2400break;24012402case SHADER_OPCODE_URB_READ_SIMD8:2403case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT:2404generate_urb_read(inst, dst, src[0]);2405send_count++;2406break;24072408case SHADER_OPCODE_URB_WRITE_SIMD8:2409case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT:2410case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED:2411case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT:2412generate_urb_write(inst, src[0]);2413send_count++;2414break;24152416case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:2417assert(inst->force_writemask_all);2418generate_uniform_pull_constant_load(inst, dst, src[0], src[1]);2419send_count++;2420break;24212422case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GFX7:2423assert(inst->force_writemask_all);2424generate_uniform_pull_constant_load_gfx7(inst, dst, src[0], src[1]);2425send_count++;2426break;24272428case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GFX4:2429generate_varying_pull_constant_load_gfx4(inst, dst, src[0]);2430send_count++;2431break;24322433case FS_OPCODE_REP_FB_WRITE:2434case FS_OPCODE_FB_WRITE:2435generate_fb_write(inst, src[0]);2436send_count++;2437break;24382439case FS_OPCODE_FB_READ:2440generate_fb_read(inst, dst, src[0]);2441send_count++;2442break;24432444case BRW_OPCODE_HALT:2445generate_halt(inst);2446break;24472448case SHADER_OPCODE_SHADER_TIME_ADD:2449generate_shader_time_add(inst, src[0], src[1], src[2]);2450break;24512452case SHADER_OPCODE_INTERLOCK:2453case SHADER_OPCODE_MEMORY_FENCE: {2454assert(src[1].file == BRW_IMMEDIATE_VALUE);2455assert(src[2].file == BRW_IMMEDIATE_VALUE);24562457const enum opcode send_op = inst->opcode == SHADER_OPCODE_INTERLOCK ?2458BRW_OPCODE_SENDC : BRW_OPCODE_SEND;24592460brw_memory_fence(p, dst, src[0], send_op,2461brw_message_target(inst->sfid),2462/* commit_enable */ src[1].ud,2463/* bti */ src[2].ud);2464send_count++;2465break;2466}24672468case FS_OPCODE_SCHEDULING_FENCE:2469if (inst->sources == 0 && swsb.regdist == 0 &&2470swsb.mode == TGL_SBID_NULL) {2471if (unlikely(debug_flag))2472disasm_info->use_tail = true;2473break;2474}24752476if (devinfo->ver >= 12) {2477/* Use the available SWSB information to stall. A single SYNC is2478* sufficient since if there were multiple dependencies, the2479* scoreboard algorithm already injected other SYNCs before this2480* instruction.2481*/2482brw_SYNC(p, TGL_SYNC_NOP);2483} else {2484for (unsigned i = 0; i < inst->sources; i++) {2485/* Emit a MOV to force a stall until the instruction producing the2486* registers finishes.2487*/2488brw_MOV(p, retype(brw_null_reg(), BRW_REGISTER_TYPE_UW),2489retype(src[i], BRW_REGISTER_TYPE_UW));2490}24912492if (inst->sources > 1)2493multiple_instructions_emitted = true;2494}24952496break;24972498case SHADER_OPCODE_FIND_LIVE_CHANNEL: {2499const struct brw_reg mask =2500brw_stage_has_packed_dispatch(devinfo, stage,2501prog_data) ? brw_imm_ud(~0u) :2502stage == MESA_SHADER_FRAGMENT ? brw_vmask_reg() :2503brw_dmask_reg();2504brw_find_live_channel(p, dst, mask);2505break;2506}2507case FS_OPCODE_LOAD_LIVE_CHANNELS: {2508assert(devinfo->ver >= 8);2509assert(inst->force_writemask_all && inst->group == 0);2510assert(inst->dst.file == BAD_FILE);2511brw_set_default_exec_size(p, BRW_EXECUTE_1);2512brw_MOV(p, retype(brw_flag_subreg(inst->flag_subreg),2513BRW_REGISTER_TYPE_UD),2514retype(brw_mask_reg(0), BRW_REGISTER_TYPE_UD));2515break;2516}2517case SHADER_OPCODE_BROADCAST:2518assert(inst->force_writemask_all);2519brw_broadcast(p, dst, src[0], src[1]);2520break;25212522case SHADER_OPCODE_SHUFFLE:2523generate_shuffle(inst, dst, src[0], src[1]);2524break;25252526case SHADER_OPCODE_SEL_EXEC:2527assert(inst->force_writemask_all);2528if (type_sz(dst.type) > 4 && !devinfo->has_64bit_float) {2529brw_set_default_mask_control(p, BRW_MASK_DISABLE);2530brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_UD, 0),2531subscript(src[1], BRW_REGISTER_TYPE_UD, 0));2532brw_set_default_swsb(p, tgl_swsb_null());2533brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_UD, 1),2534subscript(src[1], BRW_REGISTER_TYPE_UD, 1));2535brw_set_default_mask_control(p, BRW_MASK_ENABLE);2536brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_UD, 0),2537subscript(src[0], BRW_REGISTER_TYPE_UD, 0));2538brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_UD, 1),2539subscript(src[0], BRW_REGISTER_TYPE_UD, 1));2540} else {2541brw_set_default_mask_control(p, BRW_MASK_DISABLE);2542brw_MOV(p, dst, src[1]);2543brw_set_default_mask_control(p, BRW_MASK_ENABLE);2544brw_set_default_swsb(p, tgl_swsb_null());2545brw_MOV(p, dst, src[0]);2546}2547break;25482549case SHADER_OPCODE_QUAD_SWIZZLE:2550assert(src[1].file == BRW_IMMEDIATE_VALUE);2551assert(src[1].type == BRW_REGISTER_TYPE_UD);2552generate_quad_swizzle(inst, dst, src[0], src[1].ud);2553break;25542555case SHADER_OPCODE_CLUSTER_BROADCAST: {2556assert(!src[0].negate && !src[0].abs);2557assert(src[1].file == BRW_IMMEDIATE_VALUE);2558assert(src[1].type == BRW_REGISTER_TYPE_UD);2559assert(src[2].file == BRW_IMMEDIATE_VALUE);2560assert(src[2].type == BRW_REGISTER_TYPE_UD);2561const unsigned component = src[1].ud;2562const unsigned cluster_size = src[2].ud;2563unsigned vstride = cluster_size;2564unsigned width = cluster_size;25652566/* The maximum exec_size is 32, but the maximum width is only 16. */2567if (inst->exec_size == width) {2568vstride = 0;2569width = 1;2570}25712572struct brw_reg strided = stride(suboffset(src[0], component),2573vstride, width, 0);2574if (type_sz(src[0].type) > 4 &&2575(devinfo->is_cherryview || intel_device_info_is_9lp(devinfo) ||2576!devinfo->has_64bit_float)) {2577/* IVB has an issue (which we found empirically) where it reads2578* two address register components per channel for indirectly2579* addressed 64-bit sources.2580*2581* From the Cherryview PRM Vol 7. "Register Region Restrictions":2582*2583* "When source or destination datatype is 64b or operation is2584* integer DWord multiply, indirect addressing must not be2585* used."2586*2587* To work around both of these, we do two integer MOVs insead of2588* one 64-bit MOV. Because no double value should ever cross a2589* register boundary, it's safe to use the immediate offset in the2590* indirect here to handle adding 4 bytes to the offset and avoid2591* the extra ADD to the register file.2592*/2593assert(src[0].type == dst.type);2594brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 0),2595subscript(strided, BRW_REGISTER_TYPE_D, 0));2596brw_set_default_swsb(p, tgl_swsb_null());2597brw_MOV(p, subscript(dst, BRW_REGISTER_TYPE_D, 1),2598subscript(strided, BRW_REGISTER_TYPE_D, 1));2599} else {2600brw_MOV(p, dst, strided);2601}2602break;2603}26042605case FS_OPCODE_SET_SAMPLE_ID:2606generate_set_sample_id(inst, dst, src[0], src[1]);2607break;26082609case FS_OPCODE_PACK_HALF_2x16_SPLIT:2610generate_pack_half_2x16_split(inst, dst, src[0], src[1]);2611break;26122613case SHADER_OPCODE_HALT_TARGET:2614/* This is the place where the final HALT needs to be inserted if2615* we've emitted any discards. If not, this will emit no code.2616*/2617if (!patch_halt_jumps()) {2618if (unlikely(debug_flag)) {2619disasm_info->use_tail = true;2620}2621}2622break;26232624case FS_OPCODE_INTERPOLATE_AT_SAMPLE:2625generate_pixel_interpolator_query(inst, dst, src[0], src[1],2626GFX7_PIXEL_INTERPOLATOR_LOC_SAMPLE);2627send_count++;2628break;26292630case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET:2631generate_pixel_interpolator_query(inst, dst, src[0], src[1],2632GFX7_PIXEL_INTERPOLATOR_LOC_SHARED_OFFSET);2633send_count++;2634break;26352636case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET:2637generate_pixel_interpolator_query(inst, dst, src[0], src[1],2638GFX7_PIXEL_INTERPOLATOR_LOC_PER_SLOT_OFFSET);2639send_count++;2640break;26412642case CS_OPCODE_CS_TERMINATE:2643generate_cs_terminate(inst, src[0]);2644send_count++;2645break;26462647case SHADER_OPCODE_BARRIER:2648generate_barrier(inst, src[0]);2649send_count++;2650break;26512652case BRW_OPCODE_DIM:2653assert(devinfo->is_haswell);2654assert(src[0].type == BRW_REGISTER_TYPE_DF);2655assert(dst.type == BRW_REGISTER_TYPE_DF);2656brw_DIM(p, dst, retype(src[0], BRW_REGISTER_TYPE_F));2657break;26582659case SHADER_OPCODE_RND_MODE: {2660assert(src[0].file == BRW_IMMEDIATE_VALUE);2661/*2662* Changes the floating point rounding mode updating the control2663* register field defined at cr0.0[5-6] bits.2664*/2665enum brw_rnd_mode mode =2666(enum brw_rnd_mode) (src[0].d << BRW_CR0_RND_MODE_SHIFT);2667brw_float_controls_mode(p, mode, BRW_CR0_RND_MODE_MASK);2668}2669break;26702671case SHADER_OPCODE_FLOAT_CONTROL_MODE:2672assert(src[0].file == BRW_IMMEDIATE_VALUE);2673assert(src[1].file == BRW_IMMEDIATE_VALUE);2674brw_float_controls_mode(p, src[0].d, src[1].d);2675break;26762677case SHADER_OPCODE_GET_DSS_ID:2678/* The Slice, Dual-SubSlice, SubSlice, EU, and Thread IDs are all2679* stored in sr0.0. Normally, for reading from HW regs, we'd just do2680* this in the IR and let the back-end generate some code but these2681* live in the state register which tends to have special rules.2682*2683* For convenience, we combine Slice ID and Dual-SubSlice ID into a2684* single ID.2685*/2686if (devinfo->ver == 12) {2687/* There is a SWSB restriction that requires that any time sr0 is2688* accessed both the instruction doing the access and the next one2689* have SWSB set to RegDist(1).2690*/2691if (brw_get_default_swsb(p).mode != TGL_SBID_NULL)2692brw_SYNC(p, TGL_SYNC_NOP);2693brw_set_default_swsb(p, tgl_swsb_regdist(1));2694brw_SHR(p, dst, brw_sr0_reg(0), brw_imm_ud(9));2695brw_set_default_swsb(p, tgl_swsb_regdist(1));2696brw_AND(p, dst, dst, brw_imm_ud(0x1f));2697} else {2698/* These move around basically every hardware generation, so don't2699* do any >= checks and fail if the platform hasn't explicitly2700* been enabled here.2701*/2702unreachable("Unsupported platform");2703}2704break;27052706default:2707unreachable("Unsupported opcode");27082709case SHADER_OPCODE_LOAD_PAYLOAD:2710unreachable("Should be lowered by lower_load_payload()");2711}27122713if (multiple_instructions_emitted)2714continue;27152716if (inst->no_dd_clear || inst->no_dd_check || inst->conditional_mod) {2717assert(p->next_insn_offset == last_insn_offset + 16 ||2718!"conditional_mod, no_dd_check, or no_dd_clear set for IR "2719"emitting more than 1 instruction");27202721brw_inst *last = &p->store[last_insn_offset / 16];27222723if (inst->conditional_mod)2724brw_inst_set_cond_modifier(p->devinfo, last, inst->conditional_mod);2725if (devinfo->ver < 12) {2726brw_inst_set_no_dd_clear(p->devinfo, last, inst->no_dd_clear);2727brw_inst_set_no_dd_check(p->devinfo, last, inst->no_dd_check);2728}2729}2730}27312732brw_set_uip_jip(p, start_offset);27332734/* end of program sentinel */2735disasm_new_inst_group(disasm_info, p->next_insn_offset);27362737#ifndef NDEBUG2738bool validated =2739#else2740if (unlikely(debug_flag))2741#endif2742brw_validate_instructions(devinfo, p->store,2743start_offset,2744p->next_insn_offset,2745disasm_info);27462747int before_size = p->next_insn_offset - start_offset;2748brw_compact_instructions(p, start_offset, disasm_info);2749int after_size = p->next_insn_offset - start_offset;27502751if (unlikely(debug_flag)) {2752unsigned char sha1[21];2753char sha1buf[41];27542755_mesa_sha1_compute(p->store + start_offset / sizeof(brw_inst),2756after_size, sha1);2757_mesa_sha1_format(sha1buf, sha1);27582759fprintf(stderr, "Native code for %s (sha1 %s)\n"2760"SIMD%d shader: %d instructions. %d loops. %u cycles. "2761"%d:%d spills:fills, %u sends, "2762"scheduled with mode %s. "2763"Promoted %u constants. "2764"Compacted %d to %d bytes (%.0f%%)\n",2765shader_name, sha1buf,2766dispatch_width, before_size / 16,2767loop_count, perf.latency,2768spill_count, fill_count, send_count,2769shader_stats.scheduler_mode,2770shader_stats.promoted_constants,2771before_size, after_size,2772100.0f * (before_size - after_size) / before_size);27732774/* overriding the shader makes disasm_info invalid */2775if (!brw_try_override_assembly(p, start_offset, sha1buf)) {2776dump_assembly(p->store, start_offset, p->next_insn_offset,2777disasm_info, perf.block_latency);2778} else {2779fprintf(stderr, "Successfully overrode shader with sha1 %s\n\n", sha1buf);2780}2781}2782ralloc_free(disasm_info);2783#ifndef NDEBUG2784if (!validated && !debug_flag) {2785fprintf(stderr,2786"Validation failed. Rerun with INTEL_DEBUG=shaders to get more information.\n");2787}2788#endif2789assert(validated);27902791compiler->shader_debug_log(log_data,2792"%s SIMD%d shader: %d inst, %d loops, %u cycles, "2793"%d:%d spills:fills, %u sends, "2794"scheduled with mode %s, "2795"Promoted %u constants, "2796"compacted %d to %d bytes.",2797_mesa_shader_stage_to_abbrev(stage),2798dispatch_width, before_size / 16 - nop_count,2799loop_count, perf.latency,2800spill_count, fill_count, send_count,2801shader_stats.scheduler_mode,2802shader_stats.promoted_constants,2803before_size, after_size);2804if (stats) {2805stats->dispatch_width = dispatch_width;2806stats->instructions = before_size / 16 - nop_count;2807stats->sends = send_count;2808stats->loops = loop_count;2809stats->cycles = perf.latency;2810stats->spills = spill_count;2811stats->fills = fill_count;2812}28132814return start_offset;2815}28162817void2818fs_generator::add_const_data(void *data, unsigned size)2819{2820assert(prog_data->const_data_size == 0);2821if (size > 0) {2822prog_data->const_data_size = size;2823prog_data->const_data_offset = brw_append_data(p, data, size, 32);2824}2825}28262827void2828fs_generator::add_resume_sbt(unsigned num_resume_shaders, uint64_t *sbt)2829{2830assert(brw_shader_stage_is_bindless(stage));2831struct brw_bs_prog_data *bs_prog_data = brw_bs_prog_data(prog_data);2832if (num_resume_shaders > 0) {2833bs_prog_data->resume_sbt_offset =2834brw_append_data(p, sbt, num_resume_shaders * sizeof(uint64_t), 32);2835for (unsigned i = 0; i < num_resume_shaders; i++) {2836size_t offset = bs_prog_data->resume_sbt_offset + i * sizeof(*sbt);2837assert(offset <= UINT32_MAX);2838brw_add_reloc(p, BRW_SHADER_RELOC_SHADER_START_OFFSET,2839BRW_SHADER_RELOC_TYPE_U32,2840(uint32_t)offset, (uint32_t)sbt[i]);2841}2842}2843}28442845const unsigned *2846fs_generator::get_assembly()2847{2848prog_data->relocs = brw_get_shader_relocs(p, &prog_data->num_relocs);28492850return brw_get_program(p, &prog_data->program_size);2851}285228532854