<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 15, 2016 at 11:26 AM, Aaron Ballman <span dir="ltr"><<a href="mailto:aaron@aaronballman.com" target="_blank">aaron@aaronballman.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Fri, Jan 15, 2016 at 2:23 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>> wrote:<br>
><br>
><br>
> On Fri, Jan 15, 2016 at 9:21 AM, Aaron Ballman <<a href="mailto:aaron@aaronballman.com">aaron@aaronballman.com</a>><br>
> wrote:<br>
>><br>
>> On Fri, Jan 15, 2016 at 12:12 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>><br>
>> wrote:<br>
>> ><br>
>> ><br>
>> > On Fri, Jan 15, 2016 at 8:33 AM, Aaron Ballman via llvm-commits<br>
>> > <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br>
>> >><br>
>> >> On Sun, Jan 10, 2016 at 8:40 PM, Lang Hames via llvm-commits<br>
>> >> <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br>
>> >> > Author: lhames<br>
>> >> > Date: Sun Jan 10 19:40:11 2016<br>
>> >> > New Revision: 257305<br>
>> >> ><br>
>> >> > URL: <a href="http://llvm.org/viewvc/llvm-project?rev=257305&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=257305&view=rev</a><br>
>> >> > Log:<br>
>> >> > [Orc] Add support for remote JITing to the ORC API.<br>
>> >> ><br>
>> >> > This patch adds utilities to ORC for managing a remote JIT target. It<br>
>> >> > consists<br>
>> >> > of:<br>
>> >> ><br>
>> >> > 1. A very primitive RPC system for making calls over a byte-stream.<br>
>> >> > See<br>
>> >> > RPCChannel.h, RPCUtils.h.<br>
>> >> ><br>
>> >> > 2. An RPC API defined in the above system for managing memory,<br>
>> >> > looking<br>
>> >> > up<br>
>> >> > symbols, creating stubs, etc. on a remote target. See<br>
>> >> > OrcRemoteTargetRPCAPI.h.<br>
>> >> ><br>
>> >> > 3. An interface for creating high-level JIT components (memory<br>
>> >> > managers,<br>
>> >> > callback managers, stub managers, etc.) that operate over the RPC<br>
>> >> > API.<br>
>> >> > See<br>
>> >> > OrcRemoteTargetClient.h.<br>
>> >> ><br>
>> >> > 4. A helper class for building servers that can handle the RPC calls.<br>
>> >> > See<br>
>> >> > OrcRemoteTargetServer.h.<br>
>> >> ><br>
>> >> > The system is designed to work neatly with the existing ORC<br>
>> >> > components<br>
>> >> > and<br>
>> >> > functionality. In particular, the ORC callback API (and consequently<br>
>> >> > the<br>
>> >> > CompileOnDemandLayer) is supported, enabling lazy compilation of<br>
>> >> > remote<br>
>> >> > code.<br>
>> >> ><br>
>> >> > Assuming this doesn't trigger any builder failures, a follow-up patch<br>
>> >> > will be<br>
>> >> > committed which tests these utilities by using them to replace LLI's<br>
>> >> > existing<br>
>> >> > remote-JITing demo code.<br>
>> >> ><br>
>> >> ><br>
>> >> > Added:<br>
>> >> ><br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h<br>
>> >> ><br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h<br>
>> >> ><br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/RPCChannel.h<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/RPCUtils.h<br>
>> >> > llvm/trunk/lib/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.cpp<br>
>> >> > llvm/trunk/unittests/ExecutionEngine/Orc/RPCUtilsTest.cpp<br>
>> >> > Modified:<br>
>> >> > llvm/trunk/lib/ExecutionEngine/Orc/CMakeLists.txt<br>
>> >> > llvm/trunk/unittests/ExecutionEngine/Orc/CMakeLists.txt<br>
>> >> ><br>
>> >> > Added:<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h<br>
>> >> > URL:<br>
>> >> ><br>
>> >> > <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h?rev=257305&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h?rev=257305&view=auto</a><br>
>> >> ><br>
>> >> ><br>
>> >> > ==============================================================================<br>
>> >> > ---<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h<br>
>> >> > (added)<br>
>> >> > +++<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h<br>
>> >> > Sun Jan 10 19:40:11 2016<br>
>> >> > @@ -0,0 +1,743 @@<br>
>> >> > +//===---- OrcRemoteTargetClient.h - Orc Remote-target Client ----*-<br>
>> >> > C++<br>
>> >> > -*-===//<br>
>> >> > +//<br>
>> >> > +// The LLVM Compiler Infrastructure<br>
>> >> > +//<br>
>> >> > +// This file is distributed under the University of Illinois Open<br>
>> >> > Source<br>
>> >> > +// License. See LICENSE.TXT for details.<br>
>> >> > +//<br>
>> >> ><br>
>> >> ><br>
>> >> > +//===----------------------------------------------------------------------===//<br>
>> >> > +//<br>
>> >> > +// This file defines the OrcRemoteTargetClient class and helpers.<br>
>> >> > This<br>
>> >> > class<br>
>> >> > +// can be used to communicate over an RPCChannel with an<br>
>> >> > OrcRemoteTargetServer<br>
>> >> > +// instance to support remote-JITing.<br>
>> >> > +//<br>
>> >> ><br>
>> >> ><br>
>> >> > +//===----------------------------------------------------------------------===//<br>
>> >> > +<br>
>> >> > +#ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H<br>
>> >> > +#define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H<br>
>> >> > +<br>
>> >> > +#include "OrcRemoteTargetRPCAPI.h"<br>
>> >> > +<br>
>> >> > +#define DEBUG_TYPE "orc-remote"<br>
>> >> > +<br>
>> >> > +namespace llvm {<br>
>> >> > +namespace orc {<br>
>> >> > +namespace remote {<br>
>> >> > +<br>
>> >> > +/// This class provides utilities (including memory manager,<br>
>> >> > indirect<br>
>> >> > stubs<br>
>> >> > +/// manager, and compile callback manager types) that support remote<br>
>> >> > JITing<br>
>> >> > +/// in ORC.<br>
>> >> > +///<br>
>> >> > +/// Each of the utility classes talks to a JIT server (an instance<br>
>> >> > of<br>
>> >> > the<br>
>> >> > +/// OrcRemoteTargetServer class) via an RPC system (see RPCUtils.h)<br>
>> >> > to<br>
>> >> > carry out<br>
>> >> > +/// its actions.<br>
>> >> > +template <typename ChannelT><br>
>> >> > +class OrcRemoteTargetClient : public OrcRemoteTargetRPCAPI {<br>
>> >> > +public:<br>
>> >> > + /// Remote memory manager.<br>
>> >> > + class RCMemoryManager : public RuntimeDyld::MemoryManager {<br>
>> >> > + public:<br>
>> >> > + RCMemoryManager(OrcRemoteTargetClient &Client,<br>
>> >> > ResourceIdMgr::ResourceId Id)<br>
>> >> > + : Client(Client), Id(Id) {<br>
>> >> > + DEBUG(dbgs() << "Created remote allocator " << Id << "\n");<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + ~RCMemoryManager() {<br>
>> >> > + Client.destroyRemoteAllocator(Id);<br>
>> >> > + DEBUG(dbgs() << "Destroyed remote allocator " << Id << "\n");<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,<br>
>> >> > + unsigned SectionID,<br>
>> >> > + StringRef SectionName) override {<br>
>> >> > + Unmapped.back().CodeAllocs.emplace_back(Size, Alignment);<br>
>> >> > + uint8_t *Alloc = reinterpret_cast<uint8_t *>(<br>
>> >> > + Unmapped.back().CodeAllocs.back().getLocalAddress());<br>
>> >> > + DEBUG(dbgs() << "Allocator " << Id << " allocated code for "<br>
>> >> > + << SectionName << ": " << Alloc << " (" << Size<br>
>> >> > + << " bytes, alignment " << Alignment << ")\n");<br>
>> >> > + return Alloc;<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,<br>
>> >> > + unsigned SectionID, StringRef<br>
>> >> > SectionName,<br>
>> >> > + bool IsReadOnly) override {<br>
>> >> > + if (IsReadOnly) {<br>
>> >> > + Unmapped.back().RODataAllocs.emplace_back(Size, Alignment);<br>
>> >> > + uint8_t *Alloc = reinterpret_cast<uint8_t *>(<br>
>> >> > + Unmapped.back().RODataAllocs.back().getLocalAddress());<br>
>> >> > + DEBUG(dbgs() << "Allocator " << Id << " allocated ro-data<br>
>> >> > for "<br>
>> >> > + << SectionName << ": " << Alloc << " (" << Size<br>
>> >> > + << " bytes, alignment " << Alignment << ")\n");<br>
>> >> > + return Alloc;<br>
>> >> > + } // else...<br>
>> >> > +<br>
>> >> > + Unmapped.back().RWDataAllocs.emplace_back(Size, Alignment);<br>
>> >> > + uint8_t *Alloc = reinterpret_cast<uint8_t *>(<br>
>> >> > + Unmapped.back().RWDataAllocs.back().getLocalAddress());<br>
>> >> > + DEBUG(dbgs() << "Allocator " << Id << " allocated rw-data for<br>
>> >> > "<br>
>> >> > + << SectionName << ": " << Alloc << " (" << Size<br>
>> >> > + << " bytes, alignment " << Alignment << "\n");<br>
>> >> > + return Alloc;<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + void reserveAllocationSpace(uintptr_t CodeSize, uint32_t<br>
>> >> > CodeAlign,<br>
>> >> > + uintptr_t RODataSize, uint32_t<br>
>> >> > RODataAlign,<br>
>> >> > + uintptr_t RWDataSize,<br>
>> >> > + uint32_t RWDataAlign) override {<br>
>> >> > + Unmapped.push_back(ObjectAllocs());<br>
>> >> > +<br>
>> >> > + DEBUG(dbgs() << "Allocator " << Id << " reserved:\n");<br>
>> >> > +<br>
>> >> > + if (CodeSize != 0) {<br>
>> >> > + if (auto EC =<br>
>> >> > Client.reserveMem(Unmapped.back().RemoteCodeAddr,<br>
>> >> > Id,<br>
>> >> > + CodeSize, CodeAlign)) {<br>
>> >> > + // FIXME; Add error to poll.<br>
>> >> > + llvm_unreachable("Failed reserving remote memory.");<br>
>> >> > + }<br>
>> >> > + DEBUG(dbgs() << " code: "<br>
>> >> > + << format("0x%016x",<br>
>> >> > Unmapped.back().RemoteCodeAddr)<br>
>> >> > + << " (" << CodeSize << " bytes, alignment " <<<br>
>> >> > CodeAlign<br>
>> >> > + << ")\n");<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + if (RODataSize != 0) {<br>
>> >> > + if (auto EC =<br>
>> >> > Client.reserveMem(Unmapped.back().RemoteRODataAddr, Id,<br>
>> >> > + RODataSize, RODataAlign)) {<br>
>> >> > + // FIXME; Add error to poll.<br>
>> >> > + llvm_unreachable("Failed reserving remote memory.");<br>
>> >> > + }<br>
>> >> > + DEBUG(dbgs() << " ro-data: "<br>
>> >> > + << format("0x%016x",<br>
>> >> > Unmapped.back().RemoteRODataAddr)<br>
>> >> > + << " (" << RODataSize << " bytes, alignment "<br>
>> >> > + << RODataAlign << ")\n");<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + if (RWDataSize != 0) {<br>
>> >> > + if (auto EC =<br>
>> >> > Client.reserveMem(Unmapped.back().RemoteRWDataAddr, Id,<br>
>> >> > + RWDataSize, RWDataAlign)) {<br>
>> >> > + // FIXME; Add error to poll.<br>
>> >> > + llvm_unreachable("Failed reserving remote memory.");<br>
>> >> > + }<br>
>> >> > + DEBUG(dbgs() << " rw-data: "<br>
>> >> > + << format("0x%016x",<br>
>> >> > Unmapped.back().RemoteRWDataAddr)<br>
>> >> > + << " (" << RWDataSize << " bytes, alignment "<br>
>> >> > + << RWDataAlign << ")\n");<br>
>> >> > + }<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + bool needsToReserveAllocationSpace() override { return true; }<br>
>> >> > +<br>
>> >> > + void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,<br>
>> >> > + size_t Size) override {}<br>
>> >> > +<br>
>> >> > + void deregisterEHFrames(uint8_t *addr, uint64_t LoadAddr,<br>
>> >> > + size_t Size) override {}<br>
>> >> > +<br>
>> >> > + void notifyObjectLoaded(RuntimeDyld &Dyld,<br>
>> >> > + const object::ObjectFile &Obj) override<br>
>> >> > {<br>
>> >> > + DEBUG(dbgs() << "Allocator " << Id << " applied mappings:\n");<br>
>> >> > + for (auto &ObjAllocs : Unmapped) {<br>
>> >> > + {<br>
>> >> > + TargetAddress NextCodeAddr = ObjAllocs.RemoteCodeAddr;<br>
>> >> > + for (auto &Alloc : ObjAllocs.CodeAllocs) {<br>
>> >> > + NextCodeAddr = RoundUpToAlignment(NextCodeAddr,<br>
>> >> > Alloc.getAlign());<br>
>> >> > + Dyld.mapSectionAddress(Alloc.getLocalAddress(),<br>
>> >> > NextCodeAddr);<br>
>> >> > + DEBUG(dbgs() << " code: "<br>
>> >> > + << static_cast<void<br>
>> >> > *>(Alloc.getLocalAddress())<br>
>> >> > + << " -> " << format("0x%016x",<br>
>> >> > NextCodeAddr)<br>
>> >> > << "\n");<br>
>> >> > + Alloc.setRemoteAddress(NextCodeAddr);<br>
>> >> > + NextCodeAddr += Alloc.getSize();<br>
>> >> > + }<br>
>> >> > + }<br>
>> >> > + {<br>
>> >> > + TargetAddress NextRODataAddr = ObjAllocs.RemoteRODataAddr;<br>
>> >> > + for (auto &Alloc : ObjAllocs.RODataAllocs) {<br>
>> >> > + NextRODataAddr =<br>
>> >> > + RoundUpToAlignment(NextRODataAddr,<br>
>> >> > Alloc.getAlign());<br>
>> >> > + Dyld.mapSectionAddress(Alloc.getLocalAddress(),<br>
>> >> > NextRODataAddr);<br>
>> >> > + DEBUG(dbgs() << " ro-data: "<br>
>> >> > + << static_cast<void<br>
>> >> > *>(Alloc.getLocalAddress())<br>
>> >> > + << " -> " << format("0x%016x",<br>
>> >> > NextRODataAddr)<br>
>> >> > + << "\n");<br>
>> >> > + Alloc.setRemoteAddress(NextRODataAddr);<br>
>> >> > + NextRODataAddr += Alloc.getSize();<br>
>> >> > + }<br>
>> >> > + }<br>
>> >> > + {<br>
>> >> > + TargetAddress NextRWDataAddr = ObjAllocs.RemoteRWDataAddr;<br>
>> >> > + for (auto &Alloc : ObjAllocs.RWDataAllocs) {<br>
>> >> > + NextRWDataAddr =<br>
>> >> > + RoundUpToAlignment(NextRWDataAddr,<br>
>> >> > Alloc.getAlign());<br>
>> >> > + Dyld.mapSectionAddress(Alloc.getLocalAddress(),<br>
>> >> > NextRWDataAddr);<br>
>> >> > + DEBUG(dbgs() << " rw-data: "<br>
>> >> > + << static_cast<void<br>
>> >> > *>(Alloc.getLocalAddress())<br>
>> >> > + << " -> " << format("0x%016x",<br>
>> >> > NextRWDataAddr)<br>
>> >> > + << "\n");<br>
>> >> > + Alloc.setRemoteAddress(NextRWDataAddr);<br>
>> >> > + NextRWDataAddr += Alloc.getSize();<br>
>> >> > + }<br>
>> >> > + }<br>
>> >> > + Unfinalized.push_back(std::move(ObjAllocs));<br>
>> >> > + }<br>
>> >> > + Unmapped.clear();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + bool finalizeMemory(std::string *ErrMsg = nullptr) override {<br>
>> >> > + DEBUG(dbgs() << "Allocator " << Id << " finalizing:\n");<br>
>> >> > +<br>
>> >> > + for (auto &ObjAllocs : Unfinalized) {<br>
>> >> > +<br>
>> >> > + for (auto &Alloc : ObjAllocs.CodeAllocs) {<br>
>> >> > + DEBUG(dbgs() << " copying code: "<br>
>> >> > + << static_cast<void<br>
>> >> > *>(Alloc.getLocalAddress())<br>
>> >> > << " -> "<br>
>> >> > + << format("0x%016x",<br>
>> >> > Alloc.getRemoteAddress())<br>
>> >> > << " ("<br>
>> >> > + << Alloc.getSize() << " bytes)\n");<br>
>> >> > + Client.writeMem(Alloc.getRemoteAddress(),<br>
>> >> > Alloc.getLocalAddress(),<br>
>> >> > + Alloc.getSize());<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + if (ObjAllocs.RemoteCodeAddr) {<br>
>> >> > + DEBUG(dbgs() << " setting R-X permissions on code block:<br>
>> >> > "<br>
>> >> > + << format("0x%016x",<br>
>> >> > ObjAllocs.RemoteCodeAddr)<br>
>> >> > << "\n");<br>
>> >> > + Client.setProtections(Id, ObjAllocs.RemoteCodeAddr,<br>
>> >> > + sys::Memory::MF_READ |<br>
>> >> > sys::Memory::MF_EXEC);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + for (auto &Alloc : ObjAllocs.RODataAllocs) {<br>
>> >> > + DEBUG(dbgs() << " copying ro-data: "<br>
>> >> > + << static_cast<void<br>
>> >> > *>(Alloc.getLocalAddress())<br>
>> >> > << " -> "<br>
>> >> > + << format("0x%016x",<br>
>> >> > Alloc.getRemoteAddress())<br>
>> >> > << " ("<br>
>> >> > + << Alloc.getSize() << " bytes)\n");<br>
>> >> > + Client.writeMem(Alloc.getRemoteAddress(),<br>
>> >> > Alloc.getLocalAddress(),<br>
>> >> > + Alloc.getSize());<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + if (ObjAllocs.RemoteRODataAddr) {<br>
>> >> > + DEBUG(dbgs() << " setting R-- permissions on ro-data<br>
>> >> > block:<br>
>> >> > "<br>
>> >> > + << format("0x%016x",<br>
>> >> > ObjAllocs.RemoteRODataAddr)<br>
>> >> > + << "\n");<br>
>> >> > + Client.setProtections(Id, ObjAllocs.RemoteRODataAddr,<br>
>> >> > + sys::Memory::MF_READ);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + for (auto &Alloc : ObjAllocs.RWDataAllocs) {<br>
>> >> > + DEBUG(dbgs() << " copying rw-data: "<br>
>> >> > + << static_cast<void<br>
>> >> > *>(Alloc.getLocalAddress())<br>
>> >> > << " -> "<br>
>> >> > + << format("0x%016x",<br>
>> >> > Alloc.getRemoteAddress())<br>
>> >> > << " ("<br>
>> >> > + << Alloc.getSize() << " bytes)\n");<br>
>> >> > + Client.writeMem(Alloc.getRemoteAddress(),<br>
>> >> > Alloc.getLocalAddress(),<br>
>> >> > + Alloc.getSize());<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + if (ObjAllocs.RemoteRWDataAddr) {<br>
>> >> > + DEBUG(dbgs() << " setting RW- permissions on rw-data<br>
>> >> > block:<br>
>> >> > "<br>
>> >> > + << format("0x%016x",<br>
>> >> > ObjAllocs.RemoteRWDataAddr)<br>
>> >> > + << "\n");<br>
>> >> > + Client.setProtections(Id, ObjAllocs.RemoteRWDataAddr,<br>
>> >> > + sys::Memory::MF_READ |<br>
>> >> > sys::Memory::MF_WRITE);<br>
>> >> > + }<br>
>> >> > + }<br>
>> >> > + Unfinalized.clear();<br>
>> >> > +<br>
>> >> > + return false;<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + private:<br>
>> >> > + class Alloc {<br>
>> >> > + public:<br>
>> >> > + Alloc(uint64_t Size, unsigned Align)<br>
>> >> > + : Size(Size), Align(Align), Contents(new char[Size + Align<br>
>> >> > -<br>
>> >> > 1]),<br>
>> >> > + RemoteAddr(0) {}<br>
>> >> > +<br>
>> >> > + uint64_t getSize() const { return Size; }<br>
>> >> > +<br>
>> >> > + unsigned getAlign() const { return Align; }<br>
>> >> > +<br>
>> >> > + char *getLocalAddress() const {<br>
>> >> > + uintptr_t LocalAddr =<br>
>> >> > reinterpret_cast<uintptr_t>(Contents.get());<br>
>> >> > + LocalAddr = RoundUpToAlignment(LocalAddr, Align);<br>
>> >> > + return reinterpret_cast<char *>(LocalAddr);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + void setRemoteAddress(TargetAddress RemoteAddr) {<br>
>> >> > + this->RemoteAddr = RemoteAddr;<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + TargetAddress getRemoteAddress() const { return RemoteAddr; }<br>
>> >> > +<br>
>> >> > + private:<br>
>> >> > + uint64_t Size;<br>
>> >> > + unsigned Align;<br>
>> >> > + std::unique_ptr<char[]> Contents;<br>
>> >> > + TargetAddress RemoteAddr;<br>
>> >> > + };<br>
>> >> > +<br>
>> >> > + struct ObjectAllocs {<br>
>> >> > + ObjectAllocs()<br>
>> >> > + : RemoteCodeAddr(0), RemoteRODataAddr(0),<br>
>> >> > RemoteRWDataAddr(0)<br>
>> >> > {}<br>
>> >> > + TargetAddress RemoteCodeAddr;<br>
>> >> > + TargetAddress RemoteRODataAddr;<br>
>> >> > + TargetAddress RemoteRWDataAddr;<br>
>> >> > + std::vector<Alloc> CodeAllocs, RODataAllocs, RWDataAllocs;<br>
>> >> > + };<br>
>> >> > +<br>
>> >> > + OrcRemoteTargetClient &Client;<br>
>> >> > + ResourceIdMgr::ResourceId Id;<br>
>> >> > + std::vector<ObjectAllocs> Unmapped;<br>
>> >> > + std::vector<ObjectAllocs> Unfinalized;<br>
>> >> > + };<br>
>> >> > +<br>
>> >> > + /// Remote indirect stubs manager.<br>
>> >> > + class RCIndirectStubsManager : public IndirectStubsManager {<br>
>> >> > + public:<br>
>> >> > + RCIndirectStubsManager(OrcRemoteTargetClient &Remote,<br>
>> >> > + ResourceIdMgr::ResourceId Id)<br>
>> >> > + : Remote(Remote), Id(Id) {}<br>
>> >> > +<br>
>> >> > + ~RCIndirectStubsManager() {<br>
>> >> > Remote.destroyIndirectStubsManager(Id);<br>
>> >> > }<br>
>> >> > +<br>
>> >> > + std::error_code createStub(StringRef StubName, TargetAddress<br>
>> >> > StubAddr,<br>
>> >> > + JITSymbolFlags StubFlags) override {<br>
>> >> > + if (auto EC = reserveStubs(1))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + return createStubInternal(StubName, StubAddr, StubFlags);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code createStubs(const StubInitsMap &StubInits)<br>
>> >> > override<br>
>> >> > {<br>
>> >> > + if (auto EC = reserveStubs(StubInits.size()))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + for (auto &Entry : StubInits)<br>
>> >> > + if (auto EC = createStubInternal(Entry.first(),<br>
>> >> > Entry.second.first,<br>
>> >> > + Entry.second.second))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + return std::error_code();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + JITSymbol findStub(StringRef Name, bool ExportedStubsOnly)<br>
>> >> > override<br>
>> >> > {<br>
>> >> > + auto I = StubIndexes.find(Name);<br>
>> >> > + if (I == StubIndexes.end())<br>
>> >> > + return nullptr;<br>
>> >> > + auto Key = I->second.first;<br>
>> >> > + auto Flags = I->second.second;<br>
>> >> > + auto StubSymbol = JITSymbol(getStubAddr(Key), Flags);<br>
>> >> > + if (ExportedStubsOnly && !StubSymbol.isExported())<br>
>> >> > + return nullptr;<br>
>> >> > + return StubSymbol;<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + JITSymbol findPointer(StringRef Name) override {<br>
>> >> > + auto I = StubIndexes.find(Name);<br>
>> >> > + if (I == StubIndexes.end())<br>
>> >> > + return nullptr;<br>
>> >> > + auto Key = I->second.first;<br>
>> >> > + auto Flags = I->second.second;<br>
>> >> > + return JITSymbol(getPtrAddr(Key), Flags);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code updatePointer(StringRef Name,<br>
>> >> > + TargetAddress NewAddr) override {<br>
>> >> > + auto I = StubIndexes.find(Name);<br>
>> >> > + assert(I != StubIndexes.end() && "No stub pointer for<br>
>> >> > symbol");<br>
>> >> > + auto Key = I->second.first;<br>
>> >> > + return Remote.writePointer(getPtrAddr(Key), NewAddr);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + private:<br>
>> >> > + struct RemoteIndirectStubsInfo {<br>
>> >> > + RemoteIndirectStubsInfo(TargetAddress StubBase, TargetAddress<br>
>> >> > PtrBase,<br>
>> >> > + unsigned NumStubs)<br>
>> >> > + : StubBase(StubBase), PtrBase(PtrBase), NumStubs(NumStubs)<br>
>> >> > {}<br>
>> >> > + TargetAddress StubBase;<br>
>> >> > + TargetAddress PtrBase;<br>
>> >> > + unsigned NumStubs;<br>
>> >> > + };<br>
>> >> > +<br>
>> >> > + OrcRemoteTargetClient &Remote;<br>
>> >> > + ResourceIdMgr::ResourceId Id;<br>
>> >> > + std::vector<RemoteIndirectStubsInfo> RemoteIndirectStubsInfos;<br>
>> >> > + typedef std::pair<uint16_t, uint16_t> StubKey;<br>
>> >> > + std::vector<StubKey> FreeStubs;<br>
>> >> > + StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes;<br>
>> >> > +<br>
>> >> > + std::error_code reserveStubs(unsigned NumStubs) {<br>
>> >> > + if (NumStubs <= FreeStubs.size())<br>
>> >> > + return std::error_code();<br>
>> >> > +<br>
>> >> > + unsigned NewStubsRequired = NumStubs - FreeStubs.size();<br>
>> >> > + TargetAddress StubBase;<br>
>> >> > + TargetAddress PtrBase;<br>
>> >> > + unsigned NumStubsEmitted;<br>
>> >> > +<br>
>> >> > + Remote.emitIndirectStubs(StubBase, PtrBase, NumStubsEmitted,<br>
>> >> > Id,<br>
>> >> > + NewStubsRequired);<br>
>> >> > +<br>
>> >> > + unsigned NewBlockId = RemoteIndirectStubsInfos.size();<br>
>> >> > + RemoteIndirectStubsInfos.push_back(<br>
>> >> > + RemoteIndirectStubsInfo(StubBase, PtrBase,<br>
>> >> > NumStubsEmitted));<br>
>> >> > +<br>
>> >> > + for (unsigned I = 0; I < NumStubsEmitted; ++I)<br>
>> >> > + FreeStubs.push_back(std::make_pair(NewBlockId, I));<br>
>> >> > +<br>
>> >> > + return std::error_code();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code createStubInternal(StringRef StubName,<br>
>> >> > + TargetAddress InitAddr,<br>
>> >> > + JITSymbolFlags StubFlags) {<br>
>> >> > + auto Key = FreeStubs.back();<br>
>> >> > + FreeStubs.pop_back();<br>
>> >> > + StubIndexes[StubName] = std::make_pair(Key, StubFlags);<br>
>> >> > + return Remote.writePointer(getPtrAddr(Key), InitAddr);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + TargetAddress getStubAddr(StubKey K) {<br>
>> >> > + assert(RemoteIndirectStubsInfos[K.first].StubBase != 0 &&<br>
>> >> > + "Missing stub address");<br>
>> >> > + return RemoteIndirectStubsInfos[K.first].StubBase +<br>
>> >> > + K.second * Remote.getIndirectStubSize();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + TargetAddress getPtrAddr(StubKey K) {<br>
>> >> > + assert(RemoteIndirectStubsInfos[K.first].PtrBase != 0 &&<br>
>> >> > + "Missing pointer address");<br>
>> >> > + return RemoteIndirectStubsInfos[K.first].PtrBase +<br>
>> >> > + K.second * Remote.getPointerSize();<br>
>> >> > + }<br>
>> >> > + };<br>
>> >> > +<br>
>> >> > + /// Remote compile callback manager.<br>
>> >> > + class RCCompileCallbackManager : public JITCompileCallbackManager<br>
>> >> > {<br>
>> >> > + public:<br>
>> >> > + RCCompileCallbackManager(TargetAddress ErrorHandlerAddress,<br>
>> >> > + OrcRemoteTargetClient &Remote)<br>
>> >> > + : JITCompileCallbackManager(ErrorHandlerAddress),<br>
>> >> > Remote(Remote) {<br>
>> >> > + assert(!Remote.CompileCallback && "Compile callback already<br>
>> >> > set");<br>
>> >> > + Remote.CompileCallback = [this](TargetAddress TrampolineAddr)<br>
>> >> > {<br>
>> >> > + return executeCompileCallback(TrampolineAddr);<br>
>> >> > + };<br>
>> >> > + Remote.emitResolverBlock();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + private:<br>
>> >> > + void grow() {<br>
>> >> > + TargetAddress BlockAddr = 0;<br>
>> >> > + uint32_t NumTrampolines = 0;<br>
>> >> > + auto EC = Remote.emitTrampolineBlock(BlockAddr,<br>
>> >> > NumTrampolines);<br>
>> >> > + assert(!EC && "Failed to create trampolines");<br>
>> >> > +<br>
>> >> > + uint32_t TrampolineSize = Remote.getTrampolineSize();<br>
>> >> > + for (unsigned I = 0; I < NumTrampolines; ++I)<br>
>> >> > + this->AvailableTrampolines.push_back(BlockAddr + (I *<br>
>> >> > TrampolineSize));<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + OrcRemoteTargetClient &Remote;<br>
>> >> > + };<br>
>> >> > +<br>
>> >> > + /// Create an OrcRemoteTargetClient.<br>
>> >> > + /// Channel is the ChannelT instance to communicate on. It is<br>
>> >> > assumed<br>
>> >> > that<br>
>> >> > + /// the channel is ready to be read from and written to.<br>
>> >> > + static ErrorOr<OrcRemoteTargetClient> Create(ChannelT &Channel) {<br>
>> >> > + std::error_code EC;<br>
>> >> > + OrcRemoteTargetClient H(Channel, EC);<br>
>> >> > + if (EC)<br>
>> >> > + return EC;<br>
>> >> > + return H;<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + /// Call the int(void) function at the given address in the target<br>
>> >> > and return<br>
>> >> > + /// its result.<br>
>> >> > + std::error_code callIntVoid(int &Result, TargetAddress Addr) {<br>
>> >> > + DEBUG(dbgs() << "Calling int(*)(void) " << format("0x%016x",<br>
>> >> > Addr)<br>
>> >> > << "\n");<br>
>> >> > +<br>
>> >> > + if (auto EC = call<CallIntVoid>(Channel, Addr))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + unsigned NextProcId;<br>
>> >> > + if (auto EC = listenForCompileRequests(NextProcId))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + if (NextProcId != CallIntVoidResponseId)<br>
>> >> > + return orcError(OrcErrorCode::UnexpectedRPCCall);<br>
>> >> > +<br>
>> >> > + return handle<CallIntVoidResponse>(Channel, [&](int R) {<br>
>> >> > + Result = R;<br>
>> >> > + DEBUG(dbgs() << "Result: " << R << "\n");<br>
>> >> > + return std::error_code();<br>
>> >> > + });<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + /// Call the int(int, char*[]) function at the given address in<br>
>> >> > the<br>
>> >> > target and<br>
>> >> > + /// return its result.<br>
>> >> > + std::error_code callMain(int &Result, TargetAddress Addr,<br>
>> >> > + const std::vector<std::string> &Args) {<br>
>> >> > + DEBUG(dbgs() << "Calling int(*)(int, char*[]) " <<<br>
>> >> > format("0x%016x", Addr)<br>
>> >> > + << "\n");<br>
>> >> > +<br>
>> >> > + if (auto EC = call<CallMain>(Channel, Addr, Args))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + unsigned NextProcId;<br>
>> >> > + if (auto EC = listenForCompileRequests(NextProcId))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + if (NextProcId != CallMainResponseId)<br>
>> >> > + return orcError(OrcErrorCode::UnexpectedRPCCall);<br>
>> >> > +<br>
>> >> > + return handle<CallMainResponse>(Channel, [&](int R) {<br>
>> >> > + Result = R;<br>
>> >> > + DEBUG(dbgs() << "Result: " << R << "\n");<br>
>> >> > + return std::error_code();<br>
>> >> > + });<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + /// Call the void() function at the given address in the target<br>
>> >> > and<br>
>> >> > wait for<br>
>> >> > + /// it to finish.<br>
>> >> > + std::error_code callVoidVoid(TargetAddress Addr) {<br>
>> >> > + DEBUG(dbgs() << "Calling void(*)(void) " << format("0x%016x",<br>
>> >> > Addr)<br>
>> >> > + << "\n");<br>
>> >> > +<br>
>> >> > + if (auto EC = call<CallVoidVoid>(Channel, Addr))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + unsigned NextProcId;<br>
>> >> > + if (auto EC = listenForCompileRequests(NextProcId))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + if (NextProcId != CallVoidVoidResponseId)<br>
>> >> > + return orcError(OrcErrorCode::UnexpectedRPCCall);<br>
>> >> > +<br>
>> >> > + return handle<CallVoidVoidResponse>(Channel, doNothing);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + /// Create an RCMemoryManager which will allocate its memory on<br>
>> >> > the<br>
>> >> > remote<br>
>> >> > + /// target.<br>
>> >> > + std::error_code<br>
>> >> > + createRemoteMemoryManager(std::unique_ptr<RCMemoryManager> &MM) {<br>
>> >> > + assert(!MM && "MemoryManager should be null before creation.");<br>
>> >> > +<br>
>> >> > + auto Id = AllocatorIds.getNext();<br>
>> >> > + if (auto EC = call<CreateRemoteAllocator>(Channel, Id))<br>
>> >> > + return EC;<br>
>> >> > + MM = llvm::make_unique<RCMemoryManager>(*this, Id);<br>
>> >> > + return std::error_code();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + /// Create an RCIndirectStubsManager that will allocate stubs on<br>
>> >> > the<br>
>> >> > remote<br>
>> >> > + /// target.<br>
>> >> > + std::error_code<br>
>> >> > + createIndirectStubsManager(std::unique_ptr<RCIndirectStubsManager><br>
>> >> > &I) {<br>
>> >> > + assert(!I && "Indirect stubs manager should be null before<br>
>> >> > creation.");<br>
>> >> > + auto Id = IndirectStubOwnerIds.getNext();<br>
>> >> > + if (auto EC = call<CreateIndirectStubsOwner>(Channel, Id))<br>
>> >> > + return EC;<br>
>> >> > + I = llvm::make_unique<RCIndirectStubsManager>(*this, Id);<br>
>> >> > + return std::error_code();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + /// Search for symbols in the remote process. Note: This should be<br>
>> >> > used by<br>
>> >> > + /// symbol resolvers *after* they've searched the local symbol<br>
>> >> > table<br>
>> >> > in the<br>
>> >> > + /// JIT stack.<br>
>> >> > + std::error_code getSymbolAddress(TargetAddress &Addr, StringRef<br>
>> >> > Name)<br>
>> >> > {<br>
>> >> > + // Check for an 'out-of-band' error, e.g. from an MM destructor.<br>
>> >> > + if (ExistingError)<br>
>> >> > + return ExistingError;<br>
>> >> > +<br>
>> >> > + // Request remote symbol address.<br>
>> >> > + if (auto EC = call<GetSymbolAddress>(Channel, Name))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + return expect<GetSymbolAddressResponse>(Channel,<br>
>> >> > [&](TargetAddress<br>
>> >> > &A) {<br>
>> >> > + Addr = A;<br>
>> >> > + DEBUG(dbgs() << "Remote address lookup " << Name << " = "<br>
>> >> > + << format("0x%016x", Addr) << "\n");<br>
>> >> > + return std::error_code();<br>
>> >> > + });<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + /// Get the triple for the remote target.<br>
>> >> > + const std::string &getTargetTriple() const { return<br>
>> >> > RemoteTargetTriple; }<br>
>> >> > +<br>
>> >> > + std::error_code terminateSession() { return<br>
>> >> > call<TerminateSession>(Channel); }<br>
>> >> > +<br>
>> >> > +private:<br>
>> >> > + OrcRemoteTargetClient(ChannelT &Channel, std::error_code &EC)<br>
>> >> > + : Channel(Channel), RemotePointerSize(0), RemotePageSize(0),<br>
>> >> > + RemoteTrampolineSize(0), RemoteIndirectStubSize(0) {<br>
>> >> > + if ((EC = call<GetRemoteInfo>(Channel)))<br>
>> >> > + return;<br>
>> >> > +<br>
>> >> > + EC = expect<GetRemoteInfoResponse>(<br>
>> >> > + Channel, readArgs(RemoteTargetTriple, RemotePointerSize,<br>
>> >> > RemotePageSize,<br>
>> >> > + RemoteTrampolineSize,<br>
>> >> > RemoteIndirectStubSize));<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + void destroyRemoteAllocator(ResourceIdMgr::ResourceId Id) {<br>
>> >> > + if (auto EC = call<DestroyRemoteAllocator>(Channel, Id)) {<br>
>> >> > + // FIXME: This will be triggered by a removeModuleSet call:<br>
>> >> > Propagate<br>
>> >> > + // error return up through that.<br>
>> >> > + llvm_unreachable("Failed to destroy remote allocator.");<br>
>> >> > + AllocatorIds.release(Id);<br>
>> >> > + }<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code<br>
>> >> > destroyIndirectStubsManager(ResourceIdMgr::ResourceId<br>
>> >> > Id) {<br>
>> >> > + IndirectStubOwnerIds.release(Id);<br>
>> >> > + return call<DestroyIndirectStubsOwner>(Channel, Id);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code emitIndirectStubs(TargetAddress &StubBase,<br>
>> >> > + TargetAddress &PtrBase,<br>
>> >> > + uint32_t &NumStubsEmitted,<br>
>> >> > + ResourceIdMgr::ResourceId Id,<br>
>> >> > + uint32_t NumStubsRequired) {<br>
>> >> > + if (auto EC = call<EmitIndirectStubs>(Channel, Id,<br>
>> >> > NumStubsRequired))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + return expect<EmitIndirectStubsResponse>(<br>
>> >> > + Channel, readArgs(StubBase, PtrBase, NumStubsEmitted));<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code emitResolverBlock() {<br>
>> >> > + // Check for an 'out-of-band' error, e.g. from an MM destructor.<br>
>> >> > + if (ExistingError)<br>
>> >> > + return ExistingError;<br>
>> >> > +<br>
>> >> > + return call<EmitResolverBlock>(Channel);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code emitTrampolineBlock(TargetAddress &BlockAddr,<br>
>> >> > + uint32_t &NumTrampolines) {<br>
>> >> > + // Check for an 'out-of-band' error, e.g. from an MM destructor.<br>
>> >> > + if (ExistingError)<br>
>> >> > + return ExistingError;<br>
>> >> > +<br>
>> >> > + if (auto EC = call<EmitTrampolineBlock>(Channel))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + return expect<EmitTrampolineBlockResponse>(<br>
>> >> > + Channel, [&](TargetAddress BAddr, uint32_t NTrampolines) {<br>
>> >> > + BlockAddr = BAddr;<br>
>> >> > + NumTrampolines = NTrampolines;<br>
>> >> > + return std::error_code();<br>
>> >> > + });<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + uint32_t getIndirectStubSize() const { return<br>
>> >> > RemoteIndirectStubSize;<br>
>> >> > }<br>
>> >> > + uint32_t getPageSize() const { return RemotePageSize; }<br>
>> >> > + uint32_t getPointerSize() const { return RemotePointerSize; }<br>
>> >> > +<br>
>> >> > + uint32_t getTrampolineSize() const { return RemoteTrampolineSize;<br>
>> >> > }<br>
>> >> > +<br>
>> >> > + std::error_code listenForCompileRequests(uint32_t &NextId) {<br>
>> >> > + // Check for an 'out-of-band' error, e.g. from an MM destructor.<br>
>> >> > + if (ExistingError)<br>
>> >> > + return ExistingError;<br>
>> >> > +<br>
>> >> > + if (auto EC = getNextProcId(Channel, NextId))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + while (NextId == RequestCompileId) {<br>
>> >> > + TargetAddress TrampolineAddr = 0;<br>
>> >> > + if (auto EC = handle<RequestCompile>(Channel,<br>
>> >> > readArgs(TrampolineAddr)))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + TargetAddress ImplAddr = CompileCallback(TrampolineAddr);<br>
>> >> > + if (auto EC = call<RequestCompileResponse>(Channel, ImplAddr))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + if (auto EC = getNextProcId(Channel, NextId))<br>
>> >> > + return EC;<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + return std::error_code();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code readMem(char *Dst, TargetAddress Src, uint64_t<br>
>> >> > Size)<br>
>> >> > {<br>
>> >> > + // Check for an 'out-of-band' error, e.g. from an MM destructor.<br>
>> >> > + if (ExistingError)<br>
>> >> > + return ExistingError;<br>
>> >> > +<br>
>> >> > + if (auto EC = call<ReadMem>(Channel, Src, Size))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + if (auto EC = expect<ReadMemResponse>(<br>
>> >> > + Channel, [&]() { return Channel.readBytes(Dst, Size);<br>
>> >> > }))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + return std::error_code();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code reserveMem(TargetAddress &RemoteAddr,<br>
>> >> > + ResourceIdMgr::ResourceId Id, uint64_t<br>
>> >> > Size,<br>
>> >> > + uint32_t Align) {<br>
>> >> > +<br>
>> >> > + // Check for an 'out-of-band' error, e.g. from an MM destructor.<br>
>> >> > + if (ExistingError)<br>
>> >> > + return ExistingError;<br>
>> >> > +<br>
>> >> > + if (auto EC = call<ReserveMem>(Channel, Id, Size, Align))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + if (auto EC = expect<ReserveMemResponse>(Channel,<br>
>> >> > [&](TargetAddress<br>
>> >> > Addr) {<br>
>> >> > + RemoteAddr = Addr;<br>
>> >> > + return std::error_code();<br>
>> >> > + }))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + return std::error_code();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code setProtections(ResourceIdMgr::ResourceId Id,<br>
>> >> > + TargetAddress RemoteSegAddr,<br>
>> >> > + unsigned ProtFlags) {<br>
>> >> > + return call<SetProtections>(Channel, Id, RemoteSegAddr,<br>
>> >> > ProtFlags);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code writeMem(TargetAddress Addr, const char *Src,<br>
>> >> > uint64_t Size) {<br>
>> >> > + // Check for an 'out-of-band' error, e.g. from an MM destructor.<br>
>> >> > + if (ExistingError)<br>
>> >> > + return ExistingError;<br>
>> >> > +<br>
>> >> > + // Make the send call.<br>
>> >> > + if (auto EC = call<WriteMem>(Channel, Addr, Size))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + // Follow this up with the section contents.<br>
>> >> > + if (auto EC = Channel.appendBytes(Src, Size))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + return Channel.send();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code writePointer(TargetAddress Addr, TargetAddress<br>
>> >> > PtrVal) {<br>
>> >> > + // Check for an 'out-of-band' error, e.g. from an MM destructor.<br>
>> >> > + if (ExistingError)<br>
>> >> > + return ExistingError;<br>
>> >> > +<br>
>> >> > + return call<WritePtr>(Channel, Addr, PtrVal);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + static std::error_code doNothing() { return std::error_code(); }<br>
>> >> > +<br>
>> >> > + ChannelT &Channel;<br>
>> >> > + std::error_code ExistingError;<br>
>> >> > + std::string RemoteTargetTriple;<br>
>> >> > + uint32_t RemotePointerSize;<br>
>> >> > + uint32_t RemotePageSize;<br>
>> >> > + uint32_t RemoteTrampolineSize;<br>
>> >> > + uint32_t RemoteIndirectStubSize;<br>
>> >> > + ResourceIdMgr AllocatorIds, IndirectStubOwnerIds;<br>
>> >> > + std::function<TargetAddress(TargetAddress)> CompileCallback;<br>
>> >> > +};<br>
>> >> > +<br>
>> >> > +} // end namespace remote<br>
>> >> > +} // end namespace orc<br>
>> >> > +} // end namespace llvm<br>
>> >> > +<br>
>> >> > +#undef DEBUG_TYPE<br>
>> >> > +<br>
>> >> > +#endif<br>
>> >> ><br>
>> >> > Added:<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h<br>
>> >> > URL:<br>
>> >> ><br>
>> >> > <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h?rev=257305&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h?rev=257305&view=auto</a><br>
>> >> ><br>
>> >> ><br>
>> >> > ==============================================================================<br>
>> >> > ---<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h<br>
>> >> > (added)<br>
>> >> > +++<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h<br>
>> >> > Sun Jan 10 19:40:11 2016<br>
>> >> > @@ -0,0 +1,185 @@<br>
>> >> > +//===--- OrcRemoteTargetRPCAPI.h - Orc Remote-target RPC API ----*-<br>
>> >> > C++<br>
>> >> > -*-===//<br>
>> >> > +//<br>
>> >> > +// The LLVM Compiler Infrastructure<br>
>> >> > +//<br>
>> >> > +// This file is distributed under the University of Illinois Open<br>
>> >> > Source<br>
>> >> > +// License. See LICENSE.TXT for details.<br>
>> >> > +//<br>
>> >> ><br>
>> >> ><br>
>> >> > +//===----------------------------------------------------------------------===//<br>
>> >> > +//<br>
>> >> > +// This file defines the Orc remote-target RPC API. It should not be<br>
>> >> > used<br>
>> >> > +// directly, but is used by the RemoteTargetClient and<br>
>> >> > RemoteTargetServer<br>
>> >> > +// classes.<br>
>> >> > +//<br>
>> >> ><br>
>> >> ><br>
>> >> > +//===----------------------------------------------------------------------===//<br>
>> >> > +<br>
>> >> > +#ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H<br>
>> >> > +#define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H<br>
>> >> > +<br>
>> >> > +#include "JITSymbol.h"<br>
>> >> > +#include "RPCChannel.h"<br>
>> >> > +#include "RPCUtils.h"<br>
>> >> > +<br>
>> >> > +namespace llvm {<br>
>> >> > +namespace orc {<br>
>> >> > +namespace remote {<br>
>> >> > +<br>
>> >> > +class OrcRemoteTargetRPCAPI : public RPC<RPCChannel> {<br>
>> >> > +protected:<br>
>> >> > + class ResourceIdMgr {<br>
>> >> > + public:<br>
>> >> > + typedef uint64_t ResourceId;<br>
>> >> > + ResourceIdMgr() : NextId(0) {}<br>
>> >> > + ResourceId getNext() {<br>
>> >> > + if (!FreeIds.empty()) {<br>
>> >> > + ResourceId I = FreeIds.back();<br>
>> >> > + FreeIds.pop_back();<br>
>> >> > + return I;<br>
>> >> > + }<br>
>> >> > + return NextId++;<br>
>> >> > + }<br>
>> >> > + void release(ResourceId I) { FreeIds.push_back(I); }<br>
>> >> > +<br>
>> >> > + private:<br>
>> >> > + ResourceId NextId;<br>
>> >> > + std::vector<ResourceId> FreeIds;<br>
>> >> > + };<br>
>> >> > +<br>
>> >> > +public:<br>
>> >> > + enum JITProcId : uint32_t {<br>
>> >> > + InvalidId = 0,<br>
>> >> > + CallIntVoidId,<br>
>> >> > + CallIntVoidResponseId,<br>
>> >> > + CallMainId,<br>
>> >> > + CallMainResponseId,<br>
>> >> > + CallVoidVoidId,<br>
>> >> > + CallVoidVoidResponseId,<br>
>> >> > + CreateRemoteAllocatorId,<br>
>> >> > + CreateIndirectStubsOwnerId,<br>
>> >> > + DestroyRemoteAllocatorId,<br>
>> >> > + DestroyIndirectStubsOwnerId,<br>
>> >> > + EmitIndirectStubsId,<br>
>> >> > + EmitIndirectStubsResponseId,<br>
>> >> > + EmitResolverBlockId,<br>
>> >> > + EmitTrampolineBlockId,<br>
>> >> > + EmitTrampolineBlockResponseId,<br>
>> >> > + GetSymbolAddressId,<br>
>> >> > + GetSymbolAddressResponseId,<br>
>> >> > + GetRemoteInfoId,<br>
>> >> > + GetRemoteInfoResponseId,<br>
>> >> > + ReadMemId,<br>
>> >> > + ReadMemResponseId,<br>
>> >> > + ReserveMemId,<br>
>> >> > + ReserveMemResponseId,<br>
>> >> > + RequestCompileId,<br>
>> >> > + RequestCompileResponseId,<br>
>> >> > + SetProtectionsId,<br>
>> >> > + TerminateSessionId,<br>
>> >> > + WriteMemId,<br>
>> >> > + WritePtrId<br>
>> >> > + };<br>
>> >> > +<br>
>> >> > + static const char *getJITProcIdName(JITProcId Id);<br>
>> >> > +<br>
>> >> > + typedef Procedure<CallIntVoidId, TargetAddress /* FnAddr */><br>
>> >> > CallIntVoid;<br>
>> >> > +<br>
>> >> > + typedef Procedure<CallIntVoidResponseId, int /* Result */><br>
>> >> > + CallIntVoidResponse;<br>
>> >> > +<br>
>> >> > + typedef Procedure<CallMainId, TargetAddress /* FnAddr */,<br>
>> >> > + std::vector<std::string> /* Args */><br>
>> >> > + CallMain;<br>
>> >> > +<br>
>> >> > + typedef Procedure<CallMainResponseId, int /* Result */><br>
>> >> > CallMainResponse;<br>
>> >> > +<br>
>> >> > + typedef Procedure<CallVoidVoidId, TargetAddress /* FnAddr */><br>
>> >> > CallVoidVoid;<br>
>> >> > +<br>
>> >> > + typedef Procedure<CallVoidVoidResponseId> CallVoidVoidResponse;<br>
>> >> > +<br>
>> >> > + typedef Procedure<CreateRemoteAllocatorId,<br>
>> >> > + ResourceIdMgr::ResourceId /* Allocator ID */><br>
>> >> > + CreateRemoteAllocator;<br>
>> >> > +<br>
>> >> > + typedef Procedure<CreateIndirectStubsOwnerId,<br>
>> >> > + ResourceIdMgr::ResourceId /* StubsOwner ID */><br>
>> >> > + CreateIndirectStubsOwner;<br>
>> >> > +<br>
>> >> > + typedef Procedure<DestroyRemoteAllocatorId,<br>
>> >> > + ResourceIdMgr::ResourceId /* Allocator ID */><br>
>> >> > + DestroyRemoteAllocator;<br>
>> >> > +<br>
>> >> > + typedef Procedure<DestroyIndirectStubsOwnerId,<br>
>> >> > + ResourceIdMgr::ResourceId /* StubsOwner ID */><br>
>> >> > + DestroyIndirectStubsOwner;<br>
>> >> > +<br>
>> >> > + typedef Procedure<EmitIndirectStubsId,<br>
>> >> > + ResourceIdMgr::ResourceId /* StubsOwner ID */,<br>
>> >> > + uint32_t /* NumStubsRequired */><br>
>> >> > + EmitIndirectStubs;<br>
>> >> > +<br>
>> >> > + typedef Procedure<<br>
>> >> > + EmitIndirectStubsResponseId, TargetAddress /* StubsBaseAddr<br>
>> >> > */,<br>
>> >> > + TargetAddress /* PtrsBaseAddr */, uint32_t /* NumStubsEmitted<br>
>> >> > */><br>
>> >> > + EmitIndirectStubsResponse;<br>
>> >> > +<br>
>> >> > + typedef Procedure<EmitResolverBlockId> EmitResolverBlock;<br>
>> >> > +<br>
>> >> > + typedef Procedure<EmitTrampolineBlockId> EmitTrampolineBlock;<br>
>> >> > +<br>
>> >> > + typedef Procedure<EmitTrampolineBlockResponseId,<br>
>> >> > + TargetAddress /* BlockAddr */,<br>
>> >> > + uint32_t /* NumTrampolines */><br>
>> >> > + EmitTrampolineBlockResponse;<br>
>> >> > +<br>
>> >> > + typedef Procedure<GetSymbolAddressId, std::string /*SymbolName*/><br>
>> >> > + GetSymbolAddress;<br>
>> >> > +<br>
>> >> > + typedef Procedure<GetSymbolAddressResponseId, uint64_t /*<br>
>> >> > SymbolAddr<br>
>> >> > */><br>
>> >> > + GetSymbolAddressResponse;<br>
>> >> > +<br>
>> >> > + typedef Procedure<GetRemoteInfoId> GetRemoteInfo;<br>
>> >> > +<br>
>> >> > + typedef Procedure<GetRemoteInfoResponseId, std::string /* Triple<br>
>> >> > */,<br>
>> >> > + uint32_t /* PointerSize */, uint32_t /* PageSize<br>
>> >> > */,<br>
>> >> > + uint32_t /* TrampolineSize */,<br>
>> >> > + uint32_t /* IndirectStubSize */><br>
>> >> > + GetRemoteInfoResponse;<br>
>> >> > +<br>
>> >> > + typedef Procedure<ReadMemId, TargetAddress /* Src */, uint64_t /*<br>
>> >> > Size */><br>
>> >> > + ReadMem;<br>
>> >> > +<br>
>> >> > + typedef Procedure<ReadMemResponseId> ReadMemResponse;<br>
>> >> > +<br>
>> >> > + typedef Procedure<ReserveMemId, ResourceIdMgr::ResourceId /* Id<br>
>> >> > */,<br>
>> >> > + uint64_t /* Size */, uint32_t /* Align */><br>
>> >> > + ReserveMem;<br>
>> >> > +<br>
>> >> > + typedef Procedure<ReserveMemResponseId, TargetAddress /* Addr */><br>
>> >> > + ReserveMemResponse;<br>
>> >> > +<br>
>> >> > + typedef Procedure<RequestCompileId, TargetAddress /*<br>
>> >> > TrampolineAddr<br>
>> >> > */><br>
>> >> > + RequestCompile;<br>
>> >> > +<br>
>> >> > + typedef Procedure<RequestCompileResponseId, TargetAddress /*<br>
>> >> > ImplAddr<br>
>> >> > */><br>
>> >> > + RequestCompileResponse;<br>
>> >> > +<br>
>> >> > + typedef Procedure<SetProtectionsId, ResourceIdMgr::ResourceId /*<br>
>> >> > Id<br>
>> >> > */,<br>
>> >> > + TargetAddress /* Dst */, uint32_t /* ProtFlags<br>
>> >> > */><br>
>> >> > + SetProtections;<br>
>> >> > +<br>
>> >> > + typedef Procedure<TerminateSessionId> TerminateSession;<br>
>> >> > +<br>
>> >> > + typedef Procedure<WriteMemId, TargetAddress /* Dst */, uint64_t /*<br>
>> >> > Size */<br>
>> >> > + /* Data should follow */><br>
>> >> > + WriteMem;<br>
>> >> > +<br>
>> >> > + typedef Procedure<WritePtrId, TargetAddress /* Dst */,<br>
>> >> > + TargetAddress /* Val */><br>
>> >> > + WritePtr;<br>
>> >> > +};<br>
>> >> > +<br>
>> >> > +} // end namespace remote<br>
>> >> > +} // end namespace orc<br>
>> >> > +} // end namespace llvm<br>
>> >> > +<br>
>> >> > +#endif<br>
>> >> ><br>
>> >> > Added:<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h<br>
>> >> > URL:<br>
>> >> ><br>
>> >> > <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h?rev=257305&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h?rev=257305&view=auto</a><br>
>> >> ><br>
>> >> ><br>
>> >> > ==============================================================================<br>
>> >> > ---<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h<br>
>> >> > (added)<br>
>> >> > +++<br>
>> >> > llvm/trunk/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h<br>
>> >> > Sun Jan 10 19:40:11 2016<br>
>> >> > @@ -0,0 +1,479 @@<br>
>> >> > +//===---- OrcRemoteTargetServer.h - Orc Remote-target Server ----*-<br>
>> >> > C++<br>
>> >> > -*-===//<br>
>> >> > +//<br>
>> >> > +// The LLVM Compiler Infrastructure<br>
>> >> > +//<br>
>> >> > +// This file is distributed under the University of Illinois Open<br>
>> >> > Source<br>
>> >> > +// License. See LICENSE.TXT for details.<br>
>> >> > +//<br>
>> >> ><br>
>> >> ><br>
>> >> > +//===----------------------------------------------------------------------===//<br>
>> >> > +//<br>
>> >> > +// This file defines the OrcRemoteTargetServer class. It can be used<br>
>> >> > to<br>
>> >> > build a<br>
>> >> > +// JIT server that can execute code sent from an<br>
>> >> > OrcRemoteTargetClient.<br>
>> >> > +//<br>
>> >> ><br>
>> >> ><br>
>> >> > +//===----------------------------------------------------------------------===//<br>
>> >> > +<br>
>> >> > +#ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H<br>
>> >> > +#define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H<br>
>> >> > +<br>
>> >> > +#include "OrcRemoteTargetRPCAPI.h"<br>
>> >> > +#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"<br>
>> >> > +#include "llvm/Support/Debug.h"<br>
>> >> > +#include "llvm/Support/Format.h"<br>
>> >> > +#include "llvm/Support/Process.h"<br>
>> >> > +#include "llvm/Support/raw_ostream.h"<br>
>> >> > +#include <map><br>
>> >> > +<br>
>> >> > +#define DEBUG_TYPE "orc-remote"<br>
>> >> > +<br>
>> >> > +namespace llvm {<br>
>> >> > +namespace orc {<br>
>> >> > +namespace remote {<br>
>> >> > +<br>
>> >> > +template <typename ChannelT, typename TargetT><br>
>> >> > +class OrcRemoteTargetServer : public OrcRemoteTargetRPCAPI {<br>
>> >> > +public:<br>
>> >> > + typedef std::function<TargetAddress(const std::string &Name)><br>
>> >> > + SymbolLookupFtor;<br>
>> >> > +<br>
>> >> > + OrcRemoteTargetServer(ChannelT &Channel, SymbolLookupFtor<br>
>> >> > SymbolLookup)<br>
>> >> > + : Channel(Channel), SymbolLookup(std::move(SymbolLookup)) {}<br>
>> >> > +<br>
>> >> > + std::error_code getNextProcId(JITProcId &Id) {<br>
>> >> > + return deserialize(Channel, Id);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code handleKnownProcedure(JITProcId Id) {<br>
>> >> > + DEBUG(dbgs() << "Handling known proc: " << getJITProcIdName(Id)<br>
>> >> > <<<br>
>> >> > "\n");<br>
>> >> > +<br>
>> >> > + switch (Id) {<br>
>> >> > + case CallIntVoidId:<br>
>> >> > + return handleCallIntVoid();<br>
>> >> > + case CallMainId:<br>
>> >> > + return handleCallMain();<br>
>> >> > + case CallVoidVoidId:<br>
>> >> > + return handleCallVoidVoid();<br>
>> >> > + case CreateRemoteAllocatorId:<br>
>> >> > + return handleCreateRemoteAllocator();<br>
>> >> > + case CreateIndirectStubsOwnerId:<br>
>> >> > + return handleCreateIndirectStubsOwner();<br>
>> >> > + case DestroyRemoteAllocatorId:<br>
>> >> > + return handleDestroyRemoteAllocator();<br>
>> >> > + case EmitIndirectStubsId:<br>
>> >> > + return handleEmitIndirectStubs();<br>
>> >> > + case EmitResolverBlockId:<br>
>> >> > + return handleEmitResolverBlock();<br>
>> >> > + case EmitTrampolineBlockId:<br>
>> >> > + return handleEmitTrampolineBlock();<br>
>> >> > + case GetSymbolAddressId:<br>
>> >> > + return handleGetSymbolAddress();<br>
>> >> > + case GetRemoteInfoId:<br>
>> >> > + return handleGetRemoteInfo();<br>
>> >> > + case ReadMemId:<br>
>> >> > + return handleReadMem();<br>
>> >> > + case ReserveMemId:<br>
>> >> > + return handleReserveMem();<br>
>> >> > + case SetProtectionsId:<br>
>> >> > + return handleSetProtections();<br>
>> >> > + case WriteMemId:<br>
>> >> > + return handleWriteMem();<br>
>> >> > + case WritePtrId:<br>
>> >> > + return handleWritePtr();<br>
>> >> > + default:<br>
>> >> > + return orcError(OrcErrorCode::UnexpectedRPCCall);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + llvm_unreachable("Unhandled JIT RPC procedure Id.");<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code requestCompile(TargetAddress &CompiledFnAddr,<br>
>> >> > + TargetAddress TrampolineAddr) {<br>
>> >> > + if (auto EC = call<RequestCompile>(Channel, TrampolineAddr))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + while (1) {<br>
>> >> > + JITProcId Id = InvalidId;<br>
>> >> > + if (auto EC = getNextProcId(Id))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + switch (Id) {<br>
>> >> > + case RequestCompileResponseId:<br>
>> >> > + return handle<RequestCompileResponse>(Channel,<br>
>> >> > +<br>
>> >> > readArgs(CompiledFnAddr));<br>
>> >> > + default:<br>
>> >> > + if (auto EC = handleKnownProcedure(Id))<br>
>> >> > + return EC;<br>
>> >> > + }<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + llvm_unreachable("Fell through request-compile command loop.");<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > +private:<br>
>> >> > + struct Allocator {<br>
>> >> > + Allocator() = default;<br>
>> >> > + Allocator(Allocator &&) = default;<br>
>> >> > + Allocator &operator=(Allocator &&) = default;<br>
>> >> > +<br>
>> >> > + ~Allocator() {<br>
>> >> > + for (auto &Alloc : Allocs)<br>
>> >> > + sys::Memory::releaseMappedMemory(Alloc.second);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code allocate(void *&Addr, size_t Size, uint32_t<br>
>> >> > Align)<br>
>> >> > {<br>
>> >> > + std::error_code EC;<br>
>> >> > + sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(<br>
>> >> > + Size, nullptr, sys::Memory::MF_READ |<br>
>> >> > sys::Memory::MF_WRITE,<br>
>> >> > EC);<br>
>> >> > + if (EC)<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + Addr = MB.base();<br>
>> >> > + assert(Allocs.find(MB.base()) == Allocs.end() && "Duplicate<br>
>> >> > alloc");<br>
>> >> > + Allocs[MB.base()] = std::move(MB);<br>
>> >> > + return std::error_code();<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code setProtections(void *block, unsigned Flags) {<br>
>> >> > + auto I = Allocs.find(block);<br>
>> >> > + if (I == Allocs.end())<br>
>> >> > + return<br>
>> >> > orcError(OrcErrorCode::RemoteMProtectAddrUnrecognized);<br>
>> >> > + return sys::Memory::protectMappedMemory(I->second, Flags);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + private:<br>
>> >> > + std::map<void *, sys::MemoryBlock> Allocs;<br>
>> >> > + };<br>
>> >> > +<br>
>> >> > + static std::error_code doNothing() { return std::error_code(); }<br>
>> >> > +<br>
>> >> > + static TargetAddress reenter(void *JITTargetAddr, void<br>
>> >> > *TrampolineAddr) {<br>
>> >> > + TargetAddress CompiledFnAddr = 0;<br>
>> >> > +<br>
>> >> > + auto T = static_cast<OrcRemoteTargetServer *>(JITTargetAddr);<br>
>> >> > + auto EC = T->requestCompile(<br>
>> >> > + CompiledFnAddr, static_cast<TargetAddress>(<br>
>> >> > +<br>
>> >> > reinterpret_cast<uintptr_t>(TrampolineAddr)));<br>
>> >> > + assert(!EC && "Compile request failed");<br>
>> >> > + return CompiledFnAddr;<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code handleCallIntVoid() {<br>
>> >> > + typedef int (*IntVoidFnTy)();<br>
>> >> > +<br>
>> >> > + IntVoidFnTy Fn = nullptr;<br>
>> >> > + if (auto EC = handle<CallIntVoid>(Channel, [&](TargetAddress<br>
>> >> > Addr)<br>
>> >> > {<br>
>> >> > + Fn =<br>
>> >> > reinterpret_cast<IntVoidFnTy>(static_cast<uintptr_t>(Addr));<br>
>> >> > + return std::error_code();<br>
>> >> > + }))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + DEBUG(dbgs() << " Calling " << reinterpret_cast<void *>(Fn) <<<br>
>> >> > "\n");<br>
>> >> > + int Result = Fn();<br>
>> >> > + DEBUG(dbgs() << " Result = " << Result << "\n");<br>
>> >> > +<br>
>> >> > + return call<CallIntVoidResponse>(Channel, Result);<br>
>> >> > + }<br>
>> >> > +<br>
>> >> > + std::error_code handleCallMain() {<br>
>> >> > + typedef int (*MainFnTy)(int, const char *[]);<br>
>> >> > +<br>
>> >> > + MainFnTy Fn = nullptr;<br>
>> >> > + std::vector<std::string> Args;<br>
>> >> > + if (auto EC = handle<CallMain>(<br>
>> >> > + Channel, [&](TargetAddress Addr,<br>
>> >> > std::vector<std::string><br>
>> >> > &A) {<br>
>> >> > + Fn =<br>
>> >> > reinterpret_cast<MainFnTy>(static_cast<uintptr_t>(Addr));<br>
>> >> > + Args = std::move(A);<br>
>> >> > + return std::error_code();<br>
>> >> > + }))<br>
>> >> > + return EC;<br>
>> >> > +<br>
>> >> > + int ArgC = Args.size() + 1;<br>
>> >> > + int Idx = 1;<br>
>> >> > + std::unique_ptr<const char *[]> ArgV(new const char *[ArgC +<br>
>> >> > 1]);<br>
>> >> > + ArgV[0] = "<jit process>";<br>
>> >> > + for (auto &Arg : Args)<br>
>> >> > + ArgV[Idx++] = Arg.c_str();<br>
>> >> > +<br>
>> >> > + DEBUG(dbgs() << " Calling " << reinterpret_cast<void *>(Fn) <<<br>
>> >> > "\n");<br>
>> >><br>
>> >> This is causing a warning:<br>
>> >><br>
>> >><br>
>> >><br>
>> >> /opt/llvm/build-llvm/src/llvm/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h:215:5:<br>
>> >> warning: ISO C++ forbids casting between pointer-to-function and<br>
>> >> pointer-to-object [enabled by default]<br>
>> ><br>
>> ><br>
>> > I'm not seeing this on a clang self-host. Which compiler is warning<br>
>> > about<br>
>> > this? (should we enable this warning when building with clang, or<br>
>> > disable it<br>
>> > when building with whatever is firing it?)<br>
>><br>
>> I'm not certain of the compiler version information. This is on our<br>
>> attribute documentation bot, which I *think* is GCC. Tanya would know<br>
>> more if we need exact details.<br>
>><br>
>> I think it's a reasonable diagnostic to keep for portability reasons.<br>
>> We usually do pretty good about keeping this one clean, though it does<br>
>> crop up from time to time.<br>
><br>
><br>
> Hmm, interesting - Clang does have this warning and we do build with<br>
> -pedantic by default, but Clang does /not/ fire this warning when building<br>
> in C++11 mode...<br>
><br>
> The implementation in Clang (SemaCast.cpp:2022) mentions that this behavior<br>
> is "conditionally supported" - which is, implementations may allow it and if<br>
> they allow the conversion in both directions then it must yield the original<br>
> pointer value (it must roundtrip).<br>
><br>
> The comment also mentions that compilers generally support it even in C++03<br>
> mode, because you need it to use dlsym and GetProcAddress. So it seems we<br>
> probably require this of any implementation we use anyway.<br>
<br>
</div></div>That is true -- we do require that behavior to be supported anyway,<br>
and if it's not supported we're likely to find out very quickly. So<br>
perhaps we can simply disable that diagnostic for the various<br>
compilers.<br></blockquote><div><br></div><div>Thing is it's a bit of a broad flag (-Wpedantic covers many things) and probably still want the Clang version, just not the GCC version. I suppose we probably have somewhere that handles GCC-specific warning configuration...</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class="HOEnZb"><font color="#888888"><br>
~Aaron<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
><br>
>><br>
>><br>
>> > If it's worth keeping, the easy/reasonable fix is to cast the function<br>
>> > pointer to intptr_t first, then to void* to print. (or just to intptr_t<br>
>> > then<br>
>> > hex format with llvm::format)<br>
>><br>
>> Yes, I just wasn't certain whether Lang wanted to keep the DEBUG()<br>
>> statements or not, since another easy fix is to remove the debug<br>
>> logging if it's not required.<br>
>><br>
>> ~Aaron<br>
><br>
><br>
</div></div></blockquote></div><br></div></div>