CodeGenPGO.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
  14. #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
  15. #include "CGBuilder.h"
  16. #include "CodeGenModule.h"
  17. #include "CodeGenTypes.h"
  18. #include "clang/Frontend/CodeGenOptions.h"
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include <memory>
  22. namespace clang {
  23. namespace CodeGen {
  24. /// Per-function PGO state.
  25. class CodeGenPGO {
  26. private:
  27. CodeGenModule &CGM;
  28. std::string FuncName;
  29. llvm::GlobalVariable *FuncNameVar;
  30. unsigned NumRegionCounters;
  31. uint64_t FunctionHash;
  32. std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
  33. std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
  34. std::vector<uint64_t> RegionCounts;
  35. uint64_t CurrentRegionCount;
  36. /// \brief A flag that is set to true when this function doesn't need
  37. /// to have coverage mapping data.
  38. bool SkipCoverageMapping;
  39. public:
  40. CodeGenPGO(CodeGenModule &CGM)
  41. : CGM(CGM), NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0),
  42. SkipCoverageMapping(false) {}
  43. /// Whether or not we have PGO region data for the current function. This is
  44. /// false both when we have no data at all and when our data has been
  45. /// discarded.
  46. bool haveRegionCounts() const { return !RegionCounts.empty(); }
  47. /// Return the counter value of the current region.
  48. uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
  49. /// Set the counter value for the current region. This is used to keep track
  50. /// of changes to the most recent counter from control flow and non-local
  51. /// exits.
  52. void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
  53. /// Check if an execution count is known for a given statement. If so, return
  54. /// true and put the value in Count; else return false.
  55. Optional<uint64_t> getStmtCount(const Stmt *S) {
  56. if (!StmtCountMap)
  57. return None;
  58. auto I = StmtCountMap->find(S);
  59. if (I == StmtCountMap->end())
  60. return None;
  61. return I->second;
  62. }
  63. /// If the execution count for the current statement is known, record that
  64. /// as the current count.
  65. void setCurrentStmt(const Stmt *S) {
  66. if (auto Count = getStmtCount(S))
  67. setCurrentRegionCount(*Count);
  68. }
  69. /// Check if we need to emit coverage mapping for a given declaration
  70. void checkGlobalDecl(GlobalDecl GD);
  71. /// Assign counters to regions and configure them for PGO of a given
  72. /// function. Does nothing if instrumentation is not enabled and either
  73. /// generates global variables or associates PGO data with each of the
  74. /// counters depending on whether we are generating or using instrumentation.
  75. void assignRegionCounters(const Decl *D, llvm::Function *Fn);
  76. /// Emit a coverage mapping range with a counter zero
  77. /// for an unused declaration.
  78. void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
  79. llvm::GlobalValue::LinkageTypes Linkage);
  80. private:
  81. void setFuncName(llvm::Function *Fn);
  82. void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
  83. void createFuncNameVar(llvm::GlobalValue::LinkageTypes Linkage);
  84. void mapRegionCounters(const Decl *D);
  85. void computeRegionCounts(const Decl *D);
  86. void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
  87. llvm::Function *Fn);
  88. void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
  89. bool IsInMainFile);
  90. void emitCounterVariables();
  91. void emitCounterRegionMapping(const Decl *D);
  92. public:
  93. void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S);
  94. /// Return the region count for the counter at the given index.
  95. uint64_t getRegionCount(const Stmt *S) {
  96. if (!RegionCounterMap)
  97. return 0;
  98. if (!haveRegionCounts())
  99. return 0;
  100. return RegionCounts[(*RegionCounterMap)[S]];
  101. }
  102. };
  103. } // end namespace CodeGen
  104. } // end namespace clang
  105. #endif