clang 19.0.0git
CodeGenPGO.h
Go to the documentation of this file.
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 "MCDCState.h"
20#include "llvm/ProfileData/InstrProfReader.h"
21#include <array>
22#include <memory>
23#include <optional>
24
25namespace clang {
26namespace CodeGen {
27
28/// Per-function PGO state.
30private:
31 CodeGenModule &CGM;
32 std::string FuncName;
33 llvm::GlobalVariable *FuncNameVar;
34
35 std::array <unsigned, llvm::IPVK_Last + 1> NumValueSites;
36 unsigned NumRegionCounters;
37 uint64_t FunctionHash;
38 std::unique_ptr<llvm::DenseMap<const Stmt *, unsigned>> RegionCounterMap;
39 std::unique_ptr<llvm::DenseMap<const Stmt *, uint64_t>> StmtCountMap;
40 std::unique_ptr<llvm::InstrProfRecord> ProfRecord;
41 std::unique_ptr<MCDC::State> RegionMCDCState;
42 std::vector<uint64_t> RegionCounts;
43 uint64_t CurrentRegionCount;
44
45public:
47 : CGM(CGModule), FuncNameVar(nullptr), NumValueSites({{0}}),
48 NumRegionCounters(0), FunctionHash(0), CurrentRegionCount(0) {}
49
50 /// Whether or not we have PGO region data for the current function. This is
51 /// false both when we have no data at all and when our data has been
52 /// discarded.
53 bool haveRegionCounts() const { return !RegionCounts.empty(); }
54
55 /// Return the counter value of the current region.
56 uint64_t getCurrentRegionCount() const { return CurrentRegionCount; }
57
58 /// Set the counter value for the current region. This is used to keep track
59 /// of changes to the most recent counter from control flow and non-local
60 /// exits.
61 void setCurrentRegionCount(uint64_t Count) { CurrentRegionCount = Count; }
62
63 /// Check if an execution count is known for a given statement. If so, return
64 /// true and put the value in Count; else return false.
65 std::optional<uint64_t> getStmtCount(const Stmt *S) const {
66 if (!StmtCountMap)
67 return std::nullopt;
68 auto I = StmtCountMap->find(S);
69 if (I == StmtCountMap->end())
70 return std::nullopt;
71 return I->second;
72 }
73
74 /// If the execution count for the current statement is known, record that
75 /// as the current count.
76 void setCurrentStmt(const Stmt *S) {
77 if (auto Count = getStmtCount(S))
79 }
80
81 /// Assign counters to regions and configure them for PGO of a given
82 /// function. Does nothing if instrumentation is not enabled and either
83 /// generates global variables or associates PGO data with each of the
84 /// counters depending on whether we are generating or using instrumentation.
85 void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn);
86 /// Emit a coverage mapping range with a counter zero
87 /// for an unused declaration.
88 void emitEmptyCounterMapping(const Decl *D, StringRef FuncName,
89 llvm::GlobalValue::LinkageTypes Linkage);
90 // Insert instrumentation or attach profile metadata at value sites
91 void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind,
92 llvm::Instruction *ValueSite, llvm::Value *ValuePtr);
93
94 // Set a module flag indicating if value profiling is enabled.
95 void setValueProfilingFlag(llvm::Module &M);
96
97 void setProfileVersion(llvm::Module &M);
98
99private:
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
112public:
113 void emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S,
114 llvm::Value *StepV);
115 void emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
116 Address MCDCCondBitmapAddr,
117 CodeGenFunction &CGF);
118 void emitMCDCParameters(CGBuilderTy &Builder);
119 void emitMCDCCondBitmapReset(CGBuilderTy &Builder, const Expr *S,
120 Address MCDCCondBitmapAddr);
121 void emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S,
122 Address MCDCCondBitmapAddr, llvm::Value *Val,
123 CodeGenFunction &CGF);
124
125 /// Return the region count for the counter at the given index.
126 uint64_t getRegionCount(const Stmt *S) {
127 if (!RegionCounterMap)
128 return 0;
129 if (!haveRegionCounts())
130 return 0;
131 // With profiles from a differing version of clang we can have mismatched
132 // decl counts. Don't crash in such a case.
133 auto Index = (*RegionCounterMap)[S];
134 if (Index >= RegionCounts.size())
135 return 0;
136 return RegionCounts[Index];
137 }
138};
139
140} // end namespace CodeGen
141} // end namespace clang
142
143#endif
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition: Address.h:111
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
This class organizes the cross-function state that is used while generating LLVM code.
Per-function PGO state.
Definition: CodeGenPGO.h:29
void assignRegionCounters(GlobalDecl GD, llvm::Function *Fn)
Assign counters to regions and configure them for PGO of a given function.
std::optional< uint64_t > getStmtCount(const Stmt *S) const
Check if an execution count is known for a given statement.
Definition: CodeGenPGO.h:65
void emitMCDCTestVectorBitmapUpdate(CGBuilderTy &Builder, const Expr *S, Address MCDCCondBitmapAddr, CodeGenFunction &CGF)
void setCurrentRegionCount(uint64_t Count)
Set the counter value for the current region.
Definition: CodeGenPGO.h:61
uint64_t getRegionCount(const Stmt *S)
Return the region count for the counter at the given index.
Definition: CodeGenPGO.h:126
void setValueProfilingFlag(llvm::Module &M)
void valueProfile(CGBuilderTy &Builder, uint32_t ValueKind, llvm::Instruction *ValueSite, llvm::Value *ValuePtr)
void emitMCDCCondBitmapUpdate(CGBuilderTy &Builder, const Expr *S, Address MCDCCondBitmapAddr, llvm::Value *Val, CodeGenFunction &CGF)
void emitMCDCCondBitmapReset(CGBuilderTy &Builder, const Expr *S, Address MCDCCondBitmapAddr)
uint64_t getCurrentRegionCount() const
Return the counter value of the current region.
Definition: CodeGenPGO.h:56
void setProfileVersion(llvm::Module &M)
void setCurrentStmt(const Stmt *S)
If the execution count for the current statement is known, record that as the current count.
Definition: CodeGenPGO.h:76
void emitEmptyCounterMapping(const Decl *D, StringRef FuncName, llvm::GlobalValue::LinkageTypes Linkage)
Emit a coverage mapping range with a counter zero for an unused declaration.
void emitMCDCParameters(CGBuilderTy &Builder)
CodeGenPGO(CodeGenModule &CGModule)
Definition: CodeGenPGO.h:46
bool haveRegionCounts() const
Whether or not we have PGO region data for the current function.
Definition: CodeGenPGO.h:53
void emitCounterSetOrIncrement(CGBuilderTy &Builder, const Stmt *S, llvm::Value *StepV)
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
This represents one expression.
Definition: Expr.h:110
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
Stmt - This represents one statement.
Definition: Stmt.h:84
The JSON file list parser is used to communicate input to InstallAPI.
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:24