LLVM 23.0.0git
EPCIndirectionUtils.h
Go to the documentation of this file.
1//===--- EPCIndirectionUtils.h - EPC based indirection utils ----*- 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// Indirection utilities (stubs, trampolines, lazy call-throughs) that use the
10// ExecutorProcessControl API to interact with the executor process.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_EXECUTIONENGINE_ORC_EPCINDIRECTIONUTILS_H
15#define LLVM_EXECUTIONENGINE_ORC_EPCINDIRECTIONUTILS_H
16
21
22#include <mutex>
23
24namespace llvm {
25namespace orc {
26
28class MemoryAccess;
29
30/// Provides ExecutorProcessControl based indirect stubs, trampoline pool and
31/// lazy call through manager.
32class EPCIndirectionUtils {
34
35public:
36 /// ABI support base class. Used to write resolver, stub, and trampoline
37 /// blocks.
39 protected:
40 ABISupport(unsigned PointerSize, unsigned TrampolineSize, unsigned StubSize,
41 unsigned StubToPointerMaxDisplacement, unsigned ResolverCodeSize)
42 : PointerSize(PointerSize), TrampolineSize(TrampolineSize),
43 StubSize(StubSize),
44 StubToPointerMaxDisplacement(StubToPointerMaxDisplacement),
45 ResolverCodeSize(ResolverCodeSize) {}
46
47 public:
48 virtual ~ABISupport();
49
50 unsigned getPointerSize() const { return PointerSize; }
51 unsigned getTrampolineSize() const { return TrampolineSize; }
52 unsigned getStubSize() const { return StubSize; }
54 return StubToPointerMaxDisplacement;
55 }
56 unsigned getResolverCodeSize() const { return ResolverCodeSize; }
57
58 virtual void writeResolverCode(char *ResolverWorkingMem,
59 ExecutorAddr ResolverTargetAddr,
60 ExecutorAddr ReentryFnAddr,
61 ExecutorAddr ReentryCtxAddr) const = 0;
62
63 virtual void writeTrampolines(char *TrampolineBlockWorkingMem,
64 ExecutorAddr TrampolineBlockTragetAddr,
65 ExecutorAddr ResolverAddr,
66 unsigned NumTrampolines) const = 0;
67
69 char *StubsBlockWorkingMem, ExecutorAddr StubsBlockTargetAddress,
70 ExecutorAddr PointersBlockTargetAddress, unsigned NumStubs) const = 0;
71
72 private:
73 unsigned PointerSize = 0;
74 unsigned TrampolineSize = 0;
75 unsigned StubSize = 0;
76 unsigned StubToPointerMaxDisplacement = 0;
77 unsigned ResolverCodeSize = 0;
78 };
79
80 /// Create using the given ABI class.
81 template <typename ORCABI>
82 static std::unique_ptr<EPCIndirectionUtils>
84
85 /// Create based on the ExecutorProcessControl triple.
88
89 /// Return a reference to the ExecutorProcessControl object.
91
92 /// Return a reference to the MemoryAccess object for this instance.
93 MemoryAccess &getMemoryAccess() const { return MemAccess; }
94
95 /// Return a reference to the ABISupport object for this instance.
96 ABISupport &getABISupport() const { return *ABI; }
97
98 /// Release memory for resources held by this instance. This *must* be called
99 /// prior to destruction of the class.
101
102 /// Write resolver code to the executor process and return its address.
103 /// This must be called before any call to createTrampolinePool or
104 /// createLazyCallThroughManager.
106 writeResolverBlock(ExecutorAddr ReentryFnAddr, ExecutorAddr ReentryCtxAddr);
107
108 /// Returns the address of the Resolver block. Returns zero if the
109 /// writeResolverBlock method has not previously been called.
110 ExecutorAddr getResolverBlockAddress() const { return ResolverBlockAddr; }
111
112 /// Create an IndirectStubsManager for the executor process.
113 LLVM_ABI std::unique_ptr<IndirectStubsManager> createIndirectStubsManager();
114
115 /// Create a TrampolinePool for the executor process.
117
118 /// Create a LazyCallThroughManager.
119 /// This function should only be called once.
122 ExecutorAddr ErrorHandlerAddr);
123
124 /// Create a LazyCallThroughManager for the executor process.
126 assert(LCTM && "createLazyCallThroughManager must be called first");
127 return *LCTM;
128 }
129
130private:
132
133 struct IndirectStubInfo {
134 IndirectStubInfo() = default;
135 IndirectStubInfo(ExecutorAddr StubAddress, ExecutorAddr PointerAddress)
136 : StubAddress(StubAddress), PointerAddress(PointerAddress) {}
137 ExecutorAddr StubAddress;
138 ExecutorAddr PointerAddress;
139 };
140
141 using IndirectStubInfoVector = std::vector<IndirectStubInfo>;
142
143 /// Create an EPCIndirectionUtils instance.
144 EPCIndirectionUtils(ExecutorProcessControl &EPC, MemoryAccess &MemAccess,
145 std::unique_ptr<ABISupport> ABI);
146
147 Expected<IndirectStubInfoVector> getIndirectStubs(unsigned NumStubs);
148
149 std::mutex EPCUIMutex;
150 ExecutorProcessControl &EPC;
151 MemoryAccess &MemAccess;
152 std::unique_ptr<ABISupport> ABI;
153 ExecutorAddr ResolverBlockAddr;
154 FinalizedAlloc ResolverBlock;
155 std::unique_ptr<TrampolinePool> TP;
156 std::unique_ptr<LazyCallThroughManager> LCTM;
157
158 std::vector<IndirectStubInfo> AvailableIndirectStubs;
159 std::vector<FinalizedAlloc> IndirectStubAllocs;
160};
161
162/// This will call writeResolver on the given EPCIndirectionUtils instance
163/// to set up re-entry via a function that will directly return the trampoline
164/// landing address.
165///
166/// The EPCIndirectionUtils' LazyCallThroughManager must have been previously
167/// created via EPCIndirectionUtils::createLazyCallThroughManager.
168///
169/// The EPCIndirectionUtils' writeResolver method must not have been previously
170/// called.
171///
172/// This function is experimental and likely subject to revision.
174
175namespace detail {
176
177template <typename ORCABI>
179public:
181 : ABISupport(ORCABI::PointerSize, ORCABI::TrampolineSize,
182 ORCABI::StubSize, ORCABI::StubToPointerMaxDisplacement,
183 ORCABI::ResolverCodeSize) {}
184
185 void writeResolverCode(char *ResolverWorkingMem,
186 ExecutorAddr ResolverTargetAddr,
187 ExecutorAddr ReentryFnAddr,
188 ExecutorAddr ReentryCtxAddr) const override {
189 ORCABI::writeResolverCode(ResolverWorkingMem, ResolverTargetAddr,
190 ReentryFnAddr, ReentryCtxAddr);
191 }
192
193 void writeTrampolines(char *TrampolineBlockWorkingMem,
194 ExecutorAddr TrampolineBlockTargetAddr,
195 ExecutorAddr ResolverAddr,
196 unsigned NumTrampolines) const override {
197 ORCABI::writeTrampolines(TrampolineBlockWorkingMem,
198 TrampolineBlockTargetAddr, ResolverAddr,
199 NumTrampolines);
200 }
201
202 void writeIndirectStubsBlock(char *StubsBlockWorkingMem,
203 ExecutorAddr StubsBlockTargetAddress,
204 ExecutorAddr PointersBlockTargetAddress,
205 unsigned NumStubs) const override {
206 ORCABI::writeIndirectStubsBlock(StubsBlockWorkingMem,
207 StubsBlockTargetAddress,
208 PointersBlockTargetAddress, NumStubs);
209 }
210};
211
212} // end namespace detail
213
214template <typename ORCABI>
215std::unique_ptr<EPCIndirectionUtils>
217 MemoryAccess &MemAccess) {
218 return std::unique_ptr<EPCIndirectionUtils>(new EPCIndirectionUtils(
219 EPC, MemAccess, std::make_unique<detail::ABISupportImpl<ORCABI>>()));
220}
221
222} // end namespace orc
223} // end namespace llvm
224
225#endif // LLVM_EXECUTIONENGINE_ORC_EPCINDIRECTIONUTILS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
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
virtual void writeIndirectStubsBlock(char *StubsBlockWorkingMem, ExecutorAddr StubsBlockTargetAddress, ExecutorAddr PointersBlockTargetAddress, unsigned NumStubs) const =0
ABISupport(unsigned PointerSize, unsigned TrampolineSize, unsigned StubSize, unsigned StubToPointerMaxDisplacement, unsigned ResolverCodeSize)
virtual void writeResolverCode(char *ResolverWorkingMem, ExecutorAddr ResolverTargetAddr, ExecutorAddr ReentryFnAddr, ExecutorAddr ReentryCtxAddr) const =0
virtual void writeTrampolines(char *TrampolineBlockWorkingMem, ExecutorAddr TrampolineBlockTragetAddr, ExecutorAddr ResolverAddr, unsigned NumTrampolines) const =0
Provides ExecutorProcessControl based indirect stubs, trampoline pool and lazy call through manager.
LLVM_ABI std::unique_ptr< IndirectStubsManager > createIndirectStubsManager()
Create an IndirectStubsManager for the executor process.
static LLVM_ABI Expected< std::unique_ptr< EPCIndirectionUtils > > Create(ExecutorProcessControl &EPC, MemoryAccess &MemAccess)
Create based on the ExecutorProcessControl triple.
LLVM_ABI Expected< ExecutorAddr > writeResolverBlock(ExecutorAddr ReentryFnAddr, ExecutorAddr ReentryCtxAddr)
Write resolver code to the executor process and return its address.
LazyCallThroughManager & getLazyCallThroughManager()
Create a LazyCallThroughManager for the executor process.
MemoryAccess & getMemoryAccess() const
Return a reference to the MemoryAccess object for this instance.
ExecutorProcessControl & getExecutorProcessControl() const
Return a reference to the ExecutorProcessControl object.
LLVM_ABI LazyCallThroughManager & createLazyCallThroughManager(ExecutionSession &ES, ExecutorAddr ErrorHandlerAddr)
Create a LazyCallThroughManager.
LLVM_ABI Error cleanup()
Release memory for resources held by this instance.
static std::unique_ptr< EPCIndirectionUtils > CreateWithABI(ExecutorProcessControl &EPC, MemoryAccess &MemAccess)
Create using the given ABI class.
LLVM_ABI TrampolinePool & getTrampolinePool()
Create a TrampolinePool for the executor process.
ABISupport & getABISupport() const
Return a reference to the ABISupport object for this instance.
ExecutorAddr getResolverBlockAddress() const
Returns the address of the Resolver block.
An ExecutionSession represents a running JIT program.
Definition Core.h:1355
Represents an address in the executor process.
ExecutorProcessControl supports interaction with a JIT target process.
Manages a set of 'lazy call-through' trampolines.
APIs for manipulating memory in the target process.
Base class for pools of compiler re-entry trampolines.
void writeTrampolines(char *TrampolineBlockWorkingMem, ExecutorAddr TrampolineBlockTargetAddr, ExecutorAddr ResolverAddr, unsigned NumTrampolines) const override
void writeIndirectStubsBlock(char *StubsBlockWorkingMem, ExecutorAddr StubsBlockTargetAddress, ExecutorAddr PointersBlockTargetAddress, unsigned NumStubs) const override
void writeResolverCode(char *ResolverWorkingMem, ExecutorAddr ResolverTargetAddr, ExecutorAddr ReentryFnAddr, ExecutorAddr ReentryCtxAddr) const override
LLVM_ABI Error setUpInProcessLCTMReentryViaEPCIU(EPCIndirectionUtils &EPCIU)
This will call writeResolver on the given EPCIndirectionUtils instance to set up re-entry via a funct...
This is an optimization pass for GlobalISel generic memory operations.