LLVM 20.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"
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
29namespace llvm {
30namespace orc {
31
32namespace SimpleRemoteEPCDefaultBootstrapSymbolNames {
33extern const char *ExecutorSessionObjectName;
34extern const char *DispatchFnName;
35} // end namespace SimpleRemoteEPCDefaultBootstrapSymbolNames
36
37enum class SimpleRemoteEPCOpcode : uint8_t {
38 Setup,
39 Hangup,
40 Result,
43};
44
46 std::string TargetTriple;
50};
51
53
55public:
57
59
60 /// Handle receipt of a message.
61 ///
62 /// Returns an Error if the message cannot be handled, 'EndSession' if the
63 /// client will not accept any further messages, and 'ContinueSession'
64 /// otherwise.
68
69 /// Handle a disconnection from the underlying transport. No further messages
70 /// should be sent to handleMessage after this is called.
71 /// Err may contain an Error value indicating unexpected disconnection. This
72 /// allows clients to log such errors, but no attempt should be made at
73 /// recovery (which should be handled inside the transport class, if it is
74 /// supported at all).
75 virtual void handleDisconnect(Error Err) = 0;
76};
77
79public:
81
82 /// Called during setup of the client to indicate that the client is ready
83 /// to receive messages.
84 ///
85 /// Transport objects should not access the client until this method is
86 /// called.
87 virtual Error start() = 0;
88
89 /// Send a SimpleRemoteEPC message.
90 ///
91 /// This function may be called concurrently. Subclasses should implement
92 /// locking if required for the underlying transport.
94 ExecutorAddr TagAddr, ArrayRef<char> ArgBytes) = 0;
95
96 /// Trigger disconnection from the transport. The implementation should
97 /// respond by calling handleDisconnect on the client once disconnection
98 /// is complete. May be called more than once and from different threads.
99 virtual void disconnect() = 0;
100};
101
102/// Uses read/write on FileDescriptors for transport.
104public:
105 /// Create a FDSimpleRemoteEPCTransport using the given FDs for
106 /// reading (InFD) and writing (OutFD).
108 Create(SimpleRemoteEPCTransportClient &C, int InFD, int OutFD);
109
110 /// Create a FDSimpleRemoteEPCTransport using the given FD for both
111 /// reading and writing.
114 return Create(C, FD, FD);
115 }
116
118
119 Error start() override;
120
122 ExecutorAddr TagAddr, ArrayRef<char> ArgBytes) override;
123
124 void disconnect() override;
125
126private:
128 int OutFD)
129 : C(C), InFD(InFD), OutFD(OutFD) {}
130
131 Error readBytes(char *Dst, size_t Size, bool *IsEOF = nullptr);
132 int writeBytes(const char *Src, size_t Size);
133 void listenLoop();
134
135 std::mutex M;
136 SimpleRemoteEPCTransportClient &C;
137 std::thread ListenerThread;
138 int InFD, OutFD;
139 std::atomic<bool> Disconnected{false};
140};
141
143 std::string Name;
145};
146
147using RemoteSymbolLookupSet = std::vector<RemoteSymbolLookupSetElement>;
148
152};
153
154namespace shared {
155
157
159
161
162/// Tuple containing target triple, page size, and bootstrap symbols.
167
168template <>
171public:
172 static size_t size(const RemoteSymbolLookupSetElement &V) {
173 return SPSArgList<SPSString, bool>::size(V.Name, V.Required);
174 }
175
176 static size_t serialize(SPSOutputBuffer &OB,
178 return SPSArgList<SPSString, bool>::serialize(OB, V.Name, V.Required);
179 }
180
181 static size_t deserialize(SPSInputBuffer &IB,
183 return SPSArgList<SPSString, bool>::deserialize(IB, V.Name, V.Required);
184 }
185};
186
187template <>
189public:
190 static size_t size(const RemoteSymbolLookup &V) {
192 }
193
194 static size_t serialize(SPSOutputBuffer &OB, const RemoteSymbolLookup &V) {
196 V.Symbols);
197 }
198
201 IB, V.H, V.Symbols);
202 }
203};
204
205template <>
208public:
209 static size_t size(const SimpleRemoteEPCExecutorInfo &SI) {
210 return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::size(
211 SI.TargetTriple, SI.PageSize, SI.BootstrapMap, SI.BootstrapSymbols);
212 }
213
214 static bool serialize(SPSOutputBuffer &OB,
215 const SimpleRemoteEPCExecutorInfo &SI) {
216 return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::serialize(
217 OB, SI.TargetTriple, SI.PageSize, SI.BootstrapMap, SI.BootstrapSymbols);
218 }
219
221 return SPSSimpleRemoteEPCExecutorInfo::AsArgList ::deserialize(
222 IB, SI.TargetTriple, SI.PageSize, SI.BootstrapMap, SI.BootstrapSymbols);
223 }
224};
225
228
232
233} // end namespace shared
234} // end namespace orc
235} // end namespace llvm
236
237#endif // LLVM_EXECUTIONENGINE_ORC_SHARED_SIMPLEREMOTEEPCUTILS_H
This file defines the StringMap class.
uint64_t Size
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
Represents an address in the executor process.
Uses read/write on FileDescriptors for transport.
void disconnect() override
Trigger disconnection from the transport.
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).
Error start() override
Called during setup of the client to indicate that the client is ready to receive messages.
static Expected< std::unique_ptr< FDSimpleRemoteEPCTransport > > Create(SimpleRemoteEPCTransportClient &C, int FD)
Create a FDSimpleRemoteEPCTransport using the given FD for both reading and writing.
Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, ArrayRef< char > ArgBytes) override
Send a SimpleRemoteEPC message.
virtual Expected< HandleMessageAction > handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, SimpleRemoteEPCArgBytesVector ArgBytes)=0
Handle receipt of a message.
virtual void handleDisconnect(Error Err)=0
Handle a disconnection from the underlying transport.
virtual void disconnect()=0
Trigger disconnection from the transport.
virtual Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, ArrayRef< char > ArgBytes)=0
Send a SimpleRemoteEPC message.
virtual Error start()=0
Called during setup of the client to indicate that the client is ready to receive messages.
A utility class for serializing to a blob from a variadic list.
SPS tag type for expecteds, which are either a T or a string representing an error.
Input char buffer with underflow check.
Output char buffer with overflow check.
static size_t serialize(SPSOutputBuffer &OB, const RemoteSymbolLookup &V)
Specialize to describe how to serialize/deserialize to/from the given concrete type.
SPSSequence< char > SPSString
SPS tag type for strings, which are equivalent to sequences of chars.
std::vector< RemoteSymbolLookupSetElement > RemoteSymbolLookupSet
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
StringMap< ExecutorAddr > BootstrapSymbols
StringMap< std::vector< char > > BootstrapMap