BitstreamRemarkParser.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- BitstreamRemarkParser.h - Bitstream parser --------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file provides an implementation of the remark parser using the LLVM
  15. // Bitstream format.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_REMARKS_BITSTREAMREMARKPARSER_H
  19. #define LLVM_REMARKS_BITSTREAMREMARKPARSER_H
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/Bitstream/BitstreamReader.h"
  23. #include "llvm/Support/Error.h"
  24. #include <array>
  25. #include <cstdint>
  26. #include <optional>
  27. namespace llvm {
  28. namespace remarks {
  29. /// Helper to parse a META_BLOCK for a bitstream remark container.
  30. struct BitstreamMetaParserHelper {
  31. /// The Bitstream reader.
  32. BitstreamCursor &Stream;
  33. /// Reference to the storage for the block info.
  34. BitstreamBlockInfo &BlockInfo;
  35. /// The parsed content: depending on the container type, some fields might be
  36. /// empty.
  37. std::optional<uint64_t> ContainerVersion;
  38. std::optional<uint8_t> ContainerType;
  39. std::optional<StringRef> StrTabBuf;
  40. std::optional<StringRef> ExternalFilePath;
  41. std::optional<uint64_t> RemarkVersion;
  42. /// Continue parsing with \p Stream. \p Stream is expected to contain a
  43. /// ENTER_SUBBLOCK to the META_BLOCK at the current position.
  44. /// \p Stream is expected to have a BLOCKINFO_BLOCK set.
  45. BitstreamMetaParserHelper(BitstreamCursor &Stream,
  46. BitstreamBlockInfo &BlockInfo);
  47. /// Parse the META_BLOCK and fill the available entries.
  48. /// This helper does not check for the validity of the fields.
  49. Error parse();
  50. };
  51. /// Helper to parse a REMARK_BLOCK for a bitstream remark container.
  52. struct BitstreamRemarkParserHelper {
  53. /// The Bitstream reader.
  54. BitstreamCursor &Stream;
  55. /// The parsed content: depending on the remark, some fields might be empty.
  56. std::optional<uint8_t> Type;
  57. std::optional<uint64_t> RemarkNameIdx;
  58. std::optional<uint64_t> PassNameIdx;
  59. std::optional<uint64_t> FunctionNameIdx;
  60. std::optional<uint64_t> SourceFileNameIdx;
  61. std::optional<uint32_t> SourceLine;
  62. std::optional<uint32_t> SourceColumn;
  63. std::optional<uint64_t> Hotness;
  64. struct Argument {
  65. std::optional<uint64_t> KeyIdx;
  66. std::optional<uint64_t> ValueIdx;
  67. std::optional<uint64_t> SourceFileNameIdx;
  68. std::optional<uint32_t> SourceLine;
  69. std::optional<uint32_t> SourceColumn;
  70. };
  71. std::optional<ArrayRef<Argument>> Args;
  72. /// Avoid re-allocating a vector every time.
  73. SmallVector<Argument, 8> TmpArgs;
  74. /// Continue parsing with \p Stream. \p Stream is expected to contain a
  75. /// ENTER_SUBBLOCK to the REMARK_BLOCK at the current position.
  76. /// \p Stream is expected to have a BLOCKINFO_BLOCK set and to have already
  77. /// parsed the META_BLOCK.
  78. BitstreamRemarkParserHelper(BitstreamCursor &Stream);
  79. /// Parse the REMARK_BLOCK and fill the available entries.
  80. /// This helper does not check for the validity of the fields.
  81. Error parse();
  82. };
  83. /// Helper to parse any bitstream remark container.
  84. struct BitstreamParserHelper {
  85. /// The Bitstream reader.
  86. BitstreamCursor Stream;
  87. /// The block info block.
  88. BitstreamBlockInfo BlockInfo;
  89. /// Start parsing at \p Buffer.
  90. BitstreamParserHelper(StringRef Buffer);
  91. /// Parse the magic number.
  92. Expected<std::array<char, 4>> parseMagic();
  93. /// Parse the block info block containing all the abbrevs.
  94. /// This needs to be called before calling any other parsing function.
  95. Error parseBlockInfoBlock();
  96. /// Return true if the next block is a META_BLOCK. This function does not move
  97. /// the cursor.
  98. Expected<bool> isMetaBlock();
  99. /// Return true if the next block is a REMARK_BLOCK. This function does not
  100. /// move the cursor.
  101. Expected<bool> isRemarkBlock();
  102. /// Return true if the parser reached the end of the stream.
  103. bool atEndOfStream() { return Stream.AtEndOfStream(); }
  104. /// Jump to the end of the stream, skipping everything.
  105. void skipToEnd() { return Stream.skipToEnd(); }
  106. };
  107. } // end namespace remarks
  108. } // end namespace llvm
  109. #endif // LLVM_REMARKS_BITSTREAMREMARKPARSER_H
  110. #ifdef __GNUC__
  111. #pragma GCC diagnostic pop
  112. #endif