LLVM 23.0.0git
InProcessEPC.h
Go to the documentation of this file.
1//===---- InProcessEPC.h - In-process EPC for new ORC runtime ---*- 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// ExecutorProcessControl implementation for in-process JITs that use the new
10// ORC runtime (orc-rt). Interfaces with orc_rt::InProcessControllerAccess via
11// direct function calls.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_EXECUTIONENGINE_ORC_INPROCESSEPC_H
16#define LLVM_EXECUTIONENGINE_ORC_INPROCESSEPC_H
17
18#include "llvm/ADT/DenseMap.h"
23
24#include <memory>
25#include <mutex>
26
27namespace llvm::orc {
28
29/// An ExecutorProcessControl implementation for in-process JITs that use the
30/// new ORC runtime (llvm-project/orc-rt).
31///
32/// This class communicates with the runtime's InProcessControllerAccess via
33/// direct function calls through a virtual connection object.
34class LLVM_ABI InProcessEPC : public ExecutorProcessControl {
35public:
36 /// Pseudo-connection C struct. Used to facilitate calls between InProcessEPC
37 /// and InProcessControllerAccess without relying on anything but C ABI.
38 /// Must be kept in-sync with the corresponding struct in
39 /// orc_rt::InProcessControllerAccess.
40 struct Connection {
41 void (*Retain)(Connection *C) = nullptr;
42 void (*Release)(Connection *C) = nullptr;
43 void (*Disconnect)(Connection *C) = nullptr;
44 int (*EnterMessageScope)(Connection *C) = nullptr;
45 void (*LeaveMessageScope)(Connection *C) = nullptr;
46
47 /// Accessors to be set by the InProcessEPC instance.
48 void *IPEPC = nullptr;
49 void (*CallJITDispatch)(void *IPEPC, uint64_t CallId, void *HandlerTag,
50 shared::CWrapperFunctionBuffer ArgBytes) = nullptr;
51 void (*ReturnWrapperResult)(void *IPEPC, uint64_t CallId,
53 nullptr;
54
55 /// Accessors to be set by the InProcessControllerAccess instance.
56 void *IPCA = nullptr;
57 void (*CallWrapper)(void *IPCA, uint64_t CallId, void *Fn,
58 shared::CWrapperFunctionBuffer ArgBytes) = nullptr;
60 void *IPCA, uint64_t CallId,
61 shared::CWrapperFunctionBuffer ResultBytes) = nullptr;
62 };
63
64 /// Provides access to bootstrap info.
65 /// Must be kept in-sync with the corresponding struct in
66 /// orc_rt::InProcessControllerAccess.
68 uint64_t (*GetPageSize)(void *BIA) = nullptr;
69 const char *(*GetTargetTriple)(void *BIA) = nullptr;
70
71 int (*GetNextValue)(void *BIA, const char **Name, const char **ValueBytes,
72 uint64_t *ValueSize) = nullptr;
73 int (*GetNextSymbol)(void *BIA, const char **Name,
74 uint64_t *Addr) = nullptr;
75 };
76
77 /// Create a new InProcessEPC.
78 ///
79 /// If no symbol string pool is given then one will be created.
80 /// If no task dispatcher is given an InPlaceTaskDispatcher will be used.
83 std::shared_ptr<SymbolStringPool> SSP = nullptr,
84 std::unique_ptr<TaskDispatcher> D = nullptr);
85
86 ~InProcessEPC();
87
89 ArrayRef<std::string> Args) override;
90
92
93 Expected<int32_t> runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) override;
94
95 void callWrapperAsync(ExecutorAddr WrapperFnAddr,
96 IncomingWFRHandler OnComplete,
97 ArrayRef<char> ArgBuffer) override;
98
101
103
105
106 Error disconnect() override;
107
108private:
109 InProcessEPC(Connection *C, std::shared_ptr<SymbolStringPool> SSP,
110 std::unique_ptr<TaskDispatcher> D)
111 : ExecutorProcessControl(std::move(SSP), std::move(D)), C(C) {
112 C->Retain(C);
113 }
114
115 uint64_t registerPendingCallWrapperResult(IncomingWFRHandler H);
116 void doDisconnect();
117
118 // Incoming JIT-dispatch call from the ORC runtime.
119 void callJITDispatch(uint64_t CallId, void *HandlerTag,
121 static void callJITDispatchEntry(void *IPEPC, uint64_t CallId,
122 void *HandlerTag,
124
125 // Incoming wrapper function result from the ORC runtime.
126 void returnWrapperResult(uint64_t CallId,
128 static void
129 returnWrapperResultEntry(void *IPEPC, uint64_t CallId,
131
132 Connection *C;
133
134 std::mutex M;
135 uint64_t NextCallId = 0;
136 DenseMap<uint64_t, IncomingWFRHandler> PendingCallWrapperResults;
137};
138
139} // namespace llvm::orc
140
141#endif // LLVM_EXECUTIONENGINE_ORC_INPROCESSEPC_H
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
#define H(x, y, z)
Definition MD5.cpp:56
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Represents an address in the executor process.
A handler or incoming WrapperFunctionBuffers – either return values from callWrapper* calls,...
std::unique_ptr< TaskDispatcher > D
std::shared_ptr< SymbolStringPool > SSP
ExecutorProcessControl(std::shared_ptr< SymbolStringPool > SSP, std::unique_ptr< TaskDispatcher > D)
Expected< std::unique_ptr< jitlink::JITLinkMemoryManager > > createDefaultMemoryManager() override
Create a default JITLinkMemoryManager for the target process.
Expected< int32_t > runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) override
Run function with a int (*)(int) signature.
Expected< int32_t > runAsVoidFunction(ExecutorAddr VoidFnAddr) override
Run function with a int (*)(void) signature.
Expected< std::unique_ptr< DylibManager > > createDefaultDylibMgr() override
Create a default DylibManager for the target process.
void callWrapperAsync(ExecutorAddr WrapperFnAddr, IncomingWFRHandler OnComplete, ArrayRef< char > ArgBuffer) override
Run a wrapper function in the executor.
static Expected< std::unique_ptr< InProcessEPC > > Create(Connection *C, BootstrapInfoAccess *BIA, std::shared_ptr< SymbolStringPool > SSP=nullptr, std::unique_ptr< TaskDispatcher > D=nullptr)
Create a new InProcessEPC.
Error disconnect() override
Disconnect from the target process.
Expected< std::unique_ptr< MemoryAccess > > createDefaultMemoryAccess() override
Create a default MemoryAccess for the target process.
Expected< int32_t > runAsMain(ExecutorAddr MainFnAddr, ArrayRef< std::string > Args) override
Run function with a main-like signature.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860
Provides access to bootstrap info.
int(* GetNextSymbol)(void *BIA, const char **Name, uint64_t *Addr)
int(* GetNextValue)(void *BIA, const char **Name, const char **ValueBytes, uint64_t *ValueSize)
Pseudo-connection C struct.
void(* ReturnWrapperResult)(void *IPEPC, uint64_t CallId, shared::CWrapperFunctionBuffer ResultBytes)
int(* EnterMessageScope)(Connection *C)
void(* ReturnJITDispatchResult)(void *IPCA, uint64_t CallId, shared::CWrapperFunctionBuffer ResultBytes)
void * IPCA
Accessors to be set by the InProcessControllerAccess instance.
void(* CallJITDispatch)(void *IPEPC, uint64_t CallId, void *HandlerTag, shared::CWrapperFunctionBuffer ArgBytes)
void * IPEPC
Accessors to be set by the InProcessEPC instance.
void(* CallWrapper)(void *IPCA, uint64_t CallId, void *Fn, shared::CWrapperFunctionBuffer ArgBytes)
void(* Release)(Connection *C)
void(* Disconnect)(Connection *C)
void(* LeaveMessageScope)(Connection *C)
void(* Retain)(Connection *C)