xref: /openbsd-src/gnu/llvm/clang/lib/CodeGen/CodeGenPGO.h (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===--- CodeGenPGO.h - PGO Instrumentation for LLVM CodeGen ----*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // Instrumentation-based profile-guided optimization
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12e5dd7070Spatrick 
13e5dd7070Spatrick #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
14e5dd7070Spatrick #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
15e5dd7070Spatrick 
16e5dd7070Spatrick #include "CGBuilder.h"
17e5dd7070Spatrick #include "CodeGenModule.h"
18e5dd7070Spatrick #include "CodeGenTypes.h"
19e5dd7070Spatrick #include "llvm/ProfileData/InstrProfReader.h"
20e5dd7070Spatrick #include <array>
21e5dd7070Spatrick #include <memory>
22*12c85518Srobert #include <optional>
23e5dd7070Spatrick 
24e5dd7070Spatrick namespace clang {
25e5dd7070Spatrick namespace CodeGen {
26e5dd7070Spatrick 
27e5dd7070Spatrick /// Per-function PGO state.
28e5dd7070Spatrick class CodeGenPGO {
29e5dd7070Spatrick private:
30e5dd7070Spatrick   CodeGenModule &CGM;
31e5dd7070Spatrick   std::string FuncName;
32e5dd7070Spatrick   llvm::GlobalVariable *FuncNameVar;
33e5dd7070Spatrick 
34e5dd7070Spatrick   std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
35e5dd7070Spatrick   unsigned NumRegionCounters;
36e5dd7070Spatrick   uint64_t FunctionHash;
37e5dd7070Spatrick   std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
38e5dd7070Spatrick   std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
39e5dd7070Spatrick   std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
40e5dd7070Spatrick   std::vector<uint64_t> RegionCounts;
41e5dd7070Spatrick   uint64_t CurrentRegionCount;
42e5dd7070Spatrick 
43e5dd7070Spatrick public:
CodeGenPGO(CodeGenModule & CGModule)44ec727ea7Spatrick   CodeGenPGO(CodeGenModule &CGModule)
45ec727ea7Spatrick       : CGM(CGModule), FuncNameVar(nullptr), NumValueSites({{0}}),
46e5dd7070Spatrick         NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0) {}
47e5dd7070Spatrick 
48e5dd7070Spatrick   /// Whether or not we have PGO region data for the current function. This is
49e5dd7070Spatrick   /// false both when we have no data at all and when our data has been
50e5dd7070Spatrick   /// discarded.
haveRegionCounts()51e5dd7070Spatrick   bool haveRegionCounts() const { return !RegionCounts.empty(); }
52e5dd7070Spatrick 
53e5dd7070Spatrick   /// Return the counter value of the current region.
getCurrentRegionCount()54e5dd7070Spatrick   uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
55e5dd7070Spatrick 
56e5dd7070Spatrick   /// Set the counter value for the current region. This is used to keep track
57e5dd7070Spatrick   /// of changes to the most recent counter from control flow and non-local
58e5dd7070Spatrick   /// exits.
setCurrentRegionCount(uint64_t Count)59e5dd7070Spatrick   void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
60e5dd7070Spatrick 
61e5dd7070Spatrick   /// Check if an execution count is known for a given statement. If so, return
62e5dd7070Spatrick   /// true and put the value in Count; else return false.
getStmtCount(const Stmt * S)63*12c85518Srobert   std::optional<uint64_t> getStmtCount(const Stmt *S) const {
64e5dd7070Spatrick     if (!StmtCountMap)
65*12c85518Srobert       return std::nullopt;
66e5dd7070Spatrick     auto I = StmtCountMap->find(S);
67e5dd7070Spatrick     if (I == StmtCountMap->end())
68*12c85518Srobert       return std::nullopt;
69e5dd7070Spatrick     return I->second;
70e5dd7070Spatrick   }
71e5dd7070Spatrick 
72e5dd7070Spatrick   /// If the execution count for the current statement is known, record that
73e5dd7070Spatrick   /// as the current count.
setCurrentStmt(const Stmt * S)74e5dd7070Spatrick   void setCurrentStmt(const Stmt *S) {
75e5dd7070Spatrick     if (auto Count = getStmtCount(S))
76e5dd7070Spatrick       setCurrentRegionCount(*Count);
77e5dd7070Spatrick   }
78e5dd7070Spatrick 
79e5dd7070Spatrick   /// Assign counters to regions and configure them for PGO of a given
80e5dd7070Spatrick   /// function. Does nothing if instrumentation is not enabled and either
81e5dd7070Spatrick   /// generates global variables or associates PGO data with each of the
82e5dd7070Spatrick   /// counters depending on whether we are generating or using instrumentation.
83e5dd7070Spatrick   void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
84e5dd7070Spatrick   /// Emit a coverage mapping range with a counter zero
85e5dd7070Spatrick   /// for an unused declaration.
86e5dd7070Spatrick   void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
87e5dd7070Spatrick                                llvm::GlobalValue::LinkageTypes Linkage);
88e5dd7070Spatrick   // Insert instrumentation or attach profile metadata at value sites
89e5dd7070Spatrick   void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
90e5dd7070Spatrick                     llvm::Instruction *ValueSite, llvm::Value *ValuePtr);
91a9ac8606Spatrick 
92a9ac8606Spatrick   // Set a module flag indicating if value profiling is enabled.
93a9ac8606Spatrick   void setValueProfilingFlag(llvm::Module &M);
94a9ac8606Spatrick 
95e5dd7070Spatrick private:
96e5dd7070Spatrick   void setFuncName(llvm::Function *Fn);
97e5dd7070Spatrick   void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
98e5dd7070Spatrick   void mapRegionCounters(const Decl *D);
99e5dd7070Spatrick   void computeRegionCounts(const Decl *D);
100e5dd7070Spatrick   void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
101e5dd7070Spatrick                                llvm::Function *Fn);
102e5dd7070Spatrick   void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
103e5dd7070Spatrick                         bool IsInMainFile);
104e5dd7070Spatrick   bool skipRegionMappingForDecl(const Decl *D);
105e5dd7070Spatrick   void emitCounterRegionMapping(const Decl *D);
106e5dd7070Spatrick 
107e5dd7070Spatrick public:
108e5dd7070Spatrick   void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S,
109e5dd7070Spatrick                             llvm::Value *StepV);
110e5dd7070Spatrick 
111e5dd7070Spatrick   /// Return the region count for the counter at the given index.
getRegionCount(const Stmt * S)112e5dd7070Spatrick   uint64_t getRegionCount(const Stmt *S) {
113e5dd7070Spatrick     if (!RegionCounterMap)
114e5dd7070Spatrick       return 0;
115e5dd7070Spatrick     if (!haveRegionCounts())
116e5dd7070Spatrick       return 0;
117e5dd7070Spatrick     return RegionCounts[(*RegionCounterMap)[S]];
118e5dd7070Spatrick   }
119e5dd7070Spatrick };
120e5dd7070Spatrick 
121e5dd7070Spatrick }  // end namespace CodeGen
122e5dd7070Spatrick }  // end namespace clang
123e5dd7070Spatrick 
124e5dd7070Spatrick #endif
125