Path: blob/main/contrib/llvm-project/llvm/tools/llvm-xray/trie-node.h
35231 views
//===- trie-node.h - XRay Call Stack Data Structure -----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file provides a data structure and routines for working with call stacks9// of instrumented functions.10//11//===----------------------------------------------------------------------===//1213#ifndef LLVM_TOOLS_LLVM_XRAY_STACK_TRIE_H14#define LLVM_TOOLS_LLVM_XRAY_STACK_TRIE_H1516#include <forward_list>17#include <numeric>1819#include "llvm/ADT/DenseMap.h"20#include "llvm/ADT/STLExtras.h"21#include "llvm/ADT/SmallVector.h"2223/// A type to represent a trie of invocations. It is useful to construct a24/// graph of these nodes from reading an XRay trace, such that each function25/// call can be placed in a larger context.26///27/// The template parameter allows users of the template to attach their own28/// data elements to each node in the invocation graph.29template <typename AssociatedData> struct TrieNode {30/// The function ID.31int32_t FuncId;3233/// The caller of this function.34TrieNode<AssociatedData> *Parent;3536/// The callees from this function.37llvm::SmallVector<TrieNode<AssociatedData> *, 4> Callees;3839/// Additional parameterized data on each node.40AssociatedData ExtraData;41};4243/// Merges together two TrieNodes with like function ids, aggregating their44/// callee lists and durations. The caller must provide storage where new merged45/// nodes can be allocated in the form of a linked list.46template <typename T, typename Callable>47TrieNode<T> *48mergeTrieNodes(const TrieNode<T> &Left, const TrieNode<T> &Right,49/*Non-deduced pointer type for nullptr compatibility*/50std::remove_reference_t<TrieNode<T> *> NewParent,51std::forward_list<TrieNode<T>> &NodeStore,52Callable &&MergeCallable) {53llvm::function_ref<T(const T &, const T &)> MergeFn(54std::forward<Callable>(MergeCallable));55assert(Left.FuncId == Right.FuncId);56NodeStore.push_front(TrieNode<T>{57Left.FuncId, NewParent, {}, MergeFn(Left.ExtraData, Right.ExtraData)});58auto I = NodeStore.begin();59auto *Node = &*I;6061// Build a map of callees from the left side.62llvm::DenseMap<int32_t, TrieNode<T> *> LeftCalleesByFuncId;63for (auto *Callee : Left.Callees) {64LeftCalleesByFuncId[Callee->FuncId] = Callee;65}6667// Iterate through the right side, either merging with the map values or68// directly adding to the Callees vector. The iteration also removes any69// merged values from the left side map.70// TODO: Unroll into iterative and explicit stack for efficiency.71for (auto *Callee : Right.Callees) {72auto iter = LeftCalleesByFuncId.find(Callee->FuncId);73if (iter != LeftCalleesByFuncId.end()) {74Node->Callees.push_back(75mergeTrieNodes(*(iter->second), *Callee, Node, NodeStore, MergeFn));76LeftCalleesByFuncId.erase(iter);77} else {78Node->Callees.push_back(Callee);79}80}8182// Add any callees that weren't found in the right side.83for (auto MapPairIter : LeftCalleesByFuncId) {84Node->Callees.push_back(MapPairIter.second);85}8687return Node;88}8990#endif // LLVM_TOOLS_LLVM_XRAY_STACK_TRIE_H919293