1313#include " llvm/ProfileData/PGOCtxProfWriter.h"
1414#include " llvm/Bitstream/BitCodeEnums.h"
1515#include " llvm/ProfileData/CtxInstrContextNode.h"
16+ #include " llvm/ProfileData/PGOCtxProfReader.h"
1617#include " llvm/Support/CommandLine.h"
1718#include " llvm/Support/Error.h"
1819#include " llvm/Support/YAMLTraits.h"
@@ -59,6 +60,11 @@ PGOCtxProfileWriter::PGOCtxProfileWriter(
5960 DescribeRecord (PGOCtxProfileRecords::Guid, " GUID" );
6061 DescribeRecord (PGOCtxProfileRecords::CalleeIndex, " CalleeIndex" );
6162 DescribeRecord (PGOCtxProfileRecords::Counters, " Counters" );
63+ DescribeBlock (PGOCtxProfileBlockIDs::FlatProfilesSectionBlockID,
64+ " FlatProfiles" );
65+ DescribeBlock (PGOCtxProfileBlockIDs::FlatProfileBlockID, " Flat" );
66+ DescribeRecord (PGOCtxProfileRecords::Guid, " GUID" );
67+ DescribeRecord (PGOCtxProfileRecords::Counters, " Counters" );
6268 }
6369 Writer.ExitBlock ();
6470 Writer.EnterSubblock (PGOCtxProfileBlockIDs::ProfileMetadataBlockID, CodeLen);
@@ -108,12 +114,27 @@ void PGOCtxProfileWriter::startContextSection() {
108114 Writer.EnterSubblock (PGOCtxProfileBlockIDs::ContextsSectionBlockID, CodeLen);
109115}
110116
117+ void PGOCtxProfileWriter::startFlatSection () {
118+ Writer.EnterSubblock (PGOCtxProfileBlockIDs::FlatProfilesSectionBlockID,
119+ CodeLen);
120+ }
121+
111122void PGOCtxProfileWriter::endContextSection () { Writer.ExitBlock (); }
123+ void PGOCtxProfileWriter::endFlatSection () { Writer.ExitBlock (); }
112124
113125void PGOCtxProfileWriter::writeContextual (const ContextNode &RootNode) {
114126 writeImpl (std::nullopt , RootNode);
115127}
116128
129+ void PGOCtxProfileWriter::writeFlatSection (ctx_profile::GUID Guid,
130+ const uint64_t *Buffer,
131+ size_t Size) {
132+ Writer.EnterSubblock (PGOCtxProfileBlockIDs::FlatProfileBlockID, CodeLen);
133+ writeGuid (Guid);
134+ writeCounters ({Buffer, Size});
135+ Writer.ExitBlock ();
136+ }
137+
117138namespace {
118139
119140// / Representation of the context node suitable for yaml serialization /
@@ -123,8 +144,13 @@ struct SerializableCtxRepresentation {
123144 std::vector<uint64_t > Counters;
124145 std::vector<std::vector<SerializableCtxRepresentation>> Callsites;
125146};
147+
148+ using SerializableFlatProfileRepresentation =
149+ std::pair<ctx_profile::GUID, std::vector<uint64_t >>;
150+
126151struct SerializableProfileRepresentation {
127152 std::vector<SerializableCtxRepresentation> Contexts;
153+ std::vector<SerializableFlatProfileRepresentation> FlatProfiles;
128154};
129155
130156ctx_profile::ContextNode *
@@ -164,6 +190,7 @@ createNode(std::vector<std::unique_ptr<char[]>> &Nodes,
164190
165191LLVM_YAML_IS_SEQUENCE_VECTOR (SerializableCtxRepresentation)
166192LLVM_YAML_IS_SEQUENCE_VECTOR(std::vector<SerializableCtxRepresentation>)
193+ LLVM_YAML_IS_SEQUENCE_VECTOR(SerializableFlatProfileRepresentation)
167194template <> struct yaml::MappingTraits<SerializableCtxRepresentation> {
168195 static void mapping (yaml::IO &IO, SerializableCtxRepresentation &SCR) {
169196 IO.mapRequired (" Guid" , SCR.Guid );
@@ -175,6 +202,15 @@ template <> struct yaml::MappingTraits<SerializableCtxRepresentation> {
175202template <> struct yaml ::MappingTraits<SerializableProfileRepresentation> {
176203 static void mapping (yaml::IO &IO, SerializableProfileRepresentation &SPR) {
177204 IO.mapOptional (" Contexts" , SPR.Contexts );
205+ IO.mapOptional (" FlatProfiles" , SPR.FlatProfiles );
206+ }
207+ };
208+
209+ template <> struct yaml ::MappingTraits<SerializableFlatProfileRepresentation> {
210+ static void mapping (yaml::IO &IO,
211+ SerializableFlatProfileRepresentation &SFPR) {
212+ IO.mapRequired (" Guid" , SFPR.first );
213+ IO.mapRequired (" Counters" , SFPR.second );
178214 }
179215};
180216
@@ -201,6 +237,12 @@ Error llvm::createCtxProfFromYAML(StringRef Profile, raw_ostream &Out) {
201237 }
202238 Writer.endContextSection ();
203239 }
240+ if (!SPR.FlatProfiles .empty ()) {
241+ Writer.startFlatSection ();
242+ for (const auto &[Guid, Counters] : SPR.FlatProfiles )
243+ Writer.writeFlatSection (Guid, Counters.data (), Counters.size ());
244+ Writer.endFlatSection ();
245+ }
204246 if (EC)
205247 return createStringError (EC, " failed to write output" );
206248 return Error::success ();
0 commit comments