Skip to content

[NFC][MemProf] Move IndexedMemProfData to its own header. #140503

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
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
70 changes: 68 additions & 2 deletions llvm/include/llvm/ProfileData/IndexedMemProfData.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,84 @@
//
//===----------------------------------------------------------------------===//
//
// MemProf data is serialized in writeMemProf provided in this header file.
// This file implements IndexedMemProfData, a data structure to hold MemProf
// in a space optimized format. It also provides utility methods for writing
// MemProf data.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_PROFILEDATA_INDEXEDMEMPROFDATA_H
#define LLVM_PROFILEDATA_INDEXEDMEMPROFDATA_H

#include "llvm/ProfileData/InstrProf.h"
#include "llvm/ProfileData/MemProf.h"

namespace llvm {
namespace memprof {
struct IndexedMemProfData {
// A map to hold memprof data per function. The lower 64 bits obtained from
// the md5 hash of the function name is used to index into the map.
llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> Records;

// A map to hold frame id to frame mappings. The mappings are used to
// convert IndexedMemProfRecord to MemProfRecords with frame information
// inline.
llvm::MapVector<FrameId, Frame> Frames;

// A map to hold call stack id to call stacks.
llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> CallStacks;

FrameId addFrame(const Frame &F) {
const FrameId Id = hashFrame(F);
Frames.try_emplace(Id, F);
return Id;
}

CallStackId addCallStack(ArrayRef<FrameId> CS) {
CallStackId CSId = hashCallStack(CS);
CallStacks.try_emplace(CSId, CS);
return CSId;
}

CallStackId addCallStack(SmallVector<FrameId> &&CS) {
CallStackId CSId = hashCallStack(CS);
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 {
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
HashBuilder;
for (FrameId F : CS)
HashBuilder.add(F);
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
CallStackId CSId;
std::memcpy(&CSId, Hash.data(), sizeof(Hash));
return CSId;
}
};
} // namespace memprof

// Write the MemProf data to OS.
Error writeMemProf(ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
memprof::IndexedVersion MemProfVersionRequested,
bool MemProfFullSchema);

} // namespace llvm
#endif
2 changes: 1 addition & 1 deletion llvm/include/llvm/ProfileData/InstrProfWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/Object/BuildID.h"
#include "llvm/ProfileData/IndexedMemProfData.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/ProfileData/MemProf.h"
#include "llvm/Support/Error.h"
#include <cstdint>
#include <memory>
Expand Down
51 changes: 0 additions & 51 deletions llvm/include/llvm/ProfileData/MemProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -842,57 +842,6 @@ struct LineLocation {

// A pair of a call site location and its corresponding callee GUID.
using CallEdgeTy = std::pair<LineLocation, uint64_t>;

struct IndexedMemProfData {
// A map to hold memprof data per function. The lower 64 bits obtained from
// the md5 hash of the function name is used to index into the map.
llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> Records;

// A map to hold frame id to frame mappings. The mappings are used to
// convert IndexedMemProfRecord to MemProfRecords with frame information
// inline.
llvm::MapVector<FrameId, Frame> Frames;

// A map to hold call stack id to call stacks.
llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> CallStacks;

FrameId addFrame(const Frame &F) {
const FrameId Id = hashFrame(F);
Frames.try_emplace(Id, F);
return Id;
}

CallStackId addCallStack(ArrayRef<FrameId> CS) {
CallStackId CSId = hashCallStack(CS);
CallStacks.try_emplace(CSId, CS);
return CSId;
}

CallStackId addCallStack(SmallVector<FrameId> &&CS) {
CallStackId CSId = hashCallStack(CS);
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;
};
} // namespace memprof
} // namespace llvm

Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/ProfileData/MemProfRadixTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_PROFILEDATA_MEMPROFRADIXTREE_H
#define LLVM_PROFILEDATA_MEMPROFRADIXTREE_H

#include "llvm/ProfileData/IndexedMemProfData.h"
#include "llvm/ProfileData/MemProf.h"

namespace llvm {
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/ProfileData/MemProfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm/IR/GlobalValue.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/ProfileData/IndexedMemProfData.h"
#include "llvm/ProfileData/InstrProfReader.h"
#include "llvm/ProfileData/MemProfData.inc"
#include "llvm/ProfileData/MemProfRadixTree.h"
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Object/IRSymtab.h"
#include "llvm/ProfileData/IndexedMemProfData.h"
#include "llvm/ProfileData/MemProf.h"
#include "llvm/ProfileData/MemProfRadixTree.h"
#include "llvm/Support/AtomicOrdering.h"
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/ProfileData/InstrProfWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "llvm/IR/ProfileSummary.h"
#include "llvm/ProfileData/IndexedMemProfData.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/ProfileData/MemProf.h"
#include "llvm/ProfileData/ProfileCommon.h"
#include "llvm/Support/Compression.h"
#include "llvm/Support/Endian.h"
Expand Down
13 changes: 0 additions & 13 deletions llvm/lib/ProfileData/MemProf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
#include "llvm/IR/Function.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/ProfileData/SampleProf.h"
#include "llvm/Support/BLAKE3.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/HashBuilder.h"

namespace llvm {
namespace memprof {
Expand Down Expand Up @@ -384,16 +382,5 @@ Expected<MemProfSchema> readMemProfSchema(const unsigned char *&Buffer) {
Buffer = Ptr;
return Result;
}

CallStackId IndexedMemProfData::hashCallStack(ArrayRef<FrameId> CS) const {
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
HashBuilder;
for (FrameId F : CS)
HashBuilder.add(F);
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
CallStackId CSId;
std::memcpy(&CSId, Hash.data(), sizeof(Hash));
return CSId;
}
} // namespace memprof
} // namespace llvm
1 change: 1 addition & 0 deletions llvm/unittests/ProfileData/InstrProfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/ProfileData/IndexedMemProfData.h"
#include "llvm/ProfileData/InstrProfReader.h"
#include "llvm/ProfileData/InstrProfWriter.h"
#include "llvm/ProfileData/MemProf.h"
Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/ProfileData/MemProfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
#include "llvm/IR/Value.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/ProfileData/IndexedMemProfData.h"
#include "llvm/ProfileData/MemProfData.inc"
#include "llvm/ProfileData/MemProfRadixTree.h"
#include "llvm/ProfileData/MemProfReader.h"
#include "llvm/ProfileData/MemProfYAML.h"
#include "llvm/Support/raw_ostream.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/ProfileData/IndexedMemProfData.h"
#include "llvm/ProfileData/InstrProfReader.h"
#include "llvm/ProfileData/InstrProfWriter.h"
#include "llvm/ProfileData/MemProf.h"
Expand Down
Loading