Path: blob/21.2-virgl/src/gallium/drivers/nouveau/codegen/nv50_ir_target_nv50.cpp
4574 views
/*1* Copyright 2011 Christoph Bumiller2*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 shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*/2122#include "codegen/nv50_ir_target_nv50.h"2324namespace nv50_ir {2526Target *getTargetNV50(unsigned int chipset)27{28return new TargetNV50(chipset);29}3031TargetNV50::TargetNV50(unsigned int card) : Target(true, true, false)32{33chipset = card;3435wposMask = 0;36for (unsigned int i = 0; i <= SV_LAST; ++i)37sysvalLocation[i] = ~0;3839initOpInfo();40}4142#if 043// BULTINS / LIBRARY FUNCTIONS:4445// TODO46static const uint32_t nvc0_builtin_code[] =47{48};4950static const uint16_t nvc0_builtin_offsets[NV50_BUILTIN_COUNT] =51{52};53#endif5455void56TargetNV50::getBuiltinCode(const uint32_t **code, uint32_t *size) const57{58*code = NULL;59*size = 0;60}6162uint32_t63TargetNV50::getBuiltinOffset(int builtin) const64{65return 0;66}6768struct nv50_opProperties69{70operation op;71unsigned int mNeg : 4;72unsigned int mAbs : 4;73unsigned int mNot : 4;74unsigned int mSat : 4;75unsigned int fConst : 3;76unsigned int fShared : 3;77unsigned int fAttrib : 3;78unsigned int fImm : 3;79};8081static const struct nv50_opProperties _initProps[] =82{83// neg abs not sat c[] s[], a[], imm84{ OP_ADD, 0x3, 0x0, 0x0, 0x8, 0x2, 0x1, 0x1, 0x2 },85{ OP_SUB, 0x3, 0x0, 0x0, 0x8, 0x2, 0x1, 0x1, 0x2 },86{ OP_MUL, 0x3, 0x0, 0x0, 0x0, 0x2, 0x1, 0x1, 0x2 },87{ OP_MAX, 0x3, 0x3, 0x0, 0x0, 0x2, 0x1, 0x1, 0x0 },88{ OP_MIN, 0x3, 0x3, 0x0, 0x0, 0x2, 0x1, 0x1, 0x0 },89{ OP_MAD, 0x7, 0x0, 0x0, 0x8, 0x6, 0x1, 0x1, 0x0 }, // special constraint90{ OP_ABS, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0 },91{ OP_NEG, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0 },92{ OP_CVT, 0x1, 0x1, 0x0, 0x8, 0x0, 0x1, 0x1, 0x0 },93{ OP_AND, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x2 },94{ OP_OR, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x2 },95{ OP_XOR, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x2 },96{ OP_SHL, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2 },97{ OP_SHR, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2 },98{ OP_SET, 0x3, 0x3, 0x0, 0x0, 0x2, 0x1, 0x1, 0x0 },99{ OP_PREEX2, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },100{ OP_PRESIN, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },101{ OP_EX2, 0x0, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x0 },102{ OP_LG2, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },103{ OP_RCP, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },104{ OP_RSQ, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },105{ OP_DFDX, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },106{ OP_DFDY, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },107};108109void TargetNV50::initOpInfo()110{111unsigned int i, j;112113static const operation commutativeList[] =114{115OP_ADD, OP_MUL, OP_MAD, OP_FMA, OP_AND, OP_OR, OP_XOR, OP_MAX, OP_MIN,116OP_SET_AND, OP_SET_OR, OP_SET_XOR, OP_SET, OP_SELP, OP_SLCT117};118static const operation shortFormList[] =119{120OP_MOV, OP_ADD, OP_SUB, OP_MUL, OP_MAD, OP_SAD, OP_RCP, OP_LINTERP,121OP_PINTERP, OP_TEX, OP_TXF122};123static const operation noDestList[] =124{125OP_STORE, OP_WRSV, OP_EXPORT, OP_BRA, OP_CALL, OP_RET, OP_EXIT,126OP_DISCARD, OP_CONT, OP_BREAK, OP_PRECONT, OP_PREBREAK, OP_PRERET,127OP_JOIN, OP_JOINAT, OP_BRKPT, OP_MEMBAR, OP_EMIT, OP_RESTART,128OP_QUADON, OP_QUADPOP, OP_TEXBAR, OP_SUSTB, OP_SUSTP, OP_SUREDP,129OP_SUREDB, OP_BAR130};131static const operation noPredList[] =132{133OP_CALL, OP_PREBREAK, OP_PRERET, OP_QUADON, OP_QUADPOP, OP_JOINAT,134OP_EMIT, OP_RESTART135};136137for (i = 0; i < DATA_FILE_COUNT; ++i)138nativeFileMap[i] = (DataFile)i;139nativeFileMap[FILE_PREDICATE] = FILE_FLAGS;140141for (i = 0; i < OP_LAST; ++i) {142opInfo[i].variants = NULL;143opInfo[i].op = (operation)i;144opInfo[i].srcTypes = 1 << (int)TYPE_F32;145opInfo[i].dstTypes = 1 << (int)TYPE_F32;146opInfo[i].immdBits = 0xffffffff;147opInfo[i].srcNr = operationSrcNr[i];148149for (j = 0; j < opInfo[i].srcNr; ++j) {150opInfo[i].srcMods[j] = 0;151opInfo[i].srcFiles[j] = 1 << (int)FILE_GPR;152}153opInfo[i].dstMods = 0;154opInfo[i].dstFiles = 1 << (int)FILE_GPR;155156opInfo[i].hasDest = 1;157opInfo[i].vector = (i >= OP_TEX && i <= OP_TEXCSAA);158opInfo[i].commutative = false; /* set below */159opInfo[i].pseudo = (i < OP_MOV);160opInfo[i].predicate = !opInfo[i].pseudo;161opInfo[i].flow = (i >= OP_BRA && i <= OP_JOIN);162opInfo[i].minEncSize = 8; /* set below */163}164for (i = 0; i < ARRAY_SIZE(commutativeList); ++i)165opInfo[commutativeList[i]].commutative = true;166for (i = 0; i < ARRAY_SIZE(shortFormList); ++i)167opInfo[shortFormList[i]].minEncSize = 4;168for (i = 0; i < ARRAY_SIZE(noDestList); ++i)169opInfo[noDestList[i]].hasDest = 0;170for (i = 0; i < ARRAY_SIZE(noPredList); ++i)171opInfo[noPredList[i]].predicate = 0;172173for (i = 0; i < ARRAY_SIZE(_initProps); ++i) {174const struct nv50_opProperties *prop = &_initProps[i];175176for (int s = 0; s < 3; ++s) {177if (prop->mNeg & (1 << s))178opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_NEG;179if (prop->mAbs & (1 << s))180opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_ABS;181if (prop->mNot & (1 << s))182opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_NOT;183if (prop->fConst & (1 << s))184opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_MEMORY_CONST;185if (prop->fShared & (1 << s))186opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_MEMORY_SHARED;187if (prop->fAttrib & (1 << s))188opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_SHADER_INPUT;189if (prop->fImm & (1 << s))190opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_IMMEDIATE;191}192if (prop->mSat & 8)193opInfo[prop->op].dstMods = NV50_IR_MOD_SAT;194}195196if (chipset >= 0xa0)197opInfo[OP_MUL].dstMods = NV50_IR_MOD_SAT;198}199200unsigned int201TargetNV50::getFileSize(DataFile file) const202{203switch (file) {204case FILE_NULL: return 0;205case FILE_GPR: return 254; // in 16-bit units **206case FILE_PREDICATE: return 0;207case FILE_FLAGS: return 4;208case FILE_ADDRESS: return 4;209case FILE_BARRIER: return 0;210case FILE_IMMEDIATE: return 0;211case FILE_MEMORY_CONST: return 65536;212case FILE_SHADER_INPUT: return 0x200;213case FILE_SHADER_OUTPUT: return 0x200;214case FILE_MEMORY_BUFFER: return 0xffffffff;215case FILE_MEMORY_GLOBAL: return 0xffffffff;216case FILE_MEMORY_SHARED: return 16 << 10;217case FILE_MEMORY_LOCAL: return 48 << 10;218case FILE_SYSTEM_VALUE: return 16;219default:220assert(!"invalid file");221return 0;222}223// ** only first 128 units encodable for 16-bit regs224}225226unsigned int227TargetNV50::getFileUnit(DataFile file) const228{229if (file == FILE_GPR || file == FILE_ADDRESS)230return 1;231if (file == FILE_SYSTEM_VALUE)232return 2;233return 0;234}235236uint32_t237TargetNV50::getSVAddress(DataFile shaderFile, const Symbol *sym) const238{239switch (sym->reg.data.sv.sv) {240case SV_FACE:241return 0x3fc;242case SV_POSITION:243{244uint32_t addr = sysvalLocation[sym->reg.data.sv.sv];245for (int c = 0; c < sym->reg.data.sv.index; ++c)246if (wposMask & (1 << c))247addr += 4;248return addr;249}250case SV_PRIMITIVE_ID:251return shaderFile == FILE_SHADER_INPUT ? 0x18 :252sysvalLocation[sym->reg.data.sv.sv];253case SV_NCTAID:254return sym->reg.data.sv.index >= 2 ? 0x10 : 0x8 + 2 * sym->reg.data.sv.index;255case SV_CTAID:256return sym->reg.data.sv.index >= 2 ? 0x12 : 0xc + 2 * sym->reg.data.sv.index;257case SV_NTID:258return 0x2 + 2 * sym->reg.data.sv.index;259case SV_TID:260case SV_COMBINED_TID:261return 0;262case SV_SAMPLE_POS:263return 0; /* sample position is handled differently */264case SV_THREAD_KILL:265return 0;266default:267return sysvalLocation[sym->reg.data.sv.sv];268}269}270271// long: rrr, arr, rcr, acr, rrc, arc, gcr, grr272// short: rr, ar, rc, gr273// immd: ri, gi274bool275TargetNV50::insnCanLoad(const Instruction *i, int s,276const Instruction *ld) const277{278DataFile sf = ld->src(0).getFile();279280// immediate 0 can be represented by GPR $r63/$r127281// this does not work with global memory ld/st/atom282if (sf == FILE_IMMEDIATE && ld->getSrc(0)->reg.data.u64 == 0)283return (!i->isPseudo() &&284!i->asTex() &&285i->op != OP_EXPORT &&286i->op != OP_STORE &&287((i->op != OP_ATOM && i->op != OP_LOAD) ||288i->src(0).getFile() != FILE_MEMORY_GLOBAL));289290if (sf == FILE_IMMEDIATE && (i->predSrc >= 0 || i->flagsDef >= 0))291return false;292if (s >= opInfo[i->op].srcNr)293return false;294if (!(opInfo[i->op].srcFiles[s] & (1 << (int)sf)))295return false;296if (s == 2 && i->src(1).getFile() != FILE_GPR)297return false;298299// NOTE: don't rely on flagsDef300if (sf == FILE_IMMEDIATE)301for (int d = 0; i->defExists(d); ++d)302if (i->def(d).getFile() == FILE_FLAGS)303return false;304305unsigned mode = 0;306307for (int z = 0; z < Target::operationSrcNr[i->op]; ++z) {308DataFile zf = (z == s) ? sf : i->src(z).getFile();309switch (zf) {310case FILE_GPR:311break;312case FILE_MEMORY_SHARED:313case FILE_SHADER_INPUT:314mode |= 1 << (z * 2);315break;316case FILE_MEMORY_CONST:317mode |= 2 << (z * 2);318break;319case FILE_IMMEDIATE:320mode |= 3 << (z * 2);321default:322break;323}324}325326switch (mode) {327case 0x00:328case 0x01:329case 0x03:330case 0x08:331case 0x0c:332case 0x20:333case 0x21:334break;335case 0x09:336// Shader inputs get transformed to p[] in geometry shaders, and those337// aren't allowed to be used at the same time as c[].338if (ld->bb->getProgram()->getType() == Program::TYPE_GEOMETRY)339return false;340break;341case 0x0d:342if (ld->bb->getProgram()->getType() != Program::TYPE_GEOMETRY)343return false;344break;345default:346return false;347}348349uint8_t ldSize;350351if ((i->op == OP_MUL || i->op == OP_MAD) && !isFloatType(i->dType)) {352// 32-bit MUL will be split into 16-bit MULs353if (ld->src(0).isIndirect(0))354return false;355if (sf == FILE_IMMEDIATE)356return false;357if (i->subOp == NV50_IR_SUBOP_MUL_HIGH && sf == FILE_MEMORY_CONST)358return false;359ldSize = 2;360} else {361ldSize = typeSizeof(ld->dType);362}363364if (sf == FILE_IMMEDIATE) {365if (ldSize == 2 && (i->op == OP_AND || i->op == OP_OR || i->op == OP_XOR))366return false;367return ldSize <= 4;368}369370371// Check if memory access is encodable:372373if (ldSize < 4 && sf == FILE_SHADER_INPUT) // no < 4-byte aligned a[] access374return false;375if (ld->getSrc(0)->reg.data.offset > (int32_t)(127 * ldSize))376return false;377378if (ld->src(0).isIndirect(0)) {379for (int z = 0; i->srcExists(z); ++z)380if (i->src(z).isIndirect(0))381return false;382383// s[] access only possible in CP, $aX always applies384if (sf == FILE_MEMORY_SHARED)385return true;386if (!ld->bb) // can't check type ...387return false;388Program::Type pt = ld->bb->getProgram()->getType();389390// $aX applies to c[] only in VP, FP, GP if p[] is not accessed391if (pt == Program::TYPE_COMPUTE)392return false;393if (pt == Program::TYPE_GEOMETRY) {394if (sf == FILE_MEMORY_CONST)395return i->src(s).getFile() != FILE_SHADER_INPUT;396return sf == FILE_SHADER_INPUT;397}398return sf == FILE_MEMORY_CONST;399}400return true;401}402403bool404TargetNV50::insnCanLoadOffset(const Instruction *i, int s, int offset) const405{406if (!i->src(s).isIndirect(0))407return true;408offset += i->src(s).get()->reg.data.offset;409if (i->op == OP_LOAD || i->op == OP_STORE || i->op == OP_ATOM) {410// There are some restrictions in theory, but in practice they're never411// going to be hit. However offsets on global/shared memory are just412// plain not supported.413return i->src(s).getFile() != FILE_MEMORY_GLOBAL &&414i->src(s).getFile() != FILE_MEMORY_SHARED;415}416return offset >= 0 && offset <= (int32_t)(127 * i->src(s).get()->reg.size);417}418419bool420TargetNV50::isAccessSupported(DataFile file, DataType ty) const421{422if (ty == TYPE_B96 || ty == TYPE_NONE)423return false;424if (typeSizeof(ty) > 4)425return (file == FILE_MEMORY_LOCAL) || (file == FILE_MEMORY_GLOBAL) ||426(file == FILE_MEMORY_BUFFER);427return true;428}429430bool431TargetNV50::isOpSupported(operation op, DataType ty) const432{433if (ty == TYPE_F64 && chipset < 0xa0)434return false;435436switch (op) {437case OP_PRERET:438return chipset >= 0xa0;439case OP_TXG:440return chipset >= 0xa3 && chipset != 0xaa && chipset != 0xac;441case OP_POW:442case OP_SQRT:443case OP_DIV:444case OP_MOD:445case OP_SET_AND:446case OP_SET_OR:447case OP_SET_XOR:448case OP_SLCT:449case OP_SELP:450case OP_POPCNT:451case OP_INSBF:452case OP_EXTBF:453case OP_EXIT: // want exit modifier instead (on NOP if required)454case OP_MEMBAR:455case OP_SHLADD:456case OP_XMAD:457return false;458case OP_SAD:459return ty == TYPE_S32;460case OP_SET:461return !isFloatType(ty);462default:463return true;464}465}466467bool468TargetNV50::isModSupported(const Instruction *insn, int s, Modifier mod) const469{470if (!isFloatType(insn->dType)) {471switch (insn->op) {472case OP_ABS:473case OP_NEG:474case OP_CVT:475case OP_CEIL:476case OP_FLOOR:477case OP_TRUNC:478case OP_AND:479case OP_OR:480case OP_XOR:481break;482case OP_ADD:483if (insn->src(s ? 0 : 1).mod.neg())484return false;485break;486case OP_SUB:487if (s == 0)488return insn->src(1).mod.neg() ? false : true;489break;490case OP_SET:491if (insn->sType != TYPE_F32)492return false;493break;494default:495return false;496}497}498if (s >= opInfo[insn->op].srcNr || s >= 3)499return false;500return (mod & Modifier(opInfo[insn->op].srcMods[s])) == mod;501}502503bool504TargetNV50::mayPredicate(const Instruction *insn, const Value *pred) const505{506if (insn->getPredicate() || insn->flagsSrc >= 0)507return false;508for (int s = 0; insn->srcExists(s); ++s)509if (insn->src(s).getFile() == FILE_IMMEDIATE)510return false;511return opInfo[insn->op].predicate;512}513514bool515TargetNV50::isSatSupported(const Instruction *insn) const516{517if (insn->op == OP_CVT)518return true;519if (insn->dType != TYPE_F32)520return false;521return opInfo[insn->op].dstMods & NV50_IR_MOD_SAT;522}523524int TargetNV50::getLatency(const Instruction *i) const525{526// TODO: tune these values527if (i->op == OP_LOAD) {528switch (i->src(0).getFile()) {529case FILE_MEMORY_LOCAL:530case FILE_MEMORY_GLOBAL:531case FILE_MEMORY_BUFFER:532return 100; // really 400 to 800533default:534return 22;535}536}537return 22;538}539540// These are "inverse" throughput values, i.e. the number of cycles required541// to issue a specific instruction for a full warp (32 threads).542//543// Assuming we have more than 1 warp in flight, a higher issue latency results544// in a lower result latency since the MP will have spent more time with other545// warps.546// This also helps to determine the number of cycles between instructions in547// a single warp.548//549int TargetNV50::getThroughput(const Instruction *i) const550{551// TODO: tune these values552if (i->dType == TYPE_F32) {553switch (i->op) {554case OP_RCP:555case OP_RSQ:556case OP_LG2:557case OP_SIN:558case OP_COS:559case OP_PRESIN:560case OP_PREEX2:561return 16;562default:563return 4;564}565} else566if (i->dType == TYPE_U32 || i->dType == TYPE_S32) {567return 4;568} else569if (i->dType == TYPE_F64) {570return 32;571} else {572return 1;573}574}575576static void577recordLocation(uint16_t *locs, uint8_t *masks,578const struct nv50_ir_varying *var)579{580uint16_t addr = var->slot[0] * 4;581582switch (var->sn) {583case TGSI_SEMANTIC_POSITION: locs[SV_POSITION] = addr; break;584case TGSI_SEMANTIC_INSTANCEID: locs[SV_INSTANCE_ID] = addr; break;585case TGSI_SEMANTIC_VERTEXID: locs[SV_VERTEX_ID] = addr; break;586case TGSI_SEMANTIC_PRIMID: locs[SV_PRIMITIVE_ID] = addr; break;587case TGSI_SEMANTIC_LAYER: locs[SV_LAYER] = addr; break;588case TGSI_SEMANTIC_VIEWPORT_INDEX: locs[SV_VIEWPORT_INDEX] = addr; break;589default:590break;591}592if (var->sn == TGSI_SEMANTIC_POSITION && masks)593masks[0] = var->mask;594}595596void597TargetNV50::parseDriverInfo(const struct nv50_ir_prog_info *info,598const struct nv50_ir_prog_info_out *info_out)599{600unsigned int i;601for (i = 0; i < info_out->numOutputs; ++i)602recordLocation(sysvalLocation, NULL, &info_out->out[i]);603for (i = 0; i < info_out->numInputs; ++i)604recordLocation(sysvalLocation, &wposMask, &info_out->in[i]);605for (i = 0; i < info_out->numSysVals; ++i)606recordLocation(sysvalLocation, NULL, &info_out->sv[i]);607608if (sysvalLocation[SV_POSITION] >= 0x200) {609// not assigned by driver, but we need it internally610wposMask = 0x8;611sysvalLocation[SV_POSITION] = 0;612}613614Target::parseDriverInfo(info, info_out);615}616617} // namespace nv50_ir618619620