1313#include " llvm/ProfileData/PGOCtxProfWriter.h"
1414#include " llvm/Bitstream/BitCodeEnums.h"
1515#include " llvm/ProfileData/CtxInstrContextNode.h"
16+ #include " llvm/Support/Error.h"
1617#include " llvm/Support/JSON.h"
18+ #include " llvm/Support/MemoryBuffer.h"
19+ #include " llvm/Support/YAMLTraits.h"
20+ #include " llvm/Support/raw_ostream.h"
1721
1822using namespace llvm ;
1923using namespace llvm ::ctx_profile;
@@ -85,22 +89,15 @@ void PGOCtxProfileWriter::write(const ContextNode &RootNode) {
8589}
8690
8791namespace {
88- // A structural representation of the JSON input.
89- struct DeserializableCtx {
90- ctx_profile::GUID Guid = 0 ;
91- std::vector<uint64_t > Counters;
92- std::vector<std::vector<DeserializableCtx>> Callsites;
93- };
94-
9592ctx_profile::ContextNode *
9693createNode (std::vector<std::unique_ptr<char []>> &Nodes,
97- const std::vector<DeserializableCtx > &DCList);
94+ const std::vector<SerializableCtxRepresentation > &DCList);
9895
9996// Convert a DeserializableCtx into a ContextNode, potentially linking it to
10097// its sibling (e.g. callee at same callsite) "Next".
10198ctx_profile::ContextNode *
10299createNode (std::vector<std::unique_ptr<char []>> &Nodes,
103- const DeserializableCtx &DC,
100+ const SerializableCtxRepresentation &DC,
104101 ctx_profile::ContextNode *Next = nullptr ) {
105102 auto AllocSize = ctx_profile::ContextNode::getAllocSize (DC.Counters .size (),
106103 DC.Callsites .size ());
@@ -115,38 +112,34 @@ createNode(std::vector<std::unique_ptr<char[]>> &Nodes,
115112 return Ret;
116113}
117114
118- // Convert a list of DeserializableCtx into a linked list of ContextNodes.
115+ // Convert a list of SerializableCtxRepresentation into a linked list of
116+ // ContextNodes.
119117ctx_profile::ContextNode *
120118createNode (std::vector<std::unique_ptr<char []>> &Nodes,
121- const std::vector<DeserializableCtx > &DCList) {
119+ const std::vector<SerializableCtxRepresentation > &DCList) {
122120 ctx_profile::ContextNode *List = nullptr ;
123121 for (const auto &DC : DCList)
124122 List = createNode (Nodes, DC, List);
125123 return List;
126124}
127125} // namespace
128126
129- namespace llvm {
130- namespace json {
131- bool fromJSON (const Value &E, DeserializableCtx &R, Path P) {
132- json::ObjectMapper Mapper (E, P);
133- return Mapper && Mapper.map (" Guid" , R.Guid ) &&
134- Mapper.map (" Counters" , R.Counters ) &&
135- Mapper.mapOptional (" Callsites" , R.Callsites );
136- }
137- } // namespace json
138- } // namespace llvm
139-
140- Error llvm::createCtxProfFromJSON (StringRef Profile, raw_ostream &Out) {
141- auto P = json::parse (Profile);
142- if (!P)
143- return P.takeError ();
127+ LLVM_YAML_IS_SEQUENCE_VECTOR (SerializableCtxRepresentation)
128+ LLVM_YAML_IS_SEQUENCE_VECTOR(std::vector<SerializableCtxRepresentation>)
129+ template <> struct yaml::MappingTraits<SerializableCtxRepresentation> {
130+ static void mapping (yaml::IO &IO, SerializableCtxRepresentation &SCR) {
131+ IO.mapRequired (" Guid" , SCR.Guid );
132+ IO.mapRequired (" Counters" , SCR.Counters );
133+ IO.mapOptional (" Callsites" , SCR.Callsites );
134+ }
135+ };
144136
145- json::Path::Root R (" " );
146- std::vector<DeserializableCtx> DCList;
147- if (!fromJSON (*P, DCList, R))
148- return R.getError ();
149- // Nodes provides memory backing for the ContextualNodes.
137+ Error llvm::createCtxProfFromYAML (StringRef Profile, raw_ostream &Out) {
138+ yaml::Input In (Profile);
139+ std::vector<SerializableCtxRepresentation> DCList;
140+ In >> DCList;
141+ if (In.error ())
142+ return createStringError (In.error (), " incorrect yaml content" );
150143 std::vector<std::unique_ptr<char []>> Nodes;
151144 std::error_code EC;
152145 if (EC)
@@ -162,4 +155,4 @@ Error llvm::createCtxProfFromJSON(StringRef Profile, raw_ostream &Out) {
162155 if (EC)
163156 return createStringError (EC, " failed to write output" );
164157 return Error::success ();
165- }
158+ }
0 commit comments