Skip to content

Commit c3d1518

Browse files
[memprof] Move YAML traits to MemProf.h (NFC) (#118668)
This patch moves the MemProf YAML traits to MemProf.h so that the YAML writer can access them from outside MemProfReader.cpp in the future.
1 parent 9c5217c commit c3d1518

File tree

2 files changed

+90
-91
lines changed

2 files changed

+90
-91
lines changed

llvm/include/llvm/ProfileData/MemProf.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/Support/Endian.h"
1313
#include "llvm/Support/EndianStream.h"
1414
#include "llvm/Support/HashBuilder.h"
15+
#include "llvm/Support/YAMLTraits.h"
1516
#include "llvm/Support/raw_ostream.h"
1617

1718
#include <bitset>
@@ -1145,6 +1146,95 @@ template <typename FrameIdTy> class CallStackRadixTreeBuilder {
11451146
}
11461147
};
11471148
} // namespace memprof
1149+
1150+
namespace yaml {
1151+
template <> struct MappingTraits<memprof::Frame> {
1152+
static void mapping(IO &Io, memprof::Frame &F) {
1153+
Io.mapRequired("Function", F.Function);
1154+
Io.mapRequired("LineOffset", F.LineOffset);
1155+
Io.mapRequired("Column", F.Column);
1156+
Io.mapRequired("Inline", F.IsInlineFrame);
1157+
1158+
// Assert that the definition of Frame matches what we expect. The
1159+
// structured bindings below detect changes to the number of fields.
1160+
// static_assert checks the type of each field.
1161+
const auto &[Function, SymbolName, LineOffset, Column, IsInlineFrame] = F;
1162+
static_assert(
1163+
std::is_same_v<remove_cvref_t<decltype(Function)>, GlobalValue::GUID>);
1164+
static_assert(std::is_same_v<remove_cvref_t<decltype(SymbolName)>,
1165+
std::unique_ptr<std::string>>);
1166+
static_assert(
1167+
std::is_same_v<remove_cvref_t<decltype(LineOffset)>, uint32_t>);
1168+
static_assert(std::is_same_v<remove_cvref_t<decltype(Column)>, uint32_t>);
1169+
static_assert(
1170+
std::is_same_v<remove_cvref_t<decltype(IsInlineFrame)>, bool>);
1171+
1172+
// MSVC issues unused variable warnings despite the uses in static_assert
1173+
// above.
1174+
(void)Function;
1175+
(void)SymbolName;
1176+
(void)LineOffset;
1177+
(void)Column;
1178+
(void)IsInlineFrame;
1179+
}
1180+
};
1181+
1182+
template <> struct CustomMappingTraits<memprof::PortableMemInfoBlock> {
1183+
static void inputOne(IO &Io, StringRef KeyStr,
1184+
memprof::PortableMemInfoBlock &MIB) {
1185+
// PortableMemInfoBlock keeps track of the set of fields that actually have
1186+
// values. We update the set here as we receive a key-value pair from the
1187+
// YAML document.
1188+
//
1189+
// We set MIB.Name via a temporary variable because ScalarTraits<uintptr_t>
1190+
// isn't available on macOS.
1191+
#define MIBEntryDef(NameTag, Name, Type) \
1192+
if (KeyStr == #Name) { \
1193+
uint64_t Value; \
1194+
Io.mapRequired(KeyStr.str().c_str(), Value); \
1195+
MIB.Name = static_cast<Type>(Value); \
1196+
MIB.Schema.set(llvm::to_underlying(memprof::Meta::Name)); \
1197+
return; \
1198+
}
1199+
#include "llvm/ProfileData/MIBEntryDef.inc"
1200+
#undef MIBEntryDef
1201+
Io.setError("Key is not a valid validation event");
1202+
}
1203+
1204+
static void output(IO &Io, memprof::PortableMemInfoBlock &VI) {
1205+
llvm_unreachable("To be implemented");
1206+
}
1207+
};
1208+
1209+
template <> struct MappingTraits<memprof::AllocationInfo> {
1210+
static void mapping(IO &Io, memprof::AllocationInfo &AI) {
1211+
Io.mapRequired("Callstack", AI.CallStack);
1212+
Io.mapRequired("MemInfoBlock", AI.Info);
1213+
}
1214+
};
1215+
1216+
// In YAML, we use GUIDMemProfRecordPair instead of MemProfRecord so that we can
1217+
// treat the GUID and the fields within MemProfRecord at the same level as if
1218+
// the GUID were part of MemProfRecord.
1219+
template <> struct MappingTraits<memprof::GUIDMemProfRecordPair> {
1220+
static void mapping(IO &Io, memprof::GUIDMemProfRecordPair &Pair) {
1221+
Io.mapRequired("GUID", Pair.GUID);
1222+
Io.mapRequired("AllocSites", Pair.Record.AllocSites);
1223+
Io.mapRequired("CallSites", Pair.Record.CallSites);
1224+
}
1225+
};
1226+
1227+
template <> struct MappingTraits<memprof::AllMemProfData> {
1228+
static void mapping(IO &Io, memprof::AllMemProfData &Data) {
1229+
Io.mapRequired("HeapProfileRecords", Data.HeapProfileRecords);
1230+
}
1231+
};
1232+
} // namespace yaml
11481233
} // namespace llvm
11491234

