LLVM  14.0.0git
SampleContextTracker.h
Go to the documentation of this file.
1 //===- Transforms/IPO/SampleContextTracker.h --------------------*- 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 /// \file
10 /// This file provides the interface for context-sensitive profile tracker used
11 /// by CSSPGO.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TRANSFORMS_IPO_SAMPLECONTEXTTRACKER_H
16 #define LLVM_TRANSFORMS_IPO_SAMPLECONTEXTTRACKER_H
17 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/Instructions.h"
24 #include <list>
25 #include <map>
26 #include <vector>
27 
28 using namespace llvm;
29 using namespace sampleprof;
30 
31 namespace llvm {
32 
33 // Internal trie tree representation used for tracking context tree and sample
34 // profiles. The path from root node to a given node represents the context of
35 // that nodes' profile.
37 public:
38  ContextTrieNode(ContextTrieNode *Parent = nullptr,
39  StringRef FName = StringRef(),
40  FunctionSamples *FSamples = nullptr,
41  LineLocation CallLoc = {0, 0})
42  : ParentContext(Parent), FuncName(FName), FuncSamples(FSamples),
43  CallSiteLoc(CallLoc){};
44  ContextTrieNode *getChildContext(const LineLocation &CallSite,
45  StringRef ChildName);
46  ContextTrieNode *getHottestChildContext(const LineLocation &CallSite);
47  ContextTrieNode *getOrCreateChildContext(const LineLocation &CallSite,
48  StringRef ChildName,
49  bool AllowCreate = true);
50 
51  ContextTrieNode &moveToChildContext(const LineLocation &CallSite,
52  ContextTrieNode &&NodeToMove,
53  uint32_t ContextFramesToRemove,
54  bool DeleteNode = true);
55  void removeChildContext(const LineLocation &CallSite, StringRef ChildName);
56  std::map<uint64_t, ContextTrieNode> &getAllChildContext();
57  StringRef getFuncName() const;
58  FunctionSamples *getFunctionSamples() const;
59  void setFunctionSamples(FunctionSamples *FSamples);
60  Optional<uint32_t> getFunctionSize() const;
61  void addFunctionSize(uint32_t FSize);
62  LineLocation getCallSiteLoc() const;
63  ContextTrieNode *getParentContext() const;
64  void setParentContext(ContextTrieNode *Parent);
65  void dumpNode();
66  void dumpTree();
67 
68 private:
69  // Map line+discriminator location to child context
70  std::map<uint64_t, ContextTrieNode> AllChildContext;
71 
72  // Link to parent context node
73  ContextTrieNode *ParentContext;
74 
75  // Function name for current context
76  StringRef FuncName;
77 
78  // Function Samples for current context
79  FunctionSamples *FuncSamples;
80 
81  // Function size for current context
82  Optional<uint32_t> FuncSize;
83 
84  // Callsite location in parent context
85  LineLocation CallSiteLoc;
86 };
87 
88 // Profile tracker that manages profiles and its associated context. It
89 // provides interfaces used by sample profile loader to query context profile or
90 // base profile for given function or location; it also manages context tree
91 // manipulation that is needed to accommodate inline decisions so we have
92 // accurate post-inline profile for functions. Internally context profiles
93 // are organized in a trie, with each node representing profile for specific
94 // calling context and the context is identified by path from root to the node.
96 public:
97  struct ProfileComparer {
99  // Sort function profiles by the number of total samples and their
100  // contexts.
101  if (A->getTotalSamples() == B->getTotalSamples())
102  return A->getContext() < B->getContext();
103  return A->getTotalSamples() > B->getTotalSamples();
104  }
105  };
106 
107  // Keep profiles of a function sorted so that they will be processed/promoted
108  // deterministically.
109  using ContextSamplesTy = std::set<FunctionSamples *, ProfileComparer>;
110 
112  const DenseMap<uint64_t, StringRef> *GUIDToFuncNameMap);
113  // Query context profile for a specific callee with given name at a given
114  // call-site. The full context is identified by location of call instruction.
115  FunctionSamples *getCalleeContextSamplesFor(const CallBase &Inst,
116  StringRef CalleeName);
117  // Get samples for indirect call targets for call site at given location.
118  std::vector<const FunctionSamples *>
119  getIndirectCalleeContextSamplesFor(const DILocation *DIL);
120  // Query context profile for a given location. The full context
121  // is identified by input DILocation.
122  FunctionSamples *getContextSamplesFor(const DILocation *DIL);
123  // Query context profile for a given sample contxt of a function.
124  FunctionSamples *getContextSamplesFor(const SampleContext &Context);
125  // Get all context profile for given function.
126  ContextSamplesTy &getAllContextSamplesFor(const Function &Func);
127  ContextSamplesTy &getAllContextSamplesFor(StringRef Name);
128  // Query base profile for a given function. A base profile is a merged view
129  // of all context profiles for contexts that are not inlined.
130  FunctionSamples *getBaseSamplesFor(const Function &Func,
131  bool MergeContext = true);
132  // Query base profile for a given function by name.
133  FunctionSamples *getBaseSamplesFor(StringRef Name, bool MergeContext = true);
134  // Retrieve the context trie node for given profile context
135  ContextTrieNode *getContextFor(const SampleContext &Context);
136  // Get real function name for a given trie node.
137  StringRef getFuncNameFor(ContextTrieNode *Node) const;
138  // Mark a context profile as inlined when function is inlined.
139  // This makes sure that inlined context profile will be excluded in
140  // function's base profile.
141  void markContextSamplesInlined(const FunctionSamples *InlinedSamples);
142  ContextTrieNode &getRootContext();
143  void promoteMergeContextSamplesTree(const Instruction &Inst,
144  StringRef CalleeName);
145  // Dump the internal context profile trie.
146  void dump();
147 
148 private:
149  ContextTrieNode *getContextFor(const DILocation *DIL);
150  ContextTrieNode *getCalleeContextFor(const DILocation *DIL,
151  StringRef CalleeName);
152  ContextTrieNode *getOrCreateContextPath(const SampleContext &Context,
153  bool AllowCreate);
154  ContextTrieNode *getTopLevelContextNode(StringRef FName);
155  ContextTrieNode &addTopLevelContextNode(StringRef FName);
156  ContextTrieNode &promoteMergeContextSamplesTree(ContextTrieNode &NodeToPromo);
157  void mergeContextNode(ContextTrieNode &FromNode, ContextTrieNode &ToNode,
158  uint32_t ContextFramesToRemove);
160  promoteMergeContextSamplesTree(ContextTrieNode &FromNode,
161  ContextTrieNode &ToNodeParent,
162  uint32_t ContextFramesToRemove);
163 
164  // Map from function name to context profiles (excluding base profile)
165  StringMap<ContextSamplesTy> FuncToCtxtProfiles;
166 
167  // Map from function guid to real function names. Only used in md5 mode.
168  const DenseMap<uint64_t, StringRef> *GUIDToFuncNameMap;
169 
170  // Root node for context trie tree
171  ContextTrieNode RootContext;
172 };
173 
174 } // end namespace llvm
175 #endif // LLVM_TRANSFORMS_IPO_SAMPLECONTEXTTRACKER_H
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
DebugInfoMetadata.h
llvm::Function
Definition: Function.h:62
StringRef.h
llvm::sampleprof::SampleProfileMap
std::unordered_map< SampleContext, FunctionSamples, SampleContext::Hash > SampleProfileMap
Definition: SampleProf.h:1128
llvm::DILocation
Debug location.
Definition: DebugInfoMetadata.h:1580
llvm::Optional< uint32_t >
llvm::dump
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
Definition: SparseBitVector.h:876
Context
ManagedStatic< detail::RecordContext > Context
Definition: Record.cpp:96
SampleProf.h
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::Instruction
Definition: Instruction.h:45
StringMap.h
llvm::sampleprof::SampleContext
Definition: SampleProf.h:470
llvm::SampleContextTracker::ContextSamplesTy
std::set< FunctionSamples *, ProfileComparer > ContextSamplesTy
Definition: SampleContextTracker.h:109
llvm::StringMap< ContextSamplesTy >
llvm::ContextTrieNode::ContextTrieNode
ContextTrieNode(ContextTrieNode *Parent=nullptr, StringRef FName=StringRef(), FunctionSamples *FSamples=nullptr, LineLocation CallLoc={0, 0})
Definition: SampleContextTracker.h:38
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition: SampleProf.h:689
llvm::DenseMap
Definition: DenseMap.h:714
llvm::SampleContextTracker
Definition: SampleContextTracker.h:95
llvm::sampleprof::LineLocation
Represents the relative location of an instruction.
Definition: SampleProf.h:281
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
uint32_t
llvm::ContextTrieNode
Definition: SampleContextTracker.h:36
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
Instructions.h
SmallVector.h
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1176
llvm::SampleContextTracker::ProfileComparer::operator()
bool operator()(FunctionSamples *A, FunctionSamples *B) const
Definition: SampleContextTracker.h:98
llvm::SampleContextTracker::ProfileComparer
Definition: SampleContextTracker.h:97