Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
SimpleRemoteEPCUtils.cpp
Go to the documentation of this file.
1//===------ SimpleRemoteEPCUtils.cpp - Utils for Simple Remote EPC --------===//
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#include "llvm/ExecutionEngine/Orc/Shared/SimpleRemoteEPCUtils.h"
15#include "llvm/Config/llvm-config.h"// for LLVM_ENABLE_THREADS
16#include "llvm/Support/Endian.h"
17
18#if !defined(_MSC_VER) && !defined(__MINGW32__)
19#include <unistd.h>
20#else
21#include <io.h>
22#endif
23
24namespace{
25
26structFDMsgHeader {
27staticconstexprunsigned MsgSizeOffset = 0;
28staticconstexprunsigned OpCOffset = MsgSizeOffset +sizeof(uint64_t);
29staticconstexprunsigned SeqNoOffset = OpCOffset +sizeof(uint64_t);
30staticconstexprunsigned TagAddrOffset = SeqNoOffset +sizeof(uint64_t);
31staticconstexprunsignedSize = TagAddrOffset +sizeof(uint64_t);
32};
33
34}// namespace
35
36namespacellvm {
37namespaceorc {
38namespaceSimpleRemoteEPCDefaultBootstrapSymbolNames {
39
40constchar *ExecutorSessionObjectName =
41"__llvm_orc_SimpleRemoteEPC_dispatch_ctx";
42constchar *DispatchFnName ="__llvm_orc_SimpleRemoteEPC_dispatch_fn";
43
44}// end namespace SimpleRemoteEPCDefaultBootstrapSymbolNames
45
46SimpleRemoteEPCTransportClient::~SimpleRemoteEPCTransportClient() =default;
47SimpleRemoteEPCTransport::~SimpleRemoteEPCTransport() =default;
48
49Expected<std::unique_ptr<FDSimpleRemoteEPCTransport>>
50FDSimpleRemoteEPCTransport::Create(SimpleRemoteEPCTransportClient &C,int InFD,
51int OutFD) {
52#if LLVM_ENABLE_THREADS
53if (InFD == -1)
54return make_error<StringError>("Invalid input file descriptor " +
55Twine(InFD),
56inconvertibleErrorCode());
57if (OutFD == -1)
58return make_error<StringError>("Invalid output file descriptor " +
59Twine(OutFD),
60inconvertibleErrorCode());
61 std::unique_ptr<FDSimpleRemoteEPCTransport> FDT(
62newFDSimpleRemoteEPCTransport(C, InFD, OutFD));
63return std::move(FDT);
64#else
65return make_error<StringError>("FD-based SimpleRemoteEPC transport requires "
66"thread support, but llvm was built with "
67"LLVM_ENABLE_THREADS=Off",
68inconvertibleErrorCode());
69#endif
70}
71
72FDSimpleRemoteEPCTransport::~FDSimpleRemoteEPCTransport() {
73#if LLVM_ENABLE_THREADS
74 ListenerThread.join();
75#endif
76}
77
78ErrorFDSimpleRemoteEPCTransport::start() {
79#if LLVM_ENABLE_THREADS
80 ListenerThread = std::thread([this]() { listenLoop(); });
81returnError::success();
82#endif
83llvm_unreachable("Should not be called with LLVM_ENABLE_THREADS=Off");
84}
85
86ErrorFDSimpleRemoteEPCTransport::sendMessage(SimpleRemoteEPCOpcode OpC,
87uint64_t SeqNo,
88ExecutorAddr TagAddr,
89ArrayRef<char> ArgBytes) {
90char HeaderBuffer[FDMsgHeader::Size];
91
92 *((support::ulittle64_t *)(HeaderBuffer + FDMsgHeader::MsgSizeOffset)) =
93 FDMsgHeader::Size + ArgBytes.size();
94 *((support::ulittle64_t *)(HeaderBuffer + FDMsgHeader::OpCOffset)) =
95static_cast<uint64_t>(OpC);
96 *((support::ulittle64_t *)(HeaderBuffer + FDMsgHeader::SeqNoOffset)) = SeqNo;
97 *((support::ulittle64_t *)(HeaderBuffer + FDMsgHeader::TagAddrOffset)) =
98 TagAddr.getValue();
99
100 std::lock_guard<std::mutex> Lock(M);
101if (Disconnected)
102return make_error<StringError>("FD-transport disconnected",
103inconvertibleErrorCode());
104if (int ErrNo = writeBytes(HeaderBuffer, FDMsgHeader::Size))
105returnerrorCodeToError(std::error_code(ErrNo, std::generic_category()));
106if (int ErrNo = writeBytes(ArgBytes.data(), ArgBytes.size()))
107returnerrorCodeToError(std::error_code(ErrNo, std::generic_category()));
108returnError::success();
109}
110
111voidFDSimpleRemoteEPCTransport::disconnect() {
112if (Disconnected)
113return;// Return if already disconnected.
114
115 Disconnected =true;
116bool CloseOutFD = InFD != OutFD;
117
118// Close InFD.
119while (close(InFD) == -1) {
120if (errno == EBADF)
121break;
122 }
123
124// Close OutFD.
125if (CloseOutFD) {
126while (close(OutFD) == -1) {
127if (errno == EBADF)
128break;
129 }
130 }
131}
132
133staticErrormakeUnexpectedEOFError() {
134return make_error<StringError>("Unexpected end-of-file",
135inconvertibleErrorCode());
136}
137
138Error FDSimpleRemoteEPCTransport::readBytes(char *Dst,size_tSize,
139bool *IsEOF) {
140assert((Size == 0 || Dst) &&"Attempt to read into null.");
141 ssize_t Completed = 0;
142while (Completed <static_cast<ssize_t>(Size)) {
143 ssize_tRead = ::read(InFD, Dst + Completed,Size - Completed);
144if (Read <= 0) {
145auto ErrNo = errno;
146if (Read == 0) {
147if (Completed == 0 && IsEOF) {
148 *IsEOF =true;
149returnError::success();
150 }else
151returnmakeUnexpectedEOFError();
152 }elseif (ErrNo == EAGAIN || ErrNo == EINTR)
153continue;
154else {
155 std::lock_guard<std::mutex> Lock(M);
156if (Disconnected && IsEOF) {// disconnect called, pretend this is EOF.
157 *IsEOF =true;
158returnError::success();
159 }
160returnerrorCodeToError(
161 std::error_code(ErrNo, std::generic_category()));
162 }
163 }
164 Completed +=Read;
165 }
166returnError::success();
167}
168
169int FDSimpleRemoteEPCTransport::writeBytes(constchar *Src,size_tSize) {
170assert((Size == 0 || Src) &&"Attempt to append from null.");
171 ssize_t Completed = 0;
172while (Completed <static_cast<ssize_t>(Size)) {
173 ssize_t Written =::write(OutFD, Src + Completed,Size - Completed);
174if (Written < 0) {
175auto ErrNo = errno;
176if (ErrNo == EAGAIN || ErrNo == EINTR)
177continue;
178else
179return ErrNo;
180 }
181 Completed += Written;
182 }
183return 0;
184}
185
186void FDSimpleRemoteEPCTransport::listenLoop() {
187Error Err =Error::success();
188do {
189
190char HeaderBuffer[FDMsgHeader::Size];
191// Read the header buffer.
192 {
193bool IsEOF =false;
194if (auto Err2 = readBytes(HeaderBuffer, FDMsgHeader::Size, &IsEOF)) {
195 Err =joinErrors(std::move(Err), std::move(Err2));
196break;
197 }
198if (IsEOF)
199break;
200 }
201
202// Decode header buffer.
203uint64_t MsgSize;
204SimpleRemoteEPCOpcode OpC;
205uint64_t SeqNo;
206 ExecutorAddr TagAddr;
207
208 MsgSize =
209 *((support::ulittle64_t *)(HeaderBuffer + FDMsgHeader::MsgSizeOffset));
210 OpC =static_cast<SimpleRemoteEPCOpcode>(static_cast<uint64_t>(
211 *((support::ulittle64_t *)(HeaderBuffer + FDMsgHeader::OpCOffset))));
212 SeqNo =
213 *((support::ulittle64_t *)(HeaderBuffer + FDMsgHeader::SeqNoOffset));
214 TagAddr.setValue(
215 *((support::ulittle64_t *)(HeaderBuffer + FDMsgHeader::TagAddrOffset)));
216
217if (MsgSize < FDMsgHeader::Size) {
218 Err =joinErrors(std::move(Err),
219 make_error<StringError>("Message size too small",
220inconvertibleErrorCode()));
221break;
222 }
223
224// Read the argument bytes.
225SimpleRemoteEPCArgBytesVector ArgBytes;
226 ArgBytes.resize(MsgSize - FDMsgHeader::Size);
227if (auto Err2 = readBytes(ArgBytes.data(), ArgBytes.size())) {
228 Err =joinErrors(std::move(Err), std::move(Err2));
229break;
230 }
231
232if (auto Action = C.handleMessage(OpC, SeqNo, TagAddr, ArgBytes)) {
233if (*Action ==SimpleRemoteEPCTransportClient::EndSession)
234break;
235 }else {
236 Err =joinErrors(std::move(Err), Action.takeError());
237break;
238 }
239 }while (true);
240
241// Attempt to close FDs, set Disconnected to true so that subsequent
242// sendMessage calls fail.
243disconnect();
244
245// Call up to the client to handle the disconnection.
246 C.handleDisconnect(std::move(Err));
247}
248
249}// end namespace orc
250}// end namespace llvm
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
Endian.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SimpleRemoteEPCUtils.h
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition:ArrayRef.h:168
llvm::ArrayRef::data
const T * data() const
Definition:ArrayRef.h:165
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition:Error.h:160
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition:Error.h:337
llvm::Expected
Tagged union holding either a T or a Error.
Definition:Error.h:481
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition:Twine.h:81
llvm::orc::ExecutorAddr
Represents an address in the executor process.
Definition:ExecutorAddress.h:34
llvm::orc::ExecutorAddr::getValue
uint64_t getValue() const
Definition:ExecutorAddress.h:136
llvm::orc::FDSimpleRemoteEPCTransport
Uses read/write on FileDescriptors for transport.
Definition:SimpleRemoteEPCUtils.h:103
llvm::orc::FDSimpleRemoteEPCTransport::disconnect
void disconnect() override
Trigger disconnection from the transport.
Definition:SimpleRemoteEPCUtils.cpp:111
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::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::sendMessage
Error sendMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo, ExecutorAddr TagAddr, ArrayRef< char > ArgBytes) override
Send a SimpleRemoteEPC message.
Definition:SimpleRemoteEPCUtils.cpp:86
llvm::orc::FDSimpleRemoteEPCTransport::~FDSimpleRemoteEPCTransport
~FDSimpleRemoteEPCTransport() override
Definition:SimpleRemoteEPCUtils.cpp:72
llvm::orc::SimpleRemoteEPCTransportClient
Definition:SimpleRemoteEPCUtils.h:54
llvm::orc::SimpleRemoteEPCTransportClient::EndSession
@ EndSession
Definition:SimpleRemoteEPCUtils.h:56
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::SimpleRemoteEPCTransportClient::handleDisconnect
virtual void handleDisconnect(Error Err)=0
Handle a disconnection from the underlying transport.
llvm::orc::SimpleRemoteEPCTransportClient::~SimpleRemoteEPCTransportClient
virtual ~SimpleRemoteEPCTransportClient()
llvm::orc::SimpleRemoteEPCTransport::~SimpleRemoteEPCTransport
virtual ~SimpleRemoteEPCTransport()
uint64_t
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::Check::Size
@ Size
Definition:FileCheck.h:77
llvm::lltok::Error
@ Error
Definition:LLToken.h:21
llvm::orc::SimpleRemoteEPCDefaultBootstrapSymbolNames::DispatchFnName
const char * DispatchFnName
Definition:SimpleRemoteEPCUtils.cpp:42
llvm::orc::SimpleRemoteEPCDefaultBootstrapSymbolNames::ExecutorSessionObjectName
const char * ExecutorSessionObjectName
Definition:SimpleRemoteEPCUtils.cpp:40
llvm::orc::SimpleRemoteEPCOpcode
SimpleRemoteEPCOpcode
Definition:SimpleRemoteEPCUtils.h:37
llvm::orc::SimpleRemoteEPCArgBytesVector
SmallVector< char, 128 > SimpleRemoteEPCArgBytesVector
Definition:SimpleRemoteEPCUtils.h:52
llvm::orc::MemProt::Read
@ Read
llvm::orc::makeUnexpectedEOFError
static Error makeUnexpectedEOFError()
Definition:SimpleRemoteEPCUtils.cpp:133
llvm::support::ulittle64_t
detail::packed_endian_specific_integral< uint64_t, llvm::endianness::little, unaligned > ulittle64_t
Definition:Endian.h:288
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition:Error.cpp:98
llvm::joinErrors
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition:Error.h:438
llvm::write
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
Definition:DWP.cpp:625
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition:Error.cpp:111
llvm::support::detail::packed_endian_specific_integral
Definition:Endian.h:217

Generated on Sun Jul 20 2025 09:27:55 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp