Bitcode: Change module reader functions to return an llvm::Expected.

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

llvm-svn: 286752
This commit is contained in:
Peter Collingbourne 2016-11-13 07:00:17 +00:00
parent 8dff03911c
commit d9445c49ad
18 changed files with 106 additions and 125 deletions

View File

@ -3012,7 +3012,8 @@ void CGOpenMPRuntime::loadOffloadInfoMetadata() {
return; return;
llvm::LLVMContext C; llvm::LLVMContext C;
auto ME = llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C); auto ME = expectedToErrorOrAndEmitErrors(
C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C));
if (ME.getError()) if (ME.getError())
return; return;

View File

@ -771,11 +771,13 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
return nullptr; return nullptr;
} }
ErrorOr<std::unique_ptr<llvm::Module>> ModuleOrErr = Expected<std::unique_ptr<llvm::Module>> ModuleOrErr =
getOwningLazyBitcodeModule(std::move(*BCBuf), *VMContext); getOwningLazyBitcodeModule(std::move(*BCBuf), *VMContext);
if (std::error_code EC = ModuleOrErr.getError()) { if (!ModuleOrErr) {
CI.getDiagnostics().Report(diag::err_cannot_open_file) << LinkBCFile handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
<< EC.message(); CI.getDiagnostics().Report(diag::err_cannot_open_file)
<< LinkBCFile << EIB.message();
});
LinkModules.clear(); LinkModules.clear();
return nullptr; return nullptr;
} }

View File

@ -43,14 +43,14 @@ namespace llvm {
/// Read the header of the specified bitcode buffer and prepare for lazy /// Read the header of the specified bitcode buffer and prepare for lazy
/// deserialization of function bodies. If ShouldLazyLoadMetadata is true, /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
/// lazily load metadata as well. /// lazily load metadata as well.
ErrorOr<std::unique_ptr<Module>> Expected<std::unique_ptr<Module>>
getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,
bool ShouldLazyLoadMetadata = false); bool ShouldLazyLoadMetadata = false);
/// Like getLazyBitcodeModule, except that the module takes ownership of /// Like getLazyBitcodeModule, except that the module takes ownership of
/// the memory buffer if successful. If successful, this moves Buffer. On /// the memory buffer if successful. If successful, this moves Buffer. On
/// error, this *does not* move Buffer. /// error, this *does not* move Buffer.
ErrorOr<std::unique_ptr<Module>> Expected<std::unique_ptr<Module>>
getOwningLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, getOwningLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
LLVMContext &Context, LLVMContext &Context,
bool ShouldLazyLoadMetadata = false); bool ShouldLazyLoadMetadata = false);
@ -70,7 +70,7 @@ namespace llvm {
Expected<std::string> getBitcodeProducerString(MemoryBufferRef Buffer); Expected<std::string> getBitcodeProducerString(MemoryBufferRef Buffer);
/// Read the specified bitcode file, returning the module. /// Read the specified bitcode file, returning the module.
ErrorOr<std::unique_ptr<Module>> parseBitcodeFile(MemoryBufferRef Buffer, Expected<std::unique_ptr<Module>> parseBitcodeFile(MemoryBufferRef Buffer,
LLVMContext &Context); LLVMContext &Context);
/// Check if the given bitcode buffer contains a summary block. /// Check if the given bitcode buffer contains a summary block.

View File

@ -75,7 +75,7 @@ public:
static ErrorOr<MemoryBufferRef> static ErrorOr<MemoryBufferRef>
findBitcodeInMemBuffer(MemoryBufferRef Object); findBitcodeInMemBuffer(MemoryBufferRef Object);
static ErrorOr<std::unique_ptr<IRObjectFile>> create(MemoryBufferRef Object, static Expected<std::unique_ptr<IRObjectFile>> create(MemoryBufferRef Object,
LLVMContext &Context); LLVMContext &Context);
}; };
} }

View File

