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