Skip to content

Commit 7b8cf14

Browse files
[memprof] Update YAML traits for writer purposes (#118720)
For Frames, we prefer the inline notation for the brevity. For PortableMemInfoBlock, we go through all member fields and print out those that are populated.
1 parent f98c9a9 commit 7b8cf14

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

llvm/include/llvm/ProfileData/MemProf.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,10 @@ template <> struct MappingTraits<memprof::Frame> {
11771177
(void)Column;
11781178
(void)IsInlineFrame;
11791179
}
1180+
1181+
// Request the inline notation for brevity:
1182+
// { Function: 123, LineOffset: 11, Column: 10; IsInlineFrame: true }
1183+
static const bool flow = true;
11801184
};
11811185

11821186
template <> struct CustomMappingTraits<memprof::PortableMemInfoBlock> {
@@ -1201,8 +1205,13 @@ template <> struct CustomMappingTraits<memprof::PortableMemInfoBlock> {
12011205
Io.setError("Key is not a valid validation event");
12021206
}
12031207

1204-
static void output(IO &Io, memprof::PortableMemInfoBlock &VI) {
1205-
llvm_unreachable("To be implemented");
1208+
static void output(IO &Io, memprof::PortableMemInfoBlock &MIB) {
1209+
auto Schema = MIB.getSchema();
1210+
#define MIBEntryDef(NameTag, Name, Type) \
1211+
if (Schema.test(llvm::to_underlying(memprof::Meta::Name))) \
1212+
Io.mapRequired(#Name, MIB.Name);
1213+
#include "llvm/ProfileData/MIBEntryDef.inc"
1214+
#undef MIBEntryDef
12061215
}
12071216
};
12081217

llvm/unittests/ProfileData/MemProfTest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,4 +807,40 @@ TEST(MemProf, YAMLParser) {
807807
EXPECT_THAT(Record.CallSiteIds,
808808
ElementsAre(hashCallStack(CS3), hashCallStack(CS4)));
809809
}
810+
811+
template <typename T> std::string serializeInYAML(T &Val) {
812+
std::string Out;
813+
llvm::raw_string_ostream OS(Out);
814+
llvm::yaml::Output Yout(OS);
815+
Yout << Val;
816+
return Out;
817+
}
818+
819+
TEST(MemProf, YAMLWriterFrame) {
820+
Frame F(11, 22, 33, true);
821+
822+
std::string Out = serializeInYAML(F);
823+
EXPECT_EQ(Out, R"YAML(---
824+
{ Function: 11, LineOffset: 22, Column: 33, Inline: true }
825+
...
826+
)YAML");
827+
}
828+
829+
TEST(MemProf, YAMLWriterMIB) {
830+
MemInfoBlock MIB;
831+
MIB.AllocCount = 111;
832+
MIB.TotalSize = 222;
833+
MIB.TotalLifetime = 333;
834+
MIB.TotalLifetimeAccessDensity = 444;
835+
PortableMemInfoBlock PMIB(MIB, llvm::memprof::getHotColdSchema());
836+
837+
std::string Out = serializeInYAML(PMIB);
838+
EXPECT_EQ(Out, R"YAML(---
839+
AllocCount: 111
840+
TotalSize: 222
841+
TotalLifetime: 333
842+
TotalLifetimeAccessDensity: 444
843+
...
844+
)YAML");
845+
}
810846
} // namespace

0 commit comments

Comments
 (0)