1 //===- llvm/Bitcode/BitcodeWriter.h - Bitcode writers -----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This header defines interfaces to write LLVM bitcode files/streams.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_BITCODE_BITCODEWRITER_H
14 #define LLVM_BITCODE_BITCODEWRITER_H
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/ModuleSummaryIndex.h"
18 #include "llvm/MC/StringTableBuilder.h"
19 #include "llvm/Support/Allocator.h"
27 class BitstreamWriter
;
32 SmallVectorImpl
<char> &Buffer
;
33 std::unique_ptr
<BitstreamWriter
> Stream
;
35 StringTableBuilder StrtabBuilder
{StringTableBuilder::RAW
};
37 // Owns any strings created by the irsymtab writer until we create the
39 BumpPtrAllocator Alloc
;
41 bool WroteStrtab
= false, WroteSymtab
= false;
43 void writeBlob(unsigned Block
, unsigned Record
, StringRef Blob
);
45 std::vector
<Module
*> Mods
;
48 /// Create a BitcodeWriter that writes to Buffer.
49 BitcodeWriter(SmallVectorImpl
<char> &Buffer
);
53 /// Attempt to write a symbol table to the bitcode file. This must be called
54 /// at most once after all modules have been written.
56 /// A reader does not require a symbol table to interpret a bitcode file;
57 /// the symbol table is needed only to improve link-time performance. So
58 /// this function may decide not to write a symbol table. It may so decide
59 /// if, for example, the target is unregistered or the IR is malformed.
62 /// Write the bitcode file's string table. This must be called exactly once
63 /// after all modules and the optional symbol table have been written.
66 /// Copy the string table for another module into this bitcode file. This
67 /// should be called after copying the module itself into the bitcode file.
68 void copyStrtab(StringRef Strtab
);
70 /// Write the specified module to the buffer specified at construction time.
72 /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
73 /// Value in \c M. These will be reconstructed exactly when \a M is
76 /// If \c Index is supplied, the bitcode will contain the summary index
77 /// (currently for use in ThinLTO optimization).
79 /// \p GenerateHash enables hashing the Module and including the hash in the
80 /// bitcode (currently for use in ThinLTO incremental build).
82 /// If \p ModHash is non-null, when GenerateHash is true, the resulting
83 /// hash is written into ModHash. When GenerateHash is false, that value
84 /// is used as the hash instead of computing from the generated bitcode.
85 /// Can be used to produce the same module hash for a minimized bitcode
86 /// used just for the thin link as in the regular full bitcode that will
87 /// be used in the backend.
88 void writeModule(const Module
&M
, bool ShouldPreserveUseListOrder
= false,
89 const ModuleSummaryIndex
*Index
= nullptr,
90 bool GenerateHash
= false, ModuleHash
*ModHash
= nullptr);
92 /// Write the specified thin link bitcode file (i.e., the minimized bitcode
93 /// file) to the buffer specified at construction time. The thin link
94 /// bitcode file is used for thin link, and it only contains the necessary
95 /// information for thin link.
97 /// ModHash is for use in ThinLTO incremental build, generated while the
98 /// IR bitcode file writing.
99 void writeThinLinkBitcode(const Module
&M
, const ModuleSummaryIndex
&Index
,
100 const ModuleHash
&ModHash
);
103 const ModuleSummaryIndex
*Index
,
104 const std::map
<std::string
, GVSummaryMapTy
> *ModuleToSummariesForIndex
);
107 /// Write the specified module to the specified raw output stream.
109 /// For streams where it matters, the given stream should be in "binary"
112 /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
113 /// Value in \c M. These will be reconstructed exactly when \a M is
116 /// If \c Index is supplied, the bitcode will contain the summary index
117 /// (currently for use in ThinLTO optimization).
119 /// \p GenerateHash enables hashing the Module and including the hash in the
120 /// bitcode (currently for use in ThinLTO incremental build).
122 /// If \p ModHash is non-null, when GenerateHash is true, the resulting
123 /// hash is written into ModHash. When GenerateHash is false, that value
124 /// is used as the hash instead of computing from the generated bitcode.
125 /// Can be used to produce the same module hash for a minimized bitcode
126 /// used just for the thin link as in the regular full bitcode that will
127 /// be used in the backend.
128 void WriteBitcodeToFile(const Module
&M
, raw_ostream
&Out
,
129 bool ShouldPreserveUseListOrder
= false,
130 const ModuleSummaryIndex
*Index
= nullptr,
131 bool GenerateHash
= false,
132 ModuleHash
*ModHash
= nullptr);
134 /// Write the specified thin link bitcode file (i.e., the minimized bitcode
135 /// file) to the given raw output stream, where it will be written in a new
136 /// bitcode block. The thin link bitcode file is used for thin link, and it
137 /// only contains the necessary information for thin link.
139 /// ModHash is for use in ThinLTO incremental build, generated while the IR
140 /// bitcode file writing.
141 void WriteThinLinkBitcodeToFile(const Module
&M
, raw_ostream
&Out
,
142 const ModuleSummaryIndex
&Index
,
143 const ModuleHash
&ModHash
);
145 /// Write the specified module summary index to the given raw output stream,
146 /// where it will be written in a new bitcode block. This is used when
147 /// writing the combined index file for ThinLTO. When writing a subset of the
148 /// index for a distributed backend, provide the \p ModuleToSummariesForIndex
150 void WriteIndexToFile(const ModuleSummaryIndex
&Index
, raw_ostream
&Out
,
151 const std::map
<std::string
, GVSummaryMapTy
>
152 *ModuleToSummariesForIndex
= nullptr);
154 } // end namespace llvm
156 #endif // LLVM_BITCODE_BITCODEWRITER_H