@ -44,10 +44,12 @@ public:
/// The set contains an entry for every global value the module exports. /// The set contains an entry for every global value the module exports.
typedef std::unordered_set<GlobalValue::GUID> ExportSetTy; typedef std::unordered_set<GlobalValue::GUID> ExportSetTy;
/// A function of this type is used to load modules referenced by the index.
typedef std::function<Expected<std::unique_ptr<Module>>(StringRef Identifier)>
ModuleLoaderTy;
/// Create a Function Importer. /// Create a Function Importer.
FunctionImporter( FunctionImporter(const ModuleSummaryIndex &Index, ModuleLoaderTy ModuleLoader)
const ModuleSummaryIndex &Index,
std::function<std::unique_ptr<Module>(StringRef Identifier)> ModuleLoader)
: Index(Index), ModuleLoader(std::move(ModuleLoader)) {} : Index(Index), ModuleLoader(std::move(ModuleLoader)) {}
/// Import functions in Module \p M based on the supplied import list. /// Import functions in Module \p M based on the supplied import list.
@ -63,7 +65,7 @@ private:
const ModuleSummaryIndex &Index; const ModuleSummaryIndex &Index;
/// Factory function to load a Module for a given identifier /// Factory function to load a Module for a given identifier
std::function<std::unique_ptr<Module>(StringRef Identifier)> ModuleLoader; ModuleLoaderTy ModuleLoader;
}; };
/// The function importing pass /// The function importing pass

View File

