LLVM 20.0.0git
CtxProfAnalysis.h
Go to the documentation of this file.
1//===- CtxProfAnalysis.h - maintain contextual profile info -*- 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#ifndef LLVM_ANALYSIS_CTXPROFANALYSIS_H
10#define LLVM_ANALYSIS_CTXPROFANALYSIS_H
11
12#include "llvm/ADT/SetVector.h"
13#include "llvm/IR/GlobalValue.h"
14#include "llvm/IR/InstrTypes.h"
16#include "llvm/IR/PassManager.h"
18#include <optional>
19
20namespace llvm {
21
22class CtxProfAnalysis;
23
24// Setting initial capacity to 1 because all contexts must have at least 1
25// counter, and then, because all contexts belonging to a function have the same
26// size, there'll be at most one other heap allocation.
28 std::map<GlobalValue::GUID, SmallVector<uint64_t, 1>>;
29
30/// The instrumented contextual profile, produced by the CtxProfAnalysis.
32 friend class CtxProfAnalysis;
34 struct FunctionInfo {
35 uint32_t NextCounterIndex = 0;
36 uint32_t NextCallsiteIndex = 0;
37 const std::string Name;
39 FunctionInfo(StringRef Name) : Name(Name) {}
40 };
41 std::optional<PGOCtxProfContext::CallTargetMapTy> Profiles;
42 // For the GUIDs in this module, associate metadata about each function which
43 // we'll need when we maintain the profiles during IPO transformations.
44 std::map<GlobalValue::GUID, FunctionInfo> FuncInfo;
45
46 /// Get the GUID of this Function if it's defined in this module.
47 GlobalValue::GUID getDefinedFunctionGUID(const Function &F) const;
48
49 // This is meant to be constructed from CtxProfAnalysis, which will also set
50 // its state piecemeal.
51 PGOContextualProfile() = default;
52
53 void initIndex();
54
55public:
58
59 operator bool() const { return Profiles.has_value(); }
60
62 return *Profiles;
63 }
64
65 bool isFunctionKnown(const Function &F) const {
66 return getDefinedFunctionGUID(F) != 0;
67 }
68
70 auto It = FuncInfo.find(GUID);
71 if (It == FuncInfo.end())
72 return "";
73 return It->second.Name;
74 }
75
78 return FuncInfo.find(getDefinedFunctionGUID(F))->second.NextCounterIndex;
79 }
80
83 return FuncInfo.find(getDefinedFunctionGUID(F))->second.NextCallsiteIndex;
84 }
85
88 return FuncInfo.find(getDefinedFunctionGUID(F))->second.NextCounterIndex++;
89 }
90
93 return FuncInfo.find(getDefinedFunctionGUID(F))->second.NextCallsiteIndex++;
94 }
95
98
99 void update(Visitor, const Function &F);
100 void visit(ConstVisitor, const Function *F = nullptr) const;
101
102 const CtxProfFlatProfile flatten() const;
103
106 // Check whether the analysis has been explicitly invalidated. Otherwise,
107 // it's stateless and remains preserved.
108 auto PAC = PA.getChecker<CtxProfAnalysis>();
109 return !PAC.preservedWhenStateless();
110 }
111};
112
113class CtxProfAnalysis : public AnalysisInfoMixin<CtxProfAnalysis> {
114 const std::optional<StringRef> Profile;
115
116public:
118 explicit CtxProfAnalysis(std::optional<StringRef> Profile = std::nullopt);
119
121
123
124 /// Get the instruction instrumenting a callsite, or nullptr if that cannot be
125 /// found.
127
128 /// Get the instruction instrumenting a BB, or nullptr if not present.
130
131 /// Get the step instrumentation associated with a `select`
133
134 // FIXME: refactor to an advisor model, and separate
136 CallBase &IC, Result &Profile,
137 SetVector<std::pair<CallBase *, Function *>> &Candidates);
138};
139
141 : public PassInfoMixin<CtxProfAnalysisPrinterPass> {
142public:
143 enum class PrintMode { Everything, JSON };
145
147 static bool isRequired() { return true; }
148
149private:
150 raw_ostream &OS;
151 const PrintMode Mode;
152};
153
154/// Assign a GUID to functions as metadata. GUID calculation takes linkage into
155/// account, which may change especially through and after thinlto. By
156/// pre-computing and assigning as metadata, this mechanism is resilient to such
157/// changes (as well as name changes e.g. suffix ".llvm." additions).
158
159// FIXME(mtrofin): we can generalize this mechanism to calculate a GUID early in
160// the pass pipeline, associate it with any Global Value, and then use it for
161// PGO and ThinLTO.
162// At that point, this should be moved elsewhere.
163class AssignGUIDPass : public PassInfoMixin<AssignGUIDPass> {
164public:
165 explicit AssignGUIDPass() = default;
166
167 /// Assign a GUID *if* one is not already assign, as a function metadata named
168 /// `GUIDMetadataName`.
170 static const char *GUIDMetadataName;
171 // This should become GlobalValue::getGUID
172 static uint64_t getGUID(const Function &F);
173};
174
175} // namespace llvm
176#endif // LLVM_ANALYSIS_CTXPROFANALYSIS_H
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition: MD5.cpp:55
Reader for contextual iFDO profile, which comes in bitstream format.
ModuleAnalysisManager MAM
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements a set that has insertion order iteration characteristics.
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Assign a GUID to functions as metadata.
static uint64_t getGUID(const Function &F)
static const char * GUIDMetadataName
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
Assign a GUID if one is not already assign, as a function metadata named GUIDMetadataName.
AssignGUIDPass()=default
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1120
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
PGOContextualProfile run(Module &M, ModuleAnalysisManager &MAM)
static InstrProfIncrementInst * getBBInstrumentation(BasicBlock &BB)
Get the instruction instrumenting a BB, or nullptr if not present.
static InstrProfIncrementInstStep * getSelectInstrumentation(SelectInst &SI)
Get the step instrumentation associated with a select
static void collectIndirectCallPromotionList(CallBase &IC, Result &Profile, SetVector< std::pair< CallBase *, Function * > > &Candidates)
static InstrProfCallsite * getCallsiteInstrumentation(CallBase &CB)
Get the instruction instrumenting a callsite, or nullptr if that cannot be found.
static AnalysisKey Key
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:587
This represents the llvm.instrprof.callsite intrinsic.
This represents the llvm.instrprof.increment.step intrinsic.
This represents the llvm.instrprof.increment intrinsic.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
The instrumented contextual profile, produced by the CtxProfAnalysis.
void visit(ConstVisitor, const Function *F=nullptr) const
PGOContextualProfile(PGOContextualProfile &&)=default
const CtxProfFlatProfile flatten() const
const PGOCtxProfContext::CallTargetMapTy & profiles() const
void update(Visitor, const Function &F)
StringRef getFunctionName(GlobalValue::GUID GUID) const
bool invalidate(Module &, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &)
uint32_t getNumCounters(const Function &F) const
uint32_t allocateNextCounterIndex(const Function &F)
PGOContextualProfile(const PGOContextualProfile &)=delete
uint32_t getNumCallsites(const Function &F) const
uint32_t allocateNextCallsiteIndex(const Function &F)
bool isFunctionKnown(const Function &F) const
A node (context) in the loaded contextual profile, suitable for mutation during IPO passes.
std::map< GlobalValue::GUID, PGOCtxProfContext > CallTargetMapTy
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: Analysis.h:264
This class represents the LLVM 'select' instruction.
A vector that has set insertion semantics.
Definition: SetVector.h:57
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:297
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::map< GlobalValue::GUID, SmallVector< uint64_t, 1 > > CtxProfFlatProfile
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69