LLVM: lib/ProfileData/PGOCtxProfWriter.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
20
21using namespace llvm;
23
25 raw_ostream &Out, std::optional VersionOverride)
26 : Writer(Out, 0) {
30 {
35 llvm::arrayRefFromStringRef(Name));
36 };
38 auto DescribeRecord = [&](unsigned RecordID, StringRef Name) {
40 Data.push_back(RecordID);
43 };
50 }
56}
57
58void PGOCtxProfileWriter::writeCounters(const ContextNode &Node) {
62 for (uint32_t I = 0U; I < Node.counters_size(); ++I)
64}
65
66
67
68
69
70void PGOCtxProfileWriter::writeImpl(std::optional<uint32_t> CallerIndex,
75 if (CallerIndex)
78 writeCounters(Node);
80 for (const auto *Subcontext = Node.subContexts()[I]; Subcontext;
81 Subcontext = Subcontext->next())
82 writeImpl(I, *Subcontext);
84}
85
87 writeImpl(std::nullopt, RootNode);
88}
89
90namespace {
91
92
93
94struct SerializableCtxRepresentation {
96 std::vector<uint64_t> Counters;
97 std::vector<std::vector> Callsites;
98};
99
101createNode(std::vector<std::unique_ptr<char[]>> &Nodes,
102 const std::vector &DCList);
103
104
105
107createNode(std::vector<std::unique_ptr<char[]>> &Nodes,
108 const SerializableCtxRepresentation &DC,
111 DC.Callsites.size());
112 auto *Mem = Nodes.emplace_back(std::make_unique<char[]>(AllocSize)).get();
113 std::memset(Mem, 0, AllocSize);
115 DC.Callsites.size(), Next);
116 std::memcpy(Ret->counters(), DC.Counters.data(),
117 sizeof(uint64_t) * DC.Counters.size());
118 for (const auto &[I, DCList] : llvm::enumerate(DC.Callsites))
119 Ret->subContexts()[I] = createNode(Nodes, DCList);
120 return Ret;
121}
122
123
124
126createNode(std::vector<std::unique_ptr<char[]>> &Nodes,
127 const std::vector &DCList) {
129 for (const auto &DC : DCList)
130 List = createNode(Nodes, DC, List);
132}
133}
134
137template <> struct yaml::MappingTraits {
138 static void mapping(yaml::IO &IO, SerializableCtxRepresentation &SCR) {
139 IO.mapRequired("Guid", SCR.Guid);
140 IO.mapRequired("Counters", SCR.Counters);
141 IO.mapOptional("Callsites", SCR.Callsites);
142 }
143};
144
147 std::vector DCList;
148 In >> DCList;
149 if (In.error())
151 std::vector<std::unique_ptr<char[]>> Nodes;
152 std::error_code EC;
153 if (EC)
156 for (const auto &DC : DCList) {
157 auto *TopList = createNode(Nodes, DC);
158 if (!TopList)
160 "Unexpected error converting internal structure to ctx profile");
161 Writer.write(*TopList);
162 }
163 if (EC)
166}
#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)
void EmitRecord(unsigned Code, const Container &Vals, unsigned Abbrev=0)
EmitRecord - Emit the specified record to the stream, using an abbrev if we have one to compress the ...
void EmitCode(unsigned Val)
EmitCode - Emit the specified code.
void EmitVBR64(uint64_t Val, unsigned NumBits)
void EnterBlockInfoBlock()
EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
void EmitVBR(uint32_t Val, unsigned NumBits)
void EnterSubblock(unsigned BlockID, unsigned CodeLen)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Write one or more ContextNodes to the provided raw_fd_stream.
static constexpr unsigned VBREncodingBits
static constexpr uint32_t CurrentVersion
static constexpr StringRef ContainerMagic
PGOCtxProfileWriter(raw_ostream &Out, std::optional< unsigned > VersionOverride=std::nullopt)
void write(const ctx_profile::ContextNode &)
static constexpr unsigned CodeLen
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
static size_t getAllocSize(uint32_t NumCounters, uint32_t NumCallsites)
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & write(unsigned char C)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ BLOCKINFO_CODE_BLOCKNAME
@ BLOCKINFO_CODE_SETRECORDNAME
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Error createCtxProfFromYAML(StringRef Profile, raw_ostream &Out)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
static void mapping(yaml::IO &IO, SerializableCtxRepresentation &SCR)