1235+
LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::Frame)
1236+
LLVM_YAML_IS_SEQUENCE_VECTOR(std::vector<memprof::Frame>)
1237+
LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::AllocationInfo)
1238+
LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::GUIDMemProfRecordPair)
1239+
11501240
#endif // LLVM_PROFILEDATA_MEMPROF_H_

llvm/lib/ProfileData/MemProfReader.cpp

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -41,97 +41,6 @@
4141

4242
#define DEBUG_TYPE "memprof"
4343

44-
namespace llvm {
45-
namespace yaml {
46-
template <> struct MappingTraits<memprof::Frame> {
47-
static void mapping(IO &Io, memprof::Frame &F) {
48-
Io.mapRequired("Function", F.Function);
49-
Io.mapRequired("LineOffset", F.LineOffset);
50-
Io.mapRequired("Column", F.Column);
51-
Io.mapRequired("Inline", F.IsInlineFrame);
52-
53-
// Assert that the definition of Frame matches what we expect. The
54-
// structured bindings below detect changes to the number of fields.
55-
// static_assert checks the type of each field.
56-
const auto &[Function, SymbolName, LineOffset, Column, IsInlineFrame] = F;
57-
static_assert(
58-
std::is_same_v<remove_cvref_t<decltype(Function)>, GlobalValue::GUID>);
59-
static_assert(std::is_same_v<remove_cvref_t<decltype(SymbolName)>,
60-
std::unique_ptr<std::string>>);
61-
static_assert(
62-
std::is_same_v<remove_cvref_t<decltype(LineOffset)>, uint32_t>);
63-
static_assert(std::is_same_v<remove_cvref_t<decltype(Column)>, uint32_t>);
64-
static_assert(
65-
std::is_same_v<remove_cvref_t<decltype(IsInlineFrame)>, bool>);
66-
67-
// MSVC issues unused variable warnings despite the uses in static_assert
68-
// above.
69-
(void)Function;
70-
(void)SymbolName;
71-
(void)LineOffset;
72-
(void)Column;
73-
(void)IsInlineFrame;
74-
}
75-
};
76-
77-
template <> struct CustomMappingTraits<memprof::PortableMemInfoBlock> {
78-
static void inputOne(IO &Io, StringRef KeyStr,
79-
memprof::PortableMemInfoBlock &MIB) {
80-
// PortableMemInfoBlock keeps track of the set of fields that actually have
81-
// values. We update the set here as we receive a key-value pair from the
82-
// YAML document.
83-
//
84-
// We set MIB.Name via a temporary variable because ScalarTraits<uintptr_t>
85-
// isn't available on macOS.
86-
#define MIBEntryDef(NameTag, Name, Type) \
87-
if (KeyStr == #Name) { \
88-
uint64_t Value; \
89-
Io.mapRequired(KeyStr.str().c_str(), Value); \
90-
MIB.Name = static_cast<Type>(Value); \
91-
MIB.Schema.set(llvm::to_underlying(memprof::Meta::Name)); \
92-
return; \
93-
}
94-
#include "llvm/ProfileData/MIBEntryDef.inc"
95-
#undef MIBEntryDef
96-
Io.setError("Key is not a valid validation event");
97-
}
98-
99-
static void output(IO &Io, memprof::PortableMemInfoBlock &VI) {
100-
llvm_unreachable("To be implemented");
101-
}
102-
};
103-
104-
template <> struct MappingTraits<memprof::AllocationInfo> {
105-
static void mapping(IO &Io, memprof::AllocationInfo &AI) {
106-
Io.mapRequired("Callstack", AI.CallStack);
107-
Io.mapRequired("MemInfoBlock", AI.Info);
108-
}
109-
};
110-
111-
// In YAML, we use GUIDMemProfRecordPair instead of MemProfRecord so that we can
112-
// treat the GUID and the fields within MemProfRecord at the same level as if
113-
// the GUID were part of MemProfRecord.
114-
template <> struct MappingTraits<memprof::GUIDMemProfRecordPair> {
115-
static void mapping(IO &Io, memprof::GUIDMemProfRecordPair &Pair) {
116-
Io.mapRequired("GUID", Pair.GUID);
117-
Io.mapRequired("AllocSites", Pair.Record.AllocSites);
118-
Io.mapRequired("CallSites", Pair.Record.CallSites);
119-
}
120-
};
121-
122-
template <> struct MappingTraits<memprof::AllMemProfData> {
123-
static void mapping(IO &Io, memprof::AllMemProfData &Data) {
124-
Io.mapRequired("HeapProfileRecords", Data.HeapProfileRecords);
125-
}
126-
};
127-
} // namespace yaml
128-
} // namespace llvm
129-
130-
LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::Frame)
131-
LLVM_YAML_IS_SEQUENCE_VECTOR(std::vector<memprof::Frame>)
132-
LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::AllocationInfo)
133-
LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::GUIDMemProfRecordPair)
134-
13544
namespace llvm {
13645
namespace memprof {
13746
namespace {

0 commit comments

Comments
 (0)