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