Skip to content

Commit 61b636b

Browse files
committed
[NFC][MemProf] Move IndexedMemProfData to its own header.
1 parent 83ff2ba commit 61b636b

File tree

11 files changed

+75
-68
lines changed

11 files changed

+75
-68
lines changed

llvm/include/llvm/ProfileData/IndexedMemProfData.h

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,84 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// MemProf data is serialized in writeMemProf provided in this header file.
9+
// This file implements IndexedMemProfData, a data structure to hold MemProf
10+
// in a space optimized format. It also provides utility methods for writing
11+
// MemProf data.
1012
//
1113
//===----------------------------------------------------------------------===//
1214

15+
#ifndef LLVM_PROFILEDATA_INDEXEDMEMPROFDATA_H
16+
#define LLVM_PROFILEDATA_INDEXEDMEMPROFDATA_H
17+
1318
#include "llvm/ProfileData/InstrProf.h"
1419
#include "llvm/ProfileData/MemProf.h"
1520

1621
namespace llvm {
22+
namespace memprof {
23+
struct IndexedMemProfData {
24+
// A map to hold memprof data per function. The lower 64 bits obtained from
25+
// the md5 hash of the function name is used to index into the map.
26+
llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> Records;
27+
28+
// A map to hold frame id to frame mappings. The mappings are used to
29+
// convert IndexedMemProfRecord to MemProfRecords with frame information
30+
// inline.
31+
llvm::MapVector<FrameId, Frame> Frames;
32+
33+
// A map to hold call stack id to call stacks.
34+
llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> CallStacks;
35+
36+
FrameId addFrame(const Frame &F) {
37+
const FrameId Id = hashFrame(F);
38+
Frames.try_emplace(Id, F);
39+
return Id;
40+
}
41+
42+
CallStackId addCallStack(ArrayRef<FrameId> CS) {
43+
CallStackId CSId = hashCallStack(CS);
44+
CallStacks.try_emplace(CSId, CS);
45+
return CSId;
46+
}
47+
48+
CallStackId addCallStack(SmallVector<FrameId> &&CS) {
49+
CallStackId CSId = hashCallStack(CS);
50+
CallStacks.try_emplace(CSId, std::move(CS));
51+
return CSId;
52+
}
53+
54+
private:
55+
// Return a hash value based on the contents of the frame. Here we use a
56+
// cryptographic hash function to minimize the chance of hash collisions. We
57+
// do persist FrameIds as part of memprof formats up to Version 2, inclusive.
58+
// However, the deserializer never calls this function; it uses FrameIds
59+
// merely as keys to look up Frames proper.
60+
FrameId hashFrame(const Frame &F) const {
61+
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
62+
HashBuilder;
63+
HashBuilder.add(F.Function, F.LineOffset, F.Column, F.IsInlineFrame);
64+
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
65+
FrameId Id;
66+
std::memcpy(&Id, Hash.data(), sizeof(Hash));
67+
return Id;
68+
}
69+
70+
// Compute a CallStackId for a given call stack.
71+
CallStackId hashCallStack(ArrayRef<FrameId> CS) const {
72+
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
73+
HashBuilder;
74+
for (FrameId F : CS)
75+
HashBuilder.add(F);
76+
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
77+
CallStackId CSId;
78+
std::memcpy(&CSId, Hash.data(), sizeof(Hash));
79+
return CSId;
80+
}
81+
};
82+
} // namespace memprof
1783

1884
// Write the MemProf data to OS.
1985
Error writeMemProf(ProfOStream &OS, memprof::IndexedMemProfData &MemProfData,
2086
memprof::IndexedVersion MemProfVersionRequested,
2187
bool MemProfFullSchema);
22-
2388
} // namespace llvm
89+
#endif

llvm/include/llvm/ProfileData/InstrProfWriter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "llvm/IR/GlobalValue.h"
2121
#include "llvm/Object/BuildID.h"
2222
#include "llvm/ProfileData/InstrProf.h"
23-
#include "llvm/ProfileData/MemProf.h"
23+
#include "llvm/ProfileData/IndexedMemProfData.h"
2424
#include "llvm/Support/Error.h"
2525
#include <cstdint>
2626
#include <memory>

llvm/include/llvm/ProfileData/MemProf.h

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -842,57 +842,6 @@ struct LineLocation {
842842

843843
// A pair of a call site location and its corresponding callee GUID.
844844
using CallEdgeTy = std::pair<LineLocation, uint64_t>;
845-
846-
struct IndexedMemProfData {
847-
// A map to hold memprof data per function. The lower 64 bits obtained from
848-
// the md5 hash of the function name is used to index into the map.
849-
llvm::MapVector<GlobalValue::GUID, IndexedMemProfRecord> Records;
850-
851-
// A map to hold frame id to frame mappings. The mappings are used to
852-
// convert IndexedMemProfRecord to MemProfRecords with frame information
853-
// inline.
854-
llvm::MapVector<FrameId, Frame> Frames;
855-
856-
// A map to hold call stack id to call stacks.
857-
llvm::MapVector<CallStackId, llvm::SmallVector<FrameId>> CallStacks;
858-
859-
FrameId addFrame(const Frame &F) {
860-
const FrameId Id = hashFrame(F);
861-
Frames.try_emplace(Id, F);
862-
return Id;
863-
}
864-
865-
CallStackId addCallStack(ArrayRef<FrameId> CS) {
866-
CallStackId CSId = hashCallStack(CS);
867-
CallStacks.try_emplace(CSId, CS);
868-
return CSId;
869-
}
870-
871-
CallStackId addCallStack(SmallVector<FrameId> &&CS) {
872-
CallStackId CSId = hashCallStack(CS);
873-
CallStacks.try_emplace(CSId, std::move(CS));
874-
return CSId;
875-
}
876-
877-
private:
878-
// Return a hash value based on the contents of the frame. Here we use a
879-
// cryptographic hash function to minimize the chance of hash collisions. We
880-
// do persist FrameIds as part of memprof formats up to Version 2, inclusive.
881-
// However, the deserializer never calls this function; it uses FrameIds
882-
// merely as keys to look up Frames proper.
883-
FrameId hashFrame(const Frame &F) const {
884-
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
885-
HashBuilder;
886-
HashBuilder.add(F.Function, F.LineOffset, F.Column, F.IsInlineFrame);
887-
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
888-
FrameId Id;
889-
std::memcpy(&Id, Hash.data(), sizeof(Hash));
890-
return Id;
891-
}
892-
893-
// Compute a CallStackId for a given call stack.
894-
CallStackId hashCallStack(ArrayRef<FrameId> CS) const;
895-
};
896845
} // namespace memprof
897846
} // namespace llvm
898847

