1 //===- CompileOnDemandLayer.h - Compile each function on demand -*- 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 // JIT layer for breaking up modules and inserting callbacks to allow
11 // individual functions to be compiled on demand.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
16 #define LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
18 //#include "CloneSubModule.h"
19 #include "IndirectionUtils.h"
20 #include "LambdaResolver.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
23 #include "llvm/Transforms/Utils/Cloning.h"
27 #include "llvm/Support/Debug.h"
32 /// @brief Compile-on-demand layer.
34 /// When a module is added to this layer a stub is created for each of its
35 /// function definitions. The stubs and other global values are immediately
36 /// added to the layer below. When a stub is called it triggers the extraction
37 /// of the function body from the original module. The extracted body is then
38 /// compiled and executed.
39 template <typename BaseLayerT, typename CompileCallbackMgrT>
40 class CompileOnDemandLayer {
43 // Utility class for MapValue. Only materializes declarations for global
45 class GlobalDeclMaterializer : public ValueMaterializer {
47 GlobalDeclMaterializer(Module &Dst) : Dst(Dst) {}
48 Value* materializeValueFor(Value *V) final {
49 if (auto *GV = dyn_cast<GlobalVariable>(V))
50 return cloneGlobalVariableDecl(Dst, *GV);
51 else if (auto *F = dyn_cast<Function>(V))
52 return cloneFunctionDecl(Dst, *F);
60 typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;
61 class UncompiledPartition;
65 // This struct contains the handles for the global values and stubs (which
66 // cover the external symbols of the original module), plus the handes for
67 // each of the extracted partitions. These handleds are used for lookup (only
68 // the globals/stubs module is searched) and memory management. The actual
69 // searching and resource management are handled by the LogicalDylib that owns
71 struct LogicalModule {
74 LogicalModule(LogicalModule &&Other)
75 : SrcM(std::move(Other.SrcM)),
76 GVsAndStubsHandle(std::move(Other.GVsAndStubsHandle)),
77 ImplHandles(std::move(Other.ImplHandles)) {}
79 std::unique_ptr<Module> SrcM;
80 BaseLayerModuleSetHandleT GVsAndStubsHandle;
81 std::vector<BaseLayerModuleSetHandleT> ImplHandles;
86 // This class handles symbol resolution and resource management for a set of
87 // modules that were added together as a logical dylib.
89 // A logical dylib contains one-or-more LogicalModules plus a set of
90 // UncompiledPartitions. LogicalModules support symbol resolution and resource
91 // management for for code that has already been emitted. UncompiledPartitions
92 // represent code that has not yet been compiled.
95 friend class UncompiledPartition;
96 typedef std::list<LogicalModule> LogicalModuleList;
99 typedef unsigned UncompiledPartitionID;
100 typedef typename LogicalModuleList::iterator LMHandle;
102 // Construct a logical dylib.
103 LogicalDylib(CompileOnDemandLayer &CODLayer) : CODLayer(CODLayer) { }
105 // Delete this logical dylib, release logical module resources.
106 virtual ~LogicalDylib() {
107 releaseLogicalModuleResources();
110 // Get a reference to the containing layer.
111 CompileOnDemandLayer& getCODLayer() { return CODLayer; }
113 // Get a reference to the base layer.
114 BaseLayerT& getBaseLayer() { return CODLayer.BaseLayer; }
116 // Start a new context for a single logical module.
117 LMHandle createLogicalModule() {
118 LogicalModules.push_back(LogicalModule());
119 return std::prev(LogicalModules.end());
122 // Set the global-values-and-stubs module handle for this logical module.
123 void setGVsAndStubsHandle(LMHandle LMH, BaseLayerModuleSetHandleT H) {
124 LMH->GVsAndStubsHandle = H;
127 // Return the global-values-and-stubs module handle for this logical module.
128 BaseLayerModuleSetHandleT getGVsAndStubsHandle(LMHandle LMH) {
129 return LMH->GVsAndStubsHandle;
132 // Add a handle to a module containing lazy function bodies to the given
134 void addToLogicalModule(LMHandle LMH, BaseLayerModuleSetHandleT H) {
135 LMH->ImplHandles.push_back(H);
138 // Create an UncompiledPartition attached to this LogicalDylib.
139 UncompiledPartition& createUncompiledPartition(LMHandle LMH,
140 std::shared_ptr<Module> SrcM);
142 // Take ownership of the given UncompiledPartition from the logical dylib.
143 std::unique_ptr<UncompiledPartition>
144 takeUPOwnership(UncompiledPartitionID ID);
146 // Look up a symbol in this context.
147 JITSymbol findSymbolInternally(LMHandle LMH, const std::string &Name) {
148 if (auto Symbol = getBaseLayer().findSymbolIn(LMH->GVsAndStubsHandle,
152 for (auto I = LogicalModules.begin(), E = LogicalModules.end(); I != E;
155 if (auto Symbol = getBaseLayer().findSymbolIn(I->GVsAndStubsHandle,
162 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
163 for (auto &LM : LogicalModules)
164 if (auto Symbol = getBaseLayer().findSymbolIn(LM.GVsAndStubsHandle,
166 ExportedSymbolsOnly))
171 // Find an external symbol (via the user supplied SymbolResolver).
172 virtual RuntimeDyld::SymbolInfo
173 findSymbolExternally(const std::string &Name) const = 0;
177 void releaseLogicalModuleResources() {
178 for (auto I = LogicalModules.begin(), E = LogicalModules.end(); I != E;
180 getBaseLayer().removeModuleSet(I->GVsAndStubsHandle);
181 for (auto H : I->ImplHandles)
182 getBaseLayer().removeModuleSet(H);
186 CompileOnDemandLayer &CODLayer;
187 LogicalModuleList LogicalModules;
188 std::vector<std::unique_ptr<UncompiledPartition>> UncompiledPartitions;
191 template <typename ResolverPtrT>
192 class LogicalDylibImpl : public LogicalDylib {
194 LogicalDylibImpl(CompileOnDemandLayer &CODLayer, ResolverPtrT Resolver)
195 : LogicalDylib(CODLayer), Resolver(std::move(Resolver)) {}
197 RuntimeDyld::SymbolInfo
198 findSymbolExternally(const std::string &Name) const override {
199 return Resolver->findSymbol(Name);
203 ResolverPtrT Resolver;
206 template <typename ResolverPtrT>
207 static std::unique_ptr<LogicalDylib>
208 createLogicalDylib(CompileOnDemandLayer &CODLayer,
209 ResolverPtrT Resolver) {
210 typedef LogicalDylibImpl<ResolverPtrT> Impl;
211 return llvm::make_unique<Impl>(CODLayer, std::move(Resolver));
214 // Uncompiled partition.
216 // Represents one as-yet uncompiled portion of a module.
217 class UncompiledPartition {
220 struct PartitionEntry {
221 PartitionEntry(Function *F, TargetAddress CallbackID)
222 : F(F), CallbackID(CallbackID) {}
224 TargetAddress CallbackID;
227 typedef std::vector<PartitionEntry> PartitionEntryList;
229 // Creates an uncompiled partition with the list of functions that make up
231 UncompiledPartition(LogicalDylib &LD, typename LogicalDylib::LMHandle LMH,
232 std::shared_ptr<Module> SrcM)
233 : LD(LD), LMH(LMH), SrcM(std::move(SrcM)), ID(~0U) {}
235 ~UncompiledPartition() {
236 // FIXME: When we want to support threaded lazy compilation we'll need to
237 // lock the callback manager here.
238 auto &CCMgr = LD.getCODLayer().CompileCallbackMgr;
239 for (auto PEntry : PartitionEntries)
240 CCMgr.releaseCompileCallback(PEntry.CallbackID);
243 // Set the ID for this partition.
244 void setID(typename LogicalDylib::UncompiledPartitionID ID) {
248 // Set the function set and callbacks for this partition.
249 void setPartitionEntries(PartitionEntryList PartitionEntries) {
250 this->PartitionEntries = std::move(PartitionEntries);
253 // Handle a compile callback for the function at index FnIdx.
254 TargetAddress compile(unsigned FnIdx) {
255 // Take ownership of self. This will ensure we delete the partition and
256 // free all its resources once we're done compiling.
257 std::unique_ptr<UncompiledPartition> This = LD.takeUPOwnership(ID);
259 // Release all other compile callbacks for this partition.
260 // We skip the callback for this function because that's the one that
261 // called us, and the callback manager will already have removed it.
262 auto &CCMgr = LD.getCODLayer().CompileCallbackMgr;
263 for (unsigned I = 0; I < PartitionEntries.size(); ++I)
265 CCMgr.releaseCompileCallback(PartitionEntries[I].CallbackID);
267 // Grab the name of the function being called here.
268 Function *F = PartitionEntries[FnIdx].F;
269 std::string CalledFnName = Mangle(F->getName(), SrcM->getDataLayout());
271 // Extract the function and add it to the base layer.
272 auto PartitionImplH = emitPartition();
273 LD.addToLogicalModule(LMH, PartitionImplH);
275 // Update body pointers.
276 // FIXME: When we start supporting remote lazy jitting this will need to
277 // be replaced with a user-supplied callback for updating the
279 TargetAddress CalledAddr = 0;
280 for (unsigned I = 0; I < PartitionEntries.size(); ++I) {
281 auto F = PartitionEntries[I].F;
282 std::string FName(F->getName());
284 LD.getBaseLayer().findSymbolIn(PartitionImplH,
285 Mangle(FName, SrcM->getDataLayout()),
288 LD.getBaseLayer().findSymbolIn(LD.getGVsAndStubsHandle(LMH),
289 Mangle(FName + "$orc_addr",
290 SrcM->getDataLayout()),
292 assert(FnBodySym && "Couldn't find function body.");
293 assert(FnPtrSym && "Couldn't find function body pointer.");
295 auto FnBodyAddr = FnBodySym.getAddress();
296 void *FnPtrAddr = reinterpret_cast<void*>(
297 static_cast<uintptr_t>(FnPtrSym.getAddress()));
299 // If this is the function we're calling record the address so we can
300 // return it from this function.
302 CalledAddr = FnBodyAddr;
304 memcpy(FnPtrAddr, &FnBodyAddr, sizeof(uintptr_t));
307 // Finally, clear the partition structure so we don't try to
308 // double-release the callbacks in the UncompiledPartition destructor.
309 PartitionEntries.clear();
316 BaseLayerModuleSetHandleT emitPartition() {
317 // Create the module.
318 std::string NewName(SrcM->getName());
319 for (auto &PEntry : PartitionEntries) {
321 NewName += PEntry.F->getName();
323 auto PM = llvm::make_unique<Module>(NewName, SrcM->getContext());
324 PM->setDataLayout(SrcM->getDataLayout());
325 ValueToValueMapTy VMap;
326 GlobalDeclMaterializer GDM(*PM);
328 // Create decls in the new module.
329 for (auto &PEntry : PartitionEntries)
330 cloneFunctionDecl(*PM, *PEntry.F, &VMap);
332 // Move the function bodies.
333 for (auto &PEntry : PartitionEntries)
334 moveFunctionBody(*PEntry.F, VMap);
336 // Create memory manager and symbol resolver.
337 auto MemMgr = llvm::make_unique<SectionMemoryManager>();
338 auto Resolver = createLambdaResolver(
339 [this](const std::string &Name) {
340 if (auto Symbol = LD.findSymbolInternally(LMH, Name))
341 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
343 return LD.findSymbolExternally(Name);
345 [this](const std::string &Name) {
346 if (auto Symbol = LD.findSymbolInternally(LMH, Name))
347 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
349 return RuntimeDyld::SymbolInfo(nullptr);
351 std::vector<std::unique_ptr<Module>> PartMSet;
352 PartMSet.push_back(std::move(PM));
353 return LD.getBaseLayer().addModuleSet(std::move(PartMSet),
355 std::move(Resolver));
359 typename LogicalDylib::LMHandle LMH;
360 std::shared_ptr<Module> SrcM;
361 typename LogicalDylib::UncompiledPartitionID ID;
362 PartitionEntryList PartitionEntries;
365 typedef std::list<std::unique_ptr<LogicalDylib>> LogicalDylibList;
368 /// @brief Handle to a set of loaded modules.
369 typedef typename LogicalDylibList::iterator ModuleSetHandleT;
371 /// @brief Construct a compile-on-demand layer instance.
372 CompileOnDemandLayer(BaseLayerT &BaseLayer, CompileCallbackMgrT &CallbackMgr)
373 : BaseLayer(BaseLayer), CompileCallbackMgr(CallbackMgr) {}
375 /// @brief Add a module to the compile-on-demand layer.
376 template <typename ModuleSetT, typename MemoryManagerPtrT,
377 typename SymbolResolverPtrT>
378 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
379 MemoryManagerPtrT MemMgr,
380 SymbolResolverPtrT Resolver) {
382 assert(MemMgr == nullptr &&
383 "User supplied memory managers not supported with COD yet.");
385 LogicalDylibs.push_back(createLogicalDylib(*this, std::move(Resolver)));
387 // Process each of the modules in this module set.
389 std::vector<std::vector<Function*>> Partitioning;
391 if (F.isDeclaration())
393 Partitioning.emplace_back(1, &F);
395 addLogicalModule(*LogicalDylibs.back(),
396 std::shared_ptr<Module>(std::move(M)),
397 std::move(Partitioning));
400 return std::prev(LogicalDylibs.end());
403 /// @brief Remove the module represented by the given handle.
405 /// This will remove all modules in the layers below that were derived from
406 /// the module represented by H.
407 void removeModuleSet(ModuleSetHandleT H) {
408 LogicalDylibs.erase(H);
411 /// @brief Search for the given named symbol.
412 /// @param Name The name of the symbol to search for.
413 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
414 /// @return A handle for the given named symbol, if it exists.
415 JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) {
416 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
419 /// @brief Get the address of a symbol provided by this layer, or some layer
421 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
422 bool ExportedSymbolsOnly) {
423 return (*H)->findSymbol(Name, ExportedSymbolsOnly);
428 void addLogicalModule(LogicalDylib &LD, std::shared_ptr<Module> SrcM,
429 std::vector<std::vector<Function*>> Partitions) {
431 // Bump the linkage and rename any anonymous/privote members in SrcM to
432 // ensure that everything will resolve properly after we partition SrcM.
433 makeAllSymbolsExternallyAccessible(*SrcM);
435 // Create a logical module handle for SrcM within the logical dylib.
436 auto LMH = LD.createLogicalModule();
438 // Create the GVs-and-stubs module.
439 auto GVsAndStubsM = llvm::make_unique<Module>(
440 (SrcM->getName() + ".globals_and_stubs").str(),
442 GVsAndStubsM->setDataLayout(SrcM->getDataLayout());
443 ValueToValueMapTy VMap;
445 // Process partitions and create stubs.
446 // We create the stubs before copying the global variables as we know the
447 // stubs won't refer to any globals (they only refer to their implementation
448 // pointer) so there's no ordering/value-mapping issues.
449 for (auto& Partition : Partitions) {
450 auto &UP = LD.createUncompiledPartition(LMH, SrcM);
451 typename UncompiledPartition::PartitionEntryList PartitionEntries;
452 for (auto &F : Partition) {
453 assert(!F->isDeclaration() &&
454 "Partition should only contain definitions");
455 unsigned FnIdx = PartitionEntries.size();
456 auto CCI = CompileCallbackMgr.getCompileCallback(SrcM->getContext());
457 PartitionEntries.push_back(
458 typename UncompiledPartition::PartitionEntry(F, CCI.getAddress()));
459 Function *StubF = cloneFunctionDecl(*GVsAndStubsM, *F, &VMap);
460 GlobalVariable *FnBodyPtr =
461 createImplPointer(*StubF->getType(), *StubF->getParent(),
462 StubF->getName() + "$orc_addr",
463 createIRTypedAddress(*StubF->getFunctionType(),
465 makeStub(*StubF, *FnBodyPtr);
466 CCI.setCompileAction([&UP, FnIdx]() { return UP.compile(FnIdx); });
469 UP.setPartitionEntries(std::move(PartitionEntries));
472 // Now clone the global variable declarations.
473 GlobalDeclMaterializer GDMat(*GVsAndStubsM);
474 for (auto &GV : SrcM->globals())
475 if (!GV.isDeclaration())
476 cloneGlobalVariableDecl(*GVsAndStubsM, GV, &VMap);
478 // Then clone the initializers.
479 for (auto &GV : SrcM->globals())
480 if (!GV.isDeclaration())
481 moveGlobalVariableInitializer(GV, VMap, &GDMat);
483 // Build a resolver for the stubs module and add it to the base layer.
484 auto GVsAndStubsResolver = createLambdaResolver(
485 [&LD](const std::string &Name) {
486 if (auto Symbol = LD.findSymbol(Name, false))
487 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
489 return LD.findSymbolExternally(Name);
491 [&LD](const std::string &Name) {
492 return RuntimeDyld::SymbolInfo(nullptr);
495 std::vector<std::unique_ptr<Module>> GVsAndStubsMSet;
496 GVsAndStubsMSet.push_back(std::move(GVsAndStubsM));
498 BaseLayer.addModuleSet(std::move(GVsAndStubsMSet),
499 llvm::make_unique<SectionMemoryManager>(),
500 std::move(GVsAndStubsResolver));
501 LD.setGVsAndStubsHandle(LMH, GVsAndStubsH);
504 static std::string Mangle(StringRef Name, const DataLayout &DL) {
506 std::string MangledName;
508 raw_string_ostream MangledNameStream(MangledName);
509 M.getNameWithPrefix(MangledNameStream, Name);
514 BaseLayerT &BaseLayer;
515 CompileCallbackMgrT &CompileCallbackMgr;
516 LogicalDylibList LogicalDylibs;
519 template <typename BaseLayerT, typename CompileCallbackMgrT>
520 typename CompileOnDemandLayer<BaseLayerT, CompileCallbackMgrT>::
522 CompileOnDemandLayer<BaseLayerT, CompileCallbackMgrT>::LogicalDylib::
523 createUncompiledPartition(LMHandle LMH, std::shared_ptr<Module> SrcM) {
524 UncompiledPartitions.push_back(
525 llvm::make_unique<UncompiledPartition>(*this, LMH, std::move(SrcM)));
526 UncompiledPartitions.back()->setID(UncompiledPartitions.size() - 1);
527 return *UncompiledPartitions.back();
530 template <typename BaseLayerT, typename CompileCallbackMgrT>
531 std::unique_ptr<typename CompileOnDemandLayer<BaseLayerT, CompileCallbackMgrT>::
533 CompileOnDemandLayer<BaseLayerT, CompileCallbackMgrT>::LogicalDylib::
534 takeUPOwnership(UncompiledPartitionID ID) {
536 std::swap(UncompiledPartitions[ID], UncompiledPartitions.back());
537 UncompiledPartitions[ID]->setID(ID);
538 auto UP = std::move(UncompiledPartitions.back());
539 UncompiledPartitions.pop_back();
543 } // End namespace orc.
544 } // End namespace llvm.
546 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H