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 
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
14 #define LLVM_CLANG_LIB_CODEGEN_CODEGENPGO_H
15 
16 #include "CGBuilder.h"
17 #include "CodeGenModule.h"
18 #include "CodeGenTypes.h"
19 #include "llvm/ProfileData/InstrProfReader.h"
20 #include <array>
21 #include <memory>
22 #include <optional>
23 
24 namespace clang {
25 namespace CodeGen {
26 
27 /// Per-function PGO state.
28 class CodeGenPGO {
29 private:
30   CodeGenModule &CGM;
31   std::string FuncName;
32   llvm::GlobalVariable *FuncNameVar;
33 
34   std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
35   unsigned NumRegionCounters;
36   unsigned MCDCBitmapBytes;
37   uint64_t FunctionHash;
38   std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
39   std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionMCDCBitmapMap;
40   std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCondIDMap;
41   std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
42   std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
43   std::vector<uint64_t> RegionCounts;
44   uint64_t CurrentRegionCount;
45 
46 public:
CodeGenPGO(CodeGenModule & CGModule)47   CodeGenPGO(CodeGenModule &CGModule)
48       : CGM(CGModule), FuncNameVar(nullptr), NumValueSites({{0}}),
49         NumRegionCounters(0), MCDCBitmapBytes(0), FunctionHash(0),
50         CurrentRegionCount(0) {}
51 
52   /// Whether or not we have PGO region data for the current function. This is
53   /// false both when we have no data at all and when our data has been
54   /// discarded.
haveRegionCounts()55   bool haveRegionCounts() const { return !RegionCounts.empty(); }
56 
57   /// Return the counter value of the current region.
getCurrentRegionCount()58   uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
59 
60   /// Set the counter value for the current region. This is used to keep track
61   /// of changes to the most recent counter from control flow and non-local
62   /// exits.
setCurrentRegionCount(uint64_t Count)63   void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
64 
65   /// Check if an execution count is known for a given statement. If so, return
66   /// true and put the value in Count; else return false.
getStmtCount(const Stmt * S)67   std::optional<uint64_t> getStmtCount(const Stmt *S) const {
68     if (!StmtCountMap)
69       return std::nullopt;
70     auto I = StmtCountMap->find(S);
71     if (I == StmtCountMap->end())
72       return std::nullopt;
73     return I->second;
74   }
75 
76   /// If the execution count for the current statement is known, record that
77   /// as the current count.
setCurrentStmt(const Stmt * S)78   void setCurrentStmt(const Stmt *S) {
79     if (auto Count = getStmtCount(S))
80       setCurrentRegionCount(*Count);
81   }
82 
83   /// Assign counters to regions and configure them for PGO of a given
84   /// function. Does nothing if instrumentation is not enabled and either
85   /// generates global variables or associates PGO data with each of the
86   /// counters depending on whether we are generating or using instrumentation.
87   void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
88   /// Emit a coverage mapping range with a counter zero
89   /// for an unused declaration.
90   void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
91                                llvm::GlobalValue::LinkageTypes Linkage);
92   // Insert instrumentation or attach profile metadata at value sites
93   void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
94                     llvm::Instruction *ValueSite, llvm::Value *ValuePtr);
95 
96   // Set a module flag indicating if value profiling is enabled.
97   void setValueProfilingFlag(llvm::Module &M);
98 
99 private:
100   void setFuncName(llvm::Function *Fn);
101   void setFuncName(StringRef Name, llvm::GlobalValue::LinkageTypes Linkage);
102   void mapRegionCounters(const Decl *D);
103   void computeRegionCounts(const Decl *D);
104   void applyFunctionAttributes(llvm::IndexedInstrProfReader *PGOReader,
105                                llvm::Function *Fn);
106   void loadRegionCounts(llvm::IndexedInstrProfReader *PGOReader,
107                         bool IsInMainFile);
108   bool skipRegionMappingForDecl(const Decl *D);
109   void emitCounterRegionMapping(const Decl *D);
110   bool canEmitMCDCCoverage(const CGBuilderTy &Builder);
111 
112 public:
113   void emitCounterIncrement(CGBuilderTy &Builder, const Stmt *S,
114                             llvm::Value *StepV);
115   void emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
116                                       Address MCDCCondBitmapAddr);
117   void emitMCDCParameters(CGBuilderTy &Builder);
118   void emitMCDCCondBitmapReset(CGBuilderTy &Builder, const Expr *S,
119                                Address MCDCCondBitmapAddr);
120   void emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
121                                 Address MCDCCondBitmapAddr, llvm::Value *Val);
122 
123   /// Return the region count for the counter at the given index.
getRegionCount(const Stmt * S)124   uint64_t getRegionCount(const Stmt *S) {
125     if (!RegionCounterMap)
126       return 0;
127     if (!haveRegionCounts())
128       return 0;
129     // With profiles from a differing version of clang we can have mismatched
130     // decl counts. Don't crash in such a case.
131     auto Index = (*RegionCounterMap)[S];
132     if (Index >= RegionCounts.size())
133       return 0;
134     return RegionCounts[Index];
135   }
136 };
137 
138 }  // end namespace CodeGen
139 }  // end namespace clang
140 
141 #endif
142