forked from OSchip/llvm-project
[Kaleidoscope][BuildingAJIT] Add code for Chapter 5 - remote JITing.
This chapter demonstrates lazily JITing from ASTs with the expressions being executed on a remote machine via a TCP connection. It needs some polish, but is substantially complete. Currently x86-64 SysV ABI (Darwin and Linux) only, but other architectures can be supported by changing the server code to use alternative ABI support classes from llvm/include/llvm/ExecutionEngine/Orc/OrcABISupport.h. llvm-svn: 271193
This commit is contained in:
parent
ed58207c46
commit
a243ba9cad
|
@ -2,4 +2,5 @@ add_subdirectory(Chapter1)
|
|||
add_subdirectory(Chapter2)
|
||||
add_subdirectory(Chapter3)
|
||||
add_subdirectory(Chapter4)
|
||||
add_subdirectory(Chapter5)
|
||||
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
add_subdirectory(Server)
|
||||
|
||||
set(LLVM_LINK_COMPONENTS
|
||||
Analysis
|
||||
Core
|
||||
ExecutionEngine
|
||||
InstCombine
|
||||
Object
|
||||
OrcJIT
|
||||
RuntimeDyld
|
||||
ScalarOpts
|
||||
Support
|
||||
TransformUtils
|
||||
native
|
||||
)
|
||||
|
||||
add_kaleidoscope_chapter(BuildingAJIT-Ch5
|
||||
toy.cpp
|
||||
)
|
||||
|
||||
export_executable_symbols(BuildingAJIT-Ch5)
|
|
@ -0,0 +1,263 @@
|
|||
//===----- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope ----*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Contains a simple JIT definition for use in the kaleidoscope tutorials.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
|
||||
#define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
|
||||
|
||||
#include "RemoteJITUtils.h"
|
||||
#include "llvm/ADT/STLExtras.h"
|
||||
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
||||
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
||||
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
||||
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
|
||||
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
|
||||
#include "llvm/ExecutionEngine/Orc/JITSymbol.h"
|
||||
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
|
||||
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
|
||||
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
|
||||
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
|
||||
#include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
|
||||
#include "llvm/IR/DataLayout.h"
|
||||
#include "llvm/IR/Mangler.h"
|
||||
#include "llvm/Support/DynamicLibrary.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include "llvm/Target/TargetMachine.h"
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class PrototypeAST;
|
||||
class ExprAST;
|
||||
|
||||
/// FunctionAST - This class represents a function definition itself.
|
||||
class FunctionAST {
|
||||
std::unique_ptr<PrototypeAST> Proto;
|
||||
std::unique_ptr<ExprAST> Body;
|
||||
|
||||
public:
|
||||
FunctionAST(std::unique_ptr<PrototypeAST> Proto,
|
||||
std::unique_ptr<ExprAST> Body)
|
||||
: Proto(std::move(Proto)), Body(std::move(Body)) {}
|
||||
const PrototypeAST& getProto() const;
|
||||
const std::string& getName() const;
|
||||
llvm::Function *codegen();
|
||||
};
|
||||
|
||||
/// This will compile FnAST to IR, rename the function to add the given
|
||||
/// suffix (needed to prevent a name-clash with the function's stub),
|
||||
/// and then take ownership of the module that the function was compiled
|
||||
/// into.
|
||||
std::unique_ptr<llvm::Module>
|
||||
irgenAndTakeOwnership(FunctionAST &FnAST, const std::string &Suffix);
|
||||
|
||||
namespace llvm {
|
||||
namespace orc {
|
||||
|
||||
// Typedef the remote-client API.
|
||||
typedef remote::OrcRemoteTargetClient<FDRPCChannel> MyRemote;
|
||||
|
||||
class KaleidoscopeJIT {
|
||||
private:
|
||||
MyRemote &Remote;
|
||||
std::unique_ptr<TargetMachine> TM;
|
||||
const DataLayout DL;
|
||||
JITCompileCallbackManager *CompileCallbackMgr;
|
||||
std::unique_ptr<IndirectStubsManager> IndirectStubsMgr;
|
||||
ObjectLinkingLayer<> ObjectLayer;
|
||||
IRCompileLayer<decltype(ObjectLayer)> CompileLayer;
|
||||
|
||||
typedef std::function<std::unique_ptr<Module>(std::unique_ptr<Module>)>
|
||||
OptimizeFunction;
|
||||
|
||||
IRTransformLayer<decltype(CompileLayer), OptimizeFunction> OptimizeLayer;
|
||||
|
||||
public:
|
||||
typedef decltype(OptimizeLayer)::ModuleSetHandleT ModuleHandle;
|
||||
|
||||
KaleidoscopeJIT(MyRemote &Remote)
|
||||
: Remote(Remote),
|
||||
TM(EngineBuilder().selectTarget()),
|
||||
DL(TM->createDataLayout()),
|
||||
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
|
||||
OptimizeLayer(CompileLayer,
|
||||
[this](std::unique_ptr<Module> M) {
|
||||
return optimizeModule(std::move(M));
|
||||
}) {
|
||||
auto CCMgrOrErr = Remote.enableCompileCallbacks(0);
|
||||
if (!CCMgrOrErr) {
|
||||
logAllUnhandledErrors(CCMgrOrErr.takeError(), errs(),
|
||||
"Error enabling remote compile callbacks:");
|
||||
exit(1);
|
||||
}
|
||||
CompileCallbackMgr = &*CCMgrOrErr;
|
||||
std::unique_ptr<MyRemote::RCIndirectStubsManager> ISM;
|
||||
if (auto Err = Remote.createIndirectStubsManager(ISM)) {
|
||||
logAllUnhandledErrors(std::move(Err), errs(),
|
||||
"Error creating indirect stubs manager:");
|
||||
exit(1);
|
||||
}
|
||||
IndirectStubsMgr = std::move(ISM);
|
||||
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
|
||||
}
|
||||
|
||||
TargetMachine &getTargetMachine() { return *TM; }
|
||||
|
||||
ModuleHandle addModule(std::unique_ptr<Module> M) {
|
||||
|
||||
// Build our symbol resolver:
|
||||
// Lambda 1: Look back into the JIT itself to find symbols that are part of
|
||||
// the same "logical dylib".
|
||||
// Lambda 2: Search for external symbols in the host process.
|
||||
auto Resolver = createLambdaResolver(
|
||||
[&](const std::string &Name) {
|
||||
if (auto Sym = IndirectStubsMgr->findStub(Name, false))
|
||||
return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
|
||||
if (auto Sym = OptimizeLayer.findSymbol(Name, false))
|
||||
return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
|
||||
return RuntimeDyld::SymbolInfo(nullptr);
|
||||
},
|
||||
[&](const std::string &Name) {
|
||||
if (auto AddrOrErr = Remote.getSymbolAddress(Name))
|
||||
return RuntimeDyld::SymbolInfo(*AddrOrErr,
|
||||
JITSymbolFlags::Exported);
|
||||
else {
|
||||
logAllUnhandledErrors(AddrOrErr.takeError(), errs(),
|
||||
"Error resolving remote symbol:");
|
||||
exit(1);
|
||||
}
|
||||
return RuntimeDyld::SymbolInfo(nullptr);
|
||||
});
|
||||
|
||||
std::unique_ptr<MyRemote::RCMemoryManager> MemMgr;
|
||||
if (auto Err = Remote.createRemoteMemoryManager(MemMgr)) {
|
||||
logAllUnhandledErrors(std::move(Err), errs(),
|
||||
"Error creating remote memory manager:");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Build a singlton module set to hold our module.
|
||||
std::vector<std::unique_ptr<Module>> Ms;
|
||||
Ms.push_back(std::move(M));
|
||||
|
||||
// Add the set to the JIT with the resolver we created above and a newly
|
||||
// created SectionMemoryManager.
|
||||
return OptimizeLayer.addModuleSet(std::move(Ms),
|
||||
std::move(MemMgr),
|
||||
std::move(Resolver));
|
||||
}
|
||||
|
||||
Error addFunctionAST(std::unique_ptr<FunctionAST> FnAST) {
|
||||
// Create a CompileCallback - this is the re-entry point into the compiler
|
||||
// for functions that haven't been compiled yet.
|
||||
auto CCInfo = CompileCallbackMgr->getCompileCallback();
|
||||
|
||||
// Create an indirect stub. This serves as the functions "canonical
|
||||
// definition" - an unchanging (constant address) entry point to the
|
||||
// function implementation.
|
||||
// Initially we point the stub's function-pointer at the compile callback
|
||||
// that we just created. In the compile action for the callback (see below)
|
||||
// we will update the stub's function pointer to point at the function
|
||||
// implementation that we just implemented.
|
||||
if (auto Err = IndirectStubsMgr->createStub(mangle(FnAST->getName()),
|
||||
CCInfo.getAddress(),
|
||||
JITSymbolFlags::Exported))
|
||||
return Err;
|
||||
|
||||
// Move ownership of FnAST to a shared pointer - C++11 lambdas don't support
|
||||
// capture-by-move, which is be required for unique_ptr.
|
||||
auto SharedFnAST = std::shared_ptr<FunctionAST>(std::move(FnAST));
|
||||
|
||||
// Set the action to compile our AST. This lambda will be run if/when
|
||||
// execution hits the compile callback (via the stub).
|
||||
//
|
||||
// The steps to compile are:
|
||||
// (1) IRGen the function.
|
||||
// (2) Add the IR module to the JIT to make it executable like any other
|
||||
// module.
|
||||
// (3) Use findSymbol to get the address of the compiled function.
|
||||
// (4) Update the stub pointer to point at the implementation so that
|
||||
/// subsequent calls go directly to it and bypass the compiler.
|
||||
// (5) Return the address of the implementation: this lambda will actually
|
||||
// be run inside an attempted call to the function, and we need to
|
||||
// continue on to the implementation to complete the attempted call.
|
||||
// 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
|
||||
// CPU state to what it was immediately before the call.
|
||||
CCInfo.setCompileAction(
|
||||
[this, SharedFnAST]() {
|
||||
auto M = irgenAndTakeOwnership(*SharedFnAST, "$impl");
|
||||
addModule(std::move(M));
|
||||
auto Sym = findSymbol(SharedFnAST->getName() + "$impl");
|
||||
assert(Sym && "Couldn't find compiled function?");
|
||||
TargetAddress SymAddr = Sym.getAddress();
|
||||
if (auto Err =
|
||||
IndirectStubsMgr->updatePointer(mangle(SharedFnAST->getName()),
|
||||
SymAddr)) {
|
||||
logAllUnhandledErrors(std::move(Err), errs(),
|
||||
"Error updating function pointer: ");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return SymAddr;
|
||||
});
|
||||
|
||||
return Error::success();
|
||||
}
|
||||
|
||||
Error executeRemoteExpr(TargetAddress ExprAddr) {
|
||||
return Remote.callVoidVoid(ExprAddr);
|
||||
}
|
||||
|
||||
JITSymbol findSymbol(const std::string Name) {
|
||||
return OptimizeLayer.findSymbol(mangle(Name), true);
|
||||
}
|
||||
|
||||
void removeModule(ModuleHandle H) {
|
||||
OptimizeLayer.removeModuleSet(H);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::string mangle(const std::string &Name) {
|
||||
std::string MangledName;
|
||||
raw_string_ostream MangledNameStream(MangledName);
|
||||
Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
|
||||
return MangledNameStream.str();
|
||||
}
|
||||
|
||||
std::unique_ptr<Module> optimizeModule(std::unique_ptr<Module> M) {
|
||||
// Create a function pass manager.
|
||||
auto FPM = llvm::make_unique<legacy::FunctionPassManager>(M.get());
|
||||
|
||||
// Add some optimizations.
|
||||
FPM->add(createInstructionCombiningPass());
|
||||
FPM->add(createReassociatePass());
|
||||
FPM->add(createGVNPass());
|
||||
FPM->add(createCFGSimplificationPass());
|
||||
FPM->doInitialization();
|
||||
|
||||
// Run the optimizations over all functions in the module being added to
|
||||
// the JIT.
|
||||
for (auto &F : *M)
|
||||
FPM->run(F);
|
||||
|
||||
return M;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
} // end namespace orc
|
||||
} // end namespace llvm
|
||||
|
||||
#endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
|
|
@ -0,0 +1,74 @@
|
|||
//===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Utilities for remote-JITing with LLI.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_TOOLS_LLI_REMOTEJITUTILS_H
|
||||
#define LLVM_TOOLS_LLI_REMOTEJITUTILS_H
|
||||
|
||||
#include "llvm/ExecutionEngine/Orc/RPCChannel.h"
|
||||
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
|
||||
#include <mutex>
|
||||
|
||||
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
/// RPC channel that reads from and writes from file descriptors.
|
||||
class FDRPCChannel final : public llvm::orc::remote::RPCChannel {
|
||||
public:
|
||||
FDRPCChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
|
||||
|
||||
llvm::Error readBytes(char *Dst, unsigned Size) override {
|
||||
assert(Dst && "Attempt to read into null.");
|
||||
ssize_t Completed = 0;
|
||||
while (Completed < static_cast<ssize_t>(Size)) {
|
||||
ssize_t Read = ::read(InFD, Dst + Completed, Size - Completed);
|
||||
if (Read <= 0) {
|
||||
auto ErrNo = errno;
|
||||
if (ErrNo == EAGAIN || ErrNo == EINTR)
|
||||
continue;
|
||||
else
|
||||
return llvm::errorCodeToError(
|
||||
std::error_code(errno, std::generic_category()));
|
||||
}
|
||||
Completed += Read;
|
||||
}
|
||||
return llvm::Error::success();
|
||||
}
|
||||
|
||||
llvm::Error appendBytes(const char *Src, unsigned Size) override {
|
||||
assert(Src && "Attempt to append from null.");
|
||||
ssize_t Completed = 0;
|
||||
while (Completed < static_cast<ssize_t>(Size)) {
|
||||
ssize_t Written = ::write(OutFD, Src + Completed, Size - Completed);
|
||||
if (Written < 0) {
|
||||
auto ErrNo = errno;
|
||||
if (ErrNo == EAGAIN || ErrNo == EINTR)
|
||||
continue;
|
||||
else
|
||||
return llvm::errorCodeToError(
|
||||
std::error_code(errno, std::generic_category()));
|
||||
}
|
||||
Completed += Written;
|
||||
}
|
||||
return llvm::Error::success();
|
||||
}
|
||||
|
||||
llvm::Error send() override { return llvm::Error::success(); }
|
||||
|
||||
private:
|
||||
int InFD, OutFD;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,17 @@
|
|||
set(LLVM_LINK_COMPONENTS
|
||||
Analysis
|
||||
Core
|
||||
ExecutionEngine
|
||||
InstCombine
|
||||
Object
|
||||
OrcJIT
|
||||
RuntimeDyld
|
||||
ScalarOpts
|
||||
Support
|
||||
TransformUtils
|
||||
native
|
||||
)
|
||||
|
||||
add_kaleidoscope_chapter(BuildingAJIT-Ch5-Server
|
||||
server.cpp
|
||||
)
|
|
@ -0,0 +1,119 @@
|
|||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/DynamicLibrary.h"
|
||||
#include "llvm/Support/TargetSelect.h"
|
||||
#include "llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h"
|
||||
#include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
|
||||
|
||||
#include "../RemoteJITUtils.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
|
||||
using namespace llvm;
|
||||
using namespace llvm::orc;
|
||||
|
||||
// Command line argument for TCP port.
|
||||
cl::opt<uint32_t> Port("port",
|
||||
cl::desc("TCP port to listen on"),
|
||||
cl::init(20000));
|
||||
|
||||
ExitOnError ExitOnErr;
|
||||
|
||||
typedef int (*MainFun)(int, const char*[]);
|
||||
|
||||
template <typename NativePtrT>
|
||||
NativePtrT MakeNative(uint64_t P) {
|
||||
return reinterpret_cast<NativePtrT>(static_cast<uintptr_t>(P));
|
||||
}
|
||||
|
||||
extern "C"
|
||||
void printExprResult(double Val) {
|
||||
printf("Expression evaluated to: %f\n", Val);
|
||||
}
|
||||
|
||||
// --- LAZY COMPILE TEST ---
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
if (argc == 0)
|
||||
ExitOnErr.setBanner("jit_server: ");
|
||||
else
|
||||
ExitOnErr.setBanner(std::string(argv[0]) + ": ");
|
||||
|
||||
// --- Initialize LLVM ---
|
||||
cl::ParseCommandLineOptions(argc, argv, "LLVM lazy JIT example.\n");
|
||||
|
||||
InitializeNativeTarget();
|
||||
InitializeNativeTargetAsmPrinter();
|
||||
InitializeNativeTargetAsmParser();
|
||||
|
||||
if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
|
||||
errs() << "Error loading program symbols.\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
// --- Initialize remote connection ---
|
||||
|
||||
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
sockaddr_in servAddr, clientAddr;
|
||||
socklen_t clientAddrLen = sizeof(clientAddr);
|
||||
bzero(&servAddr, sizeof(servAddr));
|
||||
servAddr.sin_family = PF_INET;
|
||||
servAddr.sin_family = INADDR_ANY;
|
||||
servAddr.sin_port = htons(Port);
|
||||
|
||||
{
|
||||
// avoid "Address already in use" error.
|
||||
int yes=1;
|
||||
if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
|
||||
errs() << "Error calling setsockopt.\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (bind(sockfd, reinterpret_cast<sockaddr*>(&servAddr),
|
||||
sizeof(servAddr)) < 0) {
|
||||
errs() << "Error on binding.\n";
|
||||
return 1;
|
||||
}
|
||||
listen(sockfd, 1);
|
||||
int newsockfd = accept(sockfd, reinterpret_cast<sockaddr*>(&clientAddr),
|
||||
&clientAddrLen);
|
||||
|
||||
auto SymbolLookup =
|
||||
[](const std::string &Name) {
|
||||
return RTDyldMemoryManager::getSymbolAddressInProcess(Name);
|
||||
};
|
||||
|
||||
auto RegisterEHFrames =
|
||||
[](uint8_t *Addr, uint32_t Size) {
|
||||
RTDyldMemoryManager::registerEHFramesInProcess(Addr, Size);
|
||||
};
|
||||
|
||||
auto DeregisterEHFrames =
|
||||
[](uint8_t *Addr, uint32_t Size) {
|
||||
RTDyldMemoryManager::deregisterEHFramesInProcess(Addr, Size);
|
||||
};
|
||||
|
||||
FDRPCChannel TCPChannel(newsockfd, newsockfd);
|
||||
typedef remote::OrcRemoteTargetServer<FDRPCChannel, OrcX86_64_SysV> MyServerT;
|
||||
|
||||
MyServerT Server(TCPChannel, SymbolLookup, RegisterEHFrames, DeregisterEHFrames);
|
||||
|
||||
while (1) {
|
||||
MyServerT::JITFuncId Id = MyServerT::InvalidId;
|
||||
ExitOnErr(Server.startReceivingFunction(TCPChannel, (uint32_t&)Id));
|
||||
switch (Id) {
|
||||
case MyServerT::TerminateSessionId:
|
||||
ExitOnErr(Server.handleTerminateSession());
|
||||
return 0;
|
||||
default:
|
||||
ExitOnErr(Server.handleKnownFunction(Id));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
llvm_unreachable("Fell through server command loop.");
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue