llvm.org GIT mirror llvm / 5498e18
IR, Bitcode: Change bitcode reader to no longer own its memory buffer. 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 Peter Collingbourne 2 years ago
12 changed file(s) with 96 addition(s) and 146 deletion(s). Raw diff Collapse all Expand all
3838
3939 /// Read the header of the specified bitcode buffer and prepare for lazy
4040 /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
41 /// lazily load metadata as well. If successful, this moves Buffer. On
41 /// lazily load metadata as well.
42 ErrorOr>
43 getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,
44 bool ShouldLazyLoadMetadata = false);
45
46 /// Like getLazyBitcodeModule, except that the module takes ownership of
47 /// the memory buffer if successful. If successful, this moves Buffer. On
4248 /// error, this *does not* move Buffer.
4349 ErrorOr>
44 getLazyBitcodeModule(std::unique_ptr &&Buffer,
45 LLVMContext &Context,
46 bool ShouldLazyLoadMetadata = false);
50 getOwningLazyBitcodeModule(std::unique_ptr &&Buffer,
51 LLVMContext &Context,
52 bool ShouldLazyLoadMetadata = false);
4753
4854 /// Read the header of the specified bitcode buffer and extract just the
4955 /// triple information. If successful, this returns a string. On error, this
3232 class FunctionType;
3333 class GVMaterializer;
3434 class LLVMContext;
35 class MemoryBuffer;
3536 class RandomNumberGenerator;
3637 class StructType;
3738 template class SmallPtrSetImpl;
157158 std::string GlobalScopeAsm; ///< Inline Asm at global scope.
158159 ValueSymbolTable *ValSymTab; ///< Symbol table for values
159160 ComdatSymTabType ComdatSymTab; ///< Symbol table for COMDATs
161 std::unique_ptr
162 OwnedMemoryBuffer; ///< Memory buffer directly owned by this
163 ///< module, for legacy clients only.
160164 std::unique_ptr
161165 Materializer; ///< Used to materialize GlobalValues
162166 std::string ModuleID; ///< Human readable identifier for the module
801805 /// \brief Returns profile summary metadata
802806 Metadata *getProfileSummary();
803807 /// @}
808
809 /// Take ownership of the given memory buffer.
810 void setOwnedMemoryBuffer(std::unique_ptr MB);
804811 };
805812
806813 /// \brief Given "llvm.used" or "llvm.compiler.used" as a global name, collect
100100 std::unique_ptr Owner(unwrap(MemBuf));
101101
102102 ErrorOr> ModuleOrErr =
103 getLazyBitcodeModule(std::move(Owner), Ctx);
103 getOwningLazyBitcodeModule(std::move(Owner), Ctx);
104104 Owner.release();
105105 Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
106106
123123 std::unique_ptr Owner(unwrap(MemBuf));
124124
125125 ErrorOr> ModuleOrErr =
126 getLazyBitcodeModule(std::move(Owner), Ctx);
126 getOwningLazyBitcodeModule(std::move(Owner), Ctx);
127127 Owner.release();
128128
129129 if (ModuleOrErr.getError()) {
229229
230230 class BitcodeReaderBase {
231231 protected:
232 BitcodeReaderBase(MemoryBuffer *Buffer) : Buffer(Buffer) {}
233
234 std::unique_ptr Buffer;
232 BitcodeReaderBase(MemoryBufferRef Buffer) : Buffer(Buffer) {}
233
234 MemoryBufferRef Buffer;
235235 BitstreamBlockInfo BlockInfo;
236236 BitstreamCursor Stream;
237237
243243 };
244244
245245 std::error_code BitcodeReaderBase::initStream() {
246 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
247 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
248
249 if (Buffer->getBufferSize() & 3)
246 const unsigned char *BufPtr = (const unsigned char*)Buffer.getBufferStart();
247 const unsigned char *BufEnd = BufPtr+Buffer.getBufferSize();
248
249 if (Buffer.getBufferSize() & 3)
250250 return error("Invalid bitcode signature");
251251
252252 // If we have a wrapper header, parse it and ignore the non-bc file contents.
359359 std::error_code error(BitcodeError E, const Twine &Message);
360360 std::error_code error(const Twine &Message) override;
361361
362 BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context);
363 ~BitcodeReader() override { freeState(); }
362 BitcodeReader(MemoryBufferRef Buffer, LLVMContext &Context);
364363
365364 std::error_code materializeForwardReferencedFunctions();
366
367 void freeState();
368
369 void releaseBuffer();
370365
371366 std::error_code materialize(GlobalValue *GV) override;
372367 std::error_code materializeModule() override;
584579 std::error_code error(const Twine &Message);
585580
586581 ModuleSummaryIndexBitcodeReader(
587 MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
582 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
588583 bool CheckGlobalValSummaryPresenceOnly = false);
589 ~ModuleSummaryIndexBitcodeReader() { freeState(); }
590
591 void freeState();
592
593 void releaseBuffer();
594584
595585 /// Check if the parser has encountered a summary section.
596586 bool foundGlobalValSummary() { return SeenGlobalValSummary; }
660650 Message);
661651 }
662652
663 BitcodeReader::BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context)
653 BitcodeReader::BitcodeReader(MemoryBufferRef Buffer, LLVMContext &Context)
664654 : BitcodeReaderBase(Buffer), Context(Context), ValueList(Context),
665655 MetadataList(Context) {}
666656
695685 // Reset state.
696686 WillMaterializeAllForwardRefs = false;
697687 return std::error_code();
698 }
699
700 void BitcodeReader::freeState() {
701 Buffer = nullptr;
702 std::vector().swap(TypeList);
703 ValueList.clear();
704 MetadataList.clear();
705 std::vector().swap(ComdatList);
706
707 std::vector().swap(MAttributes);
708 std::vector().swap(FunctionBBs);
709 std::vector().swap(FunctionsWithBodies);
710 DeferredFunctionInfo.clear();
711 DeferredMetadataInfo.clear();
712 MDKindMap.clear();
713
714 assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references");
715 BasicBlockFwdRefQueue.clear();
716688 }
717689
718690 //===----------------------------------------------------------------------===//
58865858 // GVMaterializer implementation
58875859 //===----------------------------------------------------------------------===//
58885860
5889 void BitcodeReader::releaseBuffer() { Buffer.release(); }
5890
58915861 std::error_code BitcodeReader::materialize(GlobalValue *GV) {
58925862 Function *F = dyn_cast(GV);
58935863 // If it's not a function or is already material, ignore the request.
60055975 }
60065976
60075977 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
6008 MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
5978 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
60095979 bool CheckGlobalValSummaryPresenceOnly)
60105980 : BitcodeReaderBase(Buffer),
60115981 DiagnosticHandler(std::move(DiagnosticHandler)),
60125982 CheckGlobalValSummaryPresenceOnly(CheckGlobalValSummaryPresenceOnly) {}
6013
6014 void ModuleSummaryIndexBitcodeReader::freeState() { Buffer = nullptr; }
6015
6016 void ModuleSummaryIndexBitcodeReader::releaseBuffer() { Buffer.release(); }
60175983
60185984 std::pair
60195985 ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) {
62186184 break;
62196185 if (TheIndex->modulePaths().empty())
62206186 // We always seed the index with the module.
6221 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0);
6187 TheIndex->addModulePath(Buffer.getBufferIdentifier(), 0);
62226188 if (TheIndex->modulePaths().size() != 1)
62236189 return error("Don't expect multiple modules defined?");
62246190 auto &Hash = TheIndex->modulePaths().begin()->second.second;
63586324 // module path string table entry with an empty (0) ID to take
63596325 // ownership.
63606326 FS->setModulePath(
6361 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)->first());
6327 TheIndex->addModulePath(Buffer.getBufferIdentifier(), 0)->first());
63626328 static int RefListStartIndex = 4;
63636329 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
63646330 assert(Record.size() >= RefListStartIndex + NumRefs &&
63976363 // module path string table entry with an empty (0) ID to take
63986364 // ownership.
63996365 AS->setModulePath(
6400 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)->first());
6366 TheIndex->addModulePath(Buffer.getBufferIdentifier(), 0)->first());
64016367
64026368 GlobalValue::GUID AliaseeGUID = getGUIDFromValueId(AliaseeID).first;
64036369 auto *AliaseeSummary = TheIndex->getGlobalValueSummary(AliaseeGUID);
64186384 std::unique_ptr FS =
64196385 llvm::make_unique(Flags);
64206386 FS->setModulePath(
6421 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)->first());
6387 TheIndex->addModulePath(Buffer.getBufferIdentifier(), 0)->first());
64226388 for (unsigned I = 2, E = Record.size(); I != E; ++I) {
64236389 unsigned RefValueId = Record[I];
64246390 GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId).first;
66766642 // External interface
66776643 //===----------------------------------------------------------------------===//
66786644
6679 static ErrorOr>
6680 getBitcodeModuleImpl(StringRef Name, BitcodeReader *R, LLVMContext &Context,
6681 bool MaterializeAll, bool ShouldLazyLoadMetadata) {
6682 std::unique_ptr M = llvm::make_unique(Name, Context);
6683 M->setMaterializer(R);
6684
6685 auto cleanupOnError = [&](std::error_code EC) {
6686 R->releaseBuffer(); // Never take ownership on error.
6687 return EC;
6688 };
6689
6690 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
6691 if (std::error_code EC = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata))
6692 return cleanupOnError(EC);
6693
6694 if (MaterializeAll) {
6695 // Read in the entire module, and destroy the BitcodeReader.
6696 if (std::error_code EC = M->materializeAll())
6697 return cleanupOnError(EC);
6698 } else {
6699 // Resolve forward references from blockaddresses.
6700 if (std::error_code EC = R->materializeForwardReferencedFunctions())
6701 return cleanupOnError(EC);
6702 }
6703 return std::move(M);
6704 }
6705
67066645 /// \brief Get a lazy one-at-time loading module from bitcode.
67076646 ///
67086647 /// This isn't always used in a lazy context. In particular, it's also used by
67126651 /// \param[in] MaterializeAll Set to \c true if we should materialize
67136652 /// everything.
67146653 static ErrorOr>
6715 getLazyBitcodeModuleImpl(std::unique_ptr &&Buffer,
6716 LLVMContext &Context, bool MaterializeAll,
6654 getLazyBitcodeModuleImpl(MemoryBufferRef Buffer, LLVMContext &Context,
6655 bool MaterializeAll,
67176656 bool ShouldLazyLoadMetadata = false) {
6718 BitcodeReader *R = new BitcodeReader(Buffer.get(), Context);
6719
6720 ErrorOr> Ret =
6721 getBitcodeModuleImpl(Buffer->getBufferIdentifier(), R, Context,
6722 MaterializeAll, ShouldLazyLoadMetadata);
6723 if (!Ret)
6724 return Ret;
6725
6726 Buffer.release(); // The BitcodeReader owns it now.
6727 return Ret;
6657 BitcodeReader *R = new BitcodeReader(Buffer, Context);
6658
6659 std::unique_ptr M =
6660 llvm::make_unique(Buffer.getBufferIdentifier(), Context);
6661 M->setMaterializer(R);
6662
6663 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
6664 if (std::error_code EC = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata))
6665 return EC;
6666
6667 if (MaterializeAll) {
6668 // Read in the entire module, and destroy the BitcodeReader.
6669 if (std::error_code EC = M->materializeAll())
6670 return EC;
6671 } else {
6672 // Resolve forward references from blockaddresses.
6673 if (std::error_code EC = R->materializeForwardReferencedFunctions())
6674 return EC;
6675 }
6676 return std::move(M);
67286677 }
67296678
67306679 ErrorOr>
6731 llvm::getLazyBitcodeModule(std::unique_ptr &&Buffer,
6680 llvm::getLazyBitcodeModule(MemoryBufferRef Buffer,
67326681 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
6733 return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
6682 return getLazyBitcodeModuleImpl(Buffer, Context, false,
67346683 ShouldLazyLoadMetadata);
6684 }
6685
6686 ErrorOr>
6687 llvm::getOwningLazyBitcodeModule(std::unique_ptr &&Buffer,
6688 LLVMContext &Context,
6689 bool ShouldLazyLoadMetadata) {
6690 auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata);
6691 if (MOrErr)
6692 (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer));
6693 return MOrErr;
67356694 }
67366695
67376696 ErrorOr> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
67386697 LLVMContext &Context) {
6739 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6740 return getLazyBitcodeModuleImpl(std::move(Buf), Context, true);
6698 return getLazyBitcodeModuleImpl(Buffer, Context, true);
67416699 // TODO: Restore the use-lists to the in-memory state when the bitcode was
67426700 // written. We must defer until the Module has been fully materialized.
67436701 }
67446702
67456703 std::string llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer,
67466704 LLVMContext &Context) {
6747 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6748 auto R = llvm::make_unique(Buf.release(), Context);
6749 ErrorOr Triple = R->parseTriple();
6705 BitcodeReader R(Buffer, Context);
6706 ErrorOr Triple = R.parseTriple();
67506707 if (Triple.getError())
67516708 return "";
67526709 return Triple.get();
67546711
67556712 bool llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer,
67566713 LLVMContext &Context) {
6757 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6758 auto R = llvm::make_unique(Buf.release(), Context);
6759 ErrorOr hasObjCCategory = R->hasObjCCategory();
6714 BitcodeReader R(Buffer, Context);
6715 ErrorOr hasObjCCategory = R.hasObjCCategory();
67606716 if (hasObjCCategory.getError())
67616717 return false;
67626718 return hasObjCCategory.get();
67646720
67656721 std::string llvm::getBitcodeProducerString(MemoryBufferRef Buffer,
67666722 LLVMContext &Context) {
6767 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6768 BitcodeReader R(Buf.release(), Context);
6723 BitcodeReader R(Buffer, Context);
67696724 ErrorOr ProducerString = R.parseIdentificationBlock();
67706725 if (ProducerString.getError())
67716726 return "";
67766731 ErrorOr> llvm::getModuleSummaryIndex(
67776732 MemoryBufferRef Buffer,
67786733 const DiagnosticHandlerFunction &DiagnosticHandler) {
6779 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6780 ModuleSummaryIndexBitcodeReader R(Buf.get(), DiagnosticHandler);
6734 ModuleSummaryIndexBitcodeReader R(Buffer, DiagnosticHandler);
67816735
67826736 auto Index = llvm::make_unique();
67836737
6784 auto cleanupOnError = [&](std::error_code EC) {
6785 R.releaseBuffer(); // Never take ownership on error.
6738 if (std::error_code EC = R.parseSummaryIndexInto(Index.get()))
67866739 return EC;
6787 };
6788
6789 if (std::error_code EC = R.parseSummaryIndexInto(Index.get()))
6790 return cleanupOnError(EC);
6791
6792 Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now.
6740
67936741 return std::move(Index);
67946742 }
67956743
67976745 bool llvm::hasGlobalValueSummary(
67986746 MemoryBufferRef Buffer,
67996747 const DiagnosticHandlerFunction &DiagnosticHandler) {
6800 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6801 ModuleSummaryIndexBitcodeReader R(Buf.get(), DiagnosticHandler, true);
6802
6803 auto cleanupOnError = [&](std::error_code EC) {
6804 R.releaseBuffer(); // Never take ownership on error.
6748 ModuleSummaryIndexBitcodeReader R(Buffer, DiagnosticHandler, true);
6749
6750 if (R.parseSummaryIndexInto(nullptr))
68056751 return false;
6806 };
6807
6808 if (std::error_code EC = R.parseSummaryIndexInto(nullptr))
6809 return cleanupOnError(EC);
6810
6811 Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now.
6752
68126753 return R.foundGlobalValSummary();
68136754 }
2424 #include "llvm/IR/LLVMContext.h"
2525 #include "llvm/IR/TypeFinder.h"
2626 #include "llvm/Support/Dwarf.h"
27 #include "llvm/Support/MemoryBuffer.h"
2728 #include "llvm/Support/Path.h"
2829 #include "llvm/Support/RandomNumberGenerator.h"
2930 #include
518519 return getModuleFlag("ProfileSummary");
519520 }
520521
522 void Module::setOwnedMemoryBuffer(std::unique_ptr MB) {
523 OwnedMemoryBuffer = std::move(MB);
524 }
525
521526 GlobalVariable *llvm::collectUsedGlobalVariables(
522527 const Module &M, SmallPtrSetImpl &Set, bool CompilerUsed) {
523528 const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
3333 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
3434 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
3535 (const unsigned char *)Buffer->getBufferEnd())) {
36 ErrorOr> ModuleOrErr = getLazyBitcodeModule(
36 ErrorOr> ModuleOrErr = getOwningLazyBitcodeModule(
3737 std::move(Buffer), Context, ShouldLazyLoadMetadata);
3838 if (std::error_code EC = ModuleOrErr.getError()) {
3939 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
104104 SMDiagnostic Err;
105105 ErrorOr> ModuleOrErr(nullptr);
106106 if (Lazy) {
107 ModuleOrErr =
108 getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context,
109 /* ShouldLazyLoadMetadata */ Lazy);
107 ModuleOrErr = getLazyBitcodeModule(Buffer, Context,
108 /* ShouldLazyLoadMetadata */ Lazy);
110109 } else {
111110 ModuleOrErr = parseBitcodeFile(Buffer, Context);
112111 }
351351 auto ModuleLoader = [&](StringRef Identifier) {
352352 assert(Mod.getContext().isODRUniquingDebugTypes() &&
353353 "ODR Type uniquing should be enabled on the context");
354 return std::move(getLazyBitcodeModule(MemoryBuffer::getMemBuffer(
355 ModuleMap[Identifier], false),
354 return std::move(getLazyBitcodeModule(ModuleMap[Identifier],
356355 Mod.getContext(),
357356 /*ShouldLazyLoadMetadata=*/true)
358357 .get());
184184 }
185185
186186 // Parse lazily.
187 std::unique_ptr LightweightBuf =
188 MemoryBuffer::getMemBuffer(*MBOrErr, false);
189 ErrorOr> M = getLazyBitcodeModule(
190 std::move(LightweightBuf), Context, true /*ShouldLazyLoadMetadata*/);
187 ErrorOr> M =
188 getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/);
191189 if (std::error_code EC = M.getError())
192190 return EC;
193191 return std::move(*M);
316316 if (!BCOrErr)
317317 return BCOrErr.getError();
318318
319 std::unique_ptr Buff =
320 MemoryBuffer::getMemBuffer(BCOrErr.get(), false);
321
322319 ErrorOr> MOrErr =
323 getLazyBitcodeModule(std::move(Buff), Context,
320 getLazyBitcodeModule(*BCOrErr, Context,
324321 /*ShouldLazyLoadMetadata*/ true);
325322 if (std::error_code EC = MOrErr.getError())
326323 return EC;
142142 if (!MBOrErr)
143143 return errorCodeToError(MBOrErr.getError());
144144 ErrorOr> MOrErr =
145 getLazyBitcodeModule(std::move(*MBOrErr), Context,
146 /*ShouldLazyLoadMetadata=*/true);
145 getOwningLazyBitcodeModule(std::move(*MBOrErr), Context,
146 /*ShouldLazyLoadMetadata=*/true);
147147 if (!MOrErr)
148148 return errorCodeToError(MOrErr.getError());
149149 if (MaterializeMetadata)
5252 SmallString<1024> &Mem,
5353 const char *Assembly) {
5454 writeModuleToBuffer(parseAssembly(Context, Assembly), Mem);
55 std::unique_ptr Buffer =
56 MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
5755 ErrorOr> ModuleOrErr =
58 getLazyBitcodeModule(std::move(Buffer), Context);
56 getLazyBitcodeModule(MemoryBufferRef(Mem.str(), "test"), Context);
5957 return std::move(ModuleOrErr.get());
6058 }
6159