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/DenseMap.h"
13#include "llvm/IR/GlobalValue.h"
14#include "llvm/IR/InstrTypes.h"
16#include "llvm/IR/PassManager.h"
18
19namespace llvm {
20
21class CtxProfAnalysis;
22
23// Setting initial capacity to 1 because all contexts must have at least 1
24// counter, and then, because all contexts belonging to a function have the same
25// size, there'll be at most one other heap allocation.
28
29/// The instrumented contextual profile, produced by the CtxProfAnalysis.
31 friend class CtxProfAnalysis;
33 struct FunctionInfo {
34 uint32_t NextCounterIndex = 0;
35 uint32_t NextCallsiteIndex = 0;
36 const std::string Name;
37
38 FunctionInfo(StringRef Name) : Name(Name) {}
39 };
40 std::optional<PGOCtxProfContext::CallTargetMapTy> Profiles;
41 // For the GUIDs in this module, associate metadata about each function which
42 // we'll need when we maintain the profiles during IPO transformations.
43 DenseMap<GlobalValue::GUID, FunctionInfo> FuncInfo;
44
45 /// Get the GUID of this Function if it's defined in this module.
46 GlobalValue::GUID getDefinedFunctionGUID(const Function &F) const;
47
48 // This is meant to be constructed from CtxProfAnalysis, which will also set
49 // its state piecemeal.
50 PGOContextualProfile() = default;
51
52public:
55
56 operator bool() const { return Profiles.has_value(); }
57
59 return *Profiles;
60 }
61
62 bool isFunctionKnown(const Function &F) const {
63 return getDefinedFunctionGUID(F) != 0;
64 }
65
68 return FuncInfo.find(getDefinedFunctionGUID(F))->second.NextCounterIndex++;
69 }
70
73 return FuncInfo.find(getDefinedFunctionGUID(F))->second.NextCallsiteIndex++;
74 }
75
76 const CtxProfFlatProfile flatten() const;
77
80 // Check whether the analysis has been explicitly invalidated. Otherwise,
81 // it's stateless and remains preserved.
82 auto PAC = PA.getChecker<CtxProfAnalysis>();
83 return !PAC.preservedWhenStateless();
84 }
85};
86
87class CtxProfAnalysis : public AnalysisInfoMixin<CtxProfAnalysis> {
88 StringRef Profile;
89
90public:
92 explicit CtxProfAnalysis(StringRef Profile = "");
93
95
97
98 /// Get the instruction instrumenting a callsite, or nullptr if that cannot be
99 /// found.
101
102 /// Get the instruction instrumenting a BB, or nullptr if not present.
104};
105
107 : public PassInfoMixin<CtxProfAnalysisPrinterPass> {
108 raw_ostream &OS;
109
110public:
112
114 static bool isRequired() { return true; }
115};
116
117/// Assign a GUID to functions as metadata. GUID calculation takes linkage into
118/// account, which may change especially through and after thinlto. By
119/// pre-computing and assigning as metadata, this mechanism is resilient to such
120/// changes (as well as name changes e.g. suffix ".llvm." additions).
121
122// FIXME(mtrofin): we can generalize this mechanism to calculate a GUID early in
123// the pass pipeline, associate it with any Global Value, and then use it for
124// PGO and ThinLTO.
125// At that point, this should be moved elsewhere.
126class AssignGUIDPass : public PassInfoMixin<AssignGUIDPass> {
127public:
128 explicit AssignGUIDPass() = default;
129
130 /// Assign a GUID *if* one is not already assign, as a function metadata named
131 /// `GUIDMetadataName`.
133 static const char *GUIDMetadataName;
134 // This should become GlobalValue::getGUID
135 static uint64_t getGUID(const Function &F);
136};
137
138} // namespace llvm
139#endif // LLVM_ANALYSIS_CTXPROFANALYSIS_H
This file defines the DenseMap class.
#define F(x, y, z)
Definition: MD5.cpp:55
Reader for contextual iFDO profile, which comes in bitstream format.
ModuleAnalysisManager MAM
This header defines various interfaces for pass management in LLVM.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
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:1236
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
CtxProfAnalysisPrinterPass(raw_ostream &OS)
PGOContextualProfile run(Module &M, ModuleAnalysisManager &MAM)
static InstrProfIncrementInst * getBBInstrumentation(BasicBlock &BB)
Get the instruction instrumenting a BB, or nullptr if not present.
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 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.
PGOContextualProfile(PGOContextualProfile &&)=default
const CtxProfFlatProfile flatten() const
const PGOCtxProfContext::CallTargetMapTy & profiles() const
bool invalidate(Module &, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &)
uint32_t allocateNextCounterIndex(const Function &F)
PGOContextualProfile(const PGOContextualProfile &)=delete
uint32_t allocateNextCallsiteIndex(const Function &F)
bool isFunctionKnown(const Function &F) const
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
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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
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