LLVM  14.0.0git
SimpleRemoteEPCUtils.h
Go to the documentation of this file.
1 //===--- SimpleRemoteEPCUtils.h - Utils for Simple Remote EPC ---*- 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 // Message definitions and other utilities for SimpleRemoteEPC and
10 // SimpleRemoteEPCServer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_SIMPLEREMOTEEPCUTILS_H
15 #define LLVM_EXECUTIONENGINE_ORC_SHARED_SIMPLEREMOTEEPCUTILS_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Support/Error.h"
23 
24 #include <atomic>
25 #include <mutex>
26 #include <string>
27 #include <thread>
28 
29 namespace llvm {
30 namespace orc {
31 
32 namespace SimpleRemoteEPCDefaultBootstrapSymbolNames {
33 extern const char *ExecutorSessionObjectName;
34 extern const char *DispatchFnName;
35 } // end namespace SimpleRemoteEPCDefaultBootstrapSymbolNames
36 
37 enum class SimpleRemoteEPCOpcode : uint8_t {
38  Setup,
39  Hangup,
40  Result,
43 };
44 
46  std::string TargetTriple;
49 };
50 
52 
54 public:
56 
58 
59  /// Handle receipt of a message.
60  ///
61  /// Returns an Error if the message cannot be handled, 'EndSession' if the
62  /// client will not accept any further messages, and 'ContinueSession'
63  /// otherwise.
66  SimpleRemoteEPCArgBytesVector ArgBytes) = 0;
67 
68  /// Handle a disconnection from the underlying transport. No further messages
69  /// should be sent to handleMessage after this is called.
70  /// Err may contain an Error value indicating unexpected disconnection. This
71  /// allows clients to log such errors, but no attempt should be made at
72  /// recovery (which should be handled inside the transport class, if it is
73  /// supported at all).
74  virtual void handleDisconnect(Error Err) = 0;
75 };
76 
78 public:
79  virtual ~SimpleRemoteEPCTransport();
80 
81  /// Called during setup of the client to indicate that the client is ready
82  /// to receive messages.
83  ///
84  /// Transport objects should not access the client until this method is
85  /// called.
86  virtual Error start() = 0;
87 
88  /// Send a SimpleRemoteEPC message.
89  ///
90  /// This function may be called concurrently. Subclasses should implement
91  /// locking if required for the underlying transport.
93  ExecutorAddr TagAddr, ArrayRef<char> ArgBytes) = 0;
94 
95  /// Trigger disconnection from the transport. The implementation should
96  /// respond by calling handleDisconnect on the client once disconnection
97  /// is complete. May be called more than once and from different threads.
98  virtual void disconnect() = 0;
99 };
100 
101 /// Uses read/write on FileDescriptors for transport.
103 public:
104  /// Create a FDSimpleRemoteEPCTransport using the given FDs for
105  /// reading (InFD) and writing (OutFD).
107  Create(SimpleRemoteEPCTransportClient &C, int InFD, int OutFD);
108 
109  /// Create a FDSimpleRemoteEPCTransport using the given FD for both
110  /// reading and writing.
113  return Create(C, FD, FD);
114  }
115 
116  ~FDSimpleRemoteEPCTransport() override;
117 
118  Error start() override;
119 
121  ExecutorAddr TagAddr, ArrayRef<char> ArgBytes) override;
122 
123  void disconnect() override;
124 
125 private:
127  int OutFD)
128  : C(C), InFD(InFD), OutFD(OutFD) {}
129 
130  Error readBytes(char *Dst, size_t Size, bool *IsEOF = nullptr);
131  int writeBytes(const char *Src, size_t Size);
132  void listenLoop();
133 
134  std::mutex M;
135  SimpleRemoteEPCTransportClient &C;
136  std::thread ListenerThread;
137  int InFD, OutFD;
138  std::atomic<bool> Disconnected{false};
139 };
140 
142  std::string Name;
143  bool Required;
144 };
145 
146 using RemoteSymbolLookupSet = std::vector<RemoteSymbolLookupSetElement>;
147 
151 };
152 
153 namespace shared {
154 
156 
158 
160 
161 /// Tuple containing target triple, page size, and bootstrap symbols.
165 
166 template <>
169 public:
170  static size_t size(const RemoteSymbolLookupSetElement &V) {
172  }
173 
174  static size_t serialize(SPSOutputBuffer &OB,
175  const RemoteSymbolLookupSetElement &V) {
177  }
178 
179  static size_t deserialize(SPSInputBuffer &IB,
182  }
183 };
184 
185 template <>
187 public:
188  static size_t size(const RemoteSymbolLookup &V) {
190  }
191 
192  static size_t serialize(SPSOutputBuffer &OB, const RemoteSymbolLookup &V) {
194  V.Symbols);
195  }
196 
199  IB, V.H, V.Symbols);
200  }
201 };
202 
203 template <>
206 public:
207  static size_t size(const SimpleRemoteEPCExecutorInfo &SI) {
209  SI.TargetTriple, SI.PageSize, SI.BootstrapSymbols);
210  }
211 
214  return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::serialize(
215  OB, SI.TargetTriple, SI.PageSize, SI.BootstrapSymbols);
216  }
217 
219  return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::deserialize(
220  IB, SI.TargetTriple, SI.PageSize, SI.BootstrapSymbols);
221  }
222 };
223 
224 using SPSLoadDylibSignature = SPSExpected<SPSExecutorAddr>(SPSExecutorAddr,
226 
230 
231 } // end namespace shared
232 } // end namespace orc
233 } // end namespace llvm
234 
235 #endif // LLVM_EXECUTIONENGINE_ORC_SHARED_SIMPLEREMOTEEPCUTILS_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition: ExecutorAddress.h:37
llvm::orc::shared::SPSSerializationTraits< SPSRemoteSymbolLookup, RemoteSymbolLookup >::size
static size_t size(const RemoteSymbolLookup &V)
Definition: SimpleRemoteEPCUtils.h:188
llvm::orc::shared::SPSSerializationTraits< SPSRemoteSymbolLookupSetElement, RemoteSymbolLookupSetElement >::deserialize
static size_t deserialize(SPSInputBuffer &IB, RemoteSymbolLookupSetElement &V)
Definition: SimpleRemoteEPCUtils.h:179
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::orc::FDSimpleRemoteEPCTransport::sendMessage
Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, ArrayRef< char > ArgBytes) override
Send a SimpleRemoteEPC message.
Definition: SimpleRemoteEPCUtils.cpp:86
llvm::orc::SimpleRemoteEPCOpcode
SimpleRemoteEPCOpcode
Definition: SimpleRemoteEPCUtils.h:37
llvm::orc::shared::SPSSequence
SPS tag type for sequences.
Definition: SimplePackedSerialization.h:193
llvm::orc::RemoteSymbolLookupSet
std::vector< RemoteSymbolLookupSetElement > RemoteSymbolLookupSet
Definition: SimpleRemoteEPCUtils.h:146
llvm::orc::shared::SPSSerializationTraits
Specialize to describe how to serialize/deserialize to/from the given concrete type.
Definition: SimplePackedSerialization.h:103
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::orc::shared::SPSOutputBuffer
Output char buffer with overflow check.
Definition: SimplePackedSerialization.h:53
Error.h
llvm::orc::RemoteSymbolLookup::Symbols
RemoteSymbolLookupSet Symbols
Definition: SimpleRemoteEPCUtils.h:150
llvm::orc::shared::SPSSerializationTraits< SPSRemoteSymbolLookup, RemoteSymbolLookup >::serialize
static size_t serialize(SPSOutputBuffer &OB, const RemoteSymbolLookup &V)
Definition: SimpleRemoteEPCUtils.h:192
llvm::orc::FDSimpleRemoteEPCTransport::~FDSimpleRemoteEPCTransport
~FDSimpleRemoteEPCTransport() override
Definition: SimpleRemoteEPCUtils.cpp:72
llvm::orc::RemoteSymbolLookupSetElement::Name
std::string Name
Definition: SimpleRemoteEPCUtils.h:142
llvm::orc::shared::SPSInputBuffer
Input char buffer with underflow check.
Definition: SimplePackedSerialization.h:72
llvm::orc::shared::SPSSerializationTraits< SPSRemoteSymbolLookupSetElement, RemoteSymbolLookupSetElement >::serialize
static size_t serialize(SPSOutputBuffer &OB, const RemoteSymbolLookupSetElement &V)
Definition: SimpleRemoteEPCUtils.h:174
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::orc::SimpleRemoteEPCExecutorInfo::BootstrapSymbols
StringMap< ExecutorAddr > BootstrapSymbols
Definition: SimpleRemoteEPCUtils.h:48
llvm::orc::shared::SPSTuple
SPS tag type for tuples.
Definition: SimplePackedSerialization.h:183
llvm::orc::RemoteSymbolLookupSetElement::Required
bool Required
Definition: SimpleRemoteEPCUtils.h:143
llvm::orc::shared::SPSSerializationTraits< SPSSimpleRemoteEPCExecutorInfo, SimpleRemoteEPCExecutorInfo >::deserialize
static bool deserialize(SPSInputBuffer &IB, SimpleRemoteEPCExecutorInfo &SI)
Definition: SimpleRemoteEPCUtils.h:218
llvm::orc::SimpleRemoteEPCTransport::sendMessage
virtual Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, ArrayRef< char > ArgBytes)=0
Send a SimpleRemoteEPC message.
llvm::orc::SimpleRemoteEPCTransportClient::handleDisconnect
virtual void handleDisconnect(Error Err)=0
Handle a disconnection from the underlying transport.
llvm::orc::SimpleRemoteEPCTransport
Definition: SimpleRemoteEPCUtils.h:77
llvm::orc::shared::SPSSerializationTraits< SPSSimpleRemoteEPCExecutorInfo, SimpleRemoteEPCExecutorInfo >::serialize
static bool serialize(SPSOutputBuffer &OB, const SimpleRemoteEPCExecutorInfo &SI)
Definition: SimpleRemoteEPCUtils.h:212
llvm::orc::RemoteSymbolLookupSetElement
Definition: SimpleRemoteEPCUtils.h:141
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::orc::SimpleRemoteEPCTransport::disconnect
virtual void disconnect()=0
Trigger disconnection from the transport.
llvm::orc::shared::SPSSerializationTraits< SPSRemoteSymbolLookup, RemoteSymbolLookup >::deserialize
static size_t deserialize(SPSInputBuffer &IB, RemoteSymbolLookup &V)
Definition: SimpleRemoteEPCUtils.h:197
llvm::orc::SimpleRemoteEPCOpcode::Result
@ Result
llvm::orc::SimpleRemoteEPCExecutorInfo::TargetTriple
std::string TargetTriple
Definition: SimpleRemoteEPCUtils.h:46
llvm::orc::SimpleRemoteEPCDefaultBootstrapSymbolNames::ExecutorSessionObjectName
const char * ExecutorSessionObjectName
Definition: SimpleRemoteEPCUtils.cpp:40
llvm::orc::shared::SPSExecutorAddr
Definition: ExecutorAddress.h:163
llvm::orc::FDSimpleRemoteEPCTransport::Create
static Expected< std::unique_ptr< FDSimpleRemoteEPCTransport > > Create(SimpleRemoteEPCTransportClient &C, int InFD, int OutFD)
Create a FDSimpleRemoteEPCTransport using the given FDs for reading (InFD) and writing (OutFD).
Definition: SimpleRemoteEPCUtils.cpp:50
llvm::orc::shared::SPSSerializationTraits< SPSRemoteSymbolLookupSetElement, RemoteSymbolLookupSetElement >::size
static size_t size(const RemoteSymbolLookupSetElement &V)
Definition: SimpleRemoteEPCUtils.h:170
StringMap.h
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:108
llvm::orc::SimpleRemoteEPCTransportClient::handleMessage
virtual Expected< HandleMessageAction > handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, SimpleRemoteEPCArgBytesVector ArgBytes)=0
Handle receipt of a message.
llvm::orc::RemoteSymbolLookup::H
uint64_t H
Definition: SimpleRemoteEPCUtils.h:149
llvm::orc::SimpleRemoteEPCOpcode::Hangup
@ Hangup
uint64_t
llvm::orc::shared::SPSExpected
SPS tag type for expecteds, which are either a T or a string representing an error.
Definition: SimplePackedSerialization.h:498
llvm::orc::SimpleRemoteEPCExecutorInfo
Definition: SimpleRemoteEPCUtils.h:45
size
i< reg-> size
Definition: README.txt:166
ArrayRef.h
SI
StandardInstrumentations SI(Debug, VerifyEach)
llvm::orc::shared::SPSSerializationTraits< SPSSimpleRemoteEPCExecutorInfo, SimpleRemoteEPCExecutorInfo >::size
static size_t size(const SimpleRemoteEPCExecutorInfo &SI)
Definition: SimpleRemoteEPCUtils.h:207
SimplePackedSerialization.h
llvm::orc::FDSimpleRemoteEPCTransport::start
Error start() override
Called during setup of the client to indicate that the client is ready to receive messages.
Definition: SimpleRemoteEPCUtils.cpp:78
llvm::orc::FDSimpleRemoteEPCTransport::Create
static Expected< std::unique_ptr< FDSimpleRemoteEPCTransport > > Create(SimpleRemoteEPCTransportClient &C, int FD)
Create a FDSimpleRemoteEPCTransport using the given FD for both reading and writing.
Definition: SimpleRemoteEPCUtils.h:112
llvm::ArrayRef< char >
llvm::orc::SimpleRemoteEPCTransport::~SimpleRemoteEPCTransport
virtual ~SimpleRemoteEPCTransport()
Definition: SimpleRemoteEPCUtils.cpp:47
ExecutorAddress.h
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::orc::SimpleRemoteEPCTransportClient::ContinueSession
@ ContinueSession
Definition: SimpleRemoteEPCUtils.h:55
llvm::orc::SimpleRemoteEPCTransport::start
virtual Error start()=0
Called during setup of the client to indicate that the client is ready to receive messages.
llvm::orc::SimpleRemoteEPCOpcode::CallWrapper
@ CallWrapper
llvm::orc::SimpleRemoteEPCTransportClient::HandleMessageAction
HandleMessageAction
Definition: SimpleRemoteEPCUtils.h:55
llvm::orc::SimpleRemoteEPCTransportClient::~SimpleRemoteEPCTransportClient
virtual ~SimpleRemoteEPCTransportClient()
Definition: SimpleRemoteEPCUtils.cpp:46
SmallVector.h
llvm::orc::SimpleRemoteEPCDefaultBootstrapSymbolNames::DispatchFnName
const char * DispatchFnName
Definition: SimpleRemoteEPCUtils.cpp:42
llvm::orc::SimpleRemoteEPCOpcode::Setup
@ Setup
llvm::X86II::OB
@ OB
Definition: X86BaseInfo.h:801
llvm::orc::SimpleRemoteEPCTransportClient
Definition: SimpleRemoteEPCUtils.h:53
llvm::orc::RemoteSymbolLookup
Definition: SimpleRemoteEPCUtils.h:148
llvm::orc::SimpleRemoteEPCExecutorInfo::PageSize
uint64_t PageSize
Definition: SimpleRemoteEPCUtils.h:47
llvm::orc::shared::SPSLoadDylibSignature
SPSExpected< SPSExecutorAddr >(SPSExecutorAddr, SPSString, uint64_t) SPSLoadDylibSignature
Definition: SimpleRemoteEPCUtils.h:225
llvm::orc::SimpleRemoteEPCOpcode::LastOpC
@ LastOpC
llvm::orc::shared::SPSString
SPSSequence< char > SPSString
SPS tag type for strings, which are equivalent to sequences of chars.
Definition: SimplePackedSerialization.h:196
llvm::orc::SimpleRemoteEPCTransportClient::EndSession
@ EndSession
Definition: SimpleRemoteEPCUtils.h:55
llvm::orc::FDSimpleRemoteEPCTransport
Uses read/write on FileDescriptors for transport.
Definition: SimpleRemoteEPCUtils.h:102
llvm::orc::FDSimpleRemoteEPCTransport::disconnect
void disconnect() override
Trigger disconnection from the transport.
Definition: SimpleRemoteEPCUtils.cpp:111
llvm::orc::shared::SPSArgList
A utility class for serializing to a blob from a variadic list.
Definition: SimplePackedSerialization.h:106