Skip to content

Commit

Permalink
Bitcode: Change the materializer interface to return llvm::Error.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D26439

llvm-svn: 286382
  • Loading branch information
pcc committed Nov 9, 2016
1 parent 9934c54 commit 7f00d0a
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 103 deletions.
8 changes: 4 additions & 4 deletions llvm/include/llvm/IR/GVMaterializer.h
Expand Up @@ -18,10 +18,10 @@
#ifndef LLVM_IR_GVMATERIALIZER_H
#define LLVM_IR_GVMATERIALIZER_H

#include <system_error>
#include <vector>

namespace llvm {
class Error;
class Function;
class GlobalValue;
class Module;
Expand All @@ -36,13 +36,13 @@ class GVMaterializer {

/// Make sure the given GlobalValue is fully read.
///
virtual std::error_code materialize(GlobalValue *GV) = 0;
virtual Error materialize(GlobalValue *GV) = 0;

/// Make sure the entire Module has been completely read.
///
virtual std::error_code materializeModule() = 0;
virtual Error materializeModule() = 0;

virtual std::error_code materializeMetadata() = 0;
virtual Error materializeMetadata() = 0;
virtual void setStripDebugInfo() = 0;

virtual std::vector<StructType *> getIdentifiedStructTypes() const = 0;
Expand Down
8 changes: 3 additions & 5 deletions llvm/include/llvm/IR/GlobalValue.h
Expand Up @@ -21,11 +21,11 @@
#include "llvm/IR/Constant.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/Support/MD5.h"
#include <system_error>

namespace llvm {

class Comdat;
class Error;
class GlobalObject;
class PointerType;
class Module;
Expand Down Expand Up @@ -467,10 +467,8 @@ class GlobalValue : public Constant {
/// function has been read in yet or not.
bool isMaterializable() const;

/// Make sure this GlobalValue is fully read. If the module is corrupt, this
/// returns true and fills in the optional string with information about the
/// problem. If successful, this returns false.
std::error_code materialize();
/// Make sure this GlobalValue is fully read.
Error materialize();

/// @}

Expand Down
12 changes: 5 additions & 7 deletions llvm/include/llvm/IR/Module.h
Expand Up @@ -26,10 +26,10 @@
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/DataTypes.h"
#include <system_error>

namespace llvm {
template <typename T> class Optional;
class Error;
class FunctionType;
class GVMaterializer;
class LLVMContext;
Expand Down Expand Up @@ -454,16 +454,14 @@ class Module {
GVMaterializer *getMaterializer() const { return Materializer.get(); }
bool isMaterialized() const { return !getMaterializer(); }

/// Make sure the GlobalValue is fully read. If the module is corrupt, this
/// returns true and fills in the optional string with information about the
/// problem. If successful, this returns false.
std::error_code materialize(GlobalValue *GV);
/// Make sure the GlobalValue is fully read.
llvm::Error materialize(GlobalValue *GV);

/// Make sure all GlobalValues in this Module are fully read and clear the
/// Materializer.
std::error_code materializeAll();
llvm::Error materializeAll();

std::error_code materializeMetadata();
llvm::Error materializeMetadata();

/// @}
/// @name Direct access to the globals list, functions list, and symbol table
Expand Down
6 changes: 4 additions & 2 deletions llvm/include/llvm/Transforms/IPO/FunctionImport.h
Expand Up @@ -14,6 +14,7 @@
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/ModuleSummaryIndex.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/Error.h"

#include <functional>
#include <map>
Expand Down Expand Up @@ -53,8 +54,9 @@ class FunctionImporter {
/// \p ForceImportReferencedDiscardableSymbols will set the ModuleLinker in
/// a mode where referenced discarable symbols in the source modules will be
/// imported as well even if they are not present in the ImportList.
bool importFunctions(Module &M, const ImportMapTy &ImportList,
bool ForceImportReferencedDiscardableSymbols = false);
Expected<bool>
importFunctions(Module &M, const ImportMapTy &ImportList,
bool ForceImportReferencedDiscardableSymbols = false);

private:
/// The summaries index used to trigger importing.
Expand Down
39 changes: 12 additions & 27 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Expand Up @@ -370,10 +370,8 @@ class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {

Error materializeForwardReferencedFunctions();

std::error_code materialize(GlobalValue *GV) override;
Error materializeImpl(GlobalValue *GV);
std::error_code materializeModule() override;
Error materializeModuleImpl();
Error materialize(GlobalValue *GV) override;
Error materializeModule() override;
std::vector<StructType *> getIdentifiedStructTypes() const override;

/// \brief Main interface to parsing a bitcode buffer.
Expand All @@ -394,8 +392,7 @@ class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
static uint64_t decodeSignRotatedValue(uint64_t V);

/// Materialize any deferred Metadata block.
std::error_code materializeMetadata() override;
Error materializeMetadataImpl();
Error materializeMetadata() override;

void setStripDebugInfo() override;

Expand Down Expand Up @@ -677,7 +674,7 @@ Error BitcodeReader::materializeForwardReferencedFunctions() {
return error("Never resolved function from blockaddress");

// Try to materialize F.
if (Error Err = materializeImpl(F))
if (Error Err = materialize(F))
return Err;
}
assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
Expand Down Expand Up @@ -3580,11 +3577,7 @@ Error BitcodeReader::rememberAndSkipMetadata() {
return Error::success();
}

std::error_code BitcodeReader::materializeMetadata() {
return errorToErrorCodeAndEmitErrors(Context, materializeMetadataImpl());
}

Error BitcodeReader::materializeMetadataImpl() {
Error BitcodeReader::materializeMetadata() {
for (uint64_t BitPos : DeferredMetadataInfo) {
// Move the bit stream to the saved position.
Stream.JumpToBit(BitPos);
Expand Down Expand Up @@ -5856,11 +5849,7 @@ Error BitcodeReader::findFunctionInStream(
// GVMaterializer implementation
//===----------------------------------------------------------------------===//

std::error_code BitcodeReader::materialize(GlobalValue *GV) {
return errorToErrorCodeAndEmitErrors(Context, materializeImpl(GV));
}

Error BitcodeReader::materializeImpl(GlobalValue *GV) {
Error BitcodeReader::materialize(GlobalValue *GV) {
Function *F = dyn_cast<Function>(GV);
// If it's not a function or is already material, ignore the request.
if (!F || !F->isMaterializable())
Expand All @@ -5875,7 +5864,7 @@ Error BitcodeReader::materializeImpl(GlobalValue *GV) {
return Err;

// Materialize metadata before parsing any function bodies.
if (Error Err = materializeMetadataImpl())
if (Error Err = materializeMetadata())
return Err;

// Move the bit stream to the saved position of the deferred function body.
Expand Down Expand Up @@ -5915,12 +5904,8 @@ Error BitcodeReader::materializeImpl(GlobalValue *GV) {
return materializeForwardReferencedFunctions();
}

std::error_code BitcodeReader::materializeModule() {
return errorToErrorCodeAndEmitErrors(Context, materializeModuleImpl());
}

Error BitcodeReader::materializeModuleImpl() {
if (Error Err = materializeMetadataImpl())
Error BitcodeReader::materializeModule() {
if (Error Err = materializeMetadata())
return Err;

// Promise to materialize all forward references.
Expand All @@ -5929,7 +5914,7 @@ Error BitcodeReader::materializeModuleImpl() {
// Iterate over the module, deserializing any functions that are still on
// disk.
for (Function &F : *TheModule) {
if (Error Err = materializeImpl(&F))
if (Error Err = materialize(&F))
return Err;
}
// At this point, if there are any function bodies, parse the rest of
Expand Down Expand Up @@ -6664,8 +6649,8 @@ getLazyBitcodeModuleImpl(MemoryBufferRef Buffer, LLVMContext &Context,

if (MaterializeAll) {
// Read in the entire module, and destroy the BitcodeReader.
if (std::error_code EC = M->materializeAll())
return EC;
if (Error Err = M->materializeAll())
return errorToErrorCodeAndEmitErrors(Context, std::move(Err));
} else {
// Resolve forward references from blockaddresses.
if (Error Err = R->materializeForwardReferencedFunctions())
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/ExecutionEngine/Interpreter/Interpreter.cpp
Expand Up @@ -35,9 +35,13 @@ extern "C" void LLVMLinkInInterpreter() { }
ExecutionEngine *Interpreter::create(std::unique_ptr<Module> M,
std::string *ErrStr) {
// Tell this Module to materialize everything and release the GVMaterializer.
if (std::error_code EC = M->materializeAll()) {
if (Error Err = M->materializeAll()) {
std::string Msg;
handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
Msg = EIB.message();
});
if (ErrStr)
*ErrStr = EC.message();
*ErrStr = Msg;
// We got an error, just return 0
return nullptr;
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/IR/Globals.cpp
Expand Up @@ -21,6 +21,7 @@
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
using namespace llvm;

Expand All @@ -33,7 +34,7 @@ bool GlobalValue::isMaterializable() const {
return F->isMaterializable();
return false;
}
std::error_code GlobalValue::materialize() {
Error GlobalValue::materialize() {
return getParent()->materialize(this);
}

Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/IR/LegacyPassManager.cpp
Expand Up @@ -20,6 +20,7 @@
#include "llvm/Support/Chrono.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
Expand Down Expand Up @@ -1377,8 +1378,9 @@ void FunctionPassManager::add(Pass *P) {
/// so, return true.
///
bool FunctionPassManager::run(Function &F) {
if (std::error_code EC = F.materialize())
report_fatal_error("Error reading bitcode file: " + EC.message());
handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
report_fatal_error("Error reading bitcode file: " + EIB.message());
});
return FPM->run(F);
}

Expand Down
13 changes: 7 additions & 6 deletions llvm/lib/IR/Module.cpp
Expand Up @@ -25,6 +25,7 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/TypeFinder.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/RandomNumberGenerator.h"
Expand Down Expand Up @@ -405,23 +406,23 @@ void Module::setMaterializer(GVMaterializer *GVM) {
Materializer.reset(GVM);
}

std::error_code Module::materialize(GlobalValue *GV) {
Error Module::materialize(GlobalValue *GV) {
if (!Materializer)
return std::error_code();
return Error::success();

return Materializer->materialize(GV);
}

std::error_code Module::materializeAll() {
Error Module::materializeAll() {
if (!Materializer)
return std::error_code();
return Error::success();
std::unique_ptr<GVMaterializer> M = std::move(Materializer);
return M->materializeModule();
}

std::error_code Module::materializeMetadata() {
Error Module::materializeMetadata() {
if (!Materializer)
return std::error_code();
return Error::success();
return Materializer->materializeMetadata();
}

Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/LTO/LTO.cpp
Expand Up @@ -350,7 +350,8 @@ Error LTO::addRegularLTO(std::unique_ptr<InputFile> Input,
std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);

Module &M = Obj->getModule();
M.materializeMetadata();
if (Error Err = M.materializeMetadata())
return Err;
UpgradeDebugInfo(M);

SmallPtrSet<GlobalValue *, 8> Used;
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/LTO/LTOBackend.cpp
Expand Up @@ -359,7 +359,8 @@ Error lto::thinBackend(Config &Conf, unsigned Task, AddStreamFn AddStream,
};

FunctionImporter Importer(CombinedIndex, ModuleLoader);
Importer.importFunctions(Mod, ImportList);
if (Error Err = Importer.importFunctions(Mod, ImportList).takeError())
return Err;

if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))
return Error();
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/LTO/ThinLTOCodeGenerator.cpp
Expand Up @@ -152,7 +152,8 @@ crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
const FunctionImporter::ImportMapTy &ImportList) {
ModuleLoader Loader(TheModule.getContext(), ModuleMap);
FunctionImporter Importer(Index, Loader);
Importer.importFunctions(TheModule, ImportList);
if (!Importer.importFunctions(TheModule, ImportList))
report_fatal_error("importFunctions failed");
}

static void optimizeModule(Module &TheModule, TargetMachine &TM) {
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Linker/IRMover.cpp
Expand Up @@ -963,8 +963,8 @@ Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
assert(Dst.isDeclaration() && !Src.isDeclaration());

// Materialize if needed.
if (std::error_code EC = Src.materialize())
return errorCodeToError(EC);
if (Error Err = Src.materialize())
return Err;

// Link in the operands without remapping.
if (Src.hasPrefixData())
Expand Down Expand Up @@ -1191,8 +1191,8 @@ static std::string mergeTriples(const Triple &SrcTriple,
Error IRLinker::run() {
// Ensure metadata materialized before value mapping.
if (SrcM->getMaterializer())
if (std::error_code EC = SrcM->getMaterializer()->materializeMetadata())
return errorCodeToError(EC);
if (Error Err = SrcM->getMaterializer()->materializeMetadata())
return Err;

// Inherit the target data from the source module if the destination module
// doesn't have one already.
Expand Down

0 comments on commit 7f00d0a

Please sign in to comment.