[llvm] [memprof] Use llvm::equal in stackFrameIncludesInlinedCallStack (NFC) (PR #129372)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 1 00:08:06 PST 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/129372

llvm::equal hides all the iterator manipulation behind the scenes
while reducing the line count.


>From ca543b81f1fa2f3fc45e057361d9172f21a829ff Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 28 Feb 2025 23:12:33 -0800
Subject: [PATCH] [memprof] Use llvm::equal in
 stackFrameIncludesInlinedCallStack (NFC)

llvm::equal hides all the iterator manipulation behind the scenes
while reducing the line count.
---
 .../Instrumentation/MemProfiler.cpp           | 20 ++++++-------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp b/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
index c6352df39bba5..721f9ff953598 100644
--- a/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
@@ -771,23 +771,15 @@ static AllocationType addCallStack(CallStackTrie &AllocTrie,
 
 // Helper to compare the InlinedCallStack computed from an instruction's debug
 // info to a list of Frames from profile data (either the allocation data or a
-// callsite). For callsites, the StartIndex to use in the Frame array may be
-// non-zero.
+// callsite).
 static bool
 stackFrameIncludesInlinedCallStack(ArrayRef<Frame> ProfileCallStack,
                                    ArrayRef<uint64_t> InlinedCallStack) {
-  auto StackFrame = ProfileCallStack.begin();
-  auto InlCallStackIter = InlinedCallStack.begin();
-  for (; StackFrame != ProfileCallStack.end() &&
-         InlCallStackIter != InlinedCallStack.end();
-       ++StackFrame, ++InlCallStackIter) {
-    uint64_t StackId = computeStackId(*StackFrame);
-    if (StackId != *InlCallStackIter)
-      return false;
-  }
-  // Return true if we found and matched all stack ids from the call
-  // instruction.
-  return InlCallStackIter == InlinedCallStack.end();
+  return ProfileCallStack.size() >= InlinedCallStack.size() &&
+         llvm::equal(ProfileCallStack.take_front(InlinedCallStack.size()),
+                     InlinedCallStack, [](const Frame &F, uint64_t StackId) {
+                       return computeStackId(F) == StackId;
+                     });
 }
 
 static bool isAllocationWithHotColdVariant(const Function *Callee,



More information about the llvm-commits mailing list