trie-node.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //===- trie-node.h - XRay Call Stack Data Structure -----------------------===//
  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 provides a data structure and routines for working with call stacks
  10. // of instrumented functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TOOLS_LLVM_XRAY_STACK_TRIE_H
  14. #define LLVM_TOOLS_LLVM_XRAY_STACK_TRIE_H
  15. #include <forward_list>
  16. #include <numeric>
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. /// A type to represent a trie of invocations. It is useful to construct a
  21. /// graph of these nodes from reading an XRay trace, such that each function
  22. /// call can be placed in a larger context.
  23. ///
  24. /// The template parameter allows users of the template to attach their own
  25. /// data elements to each node in the invocation graph.
  26. template <typename AssociatedData> struct TrieNode {
  27. /// The function ID.
  28. int32_t FuncId;
  29. /// The caller of this function.
  30. TrieNode<AssociatedData> *Parent;
  31. /// The callees from this function.
  32. llvm::SmallVector<TrieNode<AssociatedData> *, 4> Callees;
  33. /// Additional parameterized data on each node.
  34. AssociatedData ExtraData;
  35. };
  36. /// Merges together two TrieNodes with like function ids, aggregating their
  37. /// callee lists and durations. The caller must provide storage where new merged
  38. /// nodes can be allocated in the form of a linked list.
  39. template <typename T, typename Callable>
  40. TrieNode<T> *
  41. mergeTrieNodes(const TrieNode<T> &Left, const TrieNode<T> &Right,
  42. /*Non-deduced pointer type for nullptr compatibility*/
  43. std::remove_reference_t<TrieNode<T> *> NewParent,
  44. std::forward_list<TrieNode<T>> &NodeStore,
  45. Callable &&MergeCallable) {
  46. llvm::function_ref<T(const T &, const T &)> MergeFn(
  47. std::forward<Callable>(MergeCallable));
  48. assert(Left.FuncId == Right.FuncId);
  49. NodeStore.push_front(TrieNode<T>{
  50. Left.FuncId, NewParent, {}, MergeFn(Left.ExtraData, Right.ExtraData)});
  51. auto I = NodeStore.begin();
  52. auto *Node = &*I;
  53. // Build a map of callees from the left side.
  54. llvm::DenseMap<int32_t, TrieNode<T> *> LeftCalleesByFuncId;
  55. for (auto *Callee : Left.Callees) {
  56. LeftCalleesByFuncId[Callee->FuncId] = Callee;
  57. }
  58. // Iterate through the right side, either merging with the map values or
  59. // directly adding to the Callees vector. The iteration also removes any
  60. // merged values from the left side map.
  61. // TODO: Unroll into iterative and explicit stack for efficiency.
  62. for (auto *Callee : Right.Callees) {
  63. auto iter = LeftCalleesByFuncId.find(Callee->FuncId);
  64. if (iter != LeftCalleesByFuncId.end()) {
  65. Node->Callees.push_back(
  66. mergeTrieNodes(*(iter->second), *Callee, Node, NodeStore, MergeFn));
  67. LeftCalleesByFuncId.erase(iter);
  68. } else {
  69. Node->Callees.push_back(Callee);
  70. }
  71. }
  72. // Add any callees that weren't found in the right side.
  73. for (auto MapPairIter : LeftCalleesByFuncId) {
  74. Node->Callees.push_back(MapPairIter.second);
  75. }
  76. return Node;
  77. }
  78. #endif // LLVM_TOOLS_LLVM_XRAY_STACK_TRIE_H