Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [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 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 14 | #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H |
| 15 | #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 16 | |
| 17 | #include "CGBuilder.h" |
| 18 | #include "CodeGenModule.h" |
| 19 | #include "CodeGenTypes.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/CodeGenOptions.h" |
Betul Buyukkurt | 518276a | 2016-01-23 22:50:44 +0000 | [diff] [blame] | 21 | #include "llvm/ProfileData/InstrProfReader.h" |
Betul Buyukkurt | 7e15272 | 2016-01-24 00:56:19 +0000 | [diff] [blame] | 22 | #include <array> |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 23 | #include <memory> |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 24 | |
| 25 | namespace clang { |
| 26 | namespace CodeGen { |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 27 | |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 28 | /// Per-function PGO state. |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 29 | class CodeGenPGO { |
| 30 | private: |
| 31 | CodeGenModule &CGM; |
Justin Bogner | 111c653 | 2014-12-02 23:15:30 +0000 | [diff] [blame] | 32 | std::string FuncName; |
Justin Bogner | 970ac60 | 2014-12-08 19:04:51 +0000 | [diff] [blame] | 33 | llvm::GlobalVariable *FuncNameVar; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 34 | |
Betul Buyukkurt | 7e15272 | 2016-01-24 00:56:19 +0000 | [diff] [blame] | 35 | std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 36 | unsigned NumRegionCounters; |
Justin Bogner | b4416f5 | 2014-03-18 21:58:06 +0000 | [diff] [blame] | 37 | uint64_t FunctionHash; |
Duncan P. N. Exon Smith | 1b67cfd | 2014-03-26 19:26:05 +0000 | [diff] [blame] | 38 | std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap; |
| 39 | std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap; |
Betul Buyukkurt | 518276a | 2016-01-23 22:50:44 +0000 | [diff] [blame] | 40 | std::unique_ptr<llvm::InstrProfRecord> ProfRecord; |
Justin Bogner | 7f8cf5b | 2014-12-02 22:38:52 +0000 | [diff] [blame] | 41 | std::vector<uint64_t> RegionCounts; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 42 | uint64_t CurrentRegionCount; |
| 43 | |
| 44 | public: |
| 45 | CodeGenPGO(CodeGenModule &CGM) |
Vedant Kumar | 9e32aa2 | 2017-04-24 20:54:36 +0000 | [diff] [blame] | 46 | : CGM(CGM), NumValueSites({{0}}), NumRegionCounters(0), FunctionHash(0), |
| 47 | CurrentRegionCount(0) {} |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 48 | |
| 49 | /// Whether or not we have PGO region data for the current function. This is |
| 50 | /// false both when we have no data at all and when our data has been |
| 51 | /// discarded. |
Justin Bogner | 7f8cf5b | 2014-12-02 22:38:52 +0000 | [diff] [blame] | 52 | bool haveRegionCounts() const { return !RegionCounts.empty(); } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 53 | |
| 54 | /// Return the counter value of the current region. |
| 55 | uint64_t getCurrentRegionCount() const { return CurrentRegionCount; } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 56 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 57 | /// Set the counter value for the current region. This is used to keep track |
| 58 | /// of changes to the most recent counter from control flow and non-local |
| 59 | /// exits. |
| 60 | void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; } |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 61 | |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 62 | /// Check if an execution count is known for a given statement. If so, return |
| 63 | /// true and put the value in Count; else return false. |
Justin Bogner | 1c21c28 | 2015-04-13 12:23:19 +0000 | [diff] [blame] | 64 | Optional<uint64_t> getStmtCount(const Stmt *S) { |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 65 | if (!StmtCountMap) |
Justin Bogner | 1c21c28 | 2015-04-13 12:23:19 +0000 | [diff] [blame] | 66 | return None; |
| 67 | auto I = StmtCountMap->find(S); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 68 | if (I == StmtCountMap->end()) |
Justin Bogner | 1c21c28 | 2015-04-13 12:23:19 +0000 | [diff] [blame] | 69 | return None; |
| 70 | return I->second; |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /// If the execution count for the current statement is known, record that |
| 74 | /// as the current count. |
| 75 | void setCurrentStmt(const Stmt *S) { |
Justin Bogner | 1c21c28 | 2015-04-13 12:23:19 +0000 | [diff] [blame] | 76 | if (auto Count = getStmtCount(S)) |
| 77 | setCurrentRegionCount(*Count); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 80 | /// Assign counters to regions and configure them for PGO of a given |
| 81 | /// function. Does nothing if instrumentation is not enabled and either |
| 82 | /// generates global variables or associates PGO data with each of the |
| 83 | /// counters depending on whether we are generating or using instrumentation. |
Serge Pavlov | 3a56145 | 2015-12-06 14:32:39 +0000 | [diff] [blame] | 84 | void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 85 | /// Emit a coverage mapping range with a counter zero |
| 86 | /// for an unused declaration. |
| 87 | void emitEmptyCounterMapping(const Decl *D, StringRef FuncName, |
| 88 | llvm::GlobalValue::LinkageTypes Linkage); |
Betul Buyukkurt | 518276a | 2016-01-23 22:50:44 +0000 | [diff] [blame] | 89 | // Insert instrumentation or attach profile metadata at value sites |
| 90 | void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind, |
| 91 | llvm::Instruction *ValueSite, llvm::Value *ValuePtr); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 92 | private: |
Bob Wilson | da1ebed | 2014-03-06 04:55:41 +0000 | [diff] [blame] | 93 | void setFuncName(llvm::Function *Fn); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 94 | void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 95 | void mapRegionCounters(const Decl *D); |
Bob Wilson | bf854f0 | 2014-02-17 19:21:09 +0000 | [diff] [blame] | 96 | void computeRegionCounts(const Decl *D); |
Justin Bogner | 837a6f6 | 2014-04-18 21:52:00 +0000 | [diff] [blame] | 97 | void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader, |
| 98 | llvm::Function *Fn); |
Justin Bogner | 40b8ba1 | 2014-06-26 01:45:07 +0000 | [diff] [blame] | 99 | void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader, |
| 100 | bool IsInMainFile); |
Vedant Kumar | c468bb8 | 2016-07-11 22:57:44 +0000 | [diff] [blame] | 101 | bool skipRegionMappingForDecl(const Decl *D); |
Alex Lorenz | ee02499 | 2014-08-04 18:41:51 +0000 | [diff] [blame] | 102 | void emitCounterRegionMapping(const Decl *D); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 103 | |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 104 | public: |
Vedant Kumar | 502bbfa | 2017-02-25 06:35:45 +0000 | [diff] [blame] | 105 | void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S, |
| 106 | llvm::Value *StepV); |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 107 | |
| 108 | /// Return the region count for the counter at the given index. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 109 | uint64_t getRegionCount(const Stmt *S) { |
| 110 | if (!RegionCounterMap) |
| 111 | return 0; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 112 | if (!haveRegionCounts()) |
| 113 | return 0; |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 114 | return RegionCounts[(*RegionCounterMap)[S]]; |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 115 | } |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Justin Bogner | ef512b9 | 2014-01-06 22:27:43 +0000 | [diff] [blame] | 118 | } // end namespace CodeGen |
| 119 | } // end namespace clang |
| 120 | |
| 121 | #endif |