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/IR/InstrTypes.h"
14#include "llvm/IR/PassManager.h"
16
17namespace llvm {
18
19class CtxProfAnalysis;
20
21/// The instrumented contextual profile, produced by the CtxProfAnalysis.
23 friend class CtxProfAnalysis;
25 struct FunctionInfo {
26 uint32_t NextCounterIndex = 0;
27 uint32_t NextCallsiteIndex = 0;
28 const std::string Name;
29
30 FunctionInfo(StringRef Name) : Name(Name) {}
31 };
32 std::optional<PGOCtxProfContext::CallTargetMapTy> Profiles;
33 // For the GUIDs in this module, associate metadata about each function which
34 // we'll need when we maintain the profiles during IPO transformations.
35 DenseMap<GlobalValue::GUID, FunctionInfo> FuncInfo;
36
37 /// Get the GUID of this Function if it's defined in this module.
38 GlobalValue::GUID getDefinedFunctionGUID(const Function &F) const;
39
40 // This is meant to be constructed from CtxProfAnalysis, which will also set
41 // its state piecemeal.
42 PGOContextualProfile() = default;
43
44public:
47
48 operator bool() const { return Profiles.has_value(); }
49
51 return *Profiles;
52 }
53
54 bool isFunctionKnown(const Function &F) const {
55 return getDefinedFunctionGUID(F) != 0;
56 }
57
60 return FuncInfo.find(getDefinedFunctionGUID(F))->second.NextCounterIndex++;
61 }
62
65 return FuncInfo.find(getDefinedFunctionGUID(F))->second.NextCallsiteIndex++;
66 }
67
70 // Check whether the analysis has been explicitly invalidated. Otherwise,
71 // it's stateless and remains preserved.
72 auto PAC = PA.getChecker<CtxProfAnalysis>();
73 return !PAC.preservedWhenStateless();
74 }
75};
76
77class CtxProfAnalysis : public AnalysisInfoMixin<CtxProfAnalysis> {
78 StringRef Profile;
79
80public:
82 explicit CtxProfAnalysis(StringRef Profile = "");
83
85
87
89};
90
92 : public PassInfoMixin<CtxProfAnalysisPrinterPass> {
93 raw_ostream &OS;
94
95public:
97
99 static bool isRequired() { return true; }
100};
101
102/// Assign a GUID to functions as metadata. GUID calculation takes linkage into
103/// account, which may change especially through and after thinlto. By
104/// pre-computing and assigning as metadata, this mechanism is resilient to such
105/// changes (as well as name changes e.g. suffix ".llvm." additions).
106
107// FIXME(mtrofin): we can generalize this mechanism to calculate a GUID early in
108// the pass pipeline, associate it with any Global Value, and then use it for
109// PGO and ThinLTO.
110// At that point, this should be moved elsewhere.
111class AssignGUIDPass : public PassInfoMixin<AssignGUIDPass> {
112public:
113 explicit AssignGUIDPass() = default;
114
115 /// Assign a GUID *if* one is not already assign, as a function metadata named
116 /// `GUIDMetadataName`.
118 static const char *GUIDMetadataName;
119 // This should become GlobalValue::getGUID
120 static uint64_t getGUID(const Function &F);
121};
122
123} // namespace llvm
124#endif // LLVM_ANALYSIS_CTXPROFANALYSIS_H
#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
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 InstrProfCallsite * getCallsiteInstrumentation(CallBase &CB)
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.
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 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:50
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