@ -34,13 +34,6 @@ LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule); return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
} }
static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
auto *Message = reinterpret_cast<std::string *>(C);
raw_string_ostream Stream(*Message);
DiagnosticPrinterRawOStream DP(Stream);
DI.print(DP);
}
LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
LLVMMemoryBufferRef MemBuf, LLVMMemoryBufferRef MemBuf,
LLVMModuleRef *OutModule, LLVMModuleRef *OutModule,
@ -48,17 +41,12 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef(); MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
LLVMContext &Ctx = *unwrap(ContextRef); LLVMContext &Ctx = *unwrap(ContextRef);
LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler = Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
Ctx.getDiagnosticHandler(); if (Error Err = ModuleOrErr.takeError()) {
void *OldDiagnosticContext = Ctx.getDiagnosticContext();
std::string Message; std::string Message;
Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true); handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
Message = EIB.message();
ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx); });
Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
if (ModuleOrErr.getError()) {
if (OutMessage) if (OutMessage)
*OutMessage = strdup(Message.c_str()); *OutMessage = strdup(Message.c_str());
*OutModule = wrap((Module *)nullptr); *OutModule = wrap((Module *)nullptr);
@ -75,7 +63,8 @@ LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,
MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef(); MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
LLVMContext &Ctx = *unwrap(ContextRef); LLVMContext &Ctx = *unwrap(ContextRef);
ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx); ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
expectedToErrorOrAndEmitErrors(Ctx, parseBitcodeFile(Buf, Ctx));
if (ModuleOrErr.getError()) { if (ModuleOrErr.getError()) {
*OutModule = wrap((Module *)nullptr); *OutModule = wrap((Module *)nullptr);
return 1; return 1;
@ -92,23 +81,19 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
LLVMMemoryBufferRef MemBuf, LLVMMemoryBufferRef MemBuf,
LLVMModuleRef *OutM, char **OutMessage) { LLVMModuleRef *OutM, char **OutMessage) {
LLVMContext &Ctx = *unwrap(ContextRef); LLVMContext &Ctx = *unwrap(ContextRef);
LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
Ctx.getDiagnosticHandler();
void *OldDiagnosticContext = Ctx.getDiagnosticContext();
std::string Message;
Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true);
std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf)); std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
Expected<std::unique_ptr<Module>> ModuleOrErr =
ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
getOwningLazyBitcodeModule(std::move(Owner), Ctx); getOwningLazyBitcodeModule(std::move(Owner), Ctx);
Owner.release(); Owner.release();
Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
if (ModuleOrErr.getError()) { if (Error Err = ModuleOrErr.takeError()) {
*OutM = wrap((Module *)nullptr); std::string Message;
handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
Message = EIB.message();
});
if (OutMessage) if (OutMessage)
*OutMessage = strdup(Message.c_str()); *OutMessage = strdup(Message.c_str());
*OutM = wrap((Module *)nullptr);
return 1; return 1;
} }
@ -123,8 +108,8 @@ LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
LLVMContext &Ctx = *unwrap(ContextRef); LLVMContext &Ctx = *unwrap(ContextRef);
std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf)); std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
ErrorOr<std::unique_ptr<Module>> ModuleOrErr = ErrorOr<std::unique_ptr<Module>> ModuleOrErr = expectedToErrorOrAndEmitErrors(
getOwningLazyBitcodeModule(std::move(Owner), Ctx); Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
Owner.release(); Owner.release();
if (ModuleOrErr.getError()) { if (ModuleOrErr.getError()) {

View File

@ -6571,13 +6571,13 @@ const std::error_category &llvm::BitcodeErrorCategory() {
/// ///
/// \param[in] MaterializeAll Set to \c true if we should materialize /// \param[in] MaterializeAll Set to \c true if we should materialize
/// everything. /// everything.
static ErrorOr<std::unique_ptr<Module>> static Expected<std::unique_ptr<Module>>
getLazyBitcodeModuleImpl(MemoryBufferRef Buffer, LLVMContext &Context, getLazyBitcodeModuleImpl(MemoryBufferRef Buffer, LLVMContext &Context,
bool MaterializeAll, bool MaterializeAll,
bool ShouldLazyLoadMetadata = false) { bool ShouldLazyLoadMetadata = false) {
Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);
if (!StreamOrErr) if (!StreamOrErr)
return errorToErrorCodeAndEmitErrors(Context, StreamOrErr.takeError()); return StreamOrErr.takeError();
BitcodeReader *R = new BitcodeReader(std::move(*StreamOrErr), Context); BitcodeReader *R = new BitcodeReader(std::move(*StreamOrErr), Context);
@ -6587,28 +6587,28 @@ getLazyBitcodeModuleImpl(MemoryBufferRef Buffer, LLVMContext &Context,
// Delay parsing Metadata if ShouldLazyLoadMetadata is true. // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
if (Error Err = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata)) if (Error Err = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata))
return errorToErrorCodeAndEmitErrors(Context, std::move(Err)); return std::move(Err);
if (MaterializeAll) { if (MaterializeAll) {
// Read in the entire module, and destroy the BitcodeReader. // Read in the entire module, and destroy the BitcodeReader.
if (Error Err = M->materializeAll()) if (Error Err = M->materializeAll())
return errorToErrorCodeAndEmitErrors(Context, std::move(Err)); return std::move(Err);
} else { } else {
// Resolve forward references from blockaddresses. // Resolve forward references from blockaddresses.
if (Error Err = R->materializeForwardReferencedFunctions()) if (Error Err = R->materializeForwardReferencedFunctions())
return errorToErrorCodeAndEmitErrors(Context, std::move(Err)); return std::move(Err);
} }
return std::move(M); return std::move(M);
} }
ErrorOr<std::unique_ptr<Module>> Expected<std::unique_ptr<Module>>
llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, llvm::getLazyBitcodeModule(MemoryBufferRef Buffer,
LLVMContext &Context, bool ShouldLazyLoadMetadata) { LLVMContext &Context, bool ShouldLazyLoadMetadata) {
return getLazyBitcodeModuleImpl(Buffer, Context, false, return getLazyBitcodeModuleImpl(Buffer, Context, false,
ShouldLazyLoadMetadata); ShouldLazyLoadMetadata);
} }
ErrorOr<std::unique_ptr<Module>> Expected<std::unique_ptr<Module>>
llvm::getOwningLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, llvm::getOwningLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
LLVMContext &Context, LLVMContext &Context,
bool ShouldLazyLoadMetadata) { bool ShouldLazyLoadMetadata) {
@ -6618,7 +6618,7 @@ llvm::getOwningLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
return MOrErr; return MOrErr;
} }
ErrorOr<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer, Expected<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
LLVMContext &Context) { LLVMContext &Context) {
return getLazyBitcodeModuleImpl(Buffer, Context, true); return getLazyBitcodeModuleImpl(Buffer, Context, true);
// TODO: Restore the use-lists to the in-memory state when the bitcode was // TODO: Restore the use-lists to the in-memory state when the bitcode was

View File

@ -79,7 +79,7 @@ std::unique_ptr<Module> llvm::splitCodeGen(
CodegenThreadPool.async( CodegenThreadPool.async(
[TMFactory, FileType, ThreadOS](const SmallString<0> &BC) { [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) {
LLVMContext Ctx; LLVMContext Ctx;
ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile( Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
MemoryBufferRef(StringRef(BC.data(), BC.size()), MemoryBufferRef(StringRef(BC.data(), BC.size()),
"<split-module>"), "<split-module>"),
Ctx); Ctx);

View File

@ -34,11 +34,13 @@ getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
LLVMContext &Context, bool ShouldLazyLoadMetadata) { LLVMContext &Context, bool ShouldLazyLoadMetadata) {
if (isBitcode((const unsigned char *)Buffer->getBufferStart(), if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
(const unsigned char *)Buffer->getBufferEnd())) { (const unsigned char *)Buffer->getBufferEnd())) {
ErrorOr<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule( Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
std::move(Buffer), Context, ShouldLazyLoadMetadata); std::move(Buffer), Context, ShouldLazyLoadMetadata);
if (std::error_code EC = ModuleOrErr.getError()) { if (Error E = ModuleOrErr.takeError()) {
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
EC.message()); EIB.message());
});
return nullptr; return nullptr;
} }
return std::move(ModuleOrErr.get()); return std::move(ModuleOrErr.get());
@ -69,11 +71,13 @@ std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
TimePassesIsEnabled); TimePassesIsEnabled);
if (isBitcode((const unsigned char *)Buffer.getBufferStart(), if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
(const unsigned char *)Buffer.getBufferEnd())) { (const unsigned char *)Buffer.getBufferEnd())) {
ErrorOr<std::unique_ptr<Module>> ModuleOrErr = Expected<std::unique_ptr<Module>> ModuleOrErr =
parseBitcodeFile(Buffer, Context); parseBitcodeFile(Buffer, Context);
if (std::error_code EC = ModuleOrErr.getError()) { if (Error E = ModuleOrErr.takeError()) {
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error, Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
EC.message()); EIB.message());
});
return nullptr; return nullptr;
} }
return std::move(ModuleOrErr.get()); return std::move(ModuleOrErr.get());

