1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
//===-- BitstreamRemarkParser.h - Parser for Bitstream remarks --*- C++/-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides the impementation of the Bitstream remark parser.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H
#define LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H
#include "llvm/ADT/StringRef.h"
#include "llvm/Bitstream/BitstreamReader.h"
#include "llvm/Remarks/BitstreamRemarkContainer.h"
#include "llvm/Remarks/Remark.h"
#include "llvm/Remarks/RemarkFormat.h"
#include "llvm/Remarks/RemarkParser.h"
#include "llvm/Remarks/RemarkStringTable.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FormatVariadic.h"
#include <cstdint>
#include <memory>
#include <optional>
namespace llvm {
namespace remarks {
class BitstreamBlockParserHelperBase {
protected:
BitstreamCursor &Stream;
StringRef BlockName;
unsigned BlockID;
public:
BitstreamBlockParserHelperBase(BitstreamCursor &Stream, unsigned BlockID,
StringRef BlockName)
: Stream(Stream), BlockName(BlockName), BlockID(BlockID) {}
template <typename... Ts> Error error(char const *Fmt, const Ts &...Vals) {
std::string Buffer;
raw_string_ostream OS(Buffer);
OS << "Error while parsing " << BlockName << " block: ";
OS << formatv(Fmt, Vals...);
return make_error<StringError>(
std::move(Buffer),
std::make_error_code(std::errc::illegal_byte_sequence));
}
Error expectBlock();
protected:
Error enterBlock();
Error unknownRecord(unsigned AbbrevID);
Error unexpectedRecord(StringRef RecordName);
Error malformedRecord(StringRef RecordName);
Error unexpectedBlock(unsigned Code);
};
template <typename Derived>
class BitstreamBlockParserHelper : public BitstreamBlockParserHelperBase {
protected:
using BitstreamBlockParserHelperBase::BitstreamBlockParserHelperBase;
Derived &derived() { return *static_cast<Derived *>(this); }
/// Parse a record and fill in the fields in the parser.
/// The subclass must statically override this method.
Error parseRecord(unsigned Code) = delete;
/// Parse a subblock and fill in the fields in the parser.
/// The subclass can statically override this method.
Error parseSubBlock(unsigned Code) { return unexpectedBlock(Code); }
public:
/// Enter, parse, and leave this bitstream block. This expects the
/// BitstreamCursor to be right after the SubBlock entry (i.e. after calling
/// expectBlock).
Error parseBlock() {
if (Error E = enterBlock())
return E;
// Stop when there is nothing to read anymore or when we encounter an
// END_BLOCK.
while (true) {
Expected<BitstreamEntry> Next = Stream.advance();
if (!Next)
return Next.takeError();
switch (Next->Kind) {
case BitstreamEntry::SubBlock:
if (Error E = derived().parseSubBlock(Next->ID))
return E;
continue;
case BitstreamEntry::EndBlock:
return Error::success();
case BitstreamEntry::Record:
if (Error E = derived().parseRecord(Next->ID))
return E;
continue;
case BitstreamEntry::Error:
return error("Unexpected end of bitstream.");
}
llvm_unreachable("Unexpected BitstreamEntry");
}
}
};
/// Helper to parse a META_BLOCK for a bitstream remark container.
class BitstreamMetaParserHelper
: public BitstreamBlockParserHelper<BitstreamMetaParserHelper> {
friend class BitstreamBlockParserHelper;
public:
struct ContainerInfo {
uint64_t Version;
uint64_t Type;
};
/// The parsed content: depending on the container type, some fields might
/// be empty.
std::optional<ContainerInfo> Container;
std::optional<uint64_t> RemarkVersion;
std::optional<StringRef> ExternalFilePath;
std::optional<StringRef> StrTabBuf;
BitstreamMetaParserHelper(BitstreamCursor &Stream)
: BitstreamBlockParserHelper(Stream, META_BLOCK_ID, MetaBlockName) {}
protected:
Error parseRecord(unsigned Code);
};
/// Helper to parse a REMARK_BLOCK for a bitstream remark container.
class BitstreamRemarkParserHelper
: public BitstreamBlockParserHelper<BitstreamRemarkParserHelper> {
friend class BitstreamBlockParserHelper;
protected:
SmallVector<uint64_t, 5> Record;
StringRef RecordBlob;
unsigned RecordID;
public:
struct RemarkLoc {
uint64_t SourceFileNameIdx;
uint64_t SourceLine;
uint64_t SourceColumn;
};
struct Argument {
std::optional<uint64_t> KeyIdx;
std::optional<uint64_t> ValueIdx;
std::optional<RemarkLoc> Loc;
Argument(std::optional<uint64_t> KeyIdx, std::optional<uint64_t> ValueIdx)
: KeyIdx(KeyIdx), ValueIdx(ValueIdx) {}
};
/// The parsed content: depending on the remark, some fields might be empty.
std::optional<uint8_t> Type;
std::optional<uint64_t> RemarkNameIdx;
std::optional<uint64_t> PassNameIdx;
std::optional<uint64_t> FunctionNameIdx;
std::optional<uint64_t> Hotness;
std::optional<RemarkLoc> Loc;
SmallVector<Argument, 8> Args;
BitstreamRemarkParserHelper(BitstreamCursor &Stream)
: BitstreamBlockParserHelper(Stream, REMARK_BLOCK_ID, RemarkBlockName) {}
/// Clear helper state and parse next remark block.
Error parseNext();
protected:
Error parseRecord(unsigned Code);
Error handleRecord();
};
/// Helper to parse any bitstream remark container.
struct BitstreamParserHelper {
/// The Bitstream reader.
BitstreamCursor Stream;
/// The block info block.
BitstreamBlockInfo BlockInfo;
/// Helper to parse the metadata blocks in this bitstream.
BitstreamMetaParserHelper MetaHelper;
/// Helper to parse the remark blocks in this bitstream. Only needed
/// for ContainerType RemarksFile.
std::optional<BitstreamRemarkParserHelper> RemarksHelper;
/// The position of the first remark block we encounter after
/// the initial metadata block.
std::optional<uint64_t> RemarkStartBitPos;
/// Start parsing at \p Buffer.
BitstreamParserHelper(StringRef Buffer)
: Stream(Buffer), MetaHelper(Stream), RemarksHelper(Stream) {}
/// Parse and validate the magic number.
Error expectMagic();
/// Parse the block info block containing all the abbrevs.
/// This needs to be called before calling any other parsing function.
Error parseBlockInfoBlock();
/// Parse all metadata blocks in the file. This populates the meta helper.
Error parseMeta();
/// Parse the next remark. This populates the remark helper data.
Error parseRemark();
};
/// Parses and holds the state of the latest parsed remark.
struct BitstreamRemarkParser : public RemarkParser {
/// The buffer to parse.
std::optional<BitstreamParserHelper> ParserHelper;
/// The string table used for parsing strings.
std::optional<ParsedStringTable> StrTab;
/// Temporary remark buffer used when the remarks are stored separately.
std::unique_ptr<MemoryBuffer> TmpRemarkBuffer;
/// Whether the metadata has already been parsed, so we can continue parsing
/// remarks.
bool IsMetaReady = false;
/// The common metadata used to decide how to parse the buffer.
/// This is filled when parsing the metadata block.
uint64_t ContainerVersion = 0;
uint64_t RemarkVersion = 0;
BitstreamRemarkContainerType ContainerType =
BitstreamRemarkContainerType::RemarksFile;
/// Create a parser that expects to find a string table embedded in the
/// stream.
explicit BitstreamRemarkParser(StringRef Buf);
Expected<std::unique_ptr<Remark>> next() override;
static bool classof(const RemarkParser *P) {
return P->ParserFormat == Format::Bitstream;
}
/// Parse and process the metadata of the buffer.
Error parseMeta();
private:
Error processCommonMeta();
Error processFileContainerMeta();
Error processExternalFilePath();
Expected<std::unique_ptr<Remark>> processRemark();
Error processStrTab();
Error processRemarkVersion();
};
Expected<std::unique_ptr<BitstreamRemarkParser>> createBitstreamParserFromMeta(
StringRef Buf,
std::optional<StringRef> ExternalFilePrependPath = std::nullopt);
} // end namespace remarks
} // end namespace llvm
#endif /* LLVM_LIB_REMARKS_BITSTREAM_REMARK_PARSER_H */
|