142427995SKeith Wyss //===- trie-node.h - XRay Call Stack Data Structure -----------------------===//
242427995SKeith Wyss //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
642427995SKeith Wyss //
742427995SKeith Wyss //===----------------------------------------------------------------------===//
842427995SKeith Wyss //
942427995SKeith Wyss // This file provides a data structure and routines for working with call stacks
1042427995SKeith Wyss // of instrumented functions.
1142427995SKeith Wyss //
1242427995SKeith Wyss //===----------------------------------------------------------------------===//
1342427995SKeith Wyss
1442427995SKeith Wyss #ifndef LLVM_TOOLS_LLVM_XRAY_STACK_TRIE_H
1542427995SKeith Wyss #define LLVM_TOOLS_LLVM_XRAY_STACK_TRIE_H
1642427995SKeith Wyss
1742427995SKeith Wyss #include <forward_list>
1842427995SKeith Wyss #include <numeric>
1942427995SKeith Wyss
2042427995SKeith Wyss #include "llvm/ADT/DenseMap.h"
2142427995SKeith Wyss #include "llvm/ADT/STLExtras.h"
2242427995SKeith Wyss #include "llvm/ADT/SmallVector.h"
2342427995SKeith Wyss
2442427995SKeith Wyss /// A type to represent a trie of invocations. It is useful to construct a
2542427995SKeith Wyss /// graph of these nodes from reading an XRay trace, such that each function
2642427995SKeith Wyss /// call can be placed in a larger context.
2742427995SKeith Wyss ///
2842427995SKeith Wyss /// The template parameter allows users of the template to attach their own
2942427995SKeith Wyss /// data elements to each node in the invocation graph.
3042427995SKeith Wyss template <typename AssociatedData> struct TrieNode {
3142427995SKeith Wyss /// The function ID.
3242427995SKeith Wyss int32_t FuncId;
3342427995SKeith Wyss
3442427995SKeith Wyss /// The caller of this function.
3542427995SKeith Wyss TrieNode<AssociatedData> *Parent;
3642427995SKeith Wyss
3742427995SKeith Wyss /// The callees from this function.
3842427995SKeith Wyss llvm::SmallVector<TrieNode<AssociatedData> *, 4> Callees;
3942427995SKeith Wyss
4042427995SKeith Wyss /// Additional parameterized data on each node.
4142427995SKeith Wyss AssociatedData ExtraData;
4242427995SKeith Wyss };
4342427995SKeith Wyss
4442427995SKeith Wyss /// Merges together two TrieNodes with like function ids, aggregating their
4542427995SKeith Wyss /// callee lists and durations. The caller must provide storage where new merged
4642427995SKeith Wyss /// nodes can be allocated in the form of a linked list.
4742427995SKeith Wyss template <typename T, typename Callable>
4842427995SKeith Wyss TrieNode<T> *
mergeTrieNodes(const TrieNode<T> & Left,const TrieNode<T> & Right,std::remove_reference_t<TrieNode<T> * > NewParent,std::forward_list<TrieNode<T>> & NodeStore,Callable && MergeCallable)4942427995SKeith Wyss mergeTrieNodes(const TrieNode<T> &Left, const TrieNode<T> &Right,
5042427995SKeith Wyss /*Non-deduced pointer type for nullptr compatibility*/
51*1bd6123bSJustin Lebar std::remove_reference_t<TrieNode<T> *> NewParent,
5242427995SKeith Wyss std::forward_list<TrieNode<T>> &NodeStore,
5342427995SKeith Wyss Callable &&MergeCallable) {
5442427995SKeith Wyss llvm::function_ref<T(const T &, const T &)> MergeFn(
5542427995SKeith Wyss std::forward<Callable>(MergeCallable));
5642427995SKeith Wyss assert(Left.FuncId == Right.FuncId);
5742427995SKeith Wyss NodeStore.push_front(TrieNode<T>{
5842427995SKeith Wyss Left.FuncId, NewParent, {}, MergeFn(Left.ExtraData, Right.ExtraData)});
5942427995SKeith Wyss auto I = NodeStore.begin();
6042427995SKeith Wyss auto *Node = &*I;
6142427995SKeith Wyss
6242427995SKeith Wyss // Build a map of callees from the left side.
6342427995SKeith Wyss llvm::DenseMap<int32_t, TrieNode<T> *> LeftCalleesByFuncId;
6442427995SKeith Wyss for (auto *Callee : Left.Callees) {
6542427995SKeith Wyss LeftCalleesByFuncId[Callee->FuncId] = Callee;
6642427995SKeith Wyss }
6742427995SKeith Wyss
6842427995SKeith Wyss // Iterate through the right side, either merging with the map values or
6942427995SKeith Wyss // directly adding to the Callees vector. The iteration also removes any
7042427995SKeith Wyss // merged values from the left side map.
7142427995SKeith Wyss // TODO: Unroll into iterative and explicit stack for efficiency.
7242427995SKeith Wyss for (auto *Callee : Right.Callees) {
7342427995SKeith Wyss auto iter = LeftCalleesByFuncId.find(Callee->FuncId);
7442427995SKeith Wyss if (iter != LeftCalleesByFuncId.end()) {
7542427995SKeith Wyss Node->Callees.push_back(
7642427995SKeith Wyss mergeTrieNodes(*(iter->second), *Callee, Node, NodeStore, MergeFn));
7742427995SKeith Wyss LeftCalleesByFuncId.erase(iter);
7842427995SKeith Wyss } else {
7942427995SKeith Wyss Node->Callees.push_back(Callee);
8042427995SKeith Wyss }
8142427995SKeith Wyss }
8242427995SKeith Wyss
8342427995SKeith Wyss // Add any callees that weren't found in the right side.
8442427995SKeith Wyss for (auto MapPairIter : LeftCalleesByFuncId) {
8542427995SKeith Wyss Node->Callees.push_back(MapPairIter.second);
8642427995SKeith Wyss }
8742427995SKeith Wyss
8842427995SKeith Wyss return Node;
8942427995SKeith Wyss }
9042427995SKeith Wyss
9142427995SKeith Wyss #endif // LLVM_TOOLS_LLVM_XRAY_STACK_TRIE_H
92