LLVM  10.0.0svn
BitstreamRemarkSerializer.cpp
Go to the documentation of this file.
1 //===- BitstreamRemarkSerializer.cpp --------------------------------------===//
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 the implementation of the LLVM bitstream remark serializer
10 // using LLVM's bitstream writer.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
16 using namespace llvm;
17 using namespace llvm::remarks;
18 
20  BitstreamRemarkContainerType ContainerType)
21  : Encoded(), R(), Bitstream(Encoded), ContainerType(ContainerType) {}
22 
24  for (const char C : Str)
25  R.push_back(C);
26 }
27 
28 static void setRecordName(unsigned RecordID, BitstreamWriter &Bitstream,
30  R.clear();
31  R.push_back(RecordID);
32  push(R, Str);
34 }
35 
36 static void initBlock(unsigned BlockID, BitstreamWriter &Bitstream,
38  R.clear();
39  R.push_back(BlockID);
41 
42  R.clear();
43  push(R, Str);
45 }
46 
48  // Setup the metadata block.
50 
51  // The container information.
54 
55  auto Abbrev = std::make_shared<BitCodeAbbrev>();
57  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Version.
58  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Type.
61 }
62 
66 
67  auto Abbrev = std::make_shared<BitCodeAbbrev>();
69  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Version.
72 }
73 
75  uint64_t RemarkVersion) {
76  // The remark version is emitted only if we emit remarks.
77  R.clear();
79  R.push_back(RemarkVersion);
81 }
82 
85 
86  auto Abbrev = std::make_shared<BitCodeAbbrev>();
87  Abbrev->Add(BitCodeAbbrevOp(RECORD_META_STRTAB));
88  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Raw table.
91 }
92 
94  const StringTable &StrTab) {
95  // The string table is not emitted if we emit remarks separately.
96  R.clear();
98 
99  // Serialize to a blob.
100  std::string Buf;
101  raw_string_ostream OS(Buf);
102  StrTab.serialize(OS);
103  StringRef Blob = OS.str();
105 }
106 
109 
110  auto Abbrev = std::make_shared<BitCodeAbbrev>();
112  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Filename.
115 }
116 
118  // The external file is emitted only if we emit the separate metadata.
119  R.clear();
122 }
123 
125  // Setup the remark block.
127 
128  // The header of a remark.
129  {
131 
132  auto Abbrev = std::make_shared<BitCodeAbbrev>();
134  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Type
135  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Remark Name
136  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Pass name
137  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Function name
140  }
141 
142  // The location of a remark.
143  {
145 
146  auto Abbrev = std::make_shared<BitCodeAbbrev>();
148  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // File
149  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line
150  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column
153  }
154 
155  // The hotness of a remark.
156  {
158 
159  auto Abbrev = std::make_shared<BitCodeAbbrev>();
161  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Hotness
164  }
165 
166  // An argument entry with a debug location attached.
167  {
170 
171  auto Abbrev = std::make_shared<BitCodeAbbrev>();
173  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Key
174  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Value
175  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // File
176  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line
177  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column
180  }
181 
182  // An argument entry with no debug location attached.
183  {
186 
187  auto Abbrev = std::make_shared<BitCodeAbbrev>();
189  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Key
190  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Value
193  }
194 }
195 
197  // Emit magic number.
198  for (const char C : ContainerMagic)
199  Bitstream.Emit(static_cast<unsigned>(C), 8);
200 
202 
203  // Setup the main metadata. Depending on the container type, we'll setup the
204  // required records next.
206 
207  switch (ContainerType) {
209  // Needs a string table that the separate remark file is using.
210  setupMetaStrTab();
211  // Needs to know where the external remarks file is.
213  break;
215  // Contains remarks: emit the version.
217  // Contains remarks: emit the remark abbrevs.
219  break;
221  // Contains remarks: emit the version.
223  // Needs a string table.
224  setupMetaStrTab();
225  // Contains remarks: emit the remark abbrevs.
227  break;
228  }
229 
231 }
232 
234  uint64_t ContainerVersion, Optional<uint64_t> RemarkVersion,
236  // Emit the meta block
238 
239  // The container version and type.
240  R.clear();
242  R.push_back(ContainerVersion);
243  R.push_back(static_cast<uint64_t>(ContainerType));
245 
246  switch (ContainerType) {
248  assert(StrTab != None && *StrTab != nullptr);
249  emitMetaStrTab(**StrTab);
250  assert(Filename != None);
251  emitMetaExternalFile(*Filename);
252  break;
254  assert(RemarkVersion != None);
255  emitMetaRemarkVersion(*RemarkVersion);
256  break;
258  assert(RemarkVersion != None);
259  emitMetaRemarkVersion(*RemarkVersion);
260  assert(StrTab != None && *StrTab != nullptr);
261  emitMetaStrTab(**StrTab);
262  break;
263  }
264 
266 }
267 
269  StringTable &StrTab) {
271 
272  R.clear();
274  R.push_back(static_cast<uint64_t>(Remark.RemarkType));
275  R.push_back(StrTab.add(Remark.RemarkName).first);
276  R.push_back(StrTab.add(Remark.PassName).first);
277  R.push_back(StrTab.add(Remark.FunctionName).first);
279 
280  if (const Optional<RemarkLocation> &Loc = Remark.Loc) {
281  R.clear();
283  R.push_back(StrTab.add(Loc->SourceFilePath).first);
284  R.push_back(Loc->SourceLine);
285  R.push_back(Loc->SourceColumn);
287  }
288 
289  if (Optional<uint64_t> Hotness = Remark.Hotness) {
290  R.clear();
292  R.push_back(*Hotness);
294  }
295 
296  for (const Argument &Arg : Remark.Args) {
297  R.clear();
298  unsigned Key = StrTab.add(Arg.Key).first;
299  unsigned Val = StrTab.add(Arg.Val).first;
300  bool HasDebugLoc = Arg.Loc != None;
303  R.push_back(Key);
304  R.push_back(Val);
305  if (HasDebugLoc) {
306  R.push_back(StrTab.add(Arg.Loc->SourceFilePath).first);
307  R.push_back(Arg.Loc->SourceLine);
308  R.push_back(Arg.Loc->SourceColumn);
309  }
310  Bitstream.EmitRecordWithAbbrev(HasDebugLoc
313  R);
314  }
316 }
317 
319  OS.write(Encoded.data(), Encoded.size());
320  Encoded.clear();
321 }
322 
324  return StringRef(Encoded.data(), Encoded.size());
325 }
326 
329  : RemarkSerializer(Format::Bitstream, OS, Mode),
332  "For SerializerMode::Standalone, a pre-filled string table needs to "
333  "be provided.");
334  // We always use a string table with bitstream.
335  StrTab.emplace();
336 }
337 
340  StringTable StrTabIn)
341  : RemarkSerializer(Format::Bitstream, OS, Mode),
345  StrTab = std::move(StrTabIn);
346 }
347 
349  if (!DidSetUp) {
350  // Emit the metadata that is embedded in the remark file.
351  // If we're in standalone mode, serialize the string table as well.
352  bool IsStandalone =
355  OS, Helper,
356  IsStandalone ? &*StrTab : Optional<const StringTable *>(None));
357  MetaSerializer.emit();
358  DidSetUp = true;
359  }
360 
361  assert(DidSetUp &&
362  "The Block info block and the meta block were not emitted yet.");
363  Helper.emitRemarkBlock(Remark, *StrTab);
364 
366 }
367 
368 std::unique_ptr<MetaSerializer> BitstreamRemarkSerializer::metaSerializer(
369  raw_ostream &OS, Optional<StringRef> ExternalFilename) {
372  bool IsStandalone =
374  return std::make_unique<BitstreamMetaSerializer>(
375  OS,
378  &*StrTab, ExternalFilename);
379 }
380 
384  ExternalFilename);
386 }
const NoneType None
Definition: None.h:23
uint64_t CallInst * C
BitstreamRemarkSerializer(raw_ostream &OS, SerializerMode Mode)
Construct a serializer that will create its own string table.
void setupRemarkBlockInfo()
The block info for the remarks block.
Serializer of metadata for bitstream remarks.
constexpr StringRef RemarkBlockName
void setupMetaStrTab()
The strtab in the metadata block.
SI Whole Quad Mode
This class represents lattice values for constants.
Definition: AllocatorList.h:23
StringRef PassName
Name of the pass that triggers the emission of this remark.
Definition: Remark.h:72
void push_back(const T &Elt)
Definition: SmallVector.h:211
void setupBlockInfo()
Set up the necessary block info entries according to the container type.
constexpr StringRef RemarkArgWithDebugLocName
BitstreamRemarkContainerType ContainerType
The type of the container we are serializing.
Optional< uint64_t > Hotness
If profile information is available, this is the number of times the corresponding code was executed ...
Definition: Remark.h:87
void emitMetaBlock(uint64_t ContainerVersion, Optional< uint64_t > RemarkVersion, Optional< const StringTable *> StrTab=None, Optional< StringRef > Filename=None)
Emit the metadata for the remarks.
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25
constexpr StringRef MetaContainerInfoName
A remark type used for both emission and parsing.
Definition: Remark.h:67
void serialize(raw_ostream &OS) const
Serialize the string table to a stream.
void Emit(uint32_t Val, unsigned NumBits)
StringRef RemarkName
Textual identifier for the remark (single-word, camel-case).
Definition: Remark.h:77
void EnterBlockInfoBlock()
EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
Optional< RemarkLocation > Loc
Definition: Remark.h:47
uint64_t RecordMetaContainerInfoAbbrevID
Abbrev IDs initialized in the block info block.
std::pair< unsigned, StringRef > add(StringRef Str)
Add a string to the table. It returns an unique ID of the string.
Key
PAL metadata keys.
constexpr StringRef MetaExternalFileName
void EmitRecordWithAbbrev(unsigned Abbrev, const Container &Vals)
EmitRecordWithAbbrev - Emit a record with the specified abbreviation.
void setupMetaExternalFile()
The external file in the metadata block.
constexpr StringRef RemarkArgWithoutDebugLocName
SerializerMode Mode
The serialization mode.
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 ...
The string table used for serializing remarks.
Optional< RemarkLocation > Loc
The location in the source file of the remark.
Definition: Remark.h:83
void EnterSubblock(unsigned BlockID, unsigned CodeLen)
constexpr StringRef RemarkHotnessName
static void push(SmallVectorImpl< uint64_t > &R, StringRef Str)
std::unique_ptr< MetaSerializer > metaSerializer(raw_ostream &OS, Optional< StringRef > ExternalFilename=None) override
The metadata serializer associated to this remark serializer.
constexpr StringRef MetaStrTabName
BitstreamWriter Bitstream
The Bitstream writer.
static void setRecordName(unsigned RecordID, BitstreamWriter &Bitstream, SmallVectorImpl< uint64_t > &R, StringRef Str)
BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
Definition: BitCodes.h:95
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
StringRef getBuffer()
Finalize the writing to a buffer.
One remark entry is represented using a REMARK_BLOCK.
size_t size() const
Definition: SmallVector.h:52
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:519
raw_ostream & write(unsigned char C)
This is the base class for a remark metadata serializer.
constexpr StringLiteral ContainerMagic("RMRK")
The magic number used for identifying remark blocks.
constexpr StringRef MetaRemarkVersionName
raw_ostream & OS
The open raw_ostream that the remark diagnostics are emitted to.
This is the base class for a remark serializer.
The metadata block is mandatory.
void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals, StringRef Blob)
EmitRecordWithBlob - Emit the specified record to the stream, using an abbrev that includes a blob at...
void emitRemarkBlock(const Remark &Remark, StringTable &StrTab)
Emit a remark block. The string table is required.
constexpr uint64_t CurrentContainerVersion
The current version of the remark container.
A key-value pair with a debug location that is used to display the remarks at the right place in the ...
Definition: Remark.h:42
Type RemarkType
The type of the remark.
Definition: Remark.h:69
StringRef FunctionName
Mangled name of the function that triggers the emssion of this remark.
Definition: Remark.h:80
void emit(const Remark &Remark) override
Emit a remark to the stream.
void setupMetaRemarkVersion()
The remark version in the metadata block.
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:144
void setupMetaBlockInfo()
Set up the block info for the metadata block.
bool DidSetUp
The file should contain: 1) The block info block that describes how to read the blocks.
SmallVector< char, 1024 > Encoded
Buffer used for encoding the bitstream before writing it to the final stream.
SmallVector< uint64_t, 64 > R
Buffer used to construct records and pass to the bitstream writer.
BitstreamRemarkSerializerHelper Helper
The helper to emit bitstream.
constexpr StringRef RemarkHeaderName
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void flushToStream(raw_ostream &OS)
Finalize the writing to OS.
BitstreamRemarkSerializerHelper(BitstreamRemarkContainerType ContainerType)
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:503
SmallVector< Argument, 5 > Args
Arguments collected via the streaming interface.
Definition: Remark.h:90
static void initBlock(unsigned BlockID, BitstreamWriter &Bitstream, SmallVectorImpl< uint64_t > &R, StringRef Str)
constexpr StringRef MetaBlockName
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
constexpr uint64_t CurrentRemarkVersion
The current version of the remark entry.
Definition: Remark.h:27
constexpr StringRef RemarkDebugLocName
BitstreamRemarkContainerType
Type of the remark container.
Optional< StringTable > StrTab
The string table containing all the unique strings used in the output.
unsigned EmitBlockInfoAbbrev(unsigned BlockID, std::shared_ptr< BitCodeAbbrev > Abbv)
EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified BlockID.