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)
63 BitstreamWriter Writer;
64
65 void writeCounters(const ctx_profile::ContextNode &Node);
66 void writeImpl(std::optional<uint32_t> CallerIndex,
68
69public:
71 std::optional<unsigned> VersionOverride = std::nullopt)
72 : Writer(Out, 0) {
74 CodeLen);
75 const auto Version = VersionOverride ? *VersionOverride : CurrentVersion;
76 Writer.EmitRecord(PGOCtxProfileRecords::Version,
78 }
79
80 ~PGOCtxProfileWriter() { Writer.ExitBlock(); }
81
82 void write(const ctx_profile::ContextNode &);
83
84 // constants used in writing which a reader may find useful.
85 static constexpr unsigned CodeLen = 2;
86 static constexpr uint32_t CurrentVersion = 1;
87 static constexpr unsigned VBREncodingBits = 6;
88};
89
90} // namespace llvm
91#endif
Write one or more ContextNodes to the provided raw_fd_stream.
static constexpr unsigned VBREncodingBits
static constexpr uint32_t CurrentVersion
void write(const ctx_profile::ContextNode &)
static constexpr unsigned CodeLen
PGOCtxProfileWriter(raw_ostream &Out, std::optional< unsigned > VersionOverride=std::nullopt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ 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