2017-06-20 07:37:52 +08:00
|
|
|
//===- OrcCBindingsStack.h - Orc JIT stack for C bindings -----*- C++ -*---===//
|
2015-10-28 10:40:04 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCCBINDINGSSTACK_H
|
|
|
|
#define LLVM_LIB_EXECUTIONENGINE_ORC_ORCCBINDINGSSTACK_H
|
|
|
|
|
2016-04-26 05:21:20 +08:00
|
|
|
#include "llvm-c/OrcBindings.h"
|
2017-06-20 07:37:52 +08:00
|
|
|
#include "llvm-c/TargetMachine.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ExecutionEngine/JITSymbol.h"
|
2015-10-28 10:40:04 +08:00
|
|
|
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
|
2017-06-20 07:37:52 +08:00
|
|
|
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
|
2017-02-20 13:45:14 +08:00
|
|
|
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
|
2017-06-20 07:37:52 +08:00
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
|
|
|
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Mangler.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Support/CBindingWrapping.h"
|
2016-04-26 03:56:45 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2017-06-20 07:37:52 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
2018-02-09 10:30:40 +08:00
|
|
|
#include <map>
|
2017-06-20 07:37:52 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-10-28 10:40:04 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2015-10-30 11:20:21 +08:00
|
|
|
class OrcCBindingsStack;
|
2015-10-28 10:40:04 +08:00
|
|
|
|
2015-10-30 11:20:21 +08:00
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcCBindingsStack, LLVMOrcJITStackRef)
|
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
|
2015-10-28 10:40:04 +08:00
|
|
|
|
2017-09-17 11:25:03 +08:00
|
|
|
namespace detail {
|
2017-06-23 05:06:54 +08:00
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
// FIXME: Kill this off once the Layer concept becomes an interface.
|
|
|
|
class GenericLayer {
|
|
|
|
public:
|
|
|
|
virtual ~GenericLayer() = default;
|
2015-10-28 10:40:04 +08:00
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
virtual JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name,
|
|
|
|
bool ExportedSymbolsOnly) = 0;
|
|
|
|
virtual Error removeModule(orc::VModuleKey K) = 0;
|
2015-10-28 10:40:04 +08:00
|
|
|
};
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
template <typename LayerT> class GenericLayerImpl : public GenericLayer {
|
2015-10-28 10:40:04 +08:00
|
|
|
public:
|
2018-02-09 10:30:40 +08:00
|
|
|
GenericLayerImpl(LayerT &Layer) : Layer(Layer) {}
|
2015-10-28 10:40:04 +08:00
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name,
|
2016-08-02 04:49:11 +08:00
|
|
|
bool ExportedSymbolsOnly) override {
|
2018-02-09 10:30:40 +08:00
|
|
|
return Layer.findSymbolIn(K, Name, ExportedSymbolsOnly);
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
Error removeModule(orc::VModuleKey K) override {
|
|
|
|
return Layer.removeModule(K);
|
2018-02-07 05:25:11 +08:00
|
|
|
}
|
2015-10-28 10:40:04 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
LayerT &Layer;
|
|
|
|
};
|
|
|
|
|
2017-09-17 11:25:03 +08:00
|
|
|
template <>
|
2018-02-09 10:30:40 +08:00
|
|
|
class GenericLayerImpl<orc::RTDyldObjectLinkingLayer> : public GenericLayer {
|
2017-09-17 11:25:03 +08:00
|
|
|
private:
|
|
|
|
using LayerT = orc::RTDyldObjectLinkingLayer;
|
|
|
|
public:
|
2018-02-09 10:30:40 +08:00
|
|
|
GenericLayerImpl(LayerT &Layer) : Layer(Layer) {}
|
2017-09-17 11:25:03 +08:00
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name,
|
2017-09-17 11:25:03 +08:00
|
|
|
bool ExportedSymbolsOnly) override {
|
2018-02-09 10:30:40 +08:00
|
|
|
return Layer.findSymbolIn(K, Name, ExportedSymbolsOnly);
|
2017-09-17 11:25:03 +08:00
|
|
|
}
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
Error removeModule(orc::VModuleKey K) override {
|
|
|
|
return Layer.removeObject(K);
|
2018-02-07 05:25:11 +08:00
|
|
|
}
|
2017-09-17 11:25:03 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
LayerT &Layer;
|
|
|
|
};
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
template <typename LayerT>
|
|
|
|
std::unique_ptr<GenericLayerImpl<LayerT>> createGenericLayer(LayerT &Layer) {
|
|
|
|
return llvm::make_unique<GenericLayerImpl<LayerT>>(Layer);
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
2017-09-17 11:25:03 +08:00
|
|
|
} // end namespace detail
|
|
|
|
|
|
|
|
class OrcCBindingsStack {
|
|
|
|
public:
|
|
|
|
|
|
|
|
using CompileCallbackMgr = orc::JITCompileCallbackManager;
|
|
|
|
using ObjLayerT = orc::RTDyldObjectLinkingLayer;
|
|
|
|
using CompileLayerT = orc::IRCompileLayer<ObjLayerT, orc::SimpleCompiler>;
|
|
|
|
using CODLayerT =
|
|
|
|
orc::CompileOnDemandLayer<CompileLayerT, CompileCallbackMgr>;
|
|
|
|
|
|
|
|
using CallbackManagerBuilder =
|
|
|
|
std::function<std::unique_ptr<CompileCallbackMgr>()>;
|
|
|
|
|
|
|
|
using IndirectStubsManagerBuilder = CODLayerT::IndirectStubsManagerBuilderT;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
using OwningObject = object::OwningBinary<object::ObjectFile>;
|
|
|
|
|
2018-02-07 05:25:11 +08:00
|
|
|
class CBindingsResolver : public orc::SymbolResolver {
|
|
|
|
public:
|
|
|
|
CBindingsResolver(OrcCBindingsStack &Stack,
|
|
|
|
LLVMOrcSymbolResolverFn ExternalResolver,
|
|
|
|
void *ExternalResolverCtx)
|
|
|
|
: Stack(Stack), ExternalResolver(std::move(ExternalResolver)),
|
|
|
|
ExternalResolverCtx(std::move(ExternalResolverCtx)) {}
|
|
|
|
|
|
|
|
orc::SymbolNameSet lookupFlags(orc::SymbolFlagsMap &SymbolFlags,
|
|
|
|
const orc::SymbolNameSet &Symbols) override {
|
|
|
|
orc::SymbolNameSet SymbolsNotFound;
|
|
|
|
|
|
|
|
for (auto &S : Symbols) {
|
|
|
|
if (auto Sym = findSymbol(*S))
|
|
|
|
SymbolFlags[S] = Sym.getFlags();
|
|
|
|
else if (auto Err = Sym.takeError()) {
|
|
|
|
Stack.reportError(std::move(Err));
|
2018-02-07 06:17:09 +08:00
|
|
|
return orc::SymbolNameSet();
|
2018-02-07 05:25:11 +08:00
|
|
|
} else
|
|
|
|
SymbolsNotFound.insert(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SymbolsNotFound;
|
|
|
|
}
|
|
|
|
|
2018-02-15 06:12:56 +08:00
|
|
|
orc::SymbolNameSet
|
|
|
|
lookup(std::shared_ptr<orc::AsynchronousSymbolQuery> Query,
|
|
|
|
orc::SymbolNameSet Symbols) override {
|
2018-02-07 05:25:11 +08:00
|
|
|
orc::SymbolNameSet UnresolvedSymbols;
|
|
|
|
|
|
|
|
for (auto &S : Symbols) {
|
|
|
|
if (auto Sym = findSymbol(*S)) {
|
|
|
|
if (auto Addr = Sym.getAddress())
|
2018-04-13 02:35:08 +08:00
|
|
|
Query->resolve(S, JITEvaluatedSymbol(*Addr, Sym.getFlags()));
|
2018-02-07 05:25:11 +08:00
|
|
|
else {
|
2018-05-17 06:24:30 +08:00
|
|
|
Stack.ES.failQuery(*Query, Addr.takeError());
|
2018-02-07 06:17:09 +08:00
|
|
|
return orc::SymbolNameSet();
|
2018-02-07 05:25:11 +08:00
|
|
|
}
|
|
|
|
} else if (auto Err = Sym.takeError()) {
|
2018-05-17 06:24:30 +08:00
|
|
|
Stack.ES.failQuery(*Query, std::move(Err));
|
2018-02-07 06:17:09 +08:00
|
|
|
return orc::SymbolNameSet();
|
2018-02-07 05:25:11 +08:00
|
|
|
} else
|
|
|
|
UnresolvedSymbols.insert(S);
|
|
|
|
}
|
|
|
|
|
2018-05-17 06:24:30 +08:00
|
|
|
if (Query->isFullyResolved())
|
|
|
|
Query->handleFullyResolved();
|
|
|
|
|
2018-02-07 05:25:11 +08:00
|
|
|
return UnresolvedSymbols;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JITSymbol findSymbol(const std::string &Name) {
|
|
|
|
// Search order:
|
|
|
|
// 1. JIT'd symbols.
|
|
|
|
// 2. Runtime overrides.
|
|
|
|
// 3. External resolver (if present).
|
|
|
|
|
|
|
|
if (auto Sym = Stack.CODLayer.findSymbol(Name, true))
|
|
|
|
return Sym;
|
|
|
|
else if (auto Err = Sym.takeError())
|
|
|
|
return Sym.takeError();
|
|
|
|
|
|
|
|
if (auto Sym = Stack.CXXRuntimeOverrides.searchOverrides(Name))
|
|
|
|
return Sym;
|
|
|
|
|
|
|
|
if (ExternalResolver)
|
|
|
|
return JITSymbol(ExternalResolver(Name.c_str(), ExternalResolverCtx),
|
|
|
|
JITSymbolFlags::Exported);
|
|
|
|
|
|
|
|
return JITSymbol(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
OrcCBindingsStack &Stack;
|
|
|
|
LLVMOrcSymbolResolverFn ExternalResolver;
|
|
|
|
void *ExternalResolverCtx = nullptr;
|
|
|
|
};
|
|
|
|
|
2015-10-28 10:40:04 +08:00
|
|
|
public:
|
|
|
|
|
2015-11-04 00:40:37 +08:00
|
|
|
OrcCBindingsStack(TargetMachine &TM,
|
2016-01-21 06:24:26 +08:00
|
|
|
std::unique_ptr<CompileCallbackMgr> CCMgr,
|
2015-10-28 10:40:04 +08:00
|
|
|
IndirectStubsManagerBuilder IndirectStubsMgrBuilder)
|
2018-04-03 04:57:56 +08:00
|
|
|
: DL(TM.createDataLayout()),
|
2018-02-07 05:25:11 +08:00
|
|
|
IndirectStubsMgr(IndirectStubsMgrBuilder()), CCMgr(std::move(CCMgr)),
|
|
|
|
ObjectLayer(ES,
|
|
|
|
[this](orc::VModuleKey K) {
|
|
|
|
auto ResolverI = Resolvers.find(K);
|
|
|
|
assert(ResolverI != Resolvers.end() &&
|
|
|
|
"No resolver for module K");
|
|
|
|
auto Resolver = std::move(ResolverI->second);
|
|
|
|
Resolvers.erase(ResolverI);
|
2018-02-15 06:13:02 +08:00
|
|
|
return ObjLayerT::Resources{
|
|
|
|
std::make_shared<SectionMemoryManager>(), Resolver};
|
2018-02-07 05:25:11 +08:00
|
|
|
}),
|
2016-01-21 06:24:26 +08:00
|
|
|
CompileLayer(ObjectLayer, orc::SimpleCompiler(TM)),
|
2018-02-07 05:25:11 +08:00
|
|
|
CODLayer(ES, CompileLayer,
|
|
|
|
[this](orc::VModuleKey K) {
|
|
|
|
auto ResolverI = Resolvers.find(K);
|
|
|
|
assert(ResolverI != Resolvers.end() &&
|
|
|
|
"No resolver for module K");
|
|
|
|
return ResolverI->second;
|
|
|
|
},
|
|
|
|
[this](orc::VModuleKey K,
|
|
|
|
std::shared_ptr<orc::SymbolResolver> Resolver) {
|
|
|
|
assert(!Resolvers.count(K) && "Resolver already present");
|
|
|
|
Resolvers[K] = std::move(Resolver);
|
|
|
|
},
|
2016-01-21 06:24:26 +08:00
|
|
|
[](Function &F) { return std::set<Function *>({&F}); },
|
|
|
|
*this->CCMgr, std::move(IndirectStubsMgrBuilder), false),
|
|
|
|
CXXRuntimeOverrides(
|
|
|
|
[this](const std::string &S) { return mangle(S); }) {}
|
2015-10-28 10:40:04 +08:00
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcErrorCode shutdown() {
|
2015-10-28 10:40:04 +08:00
|
|
|
// Run any destructors registered with __cxa_atexit.
|
|
|
|
CXXRuntimeOverrides.runDestructors();
|
|
|
|
// Run any IR destructors.
|
|
|
|
for (auto &DtorRunner : IRStaticDestructorRunners)
|
2017-07-07 10:59:13 +08:00
|
|
|
if (auto Err = DtorRunner.runViaLayer(*this))
|
|
|
|
return mapError(std::move(Err));
|
|
|
|
return LLVMOrcErrSuccess;
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string mangle(StringRef Name) {
|
|
|
|
std::string MangledName;
|
|
|
|
{
|
|
|
|
raw_string_ostream MangledNameStream(MangledName);
|
|
|
|
Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
|
|
|
|
}
|
|
|
|
return MangledName;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PtrTy>
|
2016-08-02 04:49:11 +08:00
|
|
|
static PtrTy fromTargetAddress(JITTargetAddress Addr) {
|
2015-10-28 10:40:04 +08:00
|
|
|
return reinterpret_cast<PtrTy>(static_cast<uintptr_t>(Addr));
|
|
|
|
}
|
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
|
|
|
|
LLVMOrcErrorCode
|
|
|
|
createLazyCompileCallback(JITTargetAddress &RetAddr,
|
|
|
|
LLVMOrcLazyCompileCallbackFn Callback,
|
2015-10-30 11:20:21 +08:00
|
|
|
void *CallbackCtx) {
|
2017-09-03 08:50:42 +08:00
|
|
|
if (auto CCInfoOrErr = CCMgr->getCompileCallback()) {
|
|
|
|
auto &CCInfo = *CCInfoOrErr;
|
|
|
|
CCInfo.setCompileAction([=]() -> JITTargetAddress {
|
|
|
|
return Callback(wrap(this), CallbackCtx);
|
|
|
|
});
|
|
|
|
RetAddr = CCInfo.getAddress();
|
|
|
|
return LLVMOrcErrSuccess;
|
|
|
|
} else
|
|
|
|
return mapError(CCInfoOrErr.takeError());
|
2015-10-30 11:20:21 +08:00
|
|
|
}
|
|
|
|
|
2016-04-26 05:21:20 +08:00
|
|
|
LLVMOrcErrorCode createIndirectStub(StringRef StubName,
|
2016-08-02 04:49:11 +08:00
|
|
|
JITTargetAddress Addr) {
|
2016-04-26 03:56:45 +08:00
|
|
|
return mapError(
|
2016-04-26 05:21:20 +08:00
|
|
|
IndirectStubsMgr->createStub(StubName, Addr, JITSymbolFlags::Exported));
|
2015-10-30 11:20:21 +08:00
|
|
|
}
|
|
|
|
|
2016-04-26 05:21:20 +08:00
|
|
|
LLVMOrcErrorCode setIndirectStubPointer(StringRef Name,
|
2016-08-02 04:49:11 +08:00
|
|
|
JITTargetAddress Addr) {
|
2016-04-26 03:56:45 +08:00
|
|
|
return mapError(IndirectStubsMgr->updatePointer(Name, Addr));
|
2015-10-30 11:20:21 +08:00
|
|
|
}
|
2015-10-28 10:40:04 +08:00
|
|
|
template <typename LayerT>
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcErrorCode
|
2018-03-15 08:30:14 +08:00
|
|
|
addIRModule(orc::VModuleKey &RetKey, LayerT &Layer, std::unique_ptr<Module> M,
|
2017-07-07 10:59:13 +08:00
|
|
|
std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
|
|
|
|
LLVMOrcSymbolResolverFn ExternalResolver,
|
|
|
|
void *ExternalResolverCtx) {
|
|
|
|
|
2015-10-28 10:40:04 +08:00
|
|
|
// Attach a data-layout if one isn't already present.
|
|
|
|
if (M->getDataLayout().isDefault())
|
|
|
|
M->setDataLayout(DL);
|
|
|
|
|
|
|
|
// Record the static constructors and destructors. We have to do this before
|
|
|
|
// we hand over ownership of the module to the JIT.
|
|
|
|
std::vector<std::string> CtorNames, DtorNames;
|
|
|
|
for (auto Ctor : orc::getConstructors(*M))
|
|
|
|
CtorNames.push_back(mangle(Ctor.Func->getName()));
|
|
|
|
for (auto Dtor : orc::getDestructors(*M))
|
|
|
|
DtorNames.push_back(mangle(Dtor.Func->getName()));
|
|
|
|
|
|
|
|
// Add the module to the JIT.
|
2018-02-09 10:30:40 +08:00
|
|
|
RetKey = ES.allocateVModule();
|
|
|
|
Resolvers[RetKey] = std::make_shared<CBindingsResolver>(
|
|
|
|
*this, ExternalResolver, ExternalResolverCtx);
|
|
|
|
if (auto Err = Layer.addModule(RetKey, std::move(M)))
|
|
|
|
return mapError(std::move(Err));
|
|
|
|
|
|
|
|
KeyLayers[RetKey] = detail::createGenericLayer(Layer);
|
2015-10-28 10:40:04 +08:00
|
|
|
|
|
|
|
// Run the static constructors, and save the static destructor runner for
|
|
|
|
// execution when the JIT is torn down.
|
2018-02-09 10:30:40 +08:00
|
|
|
orc::CtorDtorRunner<OrcCBindingsStack> CtorRunner(std::move(CtorNames),
|
|
|
|
RetKey);
|
2017-07-07 10:59:13 +08:00
|
|
|
if (auto Err = CtorRunner.runViaLayer(*this))
|
|
|
|
return mapError(std::move(Err));
|
2015-10-28 10:40:04 +08:00
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
IRStaticDestructorRunners.emplace_back(std::move(DtorNames), RetKey);
|
2015-10-28 10:40:04 +08:00
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
return LLVMOrcErrSuccess;
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
LLVMOrcErrorCode addIRModuleEager(orc::VModuleKey &RetKey,
|
2018-03-15 08:30:14 +08:00
|
|
|
std::unique_ptr<Module> M,
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcSymbolResolverFn ExternalResolver,
|
|
|
|
void *ExternalResolverCtx) {
|
2018-02-09 10:30:40 +08:00
|
|
|
return addIRModule(RetKey, CompileLayer, std::move(M),
|
2015-10-28 10:40:04 +08:00
|
|
|
llvm::make_unique<SectionMemoryManager>(),
|
|
|
|
std::move(ExternalResolver), ExternalResolverCtx);
|
|
|
|
}
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
LLVMOrcErrorCode addIRModuleLazy(orc::VModuleKey &RetKey,
|
2018-03-15 08:30:14 +08:00
|
|
|
std::unique_ptr<Module> M,
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcSymbolResolverFn ExternalResolver,
|
|
|
|
void *ExternalResolverCtx) {
|
2018-02-09 10:30:40 +08:00
|
|
|
return addIRModule(RetKey, CODLayer, std::move(M),
|
2016-04-26 05:21:20 +08:00
|
|
|
llvm::make_unique<SectionMemoryManager>(),
|
2015-10-28 10:40:04 +08:00
|
|
|
std::move(ExternalResolver), ExternalResolverCtx);
|
|
|
|
}
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
LLVMOrcErrorCode removeModule(orc::VModuleKey K) {
|
|
|
|
// FIXME: Should error release the module key?
|
|
|
|
if (auto Err = KeyLayers[K]->removeModule(K))
|
2017-07-07 10:59:13 +08:00
|
|
|
return mapError(std::move(Err));
|
2018-02-09 10:30:40 +08:00
|
|
|
ES.releaseVModule(K);
|
|
|
|
KeyLayers.erase(K);
|
2017-07-07 10:59:13 +08:00
|
|
|
return LLVMOrcErrSuccess;
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
LLVMOrcErrorCode addObject(orc::VModuleKey &RetKey,
|
2017-09-17 11:25:03 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> ObjBuffer,
|
|
|
|
LLVMOrcSymbolResolverFn ExternalResolver,
|
|
|
|
void *ExternalResolverCtx) {
|
2018-02-22 05:55:49 +08:00
|
|
|
if (auto Obj = object::ObjectFile::createObjectFile(
|
|
|
|
ObjBuffer->getMemBufferRef())) {
|
2017-09-17 11:25:03 +08:00
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
RetKey = ES.allocateVModule();
|
|
|
|
Resolvers[RetKey] = std::make_shared<CBindingsResolver>(
|
2018-02-07 05:25:11 +08:00
|
|
|
*this, ExternalResolver, ExternalResolverCtx);
|
2017-09-17 11:25:03 +08:00
|
|
|
|
2018-02-22 05:55:49 +08:00
|
|
|
if (auto Err = ObjectLayer.addObject(RetKey, std::move(ObjBuffer)))
|
2018-02-09 10:30:40 +08:00
|
|
|
return mapError(std::move(Err));
|
2017-09-17 11:25:03 +08:00
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
KeyLayers[RetKey] = detail::createGenericLayer(ObjectLayer);
|
2017-09-17 11:25:03 +08:00
|
|
|
|
|
|
|
return LLVMOrcErrSuccess;
|
|
|
|
} else
|
2018-02-22 05:55:49 +08:00
|
|
|
return mapError(Obj.takeError());
|
2017-09-17 11:25:03 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
JITSymbol findSymbol(const std::string &Name,
|
|
|
|
bool ExportedSymbolsOnly) {
|
2015-10-30 11:20:21 +08:00
|
|
|
if (auto Sym = IndirectStubsMgr->findStub(Name, ExportedSymbolsOnly))
|
|
|
|
return Sym;
|
2015-10-28 10:40:04 +08:00
|
|
|
return CODLayer.findSymbol(mangle(Name), ExportedSymbolsOnly);
|
|
|
|
}
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
JITSymbol findSymbolIn(orc::VModuleKey K, const std::string &Name,
|
2016-08-02 04:49:11 +08:00
|
|
|
bool ExportedSymbolsOnly) {
|
2018-02-09 10:30:40 +08:00
|
|
|
return KeyLayers[K]->findSymbolIn(K, Name, ExportedSymbolsOnly);
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcErrorCode findSymbolAddress(JITTargetAddress &RetAddr,
|
|
|
|
const std::string &Name,
|
|
|
|
bool ExportedSymbolsOnly) {
|
|
|
|
RetAddr = 0;
|
|
|
|
if (auto Sym = findSymbol(Name, ExportedSymbolsOnly)) {
|
|
|
|
// Successful lookup, non-null symbol:
|
|
|
|
if (auto AddrOrErr = Sym.getAddress()) {
|
|
|
|
RetAddr = *AddrOrErr;
|
|
|
|
return LLVMOrcErrSuccess;
|
|
|
|
} else
|
|
|
|
return mapError(AddrOrErr.takeError());
|
|
|
|
} else if (auto Err = Sym.takeError()) {
|
|
|
|
// Lookup failure - report error.
|
|
|
|
return mapError(std::move(Err));
|
|
|
|
}
|
|
|
|
// Otherwise we had a successful lookup but got a null result. We already
|
|
|
|
// set RetAddr to '0' above, so just return success.
|
|
|
|
return LLVMOrcErrSuccess;
|
|
|
|
}
|
|
|
|
|
2016-04-26 05:21:20 +08:00
|
|
|
const std::string &getErrorMessage() const { return ErrMsg; }
|
2016-04-26 03:56:45 +08:00
|
|
|
|
2015-10-28 10:40:04 +08:00
|
|
|
private:
|
|
|
|
|
2016-04-26 03:56:45 +08:00
|
|
|
LLVMOrcErrorCode mapError(Error Err) {
|
|
|
|
LLVMOrcErrorCode Result = LLVMOrcErrSuccess;
|
2016-04-26 05:21:20 +08:00
|
|
|
handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
|
|
|
|
// Handler of last resort.
|
|
|
|
Result = LLVMOrcErrGeneric;
|
|
|
|
ErrMsg = "";
|
|
|
|
raw_string_ostream ErrStream(ErrMsg);
|
|
|
|
EIB.log(ErrStream);
|
|
|
|
});
|
2016-04-26 03:56:45 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-02-07 05:25:11 +08:00
|
|
|
void reportError(Error Err) {
|
|
|
|
// FIXME: Report errors on the execution session.
|
|
|
|
logAllUnhandledErrors(std::move(Err), errs(), "ORC error: ");
|
|
|
|
};
|
|
|
|
|
|
|
|
orc::ExecutionSession ES;
|
|
|
|
|
2015-10-28 10:40:04 +08:00
|
|
|
DataLayout DL;
|
|
|
|
SectionMemoryManager CCMgrMemMgr;
|
|
|
|
|
2016-01-21 01:39:52 +08:00
|
|
|
std::unique_ptr<orc::IndirectStubsManager> IndirectStubsMgr;
|
|
|
|
|
2015-11-04 00:40:37 +08:00
|
|
|
std::unique_ptr<CompileCallbackMgr> CCMgr;
|
2015-10-28 10:40:04 +08:00
|
|
|
ObjLayerT ObjectLayer;
|
|
|
|
CompileLayerT CompileLayer;
|
|
|
|
CODLayerT CODLayer;
|
|
|
|
|
2018-02-09 10:30:40 +08:00
|
|
|
std::map<orc::VModuleKey, std::unique_ptr<detail::GenericLayer>> KeyLayers;
|
2015-10-28 10:40:04 +08:00
|
|
|
|
|
|
|
orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
|
|
|
|
std::vector<orc::CtorDtorRunner<OrcCBindingsStack>> IRStaticDestructorRunners;
|
2016-04-26 03:56:45 +08:00
|
|
|
std::string ErrMsg;
|
2018-02-07 05:25:11 +08:00
|
|
|
|
|
|
|
std::map<orc::VModuleKey, std::shared_ptr<orc::SymbolResolver>> Resolvers;
|
2015-10-28 10:40:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_LIB_EXECUTIONENGINE_ORC_ORCCBINDINGSSTACK_H
|