[llvm] [memprof] Add CallStackRadixTreeBuilder (PR #93784)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 4 11:59:37 PDT 2024
================
@@ -410,6 +410,161 @@ CallStackId hashCallStack(ArrayRef<FrameId> CS) {
return CSId;
}
+// Returns the sorted list of call stacks.
+std::vector<CallStackRadixTreeBuilder::CSIdPair>
+CallStackRadixTreeBuilder::sortCallStacks(
+ llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>>
+ &MemProfCallStackData) {
+ // Create a list of call stacks to be sorted.
+ std::vector<CSIdPair> CallStacks;
+ CallStacks.reserve(MemProfCallStackData.size());
+ for (auto &[CSId, CallStack] : MemProfCallStackData) {
+ CallStacks.emplace_back(CSId, &CallStack);
+ }
+
+ // Sort the list of call stacks in the dictionary order to maximize the length
+ // of the common prefix between two adjacent call stacks.
+ auto LessThan = [&](const CSIdPair &L, const CSIdPair &R) {
+ // Call stacks are stored from leaf to root. Perform comparisons from the
+ // root.
+ return std::lexicographical_compare(
+ L.second->rbegin(), L.second->rend(), R.second->rbegin(),
+ R.second->rend(), [&](FrameId F1, FrameId F2) { return F1 < F2; });
+ };
+ llvm::sort(CallStacks, LessThan);
+
+ return CallStacks;
+}
+
+// Encode a call stack into RadixArray. Return the starting index within
+// RadixArray. For each call stack we encode, we emit two or three components
+// into RadixArray. If a given call stack doesn't have a common prefix relative
+// to the previous one, we emit:
+//
+// - the frames in the given call stack in the reverse order
----------------
kazutakahirata wrote:
Expanded comments.
https://github.com/llvm/llvm-project/pull/93784
More information about the llvm-commits
mailing list