[llvm] d3a6a05 - [SuffixTree] Add suffix tree statistics

Jessica Paquette via llvm-commits llvm-commits at lists.llvm.org
Fri May 12 17:05:40 PDT 2023


Author: Jessica Paquette
Date: 2023-05-12T17:05:04-07:00
New Revision: d3a6a05b1f95564f2c66f885a83cf0dbe1a004a9

URL: https://github.com/llvm/llvm-project/commit/d3a6a05b1f95564f2c66f885a83cf0dbe1a004a9
DIFF: https://github.com/llvm/llvm-project/commit/d3a6a05b1f95564f2c66f885a83cf0dbe1a004a9.diff

LOG: [SuffixTree] Add suffix tree statistics

Sometimes you want to see how much is being allocated in your data structure
in general.

Add statistics that show how many internal and leaf nodes have been allocated
in the suffix tree over the course of its construction.

Also add a testcase that shows that we actually get these stats out when we're
outlining stuff.

The test shows that we get the expected O(n) leaf nodes, a split, and so on.

Added: 
    llvm/test/CodeGen/AArch64/machine-outliner-stats.mir

Modified: 
    llvm/include/llvm/Support/SuffixTree.h
    llvm/lib/Support/SuffixTree.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/SuffixTree.h b/llvm/include/llvm/Support/SuffixTree.h
index 4940fbbf308d..27d551ad7858 100644
--- a/llvm/include/llvm/Support/SuffixTree.h
+++ b/llvm/include/llvm/Support/SuffixTree.h
@@ -33,10 +33,17 @@
 #define LLVM_SUPPORT_SUFFIXTREE_H
 
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/Statistic.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/SuffixTreeNode.h"
+#define DEBUG_TYPE "suffix-tree"
 
 namespace llvm {
+// Statistics for the number of nodes allocated. Useful for estimating tree
+// memory usage.
+STATISTIC(NumLeafNodesAllocated, "Leaf nodes allocated");
+STATISTIC(NumInternalNodesAllocated, "Internal nodes allocated");
+
 class SuffixTree {
 public:
   /// Each element is an integer representing an instruction in the module.
@@ -196,5 +203,5 @@ class SuffixTree {
 };
 
 } // namespace llvm
-
+#undef DEBUG_TYPE
 #endif // LLVM_SUPPORT_SUFFIXTREE_H

diff  --git a/llvm/lib/Support/SuffixTree.cpp b/llvm/lib/Support/SuffixTree.cpp
index eaa653078e09..03d4abd096c3 100644
--- a/llvm/lib/Support/SuffixTree.cpp
+++ b/llvm/lib/Support/SuffixTree.cpp
@@ -54,6 +54,7 @@ SuffixTreeNode *SuffixTree::insertLeaf(SuffixTreeInternalNode &Parent,
   auto *N = new (LeafNodeAllocator.Allocate())
       SuffixTreeLeafNode(StartIdx, &LeafEndIdx);
   Parent.Children[Edge] = N;
+  ++NumLeafNodesAllocated;
   return N;
 }
 
@@ -68,6 +69,7 @@ SuffixTree::insertInternalNode(SuffixTreeInternalNode *Parent,
       SuffixTreeInternalNode(StartIdx, EndIdx, Root);
   if (Parent)
     Parent->Children[Edge] = N;
+  ++NumInternalNodesAllocated;
   return N;
 }
 

diff  --git a/llvm/test/CodeGen/AArch64/machine-outliner-stats.mir b/llvm/test/CodeGen/AArch64/machine-outliner-stats.mir
new file mode 100644
index 000000000000..f8c69ff5388f
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/machine-outliner-stats.mir
@@ -0,0 +1,33 @@
+# RUN: llc %s -mtriple aarch64 -stats -run-pass=machine-outliner -o /dev/null 2>&1 | FileCheck %s
+# REQUIRES: asserts
+
+# Check that we can collect both outliner and suffix tree statistics.
+#
+# For the suffix tree, we should get
+# - 1 leaf per suffix
+# - 2 internal nodes, 1 for the root and 1 for the duplicate instruction
+
+# CHECK: 1 machine-outliner - Unoutlinable instructions mapped + number of sentinel values
+# CHECK-NEXT: 8 machine-outliner - Outlinable instructions mapped
+# CHECK-NEXT: 1 machine-outliner - Sentinel values inserted during mapping
+# CHECK-NEXT: 9 machine-outliner - Total number of instructions mapped and saved to mapping vector
+# CHECK-NEXT: 2 suffix-tree      - Internal nodes allocated
+# CHECK-NEXT: 9 suffix-tree      - Leaf nodes allocated
+
+...
+---
+name:           fn
+tracksRegLiveness: true
+machineFunctionInfo:
+  hasRedZone:      false
+body:             |
+  bb.0:
+    liveins: $x9
+    $x9 = ADDXri $x9, 16, 1
+    $x9 = ADDXri $x9, 16, 2
+    $x9 = ADDXri $x9, 16, 3
+    $x9 = ADDXri $x9, 16, 4
+    $x9 = ADDXri $x9, 16, 5
+    $x9 = ADDXri $x9, 16, 6
+    $x9 = ADDXri $x9, 16, 1
+    RET undef $x9


        


More information about the llvm-commits mailing list