22#define LLVM_PROFILEDATA_MEMPROFYAML_H_
33
44#include " llvm/ADT/SmallVector.h"
5+ #include " llvm/ProfileData/DataAccessProf.h"
56#include " llvm/ProfileData/MemProf.h"
67#include " llvm/Support/Format.h"
78#include " llvm/Support/YAMLTraits.h"
@@ -20,9 +21,24 @@ struct GUIDMemProfRecordPair {
2021 MemProfRecord Record;
2122};
2223
24+ // Helper struct to yamlify memprof::DataAccessProfData. The struct
25+ // members use owned strings. This is for simplicity and assumes that most real
26+ // world use cases do look-ups and regression test scale is small.
27+ struct YamlDataAccessProfData {
28+ std::vector<memprof::DataAccessProfRecord> Records;
29+ std::vector<uint64_t > KnownColdStrHashes;
30+ std::vector<std::string> KnownColdSymbols;
31+
32+ bool isEmpty () const {
33+ return Records.empty () && KnownColdStrHashes.empty () &&
34+ KnownColdSymbols.empty ();
35+ }
36+ };
37+
2338// The top-level data structure, only used with YAML for now.
2439struct AllMemProfData {
2540 std::vector<GUIDMemProfRecordPair> HeapProfileRecords;
41+ YamlDataAccessProfData YamlifiedDataAccessProfiles;
2642};
2743} // namespace memprof
2844
@@ -206,9 +222,52 @@ template <> struct MappingTraits<memprof::GUIDMemProfRecordPair> {
206222 }
207223};
208224
225+ template <> struct MappingTraits <memprof::SourceLocation> {
226+ static void mapping (IO &Io, memprof::SourceLocation &Loc) {
227+ Io.mapOptional (" FileName" , Loc.FileName );
228+ Io.mapOptional (" Line" , Loc.Line );
229+ }
230+ };
231+
232+ template <> struct MappingTraits <memprof::DataAccessProfRecord> {
233+ static void mapping (IO &Io, memprof::DataAccessProfRecord &Rec) {
234+ if (Io.outputting ()) {
235+ if (std::holds_alternative<std::string>(Rec.SymHandle )) {
236+ Io.mapOptional (" Symbol" , std::get<std::string>(Rec.SymHandle ));
237+ } else {
238+ Io.mapOptional (" Hash" , std::get<uint64_t >(Rec.SymHandle ));
239+ }
240+ } else {
241+ std::string SymName;
242+ uint64_t Hash = 0 ;
243+ Io.mapOptional (" Symbol" , SymName);
244+ Io.mapOptional (" Hash" , Hash);
245+ if (!SymName.empty ()) {
246+ Rec.SymHandle = SymName;
247+ } else {
248+ Rec.SymHandle = Hash;
249+ }
250+ }
251+
252+ Io.mapOptional (" Locations" , Rec.Locations );
253+ }
254+ };
255+
256+ template <> struct MappingTraits <memprof::YamlDataAccessProfData> {
257+ static void mapping (IO &Io, memprof::YamlDataAccessProfData &Data) {
258+ Io.mapOptional (" SampledRecords" , Data.Records );
259+ Io.mapOptional (" KnownColdSymbols" , Data.KnownColdSymbols );
260+ Io.mapOptional (" KnownColdStrHashes" , Data.KnownColdStrHashes );
261+ }
262+ };
263+
209264template <> struct MappingTraits <memprof::AllMemProfData> {
210265 static void mapping (IO &Io, memprof::AllMemProfData &Data) {
211266 Io.mapRequired (" HeapProfileRecords" , Data.HeapProfileRecords );
267+ // Map data access profiles if reading input, or if writing output &&
268+ // the struct is populated.
269+ if (!Io.outputting () || !Data.YamlifiedDataAccessProfiles .isEmpty ())
270+ Io.mapOptional (" DataAccessProfiles" , Data.YamlifiedDataAccessProfiles );
212271 }
213272};
214273
@@ -234,5 +293,7 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::AllocationInfo)
234293LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::CallSiteInfo)
235294LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::GUIDMemProfRecordPair)
236295LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::GUIDHex64) // Used for CalleeGuids
296+ LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::DataAccessProfRecord)
297+ LLVM_YAML_IS_SEQUENCE_VECTOR(memprof::SourceLocation)
237298
238299#endif // LLVM_PROFILEDATA_MEMPROFYAML_H_
0 commit comments