Skip to content

Commit

Permalink
IR, Bitcode: Change bitcode reader to no longer own its memory buffer.
Browse files Browse the repository at this point in the history
Unique ownership is just one possible ownership pattern for the memory buffer
underlying the bitcode reader. In practice, as this patch shows, ownership can
often reside at a higher level. With the upcoming change to allow multiple
modules in a single bitcode file, it will no longer be appropriate for
modules to generally have unique ownership of their memory buffer.

The C API exposes the ownership relation via the LLVMGetBitcodeModuleInContext
and LLVMGetBitcodeModuleInContext2 functions, so we still need some way for
the module to own the memory buffer. This patch does so by adding an owned
memory buffer field to Module, and using it in a few other places where it
is convenient.

Differential Revision: https://reviews.llvm.org/D26384

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286214 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
pcc committed Nov 8, 2016
1 parent adbd909 commit 5498e18
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 137 deletions.
14 changes: 10 additions & 4 deletions include/llvm/Bitcode/ReaderWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ namespace llvm {

/// Read the header of the specified bitcode buffer and prepare for lazy
/// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
/// lazily load metadata as well. If successful, this moves Buffer. On
/// error, this *does not* move Buffer.
/// lazily load metadata as well.
ErrorOr<std::unique_ptr<Module>>
getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
LLVMContext &Context,
getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,
bool ShouldLazyLoadMetadata = false);

/// Like getLazyBitcodeModule, except that the module takes ownership of
/// the memory buffer if successful. If successful, this moves Buffer. On
/// error, this *does not* move Buffer.
ErrorOr<std::unique_ptr<Module>>
getOwningLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
LLVMContext &Context,
bool ShouldLazyLoadMetadata = false);

/// Read the header of the specified bitcode buffer and extract just the
/// triple information. If successful, this returns a string. On error, this
/// returns "".
Expand Down
7 changes: 7 additions & 0 deletions include/llvm/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ template <typename T> class Optional;
class FunctionType;
class GVMaterializer;
class LLVMContext;
class MemoryBuffer;
class RandomNumberGenerator;
class StructType;
template <class PtrType> class SmallPtrSetImpl;
Expand Down Expand Up @@ -158,6 +159,9 @@ class Module {
std::string GlobalScopeAsm; ///< Inline Asm at global scope.
ValueSymbolTable *ValSymTab; ///< Symbol table for values
ComdatSymTabType ComdatSymTab; ///< Symbol table for COMDATs
std::unique_ptr<MemoryBuffer>
OwnedMemoryBuffer; ///< Memory buffer directly owned by this
///< module, for legacy clients only.
std::unique_ptr<GVMaterializer>
Materializer; ///< Used to materialize GlobalValues
std::string ModuleID; ///< Human readable identifier for the module
Expand Down Expand Up @@ -802,6 +806,9 @@ class Module {
/// \brief Returns profile summary metadata
Metadata *getProfileSummary();
/// @}

/// Take ownership of the given memory buffer.
void setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB);
};

/// \brief Given "llvm.used" or "llvm.compiler.used" as a global name, collect
Expand Down
4 changes: 2 additions & 2 deletions lib/Bitcode/Reader/BitReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));

ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
getLazyBitcodeModule(std::move(Owner), Ctx);
getOwningLazyBitcodeModule(std::move(Owner), Ctx);
Owner.release();
Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);

Expand All @@ -124,7 +124,7 @@ LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));

ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
getLazyBitcodeModule(std::move(Owner), Ctx);
getOwningLazyBitcodeModule(std::move(Owner), Ctx);
Owner.release();

if (ModuleOrErr.getError()) {
Expand Down
Loading

0 comments on commit 5498e18

Please sign in to comment.