Skip to content

[memprof] Move Frame::hash and hashCallStack to IndexedMemProfData (NFC) #120365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions llvm/include/llvm/ProfileData/MemProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,21 +323,6 @@ struct Frame {
<< " Column: " << Column << "\n"
<< " Inline: " << IsInlineFrame << "\n";
}

// Return a hash value based on the contents of the frame. Here we use a
// cryptographic hash function to minimize the chance of hash collisions. We
// do persist FrameIds as part of memprof formats up to Version 2, inclusive.
// However, the deserializer never calls this function; it uses FrameIds
// merely as keys to look up Frames proper.
inline FrameId hash() const {
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
HashBuilder;
HashBuilder.add(Function, LineOffset, Column, IsInlineFrame);
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
FrameId Id;
std::memcpy(&Id, Hash.data(), sizeof(Hash));
return Id;
}
};

// A type representing the index into the table of call stacks.
Expand Down Expand Up @@ -775,9 +760,6 @@ class CallStackLookupTrait {
}
};

// Compute a CallStackId for a given call stack.
CallStackId hashCallStack(ArrayRef<FrameId> CS);

namespace detail {
// "Dereference" the iterator from DenseMap or OnDiskChainedHashTable. We have
// to do so in one of two different ways depending on the type of the hash
Expand Down Expand Up @@ -1011,7 +993,7 @@ struct IndexedMemProfData {
llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> CallStacks;

FrameId addFrame(const Frame &F) {
const FrameId Id = F.hash();
const FrameId Id = hashFrame(F);
Frames.try_emplace(Id, F);
return Id;
}
Expand All @@ -1027,6 +1009,25 @@ struct IndexedMemProfData {
CallStacks.try_emplace(CSId, std::move(CS));
return CSId;
}

private:
// Return a hash value based on the contents of the frame. Here we use a
// cryptographic hash function to minimize the chance of hash collisions. We
// do persist FrameIds as part of memprof formats up to Version 2, inclusive.
// However, the deserializer never calls this function; it uses FrameIds
// merely as keys to look up Frames proper.
FrameId hashFrame(const Frame &F) const {
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
HashBuilder;
HashBuilder.add(F.Function, F.LineOffset, F.Column, F.IsInlineFrame);
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
FrameId Id;
std::memcpy(&Id, Hash.data(), sizeof(Hash));
return Id;
}

// Compute a CallStackId for a given call stack.
CallStackId hashCallStack(ArrayRef<FrameId> CS) const;
};

struct FrameStat {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/ProfileData/MemProf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ Expected<MemProfSchema> readMemProfSchema(const unsigned char *&Buffer) {
return Result;
}

CallStackId hashCallStack(ArrayRef<FrameId> CS) {
CallStackId IndexedMemProfData::hashCallStack(ArrayRef<FrameId> CS) const {
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
HashBuilder;
for (FrameId F : CS)
Expand Down
Loading