Skip to content

Commit 7c294eb

Browse files
[memprof] Simplify readMemprof (NFC) (#119930)
This patch essentially replaces: std::pair<const std::vector<Frame> *, unsigned> with: ArrayRef<Frame> This way, we can store and pass ArrayRef<Frame>, conceptually one item, instead of the pointer and index. The only problem is that we don't have an existing hash function for ArrayRef<Frame>>, so we provide a custom one, namely CallStackHash.
1 parent ca79ff0 commit 7c294eb

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

llvm/lib/Transforms/Instrumentation/MemProfiler.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,7 @@ static uint64_t computeStackId(const memprof::Frame &Frame) {
725725

726726
// Helper to generate a single hash id for a given callstack, used for emitting
727727
// matching statistics and useful for uniquing such statistics across modules.
728-
static uint64_t
729-
computeFullStackId(const std::vector<memprof::Frame> &CallStack) {
728+
static uint64_t computeFullStackId(ArrayRef<Frame> CallStack) {
730729
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
731730
HashBuilder;
732731
for (auto &F : CallStack)
@@ -763,9 +762,8 @@ static AllocationType addCallStack(CallStackTrie &AllocTrie,
763762
// non-zero.
764763
static bool
765764
stackFrameIncludesInlinedCallStack(ArrayRef<Frame> ProfileCallStack,
766-
ArrayRef<uint64_t> InlinedCallStack,
767-
unsigned StartIndex = 0) {
768-
auto StackFrame = ProfileCallStack.begin() + StartIndex;
765+
ArrayRef<uint64_t> InlinedCallStack) {
766+
auto StackFrame = ProfileCallStack.begin();
769767
auto InlCallStackIter = InlinedCallStack.begin();
770768
for (; StackFrame != ProfileCallStack.end() &&
771769
InlCallStackIter != InlinedCallStack.end();
@@ -969,9 +967,15 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
969967
// Build maps of the location hash to all profile data with that leaf location
970968
// (allocation info and the callsites).
971969
std::map<uint64_t, std::set<const AllocationInfo *>> LocHashToAllocInfo;
972-
// For the callsites we need to record the index of the associated frame in
973-
// the frame array (see comments below where the map entries are added).
974-
std::map<uint64_t, std::set<std::pair<const std::vector<Frame> *, unsigned>>>
970+
// A hash function for std::unordered_set<ArrayRef<Frame>> to work.
971+
struct CallStackHash {
972+
size_t operator()(ArrayRef<Frame> CS) const {
973+
return computeFullStackId(CS);
974+
}
975+
};
976+
// For the callsites we need to record slices of the frame array (see comments
977+
// below where the map entries are added).
978+
std::map<uint64_t, std::unordered_set<ArrayRef<Frame>, CallStackHash>>
975979
LocHashToCallSites;
976980
for (auto &AI : MemProfRec->AllocSites) {
977981
NumOfMemProfAllocContextProfiles++;
@@ -989,7 +993,7 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
989993
unsigned Idx = 0;
990994
for (auto &StackFrame : CS) {
991995
uint64_t StackId = computeStackId(StackFrame);
992-
LocHashToCallSites[StackId].insert(std::make_pair(&CS, Idx++));
996+
LocHashToCallSites[StackId].insert(ArrayRef<Frame>(CS).drop_front(Idx++));
993997
ProfileHasColumns |= StackFrame.Column;
994998
// Once we find this function, we can stop recording.
995999
if (StackFrame.Function == FuncGUID)
@@ -1029,8 +1033,7 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
10291033
// and another callsite).
10301034
std::map<uint64_t, std::set<const AllocationInfo *>>::iterator
10311035
AllocInfoIter;
1032-
std::map<uint64_t, std::set<std::pair<const std::vector<Frame> *,
1033-
unsigned>>>::iterator CallSitesIter;
1036+
decltype(LocHashToCallSites)::iterator CallSitesIter;
10341037
for (const DILocation *DIL = I.getDebugLoc(); DIL != nullptr;
10351038
DIL = DIL->getInlinedAt()) {
10361039
// Use C++ linkage name if possible. Need to compile with
@@ -1121,8 +1124,8 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
11211124
for (auto CallStackIdx : CallSitesIter->second) {
11221125
// If we found and thus matched all frames on the call, create and
11231126
// attach call stack metadata.
1124-
if (stackFrameIncludesInlinedCallStack(
1125-
*CallStackIdx.first, InlinedCallStack, CallStackIdx.second)) {
1127+
if (stackFrameIncludesInlinedCallStack(CallStackIdx,
1128+
InlinedCallStack)) {
11261129
NumOfMemProfMatchedCallSites++;
11271130
addCallsiteMetadata(I, InlinedCallStack, Ctx);
11281131
// Only need to find one with a matching call stack and add a single

0 commit comments

Comments
 (0)