[llvm] [CGData] OutlinedHashTree (PR #89792)

Xuan Zhang via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 26 18:05:10 PDT 2024


================
@@ -0,0 +1,106 @@
+//===-- OutlinedHashTree.cpp ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// An OutlinedHashTree is a Trie that contains sequences of stable hash values
+// of instructions that have been outlined. This OutlinedHashTree can be used
+// to understand the outlined instruction sequences collected across modules.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/CodeGenData/OutlinedHashTree.h"
+
+#include <stack>
+#include <tuple>
+
+#define DEBUG_TYPE "outlined-hash-tree"
+
+using namespace llvm;
+
+void OutlinedHashTree::walkGraph(EdgeCallbackFn CallbackEdge,
+                                 NodeCallbackFn CallbackNode) const {
+  std::stack<const HashNode *> Stack;
+  Stack.push(getRoot());
+
+  while (!Stack.empty()) {
+    const auto *Current = Stack.top();
+    Stack.pop();
+    CallbackNode(Current);
+
----------------
xuanzh-meta wrote:

Can we have an additional parameter passed to the `walkGraph` function so that we only create and use `SortedSuccessors` when the order is important. For instance, for the `size()` and `depth()` functions, the order is not important but `SortedSuccessors` is created regardless. 

```
void OutlinedHashTree::walkGraph(EdgeCallbackFn CallbackEdge,
                                 NodeCallbackFn CallbackNode,
                                 bool SortedWalk = false) const {
  std::stack<const HashNode *> Stack;
  Stack.push(getRoot());

  while (!Stack.empty()) {
    const auto *Current = Stack.top();
    Stack.pop();
    if (CallbackEdge != nullptr)
      CallbackNode(Current);

    // Sorted walk for the stable output.
    if (SortedWalk) {
      std::map<stable_hash, const HashNode *> SortedSuccessors;
      for (const auto &P : Current->Successors)
        SortedSuccessors[P.first] = P.second.get();
    
      for (const auto &P : SortedSuccessors) {
        if (CallbackEdge != nullptr)
          CallbackEdge(Current, P.second);
        Stack.push(P.second);
      }
    } else {
      for (const auto &P : Current->Successors) {
        if (CallbackEdge != nullptr)
          CallbackEdge(Current, P.second.get());
        Stack.push(P.second.get());
      }
    }
  }
}
```

https://github.com/llvm/llvm-project/pull/89792


More information about the llvm-commits mailing list