Please use GitHub pull requests for new patches. Avoid migrating existing patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
llvm/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h
Show First 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | KaleidoscopeJIT(ExecutionSession &ES, MyRemote &Remote) | ||||
ES, | ES, | ||||
[this](const std::string &Name) -> JITSymbol { | [this](const std::string &Name) -> JITSymbol { | ||||
if (auto Sym = IndirectStubsMgr->findStub(Name, false)) | if (auto Sym = IndirectStubsMgr->findStub(Name, false)) | ||||
return Sym; | return Sym; | ||||
if (auto Sym = OptimizeLayer.findSymbol(Name, false)) | if (auto Sym = OptimizeLayer.findSymbol(Name, false)) | ||||
return Sym; | return Sym; | ||||
else if (auto Err = Sym.takeError()) | else if (auto Err = Sym.takeError()) | ||||
return std::move(Err); | return std::move(Err); | ||||
if (auto Addr = cantFail(this->Remote.getSymbolAddress(Name))) | if (auto Addr = llvm_cantFail(this->Remote.getSymbolAddress(Name))) | ||||
return JITSymbol(Addr, JITSymbolFlags::Exported); | return JITSymbol(Addr, JITSymbolFlags::Exported); | ||||
return nullptr; | return nullptr; | ||||
}, | }, | ||||
[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })), | [](Error Err) { llvm_cantFail(std::move(Err), "lookupFlags failed"); })), | ||||
TM(EngineBuilder().selectTarget(Triple(Remote.getTargetTriple()), "", | TM(EngineBuilder().selectTarget(Triple(Remote.getTargetTriple()), "", | ||||
"", SmallVector<std::string, 0>())), | "", SmallVector<std::string, 0>())), | ||||
DL(TM->createDataLayout()), | DL(TM->createDataLayout()), | ||||
ObjectLayer(AcknowledgeORCv1Deprecation, ES, | ObjectLayer(AcknowledgeORCv1Deprecation, ES, | ||||
[this](VModuleKey K) { | [this](VModuleKey K) { | ||||
return LegacyRTDyldObjectLinkingLayer::Resources{ | return LegacyRTDyldObjectLinkingLayer::Resources{ | ||||
cantFail(this->Remote.createRemoteMemoryManager()), | llvm_cantFail(this->Remote.createRemoteMemoryManager()), | ||||
Resolver}; | Resolver}; | ||||
}), | }), | ||||
CompileLayer(AcknowledgeORCv1Deprecation, ObjectLayer, | CompileLayer(AcknowledgeORCv1Deprecation, ObjectLayer, | ||||
SimpleCompiler(*TM)), | SimpleCompiler(*TM)), | ||||
OptimizeLayer(AcknowledgeORCv1Deprecation, CompileLayer, | OptimizeLayer(AcknowledgeORCv1Deprecation, CompileLayer, | ||||
[this](std::unique_ptr<Module> M) { | [this](std::unique_ptr<Module> M) { | ||||
return optimizeModule(std::move(M)); | return optimizeModule(std::move(M)); | ||||
}), | }), | ||||
Remote(Remote) { | Remote(Remote) { | ||||
auto CCMgrOrErr = Remote.enableCompileCallbacks(0); | auto CCMgrOrErr = Remote.enableCompileCallbacks(0); | ||||
if (!CCMgrOrErr) { | if (!CCMgrOrErr) { | ||||
logAllUnhandledErrors(CCMgrOrErr.takeError(), errs(), | logAllUnhandledErrors(CCMgrOrErr.takeError(), errs(), | ||||
"Error enabling remote compile callbacks:"); | "Error enabling remote compile callbacks:"); | ||||
exit(1); | exit(1); | ||||
} | } | ||||
CompileCallbackMgr = &*CCMgrOrErr; | CompileCallbackMgr = &*CCMgrOrErr; | ||||
IndirectStubsMgr = cantFail(Remote.createIndirectStubsManager()); | IndirectStubsMgr = llvm_cantFail(Remote.createIndirectStubsManager()); | ||||
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr); | llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr); | ||||
} | } | ||||
TargetMachine &getTargetMachine() { return *TM; } | TargetMachine &getTargetMachine() { return *TM; } | ||||
VModuleKey addModule(std::unique_ptr<Module> M) { | VModuleKey addModule(std::unique_ptr<Module> M) { | ||||
// Add the module with a new VModuleKey. | // Add the module with a new VModuleKey. | ||||
auto K = ES.allocateVModule(); | auto K = ES.allocateVModule(); | ||||
cantFail(OptimizeLayer.addModule(K, std::move(M))); | llvm_cantFail(OptimizeLayer.addModule(K, std::move(M))); | ||||
return K; | return K; | ||||
} | } | ||||
Error addFunctionAST(std::unique_ptr<FunctionAST> FnAST) { | Error addFunctionAST(std::unique_ptr<FunctionAST> FnAST) { | ||||
// Move ownership of FnAST to a shared pointer - C++11 lambdas don't support | // Move ownership of FnAST to a shared pointer - C++11 lambdas don't support | ||||
// capture-by-move, which is be required for unique_ptr. | // capture-by-move, which is be required for unique_ptr. | ||||
auto SharedFnAST = std::shared_ptr<FunctionAST>(std::move(FnAST)); | auto SharedFnAST = std::shared_ptr<FunctionAST>(std::move(FnAST)); | ||||
Show All 13 Lines | Error addFunctionAST(std::unique_ptr<FunctionAST> FnAST) { | ||||
// The JIT runtime (the resolver block) will use the return address of | // The JIT runtime (the resolver block) will use the return address of | ||||
// this function as the address to continue at once it has reset the | // this function as the address to continue at once it has reset the | ||||
// CPU state to what it was immediately before the call. | // CPU state to what it was immediately before the call. | ||||
auto CompileAction = [this, SharedFnAST]() { | auto CompileAction = [this, SharedFnAST]() { | ||||
auto M = irgenAndTakeOwnership(*SharedFnAST, "$impl"); | auto M = irgenAndTakeOwnership(*SharedFnAST, "$impl"); | ||||
addModule(std::move(M)); | addModule(std::move(M)); | ||||
auto Sym = findSymbol(SharedFnAST->getName() + "$impl"); | auto Sym = findSymbol(SharedFnAST->getName() + "$impl"); | ||||
assert(Sym && "Couldn't find compiled function?"); | assert(Sym && "Couldn't find compiled function?"); | ||||
JITTargetAddress SymAddr = cantFail(Sym.getAddress()); | JITTargetAddress SymAddr = llvm_cantFail(Sym.getAddress()); | ||||
if (auto Err = IndirectStubsMgr->updatePointer( | if (auto Err = IndirectStubsMgr->updatePointer( | ||||
mangle(SharedFnAST->getName()), SymAddr)) { | mangle(SharedFnAST->getName()), SymAddr)) { | ||||
logAllUnhandledErrors(std::move(Err), errs(), | logAllUnhandledErrors(std::move(Err), errs(), | ||||
"Error updating function pointer: "); | "Error updating function pointer: "); | ||||
exit(1); | exit(1); | ||||
} | } | ||||
return SymAddr; | return SymAddr; | ||||
}; | }; | ||||
// Create a CompileCallback suing the CompileAction - this is the re-entry | // Create a CompileCallback suing the CompileAction - this is the re-entry | ||||
// point into the compiler for functions that haven't been compiled yet. | // point into the compiler for functions that haven't been compiled yet. | ||||
auto CCAddr = cantFail( | auto CCAddr = llvm_cantFail( | ||||
CompileCallbackMgr->getCompileCallback(std::move(CompileAction))); | CompileCallbackMgr->getCompileCallback(std::move(CompileAction))); | ||||
// Create an indirect stub. This serves as the functions "canonical | // Create an indirect stub. This serves as the functions "canonical | ||||
// definition" - an unchanging (constant address) entry point to the | // definition" - an unchanging (constant address) entry point to the | ||||
// function implementation. | // function implementation. | ||||
// Initially we point the stub's function-pointer at the compile callback | // Initially we point the stub's function-pointer at the compile callback | ||||
// that we just created. In the compile action for the callback we will | // that we just created. In the compile action for the callback we will | ||||
// update the stub's function pointer to point at the function | // update the stub's function pointer to point at the function | ||||
Show All 9 Lines | Error executeRemoteExpr(JITTargetAddress ExprAddr) { | ||||
return Remote.callVoidVoid(ExprAddr); | return Remote.callVoidVoid(ExprAddr); | ||||
} | } | ||||
JITSymbol findSymbol(const std::string Name) { | JITSymbol findSymbol(const std::string Name) { | ||||
return OptimizeLayer.findSymbol(mangle(Name), true); | return OptimizeLayer.findSymbol(mangle(Name), true); | ||||
} | } | ||||
void removeModule(VModuleKey K) { | void removeModule(VModuleKey K) { | ||||
cantFail(OptimizeLayer.removeModule(K)); | llvm_cantFail(OptimizeLayer.removeModule(K)); | ||||
} | } | ||||
private: | private: | ||||
std::string mangle(const std::string &Name) { | std::string mangle(const std::string &Name) { | ||||
std::string MangledName; | std::string MangledName; | ||||
raw_string_ostream MangledNameStream(MangledName); | raw_string_ostream MangledNameStream(MangledName); | ||||
Mangler::getNameWithPrefix(MangledNameStream, Name, DL); | Mangler::getNameWithPrefix(MangledNameStream, Name, DL); | ||||
return MangledNameStream.str(); | return MangledNameStream.str(); | ||||
Show All 26 Lines |