Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1 | //===--- CodeGenPGO.h - PGO Instrumentation for LLVM CodeGen ----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Instrumentation-based profile-guided optimization |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 14 | #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H |
| 15 | #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 16 | |
| 17 | #include "CGBuilder.h" |
| 18 | #include "CodeGenModule.h" |
| 19 | #include "CodeGenTypes.h" |
| 20 | #include "clang/Frontend/CodeGenOptions.h" |
| 21 | #include "llvm/ADT/StringMap.h" |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 22 | #include "llvm/ProfileData/InstrProfReader.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 24 | #include <array> |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 25 | #include <memory> |
| 26 | |
| 27 | namespace clang { |
| 28 | namespace CodeGen { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 29 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 30 | /// Per-function PGO state. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 31 | class CodeGenPGO { |
| 32 | private: |
| 33 | CodeGenModule &CGM; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 34 | std::string FuncName; |
| 35 | llvm::GlobalVariable *FuncNameVar; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 36 | |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 37 | std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 38 | unsigned NumRegionCounters; |
| 39 | uint64_t FunctionHash; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 40 | std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap; |
| 41 | std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap; |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 42 | std::unique_ptr<llvm::InstrProfRecord> ProfRecord; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 43 | std::vector<uint64_t> RegionCounts; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 44 | uint64_t CurrentRegionCount; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 45 | /// \brief A flag that is set to true when this function doesn't need |
| 46 | /// to have coverage mapping data. |
| 47 | bool SkipCoverageMapping; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 48 | |
| 49 | public: |
| 50 | CodeGenPGO(CodeGenModule &CGM) |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 51 | : CGM(CGM), NumValueSites({{0}}), NumRegionCounters(0), |
| 52 | FunctionHash(0), CurrentRegionCount(0), SkipCoverageMapping(false) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 53 | |
| 54 | /// Whether or not we have PGO region data for the current function. This is |
| 55 | /// false both when we have no data at all and when our data has been |
| 56 | /// discarded. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 57 | bool haveRegionCounts() const { return !RegionCounts.empty(); } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 58 | |
| 59 | /// Return the counter value of the current region. |
| 60 | uint64_t getCurrentRegionCount() const { return CurrentRegionCount; } |
| 61 | |
| 62 | /// Set the counter value for the current region. This is used to keep track |
| 63 | /// of changes to the most recent counter from control flow and non-local |
| 64 | /// exits. |
| 65 | void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; } |
| 66 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 67 | /// Check if an execution count is known for a given statement. If so, return |
| 68 | /// true and put the value in Count; else return false. |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 69 | Optional<uint64_t> getStmtCount(const Stmt *S) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 70 | if (!StmtCountMap) |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 71 | return None; |
| 72 | auto I = StmtCountMap->find(S); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 73 | if (I == StmtCountMap->end()) |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 74 | return None; |
| 75 | return I->second; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /// If the execution count for the current statement is known, record that |
| 79 | /// as the current count. |
| 80 | void setCurrentStmt(const Stmt *S) { |
Pirama Arumuga Nainar | 58878f8 | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 81 | if (auto Count = getStmtCount(S)) |
| 82 | setCurrentRegionCount(*Count); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 85 | /// Assign counters to regions and configure them for PGO of a given |
| 86 | /// function. Does nothing if instrumentation is not enabled and either |
| 87 | /// generates global variables or associates PGO data with each of the |
| 88 | /// counters depending on whether we are generating or using instrumentation. |
Pirama Arumuga Nainar | 87d948e | 2016-03-03 15:49:35 -0800 | [diff] [blame] | 89 | void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 90 | /// Emit a coverage mapping range with a counter zero |
| 91 | /// for an unused declaration. |
| 92 | void emitEmptyCounterMapping(const Decl *D, StringRef FuncName, |
| 93 | llvm::GlobalValue::LinkageTypes Linkage); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 94 | // Insert instrumentation or attach profile metadata at value sites |
| 95 | void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind, |
| 96 | llvm::Instruction *ValueSite, llvm::Value *ValuePtr); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 97 | private: |
| 98 | void setFuncName(llvm::Function *Fn); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 99 | void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 100 | void mapRegionCounters(const Decl *D); |
| 101 | void computeRegionCounts(const Decl *D); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 102 | void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader, |
| 103 | llvm::Function *Fn); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 104 | void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader, |
| 105 | bool IsInMainFile); |
Pirama Arumuga Nainar | 4967a71 | 2016-09-19 22:19:55 -0700 | [diff] [blame^] | 106 | bool skipRegionMappingForDecl(const Decl *D); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 107 | void emitCounterRegionMapping(const Decl *D); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 108 | |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 109 | public: |
| 110 | void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 111 | |
| 112 | /// Return the region count for the counter at the given index. |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 113 | uint64_t getRegionCount(const Stmt *S) { |
| 114 | if (!RegionCounterMap) |
| 115 | return 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 116 | if (!haveRegionCounts()) |
| 117 | return 0; |
Pirama Arumuga Nainar | b6d6993 | 2015-07-01 12:25:36 -0700 | [diff] [blame] | 118 | return RegionCounts[(*RegionCounterMap)[S]]; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 119 | } |
| 120 | }; |
| 121 | |
| 122 | } // end namespace CodeGen |
| 123 | } // end namespace clang |
| 124 | |
| 125 | #endif |