LLVM 22.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
21
22namespace llvm {
30};
31
42};
43
44/// Write one or more ContextNodes to the provided raw_fd_stream.
45/// The caller must destroy the PGOCtxProfileWriter object before closing the
46/// stream.
47/// The design allows serializing a bunch of contexts embedded in some other
48/// file. The overall format is:
49///
50/// [... other data written to the stream...]
51/// SubBlock(ProfileMetadataBlockID)
52/// Version
53/// SubBlock(ContextNodeBlockID)
54/// [RECORDS]
55/// SubBlock(ContextNodeBlockID)
56/// [RECORDS]
57/// [... more SubBlocks]
58/// EndBlock
59/// EndBlock
60///
61/// The "RECORDS" are bitsream records. The IDs are in CtxProfileCodes (except)
62/// for Version, which is just for metadata). All contexts will have Guid and
63/// Counters, and all but the roots have CalleeIndex. The order in which the
64/// records appear does not matter, but they must precede any subcontexts,
65/// because that helps keep the reader code simpler.
66///
67/// Subblock containment captures the context->subcontext relationship. The
68/// "next()" relationship in the raw profile, between call targets of indirect
69/// calls, are just modeled as peer subblocks where the callee index is the
70/// same.
71///
72/// Versioning: the writer may produce additional records not known by the
73/// reader. The version number indicates a more structural change.
74/// The current version, in particular, is set up to expect optional extensions
75/// like value profiling - which would appear as additional records. For
76/// example, value profiling would produce a new record with a new record ID,
77/// containing the profiled values (much like the counters)
79 enum class EmptyContextCriteria { None, EntryIsZero, AllAreZero };
80
81 BitstreamWriter Writer;
82 const bool IncludeEmpty;
83
84 void writeGuid(ctx_profile::GUID Guid);
85 void writeCallsiteIndex(uint32_t Index);
86 void writeRootEntryCount(uint64_t EntryCount);
87 void writeCounters(ArrayRef<uint64_t> Counters);
88 void writeNode(uint32_t CallerIndex, const ctx_profile::ContextNode &Node);
89 void writeSubcontexts(const ctx_profile::ContextNode &Node);
90
91public:
93 std::optional<unsigned> VersionOverride = std::nullopt,
94 bool IncludeEmpty = false);
96
97 void startContextSection() override;
98 void writeContextual(const ctx_profile::ContextNode &RootNode,
99 const ctx_profile::ContextNode *Unhandled,
100 uint64_t TotalRootEntryCount) override;
101 void endContextSection() override;
102
103 void startFlatSection() override;
104 void writeFlat(ctx_profile::GUID Guid, const uint64_t *Buffer,
105 size_t BufferSize) override;
106 void endFlatSection() override;
107
108 // constants used in writing which a reader may find useful.
109 static constexpr unsigned CodeLen = 2;
110 static constexpr uint32_t CurrentVersion = 4;
111 static constexpr unsigned VBREncodingBits = 6;
112 static constexpr StringRef ContainerMagic = "CTXP";
113};
114
116} // namespace llvm
117#endif
#define LLVM_ABI
Definition: Compiler.h:213
uint32_t Index
Load MIR Sample Profile
This file contains some functions that are useful when dealing with strings.
@ None
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Write one or more ContextNodes to the provided raw_fd_stream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Abstraction for the parameter passed to __llvm_ctx_profile_fetch.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
@ FIRST_APPLICATION_BLOCKID
Definition: BitCodeEnums.h:72
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI Error createCtxProfFromYAML(StringRef Profile, raw_ostream &Out)
PGOCtxProfileBlockIDs
@ ContextNodeBlockID
@ ContextsSectionBlockID
@ ContextRootBlockID
@ FlatProfilesSectionBlockID
@ UnhandledBlockID
@ FlatProfileBlockID
@ ProfileMetadataBlockID
PGOCtxProfileRecords
@ CallsiteIndex
@ TotalRootEntryCount