llvm/include/llvm/ProfileData/MemProfRadixTree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_PROFILEDATA_MEMPROFRADIXTREE_H
1515

1616
#include "llvm/ProfileData/MemProf.h"
17+
#include "llvm/ProfileData/IndexedMemProfData.h"
1718

1819
namespace llvm {
1920
namespace memprof {

llvm/include/llvm/ProfileData/MemProfReader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/IR/GlobalValue.h"
2222
#include "llvm/Object/Binary.h"
2323
#include "llvm/Object/ObjectFile.h"
24+
#include "llvm/ProfileData/IndexedMemProfData.h"
2425
#include "llvm/ProfileData/InstrProfReader.h"
2526
#include "llvm/ProfileData/MemProfData.inc"
2627
#include "llvm/ProfileData/MemProfRadixTree.h"

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "llvm/MC/TargetRegistry.h"
6363
#include "llvm/Object/IRSymtab.h"
6464
#include "llvm/ProfileData/MemProf.h"
65+
#include "llvm/ProfileData/IndexedMemProfData.h"
6566
#include "llvm/ProfileData/MemProfRadixTree.h"
6667
#include "llvm/Support/AtomicOrdering.h"
6768
#include "llvm/Support/Casting.h"

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "llvm/IR/ProfileSummary.h"
1919
#include "llvm/ProfileData/IndexedMemProfData.h"
2020
#include "llvm/ProfileData/InstrProf.h"
21-
#include "llvm/ProfileData/MemProf.h"
2221
#include "llvm/ProfileData/ProfileCommon.h"
2322
#include "llvm/Support/Compression.h"
2423
#include "llvm/Support/Endian.h"

llvm/lib/ProfileData/MemProf.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
#include "llvm/IR/Function.h"
44
#include "llvm/ProfileData/InstrProf.h"
55
#include "llvm/ProfileData/SampleProf.h"
6-
#include "llvm/Support/BLAKE3.h"
76
#include "llvm/Support/Endian.h"
87
#include "llvm/Support/EndianStream.h"
9-
#include "llvm/Support/HashBuilder.h"
108

119
namespace llvm {
1210
namespace memprof {
@@ -384,16 +382,5 @@ Expected<MemProfSchema> readMemProfSchema(const unsigned char *&Buffer) {
384382
Buffer = Ptr;
385383
return Result;
386384
}
387-
388-
CallStackId IndexedMemProfData::hashCallStack(ArrayRef<FrameId> CS) const {
389-
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
390-
HashBuilder;
391-
for (FrameId F : CS)
392-
HashBuilder.add(F);
393-
llvm::BLAKE3Result<8> Hash = HashBuilder.final();
394-
CallStackId CSId;
395-
std::memcpy(&CSId, Hash.data(), sizeof(Hash));
396-
return CSId;
397-
}
398385
} // namespace memprof
399386
} // namespace llvm

llvm/unittests/ProfileData/InstrProfTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ProfileData/InstrProfReader.h"
1616
#include "llvm/ProfileData/InstrProfWriter.h"
1717
#include "llvm/ProfileData/MemProf.h"
18+
#include "llvm/ProfileData/IndexedMemProfData.h"
1819
#include "llvm/ProfileData/MemProfRadixTree.h"
1920
#include "llvm/ProfileData/MemProfData.inc"
2021
#include "llvm/Support/Compression.h"

llvm/unittests/ProfileData/MemProfTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/IR/Value.h"
1515
#include "llvm/Object/ObjectFile.h"
1616
#include "llvm/ProfileData/MemProf.h"
17+
#include "llvm/ProfileData/IndexedMemProfData.h"
1718
#include "llvm/ProfileData/MemProfData.inc"
1819
#include "llvm/ProfileData/MemProfReader.h"
1920
#include "llvm/ProfileData/MemProfRadixTree.h"

llvm/unittests/Transforms/Instrumentation/MemProfUseTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/ProfileData/InstrProfReader.h"
1515
#include "llvm/ProfileData/InstrProfWriter.h"
1616
#include "llvm/ProfileData/MemProf.h"
17+
#include "llvm/ProfileData/IndexedMemProfData.h"
1718
#include "llvm/Support/SourceMgr.h"
1819
#include "llvm/Testing/Support/Error.h"
1920
#include "llvm/Transforms/Instrumentation/MemProfiler.h"

0 commit comments

Comments
 (0)