BitcodeWriter.h 7.6 KB

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