View File

@ -104,17 +104,16 @@ std::unique_ptr<Module>
llvm::loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context, llvm::loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
bool Lazy) { bool Lazy) {
SMDiagnostic Err; SMDiagnostic Err;
ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr); Expected<std::unique_ptr<Module>> ModuleOrErr =
if (Lazy) { Lazy ? getLazyBitcodeModule(Buffer, Context,
ModuleOrErr = getLazyBitcodeModule(Buffer, Context, /* ShouldLazyLoadMetadata */ true)
/* ShouldLazyLoadMetadata */ Lazy); : parseBitcodeFile(Buffer, Context);
} else { if (!ModuleOrErr) {
ModuleOrErr = parseBitcodeFile(Buffer, Context); handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
} SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(),
if (std::error_code EC = ModuleOrErr.getError()) { SourceMgr::DK_Error, EIB.message());
Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
EC.message());
Err.print("ThinLTO", errs()); Err.print("ThinLTO", errs());
});
report_fatal_error("Can't load module, abort."); report_fatal_error("Can't load module, abort.");
} }
return std::move(ModuleOrErr.get()); return std::move(ModuleOrErr.get());
@ -204,25 +203,13 @@ void llvm::thinLTOInternalizeAndPromoteInIndex(
Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) { Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) {
std::unique_ptr<InputFile> File(new InputFile); std::unique_ptr<InputFile> File(new InputFile);
std::string Msg;
auto DiagHandler = [](const DiagnosticInfo &DI, void *MsgP) {
auto *Msg = reinterpret_cast<std::string *>(MsgP);
raw_string_ostream OS(*Msg);
DiagnosticPrinterRawOStream DP(OS);
DI.print(DP);
};
File->Ctx.setDiagnosticHandler(DiagHandler, static_cast<void *>(&Msg));
ErrorOr<std::unique_ptr<object::IRObjectFile>> IRObj = Expected<std::unique_ptr<object::IRObjectFile>> IRObj =
IRObjectFile::create(Object, File->Ctx); IRObjectFile::create(Object, File->Ctx);
if (!Msg.empty())
return make_error<StringError>(Msg, inconvertibleErrorCode());
if (!IRObj) if (!IRObj)
return errorCodeToError(IRObj.getError()); return IRObj.takeError();
File->Obj = std::move(*IRObj); File->Obj = std::move(*IRObj);
File->Ctx.setDiagnosticHandler(nullptr, nullptr);
for (const auto &C : File->Obj->getModule().getComdatSymbolTable()) { for (const auto &C : File->Obj->getModule().getComdatSymbolTable()) {
auto P = auto P =
File->ComdatMap.insert(std::make_pair(&C.second, File->Comdats.size())); File->ComdatMap.insert(std::make_pair(&C.second, File->Comdats.size()));
@ -346,10 +333,10 @@ Error LTO::addRegularLTO(std::unique_ptr<InputFile> Input,
llvm::make_unique<Module>("ld-temp.o", RegularLTO.Ctx); llvm::make_unique<Module>("ld-temp.o", RegularLTO.Ctx);
RegularLTO.Mover = llvm::make_unique<IRMover>(*RegularLTO.CombinedModule); RegularLTO.Mover = llvm::make_unique<IRMover>(*RegularLTO.CombinedModule);
} }
ErrorOr<std::unique_ptr<object::IRObjectFile>> ObjOrErr = Expected<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
IRObjectFile::create(Input->Obj->getMemoryBufferRef(), RegularLTO.Ctx); IRObjectFile::create(Input->Obj->getMemoryBufferRef(), RegularLTO.Ctx);
if (!ObjOrErr) if (!ObjOrErr)
return errorCodeToError(ObjOrErr.getError()); return ObjOrErr.takeError();
std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr); std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
Module &M = Obj->getModule(); Module &M = Obj->getModule();
@ -571,9 +558,10 @@ public:
MapVector<StringRef, MemoryBufferRef> &ModuleMap) { MapVector<StringRef, MemoryBufferRef> &ModuleMap) {
auto RunThinBackend = [&](AddStreamFn AddStream) { auto RunThinBackend = [&](AddStreamFn AddStream) {
LTOLLVMContext BackendContext(Conf); LTOLLVMContext BackendContext(Conf);
ErrorOr<std::unique_ptr<Module>> MOrErr = Expected<std::unique_ptr<Module>> MOrErr =
parseBitcodeFile(MBRef, BackendContext); parseBitcodeFile(MBRef, BackendContext);
assert(MOrErr && "Unable to load module in thread?"); if (!MOrErr)
return MOrErr.takeError();
return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex, return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
ImportList, DefinedGlobals, ModuleMap); ImportList, DefinedGlobals, ModuleMap);

View File

@ -237,7 +237,7 @@ void splitCodeGen(Config &C, TargetMachine *TM, AddStreamFn AddStream,
CodegenThreadPool.async( CodegenThreadPool.async(
[&](const SmallString<0> &BC, unsigned ThreadId) { [&](const SmallString<0> &BC, unsigned ThreadId) {
LTOLLVMContext Ctx(C); LTOLLVMContext Ctx(C);
ErrorOr<std::unique_ptr<Module>> MOrErr = parseBitcodeFile( Expected<std::unique_ptr<Module>> MOrErr = parseBitcodeFile(
MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"), MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"),
Ctx); Ctx);
if (!MOrErr) if (!MOrErr)
@ -353,10 +353,8 @@ Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
auto ModuleLoader = [&](StringRef Identifier) { auto ModuleLoader = [&](StringRef Identifier) {
assert(Mod.getContext().isODRUniquingDebugTypes() && assert(Mod.getContext().isODRUniquingDebugTypes() &&
"ODR Type uniquing should be enabled on the context"); "ODR Type uniquing should be enabled on the context");
return std::move(getLazyBitcodeModule(ModuleMap[Identifier], return getLazyBitcodeModule(ModuleMap[Identifier], Mod.getContext(),
Mod.getContext(), /*ShouldLazyLoadMetadata=*/true);
/*ShouldLazyLoadMetadata=*/true)
.get());
}; };
FunctionImporter Importer(CombinedIndex, ModuleLoader); FunctionImporter Importer(CombinedIndex, ModuleLoader);

View File

@ -184,18 +184,14 @@ parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context,
if (!ShouldBeLazy) { if (!ShouldBeLazy) {
// Parse the full file. // Parse the full file.
ErrorOr<std::unique_ptr<Module>> M = parseBitcodeFile(*MBOrErr, Context); return expectedToErrorOrAndEmitErrors(Context,
if (std::error_code EC = M.getError()) parseBitcodeFile(*MBOrErr, Context));
return EC;
return std::move(*M);
} }
// Parse lazily. // Parse lazily.
ErrorOr<std::unique_ptr<Module>> M = return expectedToErrorOrAndEmitErrors(
getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/); Context,
if (std::error_code EC = M.getError()) getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/));
return EC;
return std::move(*M);
} }
ErrorOr<std::unique_ptr<LTOModule>> ErrorOr<std::unique_ptr<LTOModule>>

View File

@ -310,18 +310,18 @@ ErrorOr<MemoryBufferRef> IRObjectFile::findBitcodeInMemBuffer(MemoryBufferRef Ob
} }
} }
ErrorOr<std::unique_ptr<IRObjectFile>> Expected<std::unique_ptr<IRObjectFile>>
llvm::object::IRObjectFile::create(MemoryBufferRef Object, llvm::object::IRObjectFile::create(MemoryBufferRef Object,
LLVMContext &Context) { LLVMContext &Context) {
ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object); ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
if (!BCOrErr) if (!BCOrErr)
return BCOrErr.getError(); return errorCodeToError(BCOrErr.getError());
ErrorOr<std::unique_ptr<Module>> MOrErr = Expected<std::unique_ptr<Module>> MOrErr =
getLazyBitcodeModule(*BCOrErr, Context, getLazyBitcodeModule(*BCOrErr, Context,
/*ShouldLazyLoadMetadata*/ true); /*ShouldLazyLoadMetadata*/ true);
if (std::error_code EC = MOrErr.getError()) if (!MOrErr)
return EC; return MOrErr.takeError();
std::unique_ptr<Module> &M = MOrErr.get(); std::unique_ptr<Module> &M = MOrErr.get();
return llvm::make_unique<IRObjectFile>(BCOrErr.get(), std::move(M)); return llvm::make_unique<IRObjectFile>(BCOrErr.get(), std::move(M));

View File

@ -35,7 +35,7 @@ Expected<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
switch (Type) { switch (Type) {
case sys::fs::file_magic::bitcode: case sys::fs::file_magic::bitcode:
if (Context) if (Context)
return errorOrToExpected(IRObjectFile::create(Object, *Context)); return IRObjectFile::create(Object, *Context);
LLVM_FALLTHROUGH; LLVM_FALLTHROUGH;
case sys::fs::file_magic::unknown: case sys::fs::file_magic::unknown:
case sys::fs::file_magic::archive: case sys::fs::file_magic::archive:
@ -73,9 +73,9 @@ Expected<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
if (!BCData) if (!BCData)
return std::move(Obj); return std::move(Obj);
return errorOrToExpected(IRObjectFile::create( return IRObjectFile::create(
MemoryBufferRef(BCData->getBuffer(), MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
Object.getBufferIdentifier()), *Context)); *Context);
} }
} }
llvm_unreachable("Unexpected Binary File Type"); llvm_unreachable("Unexpected Binary File Type");

