LLVM  10.0.0svn
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"
19 #include "llvm/Support/Allocator.h"
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 namespace llvm {
26 
27 class BitstreamWriter;
28 class Module;
29 class raw_ostream;
30 
31  class BitcodeWriter {
32  SmallVectorImpl<char> &Buffer;
33  std::unique_ptr<BitstreamWriter> Stream;
34 
36 
37  // Owns any strings created by the irsymtab writer until we create the
38  // string table.
39  BumpPtrAllocator Alloc;
40 
41  bool WroteStrtab = false, WroteSymtab = false;
42 
43  void writeBlob(unsigned Block, unsigned Record, StringRef Blob);
44 
45  std::vector<Module *> Mods;
46 
47  public:
48  /// 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  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  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  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  void writeModule(const Module &M, bool ShouldPreserveUseListOrder = false,
89  const ModuleSummaryIndex *Index = nullptr,
90  bool GenerateHash = false, ModuleHash *ModHash = nullptr);
91 
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.
96  ///
97  /// ModHash is for use in ThinLTO incremental build, generated while the
98  /// IR bitcode file writing.
100  const ModuleHash &ModHash);
101 
102  void writeIndex(
103  const ModuleSummaryIndex *Index,
104  const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex);
105  };
106 
107  /// Write the specified module to the specified raw output stream.
108  ///
109  /// For streams where it matters, the given stream should be in "binary"
110  /// mode.
111  ///
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
114  /// deserialized.
115  ///
116  /// If \c Index is supplied, the bitcode will contain the summary index
117  /// (currently for use in ThinLTO optimization).
118  ///
119  /// \p GenerateHash enables hashing the Module and including the hash in the
120  /// bitcode (currently for use in ThinLTO incremental build).
121  ///
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);
133 
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.
138  ///
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);
144 
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
149  /// map.
150  void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out,
151  const std::map<std::string, GVSummaryMapTy>
152  *ModuleToSummariesForIndex = nullptr);
153 
154 } // end namespace llvm
155 
156 #endif // LLVM_BITCODE_BITCODEWRITER_H
void writeIndex(const ModuleSummaryIndex *Index, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
void writeSymtab()
Attempt to write a symbol table to the bitcode file.
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...
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
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.
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
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...
Utility for building string tables with deduplicated suffixes.
Class to hold module path string table and global value map, and encapsulate methods for operating on...
BitcodeWriter(SmallVectorImpl< char > &Buffer)
Create a BitcodeWriter that writes to Buffer.
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
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.
void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
void copyStrtab(StringRef Strtab)
Copy the string table for another module into this bitcode file.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
void writeStrtab()
Write the bitcode file&#39;s string table.