CodeGenPGO.h 4.5 KB

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