LLVM 20.0.0git
PGOCtxProfWriter.cpp
Go to the documentation of this file.
1//===- PGOCtxProfWriter.cpp - Contextual Instrumentation profile writer ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Write a contextual profile to bitstream.
10//
11//===----------------------------------------------------------------------===//
12
15
16using namespace llvm;
17using namespace llvm::ctx_profile;
18
20 raw_ostream &Out, std::optional<unsigned> VersionOverride)
21 : Writer(Out, 0) {
22 static_assert(ContainerMagic.size() == 4);
24 Writer.EnterBlockInfoBlock();
25 {
26 auto DescribeBlock = [&](unsigned ID, StringRef Name) {
30 llvm::arrayRefFromStringRef(Name));
31 };
33 auto DescribeRecord = [&](unsigned RecordID, StringRef Name) {
34 Data.clear();
35 Data.push_back(RecordID);
38 };
39 DescribeBlock(PGOCtxProfileBlockIDs::ProfileMetadataBlockID, "Metadata");
40 DescribeRecord(PGOCtxProfileRecords::Version, "Version");
41 DescribeBlock(PGOCtxProfileBlockIDs::ContextNodeBlockID, "Context");
42 DescribeRecord(PGOCtxProfileRecords::Guid, "GUID");
43 DescribeRecord(PGOCtxProfileRecords::CalleeIndex, "CalleeIndex");
44 DescribeRecord(PGOCtxProfileRecords::Counters, "Counters");
45 }
46 Writer.ExitBlock();
48 const auto Version = VersionOverride ? *VersionOverride : CurrentVersion;
51}
52
53void PGOCtxProfileWriter::writeCounters(const ContextNode &Node) {
56 Writer.EmitVBR(Node.counters_size(), VBREncodingBits);
57 for (uint32_t I = 0U; I < Node.counters_size(); ++I)
58 Writer.EmitVBR64(Node.counters()[I], VBREncodingBits);
59}
60
61// recursively write all the subcontexts. We do need to traverse depth first to
62// model the context->subcontext implicitly, and since this captures call
63// stacks, we don't really need to be worried about stack overflow and we can
64// keep the implementation simple.
65void PGOCtxProfileWriter::writeImpl(std::optional<uint32_t> CallerIndex,
66 const ContextNode &Node) {
70 if (CallerIndex)
72 SmallVector<uint64_t, 1>{*CallerIndex});
73 writeCounters(Node);
74 for (uint32_t I = 0U; I < Node.callsites_size(); ++I)
75 for (const auto *Subcontext = Node.subContexts()[I]; Subcontext;
76 Subcontext = Subcontext->next())
77 writeImpl(I, *Subcontext);
78 Writer.ExitBlock();
79}
80
82 writeImpl(std::nullopt, RootNode);
83}
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
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)
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.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write(unsigned char C)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ BLOCKINFO_CODE_BLOCKNAME
Definition: BitCodeEnums.h:82
@ BLOCKINFO_CODE_SETRECORDNAME
Definition: BitCodeEnums.h:83
@ BLOCKINFO_CODE_SETBID
Definition: BitCodeEnums.h:81
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2067
@ ContextNodeBlockID
@ ProfileMetadataBlockID