• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Utilities for remote-JITing with LLI.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TOOLS_LLI_REMOTEJITUTILS_H
15 #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H
16 
17 #include "llvm/ExecutionEngine/Orc/RPCChannel.h"
18 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
19 #include <mutex>
20 
21 #if !defined(_MSC_VER) && !defined(__MINGW32__)
22 #include <unistd.h>
23 #else
24 #include <io.h>
25 #endif
26 
27 /// RPC channel that reads from and writes from file descriptors.
28 class FDRPCChannel final : public llvm::orc::remote::RPCChannel {
29 public:
FDRPCChannel(int InFD,int OutFD)30   FDRPCChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
31 
readBytes(char * Dst,unsigned Size)32   llvm::Error readBytes(char *Dst, unsigned Size) override {
33     assert(Dst && "Attempt to read into null.");
34     ssize_t Completed = 0;
35     while (Completed < static_cast<ssize_t>(Size)) {
36       ssize_t Read = ::read(InFD, Dst + Completed, Size - Completed);
37       if (Read <= 0) {
38         auto ErrNo = errno;
39         if (ErrNo == EAGAIN || ErrNo == EINTR)
40           continue;
41         else
42           return llvm::errorCodeToError(
43                    std::error_code(errno, std::generic_category()));
44       }
45       Completed += Read;
46     }
47     return llvm::Error::success();
48   }
49 
appendBytes(const char * Src,unsigned Size)50   llvm::Error appendBytes(const char *Src, unsigned Size) override {
51     assert(Src && "Attempt to append from null.");
52     ssize_t Completed = 0;
53     while (Completed < static_cast<ssize_t>(Size)) {
54       ssize_t Written = ::write(OutFD, Src + Completed, Size - Completed);
55       if (Written < 0) {
56         auto ErrNo = errno;
57         if (ErrNo == EAGAIN || ErrNo == EINTR)
58           continue;
59         else
60           return llvm::errorCodeToError(
61                    std::error_code(errno, std::generic_category()));
62       }
63       Completed += Written;
64     }
65     return llvm::Error::success();
66   }
67 
send()68   llvm::Error send() override { return llvm::Error::success(); }
69 
70 private:
71   int InFD, OutFD;
72 };
73 
74 #endif
75