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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Module.h>
#include <llvm/IR/Instructions.h>
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include <llvm/Analysis/LoopInfo.h>
#include "common/LLVMWarningsPop.hpp"
#include <3d/common/iStdLib/types.h>
#include <common/shaderHash.hpp>
#include "KernelInfo.h"
#include "IGCMetric.h"
#ifdef IGC_METRICS__PROTOBUF_ATTACHED
#include <google/protobuf/util/json_util.h>
#include <Metrics/proto_schema/igc_metrics.pb.h>
#include <Metrics/proto_schema/instruction_stats.pb.h>
#endif // IGC_METRICS
#include "Compiler/CISACodeGen/CVariable.hpp"
#include <Compiler/CISACodeGen/DebugInfoData.hpp>
#include <DebugInfo/VISADebugDecoder.hpp>
#pragma once
namespace IGCMetrics
{
const char* const funcTrackValue = "llvm.igc.metric.trackValue";
#ifdef IGC_METRICS__PROTOBUF_ATTACHED
struct VarData
{
IGC_METRICS::VarInfo* var_m;
llvm::MetadataAsValue* varDILocalVariable;
};
#endif
class IGCMetricImpl
{
private:
friend class CollectFuncData;
friend class CollectSpillFills;
friend class CollectInstrData;
bool isEnabled;
#ifdef IGC_METRICS__PROTOBUF_ATTACHED
IGC_METRICS::Program oclProgram;
IGCLLVM::Module* pModule;
// On end of collecting metrics it will contain binary format of protobuf
void* pMetricData;
// Helpers
// Map user-variables
std::map<llvm::MetadataAsValue*, struct VarData> map_Var;
// Map Function debuginfo to Function metrics
std::map<llvm::DISubprogram*, IGC_METRICS::Function*> map_Func;
// helpers for emulated calls
std::map<llvm::DILocation*, IGC_METRICS::FuncEmuCalls*> map_EmuCalls;
// helpers for loops
std::map<llvm::DILocalScope*, IGC_METRICS::CFGStats_Loops*> map_Loops;
// Current count of instruction in function
int countInstInFunc;
uint fillInstrKindID = 0;
uint spillInstrKindID = 0;
int CountInstInFunc(llvm::Function* pFunc);
void GetFunctionData(IGC_METRICS::Function* func_m, llvm::Function& func);
void UpdateFunctionArgumentsList();
inline struct VarData* GetVarData(llvm::Value* pValue);
inline IGC_METRICS::VarInfo* GetVarMetric(llvm::Value* pValue);
inline IGC_METRICS::VarInfo* AddVarMetric(llvm::DbgVariableIntrinsic* pInstr);
inline IGC_METRICS::Function* GetFuncMetric(const llvm::Instruction* const pInstr);
inline IGC_METRICS::Function* GetFuncMetric(llvm::Instruction* pInstr);
inline IGC_METRICS::Function* GetFuncMetric(llvm::Loop* pLoop);
inline IGC_METRICS::Function* GetFuncMetric(llvm::DISubprogram* pFunc);
inline IGC_METRICS::Function* GetFuncMetric(const llvm::DebugLoc* pLoc);
inline IGC_METRICS::Function* GetFuncMetric(const llvm::DebugLoc& pLoc);
inline IGC_METRICS::Function* GetFuncMetric(llvm::Function* pFunc);
void CollectLoop(llvm::Loop* loop);
void UpdateLoopsInfo();
void UpdateModelCost();
void UpdateMem2RegStats(IGC::VISAModule* CurrentVISA);
void UpdateInstructionStats();
inline void FillCodeRef(IGC_METRICS::CodeRef* codeRef, llvm::DILexicalBlock* Loc);
inline void FillCodeRef(IGC_METRICS::CodeRef* codeRef, llvm::DISubprogram* Loc);
inline void FillCodeRef(IGC_METRICS::CodeRef* codeRef, llvm::DILocation* Loc);
inline void FillCodeRef(IGC_METRICS::CodeRef* codeRef, llvm::DIVariable* Var);
static inline void FillCodeRef(IGC_METRICS::CodeRef* codeRef, const std::string& filePathName, int line);
static inline const std::string GetFullPath(const char* dir, const char* fileName);
static inline const std::string GetFullPath(const std::string& dir, const std::string& fileName);
inline llvm::CallInst* makeTrackCall(const char* const trackCall, ArrayRef<Value*> Args, llvm::Instruction* insertAfter);
static inline llvm::MetadataAsValue* makeMDasVal(llvm::Value* Value);
#endif
public:
IGCMetricImpl();
~IGCMetricImpl();
bool Enable();
size_t getMetricDataSize();
const void* const getMetricData();
void Init(ShaderHash* Hash, bool isDebugInfo);
void CollectLoops(llvm::LoopInfo* loopInfo);
void CollectLoops(llvm::Loop* loop);
void CollectFunctions(llvm::Module* pModule);
void StatBeginEmuFunc(llvm::Instruction* instruction);
void StatEndEmuFunc(llvm::Instruction* emulatedInstruction);
void StatIncCoalesced(llvm::Instruction* coalescedAccess);
void CollectRegStats(KERNEL_INFO* vISAstats, llvm::Function* pFunc);
void UpdateVariable(llvm::Value* Org, llvm::Value* New);
void CollectMem2Reg(llvm::AllocaInst* pAllocaInst, IGC::StatusPrivArr2Reg status);
void CollectLoopCyclomaticComplexity(
llvm::Function* pFunc,
int LoopCyclomaticComplexity,
int LoopCyclomaticComplexity_Max);
void CollectNestedLoopsWithMultipleExits(
llvm::Function* pFunc,
float NestedLoopsWithMultipleExitsRatio,
float NestedLoopsWithMultipleExitsRatio_Max);
void CollectLongStridedLdStInLoop(
llvm::Function* pFunc,
llvm::Loop* pProblematicLoop,
int LongStridedLdStInLoop_LdCnt,
int LongStridedLdStInLoop_StCnt,
int LongStridedLdStInLoop_MaxCntLdOrSt);
void CollectIsGeminiLakeWithDoubles(
llvm::Function* pFunc,
bool IsGeminiLakeWithDoubles);
void CollectDataFromDebugInfo(
llvm::Function* pFunc,
IGC::DebugInfoData *pDebugInfo,
const IGC::VISADebugInfo *pVisaDbgInfo);
void CollectInstructionCnt(
llvm::Function* pFunc,
int InstCnt,
int InstCntMax);
void CollectThreadGroupSize(
llvm::Function* pFunc,
int ThreadGroupSize,
int ThreadGroupSizeMax);
void CollectThreadGroupSizeHint(
llvm::Function* pFunc,
int ThreadGroupSizeHint,
int ThreadGroupSizeHintMax);
void CollectIsSubGroupFuncIn(
llvm::Function* pFunc,
bool flag);
void CollectGen9Gen10WithIEEESqrtDivFunc(
llvm::Function* pFunc,
bool flag);
void CollectNonUniformLoop(
llvm::Function* pFunc,
short LoopCount, llvm::Loop* problematicLoop);
void FinalizeStats();
void OutputMetrics();
};
}
|