LLVM 22.0.0git
BitcodeWriter.h
Go to the documentation of this file.
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
13#ifndef LLVM_BITCODE_BITCODEWRITER_H
14#define LLVM_BITCODE_BITCODEWRITER_H
15
16#include "llvm/ADT/StringRef.h"
22#include <memory>
23#include <vector>
24
25namespace llvm {
26
27class BitstreamWriter;
28class Module;
29class raw_ostream;
30
32 std::unique_ptr<BitstreamWriter> Stream;
33
35
36 // Owns any strings created by the irsymtab writer until we create the
37 // string table.
38 BumpPtrAllocator Alloc;
39
40 bool WroteStrtab = false, WroteSymtab = false;
41
42 void writeBlob(unsigned Block, unsigned Record, StringRef Blob);
43
44 std::vector<Module *> Mods;
45
46public:
47 /// Create a BitcodeWriter that writes to Buffer.
50
52
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.
55 ///
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.
60 LLVM_ABI void writeSymtab();
61
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.
64 LLVM_ABI void writeStrtab();
65
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 LLVM_ABI void copyStrtab(StringRef Strtab);
69
70 /// Write the specified module to the buffer specified at construction time.
71 ///
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
74 /// deserialized.
75 ///
76 /// If \c Index is supplied, the bitcode will contain the summary index
77 /// (currently for use in ThinLTO optimization).
78 ///
79 /// \p GenerateHash enables hashing the Module and including the hash in the
80 /// bitcode (currently for use in ThinLTO incremental build).
81 ///
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 LLVM_ABI void writeModule(const Module &M,
89 bool ShouldPreserveUseListOrder = false,
90 const ModuleSummaryIndex *Index = nullptr,
91 bool GenerateHash = false,
92 ModuleHash *ModHash = nullptr);
93
94 /// Write the specified thin link bitcode file (i.e., the minimized bitcode
95 /// file) to the buffer specified at construction time. The thin link
96 /// bitcode file is used for thin link, and it only contains the necessary
97 /// information for thin link.
98 ///
99 /// ModHash is for use in ThinLTO incremental build, generated while the
100 /// IR bitcode file writing.
102 const ModuleSummaryIndex &Index,
103 const ModuleHash &ModHash);
104
105 LLVM_ABI void
106 writeIndex(const ModuleSummaryIndex *Index,
107 const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex,
108 const GVSummaryPtrSet *DecSummaries);
109};
110
111/// Write the specified module to the specified raw output stream.
112///
113/// For streams where it matters, the given stream should be in "binary"
114/// mode.
115///
116/// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
117/// Value in \c M. These will be reconstructed exactly when \a M is
118/// deserialized.
119///
120/// If \c Index is supplied, the bitcode will contain the summary index
121/// (currently for use in ThinLTO optimization).
122///
123/// \p GenerateHash enables hashing the Module and including the hash in the
124/// bitcode (currently for use in ThinLTO incremental build).
125///
126/// If \p ModHash is non-null, when GenerateHash is true, the resulting
127/// hash is written into ModHash. When GenerateHash is false, that value
128/// is used as the hash instead of computing from the generated bitcode.
129/// Can be used to produce the same module hash for a minimized bitcode
130/// used just for the thin link as in the regular full bitcode that will
131/// be used in the backend.
133 bool ShouldPreserveUseListOrder = false,
134 const ModuleSummaryIndex *Index = nullptr,
135 bool GenerateHash = false,
136 ModuleHash *ModHash = nullptr);
137
138/// Write the specified thin link bitcode file (i.e., the minimized bitcode
139/// file) to the given raw output stream, where it will be written in a new
140/// bitcode block. The thin link bitcode file is used for thin link, and it
141/// only contains the necessary information for thin link.
142///
143/// ModHash is for use in ThinLTO incremental build, generated while the IR
144/// bitcode file writing.
146 const ModuleSummaryIndex &Index,
147 const ModuleHash &ModHash);
148
149/// Write the specified module summary index to the given raw output stream,
150/// where it will be written in a new bitcode block. This is used when
151/// writing the combined index file for ThinLTO. When writing a subset of the
152/// index for a distributed backend, provide the \p ModuleToSummariesForIndex
153/// map. \p DecSummaries specifies the set of summaries for which the
154/// corresponding value should be imported as a declaration (prototype).
156 const ModuleSummaryIndex &Index, raw_ostream &Out,
157 const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex = nullptr,
158 const GVSummaryPtrSet *DecSummaries = nullptr);
159
160/// If EmbedBitcode is set, save a copy of the llvm IR as data in the
161/// __LLVM,__bitcode section (.llvmbc on non-MacOS).
162/// If available, pass the serialized module via the Buf parameter. If not,
163/// pass an empty (default-initialized) MemoryBufferRef, and the serialization
164/// will be handled by this API. The same behavior happens if the provided Buf
165/// is not bitcode (i.e. if it's invalid data or even textual LLVM assembly).
166/// If EmbedCmdline is set, the command line is also exported in
167/// the corresponding section (__LLVM,_cmdline / .llvmcmd) - even if CmdArgs
168/// were empty.
170 bool EmbedBitcode, bool EmbedCmdline,
171 const std::vector<uint8_t> &CmdArgs);
172
173} // end namespace llvm
174
175#endif // LLVM_BITCODE_BITCODEWRITER_H
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition Compiler.h:213
static cl::opt< LTOBitcodeEmbedding > EmbedBitcode("lto-embed-bitcode", cl::init(LTOBitcodeEmbedding::DoNotEmbed), cl::values(clEnumValN(LTOBitcodeEmbedding::DoNotEmbed, "none", "Do not embed"), clEnumValN(LTOBitcodeEmbedding::EmbedOptimized, "optimized", "Embed after all optimization passes"), clEnumValN(LTOBitcodeEmbedding::EmbedPostMergePreOptimized, "post-merge-pre-opt", "Embed post merge, but before optimizations")), cl::desc("Embed LLVM bitcode in object files produced by LTO"))
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
LLVM_ABI void writeThinLinkBitcode(const Module &M, const ModuleSummaryIndex &Index, const ModuleHash &ModHash)
Write the specified thin link bitcode file (i.e., the minimized bitcode file) to the buffer specified...
LLVM_ABI void writeIndex(const ModuleSummaryIndex *Index, const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex, const GVSummaryPtrSet *DecSummaries)
LLVM_ABI void copyStrtab(StringRef Strtab)
Copy the string table for another module into this bitcode file.
LLVM_ABI void writeStrtab()
Write the bitcode file's string table.
LLVM_ABI void writeSymtab()
Attempt to write a symbol table to the bitcode file.
LLVM_ABI void writeModule(const Module &M, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the buffer specified at construction time.
LLVM_ABI BitcodeWriter(SmallVectorImpl< char > &Buffer)
Create a BitcodeWriter that writes to Buffer.
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Utility for building string tables with deduplicated suffixes.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
std::unordered_set< GlobalValueSummary * > GVSummaryPtrSet
A set of global value summary pointers.
LLVM_ABI void WriteBitcodeToFile(const Module &M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
LLVM_ABI void writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out, const ModuleSummaryIndex &Index, const ModuleHash &ModHash)
Write the specified thin link bitcode file (i.e., the minimized bitcode file) to the given raw output...
LLVM_ABI void writeIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex=nullptr, const GVSummaryPtrSet *DecSummaries=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
LLVM_ABI void embedBitcodeInModule(Module &M, MemoryBufferRef Buf, bool EmbedBitcode, bool EmbedCmdline, const std::vector< uint8_t > &CmdArgs)
If EmbedBitcode is set, save a copy of the llvm IR as data in the __LLVM,__bitcode section (....
std::map< std::string, GVSummaryMapTy, std::less<> > ModuleToSummariesForIndexTy
Map of a module name to the GUIDs and summaries we will import from that module.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383