llvm.org GIT mirror llvm / dead081
Bitcode: Change module reader functions to return an llvm::Expected. Differential Revision: https://reviews.llvm.org/D26562 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286752 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 2 years ago
16 changed file(s) with 100 addition(s) and 122 deletion(s). Raw diff Collapse all Expand all
4242 /// Read the header of the specified bitcode buffer and prepare for lazy
4343 /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
4444 /// lazily load metadata as well.
45 ErrorOr>
45 Expected>
4646 getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,
4747 bool ShouldLazyLoadMetadata = false);
4848
4949 /// Like getLazyBitcodeModule, except that the module takes ownership of
5050 /// the memory buffer if successful. If successful, this moves Buffer. On
5151 /// error, this *does not* move Buffer.
52 ErrorOr>
52 Expected>
5353 getOwningLazyBitcodeModule(std::unique_ptr &&Buffer,
5454 LLVMContext &Context,
5555 bool ShouldLazyLoadMetadata = false);
6969 Expected getBitcodeProducerString(MemoryBufferRef Buffer);
7070
7171 /// Read the specified bitcode file, returning the module.
72 ErrorOr> parseBitcodeFile(MemoryBufferRef Buffer,
73 LLVMContext &Context);
72 Expected> parseBitcodeFile(MemoryBufferRef Buffer,
73 LLVMContext &Context);
7474
7575 /// Check if the given bitcode buffer contains a summary block.
7676 Expected hasGlobalValueSummary(MemoryBufferRef Buffer);
7474 static ErrorOr
7575 findBitcodeInMemBuffer(MemoryBufferRef Object);
7676
77 static ErrorOr> create(MemoryBufferRef Object,
78 LLVMContext &Context);
77 static Expected> create(MemoryBufferRef Object,
78 LLVMContext &Context);
7979 };
8080 }
8181 }
4343 /// The set contains an entry for every global value the module exports.
4444 typedef std::unordered_set ExportSetTy;
4545
46 /// A function of this type is used to load modules referenced by the index.
47 typedef std::function>(StringRef Identifier)>
48 ModuleLoaderTy;
49
4650 /// Create a Function Importer.
47 FunctionImporter(
48 const ModuleSummaryIndex &Index,
49 std::function(StringRef Identifier)> ModuleLoader)
51 FunctionImporter(const ModuleSummaryIndex &Index, ModuleLoaderTy ModuleLoader)
5052 : Index(Index), ModuleLoader(std::move(ModuleLoader)) {}
5153
5254 /// Import functions in Module \p M based on the supplied import list.
6264 const ModuleSummaryIndex &Index;
6365
6466 /// Factory function to load a Module for a given identifier
65 std::function(StringRef Identifier)> ModuleLoader;
67 ModuleLoaderTy ModuleLoader;
6668 };
6769
6870 /// The function importing pass
3333 return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
3434 }
3535
36 static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
37 auto *Message = reinterpret_cast(C);
38 raw_string_ostream Stream(*Message);
39 DiagnosticPrinterRawOStream DP(Stream);
40 DI.print(DP);
41 }
42
4336 LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
4437 LLVMMemoryBufferRef MemBuf,
4538 LLVMModuleRef *OutModule,
4740 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
4841 LLVMContext &Ctx = *unwrap(ContextRef);
4942
50 LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
51 Ctx.getDiagnosticHandler();
52 void *OldDiagnosticContext = Ctx.getDiagnosticContext();
53 std::string Message;
54 Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true);
55
56 ErrorOr> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
57
58 Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
59
60 if (ModuleOrErr.getError()) {
43 Expected> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
44 if (Error Err = ModuleOrErr.takeError()) {
45 std::string Message;
46 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
47 Message = EIB.message();
48 });
6149 if (OutMessage)
6250 *OutMessage = strdup(Message.c_str());
6351 *OutModule = wrap((Module *)nullptr);
7462 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
7563 LLVMContext &Ctx = *unwrap(ContextRef);
7664
77 ErrorOr> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
65 ErrorOr> ModuleOrErr =
66 expectedToErrorOrAndEmitErrors(Ctx, parseBitcodeFile(Buf, Ctx));
7867 if (ModuleOrErr.getError()) {
7968 *OutModule = wrap((Module *)nullptr);
8069 return 1;
9180 LLVMMemoryBufferRef MemBuf,
9281 LLVMModuleRef *OutM, char **OutMessage) {
9382 LLVMContext &Ctx = *unwrap(ContextRef);
94 LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
95 Ctx.getDiagnosticHandler();
96 void *OldDiagnosticContext = Ctx.getDiagnosticContext();
97
98 std::string Message;
99 Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true);
10083 std::unique_ptr Owner(unwrap(MemBuf));
101
102 ErrorOr> ModuleOrErr =
84 Expected> ModuleOrErr =
10385 getOwningLazyBitcodeModule(std::move(Owner), Ctx);
10486 Owner.release();
105 Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
10687
107 if (ModuleOrErr.getError()) {
108 *OutM = wrap((Module *)nullptr);
88 if (Error Err = ModuleOrErr.takeError()) {
89 std::string Message;
90 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
91 Message = EIB.message();
92 });
10993 if (OutMessage)
11094 *OutMessage = strdup(Message.c_str());
95 *OutM = wrap((Module *)nullptr);
11196 return 1;
11297 }
11398
122107 LLVMContext &Ctx = *unwrap(ContextRef);
123108 std::unique_ptr Owner(unwrap(MemBuf));
124109
125 ErrorOr> ModuleOrErr =
126 getOwningLazyBitcodeModule(std::move(Owner), Ctx);
110 ErrorOr> ModuleOrErr = expectedToErrorOrAndEmitErrors(
111 Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
127112 Owner.release();
128113
129114 if (ModuleOrErr.getError()) {
65706570 ///
65716571 /// \param[in] MaterializeAll Set to \c true if we should materialize
65726572 /// everything.
6573 static ErrorOr>
6573 static Expected>
65746574 getLazyBitcodeModuleImpl(MemoryBufferRef Buffer, LLVMContext &Context,
65756575 bool MaterializeAll,
65766576 bool ShouldLazyLoadMetadata = false) {
65776577 Expected StreamOrErr = initStream(Buffer);
65786578 if (!StreamOrErr)
6579 return errorToErrorCodeAndEmitErrors(Context, StreamOrErr.takeError());
6579 return StreamOrErr.takeError();
65806580
65816581 BitcodeReader *R = new BitcodeReader(std::move(*StreamOrErr), Context);
65826582
65866586
65876587 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
65886588 if (Error Err = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata))
6589 return errorToErrorCodeAndEmitErrors(Context, std::move(Err));
6589 return std::move(Err);
65906590
65916591 if (MaterializeAll) {
65926592 // Read in the entire module, and destroy the BitcodeReader.
65936593 if (Error Err = M->materializeAll())
6594 return errorToErrorCodeAndEmitErrors(Context, std::move(Err));
6594 return std::move(Err);
65956595 } else {
65966596 // Resolve forward references from blockaddresses.
65976597 if (Error Err = R->materializeForwardReferencedFunctions())
6598 return errorToErrorCodeAndEmitErrors(Context, std::move(Err));
6598 return std::move(Err);
65996599 }
66006600 return std::move(M);
66016601 }
66026602
6603 ErrorOr>
6603 Expected>
66046604 llvm::getLazyBitcodeModule(MemoryBufferRef Buffer,
66056605 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
66066606 return getLazyBitcodeModuleImpl(Buffer, Context, false,
66076607 ShouldLazyLoadMetadata);
66086608 }
66096609
6610 ErrorOr>
6610 Expected>
66116611 llvm::getOwningLazyBitcodeModule(std::unique_ptr &&Buffer,
66126612 LLVMContext &Context,
66136613 bool ShouldLazyLoadMetadata) {
66176617 return MOrErr;
66186618 }
66196619
6620 ErrorOr> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
6621 LLVMContext &Context) {
6620 Expected> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
6621 LLVMContext &Context) {
66226622 return getLazyBitcodeModuleImpl(Buffer, Context, true);
66236623 // TODO: Restore the use-lists to the in-memory state when the bitcode was
66246624 // written. We must defer until the Module has been fully materialized.
7878 CodegenThreadPool.async(
7979 [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
8080 LLVMContext Ctx;
81 ErrorOr> MOrErr = parseBitcodeFile(
81 Expected> MOrErr = parseBitcodeFile(
8282 MemoryBufferRef(StringRef(BC.data(), BC.size()),
8383 ""),
8484 Ctx);
3333 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
3434 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
3535 (const unsigned char *)Buffer->getBufferEnd())) {
36 ErrorOr> ModuleOrErr = getOwningLazyBitcodeModule(
36 Expected> ModuleOrErr = getOwningLazyBitcodeModule(
3737 std::move(Buffer), Context, ShouldLazyLoadMetadata);
38 if (std::error_code EC = ModuleOrErr.getError()) {
39 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
40 EC.message());
38 if (Error E = ModuleOrErr.takeError()) {
39 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
40 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
41 EIB.message());
42 });
4143 return nullptr;
4244 }
4345 return std::move(ModuleOrErr.get());
6870 TimePassesIsEnabled);
6971 if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
7072 (const unsigned char *)Buffer.getBufferEnd())) {
71 ErrorOr> ModuleOrErr =
73 Expected> ModuleOrErr =
7274 parseBitcodeFile(Buffer, Context);
73 if (std::error_code EC = ModuleOrErr.getError()) {
74 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
75 EC.message());
75 if (Error E = ModuleOrErr.takeError()) {
76 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
77 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
78 EIB.message());
79 });
7680 return nullptr;
7781 }
7882 return std::move(ModuleOrErr.get());
103103 llvm::loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
104104 bool Lazy) {
105105 SMDiagnostic Err;
106 ErrorOr> ModuleOrErr(nullptr);
107 if (Lazy) {
108 ModuleOrErr = getLazyBitcodeModule(Buffer, Context,
109 /* ShouldLazyLoadMetadata */ Lazy);
110 } else {
111 ModuleOrErr = parseBitcodeFile(Buffer, Context);
112 }
113 if (std::error_code EC = ModuleOrErr.getError()) {
114 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
115 EC.message());
116 Err.print("ThinLTO", errs());
106 Expected> ModuleOrErr =
107 Lazy ? getLazyBitcodeModule(Buffer, Context,
108 /* ShouldLazyLoadMetadata */ true)
109 : parseBitcodeFile(Buffer, Context);
110 if (!ModuleOrErr) {
111 handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
112 SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(),
113 SourceMgr::DK_Error, EIB.message());
114 Err.print("ThinLTO", errs());
115 });
117116 report_fatal_error("Can't load module, abort.");
118117 }
119118 return std::move(ModuleOrErr.get());
203202
204203 Expected> InputFile::create(MemoryBufferRef Object) {
205204 std::unique_ptr File(new InputFile);
206 std::string Msg;
207 auto DiagHandler = [](const DiagnosticInfo &DI, void *MsgP) {
208 auto *Msg = reinterpret_cast(MsgP);
209 raw_string_ostream OS(*Msg);
210 DiagnosticPrinterRawOStream DP(OS);
211 DI.print(DP);
212 };
213 File->Ctx.setDiagnosticHandler(DiagHandler, static_cast(&Msg));
214
215 ErrorOr> IRObj =
205
206 Expected> IRObj =
216207 IRObjectFile::create(Object, File->Ctx);
217 if (!Msg.empty())
218 return make_error(Msg, inconvertibleErrorCode());
219208 if (!IRObj)
220 return errorCodeToError(IRObj.getError());
209 return IRObj.takeError();
221210 File->Obj = std::move(*IRObj);
222
223 File->Ctx.setDiagnosticHandler(nullptr, nullptr);
224211
225212 for (const auto &C : File->Obj->getModule().getComdatSymbolTable()) {
226213 auto P =
345332 llvm::make_unique("ld-temp.o", RegularLTO.Ctx);
346333 RegularLTO.Mover = llvm::make_unique(*RegularLTO.CombinedModule);
347334 }
348 ErrorOr> ObjOrErr =
335 Expected> ObjOrErr =
349336 IRObjectFile::create(Input->Obj->getMemoryBufferRef(), RegularLTO.Ctx);
350337 if (!ObjOrErr)
351 return errorCodeToError(ObjOrErr.getError());
338 return ObjOrErr.takeError();
352339 std::unique_ptr Obj = std::move(*ObjOrErr);
353340
354341 Module &M = Obj->getModule();
570557 MapVector &ModuleMap) {
571558 auto RunThinBackend = [&](AddStreamFn AddStream) {
572559 LTOLLVMContext BackendContext(Conf);
573 ErrorOr> MOrErr =
560 Expected> MOrErr =
574561 parseBitcodeFile(MBRef, BackendContext);
575 assert(MOrErr && "Unable to load module in thread?");
562 if (!MOrErr)
563 return MOrErr.takeError();
576564
577565 return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
578566 ImportList, DefinedGlobals, ModuleMap);
236236 CodegenThreadPool.async(
237237 [&](const SmallString<0> &BC, unsigned ThreadId) {
238238 LTOLLVMContext Ctx(C);
239 ErrorOr> MOrErr = parseBitcodeFile(
239 Expected> MOrErr = parseBitcodeFile(
240240 MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
241241 Ctx);
242242 if (!MOrErr)
352352 auto ModuleLoader = [&](StringRef Identifier) {
353353 assert(Mod.getContext().isODRUniquingDebugTypes() &&
354354 "ODR Type uniquing should be enabled on the context");
355 return std::move(getLazyBitcodeModule(ModuleMap[Identifier],
356 Mod.getContext(),
357 /*ShouldLazyLoadMetadata=*/true)
358 .get());
355 return getLazyBitcodeModule(ModuleMap[Identifier], Mod.getContext(),
356 /*ShouldLazyLoadMetadata=*/true);
359357 };
360358
361359 FunctionImporter Importer(CombinedIndex, ModuleLoader);
183183
184184 if (!ShouldBeLazy) {
185185 // Parse the full file.
186 ErrorOr> M = parseBitcodeFile(*MBOrErr, Context);
187 if (std::error_code EC = M.getError())
188 return EC;
189 return std::move(*M);
186 return expectedToErrorOrAndEmitErrors(Context,
187 parseBitcodeFile(*MBOrErr, Context));
190188 }
191189
192190 // Parse lazily.
193 ErrorOr> M =
194 getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/);
195 if (std::error_code EC = M.getError())
196 return EC;
197 return std::move(*M);
191 return expectedToErrorOrAndEmitErrors(
192 Context,
193 getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/));
198194 }
199195
200196 ErrorOr>
309309 }
310310 }
311311
312 ErrorOr>
312 Expected>
313313 llvm::object::IRObjectFile::create(MemoryBufferRef Object,
314314 LLVMContext &Context) {
315315 ErrorOr BCOrErr = findBitcodeInMemBuffer(Object);
316316 if (!BCOrErr)
317 return BCOrErr.getError();
318
319 ErrorOr> MOrErr =
317 return errorCodeToError(BCOrErr.getError());
318
319 Expected> MOrErr =
320320 getLazyBitcodeModule(*BCOrErr, Context,
321321 /*ShouldLazyLoadMetadata*/ true);
322 if (std::error_code EC = MOrErr.getError())
323 return EC;
322 if (!MOrErr)
323 return MOrErr.takeError();
324324
325325 std::unique_ptr &M = MOrErr.get();
326326 return llvm::make_unique(BCOrErr.get(), std::move(M));
3434 switch (Type) {
3535 case sys::fs::file_magic::bitcode:
3636 if (Context)
37 return errorOrToExpected(IRObjectFile::create(Object, *Context));
37 return IRObjectFile::create(Object, *Context);
3838 LLVM_FALLTHROUGH;
3939 case sys::fs::file_magic::unknown:
4040 case sys::fs::file_magic::archive:
7272 if (!BCData)
7373 return std::move(Obj);
7474
75 return errorOrToExpected(IRObjectFile::create(
76 MemoryBufferRef(BCData->getBuffer(),
77 Object.getBufferIdentifier()), *Context));
75 return IRObjectFile::create(
76 MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
77 *Context);
7878 }
7979 }
8080 llvm_unreachable("Unexpected Binary File Type");
623623 // Get the module for the import
624624 const auto &FunctionsToImportPerModule = ImportList.find(Name);
625625 assert(FunctionsToImportPerModule != ImportList.end());
626 std::unique_ptr SrcModule = ModuleLoader(Name);
626 Expected> SrcModuleOrErr = ModuleLoader(Name);
627 if (!SrcModuleOrErr)
628 return SrcModuleOrErr.takeError();
629 std::unique_ptr SrcModule = std::move(*SrcModuleOrErr);
627630 assert(&DestModule.getContext() == &SrcModule->getContext() &&
628631 "Context mismatch");
629632
141141 static std::unique_ptr openInputFile(LLVMContext &Context) {
142142 std::unique_ptr MB =
143143 ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
144 std::unique_ptr M = ExitOnErr(errorOrToExpected(
145 getOwningLazyBitcodeModule(std::move(MB), Context,
146 /*ShouldLazyLoadMetadata=*/true)));
144 std::unique_ptr M =
145 ExitOnErr(getOwningLazyBitcodeModule(std::move(MB), Context,
146 /*ShouldLazyLoadMetadata=*/true));
147147 if (MaterializeMetadata)
148148 ExitOnErr(M->materializeMetadata());
149149 else
159159 }
160160
161161 MemoryBuffer *Buffer = BufferOr.get().get();
162 ErrorOr> ModuleOr =
162 Expected> ModuleOr =
163163 parseBitcodeFile(Buffer->getMemBufferRef(), Context);
164164 if (!ModuleOr) {
165 errs() << "verify-uselistorder: error: " << ModuleOr.getError().message()
166 << "\n";
165 logAllUnhandledErrors(ModuleOr.takeError(), errs(),
166 "verify-uselistorder: error: ");
167167 return nullptr;
168168 }
169169 return std::move(ModuleOr.get());
5454 SmallString<1024> &Mem,
5555 const char *Assembly) {
5656 writeModuleToBuffer(parseAssembly(Context, Assembly), Mem);
57 ErrorOr> ModuleOrErr =
57 Expected> ModuleOrErr =
5858 getLazyBitcodeModule(MemoryBufferRef(Mem.str(), "test"), Context);
59 if (!ModuleOrErr)
60 report_fatal_error("Could not parse bitcode module");
5961 return std::move(ModuleOrErr.get());
6062 }
6163