LLVM  14.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 
21 
22 namespace llvm {
23 namespace remarks {
24 
25 /// Serialize the remarks to LLVM bitstream.
26 /// This class provides ways to emit remarks in the LLVM bitstream format and
27 /// its associated metadata.
28 ///
29 /// * The separate model:
30 /// Separate meta: | Container info
31 /// | String table
32 /// | External file
33 ///
34 /// Separate remarks: | Container info
35 /// | Remark version
36 /// | Remark0
37 /// | Remark1
38 /// | Remark2
39 /// | ...
40 ///
41 /// * The standalone model: | Container info
42 /// | String table
43 /// | Remark version
44 /// | Remark0
45 /// | Remark1
46 /// | Remark2
47 /// | ...
48 ///
50  /// Buffer used for encoding the bitstream before writing it to the final
51  /// stream.
53  /// Buffer used to construct records and pass to the bitstream writer.
55  /// The Bitstream writer.
57  /// The type of the container we are serializing.
59 
60  /// Abbrev IDs initialized in the block info block.
61  /// Note: depending on the container type, some IDs might be uninitialized.
62  /// Warning: When adding more abbrev IDs, make sure to update the
63  /// BlockCodeSize (in the call to EnterSubblock).
73 
75 
76  // Disable copy and move: Bitstream points to Encoded, which needs special
77  // handling during copy/move, but moving the vectors is probably useless
78  // anyway.
80  delete;
86 
87  /// Set up the necessary block info entries according to the container type.
88  void setupBlockInfo();
89 
90  /// Set up the block info for the metadata block.
91  void setupMetaBlockInfo();
92  /// The remark version in the metadata block.
94  void emitMetaRemarkVersion(uint64_t RemarkVersion);
95  /// The strtab in the metadata block.
96  void setupMetaStrTab();
97  void emitMetaStrTab(const StringTable &StrTab);
98  /// The external file in the metadata block.
99  void setupMetaExternalFile();
100  void emitMetaExternalFile(StringRef Filename);
101 
102  /// The block info for the remarks block.
103  void setupRemarkBlockInfo();
104 
105  /// Emit the metadata for the remarks.
106  void emitMetaBlock(uint64_t ContainerVersion,
107  Optional<uint64_t> RemarkVersion,
108  Optional<const StringTable *> StrTab = None,
109  Optional<StringRef> Filename = None);
110 
111  /// Emit a remark block. The string table is required.
112  void emitRemarkBlock(const Remark &Remark, StringTable &StrTab);
113  /// Finalize the writing to \p OS.
114  void flushToStream(raw_ostream &OS);
115  /// Finalize the writing to a buffer.
116  /// The contents of the buffer remain valid for the lifetime of the object.
117  /// Any call to any other function in this class will invalidate the buffer.
119 };
120 
121 /// Implementation of the remark serializer using LLVM bitstream.
123  /// The file should contain:
124  /// 1) The block info block that describes how to read the blocks.
125  /// 2) The metadata block that contains various information about the remarks
126  /// in the file.
127  /// 3) A number of remark blocks.
128 
129  /// We need to set up 1) and 2) first, so that we can emit 3) after. This flag
130  /// is used to emit the first two blocks only once.
131  bool DidSetUp = false;
132  /// The helper to emit bitstream.
134 
135  /// Construct a serializer that will create its own string table.
137  /// Construct a serializer with a pre-filled string table.
140 
141  /// Emit a remark to the stream. This also emits the metadata associated to
142  /// the remarks based on the SerializerMode specified at construction.
143  /// This writes the serialized output to the provided stream.
144  void emit(const Remark &Remark) override;
145  /// The metadata serializer associated to this remark serializer. Based on the
146  /// container type of the current serializer, the container type of the
147  /// metadata serializer will change.
148  std::unique_ptr<MetaSerializer>
150  Optional<StringRef> ExternalFilename = None) override;
151 
152  static bool classof(const RemarkSerializer *S) {
153  return S->SerializerFormat == Format::Bitstream;
154  }
155 };
156 
157 /// Serializer of metadata for bitstream remarks.
159  /// This class can be used with [1] a pre-constructed
160  /// BitstreamRemarkSerializerHelper, or with [2] one that is owned by the meta
161  /// serializer. In case of [1], we need to be able to store a reference to the
162  /// object, while in case of [2] we need to store the whole object.
164  /// The actual helper, that can point to \p TmpHelper or to an external helper
165  /// object.
167 
170 
171  /// Create a new meta serializer based on \p ContainerType.
173  BitstreamRemarkContainerType ContainerType,
178  TmpHelper.emplace(ContainerType);
179  Helper = &*TmpHelper;
180  }
181 
182  /// Create a new meta serializer based on a previously built \p Helper.
189 
190  void emit() override;
191 };
192 
193 } // end namespace remarks
194 } // end namespace llvm
195 
196 #endif // LLVM_REMARKS_BITSTREAMREMARKSERIALIZER_H
llvm::remarks::SerializerMode
SerializerMode
Definition: RemarkSerializer.h:24
llvm::remarks::BitstreamMetaSerializer::Helper
BitstreamRemarkSerializerHelper * Helper
The actual helper, that can point to TmpHelper or to an external helper object.
Definition: BitstreamRemarkSerializer.h:166
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:131
llvm::remarks::BitstreamRemarkSerializerHelper::Bitstream
BitstreamWriter Bitstream
The Bitstream writer.
Definition: BitstreamRemarkSerializer.h:56
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
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:67
llvm::remarks::BitstreamRemarkSerializerHelper::R
SmallVector< uint64_t, 64 > R
Buffer used to construct records and pass to the bitstream writer.
Definition: BitstreamRemarkSerializer.h:54
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:47
llvm::remarks::BitstreamRemarkSerializerHelper::RecordMetaRemarkVersionAbbrevID
uint64_t RecordMetaRemarkVersionAbbrevID
Definition: BitstreamRemarkSerializer.h:65
llvm::remarks::BitstreamMetaSerializer::BitstreamMetaSerializer
BitstreamMetaSerializer(raw_ostream &OS, BitstreamRemarkSerializerHelper &Helper, Optional< const StringTable * > StrTab=None, Optional< StringRef > ExternalFilename=None)
Create a new meta serializer based on a previously built Helper.
Definition: BitstreamRemarkSerializer.h:183
llvm::Optional< uint64_t >
llvm::remarks::BitstreamRemarkSerializerHelper::RecordRemarkHeaderAbbrevID
uint64_t RecordRemarkHeaderAbbrevID
Definition: BitstreamRemarkSerializer.h:68
llvm::remarks::BitstreamRemarkSerializerHelper::setupMetaRemarkVersion
void setupMetaRemarkVersion()
The remark version in the metadata block.
Definition: BitstreamRemarkSerializer.cpp:62
llvm::remarks::RemarkSerializer::OS
raw_ostream & OS
The open raw_ostream that the remark diagnostics are emitted to.
Definition: RemarkSerializer.h:42
llvm::remarks::BitstreamRemarkSerializerHelper::setupRemarkBlockInfo
void setupRemarkBlockInfo()
The block info for the remarks block.
Definition: BitstreamRemarkSerializer.cpp:123
llvm::remarks::BitstreamRemarkSerializer::Helper
BitstreamRemarkSerializerHelper Helper
The helper to emit bitstream.
Definition: BitstreamRemarkSerializer.h:133
llvm::remarks::BitstreamRemarkSerializerHelper::emitMetaExternalFile
void emitMetaExternalFile(StringRef Filename)
Definition: BitstreamRemarkSerializer.cpp:116
llvm::remarks::BitstreamRemarkSerializerHelper::RecordMetaContainerInfoAbbrevID
uint64_t RecordMetaContainerInfoAbbrevID
Abbrev IDs initialized in the block info block.
Definition: BitstreamRemarkSerializer.h:64
llvm::remarks::BitstreamRemarkSerializerHelper
Serialize the remarks to LLVM bitstream.
Definition: BitstreamRemarkSerializer.h:49
llvm::remarks::BitstreamMetaSerializer::TmpHelper
Optional< BitstreamRemarkSerializerHelper > TmpHelper
This class can be used with [1] a pre-constructed BitstreamRemarkSerializerHelper,...
Definition: BitstreamRemarkSerializer.h:163
llvm::remarks::RemarkSerializer::Mode
SerializerMode Mode
The serialization mode.
Definition: RemarkSerializer.h:44
llvm::remarks::BitstreamRemarkSerializerHelper::emitRemarkBlock
void emitRemarkBlock(const Remark &Remark, StringTable &StrTab)
Emit a remark block. The string table is required.
Definition: BitstreamRemarkSerializer.cpp:267
llvm::remarks::BitstreamRemarkSerializerHelper::BitstreamRemarkSerializerHelper
BitstreamRemarkSerializerHelper(BitstreamRemarkContainerType ContainerType)
Definition: BitstreamRemarkSerializer.cpp:19
llvm::remarks::BitstreamRemarkSerializerHelper::RecordMetaStrTabAbbrevID
uint64_t RecordMetaStrTabAbbrevID
Definition: BitstreamRemarkSerializer.h:66
remarks
annotation remarks
Definition: AnnotationRemarks.cpp:117
llvm::BitstreamWriter
Definition: BitstreamWriter.h:30
llvm::remarks::BitstreamRemarkSerializerHelper::emitMetaRemarkVersion
void emitMetaRemarkVersion(uint64_t RemarkVersion)
Definition: BitstreamRemarkSerializer.cpp:73
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::None
const NoneType None
Definition: None.h:23
llvm::remarks::RemarkSerializer
This is the base class for a remark serializer.
Definition: RemarkSerializer.h:38
llvm::remarks::BitstreamRemarkSerializerHelper::RecordRemarkArgWithDebugLocAbbrevID
uint64_t RecordRemarkArgWithDebugLocAbbrevID
Definition: BitstreamRemarkSerializer.h:71
llvm::remarks::BitstreamRemarkSerializerHelper::RecordRemarkArgWithoutDebugLocAbbrevID
uint64_t RecordRemarkArgWithoutDebugLocAbbrevID
Definition: BitstreamRemarkSerializer.h:72
llvm::remarks::BitstreamRemarkSerializerHelper::RecordRemarkDebugLocAbbrevID
uint64_t RecordRemarkDebugLocAbbrevID
Definition: BitstreamRemarkSerializer.h:69
uint64_t
llvm::remarks::BitstreamRemarkSerializerHelper::setupMetaBlockInfo
void setupMetaBlockInfo()
Set up the block info for the metadata block.
Definition: BitstreamRemarkSerializer.cpp:46
llvm::remarks::BitstreamRemarkSerializer::emit
void emit(const Remark &Remark) override
Emit a remark to the stream.
Definition: BitstreamRemarkSerializer.cpp:347
llvm::remarks::BitstreamRemarkSerializerHelper::setupBlockInfo
void setupBlockInfo()
Set up the necessary block info entries according to the container type.
Definition: BitstreamRemarkSerializer.cpp:195
BitstreamRemarkContainer.h
llvm::remarks::BitstreamRemarkSerializerHelper::setupMetaExternalFile
void setupMetaExternalFile()
The external file in the metadata block.
Definition: BitstreamRemarkSerializer.cpp:106
llvm::remarks::BitstreamMetaSerializer::StrTab
Optional< const StringTable * > StrTab
Definition: BitstreamRemarkSerializer.h:168
llvm::remarks::BitstreamRemarkSerializerHelper::emitMetaStrTab
void emitMetaStrTab(const StringTable &StrTab)
Definition: BitstreamRemarkSerializer.cpp:92
llvm::remarks::BitstreamMetaSerializer::BitstreamMetaSerializer
BitstreamMetaSerializer(raw_ostream &OS, BitstreamRemarkContainerType ContainerType, Optional< const StringTable * > StrTab=None, Optional< StringRef > ExternalFilename=None)
Create a new meta serializer based on ContainerType.
Definition: BitstreamRemarkSerializer.h:172
llvm::remarks::BitstreamRemarkSerializerHelper::setupMetaStrTab
void setupMetaStrTab()
The strtab in the metadata block.
Definition: BitstreamRemarkSerializer.cpp:82
llvm::remarks::MetaSerializer::OS
raw_ostream & OS
The open raw_ostream that the metadata is emitted to.
Definition: RemarkSerializer.h:66
llvm::remarks::BitstreamRemarkSerializer::metaSerializer
std::unique_ptr< MetaSerializer > metaSerializer(raw_ostream &OS, Optional< StringRef > ExternalFilename=None) override
The metadata serializer associated to this remark serializer.
Definition: BitstreamRemarkSerializer.cpp:367
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::remarks::BitstreamRemarkSerializer::BitstreamRemarkSerializer
BitstreamRemarkSerializer(raw_ostream &OS, SerializerMode Mode)
Construct a serializer that will create its own string table.
Definition: BitstreamRemarkSerializer.cpp:326
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:317
llvm::remarks::BitstreamRemarkSerializerHelper::operator=
BitstreamRemarkSerializerHelper & operator=(const BitstreamRemarkSerializerHelper &)=delete
llvm::remarks::MetaSerializer
This is the base class for a remark metadata serializer.
Definition: RemarkSerializer.h:64
llvm::remarks::BitstreamRemarkSerializer
Implementation of the remark serializer using LLVM bitstream.
Definition: BitstreamRemarkSerializer.h:122
llvm::remarks::BitstreamRemarkSerializerHelper::ContainerType
BitstreamRemarkContainerType ContainerType
The type of the container we are serializing.
Definition: BitstreamRemarkSerializer.h:58
llvm::remarks::BitstreamRemarkSerializer::classof
static bool classof(const RemarkSerializer *S)
Definition: BitstreamRemarkSerializer.h:152
llvm::remarks::BitstreamMetaSerializer
Serializer of metadata for bitstream remarks.
Definition: BitstreamRemarkSerializer.h:158
llvm::remarks::BitstreamRemarkSerializerHelper::emitMetaBlock
void emitMetaBlock(uint64_t ContainerVersion, Optional< uint64_t > RemarkVersion, Optional< const StringTable * > StrTab=None, Optional< StringRef > Filename=None)
Emit the metadata for the remarks.
Definition: BitstreamRemarkSerializer.cpp:232
RemarkSerializer.h
llvm::remarks::BitstreamMetaSerializer::ExternalFilename
Optional< StringRef > ExternalFilename
Definition: BitstreamRemarkSerializer.h:169
llvm::remarks::BitstreamMetaSerializer::emit
void emit() override
Definition: BitstreamRemarkSerializer.cpp:380
raw_ostream.h
llvm::remarks::BitstreamRemarkSerializerHelper::getBuffer
StringRef getBuffer()
Finalize the writing to a buffer.
Definition: BitstreamRemarkSerializer.cpp:322
llvm::remarks::BitstreamRemarkSerializerHelper::RecordRemarkHotnessAbbrevID
uint64_t RecordRemarkHotnessAbbrevID
Definition: BitstreamRemarkSerializer.h:70
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:52
llvm::remarks::BitstreamRemarkContainerType
BitstreamRemarkContainerType
Type of the remark container.
Definition: BitstreamRemarkContainer.h:35