1 //===--- CodeGenPGO.h - PGO Instrumentation for LLVM CodeGen ----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Instrumentation-based profile-guided optimization 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H 14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H 15 16 #include "CGBuilder.h" 17 #include "CodeGenModule.h" 18 #include "CodeGenTypes.h" 19 #include "MCDCState.h" 20 #include "llvm/ProfileData/InstrProfReader.h" 21 #include <array> 22 #include <memory> 23 #include <optional> 24 25 namespace clang { 26 namespace CodeGen { 27 28 /// Per-function PGO state. 29 class CodeGenPGO { 30 private: 31 CodeGenModule &CGM; 32 std::string FuncName; 33 llvm::GlobalVariable *FuncNameVar; 34 35 std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites; 36 unsigned NumRegionCounters; 37 uint64_t FunctionHash; 38 std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap; 39 std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap; 40 std::unique_ptr<llvm::InstrProfRecord> ProfRecord; 41 std::unique_ptr<MCDC::State> RegionMCDCState; 42 std::vector<uint64_t> RegionCounts; 43 uint64_t CurrentRegionCount; 44 45 public: CodeGenPGO(CodeGenModule & CGModule)46 CodeGenPGO(CodeGenModule &CGModule) 47 : CGM(CGModule), FuncNameVar(nullptr), NumValueSites({{0}}), 48 NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0) {} 49 50 /// Whether or not we have PGO region data for the current function. This is 51 /// false both when we have no data at all and when our data has been 52 /// discarded. haveRegionCounts()53 bool haveRegionCounts() const { return !RegionCounts.empty(); } 54 55 /// Return the counter value of the current region. getCurrentRegionCount()56 uint64_t getCurrentRegionCount() const { return CurrentRegionCount; } 57 58 /// Set the counter value for the current region. This is used to keep track 59 /// of changes to the most recent counter from control flow and non-local 60 /// exits. setCurrentRegionCount(uint64_t Count)61 void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; } 62 63 /// Check if an execution count is known for a given statement. If so, return 64 /// true and put the value in Count; else return false. getStmtCount(const Stmt * S)65 std::optional<uint64_t> getStmtCount(const Stmt *S) const { 66 if (!StmtCountMap) 67 return std::nullopt; 68 auto I = StmtCountMap->find(S); 69 if (I == StmtCountMap->end()) 70 return std::nullopt; 71 return I->second; 72 } 73 74 /// If the execution count for the current statement is known, record that 75 /// as the current count. setCurrentStmt(const Stmt * S)76 void setCurrentStmt(const Stmt *S) { 77 if (auto Count = getStmtCount(S)) 78 setCurrentRegionCount(*Count); 79 } 80 81 /// Assign counters to regions and configure them for PGO of a given 82 /// function. Does nothing if instrumentation is not enabled and either 83 /// generates global variables or associates PGO data with each of the 84 /// counters depending on whether we are generating or using instrumentation. 85 void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn); 86 /// Emit a coverage mapping range with a counter zero 87 /// for an unused declaration. 88 void emitEmptyCounterMapping(const Decl *D, StringRef FuncName, 89 llvm::GlobalValue::LinkageTypes Linkage); 90 // Insert instrumentation or attach profile metadata at value sites 91 void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind, 92 llvm::Instruction *ValueSite, llvm::Value *ValuePtr); 93 94 // Set a module flag indicating if value profiling is enabled. 95 void setValueProfilingFlag(llvm::Module &M); 96 97 void setProfileVersion(llvm::Module &M); 98 99 private: 100 void setFuncName(llvm::Function *Fn); 101 void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage); 102 void mapRegionCounters(const Decl *D); 103 void computeRegionCounts(const Decl *D); 104 void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader, 105 llvm::Function *Fn); 106 void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader, 107 bool IsInMainFile); 108 bool skipRegionMappingForDecl(const Decl *D); 109 void emitCounterRegionMapping(const Decl *D); 110 bool canEmitMCDCCoverage(const CGBuilderTy &Builder); 111 112 public: 113 void emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S, 114 llvm::Value *StepV); 115 void emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S, 116 Address MCDCCondBitmapAddr, 117 CodeGenFunction &CGF); 118 void emitMCDCParameters(CGBuilderTy &Builder); 119 void emitMCDCCondBitmapReset(CGBuilderTy &Builder, const Expr *S, 120 Address MCDCCondBitmapAddr); 121 void emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S, 122 Address MCDCCondBitmapAddr, llvm::Value *Val, 123 CodeGenFunction &CGF); 124 125 /// Return the region count for the counter at the given index. getRegionCount(const Stmt * S)126 uint64_t getRegionCount(const Stmt *S) { 127 if (!RegionCounterMap) 128 return 0; 129 if (!haveRegionCounts()) 130 return 0; 131 // With profiles from a differing version of clang we can have mismatched 132 // decl counts. Don't crash in such a case. 133 auto Index = (*RegionCounterMap)[S]; 134 if (Index >= RegionCounts.size()) 135 return 0; 136 return RegionCounts[Index]; 137 } 138 }; 139 140 } // end namespace CodeGen 141 } // end namespace clang 142 143 #endif 144