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 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.
23 #include "codegen/nv50_ir.h"
24 #include "codegen/nv50_ir_build_util.h"
26 #include "codegen/nv50_ir_target_nvc0.h"
27 #include "codegen/nv50_ir_lowering_nvc0.h"
39 #define QUADOP(q, r, s, t) \
40 ((QOP_##q << 6) | (QOP_##r << 4) | \
41 (QOP_##s << 2) | (QOP_##t << 0))
44 NVC0LegalizeSSA::handleDIV(Instruction
*i
)
46 FlowInstruction
*call
;
50 bld
.setPosition(i
, false);
51 def
[0] = bld
.mkMovToReg(0, i
->getSrc(0))->getDef(0);
52 def
[1] = bld
.mkMovToReg(1, i
->getSrc(1))->getDef(0);
54 case TYPE_U32
: builtin
= NVC0_BUILTIN_DIV_U32
; break;
55 case TYPE_S32
: builtin
= NVC0_BUILTIN_DIV_S32
; break;
59 call
= bld
.mkFlow(OP_CALL
, NULL
, CC_ALWAYS
, NULL
);
60 bld
.mkMov(i
->getDef(0), def
[(i
->op
== OP_DIV
) ? 0 : 1]);
61 bld
.mkClobber(FILE_GPR
, (i
->op
== OP_DIV
) ? 0xe : 0xd, 2);
62 bld
.mkClobber(FILE_PREDICATE
, (i
->dType
== TYPE_S32
) ? 0xf : 0x3, 0);
65 call
->absolute
= call
->builtin
= 1;
66 call
->target
.builtin
= builtin
;
67 delete_Instruction(prog
, i
);
71 NVC0LegalizeSSA::handleRCPRSQ(Instruction
*i
)
73 assert(i
->dType
== TYPE_F64
);
74 // There are instructions that will compute the high 32 bits of the 64-bit
75 // float. We will just stick 0 in the bottom 32 bits.
77 bld
.setPosition(i
, false);
79 // 1. Take the source and it up.
80 Value
*src
[2], *dst
[2], *def
= i
->getDef(0);
81 bld
.mkSplit(src
, 4, i
->getSrc(0));
83 // 2. We don't care about the low 32 bits of the destination. Stick a 0 in.
84 dst
[0] = bld
.loadImm(NULL
, 0);
85 dst
[1] = bld
.getSSA();
87 // 3. The new version of the instruction takes the high 32 bits of the
88 // source and outputs the high 32 bits of the destination.
92 i
->subOp
= NV50_IR_SUBOP_RCPRSQ_64H
;
94 // 4. Recombine the two dst pieces back into the original destination.
95 bld
.setPosition(i
, true);
96 bld
.mkOp2(OP_MERGE
, TYPE_U64
, def
, dst
[0], dst
[1]);
100 NVC0LegalizeSSA::handleFTZ(Instruction
*i
)
102 // Only want to flush float inputs
103 assert(i
->sType
== TYPE_F32
);
105 // If we're already flushing denorms (and NaN's) to zero, no need for this.
109 // Only certain classes of operations can flush
110 OpClass cls
= prog
->getTarget()->getOpClass(i
->op
);
111 if (cls
!= OPCLASS_ARITH
&& cls
!= OPCLASS_COMPARE
&&
112 cls
!= OPCLASS_CONVERT
)
119 NVC0LegalizeSSA::visit(Function
*fn
)
121 bld
.setProgram(fn
->getProgram());
126 NVC0LegalizeSSA::visit(BasicBlock
*bb
)
129 for (Instruction
*i
= bb
->getEntry(); i
; i
= next
) {
131 if (i
->sType
== TYPE_F32
) {
132 if (prog
->getType() != Program::TYPE_COMPUTE
)
143 if (i
->dType
== TYPE_F64
)
153 NVC0LegalizePostRA::NVC0LegalizePostRA(const Program
*prog
)
157 needTexBar(prog
->getTarget()->getChipset() >= 0xe0)
162 NVC0LegalizePostRA::insnDominatedBy(const Instruction
*later
,
163 const Instruction
*early
) const
165 if (early
->bb
== later
->bb
)
166 return early
->serial
< later
->serial
;
167 return later
->bb
->dominatedBy(early
->bb
);
171 NVC0LegalizePostRA::addTexUse(std::list
<TexUse
> &uses
,
172 Instruction
*usei
, const Instruction
*texi
)
175 for (std::list
<TexUse
>::iterator it
= uses
.begin();
177 if (insnDominatedBy(usei
, it
->insn
)) {
181 if (insnDominatedBy(it
->insn
, usei
))
187 uses
.push_back(TexUse(usei
, texi
));
190 // While it might be tempting to use the an algorithm that just looks at tex
191 // uses, not all texture results are guaranteed to be used on all paths. In
192 // the case where along some control flow path a texture result is never used,
193 // we might reuse that register for something else, creating a
194 // write-after-write hazard. So we have to manually look through all
195 // instructions looking for ones that reference the registers in question.
197 NVC0LegalizePostRA::findFirstUses(
198 Instruction
*texi
, std::list
<TexUse
> &uses
)
200 int minGPR
= texi
->def(0).rep()->reg
.data
.id
;
201 int maxGPR
= minGPR
+ texi
->def(0).rep()->reg
.size
/ 4 - 1;
203 unordered_set
<const BasicBlock
*> visited
;
204 findFirstUsesBB(minGPR
, maxGPR
, texi
->next
, texi
, uses
, visited
);
208 NVC0LegalizePostRA::findFirstUsesBB(
209 int minGPR
, int maxGPR
, Instruction
*start
,
210 const Instruction
*texi
, std::list
<TexUse
> &uses
,
211 unordered_set
<const BasicBlock
*> &visited
)
213 const BasicBlock
*bb
= start
->bb
;
215 // We don't process the whole bb the first time around. This is correct,
216 // however we might be in a loop and hit this BB again, and need to process
217 // the full thing. So only mark a bb as visited if we processed it from the
219 if (start
== bb
->getEntry()) {
220 if (visited
.find(bb
) != visited
.end())
225 for (Instruction
*insn
= start
; insn
!= bb
->getExit(); insn
= insn
->next
) {
229 for (int d
= 0; insn
->defExists(d
); ++d
) {
230 if (insn
->def(d
).getFile() != FILE_GPR
||
231 insn
->def(d
).rep()->reg
.data
.id
< minGPR
||
232 insn
->def(d
).rep()->reg
.data
.id
> maxGPR
)
234 addTexUse(uses
, insn
, texi
);
238 for (int s
= 0; insn
->srcExists(s
); ++s
) {
239 if (insn
->src(s
).getFile() != FILE_GPR
||
240 insn
->src(s
).rep()->reg
.data
.id
< minGPR
||
241 insn
->src(s
).rep()->reg
.data
.id
> maxGPR
)
243 addTexUse(uses
, insn
, texi
);
248 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next()) {
249 findFirstUsesBB(minGPR
, maxGPR
, BasicBlock::get(ei
.getNode())->getEntry(),
250 texi
, uses
, visited
);
255 // This pass is a bit long and ugly and can probably be optimized.
257 // 1. obtain a list of TEXes and their outputs' first use(s)
258 // 2. calculate the barrier level of each first use (minimal number of TEXes,
259 // over all paths, between the TEX and the use in question)
260 // 3. for each barrier, if all paths from the source TEX to that barrier
261 // contain a barrier of lesser level, it can be culled
263 NVC0LegalizePostRA::insertTextureBarriers(Function
*fn
)
265 std::list
<TexUse
> *uses
;
266 std::vector
<Instruction
*> texes
;
267 std::vector
<int> bbFirstTex
;
268 std::vector
<int> bbFirstUse
;
269 std::vector
<int> texCounts
;
270 std::vector
<TexUse
> useVec
;
273 fn
->orderInstructions(insns
);
275 texCounts
.resize(fn
->allBBlocks
.getSize(), 0);
276 bbFirstTex
.resize(fn
->allBBlocks
.getSize(), insns
.getSize());
277 bbFirstUse
.resize(fn
->allBBlocks
.getSize(), insns
.getSize());
279 // tag BB CFG nodes by their id for later
280 for (ArrayList::Iterator i
= fn
->allBBlocks
.iterator(); !i
.end(); i
.next()) {
281 BasicBlock
*bb
= reinterpret_cast<BasicBlock
*>(i
.get());
283 bb
->cfg
.tag
= bb
->getId();
286 // gather the first uses for each TEX
287 for (int i
= 0; i
< insns
.getSize(); ++i
) {
288 Instruction
*tex
= reinterpret_cast<Instruction
*>(insns
.get(i
));
289 if (isTextureOp(tex
->op
)) {
290 texes
.push_back(tex
);
291 if (!texCounts
.at(tex
->bb
->getId()))
292 bbFirstTex
[tex
->bb
->getId()] = texes
.size() - 1;
293 texCounts
[tex
->bb
->getId()]++;
299 uses
= new std::list
<TexUse
>[texes
.size()];
302 for (size_t i
= 0; i
< texes
.size(); ++i
) {
303 findFirstUses(texes
[i
], uses
[i
]);
306 // determine the barrier level at each use
307 for (size_t i
= 0; i
< texes
.size(); ++i
) {
308 for (std::list
<TexUse
>::iterator u
= uses
[i
].begin(); u
!= uses
[i
].end();
310 BasicBlock
*tb
= texes
[i
]->bb
;
311 BasicBlock
*ub
= u
->insn
->bb
;
314 for (size_t j
= i
+ 1; j
< texes
.size() &&
315 texes
[j
]->bb
== tb
&& texes
[j
]->serial
< u
->insn
->serial
;
319 u
->level
= fn
->cfg
.findLightestPathWeight(&tb
->cfg
,
320 &ub
->cfg
, texCounts
);
322 WARN("Failed to find path TEX -> TEXBAR\n");
326 // this counted all TEXes in the origin block, correct that
327 u
->level
-= i
- bbFirstTex
.at(tb
->getId()) + 1 /* this TEX */;
328 // and did not count the TEXes in the destination block, add those
329 for (size_t j
= bbFirstTex
.at(ub
->getId()); j
< texes
.size() &&
330 texes
[j
]->bb
== ub
&& texes
[j
]->serial
< u
->insn
->serial
;
334 assert(u
->level
>= 0);
335 useVec
.push_back(*u
);
340 // insert the barriers
341 for (size_t i
= 0; i
< useVec
.size(); ++i
) {
342 Instruction
*prev
= useVec
[i
].insn
->prev
;
343 if (useVec
[i
].level
< 0)
345 if (prev
&& prev
->op
== OP_TEXBAR
) {
346 if (prev
->subOp
> useVec
[i
].level
)
347 prev
->subOp
= useVec
[i
].level
;
348 prev
->setSrc(prev
->srcCount(), useVec
[i
].tex
->getDef(0));
350 Instruction
*bar
= new_Instruction(func
, OP_TEXBAR
, TYPE_NONE
);
352 bar
->subOp
= useVec
[i
].level
;
353 // make use explicit to ease latency calculation
354 bar
->setSrc(bar
->srcCount(), useVec
[i
].tex
->getDef(0));
355 useVec
[i
].insn
->bb
->insertBefore(useVec
[i
].insn
, bar
);
359 if (fn
->getProgram()->optLevel
< 3)
362 std::vector
<Limits
> limitT
, limitB
, limitS
; // entry, exit, single
364 limitT
.resize(fn
->allBBlocks
.getSize(), Limits(0, 0));
365 limitB
.resize(fn
->allBBlocks
.getSize(), Limits(0, 0));
366 limitS
.resize(fn
->allBBlocks
.getSize());
368 // cull unneeded barriers (should do that earlier, but for simplicity)
369 IteratorRef bi
= fn
->cfg
.iteratorCFG();
370 // first calculate min/max outstanding TEXes for each BB
371 for (bi
->reset(); !bi
->end(); bi
->next()) {
372 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(bi
->get());
373 BasicBlock
*bb
= BasicBlock::get(n
);
375 int max
= std::numeric_limits
<int>::max();
376 for (Instruction
*i
= bb
->getFirst(); i
; i
= i
->next
) {
377 if (isTextureOp(i
->op
)) {
379 if (max
< std::numeric_limits
<int>::max())
382 if (i
->op
== OP_TEXBAR
) {
383 min
= MIN2(min
, i
->subOp
);
384 max
= MIN2(max
, i
->subOp
);
387 // limits when looking at an isolated block
388 limitS
[bb
->getId()].min
= min
;
389 limitS
[bb
->getId()].max
= max
;
391 // propagate the min/max values
392 for (unsigned int l
= 0; l
<= fn
->loopNestingBound
; ++l
) {
393 for (bi
->reset(); !bi
->end(); bi
->next()) {
394 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(bi
->get());
395 BasicBlock
*bb
= BasicBlock::get(n
);
396 const int bbId
= bb
->getId();
397 for (Graph::EdgeIterator ei
= n
->incident(); !ei
.end(); ei
.next()) {
398 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
399 const int inId
= in
->getId();
400 limitT
[bbId
].min
= MAX2(limitT
[bbId
].min
, limitB
[inId
].min
);
401 limitT
[bbId
].max
= MAX2(limitT
[bbId
].max
, limitB
[inId
].max
);
403 // I just hope this is correct ...
404 if (limitS
[bbId
].max
== std::numeric_limits
<int>::max()) {
406 limitB
[bbId
].min
= limitT
[bbId
].min
+ limitS
[bbId
].min
;
407 limitB
[bbId
].max
= limitT
[bbId
].max
+ limitS
[bbId
].min
;
409 // block contained a barrier
410 limitB
[bbId
].min
= MIN2(limitS
[bbId
].max
,
411 limitT
[bbId
].min
+ limitS
[bbId
].min
);
412 limitB
[bbId
].max
= MIN2(limitS
[bbId
].max
,
413 limitT
[bbId
].max
+ limitS
[bbId
].min
);
417 // finally delete unnecessary barriers
418 for (bi
->reset(); !bi
->end(); bi
->next()) {
419 Graph::Node
*n
= reinterpret_cast<Graph::Node
*>(bi
->get());
420 BasicBlock
*bb
= BasicBlock::get(n
);
421 Instruction
*prev
= NULL
;
423 int max
= limitT
[bb
->getId()].max
;
424 for (Instruction
*i
= bb
->getFirst(); i
; i
= next
) {
426 if (i
->op
== OP_TEXBAR
) {
427 if (i
->subOp
>= max
) {
428 delete_Instruction(prog
, i
);
432 if (prev
&& prev
->op
== OP_TEXBAR
&& prev
->subOp
>= max
) {
433 delete_Instruction(prog
, prev
);
438 if (isTextureOp(i
->op
)) {
441 if (i
&& !i
->isNop())
449 NVC0LegalizePostRA::visit(Function
*fn
)
452 insertTextureBarriers(fn
);
454 rZero
= new_LValue(fn
, FILE_GPR
);
455 pOne
= new_LValue(fn
, FILE_PREDICATE
);
456 carry
= new_LValue(fn
, FILE_FLAGS
);
458 rZero
->reg
.data
.id
= prog
->getTarget()->getFileSize(FILE_GPR
);
459 carry
->reg
.data
.id
= 0;
460 pOne
->reg
.data
.id
= 7;
466 NVC0LegalizePostRA::replaceZero(Instruction
*i
)
468 for (int s
= 0; i
->srcExists(s
); ++s
) {
469 if (s
== 2 && i
->op
== OP_SUCLAMP
)
471 ImmediateValue
*imm
= i
->getSrc(s
)->asImm();
473 if (i
->op
== OP_SELP
&& s
== 2) {
475 if (imm
->reg
.data
.u64
== 0)
476 i
->src(s
).mod
= i
->src(s
).mod
^ Modifier(NV50_IR_MOD_NOT
);
477 } else if (imm
->reg
.data
.u64
== 0) {
484 // replace CONT with BRA for single unconditional continue
486 NVC0LegalizePostRA::tryReplaceContWithBra(BasicBlock
*bb
)
488 if (bb
->cfg
.incidentCount() != 2 || bb
->getEntry()->op
!= OP_PRECONT
)
490 Graph::EdgeIterator ei
= bb
->cfg
.incident();
491 if (ei
.getType() != Graph::Edge::BACK
)
493 if (ei
.getType() != Graph::Edge::BACK
)
495 BasicBlock
*contBB
= BasicBlock::get(ei
.getNode());
497 if (!contBB
->getExit() || contBB
->getExit()->op
!= OP_CONT
||
498 contBB
->getExit()->getPredicate())
500 contBB
->getExit()->op
= OP_BRA
;
501 bb
->remove(bb
->getEntry()); // delete PRECONT
504 assert(ei
.end() || ei
.getType() != Graph::Edge::BACK
);
508 // replace branches to join blocks with join ops
510 NVC0LegalizePostRA::propagateJoin(BasicBlock
*bb
)
512 if (bb
->getEntry()->op
!= OP_JOIN
|| bb
->getEntry()->asFlow()->limit
)
514 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next()) {
515 BasicBlock
*in
= BasicBlock::get(ei
.getNode());
516 Instruction
*exit
= in
->getExit();
518 in
->insertTail(new FlowInstruction(func
, OP_JOIN
, bb
));
519 // there should always be a terminator instruction
520 WARN("inserted missing terminator in BB:%i\n", in
->getId());
522 if (exit
->op
== OP_BRA
) {
524 exit
->asFlow()->limit
= 1; // must-not-propagate marker
527 bb
->remove(bb
->getEntry());
531 NVC0LegalizePostRA::visit(BasicBlock
*bb
)
533 Instruction
*i
, *next
;
535 // remove pseudo operations and non-fixed no-ops, split 64 bit operations
536 for (i
= bb
->getFirst(); i
; i
= next
) {
538 if (i
->op
== OP_EMIT
|| i
->op
== OP_RESTART
) {
539 if (!i
->getDef(0)->refCount())
541 if (i
->src(0).getFile() == FILE_IMMEDIATE
)
542 i
->setSrc(0, rZero
); // initial value must be 0
548 if (i
->op
== OP_BAR
&& i
->subOp
== NV50_IR_SUBOP_BAR_SYNC
&&
549 prog
->getType() != Program::TYPE_COMPUTE
) {
550 // It seems like barriers are never required for tessellation since
551 // the warp size is 32, and there are always at most 32 tcs threads.
554 if (i
->op
== OP_LOAD
&& i
->subOp
== NV50_IR_SUBOP_LDC_IS
) {
555 int offset
= i
->src(0).get()->reg
.data
.offset
;
556 if (abs(offset
) > 0x10000)
557 i
->src(0).get()->reg
.fileIndex
+= offset
>> 16;
558 i
->src(0).get()->reg
.data
.offset
= (int)(short)offset
;
560 // TODO: Move this to before register allocation for operations that
561 // need the $c register !
562 if (typeSizeof(i
->dType
) == 8) {
564 hi
= BuildUtil::split64BitOpPostRA(func
, i
, rZero
, carry
);
569 if (i
->op
!= OP_MOV
&& i
->op
!= OP_PFETCH
)
576 if (!tryReplaceContWithBra(bb
))
582 NVC0LoweringPass::NVC0LoweringPass(Program
*prog
) : targ(prog
->getTarget())
584 bld
.setProgram(prog
);
589 NVC0LoweringPass::visit(Function
*fn
)
591 if (prog
->getType() == Program::TYPE_GEOMETRY
) {
592 assert(!strncmp(fn
->getName(), "MAIN", 4));
593 // TODO: when we generate actual functions pass this value along somehow
594 bld
.setPosition(BasicBlock::get(fn
->cfg
.getRoot()), false);
595 gpEmitAddress
= bld
.loadImm(NULL
, 0)->asLValue();
597 bld
.setPosition(BasicBlock::get(fn
->cfgExit
)->getExit(), false);
598 bld
.mkMovToReg(0, gpEmitAddress
);
605 NVC0LoweringPass::visit(BasicBlock
*bb
)
611 NVC0LoweringPass::loadTexHandle(Value
*ptr
, unsigned int slot
)
613 uint8_t b
= prog
->driver
->io
.auxCBSlot
;
614 uint32_t off
= prog
->driver
->io
.texBindBase
+ slot
* 4;
616 mkLoadv(TYPE_U32
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U32
, off
), ptr
);
619 // move array source to first slot, convert to u16, add indirections
621 NVC0LoweringPass::handleTEX(TexInstruction
*i
)
623 const int dim
= i
->tex
.target
.getDim() + i
->tex
.target
.isCube();
624 const int arg
= i
->tex
.target
.getArgCount();
625 const int lyr
= arg
- (i
->tex
.target
.isMS() ? 2 : 1);
626 const int chipset
= prog
->getTarget()->getChipset();
628 /* Only normalize in the non-explicit derivatives case. For explicit
629 * derivatives, this is handled in handleManualTXD.
631 if (i
->tex
.target
.isCube() && i
->dPdx
[0].get() == NULL
) {
634 for (c
= 0; c
< 3; ++c
)
635 src
[c
] = bld
.mkOp1v(OP_ABS
, TYPE_F32
, bld
.getSSA(), i
->getSrc(c
));
636 val
= bld
.getScratch();
637 bld
.mkOp2(OP_MAX
, TYPE_F32
, val
, src
[0], src
[1]);
638 bld
.mkOp2(OP_MAX
, TYPE_F32
, val
, src
[2], val
);
639 bld
.mkOp1(OP_RCP
, TYPE_F32
, val
, val
);
640 for (c
= 0; c
< 3; ++c
) {
641 i
->setSrc(c
, bld
.mkOp2v(OP_MUL
, TYPE_F32
, bld
.getSSA(),
646 // Arguments to the TEX instruction are a little insane. Even though the
647 // encoding is identical between SM20 and SM30, the arguments mean
648 // different things between Fermi and Kepler+. A lot of arguments are
649 // optional based on flags passed to the instruction. This summarizes the
659 // - tg4: 8 bits each, either 2 (1 offset reg) or 8 (2 offset reg)
660 // - other: 4 bits each, single reg
664 // array (+ offsets for txd in upper 16 bits)
669 // offsets (same as fermi, except txd which takes it with array)
686 if (chipset
>= NVISA_GK104_CHIPSET
) {
687 if (i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0) {
688 // XXX this ignores tsc, and assumes a 1:1 mapping
689 assert(i
->tex
.rIndirectSrc
>= 0);
690 Value
*hnd
= loadTexHandle(
691 bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(),
692 i
->getIndirectR(), bld
.mkImm(2)),
696 i
->setIndirectR(hnd
);
697 i
->setIndirectS(NULL
);
698 } else if (i
->tex
.r
== i
->tex
.s
|| i
->op
== OP_TXF
) {
699 i
->tex
.r
+= prog
->driver
->io
.texBindBase
/ 4;
700 i
->tex
.s
= 0; // only a single cX[] value possible here
702 Value
*hnd
= bld
.getScratch();
703 Value
*rHnd
= loadTexHandle(NULL
, i
->tex
.r
);
704 Value
*sHnd
= loadTexHandle(NULL
, i
->tex
.s
);
706 bld
.mkOp3(OP_INSBF
, TYPE_U32
, hnd
, rHnd
, bld
.mkImm(0x1400), sHnd
);
708 i
->tex
.r
= 0; // not used for indirect tex
710 i
->setIndirectR(hnd
);
712 if (i
->tex
.target
.isArray()) {
713 LValue
*layer
= new_LValue(func
, FILE_GPR
);
714 Value
*src
= i
->getSrc(lyr
);
715 const int sat
= (i
->op
== OP_TXF
) ? 1 : 0;
716 DataType sTy
= (i
->op
== OP_TXF
) ? TYPE_U32
: TYPE_F32
;
717 bld
.mkCvt(OP_CVT
, TYPE_U16
, layer
, sTy
, src
)->saturate
= sat
;
718 if (i
->op
!= OP_TXD
|| chipset
< NVISA_GM107_CHIPSET
) {
719 for (int s
= dim
; s
>= 1; --s
)
720 i
->setSrc(s
, i
->getSrc(s
- 1));
723 i
->setSrc(dim
, layer
);
726 // Move the indirect reference to the first place
727 if (i
->tex
.rIndirectSrc
>= 0 && (
728 i
->op
== OP_TXD
|| chipset
< NVISA_GM107_CHIPSET
)) {
729 Value
*hnd
= i
->getIndirectR();
731 i
->setIndirectR(NULL
);
732 i
->moveSources(0, 1);
734 i
->tex
.rIndirectSrc
= 0;
735 i
->tex
.sIndirectSrc
= -1;
738 // (nvc0) generate and move the tsc/tic/array source to the front
739 if (i
->tex
.target
.isArray() || i
->tex
.rIndirectSrc
>= 0 || i
->tex
.sIndirectSrc
>= 0) {
740 LValue
*src
= new_LValue(func
, FILE_GPR
); // 0xttxsaaaa
742 Value
*ticRel
= i
->getIndirectR();
743 Value
*tscRel
= i
->getIndirectS();
746 i
->setSrc(i
->tex
.rIndirectSrc
, NULL
);
748 ticRel
= bld
.mkOp2v(OP_ADD
, TYPE_U32
, bld
.getScratch(),
749 ticRel
, bld
.mkImm(i
->tex
.r
));
752 i
->setSrc(i
->tex
.sIndirectSrc
, NULL
);
754 tscRel
= bld
.mkOp2v(OP_ADD
, TYPE_U32
, bld
.getScratch(),
755 tscRel
, bld
.mkImm(i
->tex
.s
));
758 Value
*arrayIndex
= i
->tex
.target
.isArray() ? i
->getSrc(lyr
) : NULL
;
760 for (int s
= dim
; s
>= 1; --s
)
761 i
->setSrc(s
, i
->getSrc(s
- 1));
762 i
->setSrc(0, arrayIndex
);
764 i
->moveSources(0, 1);
768 int sat
= (i
->op
== OP_TXF
) ? 1 : 0;
769 DataType sTy
= (i
->op
== OP_TXF
) ? TYPE_U32
: TYPE_F32
;
770 bld
.mkCvt(OP_CVT
, TYPE_U16
, src
, sTy
, arrayIndex
)->saturate
= sat
;
776 bld
.mkOp3(OP_INSBF
, TYPE_U32
, src
, ticRel
, bld
.mkImm(0x0917), src
);
778 bld
.mkOp3(OP_INSBF
, TYPE_U32
, src
, tscRel
, bld
.mkImm(0x0710), src
);
783 // For nvc0, the sample id has to be in the second operand, as the offset
784 // does. Right now we don't know how to pass both in, and this case can't
785 // happen with OpenGL. On nve0, the sample id is part of the texture
786 // coordinate argument.
787 assert(chipset
>= NVISA_GK104_CHIPSET
||
788 !i
->tex
.useOffsets
|| !i
->tex
.target
.isMS());
790 // offset is between lod and dc
791 if (i
->tex
.useOffsets
) {
793 int s
= i
->srcCount(0xff, true);
794 if (i
->op
!= OP_TXD
|| chipset
< NVISA_GK104_CHIPSET
) {
795 if (i
->tex
.target
.isShadow())
797 if (i
->srcExists(s
)) // move potential predicate out of the way
798 i
->moveSources(s
, 1);
799 if (i
->tex
.useOffsets
== 4 && i
->srcExists(s
+ 1))
800 i
->moveSources(s
+ 1, 1);
802 if (i
->op
== OP_TXG
) {
803 // Either there is 1 offset, which goes into the 2 low bytes of the
804 // first source, or there are 4 offsets, which go into 2 sources (8
805 // values, 1 byte each).
806 Value
*offs
[2] = {NULL
, NULL
};
807 for (n
= 0; n
< i
->tex
.useOffsets
; n
++) {
808 for (c
= 0; c
< 2; ++c
) {
809 if ((n
% 2) == 0 && c
== 0)
810 offs
[n
/ 2] = i
->offset
[n
][c
].get();
812 bld
.mkOp3(OP_INSBF
, TYPE_U32
,
814 i
->offset
[n
][c
].get(),
815 bld
.mkImm(0x800 | ((n
* 16 + c
* 8) % 32)),
819 i
->setSrc(s
, offs
[0]);
821 i
->setSrc(s
+ 1, offs
[1]);
824 assert(i
->tex
.useOffsets
== 1);
825 for (c
= 0; c
< 3; ++c
) {
827 if (!i
->offset
[0][c
].getImmediate(val
))
828 assert(!"non-immediate offset passed to non-TXG");
829 imm
|= (val
.reg
.data
.u32
& 0xf) << (c
* 4);
831 if (i
->op
== OP_TXD
&& chipset
>= NVISA_GK104_CHIPSET
) {
832 // The offset goes into the upper 16 bits of the array index. So
833 // create it if it's not already there, and INSBF it if it already
835 s
= (i
->tex
.rIndirectSrc
>= 0) ? 1 : 0;
836 if (chipset
>= NVISA_GM107_CHIPSET
)
838 if (i
->tex
.target
.isArray()) {
839 bld
.mkOp3(OP_INSBF
, TYPE_U32
, i
->getSrc(s
),
840 bld
.loadImm(NULL
, imm
), bld
.mkImm(0xc10),
843 i
->moveSources(s
, 1);
844 i
->setSrc(s
, bld
.loadImm(NULL
, imm
<< 16));
847 i
->setSrc(s
, bld
.loadImm(NULL
, imm
));
852 if (chipset
>= NVISA_GK104_CHIPSET
) {
854 // If TEX requires more than 4 sources, the 2nd register tuple must be
855 // aligned to 4, even if it consists of just a single 4-byte register.
857 // XXX HACK: We insert 0 sources to avoid the 5 or 6 regs case.
859 int s
= i
->srcCount(0xff, true);
860 if (s
> 4 && s
< 7) {
861 if (i
->srcExists(s
)) // move potential predicate out of the way
862 i
->moveSources(s
, 7 - s
);
864 i
->setSrc(s
++, bld
.loadImm(NULL
, 0));
872 NVC0LoweringPass::handleManualTXD(TexInstruction
*i
)
874 static const uint8_t qOps
[4][2] =
876 { QUADOP(MOV2
, ADD
, MOV2
, ADD
), QUADOP(MOV2
, MOV2
, ADD
, ADD
) }, // l0
877 { QUADOP(SUBR
, MOV2
, SUBR
, MOV2
), QUADOP(MOV2
, MOV2
, ADD
, ADD
) }, // l1
878 { QUADOP(MOV2
, ADD
, MOV2
, ADD
), QUADOP(SUBR
, SUBR
, MOV2
, MOV2
) }, // l2
879 { QUADOP(SUBR
, MOV2
, SUBR
, MOV2
), QUADOP(SUBR
, SUBR
, MOV2
, MOV2
) }, // l3
884 Value
*zero
= bld
.loadImm(bld
.getSSA(), 0);
886 const int dim
= i
->tex
.target
.getDim() + i
->tex
.target
.isCube();
888 // This function is invoked after handleTEX lowering, so we have to expect
889 // the arguments in the order that the hw wants them. For Fermi, array and
890 // indirect are both in the leading arg, while for Kepler, array and
891 // indirect are separate (and both precede the coordinates). Maxwell is
892 // handled in a separate function.
894 if (targ
->getChipset() < NVISA_GK104_CHIPSET
)
895 array
= i
->tex
.target
.isArray() || i
->tex
.rIndirectSrc
>= 0;
897 array
= i
->tex
.target
.isArray() + (i
->tex
.rIndirectSrc
>= 0);
899 i
->op
= OP_TEX
; // no need to clone dPdx/dPdy later
901 for (c
= 0; c
< dim
; ++c
)
902 crd
[c
] = bld
.getScratch();
904 bld
.mkOp(OP_QUADON
, TYPE_NONE
, NULL
);
905 for (l
= 0; l
< 4; ++l
) {
907 // mov coordinates from lane l to all lanes
908 for (c
= 0; c
< dim
; ++c
)
909 bld
.mkQuadop(0x00, crd
[c
], l
, i
->getSrc(c
+ array
), zero
);
910 // add dPdx from lane l to lanes dx
911 for (c
= 0; c
< dim
; ++c
)
912 bld
.mkQuadop(qOps
[l
][0], crd
[c
], l
, i
->dPdx
[c
].get(), crd
[c
]);
913 // add dPdy from lane l to lanes dy
914 for (c
= 0; c
< dim
; ++c
)
915 bld
.mkQuadop(qOps
[l
][1], crd
[c
], l
, i
->dPdy
[c
].get(), crd
[c
]);
916 // normalize cube coordinates
917 if (i
->tex
.target
.isCube()) {
918 for (c
= 0; c
< 3; ++c
)
919 src
[c
] = bld
.mkOp1v(OP_ABS
, TYPE_F32
, bld
.getSSA(), crd
[c
]);
920 val
= bld
.getScratch();
921 bld
.mkOp2(OP_MAX
, TYPE_F32
, val
, src
[0], src
[1]);
922 bld
.mkOp2(OP_MAX
, TYPE_F32
, val
, src
[2], val
);
923 bld
.mkOp1(OP_RCP
, TYPE_F32
, val
, val
);
924 for (c
= 0; c
< 3; ++c
)
925 src
[c
] = bld
.mkOp2v(OP_MUL
, TYPE_F32
, bld
.getSSA(), crd
[c
], val
);
927 for (c
= 0; c
< dim
; ++c
)
931 bld
.insert(tex
= cloneForward(func
, i
));
932 for (c
= 0; c
< dim
; ++c
)
933 tex
->setSrc(c
+ array
, src
[c
]);
935 for (c
= 0; i
->defExists(c
); ++c
) {
937 def
[c
][l
] = bld
.getSSA();
938 mov
= bld
.mkMov(def
[c
][l
], tex
->getDef(c
));
943 bld
.mkOp(OP_QUADPOP
, TYPE_NONE
, NULL
);
945 for (c
= 0; i
->defExists(c
); ++c
) {
946 Instruction
*u
= bld
.mkOp(OP_UNION
, TYPE_U32
, i
->getDef(c
));
947 for (l
= 0; l
< 4; ++l
)
948 u
->setSrc(l
, def
[c
][l
]);
956 NVC0LoweringPass::handleTXD(TexInstruction
*txd
)
958 int dim
= txd
->tex
.target
.getDim() + txd
->tex
.target
.isCube();
959 unsigned arg
= txd
->tex
.target
.getArgCount();
960 unsigned expected_args
= arg
;
961 const int chipset
= prog
->getTarget()->getChipset();
963 if (chipset
>= NVISA_GK104_CHIPSET
) {
964 if (!txd
->tex
.target
.isArray() && txd
->tex
.useOffsets
)
966 if (txd
->tex
.rIndirectSrc
>= 0 || txd
->tex
.sIndirectSrc
>= 0)
969 if (txd
->tex
.useOffsets
)
971 if (!txd
->tex
.target
.isArray() && (
972 txd
->tex
.rIndirectSrc
>= 0 || txd
->tex
.sIndirectSrc
>= 0))
976 if (expected_args
> 4 ||
978 txd
->tex
.target
.isShadow())
982 while (txd
->srcExists(arg
))
985 txd
->tex
.derivAll
= true;
986 if (txd
->op
== OP_TEX
)
987 return handleManualTXD(txd
);
989 assert(arg
== expected_args
);
990 for (int c
= 0; c
< dim
; ++c
) {
991 txd
->setSrc(arg
+ c
* 2 + 0, txd
->dPdx
[c
]);
992 txd
->setSrc(arg
+ c
* 2 + 1, txd
->dPdy
[c
]);
993 txd
->dPdx
[c
].set(NULL
);
994 txd
->dPdy
[c
].set(NULL
);
997 // In this case we have fewer than 4 "real" arguments, which means that
998 // handleTEX didn't apply any padding. However we have to make sure that
999 // the second "group" of arguments still gets padded up to 4.
1000 if (chipset
>= NVISA_GK104_CHIPSET
) {
1001 int s
= arg
+ 2 * dim
;
1002 if (s
>= 4 && s
< 7) {
1003 if (txd
->srcExists(s
)) // move potential predicate out of the way
1004 txd
->moveSources(s
, 7 - s
);
1006 txd
->setSrc(s
++, bld
.loadImm(NULL
, 0));
1014 NVC0LoweringPass::handleTXQ(TexInstruction
*txq
)
1016 const int chipset
= prog
->getTarget()->getChipset();
1017 if (chipset
>= NVISA_GK104_CHIPSET
&& txq
->tex
.rIndirectSrc
< 0)
1018 txq
->tex
.r
+= prog
->driver
->io
.texBindBase
/ 4;
1020 if (txq
->tex
.rIndirectSrc
< 0)
1023 Value
*ticRel
= txq
->getIndirectR();
1025 txq
->setIndirectS(NULL
);
1026 txq
->tex
.sIndirectSrc
= -1;
1030 if (chipset
< NVISA_GK104_CHIPSET
) {
1031 LValue
*src
= new_LValue(func
, FILE_GPR
); // 0xttxsaaaa
1033 txq
->setSrc(txq
->tex
.rIndirectSrc
, NULL
);
1035 ticRel
= bld
.mkOp2v(OP_ADD
, TYPE_U32
, bld
.getScratch(),
1036 ticRel
, bld
.mkImm(txq
->tex
.r
));
1038 bld
.mkOp2(OP_SHL
, TYPE_U32
, src
, ticRel
, bld
.mkImm(0x17));
1040 txq
->moveSources(0, 1);
1041 txq
->setSrc(0, src
);
1043 Value
*hnd
= loadTexHandle(
1044 bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(),
1045 txq
->getIndirectR(), bld
.mkImm(2)),
1050 txq
->setIndirectR(NULL
);
1051 txq
->moveSources(0, 1);
1052 txq
->setSrc(0, hnd
);
1053 txq
->tex
.rIndirectSrc
= 0;
1060 NVC0LoweringPass::handleTXLQ(TexInstruction
*i
)
1062 /* The outputs are inverted compared to what the TGSI instruction
1063 * expects. Take that into account in the mask.
1065 assert((i
->tex
.mask
& ~3) == 0);
1066 if (i
->tex
.mask
== 1)
1068 else if (i
->tex
.mask
== 2)
1071 bld
.setPosition(i
, true);
1073 /* The returned values are not quite what we want:
1074 * (a) convert from s16/u16 to f32
1075 * (b) multiply by 1/256
1077 for (int def
= 0; def
< 2; ++def
) {
1078 if (!i
->defExists(def
))
1080 enum DataType type
= TYPE_S16
;
1081 if (i
->tex
.mask
== 2 || def
> 0)
1083 bld
.mkCvt(OP_CVT
, TYPE_F32
, i
->getDef(def
), type
, i
->getDef(def
));
1084 bld
.mkOp2(OP_MUL
, TYPE_F32
, i
->getDef(def
),
1085 i
->getDef(def
), bld
.loadImm(NULL
, 1.0f
/ 256));
1087 if (i
->tex
.mask
== 3) {
1088 LValue
*t
= new_LValue(func
, FILE_GPR
);
1089 bld
.mkMov(t
, i
->getDef(0));
1090 bld
.mkMov(i
->getDef(0), i
->getDef(1));
1091 bld
.mkMov(i
->getDef(1), t
);
1097 NVC0LoweringPass::handleBUFQ(Instruction
*bufq
)
1100 bufq
->setSrc(0, loadBufLength32(bufq
->getIndirect(0, 1),
1101 bufq
->getSrc(0)->reg
.fileIndex
* 16));
1102 bufq
->setIndirect(0, 0, NULL
);
1103 bufq
->setIndirect(0, 1, NULL
);
1108 NVC0LoweringPass::handleSharedATOMNVE4(Instruction
*atom
)
1110 assert(atom
->src(0).getFile() == FILE_MEMORY_SHARED
);
1112 BasicBlock
*currBB
= atom
->bb
;
1113 BasicBlock
*tryLockBB
= atom
->bb
->splitBefore(atom
, false);
1114 BasicBlock
*joinBB
= atom
->bb
->splitAfter(atom
);
1115 BasicBlock
*setAndUnlockBB
= new BasicBlock(func
);
1116 BasicBlock
*failLockBB
= new BasicBlock(func
);
1118 bld
.setPosition(currBB
, true);
1119 assert(!currBB
->joinAt
);
1120 currBB
->joinAt
= bld
.mkFlow(OP_JOINAT
, joinBB
, CC_ALWAYS
, NULL
);
1122 CmpInstruction
*pred
=
1123 bld
.mkCmp(OP_SET
, CC_EQ
, TYPE_U32
, bld
.getSSA(1, FILE_PREDICATE
),
1124 TYPE_U32
, bld
.mkImm(0), bld
.mkImm(1));
1126 bld
.mkFlow(OP_BRA
, tryLockBB
, CC_ALWAYS
, NULL
);
1127 currBB
->cfg
.attach(&tryLockBB
->cfg
, Graph::Edge::TREE
);
1129 bld
.setPosition(tryLockBB
, true);
1132 bld
.mkLoad(TYPE_U32
, atom
->getDef(0), atom
->getSrc(0)->asSym(),
1133 atom
->getIndirect(0, 0));
1134 ld
->setDef(1, bld
.getSSA(1, FILE_PREDICATE
));
1135 ld
->subOp
= NV50_IR_SUBOP_LOAD_LOCKED
;
1137 bld
.mkFlow(OP_BRA
, setAndUnlockBB
, CC_P
, ld
->getDef(1));
1138 bld
.mkFlow(OP_BRA
, failLockBB
, CC_ALWAYS
, NULL
);
1139 tryLockBB
->cfg
.attach(&failLockBB
->cfg
, Graph::Edge::CROSS
);
1140 tryLockBB
->cfg
.attach(&setAndUnlockBB
->cfg
, Graph::Edge::TREE
);
1142 tryLockBB
->cfg
.detach(&joinBB
->cfg
);
1145 bld
.setPosition(setAndUnlockBB
, true);
1147 if (atom
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
) {
1148 // Read the old value, and write the new one.
1149 stVal
= atom
->getSrc(1);
1150 } else if (atom
->subOp
== NV50_IR_SUBOP_ATOM_CAS
) {
1151 CmpInstruction
*set
=
1152 bld
.mkCmp(OP_SET
, CC_EQ
, TYPE_U32
, bld
.getSSA(),
1153 TYPE_U32
, ld
->getDef(0), atom
->getSrc(1));
1155 bld
.mkCmp(OP_SLCT
, CC_NE
, TYPE_U32
, (stVal
= bld
.getSSA()),
1156 TYPE_U32
, atom
->getSrc(2), ld
->getDef(0), set
->getDef(0));
1160 switch (atom
->subOp
) {
1161 case NV50_IR_SUBOP_ATOM_ADD
:
1164 case NV50_IR_SUBOP_ATOM_AND
:
1167 case NV50_IR_SUBOP_ATOM_OR
:
1170 case NV50_IR_SUBOP_ATOM_XOR
:
1173 case NV50_IR_SUBOP_ATOM_MIN
:
1176 case NV50_IR_SUBOP_ATOM_MAX
:
1184 stVal
= bld
.mkOp2v(op
, atom
->dType
, bld
.getSSA(), ld
->getDef(0),
1189 bld
.mkStore(OP_STORE
, TYPE_U32
, atom
->getSrc(0)->asSym(),
1190 atom
->getIndirect(0, 0), stVal
);
1191 st
->setDef(0, pred
->getDef(0));
1192 st
->subOp
= NV50_IR_SUBOP_STORE_UNLOCKED
;
1194 bld
.mkFlow(OP_BRA
, failLockBB
, CC_ALWAYS
, NULL
);
1195 setAndUnlockBB
->cfg
.attach(&failLockBB
->cfg
, Graph::Edge::TREE
);
1197 // Lock until the store has not been performed.
1198 bld
.setPosition(failLockBB
, true);
1199 bld
.mkFlow(OP_BRA
, tryLockBB
, CC_NOT_P
, pred
->getDef(0));
1200 bld
.mkFlow(OP_BRA
, joinBB
, CC_ALWAYS
, NULL
);
1201 failLockBB
->cfg
.attach(&tryLockBB
->cfg
, Graph::Edge::BACK
);
1202 failLockBB
->cfg
.attach(&joinBB
->cfg
, Graph::Edge::TREE
);
1204 bld
.setPosition(joinBB
, false);
1205 bld
.mkFlow(OP_JOIN
, NULL
, CC_ALWAYS
, NULL
)->fixed
= 1;
1209 NVC0LoweringPass::handleSharedATOM(Instruction
*atom
)
1211 assert(atom
->src(0).getFile() == FILE_MEMORY_SHARED
);
1213 BasicBlock
*currBB
= atom
->bb
;
1214 BasicBlock
*tryLockAndSetBB
= atom
->bb
->splitBefore(atom
, false);
1215 BasicBlock
*joinBB
= atom
->bb
->splitAfter(atom
);
1217 bld
.setPosition(currBB
, true);
1218 assert(!currBB
->joinAt
);
1219 currBB
->joinAt
= bld
.mkFlow(OP_JOINAT
, joinBB
, CC_ALWAYS
, NULL
);
1221 bld
.mkFlow(OP_BRA
, tryLockAndSetBB
, CC_ALWAYS
, NULL
);
1222 currBB
->cfg
.attach(&tryLockAndSetBB
->cfg
, Graph::Edge::TREE
);
1224 bld
.setPosition(tryLockAndSetBB
, true);
1227 bld
.mkLoad(TYPE_U32
, atom
->getDef(0), atom
->getSrc(0)->asSym(),
1228 atom
->getIndirect(0, 0));
1229 ld
->setDef(1, bld
.getSSA(1, FILE_PREDICATE
));
1230 ld
->subOp
= NV50_IR_SUBOP_LOAD_LOCKED
;
1233 if (atom
->subOp
== NV50_IR_SUBOP_ATOM_EXCH
) {
1234 // Read the old value, and write the new one.
1235 stVal
= atom
->getSrc(1);
1236 } else if (atom
->subOp
== NV50_IR_SUBOP_ATOM_CAS
) {
1237 CmpInstruction
*set
=
1238 bld
.mkCmp(OP_SET
, CC_EQ
, TYPE_U32
, bld
.getSSA(1, FILE_PREDICATE
),
1239 TYPE_U32
, ld
->getDef(0), atom
->getSrc(1));
1240 set
->setPredicate(CC_P
, ld
->getDef(1));
1243 bld
.mkOp3(OP_SELP
, TYPE_U32
, bld
.getSSA(), ld
->getDef(0),
1244 atom
->getSrc(2), set
->getDef(0));
1245 selp
->src(2).mod
= Modifier(NV50_IR_MOD_NOT
);
1246 selp
->setPredicate(CC_P
, ld
->getDef(1));
1248 stVal
= selp
->getDef(0);
1252 switch (atom
->subOp
) {
1253 case NV50_IR_SUBOP_ATOM_ADD
:
1256 case NV50_IR_SUBOP_ATOM_AND
:
1259 case NV50_IR_SUBOP_ATOM_OR
:
1262 case NV50_IR_SUBOP_ATOM_XOR
:
1265 case NV50_IR_SUBOP_ATOM_MIN
:
1268 case NV50_IR_SUBOP_ATOM_MAX
:
1277 bld
.mkOp2(op
, atom
->dType
, bld
.getSSA(), ld
->getDef(0),
1279 i
->setPredicate(CC_P
, ld
->getDef(1));
1281 stVal
= i
->getDef(0);
1285 bld
.mkStore(OP_STORE
, TYPE_U32
, atom
->getSrc(0)->asSym(),
1286 atom
->getIndirect(0, 0), stVal
);
1287 st
->setPredicate(CC_P
, ld
->getDef(1));
1288 st
->subOp
= NV50_IR_SUBOP_STORE_UNLOCKED
;
1290 // Loop until the lock is acquired.
1291 bld
.mkFlow(OP_BRA
, tryLockAndSetBB
, CC_NOT_P
, ld
->getDef(1));
1292 tryLockAndSetBB
->cfg
.attach(&tryLockAndSetBB
->cfg
, Graph::Edge::BACK
);
1293 tryLockAndSetBB
->cfg
.attach(&joinBB
->cfg
, Graph::Edge::CROSS
);
1294 bld
.mkFlow(OP_BRA
, joinBB
, CC_ALWAYS
, NULL
);
1298 bld
.setPosition(joinBB
, false);
1299 bld
.mkFlow(OP_JOIN
, NULL
, CC_ALWAYS
, NULL
)->fixed
= 1;
1303 NVC0LoweringPass::handleATOM(Instruction
*atom
)
1306 Value
*ptr
= atom
->getIndirect(0, 0), *ind
= atom
->getIndirect(0, 1), *base
;
1308 switch (atom
->src(0).getFile()) {
1309 case FILE_MEMORY_LOCAL
:
1312 case FILE_MEMORY_SHARED
:
1313 // For Fermi/Kepler, we have to use ld lock/st unlock to perform atomic
1314 // operations on shared memory. For Maxwell, ATOMS is enough.
1315 if (targ
->getChipset() < NVISA_GK104_CHIPSET
)
1316 handleSharedATOM(atom
);
1317 else if (targ
->getChipset() < NVISA_GM107_CHIPSET
)
1318 handleSharedATOMNVE4(atom
);
1321 assert(atom
->src(0).getFile() == FILE_MEMORY_BUFFER
);
1322 base
= loadBufInfo64(ind
, atom
->getSrc(0)->reg
.fileIndex
* 16);
1323 assert(base
->reg
.size
== 8);
1325 base
= bld
.mkOp2v(OP_ADD
, TYPE_U64
, base
, base
, ptr
);
1326 assert(base
->reg
.size
== 8);
1327 atom
->setIndirect(0, 0, base
);
1328 atom
->getSrc(0)->reg
.file
= FILE_MEMORY_GLOBAL
;
1330 // Harden against out-of-bounds accesses
1331 Value
*offset
= bld
.loadImm(NULL
, atom
->getSrc(0)->reg
.data
.offset
+ typeSizeof(atom
->sType
));
1332 Value
*length
= loadBufLength32(ind
, atom
->getSrc(0)->reg
.fileIndex
* 16);
1333 Value
*pred
= new_LValue(func
, FILE_PREDICATE
);
1335 bld
.mkOp2(OP_ADD
, TYPE_U32
, offset
, offset
, ptr
);
1336 bld
.mkCmp(OP_SET
, CC_GT
, TYPE_U32
, pred
, TYPE_U32
, offset
, length
);
1337 atom
->setPredicate(CC_NOT_P
, pred
);
1338 if (atom
->defExists(0)) {
1339 Value
*zero
, *dst
= atom
->getDef(0);
1340 atom
->setDef(0, bld
.getSSA());
1342 bld
.setPosition(atom
, true);
1343 bld
.mkMov((zero
= bld
.getSSA()), bld
.mkImm(0))
1344 ->setPredicate(CC_P
, pred
);
1345 bld
.mkOp2(OP_UNION
, TYPE_U32
, dst
, atom
->getDef(0), zero
);
1351 bld
.mkOp1v(OP_RDSV
, TYPE_U32
, bld
.getScratch(), bld
.mkSysVal(sv
, 0));
1353 atom
->setSrc(0, cloneShallow(func
, atom
->getSrc(0)));
1354 atom
->getSrc(0)->reg
.file
= FILE_MEMORY_GLOBAL
;
1356 base
= bld
.mkOp2v(OP_ADD
, TYPE_U32
, base
, base
, ptr
);
1357 atom
->setIndirect(0, 1, NULL
);
1358 atom
->setIndirect(0, 0, base
);
1364 NVC0LoweringPass::handleCasExch(Instruction
*cas
, bool needCctl
)
1366 if (targ
->getChipset() < NVISA_GM107_CHIPSET
) {
1367 if (cas
->src(0).getFile() == FILE_MEMORY_SHARED
) {
1368 // ATOM_CAS and ATOM_EXCH are handled in handleSharedATOM().
1373 if (cas
->subOp
!= NV50_IR_SUBOP_ATOM_CAS
&&
1374 cas
->subOp
!= NV50_IR_SUBOP_ATOM_EXCH
)
1376 bld
.setPosition(cas
, true);
1379 Instruction
*cctl
= bld
.mkOp1(OP_CCTL
, TYPE_NONE
, NULL
, cas
->getSrc(0));
1380 cctl
->setIndirect(0, 0, cas
->getIndirect(0, 0));
1382 cctl
->subOp
= NV50_IR_SUBOP_CCTL_IV
;
1383 if (cas
->isPredicated())
1384 cctl
->setPredicate(cas
->cc
, cas
->getPredicate());
1387 if (cas
->subOp
== NV50_IR_SUBOP_ATOM_CAS
) {
1388 // CAS is crazy. It's 2nd source is a double reg, and the 3rd source
1389 // should be set to the high part of the double reg or bad things will
1390 // happen elsewhere in the universe.
1391 // Also, it sometimes returns the new value instead of the old one
1392 // under mysterious circumstances.
1393 Value
*dreg
= bld
.getSSA(8);
1394 bld
.setPosition(cas
, false);
1395 bld
.mkOp2(OP_MERGE
, TYPE_U64
, dreg
, cas
->getSrc(1), cas
->getSrc(2));
1396 cas
->setSrc(1, dreg
);
1397 cas
->setSrc(2, dreg
);
1404 NVC0LoweringPass::loadResInfo32(Value
*ptr
, uint32_t off
, uint16_t base
)
1406 uint8_t b
= prog
->driver
->io
.auxCBSlot
;
1410 mkLoadv(TYPE_U32
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U32
, off
), ptr
);
1414 NVC0LoweringPass::loadResInfo64(Value
*ptr
, uint32_t off
, uint16_t base
)
1416 uint8_t b
= prog
->driver
->io
.auxCBSlot
;
1420 ptr
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getScratch(), ptr
, bld
.mkImm(4));
1423 mkLoadv(TYPE_U64
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U64
, off
), ptr
);
1427 NVC0LoweringPass::loadResLength32(Value
*ptr
, uint32_t off
, uint16_t base
)
1429 uint8_t b
= prog
->driver
->io
.auxCBSlot
;
1433 ptr
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getScratch(), ptr
, bld
.mkImm(4));
1436 mkLoadv(TYPE_U32
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U64
, off
+ 8), ptr
);
1440 NVC0LoweringPass::loadSuInfo32(Value
*ptr
, uint32_t off
)
1442 return loadResInfo32(ptr
, off
, prog
->driver
->io
.suInfoBase
);
1446 NVC0LoweringPass::loadSuInfo64(Value
*ptr
, uint32_t off
)
1448 return loadResInfo64(ptr
, off
, prog
->driver
->io
.suInfoBase
);
1452 NVC0LoweringPass::loadSuLength32(Value
*ptr
, uint32_t off
)
1454 return loadResLength32(ptr
, off
, prog
->driver
->io
.suInfoBase
);
1458 NVC0LoweringPass::loadBufInfo32(Value
*ptr
, uint32_t off
)
1460 return loadResInfo32(ptr
, off
, prog
->driver
->io
.bufInfoBase
);
1464 NVC0LoweringPass::loadBufInfo64(Value
*ptr
, uint32_t off
)
1466 return loadResInfo64(ptr
, off
, prog
->driver
->io
.bufInfoBase
);
1470 NVC0LoweringPass::loadBufLength32(Value
*ptr
, uint32_t off
)
1472 return loadResLength32(ptr
, off
, prog
->driver
->io
.bufInfoBase
);
1476 NVC0LoweringPass::loadUboInfo32(Value
*ptr
, uint32_t off
)
1478 return loadResInfo32(ptr
, off
, prog
->driver
->io
.uboInfoBase
);
1482 NVC0LoweringPass::loadUboInfo64(Value
*ptr
, uint32_t off
)
1484 return loadResInfo64(ptr
, off
, prog
->driver
->io
.uboInfoBase
);
1488 NVC0LoweringPass::loadUboLength32(Value
*ptr
, uint32_t off
)
1490 return loadResLength32(ptr
, off
, prog
->driver
->io
.uboInfoBase
);
1494 NVC0LoweringPass::loadMsInfo32(Value
*ptr
, uint32_t off
)
1496 uint8_t b
= prog
->driver
->io
.msInfoCBSlot
;
1497 off
+= prog
->driver
->io
.msInfoBase
;
1499 mkLoadv(TYPE_U32
, bld
.mkSymbol(FILE_MEMORY_CONST
, b
, TYPE_U32
, off
), ptr
);
1502 /* On nvc0, surface info is obtained via the surface binding points passed
1503 * to the SULD/SUST instructions.
1504 * On nve4, surface info is stored in c[] and is used by various special
1505 * instructions, e.g. for clamping coordiantes or generating an address.
1506 * They couldn't just have added an equivalent to TIC now, couldn't they ?
1508 #define NVE4_SU_INFO_ADDR 0x00
1509 #define NVE4_SU_INFO_FMT 0x04
1510 #define NVE4_SU_INFO_DIM_X 0x08
1511 #define NVE4_SU_INFO_PITCH 0x0c
1512 #define NVE4_SU_INFO_DIM_Y 0x10
1513 #define NVE4_SU_INFO_ARRAY 0x14
1514 #define NVE4_SU_INFO_DIM_Z 0x18
1515 #define NVE4_SU_INFO_UNK1C 0x1c
1516 #define NVE4_SU_INFO_WIDTH 0x20
1517 #define NVE4_SU_INFO_HEIGHT 0x24
1518 #define NVE4_SU_INFO_DEPTH 0x28
1519 #define NVE4_SU_INFO_TARGET 0x2c
1520 #define NVE4_SU_INFO_BSIZE 0x30
1521 #define NVE4_SU_INFO_RAW_X 0x34
1522 #define NVE4_SU_INFO_MS_X 0x38
1523 #define NVE4_SU_INFO_MS_Y 0x3c
1525 #define NVE4_SU_INFO__STRIDE 0x40
1527 #define NVE4_SU_INFO_DIM(i) (0x08 + (i) * 8)
1528 #define NVE4_SU_INFO_SIZE(i) (0x20 + (i) * 4)
1529 #define NVE4_SU_INFO_MS(i) (0x38 + (i) * 4)
1531 static inline uint16_t getSuClampSubOp(const TexInstruction
*su
, int c
)
1533 switch (su
->tex
.target
.getEnum()) {
1534 case TEX_TARGET_BUFFER
: return NV50_IR_SUBOP_SUCLAMP_PL(0, 1);
1535 case TEX_TARGET_RECT
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1536 case TEX_TARGET_1D
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1537 case TEX_TARGET_1D_ARRAY
: return (c
== 1) ?
1538 NV50_IR_SUBOP_SUCLAMP_PL(0, 2) :
1539 NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1540 case TEX_TARGET_2D
: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1541 case TEX_TARGET_2D_MS
: return NV50_IR_SUBOP_SUCLAMP_BL(0, 2);
1542 case TEX_TARGET_2D_ARRAY
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1543 case TEX_TARGET_2D_MS_ARRAY
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1544 case TEX_TARGET_3D
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1545 case TEX_TARGET_CUBE
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1546 case TEX_TARGET_CUBE_ARRAY
: return NV50_IR_SUBOP_SUCLAMP_SD(0, 2);
1554 NVC0LoweringPass::handleSUQ(TexInstruction
*suq
)
1556 int dim
= suq
->tex
.target
.getDim();
1557 int arg
= dim
+ (suq
->tex
.target
.isArray() || suq
->tex
.target
.isCube());
1558 uint8_t s
= prog
->driver
->io
.auxCBSlot
;
1559 Value
*ind
= suq
->getIndirectR();
1563 base
= prog
->driver
->io
.suInfoBase
+ suq
->tex
.r
* NVE4_SU_INFO__STRIDE
;
1566 ind
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getScratch(),
1569 for (c
= 0; c
< arg
; ++c
) {
1570 if (suq
->defExists(c
)) {
1573 if (c
== 1 && suq
->tex
.target
== TEX_TARGET_1D_ARRAY
) {
1574 offset
= base
+ NVE4_SU_INFO_SIZE(2);
1576 offset
= base
+ NVE4_SU_INFO_SIZE(c
);
1578 bld
.mkLoad(TYPE_U32
, suq
->getDef(c
),
1579 bld
.mkSymbol(FILE_MEMORY_CONST
, s
, TYPE_U32
, offset
), ind
);
1583 if (suq
->tex
.target
.isCube()) {
1584 if (suq
->defExists(2)) {
1585 bld
.mkOp2(OP_DIV
, TYPE_U32
, suq
->getDef(2), suq
->getDef(2),
1586 bld
.loadImm(NULL
, 6));
1590 if (suq
->defExists(3)) {
1591 // .w contains the number of samples for multi-sampled images but we
1592 // don't support them for now.
1593 bld
.mkMov(suq
->getDef(3), bld
.loadImm(NULL
, 1));
1601 NVC0LoweringPass::adjustCoordinatesMS(TexInstruction
*tex
)
1603 const uint16_t base
= tex
->tex
.r
* NVE4_SU_INFO__STRIDE
;
1604 const int arg
= tex
->tex
.target
.getArgCount();
1606 if (tex
->tex
.target
== TEX_TARGET_2D_MS
)
1607 tex
->tex
.target
= TEX_TARGET_2D
;
1609 if (tex
->tex
.target
== TEX_TARGET_2D_MS_ARRAY
)
1610 tex
->tex
.target
= TEX_TARGET_2D_ARRAY
;
1614 Value
*x
= tex
->getSrc(0);
1615 Value
*y
= tex
->getSrc(1);
1616 Value
*s
= tex
->getSrc(arg
- 1);
1618 Value
*tx
= bld
.getSSA(), *ty
= bld
.getSSA(), *ts
= bld
.getSSA();
1621 if (tex
->tex
.rIndirectSrc
>= 0) {
1622 assert(tex
->tex
.r
== 0);
1623 // FIXME: out of bounds
1624 ind
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(),
1625 tex
->getIndirectR(), bld
.mkImm(6));
1628 Value
*ms_x
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_MS(0));
1629 Value
*ms_y
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_MS(1));
1631 bld
.mkOp2(OP_SHL
, TYPE_U32
, tx
, x
, ms_x
);
1632 bld
.mkOp2(OP_SHL
, TYPE_U32
, ty
, y
, ms_y
);
1634 s
= bld
.mkOp2v(OP_AND
, TYPE_U32
, ts
, s
, bld
.loadImm(NULL
, 0x7));
1635 s
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, ts
, ts
, bld
.mkImm(3));
1637 Value
*dx
= loadMsInfo32(ts
, 0x0);
1638 Value
*dy
= loadMsInfo32(ts
, 0x4);
1640 bld
.mkOp2(OP_ADD
, TYPE_U32
, tx
, tx
, dx
);
1641 bld
.mkOp2(OP_ADD
, TYPE_U32
, ty
, ty
, dy
);
1645 tex
->moveSources(arg
, -1);
1648 // Sets 64-bit "generic address", predicate and format sources for SULD/SUST.
1649 // They're computed from the coordinates using the surface info in c[] space.
1651 NVC0LoweringPass::processSurfaceCoordsNVE4(TexInstruction
*su
)
1654 const bool atom
= su
->op
== OP_SUREDB
|| su
->op
== OP_SUREDP
;
1656 su
->op
== OP_SULDB
|| su
->op
== OP_SUSTB
|| su
->op
== OP_SUREDB
;
1657 const int idx
= su
->tex
.r
;
1658 const int dim
= su
->tex
.target
.getDim();
1659 const int arg
= dim
+ (su
->tex
.target
.isArray() || su
->tex
.target
.isCube());
1660 const uint16_t base
= idx
* NVE4_SU_INFO__STRIDE
;
1662 Value
*zero
= bld
.mkImm(0);
1666 Value
*bf
, *eau
, *off
;
1670 off
= bld
.getScratch(4);
1671 bf
= bld
.getScratch(4);
1672 addr
= bld
.getSSA(8);
1673 pred
= bld
.getScratch(1, FILE_PREDICATE
);
1675 bld
.setPosition(su
, false);
1677 adjustCoordinatesMS(su
);
1679 if (su
->tex
.rIndirectSrc
>= 0) {
1680 ind
= su
->getIndirectR();
1681 if (su
->tex
.r
> 0) {
1682 ind
= bld
.mkOp2v(OP_ADD
, TYPE_U32
, bld
.getSSA(), ind
,
1683 bld
.loadImm(NULL
, su
->tex
.r
));
1685 ind
= bld
.mkOp2v(OP_AND
, TYPE_U32
, bld
.getSSA(), ind
, bld
.mkImm(7));
1686 ind
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(), ind
, bld
.mkImm(6));
1689 // calculate clamped coordinates
1690 for (c
= 0; c
< arg
; ++c
) {
1693 if (c
== 1 && su
->tex
.target
== TEX_TARGET_1D_ARRAY
) {
1694 // The array index is stored in the Z component for 1D arrays.
1698 src
[c
] = bld
.getScratch();
1700 v
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_RAW_X
);
1702 v
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_DIM(dimc
));
1703 bld
.mkOp3(OP_SUCLAMP
, TYPE_S32
, src
[c
], su
->getSrc(c
), v
, zero
)
1704 ->subOp
= getSuClampSubOp(su
, dimc
);
1709 // set predicate output
1710 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1711 src
[0]->getInsn()->setFlagsDef(1, pred
);
1713 if (su
->tex
.target
.isArray() || su
->tex
.target
.isCube()) {
1714 p1
= bld
.getSSA(1, FILE_PREDICATE
);
1715 src
[dim
]->getInsn()->setFlagsDef(1, p1
);
1718 // calculate pixel offset
1720 if (su
->tex
.target
!= TEX_TARGET_BUFFER
)
1721 bld
.mkOp2(OP_AND
, TYPE_U32
, off
, src
[0], bld
.loadImm(NULL
, 0xffff));
1724 v
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_UNK1C
);
1725 bld
.mkOp3(OP_MADSP
, TYPE_U32
, off
, src
[2], v
, src
[1])
1726 ->subOp
= NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1728 v
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_PITCH
);
1729 bld
.mkOp3(OP_MADSP
, TYPE_U32
, off
, off
, v
, src
[0])
1730 ->subOp
= NV50_IR_SUBOP_MADSP(0,2,8); // u32 u16l u16l
1733 v
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_PITCH
);
1734 bld
.mkOp3(OP_MADSP
, TYPE_U32
, off
, src
[1], v
, src
[0])
1735 ->subOp
= (su
->tex
.target
.isArray() || su
->tex
.target
.isCube()) ?
1736 NV50_IR_SUBOP_MADSP_SD
: NV50_IR_SUBOP_MADSP(4,2,8); // u16l u16l u16l
1739 // calculate effective address part 1
1740 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1744 v
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_FMT
);
1745 bld
.mkOp3(OP_VSHL
, TYPE_U32
, bf
, src
[0], v
, zero
)
1746 ->subOp
= NV50_IR_SUBOP_V1(7,6,8|2);
1760 if (!su
->tex
.target
.isArray() && !su
->tex
.target
.isCube()) {
1761 z
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_UNK1C
);
1762 subOp
= NV50_IR_SUBOP_SUBFM_3D
;
1766 subOp
= NV50_IR_SUBOP_SUBFM_3D
;
1770 insn
= bld
.mkOp3(OP_SUBFM
, TYPE_U32
, bf
, src
[0], y
, z
);
1771 insn
->subOp
= subOp
;
1772 insn
->setFlagsDef(1, pred
);
1776 v
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_ADDR
);
1778 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1781 eau
= bld
.mkOp3v(OP_SUEAU
, TYPE_U32
, bld
.getScratch(4), off
, bf
, v
);
1783 // add array layer offset
1784 if (su
->tex
.target
.isArray() || su
->tex
.target
.isCube()) {
1785 v
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_ARRAY
);
1787 bld
.mkOp3(OP_MADSP
, TYPE_U32
, eau
, src
[1], v
, eau
)
1788 ->subOp
= NV50_IR_SUBOP_MADSP(4,0,0); // u16 u24 u32
1790 bld
.mkOp3(OP_MADSP
, TYPE_U32
, eau
, v
, src
[2], eau
)
1791 ->subOp
= NV50_IR_SUBOP_MADSP(0,0,0); // u32 u24 u32
1792 // combine predicates
1794 bld
.mkOp2(OP_OR
, TYPE_U8
, pred
, pred
, p1
);
1799 if (su
->tex
.target
== TEX_TARGET_BUFFER
) {
1803 // bf == g[] address & 0xff
1804 // eau == g[] address >> 8
1805 bld
.mkOp3(OP_PERMT
, TYPE_U32
, bf
, lo
, bld
.loadImm(NULL
, 0x6540), eau
);
1806 bld
.mkOp3(OP_PERMT
, TYPE_U32
, eau
, zero
, bld
.loadImm(NULL
, 0x0007), eau
);
1808 if (su
->op
== OP_SULDP
&& su
->tex
.target
== TEX_TARGET_BUFFER
) {
1809 // Convert from u32 to u8 address format, which is what the library code
1810 // doing SULDP currently uses.
1811 // XXX: can SUEAU do this ?
1812 // XXX: does it matter that we don't mask high bytes in bf ?
1814 bld
.mkOp2(OP_SHR
, TYPE_U32
, off
, bf
, bld
.mkImm(8));
1815 bld
.mkOp2(OP_ADD
, TYPE_U32
, eau
, eau
, off
);
1818 bld
.mkOp2(OP_MERGE
, TYPE_U64
, addr
, bf
, eau
);
1820 if (atom
&& su
->tex
.target
== TEX_TARGET_BUFFER
)
1821 bld
.mkOp2(OP_ADD
, TYPE_U64
, addr
, addr
, off
);
1823 // let's just set it 0 for raw access and hope it works
1825 bld
.mkImm(0) : loadSuInfo32(ind
, base
+ NVE4_SU_INFO_FMT
);
1827 // get rid of old coordinate sources, make space for fmt info and predicate
1828 su
->moveSources(arg
, 3 - arg
);
1829 // set 64 bit address and 32-bit format sources
1830 su
->setSrc(0, addr
);
1832 su
->setSrc(2, pred
);
1834 // prevent read fault when the image is not actually bound
1835 CmpInstruction
*pred1
=
1836 bld
.mkCmp(OP_SET
, CC_EQ
, TYPE_U32
, bld
.getSSA(1, FILE_PREDICATE
),
1837 TYPE_U32
, bld
.mkImm(0),
1838 loadSuInfo32(ind
, base
+ NVE4_SU_INFO_ADDR
));
1840 if (su
->op
!= OP_SUSTP
&& su
->tex
.format
) {
1841 const TexInstruction::ImgFormatDesc
*format
= su
->tex
.format
;
1842 int blockwidth
= format
->bits
[0] + format
->bits
[1] +
1843 format
->bits
[2] + format
->bits
[3];
1845 // make sure that the format doesn't mismatch
1846 assert(format
->components
!= 0);
1847 bld
.mkCmp(OP_SET_OR
, CC_NE
, TYPE_U32
, pred1
->getDef(0),
1848 TYPE_U32
, bld
.loadImm(NULL
, blockwidth
/ 8),
1849 loadSuInfo32(ind
, base
+ NVE4_SU_INFO_BSIZE
),
1852 su
->setPredicate(CC_NOT_P
, pred1
->getDef(0));
1854 // TODO: initialize def values to 0 when the surface operation is not
1855 // performed (not needed for stores). Also, fix the "address bounds test"
1856 // subtests from arb_shader_image_load_store-invalid for buffers, because it
1857 // seems like that the predicate is not correctly set by suclamp.
1861 getSrcType(const TexInstruction::ImgFormatDesc
*t
, int c
)
1864 case FLOAT
: return t
->bits
[c
] == 16 ? TYPE_F16
: TYPE_F32
;
1865 case UNORM
: return t
->bits
[c
] == 8 ? TYPE_U8
: TYPE_U16
;
1866 case SNORM
: return t
->bits
[c
] == 8 ? TYPE_S8
: TYPE_S16
;
1868 return (t
->bits
[c
] == 8 ? TYPE_U8
:
1869 (t
->bits
[c
] == 16 ? TYPE_U16
: TYPE_U32
));
1871 return (t
->bits
[c
] == 8 ? TYPE_S8
:
1872 (t
->bits
[c
] == 16 ? TYPE_S16
: TYPE_S32
));
1878 getDestType(const ImgType type
) {
1889 assert(!"Impossible type");
1895 NVC0LoweringPass::convertSurfaceFormat(TexInstruction
*su
)
1897 const TexInstruction::ImgFormatDesc
*format
= su
->tex
.format
;
1898 int width
= format
->bits
[0] + format
->bits
[1] +
1899 format
->bits
[2] + format
->bits
[3];
1900 Value
*untypedDst
[4] = {};
1901 Value
*typedDst
[4] = {};
1903 // We must convert this to a generic load.
1906 su
->dType
= typeOfSize(width
/ 8);
1907 su
->sType
= TYPE_U8
;
1909 for (int i
= 0; i
< width
/ 32; i
++)
1910 untypedDst
[i
] = bld
.getSSA();
1912 untypedDst
[0] = bld
.getSSA();
1914 for (int i
= 0; i
< 4; i
++) {
1915 typedDst
[i
] = su
->getDef(i
);
1918 // Set the untyped dsts as the su's destinations
1919 for (int i
= 0; i
< 4; i
++)
1920 su
->setDef(i
, untypedDst
[i
]);
1922 bld
.setPosition(su
, true);
1924 // Unpack each component into the typed dsts
1926 for (int i
= 0; i
< 4; bits
+= format
->bits
[i
], i
++) {
1929 if (i
>= format
->components
) {
1930 if (format
->type
== FLOAT
||
1931 format
->type
== UNORM
||
1932 format
->type
== SNORM
)
1933 bld
.loadImm(typedDst
[i
], i
== 3 ? 1.0f
: 0.0f
);
1935 bld
.loadImm(typedDst
[i
], i
== 3 ? 1 : 0);
1939 // Get just that component's data into the relevant place
1940 if (format
->bits
[i
] == 32)
1941 bld
.mkMov(typedDst
[i
], untypedDst
[i
]);
1942 else if (format
->bits
[i
] == 16)
1943 bld
.mkCvt(OP_CVT
, getDestType(format
->type
), typedDst
[i
],
1944 getSrcType(format
, i
), untypedDst
[i
/ 2])
1945 ->subOp
= (i
& 1) << (format
->type
== FLOAT
? 0 : 1);
1946 else if (format
->bits
[i
] == 8)
1947 bld
.mkCvt(OP_CVT
, getDestType(format
->type
), typedDst
[i
],
1948 getSrcType(format
, i
), untypedDst
[0])->subOp
= i
;
1950 bld
.mkOp2(OP_EXTBF
, TYPE_U32
, typedDst
[i
], untypedDst
[bits
/ 32],
1951 bld
.mkImm((bits
% 32) | (format
->bits
[i
] << 8)));
1952 if (format
->type
== UNORM
|| format
->type
== SNORM
)
1953 bld
.mkCvt(OP_CVT
, TYPE_F32
, typedDst
[i
], getSrcType(format
, i
), typedDst
[i
]);
1956 // Normalize / convert as necessary
1957 if (format
->type
== UNORM
)
1958 bld
.mkOp2(OP_MUL
, TYPE_F32
, typedDst
[i
], typedDst
[i
], bld
.loadImm(NULL
, 1.0f
/ ((1 << format
->bits
[i
]) - 1)));
1959 else if (format
->type
== SNORM
)
1960 bld
.mkOp2(OP_MUL
, TYPE_F32
, typedDst
[i
], typedDst
[i
], bld
.loadImm(NULL
, 1.0f
/ ((1 << (format
->bits
[i
] - 1)) - 1)));
1961 else if (format
->type
== FLOAT
&& format
->bits
[i
] < 16) {
1962 bld
.mkOp2(OP_SHL
, TYPE_U32
, typedDst
[i
], typedDst
[i
], bld
.loadImm(NULL
, 15 - format
->bits
[i
]));
1963 bld
.mkCvt(OP_CVT
, TYPE_F32
, typedDst
[i
], TYPE_F16
, typedDst
[i
]);
1969 NVC0LoweringPass::handleSurfaceOpNVE4(TexInstruction
*su
)
1971 processSurfaceCoordsNVE4(su
);
1973 if (su
->op
== OP_SULDP
)
1974 convertSurfaceFormat(su
);
1976 if (su
->op
== OP_SUREDB
|| su
->op
== OP_SUREDP
) {
1977 Value
*pred
= su
->getSrc(2);
1978 CondCode cc
= CC_NOT_P
;
1979 if (su
->getPredicate()) {
1980 pred
= bld
.getScratch(1, FILE_PREDICATE
);
1982 if (cc
== CC_NOT_P
) {
1983 bld
.mkOp2(OP_OR
, TYPE_U8
, pred
, su
->getPredicate(), su
->getSrc(2));
1985 bld
.mkOp2(OP_AND
, TYPE_U8
, pred
, su
->getPredicate(), su
->getSrc(2));
1986 pred
->getInsn()->src(1).mod
= Modifier(NV50_IR_MOD_NOT
);
1989 Instruction
*red
= bld
.mkOp(OP_ATOM
, su
->dType
, bld
.getSSA());
1990 red
->subOp
= su
->subOp
;
1992 gMemBase
= bld
.mkSymbol(FILE_MEMORY_GLOBAL
, 0, TYPE_U32
, 0);
1993 red
->setSrc(0, gMemBase
);
1994 red
->setSrc(1, su
->getSrc(3));
1995 if (su
->subOp
== NV50_IR_SUBOP_ATOM_CAS
)
1996 red
->setSrc(2, su
->getSrc(4));
1997 red
->setIndirect(0, 0, su
->getSrc(0));
1999 // make sure to initialize dst value when the atomic operation is not
2001 Instruction
*mov
= bld
.mkMov(bld
.getSSA(), bld
.loadImm(NULL
, 0));
2003 assert(cc
== CC_NOT_P
);
2004 red
->setPredicate(cc
, pred
);
2005 mov
->setPredicate(CC_P
, pred
);
2007 bld
.mkOp2(OP_UNION
, TYPE_U32
, su
->getDef(0),
2008 red
->getDef(0), mov
->getDef(0));
2010 delete_Instruction(bld
.getProgram(), su
);
2011 handleCasExch(red
, true);
2014 if (su
->op
== OP_SUSTB
|| su
->op
== OP_SUSTP
)
2015 su
->sType
= (su
->tex
.target
== TEX_TARGET_BUFFER
) ? TYPE_U32
: TYPE_U8
;
2019 NVC0LoweringPass::processSurfaceCoordsNVC0(TexInstruction
*su
)
2021 const int idx
= su
->tex
.r
;
2022 const int dim
= su
->tex
.target
.getDim();
2023 const int arg
= dim
+ (su
->tex
.target
.isArray() || su
->tex
.target
.isCube());
2024 const uint16_t base
= idx
* NVE4_SU_INFO__STRIDE
;
2026 Value
*zero
= bld
.mkImm(0);
2031 if (su
->tex
.rIndirectSrc
>= 0) {
2032 ind
= su
->getIndirectR();
2033 if (su
->tex
.r
> 0) {
2034 ind
= bld
.mkOp2v(OP_ADD
, TYPE_U32
, bld
.getSSA(), ind
,
2035 bld
.loadImm(NULL
, su
->tex
.r
));
2037 ind
= bld
.mkOp2v(OP_AND
, TYPE_U32
, bld
.getSSA(), ind
, bld
.mkImm(7));
2038 ind
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(), ind
, bld
.mkImm(6));
2041 // get surface coordinates
2042 for (c
= 0; c
< arg
; ++c
)
2043 src
[c
] = su
->getSrc(c
);
2047 // calculate pixel offset
2048 if (su
->op
== OP_SULDP
|| su
->op
== OP_SUREDP
) {
2049 v
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_BSIZE
);
2050 su
->setSrc(0, bld
.mkOp2v(OP_MUL
, TYPE_U32
, bld
.getSSA(), src
[0], v
));
2053 // add array layer offset
2054 if (su
->tex
.target
.isArray() || su
->tex
.target
.isCube()) {
2055 v
= loadSuInfo32(ind
, base
+ NVE4_SU_INFO_ARRAY
);
2057 su
->setSrc(2, bld
.mkOp2v(OP_MUL
, TYPE_U32
, bld
.getSSA(), src
[2], v
));
2060 // prevent read fault when the image is not actually bound
2061 CmpInstruction
*pred
=
2062 bld
.mkCmp(OP_SET
, CC_EQ
, TYPE_U32
, bld
.getSSA(1, FILE_PREDICATE
),
2063 TYPE_U32
, bld
.mkImm(0),
2064 loadSuInfo32(ind
, base
+ NVE4_SU_INFO_ADDR
));
2065 if (su
->op
!= OP_SUSTP
&& su
->tex
.format
) {
2066 const TexInstruction::ImgFormatDesc
*format
= su
->tex
.format
;
2067 int blockwidth
= format
->bits
[0] + format
->bits
[1] +
2068 format
->bits
[2] + format
->bits
[3];
2070 assert(format
->components
!= 0);
2071 // make sure that the format doesn't mismatch when it's not FMT_NONE
2072 bld
.mkCmp(OP_SET_OR
, CC_NE
, TYPE_U32
, pred
->getDef(0),
2073 TYPE_U32
, bld
.loadImm(NULL
, blockwidth
/ 8),
2074 loadSuInfo32(ind
, base
+ NVE4_SU_INFO_BSIZE
),
2077 su
->setPredicate(CC_NOT_P
, pred
->getDef(0));
2081 NVC0LoweringPass::handleSurfaceOpNVC0(TexInstruction
*su
)
2083 if (su
->tex
.target
== TEX_TARGET_1D_ARRAY
) {
2084 /* As 1d arrays also need 3 coordinates, switching to TEX_TARGET_2D_ARRAY
2085 * will simplify the lowering pass and the texture constraints. */
2086 su
->moveSources(1, 1);
2087 su
->setSrc(1, bld
.loadImm(NULL
, 0));
2088 su
->tex
.target
= TEX_TARGET_2D_ARRAY
;
2091 processSurfaceCoordsNVC0(su
);
2093 if (su
->op
== OP_SULDP
)
2094 convertSurfaceFormat(su
);
2096 if (su
->op
== OP_SUREDB
|| su
->op
== OP_SUREDP
) {
2097 const int dim
= su
->tex
.target
.getDim();
2098 const int arg
= dim
+ (su
->tex
.target
.isArray() || su
->tex
.target
.isCube());
2099 LValue
*addr
= bld
.getSSA(8);
2100 Value
*def
= su
->getDef(0);
2104 // Set the destination to the address
2105 su
->dType
= TYPE_U64
;
2106 su
->setDef(0, addr
);
2107 su
->setDef(1, su
->getPredicate());
2109 bld
.setPosition(su
, true);
2111 // Perform the atomic op
2112 Instruction
*red
= bld
.mkOp(OP_ATOM
, su
->sType
, bld
.getSSA());
2113 red
->subOp
= su
->subOp
;
2114 red
->setSrc(0, bld
.mkSymbol(FILE_MEMORY_GLOBAL
, 0, su
->sType
, 0));
2115 red
->setSrc(1, su
->getSrc(arg
));
2116 if (red
->subOp
== NV50_IR_SUBOP_ATOM_CAS
)
2117 red
->setSrc(2, su
->getSrc(arg
+ 1));
2118 red
->setIndirect(0, 0, addr
);
2120 // make sure to initialize dst value when the atomic operation is not
2122 Instruction
*mov
= bld
.mkMov(bld
.getSSA(), bld
.loadImm(NULL
, 0));
2124 assert(su
->cc
== CC_NOT_P
);
2125 red
->setPredicate(su
->cc
, su
->getPredicate());
2126 mov
->setPredicate(CC_P
, su
->getPredicate());
2128 bld
.mkOp2(OP_UNION
, TYPE_U32
, def
, red
->getDef(0), mov
->getDef(0));
2130 handleCasExch(red
, false);
2135 NVC0LoweringPass::handleWRSV(Instruction
*i
)
2141 // must replace, $sreg are not writeable
2142 addr
= targ
->getSVAddress(FILE_SHADER_OUTPUT
, i
->getSrc(0)->asSym());
2145 sym
= bld
.mkSymbol(FILE_SHADER_OUTPUT
, 0, i
->sType
, addr
);
2147 st
= bld
.mkStore(OP_EXPORT
, i
->dType
, sym
, i
->getIndirect(0, 0),
2149 st
->perPatch
= i
->perPatch
;
2151 bld
.getBB()->remove(i
);
2156 NVC0LoweringPass::handleLDST(Instruction
*i
)
2158 if (i
->src(0).getFile() == FILE_SHADER_INPUT
) {
2159 if (prog
->getType() == Program::TYPE_COMPUTE
) {
2160 i
->getSrc(0)->reg
.file
= FILE_MEMORY_CONST
;
2161 i
->getSrc(0)->reg
.fileIndex
= 0;
2163 if (prog
->getType() == Program::TYPE_GEOMETRY
&&
2164 i
->src(0).isIndirect(0)) {
2165 // XXX: this assumes vec4 units
2166 Value
*ptr
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(),
2167 i
->getIndirect(0, 0), bld
.mkImm(4));
2168 i
->setIndirect(0, 0, ptr
);
2172 assert(prog
->getType() != Program::TYPE_FRAGMENT
); // INTERP
2174 } else if (i
->src(0).getFile() == FILE_MEMORY_CONST
) {
2175 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
&&
2176 prog
->getType() == Program::TYPE_COMPUTE
) {
2177 // The launch descriptor only allows to set up 8 CBs, but OpenGL
2178 // requires at least 12 UBOs. To bypass this limitation, we store the
2179 // addrs into the driver constbuf and we directly load from the global
2181 int8_t fileIndex
= i
->getSrc(0)->reg
.fileIndex
- 1;
2182 Value
*ind
= i
->getIndirect(0, 1);
2183 Value
*ptr
= loadUboInfo64(ind
, fileIndex
* 16);
2185 // TODO: clamp the offset to the maximum number of const buf.
2186 if (i
->src(0).isIndirect(1)) {
2187 Value
*offset
= bld
.loadImm(NULL
, i
->getSrc(0)->reg
.data
.offset
+ typeSizeof(i
->sType
));
2188 Value
*length
= loadUboLength32(ind
, fileIndex
* 16);
2189 Value
*pred
= new_LValue(func
, FILE_PREDICATE
);
2190 if (i
->src(0).isIndirect(0)) {
2191 bld
.mkOp2(OP_ADD
, TYPE_U64
, ptr
, ptr
, i
->getIndirect(0, 0));
2192 bld
.mkOp2(OP_ADD
, TYPE_U32
, offset
, offset
, i
->getIndirect(0, 0));
2194 i
->getSrc(0)->reg
.file
= FILE_MEMORY_GLOBAL
;
2195 i
->setIndirect(0, 1, NULL
);
2196 i
->setIndirect(0, 0, ptr
);
2197 bld
.mkCmp(OP_SET
, CC_GT
, TYPE_U32
, pred
, TYPE_U32
, offset
, length
);
2198 i
->setPredicate(CC_NOT_P
, pred
);
2199 if (i
->defExists(0)) {
2200 bld
.mkMov(i
->getDef(0), bld
.mkImm(0));
2202 } else if (fileIndex
>= 0) {
2203 if (i
->src(0).isIndirect(0)) {
2204 bld
.mkOp2(OP_ADD
, TYPE_U64
, ptr
, ptr
, i
->getIndirect(0, 0));
2206 i
->getSrc(0)->reg
.file
= FILE_MEMORY_GLOBAL
;
2207 i
->setIndirect(0, 1, NULL
);
2208 i
->setIndirect(0, 0, ptr
);
2210 } else if (i
->src(0).isIndirect(1)) {
2212 if (i
->src(0).isIndirect(0))
2213 ptr
= bld
.mkOp3v(OP_INSBF
, TYPE_U32
, bld
.getSSA(),
2214 i
->getIndirect(0, 1), bld
.mkImm(0x1010),
2215 i
->getIndirect(0, 0));
2217 ptr
= bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(),
2218 i
->getIndirect(0, 1), bld
.mkImm(16));
2219 i
->setIndirect(0, 1, NULL
);
2220 i
->setIndirect(0, 0, ptr
);
2221 i
->subOp
= NV50_IR_SUBOP_LDC_IS
;
2223 } else if (i
->src(0).getFile() == FILE_SHADER_OUTPUT
) {
2224 assert(prog
->getType() == Program::TYPE_TESSELLATION_CONTROL
);
2226 } else if (i
->src(0).getFile() == FILE_MEMORY_BUFFER
) {
2227 Value
*ind
= i
->getIndirect(0, 1);
2228 Value
*ptr
= loadBufInfo64(ind
, i
->getSrc(0)->reg
.fileIndex
* 16);
2229 // XXX come up with a way not to do this for EVERY little access but
2230 // rather to batch these up somehow. Unfortunately we've lost the
2231 // information about the field width by the time we get here.
2232 Value
*offset
= bld
.loadImm(NULL
, i
->getSrc(0)->reg
.data
.offset
+ typeSizeof(i
->sType
));
2233 Value
*length
= loadBufLength32(ind
, i
->getSrc(0)->reg
.fileIndex
* 16);
2234 Value
*pred
= new_LValue(func
, FILE_PREDICATE
);
2235 if (i
->src(0).isIndirect(0)) {
2236 bld
.mkOp2(OP_ADD
, TYPE_U64
, ptr
, ptr
, i
->getIndirect(0, 0));
2237 bld
.mkOp2(OP_ADD
, TYPE_U32
, offset
, offset
, i
->getIndirect(0, 0));
2239 i
->setIndirect(0, 1, NULL
);
2240 i
->setIndirect(0, 0, ptr
);
2241 i
->getSrc(0)->reg
.file
= FILE_MEMORY_GLOBAL
;
2242 bld
.mkCmp(OP_SET
, CC_GT
, TYPE_U32
, pred
, TYPE_U32
, offset
, length
);
2243 i
->setPredicate(CC_NOT_P
, pred
);
2244 if (i
->defExists(0)) {
2245 Value
*zero
, *dst
= i
->getDef(0);
2246 i
->setDef(0, bld
.getSSA());
2248 bld
.setPosition(i
, true);
2249 bld
.mkMov((zero
= bld
.getSSA()), bld
.mkImm(0))
2250 ->setPredicate(CC_P
, pred
);
2251 bld
.mkOp2(OP_UNION
, TYPE_U32
, dst
, i
->getDef(0), zero
);
2257 NVC0LoweringPass::readTessCoord(LValue
*dst
, int c
)
2259 Value
*laneid
= bld
.getSSA();
2262 bld
.mkOp1(OP_RDSV
, TYPE_U32
, laneid
, bld
.mkSysVal(SV_LANEID
, 0));
2273 if (prog
->driver
->prop
.tp
.domain
!= PIPE_PRIM_TRIANGLES
) {
2274 bld
.mkMov(dst
, bld
.loadImm(NULL
, 0));
2281 bld
.mkFetch(x
, TYPE_F32
, FILE_SHADER_OUTPUT
, 0x2f0, NULL
, laneid
);
2283 bld
.mkFetch(y
, TYPE_F32
, FILE_SHADER_OUTPUT
, 0x2f4, NULL
, laneid
);
2286 bld
.mkOp2(OP_ADD
, TYPE_F32
, dst
, x
, y
);
2287 bld
.mkOp2(OP_SUB
, TYPE_F32
, dst
, bld
.loadImm(NULL
, 1.0f
), dst
);
2292 NVC0LoweringPass::handleRDSV(Instruction
*i
)
2294 Symbol
*sym
= i
->getSrc(0)->asSym();
2295 const SVSemantic sv
= sym
->reg
.data
.sv
.sv
;
2298 uint32_t addr
= targ
->getSVAddress(FILE_SHADER_INPUT
, sym
);
2300 if (addr
>= 0x400) {
2302 if (sym
->reg
.data
.sv
.index
== 3) {
2303 // TGSI backend may use 4th component of TID,NTID,CTAID,NCTAID
2305 i
->setSrc(0, bld
.mkImm((sv
== SV_NTID
|| sv
== SV_NCTAID
) ? 1 : 0));
2307 if (sv
== SV_VERTEX_COUNT
) {
2308 bld
.setPosition(i
, true);
2309 bld
.mkOp2(OP_EXTBF
, TYPE_U32
, i
->getDef(0), i
->getDef(0), bld
.mkImm(0x808));
2316 assert(prog
->getType() == Program::TYPE_FRAGMENT
);
2317 if (i
->srcExists(1)) {
2318 // Pass offset through to the interpolation logic
2319 ld
= bld
.mkInterp(NV50_IR_INTERP_LINEAR
| NV50_IR_INTERP_OFFSET
,
2320 i
->getDef(0), addr
, NULL
);
2321 ld
->setSrc(1, i
->getSrc(1));
2323 bld
.mkInterp(NV50_IR_INTERP_LINEAR
, i
->getDef(0), addr
, NULL
);
2328 Value
*face
= i
->getDef(0);
2329 bld
.mkInterp(NV50_IR_INTERP_FLAT
, face
, addr
, NULL
);
2330 if (i
->dType
== TYPE_F32
) {
2331 bld
.mkOp2(OP_OR
, TYPE_U32
, face
, face
, bld
.mkImm(0x00000001));
2332 bld
.mkOp1(OP_NEG
, TYPE_S32
, face
, face
);
2333 bld
.mkCvt(OP_CVT
, TYPE_F32
, face
, TYPE_S32
, face
);
2338 assert(prog
->getType() == Program::TYPE_TESSELLATION_EVAL
);
2339 readTessCoord(i
->getDef(0)->asLValue(), i
->getSrc(0)->reg
.data
.sv
.index
);
2344 assert(targ
->getChipset() >= NVISA_GK104_CHIPSET
); // mov $sreg otherwise
2345 if (sym
->reg
.data
.sv
.index
== 3) {
2347 i
->setSrc(0, bld
.mkImm(sv
== SV_GRIDID
? 0 : 1));
2350 addr
+= prog
->driver
->prop
.cp
.gridInfoBase
;
2351 bld
.mkLoad(TYPE_U32
, i
->getDef(0),
2352 bld
.mkSymbol(FILE_MEMORY_CONST
, prog
->driver
->io
.auxCBSlot
,
2353 TYPE_U32
, addr
), NULL
);
2355 case SV_SAMPLE_INDEX
:
2356 // TODO: Properly pass source as an address in the PIX address space
2357 // (which can be of the form [r0+offset]). But this is currently
2359 ld
= bld
.mkOp1(OP_PIXLD
, TYPE_U32
, i
->getDef(0), bld
.mkImm(0));
2360 ld
->subOp
= NV50_IR_SUBOP_PIXLD_SAMPLEID
;
2362 case SV_SAMPLE_POS
: {
2363 Value
*off
= new_LValue(func
, FILE_GPR
);
2364 ld
= bld
.mkOp1(OP_PIXLD
, TYPE_U32
, i
->getDef(0), bld
.mkImm(0));
2365 ld
->subOp
= NV50_IR_SUBOP_PIXLD_SAMPLEID
;
2366 bld
.mkOp2(OP_SHL
, TYPE_U32
, off
, i
->getDef(0), bld
.mkImm(3));
2367 bld
.mkLoad(TYPE_F32
,
2370 FILE_MEMORY_CONST
, prog
->driver
->io
.auxCBSlot
,
2371 TYPE_U32
, prog
->driver
->io
.sampleInfoBase
+
2372 4 * sym
->reg
.data
.sv
.index
),
2376 case SV_SAMPLE_MASK
: {
2377 ld
= bld
.mkOp1(OP_PIXLD
, TYPE_U32
, i
->getDef(0), bld
.mkImm(0));
2378 ld
->subOp
= NV50_IR_SUBOP_PIXLD_COVMASK
;
2379 Instruction
*sampleid
=
2380 bld
.mkOp1(OP_PIXLD
, TYPE_U32
, bld
.getSSA(), bld
.mkImm(0));
2381 sampleid
->subOp
= NV50_IR_SUBOP_PIXLD_SAMPLEID
;
2383 bld
.mkOp2v(OP_AND
, TYPE_U32
, bld
.getSSA(), ld
->getDef(0),
2384 bld
.mkOp2v(OP_SHL
, TYPE_U32
, bld
.getSSA(),
2385 bld
.loadImm(NULL
, 1), sampleid
->getDef(0)));
2386 if (prog
->driver
->prop
.fp
.persampleInvocation
) {
2387 bld
.mkMov(i
->getDef(0), masked
);
2389 bld
.mkOp3(OP_SELP
, TYPE_U32
, i
->getDef(0), ld
->getDef(0), masked
,
2396 case SV_BASEINSTANCE
:
2398 ld
= bld
.mkLoad(TYPE_U32
, i
->getDef(0),
2399 bld
.mkSymbol(FILE_MEMORY_CONST
,
2400 prog
->driver
->io
.auxCBSlot
,
2402 prog
->driver
->io
.drawInfoBase
+
2403 4 * (sv
- SV_BASEVERTEX
)),
2407 if (prog
->getType() == Program::TYPE_TESSELLATION_EVAL
&& !i
->perPatch
)
2408 vtx
= bld
.mkOp1v(OP_PFETCH
, TYPE_U32
, bld
.getSSA(), bld
.mkImm(0));
2409 ld
= bld
.mkFetch(i
->getDef(0), i
->dType
,
2410 FILE_SHADER_INPUT
, addr
, i
->getIndirect(0, 0), vtx
);
2411 ld
->perPatch
= i
->perPatch
;
2414 bld
.getBB()->remove(i
);
2419 NVC0LoweringPass::handleDIV(Instruction
*i
)
2421 if (!isFloatType(i
->dType
))
2423 bld
.setPosition(i
, false);
2424 Instruction
*rcp
= bld
.mkOp1(OP_RCP
, i
->dType
, bld
.getSSA(typeSizeof(i
->dType
)), i
->getSrc(1));
2426 i
->setSrc(1, rcp
->getDef(0));
2431 NVC0LoweringPass::handleMOD(Instruction
*i
)
2433 if (!isFloatType(i
->dType
))
2435 LValue
*value
= bld
.getScratch(typeSizeof(i
->dType
));
2436 bld
.mkOp1(OP_RCP
, i
->dType
, value
, i
->getSrc(1));
2437 bld
.mkOp2(OP_MUL
, i
->dType
, value
, i
->getSrc(0), value
);
2438 bld
.mkOp1(OP_TRUNC
, i
->dType
, value
, value
);
2439 bld
.mkOp2(OP_MUL
, i
->dType
, value
, i
->getSrc(1), value
);
2441 i
->setSrc(1, value
);
2446 NVC0LoweringPass::handleSQRT(Instruction
*i
)
2448 if (i
->dType
== TYPE_F64
) {
2449 Value
*pred
= bld
.getSSA(1, FILE_PREDICATE
);
2450 Value
*zero
= bld
.loadImm(NULL
, 0.0);
2451 Value
*dst
= bld
.getSSA(8);
2452 bld
.mkOp1(OP_RSQ
, i
->dType
, dst
, i
->getSrc(0));
2453 bld
.mkCmp(OP_SET
, CC_LE
, i
->dType
, pred
, i
->dType
, i
->getSrc(0), zero
);
2454 bld
.mkOp3(OP_SELP
, TYPE_U64
, dst
, zero
, dst
, pred
);
2457 // TODO: Handle this properly with a library function
2459 bld
.setPosition(i
, true);
2461 bld
.mkOp1(OP_RCP
, i
->dType
, i
->getDef(0), i
->getDef(0));
2468 NVC0LoweringPass::handlePOW(Instruction
*i
)
2470 LValue
*val
= bld
.getScratch();
2472 bld
.mkOp1(OP_LG2
, TYPE_F32
, val
, i
->getSrc(0));
2473 bld
.mkOp2(OP_MUL
, TYPE_F32
, val
, i
->getSrc(1), val
)->dnz
= 1;
2474 bld
.mkOp1(OP_PREEX2
, TYPE_F32
, val
, val
);
2484 NVC0LoweringPass::handleEXPORT(Instruction
*i
)
2486 if (prog
->getType() == Program::TYPE_FRAGMENT
) {
2487 int id
= i
->getSrc(0)->reg
.data
.offset
/ 4;
2489 if (i
->src(0).isIndirect(0)) // TODO, ugly
2492 i
->subOp
= NV50_IR_SUBOP_MOV_FINAL
;
2493 i
->src(0).set(i
->src(1));
2495 i
->setDef(0, new_LValue(func
, FILE_GPR
));
2496 i
->getDef(0)->reg
.data
.id
= id
;
2498 prog
->maxGPR
= MAX2(prog
->maxGPR
, id
);
2500 if (prog
->getType() == Program::TYPE_GEOMETRY
) {
2501 i
->setIndirect(0, 1, gpEmitAddress
);
2507 NVC0LoweringPass::handleOUT(Instruction
*i
)
2509 Instruction
*prev
= i
->prev
;
2510 ImmediateValue stream
, prevStream
;
2512 // Only merge if the stream ids match. Also, note that the previous
2513 // instruction would have already been lowered, so we take arg1 from it.
2514 if (i
->op
== OP_RESTART
&& prev
&& prev
->op
== OP_EMIT
&&
2515 i
->src(0).getImmediate(stream
) &&
2516 prev
->src(1).getImmediate(prevStream
) &&
2517 stream
.reg
.data
.u32
== prevStream
.reg
.data
.u32
) {
2518 i
->prev
->subOp
= NV50_IR_SUBOP_EMIT_RESTART
;
2519 delete_Instruction(prog
, i
);
2521 assert(gpEmitAddress
);
2522 i
->setDef(0, gpEmitAddress
);
2523 i
->setSrc(1, i
->getSrc(0));
2524 i
->setSrc(0, gpEmitAddress
);
2529 // Generate a binary predicate if an instruction is predicated by
2530 // e.g. an f32 value.
2532 NVC0LoweringPass::checkPredicate(Instruction
*insn
)
2534 Value
*pred
= insn
->getPredicate();
2537 if (!pred
|| pred
->reg
.file
== FILE_PREDICATE
)
2539 pdst
= new_LValue(func
, FILE_PREDICATE
);
2541 // CAUTION: don't use pdst->getInsn, the definition might not be unique,
2542 // delay turning PSET(FSET(x,y),0) into PSET(x,y) to a later pass
2544 bld
.mkCmp(OP_SET
, CC_NEU
, insn
->dType
, pdst
, insn
->dType
, bld
.mkImm(0), pred
);
2546 insn
->setPredicate(insn
->cc
, pdst
);
2550 // - add quadop dance for texturing
2551 // - put FP outputs in GPRs
2552 // - convert instruction sequences
2555 NVC0LoweringPass::visit(Instruction
*i
)
2558 bld
.setPosition(i
, false);
2560 if (i
->cc
!= CC_ALWAYS
)
2569 return handleTEX(i
->asTex());
2571 return handleTXD(i
->asTex());
2573 return handleTXLQ(i
->asTex());
2575 return handleTXQ(i
->asTex());
2577 bld
.mkOp1(OP_PREEX2
, TYPE_F32
, i
->getDef(0), i
->getSrc(0));
2578 i
->setSrc(0, i
->getDef(0));
2581 return handlePOW(i
);
2583 return handleDIV(i
);
2585 return handleMOD(i
);
2587 return handleSQRT(i
);
2589 ret
= handleEXPORT(i
);
2593 return handleOUT(i
);
2595 return handleRDSV(i
);
2597 return handleWRSV(i
);
2604 const bool cctl
= i
->src(0).getFile() == FILE_MEMORY_BUFFER
;
2606 handleCasExch(i
, cctl
);
2615 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
)
2616 handleSurfaceOpNVE4(i
->asTex());
2618 handleSurfaceOpNVC0(i
->asTex());
2621 handleSUQ(i
->asTex());
2630 /* Kepler+ has a special opcode to compute a new base address to be used
2631 * for indirect loads.
2633 if (targ
->getChipset() >= NVISA_GK104_CHIPSET
&& !i
->perPatch
&&
2634 (i
->op
== OP_VFETCH
|| i
->op
== OP_EXPORT
) && i
->src(0).isIndirect(0)) {
2635 Instruction
*afetch
= bld
.mkOp1(OP_AFETCH
, TYPE_U32
, bld
.getSSA(),
2636 cloneShallow(func
, i
->getSrc(0)));
2637 afetch
->setIndirect(0, 0, i
->getIndirect(0, 0));
2638 i
->src(0).get()->reg
.data
.offset
= 0;
2639 i
->setIndirect(0, 0, afetch
->getDef(0));
2646 TargetNVC0::runLegalizePass(Program
*prog
, CGStage stage
) const
2648 if (stage
== CG_STAGE_PRE_SSA
) {
2649 NVC0LoweringPass
pass(prog
);
2650 return pass
.run(prog
, false, true);
2652 if (stage
== CG_STAGE_POST_RA
) {
2653 NVC0LegalizePostRA
pass(prog
);
2654 return pass
.run(prog
, false, true);
2656 if (stage
== CG_STAGE_SSA
) {
2657 NVC0LegalizeSSA pass
;
2658 return pass
.run(prog
, false, true);
2663 } // namespace nv50_ir