1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "Compiler/CISACodeGen/layout.hpp"
#include "Compiler/CISACodeGen/ShaderCodeGen.hpp"
#include "Compiler/IGCPassSupport.h"
#include "common/debug/Debug.hpp"
#include "common/debug/Dump.hpp"
#include "common/MemStats.h"
#include "common/LLVMUtils.h"
#include <vector>
#include <set>
#include "Probe/Assertion.h"
using namespace llvm;
using namespace IGC;
#define SUCCSZANY (true)
#define SUCCHASINST (succ->size() > 1)
#define SUCCNOINST (succ->size() <= 1)
#define SUCCANYLOOP (true)
#define PUSHSUCC(BLK, C1, C2) \
for(succ_iterator succIter = succ_begin(BLK), succEnd = succ_end(BLK); \
succIter!=succEnd; ++succIter) { \
llvm::BasicBlock *succ = *succIter; \
if (!visitSet.count(succ) && C1 && C2) { \
visitVec.push_back(succ); \
visitSet.insert(succ); \
break; \
} \
}
// Register pass to igc-opt
#define PASS_FLAG "igc-layout"
#define PASS_DESCRIPTION "Layout blocks"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(Layout, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
IGC_INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
IGC_INITIALIZE_PASS_END(Layout, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
char IGC::Layout::ID = 0;
Layout::Layout() : FunctionPass(ID), m_PDT(nullptr)
{
initializeLayoutPass(*PassRegistry::getPassRegistry());
}
void Layout::getAnalysisUsage(llvm::AnalysisUsage& AU) const
{
// Doesn't change the IR at all, it juts move the blocks so no changes in the IR
AU.setPreservesAll();
AU.addRequired<llvm::LoopInfoWrapperPass>();
AU.addRequired<llvm::PostDominatorTreeWrapperPass>();
AU.addRequired<llvm::DominatorTreeWrapperPass>();
}
bool Layout::runOnFunction(Function& func)
{
m_PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
m_DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
LoopInfo& LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
if (LI.empty())
{
LayoutBlocks(func);
}
else
{
LayoutBlocks(func, LI);
}
MEM_SNAPSHOT(IGC::SMS_AFTER_LAYOUTPASS);
return true;
}
// check if the instruction is atomic write (xchg or cmpxchng)
bool Layout::isAtomicWrite(llvm::Instruction* inst, bool onlyLocalMem)
{
if (AtomicRawIntrinsic *atomicRawInst = dyn_cast<AtomicRawIntrinsic>(inst))
{
bool isSpinlock = inst->getOperand(0)->getName() == "spinlock";
bool isLocalMem = inst->getOperand(0)->getType()
->getPointerAddressSpace() == ADDRESS_SPACE_LOCAL;
if ((!onlyLocalMem || isLocalMem) && !isSpinlock)
{
llvm::ConstantInt* opOperand = llvm::dyn_cast<llvm::ConstantInt>(inst->getOperand(3));
if (opOperand)
{
AtomicOp atomicOp = static_cast<AtomicOp>(opOperand->getZExtValue());
if ((atomicOp == EATOMIC_XCHG) || (atomicOp == EATOMIC_CMPXCHG))
{
return true;
}
}
GenISAIntrinsic::ID ID = atomicRawInst->getIntrinsicID();
if (ID == GenISAIntrinsic::GenISA_icmpxchgatomicraw ||
ID == GenISAIntrinsic::GenISA_icmpxchgatomicrawA64 ||
ID == GenISAIntrinsic::GenISA_fcmpxchgatomicraw ||
ID == GenISAIntrinsic::GenISA_fcmpxchgatomicrawA64)
{
return true;
}
}
}
return false;
}
// check if the instruction is atomic read (ATOMIC_OR)
bool Layout::isAtomicRead(llvm::Instruction* inst, bool onlyLocalMem)
{
if (AtomicRawIntrinsic *atomicRawInst = dyn_cast<AtomicRawIntrinsic>(inst))
{
bool isLocalMem = inst->getOperand(0)->getType()
->getPointerAddressSpace() == ADDRESS_SPACE_LOCAL;
if (!onlyLocalMem || isLocalMem)
{
AtomicOp atomicOp = atomicRawInst->getAtomicOp();
if (auto src = llvm::dyn_cast<llvm::ConstantInt>(atomicRawInst->getOperand(2)))
{
return ((atomicOp == EATOMIC_OR) || (atomicOp == EATOMIC_OR64)) && (src->getZExtValue() == 0);
}
}
}
return false;
}
// get memory operand for atomic read or write
llvm::Value* Layout::getMemoryOperand(llvm::Instruction* inst, bool onlyLocalMem)
{
if (isAtomicRead(inst, onlyLocalMem) || isAtomicWrite(inst, onlyLocalMem))
{
llvm::Value* dstAddr = inst->getOperand(1);
if (llvm::PtrToIntInst* pti = llvm::dyn_cast<llvm::PtrToIntInst>(dstAddr))
{
return pti->getPointerOperand();
}
return dstAddr;
}
return nullptr;
}
bool Layout::isReturnBlock(llvm::BasicBlock* bb)
{
return llvm::isa<llvm::ReturnInst>(bb->getTerminator());
}
// Try moving atomic write (or its loop) into the given destination loop
// If there are no direct predecessor in the needed loop,
// Try to move it together with a chain of predecessors. New BB is added in chain if
// it is either single predecessor or it is a previous node in current layout.
//
bool Layout::tryMovingWrite(llvm::Instruction* write, llvm::Loop* loop, LoopInfo& LI)
{
std::vector<llvm::BasicBlock*> blocksToMove;
if (llvm::Loop* writingLoop = LI.getLoopFor(write->getParent()))
{
auto blocks = writingLoop->getBlocks();
for (auto bbi = blocks.rbegin(), bbEnd = blocks.rend(); bbi != bbEnd; ++bbi)
{
llvm::BasicBlock* bb = *bbi;
if (isReturnBlock(bb))
{
return false;
}
blocksToMove.push_back(bb);
}
}
else
{
if (!isReturnBlock(write->getParent()))
{
blocksToMove.push_back(write->getParent());
}
else
{
return false;
}
}
while (true)
{
llvm::BasicBlock* blk = blocksToMove.back();
// If one (and only one) of the predecessors is in the needed loop, move blocks after it
llvm::BasicBlock* insertPoint = nullptr;
int predsInLoop = 0;
for (pred_iterator predIter = pred_begin(blk), predEnd = pred_end(blk);
predIter != predEnd; ++predIter)
{
llvm::BasicBlock* pred = *predIter;
if (loop->contains(pred))
{
predsInLoop++;
insertPoint = pred;
}
}
if (predsInLoop == 1)
{
for (auto bb : blocksToMove)
{
bb->moveAfter(insertPoint);
}
return true;
}
else if (predsInLoop > 1)
{
return false;
}
// Add prev node if it is the predecessor of the block
bool predPushed = false;
for (pred_iterator predIter = pred_begin(blk), predEnd = pred_end(blk);
predIter != predEnd; ++predIter)
{
llvm::BasicBlock* pred = *predIter;
if ((pred == blk->getPrevNode()) && !isReturnBlock(pred))
{
blocksToMove.push_back(pred);
predPushed = true;
break;
}
}
if (predPushed)
{
continue;
}
// Add predecessor if it is single
llvm::BasicBlock* pred = blk->getSinglePredecessor();
if (pred && !isReturnBlock(pred))
{
blocksToMove.push_back(pred);
predPushed = true;
}
else
{
// Don't move the blocks and return
return false;
}
}
}
// Place basic blocks with atomic write (or the whole loop with the
// atomic write) into the other loop if there is an atomic read
// from the same memory, which dominates the write.
//
// It benefits cases like:
//
// Loop:
// Load A
// if (!pred(Load A))
// {
// break;
// }
// if (success(do_work())
// {
// Store A;
// break;
// }
// Br Loop
//
// If the Store is placed after the back edge of the loop
// there will be goto instruction disabling channels based on some
// "success(do_work())" condition placed before the back edge in SIMD control flow,
// and the store will be delayed until the whole loop is finished.
// It makes "if (!pred(Load A))" checking useless and doesn't allow
// to perform early break based on the condition.
//
void Layout::moveAtomicWrites2Loop(Function& func, LoopInfo& LI, bool onlyLocalMem)
{
std::vector<llvm::Instruction*> writes;
std::vector<llvm::Instruction*> reads;
for (auto BI = func.begin(), BE = func.end(); BI != BE; BI++)
{
for (auto II = BI->begin(), IE = BI->end(); II != IE; II++)
{
if (isAtomicWrite(&*II, onlyLocalMem))
{
writes.push_back(&*II);
}
else if (isAtomicRead(&*II, onlyLocalMem))
{
reads.push_back(&*II);
}
}
}
// write: LoopWhereToMove mapping
std::map<Instruction*, llvm::Loop*> writesToMove;
for (auto read : reads)
{
for (auto write : writes)
{
if (getMemoryOperand(read, onlyLocalMem) == getMemoryOperand(write, onlyLocalMem))
{
llvm::Loop* readLoop = LI.getLoopFor(read->getParent());
llvm::Loop* writeLoop = LI.getLoopFor(write->getParent());
if (readLoop && (readLoop != writeLoop)
&& ((m_DT->dominates(read, write))))
{
writesToMove[write] = LI.getLoopFor(read->getParent());
}
}
}
}
for (const auto &pair : writesToMove)
{
llvm::Instruction* write = pair.first;
llvm::Loop* loop = pair.second;
tryMovingWrite(write, loop, LI);
}
}
#define BREAK_BLOCK_SIZE_LIMIT 3
static bool HasThreadGroupBarrierInBlock(BasicBlock * blk)
{
std::string Name = GenISAIntrinsic::getName(GenISAIntrinsic::GenISA_threadgroupbarrier);
Module* Mod = blk->getParent()->getParent();
if (auto GroupBarrier = Mod->getFunction(Name))
{
for (auto U : GroupBarrier->users())
{
auto Inst = dyn_cast<Instruction>(U);
if (Inst && Inst->getParent() == blk)
{
return true;
}
}
}
return false;
}
BasicBlock* Layout::getLastReturnBlock(Function& Func)
{
// If Func has any return BB, return the last return BB (may have multiple);
// otherwise, return the last BB that has no succ;
// or nullptr if every BB has Succ (infinite looping)
BasicBlock* noRetAndNoSucc = nullptr; // for func that never returns
Function::BasicBlockListType& bblist = Func.getBasicBlockList();
for (Function::BasicBlockListType::reverse_iterator RI = bblist.rbegin(),
RE = bblist.rend(); RI != RE; ++RI)
{
BasicBlock* bb = &*RI;
if (succ_begin(bb) == succ_end(bb))
{
if (isa_and_nonnull<ReturnInst>(bb->getTerminator()))
{
return bb;
}
if (!noRetAndNoSucc)
{
noRetAndNoSucc = bb;
}
}
}
// Function does not have a return block
return noRetAndNoSucc;
}
//
// selectSucc: select a succ with condition SelectNoInstBlk and return it.
//
// This is used to select one if there are two Successors with condition
// SelectNoInstBlk, rather than take the first one in the succ list.
//
// Condition SelectNoInstBlk: If SelectNoInstBlk is true, select an empty
// block, if it is false, select non-empty block.
//
BasicBlock* Layout::selectSucc(
BasicBlock* CurrBlk,
bool SelectNoInstBlk,
const LoopInfo& LI,
const std::set<BasicBlock*>& VisitSet)
{
SmallVector<BasicBlock*, 4> Succs;
for (succ_iterator SI = succ_begin(CurrBlk), SE = succ_end(CurrBlk);
SI != SE; ++SI)
{
BasicBlock* succ = *SI;
if (VisitSet.count(succ) == 0 &&
((SelectNoInstBlk && succ->size() <= 1) ||
(!SelectNoInstBlk && succ->size() > 1)))
{
Succs.push_back(succ);
}
}
// Right now, only handle the case of two empty blocks.
// If it has no two empty blocks, just take the first
// one and return it.
if (Succs.size() != 2 || !SelectNoInstBlk) {
return Succs.empty() ? nullptr : Succs[0];
}
// For two empty blocks, the case we want to handle
// is the following:
//
// (B0 = CurrBlk)
// B0 : if (c) goto THEN (else goto ELSE)
// ELSE : goto B2
// B1 : ....
// B2 : ....
// ......
// Bn :
// (ELSE, B1, B2, ..., Bn) has END as single exit
// THEN: goto END:
// END :
// PHI...
//
// where ELSE and THEN are empty BBs, and END has phi in it.
// In this case, THEN and ELSE might have phi moves as the result
// DeSSA when emitting visa. For example, suppose d0 = s0 will
// be emitted in THEN. If s0 is dead after THEN, it would be good
// to lay out THEN right after B0 as the live-range of s0 will not
// be overlapped with ones in ELSE. (If s0 is live out of THEN,
// moving THEN right after B0 or right before END does not matter
// as far as liveness is concerned.). To lay out THEN first, this
// function will select ELSE to return (as the algo does layout
// backward).
//
// For simplicity, assume those BBs are not inside loops. It could
// be applied to Loop later when appropriate testing is done.
BasicBlock* S0 = Succs[0], * S1 = Succs[1];
BasicBlock* SS0 = S0->getSingleSuccessor();
if (SS0 && (SS0 != S1) && isa<PHINode>(&*SS0->begin()) &&
!LI.getLoopFor(S0) &&
m_PDT->dominates(SS0, S1))
{
return S1;
}
return S0;
}
void Layout::LayoutBlocks(Function& func, LoopInfo& LI)
{
std::vector<llvm::BasicBlock*> visitVec;
std::set<llvm::BasicBlock*> visitSet;
// Insertion Position per loop header
std::map<llvm::BasicBlock*, llvm::BasicBlock*> InsPos;
llvm::BasicBlock* entry = &(func.getEntryBlock());
visitVec.push_back(entry);
visitSet.insert(entry);
InsPos[entry] = entry;
// Push a return block to make sure the last BB is the return block.
if (BasicBlock * lastReturnBlock = getLastReturnBlock(func))
{
if (lastReturnBlock != entry)
{
visitVec.push_back(lastReturnBlock);
visitSet.insert(lastReturnBlock);
}
}
while (!visitVec.empty())
{
llvm::BasicBlock* blk = visitVec.back();
llvm::Loop* curLoop = LI.getLoopFor(blk);
if (curLoop)
{
auto hd = curLoop->getHeader();
if (blk == hd && InsPos.find(hd) == InsPos.end())
{
InsPos[blk] = blk;
}
}
// FIXME: this is a hack to workaround an irreducible test case
if (func.getName() == "ocl_test_kernel")
{
// push: time for DFS visit
PUSHSUCC(blk, SUCCANYLOOP, SUCCNOINST);
if (blk != visitVec.back())
continue;
// push: time for DFS visit
PUSHSUCC(blk, SUCCANYLOOP, SUCCHASINST);
}
else
{
// push: time for DFS visit
PUSHSUCC(blk, SUCCANYLOOP, SUCCHASINST);
if (blk != visitVec.back())
continue;
// push: time for DFS visit
if (BasicBlock * aBlk = selectSucc(blk, true, LI, visitSet))
{
visitVec.push_back(aBlk);
visitSet.insert(aBlk);
continue;
}
//PUSHSUCC(blk, SUCCANYLOOP, SUCCNOINST);
}
// pop: time to move the block to the right location
if (blk == visitVec.back())
{
visitVec.pop_back();
if (curLoop)
{
auto hd = curLoop->getHeader();
if (blk != hd)
{
// move the block to the beginning of the loop
auto insp = InsPos[hd];
IGC_ASSERT(insp);
if (blk != insp)
{
blk->moveBefore(insp);
InsPos[hd] = blk;
}
}
else
{
// move the entire loop to the beginning of
// the parent loop
auto LoopStart = InsPos[hd];
IGC_ASSERT(LoopStart);
auto PaLoop = curLoop->getParentLoop();
auto PaHd = PaLoop ? PaLoop->getHeader() : entry;
auto insp = InsPos[PaHd];
if (LoopStart == hd)
{
// single-block loop
hd->moveBefore(insp);
}
else
{
// loop-header is not moved yet, so should be at the end
// use splice
llvm::Function::BasicBlockListType& BBList = func.getBasicBlockList();
BBList.splice(insp->getIterator(), BBList,
LoopStart->getIterator(),
hd->getIterator());
hd->moveBefore(LoopStart);
}
InsPos[PaHd] = hd;
}
}
else
{
auto insp = InsPos[entry];
if (blk != insp)
{
blk->moveBefore(insp);
InsPos[entry] = blk;
}
}
}
}
moveAtomicWrites2Loop(func, LI, false);
// if function has a single exit, then the last block must be an exit
// comment this out due to infinite loop example in OCL
// IGC_ASSERT(PDT.getRootNode()->getBlock() == 0x0 || PDT.getRootNode()->getBlock() == &(func.getBasicBlockList().back()));
// fix the loop-exit pattern, put break-blocks into the loop
for (llvm::Function::iterator blkIter = func.begin(), blkEnd = func.end();
blkIter != blkEnd; ++blkIter)
{
llvm::BasicBlock* blk = &(*blkIter);
llvm::Loop* curLoop = LI.getLoopFor(blk);
bool allPredLoopExit = true;
unsigned numPreds = 0;
llvm::SmallPtrSet<llvm::BasicBlock*, 4> predSet;
for (pred_iterator predIter = pred_begin(blk), predEnd = pred_end(blk);
predIter != predEnd; ++predIter)
{
llvm::BasicBlock* pred = *predIter;
numPreds++;
llvm::Loop* predLoop = LI.getLoopFor(pred);
if (curLoop == predLoop)
{
llvm::BasicBlock* predPred = pred->getSinglePredecessor();
if (predPred)
{
llvm::Loop* predPredLoop = LI.getLoopFor(predPred);
if (predPredLoop != curLoop &&
(!curLoop || curLoop->contains(predPredLoop)))
{
if (pred->size() <= BREAK_BLOCK_SIZE_LIMIT &&
!HasThreadGroupBarrierInBlock(pred))
{
predSet.insert(pred);
}
else
{
allPredLoopExit = false;
break;
}
}
}
}
else if (!curLoop || curLoop->contains(predLoop))
continue;
else
{
allPredLoopExit = false;
break;
}
}
if (allPredLoopExit && numPreds > 1)
{
for (SmallPtrSet<BasicBlock*, 4>::iterator predIter = predSet.begin(),
predEnd = predSet.end(); predIter != predEnd; ++predIter)
{
llvm::BasicBlock* pred = *predIter;
llvm::BasicBlock* predPred = pred->getSinglePredecessor();
pred->moveAfter(predPred);
}
}
}
}
void Layout::LayoutBlocks(Function& func)
{
std::vector<llvm::BasicBlock*> visitVec;
std::set<llvm::BasicBlock*> visitSet;
// Reorder basic block to allow more fall-through
llvm::BasicBlock* entry = &(func.getEntryBlock());
visitVec.push_back(entry);
// Push a return block to make sure the last BB is the return block.
if (BasicBlock * lastReturnBlock = getLastReturnBlock(func))
{
if (lastReturnBlock != entry)
{
visitVec.push_back(lastReturnBlock);
visitSet.insert(lastReturnBlock);
}
}
while (!visitVec.empty())
{
llvm::BasicBlock* blk = visitVec.back();
// push in the empty successor
PUSHSUCC(blk, SUCCANYLOOP, SUCCNOINST);
if (blk != visitVec.back())
continue;
// push in all the same-loop successors
PUSHSUCC(blk, SUCCANYLOOP, SUCCSZANY);
// pop
if (blk == visitVec.back())
{
visitVec.pop_back();
if (blk != entry)
{
blk->moveBefore(entry);
entry = blk;
}
}
}
}
|