LLVM 22.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
28
29/// Provides ExecutorProcessControl based indirect stubs, trampoline pool and
30/// lazy call through manager.
31class EPCIndirectionUtils {
33
34public:
35 /// ABI support base class. Used to write resolver, stub, and trampoline
36 /// blocks.
38 protected:
39 ABISupport(unsigned PointerSize, unsigned TrampolineSize, unsigned StubSize,
40 unsigned StubToPointerMaxDisplacement, unsigned ResolverCodeSize)
41 : PointerSize(PointerSize), TrampolineSize(TrampolineSize),
42 StubSize(StubSize),
43 StubToPointerMaxDisplacement(StubToPointerMaxDisplacement),
44 ResolverCodeSize(ResolverCodeSize) {}
45
46 public:
47 virtual ~ABISupport();
48
49 unsigned getPointerSize() const { return PointerSize; }
50 unsigned getTrampolineSize() const { return TrampolineSize; }
51 unsigned getStubSize() const { return StubSize; }
53 return StubToPointerMaxDisplacement;
54 }
55 unsigned getResolverCodeSize() const { return ResolverCodeSize; }
56
57 virtual void writeResolverCode(char *ResolverWorkingMem,
58 ExecutorAddr ResolverTargetAddr,
59 ExecutorAddr ReentryFnAddr,
60 ExecutorAddr ReentryCtxAddr) const = 0;
61
62 virtual void writeTrampolines(char *TrampolineBlockWorkingMem,
63 ExecutorAddr TrampolineBlockTragetAddr,
64 ExecutorAddr ResolverAddr,
65 unsigned NumTrampolines) const = 0;
66
68 char *StubsBlockWorkingMem, ExecutorAddr StubsBlockTargetAddress,
69 ExecutorAddr PointersBlockTargetAddress, unsigned NumStubs) const = 0;
70
71 private:
72 unsigned PointerSize = 0;
73 unsigned TrampolineSize = 0;
74 unsigned StubSize = 0;
75 unsigned StubToPointerMaxDisplacement = 0;
76 unsigned ResolverCodeSize = 0;
77 };
78
79 /// Create using the given ABI class.
80 template <typename ORCABI>
81 static std::unique_ptr<EPCIndirectionUtils>
83
84 /// Create based on the ExecutorProcessControl triple.
87
88 /// Create based on the ExecutorProcessControl triple.
93
94 /// Return a reference to the ExecutorProcessControl object.
96
97 /// Return a reference to the ABISupport object for this instance.
98 ABISupport &getABISupport() const { return *ABI; }
99
100 /// Release memory for resources held by this instance. This *must* be called
101 /// prior to destruction of the class.
103
104 /// Write resolver code to the executor process and return its address.
105 /// This must be called before any call to createTrampolinePool or
106 /// createLazyCallThroughManager.
108 writeResolverBlock(ExecutorAddr ReentryFnAddr, ExecutorAddr ReentryCtxAddr);
109
110 /// Returns the address of the Resolver block. Returns zero if the
111 /// writeResolverBlock method has not previously been called.
112 ExecutorAddr getResolverBlockAddress() const { return ResolverBlockAddr; }
113
114 /// Create an IndirectStubsManager for the executor process.
115 LLVM_ABI std::unique_ptr<IndirectStubsManager> createIndirectStubsManager();
116
117 /// Create a TrampolinePool for the executor process.
119
120 /// Create a LazyCallThroughManager.
121 /// This function should only be called once.
124 ExecutorAddr ErrorHandlerAddr);
125
126 /// Create a LazyCallThroughManager for the executor process.
128 assert(LCTM && "createLazyCallThroughManager must be called first");
129 return *LCTM;
130 }
131
132private:
134
135 struct IndirectStubInfo {
136 IndirectStubInfo() = default;
137 IndirectStubInfo(ExecutorAddr StubAddress, ExecutorAddr PointerAddress)
138 : StubAddress(StubAddress), PointerAddress(PointerAddress) {}
139 ExecutorAddr StubAddress;
140 ExecutorAddr PointerAddress;
141 };
142
143 using IndirectStubInfoVector = std::vector<IndirectStubInfo>;
144
145 /// Create an EPCIndirectionUtils instance.
146 EPCIndirectionUtils(ExecutorProcessControl &EPC,
147 std::unique_ptr<ABISupport> ABI);
148
149 Expected<IndirectStubInfoVector> getIndirectStubs(unsigned NumStubs);
150
151 std::mutex EPCUIMutex;
152 ExecutorProcessControl &EPC;
153 std::unique_ptr<ABISupport> ABI;
154 ExecutorAddr ResolverBlockAddr;
155 FinalizedAlloc ResolverBlock;
156 std::unique_ptr<TrampolinePool> TP;
157 std::unique_ptr<LazyCallThroughManager> LCTM;
158
159 std::vector<IndirectStubInfo> AvailableIndirectStubs;
160 std::vector<FinalizedAlloc> IndirectStubAllocs;
161};
162
163/// This will call writeResolver on the given EPCIndirectionUtils instance
164/// to set up re-entry via a function that will directly return the trampoline
165/// landing address.
166///
167/// The EPCIndirectionUtils' LazyCallThroughManager must have been previously
168/// created via EPCIndirectionUtils::createLazyCallThroughManager.
169///
170/// The EPCIndirectionUtils' writeResolver method must not have been previously
171/// called.
172///
173/// This function is experimental and likely subject to revision.
175
176namespace detail {
177
178template <typename ORCABI>
180public:
182 : ABISupport(ORCABI::PointerSize, ORCABI::TrampolineSize,
183 ORCABI::StubSize, ORCABI::StubToPointerMaxDisplacement,
184 ORCABI::ResolverCodeSize) {}
185
186 void writeResolverCode(char *ResolverWorkingMem,
187 ExecutorAddr ResolverTargetAddr,
188 ExecutorAddr ReentryFnAddr,
189 ExecutorAddr ReentryCtxAddr) const override {
190 ORCABI::writeResolverCode(ResolverWorkingMem, ResolverTargetAddr,
191 ReentryFnAddr, ReentryCtxAddr);
192 }
193
194 void writeTrampolines(char *TrampolineBlockWorkingMem,
195 ExecutorAddr TrampolineBlockTargetAddr,
196 ExecutorAddr ResolverAddr,
197 unsigned NumTrampolines) const override {
198 ORCABI::writeTrampolines(TrampolineBlockWorkingMem,
199 TrampolineBlockTargetAddr, ResolverAddr,
200 NumTrampolines);
201 }
202
203 void writeIndirectStubsBlock(char *StubsBlockWorkingMem,
204 ExecutorAddr StubsBlockTargetAddress,
205 ExecutorAddr PointersBlockTargetAddress,
206 unsigned NumStubs) const override {
207 ORCABI::writeIndirectStubsBlock(StubsBlockWorkingMem,
208 StubsBlockTargetAddress,
209 PointersBlockTargetAddress, NumStubs);
210 }
211};
212
213} // end namespace detail
214
215template <typename ORCABI>
216std::unique_ptr<EPCIndirectionUtils>
218 return std::unique_ptr<EPCIndirectionUtils>(new EPCIndirectionUtils(
219 EPC, 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 Expected< std::unique_ptr< EPCIndirectionUtils > > Create(ExecutionSession &ES)
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.
static std::unique_ptr< EPCIndirectionUtils > CreateWithABI(ExecutorProcessControl &EPC)
Create using the given ABI class.
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.
LLVM_ABI TrampolinePool & getTrampolinePool()
Create a TrampolinePool for the executor process.
static LLVM_ABI Expected< std::unique_ptr< EPCIndirectionUtils > > Create(ExecutorProcessControl &EPC)
Create based on the ExecutorProcessControl triple.
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
ExecutorProcessControl & getExecutorProcessControl()
Get the ExecutorProcessControl object associated with this ExecutionSession.
Definition Core.h:1395
Represents an address in the executor process.
ExecutorProcessControl supports interaction with a JIT target process.
Manages a set of 'lazy call-through' trampolines.
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.