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 "IndirectionUtils.h"
19 #include "LambdaResolver.h"
20 #include "LogicalDylib.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
23 #include "llvm/Transforms/Utils/Cloning.h"
28 #include "llvm/Support/Debug.h"
33 /// @brief Compile-on-demand layer.
35 /// When a module is added to this layer a stub is created for each of its
36 /// function definitions. The stubs and other global values are immediately
37 /// added to the layer below. When a stub is called it triggers the extraction
38 /// of the function body from the original module. The extracted body is then
39 /// compiled and executed.
40 template <typename BaseLayerT,
41 typename CompileCallbackMgrT = JITCompileCallbackManagerBase,
42 typename IndirectStubsMgrT = IndirectStubsManagerBase>
43 class CompileOnDemandLayer {
46 template <typename MaterializerFtor>
47 class LambdaMaterializer final : public ValueMaterializer {
49 LambdaMaterializer(MaterializerFtor M) : M(std::move(M)) {}
50 Value* materializeValueFor(Value *V) final {
57 template <typename MaterializerFtor>
58 LambdaMaterializer<MaterializerFtor>
59 createLambdaMaterializer(MaterializerFtor M) {
60 return LambdaMaterializer<MaterializerFtor>(std::move(M));
63 typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;
65 struct LogicalModuleResources {
66 std::shared_ptr<Module> SourceModule;
67 std::set<const Function*> StubsToClone;
68 std::unique_ptr<IndirectStubsMgrT> StubsMgr;
70 LogicalModuleResources() {}
72 LogicalModuleResources(LogicalModuleResources &&Other) {
73 SourceModule = std::move(Other.SourceModule);
74 StubsToClone = std::move(StubsToClone);
75 StubsMgr = std::move(StubsMgr);
78 // Explicit move constructor to make MSVC happy.
79 LogicalModuleResources& operator=(LogicalModuleResources &&Other) {
80 SourceModule = std::move(Other.SourceModule);
81 StubsToClone = std::move(StubsToClone);
82 StubsMgr = std::move(StubsMgr);
86 // Explicit move assignment to make MSVC happy.
87 JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) {
88 if (Name.endswith("$stub_ptr") && !ExportedSymbolsOnly) {
89 assert(!ExportedSymbolsOnly && "Stubs are never exported");
90 return StubsMgr->findPointer(Name.drop_back(9));
92 return StubsMgr->findStub(Name, ExportedSymbolsOnly);
97 struct LogicalDylibResources {
98 typedef std::function<RuntimeDyld::SymbolInfo(const std::string&)>
100 SymbolResolverFtor ExternalSymbolResolver;
103 typedef LogicalDylib<BaseLayerT, LogicalModuleResources,
104 LogicalDylibResources> CODLogicalDylib;
106 typedef typename CODLogicalDylib::LogicalModuleHandle LogicalModuleHandle;
107 typedef std::list<CODLogicalDylib> LogicalDylibList;
111 /// @brief Handle to a set of loaded modules.
112 typedef typename LogicalDylibList::iterator ModuleSetHandleT;
114 /// @brief Module partitioning functor.
115 typedef std::function<std::set<Function*>(Function&)> PartitioningFtor;
117 /// @brief Builder for IndirectStubsManagers.
118 typedef std::function<std::unique_ptr<IndirectStubsMgrT>()>
119 IndirectStubsManagerBuilderT;
121 /// @brief Construct a compile-on-demand layer instance.
122 CompileOnDemandLayer(BaseLayerT &BaseLayer, PartitioningFtor Partition,
123 CompileCallbackMgrT &CallbackMgr,
124 IndirectStubsManagerBuilderT CreateIndirectStubsManager,
125 bool CloneStubsIntoPartitions = true)
126 : BaseLayer(BaseLayer), Partition(Partition),
127 CompileCallbackMgr(CallbackMgr),
128 CreateIndirectStubsManager(std::move(CreateIndirectStubsManager)),
129 CloneStubsIntoPartitions(CloneStubsIntoPartitions) {}
131 /// @brief Add a module to the compile-on-demand layer.
132 template <typename ModuleSetT, typename MemoryManagerPtrT,
133 typename SymbolResolverPtrT>
134 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
135 MemoryManagerPtrT MemMgr,
136 SymbolResolverPtrT Resolver) {
138 assert(MemMgr == nullptr &&
139 "User supplied memory managers not supported with COD yet.");
141 LogicalDylibs.push_back(CODLogicalDylib(BaseLayer));
142 auto &LDResources = LogicalDylibs.back().getDylibResources();
144 LDResources.ExternalSymbolResolver =
145 [Resolver](const std::string &Name) {
146 return Resolver->findSymbol(Name);
149 // Process each of the modules in this module set.
151 addLogicalModule(LogicalDylibs.back(),
152 std::shared_ptr<Module>(std::move(M)));
154 return std::prev(LogicalDylibs.end());
157 /// @brief Remove the module represented by the given handle.
159 /// This will remove all modules in the layers below that were derived from
160 /// the module represented by H.
161 void removeModuleSet(ModuleSetHandleT H) {
162 LogicalDylibs.erase(H);
165 /// @brief Search for the given named symbol.
166 /// @param Name The name of the symbol to search for.
167 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
168 /// @return A handle for the given named symbol, if it exists.
169 JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) {
170 for (auto LDI = LogicalDylibs.begin(), LDE = LogicalDylibs.end();
172 if (auto Symbol = findSymbolIn(LDI, Name, ExportedSymbolsOnly))
177 /// @brief Get the address of a symbol provided by this layer, or some layer
179 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
180 bool ExportedSymbolsOnly) {
181 return H->findSymbol(Name, ExportedSymbolsOnly);
186 void addLogicalModule(CODLogicalDylib &LD, std::shared_ptr<Module> SrcM) {
188 // Bump the linkage and rename any anonymous/privote members in SrcM to
189 // ensure that everything will resolve properly after we partition SrcM.
190 makeAllSymbolsExternallyAccessible(*SrcM);
192 // Create a logical module handle for SrcM within the logical dylib.
193 auto LMH = LD.createLogicalModule();
194 auto &LMResources = LD.getLogicalModuleResources(LMH);
196 LMResources.SourceModule = SrcM;
198 // Create the GlobalValues module.
199 const DataLayout &DL = SrcM->getDataLayout();
200 auto GVsM = llvm::make_unique<Module>((SrcM->getName() + ".globals").str(),
202 GVsM->setDataLayout(DL);
204 // Create function stubs.
205 ValueToValueMapTy VMap;
207 typename IndirectStubsMgrT::StubInitsMap StubInits;
208 for (auto &F : *SrcM) {
209 // Skip declarations.
210 if (F.isDeclaration())
213 // Record all functions defined by this module.
214 if (CloneStubsIntoPartitions)
215 LMResources.StubsToClone.insert(&F);
217 // Create a callback, associate it with the stub for the function,
218 // and set the compile action to compile the partition containing the
220 auto CCInfo = CompileCallbackMgr.getCompileCallback(SrcM->getContext());
221 StubInits[mangle(F.getName(), DL)] =
222 std::make_pair(CCInfo.getAddress(),
223 JITSymbolBase::flagsFromGlobalValue(F));
224 CCInfo.setCompileAction(
225 [this, &LD, LMH, &F]() {
226 return this->extractAndCompile(LD, LMH, F);
230 LMResources.StubsMgr = CreateIndirectStubsManager();
231 auto EC = LMResources.StubsMgr->init(StubInits);
233 // FIXME: This should be propagated back to the user. Stub creation may
234 // fail for remote JITs.
235 assert(!EC && "Error generating stubs");
238 // Clone global variable decls.
239 for (auto &GV : SrcM->globals())
240 if (!GV.isDeclaration() && !VMap.count(&GV))
241 cloneGlobalVariableDecl(*GVsM, GV, &VMap);
244 for (auto &A : SrcM->aliases())
246 cloneGlobalAliasDecl(*GVsM, A, VMap);
248 // Now we need to clone the GV and alias initializers.
250 // Initializers may refer to functions declared (but not defined) in this
251 // module. Build a materializer to clone decls on demand.
252 auto Materializer = createLambdaMaterializer(
253 [this, &GVsM, &LMResources](Value *V) -> Value* {
254 if (auto *F = dyn_cast<Function>(V)) {
255 // Decls in the original module just get cloned.
256 if (F->isDeclaration())
257 return cloneFunctionDecl(*GVsM, *F);
259 // Definitions in the original module (which we have emitted stubs
260 // for at this point) get turned into a constant alias to the stub
262 const DataLayout &DL = GVsM->getDataLayout();
263 std::string FName = mangle(F->getName(), DL);
264 auto StubSym = LMResources.StubsMgr->findStub(FName, false);
265 unsigned PtrBitWidth = DL.getPointerTypeSizeInBits(F->getType());
266 ConstantInt *StubAddr =
267 ConstantInt::get(GVsM->getContext(),
268 APInt(PtrBitWidth, StubSym.getAddress()));
269 Constant *Init = ConstantExpr::getCast(Instruction::IntToPtr,
270 StubAddr, F->getType());
271 return GlobalAlias::create(F->getFunctionType(),
272 F->getType()->getAddressSpace(),
273 F->getLinkage(), F->getName(),
280 // Clone the global variable initializers.
281 for (auto &GV : SrcM->globals())
282 if (!GV.isDeclaration())
283 moveGlobalVariableInitializer(GV, VMap, &Materializer);
285 // Clone the global alias initializers.
286 for (auto &A : SrcM->aliases()) {
287 auto *NewA = cast<GlobalAlias>(VMap[&A]);
288 assert(NewA && "Alias not cloned?");
289 Value *Init = MapValue(A.getAliasee(), VMap, RF_None, nullptr,
291 NewA->setAliasee(cast<Constant>(Init));
294 // Build a resolver for the globals module and add it to the base layer.
295 auto GVsResolver = createLambdaResolver(
296 [&LD, LMH](const std::string &Name) {
297 auto &LMResources = LD.getLogicalModuleResources(LMH);
298 if (auto Sym = LMResources.StubsMgr->findStub(Name, false))
299 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
300 return LD.getDylibResources().ExternalSymbolResolver(Name);
302 [](const std::string &Name) {
303 return RuntimeDyld::SymbolInfo(nullptr);
306 std::vector<std::unique_ptr<Module>> GVsMSet;
307 GVsMSet.push_back(std::move(GVsM));
309 BaseLayer.addModuleSet(std::move(GVsMSet),
310 llvm::make_unique<SectionMemoryManager>(),
311 std::move(GVsResolver));
312 LD.addToLogicalModule(LMH, GVsH);
315 static std::string mangle(StringRef Name, const DataLayout &DL) {
316 std::string MangledName;
318 raw_string_ostream MangledNameStream(MangledName);
319 Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
324 TargetAddress extractAndCompile(CODLogicalDylib &LD,
325 LogicalModuleHandle LMH,
327 auto &LMResources = LD.getLogicalModuleResources(LMH);
328 Module &SrcM = *LMResources.SourceModule;
330 // If F is a declaration we must already have compiled it.
331 if (F.isDeclaration())
334 // Grab the name of the function being called here.
335 std::string CalledFnName = mangle(F.getName(), SrcM.getDataLayout());
337 auto Part = Partition(F);
338 auto PartH = emitPartition(LD, LMH, Part);
340 TargetAddress CalledAddr = 0;
341 for (auto *SubF : Part) {
342 std::string FnName = mangle(SubF->getName(), SrcM.getDataLayout());
343 auto FnBodySym = BaseLayer.findSymbolIn(PartH, FnName, false);
344 assert(FnBodySym && "Couldn't find function body.");
346 TargetAddress FnBodyAddr = FnBodySym.getAddress();
348 // If this is the function we're calling record the address so we can
349 // return it from this function.
351 CalledAddr = FnBodyAddr;
353 // Update the function body pointer for the stub.
354 if (auto EC = LMResources.StubsMgr->updatePointer(FnName, FnBodyAddr))
361 template <typename PartitionT>
362 BaseLayerModuleSetHandleT emitPartition(CODLogicalDylib &LD,
363 LogicalModuleHandle LMH,
364 const PartitionT &Part) {
365 auto &LMResources = LD.getLogicalModuleResources(LMH);
366 Module &SrcM = *LMResources.SourceModule;
368 // Create the module.
369 std::string NewName = SrcM.getName();
370 for (auto *F : Part) {
372 NewName += F->getName();
375 auto M = llvm::make_unique<Module>(NewName, SrcM.getContext());
376 M->setDataLayout(SrcM.getDataLayout());
377 ValueToValueMapTy VMap;
379 auto Materializer = createLambdaMaterializer(
380 [this, &LMResources, &M, &VMap](Value *V) -> Value* {
381 if (auto *GV = dyn_cast<GlobalVariable>(V)) {
382 return cloneGlobalVariableDecl(*M, *GV);
383 } else if (auto *F = dyn_cast<Function>(V)) {
384 // Check whether we want to clone an available_externally definition.
385 if (LMResources.StubsToClone.count(F)) {
386 // Ok - we want an inlinable stub. For that to work we need a decl
387 // for the stub pointer.
388 auto *StubPtr = createImplPointer(*F->getType(), *M,
389 F->getName() + "$stub_ptr",
391 auto *ClonedF = cloneFunctionDecl(*M, *F);
392 makeStub(*ClonedF, *StubPtr);
393 ClonedF->setLinkage(GlobalValue::AvailableExternallyLinkage);
394 ClonedF->addFnAttr(Attribute::AlwaysInline);
398 return cloneFunctionDecl(*M, *F);
399 } else if (auto *A = dyn_cast<GlobalAlias>(V)) {
400 auto *PTy = cast<PointerType>(A->getType());
401 if (PTy->getElementType()->isFunctionTy())
402 return Function::Create(cast<FunctionType>(PTy->getElementType()),
403 GlobalValue::ExternalLinkage,
404 A->getName(), M.get());
406 return new GlobalVariable(*M, PTy->getElementType(), false,
407 GlobalValue::ExternalLinkage,
408 nullptr, A->getName(), nullptr,
409 GlobalValue::NotThreadLocal,
410 PTy->getAddressSpace());
416 // Create decls in the new module.
418 cloneFunctionDecl(*M, *F, &VMap);
420 // Move the function bodies.
422 moveFunctionBody(*F, VMap, &Materializer);
424 // Create memory manager and symbol resolver.
425 auto MemMgr = llvm::make_unique<SectionMemoryManager>();
426 auto Resolver = createLambdaResolver(
427 [this, &LD, LMH](const std::string &Name) {
428 if (auto Symbol = LD.findSymbolInternally(LMH, Name))
429 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
431 return LD.getDylibResources().ExternalSymbolResolver(Name);
433 [this, &LD, LMH](const std::string &Name) {
434 if (auto Symbol = LD.findSymbolInternally(LMH, Name))
435 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
437 return RuntimeDyld::SymbolInfo(nullptr);
439 std::vector<std::unique_ptr<Module>> PartMSet;
440 PartMSet.push_back(std::move(M));
441 return BaseLayer.addModuleSet(std::move(PartMSet), std::move(MemMgr),
442 std::move(Resolver));
445 BaseLayerT &BaseLayer;
446 PartitioningFtor Partition;
447 CompileCallbackMgrT &CompileCallbackMgr;
448 IndirectStubsManagerBuilderT CreateIndirectStubsManager;
450 LogicalDylibList LogicalDylibs;
451 bool CloneStubsIntoPartitions;
454 } // End namespace orc.
455 } // End namespace llvm.
457 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H