BitstreamRemarkSerializer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //===- BitstreamRemarkSerializer.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 the implementation of the LLVM bitstream remark serializer
  10. // using LLVM's bitstream writer.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Remarks/BitstreamRemarkSerializer.h"
  14. using namespace llvm;
  15. using namespace llvm::remarks;
  16. BitstreamRemarkSerializerHelper::BitstreamRemarkSerializerHelper(
  17. BitstreamRemarkContainerType ContainerType)
  18. : Encoded(), R(), Bitstream(Encoded), ContainerType(ContainerType) {}
  19. static void push(SmallVectorImpl<uint64_t> &R, StringRef Str) {
  20. for (const char C : Str)
  21. R.push_back(C);
  22. }
  23. static void setRecordName(unsigned RecordID, BitstreamWriter &Bitstream,
  24. SmallVectorImpl<uint64_t> &R, StringRef Str) {
  25. R.clear();
  26. R.push_back(RecordID);
  27. push(R, Str);
  28. Bitstream.EmitRecord(bitc::BLOCKINFO_CODE_SETRECORDNAME, R);
  29. }
  30. static void initBlock(unsigned BlockID, BitstreamWriter &Bitstream,
  31. SmallVectorImpl<uint64_t> &R, StringRef Str) {
  32. R.clear();
  33. R.push_back(BlockID);
  34. Bitstream.EmitRecord(bitc::BLOCKINFO_CODE_SETBID, R);
  35. R.clear();
  36. push(R, Str);
  37. Bitstream.EmitRecord(bitc::BLOCKINFO_CODE_BLOCKNAME, R);
  38. }
  39. void BitstreamRemarkSerializerHelper::setupMetaBlockInfo() {
  40. // Setup the metadata block.
  41. initBlock(META_BLOCK_ID, Bitstream, R, MetaBlockName);
  42. // The container information.
  43. setRecordName(RECORD_META_CONTAINER_INFO, Bitstream, R,
  44. MetaContainerInfoName);
  45. auto Abbrev = std::make_shared<BitCodeAbbrev>();
  46. Abbrev->Add(BitCodeAbbrevOp(RECORD_META_CONTAINER_INFO));
  47. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Version.
  48. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Type.
  49. RecordMetaContainerInfoAbbrevID =
  50. Bitstream.EmitBlockInfoAbbrev(META_BLOCK_ID, Abbrev);
  51. }
  52. void BitstreamRemarkSerializerHelper::setupMetaRemarkVersion() {
  53. setRecordName(RECORD_META_REMARK_VERSION, Bitstream, R,
  54. MetaRemarkVersionName);
  55. auto Abbrev = std::make_shared<BitCodeAbbrev>();
  56. Abbrev->Add(BitCodeAbbrevOp(RECORD_META_REMARK_VERSION));
  57. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Version.
  58. RecordMetaRemarkVersionAbbrevID =
  59. Bitstream.EmitBlockInfoAbbrev(META_BLOCK_ID, Abbrev);
  60. }
  61. void BitstreamRemarkSerializerHelper::emitMetaRemarkVersion(
  62. uint64_t RemarkVersion) {
  63. // The remark version is emitted only if we emit remarks.
  64. R.clear();
  65. R.push_back(RECORD_META_REMARK_VERSION);
  66. R.push_back(RemarkVersion);
  67. Bitstream.EmitRecordWithAbbrev(RecordMetaRemarkVersionAbbrevID, R);
  68. }
  69. void BitstreamRemarkSerializerHelper::setupMetaStrTab() {
  70. setRecordName(RECORD_META_STRTAB, Bitstream, R, MetaStrTabName);
  71. auto Abbrev = std::make_shared<BitCodeAbbrev>();
  72. Abbrev->Add(BitCodeAbbrevOp(RECORD_META_STRTAB));
  73. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Raw table.
  74. RecordMetaStrTabAbbrevID =
  75. Bitstream.EmitBlockInfoAbbrev(META_BLOCK_ID, Abbrev);
  76. }
  77. void BitstreamRemarkSerializerHelper::emitMetaStrTab(
  78. const StringTable &StrTab) {
  79. // The string table is not emitted if we emit remarks separately.
  80. R.clear();
  81. R.push_back(RECORD_META_STRTAB);
  82. // Serialize to a blob.
  83. std::string Buf;
  84. raw_string_ostream OS(Buf);
  85. StrTab.serialize(OS);
  86. StringRef Blob = OS.str();
  87. Bitstream.EmitRecordWithBlob(RecordMetaStrTabAbbrevID, R, Blob);
  88. }
  89. void BitstreamRemarkSerializerHelper::setupMetaExternalFile() {
  90. setRecordName(RECORD_META_EXTERNAL_FILE, Bitstream, R, MetaExternalFileName);
  91. auto Abbrev = std::make_shared<BitCodeAbbrev>();
  92. Abbrev->Add(BitCodeAbbrevOp(RECORD_META_EXTERNAL_FILE));
  93. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Filename.
  94. RecordMetaExternalFileAbbrevID =
  95. Bitstream.EmitBlockInfoAbbrev(META_BLOCK_ID, Abbrev);
  96. }
  97. void BitstreamRemarkSerializerHelper::emitMetaExternalFile(StringRef Filename) {
  98. // The external file is emitted only if we emit the separate metadata.
  99. R.clear();
  100. R.push_back(RECORD_META_EXTERNAL_FILE);
  101. Bitstream.EmitRecordWithBlob(RecordMetaExternalFileAbbrevID, R, Filename);
  102. }
  103. void BitstreamRemarkSerializerHelper::setupRemarkBlockInfo() {
  104. // Setup the remark block.
  105. initBlock(REMARK_BLOCK_ID, Bitstream, R, RemarkBlockName);
  106. // The header of a remark.
  107. {
  108. setRecordName(RECORD_REMARK_HEADER, Bitstream, R, RemarkHeaderName);
  109. auto Abbrev = std::make_shared<BitCodeAbbrev>();
  110. Abbrev->Add(BitCodeAbbrevOp(RECORD_REMARK_HEADER));
  111. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Type
  112. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Remark Name
  113. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Pass name
  114. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Function name
  115. RecordRemarkHeaderAbbrevID =
  116. Bitstream.EmitBlockInfoAbbrev(REMARK_BLOCK_ID, Abbrev);
  117. }
  118. // The location of a remark.
  119. {
  120. setRecordName(RECORD_REMARK_DEBUG_LOC, Bitstream, R, RemarkDebugLocName);
  121. auto Abbrev = std::make_shared<BitCodeAbbrev>();
  122. Abbrev->Add(BitCodeAbbrevOp(RECORD_REMARK_DEBUG_LOC));
  123. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // File
  124. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line
  125. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column
  126. RecordRemarkDebugLocAbbrevID =
  127. Bitstream.EmitBlockInfoAbbrev(REMARK_BLOCK_ID, Abbrev);
  128. }
  129. // The hotness of a remark.
  130. {
  131. setRecordName(RECORD_REMARK_HOTNESS, Bitstream, R, RemarkHotnessName);
  132. auto Abbrev = std::make_shared<BitCodeAbbrev>();
  133. Abbrev->Add(BitCodeAbbrevOp(RECORD_REMARK_HOTNESS));
  134. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Hotness
  135. RecordRemarkHotnessAbbrevID =
  136. Bitstream.EmitBlockInfoAbbrev(REMARK_BLOCK_ID, Abbrev);
  137. }
  138. // An argument entry with a debug location attached.
  139. {
  140. setRecordName(RECORD_REMARK_ARG_WITH_DEBUGLOC, Bitstream, R,
  141. RemarkArgWithDebugLocName);
  142. auto Abbrev = std::make_shared<BitCodeAbbrev>();
  143. Abbrev->Add(BitCodeAbbrevOp(RECORD_REMARK_ARG_WITH_DEBUGLOC));
  144. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Key
  145. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Value
  146. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // File
  147. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line
  148. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column
  149. RecordRemarkArgWithDebugLocAbbrevID =
  150. Bitstream.EmitBlockInfoAbbrev(REMARK_BLOCK_ID, Abbrev);
  151. }
  152. // An argument entry with no debug location attached.
  153. {
  154. setRecordName(RECORD_REMARK_ARG_WITHOUT_DEBUGLOC, Bitstream, R,
  155. RemarkArgWithoutDebugLocName);
  156. auto Abbrev = std::make_shared<BitCodeAbbrev>();
  157. Abbrev->Add(BitCodeAbbrevOp(RECORD_REMARK_ARG_WITHOUT_DEBUGLOC));
  158. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Key
  159. Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Value
  160. RecordRemarkArgWithoutDebugLocAbbrevID =
  161. Bitstream.EmitBlockInfoAbbrev(REMARK_BLOCK_ID, Abbrev);
  162. }
  163. }
  164. void BitstreamRemarkSerializerHelper::setupBlockInfo() {
  165. // Emit magic number.
  166. for (const char C : ContainerMagic)
  167. Bitstream.Emit(static_cast<unsigned>(C), 8);
  168. Bitstream.EnterBlockInfoBlock();
  169. // Setup the main metadata. Depending on the container type, we'll setup the
  170. // required records next.
  171. setupMetaBlockInfo();
  172. switch (ContainerType) {
  173. case BitstreamRemarkContainerType::SeparateRemarksMeta:
  174. // Needs a string table that the separate remark file is using.
  175. setupMetaStrTab();
  176. // Needs to know where the external remarks file is.
  177. setupMetaExternalFile();
  178. break;
  179. case BitstreamRemarkContainerType::SeparateRemarksFile:
  180. // Contains remarks: emit the version.
  181. setupMetaRemarkVersion();
  182. // Contains remarks: emit the remark abbrevs.
  183. setupRemarkBlockInfo();
  184. break;
  185. case BitstreamRemarkContainerType::Standalone:
  186. // Contains remarks: emit the version.
  187. setupMetaRemarkVersion();
  188. // Needs a string table.
  189. setupMetaStrTab();
  190. // Contains remarks: emit the remark abbrevs.
  191. setupRemarkBlockInfo();
  192. break;
  193. }
  194. Bitstream.ExitBlock();
  195. }
  196. void BitstreamRemarkSerializerHelper::emitMetaBlock(
  197. uint64_t ContainerVersion, Optional<uint64_t> RemarkVersion,
  198. Optional<const StringTable *> StrTab, Optional<StringRef> Filename) {
  199. // Emit the meta block
  200. Bitstream.EnterSubblock(META_BLOCK_ID, 3);
  201. // The container version and type.
  202. R.clear();
  203. R.push_back(RECORD_META_CONTAINER_INFO);
  204. R.push_back(ContainerVersion);
  205. R.push_back(static_cast<uint64_t>(ContainerType));
  206. Bitstream.EmitRecordWithAbbrev(RecordMetaContainerInfoAbbrevID, R);
  207. switch (ContainerType) {
  208. case BitstreamRemarkContainerType::SeparateRemarksMeta:
  209. assert(StrTab != None && *StrTab != nullptr);
  210. emitMetaStrTab(**StrTab);
  211. assert(Filename != None);
  212. emitMetaExternalFile(*Filename);
  213. break;
  214. case BitstreamRemarkContainerType::SeparateRemarksFile:
  215. assert(RemarkVersion != None);
  216. emitMetaRemarkVersion(*RemarkVersion);
  217. break;
  218. case BitstreamRemarkContainerType::Standalone:
  219. assert(RemarkVersion != None);
  220. emitMetaRemarkVersion(*RemarkVersion);
  221. assert(StrTab != None && *StrTab != nullptr);
  222. emitMetaStrTab(**StrTab);
  223. break;
  224. }
  225. Bitstream.ExitBlock();
  226. }
  227. void BitstreamRemarkSerializerHelper::emitRemarkBlock(const Remark &Remark,
  228. StringTable &StrTab) {
  229. Bitstream.EnterSubblock(REMARK_BLOCK_ID, 4);
  230. R.clear();
  231. R.push_back(RECORD_REMARK_HEADER);
  232. R.push_back(static_cast<uint64_t>(Remark.RemarkType));
  233. R.push_back(StrTab.add(Remark.RemarkName).first);
  234. R.push_back(StrTab.add(Remark.PassName).first);
  235. R.push_back(StrTab.add(Remark.FunctionName).first);
  236. Bitstream.EmitRecordWithAbbrev(RecordRemarkHeaderAbbrevID, R);
  237. if (const Optional<RemarkLocation> &Loc = Remark.Loc) {
  238. R.clear();
  239. R.push_back(RECORD_REMARK_DEBUG_LOC);
  240. R.push_back(StrTab.add(Loc->SourceFilePath).first);
  241. R.push_back(Loc->SourceLine);
  242. R.push_back(Loc->SourceColumn);
  243. Bitstream.EmitRecordWithAbbrev(RecordRemarkDebugLocAbbrevID, R);
  244. }
  245. if (Optional<uint64_t> Hotness = Remark.Hotness) {
  246. R.clear();
  247. R.push_back(RECORD_REMARK_HOTNESS);
  248. R.push_back(*Hotness);
  249. Bitstream.EmitRecordWithAbbrev(RecordRemarkHotnessAbbrevID, R);
  250. }
  251. for (const Argument &Arg : Remark.Args) {
  252. R.clear();
  253. unsigned Key = StrTab.add(Arg.Key).first;
  254. unsigned Val = StrTab.add(Arg.Val).first;
  255. bool HasDebugLoc = Arg.Loc != None;
  256. R.push_back(HasDebugLoc ? RECORD_REMARK_ARG_WITH_DEBUGLOC
  257. : RECORD_REMARK_ARG_WITHOUT_DEBUGLOC);
  258. R.push_back(Key);
  259. R.push_back(Val);
  260. if (HasDebugLoc) {
  261. R.push_back(StrTab.add(Arg.Loc->SourceFilePath).first);
  262. R.push_back(Arg.Loc->SourceLine);
  263. R.push_back(Arg.Loc->SourceColumn);
  264. }
  265. Bitstream.EmitRecordWithAbbrev(HasDebugLoc
  266. ? RecordRemarkArgWithDebugLocAbbrevID
  267. : RecordRemarkArgWithoutDebugLocAbbrevID,
  268. R);
  269. }
  270. Bitstream.ExitBlock();
  271. }
  272. void BitstreamRemarkSerializerHelper::flushToStream(raw_ostream &OS) {
  273. OS.write(Encoded.data(), Encoded.size());
  274. Encoded.clear();
  275. }
  276. StringRef BitstreamRemarkSerializerHelper::getBuffer() {
  277. return StringRef(Encoded.data(), Encoded.size());
  278. }
  279. BitstreamRemarkSerializer::BitstreamRemarkSerializer(raw_ostream &OS,
  280. SerializerMode Mode)
  281. : RemarkSerializer(Format::Bitstream, OS, Mode),
  282. Helper(BitstreamRemarkContainerType::SeparateRemarksFile) {
  283. assert(Mode == SerializerMode::Separate &&
  284. "For SerializerMode::Standalone, a pre-filled string table needs to "
  285. "be provided.");
  286. // We always use a string table with bitstream.
  287. StrTab.emplace();
  288. }
  289. BitstreamRemarkSerializer::BitstreamRemarkSerializer(raw_ostream &OS,
  290. SerializerMode Mode,
  291. StringTable StrTabIn)
  292. : RemarkSerializer(Format::Bitstream, OS, Mode),
  293. Helper(Mode == SerializerMode::Separate
  294. ? BitstreamRemarkContainerType::SeparateRemarksFile
  295. : BitstreamRemarkContainerType::Standalone) {
  296. StrTab = std::move(StrTabIn);
  297. }
  298. void BitstreamRemarkSerializer::emit(const Remark &Remark) {
  299. if (!DidSetUp) {
  300. // Emit the metadata that is embedded in the remark file.
  301. // If we're in standalone mode, serialize the string table as well.
  302. bool IsStandalone =
  303. Helper.ContainerType == BitstreamRemarkContainerType::Standalone;
  304. BitstreamMetaSerializer MetaSerializer(
  305. OS, Helper,
  306. IsStandalone ? &*StrTab : Optional<const StringTable *>(None));
  307. MetaSerializer.emit();
  308. DidSetUp = true;
  309. }
  310. assert(DidSetUp &&
  311. "The Block info block and the meta block were not emitted yet.");
  312. Helper.emitRemarkBlock(Remark, *StrTab);
  313. Helper.flushToStream(OS);
  314. }
  315. std::unique_ptr<MetaSerializer> BitstreamRemarkSerializer::metaSerializer(
  316. raw_ostream &OS, Optional<StringRef> ExternalFilename) {
  317. assert(Helper.ContainerType !=
  318. BitstreamRemarkContainerType::SeparateRemarksMeta);
  319. bool IsStandalone =
  320. Helper.ContainerType == BitstreamRemarkContainerType::Standalone;
  321. return std::make_unique<BitstreamMetaSerializer>(
  322. OS,
  323. IsStandalone ? BitstreamRemarkContainerType::Standalone
  324. : BitstreamRemarkContainerType::SeparateRemarksMeta,
  325. &*StrTab, ExternalFilename);
  326. }
  327. void BitstreamMetaSerializer::emit() {
  328. Helper->setupBlockInfo();
  329. Helper->emitMetaBlock(CurrentContainerVersion, CurrentRemarkVersion, StrTab,
  330. ExternalFilename);
  331. Helper->flushToStream(OS);
  332. }