ade9be0300cce44d151c02d922f5dd68c40efc31
[mesa.git] / src / gallium / drivers / nouveau / codegen / nv50_ir_target_nv50.cpp
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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "codegen/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, 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, 0x00000000
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, OP_TEXBAR, OP_SUSTB, OP_SUSTP, OP_SUREDP,
129 OP_SUREDB, OP_BAR
130 };
131 static const operation noPredList[] =
132 {
133 OP_CALL, OP_PREBREAK, OP_PRERET, OP_QUADON, OP_QUADPOP, OP_JOINAT
134 };
135
136 for (i = 0; i < DATA_FILE_COUNT; ++i)
137 nativeFileMap[i] = (DataFile)i;
138 nativeFileMap[FILE_PREDICATE] = FILE_FLAGS;
139
140 for (i = 0; i < OP_LAST; ++i) {
141 opInfo[i].variants = NULL;
142 opInfo[i].op = (operation)i;
143 opInfo[i].srcTypes = 1 << (int)TYPE_F32;
144 opInfo[i].dstTypes = 1 << (int)TYPE_F32;
145 opInfo[i].immdBits = 0xffffffff;
146 opInfo[i].srcNr = operationSrcNr[i];
147
148 for (j = 0; j < opInfo[i].srcNr; ++j) {
149 opInfo[i].srcMods[j] = 0;
150 opInfo[i].srcFiles[j] = 1 << (int)FILE_GPR;
151 }
152 opInfo[i].dstMods = 0;
153 opInfo[i].dstFiles = 1 << (int)FILE_GPR;
154
155 opInfo[i].hasDest = 1;
156 opInfo[i].vector = (i >= OP_TEX && i <= OP_TEXCSAA);
157 opInfo[i].commutative = (commutative[i / 32] >> (i % 32)) & 1;
158 opInfo[i].pseudo = (i < OP_MOV);
159 opInfo[i].predicate = !opInfo[i].pseudo;
160 opInfo[i].flow = (i >= OP_BRA && i <= OP_JOIN);
161 opInfo[i].minEncSize = (shortForm[i / 32] & (1 << (i % 32))) ? 4 : 8;
162 }
163 for (i = 0; i < sizeof(noDestList) / sizeof(noDestList[0]); ++i)
164 opInfo[noDestList[i]].hasDest = 0;
165 for (i = 0; i < sizeof(noPredList) / sizeof(noPredList[0]); ++i)
166 opInfo[noPredList[i]].predicate = 0;
167
168 for (i = 0; i < sizeof(_initProps) / sizeof(_initProps[0]); ++i) {
169 const struct opProperties *prop = &_initProps[i];
170
171 for (int s = 0; s < 3; ++s) {
172 if (prop->mNeg & (1 << s))
173 opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_NEG;
174 if (prop->mAbs & (1 << s))
175 opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_ABS;
176 if (prop->mNot & (1 << s))
177 opInfo[prop->op].srcMods[s] |= NV50_IR_MOD_NOT;
178 if (prop->fConst & (1 << s))
179 opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_MEMORY_CONST;
180 if (prop->fShared & (1 << s))
181 opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_MEMORY_SHARED;
182 if (prop->fAttrib & (1 << s))
183 opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_SHADER_INPUT;
184 if (prop->fImm & (1 << s))
185 opInfo[prop->op].srcFiles[s] |= 1 << (int)FILE_IMMEDIATE;
186 }
187 if (prop->mSat & 8)
188 opInfo[prop->op].dstMods = NV50_IR_MOD_SAT;
189 }
190 }
191
192 unsigned int
193 TargetNV50::getFileSize(DataFile file) const
194 {
195 switch (file) {
196 case FILE_NULL: return 0;
197 case FILE_GPR: return 256; // in 16-bit units **
198 case FILE_PREDICATE: return 0;
199 case FILE_FLAGS: return 4;
200 case FILE_ADDRESS: return 4;
201 case FILE_IMMEDIATE: return 0;
202 case FILE_MEMORY_CONST: return 65536;
203 case FILE_SHADER_INPUT: return 0x200;
204 case FILE_SHADER_OUTPUT: return 0x200;
205 case FILE_MEMORY_GLOBAL: return 0xffffffff;
206 case FILE_MEMORY_SHARED: return 16 << 10;
207 case FILE_MEMORY_LOCAL: return 48 << 10;
208 case FILE_SYSTEM_VALUE: return 16;
209 default:
210 assert(!"invalid file");
211 return 0;
212 }
213 // ** only first 128 units encodable for 16-bit regs
214 }
215
216 unsigned int
217 TargetNV50::getFileUnit(DataFile file) const
218 {
219 if (file == FILE_GPR || file == FILE_ADDRESS)
220 return 1;
221 if (file == FILE_SYSTEM_VALUE)
222 return 2;
223 return 0;
224 }
225
226 uint32_t
227 TargetNV50::getSVAddress(DataFile shaderFile, const Symbol *sym) const
228 {
229 switch (sym->reg.data.sv.sv) {
230 case SV_FACE:
231 return 0x3fc;
232 case SV_POSITION:
233 {
234 uint32_t addr = sysvalLocation[sym->reg.data.sv.sv];
235 for (int c = 0; c < sym->reg.data.sv.index; ++c)
236 if (wposMask & (1 << c))
237 addr += 4;
238 return addr;
239 }
240 case SV_NCTAID:
241 return 0x8 + 2 * sym->reg.data.sv.index;
242 case SV_CTAID:
243 return 0xc + 2 * sym->reg.data.sv.index;
244 case SV_NTID:
245 return 0x2 + 2 * sym->reg.data.sv.index;
246 case SV_TID:
247 return 0;
248 default:
249 return sysvalLocation[sym->reg.data.sv.sv];
250 }
251 }
252
253 // long: rrr, arr, rcr, acr, rrc, arc, gcr, grr
254 // short: rr, ar, rc, gr
255 // immd: ri, gi
256 bool
257 TargetNV50::insnCanLoad(const Instruction *i, int s,
258 const Instruction *ld) const
259 {
260 DataFile sf = ld->src(0).getFile();
261
262 if (sf == FILE_IMMEDIATE && (i->predSrc >= 0 || i->flagsDef >= 0))
263 return false;
264 if (s >= opInfo[i->op].srcNr)
265 return false;
266 if (!(opInfo[i->op].srcFiles[s] & (1 << (int)sf)))
267 return false;
268 if (s == 2 && i->src(1).getFile() != FILE_GPR)
269 return false;
270
271 // NOTE: don't rely on flagsDef
272 for (int d = 0; i->defExists(d); ++d)
273 if (i->def(d).getFile() == FILE_FLAGS)
274 return false;
275
276 unsigned mode = 0;
277
278 for (int z = 0; z < Target::operationSrcNr[i->op]; ++z) {
279 DataFile zf = (z == s) ? sf : i->src(z).getFile();
280 switch (zf) {
281 case FILE_GPR:
282 break;
283 case FILE_MEMORY_SHARED:
284 case FILE_SHADER_INPUT:
285 mode |= 1 << (z * 2);
286 break;
287 case FILE_MEMORY_CONST:
288 mode |= 2 << (z * 2);
289 break;
290 case FILE_IMMEDIATE:
291 mode |= 3 << (z * 2);
292 default:
293 break;
294 }
295 }
296
297 switch (mode) {
298 case 0x00:
299 case 0x01:
300 case 0x03:
301 case 0x08:
302 case 0x09:
303 case 0x0c:
304 case 0x20:
305 case 0x21:
306 break;
307 case 0x0d:
308 if (ld->bb->getProgram()->getType() != Program::TYPE_GEOMETRY)
309 return false;
310 default:
311 return false;
312 }
313
314 uint8_t ldSize;
315
316 if ((i->op == OP_MUL || i->op == OP_MAD) && !isFloatType(i->dType)) {
317 // 32-bit MUL will be split into 16-bit MULs
318 if (ld->src(0).isIndirect(0))
319 return false;
320 if (sf == FILE_IMMEDIATE)
321 return false;
322 ldSize = 2;
323 } else {
324 ldSize = typeSizeof(ld->dType);
325 }
326
327 if (sf == FILE_IMMEDIATE)
328 return true;
329
330
331 // Check if memory access is encodable:
332
333 if (ldSize < 4 && sf == FILE_SHADER_INPUT) // no < 4-byte aligned a[] access
334 return false;
335 if (ld->getSrc(0)->reg.data.offset > (int32_t)(127 * ldSize))
336 return false;
337
338 if (ld->src(0).isIndirect(0)) {
339 for (int z = 0; i->srcExists(z); ++z)
340 if (i->src(z).isIndirect(0))
341 return false;
342
343 // s[] access only possible in CP, $aX always applies
344 if (sf == FILE_MEMORY_SHARED)
345 return true;
346 if (!ld->bb) // can't check type ...
347 return false;
348 Program::Type pt = ld->bb->getProgram()->getType();
349
350 // $aX applies to c[] only in VP, FP, GP if p[] is not accessed
351 if (pt == Program::TYPE_COMPUTE)
352 return false;
353 if (pt == Program::TYPE_GEOMETRY) {
354 if (sf == FILE_MEMORY_CONST)
355 return i->src(s).getFile() != FILE_SHADER_INPUT;
356 return sf == FILE_SHADER_INPUT;
357 }
358 return sf == FILE_MEMORY_CONST;
359 }
360 return true;
361 }
362
363 bool
364 TargetNV50::isAccessSupported(DataFile file, DataType ty) const
365 {
366 if (ty == TYPE_B96 || ty == TYPE_NONE)
367 return false;
368 if (typeSizeof(ty) > 4)
369 return (file == FILE_MEMORY_LOCAL) || (file == FILE_MEMORY_GLOBAL);
370 return true;
371 }
372
373 bool
374 TargetNV50::isOpSupported(operation op, DataType ty) const
375 {
376 if (ty == TYPE_F64 && chipset < 0xa0)
377 return false;
378
379 switch (op) {
380 case OP_PRERET:
381 return chipset >= 0xa0;
382 case OP_TXG:
383 return chipset >= 0xa3;
384 case OP_POW:
385 case OP_SQRT:
386 case OP_DIV:
387 case OP_MOD:
388 case OP_SET_AND:
389 case OP_SET_OR:
390 case OP_SET_XOR:
391 case OP_SLCT:
392 case OP_SELP:
393 case OP_POPCNT:
394 case OP_INSBF:
395 case OP_EXTBF:
396 case OP_EXIT: // want exit modifier instead (on NOP if required)
397 case OP_MEMBAR:
398 return false;
399 case OP_SAD:
400 return ty == TYPE_S32;
401 default:
402 return true;
403 }
404 }
405
406 bool
407 TargetNV50::isModSupported(const Instruction *insn, int s, Modifier mod) const
408 {
409 if (!isFloatType(insn->dType)) {
410 switch (insn->op) {
411 case OP_ABS:
412 case OP_NEG:
413 case OP_CVT:
414 case OP_CEIL:
415 case OP_FLOOR:
416 case OP_TRUNC:
417 case OP_AND:
418 case OP_OR:
419 case OP_XOR:
420 break;
421 case OP_ADD:
422 if (insn->src(s ? 0 : 1).mod.neg())
423 return false;
424 break;
425 case OP_SUB:
426 if (s == 0)
427 return insn->src(1).mod.neg() ? false : true;
428 break;
429 case OP_SET:
430 if (insn->sType != TYPE_F32)
431 return false;
432 break;
433 default:
434 return false;
435 }
436 }
437 if (s > 3)
438 return false;
439 return (mod & Modifier(opInfo[insn->op].srcMods[s])) == mod;
440 }
441
442 bool
443 TargetNV50::mayPredicate(const Instruction *insn, const Value *pred) const
444 {
445 if (insn->getPredicate() || insn->flagsSrc >= 0)
446 return false;
447 for (int s = 0; insn->srcExists(s); ++s)
448 if (insn->src(s).getFile() == FILE_IMMEDIATE)
449 return false;
450 return opInfo[insn->op].predicate;
451 }
452
453 bool
454 TargetNV50::isSatSupported(const Instruction *insn) const
455 {
456 if (insn->op == OP_CVT)
457 return true;
458 if (insn->dType != TYPE_F32)
459 return false;
460 return opInfo[insn->op].dstMods & NV50_IR_MOD_SAT;
461 }
462
463 int TargetNV50::getLatency(const Instruction *i) const
464 {
465 // TODO: tune these values
466 if (i->op == OP_LOAD) {
467 switch (i->src(0).getFile()) {
468 case FILE_MEMORY_LOCAL:
469 case FILE_MEMORY_GLOBAL:
470 return 100; // really 400 to 800
471 default:
472 return 22;
473 }
474 }
475 return 22;
476 }
477
478 // These are "inverse" throughput values, i.e. the number of cycles required
479 // to issue a specific instruction for a full warp (32 threads).
480 //
481 // Assuming we have more than 1 warp in flight, a higher issue latency results
482 // in a lower result latency since the MP will have spent more time with other
483 // warps.
484 // This also helps to determine the number of cycles between instructions in
485 // a single warp.
486 //
487 int TargetNV50::getThroughput(const Instruction *i) const
488 {
489 // TODO: tune these values
490 if (i->dType == TYPE_F32) {
491 switch (i->op) {
492 case OP_RCP:
493 case OP_RSQ:
494 case OP_LG2:
495 case OP_SIN:
496 case OP_COS:
497 case OP_PRESIN:
498 case OP_PREEX2:
499 return 16;
500 default:
501 return 4;
502 }
503 } else
504 if (i->dType == TYPE_U32 || i->dType == TYPE_S32) {
505 return 4;
506 } else
507 if (i->dType == TYPE_F64) {
508 return 32;
509 } else {
510 return 1;
511 }
512 }
513
514 static void
515 recordLocation(uint16_t *locs, uint8_t *masks,
516 const struct nv50_ir_varying *var)
517 {
518 uint16_t addr = var->slot[0] * 4;
519
520 switch (var->sn) {
521 case TGSI_SEMANTIC_POSITION: locs[SV_POSITION] = addr; break;
522 case TGSI_SEMANTIC_INSTANCEID: locs[SV_INSTANCE_ID] = addr; break;
523 case TGSI_SEMANTIC_VERTEXID: locs[SV_VERTEX_ID] = addr; break;
524 case TGSI_SEMANTIC_PRIMID: locs[SV_PRIMITIVE_ID] = addr; break;
525 case NV50_SEMANTIC_LAYER: locs[SV_LAYER] = addr; break;
526 case NV50_SEMANTIC_VIEWPORTINDEX: locs[SV_VIEWPORT_INDEX] = addr; break;
527 default:
528 break;
529 }
530 if (var->sn == TGSI_SEMANTIC_POSITION && masks)
531 masks[0] = var->mask;
532 }
533
534 void
535 TargetNV50::parseDriverInfo(const struct nv50_ir_prog_info *info)
536 {
537 unsigned int i;
538 for (i = 0; i < info->numOutputs; ++i)
539 recordLocation(sysvalLocation, NULL, &info->out[i]);
540 for (i = 0; i < info->numInputs; ++i)
541 recordLocation(sysvalLocation, &wposMask, &info->in[i]);
542 for (i = 0; i < info->numSysVals; ++i)
543 recordLocation(sysvalLocation, NULL, &info->sv[i]);
544
545 if (sysvalLocation[SV_POSITION] >= 0x200) {
546 // not assigned by driver, but we need it internally
547 wposMask = 0x8;
548 sysvalLocation[SV_POSITION] = 0;
549 }
550 }
551
552 } // namespace nv50_ir