Skip to content

[memprof] Move YAML traits to MemProf.h (NFC) #118668

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 1 commit into from
Dec 4, 2024
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
90 changes: 90 additions & 0 deletions llvm/include/llvm/ProfileData/MemProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/Support/Endian.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/HashBuilder.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"

#include <bitset>
Expand Down Expand Up @@ -1145,6 +1146,95 @@ template <typename FrameIdTy> class CallStackRadixTreeBuilder {
}
};
} // namespace memprof

namespace yaml {
template <> struct MappingTraits<memprof::Frame> {
static void mapping(IO &Io, memprof::Frame &F) {
Io.mapRequired("Function", F.Function);
Io.mapRequired("LineOffset", F.LineOffset);
Io.mapRequired("Column", F.Column);
Io.mapRequired("Inline", F.IsInlineFrame);

// Assert that the definition of Frame matches what we expect. The
// structured bindings below detect changes to the number of fields.
// static_assert checks the type of each field.
const auto &[Function, SymbolName, LineOffset, Column, IsInlineFrame] = F;
static_assert(
std::is_same_v<remove_cvref_t<decltype(Function)>, GlobalValue::GUID>);
static_assert(std::is_same_v<remove_cvref_t<decltype(SymbolName)>,
std::unique_ptr<std::string>>);
static_assert(
std::is_same_v<remove_cvref_t<decltype(LineOffset)>, uint32_t>);
static_assert(std::is_same_v<remove_cvref_t<decltype(Column)>, uint32_t>);
static_assert(
std::is_same_v<remove_cvref_t<decltype(IsInlineFrame)>, bool>);

// MSVC issues unused variable warnings despite the uses in static_assert
// above.
(void)Function;
(void)SymbolName;
(void)LineOffset;
(void)Column;
(void)IsInlineFrame;
}
};

template <> struct CustomMappingTraits<memprof::PortableMemInfoBlock> {
static void inputOne(IO &Io, StringRef KeyStr,
memprof::PortableMemInfoBlock &MIB) {
// PortableMemInfoBlock keeps track of the set of fields that actually have
// values. We update the set here as we receive a key-value pair from the
// YAML document.
//
// We set MIB.Name via a temporary variable because ScalarTraits<uintptr_t>
// isn't available on macOS.
#define MIBEntryDef(NameTag, Name, Type) \
if (KeyStr == #Name) { \
uint64_t Value; \
Io.mapRequired(KeyStr.str().c_str(), Value); \
MIB.Name = static_cast<Type>(Value); \
MIB.Schema.set(llvm::to_underlying(memprof::Meta::Name)); \
return; \
}
#include "llvm/ProfileData/MIBEntryDef.inc"
#undef MIBEntryDef
Io.setError("Key is not a valid validation event");
}

static void output(IO &Io, memprof::PortableMemInfoBlock &VI) {
llvm_unreachable("To be implemented");
}
};

template <> struct MappingTraits<memprof::AllocationInfo> {
static void mapping(IO &Io, memprof::AllocationInfo &AI) {
Io.mapRequired("Callstack", AI.CallStack);
Io.mapRequired("MemInfoBlock", AI.Info);
}
};

// In YAML, we use GUIDMemProfRecordPair instead of MemProfRecord so that we can
// treat the GUID and the fields within MemProfRecord at the same level as if
// the GUID were part of MemProfRecord.
template <> struct MappingTraits<memprof::GUIDMemProfRecordPair> {
static void mapping(IO &Io, memprof::GUIDMemProfRecordPair &Pair) {
Io.mapRequired("GUID", Pair.GUID);
Io.mapRequired("AllocSites", Pair.Record.AllocSites);
Io.mapRequired("CallSites", Pair.Record.CallSites);
}
};

template <> struct MappingTraits<memprof::AllMemProfData> {
static void mapping(IO &Io, memprof::AllMemProfData &Data) {
Io.mapRequired("HeapProfileRecords", Data.HeapProfileRecords);
}
};
} // namespace yaml
} // namespace llvm

LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::Frame)
LLVM_YAML_IS_SEQUENCE_VECTOR(std::vector<memprof::Frame>)
LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::AllocationInfo)
LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::GUIDMemProfRecordPair)