View File

@ -624,7 +624,10 @@ Expected<bool> FunctionImporter::importFunctions(
// Get the module for the import // Get the module for the import
const auto &FunctionsToImportPerModule = ImportList.find(Name); const auto &FunctionsToImportPerModule = ImportList.find(Name);
assert(FunctionsToImportPerModule != ImportList.end()); assert(FunctionsToImportPerModule != ImportList.end());
std::unique_ptr<Module> SrcModule = ModuleLoader(Name); Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
if (!SrcModuleOrErr)
return SrcModuleOrErr.takeError();
std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
assert(&DestModule.getContext() == &SrcModule->getContext() && assert(&DestModule.getContext() == &SrcModule->getContext() &&
"Context mismatch"); "Context mismatch");

View File

@ -142,9 +142,9 @@ static ExitOnError ExitOnErr;
static std::unique_ptr<Module> openInputFile(LLVMContext &Context) { static std::unique_ptr<Module> openInputFile(LLVMContext &Context) {
std::unique_ptr<MemoryBuffer> MB = std::unique_ptr<MemoryBuffer> MB =
ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename))); ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename)));
std::unique_ptr<Module> M = ExitOnErr(errorOrToExpected( std::unique_ptr<Module> M =
getOwningLazyBitcodeModule(std::move(MB), Context, ExitOnErr(getOwningLazyBitcodeModule(std::move(MB), Context,
/*ShouldLazyLoadMetadata=*/true))); /*ShouldLazyLoadMetadata=*/true));
if (MaterializeMetadata) if (MaterializeMetadata)
ExitOnErr(M->materializeMetadata()); ExitOnErr(M->materializeMetadata());
else else

