Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:25

0001 //===-- BitstreamRemarkParser.h - Bitstream parser --------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file provides an implementation of the remark parser using the LLVM
0010 // Bitstream format.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_REMARKS_BITSTREAMREMARKPARSER_H
0015 #define LLVM_REMARKS_BITSTREAMREMARKPARSER_H
0016 
0017 #include "llvm/ADT/ArrayRef.h"
0018 #include "llvm/ADT/StringRef.h"
0019 #include "llvm/Bitstream/BitstreamReader.h"
0020 #include "llvm/Support/Error.h"
0021 #include <array>
0022 #include <cstdint>
0023 #include <optional>
0024 
0025 namespace llvm {
0026 namespace remarks {
0027 
0028 /// Helper to parse a META_BLOCK for a bitstream remark container.
0029 struct BitstreamMetaParserHelper {
0030   /// The Bitstream reader.
0031   BitstreamCursor &Stream;
0032   /// Reference to the storage for the block info.
0033   BitstreamBlockInfo &BlockInfo;
0034   /// The parsed content: depending on the container type, some fields might be
0035   /// empty.
0036   std::optional<uint64_t> ContainerVersion;
0037   std::optional<uint8_t> ContainerType;
0038   std::optional<StringRef> StrTabBuf;
0039   std::optional<StringRef> ExternalFilePath;
0040   std::optional<uint64_t> RemarkVersion;
0041 
0042   /// Continue parsing with \p Stream. \p Stream is expected to contain a
0043   /// ENTER_SUBBLOCK to the META_BLOCK at the current position.
0044   /// \p Stream is expected to have a BLOCKINFO_BLOCK set.
0045   BitstreamMetaParserHelper(BitstreamCursor &Stream,
0046                             BitstreamBlockInfo &BlockInfo);
0047 
0048   /// Parse the META_BLOCK and fill the available entries.
0049   /// This helper does not check for the validity of the fields.
0050   Error parse();
0051 };
0052 
0053 /// Helper to parse a REMARK_BLOCK for a bitstream remark container.
0054 struct BitstreamRemarkParserHelper {
0055   /// The Bitstream reader.
0056   BitstreamCursor &Stream;
0057   /// The parsed content: depending on the remark, some fields might be empty.
0058   std::optional<uint8_t> Type;
0059   std::optional<uint64_t> RemarkNameIdx;
0060   std::optional<uint64_t> PassNameIdx;
0061   std::optional<uint64_t> FunctionNameIdx;
0062   std::optional<uint64_t> SourceFileNameIdx;
0063   std::optional<uint32_t> SourceLine;
0064   std::optional<uint32_t> SourceColumn;
0065   std::optional<uint64_t> Hotness;
0066   struct Argument {
0067     std::optional<uint64_t> KeyIdx;
0068     std::optional<uint64_t> ValueIdx;
0069     std::optional<uint64_t> SourceFileNameIdx;
0070     std::optional<uint32_t> SourceLine;
0071     std::optional<uint32_t> SourceColumn;
0072   };
0073   std::optional<ArrayRef<Argument>> Args;
0074   /// Avoid re-allocating a vector every time.
0075   SmallVector<Argument, 8> TmpArgs;
0076 
0077   /// Continue parsing with \p Stream. \p Stream is expected to contain a
0078   /// ENTER_SUBBLOCK to the REMARK_BLOCK at the current position.
0079   /// \p Stream is expected to have a BLOCKINFO_BLOCK set and to have already
0080   /// parsed the META_BLOCK.
0081   BitstreamRemarkParserHelper(BitstreamCursor &Stream);
0082 
0083   /// Parse the REMARK_BLOCK and fill the available entries.
0084   /// This helper does not check for the validity of the fields.
0085   Error parse();
0086 };
0087 
0088 /// Helper to parse any bitstream remark container.
0089 struct BitstreamParserHelper {
0090   /// The Bitstream reader.
0091   BitstreamCursor Stream;
0092   /// The block info block.
0093   BitstreamBlockInfo BlockInfo;
0094   /// Start parsing at \p Buffer.
0095   BitstreamParserHelper(StringRef Buffer);
0096   /// Parse the magic number.
0097   Expected<std::array<char, 4>> parseMagic();
0098   /// Parse the block info block containing all the abbrevs.
0099   /// This needs to be called before calling any other parsing function.
0100   Error parseBlockInfoBlock();
0101   /// Return true if the next block is a META_BLOCK. This function does not move
0102   /// the cursor.
0103   Expected<bool> isMetaBlock();
0104   /// Return true if the next block is a REMARK_BLOCK. This function does not
0105   /// move the cursor.
0106   Expected<bool> isRemarkBlock();
0107   /// Return true if the parser reached the end of the stream.
0108   bool atEndOfStream() { return Stream.AtEndOfStream(); }
0109   /// Jump to the end of the stream, skipping everything.
0110   void skipToEnd() { return Stream.skipToEnd(); }
0111 };
0112 
0113 } // end namespace remarks
0114 } // end namespace llvm
0115 
0116 #endif // LLVM_REMARKS_BITSTREAMREMARKPARSER_H