LLVM 19.0.0git
PGOCtxProfWriter.h
Go to the documentation of this file.
1//===- PGOCtxProfWriter.h - Contextual Profile Writer -----------*- C++ -*-===//
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// This file declares a utility for writing a contextual profile to bitstream.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_PROFILEDATA_PGOCTXPROFWRITER_H_
14#define LLVM_PROFILEDATA_PGOCTXPROFWRITER_H_
15
19
20namespace llvm {
22
26};
27
28/// Write one or more ContextNodes to the provided raw_fd_stream.
29/// The caller must destroy the PGOCtxProfileWriter object before closing the
30/// stream.
31/// The design allows serializing a bunch of contexts embedded in some other
32/// file. The overall format is:
33///
34/// [... other data written to the stream...]
35/// SubBlock(ProfileMetadataBlockID)
36/// Version
37/// SubBlock(ContextNodeBlockID)
38/// [RECORDS]
39/// SubBlock(ContextNodeBlockID)
40/// [RECORDS]
41/// [... more SubBlocks]
42/// EndBlock
43/// EndBlock
44///
45/// The "RECORDS" are bitsream records. The IDs are in CtxProfileCodes (except)
46/// for Version, which is just for metadata). All contexts will have Guid and
47/// Counters, and all but the roots have CalleeIndex. The order in which the
48/// records appear does not matter, but they must precede any subcontexts,
49/// because that helps keep the reader code simpler.
50///
51/// Subblock containment captures the context->subcontext relationship. The
52/// "next()" relationship in the raw profile, between call targets of indirect
53/// calls, are just modeled as peer subblocks where the callee index is the
54/// same.
55///
56/// Versioning: the writer may produce additional records not known by the
57/// reader. The version number indicates a more structural change.
58/// The current version, in particular, is set up to expect optional extensions
59/// like value profiling - which would appear as additional records. For
60/// example, value profiling would produce a new record with a new record ID,
61/// containing the profiled values (much like the counters)
64 BitstreamWriter Writer;
65
66 void writeCounters(const ctx_profile::ContextNode &Node);
67 void writeImpl(std::optional<uint32_t> CallerIndex,
69
70public:
72 std::optional<unsigned> VersionOverride = std::nullopt)
73 : Writer(Buff, &Out, 0) {
75 CodeLen);
76 const auto Version = VersionOverride ? *VersionOverride : CurrentVersion;
79 }
80
82
83 void write(const ctx_profile::ContextNode &);
84
85 // constants used in writing which a reader may find useful.
86 static constexpr unsigned CodeLen = 2;
87 static constexpr uint32_t CurrentVersion = 1;
88 static constexpr unsigned VBREncodingBits = 6;
89};
90
91} // namespace llvm
92#endif
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 EnterSubblock(unsigned BlockID, unsigned CodeLen)
Write one or more ContextNodes to the provided raw_fd_stream.
PGOCtxProfileWriter(raw_fd_stream &Out, std::optional< unsigned > VersionOverride=std::nullopt)
static constexpr unsigned VBREncodingBits
static constexpr uint32_t CurrentVersion
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
A raw_ostream of a file for reading/writing/seeking.
Definition: raw_ostream.h:627
@ FIRST_APPLICATION_BLOCKID
Definition: BitCodeEnums.h:72
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
PGOCtxProfileBlockIDs
@ ContextNodeBlockID
@ ProfileMetadataBlockID
PGOCtxProfileRecords