LLVM  16.0.0git
BitstreamRemarkSerializer.h
Go to the documentation of this file.
1 //===-- BitstreamRemarkSerializer.h - Bitstream serializer ------*- 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 provides an implementation of the serializer using the LLVM
10 // Bitstream format.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_REMARKS_BITSTREAMREMARKSERIALIZER_H
15 #define LLVM_REMARKS_BITSTREAMREMARKSERIALIZER_H
16 
20 
21 namespace llvm {
22 namespace remarks {
23 
24 struct Remarks;
25 
26 /// Serialize the remarks to LLVM bitstream.
27 /// This class provides ways to emit remarks in the LLVM bitstream format and
28 /// its associated metadata.
29 ///
30 /// * The separate model:
31 /// Separate meta: | Container info
32 /// | String table
33 /// | External file
34 ///
35 /// Separate remarks: | Container info
36 /// | Remark version
37 /// | Remark0
38 /// | Remark1
39 /// | Remark2
40 /// | ...
41 ///
42 /// * The standalone model: | Container info
43 /// | String table
44 /// | Remark version
45 /// | Remark0
46 /// | Remark1
47 /// | Remark2
48 /// | ...
49 ///
51  /// Buffer used for encoding the bitstream before writing it to the final
52  /// stream.
54  /// Buffer used to construct records and pass to the bitstream writer.
56  /// The Bitstream writer.
58  /// The type of the container we are serializing.
60 
61  /// Abbrev IDs initialized in the block info block.
62  /// Note: depending on the container type, some IDs might be uninitialized.
63  /// Warning: When adding more abbrev IDs, make sure to update the
64  /// BlockCodeSize (in the call to EnterSubblock).
74 
76 
77  // Disable copy and move: Bitstream points to Encoded, which needs special
78  // handling during copy/move, but moving the vectors is probably useless
79  // anyway.
81  delete;
87 
88  /// Set up the necessary block info entries according to the container type.
89  void setupBlockInfo();
90 
91  /// Set up the block info for the metadata block.
92  void setupMetaBlockInfo();
93  /// The remark version in the metadata block.
95  void emitMetaRemarkVersion(uint64_t RemarkVersion);
96  /// The strtab in the metadata block.
97  void setupMetaStrTab();
98  void emitMetaStrTab(const StringTable &StrTab);
99  /// The external file in the metadata block.
100  void setupMetaExternalFile();
101  void emitMetaExternalFile(StringRef Filename);
102 
103  /// The block info for the remarks block.
104  void setupRemarkBlockInfo();
105 
106  /// Emit the metadata for the remarks.
107  void emitMetaBlock(uint64_t ContainerVersion,
108  Optional<uint64_t> RemarkVersion,
109  Optional<const StringTable *> StrTab = std::nullopt,
110  Optional<StringRef> Filename = std::nullopt);
111 
112  /// Emit a remark block. The string table is required.
113  void emitRemarkBlock(const Remark &Remark, StringTable &StrTab);
114  /// Finalize the writing to \p OS.
115  void flushToStream(raw_ostream &OS);
116  /// Finalize the writing to a buffer.
117  /// The contents of the buffer remain valid for the lifetime of the object.
118  /// Any call to any other function in this class will invalidate the buffer.
120 };
121 
122 /// Implementation of the remark serializer using LLVM bitstream.
124  /// The file should contain:
125  /// 1) The block info block that describes how to read the blocks.
126  /// 2) The metadata block that contains various information about the remarks
127  /// in the file.
128  /// 3) A number of remark blocks.
129 
130  /// We need to set up 1) and 2) first, so that we can emit 3) after. This flag
131  /// is used to emit the first two blocks only once.
132  bool DidSetUp = false;
133  /// The helper to emit bitstream.
135 
136  /// Construct a serializer that will create its own string table.
138  /// Construct a serializer with a pre-filled string table.
141 
142  /// Emit a remark to the stream. This also emits the metadata associated to
143  /// the remarks based on the SerializerMode specified at construction.
144  /// This writes the serialized output to the provided stream.
145  void emit(const Remark &Remark) override;
146  /// The metadata serializer associated to this remark serializer. Based on the
147  /// container type of the current serializer, the container type of the
148  /// metadata serializer will change.
149  std::unique_ptr<MetaSerializer>
151  Optional<StringRef> ExternalFilename = std::nullopt) override;
152 
153  static bool classof(const RemarkSerializer *S) {
154  return S->SerializerFormat == Format::Bitstream;
155  }
156 };
157 
158 /// Serializer of metadata for bitstream remarks.
160  /// This class can be used with [1] a pre-constructed
161  /// BitstreamRemarkSerializerHelper, or with [2] one that is owned by the meta
162  /// serializer. In case of [1], we need to be able to store a reference to the
163  /// object, while in case of [2] we need to store the whole object.
165  /// The actual helper, that can point to \p TmpHelper or to an external helper
166  /// object.
168 
171 
172  /// Create a new meta serializer based on \p ContainerType.
174  BitstreamRemarkContainerType ContainerType,
175  Optional<const StringTable *> StrTab = std::nullopt,
176  Optional<StringRef> ExternalFilename = std::nullopt)
177  : MetaSerializer(OS), TmpHelper(std::nullopt), Helper(nullptr),
179  TmpHelper.emplace(ContainerType);
180  Helper = &*TmpHelper;
181  }
182 
183  /// Create a new meta serializer based on a previously built \p Helper.
186  Optional<const StringTable *> StrTab = std::nullopt,
187  Optional<StringRef> ExternalFilename = std::nullopt)
188  : MetaSerializer(OS), TmpHelper(std::nullopt), Helper(&Helper),
190 
191  void emit() override;
192 };
193 
194 } // end namespace remarks
195 } // end namespace llvm
196 
197 #endif // LLVM_REMARKS_BITSTREAMREMARKSERIALIZER_H
llvm::remarks::SerializerMode
SerializerMode
Definition: RemarkSerializer.h:27
llvm::remarks::BitstreamMetaSerializer::Helper
BitstreamRemarkSerializerHelper * Helper
The actual helper, that can point to TmpHelper or to an external helper object.
Definition: BitstreamRemarkSerializer.h:167
llvm::remarks::BitstreamRemarkSerializer::metaSerializer
std::unique_ptr< MetaSerializer > metaSerializer(raw_ostream &OS, Optional< StringRef > ExternalFilename=std::nullopt) override
The metadata serializer associated to this remark serializer.
Definition: BitstreamRemarkSerializer.cpp:368
llvm::remarks::BitstreamRemarkSerializer::DidSetUp
bool DidSetUp
The file should contain: 1) The block info block that describes how to read the blocks.
Definition: BitstreamRemarkSerializer.h:132
llvm::remarks::BitstreamRemarkSerializerHelper::Bitstream
BitstreamWriter Bitstream
The Bitstream writer.
Definition: BitstreamRemarkSerializer.h:57
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::remarks::Format::Bitstream
@ Bitstream
llvm::remarks::StringTable
The string table used for serializing remarks.
Definition: RemarkStringTable.h:36
llvm::remarks::BitstreamRemarkSerializerHelper::RecordMetaExternalFileAbbrevID
uint64_t RecordMetaExternalFileAbbrevID
Definition: BitstreamRemarkSerializer.h:68
llvm::remarks::BitstreamRemarkSerializerHelper::R
SmallVector< uint64_t, 64 > R
Buffer used to construct records and pass to the bitstream writer.
Definition: BitstreamRemarkSerializer.h:55
llvm::SmallVector< char, 1024 >
llvm::remarks::RemarkSerializer::StrTab
Optional< StringTable > StrTab
The string table containing all the unique strings used in the output.
Definition: RemarkSerializer.h:50
llvm::remarks::BitstreamRemarkSerializerHelper::RecordMetaRemarkVersionAbbrevID
uint64_t RecordMetaRemarkVersionAbbrevID
Definition: BitstreamRemarkSerializer.h:66
llvm::Optional< uint64_t >
llvm::remarks::BitstreamRemarkSerializerHelper::RecordRemarkHeaderAbbrevID
uint64_t RecordRemarkHeaderAbbrevID
Definition: BitstreamRemarkSerializer.h:69
llvm::remarks::BitstreamRemarkSerializerHelper::setupMetaRemarkVersion
void setupMetaRemarkVersion()
The remark version in the metadata block.
Definition: BitstreamRemarkSerializer.cpp:63
llvm::remarks::RemarkSerializer::OS
raw_ostream & OS
The open raw_ostream that the remark diagnostics are emitted to.
Definition: RemarkSerializer.h:45
llvm::remarks::BitstreamRemarkSerializerHelper::setupRemarkBlockInfo
void setupRemarkBlockInfo()
The block info for the remarks block.
Definition: BitstreamRemarkSerializer.cpp:124
llvm::remarks::BitstreamRemarkSerializer::Helper
BitstreamRemarkSerializerHelper Helper
The helper to emit bitstream.
Definition: BitstreamRemarkSerializer.h:134
llvm::remarks::BitstreamRemarkSerializerHelper::emitMetaBlock
void emitMetaBlock(uint64_t ContainerVersion, Optional< uint64_t > RemarkVersion, Optional< const StringTable * > StrTab=std::nullopt, Optional< StringRef > Filename=std::nullopt)
Emit the metadata for the remarks.
Definition: BitstreamRemarkSerializer.cpp:233
llvm::remarks::BitstreamRemarkSerializerHelper::emitMetaExternalFile
void emitMetaExternalFile(StringRef Filename)
Definition: BitstreamRemarkSerializer.cpp:117
llvm::remarks::BitstreamRemarkSerializerHelper::RecordMetaContainerInfoAbbrevID
uint64_t RecordMetaContainerInfoAbbrevID
Abbrev IDs initialized in the block info block.
Definition: BitstreamRemarkSerializer.h:65
llvm::remarks::BitstreamRemarkSerializerHelper
Serialize the remarks to LLVM bitstream.
Definition: BitstreamRemarkSerializer.h:50
llvm::remarks::BitstreamMetaSerializer::TmpHelper
Optional< BitstreamRemarkSerializerHelper > TmpHelper
This class can be used with [1] a pre-constructed BitstreamRemarkSerializerHelper,...
Definition: BitstreamRemarkSerializer.h:164
llvm::remarks::RemarkSerializer::Mode
SerializerMode Mode
The serialization mode.
Definition: RemarkSerializer.h:47
llvm::remarks::BitstreamRemarkSerializerHelper::emitRemarkBlock
void emitRemarkBlock(const Remark &Remark, StringTable &StrTab)
Emit a remark block. The string table is required.
Definition: BitstreamRemarkSerializer.cpp:268
llvm::remarks::BitstreamRemarkSerializerHelper::BitstreamRemarkSerializerHelper
BitstreamRemarkSerializerHelper(BitstreamRemarkContainerType ContainerType)
Definition: BitstreamRemarkSerializer.cpp:20
llvm::remarks::BitstreamRemarkSerializerHelper::RecordMetaStrTabAbbrevID
uint64_t RecordMetaStrTabAbbrevID
Definition: BitstreamRemarkSerializer.h:67
remarks
annotation remarks
Definition: AnnotationRemarks.cpp:114
llvm::BitstreamWriter
Definition: BitstreamWriter.h:30
llvm::remarks::BitstreamRemarkSerializerHelper::emitMetaRemarkVersion
void emitMetaRemarkVersion(uint64_t RemarkVersion)
Definition: BitstreamRemarkSerializer.cpp:74
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::remarks::Remark
A remark type used for both emission and parsing.
Definition: Remark.h:67
llvm::remarks::RemarkSerializer
This is the base class for a remark serializer.
Definition: RemarkSerializer.h:41
llvm::remarks::BitstreamRemarkSerializerHelper::RecordRemarkArgWithDebugLocAbbrevID
uint64_t RecordRemarkArgWithDebugLocAbbrevID
Definition: BitstreamRemarkSerializer.h:72
llvm::remarks::BitstreamRemarkSerializerHelper::RecordRemarkArgWithoutDebugLocAbbrevID
uint64_t RecordRemarkArgWithoutDebugLocAbbrevID
Definition: BitstreamRemarkSerializer.h:73
llvm::remarks::BitstreamRemarkSerializerHelper::RecordRemarkDebugLocAbbrevID
uint64_t RecordRemarkDebugLocAbbrevID
Definition: BitstreamRemarkSerializer.h:70
uint64_t
llvm::remarks::BitstreamRemarkSerializerHelper::setupMetaBlockInfo
void setupMetaBlockInfo()
Set up the block info for the metadata block.
Definition: BitstreamRemarkSerializer.cpp:47
llvm::remarks::BitstreamRemarkSerializer::emit
void emit(const Remark &Remark) override
Emit a remark to the stream.
Definition: BitstreamRemarkSerializer.cpp:348
Remarks
annotation Annotation Remarks
Definition: AnnotationRemarks.cpp:115
llvm::remarks::BitstreamRemarkSerializerHelper::setupBlockInfo
void setupBlockInfo()
Set up the necessary block info entries according to the container type.
Definition: BitstreamRemarkSerializer.cpp:196
llvm::remarks::BitstreamMetaSerializer::BitstreamMetaSerializer
BitstreamMetaSerializer(raw_ostream &OS, BitstreamRemarkSerializerHelper &Helper, Optional< const StringTable * > StrTab=std::nullopt, Optional< StringRef > ExternalFilename=std::nullopt)
Create a new meta serializer based on a previously built Helper.
Definition: BitstreamRemarkSerializer.h:184
BitstreamRemarkContainer.h
llvm::remarks::BitstreamRemarkSerializerHelper::setupMetaExternalFile
void setupMetaExternalFile()
The external file in the metadata block.
Definition: BitstreamRemarkSerializer.cpp:107
llvm::remarks::BitstreamMetaSerializer::StrTab
Optional< const StringTable * > StrTab
Definition: BitstreamRemarkSerializer.h:169
llvm::remarks::BitstreamRemarkSerializerHelper::emitMetaStrTab
void emitMetaStrTab(const StringTable &StrTab)
Definition: BitstreamRemarkSerializer.cpp:93
llvm::remarks::BitstreamRemarkSerializerHelper::setupMetaStrTab
void setupMetaStrTab()
The strtab in the metadata block.
Definition: BitstreamRemarkSerializer.cpp:83
llvm::remarks::MetaSerializer::OS
raw_ostream & OS
The open raw_ostream that the metadata is emitted to.
Definition: RemarkSerializer.h:69
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::remarks::BitstreamRemarkSerializer::BitstreamRemarkSerializer
BitstreamRemarkSerializer(raw_ostream &OS, SerializerMode Mode)
Construct a serializer that will create its own string table.
Definition: BitstreamRemarkSerializer.cpp:327
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::remarks::BitstreamRemarkSerializerHelper::flushToStream
void flushToStream(raw_ostream &OS)
Finalize the writing to OS.
Definition: BitstreamRemarkSerializer.cpp:318
llvm::remarks::BitstreamRemarkSerializerHelper::operator=
BitstreamRemarkSerializerHelper & operator=(const BitstreamRemarkSerializerHelper &)=delete
llvm::remarks::BitstreamMetaSerializer::BitstreamMetaSerializer
BitstreamMetaSerializer(raw_ostream &OS, BitstreamRemarkContainerType ContainerType, Optional< const StringTable * > StrTab=std::nullopt, Optional< StringRef > ExternalFilename=std::nullopt)
Create a new meta serializer based on ContainerType.
Definition: BitstreamRemarkSerializer.h:173
llvm::remarks::MetaSerializer
This is the base class for a remark metadata serializer.
Definition: RemarkSerializer.h:67
std
Definition: BitVector.h:851
llvm::remarks::BitstreamRemarkSerializer
Implementation of the remark serializer using LLVM bitstream.
Definition: BitstreamRemarkSerializer.h:123
llvm::remarks::BitstreamRemarkSerializerHelper::ContainerType
BitstreamRemarkContainerType ContainerType
The type of the container we are serializing.
Definition: BitstreamRemarkSerializer.h:59
llvm::remarks::BitstreamRemarkSerializer::classof
static bool classof(const RemarkSerializer *S)
Definition: BitstreamRemarkSerializer.h:153
llvm::remarks::BitstreamMetaSerializer
Serializer of metadata for bitstream remarks.
Definition: BitstreamRemarkSerializer.h:159
RemarkSerializer.h
llvm::remarks::BitstreamMetaSerializer::ExternalFilename
Optional< StringRef > ExternalFilename
Definition: BitstreamRemarkSerializer.h:170
llvm::remarks::BitstreamMetaSerializer::emit
void emit() override
Definition: BitstreamRemarkSerializer.cpp:381
llvm::remarks::BitstreamRemarkSerializerHelper::getBuffer
StringRef getBuffer()
Finalize the writing to a buffer.
Definition: BitstreamRemarkSerializer.cpp:323
llvm::remarks::BitstreamRemarkSerializerHelper::RecordRemarkHotnessAbbrevID
uint64_t RecordRemarkHotnessAbbrevID
Definition: BitstreamRemarkSerializer.h:71
BitstreamWriter.h
llvm::remarks::BitstreamRemarkSerializerHelper::Encoded
SmallVector< char, 1024 > Encoded
Buffer used for encoding the bitstream before writing it to the final stream.
Definition: BitstreamRemarkSerializer.h:53
llvm::remarks::BitstreamRemarkContainerType
BitstreamRemarkContainerType
Type of the remark container.
Definition: BitstreamRemarkContainer.h:35