#endif // LLVM_PROFILEDATA_MEMPROF_H_
91 changes: 0 additions & 91 deletions llvm/lib/ProfileData/MemProfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,97 +41,6 @@

#define DEBUG_TYPE "memprof"

namespace llvm {
namespace yaml {
template <> struct MappingTraits<memprof::Frame> {
static void mapping(IO &Io, memprof::Frame &F) {
Io.mapRequired("Function", F.Function);
Io.mapRequired("LineOffset", F.LineOffset);
Io.mapRequired("Column", F.Column);
Io.mapRequired("Inline", F.IsInlineFrame);

// Assert that the definition of Frame matches what we expect. The
// structured bindings below detect changes to the number of fields.
// static_assert checks the type of each field.
const auto &[Function, SymbolName, LineOffset, Column, IsInlineFrame] = F;
static_assert(
std::is_same_v<remove_cvref_t<decltype(Function)>, GlobalValue::GUID>);
static_assert(std::is_same_v<remove_cvref_t<decltype(SymbolName)>,
std::unique_ptr<std::string>>);
static_assert(
std::is_same_v<remove_cvref_t<decltype(LineOffset)>, uint32_t>);
static_assert(std::is_same_v<remove_cvref_t<decltype(Column)>, uint32_t>);
static_assert(
std::is_same_v<remove_cvref_t<decltype(IsInlineFrame)>, bool>);

// MSVC issues unused variable warnings despite the uses in static_assert
// above.
(void)Function;
(void)SymbolName;
(void)LineOffset;
(void)Column;
(void)IsInlineFrame;
}
};

template <> struct CustomMappingTraits<memprof::PortableMemInfoBlock> {
static void inputOne(IO &Io, StringRef KeyStr,
memprof::PortableMemInfoBlock &MIB) {
// PortableMemInfoBlock keeps track of the set of fields that actually have
// values. We update the set here as we receive a key-value pair from the
// YAML document.
//
// We set MIB.Name via a temporary variable because ScalarTraits<uintptr_t>
// isn't available on macOS.
#define MIBEntryDef(NameTag, Name, Type) \
if (KeyStr == #Name) { \
uint64_t Value; \
Io.mapRequired(KeyStr.str().c_str(), Value); \
MIB.Name = static_cast<Type>(Value); \
MIB.Schema.set(llvm::to_underlying(memprof::Meta::Name)); \
return; \
}
#include "llvm/ProfileData/MIBEntryDef.inc"
#undef MIBEntryDef
Io.setError("Key is not a valid validation event");
}

static void output(IO &Io, memprof::PortableMemInfoBlock &VI) {
llvm_unreachable("To be implemented");
}
};

template <> struct MappingTraits<memprof::AllocationInfo> {
static void mapping(IO &Io, memprof::AllocationInfo &AI) {
Io.mapRequired("Callstack", AI.CallStack);
Io.mapRequired("MemInfoBlock", AI.Info);
}
};

// In YAML, we use GUIDMemProfRecordPair instead of MemProfRecord so that we can
// treat the GUID and the fields within MemProfRecord at the same level as if
// the GUID were part of MemProfRecord.
template <> struct MappingTraits<memprof::GUIDMemProfRecordPair> {
static void mapping(IO &Io, memprof::GUIDMemProfRecordPair &Pair) {
Io.mapRequired("GUID", Pair.GUID);
Io.mapRequired("AllocSites", Pair.Record.AllocSites);
Io.mapRequired("CallSites", Pair.Record.CallSites);
}
};

template <> struct MappingTraits<memprof::AllMemProfData> {
static void mapping(IO &Io, memprof::AllMemProfData &Data) {
Io.mapRequired("HeapProfileRecords", Data.HeapProfileRecords);
}
};
} // namespace yaml
} // namespace llvm

LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::Frame)
LLVM_YAML_IS_SEQUENCE_VECTOR(std::vector<memprof::Frame>)
LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::AllocationInfo)
LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::GUIDMemProfRecordPair)

namespace llvm {
namespace memprof {
namespace {
Expand Down
Loading