LLVM 22.0.0git
BitstreamRemarkParser.h
Go to the documentation of this file.
1//===-- BitstreamRemarkParser.h - Parser for Bitstream remarks --*- 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 the impementation of the Bitstream remark parser.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H
14#define LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Error.h"
23#include <array>
24#include <cstdint>
25#include <memory>
26#include <optional>
27
28namespace llvm {
29namespace remarks {
30
31struct Remark;
32
33/// Helper to parse a META_BLOCK for a bitstream remark container.
35 /// The Bitstream reader.
37 /// Reference to the storage for the block info.
39 /// The parsed content: depending on the container type, some fields might be
40 /// empty.
41 std::optional<uint64_t> ContainerVersion;
42 std::optional<uint8_t> ContainerType;
43 std::optional<StringRef> StrTabBuf;
44 std::optional<StringRef> ExternalFilePath;
45 std::optional<uint64_t> RemarkVersion;
46
47 /// Continue parsing with \p Stream. \p Stream is expected to contain a
48 /// ENTER_SUBBLOCK to the META_BLOCK at the current position.
49 /// \p Stream is expected to have a BLOCKINFO_BLOCK set.
52
53 /// Parse the META_BLOCK and fill the available entries.
54 /// This helper does not check for the validity of the fields.
55 Error parse();
56};
57
58/// Helper to parse a REMARK_BLOCK for a bitstream remark container.
60 /// The Bitstream reader.
62 /// The parsed content: depending on the remark, some fields might be empty.
63 std::optional<uint8_t> Type;
64 std::optional<uint64_t> RemarkNameIdx;
65 std::optional<uint64_t> PassNameIdx;
66 std::optional<uint64_t> FunctionNameIdx;
67 std::optional<uint64_t> SourceFileNameIdx;
68 std::optional<uint32_t> SourceLine;
69 std::optional<uint32_t> SourceColumn;
70 std::optional<uint64_t> Hotness;
71 struct Argument {
72 std::optional<uint64_t> KeyIdx;
73 std::optional<uint64_t> ValueIdx;
74 std::optional<uint64_t> SourceFileNameIdx;
75 std::optional<uint32_t> SourceLine;
76 std::optional<uint32_t> SourceColumn;
77 };
78 std::optional<ArrayRef<Argument>> Args;
79 /// Avoid re-allocating a vector every time.
81
82 /// Continue parsing with \p Stream. \p Stream is expected to contain a
83 /// ENTER_SUBBLOCK to the REMARK_BLOCK at the current position.
84 /// \p Stream is expected to have a BLOCKINFO_BLOCK set and to have already
85 /// parsed the META_BLOCK.
87
88 /// Parse the REMARK_BLOCK and fill the available entries.
89 /// This helper does not check for the validity of the fields.
90 Error parse();
91};
92
93/// Helper to parse any bitstream remark container.
95 /// The Bitstream reader.
97 /// The block info block.
99 /// Start parsing at \p Buffer.
101 /// Parse the magic number.
103 /// Parse the block info block containing all the abbrevs.
104 /// This needs to be called before calling any other parsing function.
106 /// Return true if the next block is a META_BLOCK. This function does not move
107 /// the cursor.
109 /// Return true if the next block is a REMARK_BLOCK. This function does not
110 /// move the cursor.
112 /// Return true if the parser reached the end of the stream.
113 bool atEndOfStream() { return Stream.AtEndOfStream(); }
114 /// Jump to the end of the stream, skipping everything.
115 void skipToEnd() { return Stream.skipToEnd(); }
116};
117
118/// Parses and holds the state of the latest parsed remark.
120 /// The buffer to parse.
122 /// The string table used for parsing strings.
123 std::optional<ParsedStringTable> StrTab;
124 /// Temporary remark buffer used when the remarks are stored separately.
125 std::unique_ptr<MemoryBuffer> TmpRemarkBuffer;
126 /// The common metadata used to decide how to parse the buffer.
127 /// This is filled when parsing the metadata block.
132 /// Wether the parser is ready to parse remarks.
134
135 /// Create a parser that expects to find a string table embedded in the
136 /// stream.
137 explicit BitstreamRemarkParser(StringRef Buf);
138
140
141 static bool classof(const RemarkParser *P) {
142 return P->ParserFormat == Format::Bitstream;
143 }
144
145 /// Parse and process the metadata of the buffer.
147
148 /// Parse a Bitstream remark.
150
151private:
152 /// Helper functions.
153 Error processCommonMeta(BitstreamMetaParserHelper &Helper);
154 Error processStandaloneMeta(BitstreamMetaParserHelper &Helper);
155 Error processSeparateRemarksFileMeta(BitstreamMetaParserHelper &Helper);
156 Error processSeparateRemarksMetaMeta(BitstreamMetaParserHelper &Helper);
158 processRemark(BitstreamRemarkParserHelper &Helper);
159 Error processExternalFilePath(std::optional<StringRef> ExternalFilePath);
160};
161
163 StringRef Buf,
164 std::optional<StringRef> ExternalFilePrependPath = std::nullopt);
165
166} // end namespace remarks
167} // end namespace llvm
168
169#endif /* LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H */
#define P(N)
This class maintains the abbreviations read from a block info block.
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
BitstreamRemarkContainerType
Type of the remark container.
Expected< std::unique_ptr< BitstreamRemarkParser > > createBitstreamParserFromMeta(StringRef Buf, std::optional< StringRef > ExternalFilePrependPath=std::nullopt)
This is an optimization pass for GlobalISel generic memory operations.
Helper to parse a META_BLOCK for a bitstream remark container.
std::optional< uint64_t > ContainerVersion
The parsed content: depending on the container type, some fields might be empty.
BitstreamCursor & Stream
The Bitstream reader.
Error parse()
Parse the META_BLOCK and fill the available entries.
BitstreamBlockInfo & BlockInfo
Reference to the storage for the block info.
BitstreamMetaParserHelper(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo)
Continue parsing with Stream.
Helper to parse any bitstream remark container.
BitstreamParserHelper(StringRef Buffer)
Start parsing at Buffer.
Expected< bool > isRemarkBlock()
Return true if the next block is a REMARK_BLOCK.
bool atEndOfStream()
Return true if the parser reached the end of the stream.
void skipToEnd()
Jump to the end of the stream, skipping everything.
Expected< std::array< char, 4 > > parseMagic()
Parse the magic number.
BitstreamBlockInfo BlockInfo
The block info block.
BitstreamCursor Stream
The Bitstream reader.
Expected< bool > isMetaBlock()
Return true if the next block is a META_BLOCK.
Error parseBlockInfoBlock()
Parse the block info block containing all the abbrevs.
Helper to parse a REMARK_BLOCK for a bitstream remark container.
std::optional< uint8_t > Type
The parsed content: depending on the remark, some fields might be empty.
std::optional< ArrayRef< Argument > > Args
BitstreamCursor & Stream
The Bitstream reader.
SmallVector< Argument, 8 > TmpArgs
Avoid re-allocating a vector every time.
BitstreamRemarkParserHelper(BitstreamCursor &Stream)
Continue parsing with Stream.
Error parse()
Parse the REMARK_BLOCK and fill the available entries.
Error parseMeta()
Parse and process the metadata of the buffer.
std::optional< ParsedStringTable > StrTab
The string table used for parsing strings.
Expected< std::unique_ptr< Remark > > parseRemark()
Parse a Bitstream remark.
std::unique_ptr< MemoryBuffer > TmpRemarkBuffer
Temporary remark buffer used when the remarks are stored separately.
uint64_t ContainerVersion
The common metadata used to decide how to parse the buffer.
BitstreamParserHelper ParserHelper
The buffer to parse.
Expected< std::unique_ptr< Remark > > next() override
If no error occurs, this returns a valid Remark object.
static bool classof(const RemarkParser *P)
BitstreamRemarkParser(StringRef Buf)
Create a parser that expects to find a string table embedded in the stream.
BitstreamRemarkContainerType ContainerType
bool ReadyToParseRemarks
Wether the parser is ready to parse remarks.
RemarkParser(Format ParserFormat)
A remark type used for both emission and parsing.
Definition Remark.h:98