CodeGenPGO.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
  13. #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
  14. #include "CGBuilder.h"
  15. #include "CodeGenModule.h"
  16. #include "CodeGenTypes.h"
  17. #include "llvm/ProfileData/InstrProfReader.h"
  18. #include <array>
  19. #include <memory>
  20. namespace clang {
  21. namespace CodeGen {
  22. /// Per-function PGO state.
  23. class CodeGenPGO {
  24. private:
  25. CodeGenModule &CGM;
  26. std::string FuncName;
  27. llvm::GlobalVariable *FuncNameVar;
  28. std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
  29. unsigned NumRegionCounters;
  30. uint64_t FunctionHash;
  31. std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
  32. std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
  33. std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
  34. std::vector<uint64_t> RegionCounts;
  35. uint64_t CurrentRegionCount;
  36. public:
  37. CodeGenPGO(CodeGenModule &CGModule)
  38. : CGM(CGModule), FuncNameVar(nullptr), NumValueSites({{0}}),
  39. NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0) {}
  40. /// Whether or not we have PGO region data for the current function. This is
  41. /// false both when we have no data at all and when our data has been
  42. /// discarded.
  43. bool haveRegionCounts() const { return !RegionCounts.empty(); }
  44. /// Return the counter value of the current region.
  45. uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
  46. /// Set the counter value for the current region. This is used to keep track
  47. /// of changes to the most recent counter from control flow and non-local
  48. /// exits.
  49. void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
  50. /// Check if an execution count is known for a given statement. If so, return
  51. /// true and put the value in Count; else return false.
  52. Optional<uint64_t> getStmtCount(const Stmt *S) const {
  53. if (!StmtCountMap)
  54. return None;
  55. auto I = StmtCountMap->find(S);
  56. if (I == StmtCountMap->end())
  57. return None;
  58. return I->second;
  59. }
  60. /// If the execution count for the current statement is known, record that
  61. /// as the current count.
  62. void setCurrentStmt(const Stmt *S) {
  63. if (auto Count = getStmtCount(S))
  64. setCurrentRegionCount(*Count);
  65. }
  66. /// Assign counters to regions and configure them for PGO of a given
  67. /// function. Does nothing if instrumentation is not enabled and either
  68. /// generates global variables or associates PGO data with each of the
  69. /// counters depending on whether we are generating or using instrumentation.
  70. void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
  71. /// Emit a coverage mapping range with a counter zero
  72. /// for an unused declaration.
  73. void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
  74. llvm::GlobalValue::LinkageTypes Linkage);
  75. // Insert instrumentation or attach profile metadata at value sites
  76. void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
  77. llvm::Instruction *ValueSite, llvm::Value *ValuePtr);
  78. // Set a module flag indicating if value profiling is enabled.
  79. void setValueProfilingFlag(llvm::Module &M);
  80. private:
  81. void setFuncName(llvm::Function *Fn);
  82. void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
  83. void mapRegionCounters(const Decl *D);
  84. void computeRegionCounts(const Decl *D);
  85. void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
  86. llvm::Function *Fn);
  87. void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
  88. bool IsInMainFile);
  89. bool skipRegionMappingForDecl(const Decl *D);
  90. void emitCounterRegionMapping(const Decl *D);
  91. public:
  92. void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S,
  93. llvm::Value *StepV);
  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