1 //===- SampleContextTracker.cpp - Context-sensitive Profile Tracker -------===//
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 // This file implements the SampleContextTracker used by CSSPGO.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Transforms/IPO/SampleContextTracker.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/IR/DebugInfoMetadata.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/ProfileData/SampleProf.h"
19 #include <map>
20 #include <queue>
21 #include <vector>
22 
23 using namespace llvm;
24 using namespace sampleprof;
25 
26 #define DEBUG_TYPE "sample-context-tracker"
27 
28 namespace llvm {
29 
30 ContextTrieNode *ContextTrieNode::getChildContext(const LineLocation &CallSite,
31                                                   StringRef CalleeName) {
32   if (CalleeName.empty())
33     return getHottestChildContext(CallSite);
34 
35   uint32_t Hash = nodeHash(CalleeName, CallSite);
36   auto It = AllChildContext.find(Hash);
37   if (It != AllChildContext.end())
38     return &It->second;
39   return nullptr;
40 }
41 
42 ContextTrieNode *
43 ContextTrieNode::getHottestChildContext(const LineLocation &CallSite) {
44   // CSFDO-TODO: This could be slow, change AllChildContext so we can
45   // do point look up for child node by call site alone.
46   // Retrieve the child node with max count for indirect call
47   ContextTrieNode *ChildNodeRet = nullptr;
48   uint64_t MaxCalleeSamples = 0;
49   for (auto &It : AllChildContext) {
50     ContextTrieNode &ChildNode = It.second;
51     if (ChildNode.CallSiteLoc != CallSite)
52       continue;
53     FunctionSamples *Samples = ChildNode.getFunctionSamples();
54     if (!Samples)
55       continue;
56     if (Samples->getTotalSamples() > MaxCalleeSamples) {
57       ChildNodeRet = &ChildNode;
58       MaxCalleeSamples = Samples->getTotalSamples();
59     }
60   }
61 
62   return ChildNodeRet;
63 }
64 
65 ContextTrieNode &ContextTrieNode::moveToChildContext(
66     const LineLocation &CallSite, ContextTrieNode &&NodeToMove,
67     StringRef ContextStrToRemove, bool DeleteNode) {
68   uint32_t Hash = nodeHash(NodeToMove.getFuncName(), CallSite);
69   assert(!AllChildContext.count(Hash) && "Node to remove must exist");
70   LineLocation OldCallSite = NodeToMove.CallSiteLoc;
71   ContextTrieNode &OldParentContext = *NodeToMove.getParentContext();
72   AllChildContext[Hash] = NodeToMove;
73   ContextTrieNode &NewNode = AllChildContext[Hash];
74   NewNode.CallSiteLoc = CallSite;
75 
76   // Walk through nodes in the moved the subtree, and update
77   // FunctionSamples' context as for the context promotion.
78   // We also need to set new parant link for all children.
79   std::queue<ContextTrieNode *> NodeToUpdate;
80   NewNode.setParentContext(this);
81   NodeToUpdate.push(&NewNode);
82 
83   while (!NodeToUpdate.empty()) {
84     ContextTrieNode *Node = NodeToUpdate.front();
85     NodeToUpdate.pop();
86     FunctionSamples *FSamples = Node->getFunctionSamples();
87 
88     if (FSamples) {
89       FSamples->getContext().promoteOnPath(ContextStrToRemove);
90       FSamples->getContext().setState(SyntheticContext);
91       LLVM_DEBUG(dbgs() << "  Context promoted to: " << FSamples->getContext()
92                         << "\n");
93     }
94 
95     for (auto &It : Node->getAllChildContext()) {
96       ContextTrieNode *ChildNode = &It.second;
97       ChildNode->setParentContext(Node);
98       NodeToUpdate.push(ChildNode);
99     }
100   }
101 
102   // Original context no longer needed, destroy if requested.
103   if (DeleteNode)
104     OldParentContext.removeChildContext(OldCallSite, NewNode.getFuncName());
105 
106   return NewNode;
107 }
108 
109 void ContextTrieNode::removeChildContext(const LineLocation &CallSite,
110                                          StringRef CalleeName) {
111   uint32_t Hash = nodeHash(CalleeName, CallSite);
112   // Note this essentially calls dtor and destroys that child context
113   AllChildContext.erase(Hash);
114 }
115 
116 std::map<uint32_t, ContextTrieNode> &ContextTrieNode::getAllChildContext() {
117   return AllChildContext;
118 }
119 
120 StringRef ContextTrieNode::getFuncName() const { return FuncName; }
121 
122 FunctionSamples *ContextTrieNode::getFunctionSamples() const {
123   return FuncSamples;
124 }
125 
126 void ContextTrieNode::setFunctionSamples(FunctionSamples *FSamples) {
127   FuncSamples = FSamples;
128 }
129 
130 LineLocation ContextTrieNode::getCallSiteLoc() const { return CallSiteLoc; }
131 
132 ContextTrieNode *ContextTrieNode::getParentContext() const {
133   return ParentContext;
134 }
135 
136 void ContextTrieNode::setParentContext(ContextTrieNode *Parent) {
137   ParentContext = Parent;
138 }
139 
140 void ContextTrieNode::dump() {
141   dbgs() << "Node: " << FuncName << "\n"
142          << "  Callsite: " << CallSiteLoc << "\n"
143          << "  Children:\n";
144 
145   for (auto &It : AllChildContext) {
146     dbgs() << "    Node: " << It.second.getFuncName() << "\n";
147   }
148 }
149 
150 uint32_t ContextTrieNode::nodeHash(StringRef ChildName,
151                                    const LineLocation &Callsite) {
152   // We still use child's name for child hash, this is
153   // because for children of root node, we don't have
154   // different line/discriminator, and we'll rely on name
155   // to differentiate children.
156   uint32_t NameHash = std::hash<std::string>{}(ChildName.str());
157   uint32_t LocId = (Callsite.LineOffset << 16) | Callsite.Discriminator;
158   return NameHash + (LocId << 5) + LocId;
159 }
160 
161 ContextTrieNode *ContextTrieNode::getOrCreateChildContext(
162     const LineLocation &CallSite, StringRef CalleeName, bool AllowCreate) {
163   uint32_t Hash = nodeHash(CalleeName, CallSite);
164   auto It = AllChildContext.find(Hash);
165   if (It != AllChildContext.end()) {
166     assert(It->second.getFuncName() == CalleeName &&
167            "Hash collision for child context node");
168     return &It->second;
169   }
170 
171   if (!AllowCreate)
172     return nullptr;
173 
174   AllChildContext[Hash] = ContextTrieNode(this, CalleeName, nullptr, CallSite);
175   return &AllChildContext[Hash];
176 }
177 
178 // Profiler tracker than manages profiles and its associated context
179 SampleContextTracker::SampleContextTracker(
180     StringMap<FunctionSamples> &Profiles) {
181   for (auto &FuncSample : Profiles) {
182     FunctionSamples *FSamples = &FuncSample.second;
183     SampleContext Context(FuncSample.first(), RawContext);
184     LLVM_DEBUG(dbgs() << "Tracking Context for function: " << Context << "\n");
185     if (!Context.isBaseContext())
186       FuncToCtxtProfileSet[Context.getNameWithoutContext()].insert(FSamples);
187     ContextTrieNode *NewNode = getOrCreateContextPath(Context, true);
188     assert(!NewNode->getFunctionSamples() &&
189            "New node can't have sample profile");
190     NewNode->setFunctionSamples(FSamples);
191   }
192 }
193 
194 FunctionSamples *
195 SampleContextTracker::getCalleeContextSamplesFor(const CallBase &Inst,
196                                                  StringRef CalleeName) {
197   LLVM_DEBUG(dbgs() << "Getting callee context for instr: " << Inst << "\n");
198   DILocation *DIL = Inst.getDebugLoc();
199   if (!DIL)
200     return nullptr;
201 
202   // For indirect call, CalleeName will be empty, in which case the context
203   // profile for callee with largest total samples will be returned.
204   ContextTrieNode *CalleeContext = getCalleeContextFor(DIL, CalleeName);
205   if (CalleeContext) {
206     FunctionSamples *FSamples = CalleeContext->getFunctionSamples();
207     LLVM_DEBUG(if (FSamples) {
208       dbgs() << "  Callee context found: " << FSamples->getContext() << "\n";
209     });
210     return FSamples;
211   }
212 
213   return nullptr;
214 }
215 
216 std::vector<const FunctionSamples *>
217 SampleContextTracker::getIndirectCalleeContextSamplesFor(
218     const DILocation *DIL) {
219   std::vector<const FunctionSamples *> R;
220   if (!DIL)
221     return R;
222 
223   ContextTrieNode *CallerNode = getContextFor(DIL);
224   LineLocation CallSite = FunctionSamples::getCallSiteIdentifier(DIL);
225   for (auto &It : CallerNode->getAllChildContext()) {
226     ContextTrieNode &ChildNode = It.second;
227     if (ChildNode.getCallSiteLoc() != CallSite)
228       continue;
229     if (FunctionSamples *CalleeSamples = ChildNode.getFunctionSamples())
230       R.push_back(CalleeSamples);
231   }
232 
233   return R;
234 }
235 
236 FunctionSamples *
237 SampleContextTracker::getContextSamplesFor(const DILocation *DIL) {
238   assert(DIL && "Expect non-null location");
239 
240   ContextTrieNode *ContextNode = getContextFor(DIL);
241   if (!ContextNode)
242     return nullptr;
243 
244   // We may have inlined callees during pre-LTO compilation, in which case
245   // we need to rely on the inline stack from !dbg to mark context profile
246   // as inlined, instead of `MarkContextSamplesInlined` during inlining.
247   // Sample profile loader walks through all instructions to get profile,
248   // which calls this function. So once that is done, all previously inlined
249   // context profile should be marked properly.
250   FunctionSamples *Samples = ContextNode->getFunctionSamples();
251   if (Samples && ContextNode->getParentContext() != &RootContext)
252     Samples->getContext().setState(InlinedContext);
253 
254   return Samples;
255 }
256 
257 FunctionSamples *
258 SampleContextTracker::getContextSamplesFor(const SampleContext &Context) {
259   ContextTrieNode *Node = getContextFor(Context);
260   if (!Node)
261     return nullptr;
262 
263   return Node->getFunctionSamples();
264 }
265 
266 FunctionSamples *SampleContextTracker::getBaseSamplesFor(const Function &Func,
267                                                          bool MergeContext) {
268   StringRef CanonName = FunctionSamples::getCanonicalFnName(Func);
269   return getBaseSamplesFor(CanonName, MergeContext);
270 }
271 
272 FunctionSamples *SampleContextTracker::getBaseSamplesFor(StringRef Name,
273                                                          bool MergeContext) {
274   LLVM_DEBUG(dbgs() << "Getting base profile for function: " << Name << "\n");
275   // Base profile is top-level node (child of root node), so try to retrieve
276   // existing top-level node for given function first. If it exists, it could be
277   // that we've merged base profile before, or there's actually context-less
278   // profile from the input (e.g. due to unreliable stack walking).
279   ContextTrieNode *Node = getTopLevelContextNode(Name);
280   if (MergeContext) {
281     LLVM_DEBUG(dbgs() << "  Merging context profile into base profile: " << Name
282                       << "\n");
283 
284     // We have profile for function under different contexts,
285     // create synthetic base profile and merge context profiles
286     // into base profile.
287     for (auto *CSamples : FuncToCtxtProfileSet[Name]) {
288       SampleContext &Context = CSamples->getContext();
289       ContextTrieNode *FromNode = getContextFor(Context);
290       if (FromNode == Node)
291         continue;
292 
293       // Skip inlined context profile and also don't re-merge any context
294       if (Context.hasState(InlinedContext) || Context.hasState(MergedContext))
295         continue;
296 
297       ContextTrieNode &ToNode = promoteMergeContextSamplesTree(*FromNode);
298       assert((!Node || Node == &ToNode) && "Expect only one base profile");
299       Node = &ToNode;
300     }
301   }
302 
303   // Still no profile even after merge/promotion (if allowed)
304   if (!Node)
305     return nullptr;
306 
307   return Node->getFunctionSamples();
308 }
309 
310 void SampleContextTracker::markContextSamplesInlined(
311     const FunctionSamples *InlinedSamples) {
312   assert(InlinedSamples && "Expect non-null inlined samples");
313   LLVM_DEBUG(dbgs() << "Marking context profile as inlined: "
314                     << InlinedSamples->getContext() << "\n");
315   InlinedSamples->getContext().setState(InlinedContext);
316 }
317 
318 void SampleContextTracker::promoteMergeContextSamplesTree(
319     const Instruction &Inst, StringRef CalleeName) {
320   LLVM_DEBUG(dbgs() << "Promoting and merging context tree for instr: \n"
321                     << Inst << "\n");
322   // Get the caller context for the call instruction, we don't use callee
323   // name from call because there can be context from indirect calls too.
324   DILocation *DIL = Inst.getDebugLoc();
325   ContextTrieNode *CallerNode = getContextFor(DIL);
326   if (!CallerNode)
327     return;
328 
329   // Get the context that needs to be promoted
330   LineLocation CallSite = FunctionSamples::getCallSiteIdentifier(DIL);
331   // For indirect call, CalleeName will be empty, in which case we need to
332   // promote all non-inlined child context profiles.
333   if (CalleeName.empty()) {
334     for (auto &It : CallerNode->getAllChildContext()) {
335       ContextTrieNode *NodeToPromo = &It.second;
336       if (CallSite != NodeToPromo->getCallSiteLoc())
337         continue;
338       FunctionSamples *FromSamples = NodeToPromo->getFunctionSamples();
339       if (FromSamples && FromSamples->getContext().hasState(InlinedContext))
340         continue;
341       promoteMergeContextSamplesTree(*NodeToPromo);
342     }
343     return;
344   }
345 
346   // Get the context for the given callee that needs to be promoted
347   ContextTrieNode *NodeToPromo =
348       CallerNode->getChildContext(CallSite, CalleeName);
349   if (!NodeToPromo)
350     return;
351 
352   promoteMergeContextSamplesTree(*NodeToPromo);
353 }
354 
355 ContextTrieNode &SampleContextTracker::promoteMergeContextSamplesTree(
356     ContextTrieNode &NodeToPromo) {
357   // Promote the input node to be directly under root. This can happen
358   // when we decided to not inline a function under context represented
359   // by the input node. The promote and merge is then needed to reflect
360   // the context profile in the base (context-less) profile.
361   FunctionSamples *FromSamples = NodeToPromo.getFunctionSamples();
362   assert(FromSamples && "Shouldn't promote a context without profile");
363   LLVM_DEBUG(dbgs() << "  Found context tree root to promote: "
364                     << FromSamples->getContext() << "\n");
365 
366   assert(!FromSamples->getContext().hasState(InlinedContext) &&
367          "Shouldn't promote inlined context profile");
368   StringRef ContextStrToRemove = FromSamples->getContext().getCallingContext();
369   return promoteMergeContextSamplesTree(NodeToPromo, RootContext,
370                                         ContextStrToRemove);
371 }
372 
373 void SampleContextTracker::dump() {
374   dbgs() << "Context Profile Tree:\n";
375   std::queue<ContextTrieNode *> NodeQueue;
376   NodeQueue.push(&RootContext);
377 
378   while (!NodeQueue.empty()) {
379     ContextTrieNode *Node = NodeQueue.front();
380     NodeQueue.pop();
381     Node->dump();
382 
383     for (auto &It : Node->getAllChildContext()) {
384       ContextTrieNode *ChildNode = &It.second;
385       NodeQueue.push(ChildNode);
386     }
387   }
388 }
389 
390 ContextTrieNode *
391 SampleContextTracker::getContextFor(const SampleContext &Context) {
392   return getOrCreateContextPath(Context, false);
393 }
394 
395 ContextTrieNode *
396 SampleContextTracker::getCalleeContextFor(const DILocation *DIL,
397                                           StringRef CalleeName) {
398   assert(DIL && "Expect non-null location");
399 
400   ContextTrieNode *CallContext = getContextFor(DIL);
401   if (!CallContext)
402     return nullptr;
403 
404   // When CalleeName is empty, the child context profile with max
405   // total samples will be returned.
406   return CallContext->getChildContext(
407       FunctionSamples::getCallSiteIdentifier(DIL), CalleeName);
408 }
409 
410 ContextTrieNode *SampleContextTracker::getContextFor(const DILocation *DIL) {
411   assert(DIL && "Expect non-null location");
412   SmallVector<std::pair<LineLocation, StringRef>, 10> S;
413 
414   // Use C++ linkage name if possible.
415   const DILocation *PrevDIL = DIL;
416   for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
417     StringRef Name = PrevDIL->getScope()->getSubprogram()->getLinkageName();
418     if (Name.empty())
419       Name = PrevDIL->getScope()->getSubprogram()->getName();
420     S.push_back(
421         std::make_pair(FunctionSamples::getCallSiteIdentifier(DIL),
422                        PrevDIL->getScope()->getSubprogram()->getLinkageName()));
423     PrevDIL = DIL;
424   }
425 
426   // Push root node, note that root node like main may only
427   // a name, but not linkage name.
428   StringRef RootName = PrevDIL->getScope()->getSubprogram()->getLinkageName();
429   if (RootName.empty())
430     RootName = PrevDIL->getScope()->getSubprogram()->getName();
431   S.push_back(std::make_pair(LineLocation(0, 0), RootName));
432 
433   ContextTrieNode *ContextNode = &RootContext;
434   int I = S.size();
435   while (--I >= 0 && ContextNode) {
436     LineLocation &CallSite = S[I].first;
437     StringRef &CalleeName = S[I].second;
438     ContextNode = ContextNode->getChildContext(CallSite, CalleeName);
439   }
440 
441   if (I < 0)
442     return ContextNode;
443 
444   return nullptr;
445 }
446 
447 ContextTrieNode *
448 SampleContextTracker::getOrCreateContextPath(const SampleContext &Context,
449                                              bool AllowCreate) {
450   ContextTrieNode *ContextNode = &RootContext;
451   StringRef ContextRemain = Context;
452   StringRef ChildContext;
453   StringRef CalleeName;
454   LineLocation CallSiteLoc(0, 0);
455 
456   while (ContextNode && !ContextRemain.empty()) {
457     auto ContextSplit = SampleContext::splitContextString(ContextRemain);
458     ChildContext = ContextSplit.first;
459     ContextRemain = ContextSplit.second;
460     LineLocation NextCallSiteLoc(0, 0);
461     SampleContext::decodeContextString(ChildContext, CalleeName,
462                                        NextCallSiteLoc);
463 
464     // Create child node at parent line/disc location
465     if (AllowCreate) {
466       ContextNode =
467           ContextNode->getOrCreateChildContext(CallSiteLoc, CalleeName);
468     } else {
469       ContextNode = ContextNode->getChildContext(CallSiteLoc, CalleeName);
470     }
471     CallSiteLoc = NextCallSiteLoc;
472   }
473 
474   assert((!AllowCreate || ContextNode) &&
475          "Node must exist if creation is allowed");
476   return ContextNode;
477 }
478 
479 ContextTrieNode *SampleContextTracker::getTopLevelContextNode(StringRef FName) {
480   return RootContext.getChildContext(LineLocation(0, 0), FName);
481 }
482 
483 ContextTrieNode &SampleContextTracker::addTopLevelContextNode(StringRef FName) {
484   assert(!getTopLevelContextNode(FName) && "Node to add must not exist");
485   return *RootContext.getOrCreateChildContext(LineLocation(0, 0), FName);
486 }
487 
488 void SampleContextTracker::mergeContextNode(ContextTrieNode &FromNode,
489                                             ContextTrieNode &ToNode,
490                                             StringRef ContextStrToRemove) {
491   FunctionSamples *FromSamples = FromNode.getFunctionSamples();
492   FunctionSamples *ToSamples = ToNode.getFunctionSamples();
493   if (FromSamples && ToSamples) {
494     // Merge/duplicate FromSamples into ToSamples
495     ToSamples->merge(*FromSamples);
496     ToSamples->getContext().setState(SyntheticContext);
497     FromSamples->getContext().setState(MergedContext);
498   } else if (FromSamples) {
499     // Transfer FromSamples from FromNode to ToNode
500     ToNode.setFunctionSamples(FromSamples);
501     FromSamples->getContext().setState(SyntheticContext);
502     FromSamples->getContext().promoteOnPath(ContextStrToRemove);
503     FromNode.setFunctionSamples(nullptr);
504   }
505 }
506 
507 ContextTrieNode &SampleContextTracker::promoteMergeContextSamplesTree(
508     ContextTrieNode &FromNode, ContextTrieNode &ToNodeParent,
509     StringRef ContextStrToRemove) {
510   assert(!ContextStrToRemove.empty() && "Context to remove can't be empty");
511 
512   // Ignore call site location if destination is top level under root
513   LineLocation NewCallSiteLoc = LineLocation(0, 0);
514   LineLocation OldCallSiteLoc = FromNode.getCallSiteLoc();
515   ContextTrieNode &FromNodeParent = *FromNode.getParentContext();
516   ContextTrieNode *ToNode = nullptr;
517   bool MoveToRoot = (&ToNodeParent == &RootContext);
518   if (!MoveToRoot) {
519     NewCallSiteLoc = OldCallSiteLoc;
520   }
521 
522   // Locate destination node, create/move if not existing
523   ToNode = ToNodeParent.getChildContext(NewCallSiteLoc, FromNode.getFuncName());
524   if (!ToNode) {
525     // Do not delete node to move from its parent here because
526     // caller is iterating over children of that parent node.
527     ToNode = &ToNodeParent.moveToChildContext(
528         NewCallSiteLoc, std::move(FromNode), ContextStrToRemove, false);
529   } else {
530     // Destination node exists, merge samples for the context tree
531     mergeContextNode(FromNode, *ToNode, ContextStrToRemove);
532     LLVM_DEBUG(dbgs() << "  Context promoted and merged to: "
533                       << ToNode->getFunctionSamples()->getContext() << "\n");
534 
535     // Recursively promote and merge children
536     for (auto &It : FromNode.getAllChildContext()) {
537       ContextTrieNode &FromChildNode = It.second;
538       promoteMergeContextSamplesTree(FromChildNode, *ToNode,
539                                      ContextStrToRemove);
540     }
541 
542     // Remove children once they're all merged
543     FromNode.getAllChildContext().clear();
544   }
545 
546   // For root of subtree, remove itself from old parent too
547   if (MoveToRoot)
548     FromNodeParent.removeChildContext(OldCallSiteLoc, ToNode->getFuncName());
549 
550   return *ToNode;
551 }
552 
553 } // namespace llvm
554