Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:22

0001 //===- llvm/Bitcode/BitcodeWriter.h - Bitcode writers -----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This header defines interfaces to write LLVM bitcode files/streams.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_BITCODE_BITCODEWRITER_H
0014 #define LLVM_BITCODE_BITCODEWRITER_H
0015 
0016 #include "llvm/ADT/StringRef.h"
0017 #include "llvm/IR/ModuleSummaryIndex.h"
0018 #include "llvm/MC/StringTableBuilder.h"
0019 #include "llvm/Support/Allocator.h"
0020 #include "llvm/Support/MemoryBufferRef.h"
0021 #include <map>
0022 #include <memory>
0023 #include <string>
0024 #include <vector>
0025 
0026 namespace llvm {
0027 
0028 class BitstreamWriter;
0029 class Module;
0030 class raw_ostream;
0031 
0032 class BitcodeWriter {
0033   std::unique_ptr<BitstreamWriter> Stream;
0034 
0035   StringTableBuilder StrtabBuilder{StringTableBuilder::RAW};
0036 
0037   // Owns any strings created by the irsymtab writer until we create the
0038   // string table.
0039   BumpPtrAllocator Alloc;
0040 
0041   bool WroteStrtab = false, WroteSymtab = false;
0042 
0043   void writeBlob(unsigned Block, unsigned Record, StringRef Blob);
0044 
0045   std::vector<Module *> Mods;
0046 
0047 public:
0048   /// Create a BitcodeWriter that writes to Buffer.
0049   BitcodeWriter(SmallVectorImpl<char> &Buffer);
0050   BitcodeWriter(raw_ostream &FS);
0051 
0052   ~BitcodeWriter();
0053 
0054   /// Attempt to write a symbol table to the bitcode file. This must be called
0055   /// at most once after all modules have been written.
0056   ///
0057   /// A reader does not require a symbol table to interpret a bitcode file;
0058   /// the symbol table is needed only to improve link-time performance. So
0059   /// this function may decide not to write a symbol table. It may so decide
0060   /// if, for example, the target is unregistered or the IR is malformed.
0061   void writeSymtab();
0062 
0063   /// Write the bitcode file's string table. This must be called exactly once
0064   /// after all modules and the optional symbol table have been written.
0065   void writeStrtab();
0066 
0067   /// Copy the string table for another module into this bitcode file. This
0068   /// should be called after copying the module itself into the bitcode file.
0069   void copyStrtab(StringRef Strtab);
0070 
0071   /// Write the specified module to the buffer specified at construction time.
0072   ///
0073   /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
0074   /// Value in \c M.  These will be reconstructed exactly when \a M is
0075   /// deserialized.
0076   ///
0077   /// If \c Index is supplied, the bitcode will contain the summary index
0078   /// (currently for use in ThinLTO optimization).
0079   ///
0080   /// \p GenerateHash enables hashing the Module and including the hash in the
0081   /// bitcode (currently for use in ThinLTO incremental build).
0082   ///
0083   /// If \p ModHash is non-null, when GenerateHash is true, the resulting
0084   /// hash is written into ModHash. When GenerateHash is false, that value
0085   /// is used as the hash instead of computing from the generated bitcode.
0086   /// Can be used to produce the same module hash for a minimized bitcode
0087   /// used just for the thin link as in the regular full bitcode that will
0088   /// be used in the backend.
0089   void writeModule(const Module &M, bool ShouldPreserveUseListOrder = false,
0090                    const ModuleSummaryIndex *Index = nullptr,
0091                    bool GenerateHash = false, ModuleHash *ModHash = nullptr);
0092 
0093   /// Write the specified thin link bitcode file (i.e., the minimized bitcode
0094   /// file) to the buffer specified at construction time. The thin link
0095   /// bitcode file is used for thin link, and it only contains the necessary
0096   /// information for thin link.
0097   ///
0098   /// ModHash is for use in ThinLTO incremental build, generated while the
0099   /// IR bitcode file writing.
0100   void writeThinLinkBitcode(const Module &M, const ModuleSummaryIndex &Index,
0101                             const ModuleHash &ModHash);
0102 
0103   void writeIndex(const ModuleSummaryIndex *Index,
0104                   const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex,
0105                   const GVSummaryPtrSet *DecSummaries);
0106 };
0107 
0108 /// Write the specified module to the specified raw output stream.
0109 ///
0110 /// For streams where it matters, the given stream should be in "binary"
0111 /// mode.
0112 ///
0113 /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
0114 /// Value in \c M.  These will be reconstructed exactly when \a M is
0115 /// deserialized.
0116 ///
0117 /// If \c Index is supplied, the bitcode will contain the summary index
0118 /// (currently for use in ThinLTO optimization).
0119 ///
0120 /// \p GenerateHash enables hashing the Module and including the hash in the
0121 /// bitcode (currently for use in ThinLTO incremental build).
0122 ///
0123 /// If \p ModHash is non-null, when GenerateHash is true, the resulting
0124 /// hash is written into ModHash. When GenerateHash is false, that value
0125 /// is used as the hash instead of computing from the generated bitcode.
0126 /// Can be used to produce the same module hash for a minimized bitcode
0127 /// used just for the thin link as in the regular full bitcode that will
0128 /// be used in the backend.
0129 void WriteBitcodeToFile(const Module &M, raw_ostream &Out,
0130                         bool ShouldPreserveUseListOrder = false,
0131                         const ModuleSummaryIndex *Index = nullptr,
0132                         bool GenerateHash = false,
0133                         ModuleHash *ModHash = nullptr);
0134 
0135 /// Write the specified thin link bitcode file (i.e., the minimized bitcode
0136 /// file) to the given raw output stream, where it will be written in a new
0137 /// bitcode block. The thin link bitcode file is used for thin link, and it
0138 /// only contains the necessary information for thin link.
0139 ///
0140 /// ModHash is for use in ThinLTO incremental build, generated while the IR
0141 /// bitcode file writing.
0142 void writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
0143                                 const ModuleSummaryIndex &Index,
0144                                 const ModuleHash &ModHash);
0145 
0146 /// Write the specified module summary index to the given raw output stream,
0147 /// where it will be written in a new bitcode block. This is used when
0148 /// writing the combined index file for ThinLTO. When writing a subset of the
0149 /// index for a distributed backend, provide the \p ModuleToSummariesForIndex
0150 /// map. \p DecSummaries specifies the set of summaries for which the
0151 /// corresponding value should be imported as a declaration (prototype).
0152 void writeIndexToFile(
0153     const ModuleSummaryIndex &Index, raw_ostream &Out,
0154     const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex = nullptr,
0155     const GVSummaryPtrSet *DecSummaries = nullptr);
0156 
0157 /// If EmbedBitcode is set, save a copy of the llvm IR as data in the
0158 ///  __LLVM,__bitcode section (.llvmbc on non-MacOS).
0159 /// If available, pass the serialized module via the Buf parameter. If not,
0160 /// pass an empty (default-initialized) MemoryBufferRef, and the serialization
0161 /// will be handled by this API. The same behavior happens if the provided Buf
0162 /// is not bitcode (i.e. if it's invalid data or even textual LLVM assembly).
0163 /// If EmbedCmdline is set, the command line is also exported in
0164 /// the corresponding section (__LLVM,_cmdline / .llvmcmd) - even if CmdArgs
0165 /// were empty.
0166 void embedBitcodeInModule(Module &M, MemoryBufferRef Buf, bool EmbedBitcode,
0167                           bool EmbedCmdline,
0168                           const std::vector<uint8_t> &CmdArgs);
0169 
0170 } // end namespace llvm
0171 
0172 #endif // LLVM_BITCODE_BITCODEWRITER_H