View File

@ -160,11 +160,11 @@ std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
} }
MemoryBuffer *Buffer = BufferOr.get().get(); MemoryBuffer *Buffer = BufferOr.get().get();
ErrorOr<std::unique_ptr<Module>> ModuleOr = Expected<std::unique_ptr<Module>> ModuleOr =
parseBitcodeFile(Buffer->getMemBufferRef(), Context); parseBitcodeFile(Buffer->getMemBufferRef(), Context);
if (!ModuleOr) { if (!ModuleOr) {
errs() << "verify-uselistorder: error: " << ModuleOr.getError().message() logAllUnhandledErrors(ModuleOr.takeError(), errs(),
<< "\n"; "verify-uselistorder: error: ");
return nullptr; return nullptr;
} }
return std::move(ModuleOr.get()); return std::move(ModuleOr.get());

View File

@ -55,8 +55,10 @@ static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
SmallString<1024> &Mem, SmallString<1024> &Mem,
const char *Assembly) { const char *Assembly) {
writeModuleToBuffer(parseAssembly(Context, Assembly), Mem); writeModuleToBuffer(parseAssembly(Context, Assembly), Mem);
ErrorOr<std::unique_ptr<Module>> ModuleOrErr = Expected<std::unique_ptr<Module>> ModuleOrErr =
getLazyBitcodeModule(MemoryBufferRef(Mem.str(), "test"), Context); getLazyBitcodeModule(MemoryBufferRef(Mem.str(), "test"), Context);
if (!ModuleOrErr)
report_fatal_error("Could not parse bitcode module");
return std::move(ModuleOrErr.get()); return std::move(ModuleOrErr.get());
} }