libraries. This patch substantially updates ORCv2's lookup API in order to support weak references, and to better support static archives. Key changes: -- Each symbol being looked for is now associated with a SymbolLookupFlags value. If the associated value is SymbolLookupFlags::RequiredSymbol then the symbol must be defined in one of the JITDylibs being searched (or be able to be generated in one of these JITDylibs via an attached definition generator) or the lookup will fail with an error. If the associated value is SymbolLookupFlags::WeaklyReferencedSymbol then the symbol is permitted to be undefined, in which case it will simply not appear in the resulting SymbolMap if the rest of the lookup succeeds. Since lookup now requires these flags for each symbol, the lookup method now takes an instance of a new SymbolLookupSet type rather than a SymbolNameSet. SymbolLookupSet is a vector-backed set of (name, flags) pairs. Clients are responsible for ensuring that the set property (i.e. unique elements) holds, though this is usually simple and SymbolLookupSet provides convenience methods to support this. -- Lookups now have an associated LookupKind value, which is either LookupKind::Static or LookupKind::DLSym. Definition generators can inspect the lookup kind when determining whether or not to generate new definitions. The StaticLibraryDefinitionGenerator is updated to only pull in new objects from the archive if the lookup kind is Static. This allows lookup to be re-used to emulate dlsym for JIT'd symbols without pulling in new objects from archives (which would not happen in a normal dlsym call). -- JITLink is updated to allow externals to be assigned weak linkage, and weak externals now use the SymbolLookupFlags::WeaklyReferencedSymbol value for lookups. Unresolved weak references will be assigned the default value of zero. Since this patch was modifying the lookup API anyway, it alo replaces all of the "MatchNonExported" boolean arguments with a "JITDylibLookupFlags" enum for readability. If a JITDylib's associated value is JITDylibLookupFlags::MatchExportedSymbolsOnly then the lookup will only match against exported (non-hidden) symbols in that JITDylib. If a JITDylib's associated value is JITDylibLookupFlags::MatchAllSymbols then the lookup will match against any symbol defined in the JITDylib.
243 lines
7.5 KiB
C++
243 lines
7.5 KiB
C++
//===--------- LLJIT.cpp - An ORC-based JIT for compiling LLVM IR ---------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
|
|
#include "llvm/ExecutionEngine/Orc/OrcError.h"
|
|
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
|
|
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
|
#include "llvm/IR/Mangler.h"
|
|
|
|
namespace llvm {
|
|
namespace orc {
|
|
|
|
Error LLJITBuilderState::prepareForConstruction() {
|
|
|
|
if (!JTMB) {
|
|
if (auto JTMBOrErr = JITTargetMachineBuilder::detectHost())
|
|
JTMB = std::move(*JTMBOrErr);
|
|
else
|
|
return JTMBOrErr.takeError();
|
|
}
|
|
|
|
return Error::success();
|
|
}
|
|
|
|
LLJIT::~LLJIT() {
|
|
if (CompileThreads)
|
|
CompileThreads->wait();
|
|
}
|
|
|
|
Error LLJIT::defineAbsolute(StringRef Name, JITEvaluatedSymbol Sym) {
|
|
auto InternedName = ES->intern(Name);
|
|
SymbolMap Symbols({{InternedName, Sym}});
|
|
return Main.define(absoluteSymbols(std::move(Symbols)));
|
|
}
|
|
|
|
Error LLJIT::addIRModule(JITDylib &JD, ThreadSafeModule TSM) {
|
|
assert(TSM && "Can not add null module");
|
|
|
|
if (auto Err =
|
|
TSM.withModuleDo([&](Module &M) { return applyDataLayout(M); }))
|
|
return Err;
|
|
|
|
return CompileLayer->add(JD, std::move(TSM), ES->allocateVModule());
|
|
}
|
|
|
|
Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
|
|
assert(Obj && "Can not add null object");
|
|
|
|
return ObjTransformLayer.add(JD, std::move(Obj), ES->allocateVModule());
|
|
}
|
|
|
|
Expected<JITEvaluatedSymbol> LLJIT::lookupLinkerMangled(JITDylib &JD,
|
|
StringRef Name) {
|
|
return ES->lookup(
|
|
makeJITDylibSearchOrder(&JD, JITDylibLookupFlags::MatchAllSymbols),
|
|
ES->intern(Name));
|
|
}
|
|
|
|
std::unique_ptr<ObjectLayer>
|
|
LLJIT::createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES) {
|
|
|
|
// If the config state provided an ObjectLinkingLayer factory then use it.
|
|
if (S.CreateObjectLinkingLayer)
|
|
return S.CreateObjectLinkingLayer(ES, S.JTMB->getTargetTriple());
|
|
|
|
// Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
|
|
// a new SectionMemoryManager for each object.
|
|
auto GetMemMgr = []() { return std::make_unique<SectionMemoryManager>(); };
|
|
auto ObjLinkingLayer =
|
|
std::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr));
|
|
|
|
if (S.JTMB->getTargetTriple().isOSBinFormatCOFF())
|
|
ObjLinkingLayer->setOverrideObjectFlagsWithResponsibilityFlags(true);
|
|
|
|
// FIXME: Explicit conversion to std::unique_ptr<ObjectLayer> added to silence
|
|
// errors from some GCC / libstdc++ bots. Remove this conversion (i.e.
|
|
// just return ObjLinkingLayer) once those bots are upgraded.
|
|
return std::unique_ptr<ObjectLayer>(std::move(ObjLinkingLayer));
|
|
}
|
|
|
|
Expected<IRCompileLayer::CompileFunction>
|
|
LLJIT::createCompileFunction(LLJITBuilderState &S,
|
|
JITTargetMachineBuilder JTMB) {
|
|
|
|
/// If there is a custom compile function creator set then use it.
|
|
if (S.CreateCompileFunction)
|
|
return S.CreateCompileFunction(std::move(JTMB));
|
|
|
|
// Otherwise default to creating a SimpleCompiler, or ConcurrentIRCompiler,
|
|
// depending on the number of threads requested.
|
|
if (S.NumCompileThreads > 0)
|
|
return ConcurrentIRCompiler(std::move(JTMB));
|
|
|
|
auto TM = JTMB.createTargetMachine();
|
|
if (!TM)
|
|
return TM.takeError();
|
|
|
|
return TMOwningSimpleCompiler(std::move(*TM));
|
|
}
|
|
|
|
LLJIT::LLJIT(LLJITBuilderState &S, Error &Err)
|
|
: ES(S.ES ? std::move(S.ES) : std::make_unique<ExecutionSession>()),
|
|
Main(this->ES->getMainJITDylib()), DL(""),
|
|
ObjLinkingLayer(createObjectLinkingLayer(S, *ES)),
|
|
ObjTransformLayer(*this->ES, *ObjLinkingLayer), CtorRunner(Main),
|
|
DtorRunner(Main) {
|
|
|
|
ErrorAsOutParameter _(&Err);
|
|
|
|
if (auto DLOrErr = S.JTMB->getDefaultDataLayoutForTarget())
|
|
DL = std::move(*DLOrErr);
|
|
else {
|
|
Err = DLOrErr.takeError();
|
|
return;
|
|
}
|
|
|
|
{
|
|
auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB));
|
|
if (!CompileFunction) {
|
|
Err = CompileFunction.takeError();
|
|
return;
|
|
}
|
|
CompileLayer = std::make_unique<IRCompileLayer>(
|
|
*ES, ObjTransformLayer, std::move(*CompileFunction));
|
|
}
|
|
|
|
if (S.NumCompileThreads > 0) {
|
|
CompileLayer->setCloneToNewContextOnEmit(true);
|
|
CompileThreads = std::make_unique<ThreadPool>(S.NumCompileThreads);
|
|
ES->setDispatchMaterialization(
|
|
[this](JITDylib &JD, std::unique_ptr<MaterializationUnit> MU) {
|
|
// FIXME: Switch to move capture once we have c++14.
|
|
auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU));
|
|
auto Work = [SharedMU, &JD]() { SharedMU->doMaterialize(JD); };
|
|
CompileThreads->async(std::move(Work));
|
|
});
|
|
}
|
|
}
|
|
|
|
std::string LLJIT::mangle(StringRef UnmangledName) {
|
|
std::string MangledName;
|
|
{
|
|
raw_string_ostream MangledNameStream(MangledName);
|
|
Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL);
|
|
}
|
|
return MangledName;
|
|
}
|
|
|
|
Error LLJIT::applyDataLayout(Module &M) {
|
|
if (M.getDataLayout().isDefault())
|
|
M.setDataLayout(DL);
|
|
|
|
if (M.getDataLayout() != DL)
|
|
return make_error<StringError>(
|
|
"Added modules have incompatible data layouts",
|
|
inconvertibleErrorCode());
|
|
|
|
return Error::success();
|
|
}
|
|
|
|
void LLJIT::recordCtorDtors(Module &M) {
|
|
CtorRunner.add(getConstructors(M));
|
|
DtorRunner.add(getDestructors(M));
|
|
}
|
|
|
|
Error LLLazyJITBuilderState::prepareForConstruction() {
|
|
if (auto Err = LLJITBuilderState::prepareForConstruction())
|
|
return Err;
|
|
TT = JTMB->getTargetTriple();
|
|
return Error::success();
|
|
}
|
|
|
|
Error LLLazyJIT::addLazyIRModule(JITDylib &JD, ThreadSafeModule TSM) {
|
|
assert(TSM && "Can not add null module");
|
|
|
|
if (auto Err = TSM.withModuleDo([&](Module &M) -> Error {
|
|
if (auto Err = applyDataLayout(M))
|
|
return Err;
|
|
|
|
recordCtorDtors(M);
|
|
return Error::success();
|
|
}))
|
|
return Err;
|
|
|
|
return CODLayer->add(JD, std::move(TSM), ES->allocateVModule());
|
|
}
|
|
|
|
LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
|
|
|
|
// If LLJIT construction failed then bail out.
|
|
if (Err)
|
|
return;
|
|
|
|
ErrorAsOutParameter _(&Err);
|
|
|
|
/// Take/Create the lazy-compile callthrough manager.
|
|
if (S.LCTMgr)
|
|
LCTMgr = std::move(S.LCTMgr);
|
|
else {
|
|
if (auto LCTMgrOrErr = createLocalLazyCallThroughManager(
|
|
S.TT, *ES, S.LazyCompileFailureAddr))
|
|
LCTMgr = std::move(*LCTMgrOrErr);
|
|
else {
|
|
Err = LCTMgrOrErr.takeError();
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Take/Create the indirect stubs manager builder.
|
|
auto ISMBuilder = std::move(S.ISMBuilder);
|
|
|
|
// If none was provided, try to build one.
|
|
if (!ISMBuilder)
|
|
ISMBuilder = createLocalIndirectStubsManagerBuilder(S.TT);
|
|
|
|
// No luck. Bail out.
|
|
if (!ISMBuilder) {
|
|
Err = make_error<StringError>("Could not construct "
|
|
"IndirectStubsManagerBuilder for target " +
|
|
S.TT.str(),
|
|
inconvertibleErrorCode());
|
|
return;
|
|
}
|
|
|
|
// Create the transform layer.
|
|
TransformLayer = std::make_unique<IRTransformLayer>(*ES, *CompileLayer);
|
|
|
|
// Create the COD layer.
|
|
CODLayer = std::make_unique<CompileOnDemandLayer>(
|
|
*ES, *TransformLayer, *LCTMgr, std::move(ISMBuilder));
|
|
|
|
if (S.NumCompileThreads > 0)
|
|
CODLayer->setCloneToNewContextOnEmit(true);
|
|
}
|
|
|
|
} // End namespace orc.
|
|
} // End namespace llvm.
|