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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2018-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "common/LLVMWarningsPush.hpp"
#include <llvm/ADT/PostOrderIterator.h>
#include <llvm/Analysis/LoopInfo.h>
#include <llvm/Analysis/ScalarEvolution.h>
#include <llvm/Analysis/ScalarEvolutionExpressions.h>
#include <llvm/IR/CFG.h>
#include <llvm/IR/PatternMatch.h>
#include <llvm/Pass.h>
#include <llvm/Support/Debug.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Transforms/Utils/Local.h>
#include "llvmWrapper/Transforms/Utils/LoopUtils.h"
#include "common/LLVMWarningsPop.hpp"
#include "GenISAIntrinsics/GenIntrinsics.h"
#include "Compiler/CISACodeGen/ShaderCodeGen.hpp"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/MetaDataUtilsWrapper.h"
#include "Compiler/CISACodeGen/AdvMemOpt.h"
#include "Compiler/CISACodeGen/WIAnalysis.hpp"
#include "Compiler/CISACodeGen/PrepareLoadsStoresUtils.h"
#include "Probe/Assertion.h"
using namespace llvm;
using namespace llvm::PatternMatch;
using namespace IGC;
using namespace IGC::IGCMD;
namespace {
class AdvMemOpt : public FunctionPass {
DominatorTree* DT;
LoopInfo* LI;
PostDominatorTree* PDT;
ScalarEvolution* SE;
WIAnalysis* WI;
public:
static char ID;
AdvMemOpt() : FunctionPass(ID) {
initializeAdvMemOptPass(*PassRegistry::getPassRegistry());
}
bool runOnFunction(Function& F) override;
StringRef getPassName() const override { return "Advanced MemOpt"; }
private:
void getAnalysisUsage(AnalysisUsage& AU) const override {
AU.setPreservesCFG();
AU.addPreservedID(WIAnalysis::ID);
AU.addRequired<CodeGenContextWrapper>();
AU.addRequired<MetaDataUtilsWrapper>();
AU.addRequired<WIAnalysis>();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<LoopInfoWrapperPass>();
AU.addRequired<PostDominatorTreeWrapperPass>();
AU.addRequired<ScalarEvolutionWrapperPass>();
}
bool collectOperandInst(SmallPtrSetImpl<Instruction*>&,
Instruction*, BasicBlock*) const;
bool collectTrivialUser(SmallPtrSetImpl<Instruction*>&,
Instruction*) const;
bool hoistUniformLoad(ArrayRef<BasicBlock*>) const;
bool hoistInst(Instruction *inst, BasicBlock*) const;
bool isLeadCandidate(BasicBlock*) const;
bool hasMemoryWrite(BasicBlock* BB) const;
bool hasMemoryWrite(BasicBlock* Entry, BasicBlock* Exit) const;
};
char AdvMemOpt::ID = 0;
} // End anonymous namespace
FunctionPass* IGC::createAdvMemOptPass() {
return new AdvMemOpt();
}
#define PASS_FLAG "igc-advmemopt"
#define PASS_DESC "Advanced Memory Optimization"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
namespace IGC {
IGC_INITIALIZE_PASS_BEGIN(AdvMemOpt, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(WIAnalysis)
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
IGC_INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
IGC_INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
IGC_INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
IGC_INITIALIZE_PASS_END(AdvMemOpt, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
} // End namespace IGC
bool AdvMemOpt::runOnFunction(Function& F) {
// Skip non-kernel function.
MetaDataUtils* MDU = nullptr;
MDU = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
auto FII = MDU->findFunctionsInfoItem(&F);
if (FII == MDU->end_FunctionsInfo())
return false;
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
WI = &getAnalysis<WIAnalysis>();
SmallVector<Loop*, 8> InnermostLoops;
for (auto I = LI->begin(), E = LI->end(); I != E; ++I)
for (auto DFI = df_begin(*I), DFE = df_end(*I); DFI != DFE; ++DFI) {
Loop* L = *DFI;
if (IGCLLVM::isInnermost(L))
InnermostLoops.push_back(L);
}
for (Loop* L : InnermostLoops) {
SmallVector<BasicBlock*, 8> Line;
BasicBlock* BB = L->getHeader();
while (BB) {
Line.push_back(BB);
BasicBlock* CurrBB = BB;
BB = nullptr;
for (auto BI = succ_begin(CurrBB),
BE = succ_end(CurrBB); BI != BE; ++BI) {
BasicBlock* OtherBB = *BI;
if (CurrBB == OtherBB || !L->contains(OtherBB))
continue;
if (DT->dominates(CurrBB, OtherBB) && PDT->dominates(OtherBB, CurrBB)) {
BB = OtherBB;
break;
}
}
}
hoistUniformLoad(Line);
}
auto* Ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
if (Ctx->platform.isProductChildOf(IGFX_DG2)) {
// split 64-bit uniform store into <2 x i32>, so it has better chance
// to merge with other i32 stores in order to form 16-byte stores that
// can use L1 cache
auto& DL = F.getParent()->getDataLayout();
IRBuilder<> IRB(F.getContext());
for (auto II = inst_begin(&F), EI = inst_end(&F); II != EI; /* empty */) {
auto* I = &*II++;
if (auto* SI = dyn_cast<StoreInst>(I)) {
if (!WI->isUniform(SI))
continue;
unsigned AS = SI->getPointerAddressSpace();
if (AS != ADDRESS_SPACE_PRIVATE &&
AS != ADDRESS_SPACE_GLOBAL)
continue;
IRB.SetInsertPoint(SI);
if (auto NewSI = expand64BitStore(IRB, DL, SI)) {
WI->incUpdateDepend(NewSI, WIAnalysis::UNIFORM_THREAD);
WI->incUpdateDepend(NewSI->getValueOperand(), WIAnalysis::UNIFORM_THREAD);
WI->incUpdateDepend(NewSI->getPointerOperand(), WIAnalysis::UNIFORM_THREAD);
SI->eraseFromParent();
}
}
}
}
// hoist input-based sample instructions in pixel shader into Entry-Block
unsigned MaxSampleHoisted = IGC_GET_FLAG_VALUE(PixelSampleHoistingLimit);
if (Ctx->type == ShaderType::PIXEL_SHADER && MaxSampleHoisted)
{
auto DL = F.getParent()->getDataLayout();
BasicBlock& EntryBlk = F.getEntryBlock();
unsigned LiveOutPressure = 0;
unsigned NumSampleInsts = 0;
unsigned NumOtherInsts = 0;
unsigned MaxLevelDeps = 0;
std::map<Instruction*, unsigned> InstDepLevel;
// check several parameters as the indicator of sampler hoisting:
// the number of sample instructions
// the number of other instructions
// the level of dependencies on sample instructions
// the live-out registers in the entry-block
for (auto BI = EntryBlk.begin(), BE = EntryBlk.end(); BI != BE; ++BI)
{
auto Inst = &*BI;
// compute instruction's dependency level
unsigned DepLevel = 0;
for (Value* Opnd : Inst->operands())
{
if (auto SrcI = dyn_cast<Instruction>(Opnd))
{
if (InstDepLevel.find(SrcI) != InstDepLevel.end())
{
DepLevel = max(DepLevel, InstDepLevel[SrcI]);
}
}
}
if (isSampleLoadGather4InfoInstruction(Inst))
{
NumSampleInsts++;
DepLevel++; // bump up the dependency-level
}
else
NumOtherInsts++;
InstDepLevel[Inst] = DepLevel;
MaxLevelDeps = max(MaxLevelDeps, DepLevel);
if (Inst->isUsedOutsideOfBlock(&EntryBlk))
{
LiveOutPressure += (uint)(DL.getTypeAllocSize(Inst->getType()));
}
}
// hoist sampler instructions from the blocks post-dominiating the entry block
if ((NumSampleInsts * 33) >= NumOtherInsts &&
MaxLevelDeps >= 2 && LiveOutPressure <= 96)
{
auto Node = PDT->getNode(&EntryBlk);
unsigned NumSampleHoisted = 0;
while (Node->getIDom() && NumSampleHoisted < MaxSampleHoisted)
{
Node = Node->getIDom();
BasicBlock* SuccBlk = Node->getBlock();
auto BI = SuccBlk->begin();
auto BE = SuccBlk->end();
auto Inst = &*BI;
while (!isSampleLoadGather4InfoInstruction(Inst) && BI != BE)
{
Inst = &*BI++;
}
while (BI != BE && NumSampleHoisted < MaxSampleHoisted)
{
auto NextCand = &*BI++;
while (!isSampleLoadGather4InfoInstruction(NextCand) && BI != BE)
{
NextCand = &*BI++;
}
if (hoistInst(Inst, &EntryBlk))
{
NumSampleHoisted++;
}
if (BI != BE)
Inst = NextCand;
}
}
}
}
return false;
}
bool AdvMemOpt::isLeadCandidate(BasicBlock* BB) const {
// A candidate lead should have at least one uniform loads. In addition,
// there's no instruction might to write memory from the last uniform loads
// to the end.
for (auto II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
if (II->mayWriteToMemory())
return false;
LoadInst* LD = dyn_cast<LoadInst>(&*II);
if (!LD || !WI->isUniform(LD))
continue;
// Found uniform loads.
return true;
}
return false;
}
namespace {
class RegionSubgraph {
BasicBlock* Exit;
SmallPtrSet<BasicBlock*, 32> Visited;
public:
RegionSubgraph(BasicBlock* E) : Exit(E) {}
bool preVisit(Optional<BasicBlock*> From, BasicBlock* To) {
if (To == Exit)
return false;
return Visited.insert(To).second;
}
};
} // End anonymous namespace
namespace llvm {
template<>
class po_iterator_storage<RegionSubgraph, true> {
RegionSubgraph& RSG;
public:
po_iterator_storage(RegionSubgraph& G) : RSG(G) {}
bool insertEdge(Optional<BasicBlock*> From, BasicBlock* To) {
return RSG.preVisit(From, To);
}
void finishPostorder(BasicBlock*) {}
};
} // End llvm namespace
bool AdvMemOpt::hasMemoryWrite(BasicBlock* BB) const {
for (auto II = BB->begin(), IE = BB->end(); II != IE; ++II)
if (II->mayWriteToMemory())
return true;
return false;
}
bool AdvMemOpt::hasMemoryWrite(BasicBlock* Entry, BasicBlock* Exit) const {
// Entry and Exit must be on line of code.
IGC_ASSERT(nullptr != DT);
IGC_ASSERT(DT->dominates(Entry, Exit));
IGC_ASSERT(nullptr != PDT);
IGC_ASSERT(PDT->dominates(Exit, Entry));
RegionSubgraph RSG(Exit);
for (auto SI = po_ext_begin(Entry, RSG),
SE = po_ext_end(Entry, RSG); SI != SE; ++SI)
if (*SI != Entry && hasMemoryWrite(*SI))
return true;
return false;
}
bool AdvMemOpt::collectOperandInst(SmallPtrSetImpl<Instruction*>& Set,
Instruction* Inst, BasicBlock* LeadingBlock) const {
for (Value* V : Inst->operands()) {
Instruction* I = dyn_cast<Instruction>(V);
if (!I)
continue;
if (isa<PHINode>(I) ||
I->mayHaveSideEffects() ||
I->mayReadOrWriteMemory())
return true;
if (I->getParent() != Inst->getParent())
{
// moving load instruction can be done only if operands
// comes from the same basic block or a dominator of
// the destination basic block. The condition is required
// to counteract using uninitialized or wrong filled registers
if (DT->dominates(I->getParent(), LeadingBlock))
continue;
else
return true;
}
if (collectOperandInst(Set, I, LeadingBlock))
return true;
}
Set.insert(Inst);
return false;
}
bool AdvMemOpt::collectTrivialUser(SmallPtrSetImpl<Instruction*>& Set,
Instruction* Inst) const {
for (auto* U : Inst->users()) {
Instruction* I = dyn_cast<Instruction>(U);
if (!I || I->getParent() != Inst->getParent())
continue;
if (!isa<BitCastInst>(I) && !isa<ExtractElementInst>(I))
continue;
if (collectTrivialUser(Set, I))
return true;
}
Set.insert(Inst);
return false;
}
bool AdvMemOpt::hoistInst(Instruction* LD, BasicBlock* BB) const {
SmallPtrSet<Instruction*, 32> ToHoist;
if (collectOperandInst(ToHoist, LD, BB))
return false;
if (collectTrivialUser(ToHoist, LD))
return false;
BasicBlock* FromBB = LD->getParent();
Instruction* Pos = BB->getTerminator();
for (auto II = FromBB->getFirstNonPHI()->getIterator(),
IE = FromBB->end(); II != IE; /*EMPTY*/) {
Instruction* I = &*II++;
if (ToHoist.count(I)) {
I->moveBefore(Pos);
ToHoist.erase(I);
if (ToHoist.empty())
break;
}
}
return true;
}
bool AdvMemOpt::hoistUniformLoad(ArrayRef<BasicBlock*> Line) const {
bool Changed = false;
// Find the lead BB where to hoist uniform load.
auto BI = Line.begin();
auto BE = Line.end();
while (BI != BE) {
if (!isLeadCandidate(*BI)) {
++BI;
continue;
}
// Found lead.
BasicBlock* Lead = *BI++;
BasicBlock* Prev = Lead;
for (; BI != BE; ++BI) {
BasicBlock* Curr = *BI;
// Check whether it's safe to hoist uniform loads from Curr to Lead by
// checking all blocks between Prev and Curr.
if (hasMemoryWrite(Prev, Curr))
break;
// Hoist uniform loads from Curr into Lead.
for (auto II = Curr->getFirstNonPHI()->getIterator(),
IE = Curr->end(); II != IE; /*EMPTY*/) {
if (II->mayWriteToMemory())
break;
LoadInst* LD = dyn_cast<LoadInst>(&*II++);
if (!LD || !WI->isUniform(LD))
continue;
if (!hoistInst(LD, Lead))
break; // Bail out if any uniform load could not be hoisted safely.
// Reset iterator
II = Curr->getFirstNonPHI()->getIterator();
Changed = true;
}
// After hoisting uniform loads safely, if Curr has memory write, stop
// hoisting further.
if (hasMemoryWrite(Curr))
break;
}
}
return Changed;
}
|