[obj2yaml] - Fix BB after r373315.
[llvm-complete.git] / examples / Kaleidoscope / BuildingAJIT / Chapter5 / RemoteJITUtils.h
blob14a2815bc021f42e0a5c13152c6edaf8ff982b03
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TOOLS_LLI_REMOTEJITUTILS_H
14 #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H
16 #include "llvm/ExecutionEngine/Orc/RawByteChannel.h"
17 #include "llvm/Support/Error.h"
18 #include <cassert>
19 #include <cerrno>
20 #include <system_error>
22 #if !defined(_MSC_VER) && !defined(__MINGW32__)
23 #include <unistd.h>
24 #else
25 #include <io.h>
26 #endif
28 /// RPC channel that reads from and writes from file descriptors.
29 class FDRPCChannel final : public llvm::orc::rpc::RawByteChannel {
30 public:
31 FDRPCChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
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()));
46 Completed += Read;
48 return llvm::Error::success();
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()));
64 Completed += Written;
66 return llvm::Error::success();
69 llvm::Error send() override { return llvm::Error::success(); }
71 private:
72 int InFD, OutFD;
75 #endif // LLVM_TOOLS_LLI_REMOTEJITUTILS_H