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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2023 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#pragma once
#include "Compiler/IGCPassSupport.h"
#include "DebugInfo/VISAIDebugEmitter.hpp"
#include "DebugInfo/VISAModule.hpp"
#include "Probe/Assertion.h"
#include "ShaderCodeGen.hpp"
#include "common/LLVMWarningsPush.hpp"
#include "llvm/Config/llvm-config.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/IR/DIBuilder.h"
#include "common/LLVMWarningsPop.hpp"
using namespace IGC;
namespace IGC {
typedef std::unordered_map<llvm::Value *, unsigned int> InclusionSet;
typedef llvm::SmallPtrSet<llvm::Value *, 32> ValueSet;
typedef llvm::SmallPtrSet<llvm::BasicBlock *, 32> BBSet;
typedef std::unordered_map<llvm::BasicBlock *, ValueSet> DFSet;
typedef std::unordered_map<llvm::BasicBlock *, ValueSet> PhiSet;
typedef std::unordered_map<llvm::BasicBlock *, PhiSet> InPhiSet;
typedef std::unordered_map<llvm::Value *, unsigned int> InsideBlockPressureMap;
class IGCLivenessAnalysisBase {
public:
// contains all values that liveIn into this block
DFSet In;
// this is a redundant set for visualization purposes,
// contains all values that go into PHIs grouped
// by the block from which they are coming
InPhiSet InPhi;
// contains all of the values that liveOut out of this block
DFSet Out;
IGC::CodeGenContext *CGCtx = nullptr;
IGCMD::MetaDataUtils* MDUtils = nullptr;
GenXFunctionGroupAnalysis* FGA = nullptr;
// returns all definitions that were made in the block
// computed by taking difference between In and Out,
// everyting that was originated in the block and got into OUT
ValueSet getDefs(llvm::BasicBlock &BB);
DFSet &getInSet() { return In; }
const DFSet &getInSet() const { return In; }
InPhiSet &getInPhiSet() { return InPhi; }
const InPhiSet &getInPhiSet() const { return InPhi; }
DFSet &getOutSet() { return Out; }
const DFSet &getOutSet() const { return Out; }
unsigned int estimateSizeInBytes(ValueSet &Set, llvm::Function &F, unsigned int SIMD, WIAnalysisRunner* WI = nullptr);
void collectPressureForBB(llvm::BasicBlock &BB,
InsideBlockPressureMap &BBListing,
unsigned int SIMD,
WIAnalysisRunner* WI = nullptr);
SIMDMode bestGuessSIMDSize(Function* F = nullptr);
unsigned int bytesToRegisters(unsigned int Bytes) {
unsigned int RegisterSizeInBytes = registerSizeInBytes();
unsigned int AmountOfRegistersRoundUp =
(Bytes + RegisterSizeInBytes - 1) / RegisterSizeInBytes;
return AmountOfRegistersRoundUp;
}
unsigned int registerSizeInBytes();
void mergeSets(ValueSet *OutSet, llvm::BasicBlock *Succ);
void combineOut(llvm::BasicBlock *BB, ValueSet *Set);
void addToPhiSet(llvm::PHINode *Phi, PhiSet *InPhiSet);
unsigned int addOperandsToSet(llvm::Instruction *Inst, ValueSet &Set, unsigned int SIMD, WIAnalysisRunner* WI, const DataLayout& DL);
void addNonLocalOperandsToSet(llvm::Instruction *Inst, ValueSet &Set);
void processBlock(llvm::BasicBlock *BB, ValueSet &Set, PhiSet *PhiSet);
void livenessAnalysis(llvm::Function &F, BBSet *StartBBs = nullptr);
};
class IGCLivenessAnalysis : public llvm::FunctionPass, public IGCLivenessAnalysisBase {
public:
void publishRegPressureMetadata(llvm::Function& F, unsigned int MaxPressure) {
if (MDUtils->findFunctionsInfoItem(&F) != MDUtils->end_FunctionsInfo()) {
IGC::IGCMD::FunctionInfoMetaDataHandle funcInfoMD = MDUtils->getFunctionsInfoItem(&F);
funcInfoMD->getMaxRegPressure()->setMaxPressure(MaxPressure);
}
}
unsigned int getMaxRegCountForBB(llvm::BasicBlock &BB, unsigned int SIMD, WIAnalysisRunner* WI = nullptr) {
InsideBlockPressureMap PressureMap;
collectPressureForBB(BB, PressureMap, SIMD, WI);
unsigned int MaxSizeInBytes = 0;
for (const auto &Pair : PressureMap) {
MaxSizeInBytes = std::max(MaxSizeInBytes, Pair.second);
}
return bytesToRegisters(MaxSizeInBytes);
}
// be aware, for now, it doesn't count properly nested functions, and their
// register pressure
unsigned int getMaxRegCountForFunction(llvm::Function &F,
unsigned int SIMD, WIAnalysisRunner* WI = nullptr) {
unsigned int Max = 0;
for (BasicBlock &BB : F) {
Max = std::max(getMaxRegCountForBB(BB, SIMD, WI), Max);
}
return Max;
}
unsigned int getMaxRegCountForLoop(llvm::Loop &L,
unsigned int SIMD,
WIAnalysisRunner* WI = nullptr) {
unsigned int Max = 0;
for (BasicBlock *BB : L.getBlocks())
{
unsigned int BBPressure = getMaxRegCountForBB(*BB, SIMD, WI);
Max = std::max(BBPressure, Max);
}
return Max;
}
llvm::BasicBlock *getMaxRegCountBBForFunction(llvm::Function &F, WIAnalysisRunner* WI = nullptr) {
llvm::BasicBlock *HottestBB = NULL;
unsigned int Max = 0;
for (BasicBlock &BB : F) {
unsigned int BBPressure = getMaxRegCountForBB(BB, 8, WI);
HottestBB = BBPressure > Max ? &BB : HottestBB;
Max = std::max(BBPressure, Max);
}
return HottestBB;
}
void releaseMemory() override {
In.clear();
InPhi.clear();
Out.clear();
}
// if you need to recompute pressure analysis after modifications were made
// that can potentially change In Out sets, we need to update them, it's fast
// collectPressureForBB()
// ...
// modifications that change In & Out
// ...
// rerunLivenessAnalysis()
// collectPressureForBB()
void rerunLivenessAnalysis(llvm::Function &F, BBSet *BBs = nullptr) {
if (BBs != nullptr)
{
for (BasicBlock *BB : *BBs)
{
In[BB].clear();
Out[BB].clear();
InPhi[BB].clear();
}
}
else
{
releaseMemory();
}
livenessAnalysis(F, BBs);
}
static char ID;
llvm::StringRef getPassName() const override { return "IGCLivenessAnalysis"; }
IGCLivenessAnalysis();
virtual ~IGCLivenessAnalysis() {}
virtual bool runOnFunction(llvm::Function &F) override;
virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
AU.setPreservesAll();
AU.addRequired<CodeGenContextWrapper>();
AU.addRequired<MetaDataUtilsWrapper>();
}
};
typedef std::unordered_map<llvm::CallInst *, unsigned int> CallSiteToPressureMap;
class IGCFunctionExternalRegPressureAnalysis : public llvm::ModulePass, public IGCLivenessAnalysisBase {
// this map contains external pressure for a function
std::unordered_map<Function *, unsigned int> ExternalFunctionPressure;
// this map contains all the callsites in the module and their pressure
CallSiteToPressureMap CallSitePressure;
// contains all spir_func definitions inside our module, to check against
// if we have 0 of them, we don't have to compute external pressure
llvm::SmallPtrSet<llvm::Function *, 32> SetOfDefinitions;
// already present in IGCLivenessAnalysisBase
//IGC::CodeGenContext *CGCtx = nullptr;
ModuleMetaData* ModMD = nullptr;
std::unique_ptr<WIAnalysisRunner> runWIAnalysis(Function &F);
void generateTableOfPressure(Module &M);
public:
static char ID;
llvm::StringRef getPassName() const override {
return "FunctionExternalPressure";
}
std::unique_ptr<InsideBlockPressureMap> getPressureMapForBB(llvm::BasicBlock &BB,
unsigned int SIMD, WIAnalysisRunner& WI) {
std::unique_ptr<InsideBlockPressureMap> PressureMap = std::make_unique<InsideBlockPressureMap>();
collectPressureForBB(BB, *PressureMap, SIMD, &WI);
return PressureMap;
}
// returns pressure in registers
unsigned int getExternalPressureForFunction(llvm::Function* F) {
unsigned int Registers = bytesToRegisters(ExternalFunctionPressure[F]);
return Registers;
}
IGCFunctionExternalRegPressureAnalysis();
virtual ~IGCFunctionExternalRegPressureAnalysis() {}
bool runOnModule(llvm::Module &M) override;
virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
AU.setPreservesAll();
AU.addRequired<CallGraphWrapperPass>();
AU.addRequired<CodeGenContextWrapper>();
AU.addRequired<MetaDataUtilsWrapper>();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<PostDominatorTreeWrapperPass>();
AU.addRequired<LoopInfoWrapperPass>();
}
void releaseMemory() override {
In.clear();
InPhi.clear();
Out.clear();
ExternalFunctionPressure.clear();
CallSitePressure.clear();
}
};
class IGCRegisterPressurePrinter : public llvm::FunctionPass {
IGCLivenessAnalysis *RPE = nullptr;
WIAnalysis *WI = nullptr;
CodeGenContext *CGCtx = nullptr;
bool DumpToFile = false;
std::string DumpFileName = "default";
// controls printer verbocity
// 1 -> print instruction dump
// 2 -> print with phi listing
// 3 -> print with ssa value names DEF, KILL, IN, OUT
unsigned int PrinterType = IGC_GET_FLAG_VALUE(RegPressureVerbocity);
// maximum potential calling context pressure of a function
unsigned int ExternalPressure = 0;
unsigned int MaxPressureInFunction = 0;
void intraBlock(llvm::BasicBlock &BB, std::string &Output, unsigned int SIMD);
void dumpRegPressure(llvm::Function &F, unsigned int SIMD);
void printInstruction(llvm::Instruction *Inst, std::string &Str);
void printNames(const ValueSet &Set, std::string &name);
void printName(llvm::Value *Val, std::string &String);
void printDefNames(const ValueSet &Set, std::string &name);
void printSets(llvm::BasicBlock *BB, std::string &Output, unsigned int SIMD);
void printDefs(const ValueSet &In, const ValueSet &Out, std::string &Output);
void printPhi(const PhiSet &Set, std::string &Output);
void printIntraBlock(llvm::BasicBlock &BB, std::string &Output, InsideBlockPressureMap &BBListing);
public:
llvm::StringRef getPassName() const override { return "IGCRegPressurePrinter"; }
virtual ~IGCRegisterPressurePrinter() {}
virtual bool runOnFunction(llvm::Function &F) override;
virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
AU.setPreservesAll();
AU.addRequired<IGCLivenessAnalysis>();
AU.addRequired<CodeGenContextWrapper>();
AU.addRequired<WIAnalysis>();
AU.addRequired<IGCFunctionExternalRegPressureAnalysis>();
}
IGCRegisterPressurePrinter();
IGCRegisterPressurePrinter(const std::string& FileName);
static char ID;
};
}; // namespace IGC
|