1 /*========================== begin_copyright_notice ============================ 2 3 Copyright (C) 2021 Intel Corporation 4 5 SPDX-License-Identifier: MIT 6 7 ============================= end_copyright_notice ===========================*/ 8 9 #include "common/LLVMWarningsPush.hpp" 10 #include <llvm/IR/Module.h> 11 #include "llvm/IR/DebugInfoMetadata.h" 12 #include "llvm/IR/DebugLoc.h" 13 #include <llvm/Analysis/LoopInfo.h> 14 #include "common/LLVMWarningsPop.hpp" 15 16 #include <3d/common/iStdLib/types.h> 17 #include <common/shaderHash.hpp> 18 #include "KernelInfo.h" 19 20 #ifdef IGC_METRICS__PROTOBUF_ATTACHED 21 #include <google/protobuf/util/json_util.h> 22 #include <Metrics/proto_schema/igc_metrics.pb.h> 23 #include <Metrics/proto_schema/instruction_stats.pb.h> 24 25 #define HashKey size_t 26 #define HashKey_NULL 0 27 #endif // IGC_METRICS 28 29 #pragma once 30 31 namespace IGCMetrics 32 { 33 #ifdef IGC_METRICS__PROTOBUF_ATTACHED 34 typedef std::vector<IGC_METRICS::Function*> MFuncList; 35 #endif 36 37 class IGCMetricImpl 38 { 39 private: 40 bool isEnabled; 41 #ifdef IGC_METRICS__PROTOBUF_ATTACHED 42 IGC_METRICS::Program oclProgram; 43 44 // Helpers 45 // Map all instruction line number, filepath to Function 46 // { Key: Hash(fullPathFile+LineNb), Value:Function list containing this instruction } 47 std::map<HashKey, MFuncList*> map_InstrLoc2Func; 48 // Map Function debuginfo to Function metrics 49 std::map<llvm::DISubprogram*, IGC_METRICS::Function*> map_Func; 50 // helpers for emulated calls 51 std::map<llvm::DILocation*, IGC_METRICS::FuncEmuCalls*> map_EmuCalls; 52 // helpers for loops 53 std::map<llvm::DILocalScope*, IGC_METRICS::CFGStats_Loops*> map_Loops; 54 // Current count of instruction in function 55 int countInstInFunc; 56 57 int CountInstInFunc(llvm::Function* pFunc); 58 59 void GetFunctionCalls(IGC_METRICS::Function* func_m, llvm::Function& func); 60 61 inline MFuncList* GetFuncMetric(llvm::Instruction* pInstr); 62 inline MFuncList* GetFuncMetric(llvm::Loop* pLoop); 63 inline MFuncList* GetFuncMetric(HashKey Key); 64 65 void CollectInstructions(llvm::Module* pModule); 66 67 void UpdateLoopsInfo(); 68 void CollectLoop(llvm::Loop* loop); 69 70 static HashKey GetHash(const char* dir, const char* fileName, int line); 71 static HashKey GetHash(const char* filePathName, int line); 72 static HashKey GetHash(const std::string& dir, const std::string& fileName, int line); 73 static HashKey GetHash(const std::string& filePathName, int line); 74 static HashKey GetHash(llvm::DILocation* Loc); 75 76 static inline void FillCodeRef(IGC_METRICS::CodeRef* codeRef, llvm::DISubprogram* Loc); 77 static inline void FillCodeRef(IGC_METRICS::CodeRef* codeRef, llvm::DILocation* Loc); 78 static inline void FillCodeRef(IGC_METRICS::CodeRef* codeRef, const std::string& filePathName, int line); 79 80 static inline const std::string GetFullPath(const char* dir, const char* fileName); 81 static inline const std::string GetFullPath(const std::string& dir, const std::string& fileName); 82 83 void UpdateCollectInstructions(llvm::Function* func); 84 #endif 85 public: 86 IGCMetricImpl(); 87 ~IGCMetricImpl(); 88 bool Enable(); 89 void Init(ShaderHash* Hash, bool isDebugInfo); 90 91 void CollectLoops(llvm::LoopInfo* loopInfo); 92 void CollectLoops(llvm::Loop* loop); 93 94 void CollectFunctions(llvm::Module* pModule); 95 96 97 void StatBeginEmuFunc(llvm::Instruction* instruction); 98 void StatEndEmuFunc(llvm::Instruction* emulatedInstruction); 99 100 void StatIncCoalesced(llvm::Instruction* coalescedAccess); 101 102 void CollectRegStats(KERNEL_INFO* vISAstats); 103 104 void FinalizeStats(); 105 106 void OutputMetrics(); 107 }; 108 }