LLVM 24.0.0git
MemProfContextDisambiguation.cpp
Go to the documentation of this file.
1//==-- MemProfContextDisambiguation.cpp - Disambiguate contexts -------------=//
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 support for context disambiguation of allocation
10// calls for profile guided heap optimization. Specifically, it uses Memprof
11// profiles which indicate context specific allocation behavior (currently
12// distinguishing cold vs hot memory allocations). Cloning is performed to
13// expose the cold allocation call contexts, and the allocation calls are
14// subsequently annotated with an attribute for later transformation.
15//
16// The transformations can be performed either directly on IR (regular LTO), or
17// on a ThinLTO index (and later applied to the IR during the ThinLTO backend).
18// Both types of LTO operate on a the same base graph representation, which
19// uses CRTP to support either IR or Index formats.
20//
21//===----------------------------------------------------------------------===//
22
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/DenseSet.h"
26#include "llvm/ADT/MapVector.h"
29#include "llvm/ADT/SmallSet.h"
31#include "llvm/ADT/Statistic.h"
38#include "llvm/IR/Module.h"
40#include "llvm/Pass.h"
43#include "llvm/Support/SHA1.h"
45#include "llvm/Transforms/IPO.h"
49#include <deque>
50#include <sstream>
51#include <vector>
52using namespace llvm;
53using namespace llvm::memprof;
54
55#define DEBUG_TYPE "memprof-context-disambiguation"
56
57STATISTIC(FunctionClonesAnalysis,
58 "Number of function clones created during whole program analysis");
59STATISTIC(FunctionClonesThinBackend,
60 "Number of function clones created during ThinLTO backend");
61STATISTIC(FunctionsClonedThinBackend,
62 "Number of functions that had clones created during ThinLTO backend");
64 FunctionCloneDuplicatesThinBackend,
65 "Number of function clone duplicates detected during ThinLTO backend");
66STATISTIC(AllocTypeNotCold, "Number of not cold static allocations (possibly "
67 "cloned) during whole program analysis");
68STATISTIC(AllocTypeCold, "Number of cold static allocations (possibly cloned) "
69 "during whole program analysis");
70STATISTIC(AllocTypeNotColdThinBackend,
71 "Number of not cold static allocations (possibly cloned) during "
72 "ThinLTO backend");
73STATISTIC(AllocTypeColdThinBackend, "Number of cold static allocations "
74 "(possibly cloned) during ThinLTO backend");
75STATISTIC(OrigAllocsThinBackend,
76 "Number of original (not cloned) allocations with memprof profiles "
77 "during ThinLTO backend");
79 AllocVersionsThinBackend,
80 "Number of allocation versions (including clones) during ThinLTO backend");
81STATISTIC(MaxAllocVersionsThinBackend,
82 "Maximum number of allocation versions created for an original "
83 "allocation during ThinLTO backend");
84STATISTIC(UnclonableAllocsThinBackend,
85 "Number of unclonable ambigous allocations during ThinLTO backend");
86STATISTIC(RemovedEdgesWithMismatchedCallees,
87 "Number of edges removed due to mismatched callees (profiled vs IR)");
88STATISTIC(FoundProfiledCalleeCount,
89 "Number of profiled callees found via tail calls");
90STATISTIC(FoundProfiledCalleeDepth,
91 "Aggregate depth of profiled callees found via tail calls");
92STATISTIC(FoundProfiledCalleeMaxDepth,
93 "Maximum depth of profiled callees found via tail calls");
94STATISTIC(FoundProfiledCalleeNonUniquelyCount,
95 "Number of profiled callees found via multiple tail call chains");
96STATISTIC(DeferredBackedges, "Number of backedges with deferred cloning");
97STATISTIC(NewMergedNodes, "Number of new nodes created during merging");
98STATISTIC(NonNewMergedNodes, "Number of non new nodes used during merging");
99STATISTIC(MissingAllocForContextId,
100 "Number of missing alloc nodes for context ids");
101STATISTIC(SkippedCallsCloning,
102 "Number of calls skipped during cloning due to unexpected operand");
103STATISTIC(MismatchedCloneAssignments,
104 "Number of callsites assigned to call multiple non-matching clones");
105STATISTIC(TotalMergeInvokes, "Number of merge invocations for nodes");
106STATISTIC(TotalMergeIters, "Number of merge iterations for nodes");
107STATISTIC(MaxMergeIters, "Max merge iterations for nodes");
108STATISTIC(NumImportantContextIds, "Number of important context ids");
109STATISTIC(NumFixupEdgeIdsInserted, "Number of fixup edge ids inserted");
110STATISTIC(NumFixupEdgesAdded, "Number of fixup edges added");
111STATISTIC(NumFixedContexts, "Number of contexts with fixed edges");
112STATISTIC(AliaseesPrevailingInDiffModuleFromAlias,
113 "Number of aliasees prevailing in a different module than its alias");
114
116 "memprof-dot-file-path-prefix", cl::init(""), cl::Hidden,
117 cl::value_desc("filename"),
118 cl::desc("Specify the path prefix of the MemProf dot files."));
119
120static cl::opt<bool> ExportToDot("memprof-export-to-dot", cl::init(false),
122 cl::desc("Export graph to dot files."));
123
124// TODO: Remove this option once new handling is validated more widely.
126 "memprof-merge-iteration", cl::init(true), cl::Hidden,
127 cl::desc("Iteratively apply merging on a node to catch new callers"));
128
129// How much of the graph to export to dot.
131 All, // The full CCG graph.
132 Alloc, // Only contexts for the specified allocation.
133 Context, // Only the specified context.
134};
135
137 "memprof-dot-scope", cl::desc("Scope of graph to export to dot"),
140 clEnumValN(DotScope::All, "all", "Export full callsite graph"),
142 "Export only nodes with contexts feeding given "
143 "-memprof-dot-alloc-id"),
144 clEnumValN(DotScope::Context, "context",
145 "Export only nodes with given -memprof-dot-context-id")));
146
148 AllocIdForDot("memprof-dot-alloc-id", cl::init(0), cl::Hidden,
149 cl::desc("Id of alloc to export if -memprof-dot-scope=alloc "
150 "or to highlight if -memprof-dot-scope=all"));
151
153 "memprof-dot-context-id", cl::init(0), cl::Hidden,
154 cl::desc("Id of context to export if -memprof-dot-scope=context or to "
155 "highlight otherwise"));
156
157static cl::opt<bool>
158 DumpCCG("memprof-dump-ccg", cl::init(false), cl::Hidden,
159 cl::desc("Dump CallingContextGraph to stdout after each stage."));
160
161static cl::opt<bool>
162 VerifyCCG("memprof-verify-ccg", cl::init(false), cl::Hidden,
163 cl::desc("Perform verification checks on CallingContextGraph."));
164
165static cl::opt<bool>
166 VerifyNodes("memprof-verify-nodes", cl::init(false), cl::Hidden,
167 cl::desc("Perform frequent verification checks on nodes."));
168
170 "memprof-import-summary",
171 cl::desc("Import summary to use for testing the ThinLTO backend via opt"),
172 cl::Hidden);
173
175 TailCallSearchDepth("memprof-tail-call-search-depth", cl::init(5),
177 cl::desc("Max depth to recursively search for missing "
178 "frames through tail calls."));
179
180// Optionally enable cloning of callsites involved with recursive cycles
182 "memprof-allow-recursive-callsites", cl::init(true), cl::Hidden,
183 cl::desc("Allow cloning of callsites involved in recursive cycles"));
184
186 "memprof-clone-recursive-contexts", cl::init(true), cl::Hidden,
187 cl::desc("Allow cloning of contexts through recursive cycles"));
188
189// Generally this is needed for correct assignment of allocation clones to
190// function clones, however, allow it to be disabled for debugging while the
191// functionality is new and being tested more widely.
192static cl::opt<bool>
193 MergeClones("memprof-merge-clones", cl::init(true), cl::Hidden,
194 cl::desc("Merge clones before assigning functions"));
195
196// When disabled, try to detect and prevent cloning of recursive contexts.
197// This is only necessary until we support cloning through recursive cycles.
198// Leave on by default for now, as disabling requires a little bit of compile
199// time overhead and doesn't affect correctness, it will just inflate the cold
200// hinted bytes reporting a bit when -memprof-report-hinted-sizes is enabled.
202 "memprof-allow-recursive-contexts", cl::init(true), cl::Hidden,
203 cl::desc("Allow cloning of contexts having recursive cycles"));
204
205// Set the minimum absolute count threshold for allowing inlining of indirect
206// calls promoted during cloning.
208 "memprof-icp-noinline-threshold", cl::init(0), cl::Hidden,
209 cl::desc("Minimum absolute count for promoted target to be inlinable"));
210
211namespace llvm {
213 "enable-memprof-context-disambiguation", cl::Hidden,
214 cl::desc("Enable MemProf context disambiguation"));
215
216// Indicate we are linking with an allocator that supports hot/cold operator
217// new interfaces.
219 "supports-hot-cold-new", cl::init(false), cl::Hidden,
220 cl::desc("Linking with hot/cold operator new interfaces"));
221
223 "memprof-require-definition-for-promotion", cl::init(false), cl::Hidden,
224 cl::desc(
225 "Require target function definition when promoting indirect calls"));
226
229
231 "memprof-top-n-important", cl::init(10), cl::Hidden,
232 cl::desc("Number of largest cold contexts to consider important"));
233
235 "memprof-fixup-important", cl::init(true), cl::Hidden,
236 cl::desc("Enables edge fixup for important contexts"));
237
239
240} // namespace llvm
241
242namespace {
243
244/// CRTP base for graphs built from either IR or ThinLTO summary index.
245///
246/// The graph represents the call contexts in all memprof metadata on allocation
247/// calls, with nodes for the allocations themselves, as well as for the calls
248/// in each context. The graph is initially built from the allocation memprof
249/// metadata (or summary) MIBs. It is then updated to match calls with callsite
250/// metadata onto the nodes, updating it to reflect any inlining performed on
251/// those calls.
252///
253/// Each MIB (representing an allocation's call context with allocation
254/// behavior) is assigned a unique context id during the graph build. The edges
255/// and nodes in the graph are decorated with the context ids they carry. This
256/// is used to correctly update the graph when cloning is performed so that we
257/// can uniquify the context for a single (possibly cloned) allocation.
258template <typename DerivedCCG, typename FuncTy, typename CallTy>
259class CallsiteContextGraph {
260public:
261 CallsiteContextGraph() = default;
262 CallsiteContextGraph(const CallsiteContextGraph &) = default;
263 CallsiteContextGraph(CallsiteContextGraph &&) = default;
264
265 /// Main entry point to perform analysis and transformations on graph.
266 bool process(function_ref<void(StringRef, StringRef, const Twine &)>
267 EmitRemark = nullptr,
268 bool AllowExtraAnalysis = false);
269
270 /// Perform cloning on the graph necessary to uniquely identify the allocation
271 /// behavior of an allocation based on its context.
272 void identifyClones();
273
274 /// Assign callsite clones to functions, cloning functions as needed to
275 /// accommodate the combinations of their callsite clones reached by callers.
276 /// For regular LTO this clones functions and callsites in the IR, but for
277 /// ThinLTO the cloning decisions are noted in the summaries and later applied
278 /// in applyImport.
279 bool assignFunctions();
280
281 void dump() const;
282 void print(raw_ostream &OS) const;
283 void printTotalSizes(raw_ostream &OS,
284 function_ref<void(StringRef, StringRef, const Twine &)>
285 EmitRemark = nullptr) const;
286
288 const CallsiteContextGraph &CCG) {
289 CCG.print(OS);
290 return OS;
291 }
292
293 friend struct GraphTraits<
294 const CallsiteContextGraph<DerivedCCG, FuncTy, CallTy> *>;
295 friend struct DOTGraphTraits<
296 const CallsiteContextGraph<DerivedCCG, FuncTy, CallTy> *>;
297
298 void exportToDot(std::string Label) const;
299
300 /// Represents a function clone via FuncTy pointer and clone number pair.
301 struct FuncInfo final
302 : public std::pair<FuncTy *, unsigned /*Clone number*/> {
303 using Base = std::pair<FuncTy *, unsigned>;
304 FuncInfo(const Base &B) : Base(B) {}
305 FuncInfo(FuncTy *F = nullptr, unsigned CloneNo = 0) : Base(F, CloneNo) {}
306 explicit operator bool() const { return this->first != nullptr; }
307 FuncTy *func() const { return this->first; }
308 unsigned cloneNo() const { return this->second; }
309 };
310
311 /// Represents a callsite clone via CallTy and clone number pair.
312 struct CallInfo final : public std::pair<CallTy, unsigned /*Clone number*/> {
313 using Base = std::pair<CallTy, unsigned>;
314 CallInfo(const Base &B) : Base(B) {}
315 CallInfo(CallTy Call = nullptr, unsigned CloneNo = 0)
316 : Base(Call, CloneNo) {}
317 explicit operator bool() const { return (bool)this->first; }
318 CallTy call() const { return this->first; }
319 unsigned cloneNo() const { return this->second; }
320 void setCloneNo(unsigned N) { this->second = N; }
321 void print(raw_ostream &OS) const {
322 if (!operator bool()) {
323 assert(!cloneNo());
324 OS << "null Call";
325 return;
326 }
327 call()->print(OS);
328 OS << "\t(clone " << cloneNo() << ")";
329 }
330 void dump() const {
331 print(dbgs());
332 dbgs() << "\n";
333 }
334 friend raw_ostream &operator<<(raw_ostream &OS, const CallInfo &Call) {
335 Call.print(OS);
336 return OS;
337 }
338 };
339
340 struct ContextEdge;
341
342 /// Node in the Callsite Context Graph
343 struct ContextNode {
344 // Assigned to nodes as they are created, useful for debugging.
345 unsigned NodeId = 0;
346
347 // Keep this for now since in the IR case where we have an Instruction* it
348 // is not as immediately discoverable. Used for printing richer information
349 // when dumping graph.
350 bool IsAllocation;
351
352 // Keeps track of when the Call was reset to null because there was
353 // recursion.
354 bool Recursive = false;
355
356 // This will be formed by ORing together the AllocationType enum values
357 // for contexts including this node.
358 uint8_t AllocTypes = 0;
359
360 // The corresponding allocation or interior call. This is the primary call
361 // for which we have created this node.
362 CallInfo Call;
363
364 // List of other calls that can be treated the same as the primary call
365 // through cloning. I.e. located in the same function and have the same
366 // (possibly pruned) stack ids. They will be updated the same way as the
367 // primary call when assigning to function clones.
368 SmallVector<CallInfo, 0> MatchingCalls;
369
370 // For alloc nodes this is a unique id assigned when constructed, and for
371 // callsite stack nodes it is the original stack id when the node is
372 // constructed from the memprof MIB metadata on the alloc nodes. Note that
373 // this is only used when matching callsite metadata onto the stack nodes
374 // created when processing the allocation memprof MIBs, and for labeling
375 // nodes in the dot graph. Therefore we don't bother to assign a value for
376 // clones.
377 uint64_t OrigStackOrAllocId = 0;
378
379 // Edges to all callees in the profiled call stacks.
380 // TODO: Should this be a map (from Callee node) for more efficient lookup?
381 std::vector<std::shared_ptr<ContextEdge>> CalleeEdges;
382
383 // Edges to all callers in the profiled call stacks.
384 // TODO: Should this be a map (from Caller node) for more efficient lookup?
385 std::vector<std::shared_ptr<ContextEdge>> CallerEdges;
386
387 // Returns true if we need to look at the callee edges for determining the
388 // node context ids and allocation type.
389 bool useCallerEdgesForContextInfo() const {
390 // Typically if the callee edges are empty either the caller edges are
391 // also empty, or this is an allocation (leaf node). However, if we are
392 // allowing recursive callsites and contexts this will be violated for
393 // incompletely cloned recursive cycles.
394 assert(!CalleeEdges.empty() || CallerEdges.empty() || IsAllocation ||
396 // When cloning for a recursive context, during cloning we might be in the
397 // midst of cloning for a recurrence and have moved context ids off of a
398 // caller edge onto the clone but not yet off of the incoming caller
399 // (back) edge. If we don't look at those we miss the fact that this node
400 // still has context ids of interest.
401 return IsAllocation || CloneRecursiveContexts;
402 }
403
404 // Compute the context ids for this node from the union of its edge context
405 // ids.
406 DenseSet<uint32_t> getContextIds() const {
407 unsigned Count = 0;
408 // Compute the number of ids for reserve below. In general we only need to
409 // look at one set of edges, typically the callee edges, since other than
410 // allocations and in some cases during recursion cloning, all the context
411 // ids on the callers should also flow out via callee edges.
412 for (auto &Edge : CalleeEdges.empty() ? CallerEdges : CalleeEdges)
413 Count += Edge->getContextIds().size();
414 DenseSet<uint32_t> ContextIds;
415 ContextIds.reserve(Count);
417 CalleeEdges, useCallerEdgesForContextInfo()
418 ? CallerEdges
419 : std::vector<std::shared_ptr<ContextEdge>>());
420 for (const auto &Edge : Edges)
421 ContextIds.insert_range(Edge->getContextIds());
422 return ContextIds;
423 }
424
425 // Compute the allocation type for this node from the OR of its edge
426 // allocation types.
427 uint8_t computeAllocType() const {
428 uint8_t BothTypes =
432 CalleeEdges, useCallerEdgesForContextInfo()
433 ? CallerEdges
434 : std::vector<std::shared_ptr<ContextEdge>>());
435 for (const auto &Edge : Edges) {
436 AllocType |= Edge->AllocTypes;
437 // Bail early if alloc type reached both, no further refinement.
438 if (AllocType == BothTypes)
439 return AllocType;
440 }
441 return AllocType;
442 }
443
444 // The context ids set for this node is empty if its edge context ids are
445 // also all empty.
446 bool emptyContextIds() const {
448 CalleeEdges, useCallerEdgesForContextInfo()
449 ? CallerEdges
450 : std::vector<std::shared_ptr<ContextEdge>>());
451 for (const auto &Edge : Edges) {
452 if (!Edge->getContextIds().empty())
453 return false;
454 }
455 return true;
456 }
457
458 // List of clones of this ContextNode, initially empty.
459 std::vector<ContextNode *> Clones;
460
461 // If a clone, points to the original uncloned node.
462 ContextNode *CloneOf = nullptr;
463
464 ContextNode(bool IsAllocation) : IsAllocation(IsAllocation), Call() {}
465
466 ContextNode(bool IsAllocation, CallInfo C)
467 : IsAllocation(IsAllocation), Call(C) {}
468
469 void addClone(ContextNode *Clone) {
470 if (CloneOf) {
471 CloneOf->Clones.push_back(Clone);
472 Clone->CloneOf = CloneOf;
473 } else {
474 Clones.push_back(Clone);
475 assert(!Clone->CloneOf);
476 Clone->CloneOf = this;
477 }
478 }
479
480 ContextNode *getOrigNode() {
481 if (!CloneOf)
482 return this;
483 return CloneOf;
484 }
485
486 void addOrUpdateCallerEdge(ContextNode *Caller, AllocationType AllocType,
487 unsigned int ContextId);
488
489 ContextEdge *findEdgeFromCallee(const ContextNode *Callee);
490 ContextEdge *findEdgeFromCaller(const ContextNode *Caller);
491 void eraseCalleeEdge(const ContextEdge *Edge);
492 void eraseCallerEdge(const ContextEdge *Edge);
493
494 void setCall(CallInfo C) { Call = std::move(C); }
495
496 bool hasCall() const { return (bool)Call.call(); }
497
498 void printCall(raw_ostream &OS) const { Call.print(OS); }
499
500 // True if this node was effectively removed from the graph, in which case
501 // it should have an allocation type of None and empty context ids.
502 bool isRemoved() const {
503 // Typically if the callee edges are empty either the caller edges are
504 // also empty, or this is an allocation (leaf node). However, if we are
505 // allowing recursive callsites and contexts this will be violated for
506 // incompletely cloned recursive cycles.
508 (AllocTypes == (uint8_t)AllocationType::None) ==
509 emptyContextIds());
510 return AllocTypes == (uint8_t)AllocationType::None;
511 }
512
513 void dump() const;
514 void print(raw_ostream &OS) const;
515
516 friend raw_ostream &operator<<(raw_ostream &OS, const ContextNode &Node) {
517 Node.print(OS);
518 return OS;
519 }
520 };
521
522 /// Edge in the Callsite Context Graph from a ContextNode N to a caller or
523 /// callee.
524 struct ContextEdge {
525 ContextNode *Callee;
526 ContextNode *Caller;
527
528 // This will be formed by ORing together the AllocationType enum values
529 // for contexts including this edge.
530 uint8_t AllocTypes = 0;
531
532 // Set just before initiating cloning when cloning of recursive contexts is
533 // enabled. Used to defer cloning of backedges until we have done cloning of
534 // the callee node for non-backedge caller edges. This exposes cloning
535 // opportunities through the backedge of the cycle.
536 // TODO: Note that this is not updated during cloning, and it is unclear
537 // whether that would be needed.
538 bool IsBackedge = false;
539
540 // The set of IDs for contexts including this edge.
541 DenseSet<uint32_t> ContextIds;
542
543 ContextEdge(ContextNode *Callee, ContextNode *Caller, uint8_t AllocType,
544 DenseSet<uint32_t> ContextIds)
545 : Callee(Callee), Caller(Caller), AllocTypes(AllocType),
546 ContextIds(std::move(ContextIds)) {}
547
548 DenseSet<uint32_t> &getContextIds() { return ContextIds; }
549
550 // Helper to clear the fields of this edge when we are removing it from the
551 // graph.
552 inline void clear() {
553 ContextIds.clear();
554 AllocTypes = (uint8_t)AllocationType::None;
555 Caller = nullptr;
556 Callee = nullptr;
557 }
558
559 // Check if edge was removed from the graph. This is useful while iterating
560 // over a copy of edge lists when performing operations that mutate the
561 // graph in ways that might remove one of the edges.
562 inline bool isRemoved() const {
563 if (Callee || Caller)
564 return false;
565 // Any edges that have been removed from the graph but are still in a
566 // shared_ptr somewhere should have all fields null'ed out by clear()
567 // above.
568 assert(AllocTypes == (uint8_t)AllocationType::None);
569 assert(ContextIds.empty());
570 return true;
571 }
572
573 void dump() const;
574 void print(raw_ostream &OS) const;
575
576 friend raw_ostream &operator<<(raw_ostream &OS, const ContextEdge &Edge) {
577 Edge.print(OS);
578 return OS;
579 }
580 };
581
582 /// Helpers to remove edges that have allocation type None (due to not
583 /// carrying any context ids) after transformations.
584 void removeNoneTypeCalleeEdges(ContextNode *Node);
585 void removeNoneTypeCallerEdges(ContextNode *Node);
586 void
587 recursivelyRemoveNoneTypeCalleeEdges(ContextNode *Node,
589
590protected:
591 /// Get a list of nodes corresponding to the stack ids in the given callsite
592 /// context.
593 template <class NodeT, class IteratorT>
594 std::vector<uint64_t>
595 getStackIdsWithContextNodes(CallStack<NodeT, IteratorT> &CallsiteContext);
596
597 /// Adds nodes for the given allocation and any stack ids on its memprof MIB
598 /// metadata (or summary).
599 ContextNode *addAllocNode(CallInfo Call, const FuncTy *F);
600
601 /// Adds nodes for the given MIB stack ids.
602 template <class NodeT, class IteratorT>
603 void addStackNodesForMIB(
604 ContextNode *AllocNode, CallStack<NodeT, IteratorT> &StackContext,
606 ArrayRef<ContextTotalSize> ContextSizeInfo,
607 std::map<uint64_t, uint32_t> &TotalSizeToContextIdTopNCold);
608
609 /// Matches all callsite metadata (or summary) to the nodes created for
610 /// allocation memprof MIB metadata, synthesizing new nodes to reflect any
611 /// inlining performed on those callsite instructions.
612 void updateStackNodes();
613
614 /// Optionally fixup edges for the N largest cold contexts to better enable
615 /// cloning. This is particularly helpful if the context includes recursion
616 /// as well as inlining, resulting in a single stack node for multiple stack
617 /// ids in the context. With recursion it is particularly difficult to get the
618 /// edge updates correct as in the general case we have lost the original
619 /// stack id ordering for the context. Do more expensive fixup for the largest
620 /// contexts, controlled by MemProfTopNImportant and MemProfFixupImportant.
621 void fixupImportantContexts();
622
623 /// Update graph to conservatively handle any callsite stack nodes that target
624 /// multiple different callee target functions.
625 void handleCallsitesWithMultipleTargets();
626
627 /// Mark backedges via the standard DFS based backedge algorithm.
628 void markBackedges();
629
630 /// Merge clones generated during cloning for different allocations but that
631 /// are called by the same caller node, to ensure proper function assignment.
632 void mergeClones();
633
634 // Try to partition calls on the given node (already placed into the AllCalls
635 // array) by callee function, creating new copies of Node as needed to hold
636 // calls with different callees, and moving the callee edges appropriately.
637 // Returns true if partitioning was successful.
638 bool partitionCallsByCallee(
639 ContextNode *Node, ArrayRef<CallInfo> AllCalls,
640 std::vector<std::pair<CallInfo, ContextNode *>> &NewCallToNode);
641
642 /// Save lists of calls with MemProf metadata in each function, for faster
643 /// iteration.
644 MapVector<FuncTy *, std::vector<CallInfo>> FuncToCallsWithMetadata;
645
646 /// Map from callsite node to the enclosing caller function.
647 std::map<const ContextNode *, const FuncTy *> NodeToCallingFunc;
648
649 // When exporting to dot, and an allocation id is specified, contains the
650 // context ids on that allocation.
651 DenseSet<uint32_t> DotAllocContextIds;
652
653private:
654 using EdgeIter = typename std::vector<std::shared_ptr<ContextEdge>>::iterator;
655
656 // Structure to keep track of information for each call as we are matching
657 // non-allocation callsites onto context nodes created from the allocation
658 // call metadata / summary contexts.
659 struct CallContextInfo {
660 // The callsite we're trying to match.
661 CallTy Call;
662 // The callsites stack ids that have a context node in the graph.
663 std::vector<uint64_t> StackIds;
664 // The function containing this callsite.
665 const FuncTy *Func;
666 // Initially empty, if needed this will be updated to contain the context
667 // ids for use in a new context node created for this callsite.
668 DenseSet<uint32_t> ContextIds;
669 };
670
671 /// Helper to remove edge from graph, updating edge iterator if it is provided
672 /// (in which case CalleeIter indicates which edge list is being iterated).
673 /// This will also perform the necessary clearing of the ContextEdge members
674 /// to enable later checking if the edge has been removed (since we may have
675 /// other copies of the shared_ptr in existence, and in fact rely on this to
676 /// enable removal while iterating over a copy of a node's edge list).
677 void removeEdgeFromGraph(ContextEdge *Edge, EdgeIter *EI = nullptr,
678 bool CalleeIter = true);
679
680 /// Assigns the given Node to calls at or inlined into the location with
681 /// the Node's stack id, after post order traversing and processing its
682 /// caller nodes. Uses the call information recorded in the given
683 /// StackIdToMatchingCalls map, and creates new nodes for inlined sequences
684 /// as needed. Called by updateStackNodes which sets up the given
685 /// StackIdToMatchingCalls map.
686 void assignStackNodesPostOrder(
687 ContextNode *Node, DenseSet<const ContextNode *> &Visited,
688 DenseMap<uint64_t, std::vector<CallContextInfo>> &StackIdToMatchingCalls,
689 DenseMap<CallInfo, CallInfo> &CallToMatchingCall,
690 const DenseSet<uint32_t> &ImportantContextIds);
691
692 /// Duplicates the given set of context ids, updating the provided
693 /// map from each original id with the newly generated context ids,
694 /// and returning the new duplicated id set.
695 DenseSet<uint32_t> duplicateContextIds(
696 const DenseSet<uint32_t> &StackSequenceContextIds,
697 DenseMap<uint32_t, DenseSet<uint32_t>> &OldToNewContextIds);
698
699 /// Propagates all duplicated context ids across the graph.
700 void propagateDuplicateContextIds(
701 const DenseMap<uint32_t, DenseSet<uint32_t>> &OldToNewContextIds);
702
703 /// Connect the NewNode to OrigNode's callees if TowardsCallee is true,
704 /// else to its callers. Also updates OrigNode's edges to remove any context
705 /// ids moved to the newly created edge.
706 void connectNewNode(ContextNode *NewNode, ContextNode *OrigNode,
707 bool TowardsCallee,
708 DenseSet<uint32_t> RemainingContextIds);
709
710 /// Get the stack id corresponding to the given Id or Index (for IR this will
711 /// return itself, for a summary index this will return the id recorded in the
712 /// index for that stack id index value).
713 uint64_t getStackId(uint64_t IdOrIndex) const {
714 return static_cast<const DerivedCCG *>(this)->getStackId(IdOrIndex);
715 }
716
717 /// Returns true if the given call targets the callee of the given edge, or if
718 /// we were able to identify the call chain through intermediate tail calls.
719 /// In the latter case new context nodes are added to the graph for the
720 /// identified tail calls, and their synthesized nodes are added to
721 /// TailCallToContextNodeMap. The EdgeIter is updated in the latter case for
722 /// the updated edges and to prepare it for an increment in the caller.
723 bool
724 calleesMatch(CallTy Call, EdgeIter &EI,
725 MapVector<CallInfo, ContextNode *> &TailCallToContextNodeMap);
726
727 // Return the callee function of the given call, or nullptr if it can't be
728 // determined
729 const FuncTy *getCalleeFunc(CallTy Call) {
730 return static_cast<DerivedCCG *>(this)->getCalleeFunc(Call);
731 }
732
733 /// Returns true if the given call targets the given function, or if we were
734 /// able to identify the call chain through intermediate tail calls (in which
735 /// case FoundCalleeChain will be populated).
736 bool calleeMatchesFunc(
737 CallTy Call, const FuncTy *Func, const FuncTy *CallerFunc,
738 std::vector<std::pair<CallTy, FuncTy *>> &FoundCalleeChain) {
739 return static_cast<DerivedCCG *>(this)->calleeMatchesFunc(
740 Call, Func, CallerFunc, FoundCalleeChain);
741 }
742
743 /// Returns true if both call instructions have the same callee.
744 bool sameCallee(CallTy Call1, CallTy Call2) {
745 return static_cast<DerivedCCG *>(this)->sameCallee(Call1, Call2);
746 }
747
748 /// Get a list of nodes corresponding to the stack ids in the given
749 /// callsite's context.
750 std::vector<uint64_t> getStackIdsWithContextNodesForCall(CallTy Call) {
751 return static_cast<DerivedCCG *>(this)->getStackIdsWithContextNodesForCall(
752 Call);
753 }
754
755 /// Get the last stack id in the context for callsite.
756 uint64_t getLastStackId(CallTy Call) {
757 return static_cast<DerivedCCG *>(this)->getLastStackId(Call);
758 }
759
760 /// Update the allocation call to record type of allocated memory.
761 void updateAllocationCall(CallInfo &Call, AllocationType AllocType) {
762 AllocType == AllocationType::Cold ? AllocTypeCold++ : AllocTypeNotCold++;
763 static_cast<DerivedCCG *>(this)->updateAllocationCall(Call, AllocType);
764 }
765
766 /// Get the AllocationType assigned to the given allocation instruction clone.
767 AllocationType getAllocationCallType(const CallInfo &Call) const {
768 return static_cast<const DerivedCCG *>(this)->getAllocationCallType(Call);
769 }
770
771 /// Update non-allocation call to invoke (possibly cloned) function
772 /// CalleeFunc.
773 void updateCall(CallInfo &CallerCall, FuncInfo CalleeFunc) {
774 static_cast<DerivedCCG *>(this)->updateCall(CallerCall, CalleeFunc);
775 }
776
777 /// Clone the given function for the given callsite, recording mapping of all
778 /// of the functions tracked calls to their new versions in the CallMap.
779 /// Assigns new clones to clone number CloneNo.
780 FuncInfo cloneFunctionForCallsite(
781 FuncInfo &Func, CallInfo &Call, DenseMap<CallInfo, CallInfo> &CallMap,
782 std::vector<CallInfo> &CallsWithMetadataInFunc, unsigned CloneNo) {
783 return static_cast<DerivedCCG *>(this)->cloneFunctionForCallsite(
784 Func, Call, CallMap, CallsWithMetadataInFunc, CloneNo);
785 }
786
787 /// Gets a label to use in the dot graph for the given call clone in the given
788 /// function.
789 std::string getLabel(const FuncTy *Func, const CallTy Call,
790 unsigned CloneNo) const {
791 return static_cast<const DerivedCCG *>(this)->getLabel(Func, Call, CloneNo);
792 }
793
794 // Create and return a new ContextNode.
795 ContextNode *createNewNode(bool IsAllocation, const FuncTy *F = nullptr,
796 CallInfo C = CallInfo()) {
797 NodeOwner.push_back(std::make_unique<ContextNode>(IsAllocation, C));
798 auto *NewNode = NodeOwner.back().get();
799 if (F)
800 NodeToCallingFunc[NewNode] = F;
801 NewNode->NodeId = NodeOwner.size();
802 return NewNode;
803 }
804
805 /// Helpers to find the node corresponding to the given call or stackid.
806 ContextNode *getNodeForInst(const CallInfo &C);
807 ContextNode *getNodeForAlloc(const CallInfo &C);
808 ContextNode *getNodeForStackId(uint64_t StackId);
809
810 /// Computes the alloc type corresponding to the given context ids, by
811 /// unioning their recorded alloc types.
812 uint8_t computeAllocType(DenseSet<uint32_t> &ContextIds) const;
813
814 /// Returns the allocation type of the intersection of the contexts of two
815 /// nodes (based on their provided context id sets), optimized for the case
816 /// when Node1Ids is smaller than Node2Ids.
817 uint8_t intersectAllocTypesImpl(const DenseSet<uint32_t> &Node1Ids,
818 const DenseSet<uint32_t> &Node2Ids) const;
819
820 /// Returns the allocation type of the intersection of the contexts of two
821 /// nodes (based on their provided context id sets).
822 uint8_t intersectAllocTypes(const DenseSet<uint32_t> &Node1Ids,
823 const DenseSet<uint32_t> &Node2Ids) const;
824
825 /// Create a clone of Edge's callee and move Edge to that new callee node,
826 /// performing the necessary context id and allocation type updates.
827 /// If ContextIdsToMove is non-empty, only that subset of Edge's ids are
828 /// moved to an edge to the new callee.
829 ContextNode *
830 moveEdgeToNewCalleeClone(const std::shared_ptr<ContextEdge> &Edge,
831 DenseSet<uint32_t> ContextIdsToMove = {});
832
833 /// Change the callee of Edge to existing callee clone NewCallee, performing
834 /// the necessary context id and allocation type updates.
835 /// If ContextIdsToMove is non-empty, only that subset of Edge's ids are
836 /// moved to an edge to the new callee.
837 void moveEdgeToExistingCalleeClone(const std::shared_ptr<ContextEdge> &Edge,
838 ContextNode *NewCallee,
839 bool NewClone = false,
840 DenseSet<uint32_t> ContextIdsToMove = {});
841
842 /// Change the caller of the edge at the given callee edge iterator to be
843 /// NewCaller, performing the necessary context id and allocation type
844 /// updates. This is similar to the above moveEdgeToExistingCalleeClone, but
845 /// a simplified version of it as we always move the given edge and all of its
846 /// context ids.
847 void moveCalleeEdgeToNewCaller(const std::shared_ptr<ContextEdge> &Edge,
848 ContextNode *NewCaller);
849
850 /// Recursive helper for marking backedges via DFS.
851 void markBackedges(ContextNode *Node, DenseSet<const ContextNode *> &Visited,
852 DenseSet<const ContextNode *> &CurrentStack);
853
854 /// Recursive helper for merging clones.
855 void
856 mergeClones(ContextNode *Node, DenseSet<const ContextNode *> &Visited,
857 DenseMap<uint32_t, ContextNode *> &ContextIdToAllocationNode);
858 /// Main worker for merging callee clones for a given node.
859 void mergeNodeCalleeClones(
860 ContextNode *Node, DenseSet<const ContextNode *> &Visited,
861 DenseMap<uint32_t, ContextNode *> &ContextIdToAllocationNode);
862 /// Helper to find other callers of the given set of callee edges that can
863 /// share the same callee merge node.
864 void findOtherCallersToShareMerge(
865 ContextNode *Node, std::vector<std::shared_ptr<ContextEdge>> &CalleeEdges,
866 DenseMap<uint32_t, ContextNode *> &ContextIdToAllocationNode,
867 DenseSet<ContextNode *> &OtherCallersToShareMerge);
868
869 /// Recursively perform cloning on the graph for the given Node and its
870 /// callers, in order to uniquely identify the allocation behavior of an
871 /// allocation given its context. The context ids of the allocation being
872 /// processed are given in AllocContextIds.
873 void identifyClones(ContextNode *Node, DenseSet<const ContextNode *> &Visited,
874 const DenseSet<uint32_t> &AllocContextIds);
875
876 /// Map from each context ID to the AllocationType assigned to that context.
877 DenseMap<uint32_t, AllocationType> ContextIdToAllocationType;
878
879 /// Map from each contextID to the profiled full contexts and their total
880 /// sizes (there may be more than one due to context trimming),
881 /// optionally populated when requested (via MemProfReportHintedSizes or
882 /// MinClonedColdBytePercent).
883 DenseMap<uint32_t, std::vector<ContextTotalSize>> ContextIdToContextSizeInfos;
884
885 /// Identifies the context node created for a stack id when adding the MIB
886 /// contexts to the graph. This is used to locate the context nodes when
887 /// trying to assign the corresponding callsites with those stack ids to these
888 /// nodes.
889 DenseMap<uint64_t, ContextNode *> StackEntryIdToContextNodeMap;
890
891 /// Saves information for the contexts identified as important (the largest
892 /// cold contexts up to MemProfTopNImportant).
893 struct ImportantContextInfo {
894 // The original list of leaf first stack ids corresponding to this context.
895 std::vector<uint64_t> StackIds;
896 // Max length of stack ids corresponding to a single stack ContextNode for
897 // this context (i.e. the max length of a key in StackIdsToNode below).
898 unsigned MaxLength = 0;
899 // Mapping of slices of the stack ids to the corresponding ContextNode
900 // (there can be multiple stack ids due to inlining). Populated when
901 // updating stack nodes while matching them to the IR or summary.
902 std::map<std::vector<uint64_t>, ContextNode *> StackIdsToNode;
903 };
904
905 // Map of important full context ids to information about each.
906 DenseMap<uint32_t, ImportantContextInfo> ImportantContextIdInfo;
907
908 // For each important context id found in Node (if any), records the list of
909 // stack ids that corresponded to the given callsite Node. There can be more
910 // than one in the case of inlining.
911 void recordStackNode(std::vector<uint64_t> &StackIds, ContextNode *Node,
912 // We pass in the Node's context ids to avoid the
913 // overhead of computing them as the caller already has
914 // them in some cases.
915 const DenseSet<uint32_t> &NodeContextIds,
916 const DenseSet<uint32_t> &ImportantContextIds) {
918 assert(ImportantContextIds.empty());
919 return;
920 }
922 set_intersection(NodeContextIds, ImportantContextIds);
923 if (Ids.empty())
924 return;
925 auto Size = StackIds.size();
926 for (auto Id : Ids) {
927 auto &Entry = ImportantContextIdInfo[Id];
928 Entry.StackIdsToNode[StackIds] = Node;
929 // Keep track of the max to simplify later analysis.
930 if (Size > Entry.MaxLength)
931 Entry.MaxLength = Size;
932 }
933 }
934
935 /// Maps to track the calls to their corresponding nodes in the graph.
936 MapVector<CallInfo, ContextNode *> AllocationCallToContextNodeMap;
937 MapVector<CallInfo, ContextNode *> NonAllocationCallToContextNodeMap;
938
939 /// Owner of all ContextNode unique_ptrs.
940 std::vector<std::unique_ptr<ContextNode>> NodeOwner;
941
942 /// Perform sanity checks on graph when requested.
943 void check() const;
944
945 /// Keeps track of the last unique context id assigned.
946 unsigned int LastContextId = 0;
947};
948
949template <typename DerivedCCG, typename FuncTy, typename CallTy>
950using ContextNode =
951 typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode;
952template <typename DerivedCCG, typename FuncTy, typename CallTy>
953using ContextEdge =
954 typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextEdge;
955template <typename DerivedCCG, typename FuncTy, typename CallTy>
956using FuncInfo =
957 typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::FuncInfo;
958template <typename DerivedCCG, typename FuncTy, typename CallTy>
959using CallInfo =
960 typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::CallInfo;
961
962/// CRTP derived class for graphs built from IR (regular LTO).
963class ModuleCallsiteContextGraph
964 : public CallsiteContextGraph<ModuleCallsiteContextGraph, Function,
965 Instruction *> {
966public:
967 ModuleCallsiteContextGraph(
968 Module &M,
969 llvm::function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter);
970
971private:
972 friend CallsiteContextGraph<ModuleCallsiteContextGraph, Function,
973 Instruction *>;
974
975 uint64_t getStackId(uint64_t IdOrIndex) const;
976 const Function *getCalleeFunc(Instruction *Call);
977 bool calleeMatchesFunc(
978 Instruction *Call, const Function *Func, const Function *CallerFunc,
979 std::vector<std::pair<Instruction *, Function *>> &FoundCalleeChain);
980 bool sameCallee(Instruction *Call1, Instruction *Call2);
981 bool findProfiledCalleeThroughTailCalls(
982 const Function *ProfiledCallee, Value *CurCallee, unsigned Depth,
983 std::vector<std::pair<Instruction *, Function *>> &FoundCalleeChain,
984 bool &FoundMultipleCalleeChains);
985 uint64_t getLastStackId(Instruction *Call);
986 std::vector<uint64_t> getStackIdsWithContextNodesForCall(Instruction *Call);
987 void updateAllocationCall(CallInfo &Call, AllocationType AllocType);
988 AllocationType getAllocationCallType(const CallInfo &Call) const;
989 void updateCall(CallInfo &CallerCall, FuncInfo CalleeFunc);
990 CallsiteContextGraph<ModuleCallsiteContextGraph, Function,
991 Instruction *>::FuncInfo
992 cloneFunctionForCallsite(FuncInfo &Func, CallInfo &Call,
993 DenseMap<CallInfo, CallInfo> &CallMap,
994 std::vector<CallInfo> &CallsWithMetadataInFunc,
995 unsigned CloneNo);
996 std::string getLabel(const Function *Func, const Instruction *Call,
997 unsigned CloneNo) const;
998
999 const Module &Mod;
1000 llvm::function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter;
1001};
1002
1003/// Represents a call in the summary index graph, which can either be an
1004/// allocation or an interior callsite node in an allocation's context.
1005/// Holds a pointer to the corresponding data structure in the index.
1006struct IndexCall : public PointerUnion<CallsiteInfo *, AllocInfo *> {
1007 IndexCall() : PointerUnion() {}
1008 IndexCall(std::nullptr_t) : IndexCall() {}
1009 IndexCall(CallsiteInfo *StackNode) : PointerUnion(StackNode) {}
1010 IndexCall(AllocInfo *AllocNode) : PointerUnion(AllocNode) {}
1011 IndexCall(PointerUnion PT) : PointerUnion(PT) {}
1012
1013 IndexCall *operator->() { return this; }
1014
1015 void print(raw_ostream &OS) const {
1016 PointerUnion<CallsiteInfo *, AllocInfo *> Base = *this;
1018 OS << *AI;
1019 } else {
1021 assert(CI);
1022 OS << *CI;
1023 }
1024 }
1025};
1026} // namespace
1027
1028namespace llvm {
1029template <> struct simplify_type<IndexCall> {
1031 static SimpleType getSimplifiedValue(IndexCall &Val) { return Val; }
1032};
1033template <> struct simplify_type<const IndexCall> {
1035 static SimpleType getSimplifiedValue(const IndexCall &Val) { return Val; }
1036};
1037} // namespace llvm
1038
1039namespace {
1040/// CRTP derived class for graphs built from summary index (ThinLTO).
1041class IndexCallsiteContextGraph
1042 : public CallsiteContextGraph<IndexCallsiteContextGraph, FunctionSummary,
1043 IndexCall> {
1044public:
1045 IndexCallsiteContextGraph(
1046 ModuleSummaryIndex &Index,
1047 llvm::function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
1048 isPrevailing);
1049
1050 ~IndexCallsiteContextGraph() {
1051 // Now that we are done with the graph it is safe to add the new
1052 // CallsiteInfo structs to the function summary vectors. The graph nodes
1053 // point into locations within these vectors, so we don't want to add them
1054 // any earlier.
1055 for (auto &I : FunctionCalleesToSynthesizedCallsiteInfos) {
1056 auto *FS = I.first;
1057 for (auto &Callsite : I.second)
1058 FS->addCallsite(std::move(*Callsite.second));
1059 }
1060 }
1061
1062private:
1063 friend CallsiteContextGraph<IndexCallsiteContextGraph, FunctionSummary,
1064 IndexCall>;
1065
1066 uint64_t getStackId(uint64_t IdOrIndex) const;
1067 const FunctionSummary *getCalleeFunc(IndexCall &Call);
1068 bool calleeMatchesFunc(
1069 IndexCall &Call, const FunctionSummary *Func,
1070 const FunctionSummary *CallerFunc,
1071 std::vector<std::pair<IndexCall, FunctionSummary *>> &FoundCalleeChain);
1072 bool sameCallee(IndexCall &Call1, IndexCall &Call2);
1073 bool findProfiledCalleeThroughTailCalls(
1074 ValueInfo ProfiledCallee, ValueInfo CurCallee, unsigned Depth,
1075 std::vector<std::pair<IndexCall, FunctionSummary *>> &FoundCalleeChain,
1076 bool &FoundMultipleCalleeChains);
1077 uint64_t getLastStackId(IndexCall &Call);
1078 std::vector<uint64_t> getStackIdsWithContextNodesForCall(IndexCall &Call);
1079 void updateAllocationCall(CallInfo &Call, AllocationType AllocType);
1080 AllocationType getAllocationCallType(const CallInfo &Call) const;
1081 void updateCall(CallInfo &CallerCall, FuncInfo CalleeFunc);
1082 CallsiteContextGraph<IndexCallsiteContextGraph, FunctionSummary,
1083 IndexCall>::FuncInfo
1084 cloneFunctionForCallsite(FuncInfo &Func, CallInfo &Call,
1085 DenseMap<CallInfo, CallInfo> &CallMap,
1086 std::vector<CallInfo> &CallsWithMetadataInFunc,
1087 unsigned CloneNo);
1088 std::string getLabel(const FunctionSummary *Func, const IndexCall &Call,
1089 unsigned CloneNo) const;
1090 DenseSet<GlobalValue::GUID> findAliaseeGUIDsPrevailingInDifferentModule();
1091
1092 // Saves mapping from function summaries containing memprof records back to
1093 // its VI, for use in checking and debugging.
1094 std::map<const FunctionSummary *, ValueInfo> FSToVIMap;
1095
1096 const ModuleSummaryIndex &Index;
1097 llvm::function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
1098 isPrevailing;
1099
1100 // Saves/owns the callsite info structures synthesized for missing tail call
1101 // frames that we discover while building the graph.
1102 // It maps from the summary of the function making the tail call, to a map
1103 // of callee ValueInfo to corresponding synthesized callsite info.
1104 DenseMap<FunctionSummary *,
1105 std::map<ValueInfo, std::unique_ptr<CallsiteInfo>>>
1106 FunctionCalleesToSynthesizedCallsiteInfos;
1107};
1108} // namespace
1109
1110template <>
1111struct llvm::DenseMapInfo<CallsiteContextGraph<
1112 ModuleCallsiteContextGraph, Function, Instruction *>::CallInfo>
1114template <>
1115struct llvm::DenseMapInfo<CallsiteContextGraph<
1116 IndexCallsiteContextGraph, FunctionSummary, IndexCall>::CallInfo>
1117 : public DenseMapInfo<std::pair<IndexCall, unsigned>> {};
1118template <>
1119struct llvm::DenseMapInfo<IndexCall>
1120 : public DenseMapInfo<PointerUnion<CallsiteInfo *, AllocInfo *>> {};
1121
1122namespace {
1123
1124// Map the uint8_t alloc types (which may contain NotCold|Cold) to the alloc
1125// type we should actually use on the corresponding allocation.
1126// If we can't clone a node that has NotCold+Cold alloc type, we will fall
1127// back to using NotCold. So don't bother cloning to distinguish NotCold+Cold
1128// from NotCold.
1129AllocationType allocTypeToUse(uint8_t AllocTypes) {
1130 assert(AllocTypes != (uint8_t)AllocationType::None);
1131 if (AllocTypes ==
1134 else
1135 return (AllocationType)AllocTypes;
1136}
1137
1138// Helper to check if the alloc types for all edges recorded in the
1139// InAllocTypes vector match the alloc types for all edges in the Edges
1140// vector.
1141template <typename DerivedCCG, typename FuncTy, typename CallTy>
1142bool allocTypesMatch(
1143 const std::vector<uint8_t> &InAllocTypes,
1144 const std::vector<std::shared_ptr<ContextEdge<DerivedCCG, FuncTy, CallTy>>>
1145 &Edges) {
1146 // This should be called only when the InAllocTypes vector was computed for
1147 // this set of Edges. Make sure the sizes are the same.
1148 assert(InAllocTypes.size() == Edges.size());
1149 return std::equal(
1150 InAllocTypes.begin(), InAllocTypes.end(), Edges.begin(), Edges.end(),
1151 [](const uint8_t &l,
1152 const std::shared_ptr<ContextEdge<DerivedCCG, FuncTy, CallTy>> &r) {
1153 // Can share if one of the edges is None type - don't
1154 // care about the type along that edge as it doesn't
1155 // exist for those context ids.
1156 if (l == (uint8_t)AllocationType::None ||
1157 r->AllocTypes == (uint8_t)AllocationType::None)
1158 return true;
1159 return allocTypeToUse(l) == allocTypeToUse(r->AllocTypes);
1160 });
1161}
1162
1163// Helper to check if the alloc types for all edges recorded in the
1164// InAllocTypes vector match the alloc types for callee edges in the given
1165// clone. Because the InAllocTypes were computed from the original node's callee
1166// edges, and other cloning could have happened after this clone was created, we
1167// need to find the matching clone callee edge, which may or may not exist.
1168template <typename DerivedCCG, typename FuncTy, typename CallTy>
1169bool allocTypesMatchClone(
1170 const std::vector<uint8_t> &InAllocTypes,
1171 const ContextNode<DerivedCCG, FuncTy, CallTy> *Clone) {
1172 const ContextNode<DerivedCCG, FuncTy, CallTy> *Node = Clone->CloneOf;
1173 assert(Node);
1174 // InAllocTypes should have been computed for the original node's callee
1175 // edges.
1176 assert(InAllocTypes.size() == Node->CalleeEdges.size());
1177 // First create a map of the clone callee edge callees to the edge alloc type.
1179 EdgeCalleeMap;
1180 for (const auto &E : Clone->CalleeEdges) {
1181 assert(!EdgeCalleeMap.contains(E->Callee));
1182 EdgeCalleeMap[E->Callee] = E->AllocTypes;
1183 }
1184 // Next, walk the original node's callees, and look for the corresponding
1185 // clone edge to that callee.
1186 for (unsigned I = 0; I < Node->CalleeEdges.size(); I++) {
1187 auto Iter = EdgeCalleeMap.find(Node->CalleeEdges[I]->Callee);
1188 // Not found is ok, we will simply add an edge if we use this clone.
1189 if (Iter == EdgeCalleeMap.end())
1190 continue;
1191 // Can share if one of the edges is None type - don't
1192 // care about the type along that edge as it doesn't
1193 // exist for those context ids.
1194 if (InAllocTypes[I] == (uint8_t)AllocationType::None ||
1195 Iter->second == (uint8_t)AllocationType::None)
1196 continue;
1197 if (allocTypeToUse(Iter->second) != allocTypeToUse(InAllocTypes[I]))
1198 return false;
1199 }
1200 return true;
1201}
1202
1203} // end anonymous namespace
1204
1205template <typename DerivedCCG, typename FuncTy, typename CallTy>
1206typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode *
1207CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::getNodeForInst(
1208 const CallInfo &C) {
1209 ContextNode *Node = getNodeForAlloc(C);
1210 if (Node)
1211 return Node;
1212
1213 return NonAllocationCallToContextNodeMap.lookup(C);
1214}
1215
1216template <typename DerivedCCG, typename FuncTy, typename CallTy>
1217typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode *
1218CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::getNodeForAlloc(
1219 const CallInfo &C) {
1220 return AllocationCallToContextNodeMap.lookup(C);
1221}
1222
1223template <typename DerivedCCG, typename FuncTy, typename CallTy>
1224typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode *
1225CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::getNodeForStackId(
1226 uint64_t StackId) {
1227 auto StackEntryNode = StackEntryIdToContextNodeMap.find(StackId);
1228 if (StackEntryNode != StackEntryIdToContextNodeMap.end())
1229 return StackEntryNode->second;
1230 return nullptr;
1231}
1232
1233template <typename DerivedCCG, typename FuncTy, typename CallTy>
1234void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode::
1235 addOrUpdateCallerEdge(ContextNode *Caller, AllocationType AllocType,
1236 unsigned int ContextId) {
1237 for (auto &Edge : CallerEdges) {
1238 if (Edge->Caller == Caller) {
1239 Edge->AllocTypes |= (uint8_t)AllocType;
1240 Edge->getContextIds().insert(ContextId);
1241 return;
1242 }
1243 }
1244 std::shared_ptr<ContextEdge> Edge = std::make_shared<ContextEdge>(
1245 this, Caller, (uint8_t)AllocType, DenseSet<uint32_t>({ContextId}));
1246 CallerEdges.push_back(Edge);
1247 Caller->CalleeEdges.push_back(Edge);
1248}
1249
1250template <typename DerivedCCG, typename FuncTy, typename CallTy>
1251void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::removeEdgeFromGraph(
1252 ContextEdge *Edge, EdgeIter *EI, bool CalleeIter) {
1253 assert(!EI || (*EI)->get() == Edge);
1254 assert(!Edge->isRemoved());
1255 // Save the Caller and Callee pointers so we can erase Edge from their edge
1256 // lists after clearing Edge below. We do the clearing first in case it is
1257 // destructed after removing from the edge lists (if those were the last
1258 // shared_ptr references to Edge).
1259 auto *Callee = Edge->Callee;
1260 auto *Caller = Edge->Caller;
1261
1262 // Make sure the edge fields are cleared out so we can properly detect
1263 // removed edges if Edge is not destructed because there is still a shared_ptr
1264 // reference.
1265 Edge->clear();
1266
1267#ifndef NDEBUG
1268 auto CalleeCallerCount = Callee->CallerEdges.size();
1269 auto CallerCalleeCount = Caller->CalleeEdges.size();
1270#endif
1271 if (!EI) {
1272 Callee->eraseCallerEdge(Edge);
1273 Caller->eraseCalleeEdge(Edge);
1274 } else if (CalleeIter) {
1275 Callee->eraseCallerEdge(Edge);
1276 *EI = Caller->CalleeEdges.erase(*EI);
1277 } else {
1278 Caller->eraseCalleeEdge(Edge);
1279 *EI = Callee->CallerEdges.erase(*EI);
1280 }
1281 assert(Callee->CallerEdges.size() < CalleeCallerCount);
1282 assert(Caller->CalleeEdges.size() < CallerCalleeCount);
1283}
1284
1285template <typename DerivedCCG, typename FuncTy, typename CallTy>
1286void CallsiteContextGraph<
1287 DerivedCCG, FuncTy, CallTy>::removeNoneTypeCalleeEdges(ContextNode *Node) {
1288 for (auto EI = Node->CalleeEdges.begin(); EI != Node->CalleeEdges.end();) {
1289 auto Edge = *EI;
1290 if (Edge->AllocTypes == (uint8_t)AllocationType::None) {
1291 assert(Edge->ContextIds.empty());
1292 removeEdgeFromGraph(Edge.get(), &EI, /*CalleeIter=*/true);
1293 } else
1294 ++EI;
1295 }
1296}
1297
1298template <typename DerivedCCG, typename FuncTy, typename CallTy>
1299void CallsiteContextGraph<
1300 DerivedCCG, FuncTy, CallTy>::removeNoneTypeCallerEdges(ContextNode *Node) {
1301 for (auto EI = Node->CallerEdges.begin(); EI != Node->CallerEdges.end();) {
1302 auto Edge = *EI;
1303 if (Edge->AllocTypes == (uint8_t)AllocationType::None) {
1304 assert(Edge->ContextIds.empty());
1305 Edge->Caller->eraseCalleeEdge(Edge.get());
1306 EI = Node->CallerEdges.erase(EI);
1307 } else
1308 ++EI;
1309 }
1310}
1311
1312template <typename DerivedCCG, typename FuncTy, typename CallTy>
1313typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextEdge *
1314CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode::
1315 findEdgeFromCallee(const ContextNode *Callee) {
1316 for (const auto &Edge : CalleeEdges)
1317 if (Edge->Callee == Callee)
1318 return Edge.get();
1319 return nullptr;
1320}
1321
1322template <typename DerivedCCG, typename FuncTy, typename CallTy>
1323typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextEdge *
1324CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode::
1325 findEdgeFromCaller(const ContextNode *Caller) {
1326 for (const auto &Edge : CallerEdges)
1327 if (Edge->Caller == Caller)
1328 return Edge.get();
1329 return nullptr;
1330}
1331
1332template <typename DerivedCCG, typename FuncTy, typename CallTy>
1333void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode::
1334 eraseCalleeEdge(const ContextEdge *Edge) {
1335 auto EI = llvm::find_if(
1336 CalleeEdges, [Edge](const std::shared_ptr<ContextEdge> &CalleeEdge) {
1337 return CalleeEdge.get() == Edge;
1338 });
1339 assert(EI != CalleeEdges.end());
1340 CalleeEdges.erase(EI);
1341}
1342
1343template <typename DerivedCCG, typename FuncTy, typename CallTy>
1344void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode::
1345 eraseCallerEdge(const ContextEdge *Edge) {
1346 auto EI = llvm::find_if(
1347 CallerEdges, [Edge](const std::shared_ptr<ContextEdge> &CallerEdge) {
1348 return CallerEdge.get() == Edge;
1349 });
1350 assert(EI != CallerEdges.end());
1351 CallerEdges.erase(EI);
1352}
1353
1354template <typename DerivedCCG, typename FuncTy, typename CallTy>
1355uint8_t CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::computeAllocType(
1356 DenseSet<uint32_t> &ContextIds) const {
1357 uint8_t BothTypes =
1358 (uint8_t)AllocationType::Cold | (uint8_t)AllocationType::NotCold;
1359 uint8_t AllocType = (uint8_t)AllocationType::None;
1360 for (auto Id : ContextIds) {
1361 AllocType |= (uint8_t)ContextIdToAllocationType.at(Id);
1362 // Bail early if alloc type reached both, no further refinement.
1363 if (AllocType == BothTypes)
1364 return AllocType;
1365 }
1366 return AllocType;
1367}
1368
1369template <typename DerivedCCG, typename FuncTy, typename CallTy>
1370uint8_t
1371CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::intersectAllocTypesImpl(
1372 const DenseSet<uint32_t> &Node1Ids,
1373 const DenseSet<uint32_t> &Node2Ids) const {
1374 uint8_t BothTypes =
1375 (uint8_t)AllocationType::Cold | (uint8_t)AllocationType::NotCold;
1376 uint8_t AllocType = (uint8_t)AllocationType::None;
1377 for (auto Id : Node1Ids) {
1378 if (!Node2Ids.count(Id))
1379 continue;
1380 AllocType |= (uint8_t)ContextIdToAllocationType.at(Id);
1381 // Bail early if alloc type reached both, no further refinement.
1382 if (AllocType == BothTypes)
1383 return AllocType;
1384 }
1385 return AllocType;
1386}
1387
1388template <typename DerivedCCG, typename FuncTy, typename CallTy>
1389uint8_t CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::intersectAllocTypes(
1390 const DenseSet<uint32_t> &Node1Ids,
1391 const DenseSet<uint32_t> &Node2Ids) const {
1392 if (Node1Ids.size() < Node2Ids.size())
1393 return intersectAllocTypesImpl(Node1Ids, Node2Ids);
1394 else
1395 return intersectAllocTypesImpl(Node2Ids, Node1Ids);
1396}
1397
1398template <typename DerivedCCG, typename FuncTy, typename CallTy>
1399typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode *
1400CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::addAllocNode(
1401 CallInfo Call, const FuncTy *F) {
1402 assert(!getNodeForAlloc(Call));
1403 ContextNode *AllocNode = createNewNode(/*IsAllocation=*/true, F, Call);
1404 AllocationCallToContextNodeMap[Call] = AllocNode;
1405 // Use LastContextId as a uniq id for MIB allocation nodes.
1406 AllocNode->OrigStackOrAllocId = LastContextId;
1407 // Alloc type should be updated as we add in the MIBs. We should assert
1408 // afterwards that it is not still None.
1409 AllocNode->AllocTypes = (uint8_t)AllocationType::None;
1410
1411 return AllocNode;
1412}
1413
1414static std::string getAllocTypeString(uint8_t AllocTypes) {
1415 if (!AllocTypes)
1416 return "None";
1417 std::string Str;
1418 if (AllocTypes & (uint8_t)AllocationType::NotCold)
1419 Str += "NotCold";
1420 if (AllocTypes & (uint8_t)AllocationType::Cold)
1421 Str += "Cold";
1422 return Str;
1423}
1424
1425template <typename DerivedCCG, typename FuncTy, typename CallTy>
1426template <class NodeT, class IteratorT>
1427void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::addStackNodesForMIB(
1428 ContextNode *AllocNode, CallStack<NodeT, IteratorT> &StackContext,
1429 CallStack<NodeT, IteratorT> &CallsiteContext, AllocationType AllocType,
1430 ArrayRef<ContextTotalSize> ContextSizeInfo,
1431 std::map<uint64_t, uint32_t> &TotalSizeToContextIdTopNCold) {
1432 // Treating the hot alloc type as NotCold before the disambiguation for "hot"
1433 // is done.
1434 if (AllocType == AllocationType::Hot)
1435 AllocType = AllocationType::NotCold;
1436
1437 ContextIdToAllocationType[++LastContextId] = AllocType;
1438
1439 bool IsImportant = false;
1440 if (!ContextSizeInfo.empty()) {
1441 auto &Entry = ContextIdToContextSizeInfos[LastContextId];
1442 // If this is a cold allocation, and we are collecting non-zero largest
1443 // contexts, see if this is a candidate.
1444 if (AllocType == AllocationType::Cold && MemProfTopNImportant > 0) {
1445 uint64_t TotalCold = 0;
1446 for (auto &CSI : ContextSizeInfo)
1447 TotalCold += CSI.TotalSize;
1448 // Record this context if either we haven't found the first top-n largest
1449 // yet, or if it is larger than the smallest already recorded.
1450 if (TotalSizeToContextIdTopNCold.size() < MemProfTopNImportant ||
1451 // Since TotalSizeToContextIdTopNCold is a std::map, it is implicitly
1452 // sorted in ascending size of its key which is the size.
1453 TotalCold > TotalSizeToContextIdTopNCold.begin()->first) {
1454 if (TotalSizeToContextIdTopNCold.size() == MemProfTopNImportant) {
1455 // Remove old one and its associated entries.
1456 auto IdToRemove = TotalSizeToContextIdTopNCold.begin()->second;
1457 TotalSizeToContextIdTopNCold.erase(
1458 TotalSizeToContextIdTopNCold.begin());
1459 assert(ImportantContextIdInfo.count(IdToRemove));
1460 ImportantContextIdInfo.erase(IdToRemove);
1461 }
1462 TotalSizeToContextIdTopNCold[TotalCold] = LastContextId;
1463 IsImportant = true;
1464 }
1465 }
1466 Entry.insert(Entry.begin(), ContextSizeInfo.begin(), ContextSizeInfo.end());
1467 }
1468
1469 // Update alloc type and context ids for this MIB.
1470 AllocNode->AllocTypes |= (uint8_t)AllocType;
1471
1472 // Now add or update nodes for each stack id in alloc's context.
1473 // Later when processing the stack ids on non-alloc callsites we will adjust
1474 // for any inlining in the context.
1475 ContextNode *PrevNode = AllocNode;
1476 // Look for recursion (direct recursion should have been collapsed by
1477 // module summary analysis, here we should just be detecting mutual
1478 // recursion). Mark these nodes so we don't try to clone.
1479 SmallSet<uint64_t, 8> StackIdSet;
1480 // Skip any on the allocation call (inlining).
1481 for (auto ContextIter = StackContext.beginAfterSharedPrefix(CallsiteContext);
1482 ContextIter != StackContext.end(); ++ContextIter) {
1483 auto StackId = getStackId(*ContextIter);
1484 if (IsImportant)
1485 ImportantContextIdInfo[LastContextId].StackIds.push_back(StackId);
1486 ContextNode *StackNode = getNodeForStackId(StackId);
1487 if (!StackNode) {
1488 StackNode = createNewNode(/*IsAllocation=*/false);
1489 StackEntryIdToContextNodeMap[StackId] = StackNode;
1490 StackNode->OrigStackOrAllocId = StackId;
1491 }
1492 // Marking a node recursive will prevent its cloning completely, even for
1493 // non-recursive contexts flowing through it.
1495 auto Ins = StackIdSet.insert(StackId);
1496 if (!Ins.second)
1497 StackNode->Recursive = true;
1498 }
1499 StackNode->AllocTypes |= (uint8_t)AllocType;
1500 PrevNode->addOrUpdateCallerEdge(StackNode, AllocType, LastContextId);
1501 PrevNode = StackNode;
1502 }
1503}
1504
1505template <typename DerivedCCG, typename FuncTy, typename CallTy>
1506DenseSet<uint32_t>
1507CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::duplicateContextIds(
1508 const DenseSet<uint32_t> &StackSequenceContextIds,
1509 DenseMap<uint32_t, DenseSet<uint32_t>> &OldToNewContextIds) {
1510 DenseSet<uint32_t> NewContextIds;
1511 for (auto OldId : StackSequenceContextIds) {
1512 NewContextIds.insert(++LastContextId);
1513 OldToNewContextIds[OldId].insert(LastContextId);
1514 assert(ContextIdToAllocationType.count(OldId));
1515 // The new context has the same allocation type and size info as original.
1516 ContextIdToAllocationType[LastContextId] = ContextIdToAllocationType[OldId];
1517 auto CSI = ContextIdToContextSizeInfos.find(OldId);
1518 if (CSI != ContextIdToContextSizeInfos.end())
1519 ContextIdToContextSizeInfos[LastContextId] = CSI->second;
1520 if (DotAllocContextIds.contains(OldId))
1521 DotAllocContextIds.insert(LastContextId);
1522 }
1523 return NewContextIds;
1524}
1525
1526template <typename DerivedCCG, typename FuncTy, typename CallTy>
1527void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::
1528 propagateDuplicateContextIds(
1529 const DenseMap<uint32_t, DenseSet<uint32_t>> &OldToNewContextIds) {
1530 // Build a set of duplicated context ids corresponding to the input id set.
1531 auto GetNewIds = [&OldToNewContextIds](const DenseSet<uint32_t> &ContextIds) {
1532 DenseSet<uint32_t> NewIds;
1533 for (auto Id : ContextIds)
1534 if (auto NewId = OldToNewContextIds.find(Id);
1535 NewId != OldToNewContextIds.end())
1536 NewIds.insert_range(NewId->second);
1537 return NewIds;
1538 };
1539
1540 // Recursively update context ids sets along caller edges.
1541 auto UpdateCallers = [&](ContextNode *Node,
1542 DenseSet<const ContextEdge *> &Visited,
1543 auto &&UpdateCallers) -> void {
1544 for (const auto &Edge : Node->CallerEdges) {
1545 auto Inserted = Visited.insert(Edge.get());
1546 if (!Inserted.second)
1547 continue;
1548 ContextNode *NextNode = Edge->Caller;
1549 DenseSet<uint32_t> NewIdsToAdd = GetNewIds(Edge->getContextIds());
1550 // Only need to recursively iterate to NextNode via this caller edge if
1551 // it resulted in any added ids to NextNode.
1552 if (!NewIdsToAdd.empty()) {
1553 Edge->getContextIds().insert_range(NewIdsToAdd);
1554 UpdateCallers(NextNode, Visited, UpdateCallers);
1555 }
1556 }
1557 };
1558
1559 DenseSet<const ContextEdge *> Visited;
1560 for (auto &Entry : AllocationCallToContextNodeMap) {
1561 auto *Node = Entry.second;
1562 UpdateCallers(Node, Visited, UpdateCallers);
1563 }
1564}
1565
1566template <typename DerivedCCG, typename FuncTy, typename CallTy>
1567void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::connectNewNode(
1568 ContextNode *NewNode, ContextNode *OrigNode, bool TowardsCallee,
1569 // This must be passed by value to make a copy since it will be adjusted
1570 // as ids are moved.
1571 DenseSet<uint32_t> RemainingContextIds) {
1572 auto &OrigEdges =
1573 TowardsCallee ? OrigNode->CalleeEdges : OrigNode->CallerEdges;
1574 DenseSet<uint32_t> RecursiveContextIds;
1575 DenseSet<uint32_t> AllCallerContextIds;
1577 // Identify which context ids are recursive which is needed to properly
1578 // update the RemainingContextIds set. The relevant recursive context ids
1579 // are those that are in multiple edges.
1580 for (auto &CE : OrigEdges) {
1581 AllCallerContextIds.reserve(CE->getContextIds().size());
1582 for (auto Id : CE->getContextIds())
1583 if (!AllCallerContextIds.insert(Id).second)
1584 RecursiveContextIds.insert(Id);
1585 }
1586 }
1587 // Increment iterator in loop so that we can remove edges as needed.
1588 for (auto EI = OrigEdges.begin(); EI != OrigEdges.end();) {
1589 auto Edge = *EI;
1590 DenseSet<uint32_t> NewEdgeContextIds;
1591 // Remove any matching context ids from Edge, return set that were found and
1592 // removed, these are the new edge's context ids.
1593 set_subtract(Edge->getContextIds(), RemainingContextIds, NewEdgeContextIds);
1594 // If no matching context ids for this edge, skip it.
1595 if (NewEdgeContextIds.empty()) {
1596 ++EI;
1597 continue;
1598 }
1599 // Update the remaining context ids set for the later edges. This is a
1600 // compile time optimization.
1601 if (RecursiveContextIds.empty()) {
1602 set_subtract(RemainingContextIds, NewEdgeContextIds);
1603 } else {
1604 // Keep the recursive ids in the remaining set as we expect to see those
1605 // on another edge. We can remove the non-recursive remaining ids that
1606 // were seen on this edge, however. We already have the set of remaining
1607 // ids that were on this edge (in NewEdgeContextIds). Figure out which are
1608 // non-recursive and only remove those. Note that despite the higher
1609 // overhead of updating the remaining context ids set when recursion
1610 // handling is enabled, it was found to be at worst performance neutral
1611 // and in one case a clear win.
1612 DenseSet<uint32_t> NonRecursiveRemainingCurEdgeIds =
1613 set_difference(NewEdgeContextIds, RecursiveContextIds);
1614 set_subtract(RemainingContextIds, NonRecursiveRemainingCurEdgeIds);
1615 }
1616 if (TowardsCallee) {
1617 uint8_t NewAllocType = computeAllocType(NewEdgeContextIds);
1618 auto NewEdge = std::make_shared<ContextEdge>(
1619 Edge->Callee, NewNode, NewAllocType, std::move(NewEdgeContextIds));
1620 NewNode->CalleeEdges.push_back(NewEdge);
1621 NewEdge->Callee->CallerEdges.push_back(NewEdge);
1622 } else {
1623 uint8_t NewAllocType = computeAllocType(NewEdgeContextIds);
1624 auto NewEdge = std::make_shared<ContextEdge>(
1625 NewNode, Edge->Caller, NewAllocType, std::move(NewEdgeContextIds));
1626 NewNode->CallerEdges.push_back(NewEdge);
1627 NewEdge->Caller->CalleeEdges.push_back(NewEdge);
1628 }
1629 // Remove old edge if context ids empty.
1630 if (Edge->getContextIds().empty()) {
1631 removeEdgeFromGraph(Edge.get(), &EI, TowardsCallee);
1632 continue;
1633 }
1634 ++EI;
1635 }
1636}
1637
1638template <typename DerivedCCG, typename FuncTy, typename CallTy>
1639static void checkEdge(
1640 const std::shared_ptr<ContextEdge<DerivedCCG, FuncTy, CallTy>> &Edge) {
1641 // Confirm that alloc type is not None and that we have at least one context
1642 // id.
1643 assert(Edge->AllocTypes != (uint8_t)AllocationType::None);
1644 assert(!Edge->ContextIds.empty());
1645}
1646
1647template <typename DerivedCCG, typename FuncTy, typename CallTy>
1648static void checkNode(const ContextNode<DerivedCCG, FuncTy, CallTy> *Node,
1649 bool CheckEdges = true) {
1650 if (Node->isRemoved())
1651 return;
1652#ifndef NDEBUG
1653 // Compute node's context ids once for use in asserts.
1654 auto NodeContextIds = Node->getContextIds();
1655#endif
1656 // Node's context ids should be the union of both its callee and caller edge
1657 // context ids.
1658 if (Node->CallerEdges.size()) {
1659 DenseSet<uint32_t> CallerEdgeContextIds(
1660 Node->CallerEdges.front()->ContextIds);
1661 for (const auto &Edge : llvm::drop_begin(Node->CallerEdges)) {
1662 if (CheckEdges)
1664 set_union(CallerEdgeContextIds, Edge->ContextIds);
1665 }
1666 // Node can have more context ids than callers if some contexts terminate at
1667 // node and some are longer. If we are allowing recursive callsites and
1668 // contexts this will be violated for incompletely cloned recursive cycles,
1669 // so skip the checking in that case.
1671 NodeContextIds == CallerEdgeContextIds ||
1672 set_is_subset(CallerEdgeContextIds, NodeContextIds));
1673 }
1674 if (Node->CalleeEdges.size()) {
1675 DenseSet<uint32_t> CalleeEdgeContextIds(
1676 Node->CalleeEdges.front()->ContextIds);
1677 for (const auto &Edge : llvm::drop_begin(Node->CalleeEdges)) {
1678 if (CheckEdges)
1680 set_union(CalleeEdgeContextIds, Edge->getContextIds());
1681 }
1682 // If we are allowing recursive callsites and contexts this will be violated
1683 // for incompletely cloned recursive cycles, so skip the checking in that
1684 // case.
1686 NodeContextIds == CalleeEdgeContextIds);
1687 }
1688 // FIXME: Since this checking is only invoked under an option, we should
1689 // change the error checking from using assert to something that will trigger
1690 // an error on a release build.
1691#ifndef NDEBUG
1692 // Make sure we don't end up with duplicate edges between the same caller and
1693 // callee.
1695 for (const auto &E : Node->CalleeEdges)
1696 NodeSet.insert(E->Callee);
1697 assert(NodeSet.size() == Node->CalleeEdges.size());
1698#endif
1699}
1700
1701template <typename DerivedCCG, typename FuncTy, typename CallTy>
1702void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::
1703 assignStackNodesPostOrder(ContextNode *Node,
1704 DenseSet<const ContextNode *> &Visited,
1705 DenseMap<uint64_t, std::vector<CallContextInfo>>
1706 &StackIdToMatchingCalls,
1707 DenseMap<CallInfo, CallInfo> &CallToMatchingCall,
1708 const DenseSet<uint32_t> &ImportantContextIds) {
1709 auto Inserted = Visited.insert(Node);
1710 if (!Inserted.second)
1711 return;
1712 // Post order traversal. Iterate over a copy since we may add nodes and
1713 // therefore new callers during the recursive call, invalidating any
1714 // iterator over the original edge vector. We don't need to process these
1715 // new nodes as they were already processed on creation.
1716 auto CallerEdges = Node->CallerEdges;
1717 for (auto &Edge : CallerEdges) {
1718 // Skip any that have been removed during the recursion.
1719 if (Edge->isRemoved()) {
1720 assert(!is_contained(Node->CallerEdges, Edge));
1721 continue;
1722 }
1723 assignStackNodesPostOrder(Edge->Caller, Visited, StackIdToMatchingCalls,
1724 CallToMatchingCall, ImportantContextIds);
1725 }
1726
1727 // If this node's stack id is in the map, update the graph to contain new
1728 // nodes representing any inlining at interior callsites. Note we move the
1729 // associated context ids over to the new nodes.
1730
1731 // Ignore this node if it is for an allocation or we didn't record any
1732 // stack id lists ending at it.
1733 if (Node->IsAllocation ||
1734 !StackIdToMatchingCalls.count(Node->OrigStackOrAllocId))
1735 return;
1736
1737 auto &Calls = StackIdToMatchingCalls[Node->OrigStackOrAllocId];
1738 // Handle the simple case first. A single call with a single stack id.
1739 // In this case there is no need to create any new context nodes, simply
1740 // assign the context node for stack id to this Call.
1741 if (Calls.size() == 1) {
1742 auto &[Call, Ids, Func, SavedContextIds] = Calls[0];
1743 if (Ids.size() == 1) {
1744 assert(SavedContextIds.empty());
1745 // It should be this Node
1746 assert(Node == getNodeForStackId(Ids[0]));
1747 if (Node->Recursive)
1748 return;
1749 Node->setCall(Call);
1750 NonAllocationCallToContextNodeMap[Call] = Node;
1751 NodeToCallingFunc[Node] = Func;
1752 recordStackNode(Ids, Node, Node->getContextIds(), ImportantContextIds);
1753 return;
1754 }
1755 }
1756
1757#ifndef NDEBUG
1758 // Find the node for the last stack id, which should be the same
1759 // across all calls recorded for this id, and is this node's id.
1760 uint64_t LastId = Node->OrigStackOrAllocId;
1761 ContextNode *LastNode = getNodeForStackId(LastId);
1762 // We should only have kept stack ids that had nodes.
1763 assert(LastNode);
1764 assert(LastNode == Node);
1765#else
1766 ContextNode *LastNode = Node;
1767#endif
1768
1769 // Compute the last node's context ids once, as it is shared by all calls in
1770 // this entry.
1771 DenseSet<uint32_t> LastNodeContextIds = LastNode->getContextIds();
1772
1773 [[maybe_unused]] bool PrevIterCreatedNode = false;
1774 bool CreatedNode = false;
1775 for (unsigned I = 0; I < Calls.size();
1776 I++, PrevIterCreatedNode = CreatedNode) {
1777 CreatedNode = false;
1778 auto &[Call, Ids, Func, SavedContextIds] = Calls[I];
1779 // Skip any for which we didn't assign any ids, these don't get a node in
1780 // the graph.
1781 if (SavedContextIds.empty()) {
1782 // If this call has a matching call (located in the same function and
1783 // having the same stack ids), simply add it to the context node created
1784 // for its matching call earlier. These can be treated the same through
1785 // cloning and get updated at the same time.
1786 if (!CallToMatchingCall.contains(Call))
1787 continue;
1788 auto MatchingCall = CallToMatchingCall[Call];
1789 if (!NonAllocationCallToContextNodeMap.contains(MatchingCall)) {
1790 // This should only happen if we had a prior iteration, and it didn't
1791 // create a node because of the below recomputation of context ids
1792 // finding none remaining and continuing early.
1793 assert(I > 0 && !PrevIterCreatedNode);
1794 continue;
1795 }
1796 NonAllocationCallToContextNodeMap[MatchingCall]->MatchingCalls.push_back(
1797 Call);
1798 continue;
1799 }
1800
1801 assert(LastId == Ids.back());
1802
1803 // Recompute the context ids for this stack id sequence (the
1804 // intersection of the context ids of the corresponding nodes).
1805 // Start with the ids we saved in the map for this call, which could be
1806 // duplicated context ids. We have to recompute as we might have overlap
1807 // overlap between the saved context ids for different last nodes, and
1808 // removed them already during the post order traversal.
1809 set_intersect(SavedContextIds, LastNodeContextIds);
1810 ContextNode *PrevNode = LastNode;
1811 bool Skip = false;
1812 // Iterate backwards through the stack Ids, starting after the last Id
1813 // in the list, which was handled once outside for all Calls.
1814 for (auto IdIter = Ids.rbegin() + 1; IdIter != Ids.rend(); IdIter++) {
1815 auto Id = *IdIter;
1816 ContextNode *CurNode = getNodeForStackId(Id);
1817 // We should only have kept stack ids that had nodes and weren't
1818 // recursive.
1819 assert(CurNode);
1820 assert(!CurNode->Recursive);
1821
1822 auto *Edge = CurNode->findEdgeFromCaller(PrevNode);
1823 if (!Edge) {
1824 Skip = true;
1825 break;
1826 }
1827 PrevNode = CurNode;
1828
1829 // Update the context ids, which is the intersection of the ids along
1830 // all edges in the sequence.
1831 set_intersect(SavedContextIds, Edge->getContextIds());
1832
1833 // If we now have no context ids for clone, skip this call.
1834 if (SavedContextIds.empty()) {
1835 Skip = true;
1836 break;
1837 }
1838 }
1839 if (Skip)
1840 continue;
1841
1842 // Create new context node.
1843 ContextNode *NewNode = createNewNode(/*IsAllocation=*/false, Func, Call);
1844 NonAllocationCallToContextNodeMap[Call] = NewNode;
1845 CreatedNode = true;
1846 NewNode->AllocTypes = computeAllocType(SavedContextIds);
1847
1848 ContextNode *FirstNode = getNodeForStackId(Ids[0]);
1849 assert(FirstNode);
1850
1851 // Connect to callees of innermost stack frame in inlined call chain.
1852 // This updates context ids for FirstNode's callee's to reflect those
1853 // moved to NewNode.
1854 connectNewNode(NewNode, FirstNode, /*TowardsCallee=*/true, SavedContextIds);
1855
1856 // Connect to callers of outermost stack frame in inlined call chain.
1857 // This updates context ids for FirstNode's caller's to reflect those
1858 // moved to NewNode.
1859 connectNewNode(NewNode, LastNode, /*TowardsCallee=*/false, SavedContextIds);
1860
1861 // Now we need to remove context ids from edges/nodes between First and
1862 // Last Node.
1863 PrevNode = nullptr;
1864 for (auto Id : Ids) {
1865 ContextNode *CurNode = getNodeForStackId(Id);
1866 // We should only have kept stack ids that had nodes.
1867 assert(CurNode);
1868
1869 // Remove the context ids moved to NewNode from CurNode, and the
1870 // edge from the prior node.
1871 if (PrevNode) {
1872 auto *PrevEdge = CurNode->findEdgeFromCallee(PrevNode);
1873 // If the sequence contained recursion, we might have already removed
1874 // some edges during the connectNewNode calls above.
1875 if (!PrevEdge) {
1876 PrevNode = CurNode;
1877 continue;
1878 }
1879 set_subtract(PrevEdge->getContextIds(), SavedContextIds);
1880 if (PrevEdge->getContextIds().empty())
1881 removeEdgeFromGraph(PrevEdge);
1882 }
1883 // Since we update the edges from leaf to tail, only look at the callee
1884 // edges. This isn't an alloc node, so if there are no callee edges, the
1885 // alloc type is None.
1886 CurNode->AllocTypes = CurNode->CalleeEdges.empty()
1887 ? (uint8_t)AllocationType::None
1888 : CurNode->computeAllocType();
1889 PrevNode = CurNode;
1890 }
1891
1892 recordStackNode(Ids, NewNode, SavedContextIds, ImportantContextIds);
1893
1894 if (VerifyNodes) {
1895 checkNode<DerivedCCG, FuncTy, CallTy>(NewNode, /*CheckEdges=*/true);
1896 for (auto Id : Ids) {
1897 ContextNode *CurNode = getNodeForStackId(Id);
1898 // We should only have kept stack ids that had nodes.
1899 assert(CurNode);
1900 checkNode<DerivedCCG, FuncTy, CallTy>(CurNode, /*CheckEdges=*/true);
1901 }
1902 }
1903 }
1904}
1905
1906template <typename DerivedCCG, typename FuncTy, typename CallTy>
1907void CallsiteContextGraph<DerivedCCG, FuncTy,
1908 CallTy>::fixupImportantContexts() {
1909 if (ImportantContextIdInfo.empty())
1910 return;
1911
1912 // Update statistics as we are done building this map at this point.
1913 NumImportantContextIds = ImportantContextIdInfo.size();
1914
1916 return;
1917
1918 if (ExportToDot)
1919 exportToDot("beforestackfixup");
1920
1921 // For each context we identified as important, walk through the saved context
1922 // stack ids in order from leaf upwards, and make sure all edges are correct.
1923 // These can be difficult to get right when updating the graph while mapping
1924 // nodes onto summary or IR, especially when there is recursion. In
1925 // particular, when we have created new nodes to reflect inlining, it is
1926 // sometimes impossible to know exactly how to update the edges in the face of
1927 // recursion, as we have lost the original ordering of the stack ids in the
1928 // contexts.
1929 // TODO: Consider only doing this if we detect the context has recursive
1930 // cycles.
1931 //
1932 // I.e. assume we have a context with stack ids like: {A B A C A D E}
1933 // and let's say A was inlined into B, C, and D. The original graph will have
1934 // multiple recursive cycles through A. When we match the original context
1935 // nodes onto the IR or summary, we will merge {A B} into one context node,
1936 // {A C} onto another, and {A D} onto another. Looking at the stack sequence
1937 // above, we should end up with a non-cyclic set of edges like:
1938 // {AB} <- {AC} <- {AD} <- E. However, because we normally have lost the
1939 // original ordering, we won't get the edges correct initially (it's
1940 // impossible without the original ordering). Here we do the fixup (add and
1941 // removing edges where necessary) for this context. In the
1942 // ImportantContextInfo struct in this case we should have a MaxLength = 2,
1943 // and map entries for {A B}, {A C}, {A D}, and {E}.
1944 for (auto &[CurContextId, Info] : ImportantContextIdInfo) {
1945 if (Info.StackIdsToNode.empty())
1946 continue;
1947 bool Changed = false;
1948 ContextNode *PrevNode = nullptr;
1949 ContextNode *CurNode = nullptr;
1950 DenseSet<const ContextEdge *> VisitedEdges;
1951 ArrayRef<uint64_t> AllStackIds(Info.StackIds);
1952 // Try to identify what callsite ContextNode maps to which slice of the
1953 // context's ordered stack ids.
1954 for (unsigned I = 0; I < AllStackIds.size(); I++, PrevNode = CurNode) {
1955 // We will do this greedily, trying up to MaxLength stack ids in a row, to
1956 // see if we recorded a context node for that sequence.
1957 auto Len = Info.MaxLength;
1958 auto LenToEnd = AllStackIds.size() - I;
1959 if (Len > LenToEnd)
1960 Len = LenToEnd;
1961 CurNode = nullptr;
1962 // Try to find a recorded context node starting with the longest length
1963 // recorded, and on down until we check for just a single stack node.
1964 for (; Len > 0; Len--) {
1965 // Get the slice of the original stack id sequence to check.
1966 auto CheckStackIds = AllStackIds.slice(I, Len);
1967 auto EntryIt = Info.StackIdsToNode.find(CheckStackIds);
1968 if (EntryIt == Info.StackIdsToNode.end())
1969 continue;
1970 CurNode = EntryIt->second;
1971 // Skip forward so we don't try to look for the ones we just matched.
1972 // We increment by Len - 1, because the outer for loop will increment I.
1973 I += Len - 1;
1974 break;
1975 }
1976 // Give up if we couldn't find a node. Since we need to clone from the
1977 // leaf allocation upwards, no sense in doing anymore fixup further up
1978 // the context if we couldn't match part of the original stack context
1979 // onto a callsite node.
1980 if (!CurNode)
1981 break;
1982 // No edges to fix up until we have a pair of nodes that should be
1983 // adjacent in the graph.
1984 if (!PrevNode)
1985 continue;
1986 // See if we already have a call edge from CurNode to PrevNode.
1987 auto *CurEdge = PrevNode->findEdgeFromCaller(CurNode);
1988 if (CurEdge) {
1989 // We already have an edge. Make sure it contains this context id.
1990 if (CurEdge->getContextIds().insert(CurContextId).second) {
1991 NumFixupEdgeIdsInserted++;
1992 Changed = true;
1993 }
1994 } else {
1995 // No edge exists - add one.
1996 NumFixupEdgesAdded++;
1997 DenseSet<uint32_t> ContextIds({CurContextId});
1998 auto AllocType = computeAllocType(ContextIds);
1999 auto NewEdge = std::make_shared<ContextEdge>(
2000 PrevNode, CurNode, AllocType, std::move(ContextIds));
2001 PrevNode->CallerEdges.push_back(NewEdge);
2002 CurNode->CalleeEdges.push_back(NewEdge);
2003 // Save the new edge for the below handling.
2004 CurEdge = NewEdge.get();
2005 Changed = true;
2006 }
2007 VisitedEdges.insert(CurEdge);
2008 // Now remove this context id from any other caller edges calling
2009 // PrevNode.
2010 for (auto &Edge : PrevNode->CallerEdges) {
2011 // Skip the edge updating/created above and edges we have already
2012 // visited (due to recursion).
2013 if (Edge.get() != CurEdge && !VisitedEdges.contains(Edge.get()))
2014 Edge->getContextIds().erase(CurContextId);
2015 }
2016 }
2017 if (Changed)
2018 NumFixedContexts++;
2019 }
2020}
2021
2022template <typename DerivedCCG, typename FuncTy, typename CallTy>
2023void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::updateStackNodes() {
2024 // Map of stack id to all calls with that as the last (outermost caller)
2025 // callsite id that has a context node (some might not due to pruning
2026 // performed during matching of the allocation profile contexts).
2027 // The CallContextInfo contains the Call and a list of its stack ids with
2028 // ContextNodes, the function containing Call, and the set of context ids
2029 // the analysis will eventually identify for use in any new node created
2030 // for that callsite.
2031 DenseMap<uint64_t, std::vector<CallContextInfo>> StackIdToMatchingCalls;
2032 for (auto &[Func, CallsWithMetadata] : FuncToCallsWithMetadata) {
2033 for (auto &Call : CallsWithMetadata) {
2034 // Ignore allocations, already handled.
2035 if (AllocationCallToContextNodeMap.count(Call))
2036 continue;
2037 auto StackIdsWithContextNodes =
2038 getStackIdsWithContextNodesForCall(Call.call());
2039 // If there were no nodes created for MIBs on allocs (maybe this was in
2040 // the unambiguous part of the MIB stack that was pruned), ignore.
2041 if (StackIdsWithContextNodes.empty())
2042 continue;
2043 // Otherwise, record this Call along with the list of ids for the last
2044 // (outermost caller) stack id with a node.
2045 StackIdToMatchingCalls[StackIdsWithContextNodes.back()].push_back(
2046 {Call.call(), StackIdsWithContextNodes, Func, {}});
2047 }
2048 }
2049
2050 // First make a pass through all stack ids that correspond to a call,
2051 // as identified in the above loop. Compute the context ids corresponding to
2052 // each of these calls when they correspond to multiple stack ids due to
2053 // due to inlining. Perform any duplication of context ids required when
2054 // there is more than one call with the same stack ids. Their (possibly newly
2055 // duplicated) context ids are saved in the StackIdToMatchingCalls map.
2056 DenseMap<uint32_t, DenseSet<uint32_t>> OldToNewContextIds;
2057 // Save a map from each call to any that are found to match it. I.e. located
2058 // in the same function and have the same (possibly pruned) stack ids. We use
2059 // this to avoid creating extra graph nodes as they can be treated the same.
2060 DenseMap<CallInfo, CallInfo> CallToMatchingCall;
2061 for (auto &It : StackIdToMatchingCalls) {
2062 auto &Calls = It.getSecond();
2063 // Skip single calls with a single stack id. These don't need a new node.
2064 if (Calls.size() == 1) {
2065 auto &Ids = Calls[0].StackIds;
2066 if (Ids.size() == 1)
2067 continue;
2068 }
2069 // In order to do the best and maximal matching of inlined calls to context
2070 // node sequences we will sort the vectors of stack ids in descending order
2071 // of length, and within each length, lexicographically by stack id. The
2072 // latter is so that we can specially handle calls that have identical stack
2073 // id sequences (either due to cloning or artificially because of the MIB
2074 // context pruning). Those with the same Ids are then sorted by function to
2075 // facilitate efficiently mapping them to the same context node.
2076 // Because the functions are pointers, to ensure a stable sort first assign
2077 // each function pointer to its first index in the Calls array, and then use
2078 // that to sort by.
2079 DenseMap<const FuncTy *, unsigned> FuncToIndex;
2080 for (const auto &[Idx, CallCtxInfo] : enumerate(Calls))
2081 FuncToIndex.insert({CallCtxInfo.Func, Idx});
2083 Calls,
2084 [&FuncToIndex](const CallContextInfo &A, const CallContextInfo &B) {
2085 return A.StackIds.size() > B.StackIds.size() ||
2086 (A.StackIds.size() == B.StackIds.size() &&
2087 (A.StackIds < B.StackIds ||
2088 (A.StackIds == B.StackIds &&
2089 FuncToIndex[A.Func] < FuncToIndex[B.Func])));
2090 });
2091
2092 // Find the node for the last stack id, which should be the same
2093 // across all calls recorded for this id, and is the id for this
2094 // entry in the StackIdToMatchingCalls map.
2095 uint64_t LastId = It.getFirst();
2096 ContextNode *LastNode = getNodeForStackId(LastId);
2097 // We should only have kept stack ids that had nodes.
2098 assert(LastNode);
2099
2100 if (LastNode->Recursive)
2101 continue;
2102
2103 // Initialize the context ids with the last node's. We will subsequently
2104 // refine the context ids by computing the intersection along all edges.
2105 DenseSet<uint32_t> LastNodeContextIds = LastNode->getContextIds();
2106 assert(!LastNodeContextIds.empty());
2107
2108#ifndef NDEBUG
2109 // Save the set of functions seen for a particular set of the same stack
2110 // ids. This is used to ensure that they have been correctly sorted to be
2111 // adjacent in the Calls list, since we rely on that to efficiently place
2112 // all such matching calls onto the same context node.
2113 DenseSet<const FuncTy *> MatchingIdsFuncSet;
2114#endif
2115
2116 for (unsigned I = 0; I < Calls.size(); I++) {
2117 auto &[Call, Ids, Func, SavedContextIds] = Calls[I];
2118 assert(SavedContextIds.empty());
2119 assert(LastId == Ids.back());
2120
2121#ifndef NDEBUG
2122 // If this call has a different set of ids than the last one, clear the
2123 // set used to ensure they are sorted properly.
2124 if (I > 0 && Ids != Calls[I - 1].StackIds)
2125 MatchingIdsFuncSet.clear();
2126#endif
2127
2128 // First compute the context ids for this stack id sequence (the
2129 // intersection of the context ids of the corresponding nodes).
2130 // Start with the remaining saved ids for the last node.
2131 assert(!LastNodeContextIds.empty());
2132 DenseSet<uint32_t> StackSequenceContextIds = LastNodeContextIds;
2133
2134 ContextNode *PrevNode = LastNode;
2135 ContextNode *CurNode = LastNode;
2136 bool Skip = false;
2137
2138 // Iterate backwards through the stack Ids, starting after the last Id
2139 // in the list, which was handled once outside for all Calls.
2140 for (auto IdIter = Ids.rbegin() + 1; IdIter != Ids.rend(); IdIter++) {
2141 auto Id = *IdIter;
2142 CurNode = getNodeForStackId(Id);
2143 // We should only have kept stack ids that had nodes.
2144 assert(CurNode);
2145
2146 if (CurNode->Recursive) {
2147 Skip = true;
2148 break;
2149 }
2150
2151 auto *Edge = CurNode->findEdgeFromCaller(PrevNode);
2152 // If there is no edge then the nodes belong to different MIB contexts,
2153 // and we should skip this inlined context sequence. For example, this
2154 // particular inlined context may include stack ids A->B, and we may
2155 // indeed have nodes for both A and B, but it is possible that they were
2156 // never profiled in sequence in a single MIB for any allocation (i.e.
2157 // we might have profiled an allocation that involves the callsite A,
2158 // but through a different one of its callee callsites, and we might
2159 // have profiled an allocation that involves callsite B, but reached
2160 // from a different caller callsite).
2161 if (!Edge) {
2162 Skip = true;
2163 break;
2164 }
2165 PrevNode = CurNode;
2166
2167 // Update the context ids, which is the intersection of the ids along
2168 // all edges in the sequence.
2169 set_intersect(StackSequenceContextIds, Edge->getContextIds());
2170
2171 // If we now have no context ids for clone, skip this call.
2172 if (StackSequenceContextIds.empty()) {
2173 Skip = true;
2174 break;
2175 }
2176 }
2177 if (Skip)
2178 continue;
2179
2180 // If some of this call's stack ids did not have corresponding nodes (due
2181 // to pruning), don't include any context ids for contexts that extend
2182 // beyond these nodes. Otherwise we would be matching part of unrelated /
2183 // not fully matching stack contexts. To do this, subtract any context ids
2184 // found in caller nodes of the last node found above.
2185 if (Ids.back() != getLastStackId(Call)) {
2186 for (const auto &PE : LastNode->CallerEdges) {
2187 set_subtract(StackSequenceContextIds, PE->getContextIds());
2188 if (StackSequenceContextIds.empty())
2189 break;
2190 }
2191 // If we now have no context ids for clone, skip this call.
2192 if (StackSequenceContextIds.empty())
2193 continue;
2194 }
2195
2196#ifndef NDEBUG
2197 // If the prior call had the same stack ids this set would not be empty.
2198 // Check if we already have a call that "matches" because it is located
2199 // in the same function. If the Calls list was sorted properly we should
2200 // not encounter this situation as all such entries should be adjacent
2201 // and processed in bulk further below.
2202 assert(!MatchingIdsFuncSet.contains(Func));
2203
2204 MatchingIdsFuncSet.insert(Func);
2205#endif
2206
2207 // Check if the next set of stack ids is the same (since the Calls vector
2208 // of tuples is sorted by the stack ids we can just look at the next one).
2209 // If so, save them in the CallToMatchingCall map so that they get
2210 // assigned to the same context node, and skip them.
2211 bool DuplicateContextIds = false;
2212 for (unsigned J = I + 1; J < Calls.size(); J++) {
2213 auto &CallCtxInfo = Calls[J];
2214 auto &NextIds = CallCtxInfo.StackIds;
2215 if (NextIds != Ids)
2216 break;
2217 auto *NextFunc = CallCtxInfo.Func;
2218 if (NextFunc != Func) {
2219 // We have another Call with the same ids but that cannot share this
2220 // node, must duplicate ids for it.
2221 DuplicateContextIds = true;
2222 break;
2223 }
2224 auto &NextCall = CallCtxInfo.Call;
2225 CallToMatchingCall[NextCall] = Call;
2226 // Update I so that it gets incremented correctly to skip this call.
2227 I = J;
2228 }
2229
2230 // If we don't have duplicate context ids, then we can assign all the
2231 // context ids computed for the original node sequence to this call.
2232 // If there are duplicate calls with the same stack ids then we synthesize
2233 // new context ids that are duplicates of the originals. These are
2234 // assigned to SavedContextIds, which is a reference into the map entry
2235 // for this call, allowing us to access these ids later on.
2236 OldToNewContextIds.reserve(OldToNewContextIds.size() +
2237 StackSequenceContextIds.size());
2238 SavedContextIds =
2239 DuplicateContextIds
2240 ? duplicateContextIds(StackSequenceContextIds, OldToNewContextIds)
2241 : StackSequenceContextIds;
2242 assert(!SavedContextIds.empty());
2243
2244 if (!DuplicateContextIds) {
2245 // Update saved last node's context ids to remove those that are
2246 // assigned to other calls, so that it is ready for the next call at
2247 // this stack id.
2248 set_subtract(LastNodeContextIds, StackSequenceContextIds);
2249 if (LastNodeContextIds.empty())
2250 break;
2251 }
2252 }
2253 }
2254
2255 // Propagate the duplicate context ids over the graph.
2256 propagateDuplicateContextIds(OldToNewContextIds);
2257
2258 if (VerifyCCG)
2259 check();
2260
2261 // Now perform a post-order traversal over the graph, starting with the
2262 // allocation nodes, essentially processing nodes from callers to callees.
2263 // For any that contains an id in the map, update the graph to contain new
2264 // nodes representing any inlining at interior callsites. Note we move the
2265 // associated context ids over to the new nodes.
2266 DenseSet<const ContextNode *> Visited;
2267 DenseSet<uint32_t> ImportantContextIds(llvm::from_range,
2268 ImportantContextIdInfo.keys());
2269 for (auto &Entry : AllocationCallToContextNodeMap)
2270 assignStackNodesPostOrder(Entry.second, Visited, StackIdToMatchingCalls,
2271 CallToMatchingCall, ImportantContextIds);
2272
2273 fixupImportantContexts();
2274
2275 if (VerifyCCG)
2276 check();
2277}
2278
2279uint64_t ModuleCallsiteContextGraph::getLastStackId(Instruction *Call) {
2280 CallStack<MDNode, MDNode::op_iterator> CallsiteContext(
2281 Call->getMetadata(LLVMContext::MD_callsite));
2282 return CallsiteContext.back();
2283}
2284
2285uint64_t IndexCallsiteContextGraph::getLastStackId(IndexCall &Call) {
2287 CallStack<CallsiteInfo, SmallVector<unsigned>::const_iterator>
2288 CallsiteContext(dyn_cast_if_present<CallsiteInfo *>(Call));
2289 // Need to convert index into stack id.
2290 return Index.getStackIdAtIndex(CallsiteContext.back());
2291}
2292
2293static const std::string MemProfCloneSuffix = ".memprof.";
2294
2295static std::string getMemProfFuncName(Twine Base, unsigned CloneNo) {
2296 // We use CloneNo == 0 to refer to the original version, which doesn't get
2297 // renamed with a suffix.
2298 if (!CloneNo)
2299 return Base.str();
2300 return (Base + MemProfCloneSuffix + Twine(CloneNo)).str();
2301}
2302
2303static bool isMemProfClone(const Function &F) {
2304 return F.getName().contains(MemProfCloneSuffix);
2305}
2306
2307// Return the clone number of the given function by extracting it from the
2308// memprof suffix. Assumes the caller has already confirmed it is a memprof
2309// clone.
2310static unsigned getMemProfCloneNum(const Function &F) {
2312 auto Pos = F.getName().find_last_of('.');
2313 assert(Pos > 0);
2314 unsigned CloneNo;
2315 bool Err = F.getName().drop_front(Pos + 1).getAsInteger(10, CloneNo);
2316 assert(!Err);
2317 (void)Err;
2318 return CloneNo;
2319}
2320
2321std::string ModuleCallsiteContextGraph::getLabel(const Function *Func,
2322 const Instruction *Call,
2323 unsigned CloneNo) const {
2324 return (Twine(Call->getFunction()->getName()) + " -> " +
2325 cast<CallBase>(Call)->getCalledFunction()->getName())
2326 .str();
2327}
2328
2329std::string IndexCallsiteContextGraph::getLabel(const FunctionSummary *Func,
2330 const IndexCall &Call,
2331 unsigned CloneNo) const {
2332 auto VI = FSToVIMap.find(Func);
2333 assert(VI != FSToVIMap.end());
2334 std::string CallerName = getMemProfFuncName(VI->second.name(), CloneNo);
2336 return CallerName + " -> alloc";
2337 else {
2338 auto *Callsite = dyn_cast_if_present<CallsiteInfo *>(Call);
2339 return CallerName + " -> " +
2340 getMemProfFuncName(Callsite->Callee.name(),
2341 Callsite->Clones[CloneNo]);
2342 }
2343}
2344
2345std::vector<uint64_t>
2346ModuleCallsiteContextGraph::getStackIdsWithContextNodesForCall(
2347 Instruction *Call) {
2348 CallStack<MDNode, MDNode::op_iterator> CallsiteContext(
2349 Call->getMetadata(LLVMContext::MD_callsite));
2350 return getStackIdsWithContextNodes<MDNode, MDNode::op_iterator>(
2351 CallsiteContext);
2352}
2353
2354std::vector<uint64_t>
2355IndexCallsiteContextGraph::getStackIdsWithContextNodesForCall(IndexCall &Call) {
2357 CallStack<CallsiteInfo, SmallVector<unsigned>::const_iterator>
2358 CallsiteContext(dyn_cast_if_present<CallsiteInfo *>(Call));
2359 return getStackIdsWithContextNodes<CallsiteInfo,
2360 SmallVector<unsigned>::const_iterator>(
2361 CallsiteContext);
2362}
2363
2364template <typename DerivedCCG, typename FuncTy, typename CallTy>
2365template <class NodeT, class IteratorT>
2366std::vector<uint64_t>
2367CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::getStackIdsWithContextNodes(
2368 CallStack<NodeT, IteratorT> &CallsiteContext) {
2369 std::vector<uint64_t> StackIds;
2370 for (auto IdOrIndex : CallsiteContext) {
2371 auto StackId = getStackId(IdOrIndex);
2372 ContextNode *Node = getNodeForStackId(StackId);
2373 if (!Node)
2374 break;
2375 StackIds.push_back(StackId);
2376 }
2377 return StackIds;
2378}
2379
2380ModuleCallsiteContextGraph::ModuleCallsiteContextGraph(
2381 Module &M,
2382 llvm::function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter)
2383 : Mod(M), OREGetter(OREGetter) {
2384 // Map for keeping track of the largest cold contexts up to the number given
2385 // by MemProfTopNImportant. Must be a std::map (not DenseMap) because keys
2386 // must be sorted.
2387 std::map<uint64_t, uint32_t> TotalSizeToContextIdTopNCold;
2388 for (auto &F : M) {
2389 std::vector<CallInfo> CallsWithMetadata;
2390 for (auto &BB : F) {
2391 for (auto &I : BB) {
2392 if (!isa<CallBase>(I))
2393 continue;
2394 if (auto *MemProfMD = I.getMetadata(LLVMContext::MD_memprof)) {
2395 CallsWithMetadata.push_back(&I);
2396 auto *AllocNode = addAllocNode(&I, &F);
2397 auto *CallsiteMD = I.getMetadata(LLVMContext::MD_callsite);
2398 assert(CallsiteMD);
2399 CallStack<MDNode, MDNode::op_iterator> CallsiteContext(CallsiteMD);
2400 // Add all of the MIBs and their stack nodes.
2401 for (auto &MDOp : MemProfMD->operands()) {
2402 auto *MIBMD = cast<const MDNode>(MDOp);
2403 std::vector<ContextTotalSize> ContextSizeInfo;
2404 // Collect the context size information if it exists.
2405 if (MIBMD->getNumOperands() > 2) {
2406 for (unsigned I = 2; I < MIBMD->getNumOperands(); I++) {
2407 MDNode *ContextSizePair =
2408 dyn_cast<MDNode>(MIBMD->getOperand(I));
2409 assert(ContextSizePair->getNumOperands() == 2);
2411 ContextSizePair->getOperand(0))
2412 ->getZExtValue();
2414 ContextSizePair->getOperand(1))
2415 ->getZExtValue();
2416 ContextSizeInfo.push_back({FullStackId, TotalSize});
2417 }
2418 }
2422 addStackNodesForMIB<MDNode, MDNode::op_iterator>(
2423 AllocNode, StackContext, CallsiteContext,
2424 getMIBAllocType(MIBMD), ContextSizeInfo,
2425 TotalSizeToContextIdTopNCold);
2426 }
2427 // If exporting the graph to dot and an allocation id of interest was
2428 // specified, record all the context ids for this allocation node.
2429 if (ExportToDot && AllocNode->OrigStackOrAllocId == AllocIdForDot)
2430 DotAllocContextIds = AllocNode->getContextIds();
2431 assert(AllocNode->AllocTypes != (uint8_t)AllocationType::None);
2432 // Memprof and callsite metadata on memory allocations no longer
2433 // needed.
2434 I.setMetadata(LLVMContext::MD_memprof, nullptr);
2435 I.setMetadata(LLVMContext::MD_callsite, nullptr);
2436 }
2437 // For callsite metadata, add to list for this function for later use.
2438 else if (I.getMetadata(LLVMContext::MD_callsite)) {
2439 CallsWithMetadata.push_back(&I);
2440 }
2441 }
2442 }
2443 if (!CallsWithMetadata.empty())
2444 FuncToCallsWithMetadata[&F] = CallsWithMetadata;
2445 }
2446
2447 if (DumpCCG) {
2448 dbgs() << "CCG before updating call stack chains:\n";
2449 dbgs() << *this;
2450 }
2451
2452 if (ExportToDot)
2453 exportToDot("prestackupdate");
2454
2455 updateStackNodes();
2456
2457 if (ExportToDot)
2458 exportToDot("poststackupdate");
2459
2460 handleCallsitesWithMultipleTargets();
2461
2462 markBackedges();
2463
2464 // Strip off remaining callsite metadata, no longer needed.
2465 for (auto &FuncEntry : FuncToCallsWithMetadata)
2466 for (auto &Call : FuncEntry.second)
2467 Call.call()->setMetadata(LLVMContext::MD_callsite, nullptr);
2468}
2469
2470// Finds the set of GUIDs for weak aliasees that are prevailing in different
2471// modules than any of their aliases. We need to handle these specially.
2473IndexCallsiteContextGraph::findAliaseeGUIDsPrevailingInDifferentModule() {
2474 DenseSet<GlobalValue::GUID> AliaseeGUIDs;
2475 for (auto &I : Index) {
2476 auto VI = Index.getValueInfo(I);
2477 for (auto &S : VI.getSummaryList()) {
2478 // We only care about aliases to functions.
2479 auto *AS = dyn_cast<AliasSummary>(S.get());
2480 if (!AS)
2481 continue;
2482 auto *AliaseeSummary = &AS->getAliasee();
2483 auto *AliaseeFS = dyn_cast<FunctionSummary>(AliaseeSummary);
2484 if (!AliaseeFS)
2485 continue;
2486 // Skip this summary if it is not for the prevailing symbol for this GUID.
2487 // The linker doesn't resolve local linkage values so don't check whether
2488 // those are prevailing.
2489 if (!GlobalValue::isLocalLinkage(S->linkage()) &&
2490 !isPrevailing(VI.getGUID(), S.get()))
2491 continue;
2492 // Prevailing aliasee could be in a different module only if it is weak.
2493 if (!GlobalValue::isWeakForLinker(AliaseeSummary->linkage()))
2494 continue;
2495 auto AliaseeGUID = AS->getAliaseeGUID();
2496 // If the aliasee copy in this module is not prevailing, record it.
2497 if (!isPrevailing(AliaseeGUID, AliaseeSummary))
2498 AliaseeGUIDs.insert(AliaseeGUID);
2499 }
2500 }
2501 AliaseesPrevailingInDiffModuleFromAlias += AliaseeGUIDs.size();
2502 return AliaseeGUIDs;
2503}
2504
2505IndexCallsiteContextGraph::IndexCallsiteContextGraph(
2506 ModuleSummaryIndex &Index,
2507 llvm::function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
2508 isPrevailing)
2509 : Index(Index), isPrevailing(isPrevailing) {
2510 // Since we use the aliasee summary info to create the necessary clones for
2511 // its aliases, conservatively skip recording the aliasee function's callsites
2512 // in the CCG for any that are prevailing in a different module than one of
2513 // its aliases. We could record the necessary information to do this in the
2514 // summary, but this case should not be common.
2515 DenseSet<GlobalValue::GUID> GUIDsToSkip =
2516 findAliaseeGUIDsPrevailingInDifferentModule();
2517 // Map for keeping track of the largest cold contexts up to the number given
2518 // by MemProfTopNImportant. Must be a std::map (not DenseMap) because keys
2519 // must be sorted.
2520 std::map<uint64_t, uint32_t> TotalSizeToContextIdTopNCold;
2521 // Sort by GUID for deterministic graph construction order.
2522 // TODO: This sort has a measurable cost on the thin link when memprof is
2523 // enabled. Investigate gating it behind an option that is only enabled for
2524 // tests that check internal state.
2525 for (const auto &I : Index.sortedGlobalValueSummariesRange()) {
2526 auto VI = Index.getValueInfo(I);
2527 if (GUIDsToSkip.contains(VI.getGUID()))
2528 continue;
2529 for (auto &S : VI.getSummaryList()) {
2530 // We should only add the prevailing nodes. Otherwise we may try to clone
2531 // in a weak copy that won't be linked (and may be different than the
2532 // prevailing version).
2533 // We only keep the memprof summary on the prevailing copy now when
2534 // building the combined index, as a space optimization, however don't
2535 // rely on this optimization. The linker doesn't resolve local linkage
2536 // values so don't check whether those are prevailing.
2537 if (!GlobalValue::isLocalLinkage(S->linkage()) &&
2538 !isPrevailing(VI.getGUID(), S.get()))
2539 continue;
2540 auto *FS = dyn_cast<FunctionSummary>(S.get());
2541 if (!FS)
2542 continue;
2543 std::vector<CallInfo> CallsWithMetadata;
2544 if (!FS->allocs().empty()) {
2545 for (auto &AN : FS->mutableAllocs()) {
2546 // This can happen because of recursion elimination handling that
2547 // currently exists in ModuleSummaryAnalysis. Skip these for now.
2548 // We still added them to the summary because we need to be able to
2549 // correlate properly in applyImport in the backends.
2550 if (AN.MIBs.empty())
2551 continue;
2552 IndexCall AllocCall(&AN);
2553 CallsWithMetadata.push_back(AllocCall);
2554 auto *AllocNode = addAllocNode(AllocCall, FS);
2555 // Pass an empty CallStack to the CallsiteContext (second)
2556 // parameter, since for ThinLTO we already collapsed out the inlined
2557 // stack ids on the allocation call during ModuleSummaryAnalysis.
2559 EmptyContext;
2560 unsigned I = 0;
2562 AN.ContextSizeInfos.size() == AN.MIBs.size());
2563 // Now add all of the MIBs and their stack nodes.
2564 for (auto &MIB : AN.MIBs) {
2566 StackContext(&MIB);
2567 std::vector<ContextTotalSize> ContextSizeInfo;
2568 if (!AN.ContextSizeInfos.empty()) {
2569 for (auto [FullStackId, TotalSize] : AN.ContextSizeInfos[I])
2570 ContextSizeInfo.push_back({FullStackId, TotalSize});
2571 }
2572 addStackNodesForMIB<MIBInfo, SmallVector<unsigned>::const_iterator>(
2573 AllocNode, StackContext, EmptyContext, MIB.AllocType,
2574 ContextSizeInfo, TotalSizeToContextIdTopNCold);
2575 I++;
2576 }
2577 // If exporting the graph to dot and an allocation id of interest was
2578 // specified, record all the context ids for this allocation node.
2579 if (ExportToDot && AllocNode->OrigStackOrAllocId == AllocIdForDot)
2580 DotAllocContextIds = AllocNode->getContextIds();
2581 assert(AllocNode->AllocTypes != (uint8_t)AllocationType::None);
2582 // Initialize version 0 on the summary alloc node to the current alloc
2583 // type, unless it has both types in which case make it default, so
2584 // that in the case where we aren't able to clone the original version
2585 // always ends up with the default allocation behavior.
2586 AN.Versions[0] = (uint8_t)allocTypeToUse(AllocNode->AllocTypes);
2587 }
2588 }
2589 // For callsite metadata, add to list for this function for later use.
2590 if (!FS->callsites().empty())
2591 for (auto &SN : FS->mutableCallsites()) {
2592 IndexCall StackNodeCall(&SN);
2593 CallsWithMetadata.push_back(StackNodeCall);
2594 }
2595
2596 if (!CallsWithMetadata.empty())
2597 FuncToCallsWithMetadata[FS] = CallsWithMetadata;
2598
2599 if (!FS->allocs().empty() || !FS->callsites().empty())
2600 FSToVIMap[FS] = VI;
2601 }
2602 }
2603
2604 if (DumpCCG) {
2605 dbgs() << "CCG before updating call stack chains:\n";
2606 dbgs() << *this;
2607 }
2608
2609 if (ExportToDot)
2610 exportToDot("prestackupdate");
2611
2612 updateStackNodes();
2613
2614 if (ExportToDot)
2615 exportToDot("poststackupdate");
2616
2617 handleCallsitesWithMultipleTargets();
2618
2619 markBackedges();
2620}
2621
2622template <typename DerivedCCG, typename FuncTy, typename CallTy>
2623void CallsiteContextGraph<DerivedCCG, FuncTy,
2624 CallTy>::handleCallsitesWithMultipleTargets() {
2625 // Look for and workaround callsites that call multiple functions.
2626 // This can happen for indirect calls, which needs better handling, and in
2627 // more rare cases (e.g. macro expansion).
2628 // TODO: To fix this for indirect calls we will want to perform speculative
2629 // devirtualization using either the normal PGO info with ICP, or using the
2630 // information in the profiled MemProf contexts. We can do this prior to
2631 // this transformation for regular LTO, and for ThinLTO we can simulate that
2632 // effect in the summary and perform the actual speculative devirtualization
2633 // while cloning in the ThinLTO backend.
2634
2635 // Keep track of the new nodes synthesized for discovered tail calls missing
2636 // from the profiled contexts.
2637 MapVector<CallInfo, ContextNode *> TailCallToContextNodeMap;
2638
2639 std::vector<std::pair<CallInfo, ContextNode *>> NewCallToNode;
2640 for (auto &Entry : NonAllocationCallToContextNodeMap) {
2641 auto *Node = Entry.second;
2642 assert(Node->Clones.empty());
2643 // Check all node callees and see if in the same function.
2644 // We need to check all of the calls recorded in this Node, because in some
2645 // cases we may have had multiple calls with the same debug info calling
2646 // different callees. This can happen, for example, when an object is
2647 // constructed in the paramter list - the destructor call of the object has
2648 // the same debug info (line/col) as the call the object was passed to.
2649 // Here we will prune any that don't match all callee nodes.
2650 std::vector<CallInfo> AllCalls;
2651 AllCalls.reserve(Node->MatchingCalls.size() + 1);
2652 AllCalls.push_back(Node->Call);
2653 llvm::append_range(AllCalls, Node->MatchingCalls);
2654
2655 // First see if we can partition the calls by callee function, creating new
2656 // nodes to host each set of calls calling the same callees. This is
2657 // necessary for support indirect calls with ThinLTO, for which we
2658 // synthesized CallsiteInfo records for each target. They will all have the
2659 // same callsite stack ids and would be sharing a context node at this
2660 // point. We need to perform separate cloning for each, which will be
2661 // applied along with speculative devirtualization in the ThinLTO backends
2662 // as needed. Note this does not currently support looking through tail
2663 // calls, it is unclear if we need that for indirect call targets.
2664 // First partition calls by callee func. Map indexed by func, value is
2665 // struct with list of matching calls, assigned node.
2666 if (partitionCallsByCallee(Node, AllCalls, NewCallToNode))
2667 continue;
2668
2669 auto It = AllCalls.begin();
2670 // Iterate through the calls until we find the first that matches.
2671 for (; It != AllCalls.end(); ++It) {
2672 auto ThisCall = *It;
2673 bool Match = true;
2674 for (auto EI = Node->CalleeEdges.begin(); EI != Node->CalleeEdges.end();
2675 ++EI) {
2676 auto Edge = *EI;
2677 if (!Edge->Callee->hasCall())
2678 continue;
2679 assert(NodeToCallingFunc.count(Edge->Callee));
2680 // Check if the called function matches that of the callee node.
2681 if (!calleesMatch(ThisCall.call(), EI, TailCallToContextNodeMap)) {
2682 Match = false;
2683 break;
2684 }
2685 }
2686 // Found a call that matches the callee nodes, we can quit now.
2687 if (Match) {
2688 // If the first match is not the primary call on the Node, update it
2689 // now. We will update the list of matching calls further below.
2690 if (Node->Call != ThisCall) {
2691 Node->setCall(ThisCall);
2692 // We need to update the NonAllocationCallToContextNodeMap, but don't
2693 // want to do this during iteration over that map, so save the calls
2694 // that need updated entries.
2695 NewCallToNode.push_back({ThisCall, Node});
2696 }
2697 break;
2698 }
2699 }
2700 // We will update this list below (or leave it cleared if there was no
2701 // match found above).
2702 Node->MatchingCalls.clear();
2703 // If we hit the end of the AllCalls vector, no call matching the callee
2704 // nodes was found, clear the call information in the node.
2705 if (It == AllCalls.end()) {
2706 RemovedEdgesWithMismatchedCallees++;
2707 // Work around by setting Node to have a null call, so it gets
2708 // skipped during cloning. Otherwise assignFunctions will assert
2709 // because its data structures are not designed to handle this case.
2710 Node->setCall(CallInfo());
2711 continue;
2712 }
2713 // Now add back any matching calls that call the same function as the
2714 // matching primary call on Node.
2715 for (++It; It != AllCalls.end(); ++It) {
2716 auto ThisCall = *It;
2717 if (!sameCallee(Node->Call.call(), ThisCall.call()))
2718 continue;
2719 Node->MatchingCalls.push_back(ThisCall);
2720 }
2721 }
2722
2723 // Remove all mismatched nodes identified in the above loop from the node map
2724 // (checking whether they have a null call which is set above). For a
2725 // MapVector like NonAllocationCallToContextNodeMap it is much more efficient
2726 // to do the removal via remove_if than by individually erasing entries above.
2727 // Also remove any entries if we updated the node's primary call above.
2728 NonAllocationCallToContextNodeMap.remove_if([](const auto &it) {
2729 return !it.second->hasCall() || it.second->Call != it.first;
2730 });
2731
2732 // Add entries for any new primary calls recorded above.
2733 for (auto &[Call, Node] : NewCallToNode)
2734 NonAllocationCallToContextNodeMap[Call] = Node;
2735
2736 // Add the new nodes after the above loop so that the iteration is not
2737 // invalidated.
2738 for (auto &[Call, Node] : TailCallToContextNodeMap)
2739 NonAllocationCallToContextNodeMap[Call] = Node;
2740}
2741
2742template <typename DerivedCCG, typename FuncTy, typename CallTy>
2743bool CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::partitionCallsByCallee(
2744 ContextNode *Node, ArrayRef<CallInfo> AllCalls,
2745 std::vector<std::pair<CallInfo, ContextNode *>> &NewCallToNode) {
2746 // Struct to keep track of all the calls having the same callee function,
2747 // and the node we eventually assign to them. Eventually we will record the
2748 // context node assigned to this group of calls.
2749 struct CallsWithSameCallee {
2750 std::vector<CallInfo> Calls;
2751 ContextNode *Node = nullptr;
2752 };
2753
2754 // First partition calls by callee function. Build map from each function
2755 // to the list of matching calls.
2757 for (auto ThisCall : AllCalls) {
2758 auto *F = getCalleeFunc(ThisCall.call());
2759 if (F)
2760 CalleeFuncToCallInfo[F].Calls.push_back(ThisCall);
2761 }
2762
2763 // Next, walk through all callee edges. For each callee node, get its
2764 // containing function and see if it was recorded in the above map (meaning we
2765 // have at least one matching call). Build another map from each callee node
2766 // with a matching call to the structure instance created above containing all
2767 // the calls.
2769 for (const auto &Edge : Node->CalleeEdges) {
2770 if (!Edge->Callee->hasCall())
2771 continue;
2772 const FuncTy *ProfiledCalleeFunc = NodeToCallingFunc[Edge->Callee];
2773 if (CalleeFuncToCallInfo.contains(ProfiledCalleeFunc))
2774 CalleeNodeToCallInfo[Edge->Callee] =
2775 &CalleeFuncToCallInfo[ProfiledCalleeFunc];
2776 }
2777
2778 // If there are entries in the second map, then there were no matching
2779 // calls/callees, nothing to do here. Return so we can go to the handling that
2780 // looks through tail calls.
2781 if (CalleeNodeToCallInfo.empty())
2782 return false;
2783
2784 // Walk through all callee edges again. Any and all callee edges that didn't
2785 // match any calls (callee not in the CalleeNodeToCallInfo map) are moved to a
2786 // new caller node (UnmatchedCalleesNode) which gets a null call so that it is
2787 // ignored during cloning. If it is in the map, then we use the node recorded
2788 // in that entry (creating it if needed), and move the callee edge to it.
2789 // The first callee will use the original node instead of creating a new one.
2790 // Note that any of the original calls on this node (in AllCalls) that didn't
2791 // have a callee function automatically get dropped from the node as part of
2792 // this process.
2793 ContextNode *UnmatchedCalleesNode = nullptr;
2794 // Track whether we already assigned original node to a callee.
2795 bool UsedOrigNode = false;
2796 assert(NodeToCallingFunc[Node]);
2797 // Iterate over a copy of Node's callee edges, since we may need to remove
2798 // edges in moveCalleeEdgeToNewCaller, and this simplifies the handling and
2799 // makes it less error-prone.
2800 auto CalleeEdges = Node->CalleeEdges;
2801 for (auto &Edge : CalleeEdges) {
2802 if (!Edge->Callee->hasCall())
2803 continue;
2804
2805 // Will be updated below to point to whatever (caller) node this callee edge
2806 // should be moved to.
2807 ContextNode *CallerNodeToUse = nullptr;
2808
2809 // Handle the case where there were no matching calls first. Move this
2810 // callee edge to the UnmatchedCalleesNode, creating it if needed.
2811 if (!CalleeNodeToCallInfo.contains(Edge->Callee)) {
2812 if (!UnmatchedCalleesNode)
2813 UnmatchedCalleesNode =
2814 createNewNode(/*IsAllocation=*/false, NodeToCallingFunc[Node]);
2815 CallerNodeToUse = UnmatchedCalleesNode;
2816 } else {
2817 // Look up the information recorded for this callee node, and use the
2818 // recorded caller node (creating it if needed).
2819 auto *Info = CalleeNodeToCallInfo[Edge->Callee];
2820 if (!Info->Node) {
2821 // If we haven't assigned any callees to the original node use it.
2822 if (!UsedOrigNode) {
2823 Info->Node = Node;
2824 // Clear the set of matching calls which will be updated below.
2825 Node->MatchingCalls.clear();
2826 UsedOrigNode = true;
2827 } else
2828 Info->Node =
2829 createNewNode(/*IsAllocation=*/false, NodeToCallingFunc[Node]);
2830 assert(!Info->Calls.empty());
2831 // The first call becomes the primary call for this caller node, and the
2832 // rest go in the matching calls list.
2833 Info->Node->setCall(Info->Calls.front());
2834 llvm::append_range(Info->Node->MatchingCalls,
2835 llvm::drop_begin(Info->Calls));
2836 // Save the primary call to node correspondence so that we can update
2837 // the NonAllocationCallToContextNodeMap, which is being iterated in the
2838 // caller of this function.
2839 NewCallToNode.push_back({Info->Node->Call, Info->Node});
2840 }
2841 CallerNodeToUse = Info->Node;
2842 }
2843
2844 // Don't need to move edge if we are using the original node;
2845 if (CallerNodeToUse == Node)
2846 continue;
2847
2848 moveCalleeEdgeToNewCaller(Edge, CallerNodeToUse);
2849 }
2850 // Now that we are done moving edges, clean up any caller edges that ended
2851 // up with no type or context ids. During moveCalleeEdgeToNewCaller all
2852 // caller edges from Node are replicated onto the new callers, and it
2853 // simplifies the handling to leave them until we have moved all
2854 // edges/context ids.
2855 for (auto &I : CalleeNodeToCallInfo)
2856 removeNoneTypeCallerEdges(I.second->Node);
2857 if (UnmatchedCalleesNode)
2858 removeNoneTypeCallerEdges(UnmatchedCalleesNode);
2859 removeNoneTypeCallerEdges(Node);
2860
2861 return true;
2862}
2863
2864uint64_t ModuleCallsiteContextGraph::getStackId(uint64_t IdOrIndex) const {
2865 // In the Module (IR) case this is already the Id.
2866 return IdOrIndex;
2867}
2868
2869uint64_t IndexCallsiteContextGraph::getStackId(uint64_t IdOrIndex) const {
2870 // In the Index case this is an index into the stack id list in the summary
2871 // index, convert it to an Id.
2872 return Index.getStackIdAtIndex(IdOrIndex);
2873}
2874
2875template <typename DerivedCCG, typename FuncTy, typename CallTy>
2876bool CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::calleesMatch(
2877 CallTy Call, EdgeIter &EI,
2878 MapVector<CallInfo, ContextNode *> &TailCallToContextNodeMap) {
2879 auto Edge = *EI;
2880 const FuncTy *ProfiledCalleeFunc = NodeToCallingFunc[Edge->Callee];
2881 const FuncTy *CallerFunc = NodeToCallingFunc[Edge->Caller];
2882 // Will be populated in order of callee to caller if we find a chain of tail
2883 // calls between the profiled caller and callee.
2884 std::vector<std::pair<CallTy, FuncTy *>> FoundCalleeChain;
2885 if (!calleeMatchesFunc(Call, ProfiledCalleeFunc, CallerFunc,
2886 FoundCalleeChain))
2887 return false;
2888
2889 // The usual case where the profiled callee matches that of the IR/summary.
2890 if (FoundCalleeChain.empty())
2891 return true;
2892
2893 auto AddEdge = [Edge, &EI](ContextNode *Caller, ContextNode *Callee) {
2894 auto *CurEdge = Callee->findEdgeFromCaller(Caller);
2895 // If there is already an edge between these nodes, simply update it and
2896 // return.
2897 if (CurEdge) {
2898 CurEdge->ContextIds.insert_range(Edge->ContextIds);
2899 CurEdge->AllocTypes |= Edge->AllocTypes;
2900 return;
2901 }
2902 // Otherwise, create a new edge and insert it into the caller and callee
2903 // lists.
2904 auto NewEdge = std::make_shared<ContextEdge>(
2905 Callee, Caller, Edge->AllocTypes, Edge->ContextIds);
2906 Callee->CallerEdges.push_back(NewEdge);
2907 if (Caller == Edge->Caller) {
2908 // If we are inserting the new edge into the current edge's caller, insert
2909 // the new edge before the current iterator position, and then increment
2910 // back to the current edge.
2911 EI = Caller->CalleeEdges.insert(EI, NewEdge);
2912 ++EI;
2913 assert(*EI == Edge &&
2914 "Iterator position not restored after insert and increment");
2915 } else
2916 Caller->CalleeEdges.push_back(NewEdge);
2917 };
2918
2919 // Create new nodes for each found callee and connect in between the profiled
2920 // caller and callee.
2921 auto *CurCalleeNode = Edge->Callee;
2922 for (auto &[NewCall, Func] : FoundCalleeChain) {
2923 ContextNode *NewNode = nullptr;
2924 // First check if we have already synthesized a node for this tail call.
2925 if (TailCallToContextNodeMap.count(NewCall)) {
2926 NewNode = TailCallToContextNodeMap[NewCall];
2927 NewNode->AllocTypes |= Edge->AllocTypes;
2928 } else {
2929 FuncToCallsWithMetadata[Func].push_back({NewCall});
2930 // Create Node and record node info.
2931 NewNode = createNewNode(/*IsAllocation=*/false, Func, NewCall);
2932 TailCallToContextNodeMap[NewCall] = NewNode;
2933 NewNode->AllocTypes = Edge->AllocTypes;
2934 }
2935
2936 // Hook up node to its callee node
2937 AddEdge(NewNode, CurCalleeNode);
2938
2939 CurCalleeNode = NewNode;
2940 }
2941
2942 // Hook up edge's original caller to new callee node.
2943 AddEdge(Edge->Caller, CurCalleeNode);
2944
2945#ifndef NDEBUG
2946 // Save this because Edge's fields get cleared below when removed.
2947 auto *Caller = Edge->Caller;
2948#endif
2949
2950 // Remove old edge
2951 removeEdgeFromGraph(Edge.get(), &EI, /*CalleeIter=*/true);
2952
2953 // To simplify the increment of EI in the caller, subtract one from EI.
2954 // In the final AddEdge call we would have either added a new callee edge,
2955 // to Edge->Caller, or found an existing one. Either way we are guaranteed
2956 // that there is at least one callee edge.
2957 assert(!Caller->CalleeEdges.empty());
2958 --EI;
2959
2960 return true;
2961}
2962
2963bool ModuleCallsiteContextGraph::findProfiledCalleeThroughTailCalls(
2964 const Function *ProfiledCallee, Value *CurCallee, unsigned Depth,
2965 std::vector<std::pair<Instruction *, Function *>> &FoundCalleeChain,
2966 bool &FoundMultipleCalleeChains) {
2967 // Stop recursive search if we have already explored the maximum specified
2968 // depth.
2970 return false;
2971
2972 auto SaveCallsiteInfo = [&](Instruction *Callsite, Function *F) {
2973 FoundCalleeChain.push_back({Callsite, F});
2974 };
2975
2976 auto *CalleeFunc = dyn_cast<Function>(CurCallee);
2977 if (!CalleeFunc) {
2978 auto *Alias = dyn_cast<GlobalAlias>(CurCallee);
2979 assert(Alias);
2980 CalleeFunc = dyn_cast<Function>(Alias->getAliasee());
2981 assert(CalleeFunc);
2982 }
2983
2984 // Look for tail calls in this function, and check if they either call the
2985 // profiled callee directly, or indirectly (via a recursive search).
2986 // Only succeed if there is a single unique tail call chain found between the
2987 // profiled caller and callee, otherwise we could perform incorrect cloning.
2988 bool FoundSingleCalleeChain = false;
2989 for (auto &BB : *CalleeFunc) {
2990 for (auto &I : BB) {
2991 auto *CB = dyn_cast<CallBase>(&I);
2992 if (!CB || !CB->isTailCall())
2993 continue;
2994 auto *CalledValue = CB->getCalledOperand();
2995 auto *CalledFunction = CB->getCalledFunction();
2996 if (CalledValue && !CalledFunction) {
2997 CalledValue = CalledValue->stripPointerCasts();
2998 // Stripping pointer casts can reveal a called function.
2999 CalledFunction = dyn_cast<Function>(CalledValue);
3000 }
3001 // Check if this is an alias to a function. If so, get the
3002 // called aliasee for the checks below.
3003 if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
3004 assert(!CalledFunction &&
3005 "Expected null called function in callsite for alias");
3006 CalledFunction = dyn_cast<Function>(GA->getAliaseeObject());
3007 }
3008 if (!CalledFunction)
3009 continue;
3010 if (CalledFunction == ProfiledCallee) {
3011 if (FoundSingleCalleeChain) {
3012 FoundMultipleCalleeChains = true;
3013 return false;
3014 }
3015 FoundSingleCalleeChain = true;
3016 FoundProfiledCalleeCount++;
3017 FoundProfiledCalleeDepth += Depth;
3018 if (Depth > FoundProfiledCalleeMaxDepth)
3019 FoundProfiledCalleeMaxDepth = Depth;
3020 SaveCallsiteInfo(&I, CalleeFunc);
3021 } else if (findProfiledCalleeThroughTailCalls(
3022 ProfiledCallee, CalledFunction, Depth + 1,
3023 FoundCalleeChain, FoundMultipleCalleeChains)) {
3024 // findProfiledCalleeThroughTailCalls should not have returned
3025 // true if FoundMultipleCalleeChains.
3026 assert(!FoundMultipleCalleeChains);
3027 if (FoundSingleCalleeChain) {
3028 FoundMultipleCalleeChains = true;
3029 return false;
3030 }
3031 FoundSingleCalleeChain = true;
3032 SaveCallsiteInfo(&I, CalleeFunc);
3033 } else if (FoundMultipleCalleeChains)
3034 return false;
3035 }
3036 }
3037
3038 return FoundSingleCalleeChain;
3039}
3040
3041const Function *ModuleCallsiteContextGraph::getCalleeFunc(Instruction *Call) {
3042 auto *CB = dyn_cast<CallBase>(Call);
3043 if (!CB->getCalledOperand() || CB->isIndirectCall())
3044 return nullptr;
3045 auto *CalleeVal = CB->getCalledOperand()->stripPointerCasts();
3046 auto *Alias = dyn_cast<GlobalAlias>(CalleeVal);
3047 if (Alias)
3048 return dyn_cast<Function>(Alias->getAliasee());
3049 return dyn_cast<Function>(CalleeVal);
3050}
3051
3052bool ModuleCallsiteContextGraph::calleeMatchesFunc(
3053 Instruction *Call, const Function *Func, const Function *CallerFunc,
3054 std::vector<std::pair<Instruction *, Function *>> &FoundCalleeChain) {
3055 auto *CB = dyn_cast<CallBase>(Call);
3056 if (!CB->getCalledOperand() || CB->isIndirectCall())
3057 return false;
3058 auto *CalleeVal = CB->getCalledOperand()->stripPointerCasts();
3059 auto *CalleeFunc = dyn_cast<Function>(CalleeVal);
3060 if (CalleeFunc == Func)
3061 return true;
3062 auto *Alias = dyn_cast<GlobalAlias>(CalleeVal);
3063 if (Alias && Alias->getAliasee() == Func)
3064 return true;
3065
3066 // Recursively search for the profiled callee through tail calls starting with
3067 // the actual Callee. The discovered tail call chain is saved in
3068 // FoundCalleeChain, and we will fixup the graph to include these callsites
3069 // after returning.
3070 // FIXME: We will currently redo the same recursive walk if we find the same
3071 // mismatched callee from another callsite. We can improve this with more
3072 // bookkeeping of the created chain of new nodes for each mismatch.
3073 unsigned Depth = 1;
3074 bool FoundMultipleCalleeChains = false;
3075 if (!findProfiledCalleeThroughTailCalls(Func, CalleeVal, Depth,
3076 FoundCalleeChain,
3077 FoundMultipleCalleeChains)) {
3078 LLVM_DEBUG(dbgs() << "Not found through unique tail call chain: "
3079 << Func->getName() << " from " << CallerFunc->getName()
3080 << " that actually called " << CalleeVal->getName()
3081 << (FoundMultipleCalleeChains
3082 ? " (found multiple possible chains)"
3083 : "")
3084 << "\n");
3085 if (FoundMultipleCalleeChains)
3086 FoundProfiledCalleeNonUniquelyCount++;
3087 return false;
3088 }
3089
3090 return true;
3091}
3092
3093bool ModuleCallsiteContextGraph::sameCallee(Instruction *Call1,
3094 Instruction *Call2) {
3095 auto *CB1 = cast<CallBase>(Call1);
3096 if (!CB1->getCalledOperand() || CB1->isIndirectCall())
3097 return false;
3098 auto *CalleeVal1 = CB1->getCalledOperand()->stripPointerCasts();
3099 auto *CalleeFunc1 = dyn_cast<Function>(CalleeVal1);
3100 auto *CB2 = cast<CallBase>(Call2);
3101 if (!CB2->getCalledOperand() || CB2->isIndirectCall())
3102 return false;
3103 auto *CalleeVal2 = CB2->getCalledOperand()->stripPointerCasts();
3104 auto *CalleeFunc2 = dyn_cast<Function>(CalleeVal2);
3105 return CalleeFunc1 == CalleeFunc2;
3106}
3107
3108bool IndexCallsiteContextGraph::findProfiledCalleeThroughTailCalls(
3109 ValueInfo ProfiledCallee, ValueInfo CurCallee, unsigned Depth,
3110 std::vector<std::pair<IndexCall, FunctionSummary *>> &FoundCalleeChain,
3111 bool &FoundMultipleCalleeChains) {
3112 // Stop recursive search if we have already explored the maximum specified
3113 // depth.
3115 return false;
3116
3117 auto CreateAndSaveCallsiteInfo = [&](ValueInfo Callee, FunctionSummary *FS) {
3118 // Make a CallsiteInfo for each discovered callee, if one hasn't already
3119 // been synthesized.
3120 if (!FunctionCalleesToSynthesizedCallsiteInfos.count(FS) ||
3121 !FunctionCalleesToSynthesizedCallsiteInfos[FS].count(Callee))
3122 // StackIds is empty (we don't have debug info available in the index for
3123 // these callsites)
3124 FunctionCalleesToSynthesizedCallsiteInfos[FS][Callee] =
3125 std::make_unique<CallsiteInfo>(Callee, SmallVector<unsigned>());
3126 CallsiteInfo *NewCallsiteInfo =
3127 FunctionCalleesToSynthesizedCallsiteInfos[FS][Callee].get();
3128 FoundCalleeChain.push_back({NewCallsiteInfo, FS});
3129 };
3130
3131 // Look for tail calls in this function, and check if they either call the
3132 // profiled callee directly, or indirectly (via a recursive search).
3133 // Only succeed if there is a single unique tail call chain found between the
3134 // profiled caller and callee, otherwise we could perform incorrect cloning.
3135 bool FoundSingleCalleeChain = false;
3136 for (auto &S : CurCallee.getSummaryList()) {
3137 if (!GlobalValue::isLocalLinkage(S->linkage()) &&
3138 !isPrevailing(CurCallee.getGUID(), S.get()))
3139 continue;
3140 auto *FS = dyn_cast<FunctionSummary>(S->getBaseObject());
3141 if (!FS)
3142 continue;
3143 auto FSVI = CurCallee;
3144 auto *AS = dyn_cast<AliasSummary>(S.get());
3145 if (AS)
3146 FSVI = AS->getAliaseeVI();
3147 for (auto &CallEdge : FS->calls()) {
3148 if (!CallEdge.second.hasTailCall())
3149 continue;
3150 if (CallEdge.first == ProfiledCallee) {
3151 if (FoundSingleCalleeChain) {
3152 FoundMultipleCalleeChains = true;
3153 return false;
3154 }
3155 FoundSingleCalleeChain = true;
3156 FoundProfiledCalleeCount++;
3157 FoundProfiledCalleeDepth += Depth;
3158 if (Depth > FoundProfiledCalleeMaxDepth)
3159 FoundProfiledCalleeMaxDepth = Depth;
3160 CreateAndSaveCallsiteInfo(CallEdge.first, FS);
3161 // Add FS to FSToVIMap in case it isn't already there.
3162 assert(!FSToVIMap.count(FS) || FSToVIMap[FS] == FSVI);
3163 FSToVIMap[FS] = FSVI;
3164 } else if (findProfiledCalleeThroughTailCalls(
3165 ProfiledCallee, CallEdge.first, Depth + 1,
3166 FoundCalleeChain, FoundMultipleCalleeChains)) {
3167 // findProfiledCalleeThroughTailCalls should not have returned
3168 // true if FoundMultipleCalleeChains.
3169 assert(!FoundMultipleCalleeChains);
3170 if (FoundSingleCalleeChain) {
3171 FoundMultipleCalleeChains = true;
3172 return false;
3173 }
3174 FoundSingleCalleeChain = true;
3175 CreateAndSaveCallsiteInfo(CallEdge.first, FS);
3176 // Add FS to FSToVIMap in case it isn't already there.
3177 assert(!FSToVIMap.count(FS) || FSToVIMap[FS] == FSVI);
3178 FSToVIMap[FS] = FSVI;
3179 } else if (FoundMultipleCalleeChains)
3180 return false;
3181 }
3182 }
3183
3184 return FoundSingleCalleeChain;
3185}
3186
3187const FunctionSummary *
3188IndexCallsiteContextGraph::getCalleeFunc(IndexCall &Call) {
3189 ValueInfo Callee = dyn_cast_if_present<CallsiteInfo *>(Call)->Callee;
3190 if (Callee.getSummaryList().empty())
3191 return nullptr;
3192 return dyn_cast<FunctionSummary>(Callee.getSummaryList()[0]->getBaseObject());
3193}
3194
3195bool IndexCallsiteContextGraph::calleeMatchesFunc(
3196 IndexCall &Call, const FunctionSummary *Func,
3197 const FunctionSummary *CallerFunc,
3198 std::vector<std::pair<IndexCall, FunctionSummary *>> &FoundCalleeChain) {
3199 ValueInfo Callee = dyn_cast_if_present<CallsiteInfo *>(Call)->Callee;
3200 // If there is no summary list then this is a call to an externally defined
3201 // symbol.
3202 AliasSummary *Alias =
3203 Callee.getSummaryList().empty()
3204 ? nullptr
3205 : dyn_cast<AliasSummary>(Callee.getSummaryList()[0].get());
3206 assert(FSToVIMap.count(Func));
3207 auto FuncVI = FSToVIMap[Func];
3208 if (Callee == FuncVI ||
3209 // If callee is an alias, check the aliasee, since only function
3210 // summary base objects will contain the stack node summaries and thus
3211 // get a context node.
3212 (Alias && Alias->getAliaseeVI() == FuncVI))
3213 return true;
3214
3215 // Recursively search for the profiled callee through tail calls starting with
3216 // the actual Callee. The discovered tail call chain is saved in
3217 // FoundCalleeChain, and we will fixup the graph to include these callsites
3218 // after returning.
3219 // FIXME: We will currently redo the same recursive walk if we find the same
3220 // mismatched callee from another callsite. We can improve this with more
3221 // bookkeeping of the created chain of new nodes for each mismatch.
3222 unsigned Depth = 1;
3223 bool FoundMultipleCalleeChains = false;
3224 if (!findProfiledCalleeThroughTailCalls(
3225 FuncVI, Callee, Depth, FoundCalleeChain, FoundMultipleCalleeChains)) {
3226 LLVM_DEBUG(dbgs() << "Not found through unique tail call chain: " << FuncVI
3227 << " from " << FSToVIMap[CallerFunc]
3228 << " that actually called " << Callee
3229 << (FoundMultipleCalleeChains
3230 ? " (found multiple possible chains)"
3231 : "")
3232 << "\n");
3233 if (FoundMultipleCalleeChains)
3234 FoundProfiledCalleeNonUniquelyCount++;
3235 return false;
3236 }
3237
3238 return true;
3239}
3240
3241bool IndexCallsiteContextGraph::sameCallee(IndexCall &Call1, IndexCall &Call2) {
3242 ValueInfo Callee1 = dyn_cast_if_present<CallsiteInfo *>(Call1)->Callee;
3243 ValueInfo Callee2 = dyn_cast_if_present<CallsiteInfo *>(Call2)->Callee;
3244 return Callee1 == Callee2;
3245}
3246
3247template <typename DerivedCCG, typename FuncTy, typename CallTy>
3248void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode::dump()
3249 const {
3250 print(dbgs());
3251 dbgs() << "\n";
3252}
3253
3254template <typename DerivedCCG, typename FuncTy, typename CallTy>
3255void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode::print(
3256 raw_ostream &OS) const {
3257 OS << "Node " << this << "\n";
3258 OS << "\t";
3259 printCall(OS);
3260 if (Recursive)
3261 OS << " (recursive)";
3262 OS << "\n";
3263 if (!MatchingCalls.empty()) {
3264 OS << "\tMatchingCalls:\n";
3265 for (auto &MatchingCall : MatchingCalls) {
3266 OS << "\t";
3267 MatchingCall.print(OS);
3268 OS << "\n";
3269 }
3270 }
3271 OS << "\tNodeId: " << NodeId << "\n";
3272 OS << "\tAllocTypes: " << getAllocTypeString(AllocTypes) << "\n";
3273 OS << "\tContextIds:";
3274 // Make a copy of the computed context ids that we can sort for stability.
3275 auto ContextIds = getContextIds();
3276 std::vector<uint32_t> SortedIds(ContextIds.begin(), ContextIds.end());
3277 std::sort(SortedIds.begin(), SortedIds.end());
3278 for (auto Id : SortedIds)
3279 OS << " " << Id;
3280 OS << "\n";
3281 OS << "\tCalleeEdges:\n";
3282 for (auto &Edge : CalleeEdges)
3283 OS << "\t\t" << *Edge << " (Callee NodeId: " << Edge->Callee->NodeId
3284 << ")\n";
3285 OS << "\tCallerEdges:\n";
3286 for (auto &Edge : CallerEdges)
3287 OS << "\t\t" << *Edge << " (Caller NodeId: " << Edge->Caller->NodeId
3288 << ")\n";
3289 if (!Clones.empty()) {
3290 OS << "\tClones: ";
3291 ListSeparator LS;
3292 for (auto *C : Clones)
3293 OS << LS << C << " NodeId: " << C->NodeId;
3294 OS << "\n";
3295 } else if (CloneOf) {
3296 OS << "\tClone of " << CloneOf << " NodeId: " << CloneOf->NodeId << "\n";
3297 }
3298}
3299
3300template <typename DerivedCCG, typename FuncTy, typename CallTy>
3301void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextEdge::dump()
3302 const {
3303 print(dbgs());
3304 dbgs() << "\n";
3305}
3306
3307template <typename DerivedCCG, typename FuncTy, typename CallTy>
3308void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextEdge::print(
3309 raw_ostream &OS) const {
3310 OS << "Edge from Callee " << Callee << " to Caller: " << Caller
3311 << (IsBackedge ? " (BE)" : "")
3312 << " AllocTypes: " << getAllocTypeString(AllocTypes);
3313 OS << " ContextIds:";
3314 std::vector<uint32_t> SortedIds(ContextIds.begin(), ContextIds.end());
3315 std::sort(SortedIds.begin(), SortedIds.end());
3316 for (auto Id : SortedIds)
3317 OS << " " << Id;
3318}
3319
3320template <typename DerivedCCG, typename FuncTy, typename CallTy>
3321void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::dump() const {
3322 print(dbgs());
3323}
3324
3325template <typename DerivedCCG, typename FuncTy, typename CallTy>
3326void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::print(
3327 raw_ostream &OS) const {
3328 OS << "Callsite Context Graph:\n";
3329 using GraphType = const CallsiteContextGraph<DerivedCCG, FuncTy, CallTy> *;
3330 for (const auto Node : nodes<GraphType>(this)) {
3331 if (Node->isRemoved())
3332 continue;
3333 Node->print(OS);
3334 OS << "\n";
3335 }
3336}
3337
3338template <typename DerivedCCG, typename FuncTy, typename CallTy>
3339void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::printTotalSizes(
3340 raw_ostream &OS,
3341 function_ref<void(StringRef, StringRef, const Twine &)> EmitRemark) const {
3342 using GraphType = const CallsiteContextGraph<DerivedCCG, FuncTy, CallTy> *;
3343 for (const auto Node : nodes<GraphType>(this)) {
3344 if (Node->isRemoved())
3345 continue;
3346 if (!Node->IsAllocation)
3347 continue;
3348 DenseSet<uint32_t> ContextIds = Node->getContextIds();
3349 auto AllocTypeFromCall = getAllocationCallType(Node->Call);
3350 std::vector<uint32_t> SortedIds(ContextIds.begin(), ContextIds.end());
3351 std::sort(SortedIds.begin(), SortedIds.end());
3352 for (auto Id : SortedIds) {
3353 auto TypeI = ContextIdToAllocationType.find(Id);
3354 assert(TypeI != ContextIdToAllocationType.end());
3355 auto CSI = ContextIdToContextSizeInfos.find(Id);
3356 if (CSI != ContextIdToContextSizeInfos.end()) {
3357 for (auto &Info : CSI->second) {
3358 std::string Msg =
3359 "MemProf hinting: " + getAllocTypeString((uint8_t)TypeI->second) +
3360 " full allocation context " + std::to_string(Info.FullStackId) +
3361 " with total size " + std::to_string(Info.TotalSize) + " is " +
3362 getAllocTypeString(Node->AllocTypes) + " after cloning";
3363 if (allocTypeToUse(Node->AllocTypes) != AllocTypeFromCall)
3364 Msg += " marked " + getAllocTypeString((uint8_t)AllocTypeFromCall) +
3365 " due to cold byte percent";
3366 // Print the internal context id to aid debugging and visualization.
3367 Msg += " (internal context id " + std::to_string(Id) + ")";
3369 OS << Msg << "\n";
3370 if (EmitRemark)
3371 EmitRemark(DEBUG_TYPE, "MemProfReport", Msg);
3372 }
3373 } else {
3374 // This is only emitted if the context size info is not present.
3375 std::string Msg =
3376 "MemProf hinting: " + getAllocTypeString((uint8_t)TypeI->second) +
3377 " context is " + getAllocTypeString(Node->AllocTypes) +
3378 " after cloning";
3379 if (allocTypeToUse(Node->AllocTypes) != AllocTypeFromCall)
3380 Msg += " marked " + getAllocTypeString((uint8_t)AllocTypeFromCall) +
3381 " due to cold byte percent";
3382 // Print the internal context id to aid debugging and visualization.
3383 Msg += " (internal context id " + std::to_string(Id) + ")";
3385 OS << Msg << "\n";
3386 if (EmitRemark)
3387 EmitRemark(DEBUG_TYPE, "MemProfReport", Msg);
3388 }
3389 }
3390 }
3391}
3392
3393template <typename DerivedCCG, typename FuncTy, typename CallTy>
3394void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::check() const {
3395 using GraphType = const CallsiteContextGraph<DerivedCCG, FuncTy, CallTy> *;
3396 for (const auto Node : nodes<GraphType>(this)) {
3397 checkNode<DerivedCCG, FuncTy, CallTy>(Node, /*CheckEdges=*/false);
3398 for (auto &Edge : Node->CallerEdges)
3400 }
3401}
3402
3403template <typename DerivedCCG, typename FuncTy, typename CallTy>
3404struct GraphTraits<const CallsiteContextGraph<DerivedCCG, FuncTy, CallTy> *> {
3405 using GraphType = const CallsiteContextGraph<DerivedCCG, FuncTy, CallTy> *;
3406 using NodeRef = const ContextNode<DerivedCCG, FuncTy, CallTy> *;
3407
3408 using NodePtrTy = std::unique_ptr<ContextNode<DerivedCCG, FuncTy, CallTy>>;
3409 static NodeRef getNode(const NodePtrTy &P) { return P.get(); }
3410
3413 decltype(&getNode)>;
3414
3416 return nodes_iterator(G->NodeOwner.begin(), &getNode);
3417 }
3418
3420 return nodes_iterator(G->NodeOwner.end(), &getNode);
3421 }
3422
3424 return G->NodeOwner.begin()->get();
3425 }
3426
3427 using EdgePtrTy = std::shared_ptr<ContextEdge<DerivedCCG, FuncTy, CallTy>>;
3428 static const ContextNode<DerivedCCG, FuncTy, CallTy> *
3430 return P->Callee;
3431 }
3432
3434 mapped_iterator<typename std::vector<std::shared_ptr<ContextEdge<
3435 DerivedCCG, FuncTy, CallTy>>>::const_iterator,
3436 decltype(&GetCallee)>;
3437
3439 return ChildIteratorType(N->CalleeEdges.begin(), &GetCallee);
3440 }
3441
3443 return ChildIteratorType(N->CalleeEdges.end(), &GetCallee);
3444 }
3445};
3446
3447template <typename DerivedCCG, typename FuncTy, typename CallTy>
3448struct DOTGraphTraits<const CallsiteContextGraph<DerivedCCG, FuncTy, CallTy> *>
3449 : public DefaultDOTGraphTraits {
3450 DOTGraphTraits(bool IsSimple = false) : DefaultDOTGraphTraits(IsSimple) {
3451 // If the user requested the full graph to be exported, but provided an
3452 // allocation id, or if the user gave a context id and requested more than
3453 // just a specific context to be exported, note that highlighting is
3454 // enabled.
3455 DoHighlight =
3456 (AllocIdForDot.getNumOccurrences() && DotGraphScope == DotScope::All) ||
3457 (ContextIdForDot.getNumOccurrences() &&
3459 }
3460
3461 using GraphType = const CallsiteContextGraph<DerivedCCG, FuncTy, CallTy> *;
3463 using NodeRef = typename GTraits::NodeRef;
3464 using ChildIteratorType = typename GTraits::ChildIteratorType;
3465
3466 static std::string getNodeLabel(NodeRef Node, GraphType G) {
3467 std::string LabelString =
3468 (Twine("OrigId: ") + (Node->IsAllocation ? "Alloc" : "") +
3469 Twine(Node->OrigStackOrAllocId) + " NodeId: " + Twine(Node->NodeId))
3470 .str();
3471 LabelString += "\n";
3472 if (Node->hasCall()) {
3473 auto Func = G->NodeToCallingFunc.find(Node);
3474 assert(Func != G->NodeToCallingFunc.end());
3475 LabelString +=
3476 G->getLabel(Func->second, Node->Call.call(), Node->Call.cloneNo());
3477 for (auto &MatchingCall : Node->MatchingCalls) {
3478 LabelString += "\n";
3479 LabelString += G->getLabel(Func->second, MatchingCall.call(),
3480 MatchingCall.cloneNo());
3481 }
3482 } else {
3483 LabelString += "null call";
3484 if (Node->Recursive)
3485 LabelString += " (recursive)";
3486 else
3487 LabelString += " (external)";
3488 }
3489 return LabelString;
3490 }
3491
3493 auto ContextIds = Node->getContextIds();
3494 // If highlighting enabled, see if this node contains any of the context ids
3495 // of interest. If so, it will use a different color and a larger fontsize
3496 // (which makes the node larger as well).
3497 bool Highlight = false;
3498 if (DoHighlight) {
3499 assert(ContextIdForDot.getNumOccurrences() ||
3500 AllocIdForDot.getNumOccurrences());
3501 if (ContextIdForDot.getNumOccurrences())
3502 Highlight = ContextIds.contains(ContextIdForDot);
3503 else
3504 Highlight = set_intersects(ContextIds, G->DotAllocContextIds);
3505 }
3506 std::string AttributeString = (Twine("tooltip=\"") + getNodeId(Node) + " " +
3507 getContextIds(ContextIds) + "\"")
3508 .str();
3509 // Default fontsize is 14
3510 if (Highlight)
3511 AttributeString += ",fontsize=\"30\"";
3512 AttributeString +=
3513 (Twine(",fillcolor=\"") + getColor(Node->AllocTypes, Highlight) + "\"")
3514 .str();
3515 if (Node->CloneOf) {
3516 AttributeString += ",color=\"blue\"";
3517 AttributeString += ",style=\"filled,bold,dashed\"";
3518 } else
3519 AttributeString += ",style=\"filled\"";
3520 return AttributeString;
3521 }
3522
3523 static std::string getEdgeAttributes(NodeRef, ChildIteratorType ChildIter,
3524 GraphType G) {
3525 auto &Edge = *(ChildIter.getCurrent());
3526 // If highlighting enabled, see if this edge contains any of the context ids
3527 // of interest. If so, it will use a different color and a heavier arrow
3528 // size and weight (the larger weight makes the highlighted path
3529 // straighter).
3530 bool Highlight = false;
3531 if (DoHighlight) {
3532 assert(ContextIdForDot.getNumOccurrences() ||
3533 AllocIdForDot.getNumOccurrences());
3534 if (ContextIdForDot.getNumOccurrences())
3535 Highlight = Edge->ContextIds.contains(ContextIdForDot);
3536 else
3537 Highlight = set_intersects(Edge->ContextIds, G->DotAllocContextIds);
3538 }
3539 auto Color = getColor(Edge->AllocTypes, Highlight);
3540 std::string AttributeString =
3541 (Twine("tooltip=\"") + getContextIds(Edge->ContextIds) + "\"" +
3542 // fillcolor is the arrow head and color is the line
3543 Twine(",fillcolor=\"") + Color + "\"" + Twine(",color=\"") + Color +
3544 "\"")
3545 .str();
3546 if (Edge->IsBackedge)
3547 AttributeString += ",style=\"dotted\"";
3548 // Default penwidth and weight are both 1.
3549 if (Highlight)
3550 AttributeString += ",penwidth=\"2.0\",weight=\"2\"";
3551 return AttributeString;
3552 }
3553
3554 // Since the NodeOwners list includes nodes that are no longer connected to
3555 // the graph, skip them here.
3557 if (Node->isRemoved())
3558 return true;
3559 // If a scope smaller than the full graph was requested, see if this node
3560 // contains any of the context ids of interest.
3562 return !set_intersects(Node->getContextIds(), G->DotAllocContextIds);
3564 return !Node->getContextIds().contains(ContextIdForDot);
3565 return false;
3566 }
3567
3568private:
3569 static std::string getContextIds(const DenseSet<uint32_t> &ContextIds) {
3570 std::string IdString = "ContextIds:";
3571 if (ContextIds.size() < 100) {
3572 std::vector<uint32_t> SortedIds(ContextIds.begin(), ContextIds.end());
3573 std::sort(SortedIds.begin(), SortedIds.end());
3574 for (auto Id : SortedIds)
3575 IdString += (" " + Twine(Id)).str();
3576 } else {
3577 IdString += (" (" + Twine(ContextIds.size()) + " ids)").str();
3578 }
3579 return IdString;
3580 }
3581
3582 static std::string getColor(uint8_t AllocTypes, bool Highlight) {
3583 // If DoHighlight is not enabled, we want to use the highlight colors for
3584 // NotCold and Cold, and the non-highlight color for NotCold+Cold. This is
3585 // both compatible with the color scheme before highlighting was supported,
3586 // and for the NotCold+Cold color the non-highlight color is a bit more
3587 // readable.
3588 if (AllocTypes == (uint8_t)AllocationType::NotCold)
3589 // Color "brown1" actually looks like a lighter red.
3590 return !DoHighlight || Highlight ? "brown1" : "lightpink";
3591 if (AllocTypes == (uint8_t)AllocationType::Cold)
3592 return !DoHighlight || Highlight ? "cyan" : "lightskyblue";
3593 if (AllocTypes ==
3594 ((uint8_t)AllocationType::NotCold | (uint8_t)AllocationType::Cold))
3595 return Highlight ? "magenta" : "mediumorchid1";
3596 return "gray";
3597 }
3598
3599 static std::string getNodeId(NodeRef Node) {
3600 std::stringstream SStream;
3601 SStream << std::hex << "N0x" << (unsigned long long)Node;
3602 std::string Result = SStream.str();
3603 return Result;
3604 }
3605
3606 // True if we should highlight a specific context or allocation's contexts in
3607 // the emitted graph.
3608 static bool DoHighlight;
3609};
3610
3611template <typename DerivedCCG, typename FuncTy, typename CallTy>
3612bool DOTGraphTraits<
3613 const CallsiteContextGraph<DerivedCCG, FuncTy, CallTy> *>::DoHighlight =
3614 false;
3615
3616template <typename DerivedCCG, typename FuncTy, typename CallTy>
3617void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::exportToDot(
3618 std::string Label) const {
3619 WriteGraph(this, "", false, Label,
3620 DotFilePathPrefix + "ccg." + Label + ".dot");
3621}
3622
3623template <typename DerivedCCG, typename FuncTy, typename CallTy>
3624typename CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::ContextNode *
3625CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::moveEdgeToNewCalleeClone(
3626 const std::shared_ptr<ContextEdge> &Edge,
3627 DenseSet<uint32_t> ContextIdsToMove) {
3628 ContextNode *Node = Edge->Callee;
3629 assert(NodeToCallingFunc.count(Node));
3630 ContextNode *Clone =
3631 createNewNode(Node->IsAllocation, NodeToCallingFunc[Node], Node->Call);
3632 Node->addClone(Clone);
3633 Clone->MatchingCalls = Node->MatchingCalls;
3634 moveEdgeToExistingCalleeClone(Edge, Clone, /*NewClone=*/true,
3635 ContextIdsToMove);
3636 return Clone;
3637}
3638
3639template <typename DerivedCCG, typename FuncTy, typename CallTy>
3640void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::
3641 moveEdgeToExistingCalleeClone(const std::shared_ptr<ContextEdge> &Edge,
3642 ContextNode *NewCallee, bool NewClone,
3643 DenseSet<uint32_t> ContextIdsToMove) {
3644 // NewCallee and Edge's current callee must be clones of the same original
3645 // node (Edge's current callee may be the original node too).
3646 assert(NewCallee->getOrigNode() == Edge->Callee->getOrigNode());
3647
3648 bool EdgeIsRecursive = Edge->Callee == Edge->Caller;
3649
3650 ContextNode *OldCallee = Edge->Callee;
3651
3652 // We might already have an edge to the new callee from earlier cloning for a
3653 // different allocation. If one exists we will reuse it.
3654 auto ExistingEdgeToNewCallee = NewCallee->findEdgeFromCaller(Edge->Caller);
3655
3656 // Callers will pass an empty ContextIdsToMove set when they want to move the
3657 // edge. Copy in Edge's ids for simplicity.
3658 if (ContextIdsToMove.empty())
3659 ContextIdsToMove = Edge->getContextIds();
3660
3661 // If we are moving all of Edge's ids, then just move the whole Edge.
3662 // Otherwise only move the specified subset, to a new edge if needed.
3663 if (Edge->getContextIds().size() == ContextIdsToMove.size()) {
3664 // First, update the alloc types on New Callee from Edge.
3665 // Do this before we potentially clear Edge's fields below!
3666 NewCallee->AllocTypes |= Edge->AllocTypes;
3667 // Moving the whole Edge.
3668 if (ExistingEdgeToNewCallee) {
3669 // Since we already have an edge to NewCallee, simply move the ids
3670 // onto it, and remove the existing Edge.
3671 ExistingEdgeToNewCallee->getContextIds().insert_range(ContextIdsToMove);
3672 ExistingEdgeToNewCallee->AllocTypes |= Edge->AllocTypes;
3673 assert(Edge->ContextIds == ContextIdsToMove);
3674 removeEdgeFromGraph(Edge.get());
3675 } else {
3676 // Otherwise just reconnect Edge to NewCallee.
3677 Edge->Callee = NewCallee;
3678 NewCallee->CallerEdges.push_back(Edge);
3679 // Remove it from callee where it was previously connected.
3680 OldCallee->eraseCallerEdge(Edge.get());
3681 // Don't need to update Edge's context ids since we are simply
3682 // reconnecting it.
3683 }
3684 } else {
3685 // Only moving a subset of Edge's ids.
3686 // Compute the alloc type of the subset of ids being moved.
3687 auto CallerEdgeAllocType = computeAllocType(ContextIdsToMove);
3688 if (ExistingEdgeToNewCallee) {
3689 // Since we already have an edge to NewCallee, simply move the ids
3690 // onto it.
3691 ExistingEdgeToNewCallee->getContextIds().insert_range(ContextIdsToMove);
3692 ExistingEdgeToNewCallee->AllocTypes |= CallerEdgeAllocType;
3693 } else {
3694 // Otherwise, create a new edge to NewCallee for the ids being moved.
3695 auto NewEdge = std::make_shared<ContextEdge>(
3696 NewCallee, Edge->Caller, CallerEdgeAllocType, ContextIdsToMove);
3697 Edge->Caller->CalleeEdges.push_back(NewEdge);
3698 NewCallee->CallerEdges.push_back(NewEdge);
3699 }
3700 // In either case, need to update the alloc types on NewCallee, and remove
3701 // those ids and update the alloc type on the original Edge.
3702 NewCallee->AllocTypes |= CallerEdgeAllocType;
3703 set_subtract(Edge->ContextIds, ContextIdsToMove);
3704 Edge->AllocTypes = computeAllocType(Edge->ContextIds);
3705 }
3706 // Now walk the old callee node's callee edges and move Edge's context ids
3707 // over to the corresponding edge into the clone (which is created here if
3708 // this is a newly created clone).
3709 for (auto &OldCalleeEdge : OldCallee->CalleeEdges) {
3710 ContextNode *CalleeToUse = OldCalleeEdge->Callee;
3711 // If this is a direct recursion edge, use NewCallee (the clone) as the
3712 // callee as well, so that any edge updated/created here is also direct
3713 // recursive.
3714 if (CalleeToUse == OldCallee) {
3715 // If this is a recursive edge, see if we already moved a recursive edge
3716 // (which would have to have been this one) - if we were only moving a
3717 // subset of context ids it would still be on OldCallee.
3718 if (EdgeIsRecursive) {
3719 assert(OldCalleeEdge == Edge);
3720 continue;
3721 }
3722 CalleeToUse = NewCallee;
3723 }
3724 // The context ids moving to the new callee are the subset of this edge's
3725 // context ids and the context ids on the caller edge being moved.
3726 DenseSet<uint32_t> EdgeContextIdsToMove =
3727 set_intersection(OldCalleeEdge->getContextIds(), ContextIdsToMove);
3728 set_subtract(OldCalleeEdge->getContextIds(), EdgeContextIdsToMove);
3729 OldCalleeEdge->AllocTypes =
3730 computeAllocType(OldCalleeEdge->getContextIds());
3731 if (!NewClone) {
3732 // Update context ids / alloc type on corresponding edge to NewCallee.
3733 // There is a chance this may not exist if we are reusing an existing
3734 // clone, specifically during function assignment, where we would have
3735 // removed none type edges after creating the clone. If we can't find
3736 // a corresponding edge there, fall through to the cloning below.
3737 if (auto *NewCalleeEdge = NewCallee->findEdgeFromCallee(CalleeToUse)) {
3738 NewCalleeEdge->getContextIds().insert_range(EdgeContextIdsToMove);
3739 NewCalleeEdge->AllocTypes |= computeAllocType(EdgeContextIdsToMove);
3740 continue;
3741 }
3742 }
3743 auto NewEdge = std::make_shared<ContextEdge>(
3744 CalleeToUse, NewCallee, computeAllocType(EdgeContextIdsToMove),
3745 EdgeContextIdsToMove);
3746 NewCallee->CalleeEdges.push_back(NewEdge);
3747 NewEdge->Callee->CallerEdges.push_back(NewEdge);
3748 }
3749 // Recompute the node alloc type now that its callee edges have been
3750 // updated (since we will compute from those edges).
3751 OldCallee->AllocTypes = OldCallee->computeAllocType();
3752 // OldCallee alloc type should be None iff its context id set is now empty.
3753 assert((OldCallee->AllocTypes == (uint8_t)AllocationType::None) ==
3754 OldCallee->emptyContextIds());
3755 if (VerifyCCG) {
3756 checkNode<DerivedCCG, FuncTy, CallTy>(OldCallee, /*CheckEdges=*/false);
3757 checkNode<DerivedCCG, FuncTy, CallTy>(NewCallee, /*CheckEdges=*/false);
3758 for (const auto &OldCalleeEdge : OldCallee->CalleeEdges)
3759 checkNode<DerivedCCG, FuncTy, CallTy>(OldCalleeEdge->Callee,
3760 /*CheckEdges=*/false);
3761 for (const auto &NewCalleeEdge : NewCallee->CalleeEdges)
3762 checkNode<DerivedCCG, FuncTy, CallTy>(NewCalleeEdge->Callee,
3763 /*CheckEdges=*/false);
3764 }
3765}
3766
3767template <typename DerivedCCG, typename FuncTy, typename CallTy>
3768void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::
3769 moveCalleeEdgeToNewCaller(const std::shared_ptr<ContextEdge> &Edge,
3770 ContextNode *NewCaller) {
3771 auto *OldCallee = Edge->Callee;
3772 auto *NewCallee = OldCallee;
3773 // If this edge was direct recursive, make any new/updated edge also direct
3774 // recursive to NewCaller.
3775 bool Recursive = Edge->Caller == Edge->Callee;
3776 if (Recursive)
3777 NewCallee = NewCaller;
3778
3779 ContextNode *OldCaller = Edge->Caller;
3780 OldCaller->eraseCalleeEdge(Edge.get());
3781
3782 // We might already have an edge to the new caller. If one exists we will
3783 // reuse it.
3784 auto ExistingEdgeToNewCaller = NewCaller->findEdgeFromCallee(NewCallee);
3785
3786 if (ExistingEdgeToNewCaller) {
3787 // Since we already have an edge to NewCaller, simply move the ids
3788 // onto it, and remove the existing Edge.
3789 ExistingEdgeToNewCaller->getContextIds().insert_range(
3790 Edge->getContextIds());
3791 ExistingEdgeToNewCaller->AllocTypes |= Edge->AllocTypes;
3792 Edge->ContextIds.clear();
3793 Edge->AllocTypes = (uint8_t)AllocationType::None;
3794 OldCallee->eraseCallerEdge(Edge.get());
3795 } else {
3796 // Otherwise just reconnect Edge to NewCaller.
3797 Edge->Caller = NewCaller;
3798 NewCaller->CalleeEdges.push_back(Edge);
3799 if (Recursive) {
3800 assert(NewCallee == NewCaller);
3801 // In the case of (direct) recursive edges, we update the callee as well
3802 // so that it becomes recursive on the new caller.
3803 Edge->Callee = NewCallee;
3804 NewCallee->CallerEdges.push_back(Edge);
3805 OldCallee->eraseCallerEdge(Edge.get());
3806 }
3807 // Don't need to update Edge's context ids since we are simply
3808 // reconnecting it.
3809 }
3810 // In either case, need to update the alloc types on New Caller.
3811 NewCaller->AllocTypes |= Edge->AllocTypes;
3812
3813 // Now walk the old caller node's caller edges and move Edge's context ids
3814 // over to the corresponding edge into the node (which is created here if
3815 // this is a newly created node). We can tell whether this is a newly created
3816 // node by seeing if it has any caller edges yet.
3817#ifndef NDEBUG
3818 bool IsNewNode = NewCaller->CallerEdges.empty();
3819#endif
3820 // If we just moved a direct recursive edge, presumably its context ids should
3821 // also flow out of OldCaller via some other non-recursive callee edge. We
3822 // don't want to remove the recursive context ids from other caller edges yet,
3823 // otherwise the context ids get into an inconsistent state on OldCaller.
3824 // We will update these context ids on the non-recursive caller edge when and
3825 // if they are updated on the non-recursive callee.
3826 if (!Recursive) {
3827 for (auto &OldCallerEdge : OldCaller->CallerEdges) {
3828 auto OldCallerCaller = OldCallerEdge->Caller;
3829 // The context ids moving to the new caller are the subset of this edge's
3830 // context ids and the context ids on the callee edge being moved.
3831 DenseSet<uint32_t> EdgeContextIdsToMove = set_intersection(
3832 OldCallerEdge->getContextIds(), Edge->getContextIds());
3833 if (OldCaller == OldCallerCaller) {
3834 OldCallerCaller = NewCaller;
3835 // Don't actually move this one. The caller will move it directly via a
3836 // call to this function with this as the Edge if it is appropriate to
3837 // move to a diff node that has a matching callee (itself).
3838 continue;
3839 }
3840 set_subtract(OldCallerEdge->getContextIds(), EdgeContextIdsToMove);
3841 OldCallerEdge->AllocTypes =
3842 computeAllocType(OldCallerEdge->getContextIds());
3843 // In this function we expect that any pre-existing node already has edges
3844 // from the same callers as the old node. That should be true in the
3845 // current use case, where we will remove None-type edges after copying
3846 // over all caller edges from the callee.
3847 auto *ExistingCallerEdge = NewCaller->findEdgeFromCaller(OldCallerCaller);
3848 // Since we would have skipped caller edges when moving a direct recursive
3849 // edge, this may not hold true when recursive handling enabled.
3850 assert(IsNewNode || ExistingCallerEdge || AllowRecursiveCallsites);
3851 if (ExistingCallerEdge) {
3852 ExistingCallerEdge->getContextIds().insert_range(EdgeContextIdsToMove);
3853 ExistingCallerEdge->AllocTypes |=
3854 computeAllocType(EdgeContextIdsToMove);
3855 continue;
3856 }
3857 auto NewEdge = std::make_shared<ContextEdge>(
3858 NewCaller, OldCallerCaller, computeAllocType(EdgeContextIdsToMove),
3859 EdgeContextIdsToMove);
3860 NewCaller->CallerEdges.push_back(NewEdge);
3861 NewEdge->Caller->CalleeEdges.push_back(NewEdge);
3862 }
3863 }
3864 // Recompute the node alloc type now that its caller edges have been
3865 // updated (since we will compute from those edges).
3866 OldCaller->AllocTypes = OldCaller->computeAllocType();
3867 // OldCaller alloc type should be None iff its context id set is now empty.
3868 assert((OldCaller->AllocTypes == (uint8_t)AllocationType::None) ==
3869 OldCaller->emptyContextIds());
3870 if (VerifyCCG) {
3871 checkNode<DerivedCCG, FuncTy, CallTy>(OldCaller, /*CheckEdges=*/false);
3872 checkNode<DerivedCCG, FuncTy, CallTy>(NewCaller, /*CheckEdges=*/false);
3873 for (const auto &OldCallerEdge : OldCaller->CallerEdges)
3874 checkNode<DerivedCCG, FuncTy, CallTy>(OldCallerEdge->Caller,
3875 /*CheckEdges=*/false);
3876 for (const auto &NewCallerEdge : NewCaller->CallerEdges)
3877 checkNode<DerivedCCG, FuncTy, CallTy>(NewCallerEdge->Caller,
3878 /*CheckEdges=*/false);
3879 }
3880}
3881
3882template <typename DerivedCCG, typename FuncTy, typename CallTy>
3883void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::
3884 recursivelyRemoveNoneTypeCalleeEdges(
3885 ContextNode *Node, DenseSet<const ContextNode *> &Visited) {
3886 auto Inserted = Visited.insert(Node);
3887 if (!Inserted.second)
3888 return;
3889
3890 removeNoneTypeCalleeEdges(Node);
3891
3892 for (auto *Clone : Node->Clones)
3893 recursivelyRemoveNoneTypeCalleeEdges(Clone, Visited);
3894
3895 // The recursive call may remove some of this Node's caller edges.
3896 // Iterate over a copy and skip any that were removed.
3897 auto CallerEdges = Node->CallerEdges;
3898 for (auto &Edge : CallerEdges) {
3899 // Skip any that have been removed by an earlier recursive call.
3900 if (Edge->isRemoved()) {
3901 assert(!is_contained(Node->CallerEdges, Edge));
3902 continue;
3903 }
3904 recursivelyRemoveNoneTypeCalleeEdges(Edge->Caller, Visited);
3905 }
3906}
3907
3908// This is the standard DFS based backedge discovery algorithm.
3909template <typename DerivedCCG, typename FuncTy, typename CallTy>
3910void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::markBackedges() {
3911 // If we are cloning recursive contexts, find and mark backedges from all root
3912 // callers, using the typical DFS based backedge analysis.
3914 return;
3915 DenseSet<const ContextNode *> Visited;
3916 DenseSet<const ContextNode *> CurrentStack;
3917 for (auto &Entry : NonAllocationCallToContextNodeMap) {
3918 auto *Node = Entry.second;
3919 if (Node->isRemoved())
3920 continue;
3921 // It is a root if it doesn't have callers.
3922 if (!Node->CallerEdges.empty())
3923 continue;
3924 markBackedges(Node, Visited, CurrentStack);
3925 assert(CurrentStack.empty());
3926 }
3927}
3928
3929// Recursive helper for above markBackedges method.
3930template <typename DerivedCCG, typename FuncTy, typename CallTy>
3931void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::markBackedges(
3932 ContextNode *Node, DenseSet<const ContextNode *> &Visited,
3933 DenseSet<const ContextNode *> &CurrentStack) {
3934 auto I = Visited.insert(Node);
3935 // We should only call this for unvisited nodes.
3936 assert(I.second);
3937 (void)I;
3938 for (auto &CalleeEdge : Node->CalleeEdges) {
3939 auto *Callee = CalleeEdge->Callee;
3940 if (Visited.count(Callee)) {
3941 // Since this was already visited we need to check if it is currently on
3942 // the recursive stack in which case it is a backedge.
3943 if (CurrentStack.count(Callee))
3944 CalleeEdge->IsBackedge = true;
3945 continue;
3946 }
3947 CurrentStack.insert(Callee);
3948 markBackedges(Callee, Visited, CurrentStack);
3949 CurrentStack.erase(Callee);
3950 }
3951}
3952
3953template <typename DerivedCCG, typename FuncTy, typename CallTy>
3954void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::identifyClones() {
3955 DenseSet<const ContextNode *> Visited;
3956 for (auto &Entry : AllocationCallToContextNodeMap) {
3957 Visited.clear();
3958 identifyClones(Entry.second, Visited, Entry.second->getContextIds());
3959 }
3960 Visited.clear();
3961 for (auto &Entry : AllocationCallToContextNodeMap)
3962 recursivelyRemoveNoneTypeCalleeEdges(Entry.second, Visited);
3963 if (VerifyCCG)
3964 check();
3965}
3966
3967// helper function to check an AllocType is cold or notcold or both.
3974
3975template <typename DerivedCCG, typename FuncTy, typename CallTy>
3976void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::identifyClones(
3977 ContextNode *Node, DenseSet<const ContextNode *> &Visited,
3978 const DenseSet<uint32_t> &AllocContextIds) {
3979 if (VerifyNodes)
3980 checkNode<DerivedCCG, FuncTy, CallTy>(Node, /*CheckEdges=*/false);
3981 assert(!Node->CloneOf);
3982
3983 // If Node as a null call, then either it wasn't found in the module (regular
3984 // LTO) or summary index (ThinLTO), or there were other conditions blocking
3985 // cloning (e.g. recursion, calls multiple targets, etc).
3986 // Do this here so that we don't try to recursively clone callers below, which
3987 // isn't useful at least for this node.
3988 if (!Node->hasCall())
3989 return;
3990
3991 // No need to look at any callers if allocation type already unambiguous.
3992 if (hasSingleAllocType(Node->AllocTypes))
3993 return;
3994
3995#ifndef NDEBUG
3996 auto Insert =
3997#endif
3998 Visited.insert(Node);
3999 // We should not have visited this node yet.
4000 assert(Insert.second);
4001 // The recursive call to identifyClones may delete the current edge from the
4002 // CallerEdges vector. Make a copy and iterate on that, simpler than passing
4003 // in an iterator and having recursive call erase from it. Other edges may
4004 // also get removed during the recursion, which will have null Callee and
4005 // Caller pointers (and are deleted later), so we skip those below.
4006 {
4007 auto CallerEdges = Node->CallerEdges;
4008 for (auto &Edge : CallerEdges) {
4009 // Skip any that have been removed by an earlier recursive call.
4010 if (Edge->isRemoved()) {
4011 assert(!is_contained(Node->CallerEdges, Edge));
4012 continue;
4013 }
4014 // Defer backedges. See comments further below where these edges are
4015 // handled during the cloning of this Node.
4016 if (Edge->IsBackedge) {
4017 // We should only mark these if cloning recursive contexts, where we
4018 // need to do this deferral.
4020 continue;
4021 }
4022 // Ignore any caller we previously visited via another edge.
4023 if (!Visited.count(Edge->Caller) && !Edge->Caller->CloneOf) {
4024 identifyClones(Edge->Caller, Visited, AllocContextIds);
4025 }
4026 }
4027 }
4028
4029 // Check if we reached an unambiguous call or have have only a single caller.
4030 if (hasSingleAllocType(Node->AllocTypes) || Node->CallerEdges.size() <= 1)
4031 return;
4032
4033 // We need to clone.
4034
4035 // Try to keep the original version as alloc type NotCold. This will make
4036 // cases with indirect calls or any other situation with an unknown call to
4037 // the original function get the default behavior. We do this by sorting the
4038 // CallerEdges of the Node we will clone by alloc type.
4039 //
4040 // Give NotCold edge the lowest sort priority so those edges are at the end of
4041 // the caller edges vector, and stay on the original version (since the below
4042 // code clones greedily until it finds all remaining edges have the same type
4043 // and leaves the remaining ones on the original Node).
4044 //
4045 // We shouldn't actually have any None type edges, so the sorting priority for
4046 // that is arbitrary, and we assert in that case below.
4047 const unsigned AllocTypeCloningPriority[] = {/*None*/ 3, /*NotCold*/ 4,
4048 /*Cold*/ 1,
4049 /*NotColdCold*/ 2};
4050 llvm::stable_sort(Node->CallerEdges,
4051 [&](const std::shared_ptr<ContextEdge> &A,
4052 const std::shared_ptr<ContextEdge> &B) {
4053 // Nodes with non-empty context ids should be sorted
4054 // before those with empty context ids.
4055 if (A->ContextIds.empty())
4056 // Either B ContextIds are non-empty (in which case we
4057 // should return false because B < A), or B ContextIds
4058 // are empty, in which case they are equal, and we
4059 // should maintain the original relative ordering.
4060 return false;
4061 if (B->ContextIds.empty())
4062 return true;
4063
4064 if (A->AllocTypes == B->AllocTypes)
4065 // Use the first context id for each edge as a
4066 // tie-breaker.
4067 return *A->ContextIds.begin() < *B->ContextIds.begin();
4068 return AllocTypeCloningPriority[A->AllocTypes] <
4069 AllocTypeCloningPriority[B->AllocTypes];
4070 });
4071
4072 assert(Node->AllocTypes != (uint8_t)AllocationType::None);
4073
4074 DenseSet<uint32_t> RecursiveContextIds;
4076 // If we are allowing recursive callsites, but have also disabled recursive
4077 // contexts, look for context ids that show up in multiple caller edges.
4079 DenseSet<uint32_t> AllCallerContextIds;
4080 for (auto &CE : Node->CallerEdges) {
4081 // Resize to the largest set of caller context ids, since we know the
4082 // final set will be at least that large.
4083 AllCallerContextIds.reserve(CE->getContextIds().size());
4084 for (auto Id : CE->getContextIds())
4085 if (!AllCallerContextIds.insert(Id).second)
4086 RecursiveContextIds.insert(Id);
4087 }
4088 }
4089
4090 // Iterate until we find no more opportunities for disambiguating the alloc
4091 // types via cloning. In most cases this loop will terminate once the Node
4092 // has a single allocation type, in which case no more cloning is needed.
4093 // Iterate over a copy of Node's caller edges, since we may need to remove
4094 // edges in the moveEdgeTo* methods, and this simplifies the handling and
4095 // makes it less error-prone.
4096 auto CallerEdges = Node->CallerEdges;
4097 for (auto &CallerEdge : CallerEdges) {
4098 // Skip any that have been removed by an earlier recursive call.
4099 if (CallerEdge->isRemoved()) {
4100 assert(!is_contained(Node->CallerEdges, CallerEdge));
4101 continue;
4102 }
4103 assert(CallerEdge->Callee == Node);
4104
4105 // See if cloning the prior caller edge left this node with a single alloc
4106 // type or a single caller. In that case no more cloning of Node is needed.
4107 if (hasSingleAllocType(Node->AllocTypes) || Node->CallerEdges.size() <= 1)
4108 break;
4109
4110 // If the caller was not successfully matched to a call in the IR/summary,
4111 // there is no point in trying to clone for it as we can't update that call.
4112 if (!CallerEdge->Caller->hasCall())
4113 continue;
4114
4115 // Only need to process the ids along this edge pertaining to the given
4116 // allocation.
4117 auto CallerEdgeContextsForAlloc =
4118 set_intersection(CallerEdge->getContextIds(), AllocContextIds);
4119 if (!RecursiveContextIds.empty())
4120 CallerEdgeContextsForAlloc =
4121 set_difference(CallerEdgeContextsForAlloc, RecursiveContextIds);
4122 if (CallerEdgeContextsForAlloc.empty())
4123 continue;
4124
4125 auto CallerAllocTypeForAlloc = computeAllocType(CallerEdgeContextsForAlloc);
4126
4127 // Compute the node callee edge alloc types corresponding to the context ids
4128 // for this caller edge.
4129 std::vector<uint8_t> CalleeEdgeAllocTypesForCallerEdge;
4130 CalleeEdgeAllocTypesForCallerEdge.reserve(Node->CalleeEdges.size());
4131 for (auto &CalleeEdge : Node->CalleeEdges)
4132 CalleeEdgeAllocTypesForCallerEdge.push_back(intersectAllocTypes(
4133 CalleeEdge->getContextIds(), CallerEdgeContextsForAlloc));
4134
4135 // Don't clone if doing so will not disambiguate any alloc types amongst
4136 // caller edges (including the callee edges that would be cloned).
4137 // Otherwise we will simply move all edges to the clone.
4138 //
4139 // First check if by cloning we will disambiguate the caller allocation
4140 // type from node's allocation type. Query allocTypeToUse so that we don't
4141 // bother cloning to distinguish NotCold+Cold from NotCold. Note that
4142 // neither of these should be None type.
4143 //
4144 // Then check if by cloning node at least one of the callee edges will be
4145 // disambiguated by splitting out different context ids.
4146 //
4147 // However, always do the cloning if this is a backedge, in which case we
4148 // have not yet cloned along this caller edge.
4149 assert(CallerEdge->AllocTypes != (uint8_t)AllocationType::None);
4150 assert(Node->AllocTypes != (uint8_t)AllocationType::None);
4151 if (!CallerEdge->IsBackedge &&
4152 allocTypeToUse(CallerAllocTypeForAlloc) ==
4153 allocTypeToUse(Node->AllocTypes) &&
4154 allocTypesMatch<DerivedCCG, FuncTy, CallTy>(
4155 CalleeEdgeAllocTypesForCallerEdge, Node->CalleeEdges)) {
4156 continue;
4157 }
4158
4159 if (CallerEdge->IsBackedge) {
4160 // We should only mark these if cloning recursive contexts, where we
4161 // need to do this deferral.
4163 DeferredBackedges++;
4164 }
4165
4166 // If this is a backedge, we now do recursive cloning starting from its
4167 // caller since we may have moved unambiguous caller contexts to a clone
4168 // of this Node in a previous iteration of the current loop, giving more
4169 // opportunity for cloning through the backedge. Because we sorted the
4170 // caller edges earlier so that cold caller edges are first, we would have
4171 // visited and cloned this node for any unamibiguously cold non-recursive
4172 // callers before any ambiguous backedge callers. Note that we don't do this
4173 // if the caller is already cloned or visited during cloning (e.g. via a
4174 // different context path from the allocation).
4175 // TODO: Can we do better in the case where the caller was already visited?
4176 if (CallerEdge->IsBackedge && !CallerEdge->Caller->CloneOf &&
4177 !Visited.count(CallerEdge->Caller)) {
4178 const auto OrigIdCount = CallerEdge->getContextIds().size();
4179 // Now do the recursive cloning of this backedge's caller, which was
4180 // deferred earlier.
4181 identifyClones(CallerEdge->Caller, Visited, CallerEdgeContextsForAlloc);
4182 removeNoneTypeCalleeEdges(CallerEdge->Caller);
4183 // See if the recursive call to identifyClones moved the context ids to a
4184 // new edge from this node to a clone of caller, and switch to looking at
4185 // that new edge so that we clone Node for the new caller clone.
4186 bool UpdatedEdge = false;
4187 if (OrigIdCount > CallerEdge->getContextIds().size()) {
4188 for (auto E : Node->CallerEdges) {
4189 // Only interested in clones of the current edges caller.
4190 if (E->Caller->CloneOf != CallerEdge->Caller)
4191 continue;
4192 // See if this edge contains any of the context ids originally on the
4193 // current caller edge.
4194 auto CallerEdgeContextsForAllocNew =
4195 set_intersection(CallerEdgeContextsForAlloc, E->getContextIds());
4196 if (CallerEdgeContextsForAllocNew.empty())
4197 continue;
4198 // Make sure we don't pick a previously existing caller edge of this
4199 // Node, which would be processed on a different iteration of the
4200 // outer loop over the saved CallerEdges.
4201 if (llvm::is_contained(CallerEdges, E))
4202 continue;
4203 // The CallerAllocTypeForAlloc and CalleeEdgeAllocTypesForCallerEdge
4204 // are updated further below for all cases where we just invoked
4205 // identifyClones recursively.
4206 CallerEdgeContextsForAlloc.swap(CallerEdgeContextsForAllocNew);
4207 CallerEdge = E;
4208 UpdatedEdge = true;
4209 break;
4210 }
4211 }
4212 // If cloning removed this edge (and we didn't update it to a new edge
4213 // above), we're done with this edge. It's possible we moved all of the
4214 // context ids to an existing clone, in which case there's no need to do
4215 // further processing for them.
4216 if (CallerEdge->isRemoved())
4217 continue;
4218
4219 // Now we need to update the information used for the cloning decisions
4220 // further below, as we may have modified edges and their context ids.
4221
4222 // Note if we changed the CallerEdge above we would have already updated
4223 // the context ids.
4224 if (!UpdatedEdge) {
4225 CallerEdgeContextsForAlloc = set_intersection(
4226 CallerEdgeContextsForAlloc, CallerEdge->getContextIds());
4227 if (CallerEdgeContextsForAlloc.empty())
4228 continue;
4229 }
4230 // Update the other information that depends on the edges and on the now
4231 // updated CallerEdgeContextsForAlloc.
4232 CallerAllocTypeForAlloc = computeAllocType(CallerEdgeContextsForAlloc);
4233 CalleeEdgeAllocTypesForCallerEdge.clear();
4234 for (auto &CalleeEdge : Node->CalleeEdges) {
4235 CalleeEdgeAllocTypesForCallerEdge.push_back(intersectAllocTypes(
4236 CalleeEdge->getContextIds(), CallerEdgeContextsForAlloc));
4237 }
4238 }
4239
4240 // First see if we can use an existing clone. Check each clone and its
4241 // callee edges for matching alloc types.
4242 ContextNode *Clone = nullptr;
4243 for (auto *CurClone : Node->Clones) {
4244 if (allocTypeToUse(CurClone->AllocTypes) !=
4245 allocTypeToUse(CallerAllocTypeForAlloc))
4246 continue;
4247
4248 bool BothSingleAlloc = hasSingleAllocType(CurClone->AllocTypes) &&
4249 hasSingleAllocType(CallerAllocTypeForAlloc);
4250 // The above check should mean that if both have single alloc types that
4251 // they should be equal.
4252 assert(!BothSingleAlloc ||
4253 CurClone->AllocTypes == CallerAllocTypeForAlloc);
4254
4255 // If either both have a single alloc type (which are the same), or if the
4256 // clone's callee edges have the same alloc types as those for the current
4257 // allocation on Node's callee edges (CalleeEdgeAllocTypesForCallerEdge),
4258 // then we can reuse this clone.
4259 if (BothSingleAlloc || allocTypesMatchClone<DerivedCCG, FuncTy, CallTy>(
4260 CalleeEdgeAllocTypesForCallerEdge, CurClone)) {
4261 Clone = CurClone;
4262 break;
4263 }
4264 }
4265
4266 // The edge iterator is adjusted when we move the CallerEdge to the clone.
4267 if (Clone)
4268 moveEdgeToExistingCalleeClone(CallerEdge, Clone, /*NewClone=*/false,
4269 CallerEdgeContextsForAlloc);
4270 else
4271 Clone = moveEdgeToNewCalleeClone(CallerEdge, CallerEdgeContextsForAlloc);
4272
4273 // Sanity check that no alloc types on clone or its edges are None.
4274 assert(Clone->AllocTypes != (uint8_t)AllocationType::None);
4275 }
4276
4277 // We should still have some context ids on the original Node.
4278 assert(!Node->emptyContextIds());
4279
4280 // Sanity check that no alloc types on node or edges are None.
4281 assert(Node->AllocTypes != (uint8_t)AllocationType::None);
4282
4283 if (VerifyNodes)
4284 checkNode<DerivedCCG, FuncTy, CallTy>(Node, /*CheckEdges=*/false);
4285}
4286
4287void ModuleCallsiteContextGraph::updateAllocationCall(
4288 CallInfo &Call, AllocationType AllocType) {
4289 std::string AllocTypeString = getAllocTypeAttributeString(AllocType);
4291 auto A = llvm::Attribute::get(Call.call()->getFunction()->getContext(),
4292 "memprof", AllocTypeString);
4293 cast<CallBase>(Call.call())->addFnAttr(A);
4294 OREGetter(Call.call()->getFunction())
4295 .emit(OptimizationRemark(DEBUG_TYPE, "MemprofAttribute", Call.call())
4296 << ore::NV("AllocationCall", Call.call()) << " in clone "
4297 << ore::NV("Caller", Call.call()->getFunction())
4298 << " marked with memprof allocation attribute "
4299 << ore::NV("Attribute", AllocTypeString));
4300}
4301
4302void IndexCallsiteContextGraph::updateAllocationCall(CallInfo &Call,
4304 auto *AI = cast<AllocInfo *>(Call.call());
4305 assert(AI);
4306 assert(AI->Versions.size() > Call.cloneNo());
4307 AI->Versions[Call.cloneNo()] = (uint8_t)AllocType;
4308}
4309
4311ModuleCallsiteContextGraph::getAllocationCallType(const CallInfo &Call) const {
4312 const auto *CB = cast<CallBase>(Call.call());
4313 if (!CB->getAttributes().hasFnAttr("memprof"))
4314 return AllocationType::None;
4315 return CB->getAttributes().getFnAttr("memprof").getValueAsString() == "cold"
4316 ? AllocationType::Cold
4317 : AllocationType::NotCold;
4318}
4319
4321IndexCallsiteContextGraph::getAllocationCallType(const CallInfo &Call) const {
4322 const auto *AI = cast<AllocInfo *>(Call.call());
4323 assert(AI->Versions.size() > Call.cloneNo());
4324 return (AllocationType)AI->Versions[Call.cloneNo()];
4325}
4326
4327void ModuleCallsiteContextGraph::updateCall(CallInfo &CallerCall,
4328 FuncInfo CalleeFunc) {
4329 auto *CurF = getCalleeFunc(CallerCall.call());
4330 auto NewCalleeCloneNo = CalleeFunc.cloneNo();
4331 if (isMemProfClone(*CurF)) {
4332 // If we already assigned this callsite to call a specific non-default
4333 // clone (i.e. not the original function which is clone 0), ensure that we
4334 // aren't trying to now update it to call a different clone, which is
4335 // indicative of a bug in the graph or function assignment.
4336 auto CurCalleeCloneNo = getMemProfCloneNum(*CurF);
4337 if (CurCalleeCloneNo != NewCalleeCloneNo) {
4338 LLVM_DEBUG(dbgs() << "Mismatch in call clone assignment: was "
4339 << CurCalleeCloneNo << " now " << NewCalleeCloneNo
4340 << "\n");
4341 MismatchedCloneAssignments++;
4342 }
4343 }
4344 if (NewCalleeCloneNo > 0)
4345 cast<CallBase>(CallerCall.call())->setCalledFunction(CalleeFunc.func());
4346 OREGetter(CallerCall.call()->getFunction())
4347 .emit(OptimizationRemark(DEBUG_TYPE, "MemprofCall", CallerCall.call())
4348 << ore::NV("Call", CallerCall.call()) << " in clone "
4349 << ore::NV("Caller", CallerCall.call()->getFunction())
4350 << " assigned to call function clone "
4351 << ore::NV("Callee", CalleeFunc.func()));
4352}
4353
4354void IndexCallsiteContextGraph::updateCall(CallInfo &CallerCall,
4355 FuncInfo CalleeFunc) {
4356 auto *CI = cast<CallsiteInfo *>(CallerCall.call());
4357 assert(CI &&
4358 "Caller cannot be an allocation which should not have profiled calls");
4359 assert(CI->Clones.size() > CallerCall.cloneNo());
4360 auto NewCalleeCloneNo = CalleeFunc.cloneNo();
4361 auto &CurCalleeCloneNo = CI->Clones[CallerCall.cloneNo()];
4362 // If we already assigned this callsite to call a specific non-default
4363 // clone (i.e. not the original function which is clone 0), ensure that we
4364 // aren't trying to now update it to call a different clone, which is
4365 // indicative of a bug in the graph or function assignment.
4366 if (CurCalleeCloneNo != 0 && CurCalleeCloneNo != NewCalleeCloneNo) {
4367 LLVM_DEBUG(dbgs() << "Mismatch in call clone assignment: was "
4368 << CurCalleeCloneNo << " now " << NewCalleeCloneNo
4369 << "\n");
4370 MismatchedCloneAssignments++;
4371 }
4372 CurCalleeCloneNo = NewCalleeCloneNo;
4373}
4374
4375// Update the debug information attached to NewFunc to use the clone Name. Note
4376// this needs to be done for both any existing DISubprogram for the definition,
4377// as well as any separate declaration DISubprogram.
4379 assert(Name == NewFunc->getName());
4380 auto *SP = NewFunc->getSubprogram();
4381 if (!SP)
4382 return;
4383 auto *MDName = MDString::get(NewFunc->getParent()->getContext(), Name);
4384 SP->replaceLinkageName(MDName);
4385 DISubprogram *Decl = SP->getDeclaration();
4386 if (!Decl)
4387 return;
4388 TempDISubprogram NewDecl = Decl->clone();
4389 NewDecl->replaceLinkageName(MDName);
4390 SP->replaceDeclaration(MDNode::replaceWithUniqued(std::move(NewDecl)));
4391}
4392
4393CallsiteContextGraph<ModuleCallsiteContextGraph, Function,
4394 Instruction *>::FuncInfo
4395ModuleCallsiteContextGraph::cloneFunctionForCallsite(
4396 FuncInfo &Func, CallInfo &Call, DenseMap<CallInfo, CallInfo> &CallMap,
4397 std::vector<CallInfo> &CallsWithMetadataInFunc, unsigned CloneNo) {
4398 // Use existing LLVM facilities for cloning and obtaining Call in clone
4399 ValueToValueMapTy VMap;
4400 auto *NewFunc = CloneFunction(Func.func(), VMap);
4401 std::string Name = getMemProfFuncName(Func.func()->getName(), CloneNo);
4402 assert(!Func.func()->getParent()->getFunction(Name));
4403 NewFunc->setName(Name);
4404 updateSubprogramLinkageName(NewFunc, Name);
4405 for (auto &Inst : CallsWithMetadataInFunc) {
4406 // This map always has the initial version in it.
4407 assert(Inst.cloneNo() == 0);
4408 CallMap[Inst] = {cast<Instruction>(VMap[Inst.call()]), CloneNo};
4409 }
4410 OREGetter(Func.func())
4411 .emit(OptimizationRemark(DEBUG_TYPE, "MemprofClone", Func.func())
4412 << "created clone " << ore::NV("NewFunction", NewFunc));
4413 return {NewFunc, CloneNo};
4414}
4415
4416CallsiteContextGraph<IndexCallsiteContextGraph, FunctionSummary,
4417 IndexCall>::FuncInfo
4418IndexCallsiteContextGraph::cloneFunctionForCallsite(
4419 FuncInfo &Func, CallInfo &Call, DenseMap<CallInfo, CallInfo> &CallMap,
4420 std::vector<CallInfo> &CallsWithMetadataInFunc, unsigned CloneNo) {
4421 // Check how many clones we have of Call (and therefore function).
4422 // The next clone number is the current size of versions array.
4423 // Confirm this matches the CloneNo provided by the caller, which is based on
4424 // the number of function clones we have.
4425 assert(CloneNo == (isa<AllocInfo *>(Call.call())
4426 ? cast<AllocInfo *>(Call.call())->Versions.size()
4427 : cast<CallsiteInfo *>(Call.call())->Clones.size()));
4428 // Walk all the instructions in this function. Create a new version for
4429 // each (by adding an entry to the Versions/Clones summary array), and copy
4430 // over the version being called for the function clone being cloned here.
4431 // Additionally, add an entry to the CallMap for the new function clone,
4432 // mapping the original call (clone 0, what is in CallsWithMetadataInFunc)
4433 // to the new call clone.
4434 for (auto &Inst : CallsWithMetadataInFunc) {
4435 // This map always has the initial version in it.
4436 assert(Inst.cloneNo() == 0);
4437 if (auto *AI = dyn_cast<AllocInfo *>(Inst.call())) {
4438 assert(AI->Versions.size() == CloneNo);
4439 // We assign the allocation type later (in updateAllocationCall), just add
4440 // an entry for it here.
4441 AI->Versions.push_back(0);
4442 } else {
4443 auto *CI = cast<CallsiteInfo *>(Inst.call());
4444 assert(CI && CI->Clones.size() == CloneNo);
4445 // We assign the clone number later (in updateCall), just add an entry for
4446 // it here.
4447 CI->Clones.push_back(0);
4448 }
4449 CallMap[Inst] = {Inst.call(), CloneNo};
4450 }
4451 return {Func.func(), CloneNo};
4452}
4453
4454// We perform cloning for each allocation node separately. However, this
4455// sometimes results in a situation where the same node calls multiple
4456// clones of the same callee, created for different allocations. This
4457// causes issues when assigning functions to these clones, as each node can
4458// in reality only call a single callee clone.
4459//
4460// To address this, before assigning functions, merge callee clone nodes as
4461// needed using a post order traversal from the allocations. We attempt to
4462// use existing clones as the merge node when legal, and to share them
4463// among callers with the same properties (callers calling the same set of
4464// callee clone nodes for the same allocations).
4465//
4466// Without this fix, in some cases incorrect function assignment will lead
4467// to calling the wrong allocation clone.
4468template <typename DerivedCCG, typename FuncTy, typename CallTy>
4469void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::mergeClones() {
4470 if (!MergeClones)
4471 return;
4472
4473 // Generate a map from context id to the associated allocation node for use
4474 // when merging clones.
4475 DenseMap<uint32_t, ContextNode *> ContextIdToAllocationNode;
4476 for (auto &Entry : AllocationCallToContextNodeMap) {
4477 auto *Node = Entry.second;
4478 for (auto Id : Node->getContextIds())
4479 ContextIdToAllocationNode[Id] = Node->getOrigNode();
4480 for (auto *Clone : Node->Clones) {
4481 for (auto Id : Clone->getContextIds())
4482 ContextIdToAllocationNode[Id] = Clone->getOrigNode();
4483 }
4484 }
4485
4486 // Post order traversal starting from allocations to ensure each callsite
4487 // calls a single clone of its callee. Callee nodes that are clones of each
4488 // other are merged (via new merge nodes if needed) to achieve this.
4489 DenseSet<const ContextNode *> Visited;
4490 for (auto &Entry : AllocationCallToContextNodeMap) {
4491 auto *Node = Entry.second;
4492
4493 mergeClones(Node, Visited, ContextIdToAllocationNode);
4494
4495 // Make a copy so the recursive post order traversal that may create new
4496 // clones doesn't mess up iteration. Note that the recursive traversal
4497 // itself does not call mergeClones on any of these nodes, which are all
4498 // (clones of) allocations.
4499 auto Clones = Node->Clones;
4500 for (auto *Clone : Clones)
4501 mergeClones(Clone, Visited, ContextIdToAllocationNode);
4502 }
4503
4504 if (DumpCCG) {
4505 dbgs() << "CCG after merging:\n";
4506 dbgs() << *this;
4507 }
4508 if (ExportToDot)
4509 exportToDot("aftermerge");
4510
4511 if (VerifyCCG) {
4512 check();
4513 }
4514}
4515
4516// Recursive helper for above mergeClones method.
4517template <typename DerivedCCG, typename FuncTy, typename CallTy>
4518void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::mergeClones(
4519 ContextNode *Node, DenseSet<const ContextNode *> &Visited,
4520 DenseMap<uint32_t, ContextNode *> &ContextIdToAllocationNode) {
4521 auto Inserted = Visited.insert(Node);
4522 if (!Inserted.second)
4523 return;
4524
4525 // Iteratively perform merging on this node to handle new caller nodes created
4526 // during the recursive traversal. We could do something more elegant such as
4527 // maintain a worklist, but this is a simple approach that doesn't cause a
4528 // measureable compile time effect, as most nodes don't have many caller
4529 // edges to check.
4530 bool FoundUnvisited = true;
4531 unsigned Iters = 0;
4532 while (FoundUnvisited) {
4533 Iters++;
4534 FoundUnvisited = false;
4535 // Make a copy since the recursive call may move a caller edge to a new
4536 // callee, messing up the iterator.
4537 auto CallerEdges = Node->CallerEdges;
4538 for (auto CallerEdge : CallerEdges) {
4539 // Skip any caller edge moved onto a different callee during recursion.
4540 if (CallerEdge->Callee != Node)
4541 continue;
4542 // If we found an unvisited caller, note that we should check the caller
4543 // edges again as mergeClones may add or change caller nodes.
4544 if (DoMergeIteration && !Visited.contains(CallerEdge->Caller))
4545 FoundUnvisited = true;
4546 mergeClones(CallerEdge->Caller, Visited, ContextIdToAllocationNode);
4547 }
4548 }
4549
4550 TotalMergeInvokes++;
4551 TotalMergeIters += Iters;
4552 if (Iters > MaxMergeIters)
4553 MaxMergeIters = Iters;
4554
4555 // Merge for this node after we handle its callers.
4556 mergeNodeCalleeClones(Node, Visited, ContextIdToAllocationNode);
4557}
4558
4559template <typename DerivedCCG, typename FuncTy, typename CallTy>
4560void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::mergeNodeCalleeClones(
4561 ContextNode *Node, DenseSet<const ContextNode *> &Visited,
4562 DenseMap<uint32_t, ContextNode *> &ContextIdToAllocationNode) {
4563 // Ignore Node if we moved all of its contexts to clones.
4564 if (Node->emptyContextIds())
4565 return;
4566
4567 // First identify groups of clones among Node's callee edges, by building
4568 // a map from each callee base node to the associated callee edges from Node.
4569 MapVector<ContextNode *, std::vector<std::shared_ptr<ContextEdge>>>
4570 OrigNodeToCloneEdges;
4571 for (const auto &E : Node->CalleeEdges) {
4572 auto *Callee = E->Callee;
4573 if (!Callee->CloneOf && Callee->Clones.empty())
4574 continue;
4575 ContextNode *Base = Callee->getOrigNode();
4576 OrigNodeToCloneEdges[Base].push_back(E);
4577 }
4578
4579 // Helper for callee edge sorting below. Return true if A's callee has fewer
4580 // caller edges than B, or if A is a clone and B is not, or if A's first
4581 // context id is smaller than B's.
4582 auto CalleeCallerEdgeLessThan = [](const std::shared_ptr<ContextEdge> &A,
4583 const std::shared_ptr<ContextEdge> &B) {
4584 if (A->Callee->CallerEdges.size() != B->Callee->CallerEdges.size())
4585 return A->Callee->CallerEdges.size() < B->Callee->CallerEdges.size();
4586 if (A->Callee->CloneOf && !B->Callee->CloneOf)
4587 return true;
4588 else if (!A->Callee->CloneOf && B->Callee->CloneOf)
4589 return false;
4590 // Use the first context id for each edge as a
4591 // tie-breaker.
4592 return *A->ContextIds.begin() < *B->ContextIds.begin();
4593 };
4594
4595 // Process each set of callee clones called by Node, performing the needed
4596 // merging.
4597 for (auto Entry : OrigNodeToCloneEdges) {
4598 // CalleeEdges is the set of edges from Node reaching callees that are
4599 // mutual clones of each other.
4600 auto &CalleeEdges = Entry.second;
4601 auto NumCalleeClones = CalleeEdges.size();
4602 // A single edge means there is no merging needed.
4603 if (NumCalleeClones == 1)
4604 continue;
4605 // Sort the CalleeEdges calling this group of clones in ascending order of
4606 // their caller edge counts, putting the original non-clone node first in
4607 // cases of a tie. This simplifies finding an existing node to use as the
4608 // merge node.
4609 llvm::stable_sort(CalleeEdges, CalleeCallerEdgeLessThan);
4610
4611 /// Find other callers of the given set of callee edges that can
4612 /// share the same callee merge node. See the comments at this method
4613 /// definition for details.
4614 DenseSet<ContextNode *> OtherCallersToShareMerge;
4615 findOtherCallersToShareMerge(Node, CalleeEdges, ContextIdToAllocationNode,
4616 OtherCallersToShareMerge);
4617
4618 // Now do the actual merging. Identify existing or create a new MergeNode
4619 // during the first iteration. Move each callee over, along with edges from
4620 // other callers we've determined above can share the same merge node.
4621 ContextNode *MergeNode = nullptr;
4622 DenseMap<ContextNode *, unsigned> CallerToMoveCount;
4623 for (auto CalleeEdge : CalleeEdges) {
4624 auto *OrigCallee = CalleeEdge->Callee;
4625 // If we don't have a MergeNode yet (only happens on the first iteration,
4626 // as a new one will be created when we go to move the first callee edge
4627 // over as needed), see if we can use this callee.
4628 if (!MergeNode) {
4629 // If there are no other callers, simply use this callee.
4630 if (CalleeEdge->Callee->CallerEdges.size() == 1) {
4631 MergeNode = OrigCallee;
4632 NonNewMergedNodes++;
4633 continue;
4634 }
4635 // Otherwise, if we have identified other caller nodes that can share
4636 // the merge node with Node, see if all of OrigCallee's callers are
4637 // going to share the same merge node. In that case we can use callee
4638 // (since all of its callers would move to the new merge node).
4639 if (!OtherCallersToShareMerge.empty()) {
4640 bool MoveAllCallerEdges = true;
4641 for (auto CalleeCallerE : OrigCallee->CallerEdges) {
4642 if (CalleeCallerE == CalleeEdge)
4643 continue;
4644 if (!OtherCallersToShareMerge.contains(CalleeCallerE->Caller)) {
4645 MoveAllCallerEdges = false;
4646 break;
4647 }
4648 }
4649 // If we are going to move all callers over, we can use this callee as
4650 // the MergeNode.
4651 if (MoveAllCallerEdges) {
4652 MergeNode = OrigCallee;
4653 NonNewMergedNodes++;
4654 continue;
4655 }
4656 }
4657 }
4658 // Move this callee edge, creating a new merge node if necessary.
4659 if (MergeNode) {
4660 assert(MergeNode != OrigCallee);
4661 moveEdgeToExistingCalleeClone(CalleeEdge, MergeNode,
4662 /*NewClone*/ false);
4663 } else {
4664 MergeNode = moveEdgeToNewCalleeClone(CalleeEdge);
4665 NewMergedNodes++;
4666 }
4667 // Now move all identified edges from other callers over to the merge node
4668 // as well.
4669 if (!OtherCallersToShareMerge.empty()) {
4670 // Make and iterate over a copy of OrigCallee's caller edges because
4671 // some of these will be moved off of the OrigCallee and that would mess
4672 // up the iteration from OrigCallee.
4673 auto OrigCalleeCallerEdges = OrigCallee->CallerEdges;
4674 for (auto &CalleeCallerE : OrigCalleeCallerEdges) {
4675 if (CalleeCallerE == CalleeEdge)
4676 continue;
4677 if (!OtherCallersToShareMerge.contains(CalleeCallerE->Caller))
4678 continue;
4679 CallerToMoveCount[CalleeCallerE->Caller]++;
4680 moveEdgeToExistingCalleeClone(CalleeCallerE, MergeNode,
4681 /*NewClone*/ false);
4682 }
4683 }
4684 removeNoneTypeCalleeEdges(OrigCallee);
4685 removeNoneTypeCalleeEdges(MergeNode);
4686 }
4687 }
4688}
4689
4690// Look for other nodes that have edges to the same set of callee
4691// clones as the current Node. Those can share the eventual merge node
4692// (reducing cloning and binary size overhead) iff:
4693// - they have edges to the same set of callee clones
4694// - each callee edge reaches a subset of the same allocations as Node's
4695// corresponding edge to the same callee clone.
4696// The second requirement is to ensure that we don't undo any of the
4697// necessary cloning to distinguish contexts with different allocation
4698// behavior.
4699// FIXME: This is somewhat conservative, as we really just need to ensure
4700// that they don't reach the same allocations as contexts on edges from Node
4701// going to any of the *other* callee clones being merged. However, that
4702// requires more tracking and checking to get right.
4703template <typename DerivedCCG, typename FuncTy, typename CallTy>
4704void CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::
4705 findOtherCallersToShareMerge(
4706 ContextNode *Node,
4707 std::vector<std::shared_ptr<ContextEdge>> &CalleeEdges,
4708 DenseMap<uint32_t, ContextNode *> &ContextIdToAllocationNode,
4709 DenseSet<ContextNode *> &OtherCallersToShareMerge) {
4710 auto NumCalleeClones = CalleeEdges.size();
4711 // This map counts how many edges to the same callee clone exist for other
4712 // caller nodes of each callee clone.
4713 DenseMap<ContextNode *, unsigned> OtherCallersToSharedCalleeEdgeCount;
4714 // Counts the number of other caller nodes that have edges to all callee
4715 // clones that don't violate the allocation context checking.
4716 unsigned PossibleOtherCallerNodes = 0;
4717
4718 // We only need to look at other Caller nodes if the first callee edge has
4719 // multiple callers (recall they are sorted in ascending order above).
4720 if (CalleeEdges[0]->Callee->CallerEdges.size() < 2)
4721 return;
4722
4723 // For each callee edge:
4724 // - Collect the count of other caller nodes calling the same callees.
4725 // - Collect the alloc nodes reached by contexts on each callee edge.
4726 DenseMap<ContextEdge *, DenseSet<ContextNode *>> CalleeEdgeToAllocNodes;
4727 for (auto CalleeEdge : CalleeEdges) {
4728 assert(CalleeEdge->Callee->CallerEdges.size() > 1);
4729 // For each other caller of the same callee, increment the count of
4730 // edges reaching the same callee clone.
4731 for (auto CalleeCallerEdges : CalleeEdge->Callee->CallerEdges) {
4732 if (CalleeCallerEdges->Caller == Node) {
4733 assert(CalleeCallerEdges == CalleeEdge);
4734 continue;
4735 }
4736 OtherCallersToSharedCalleeEdgeCount[CalleeCallerEdges->Caller]++;
4737 // If this caller edge now reaches all of the same callee clones,
4738 // increment the count of candidate other caller nodes.
4739 if (OtherCallersToSharedCalleeEdgeCount[CalleeCallerEdges->Caller] ==
4740 NumCalleeClones)
4741 PossibleOtherCallerNodes++;
4742 }
4743 // Collect the alloc nodes reached by contexts on each callee edge, for
4744 // later analysis.
4745 for (auto Id : CalleeEdge->getContextIds()) {
4746 auto *Alloc = ContextIdToAllocationNode.lookup(Id);
4747 if (!Alloc) {
4748 // FIXME: unclear why this happens occasionally, presumably
4749 // imperfect graph updates possibly with recursion.
4750 MissingAllocForContextId++;
4751 continue;
4752 }
4753 CalleeEdgeToAllocNodes[CalleeEdge.get()].insert(Alloc);
4754 }
4755 }
4756
4757 // Now walk the callee edges again, and make sure that for each candidate
4758 // caller node all of its edges to the callees reach the same allocs (or
4759 // a subset) as those along the corresponding callee edge from Node.
4760 for (auto CalleeEdge : CalleeEdges) {
4761 assert(CalleeEdge->Callee->CallerEdges.size() > 1);
4762 // Stop if we do not have any (more) candidate other caller nodes.
4763 if (!PossibleOtherCallerNodes)
4764 break;
4765 auto &CurCalleeAllocNodes = CalleeEdgeToAllocNodes[CalleeEdge.get()];
4766 // Check each other caller of this callee clone.
4767 for (auto &CalleeCallerE : CalleeEdge->Callee->CallerEdges) {
4768 // Not interested in the callee edge from Node itself.
4769 if (CalleeCallerE == CalleeEdge)
4770 continue;
4771 // Skip any callers that didn't have callee edges to all the same
4772 // callee clones.
4773 if (OtherCallersToSharedCalleeEdgeCount[CalleeCallerE->Caller] !=
4774 NumCalleeClones)
4775 continue;
4776 // Make sure that each context along edge from candidate caller node
4777 // reaches an allocation also reached by this callee edge from Node.
4778 for (auto Id : CalleeCallerE->getContextIds()) {
4779 auto *Alloc = ContextIdToAllocationNode.lookup(Id);
4780 if (!Alloc)
4781 continue;
4782 // If not, simply reset the map entry to 0 so caller is ignored, and
4783 // reduce the count of candidate other caller nodes.
4784 if (!CurCalleeAllocNodes.contains(Alloc)) {
4785 OtherCallersToSharedCalleeEdgeCount[CalleeCallerE->Caller] = 0;
4786 PossibleOtherCallerNodes--;
4787 break;
4788 }
4789 }
4790 }
4791 }
4792
4793 if (!PossibleOtherCallerNodes)
4794 return;
4795
4796 // Build the set of other caller nodes that can use the same callee merge
4797 // node.
4798 for (auto &[OtherCaller, Count] : OtherCallersToSharedCalleeEdgeCount) {
4799 if (Count != NumCalleeClones)
4800 continue;
4801 OtherCallersToShareMerge.insert(OtherCaller);
4802 }
4803}
4804
4805// This method assigns cloned callsites to functions, cloning the functions as
4806// needed. The assignment is greedy and proceeds roughly as follows:
4807//
4808// For each function Func:
4809// For each call with graph Node having clones:
4810// Initialize ClonesWorklist to Node and its clones
4811// Initialize NodeCloneCount to 0
4812// While ClonesWorklist is not empty:
4813// Clone = pop front ClonesWorklist
4814// NodeCloneCount++
4815// If Func has been cloned less than NodeCloneCount times:
4816// If NodeCloneCount is 1:
4817// Assign Clone to original Func
4818// Continue
4819// Create a new function clone
4820// If other callers not assigned to call a function clone yet:
4821// Assign them to call new function clone
4822// Continue
4823// Assign any other caller calling the cloned version to new clone
4824//
4825// For each caller of Clone:
4826// If caller is assigned to call a specific function clone:
4827// If we cannot assign Clone to that function clone:
4828// Create new callsite Clone NewClone
4829// Add NewClone to ClonesWorklist
4830// Continue
4831// Assign Clone to existing caller's called function clone
4832// Else:
4833// If Clone not already assigned to a function clone:
4834// Assign to first function clone without assignment
4835// Assign caller to selected function clone
4836// For each call with graph Node having clones:
4837// If number func clones > number call's callsite Node clones:
4838// Record func CallInfo clones without Node clone in UnassignedCallClones
4839// For callsite Nodes in DFS order from allocations:
4840// If IsAllocation:
4841// Update allocation with alloc type
4842// Else:
4843// For Call, all MatchingCalls, and associated UnnassignedCallClones:
4844// Update call to call recorded callee clone
4845//
4846template <typename DerivedCCG, typename FuncTy, typename CallTy>
4847bool CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::assignFunctions() {
4848 bool Changed = false;
4849
4850 mergeClones();
4851
4852 // Keep track of the assignment of nodes (callsites) to function clones they
4853 // call.
4854 DenseMap<ContextNode *, FuncInfo> CallsiteToCalleeFuncCloneMap;
4855
4856 // Update caller node to call function version CalleeFunc, by recording the
4857 // assignment in CallsiteToCalleeFuncCloneMap.
4858 auto RecordCalleeFuncOfCallsite = [&](ContextNode *Caller,
4859 const FuncInfo &CalleeFunc) {
4860 assert(Caller->hasCall());
4861 CallsiteToCalleeFuncCloneMap[Caller] = CalleeFunc;
4862 };
4863
4864 // Information for a single clone of this Func.
4865 struct FuncCloneInfo {
4866 // The function clone.
4867 FuncInfo FuncClone;
4868 // Remappings of each call of interest (from original uncloned call to the
4869 // corresponding cloned call in this function clone).
4870 DenseMap<CallInfo, CallInfo> CallMap;
4871 };
4872
4873 // Map to keep track of information needed to update calls in function clones
4874 // when their corresponding callsite node was not itself cloned for that
4875 // function clone. Because of call context pruning (i.e. we only keep as much
4876 // caller information as needed to distinguish hot vs cold), we may not have
4877 // caller edges coming to each callsite node from all possible function
4878 // callers. A function clone may get created for other callsites in the
4879 // function for which there are caller edges that were not pruned. Any other
4880 // callsites in that function clone, which were not themselved cloned for
4881 // that function clone, should get updated the same way as the corresponding
4882 // callsite in the original function (which may call a clone of its callee).
4883 //
4884 // We build this map after completing function cloning for each function, so
4885 // that we can record the information from its call maps before they are
4886 // destructed. The map will be used as we update calls to update any still
4887 // unassigned call clones. Note that we may create new node clones as we clone
4888 // other functions, so later on we check which node clones were still not
4889 // created. To this end, the inner map is a map from function clone number to
4890 // the list of calls cloned for that function (can be more than one due to the
4891 // Node's MatchingCalls array).
4892 //
4893 // The alternative is creating new callsite clone nodes below as we clone the
4894 // function, but that is tricker to get right and likely more overhead.
4895 //
4896 // Inner map is a std::map so sorted by key (clone number), in order to get
4897 // ordered remarks in the full LTO case.
4898 DenseMap<const ContextNode *, std::map<unsigned, SmallVector<CallInfo, 0>>>
4899 UnassignedCallClones;
4900
4901 // Walk all functions for which we saw calls with memprof metadata, and handle
4902 // cloning for each of its calls.
4903 for (auto &[Func, CallsWithMetadata] : FuncToCallsWithMetadata) {
4904 FuncInfo OrigFunc(Func);
4905 // Map from each clone number of OrigFunc to information about that function
4906 // clone (the function clone FuncInfo and call remappings). The index into
4907 // the vector is the clone number, as function clones are created and
4908 // numbered sequentially.
4909 std::vector<FuncCloneInfo> FuncCloneInfos;
4910 for (auto &Call : CallsWithMetadata) {
4911 ContextNode *Node = getNodeForInst(Call);
4912 // Skip call if we do not have a node for it (all uses of its stack ids
4913 // were either on inlined chains or pruned from the MIBs), or if we did
4914 // not create any clones for it.
4915 if (!Node || Node->Clones.empty())
4916 continue;
4917 assert(Node->hasCall() &&
4918 "Not having a call should have prevented cloning");
4919
4920 // Track the assignment of function clones to clones of the current
4921 // callsite Node being handled.
4922 std::map<FuncInfo, ContextNode *> FuncCloneToCurNodeCloneMap;
4923
4924 // Assign callsite version CallsiteClone to function version FuncClone,
4925 // and also assign (possibly cloned) Call to CallsiteClone.
4926 auto AssignCallsiteCloneToFuncClone = [&](const FuncInfo &FuncClone,
4927 CallInfo &Call,
4928 ContextNode *CallsiteClone,
4929 bool IsAlloc) {
4930 // Record the clone of callsite node assigned to this function clone.
4931 FuncCloneToCurNodeCloneMap[FuncClone] = CallsiteClone;
4932
4933 assert(FuncCloneInfos.size() > FuncClone.cloneNo());
4934 DenseMap<CallInfo, CallInfo> &CallMap =
4935 FuncCloneInfos[FuncClone.cloneNo()].CallMap;
4936 CallInfo CallClone(Call);
4937 if (auto It = CallMap.find(Call); It != CallMap.end())
4938 CallClone = It->second;
4939 CallsiteClone->setCall(CallClone);
4940 // Need to do the same for all matching calls.
4941 for (auto &MatchingCall : Node->MatchingCalls) {
4942 CallInfo CallClone(MatchingCall);
4943 if (auto It = CallMap.find(MatchingCall); It != CallMap.end())
4944 CallClone = It->second;
4945 // Updates the call in the list.
4946 MatchingCall = CallClone;
4947 }
4948 };
4949
4950 // Invokes moveEdgeToNewCalleeClone which creates a new clone, and then
4951 // performs the necessary fixups (removing none type edges, and
4952 // importantly, propagating any function call assignment of the original
4953 // node to the new clone).
4954 auto MoveEdgeToNewCalleeCloneAndSetUp =
4955 [&](const std::shared_ptr<ContextEdge> &Edge) {
4956 ContextNode *OrigCallee = Edge->Callee;
4957 ContextNode *NewClone = moveEdgeToNewCalleeClone(Edge);
4958 removeNoneTypeCalleeEdges(NewClone);
4959 assert(NewClone->AllocTypes != (uint8_t)AllocationType::None);
4960 // If the original Callee was already assigned to call a specific
4961 // function version, make sure its new clone is assigned to call
4962 // that same function clone.
4963 if (CallsiteToCalleeFuncCloneMap.count(OrigCallee))
4964 RecordCalleeFuncOfCallsite(
4965 NewClone, CallsiteToCalleeFuncCloneMap[OrigCallee]);
4966 return NewClone;
4967 };
4968
4969 // Keep track of the clones of callsite Node that need to be assigned to
4970 // function clones. This list may be expanded in the loop body below if we
4971 // find additional cloning is required.
4972 std::deque<ContextNode *> ClonesWorklist;
4973 // Ignore original Node if we moved all of its contexts to clones.
4974 if (!Node->emptyContextIds())
4975 ClonesWorklist.push_back(Node);
4976 llvm::append_range(ClonesWorklist, Node->Clones);
4977
4978 // Now walk through all of the clones of this callsite Node that we need,
4979 // and determine the assignment to a corresponding clone of the current
4980 // function (creating new function clones as needed).
4981 unsigned NodeCloneCount = 0;
4982 while (!ClonesWorklist.empty()) {
4983 ContextNode *Clone = ClonesWorklist.front();
4984 ClonesWorklist.pop_front();
4985 NodeCloneCount++;
4986 if (VerifyNodes)
4988
4989 // Need to create a new function clone if we have more callsite clones
4990 // than existing function clones, which would have been assigned to an
4991 // earlier clone in the list (we assign callsite clones to function
4992 // clones greedily).
4993 if (FuncCloneInfos.size() < NodeCloneCount) {
4994 // If this is the first callsite copy, assign to original function.
4995 if (NodeCloneCount == 1) {
4996 // Since FuncCloneInfos is empty in this case, no clones have
4997 // been created for this function yet, and no callers should have
4998 // been assigned a function clone for this callee node yet.
5000 Clone->CallerEdges, [&](const std::shared_ptr<ContextEdge> &E) {
5001 return CallsiteToCalleeFuncCloneMap.count(E->Caller);
5002 }));
5003 // Initialize with empty call map, assign Clone to original function
5004 // and its callers, and skip to the next clone.
5005 FuncCloneInfos.push_back(
5006 {OrigFunc, DenseMap<CallInfo, CallInfo>()});
5007 AssignCallsiteCloneToFuncClone(
5008 OrigFunc, Call, Clone,
5009 AllocationCallToContextNodeMap.count(Call));
5010 for (auto &CE : Clone->CallerEdges) {
5011 // Ignore any caller that does not have a recorded callsite Call.
5012 if (!CE->Caller->hasCall())
5013 continue;
5014 RecordCalleeFuncOfCallsite(CE->Caller, OrigFunc);
5015 }
5016 continue;
5017 }
5018
5019 // First locate which copy of OrigFunc to clone again. If a caller
5020 // of this callsite clone was already assigned to call a particular
5021 // function clone, we need to redirect all of those callers to the
5022 // new function clone, and update their other callees within this
5023 // function.
5024 FuncInfo PreviousAssignedFuncClone;
5025 auto EI = llvm::find_if(
5026 Clone->CallerEdges, [&](const std::shared_ptr<ContextEdge> &E) {
5027 return CallsiteToCalleeFuncCloneMap.count(E->Caller);
5028 });
5029 bool CallerAssignedToCloneOfFunc = false;
5030 if (EI != Clone->CallerEdges.end()) {
5031 const std::shared_ptr<ContextEdge> &Edge = *EI;
5032 PreviousAssignedFuncClone =
5033 CallsiteToCalleeFuncCloneMap[Edge->Caller];
5034 CallerAssignedToCloneOfFunc = true;
5035 }
5036
5037 // Clone function and save it along with the CallInfo map created
5038 // during cloning in the FuncCloneInfos.
5039 DenseMap<CallInfo, CallInfo> NewCallMap;
5040 unsigned CloneNo = FuncCloneInfos.size();
5041 assert(CloneNo > 0 && "Clone 0 is the original function, which "
5042 "should already exist in the map");
5043 FuncInfo NewFuncClone = cloneFunctionForCallsite(
5044 OrigFunc, Call, NewCallMap, CallsWithMetadata, CloneNo);
5045 FuncCloneInfos.push_back({NewFuncClone, std::move(NewCallMap)});
5046 FunctionClonesAnalysis++;
5047 Changed = true;
5048
5049 // If no caller callsites were already assigned to a clone of this
5050 // function, we can simply assign this clone to the new func clone
5051 // and update all callers to it, then skip to the next clone.
5052 if (!CallerAssignedToCloneOfFunc) {
5053 AssignCallsiteCloneToFuncClone(
5054 NewFuncClone, Call, Clone,
5055 AllocationCallToContextNodeMap.count(Call));
5056 for (auto &CE : Clone->CallerEdges) {
5057 // Ignore any caller that does not have a recorded callsite Call.
5058 if (!CE->Caller->hasCall())
5059 continue;
5060 RecordCalleeFuncOfCallsite(CE->Caller, NewFuncClone);
5061 }
5062 continue;
5063 }
5064
5065 // We may need to do additional node cloning in this case.
5066 // Reset the CallsiteToCalleeFuncCloneMap entry for any callers
5067 // that were previously assigned to call PreviousAssignedFuncClone,
5068 // to record that they now call NewFuncClone.
5069 // The none type edge removal may remove some of this Clone's caller
5070 // edges, if it is reached via another of its caller's callees.
5071 // Iterate over a copy and skip any that were removed.
5072 auto CallerEdges = Clone->CallerEdges;
5073 for (auto CE : CallerEdges) {
5074 // Skip any that have been removed on an earlier iteration.
5075 if (CE->isRemoved()) {
5076 assert(!is_contained(Clone->CallerEdges, CE));
5077 continue;
5078 }
5079 assert(CE);
5080 // Ignore any caller that does not have a recorded callsite Call.
5081 if (!CE->Caller->hasCall())
5082 continue;
5083
5084 if (!CallsiteToCalleeFuncCloneMap.count(CE->Caller) ||
5085 // We subsequently fall through to later handling that
5086 // will perform any additional cloning required for
5087 // callers that were calling other function clones.
5088 CallsiteToCalleeFuncCloneMap[CE->Caller] !=
5089 PreviousAssignedFuncClone)
5090 continue;
5091
5092 RecordCalleeFuncOfCallsite(CE->Caller, NewFuncClone);
5093
5094 // If we are cloning a function that was already assigned to some
5095 // callers, then essentially we are creating new callsite clones
5096 // of the other callsites in that function that are reached by those
5097 // callers. Clone the other callees of the current callsite's caller
5098 // that were already assigned to PreviousAssignedFuncClone
5099 // accordingly. This is important since we subsequently update the
5100 // calls from the nodes in the graph and their assignments to callee
5101 // functions recorded in CallsiteToCalleeFuncCloneMap.
5102 // The none type edge removal may remove some of this caller's
5103 // callee edges, if it is reached via another of its callees.
5104 // Iterate over a copy and skip any that were removed.
5105 auto CalleeEdges = CE->Caller->CalleeEdges;
5106 for (auto CalleeEdge : CalleeEdges) {
5107 // Skip any that have been removed on an earlier iteration when
5108 // cleaning up newly None type callee edges.
5109 if (CalleeEdge->isRemoved()) {
5110 assert(!is_contained(CE->Caller->CalleeEdges, CalleeEdge));
5111 continue;
5112 }
5113 assert(CalleeEdge);
5114 ContextNode *Callee = CalleeEdge->Callee;
5115 // Skip the current callsite, we are looking for other
5116 // callsites Caller calls, as well as any that does not have a
5117 // recorded callsite Call.
5118 if (Callee == Clone || !Callee->hasCall())
5119 continue;
5120 // Skip direct recursive calls. We don't need/want to clone the
5121 // caller node again, and this loop will not behave as expected if
5122 // we tried.
5123 if (Callee == CalleeEdge->Caller)
5124 continue;
5125 ContextNode *NewClone =
5126 MoveEdgeToNewCalleeCloneAndSetUp(CalleeEdge);
5127 // Moving the edge may have resulted in some none type
5128 // callee edges on the original Callee.
5129 removeNoneTypeCalleeEdges(Callee);
5130 // Update NewClone with the new Call clone of this callsite's Call
5131 // created for the new function clone created earlier.
5132 // Recall that we have already ensured when building the graph
5133 // that each caller can only call callsites within the same
5134 // function, so we are guaranteed that Callee Call is in the
5135 // current OrigFunc.
5136 // CallMap is set up as indexed by original Call at clone 0.
5137 CallInfo OrigCall(Callee->getOrigNode()->Call);
5138 OrigCall.setCloneNo(0);
5139 DenseMap<CallInfo, CallInfo> &CallMap =
5140 FuncCloneInfos[NewFuncClone.cloneNo()].CallMap;
5141 assert(CallMap.count(OrigCall));
5142 CallInfo NewCall(CallMap[OrigCall]);
5143 assert(NewCall);
5144 NewClone->setCall(NewCall);
5145 // Need to do the same for all matching calls.
5146 for (auto &MatchingCall : NewClone->MatchingCalls) {
5147 CallInfo OrigMatchingCall(MatchingCall);
5148 OrigMatchingCall.setCloneNo(0);
5149 assert(CallMap.count(OrigMatchingCall));
5150 CallInfo NewCall(CallMap[OrigMatchingCall]);
5151 assert(NewCall);
5152 // Updates the call in the list.
5153 MatchingCall = NewCall;
5154 }
5155 }
5156 }
5157 // Fall through to handling below to perform the recording of the
5158 // function for this callsite clone. This enables handling of cases
5159 // where the callers were assigned to different clones of a function.
5160 }
5161
5162 auto FindFirstAvailFuncClone = [&]() {
5163 // Find first function in FuncCloneInfos without an assigned
5164 // clone of this callsite Node. We should always have one
5165 // available at this point due to the earlier cloning when the
5166 // FuncCloneInfos size was smaller than the clone number.
5167 for (auto &CF : FuncCloneInfos) {
5168 if (!FuncCloneToCurNodeCloneMap.count(CF.FuncClone))
5169 return CF.FuncClone;
5170 }
5172 "Expected an available func clone for this callsite clone");
5173 };
5174
5175 // See if we can use existing function clone. Walk through
5176 // all caller edges to see if any have already been assigned to
5177 // a clone of this callsite's function. If we can use it, do so. If not,
5178 // because that function clone is already assigned to a different clone
5179 // of this callsite, then we need to clone again.
5180 // Basically, this checking is needed to handle the case where different
5181 // caller functions/callsites may need versions of this function
5182 // containing different mixes of callsite clones across the different
5183 // callsites within the function. If that happens, we need to create
5184 // additional function clones to handle the various combinations.
5185 //
5186 // Keep track of any new clones of this callsite created by the
5187 // following loop, as well as any existing clone that we decided to
5188 // assign this clone to.
5189 std::map<FuncInfo, ContextNode *> FuncCloneToNewCallsiteCloneMap;
5190 FuncInfo FuncCloneAssignedToCurCallsiteClone;
5191 // Iterate over a copy of Clone's caller edges, since we may need to
5192 // remove edges in the moveEdgeTo* methods, and this simplifies the
5193 // handling and makes it less error-prone.
5194 auto CloneCallerEdges = Clone->CallerEdges;
5195 for (auto &Edge : CloneCallerEdges) {
5196 // Skip removed edges (due to direct recursive edges updated when
5197 // updating callee edges when moving an edge and subsequently
5198 // removed by call to removeNoneTypeCalleeEdges on the Clone).
5199 if (Edge->isRemoved())
5200 continue;
5201 // Ignore any caller that does not have a recorded callsite Call.
5202 if (!Edge->Caller->hasCall())
5203 continue;
5204 // If this caller already assigned to call a version of OrigFunc, need
5205 // to ensure we can assign this callsite clone to that function clone.
5206 if (CallsiteToCalleeFuncCloneMap.count(Edge->Caller)) {
5207 FuncInfo FuncCloneCalledByCaller =
5208 CallsiteToCalleeFuncCloneMap[Edge->Caller];
5209 // First we need to confirm that this function clone is available
5210 // for use by this callsite node clone.
5211 //
5212 // While FuncCloneToCurNodeCloneMap is built only for this Node and
5213 // its callsite clones, one of those callsite clones X could have
5214 // been assigned to the same function clone called by Edge's caller
5215 // - if Edge's caller calls another callsite within Node's original
5216 // function, and that callsite has another caller reaching clone X.
5217 // We need to clone Node again in this case.
5218 if ((FuncCloneToCurNodeCloneMap.count(FuncCloneCalledByCaller) &&
5219 FuncCloneToCurNodeCloneMap[FuncCloneCalledByCaller] !=
5220 Clone) ||
5221 // Detect when we have multiple callers of this callsite that
5222 // have already been assigned to specific, and different, clones
5223 // of OrigFunc (due to other unrelated callsites in Func they
5224 // reach via call contexts). Is this Clone of callsite Node
5225 // assigned to a different clone of OrigFunc? If so, clone Node
5226 // again.
5227 (FuncCloneAssignedToCurCallsiteClone &&
5228 FuncCloneAssignedToCurCallsiteClone !=
5229 FuncCloneCalledByCaller)) {
5230 // We need to use a different newly created callsite clone, in
5231 // order to assign it to another new function clone on a
5232 // subsequent iteration over the Clones array (adjusted below).
5233 // Note we specifically do not reset the
5234 // CallsiteToCalleeFuncCloneMap entry for this caller, so that
5235 // when this new clone is processed later we know which version of
5236 // the function to copy (so that other callsite clones we have
5237 // assigned to that function clone are properly cloned over). See
5238 // comments in the function cloning handling earlier.
5239
5240 // Check if we already have cloned this callsite again while
5241 // walking through caller edges, for a caller calling the same
5242 // function clone. If so, we can move this edge to that new clone
5243 // rather than creating yet another new clone.
5244 if (FuncCloneToNewCallsiteCloneMap.count(
5245 FuncCloneCalledByCaller)) {
5246 ContextNode *NewClone =
5247 FuncCloneToNewCallsiteCloneMap[FuncCloneCalledByCaller];
5248 moveEdgeToExistingCalleeClone(Edge, NewClone);
5249 // Cleanup any none type edges cloned over.
5250 removeNoneTypeCalleeEdges(NewClone);
5251 } else {
5252 // Create a new callsite clone.
5253 ContextNode *NewClone = MoveEdgeToNewCalleeCloneAndSetUp(Edge);
5254 FuncCloneToNewCallsiteCloneMap[FuncCloneCalledByCaller] =
5255 NewClone;
5256 // Add to list of clones and process later.
5257 ClonesWorklist.push_back(NewClone);
5258 }
5259 // Moving the caller edge may have resulted in some none type
5260 // callee edges.
5261 removeNoneTypeCalleeEdges(Clone);
5262 // We will handle the newly created callsite clone in a subsequent
5263 // iteration over this Node's Clones.
5264 continue;
5265 }
5266
5267 // Otherwise, we can use the function clone already assigned to this
5268 // caller.
5269 if (!FuncCloneAssignedToCurCallsiteClone) {
5270 FuncCloneAssignedToCurCallsiteClone = FuncCloneCalledByCaller;
5271 // Assign Clone to FuncCloneCalledByCaller
5272 AssignCallsiteCloneToFuncClone(
5273 FuncCloneCalledByCaller, Call, Clone,
5274 AllocationCallToContextNodeMap.count(Call));
5275 } else
5276 // Don't need to do anything - callsite is already calling this
5277 // function clone.
5278 assert(FuncCloneAssignedToCurCallsiteClone ==
5279 FuncCloneCalledByCaller);
5280
5281 } else {
5282 // We have not already assigned this caller to a version of
5283 // OrigFunc. Do the assignment now.
5284
5285 // First check if we have already assigned this callsite clone to a
5286 // clone of OrigFunc for another caller during this iteration over
5287 // its caller edges.
5288 if (!FuncCloneAssignedToCurCallsiteClone) {
5289 FuncCloneAssignedToCurCallsiteClone = FindFirstAvailFuncClone();
5290 assert(FuncCloneAssignedToCurCallsiteClone);
5291 // Assign Clone to FuncCloneAssignedToCurCallsiteClone
5292 AssignCallsiteCloneToFuncClone(
5293 FuncCloneAssignedToCurCallsiteClone, Call, Clone,
5294 AllocationCallToContextNodeMap.count(Call));
5295 } else
5296 assert(FuncCloneToCurNodeCloneMap
5297 [FuncCloneAssignedToCurCallsiteClone] == Clone);
5298 // Update callers to record function version called.
5299 RecordCalleeFuncOfCallsite(Edge->Caller,
5300 FuncCloneAssignedToCurCallsiteClone);
5301 }
5302 }
5303 // If we didn't assign a function clone to this callsite clone yet, e.g.
5304 // none of its callers has a non-null call, do the assignment here.
5305 // We want to ensure that every callsite clone is assigned to some
5306 // function clone, so that the call updates below work as expected.
5307 // In particular if this is the original callsite, we want to ensure it
5308 // is assigned to the original function, otherwise the original function
5309 // will appear available for assignment to other callsite clones,
5310 // leading to unintended effects. For one, the unknown and not updated
5311 // callers will call into cloned paths leading to the wrong hints,
5312 // because they still call the original function (clone 0). Also,
5313 // because all callsites start out as being clone 0 by default, we can't
5314 // easily distinguish between callsites explicitly assigned to clone 0
5315 // vs those never assigned, which can lead to multiple updates of the
5316 // calls when invoking updateCall below, with mismatched clone values.
5317 // TODO: Add a flag to the callsite nodes or some other mechanism to
5318 // better distinguish and identify callsite clones that are not getting
5319 // assigned to function clones as expected.
5320 if (!FuncCloneAssignedToCurCallsiteClone) {
5321 FuncCloneAssignedToCurCallsiteClone = FindFirstAvailFuncClone();
5322 assert(FuncCloneAssignedToCurCallsiteClone &&
5323 "No available func clone for this callsite clone");
5324 AssignCallsiteCloneToFuncClone(
5325 FuncCloneAssignedToCurCallsiteClone, Call, Clone,
5326 /*IsAlloc=*/AllocationCallToContextNodeMap.contains(Call));
5327 }
5328 }
5329 if (VerifyCCG) {
5331 for (const auto &PE : Node->CalleeEdges)
5333 for (const auto &CE : Node->CallerEdges)
5335 for (auto *Clone : Node->Clones) {
5337 for (const auto &PE : Clone->CalleeEdges)
5339 for (const auto &CE : Clone->CallerEdges)
5341 }
5342 }
5343 }
5344
5345 if (FuncCloneInfos.size() < 2)
5346 continue;
5347
5348 // In this case there is more than just the original function copy.
5349 // Record call clones of any callsite nodes in the function that did not
5350 // themselves get cloned for all of the function clones.
5351 for (auto &Call : CallsWithMetadata) {
5352 ContextNode *Node = getNodeForInst(Call);
5353 if (!Node || !Node->hasCall() || Node->emptyContextIds())
5354 continue;
5355 // If Node has enough clones already to cover all function clones, we can
5356 // skip it. Need to add one for the original copy.
5357 // Use >= in case there were clones that were skipped due to having empty
5358 // context ids
5359 if (Node->Clones.size() + 1 >= FuncCloneInfos.size())
5360 continue;
5361 // First collect all function clones we cloned this callsite node for.
5362 // They may not be sequential due to empty clones e.g.
5363 DenseSet<unsigned> NodeCallClones;
5364 for (auto *C : Node->Clones)
5365 NodeCallClones.insert(C->Call.cloneNo());
5366 unsigned I = 0;
5367 // Now check all the function clones.
5368 for (auto &FC : FuncCloneInfos) {
5369 // Function clones should be sequential.
5370 assert(FC.FuncClone.cloneNo() == I);
5371 // Skip the first clone which got the original call.
5372 // Also skip any other clones created for this Node.
5373 if (++I == 1 || NodeCallClones.contains(I)) {
5374 continue;
5375 }
5376 // Record the call clones created for this callsite in this function
5377 // clone.
5378 auto &CallVector = UnassignedCallClones[Node][I];
5379 DenseMap<CallInfo, CallInfo> &CallMap = FC.CallMap;
5380 if (auto It = CallMap.find(Call); It != CallMap.end()) {
5381 CallInfo CallClone = It->second;
5382 CallVector.push_back(CallClone);
5383 } else {
5384 // All but the original clone (skipped earlier) should have an entry
5385 // for all calls.
5386 assert(false && "Expected to find call in CallMap");
5387 }
5388 // Need to do the same for all matching calls.
5389 for (auto &MatchingCall : Node->MatchingCalls) {
5390 if (auto It = CallMap.find(MatchingCall); It != CallMap.end()) {
5391 CallInfo CallClone = It->second;
5392 CallVector.push_back(CallClone);
5393 } else {
5394 // All but the original clone (skipped earlier) should have an entry
5395 // for all calls.
5396 assert(false && "Expected to find call in CallMap");
5397 }
5398 }
5399 }
5400 }
5401 }
5402
5403 uint8_t BothTypes =
5404 (uint8_t)AllocationType::Cold | (uint8_t)AllocationType::NotCold;
5405
5406 auto UpdateCalls = [&](ContextNode *Node,
5407 DenseSet<const ContextNode *> &Visited,
5408 auto &&UpdateCalls) {
5409 auto Inserted = Visited.insert(Node);
5410 if (!Inserted.second)
5411 return;
5412
5413 for (auto *Clone : Node->Clones)
5414 UpdateCalls(Clone, Visited, UpdateCalls);
5415
5416 for (auto &Edge : Node->CallerEdges)
5417 UpdateCalls(Edge->Caller, Visited, UpdateCalls);
5418
5419 // Skip if either no call to update, or if we ended up with no context ids
5420 // (we moved all edges onto other clones).
5421 if (!Node->hasCall() || Node->emptyContextIds())
5422 return;
5423
5424 if (Node->IsAllocation) {
5425 auto AT = allocTypeToUse(Node->AllocTypes);
5426 // If the allocation type is ambiguous, and more aggressive hinting
5427 // has been enabled via the MinClonedColdBytePercent flag, see if this
5428 // allocation should be hinted cold anyway because its fraction cold bytes
5429 // allocated is at least the given threshold.
5430 if (Node->AllocTypes == BothTypes && MinClonedColdBytePercent < 100 &&
5431 !ContextIdToContextSizeInfos.empty()) {
5432 uint64_t TotalCold = 0;
5433 uint64_t Total = 0;
5434 for (auto Id : Node->getContextIds()) {
5435 auto TypeI = ContextIdToAllocationType.find(Id);
5436 assert(TypeI != ContextIdToAllocationType.end());
5437 auto CSI = ContextIdToContextSizeInfos.find(Id);
5438 if (CSI != ContextIdToContextSizeInfos.end()) {
5439 for (auto &Info : CSI->second) {
5440 Total += Info.TotalSize;
5441 if (TypeI->second == AllocationType::Cold)
5442 TotalCold += Info.TotalSize;
5443 }
5444 }
5445 }
5446 if (TotalCold * 100 >= Total * MinClonedColdBytePercent)
5447 AT = AllocationType::Cold;
5448 }
5449 updateAllocationCall(Node->Call, AT);
5450 assert(Node->MatchingCalls.empty());
5451 return;
5452 }
5453
5454 if (!CallsiteToCalleeFuncCloneMap.count(Node))
5455 return;
5456
5457 auto CalleeFunc = CallsiteToCalleeFuncCloneMap[Node];
5458 updateCall(Node->Call, CalleeFunc);
5459 // Update all the matching calls as well.
5460 for (auto &Call : Node->MatchingCalls)
5461 updateCall(Call, CalleeFunc);
5462
5463 // Now update all calls recorded earlier that are still in function clones
5464 // which don't have a clone of this callsite node.
5465 if (!UnassignedCallClones.contains(Node))
5466 return;
5467 DenseSet<unsigned> NodeCallClones;
5468 for (auto *C : Node->Clones)
5469 NodeCallClones.insert(C->Call.cloneNo());
5470 // Note that we already confirmed Node is in this map a few lines above.
5471 auto &ClonedCalls = UnassignedCallClones[Node];
5472 for (auto &[CloneNo, CallVector] : ClonedCalls) {
5473 // Should start at 1 as we never create an entry for original node.
5474 assert(CloneNo > 0);
5475 // If we subsequently created a clone, skip this one.
5476 if (NodeCallClones.contains(CloneNo))
5477 continue;
5478 // Use the original Node's CalleeFunc.
5479 for (auto &Call : CallVector)
5480 updateCall(Call, CalleeFunc);
5481 }
5482 };
5483
5484 // Performs DFS traversal starting from allocation nodes to update calls to
5485 // reflect cloning decisions recorded earlier. For regular LTO this will
5486 // update the actual calls in the IR to call the appropriate function clone
5487 // (and add attributes to allocation calls), whereas for ThinLTO the decisions
5488 // are recorded in the summary entries.
5489 DenseSet<const ContextNode *> Visited;
5490 for (auto &Entry : AllocationCallToContextNodeMap)
5491 UpdateCalls(Entry.second, Visited, UpdateCalls);
5492
5493 return Changed;
5494}
5495
5496// Compute a SHA1 hash of the callsite and alloc version information of clone I
5497// in the summary, to use in detection of duplicate clones.
5499 SHA1 Hasher;
5500 // Update hash with any callsites that call non-default (non-zero) callee
5501 // versions.
5502 for (auto &SN : FS->callsites()) {
5503 // In theory all callsites and allocs in this function should have the same
5504 // number of clone entries, but handle any discrepancies gracefully below
5505 // for NDEBUG builds.
5506 assert(
5507 SN.Clones.size() > I &&
5508 "Callsite summary has fewer entries than other summaries in function");
5509 if (SN.Clones.size() <= I || !SN.Clones[I])
5510 continue;
5511 uint8_t Data[sizeof(SN.Clones[I])];
5512 support::endian::write32le(Data, SN.Clones[I]);
5513 Hasher.update(Data);
5514 }
5515 // Update hash with any allocs that have non-default (non-None) hints.
5516 for (auto &AN : FS->allocs()) {
5517 // In theory all callsites and allocs in this function should have the same
5518 // number of clone entries, but handle any discrepancies gracefully below
5519 // for NDEBUG builds.
5520 assert(AN.Versions.size() > I &&
5521 "Alloc summary has fewer entries than other summaries in function");
5522 if (AN.Versions.size() <= I ||
5523 (AllocationType)AN.Versions[I] == AllocationType::None)
5524 continue;
5525 Hasher.update(ArrayRef<uint8_t>(&AN.Versions[I], 1));
5526 }
5527 return support::endian::read64le(Hasher.result().data());
5528}
5529
5531 Function &F, unsigned NumClones, Module &M, OptimizationRemarkEmitter &ORE,
5533 &FuncToAliasMap,
5534 FunctionSummary *FS) {
5535 auto TakeDeclNameAndReplace = [](GlobalValue *DeclGV, GlobalValue *NewGV) {
5536 // We might have created this when adjusting callsite in another
5537 // function. It should be a declaration.
5538 assert(DeclGV->isDeclaration());
5539 NewGV->takeName(DeclGV);
5540 DeclGV->replaceAllUsesWith(NewGV);
5541 DeclGV->eraseFromParent();
5542 };
5543
5544 // Handle aliases to this function, and create analogous alias clones to the
5545 // provided clone of this function.
5546 auto CloneFuncAliases = [&](Function *NewF, unsigned I) {
5547 if (!FuncToAliasMap.count(&F))
5548 return;
5549 for (auto *A : FuncToAliasMap[&F]) {
5550 std::string AliasName = getMemProfFuncName(A->getName(), I);
5551 auto *PrevA = M.getNamedAlias(AliasName);
5552 auto *NewA = GlobalAlias::create(A->getValueType(),
5553 A->getType()->getPointerAddressSpace(),
5554 A->getLinkage(), AliasName, NewF);
5555 NewA->copyAttributesFrom(A);
5556 if (PrevA)
5557 TakeDeclNameAndReplace(PrevA, NewA);
5558 }
5559 };
5560
5561 // The first "clone" is the original copy, we should only call this if we
5562 // needed to create new clones.
5563 assert(NumClones > 1);
5565 VMaps.reserve(NumClones - 1);
5566 FunctionsClonedThinBackend++;
5567
5568 // Map of hash of callsite/alloc versions to the instantiated function clone
5569 // (possibly the original) implementing those calls. Used to avoid
5570 // instantiating duplicate function clones.
5571 // FIXME: Ideally the thin link would not generate such duplicate clones to
5572 // start with, but right now it happens due to phase ordering in the function
5573 // assignment and possible new clones that produces. We simply make each
5574 // duplicate an alias to the matching instantiated clone recorded in the map
5575 // (except for available_externally which are made declarations as they would
5576 // be aliases in the prevailing module, and available_externally aliases are
5577 // not well supported right now).
5579
5580 // Save the hash of the original function version.
5581 HashToFunc[ComputeHash(FS, 0)] = &F;
5582
5583 for (unsigned I = 1; I < NumClones; I++) {
5584 VMaps.emplace_back(std::make_unique<ValueToValueMapTy>());
5585 std::string Name = getMemProfFuncName(F.getName(), I);
5586 auto Hash = ComputeHash(FS, I);
5587 // If this clone would duplicate a previously seen clone, don't generate the
5588 // duplicate clone body, just make an alias to satisfy any (potentially
5589 // cross-module) references.
5590 if (HashToFunc.contains(Hash)) {
5591 FunctionCloneDuplicatesThinBackend++;
5592 auto *Func = HashToFunc[Hash];
5593 if (Func->hasAvailableExternallyLinkage()) {
5594 // Skip these as EliminateAvailableExternallyPass does not handle
5595 // available_externally aliases correctly and we end up with an
5596 // available_externally alias to a declaration. Just create a
5597 // declaration for now as we know we will have a definition in another
5598 // module.
5599 auto Decl = M.getOrInsertFunction(Name, Func->getFunctionType());
5600 ORE.emit(OptimizationRemark(DEBUG_TYPE, "MemprofClone", &F)
5601 << "created clone decl " << ore::NV("Decl", Decl.getCallee()));
5602 continue;
5603 }
5604 auto *PrevF = M.getFunction(Name);
5605 auto *Alias = GlobalAlias::create(Name, Func);
5606 if (PrevF)
5607 TakeDeclNameAndReplace(PrevF, Alias);
5608 ORE.emit(OptimizationRemark(DEBUG_TYPE, "MemprofClone", &F)
5609 << "created clone alias " << ore::NV("Alias", Alias));
5610
5611 // Now handle aliases to this function, and clone those as well.
5612 CloneFuncAliases(Func, I);
5613 continue;
5614 }
5615 auto *NewF = CloneFunction(&F, *VMaps.back());
5616 HashToFunc[Hash] = NewF;
5617 FunctionClonesThinBackend++;
5618 // Strip memprof and callsite metadata from clone as they are no longer
5619 // needed.
5620 for (auto &BB : *NewF) {
5621 for (auto &Inst : BB) {
5622 Inst.setMetadata(LLVMContext::MD_memprof, nullptr);
5623 Inst.setMetadata(LLVMContext::MD_callsite, nullptr);
5624 }
5625 }
5626 auto *PrevF = M.getFunction(Name);
5627 if (PrevF)
5628 TakeDeclNameAndReplace(PrevF, NewF);
5629 else
5630 NewF->setName(Name);
5631 updateSubprogramLinkageName(NewF, Name);
5632 ORE.emit(OptimizationRemark(DEBUG_TYPE, "MemprofClone", &F)
5633 << "created clone " << ore::NV("NewFunction", NewF));
5634
5635 // Now handle aliases to this function, and clone those as well.
5636 CloneFuncAliases(NewF, I);
5637 }
5638 return VMaps;
5639}
5640
5641// Locate the summary for F. This is complicated by the fact that it might
5642// have been internalized or promoted.
5644 const ModuleSummaryIndex *ImportSummary,
5645 const Function *CallingFunc = nullptr) {
5646 // FIXME: Ideally we would retain the original GUID in some fashion on the
5647 // function (e.g. as metadata), but for now do our best to locate the
5648 // summary without that information.
5649 ValueInfo TheFnVI = ImportSummary->getValueInfo(F.getGUID());
5650 if (!TheFnVI)
5651 // See if theFn was internalized, by checking index directly with
5652 // original name (this avoids the name adjustment done by getGUID() for
5653 // internal symbols).
5654 TheFnVI = ImportSummary->getValueInfo(
5656 if (TheFnVI)
5657 return TheFnVI;
5658 // Now query with the original name before any promotion was performed.
5659 StringRef OrigName =
5661 // When this pass is enabled, we always add thinlto_src_file provenance
5662 // metadata to imported function definitions, which allows us to recreate the
5663 // original internal symbol's GUID.
5664 auto SrcFileMD = F.getMetadata("thinlto_src_file");
5665 // If this is a call to an imported/promoted local for which we didn't import
5666 // the definition, the metadata will not exist on the declaration. However,
5667 // since we are doing this early, before any inlining in the LTO backend, we
5668 // can simply look at the metadata on the calling function which must have
5669 // been from the same module if F was an internal symbol originally.
5670 if (!SrcFileMD && F.isDeclaration()) {
5671 // We would only call this for a declaration for a direct callsite, in which
5672 // case the caller would have provided the calling function pointer.
5673 assert(CallingFunc);
5674 SrcFileMD = CallingFunc->getMetadata("thinlto_src_file");
5675 // If this is a promoted local (OrigName != F.getName()), since this is a
5676 // declaration, it must be imported from a different module and therefore we
5677 // should always find the metadata on its calling function. Any call to a
5678 // promoted local that came from this module should still be a definition.
5679 assert(SrcFileMD || OrigName == F.getName());
5680 }
5681 StringRef SrcFile = M.getSourceFileName();
5682 if (SrcFileMD)
5683 SrcFile = dyn_cast<MDString>(SrcFileMD->getOperand(0))->getString();
5684 std::string OrigId = GlobalValue::getGlobalIdentifier(
5685 OrigName, GlobalValue::InternalLinkage, SrcFile);
5686 TheFnVI = ImportSummary->getValueInfo(
5688 // Internal func in original module may have gotten a numbered suffix if we
5689 // imported an external function with the same name. This happens
5690 // automatically during IR linking for naming conflicts. It would have to
5691 // still be internal in that case (otherwise it would have been renamed on
5692 // promotion in which case we wouldn't have a naming conflict).
5693 if (!TheFnVI && OrigName == F.getName() && F.hasLocalLinkage() &&
5694 F.getName().contains('.')) {
5695 OrigName = F.getName().rsplit('.').first;
5697 OrigName, GlobalValue::InternalLinkage, SrcFile);
5698 TheFnVI = ImportSummary->getValueInfo(
5700 }
5701 // The only way we may not have a VI is if this is a declaration created for
5702 // an imported reference. For distributed ThinLTO we may not have a VI for
5703 // such declarations in the distributed summary.
5704 assert(TheFnVI || F.isDeclaration());
5705 return TheFnVI;
5706}
5707
5708bool MemProfContextDisambiguation::initializeIndirectCallPromotionInfo(
5709 Module &M) {
5710 ICallAnalysis = std::make_unique<ICallPromotionAnalysis>();
5711 Symtab = std::make_unique<InstrProfSymtab>();
5712 // Don't add canonical names, to avoid multiple functions to the symtab
5713 // when they both have the same root name with "." suffixes stripped.
5714 // If we pick the wrong one then this could lead to incorrect ICP and calling
5715 // a memprof clone that we don't actually create (resulting in linker unsats).
5716 // What this means is that the GUID of the function (or its PGOFuncName
5717 // metadata) *must* match that in the VP metadata to allow promotion.
5718 // In practice this should not be a limitation, since local functions should
5719 // have PGOFuncName metadata and global function names shouldn't need any
5720 // special handling (they should not get the ".llvm.*" suffix that the
5721 // canonicalization handling is attempting to strip).
5722 if (Error E = Symtab->create(M, /*InLTO=*/true, /*AddCanonical=*/false)) {
5723 std::string SymtabFailure = toString(std::move(E));
5724 M.getContext().emitError("Failed to create symtab: " + SymtabFailure);
5725 return false;
5726 }
5727 return true;
5728}
5729
5730#ifndef NDEBUG
5731// Sanity check that the MIB stack ids match between the summary and
5732// instruction metadata.
5734 const AllocInfo &AllocNode, const MDNode *MemProfMD,
5735 const CallStack<MDNode, MDNode::op_iterator> &CallsiteContext,
5736 const ModuleSummaryIndex *ImportSummary) {
5737 auto MIBIter = AllocNode.MIBs.begin();
5738 for (auto &MDOp : MemProfMD->operands()) {
5739 assert(MIBIter != AllocNode.MIBs.end());
5740 auto StackIdIndexIter = MIBIter->StackIdIndices.begin();
5741 auto *MIBMD = cast<const MDNode>(MDOp);
5742 MDNode *StackMDNode = getMIBStackNode(MIBMD);
5743 assert(StackMDNode);
5744 CallStack<MDNode, MDNode::op_iterator> StackContext(StackMDNode);
5745 auto ContextIterBegin =
5746 StackContext.beginAfterSharedPrefix(CallsiteContext);
5747 // Skip the checking on the first iteration.
5748 uint64_t LastStackContextId =
5749 (ContextIterBegin != StackContext.end() && *ContextIterBegin == 0) ? 1
5750 : 0;
5751 for (auto ContextIter = ContextIterBegin; ContextIter != StackContext.end();
5752 ++ContextIter) {
5753 // If this is a direct recursion, simply skip the duplicate
5754 // entries, to be consistent with how the summary ids were
5755 // generated during ModuleSummaryAnalysis.
5756 if (LastStackContextId == *ContextIter)
5757 continue;
5758 LastStackContextId = *ContextIter;
5759 assert(StackIdIndexIter != MIBIter->StackIdIndices.end());
5760 assert(ImportSummary->getStackIdAtIndex(*StackIdIndexIter) ==
5761 *ContextIter);
5762 StackIdIndexIter++;
5763 }
5764 MIBIter++;
5765 }
5766}
5767#endif
5768
5769bool MemProfContextDisambiguation::applyImport(Module &M) {
5770 assert(ImportSummary);
5771 bool Changed = false;
5772
5773 // We also need to clone any aliases that reference cloned functions, because
5774 // the modified callsites may invoke via the alias. Keep track of the aliases
5775 // for each function.
5776 std::map<const Function *, SmallPtrSet<const GlobalAlias *, 1>>
5777 FuncToAliasMap;
5778 for (auto &A : M.aliases()) {
5779 auto *Aliasee = A.getAliaseeObject();
5780 if (auto *F = dyn_cast<Function>(Aliasee))
5781 FuncToAliasMap[F].insert(&A);
5782 }
5783
5784 if (!initializeIndirectCallPromotionInfo(M))
5785 return false;
5786
5787 for (auto &F : M) {
5788 if (F.isDeclaration() || isMemProfClone(F))
5789 continue;
5790
5791 OptimizationRemarkEmitter ORE(&F);
5792
5794 bool ClonesCreated = false;
5795 unsigned NumClonesCreated = 0;
5796 auto CloneFuncIfNeeded = [&](unsigned NumClones, FunctionSummary *FS) {
5797 // We should at least have version 0 which is the original copy.
5798 assert(NumClones > 0);
5799 // If only one copy needed use original.
5800 if (NumClones == 1)
5801 return;
5802 // If we already performed cloning of this function, confirm that the
5803 // requested number of clones matches (the thin link should ensure the
5804 // number of clones for each constituent callsite is consistent within
5805 // each function), before returning.
5806 if (ClonesCreated) {
5807 assert(NumClonesCreated == NumClones);
5808 return;
5809 }
5810 VMaps = createFunctionClones(F, NumClones, M, ORE, FuncToAliasMap, FS);
5811 // The first "clone" is the original copy, which doesn't have a VMap.
5812 assert(VMaps.size() == NumClones - 1);
5813 Changed = true;
5814 ClonesCreated = true;
5815 NumClonesCreated = NumClones;
5816 };
5817
5818 auto CloneCallsite = [&](const CallsiteInfo &StackNode, CallBase *CB,
5819 Function *CalledFunction, FunctionSummary *FS) {
5820 // Perform cloning if not yet done.
5821 CloneFuncIfNeeded(/*NumClones=*/StackNode.Clones.size(), FS);
5822
5823 assert(!isMemProfClone(*CalledFunction));
5824
5825 // Because we update the cloned calls by calling setCalledOperand (see
5826 // comment below), out of an abundance of caution make sure the called
5827 // function was actually the called operand (or its aliasee). We also
5828 // strip pointer casts when looking for calls (to match behavior during
5829 // summary generation), however, with opaque pointers in theory this
5830 // should not be an issue. Note we still clone the current function
5831 // (containing this call) above, as that could be needed for its callers.
5832 auto *GA = dyn_cast_or_null<GlobalAlias>(CB->getCalledOperand());
5833 if (CalledFunction != CB->getCalledOperand() &&
5834 (!GA || CalledFunction != GA->getAliaseeObject())) {
5835 SkippedCallsCloning++;
5836 return;
5837 }
5838 // Update the calls per the summary info.
5839 // Save orig name since it gets updated in the first iteration
5840 // below.
5841 auto CalleeOrigName = CalledFunction->getName();
5842 for (unsigned J = 0; J < StackNode.Clones.size(); J++) {
5843 // If the VMap is empty, this clone was a duplicate of another and was
5844 // created as an alias or a declaration.
5845 if (J > 0 && VMaps[J - 1]->empty())
5846 continue;
5847 // Do nothing if this version calls the original version of its
5848 // callee.
5849 if (!StackNode.Clones[J])
5850 continue;
5851 auto NewF = M.getOrInsertFunction(
5852 getMemProfFuncName(CalleeOrigName, StackNode.Clones[J]),
5853 CalledFunction->getFunctionType());
5854 CallBase *CBClone;
5855 // Copy 0 is the original function.
5856 if (!J)
5857 CBClone = CB;
5858 else
5859 CBClone = cast<CallBase>((*VMaps[J - 1])[CB]);
5860 // Set the called operand directly instead of calling setCalledFunction,
5861 // as the latter mutates the function type on the call. In rare cases
5862 // we may have a slightly different type on a callee function
5863 // declaration due to it being imported from a different module with
5864 // incomplete types. We really just want to change the name of the
5865 // function to the clone, and not make any type changes.
5866 CBClone->setCalledOperand(NewF.getCallee());
5867 ORE.emit(OptimizationRemark(DEBUG_TYPE, "MemprofCall", CBClone)
5868 << ore::NV("Call", CBClone) << " in clone "
5869 << ore::NV("Caller", CBClone->getFunction())
5870 << " assigned to call function clone "
5871 << ore::NV("Callee", NewF.getCallee()));
5872 }
5873 };
5874
5875 // Locate the summary for F.
5876 ValueInfo TheFnVI = findValueInfoForFunc(F, M, ImportSummary);
5877 // If not found, this could be an imported local (see comment in
5878 // findValueInfoForFunc). Skip for now as it will be cloned in its original
5879 // module (where it would have been promoted to global scope so should
5880 // satisfy any reference in this module).
5881 if (!TheFnVI)
5882 continue;
5883
5884 auto *GVSummary =
5885 ImportSummary->findSummaryInModule(TheFnVI, M.getModuleIdentifier());
5886 if (!GVSummary) {
5887 // Must have been imported, use the summary which matches the definition。
5888 // (might be multiple if this was a linkonce_odr).
5889 auto SrcModuleMD = F.getMetadata("thinlto_src_module");
5890 assert(SrcModuleMD &&
5891 "enable-import-metadata is needed to emit thinlto_src_module");
5892 StringRef SrcModule =
5893 dyn_cast<MDString>(SrcModuleMD->getOperand(0))->getString();
5894 for (auto &GVS : TheFnVI.getSummaryList()) {
5895 if (GVS->modulePath() == SrcModule) {
5896 GVSummary = GVS.get();
5897 break;
5898 }
5899 }
5900 // TODO: Put back the assert once we have metadata on imported copies of
5901 // aliases linking them back to the original alias GUID, which would allow
5902 // us to locate the alias summary here.
5903 // assert(GVSummary && GVSummary->modulePath() == SrcModule);
5904 }
5905
5906 // GVSummary can be null if this is a function imported as a copy of an
5907 // alias, and we don't have the aliasee's summary in our distributed index.
5908 // TODO: Once we can locate the original GUID for imported aliases (e.g. via
5909 // TBD additional metadata), we should find the alias summary instead, and
5910 // we can remove this check and fall back to the original check below.
5911 if (!GVSummary)
5912 continue;
5913
5914 // If this was an imported alias skip it as we won't have the function
5915 // summary, and it should be cloned in the original module.
5916 if (isa<AliasSummary>(GVSummary))
5917 continue;
5918
5919 auto *FS = cast<FunctionSummary>(GVSummary->getBaseObject());
5920
5921 if (FS->allocs().empty() && FS->callsites().empty())
5922 continue;
5923
5924 auto SI = FS->callsites().begin();
5925 auto AI = FS->allocs().begin();
5926
5927 // To handle callsite infos synthesized for tail calls which have missing
5928 // frames in the profiled context, map callee VI to the synthesized callsite
5929 // info.
5930 DenseMap<ValueInfo, CallsiteInfo> MapTailCallCalleeVIToCallsite;
5931 // Iterate the callsites for this function in reverse, since we place all
5932 // those synthesized for tail calls at the end.
5933 for (auto CallsiteIt = FS->callsites().rbegin();
5934 CallsiteIt != FS->callsites().rend(); CallsiteIt++) {
5935 auto &Callsite = *CallsiteIt;
5936 // Stop as soon as we see a non-synthesized callsite info (see comment
5937 // above loop). All the entries added for discovered tail calls have empty
5938 // stack ids.
5939 if (!Callsite.StackIdIndices.empty())
5940 break;
5941 MapTailCallCalleeVIToCallsite.insert({Callsite.Callee, Callsite});
5942 }
5943
5944 // Keeps track of needed ICP for the function.
5945 SmallVector<ICallAnalysisData> ICallAnalysisInfo;
5946
5947 // Assume for now that the instructions are in the exact same order
5948 // as when the summary was created, but confirm this is correct by
5949 // matching the stack ids.
5950 for (auto &BB : F) {
5951 for (auto &I : BB) {
5952 auto *CB = dyn_cast<CallBase>(&I);
5953 // Same handling as when creating module summary.
5954 if (!mayHaveMemprofSummary(CB))
5955 continue;
5956
5957 auto *CalledValue = CB->getCalledOperand();
5958 auto *CalledFunction = CB->getCalledFunction();
5959 if (CalledValue && !CalledFunction) {
5960 CalledValue = CalledValue->stripPointerCasts();
5961 // Stripping pointer casts can reveal a called function.
5962 CalledFunction = dyn_cast<Function>(CalledValue);
5963 }
5964 // Check if this is an alias to a function. If so, get the
5965 // called aliasee for the checks below.
5966 if (auto *GA = dyn_cast<GlobalAlias>(CalledValue)) {
5967 assert(!CalledFunction &&
5968 "Expected null called function in callsite for alias");
5969 CalledFunction = dyn_cast<Function>(GA->getAliaseeObject());
5970 }
5971
5972 CallStack<MDNode, MDNode::op_iterator> CallsiteContext(
5973 I.getMetadata(LLVMContext::MD_callsite));
5974 auto *MemProfMD = I.getMetadata(LLVMContext::MD_memprof);
5975
5976 // Include allocs that were already assigned a memprof function
5977 // attribute in the statistics. Only do this for those that do not have
5978 // memprof metadata, since we add an "ambiguous" memprof attribute by
5979 // default.
5980 if (CB->getAttributes().hasFnAttr("memprof") && !MemProfMD) {
5981 CB->getAttributes().getFnAttr("memprof").getValueAsString() == "cold"
5982 ? AllocTypeColdThinBackend++
5983 : AllocTypeNotColdThinBackend++;
5984 OrigAllocsThinBackend++;
5985 AllocVersionsThinBackend++;
5986 if (!MaxAllocVersionsThinBackend)
5987 MaxAllocVersionsThinBackend = 1;
5988 continue;
5989 }
5990
5991 if (MemProfMD) {
5992 // Consult the next alloc node.
5993 assert(AI != FS->allocs().end());
5994 auto &AllocNode = *(AI++);
5995
5996#ifndef NDEBUG
5997 checkAllocContextIds(AllocNode, MemProfMD, CallsiteContext,
5998 ImportSummary);
5999#endif
6000
6001 // Perform cloning if not yet done.
6002 CloneFuncIfNeeded(/*NumClones=*/AllocNode.Versions.size(), FS);
6003
6004 OrigAllocsThinBackend++;
6005 AllocVersionsThinBackend += AllocNode.Versions.size();
6006 if (MaxAllocVersionsThinBackend < AllocNode.Versions.size())
6007 MaxAllocVersionsThinBackend = AllocNode.Versions.size();
6008
6009 // If there is only one version that means we didn't end up
6010 // considering this function for cloning, and in that case the alloc
6011 // will still be none type or should have gotten the default NotCold.
6012 // Skip that after calling clone helper since that does some sanity
6013 // checks that confirm we haven't decided yet that we need cloning.
6014 // We might have a single version that is cold due to the
6015 // MinClonedColdBytePercent heuristic, make sure we don't skip in that
6016 // case.
6017 if (AllocNode.Versions.size() == 1 &&
6018 (AllocationType)AllocNode.Versions[0] != AllocationType::Cold) {
6019 assert((AllocationType)AllocNode.Versions[0] ==
6020 AllocationType::NotCold ||
6021 (AllocationType)AllocNode.Versions[0] ==
6022 AllocationType::None);
6023 UnclonableAllocsThinBackend++;
6024 continue;
6025 }
6026
6027 // All versions should have a singular allocation type.
6028 assert(llvm::none_of(AllocNode.Versions, [](uint8_t Type) {
6029 return Type == ((uint8_t)AllocationType::NotCold |
6030 (uint8_t)AllocationType::Cold);
6031 }));
6032
6033 // Update the allocation types per the summary info.
6034 for (unsigned J = 0; J < AllocNode.Versions.size(); J++) {
6035 // If the VMap is empty, this clone was a duplicate of another and
6036 // was created as an alias or a declaration.
6037 if (J > 0 && VMaps[J - 1]->empty())
6038 continue;
6039 // Ignore any that didn't get an assigned allocation type.
6040 if (AllocNode.Versions[J] == (uint8_t)AllocationType::None)
6041 continue;
6042 AllocationType AllocTy = (AllocationType)AllocNode.Versions[J];
6043 AllocTy == AllocationType::Cold ? AllocTypeColdThinBackend++
6044 : AllocTypeNotColdThinBackend++;
6045 std::string AllocTypeString = getAllocTypeAttributeString(AllocTy);
6046 auto A = llvm::Attribute::get(F.getContext(), "memprof",
6047 AllocTypeString);
6048 CallBase *CBClone;
6049 // Copy 0 is the original function.
6050 if (!J)
6051 CBClone = CB;
6052 else
6053 // Since VMaps are only created for new clones, we index with
6054 // clone J-1 (J==0 is the original clone and does not have a VMaps
6055 // entry).
6056 CBClone = cast<CallBase>((*VMaps[J - 1])[CB]);
6058 CBClone->addFnAttr(A);
6059 ORE.emit(OptimizationRemark(DEBUG_TYPE, "MemprofAttribute", CBClone)
6060 << ore::NV("AllocationCall", CBClone) << " in clone "
6061 << ore::NV("Caller", CBClone->getFunction())
6062 << " marked with memprof allocation attribute "
6063 << ore::NV("Attribute", AllocTypeString));
6064 }
6065 } else if (!CallsiteContext.empty()) {
6066 if (!CalledFunction) {
6067#ifndef NDEBUG
6068 // We should have skipped inline assembly calls.
6069 auto *CI = dyn_cast<CallInst>(CB);
6070 assert(!CI || !CI->isInlineAsm());
6071#endif
6072 // We should have skipped direct calls via a Constant.
6073 assert(CalledValue && !isa<Constant>(CalledValue));
6074
6075 // This is an indirect call, see if we have profile information and
6076 // whether any clones were recorded for the profiled targets (that
6077 // we synthesized CallsiteInfo summary records for when building the
6078 // index).
6079 auto NumClones =
6080 recordICPInfo(CB, FS->callsites(), SI, ICallAnalysisInfo);
6081
6082 // Perform cloning if not yet done. This is done here in case
6083 // we don't need to do ICP, but might need to clone this
6084 // function as it is the target of other cloned calls.
6085 if (NumClones)
6086 CloneFuncIfNeeded(NumClones, FS);
6087 }
6088
6089 else {
6090 // Consult the next callsite node.
6091 assert(SI != FS->callsites().end());
6092 auto &StackNode = *(SI++);
6093
6094#ifndef NDEBUG
6095 // Sanity check that the stack ids match between the summary and
6096 // instruction metadata.
6097 auto StackIdIndexIter = StackNode.StackIdIndices.begin();
6098 for (auto StackId : CallsiteContext) {
6099 assert(StackIdIndexIter != StackNode.StackIdIndices.end());
6100 assert(ImportSummary->getStackIdAtIndex(*StackIdIndexIter) ==
6101 StackId);
6102 StackIdIndexIter++;
6103 }
6104#endif
6105
6106 CloneCallsite(StackNode, CB, CalledFunction, FS);
6107 }
6108 } else if (CB->isTailCall() && CalledFunction) {
6109 // Locate the synthesized callsite info for the callee VI, if any was
6110 // created, and use that for cloning.
6111 ValueInfo CalleeVI =
6112 findValueInfoForFunc(*CalledFunction, M, ImportSummary, &F);
6113 if (CalleeVI && MapTailCallCalleeVIToCallsite.count(CalleeVI)) {
6114 auto Callsite = MapTailCallCalleeVIToCallsite.find(CalleeVI);
6115 assert(Callsite != MapTailCallCalleeVIToCallsite.end());
6116 CloneCallsite(Callsite->second, CB, CalledFunction, FS);
6117 }
6118 }
6119 }
6120 }
6121
6122 // Now do any promotion required for cloning.
6123 performICP(M, FS->callsites(), VMaps, ICallAnalysisInfo, ORE);
6124 }
6125
6126 // We skip some of the functions and instructions above, so remove all the
6127 // metadata in a single sweep here.
6128 for (auto &F : M) {
6129 // We can skip memprof clones because createFunctionClones already strips
6130 // the metadata from the newly created clones.
6131 if (F.isDeclaration() || isMemProfClone(F))
6132 continue;
6133 for (auto &BB : F) {
6134 for (auto &I : BB) {
6135 if (!isa<CallBase>(I))
6136 continue;
6137 I.setMetadata(LLVMContext::MD_memprof, nullptr);
6138 I.setMetadata(LLVMContext::MD_callsite, nullptr);
6139 }
6140 }
6141 }
6142
6143 return Changed;
6144}
6145
6146unsigned MemProfContextDisambiguation::recordICPInfo(
6147 CallBase *CB, ArrayRef<CallsiteInfo> AllCallsites,
6149 SmallVector<ICallAnalysisData> &ICallAnalysisInfo) {
6150 // First see if we have profile information for this indirect call.
6151 uint32_t NumCandidates;
6152 uint64_t TotalCount;
6153 auto CandidateProfileData =
6154 ICallAnalysis->getPromotionCandidatesForInstruction(
6155 CB, TotalCount, NumCandidates, MaxSummaryIndirectEdges);
6156 if (CandidateProfileData.empty())
6157 return 0;
6158
6159 // Iterate through all of the candidate profiled targets along with the
6160 // CallsiteInfo summary records synthesized for them when building the index,
6161 // and see if any are cloned and/or refer to clones.
6162 bool ICPNeeded = false;
6163 unsigned NumClones = 0;
6164 size_t CallsiteInfoStartIndex = std::distance(AllCallsites.begin(), SI);
6165 for (const auto &Candidate : CandidateProfileData) {
6166#ifndef NDEBUG
6167 auto CalleeValueInfo =
6168#endif
6169 ImportSummary->getValueInfo(Candidate.Value);
6170 // We might not have a ValueInfo if this is a distributed
6171 // ThinLTO backend and decided not to import that function.
6172 assert(!CalleeValueInfo || SI->Callee == CalleeValueInfo);
6173 assert(SI != AllCallsites.end());
6174 auto &StackNode = *(SI++);
6175 // See if any of the clones of the indirect callsite for this
6176 // profiled target should call a cloned version of the profiled
6177 // target. We only need to do the ICP here if so.
6178 ICPNeeded |= llvm::any_of(StackNode.Clones,
6179 [](unsigned CloneNo) { return CloneNo != 0; });
6180 // Every callsite in the same function should have been cloned the same
6181 // number of times.
6182 assert(!NumClones || NumClones == StackNode.Clones.size());
6183 NumClones = StackNode.Clones.size();
6184 }
6185 if (!ICPNeeded)
6186 return NumClones;
6187 // Save information for ICP, which is performed later to avoid messing up the
6188 // current function traversal.
6189 ICallAnalysisInfo.push_back({CB, CandidateProfileData.vec(), NumCandidates,
6190 TotalCount, CallsiteInfoStartIndex});
6191 return NumClones;
6192}
6193
6194void MemProfContextDisambiguation::performICP(
6195 Module &M, ArrayRef<CallsiteInfo> AllCallsites,
6196 ArrayRef<std::unique_ptr<ValueToValueMapTy>> VMaps,
6197 ArrayRef<ICallAnalysisData> ICallAnalysisInfo,
6198 OptimizationRemarkEmitter &ORE) {
6199 // Now do any promotion required for cloning. Specifically, for each
6200 // recorded ICP candidate (which was only recorded because one clone of that
6201 // candidate should call a cloned target), we perform ICP (speculative
6202 // devirtualization) for each clone of the callsite, and update its callee
6203 // to the appropriate clone. Note that the ICP compares against the original
6204 // version of the target, which is what is in the vtable.
6205 for (auto &Info : ICallAnalysisInfo) {
6206 auto *CB = Info.CB;
6207 auto CallsiteIndex = Info.CallsiteInfoStartIndex;
6208 auto TotalCount = Info.TotalCount;
6209 unsigned NumClones = 0;
6210 SmallVector<InstrProfValueData, 8> RemainingCandidates;
6211
6212 for (auto &Candidate : Info.CandidateProfileData) {
6213 auto &StackNode = AllCallsites[CallsiteIndex++];
6214
6215 // All calls in the same function must have the same number of clones.
6216 assert(!NumClones || NumClones == StackNode.Clones.size());
6217 NumClones = StackNode.Clones.size();
6218
6219 // See if the target is in the module. If it wasn't imported, it is
6220 // possible that this profile could have been collected on a different
6221 // target (or version of the code), and we need to be conservative
6222 // (similar to what is done in the ICP pass).
6223 Function *TargetFunction = Symtab->getFunction(Candidate.Value);
6224 if (TargetFunction == nullptr ||
6225 // Any ThinLTO global dead symbol removal should have already
6226 // occurred, so it should be safe to promote when the target is a
6227 // declaration.
6228 // TODO: Remove internal option once more fully tested.
6230 TargetFunction->isDeclaration())) {
6231 ORE.emit([&]() {
6232 return OptimizationRemarkMissed(DEBUG_TYPE, "UnableToFindTarget", CB)
6233 << "Memprof cannot promote indirect call: target with md5sum "
6234 << ore::NV("target md5sum", Candidate.Value) << " not found";
6235 });
6236 // FIXME: See if we can use the new declaration importing support to
6237 // at least get the declarations imported for this case. Hot indirect
6238 // targets should have been imported normally, however.
6239 RemainingCandidates.push_back(Candidate);
6240 continue;
6241 }
6242
6243 // Check if legal to promote
6244 const char *Reason = nullptr;
6245 if (!isLegalToPromote(*CB, TargetFunction, &Reason)) {
6246 ORE.emit([&]() {
6247 return OptimizationRemarkMissed(DEBUG_TYPE, "UnableToPromote", CB)
6248 << "Memprof cannot promote indirect call to "
6249 << ore::NV("TargetFunction", TargetFunction)
6250 << " with count of " << ore::NV("TotalCount", TotalCount)
6251 << ": " << Reason;
6252 });
6253 RemainingCandidates.push_back(Candidate);
6254 continue;
6255 }
6256
6257 assert(!isMemProfClone(*TargetFunction));
6258
6259 // Handle each call clone, applying ICP so that each clone directly
6260 // calls the specified callee clone, guarded by the appropriate ICP
6261 // check.
6262 CallBase *CBClone = CB;
6263 for (unsigned J = 0; J < NumClones; J++) {
6264 // If the VMap is empty, this clone was a duplicate of another and was
6265 // created as an alias or a declaration.
6266 if (J > 0 && VMaps[J - 1]->empty())
6267 continue;
6268 // Copy 0 is the original function.
6269 if (J > 0)
6270 CBClone = cast<CallBase>((*VMaps[J - 1])[CB]);
6271 // We do the promotion using the original name, so that the comparison
6272 // is against the name in the vtable. Then just below, change the new
6273 // direct call to call the cloned function.
6274 auto &DirectCall =
6275 pgo::promoteIndirectCall(*CBClone, TargetFunction, Candidate.Count,
6276 TotalCount, isSamplePGO, &ORE);
6277 auto *TargetToUse = TargetFunction;
6278 // Call original if this version calls the original version of its
6279 // callee.
6280 if (StackNode.Clones[J]) {
6281 TargetToUse =
6282 cast<Function>(M.getOrInsertFunction(
6283 getMemProfFuncName(TargetFunction->getName(),
6284 StackNode.Clones[J]),
6285 TargetFunction->getFunctionType())
6286 .getCallee());
6287 }
6288 DirectCall.setCalledFunction(TargetToUse);
6289 // During matching we generate synthetic VP metadata for indirect calls
6290 // not already having any, from the memprof profile's callee GUIDs. If
6291 // we subsequently promote and inline those callees, we currently lose
6292 // the ability to generate this synthetic VP metadata. Optionally apply
6293 // a noinline attribute to promoted direct calls, where the threshold is
6294 // set to capture synthetic VP metadata targets which get a count of 1.
6296 Candidate.Count < MemProfICPNoInlineThreshold)
6297 DirectCall.setIsNoInline();
6298 ORE.emit(OptimizationRemark(DEBUG_TYPE, "MemprofCall", CBClone)
6299 << ore::NV("Call", CBClone) << " in clone "
6300 << ore::NV("Caller", CBClone->getFunction())
6301 << " promoted and assigned to call function clone "
6302 << ore::NV("Callee", TargetToUse));
6303 }
6304
6305 // Update TotalCount (all clones should get same count above)
6306 TotalCount -= Candidate.Count;
6307 }
6308 // Adjust the MD.prof metadata for all clones, now that we have the new
6309 // TotalCount and the remaining candidates.
6310 CallBase *CBClone = CB;
6311 for (unsigned J = 0; J < NumClones; J++) {
6312 // If the VMap is empty, this clone was a duplicate of another and was
6313 // created as an alias or a declaration.
6314 if (J > 0 && VMaps[J - 1]->empty())
6315 continue;
6316 // Copy 0 is the original function.
6317 if (J > 0)
6318 CBClone = cast<CallBase>((*VMaps[J - 1])[CB]);
6319 // First delete the old one.
6320 CBClone->setMetadata(LLVMContext::MD_prof, nullptr);
6321 // If all promoted, we don't need the MD.prof metadata.
6322 // Otherwise we need update with the un-promoted records back.
6323 if (TotalCount != 0)
6324 annotateValueSite(M, *CBClone, RemainingCandidates, TotalCount,
6325 IPVK_IndirectCallTarget, Info.NumCandidates);
6326 }
6327 }
6328}
6329
6330template <typename DerivedCCG, typename FuncTy, typename CallTy>
6331bool CallsiteContextGraph<DerivedCCG, FuncTy, CallTy>::process(
6332 function_ref<void(StringRef, StringRef, const Twine &)> EmitRemark,
6333 bool AllowExtraAnalysis) {
6334 if (DumpCCG) {
6335 dbgs() << "CCG before cloning:\n";
6336 dbgs() << *this;
6337 }
6338 if (ExportToDot)
6339 exportToDot("postbuild");
6340
6341 if (VerifyCCG) {
6342 check();
6343 }
6344
6345 identifyClones();
6346
6347 if (VerifyCCG) {
6348 check();
6349 }
6350
6351 if (DumpCCG) {
6352 dbgs() << "CCG after cloning:\n";
6353 dbgs() << *this;
6354 }
6355 if (ExportToDot)
6356 exportToDot("cloned");
6357
6358 bool Changed = assignFunctions();
6359
6360 if (DumpCCG) {
6361 dbgs() << "CCG after assigning function clones:\n";
6362 dbgs() << *this;
6363 }
6364 if (ExportToDot)
6365 exportToDot("clonefuncassign");
6366
6367 if (MemProfReportHintedSizes || AllowExtraAnalysis)
6368 printTotalSizes(errs(), EmitRemark);
6369
6370 return Changed;
6371}
6372
6373bool MemProfContextDisambiguation::processModule(
6374 Module &M,
6375 llvm::function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter) {
6376
6377 // If we have an import summary, then the cloning decisions were made during
6378 // the thin link on the index. Apply them and return.
6379 if (ImportSummary)
6380 return applyImport(M);
6381
6382 // TODO: If/when other types of memprof cloning are enabled beyond just for
6383 // hot and cold, we will need to change this to individually control the
6384 // AllocationType passed to addStackNodesForMIB during CCG construction.
6385 // Note that we specifically check this after applying imports above, so that
6386 // the option isn't needed to be passed to distributed ThinLTO backend
6387 // clang processes, which won't necessarily have visibility into the linker
6388 // dependences. Instead the information is communicated from the LTO link to
6389 // the backends via the combined summary index.
6390 if (!SupportsHotColdNew)
6391 return false;
6392
6393 ModuleCallsiteContextGraph CCG(M, OREGetter);
6394 // TODO: Set up remarks for regular LTO. We need to decide what function to
6395 // use in the callback.
6396 return CCG.process();
6397}
6398
6400 const ModuleSummaryIndex *Summary, bool isSamplePGO)
6401 : ImportSummary(Summary), isSamplePGO(isSamplePGO) {
6402 // Check the dot graph printing options once here, to make sure we have valid
6403 // and expected combinations.
6404 if (DotGraphScope == DotScope::Alloc && !AllocIdForDot.getNumOccurrences())
6406 "-memprof-dot-scope=alloc requires -memprof-dot-alloc-id");
6408 !ContextIdForDot.getNumOccurrences())
6410 "-memprof-dot-scope=context requires -memprof-dot-context-id");
6411 if (DotGraphScope == DotScope::All && AllocIdForDot.getNumOccurrences() &&
6412 ContextIdForDot.getNumOccurrences())
6414 "-memprof-dot-scope=all can't have both -memprof-dot-alloc-id and "
6415 "-memprof-dot-context-id");
6416 if (ImportSummary) {
6417 // The MemProfImportSummary should only be used for testing ThinLTO
6418 // distributed backend handling via opt, in which case we don't have a
6419 // summary from the pass pipeline.
6421 return;
6422 }
6423 if (MemProfImportSummary.empty())
6424 return;
6425
6426 auto ReadSummaryFile =
6428 if (!ReadSummaryFile) {
6429 logAllUnhandledErrors(ReadSummaryFile.takeError(), errs(),
6430 "Error loading file '" + MemProfImportSummary +
6431 "': ");
6432 return;
6433 }
6434 auto ImportSummaryForTestingOrErr = getModuleSummaryIndex(**ReadSummaryFile);
6435 if (!ImportSummaryForTestingOrErr) {
6436 logAllUnhandledErrors(ImportSummaryForTestingOrErr.takeError(), errs(),
6437 "Error parsing file '" + MemProfImportSummary +
6438 "': ");
6439 return;
6440 }
6441 ImportSummaryForTesting = std::move(*ImportSummaryForTestingOrErr);
6442 ImportSummary = ImportSummaryForTesting.get();
6443}
6444
6447 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
6448 auto OREGetter = [&](Function *F) -> OptimizationRemarkEmitter & {
6449 return FAM.getResult<OptimizationRemarkEmitterAnalysis>(*F);
6450 };
6451 if (!processModule(M, OREGetter))
6452 return PreservedAnalyses::all();
6453 return PreservedAnalyses::none();
6454}
6455
6457 ModuleSummaryIndex &Index,
6459 isPrevailing,
6460 LLVMContext &Ctx,
6461 function_ref<void(StringRef, StringRef, const Twine &)> EmitRemark) {
6462 // TODO: If/when other types of memprof cloning are enabled beyond just for
6463 // hot and cold, we will need to change this to individually control the
6464 // AllocationType passed to addStackNodesForMIB during CCG construction.
6465 // The index was set from the option, so these should be in sync.
6466 assert(Index.withSupportsHotColdNew() == SupportsHotColdNew);
6467 if (!SupportsHotColdNew)
6468 return;
6469
6470 bool AllowExtraAnalysis =
6472
6473 IndexCallsiteContextGraph CCG(Index, isPrevailing);
6474 CCG.process(EmitRemark, AllowExtraAnalysis);
6475}
6476
6477// Strips MemProf attributes and metadata. Can be invoked by the pass pipeline
6478// when we don't have an index that has recorded that we are linking with
6479// allocation libraries containing the necessary APIs for downstream
6480// transformations.
6482 // The profile matcher applies hotness attributes directly for allocations,
6483 // and those will cause us to generate calls to the hot/cold interfaces
6484 // unconditionally. If supports-hot-cold-new was not enabled in the LTO
6485 // link then assume we don't want these calls (e.g. not linking with
6486 // the appropriate library, or otherwise trying to disable this behavior).
6487 bool Changed = false;
6488 for (auto &F : M) {
6489 for (auto &BB : F) {
6490 for (auto &I : BB) {
6491 auto *CI = dyn_cast<CallBase>(&I);
6492 if (!CI)
6493 continue;
6494 if (CI->hasFnAttr("memprof")) {
6495 CI->removeFnAttr("memprof");
6496 Changed = true;
6497 }
6498 if (!CI->hasMetadata(LLVMContext::MD_callsite)) {
6499 assert(!CI->hasMetadata(LLVMContext::MD_memprof));
6500 continue;
6501 }
6502 // Strip off all memprof metadata as it is no longer needed.
6503 // Importantly, this avoids the addition of new memprof attributes
6504 // after inlining propagation.
6505 CI->setMetadata(LLVMContext::MD_memprof, nullptr);
6506 CI->setMetadata(LLVMContext::MD_callsite, nullptr);
6507 Changed = true;
6508 }
6509 }
6510 }
6511 if (!Changed)
6512 return PreservedAnalyses::all();
6513 return PreservedAnalyses::none();
6514}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
unsigned uint64_t
Unify divergent function exit nodes
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
Machine Check Debug Module
This file implements a map that provides insertion order iteration.
static cl::opt< unsigned > TailCallSearchDepth("memprof-tail-call-search-depth", cl::init(5), cl::Hidden, cl::desc("Max depth to recursively search for missing " "frames through tail calls."))
uint64_t ComputeHash(const FunctionSummary *FS, unsigned I)
static cl::opt< DotScope > DotGraphScope("memprof-dot-scope", cl::desc("Scope of graph to export to dot"), cl::Hidden, cl::init(DotScope::All), cl::values(clEnumValN(DotScope::All, "all", "Export full callsite graph"), clEnumValN(DotScope::Alloc, "alloc", "Export only nodes with contexts feeding given " "-memprof-dot-alloc-id"), clEnumValN(DotScope::Context, "context", "Export only nodes with given -memprof-dot-context-id")))
static cl::opt< bool > DoMergeIteration("memprof-merge-iteration", cl::init(true), cl::Hidden, cl::desc("Iteratively apply merging on a node to catch new callers"))
static bool isMemProfClone(const Function &F)
static cl::opt< unsigned > AllocIdForDot("memprof-dot-alloc-id", cl::init(0), cl::Hidden, cl::desc("Id of alloc to export if -memprof-dot-scope=alloc " "or to highlight if -memprof-dot-scope=all"))
static cl::opt< unsigned > ContextIdForDot("memprof-dot-context-id", cl::init(0), cl::Hidden, cl::desc("Id of context to export if -memprof-dot-scope=context or to " "highlight otherwise"))
static cl::opt< bool > ExportToDot("memprof-export-to-dot", cl::init(false), cl::Hidden, cl::desc("Export graph to dot files."))
static void checkEdge(const std::shared_ptr< ContextEdge< DerivedCCG, FuncTy, CallTy > > &Edge)
static cl::opt< bool > AllowRecursiveCallsites("memprof-allow-recursive-callsites", cl::init(true), cl::Hidden, cl::desc("Allow cloning of callsites involved in recursive cycles"))
bool checkColdOrNotCold(uint8_t AllocType)
static ValueInfo findValueInfoForFunc(const Function &F, const Module &M, const ModuleSummaryIndex *ImportSummary, const Function *CallingFunc=nullptr)
static cl::opt< bool > CloneRecursiveContexts("memprof-clone-recursive-contexts", cl::init(true), cl::Hidden, cl::desc("Allow cloning of contexts through recursive cycles"))
static std::string getAllocTypeString(uint8_t AllocTypes)
bool DOTGraphTraits< constCallsiteContextGraph< DerivedCCG, FuncTy, CallTy > * >::DoHighlight
static unsigned getMemProfCloneNum(const Function &F)
static cl::opt< unsigned > MemProfICPNoInlineThreshold("memprof-icp-noinline-threshold", cl::init(0), cl::Hidden, cl::desc("Minimum absolute count for promoted target to be inlinable"))
static SmallVector< std::unique_ptr< ValueToValueMapTy >, 4 > createFunctionClones(Function &F, unsigned NumClones, Module &M, OptimizationRemarkEmitter &ORE, std::map< const Function *, SmallPtrSet< const GlobalAlias *, 1 > > &FuncToAliasMap, FunctionSummary *FS)
static cl::opt< bool > VerifyCCG("memprof-verify-ccg", cl::init(false), cl::Hidden, cl::desc("Perform verification checks on CallingContextGraph."))
static void checkNode(const ContextNode< DerivedCCG, FuncTy, CallTy > *Node, bool CheckEdges=true)
static cl::opt< bool > MergeClones("memprof-merge-clones", cl::init(true), cl::Hidden, cl::desc("Merge clones before assigning functions"))
static std::string getMemProfFuncName(Twine Base, unsigned CloneNo)
static cl::opt< std::string > MemProfImportSummary("memprof-import-summary", cl::desc("Import summary to use for testing the ThinLTO backend via opt"), cl::Hidden)
static const std::string MemProfCloneSuffix
static void updateSubprogramLinkageName(Function *NewFunc, StringRef Name)
static cl::opt< bool > AllowRecursiveContexts("memprof-allow-recursive-contexts", cl::init(true), cl::Hidden, cl::desc("Allow cloning of contexts having recursive cycles"))
static cl::opt< std::string > DotFilePathPrefix("memprof-dot-file-path-prefix", cl::init(""), cl::Hidden, cl::value_desc("filename"), cl::desc("Specify the path prefix of the MemProf dot files."))
static cl::opt< bool > VerifyNodes("memprof-verify-nodes", cl::init(false), cl::Hidden, cl::desc("Perform frequent verification checks on nodes."))
static void checkAllocContextIds(const AllocInfo &AllocNode, const MDNode *MemProfMD, const CallStack< MDNode, MDNode::op_iterator > &CallsiteContext, const ModuleSummaryIndex *ImportSummary)
static cl::opt< bool > DumpCCG("memprof-dump-ccg", cl::init(false), cl::Hidden, cl::desc("Dump CallingContextGraph to stdout after each stage."))
AllocType
This is the interface to build a ModuleSummaryIndex for a module.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
#define P(N)
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
FunctionAnalysisManager FAM
if(PassOpts->AAPipeline)
const char * Msg
std::pair< BasicBlock *, BasicBlock * > Edge
This file defines generic set operations that may be used on set's of different types,...
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
void print(OutputBuffer &OB) const
ValueInfo getAliaseeVI() const
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
const_pointer iterator
Definition ArrayRef.h:47
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
void addFnAttr(Attribute::AttrKind Kind)
Adds the attribute to the function.
void setCalledOperand(Value *V)
Subprogram description. Uses SubclassData1.
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:250
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:223
unsigned size() const
Definition DenseMap.h:172
bool empty() const
Definition DenseMap.h:171
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:219
iterator end()
Definition DenseMap.h:141
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:214
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:284
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:176
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
Function summary information to aid decisions and implementation of importing.
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:212
DISubprogram * getSubprogram() const
Get the attached subprogram.
const Function & getFunction() const
Definition Function.h:167
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:356
static LLVM_ABI GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition Globals.cpp:692
Function and variable summary information to aid decisions and implementation of importing.
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:80
static bool isLocalLinkage(LinkageTypes Linkage)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:408
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:158
static LLVM_ABI std::string getGlobalIdentifier(StringRef Name, GlobalValue::LinkageTypes Linkage, StringRef FileName)
Return the modified name for a global value suitable to be used as the key for a global lookup (e....
Definition Globals.cpp:234
bool isWeakForLinker() const
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Metadata node.
Definition Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1426
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1424
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1432
LLVM_ABI TempMDNode clone() const
Create a (temporary) clone of this.
Definition Metadata.cpp:688
static std::enable_if_t< std::is_base_of< MDNode, T >::value, T * > replaceWithUniqued(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a uniqued one.
Definition Metadata.h:1301
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:615
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
size_type count(const KeyT &Key) const
Definition MapVector.h:152
LLVM_ABI MemProfContextDisambiguation(const ModuleSummaryIndex *Summary=nullptr, bool isSamplePGO=false)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Class to hold module path string table and global value map, and encapsulate methods for operating on...
static StringRef getOriginalNameBeforePromote(StringRef Name)
Helper to obtain the unpromoted name for a global value (or the original name if not promoted).
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const
Return a ValueInfo for the index value_type (convenient when iterating index).
uint64_t getStackIdAtIndex(unsigned Index) const
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
LLVMContext & getContext() const
Get the global data context.
Definition Module.h:332
A NodeSet contains a set of SUnit DAG nodes with additional information that assigns a priority to th...
unsigned size() const
bool insert(SUnit *SU)
The optimization diagnostic interface.
bool allowExtraAnalysis(StringRef PassName) const
Whether we allow for extra compile-time budget to perform more analysis to produce fewer false positi...
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Diagnostic information for applied optimization remarks.
A discriminated union of two or more pointer types, with the discriminator in the low bits of the poi...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
A class that wrap the SHA1 algorithm.
Definition SHA1.h:27
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition SHA1.cpp:208
LLVM_ABI std::array< uint8_t, 20 > result()
Return the current raw 160-bits SHA1 for the digested data since the last call to init().
Definition SHA1.cpp:288
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:209
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:182
bool erase(const ValueT &V)
Definition DenseSet.h:97
void insert_range(Range &&R)
Definition DenseSet.h:235
size_type size() const
Definition DenseSet.h:84
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:187
void reserve(size_t Size)
Grow the DenseSet so that it can contain at least NumEntries items before resizing again.
Definition DenseSet.h:93
An efficient, type-erasing, non-owning reference to a callable.
Helper class to iterate through stack ids in both metadata (memprof MIB and callsite) and the corresp...
CallStackIterator beginAfterSharedPrefix(const CallStack &Other)
CallStackIterator end() const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Entry
Definition COFF.h:862
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:696
LLVM_ABI AllocationType getMIBAllocType(const MDNode *MIB)
Returns the allocation type from an MIB metadata node.
LLVM_ABI bool metadataMayIncludeContextSizeInfo()
Whether the alloc memprof metadata may include context size info for some MIBs (but possibly not all)...
LLVM_ABI bool hasSingleAllocType(uint8_t AllocTypes)
True if the AllocTypes bitmask contains just a single type.
LLVM_ABI std::string getAllocTypeAttributeString(AllocationType Type)
Returns the string to use in attributes with the given type.
LLVM_ABI MDNode * getMIBStackNode(const MDNode *MIB)
Returns the stack node from an MIB metadata node.
LLVM_ABI void removeAnyExistingAmbiguousAttribute(CallBase *CB)
Removes any existing "ambiguous" memprof attribute.
DiagnosticInfoOptimizationBase::Argument NV
LLVM_ABI CallBase & promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count, uint64_t TotalCount, bool AttachProfToDirectCall, OptimizationRemarkEmitter *ORE)
uint32_t NodeId
Definition RDFGraph.h:262
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
bool empty() const
Definition BasicBlock.h:101
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
uint64_t read64le(const void *P)
Definition Endian.h:415
void write32le(void *P, uint32_t V)
Definition Endian.h:455
This is an optimization pass for GlobalISel generic memory operations.
cl::opt< unsigned > MinClonedColdBytePercent("memprof-cloning-cold-threshold", cl::init(100), cl::Hidden, cl::desc("Min percent of cold bytes to hint alloc cold during cloning"))
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
LLVM_ABI void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition Error.cpp:61
void stable_sort(R &&Range)
Definition STLExtras.h:2116
cl::opt< bool > MemProfReportHintedSizes("memprof-report-hinted-sizes", cl::init(false), cl::Hidden, cl::desc("Report total allocation sizes of hinted allocations"))
LLVM_ABI bool isLegalToPromote(const CallBase &CB, Function *Callee, const char **FailureReason=nullptr)
Return true if the given indirect call site can be made to call Callee.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
void set_intersect(S1Ty &S1, const S2Ty &S2)
set_intersect(A, B) - Compute A := A ^ B Identical to set_intersection, except that it works on set<>...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool mayHaveMemprofSummary(const CallBase *CB)
Returns true if the instruction could have memprof metadata, used to ensure consistency between summa...
constexpr from_range_t from_range
static cl::opt< bool > MemProfRequireDefinitionForPromotion("memprof-require-definition-for-promotion", cl::init(false), cl::Hidden, cl::desc("Require target function definition when promoting indirect calls"))
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
cl::opt< unsigned > MemProfTopNImportant("memprof-top-n-important", cl::init(10), cl::Hidden, cl::desc("Number of largest cold contexts to consider important"))
bool set_is_subset(const S1Ty &S1, const S2Ty &S2)
set_is_subset(A, B) - Return true iff A in B
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
void set_subtract(S1Ty &S1, const S2Ty &S2)
set_subtract(A, B) - Compute A := A - B
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
bool set_intersects(const S1Ty &S1, const S2Ty &S2)
set_intersects(A, B) - Return true iff A ^ B is non empty
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
Definition STLExtras.h:1151
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI Expected< std::unique_ptr< ModuleSummaryIndex > > getModuleSummaryIndex(MemoryBufferRef Buffer)
Parse the specified bitcode buffer, returning the module summary index.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI void annotateValueSite(Module &M, Instruction &Inst, const InstrProfRecord &InstrProfR, InstrProfValueKind ValueKind, uint32_t SiteIndx, uint32_t MaxMDCount=3)
Get the value profile data for value site SiteIdx from InstrProfR and annotate the instruction Inst w...
cl::opt< unsigned > MaxSummaryIndirectEdges("module-summary-max-indirect-edges", cl::init(0), cl::Hidden, cl::desc("Max number of summary edges added from " "indirect call profile metadata"))
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
cl::opt< bool > SupportsHotColdNew
Indicate we are linking with an allocator that supports hot/cold operator new interfaces.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
S1Ty set_intersection(const S1Ty &S1, const S2Ty &S2)
set_intersection(A, B) - Return A ^ B
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
cl::opt< bool > EnableMemProfContextDisambiguation
Enable MemProf context disambiguation for thin link.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
S1Ty set_difference(const S1Ty &S1, const S2Ty &S2)
set_difference(A, B) - Return A - B
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr<T> to an Expected<T>.
Definition Error.h:1261
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1772
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
LLVM_ABI Function * CloneFunction(Function *F, ValueToValueMapTy &VMap, ClonedCodeInfo *CodeInfo=nullptr)
Return a copy of the specified function and add it to that function's module.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
cl::opt< bool > MemProfFixupImportant("memprof-fixup-important", cl::init(true), cl::Hidden, cl::desc("Enables edge fixup for important contexts"))
#define N
static std::string getEdgeAttributes(NodeRef, ChildIteratorType ChildIter, GraphType G)
static const ContextNode< DerivedCCG, FuncTy, CallTy > * GetCallee(const EdgePtrTy &P)
std::unique_ptr< ContextNode< DerivedCCG, FuncTy, CallTy > > NodePtrTy
mapped_iterator< typename std::vector< std::shared_ptr< ContextEdge< DerivedCCG, FuncTy, CallTy > > >::const_iterator, decltype(&GetCallee)> ChildIteratorType
mapped_iterator< typename std::vector< NodePtrTy >::const_iterator, decltype(&getNode)> nodes_iterator
std::shared_ptr< ContextEdge< DerivedCCG, FuncTy, CallTy > > EdgePtrTy
Summary of memprof metadata on allocations.
std::vector< MIBInfo > MIBs
SmallVector< unsigned > StackIdIndices
SmallVector< unsigned > Clones
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
DefaultDOTGraphTraits(bool simple=false)
An information struct used to provide DenseMap with the various necessary components for a given valu...
typename GraphType::UnknownGraphTypeError NodeRef
Definition GraphTraits.h:95
Struct that holds a reference to a particular GUID in a global value summary.
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
GlobalValue::GUID getGUID() const
PointerUnion< CallsiteInfo *, AllocInfo * > SimpleType
static SimpleType getSimplifiedValue(IndexCall &Val)
const PointerUnion< CallsiteInfo *, AllocInfo * > SimpleType
static SimpleType getSimplifiedValue(const IndexCall &Val)
Define a template that can be specialized by smart pointers to reflect the fact that they are automat...
Definition Casting.h:34