1//===- llvm/Bitcode/BitcodeReader.h - Bitcode reader ------------*- 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 read LLVM bitcode files/streams. 11//===----------------------------------------------------------------------===// 13#ifndef LLVM_BITCODE_BITCODEREADER_H 14#define LLVM_BITCODE_BITCODEREADER_H 28#include <system_error> 36classModuleSummaryIndex;
40// Callback to override the data layout string of an imported bitcode module. 41// The first argument is the target triple, the second argument the data layout 42// string from the input, or a default string. It will be used if the callback 43// returns std::nullopt. 44typedef std::function<std::optional<std::string>(StringRef, StringRef)>
59// These functions are for converting Expected/Error values to 60// ErrorOr/std::error_code for compatibility with legacy clients. FIXME: 61// Remove these functions once no longer needed by the C and libLTO APIs. 69return std::move(*Val);
74 /// The ValueType callback is called for every function definition or 75 /// declaration and allows accessing the type information, also behind 76 /// pointers. This can be useful, when the opaque pointer upgrade cleans all 77 /// type information behind pointers. 78 /// The second argument to ValueTypeCallback is the type ID of the 79 /// function, the two passed functions can be used to extract type 82 /// The MDType callback is called for every value in metadata. 83 std::optional<MDTypeCallbackTy>
MDType;
90structBitcodeFileContents;
92 /// Basic information extracted from a bitcode module to be used for LTO. 100 /// Represents a module in a bitcode file. 102// This covers the identification (if present) and module blocks. 106// The string table used to interpret this module. 109// The bitstream location of the IDENTIFICATION_BLOCK. 112// The bitstream location of this module's MODULE_BLOCK. 117 : Buffer(Buffer), ModuleIdentifier(ModuleIdentifier),
118 IdentificationBit(IdentificationBit), ModuleBit(ModuleBit) {}
125 getModuleImpl(
LLVMContext &Context,
bool MaterializeAll,
126bool ShouldLazyLoadMetadata,
bool IsImporting,
138 /// Read the bitcode module and prepare for lazy deserialization of function 139 /// bodies. If ShouldLazyLoadMetadata is true, lazily load metadata as well. 140 /// If IsImporting is true, this module is being parsed for ThinLTO 141 /// importing into another module. 146 /// Read the entire bitcode module and return it. 147 Expected<std::unique_ptr<Module>>
148parseModule(LLVMContext &Context, ParserCallbacks Callbacks = {});
150 /// Returns information about the module to be used for LTO: whether to 151 /// compile with ThinLTO, and whether it has a summary. 154 /// Parse the specified bitcode buffer, returning the module summary index. 155 Expected<std::unique_ptr<ModuleSummaryIndex>>
getSummary();
157 /// Parse the specified bitcode buffer and merge its module summary index 158 /// into CombinedIndex. 160readSummary(ModuleSummaryIndex &CombinedIndex, StringRef ModulePath,
165 std::vector<BitcodeModule>
Mods;
169 /// Returns the contents of a bitcode file. This includes the raw contents of 170 /// the symbol table embedded in the bitcode file. Clients which require a 171 /// symbol table should prefer to use irsymtab::read instead of this function 172 /// because it creates a reader for the irsymtab and handles upgrading bitcode 173 /// files without a symbol table or with an old symbol table. 176 /// Returns a list of modules in the specified bitcode buffer. 180 /// Read the header of the specified bitcode buffer and prepare for lazy 181 /// deserialization of function bodies. If ShouldLazyLoadMetadata is true, 182 /// lazily load metadata as well. If IsImporting is true, this module is 183 /// being parsed for ThinLTO importing into another module. 186bool ShouldLazyLoadMetadata =
false,
187bool IsImporting =
false,
190 /// Like getLazyBitcodeModule, except that the module takes ownership of 191 /// the memory buffer if successful. If successful, this moves Buffer. On 192 /// error, this *does not* move Buffer. If IsImporting is true, this module is 193 /// being parsed for ThinLTO importing into another module. 195 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
196bool ShouldLazyLoadMetadata =
false,
bool IsImporting =
false,
197 ParserCallbacks Callbacks = {});
199 /// Read the header of the specified bitcode buffer and extract just the 200 /// triple information. If successful, this returns a string. On error, this 204 /// Return true if \p Buffer contains a bitcode file with ObjC code (category 208 /// Read the header of the specified bitcode buffer and extract just the 209 /// producer string information. If successful, this returns a string. On 210 /// error, this returns "". 213 /// Read the specified bitcode file, returning the module. 214 Expected<std::unique_ptr<Module>>
216 ParserCallbacks Callbacks = {});
218 /// Returns LTO information for the specified bitcode file. 221 /// Parse the specified bitcode buffer, returning the module summary index. 222 Expected<std::unique_ptr<ModuleSummaryIndex>>
225 /// Parse the specified bitcode buffer and merge the index into CombinedIndex. 227 ModuleSummaryIndex &CombinedIndex);
229 /// Parse the module summary index out of an IR file and return the module 230 /// summary index object if found, or an empty summary if not. If Path refers 231 /// to an empty file and IgnoreEmptyThinLTOIndexFile is true, then 232 /// this function will return nullptr. 233 Expected<std::unique_ptr<ModuleSummaryIndex>>
235bool IgnoreEmptyThinLTOIndexFile =
false);
237 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes 238 /// for an LLVM IR bitcode wrapper. 240constunsignedchar *BufEnd) {
241// See if you can find the hidden message in the magic bytes :-). 242// (Hint: it's a little-endian encoding.) 243return BufPtr != BufEnd &&
250 /// isRawBitcode - Return true if the given bytes are the magic bytes for 251 /// raw LLVM IR bitcode (without a wrapper). 253constunsignedchar *BufEnd) {
254// These bytes sort of have a hidden message, but it's not in 255// little-endian this time, and it's a little redundant. 256return BufPtr != BufEnd &&
263 /// isBitcode - Return true if the given bytes are the magic bytes for 264 /// LLVM IR bitcode, either with or without a wrapper. 266constunsignedchar *BufEnd) {
271 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special 272 /// header for padding or other reasons. The format of this header is: 274 /// struct bc_header { 275 /// uint32_t Magic; // 0x0B17C0DE 276 /// uint32_t Version; // Version, currently always 0. 277 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file. 278 /// uint32_t BitcodeSize; // Size of traditional bitcode file. 279 /// ... potentially other gunk ... 282 /// This function is called when we find a file with a matching magic number. 283 /// In this case, skip down to the subsection of the file that is actually a 285 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to 286 /// contain the whole bitcode file. 288constunsignedchar *&BufEnd,
289bool VerifyBufferSize) {
290// Must contain the offset and size field! 298// Verify that Offset+Size fits in the file. 299if (VerifyBufferSize && BitcodeOffsetEnd >
uint64_t(BufEnd-BufPtr))
302 BufEnd = BufPtr+
Size;
306 APInt
readWideAPInt(ArrayRef<uint64_t> Vals,
unsigned TypeBits);
314}
// end namespace llvm 318template <>
structis_error_code_enum<
llvm::BitcodeError> : std::true_type {};
322#endif// LLVM_BITCODE_BITCODEREADER_H static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
dxil translate DXIL Translate Metadata
Provides ErrorOr<T> smart pointer.
Machine Check Debug Module
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Represents a module in a bitcode file.
StringRef getModuleIdentifier() const
Expected< std::unique_ptr< ModuleSummaryIndex > > getSummary()
Parse the specified bitcode buffer, returning the module summary index.
Expected< BitcodeLTOInfo > getLTOInfo()
Returns information about the module to be used for LTO: whether to compile with ThinLTO,...
StringRef getBuffer() const
Error readSummary(ModuleSummaryIndex &CombinedIndex, StringRef ModulePath, std::function< bool(GlobalValue::GUID)> IsPrevailing=nullptr)
Parse the specified bitcode buffer and merge its module summary index into CombinedIndex.
Expected< std::unique_ptr< Module > > parseModule(LLVMContext &Context, ParserCallbacks Callbacks={})
Read the entire bitcode module and return it.
StringRef getStrtab() const
friend Expected< BitcodeFileContents > getBitcodeFileContents(MemoryBufferRef Buffer)
Returns the contents of a bitcode file.
Expected< std::unique_ptr< Module > > getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, bool IsImporting, ParserCallbacks Callbacks={})
Read the bitcode module and prepare for lazy deserialization of function bodies.
A parsed version of the target data layout string in and methods for querying it.
Represents either an error or a value T.
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
This is an important class for using LLVM in a threaded context.
Root of the metadata hierarchy.
StringRef - Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
uint32_t read32le(const void *P)
This is an optimization pass for GlobalISel generic memory operations.
const std::error_category & BitcodeErrorCategory()
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, ParserCallbacks Callbacks={})
Read the specified bitcode file, returning the module.
std::error_code make_error_code(BitcodeError E)
std::function< Type *(unsigned)> GetTypeByIDTy
Expected< bool > isBitcodeContainingObjCCategory(MemoryBufferRef Buffer)
Return true if Buffer contains a bitcode file with ObjC code (category or class) in it.
std::function< unsigned(unsigned, unsigned)> GetContainedTypeIDTy
Expected< std::string > getBitcodeTargetTriple(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the triple information.
Expected< BitcodeFileContents > getBitcodeFileContents(MemoryBufferRef Buffer)
Returns the contents of a bitcode file.
ErrorOr< T > expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected< T > Val)
bool isRawBitcode(const unsigned char *BufPtr, const unsigned char *BufEnd)
isRawBitcode - Return true if the given bytes are the magic bytes for raw LLVM IR bitcode (without a ...
Expected< std::unique_ptr< ModuleSummaryIndex > > getModuleSummaryIndex(MemoryBufferRef Buffer)
Parse the specified bitcode buffer, returning the module summary index.
Expected< std::string > getBitcodeProducerString(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the producer string information.
Expected< std::unique_ptr< Module > > getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata=false, bool IsImporting=false, ParserCallbacks Callbacks={})
Read the header of the specified bitcode buffer and prepare for lazy deserialization of function bodi...
std::function< void(Metadata **, unsigned, GetTypeByIDTy, GetContainedTypeIDTy)> MDTypeCallbackTy
Expected< std::vector< BitcodeModule > > getBitcodeModuleList(MemoryBufferRef Buffer)
Returns a list of modules in the specified bitcode buffer.
Expected< BitcodeLTOInfo > getBitcodeLTOInfo(MemoryBufferRef Buffer)
Returns LTO information for the specified bitcode file.
bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr, const unsigned char *&BufEnd, bool VerifyBufferSize)
SkipBitcodeWrapperHeader - Some systems wrap bc files with a special header for padding or other reas...
bool isBitcodeWrapper(const unsigned char *BufPtr, const unsigned char *BufEnd)
isBitcodeWrapper - Return true if the given bytes are the magic bytes for an LLVM IR bitcode wrapper.
APInt readWideAPInt(ArrayRef< uint64_t > Vals, unsigned TypeBits)
std::function< void(Value *, unsigned, GetTypeByIDTy, GetContainedTypeIDTy)> ValueTypeCallbackTy
std::function< std::optional< std::string >(StringRef, StringRef)> DataLayoutCallbackFuncTy
bool isBitcode(const unsigned char *BufPtr, const unsigned char *BufEnd)
isBitcode - Return true if the given bytes are the magic bytes for LLVM IR bitcode,...
Error readModuleSummaryIndex(MemoryBufferRef Buffer, ModuleSummaryIndex &CombinedIndex)
Parse the specified bitcode buffer and merge the index into CombinedIndex.
Expected< std::unique_ptr< ModuleSummaryIndex > > getModuleSummaryIndexForFile(StringRef Path, bool IgnoreEmptyThinLTOIndexFile=false)
Parse the module summary index out of an IR file and return the module summary index object if found,...
Expected< std::unique_ptr< Module > > getOwningLazyBitcodeModule(std::unique_ptr< MemoryBuffer > &&Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata=false, bool IsImporting=false, ParserCallbacks Callbacks={})
Like getLazyBitcodeModule, except that the module takes ownership of the memory buffer if successful.
std::error_code errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, Error Err)
Implement std::hash so that hash_code can be used in STL containers.
StringRef StrtabForSymtab
std::vector< BitcodeModule > Mods
Basic information extracted from a bitcode module to be used for LTO.
std::optional< ValueTypeCallbackTy > ValueType
The ValueType callback is called for every function definition or declaration and allows accessing th...
std::optional< DataLayoutCallbackFuncTy > DataLayout
ParserCallbacks(DataLayoutCallbackFuncTy DataLayout)
std::optional< MDTypeCallbackTy > MDType
The MDType callback is called for every value in metadata.
ParserCallbacks()=default