Home | History | Annotate | Download | only in codegen
      1 /*
      2  * Copyright 2011 Christoph Bumiller
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice shall be included in
     12  * all copies or substantial portions of the Software.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     17  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     18  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
     19  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     20  * SOFTWARE.
     21  */
     22 
     23 #include "nv50_ir_target_nv50.h"
     24 
     25 namespace nv50_ir {
     26 
     27 Target *getTargetNV50(unsigned int chipset)
     28 {
     29    return new TargetNV50(chipset);
     30 }
     31 
     32 TargetNV50::TargetNV50(unsigned int card) : Target(true, false)
     33 {
     34    chipset = card;
     35 
     36    wposMask = 0;
     37    for (unsigned int i = 0; i <= SV_LAST; ++i)
     38       sysvalLocation[i] = ~0;
     39 
     40    initOpInfo();
     41 }
     42 
     43 #if 0
     44 // BULTINS / LIBRARY FUNCTIONS:
     45 
     46 // TODO
     47 static const uint32_t nvc0_builtin_code[] =
     48 {
     49 };
     50 
     51 static const uint16_t nvc0_builtin_offsets[NV50_BUILTIN_COUNT] =
     52 {
     53 };
     54 #endif
     55 
     56 void
     57 TargetNV50::getBuiltinCode(const uint32_t **code, uint32_t *size) const
     58 {
     59    *code = NULL;
     60    *size = 0;
     61 }
     62 
     63 uint32_t
     64 TargetNV50::getBuiltinOffset(int builtin) const
     65 {
     66    return 0;
     67 }
     68 
     69 struct opProperties
     70 {
     71    operation op;
     72    unsigned int mNeg    : 4;
     73    unsigned int mAbs    : 4;
     74    unsigned int mNot    : 4;
     75    unsigned int mSat    : 4;
     76    unsigned int fConst  : 3;
     77    unsigned int fShared : 3;
     78    unsigned int fAttrib : 3;
     79    unsigned int fImm    : 3;
     80 };
     81 
     82 static const struct opProperties _initProps[] =
     83 {
     84    //           neg  abs  not  sat  c[]  s[], a[], imm
     85    { OP_ADD,    0x3, 0x0, 0x0, 0x8, 0x2, 0x1, 0x1, 0x2 },
     86    { OP_SUB,    0x3, 0x0, 0x0, 0x0, 0x2, 0x1, 0x1, 0x2 },
     87    { OP_MUL,    0x3, 0x0, 0x0, 0x0, 0x2, 0x1, 0x1, 0x2 },
     88    { OP_MAX,    0x3, 0x3, 0x0, 0x0, 0x2, 0x1, 0x1, 0x0 },
     89    { OP_MIN,    0x3, 0x3, 0x0, 0x0, 0x2, 0x1, 0x1, 0x0 },
     90    { OP_MAD,    0x7, 0x0, 0x0, 0x0, 0x6, 0x1, 0x1, 0x0 }, // special constraint
     91    { OP_ABS,    0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0 },
     92    { OP_NEG,    0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0 },
     93    { OP_CVT,    0x1, 0x1, 0x0, 0x8, 0x0, 0x1, 0x1, 0x0 },
     94    { OP_AND,    0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x2 },
     95    { OP_OR,     0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x2 },
     96    { OP_XOR,    0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x2 },
     97    { OP_SHL,    0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2 },
     98    { OP_SHR,    0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2 },
     99    { OP_SET,    0x3, 0x3, 0x0, 0x0, 0x2, 0x1, 0x1, 0x0 },
    100    { OP_PREEX2, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
    101    { OP_PRESIN, 0x1, 0x1, 0x0, 0x0, 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 };
    108 
    109 void TargetNV50::initOpInfo()
    110 {
    111    unsigned int i, j;
    112 
    113    static const uint32_t commutative[(OP_LAST + 31) / 32] =
    114    {
    115       // ADD,MAD,MUL,AND,OR,XOR,MAX,MIN
    116       0x0670ca00, 0x0000003f, 0x00000000
    117    };
    118    static const uint32_t shortForm[(OP_LAST + 31) / 32] =
    119    {
    120       // MOV,ADD,SUB,MUL,SAD,L/PINTERP,RCP,TEX,TXF
    121       0x00010e40, 0x00000040, 0x00000498
    122    };
    123    static const operation noDestList[] =
    124    {
    125       OP_STORE, OP_WRSV, OP_EXPORT, OP_BRA, OP_CALL, OP_RET, OP_EXIT,
    126       OP_DISCARD, OP_CONT, OP_BREAK, OP_PRECONT, OP_PREBREAK, OP_PRERET,
    127       OP_JOIN, OP_JOINAT, OP_BRKPT, OP_MEMBAR, OP_EMIT, OP_RESTART,
    128       OP_QUADON, OP_QUADPOP
    129    };
    130    static const operation noPredList[] =
    131    {
    132       OP_CALL, OP_PREBREAK, OP_PRERET, OP_QUADON, OP_QUADPOP, OP_JOINAT
    133    };
    134 
    135    for (i = 0; i < DATA_FILE_COUNT; ++i)
    136       nativeFileMap[i] = (DataFile)i;
    137    nativeFileMap[FILE_PREDICATE] = FILE_FLAGS;
    138 
    139    for (i = 0; i < OP_LAST; ++i) {
    140       opInfo[i].variants = NULL;
    141       opInfo[i].op = (operation)i;
    142       opInfo[i].srcTypes = 1 << (int)TYPE_F32;
    143       opInfo[i].dstTypes = 1 << (int)TYPE_F32;
    144       opInfo[i].immdBits = 0xffffffff;
    145       opInfo[i].srcNr = operationSrcNr[i];
    146 
    147       for (j = 0; j < opInfo[i].srcNr; ++j) {
    148          opInfo[i].srcMods[j] = 0;
    149          opInfo[i].srcFiles[j] = 1 << (int)FILE_GPR;
    150       }
    151       opInfo[i].dstMods = 0;
    152       opInfo[i].dstFiles = 1 << (int)FILE_GPR;
    153 
    154       opInfo[i].hasDest = 1;
    155       opInfo[i].vector = (i >= OP_TEX && i <= OP_TEXCSAA);
    156       opInfo[i].commutative = (commutative[i / 32] >> (i % 32)) & 1;
    157       opInfo[i].pseudo = (i < OP_MOV);
    158       opInfo[i].predicate = !opInfo[i].pseudo;
    159       opInfo[i].flow = (i >= OP_BRA && i <= OP_JOIN);
    160       opInfo[i].minEncSize = (shortForm[i / 32] & (1 << (i % 32))) ? 4 : 8;
    161    }
    162    for (i = 0; i < sizeof(noDestList) / sizeof(noDestList[0]); ++i)
    163       opInfo[noDestList[i]].hasDest = 0;
    164    for (i = 0; i < sizeof(noPredList) / sizeof(noPredList[0]); ++i)
    165       opInfo[noPredList[i]].predicate = 0;
    166 
    167    for (i = 0; i < sizeof(_initProps) / sizeof(_initProps[0]); ++i) {
    168       const struct opProperties *prop = &_initProps[i];
    169 
    170       for (int s = 0; s < 3; ++s) {
    171          if (prop->mNeg & (1 << s))
    172             opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_NEG;
    173          if (prop->mAbs & (1 << s))
    174             opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_ABS;
    175          if (prop->mNot & (1 << s))
    176             opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_NOT;
    177          if (prop->fConst & (1 << s))
    178             opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_MEMORY_CONST;
    179          if (prop->fShared & (1 << s))
    180             opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_MEMORY_SHARED;
    181          if (prop->fAttrib & (1 << s))
    182             opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_SHADER_INPUT;
    183          if (prop->fImm & (1 << s))
    184             opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_IMMEDIATE;
    185       }
    186       if (prop->mSat & 8)
    187          opInfo[prop->op].dstMods = NV50_IR_MOD_SAT;
    188    }
    189 }
    190 
    191 unsigned int
    192 TargetNV50::getFileSize(DataFile file) const
    193 {
    194    switch (file) {
    195    case FILE_NULL:          return 0;
    196    case FILE_GPR:           return 256; // in 16-bit units **
    197    case FILE_PREDICATE:     return 0;
    198    case FILE_FLAGS:         return 4;
    199    case FILE_ADDRESS:       return 4;
    200    case FILE_IMMEDIATE:     return 0;
    201    case FILE_MEMORY_CONST:  return 65536;
    202    case FILE_SHADER_INPUT:  return 0x200;
    203    case FILE_SHADER_OUTPUT: return 0x200;
    204    case FILE_MEMORY_GLOBAL: return 0xffffffff;
    205    case FILE_MEMORY_SHARED: return 16 << 10;
    206    case FILE_MEMORY_LOCAL:  return 48 << 10;
    207    case FILE_SYSTEM_VALUE:  return 16;
    208    default:
    209       assert(!"invalid file");
    210       return 0;
    211    }
    212    // ** only first 128 units encodable for 16-bit regs
    213 }
    214 
    215 unsigned int
    216 TargetNV50::getFileUnit(DataFile file) const
    217 {
    218    if (file == FILE_GPR || file == FILE_ADDRESS)
    219       return 1;
    220    if (file == FILE_SYSTEM_VALUE)
    221       return 2;
    222    return 0;
    223 }
    224 
    225 uint32_t
    226 TargetNV50::getSVAddress(DataFile shaderFile, const Symbol *sym) const
    227 {
    228    switch (sym->reg.data.sv.sv) {
    229    case SV_FACE:
    230       return 0x3fc;
    231    case SV_POSITION:
    232    {
    233       uint32_t addr = sysvalLocation[sym->reg.data.sv.sv];
    234       for (int c = 0; c < sym->reg.data.sv.index; ++c)
    235          if (wposMask & (1 << c))
    236             addr += 4;
    237       return addr;
    238    }
    239    case SV_NCTAID:
    240       return 0x8 + 2 * sym->reg.data.sv.index;
    241    case SV_CTAID:
    242       return 0xc + 2 * sym->reg.data.sv.index;
    243    case SV_NTID:
    244       return 0x2 + 2 * sym->reg.data.sv.index;
    245    case SV_TID:
    246       return 0;
    247    default:
    248       return sysvalLocation[sym->reg.data.sv.sv];
    249    }
    250 }
    251 
    252 // long:  rrr, arr, rcr, acr, rrc, arc, gcr, grr
    253 // short: rr, ar, rc, gr
    254 // immd:  ri, gi
    255 bool
    256 TargetNV50::insnCanLoad(const Instruction *i, int s,
    257                         const Instruction *ld) const
    258 {
    259    DataFile sf = ld->src(0).getFile();
    260 
    261    if (sf == FILE_IMMEDIATE && (i->predSrc >= 0 || i->flagsDef >= 0))
    262       return false;
    263    if (s >= opInfo[i->op].srcNr)
    264       return false;
    265    if (!(opInfo[i->op].srcFiles[s] & (1 << (int)sf)))
    266       return false;
    267    if (s == 2 && i->src(1).getFile() != FILE_GPR)
    268       return false;
    269 
    270    // NOTE: don't rely on flagsDef
    271    for (int d = 0; i->defExists(d); ++d)
    272       if (i->def(d).getFile() == FILE_FLAGS)
    273          return false;
    274 
    275    unsigned mode = 0;
    276 
    277    for (int z = 0; z < Target::operationSrcNr[i->op]; ++z) {
    278       DataFile zf = (z == s) ? sf : i->src(z).getFile();
    279       switch (zf) {
    280       case FILE_GPR:
    281          break;
    282       case FILE_MEMORY_SHARED:
    283       case FILE_SHADER_INPUT:
    284          mode |= 1 << (z * 2);
    285          break;
    286       case FILE_MEMORY_CONST:
    287          mode |= 2 << (z * 2);
    288          break;
    289       case FILE_IMMEDIATE:
    290          mode |= 3 << (z * 2);
    291       default:
    292          break;
    293       }
    294    }
    295 
    296    switch (mode) {
    297    case 0x00:
    298    case 0x01:
    299    case 0x03:
    300    case 0x08:
    301    case 0x09:
    302    case 0x0c:
    303    case 0x20:
    304    case 0x21:
    305       break;
    306    case 0x0d:
    307       if (ld->bb->getProgram()->getType() != Program::TYPE_GEOMETRY)
    308          return false;
    309    default:
    310       return false;
    311    }
    312 
    313    uint8_t ldSize;
    314 
    315    if ((i->op == OP_MUL || i->op == OP_MAD) && !isFloatType(i->dType)) {
    316       // 32-bit MUL will be split into 16-bit MULs
    317       if (ld->src(0).isIndirect(0))
    318          return false;
    319       if (sf == FILE_IMMEDIATE)
    320          return false;
    321       ldSize = 2;
    322    } else {
    323       ldSize = typeSizeof(ld->dType);
    324    }
    325 
    326    if (sf == FILE_IMMEDIATE)
    327       return true;
    328 
    329 
    330    // Check if memory access is encodable:
    331 
    332    if (ldSize < 4 && sf == FILE_SHADER_INPUT) // no < 4-byte aligned a[] access
    333       return false;
    334    if (ld->getSrc(0)->reg.data.offset > (int32_t)(127 * ldSize))
    335       return false;
    336 
    337    if (ld->src(0).isIndirect(0)) {
    338       for (int z = 0; i->srcExists(z); ++z)
    339          if (i->src(z).isIndirect(0))
    340             return false;
    341 
    342       // s[] access only possible in CP, $aX always applies
    343       if (sf == FILE_MEMORY_SHARED)
    344          return true;
    345       if (!ld->bb) // can't check type ...
    346          return false;
    347       Program::Type pt = ld->bb->getProgram()->getType();
    348 
    349       // $aX applies to c[] only in VP, FP, GP if p[] is not accessed
    350       if (pt == Program::TYPE_COMPUTE)
    351          return false;
    352       if (pt == Program::TYPE_GEOMETRY) {
    353          if (sf == FILE_MEMORY_CONST)
    354             return i->src(s).getFile() != FILE_SHADER_INPUT;
    355          return sf == FILE_SHADER_INPUT;
    356       }
    357       return sf == FILE_MEMORY_CONST;
    358    }
    359    return true;
    360 }
    361 
    362 bool
    363 TargetNV50::isAccessSupported(DataFile file, DataType ty) const
    364 {
    365    if (ty == TYPE_B96 || ty == TYPE_NONE)
    366       return false;
    367    if (typeSizeof(ty) > 4)
    368       return (file == FILE_MEMORY_LOCAL) || (file == FILE_MEMORY_GLOBAL);
    369    return true;
    370 }
    371 
    372 bool
    373 TargetNV50::isOpSupported(operation op, DataType ty) const
    374 {
    375    if (ty == TYPE_F64 && chipset < 0xa0)
    376       return false;
    377 
    378    switch (op) {
    379    case OP_PRERET:
    380       return chipset >= 0xa0;
    381    case OP_TXG:
    382       return chipset >= 0xa3;
    383    case OP_POW:
    384    case OP_SQRT:
    385    case OP_DIV:
    386    case OP_MOD:
    387    case OP_SET_AND:
    388    case OP_SET_OR:
    389    case OP_SET_XOR:
    390    case OP_SLCT:
    391    case OP_SELP:
    392    case OP_POPCNT:
    393    case OP_INSBF:
    394    case OP_EXTBF:
    395    case OP_EXIT: // want exit modifier instead (on NOP if required)
    396       return false;
    397    case OP_SAD:
    398       return ty == TYPE_S32;
    399    default:
    400       return true;
    401    }
    402 }
    403 
    404 bool
    405 TargetNV50::isModSupported(const Instruction *insn, int s, Modifier mod) const
    406 {
    407    if (!isFloatType(insn->dType)) {
    408       switch (insn->op) {
    409       case OP_ABS:
    410       case OP_NEG:
    411       case OP_CVT:
    412       case OP_CEIL:
    413       case OP_FLOOR:
    414       case OP_TRUNC:
    415       case OP_AND:
    416       case OP_OR:
    417       case OP_XOR:
    418          break;
    419       case OP_ADD:
    420          if (insn->src(s ? 0 : 1).mod.neg())
    421             return false;
    422          break;
    423       case OP_SUB:
    424          if (s == 0)
    425             return insn->src(1).mod.neg() ? false : true;
    426          break;
    427       case OP_SET:
    428          if (insn->sType != TYPE_F32)
    429             return false;
    430          break;
    431       default:
    432          return false;
    433       }
    434    }
    435    if (s > 3)
    436       return false;
    437    return (mod & Modifier(opInfo[insn->op].srcMods[s])) == mod;
    438 }
    439 
    440 bool
    441 TargetNV50::mayPredicate(const Instruction *insn, const Value *pred) const
    442 {
    443    if (insn->getPredicate() || insn->flagsSrc >= 0)
    444       return false;
    445    for (int s = 0; insn->srcExists(s); ++s)
    446       if (insn->src(s).getFile() == FILE_IMMEDIATE)
    447          return false;
    448    return opInfo[insn->op].predicate;
    449 }
    450 
    451 bool
    452 TargetNV50::isSatSupported(const Instruction *insn) const
    453 {
    454    if (insn->op == OP_CVT)
    455       return true;
    456    if (insn->dType != TYPE_F32)
    457       return false;
    458    return opInfo[insn->op].dstMods & NV50_IR_MOD_SAT;
    459 }
    460 
    461 int TargetNV50::getLatency(const Instruction *i) const
    462 {
    463    // TODO: tune these values
    464    if (i->op == OP_LOAD) {
    465       switch (i->src(0).getFile()) {
    466       case FILE_MEMORY_LOCAL:
    467       case FILE_MEMORY_GLOBAL:
    468          return 100; // really 400 to 800
    469       default:
    470          return 22;
    471       }
    472    }
    473    return 22;
    474 }
    475 
    476 // These are "inverse" throughput values, i.e. the number of cycles required
    477 // to issue a specific instruction for a full warp (32 threads).
    478 //
    479 // Assuming we have more than 1 warp in flight, a higher issue latency results
    480 // in a lower result latency since the MP will have spent more time with other
    481 // warps.
    482 // This also helps to determine the number of cycles between instructions in
    483 // a single warp.
    484 //
    485 int TargetNV50::getThroughput(const Instruction *i) const
    486 {
    487    // TODO: tune these values
    488    if (i->dType == TYPE_F32) {
    489       switch (i->op) {
    490       case OP_RCP:
    491       case OP_RSQ:
    492       case OP_LG2:
    493       case OP_SIN:
    494       case OP_COS:
    495       case OP_PRESIN:
    496       case OP_PREEX2:
    497          return 16;
    498       default:
    499          return 4;
    500       }
    501    } else
    502    if (i->dType == TYPE_U32 || i->dType == TYPE_S32) {
    503       return 4;
    504    } else
    505    if (i->dType == TYPE_F64) {
    506       return 32;
    507    } else {
    508       return 1;
    509    }
    510 }
    511 
    512 static void
    513 recordLocation(uint16_t *locs, uint8_t *masks,
    514                const struct nv50_ir_varying *var)
    515 {
    516    uint16_t addr = var->slot[0] * 4;
    517 
    518    switch (var->sn) {
    519    case TGSI_SEMANTIC_POSITION: locs[SV_POSITION] = addr; break;
    520    case TGSI_SEMANTIC_INSTANCEID: locs[SV_INSTANCE_ID] = addr; break;
    521    case TGSI_SEMANTIC_VERTEXID: locs[SV_VERTEX_ID] = addr; break;
    522    case TGSI_SEMANTIC_PRIMID: locs[SV_PRIMITIVE_ID] = addr; break;
    523    case NV50_SEMANTIC_LAYER: locs[SV_LAYER] = addr; break;
    524    case NV50_SEMANTIC_VIEWPORTINDEX: locs[SV_VIEWPORT_INDEX] = addr; break;
    525    default:
    526       break;
    527    }
    528    if (var->sn == TGSI_SEMANTIC_POSITION && masks)
    529       masks[0] = var->mask;
    530 }
    531 
    532 void
    533 TargetNV50::parseDriverInfo(const struct nv50_ir_prog_info *info)
    534 {
    535    unsigned int i;
    536    for (i = 0; i < info->numOutputs; ++i)
    537       recordLocation(sysvalLocation, NULL, &info->out[i]);
    538    for (i = 0; i < info->numInputs; ++i)
    539       recordLocation(sysvalLocation, &wposMask, &info->in[i]);
    540    for (i = 0; i < info->numSysVals; ++i)
    541       recordLocation(sysvalLocation, NULL, &info->sv[i]);
    542 
    543    if (sysvalLocation[SV_POSITION] >= 0x200) {
    544       // not assigned by driver, but we need it internally
    545       wposMask = 0x8;
    546       sysvalLocation[SV_POSITION] = 0;
    547    }
    548 }
    549 
    550 } // namespace nv50_ir
    551