2 * Copyright 2011 Christoph Bumiller
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 #include "nv50_ir_target_nv50.h"
27 Target
*getTargetNV50(unsigned int chipset
)
29 return new TargetNV50(chipset
);
32 TargetNV50::TargetNV50(unsigned int card
)
37 for (unsigned int i
= 0; i
<= SV_LAST
; ++i
)
38 sysvalLocation
[i
] = ~0;
44 // BULTINS / LIBRARY FUNCTIONS:
47 static const uint32_t nvc0_builtin_code
[] =
51 static const uint16_t nvc0_builtin_offsets
[NV50_BUILTIN_COUNT
] =
57 TargetNV50::getBuiltinCode(const uint32_t **code
, uint32_t *size
) const
64 TargetNV50::getBuiltinOffset(int builtin
) const
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;
82 static const struct opProperties _initProps
[] =
84 // neg abs not sat c[] s[], a[], imm
85 { OP_ADD
, 0x3, 0x0, 0x0, 0x8,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, 0x0, 0x0 },
105 { OP_DFDX
, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
106 { OP_DFDY
, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
109 void TargetNV50::initOpInfo()
113 static const uint32_t commutative
[(OP_LAST
+ 31) / 32] =
115 // ADD,MAD,MUL,AND,OR,XOR,MAX,MIN
116 0x0670ca00, 0x0000003f, 0x00000000
118 static const uint32_t shortForm
[(OP_LAST
+ 31) / 32] =
120 // MOV,ADD,SUB,MUL,SAD,L/PINTERP,RCP,TEX,TXF
121 0x00010e40, 0x00000040, 0x00000498
123 static const operation noDestList
[] =
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
130 static const operation noPredList
[] =
132 OP_CALL
, OP_PREBREAK
, OP_PRERET
, OP_QUADON
, OP_QUADPOP
, OP_JOINAT
137 for (i
= 0; i
< DATA_FILE_COUNT
; ++i
)
138 nativeFileMap
[i
] = (DataFile
)i
;
139 nativeFileMap
[FILE_PREDICATE
] = FILE_FLAGS
;
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
];
149 for (j
= 0; j
< opInfo
[i
].srcNr
; ++j
) {
150 opInfo
[i
].srcMods
[j
] = 0;
151 opInfo
[i
].srcFiles
[j
] = 1 << (int)FILE_GPR
;
153 opInfo
[i
].dstMods
= 0;
154 opInfo
[i
].dstFiles
= 1 << (int)FILE_GPR
;
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;
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;
169 for (i
= 0; i
< sizeof(_initProps
) / sizeof(_initProps
[0]); ++i
) {
170 const struct opProperties
*prop
= &_initProps
[i
];
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
;
189 opInfo
[prop
->op
].dstMods
= NV50_IR_MOD_SAT
;
194 TargetNV50::getFileSize(DataFile file
) const
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;
211 assert(!"invalid file");
214 // ** only first 128 units encodable for 16-bit regs
218 TargetNV50::getFileUnit(DataFile file
) const
220 if (file
== FILE_GPR
|| file
== FILE_ADDRESS
)
222 if (file
== FILE_SYSTEM_VALUE
)
228 TargetNV50::getSVAddress(DataFile shaderFile
, const Symbol
*sym
) const
230 switch (sym
->reg
.data
.sv
.sv
) {
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
))
242 return 0x8 + 2 * sym
->reg
.data
.sv
.index
;
244 return 0xc + 2 * sym
->reg
.data
.sv
.index
;
246 return 0x2 + 2 * sym
->reg
.data
.sv
.index
;
250 return sysvalLocation
[sym
->reg
.data
.sv
.sv
];
254 // long: rrr, arr, rcr, acr, rrc, arc, gcr, grr
255 // short: rr, ar, rc, gr
258 TargetNV50::insnCanLoad(const Instruction
*i
, int s
,
259 const Instruction
*ld
) const
261 DataFile sf
= ld
->src(0).getFile();
263 if (sf
== FILE_IMMEDIATE
&& (i
->predSrc
>= 0 || i
->flagsDef
>= 0))
265 if (s
>= opInfo
[i
->op
].srcNr
)
267 if (!(opInfo
[i
->op
].srcFiles
[s
] & (1 << (int)sf
)))
269 if (s
== 2 && i
->src(1).getFile() != FILE_GPR
)
272 // NOTE: don't rely on flagsDef
273 for (int d
= 0; i
->defExists(d
); ++d
)
274 if (i
->def(d
).getFile() == FILE_FLAGS
)
279 for (int z
= 0; z
< Target::operationSrcNr
[i
->op
]; ++z
) {
280 DataFile zf
= (z
== s
) ? sf
: i
->src(z
).getFile();
284 case FILE_MEMORY_SHARED
:
285 case FILE_SHADER_INPUT
:
286 mode
|= 1 << (z
* 2);
288 case FILE_MEMORY_CONST
:
289 mode
|= 2 << (z
* 2);
292 mode
|= 3 << (z
* 2);
309 if (ld
->bb
->getProgram()->getType() != Program::TYPE_GEOMETRY
)
315 if (ld
->getSrc(0)->reg
.data
.offset
> (int32_t)(127 * typeSizeof(ld
->dType
)))
318 if (ld
->src(0).isIndirect(0)) {
319 for (int z
= 0; i
->srcExists(z
); ++z
)
320 if (i
->src(z
).isIndirect(0))
323 // s[] access only possible in CP, $aX always applies
324 if (sf
== FILE_MEMORY_SHARED
)
326 if (!ld
->bb
) // can't check type ...
328 Program::Type pt
= ld
->bb
->getProgram()->getType();
330 // $aX applies to c[] only in VP, FP, GP if p[] is not accessed
331 if (pt
== Program::TYPE_COMPUTE
)
333 if (pt
== Program::TYPE_GEOMETRY
) {
334 if (sf
== FILE_MEMORY_CONST
)
335 return i
->src(s
).getFile() != FILE_SHADER_INPUT
;
336 return sf
== FILE_SHADER_INPUT
;
338 return sf
== FILE_MEMORY_CONST
;
344 TargetNV50::isAccessSupported(DataFile file
, DataType ty
) const
346 if (ty
== TYPE_B96
|| ty
== TYPE_NONE
)
348 if (typeSizeof(ty
) > 4)
349 return (file
== FILE_MEMORY_LOCAL
) || (file
== FILE_MEMORY_GLOBAL
);
354 TargetNV50::isOpSupported(operation op
, DataType ty
) const
356 if (ty
== TYPE_F64
&& chipset
< 0xa0)
361 return chipset
>= 0xa0;
363 return chipset
>= 0xa3;
376 case OP_EXIT
: // want exit modifier instead (on NOP if required)
379 return ty
== TYPE_S32
;
386 TargetNV50::isModSupported(const Instruction
*insn
, int s
, Modifier mod
) const
388 if (!isFloatType(insn
->dType
)) {
401 if (insn
->src(s
? 0 : 1).mod
.neg())
406 return insn
->src(1).mod
.neg() ? false : true;
409 if (insn
->sType
!= TYPE_F32
)
418 return (mod
& Modifier(opInfo
[insn
->op
].srcMods
[s
])) == mod
;
422 TargetNV50::mayPredicate(const Instruction
*insn
, const Value
*pred
) const
424 if (insn
->getPredicate() || insn
->flagsSrc
>= 0)
426 for (int s
= 0; insn
->srcExists(s
); ++s
)
427 if (insn
->src(s
).getFile() == FILE_IMMEDIATE
)
429 return opInfo
[insn
->op
].predicate
;
433 TargetNV50::isSatSupported(const Instruction
*insn
) const
435 if (insn
->op
== OP_CVT
)
437 if (insn
->dType
!= TYPE_F32
)
439 return opInfo
[insn
->op
].dstMods
& NV50_IR_MOD_SAT
;
442 int TargetNV50::getLatency(const Instruction
*i
) const
444 // TODO: tune these values
445 if (i
->op
== OP_LOAD
) {
446 switch (i
->src(0).getFile()) {
447 case FILE_MEMORY_LOCAL
:
448 case FILE_MEMORY_GLOBAL
:
449 return 100; // really 400 to 800
457 // These are "inverse" throughput values, i.e. the number of cycles required
458 // to issue a specific instruction for a full warp (32 threads).
460 // Assuming we have more than 1 warp in flight, a higher issue latency results
461 // in a lower result latency since the MP will have spent more time with other
463 // This also helps to determine the number of cycles between instructions in
466 int TargetNV50::getThroughput(const Instruction
*i
) const
468 // TODO: tune these values
469 if (i
->dType
== TYPE_F32
) {
483 if (i
->dType
== TYPE_U32
|| i
->dType
== TYPE_S32
) {
486 if (i
->dType
== TYPE_F64
) {
494 recordLocation(uint16_t *locs
, uint8_t *masks
,
495 const struct nv50_ir_varying
*var
)
497 uint16_t addr
= var
->slot
[0] * 4;
500 case TGSI_SEMANTIC_POSITION
: locs
[SV_POSITION
] = addr
; break;
501 case TGSI_SEMANTIC_INSTANCEID
: locs
[SV_INSTANCE_ID
] = addr
; break;
502 case TGSI_SEMANTIC_VERTEXID
: locs
[SV_VERTEX_ID
] = addr
; break;
503 case TGSI_SEMANTIC_PRIMID
: locs
[SV_PRIMITIVE_ID
] = addr
; break;
504 case NV50_SEMANTIC_LAYER
: locs
[SV_LAYER
] = addr
; break;
505 case NV50_SEMANTIC_VIEWPORTINDEX
: locs
[SV_VIEWPORT_INDEX
] = addr
; break;
509 if (var
->sn
== TGSI_SEMANTIC_POSITION
&& masks
)
510 masks
[0] = var
->mask
;
514 TargetNV50::parseDriverInfo(const struct nv50_ir_prog_info
*info
)
517 for (i
= 0; i
< info
->numOutputs
; ++i
)
518 recordLocation(sysvalLocation
, NULL
, &info
->out
[i
]);
519 for (i
= 0; i
< info
->numInputs
; ++i
)
520 recordLocation(sysvalLocation
, &wposMask
, &info
->in
[i
]);
521 for (i
= 0; i
< info
->numSysVals
; ++i
)
522 recordLocation(sysvalLocation
, NULL
, &info
->sv
[i
]);
524 if (sysvalLocation
[SV_POSITION
] >= 0x200) {
525 // not assigned by driver, but we need it internally
527 sysvalLocation
[SV_POSITION
] = 0;
531 } // namespace nv50_ir