BitcodeWriter.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //===- llvm/Bitcode/BitcodeWriter.h - Bitcode writers -----------*- C++ -*-===//
  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 header defines interfaces to write LLVM bitcode files/streams.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_BITCODE_BITCODEWRITER_H
  13. #define LLVM_BITCODE_BITCODEWRITER_H
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/IR/ModuleSummaryIndex.h"
  16. #include "llvm/MC/StringTableBuilder.h"
  17. #include "llvm/Support/Allocator.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. #include <map>
  20. #include <memory>
  21. #include <string>
  22. #include <vector>
  23. namespace llvm {
  24. class BitstreamWriter;
  25. class Module;
  26. class raw_ostream;
  27. class BitcodeWriter {
  28. SmallVectorImpl<char> &Buffer;
  29. std::unique_ptr<BitstreamWriter> Stream;
  30. StringTableBuilder StrtabBuilder{StringTableBuilder::RAW};
  31. // Owns any strings created by the irsymtab writer until we create the
  32. // string table.
  33. BumpPtrAllocator Alloc;
  34. bool WroteStrtab = false, WroteSymtab = false;
  35. void writeBlob(unsigned Block, unsigned Record, StringRef Blob);
  36. std::vector<Module *> Mods;
  37. public:
  38. /// Create a BitcodeWriter that writes to Buffer.
  39. BitcodeWriter(SmallVectorImpl<char> &Buffer, raw_fd_stream *FS = nullptr);
  40. ~BitcodeWriter();
  41. /// Attempt to write a symbol table to the bitcode file. This must be called
  42. /// at most once after all modules have been written.
  43. ///
  44. /// A reader does not require a symbol table to interpret a bitcode file;
  45. /// the symbol table is needed only to improve link-time performance. So
  46. /// this function may decide not to write a symbol table. It may so decide
  47. /// if, for example, the target is unregistered or the IR is malformed.
  48. void writeSymtab();
  49. /// Write the bitcode file's string table. This must be called exactly once
  50. /// after all modules and the optional symbol table have been written.
  51. void writeStrtab();
  52. /// Copy the string table for another module into this bitcode file. This
  53. /// should be called after copying the module itself into the bitcode file.
  54. void copyStrtab(StringRef Strtab);
  55. /// Write the specified module to the buffer specified at construction time.
  56. ///
  57. /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
  58. /// Value in \c M. These will be reconstructed exactly when \a M is
  59. /// deserialized.
  60. ///
  61. /// If \c Index is supplied, the bitcode will contain the summary index
  62. /// (currently for use in ThinLTO optimization).
  63. ///
  64. /// \p GenerateHash enables hashing the Module and including the hash in the
  65. /// bitcode (currently for use in ThinLTO incremental build).
  66. ///
  67. /// If \p ModHash is non-null, when GenerateHash is true, the resulting
  68. /// hash is written into ModHash. When GenerateHash is false, that value
  69. /// is used as the hash instead of computing from the generated bitcode.
  70. /// Can be used to produce the same module hash for a minimized bitcode
  71. /// used just for the thin link as in the regular full bitcode that will
  72. /// be used in the backend.
  73. void writeModule(const Module &M, bool ShouldPreserveUseListOrder = false,
  74. const ModuleSummaryIndex *Index = nullptr,
  75. bool GenerateHash = false, ModuleHash *ModHash = nullptr);
  76. /// Write the specified thin link bitcode file (i.e., the minimized bitcode
  77. /// file) to the buffer specified at construction time. The thin link
  78. /// bitcode file is used for thin link, and it only contains the necessary
  79. /// information for thin link.
  80. ///
  81. /// ModHash is for use in ThinLTO incremental build, generated while the
  82. /// IR bitcode file writing.
  83. void writeThinLinkBitcode(const Module &M, const ModuleSummaryIndex &Index,
  84. const ModuleHash &ModHash);
  85. void writeIndex(
  86. const ModuleSummaryIndex *Index,
  87. const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex);
  88. };
  89. /// Write the specified module to the specified raw output stream.
  90. ///
  91. /// For streams where it matters, the given stream should be in "binary"
  92. /// mode.
  93. ///
  94. /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
  95. /// Value in \c M. These will be reconstructed exactly when \a M is
  96. /// deserialized.
  97. ///
  98. /// If \c Index is supplied, the bitcode will contain the summary index
  99. /// (currently for use in ThinLTO optimization).
  100. ///
  101. /// \p GenerateHash enables hashing the Module and including the hash in the
  102. /// bitcode (currently for use in ThinLTO incremental build).
  103. ///
  104. /// If \p ModHash is non-null, when GenerateHash is true, the resulting
  105. /// hash is written into ModHash. When GenerateHash is false, that value
  106. /// is used as the hash instead of computing from the generated bitcode.
  107. /// Can be used to produce the same module hash for a minimized bitcode
  108. /// used just for the thin link as in the regular full bitcode that will
  109. /// be used in the backend.
  110. void WriteBitcodeToFile(const Module &M, raw_ostream &Out,
  111. bool ShouldPreserveUseListOrder = false,
  112. const ModuleSummaryIndex *Index = nullptr,
  113. bool GenerateHash = false,
  114. ModuleHash *ModHash = nullptr);
  115. /// Write the specified thin link bitcode file (i.e., the minimized bitcode
  116. /// file) to the given raw output stream, where it will be written in a new
  117. /// bitcode block. The thin link bitcode file is used for thin link, and it
  118. /// only contains the necessary information for thin link.
  119. ///
  120. /// ModHash is for use in ThinLTO incremental build, generated while the IR
  121. /// bitcode file writing.
  122. void WriteThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
  123. const ModuleSummaryIndex &Index,
  124. const ModuleHash &ModHash);
  125. /// Write the specified module summary index to the given raw output stream,
  126. /// where it will be written in a new bitcode block. This is used when
  127. /// writing the combined index file for ThinLTO. When writing a subset of the
  128. /// index for a distributed backend, provide the \p ModuleToSummariesForIndex
  129. /// map.
  130. void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out,
  131. const std::map<std::string, GVSummaryMapTy>
  132. *ModuleToSummariesForIndex = nullptr);
  133. /// If EmbedBitcode is set, save a copy of the llvm IR as data in the
  134. /// __LLVM,__bitcode section (.llvmbc on non-MacOS).
  135. /// If available, pass the serialized module via the Buf parameter. If not,
  136. /// pass an empty (default-initialized) MemoryBufferRef, and the serialization
  137. /// will be handled by this API. The same behavior happens if the provided Buf
  138. /// is not bitcode (i.e. if it's invalid data or even textual LLVM assembly).
  139. /// If EmbedCmdline is set, the command line is also exported in
  140. /// the corresponding section (__LLVM,_cmdline / .llvmcmd) - even if CmdArgs
  141. /// were empty.
  142. void EmbedBitcodeInModule(Module &M, MemoryBufferRef Buf, bool EmbedBitcode,
  143. bool EmbedCmdline,
  144. const std::vector<uint8_t> &CmdArgs);
  145. } // end namespace llvm
  146. #endif // LLVM_BITCODE_BITCODEWRITER_H