2016-01-11 17:35:55 +01:00
|
|
|
//===-- 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
|
|
|
|
|
2016-11-11 22:42:09 +01:00
|
|
|
#include "llvm/ExecutionEngine/Orc/RawByteChannel.h"
|
2016-01-11 17:35:55 +01:00
|
|
|
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
|
2016-04-18 21:55:43 +02:00
|
|
|
#include <mutex>
|
2016-01-11 17:35:55 +01:00
|
|
|
|
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// RPC channel that reads from and writes from file descriptors.
|
2016-11-11 22:42:09 +01:00
|
|
|
class FDRawChannel final : public llvm::orc::rpc::RawByteChannel {
|
2016-01-11 17:35:55 +01:00
|
|
|
public:
|
2016-11-11 22:42:09 +01:00
|
|
|
FDRawChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
|
2016-01-11 17:35:55 +01:00
|
|
|
|
2016-04-25 21:56:45 +02:00
|
|
|
llvm::Error readBytes(char *Dst, unsigned Size) override {
|
2016-01-11 17:35:55 +01:00
|
|
|
assert(Dst && "Attempt to read into null.");
|
2016-04-25 21:56:45 +02:00
|
|
|
ssize_t Completed = 0;
|
2016-04-26 03:45:25 +02:00
|
|
|
while (Completed < static_cast<ssize_t>(Size)) {
|
2016-04-25 21:56:45 +02:00
|
|
|
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();
|
2016-01-11 17:35:55 +01:00
|
|
|
}
|
|
|
|
|
2016-04-25 21:56:45 +02:00
|
|
|
llvm::Error appendBytes(const char *Src, unsigned Size) override {
|
2016-01-11 17:35:55 +01:00
|
|
|
assert(Src && "Attempt to append from null.");
|
2016-04-25 21:56:45 +02:00
|
|
|
ssize_t Completed = 0;
|
2016-04-26 03:45:25 +02:00
|
|
|
while (Completed < static_cast<ssize_t>(Size)) {
|
2016-04-25 21:56:45 +02:00
|
|
|
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();
|
2016-01-11 17:35:55 +01:00
|
|
|
}
|
|
|
|
|
2016-04-25 21:56:45 +02:00
|
|
|
llvm::Error send() override { return llvm::Error::success(); }
|
2016-01-11 17:35:55 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
int InFD, OutFD;
|
|
|
|
};
|
|
|
|
|
|
|
|
// launch the remote process (see lli.cpp) and return a channel to it.
|
2016-11-11 22:42:09 +01:00
|
|
|
std::unique_ptr<FDRawChannel> launchRemote();
|
2016-01-11 17:35:55 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2016-11-11 22:42:09 +01:00
|
|
|
// ForwardingMM - Adapter to connect MCJIT to Orc's Remote8
|
|
|
|
// memory manager.
|
2016-01-11 17:35:55 +01:00
|
|
|
class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {
|
|
|
|
public:
|
|
|
|
void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {
|
|
|
|
this->MemMgr = std::move(MemMgr);
|
|
|
|
}
|
|
|
|
|
2017-07-04 06:42:30 +02:00
|
|
|
void setResolver(std::shared_ptr<JITSymbolResolver> Resolver) {
|
2016-01-11 17:35:55 +01:00
|
|
|
this->Resolver = std::move(Resolver);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
|
|
|
|
unsigned SectionID,
|
|
|
|
StringRef SectionName) override {
|
|
|
|
return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
|
|
|
|
unsigned SectionID, StringRef SectionName,
|
|
|
|
bool IsReadOnly) override {
|
|
|
|
return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,
|
|
|
|
IsReadOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
|
|
|
|
uintptr_t RODataSize, uint32_t RODataAlign,
|
|
|
|
uintptr_t RWDataSize,
|
|
|
|
uint32_t RWDataAlign) override {
|
|
|
|
MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
|
|
|
|
RWDataSize, RWDataAlign);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool needsToReserveAllocationSpace() override {
|
|
|
|
return MemMgr->needsToReserveAllocationSpace();
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
|
|
|
|
size_t Size) override {
|
|
|
|
MemMgr->registerEHFrames(Addr, LoadAddr, Size);
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:32:18 +02:00
|
|
|
void deregisterEHFrames() override {
|
|
|
|
MemMgr->deregisterEHFrames();
|
2016-01-11 17:35:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool finalizeMemory(std::string *ErrMsg = nullptr) override {
|
|
|
|
return MemMgr->finalizeMemory(ErrMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void notifyObjectLoaded(RuntimeDyld &RTDyld,
|
|
|
|
const object::ObjectFile &Obj) override {
|
|
|
|
MemMgr->notifyObjectLoaded(RTDyld, Obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
|
|
|
|
using RTDyldMemoryManager::notifyObjectLoaded;
|
|
|
|
|
2016-08-01 22:49:11 +02:00
|
|
|
JITSymbol findSymbol(const std::string &Name) override {
|
2016-01-11 17:35:55 +01:00
|
|
|
return Resolver->findSymbol(Name);
|
|
|
|
}
|
|
|
|
|
2016-08-01 22:49:11 +02:00
|
|
|
JITSymbol
|
2016-01-11 17:35:55 +01:00
|
|
|
findSymbolInLogicalDylib(const std::string &Name) override {
|
|
|
|
return Resolver->findSymbolInLogicalDylib(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;
|
2017-07-04 06:42:30 +02:00
|
|
|
std::shared_ptr<JITSymbolResolver> Resolver;
|
2016-01-11 17:35:55 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|