LLVM 24.0.0git
BitstreamRemarkParser.cpp
Go to the documentation of this file.
1//===- BitstreamRemarkParser.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 utility methods used by clients that want to use the
10// parser for remark diagnostics in LLVM.
11//
12//===----------------------------------------------------------------------===//
13
16#include "llvm/Support/Path.h"
17#include <optional>
18
19using namespace llvm;
20using namespace llvm::remarks;
21
22namespace {
23
24template <typename... Ts> Error error(char const *Fmt, const Ts &...Vals) {
25 std::string Buffer;
26 raw_string_ostream OS(Buffer);
27 OS << formatv(Fmt, Vals...);
29 std::move(Buffer),
30 std::make_error_code(std::errc::illegal_byte_sequence));
31}
32
33} // namespace
34
36 return error("Unknown record entry ({}).", AbbrevID);
37}
38
40 return error("Unexpected record entry ({}).", RecordName);
41}
42
44 return error("Malformed record entry ({}).", RecordName);
45}
46
48 return error("Unexpected subblock ({}).", Code);
49}
50
53 if (!Next)
54 return Next.takeError();
55 switch (Next->Kind) {
57 return Next->ID;
60 return error("Expected subblock, but got unexpected record.");
62 return error("Expected subblock, but got unexpected end of bitstream.");
63 }
64 llvm_unreachable("Unexpected BitstreamEntry");
65}
66
68 auto MaybeBlockID = expectSubBlock(Stream);
69 if (!MaybeBlockID)
70 return MaybeBlockID.takeError();
71 if (*MaybeBlockID != BlockID)
72 return error("Expected {} block, but got unexpected block ({}).", BlockName,
73 *MaybeBlockID);
74 return Error::success();
75}
76
80
82 // Note: 2 is used here because it's the max number of fields we have per
83 // record.
85 StringRef Blob;
86 Expected<unsigned> RecordID = Stream.readRecord(Code, Record, &Blob);
87 if (!RecordID)
88 return RecordID.takeError();
89
90 switch (*RecordID) {
92 if (Record.size() != 2)
94 Container = {Record[0], Record[1]};
95 // Error immediately if container version is outdated, so the user sees an
96 // explanation instead of a parser error.
97 if (Container->Version != CurrentContainerVersion) {
98 return ::error(
99 "Unsupported remark container version (expected: {}, read: {}). "
100 "Please upgrade/downgrade your toolchain to read this container.",
102 }
103 break;
104 }
106 if (Record.size() != 1)
109 // Error immediately if remark version is outdated, so the user sees an
110 // explanation instead of a parser error.
112 return ::error(
113 "Unsupported remark version in container (expected: {}, read: {}). "
114 "Please upgrade/downgrade your toolchain to read this container.",
116 }
117 break;
118 }
119 case RECORD_META_STRTAB: {
120 if (Record.size() != 0)
122 StrTabBuf = Blob;
123 break;
124 }
126 if (Record.size() != 0)
128 ExternalFilePath = Blob;
129 break;
130 }
131 default:
132 return unknownRecord(*RecordID);
133 }
134 return Error::success();
135}
136
138 Record.clear();
139 Expected<unsigned> MaybeRecordID =
140 Stream.readRecord(Code, Record, &RecordBlob);
141 if (!MaybeRecordID)
142 return MaybeRecordID.takeError();
143 RecordID = *MaybeRecordID;
144 return handleRecord();
145}
146
148 switch (RecordID) {
150 if (Record.size() != 4)
152 Type = Record[0];
154 PassNameIdx = Record[2];
156 break;
157 }
159 if (Record.size() != 3)
161 Loc = {Record[0], Record[1], Record[2]};
162 break;
163 }
165 if (Record.size() != 1)
167 Hotness = Record[0];
168 break;
169 }
171 if (Record.size() != 5)
173 auto &Arg = Args.emplace_back(Record[0], Record[1]);
174 Arg.Loc = {Record[2], Record[3], Record[4]};
175 break;
176 }
178 if (Record.size() != 2)
180 Args.emplace_back(Record[0], Record[1]);
181 break;
182 }
183 default:
184 return unknownRecord(RecordID);
185 }
186 return Error::success();
187}
188
190 Type.reset();
191 RemarkNameIdx.reset();
192 PassNameIdx.reset();
193 FunctionNameIdx.reset();
194 Hotness.reset();
195 Loc.reset();
196 Args.clear();
197
198 return parseBlock();
199}
200
202 std::array<char, 4> Result;
203 for (unsigned I = 0; I < 4; ++I)
204 if (Expected<unsigned> R = Stream.Read(8))
205 Result[I] = *R;
206 else
207 return R.takeError();
208
209 StringRef MagicNumber{Result.data(), Result.size()};
211 return error("Unknown magic number: expecting {}, got {}.",
213 return Error::success();
214}
215
218 if (!Next)
219 return Next.takeError();
220 if (Next->Kind != BitstreamEntry::SubBlock ||
222 return error(
223 "Error while parsing BLOCKINFO_BLOCK: expecting [ENTER_SUBBLOCK, "
224 "BLOCKINFO_BLOCK, ...].");
225
227 Stream.ReadBlockInfoBlock();
228 if (!MaybeBlockInfo)
229 return MaybeBlockInfo.takeError();
230
231 if (!*MaybeBlockInfo)
232 return error("Missing BLOCKINFO_BLOCK.");
233
234 BlockInfo = **MaybeBlockInfo;
235
236 Stream.setBlockInfo(&BlockInfo);
237 return Error::success();
238}
239
241 if (Error E = expectMagic())
242 return E;
243 if (Error E = parseBlockInfoBlock())
244 return E;
245
246 // Parse early meta block.
247 if (Error E = MetaHelper.expectBlock())
248 return E;
249 if (Error E = MetaHelper.parseBlock())
250 return E;
251
252 // Skip all Remarks blocks.
253 while (!Stream.AtEndOfStream()) {
254 auto MaybeBlockID = expectSubBlock(Stream);
255 if (!MaybeBlockID)
256 return MaybeBlockID.takeError();
257 if (*MaybeBlockID == META_BLOCK_ID)
258 break;
259 if (*MaybeBlockID != REMARK_BLOCK_ID)
260 return error("Unexpected block between meta blocks.");
261 // Remember first remark block.
263 RemarkStartBitPos = Stream.GetCurrentBitNo();
264 if (Error E = Stream.SkipBlock())
265 return E;
266 }
267
268 // Late meta block is optional if there are no remarks.
269 if (Stream.AtEndOfStream())
270 return Error::success();
271
272 // Parse late meta block.
273 if (Error E = MetaHelper.parseBlock())
274 return E;
275 return Error::success();
276}
277
279 if (RemarkStartBitPos) {
280 RemarkStartBitPos.reset();
281 } else {
282 auto MaybeBlockID = expectSubBlock(Stream);
283 if (!MaybeBlockID)
284 return MaybeBlockID.takeError();
285 if (*MaybeBlockID != REMARK_BLOCK_ID)
287 }
288 return RemarksHelper->parseNext();
289}
290
293 StringRef Buf, std::optional<StringRef> ExternalFilePrependPath) {
294 auto Parser = std::make_unique<BitstreamRemarkParser>(Buf);
295
296 if (ExternalFilePrependPath)
297 Parser->ExternalFilePrependPath = std::string(*ExternalFilePrependPath);
298
299 return std::move(Parser);
300}
301
304
306 if (!IsMetaReady) {
307 // Container is completely empty.
308 if (ParserHelper->Stream.AtEndOfStream())
310
311 if (Error E = parseMeta())
312 return std::move(E);
313 IsMetaReady = true;
314
315 // Container has meta, but no remarks blocks.
316 if (!ParserHelper->RemarkStartBitPos)
317 return error(
318 "Container is non-empty, but does not contain any remarks blocks.");
319
320 if (Error E =
321 ParserHelper->Stream.JumpToBit(*ParserHelper->RemarkStartBitPos))
322 return std::move(E);
323 ParserHelper->RemarksHelper.emplace(ParserHelper->Stream);
324 }
325
326 if (Error E = ParserHelper->parseRemark())
327 return std::move(E);
328 return processRemark();
329}
330
332 if (Error E = ParserHelper->parseMeta())
333 return E;
334 if (Error E = processCommonMeta())
335 return E;
336
337 switch (ContainerType) {
339 return processExternalFilePath();
341 return processFileContainerMeta();
342 }
343 llvm_unreachable("Unknown BitstreamRemarkContainerType enum");
344}
345
346Error BitstreamRemarkParser::processCommonMeta() {
347 auto &Helper = ParserHelper->MetaHelper;
348 if (!Helper.Container)
349 return Helper.error("Missing container info.");
350 auto &Container = *Helper.Container;
351 ContainerVersion = Container.Version;
352 // Always >= BitstreamRemarkContainerType::First since it's unsigned.
353 if (Container.Type > static_cast<uint8_t>(BitstreamRemarkContainerType::Last))
354 return Helper.error("Invalid container type.");
355 ContainerType = static_cast<BitstreamRemarkContainerType>(Container.Type);
356 return Error::success();
357}
358
359Error BitstreamRemarkParser::processFileContainerMeta() {
360 if (Error E = processRemarkVersion())
361 return E;
362 if (Error E = processStrTab())
363 return E;
364 return Error::success();
365}
366
367Error BitstreamRemarkParser::processStrTab() {
368 auto &Helper = ParserHelper->MetaHelper;
369 if (!Helper.StrTabBuf)
370 return Helper.error("Missing string table.");
371 // Parse and assign the string table.
372 StrTab.emplace(*Helper.StrTabBuf);
373 return Error::success();
374}
375
376Error BitstreamRemarkParser::processRemarkVersion() {
377 auto &Helper = ParserHelper->MetaHelper;
378 if (!Helper.RemarkVersion)
379 return Helper.error("Missing remark version.");
380 RemarkVersion = *Helper.RemarkVersion;
381 return Error::success();
382}
383
384Error BitstreamRemarkParser::processExternalFilePath() {
385 auto &Helper = ParserHelper->MetaHelper;
386 if (!Helper.ExternalFilePath)
387 return Helper.error("Missing external file path.");
388
389 SmallString<80> FullPath(ExternalFilePrependPath);
390 sys::path::append(FullPath, *Helper.ExternalFilePath);
391
392 // External file: open the external file, parse it, check if its metadata
393 // matches the one from the separate metadata, then replace the current
394 // parser with the one parsing the remarks.
395 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
396 MemoryBuffer::getFile(FullPath);
397 if (std::error_code EC = BufferOrErr.getError())
398 return createFileError(FullPath, EC);
399
400 TmpRemarkBuffer = std::move(*BufferOrErr);
401
402 // Don't try to parse the file if it's empty.
403 if (TmpRemarkBuffer->getBufferSize() == 0)
405
406 // Create a separate parser used for parsing the separate file.
407 ParserHelper.emplace(TmpRemarkBuffer->getBuffer());
408 if (Error E = parseMeta())
409 return E;
410
412 return ParserHelper->MetaHelper.error(
413 "Wrong container type in external file.");
414
415 return Error::success();
416}
417
418Expected<std::unique_ptr<Remark>> BitstreamRemarkParser::processRemark() {
419 auto &Helper = *ParserHelper->RemarksHelper;
420 std::unique_ptr<Remark> Result = std::make_unique<Remark>();
421 Remark &R = *Result;
422
423 if (!StrTab)
424 return Helper.error("Missing string table.");
425
426 if (!Helper.Type)
427 return Helper.error("Missing remark type.");
428
429 // Type::Unknown marks a remark without a type.
430 if (*Helper.Type == static_cast<uint8_t>(Type::Unknown) ||
431 *Helper.Type > static_cast<uint8_t>(Type::Last))
432 return Helper.error("Unknown remark type.");
433
434 R.RemarkType = static_cast<Type>(*Helper.Type);
435
436 if (!Helper.RemarkNameIdx)
437 return Helper.error("Missing remark name.");
438
439 if (Expected<StringRef> RemarkName = (*StrTab)[*Helper.RemarkNameIdx])
440 R.RemarkName = *RemarkName;
441 else
442 return RemarkName.takeError();
443
444 if (!Helper.PassNameIdx)
445 return Helper.error("Missing remark pass.");
446
447 if (Expected<StringRef> PassName = (*StrTab)[*Helper.PassNameIdx])
448 R.PassName = *PassName;
449 else
450 return PassName.takeError();
451
452 if (!Helper.FunctionNameIdx)
453 return Helper.error("Missing remark function name.");
454
455 if (Expected<StringRef> FunctionName = (*StrTab)[*Helper.FunctionNameIdx])
456 R.FunctionName = *FunctionName;
457 else
458 return FunctionName.takeError();
459
460 if (Helper.Loc) {
461 Expected<StringRef> SourceFileName =
462 (*StrTab)[Helper.Loc->SourceFileNameIdx];
463 if (!SourceFileName)
464 return SourceFileName.takeError();
465 R.Loc.emplace();
466 R.Loc->SourceFilePath = *SourceFileName;
467 R.Loc->SourceLine = Helper.Loc->SourceLine;
468 R.Loc->SourceColumn = Helper.Loc->SourceColumn;
469 }
470
471 if (Helper.Hotness)
472 R.Hotness = *Helper.Hotness;
473
474 for (const BitstreamRemarkParserHelper::Argument &Arg : Helper.Args) {
475 if (!Arg.KeyIdx)
476 return Helper.error("Missing key in remark argument.");
477 if (!Arg.ValueIdx)
478 return Helper.error("Missing value in remark argument.");
479
480 // We have at least a key and a value, create an entry.
481 auto &RArg = R.Args.emplace_back();
482
483 if (Expected<StringRef> Key = (*StrTab)[*Arg.KeyIdx])
484 RArg.Key = *Key;
485 else
486 return Key.takeError();
487
488 if (Expected<StringRef> Value = (*StrTab)[*Arg.ValueIdx])
489 RArg.Val = *Value;
490 else
491 return Value.takeError();
492
493 if (Arg.Loc) {
494 if (Expected<StringRef> SourceFileName =
495 (*StrTab)[Arg.Loc->SourceFileNameIdx]) {
496 RArg.Loc.emplace();
497 RArg.Loc->SourceFilePath = *SourceFileName;
498 RArg.Loc->SourceLine = Arg.Loc->SourceLine;
499 RArg.Loc->SourceColumn = Arg.Loc->SourceColumn;
500 } else
501 return SourceFileName.takeError();
502 }
503 }
504
505 return std::move(Result);
506}
static Expected< unsigned > expectSubBlock(BitstreamCursor &Stream)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define I(x, y, z)
Definition MD5.cpp:57
#define error(X)
static const char PassName[]
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
Expected< BitstreamEntry > advance(unsigned Flags=0)
Advance the current bitstream, returning the next entry in the stream.
std::error_code getError() const
Definition ErrorOr.h:152
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
A raw_ostream that writes to an std::string.
std::optional< ContainerInfo > Container
The parsed content: depending on the container type, some fields might be empty.
std::optional< uint8_t > Type
The parsed content: depending on the remark, some fields might be empty.
Error parseNext()
Clear helper state and parse next remark block.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BLOCKINFO_BLOCK_ID
BLOCKINFO_BLOCK is used to define metadata about blocks, for example, standard abbrevs that should be...
constexpr StringLiteral RemarkDebugLocName("Remark debug location")
BitstreamRemarkContainerType
Type of the remark container.
@ RemarksFileExternal
Emit a link to an external remarks file (usually as a section of the object file, to enable discovery...
@ RemarksFile
Emit metadata and remarks into a file RemarksFile: | Meta: | | Container info | | Remark version | Re...
constexpr StringLiteral RemarkArgWithDebugLocName("Argument with debug location")
@ REMARK_BLOCK_ID
One remark entry is represented using a REMARK_BLOCK.
@ META_BLOCK_ID
The metadata block is mandatory.
constexpr uint64_t CurrentContainerVersion
The current version of the remark container.
constexpr StringLiteral MetaExternalFileName("External File")
constexpr StringLiteral MetaRemarkVersionName("Remark version")
Format
The format used for serializing/deserializing remarks.
constexpr StringLiteral MetaContainerInfoName("Container info")
Expected< std::unique_ptr< BitstreamRemarkParser > > createBitstreamParserFromMeta(StringRef Buf, std::optional< StringRef > ExternalFilePrependPath=std::nullopt)
constexpr StringLiteral RemarkHeaderName("Remark header")
constexpr StringLiteral RemarkArgWithoutDebugLocName("Argument")
constexpr uint64_t CurrentRemarkVersion
The current version of the remark entry.
Definition Remark.h:29
constexpr StringLiteral ContainerMagic("RMRK")
The magic number used for identifying remark blocks.
constexpr StringLiteral MetaStrTabName("String table")
Type
The type of the remark.
Definition Remark.h:75
constexpr StringLiteral RemarkHotnessName("Remark hotness")
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:467
This is an optimization pass for GlobalISel generic memory operations.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1415
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
Error parseRemark()
Parse the next remark. This populates the remark helper data.
Error parseMeta()
Parse all metadata blocks in the file. This populates the meta helper.
BitstreamMetaParserHelper MetaHelper
Helper to parse the metadata blocks in this bitstream.
BitstreamBlockInfo BlockInfo
The block info block.
BitstreamCursor Stream
The Bitstream reader.
std::optional< uint64_t > RemarkStartBitPos
The position of the first remark block we encounter after the initial metadata block.
std::optional< BitstreamRemarkParserHelper > RemarksHelper
Helper to parse the remark blocks in this bitstream.
Error parseBlockInfoBlock()
Parse the block info block containing all the abbrevs.
Error expectMagic()
Parse and validate the magic number.
Error parseMeta()
Parse and process the metadata of the buffer.
std::optional< ParsedStringTable > StrTab
The string table used for parsing strings.
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.
std::optional< BitstreamParserHelper > ParserHelper
The buffer to parse.
Expected< std::unique_ptr< Remark > > next() override
If no error occurs, this returns a valid Remark object.
BitstreamRemarkParser(StringRef Buf)
Create a parser that expects to find a string table embedded in the stream.
bool IsMetaReady
Whether the metadata has already been parsed, so we can continue parsing remarks.
BitstreamRemarkContainerType ContainerType
std::string ExternalFilePrependPath
Path to prepend when opening an external remark file.
RemarkParser(Format ParserFormat)