1 //===-- IndirectionUtils.h - Utilities for adding indirections --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Contains utilities for adding indirections and breaking up modules.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_INDIRECTIONUTILS_H
15 #define LLVM_EXECUTIONENGINE_ORC_INDIRECTIONUTILS_H
17 #include "JITSymbol.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ExecutionEngine/RuntimeDyld.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/IR/Module.h"
28 /// @brief Base class for JITLayer independent aspects of
29 /// JITCompileCallbackManager.
30 class JITCompileCallbackManagerBase {
33 typedef std::function<TargetAddress()> CompileFtor;
34 typedef std::function<void(TargetAddress)> UpdateFtor;
36 /// @brief Handle to a newly created compile callback. Can be used to get an
37 /// IR constant representing the address of the trampoline, and to set
38 /// the compile and update actions for the callback.
39 class CompileCallbackInfo {
41 CompileCallbackInfo(TargetAddress Addr, CompileFtor &Compile,
43 : Addr(Addr), Compile(Compile), Update(Update) {}
45 TargetAddress getAddress() const { return Addr; }
46 void setCompileAction(CompileFtor Compile) {
47 this->Compile = std::move(Compile);
49 void setUpdateAction(UpdateFtor Update) {
50 this->Update = std::move(Update);
58 /// @brief Construct a JITCompileCallbackManagerBase.
59 /// @param ErrorHandlerAddress The address of an error handler in the target
60 /// process to be used if a compile callback fails.
61 /// @param NumTrampolinesPerBlock Number of trampolines to emit if there is no
62 /// available trampoline when getCompileCallback is
64 JITCompileCallbackManagerBase(TargetAddress ErrorHandlerAddress,
65 unsigned NumTrampolinesPerBlock)
66 : ErrorHandlerAddress(ErrorHandlerAddress),
67 NumTrampolinesPerBlock(NumTrampolinesPerBlock) {}
69 virtual ~JITCompileCallbackManagerBase() {}
71 /// @brief Execute the callback for the given trampoline id. Called by the JIT
72 /// to compile functions on demand.
73 TargetAddress executeCompileCallback(TargetAddress TrampolineID) {
74 TrampolineMapT::iterator I = ActiveTrampolines.find(TrampolineID);
75 // FIXME: Also raise an error in the Orc error-handler when we finally have
77 if (I == ActiveTrampolines.end())
78 return ErrorHandlerAddress;
80 // Found a callback handler. Yank this trampoline out of the active list and
81 // put it back in the available trampolines list, then try to run the
82 // handler's compile and update actions.
83 // Moving the trampoline ID back to the available list first means there's at
84 // least one available trampoline if the compile action triggers a request for
86 AvailableTrampolines.push_back(I->first);
87 auto CallbackHandler = std::move(I->second);
88 ActiveTrampolines.erase(I);
90 if (auto Addr = CallbackHandler.Compile()) {
91 CallbackHandler.Update(Addr);
94 return ErrorHandlerAddress;
97 /// @brief Get/create a compile callback with the given signature.
98 virtual CompileCallbackInfo getCompileCallback(LLVMContext &Context) = 0;
102 struct CallbackHandler {
107 TargetAddress ErrorHandlerAddress;
108 unsigned NumTrampolinesPerBlock;
110 typedef std::map<TargetAddress, CallbackHandler> TrampolineMapT;
111 TrampolineMapT ActiveTrampolines;
112 std::vector<TargetAddress> AvailableTrampolines;
115 /// @brief Manage compile callbacks.
116 template <typename JITLayerT, typename TargetT>
117 class JITCompileCallbackManager : public JITCompileCallbackManagerBase {
120 /// @brief Construct a JITCompileCallbackManager.
121 /// @param JIT JIT layer to emit callback trampolines, etc. into.
122 /// @param Context LLVMContext to use for trampoline & resolve block modules.
123 /// @param ErrorHandlerAddress The address of an error handler in the target
124 /// process to be used if a compile callback fails.
125 /// @param NumTrampolinesPerBlock Number of trampolines to allocate whenever
126 /// there is no existing callback trampoline.
127 /// (Trampolines are allocated in blocks for
129 JITCompileCallbackManager(JITLayerT &JIT, RuntimeDyld::MemoryManager &MemMgr,
130 LLVMContext &Context,
131 TargetAddress ErrorHandlerAddress,
132 unsigned NumTrampolinesPerBlock)
133 : JITCompileCallbackManagerBase(ErrorHandlerAddress,
134 NumTrampolinesPerBlock),
135 JIT(JIT), MemMgr(MemMgr) {
136 emitResolverBlock(Context);
139 /// @brief Get/create a compile callback with the given signature.
140 CompileCallbackInfo getCompileCallback(LLVMContext &Context) final {
141 TargetAddress TrampolineAddr = getAvailableTrampolineAddr(Context);
142 auto &CallbackHandler =
143 this->ActiveTrampolines[TrampolineAddr];
145 return CompileCallbackInfo(TrampolineAddr, CallbackHandler.Compile,
146 CallbackHandler.Update);
151 std::vector<std::unique_ptr<Module>>
152 SingletonSet(std::unique_ptr<Module> M) {
153 std::vector<std::unique_ptr<Module>> Ms;
154 Ms.push_back(std::move(M));
158 void emitResolverBlock(LLVMContext &Context) {
159 std::unique_ptr<Module> M(new Module("resolver_block_module",
161 TargetT::insertResolverBlock(*M, *this);
162 auto H = JIT.addModuleSet(SingletonSet(std::move(M)), &MemMgr,
163 static_cast<RuntimeDyld::SymbolResolver*>(
165 JIT.emitAndFinalize(H);
166 auto ResolverBlockSymbol =
167 JIT.findSymbolIn(H, TargetT::ResolverBlockName, false);
168 assert(ResolverBlockSymbol && "Failed to insert resolver block");
169 ResolverBlockAddr = ResolverBlockSymbol.getAddress();
172 TargetAddress getAvailableTrampolineAddr(LLVMContext &Context) {
173 if (this->AvailableTrampolines.empty())
175 assert(!this->AvailableTrampolines.empty() &&
176 "Failed to grow available trampolines.");
177 TargetAddress TrampolineAddr = this->AvailableTrampolines.back();
178 this->AvailableTrampolines.pop_back();
179 return TrampolineAddr;
182 void grow(LLVMContext &Context) {
183 assert(this->AvailableTrampolines.empty() && "Growing prematurely?");
184 std::unique_ptr<Module> M(new Module("trampoline_block", Context));
186 TargetT::insertCompileCallbackTrampolines(*M, ResolverBlockAddr,
187 this->NumTrampolinesPerBlock,
188 this->ActiveTrampolines.size());
189 auto H = JIT.addModuleSet(SingletonSet(std::move(M)), &MemMgr,
190 static_cast<RuntimeDyld::SymbolResolver*>(
192 JIT.emitAndFinalize(H);
193 for (unsigned I = 0; I < this->NumTrampolinesPerBlock; ++I) {
194 std::string Name = GetLabelName(I);
195 auto TrampolineSymbol = JIT.findSymbolIn(H, Name, false);
196 assert(TrampolineSymbol && "Failed to emit trampoline.");
197 this->AvailableTrampolines.push_back(TrampolineSymbol.getAddress());
202 RuntimeDyld::MemoryManager &MemMgr;
203 TargetAddress ResolverBlockAddr;
206 inline Constant* createIRTypedAddress(FunctionType &FT, TargetAddress Addr) {
207 Constant *AddrIntVal =
208 ConstantInt::get(Type::getInt64Ty(FT.getContext()), Addr);
209 Constant *AddrPtrVal =
210 ConstantExpr::getCast(Instruction::IntToPtr, AddrIntVal,
211 PointerType::get(&FT, 0));
215 /// @brief Get an update functor for updating the value of a named function
217 template <typename JITLayerT>
218 JITCompileCallbackManagerBase::UpdateFtor
219 getLocalFPUpdater(JITLayerT &JIT, typename JITLayerT::ModuleSetHandleT H,
221 // FIXME: Move-capture Name once we can use C++14.
222 return [=,&JIT](TargetAddress Addr) {
223 auto FPSym = JIT.findSymbolIn(H, Name, true);
224 assert(FPSym && "Cannot find function pointer to update.");
225 void *FPAddr = reinterpret_cast<void*>(
226 static_cast<uintptr_t>(FPSym.getAddress()));
227 memcpy(FPAddr, &Addr, sizeof(uintptr_t));
231 GlobalVariable* createImplPointer(Function &F, const Twine &Name,
232 Constant *Initializer);
234 void makeStub(Function &F, GlobalVariable &ImplPointer);
236 typedef std::map<Module*, DenseSet<const GlobalValue*>> ModulePartitionMap;
238 void partition(Module &M, const ModulePartitionMap &PMap);
240 /// @brief Struct for trivial "complete" partitioning of a module.
241 class FullyPartitionedModule {
243 std::unique_ptr<Module> GlobalVars;
244 std::unique_ptr<Module> Commons;
245 std::vector<std::unique_ptr<Module>> Functions;
247 FullyPartitionedModule() = default;
248 FullyPartitionedModule(FullyPartitionedModule &&S)
249 : GlobalVars(std::move(S.GlobalVars)), Commons(std::move(S.Commons)),
250 Functions(std::move(S.Functions)) {}
253 FullyPartitionedModule fullyPartition(Module &M);
255 } // End namespace orc.
256 } // End namespace llvm.
258 #endif // LLVM_EXECUTIONENGINE_ORC_INDIRECTIONUTILS_H