LLVM  5.0.0
CompileOnDemandLayer.h
Go to the documentation of this file.
1 //===- CompileOnDemandLayer.h - Compile each function on demand -*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // JIT layer for breaking up modules and inserting callbacks to allow
11 // individual functions to be compiled on demand.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
16 #define LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
17 
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
27 #include "llvm/IR/Attributes.h"
28 #include "llvm/IR/Constant.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/GlobalAlias.h"
33 #include "llvm/IR/GlobalValue.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/IR/Instruction.h"
36 #include "llvm/IR/Mangler.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/IR/Type.h"
39 #include "llvm/Support/Casting.h"
42 #include <algorithm>
43 #include <cassert>
44 #include <functional>
45 #include <iterator>
46 #include <list>
47 #include <memory>
48 #include <set>
49 #include <string>
50 #include <utility>
51 #include <vector>
52 
53 namespace llvm {
54 
55 class Value;
56 
57 namespace orc {
58 
59 /// @brief Compile-on-demand layer.
60 ///
61 /// When a module is added to this layer a stub is created for each of its
62 /// function definitions. The stubs and other global values are immediately
63 /// added to the layer below. When a stub is called it triggers the extraction
64 /// of the function body from the original module. The extracted body is then
65 /// compiled and executed.
66 template <typename BaseLayerT,
67  typename CompileCallbackMgrT = JITCompileCallbackManager,
68  typename IndirectStubsMgrT = IndirectStubsManager>
70 private:
71  template <typename MaterializerFtor>
72  class LambdaMaterializer final : public ValueMaterializer {
73  public:
74  LambdaMaterializer(MaterializerFtor M) : M(std::move(M)) {}
75 
76  Value *materialize(Value *V) final { return M(V); }
77 
78  private:
79  MaterializerFtor M;
80  };
81 
82  template <typename MaterializerFtor>
83  LambdaMaterializer<MaterializerFtor>
84  createLambdaMaterializer(MaterializerFtor M) {
85  return LambdaMaterializer<MaterializerFtor>(std::move(M));
86  }
87 
88  using BaseLayerModuleHandleT = typename BaseLayerT::ModuleHandleT;
89 
90  // Provide type-erasure for the Modules and MemoryManagers.
91  template <typename ResourceT>
92  class ResourceOwner {
93  public:
94  ResourceOwner() = default;
95  ResourceOwner(const ResourceOwner &) = delete;
96  ResourceOwner &operator=(const ResourceOwner &) = delete;
97  virtual ~ResourceOwner() = default;
98 
99  virtual ResourceT& getResource() const = 0;
100  };
101 
102  template <typename ResourceT, typename ResourcePtrT>
103  class ResourceOwnerImpl : public ResourceOwner<ResourceT> {
104  public:
105  ResourceOwnerImpl(ResourcePtrT ResourcePtr)
106  : ResourcePtr(std::move(ResourcePtr)) {}
107 
108  ResourceT& getResource() const override { return *ResourcePtr; }
109 
110  private:
111  ResourcePtrT ResourcePtr;
112  };
113 
114  template <typename ResourceT, typename ResourcePtrT>
115  std::unique_ptr<ResourceOwner<ResourceT>>
116  wrapOwnership(ResourcePtrT ResourcePtr) {
117  using RO = ResourceOwnerImpl<ResourceT, ResourcePtrT>;
118  return llvm::make_unique<RO>(std::move(ResourcePtr));
119  }
120 
121  class StaticGlobalRenamer {
122  public:
123  StaticGlobalRenamer() = default;
124  StaticGlobalRenamer(StaticGlobalRenamer &&) = default;
125  StaticGlobalRenamer &operator=(StaticGlobalRenamer &&) = default;
126 
127  void rename(Module &M) {
128  for (auto &F : M)
129  if (F.hasLocalLinkage())
130  F.setName("$static." + Twine(NextId++));
131  for (auto &G : M.globals())
132  if (G.hasLocalLinkage())
133  G.setName("$static." + Twine(NextId++));
134  }
135 
136  private:
137  unsigned NextId = 0;
138  };
139 
140  struct LogicalDylib {
141  using SymbolResolverFtor = std::function<JITSymbol(const std::string&)>;
142 
143  using ModuleAdderFtor =
145  BaseLayerT&,
146  std::unique_ptr<Module>,
147  std::unique_ptr<JITSymbolResolver>)>;
148 
150  std::shared_ptr<Module> SourceMod;
151  std::set<Function*> StubsToClone;
152  };
153 
154  using SourceModulesList = std::vector<SourceModuleEntry>;
155  using SourceModuleHandle = typename SourceModulesList::size_type;
156 
157  SourceModuleHandle
158  addSourceModule(std::shared_ptr<Module> M) {
159  SourceModuleHandle H = SourceModules.size();
160  SourceModules.push_back(SourceModuleEntry());
161  SourceModules.back().SourceMod = std::move(M);
162  return H;
163  }
164 
165  Module& getSourceModule(SourceModuleHandle H) {
166  return *SourceModules[H].SourceMod;
167  }
168 
169  std::set<Function*>& getStubsToClone(SourceModuleHandle H) {
170  return SourceModules[H].StubsToClone;
171  }
172 
173  JITSymbol findSymbol(BaseLayerT &BaseLayer, const std::string &Name,
174  bool ExportedSymbolsOnly) {
175  if (auto Sym = StubsMgr->findStub(Name, ExportedSymbolsOnly))
176  return Sym;
177  for (auto BLH : BaseLayerHandles)
178  if (auto Sym = BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly))
179  return Sym;
180  else if (auto Err = Sym.takeError())
181  return std::move(Err);
182  return nullptr;
183  }
184 
185  Error removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
186  for (auto &BLH : BaseLayerHandles)
187  if (auto Err = BaseLayer.removeModule(BLH))
188  return Err;
189  return Error::success();
190  }
191 
192  std::shared_ptr<JITSymbolResolver> ExternalSymbolResolver;
193  std::unique_ptr<IndirectStubsMgrT> StubsMgr;
194  StaticGlobalRenamer StaticRenamer;
195  SourceModulesList SourceModules;
196  std::vector<BaseLayerModuleHandleT> BaseLayerHandles;
197  };
198 
199  using LogicalDylibList = std::list<LogicalDylib>;
200 
201 public:
202 
203  /// @brief Handle to loaded module.
204  using ModuleHandleT = typename LogicalDylibList::iterator;
205 
206  /// @brief Module partitioning functor.
207  using PartitioningFtor = std::function<std::set<Function*>(Function&)>;
208 
209  /// @brief Builder for IndirectStubsManagers.
211  std::function<std::unique_ptr<IndirectStubsMgrT>()>;
212 
213  /// @brief Construct a compile-on-demand layer instance.
214  CompileOnDemandLayer(BaseLayerT &BaseLayer, PartitioningFtor Partition,
215  CompileCallbackMgrT &CallbackMgr,
216  IndirectStubsManagerBuilderT CreateIndirectStubsManager,
217  bool CloneStubsIntoPartitions = true)
218  : BaseLayer(BaseLayer), Partition(std::move(Partition)),
219  CompileCallbackMgr(CallbackMgr),
220  CreateIndirectStubsManager(std::move(CreateIndirectStubsManager)),
221  CloneStubsIntoPartitions(CloneStubsIntoPartitions) {}
222 
224  // FIXME: Report error on log.
225  while (!LogicalDylibs.empty())
226  consumeError(removeModule(LogicalDylibs.begin()));
227  }
228 
229  /// @brief Add a module to the compile-on-demand layer.
231  addModule(std::shared_ptr<Module> M,
232  std::shared_ptr<JITSymbolResolver> Resolver) {
233 
234  LogicalDylibs.push_back(LogicalDylib());
235  auto &LD = LogicalDylibs.back();
236  LD.ExternalSymbolResolver = std::move(Resolver);
237  LD.StubsMgr = CreateIndirectStubsManager();
238 
239  // Process each of the modules in this module set.
240  if (auto Err = addLogicalModule(LD, std::move(M)))
241  return std::move(Err);
242 
243  return std::prev(LogicalDylibs.end());
244  }
245 
246  /// @brief Add extra modules to an existing logical module.
247  Error addExtraModule(ModuleHandleT H, std::shared_ptr<Module> M) {
248  return addLogicalModule(*H, std::move(M));
249  }
250 
251  /// @brief Remove the module represented by the given handle.
252  ///
253  /// This will remove all modules in the layers below that were derived from
254  /// the module represented by H.
256  auto Err = H->removeModulesFromBaseLayer(BaseLayer);
257  LogicalDylibs.erase(H);
258  return Err;
259  }
260 
261  /// @brief Search for the given named symbol.
262  /// @param Name The name of the symbol to search for.
263  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
264  /// @return A handle for the given named symbol, if it exists.
265  JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) {
266  for (auto LDI = LogicalDylibs.begin(), LDE = LogicalDylibs.end();
267  LDI != LDE; ++LDI) {
268  if (auto Sym = LDI->StubsMgr->findStub(Name, ExportedSymbolsOnly))
269  return Sym;
270  if (auto Sym = findSymbolIn(LDI, Name, ExportedSymbolsOnly))
271  return Sym;
272  else if (auto Err = Sym.takeError())
273  return std::move(Err);
274  }
275  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
276  }
277 
278  /// @brief Get the address of a symbol provided by this layer, or some layer
279  /// below this one.
280  JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
281  bool ExportedSymbolsOnly) {
282  return H->findSymbol(BaseLayer, Name, ExportedSymbolsOnly);
283  }
284 
285  /// @brief Update the stub for the given function to point at FnBodyAddr.
286  /// This can be used to support re-optimization.
287  /// @return true if the function exists and the stub is updated, false
288  /// otherwise.
289  //
290  // FIXME: We should track and free associated resources (unused compile
291  // callbacks, uncompiled IR, and no-longer-needed/reachable function
292  // implementations).
293  Error updatePointer(std::string FuncName, JITTargetAddress FnBodyAddr) {
294  //Find out which logical dylib contains our symbol
295  auto LDI = LogicalDylibs.begin();
296  for (auto LDE = LogicalDylibs.end(); LDI != LDE; ++LDI) {
297  if (auto LMResources =
298  LDI->getLogicalModuleResourcesForSymbol(FuncName, false)) {
299  Module &SrcM = LMResources->SourceModule->getResource();
300  std::string CalledFnName = mangle(FuncName, SrcM.getDataLayout());
301  if (auto Err = LMResources->StubsMgr->updatePointer(CalledFnName,
302  FnBodyAddr))
303  return Err;
304  return Error::success();
305  }
306  }
307  return make_error<JITSymbolNotFound>(FuncName);
308  }
309 
310 private:
311 
312  Error addLogicalModule(LogicalDylib &LD, std::shared_ptr<Module> SrcMPtr) {
313 
314  // Rename all static functions / globals to $static.X :
315  // This will unique the names across all modules in the logical dylib,
316  // simplifying symbol lookup.
317  LD.StaticRenamer.rename(*SrcMPtr);
318 
319  // Bump the linkage and rename any anonymous/privote members in SrcM to
320  // ensure that everything will resolve properly after we partition SrcM.
322 
323  // Create a logical module handle for SrcM within the logical dylib.
324  Module &SrcM = *SrcMPtr;
325  auto LMId = LD.addSourceModule(std::move(SrcMPtr));
326 
327  // Create stub functions.
328  const DataLayout &DL = SrcM.getDataLayout();
329  {
330  typename IndirectStubsMgrT::StubInitsMap StubInits;
331  for (auto &F : SrcM) {
332  // Skip declarations.
333  if (F.isDeclaration())
334  continue;
335 
336  // Skip weak functions for which we already have definitions.
337  auto MangledName = mangle(F.getName(), DL);
338  if (F.hasWeakLinkage() || F.hasLinkOnceLinkage()) {
339  if (auto Sym = LD.findSymbol(BaseLayer, MangledName, false))
340  continue;
341  else if (auto Err = Sym.takeError())
342  return std::move(Err);
343  }
344 
345  // Record all functions defined by this module.
346  if (CloneStubsIntoPartitions)
347  LD.getStubsToClone(LMId).insert(&F);
348 
349  // Create a callback, associate it with the stub for the function,
350  // and set the compile action to compile the partition containing the
351  // function.
352  auto CCInfo = CompileCallbackMgr.getCompileCallback();
353  StubInits[MangledName] =
354  std::make_pair(CCInfo.getAddress(),
356  CCInfo.setCompileAction([this, &LD, LMId, &F]() -> JITTargetAddress {
357  if (auto FnImplAddrOrErr = this->extractAndCompile(LD, LMId, F))
358  return *FnImplAddrOrErr;
359  else {
360  // FIXME: Report error, return to 'abort' or something similar.
361  consumeError(FnImplAddrOrErr.takeError());
362  return 0;
363  }
364  });
365  }
366 
367  if (auto Err = LD.StubsMgr->createStubs(StubInits))
368  return Err;
369  }
370 
371  // If this module doesn't contain any globals, aliases, or module flags then
372  // we can bail out early and avoid the overhead of creating and managing an
373  // empty globals module.
374  if (SrcM.global_empty() && SrcM.alias_empty() &&
375  !SrcM.getModuleFlagsMetadata())
376  return Error::success();
377 
378  // Create the GlobalValues module.
379  auto GVsM = llvm::make_unique<Module>((SrcM.getName() + ".globals").str(),
380  SrcM.getContext());
381  GVsM->setDataLayout(DL);
382 
383  ValueToValueMapTy VMap;
384 
385  // Clone global variable decls.
386  for (auto &GV : SrcM.globals())
387  if (!GV.isDeclaration() && !VMap.count(&GV))
388  cloneGlobalVariableDecl(*GVsM, GV, &VMap);
389 
390  // And the aliases.
391  for (auto &A : SrcM.aliases())
392  if (!VMap.count(&A))
393  cloneGlobalAliasDecl(*GVsM, A, VMap);
394 
395  // Clone the module flags.
396  cloneModuleFlagsMetadata(*GVsM, SrcM, VMap);
397 
398  // Now we need to clone the GV and alias initializers.
399 
400  // Initializers may refer to functions declared (but not defined) in this
401  // module. Build a materializer to clone decls on demand.
402  Error MaterializerErrors = Error::success();
403  auto Materializer = createLambdaMaterializer(
404  [&LD, &GVsM, &MaterializerErrors](Value *V) -> Value* {
405  if (auto *F = dyn_cast<Function>(V)) {
406  // Decls in the original module just get cloned.
407  if (F->isDeclaration())
408  return cloneFunctionDecl(*GVsM, *F);
409 
410  // Definitions in the original module (which we have emitted stubs
411  // for at this point) get turned into a constant alias to the stub
412  // instead.
413  const DataLayout &DL = GVsM->getDataLayout();
414  std::string FName = mangle(F->getName(), DL);
415  unsigned PtrBitWidth = DL.getPointerTypeSizeInBits(F->getType());
416  JITTargetAddress StubAddr = 0;
417 
418  // Get the address for the stub. If we encounter an error while
419  // doing so, stash it in the MaterializerErrors variable and use a
420  // null address as a placeholder.
421  if (auto StubSym = LD.StubsMgr->findStub(FName, false)) {
422  if (auto StubAddrOrErr = StubSym.getAddress())
423  StubAddr = *StubAddrOrErr;
424  else
425  MaterializerErrors = joinErrors(std::move(MaterializerErrors),
426  StubAddrOrErr.takeError());
427  }
428 
429  ConstantInt *StubAddrCI =
430  ConstantInt::get(GVsM->getContext(), APInt(PtrBitWidth, StubAddr));
431  Constant *Init = ConstantExpr::getCast(Instruction::IntToPtr,
432  StubAddrCI, F->getType());
433  return GlobalAlias::create(F->getFunctionType(),
434  F->getType()->getAddressSpace(),
435  F->getLinkage(), F->getName(),
436  Init, GVsM.get());
437  }
438  // else....
439  return nullptr;
440  });
441 
442  // Clone the global variable initializers.
443  for (auto &GV : SrcM.globals())
444  if (!GV.isDeclaration())
445  moveGlobalVariableInitializer(GV, VMap, &Materializer);
446 
447  // Clone the global alias initializers.
448  for (auto &A : SrcM.aliases()) {
449  auto *NewA = cast<GlobalAlias>(VMap[&A]);
450  assert(NewA && "Alias not cloned?");
451  Value *Init = MapValue(A.getAliasee(), VMap, RF_None, nullptr,
452  &Materializer);
453  NewA->setAliasee(cast<Constant>(Init));
454  }
455 
456  if (MaterializerErrors)
457  return MaterializerErrors;
458 
459  // Build a resolver for the globals module and add it to the base layer.
460  auto GVsResolver = createLambdaResolver(
461  [this, &LD](const std::string &Name) -> JITSymbol {
462  if (auto Sym = LD.StubsMgr->findStub(Name, false))
463  return Sym;
464  if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
465  return Sym;
466  else if (auto Err = Sym.takeError())
467  return std::move(Err);
468  return LD.ExternalSymbolResolver->findSymbolInLogicalDylib(Name);
469  },
470  [&LD](const std::string &Name) {
471  return LD.ExternalSymbolResolver->findSymbol(Name);
472  });
473 
474  if (auto GVsHOrErr =
475  BaseLayer.addModule(std::move(GVsM), std::move(GVsResolver)))
476  LD.BaseLayerHandles.push_back(*GVsHOrErr);
477  else
478  return GVsHOrErr.takeError();
479 
480  return Error::success();
481  }
482 
483  static std::string mangle(StringRef Name, const DataLayout &DL) {
484  std::string MangledName;
485  {
486  raw_string_ostream MangledNameStream(MangledName);
487  Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
488  }
489  return MangledName;
490  }
491 
492  Expected<JITTargetAddress>
493  extractAndCompile(LogicalDylib &LD,
494  typename LogicalDylib::SourceModuleHandle LMId,
495  Function &F) {
496  Module &SrcM = LD.getSourceModule(LMId);
497 
498  // If F is a declaration we must already have compiled it.
499  if (F.isDeclaration())
500  return 0;
501 
502  // Grab the name of the function being called here.
503  std::string CalledFnName = mangle(F.getName(), SrcM.getDataLayout());
504 
505  JITTargetAddress CalledAddr = 0;
506  auto Part = Partition(F);
507  if (auto PartHOrErr = emitPartition(LD, LMId, Part)) {
508  auto &PartH = *PartHOrErr;
509  for (auto *SubF : Part) {
510  std::string FnName = mangle(SubF->getName(), SrcM.getDataLayout());
511  if (auto FnBodySym = BaseLayer.findSymbolIn(PartH, FnName, false)) {
512  if (auto FnBodyAddrOrErr = FnBodySym.getAddress()) {
513  JITTargetAddress FnBodyAddr = *FnBodyAddrOrErr;
514 
515  // If this is the function we're calling record the address so we can
516  // return it from this function.
517  if (SubF == &F)
518  CalledAddr = FnBodyAddr;
519 
520  // Update the function body pointer for the stub.
521  if (auto EC = LD.StubsMgr->updatePointer(FnName, FnBodyAddr))
522  return 0;
523 
524  } else
525  return FnBodyAddrOrErr.takeError();
526  } else if (auto Err = FnBodySym.takeError())
527  return std::move(Err);
528  else
529  llvm_unreachable("Function not emitted for partition");
530  }
531 
532  LD.BaseLayerHandles.push_back(PartH);
533  } else
534  return PartHOrErr.takeError();
535 
536  return CalledAddr;
537  }
538 
539  template <typename PartitionT>
540  Expected<BaseLayerModuleHandleT>
541  emitPartition(LogicalDylib &LD,
542  typename LogicalDylib::SourceModuleHandle LMId,
543  const PartitionT &Part) {
544  Module &SrcM = LD.getSourceModule(LMId);
545 
546  // Create the module.
547  std::string NewName = SrcM.getName();
548  for (auto *F : Part) {
549  NewName += ".";
550  NewName += F->getName();
551  }
552 
553  auto M = llvm::make_unique<Module>(NewName, SrcM.getContext());
554  M->setDataLayout(SrcM.getDataLayout());
555  ValueToValueMapTy VMap;
556 
557  auto Materializer = createLambdaMaterializer([&LD, &LMId,
558  &M](Value *V) -> Value * {
559  if (auto *GV = dyn_cast<GlobalVariable>(V))
560  return cloneGlobalVariableDecl(*M, *GV);
561 
562  if (auto *F = dyn_cast<Function>(V)) {
563  // Check whether we want to clone an available_externally definition.
564  if (!LD.getStubsToClone(LMId).count(F))
565  return cloneFunctionDecl(*M, *F);
566 
567  // Ok - we want an inlinable stub. For that to work we need a decl
568  // for the stub pointer.
569  auto *StubPtr = createImplPointer(*F->getType(), *M,
570  F->getName() + "$stub_ptr", nullptr);
571  auto *ClonedF = cloneFunctionDecl(*M, *F);
572  makeStub(*ClonedF, *StubPtr);
573  ClonedF->setLinkage(GlobalValue::AvailableExternallyLinkage);
574  ClonedF->addFnAttr(Attribute::AlwaysInline);
575  return ClonedF;
576  }
577 
578  if (auto *A = dyn_cast<GlobalAlias>(V)) {
579  auto *Ty = A->getValueType();
580  if (Ty->isFunctionTy())
581  return Function::Create(cast<FunctionType>(Ty),
582  GlobalValue::ExternalLinkage, A->getName(),
583  M.get());
584 
585  return new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage,
586  nullptr, A->getName(), nullptr,
588  A->getType()->getAddressSpace());
589  }
590 
591  return nullptr;
592  });
593 
594  // Create decls in the new module.
595  for (auto *F : Part)
596  cloneFunctionDecl(*M, *F, &VMap);
597 
598  // Move the function bodies.
599  for (auto *F : Part)
600  moveFunctionBody(*F, VMap, &Materializer);
601 
602  // Create memory manager and symbol resolver.
603  auto Resolver = createLambdaResolver(
604  [this, &LD](const std::string &Name) -> JITSymbol {
605  if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
606  return Sym;
607  else if (auto Err = Sym.takeError())
608  return std::move(Err);
609  return LD.ExternalSymbolResolver->findSymbolInLogicalDylib(Name);
610  },
611  [&LD](const std::string &Name) {
612  return LD.ExternalSymbolResolver->findSymbol(Name);
613  });
614 
615  return BaseLayer.addModule(std::move(M), std::move(Resolver));
616  }
617 
618  BaseLayerT &BaseLayer;
619  PartitioningFtor Partition;
620  CompileCallbackMgrT &CompileCallbackMgr;
621  IndirectStubsManagerBuilderT CreateIndirectStubsManager;
622 
623  LogicalDylibList LogicalDylibs;
624  bool CloneStubsIntoPartitions;
625 };
626 
627 } // end namespace orc
628 
629 } // end namespace llvm
630 
631 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
Definition: ValueMapper.h:25
Base class for managing collections of named indirect stubs.
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV)
Construct a JITSymbolFlags value based on the flags of the given global value.
Definition: JITSymbol.cpp:20
Represents a symbol in the JIT.
Definition: JITSymbol.h:122
GlobalAlias * cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA, ValueToValueMapTy &VMap)
Clone a global alias declaration into a new module.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
void cloneModuleFlagsMetadata(Module &Dst, const Module &Src, ValueToValueMapTy &VMap)
Clone module flags metadata into the destination module.
Available for inspection, not emission.
Definition: GlobalValue.h:50
DataLayout DL
Externally visible function.
Definition: GlobalValue.h:49
Target-independent base class for compile callback management.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
std::function< std::unique_ptr< IndirectStubsMgrT >()> IndirectStubsManagerBuilderT
Builder for IndirectStubsManagers.
GlobalVariable * createImplPointer(PointerType &PT, Module &M, const Twine &Name, Constant *Initializer)
Create a function pointer with the given type, name, and initializer in the given Module...
This file contains the simple types necessary to represent the attributes associated with functions a...
void moveGlobalVariableInitializer(GlobalVariable &OrigGV, ValueToValueMapTy &VMap, ValueMaterializer *Materializer=nullptr, GlobalVariable *NewGV=nullptr)
Move global variable GV from its parent module to cloned global declaration in a different module...
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
This file implements a class to represent arbitrary precision integral constant values and operations...
void makeStub(Function &F, Value &ImplPointer)
Turn a function declaration into a stub function that makes an indirect call using the given function...
CompileOnDemandLayer(BaseLayerT &BaseLayer, PartitioningFtor Partition, CompileCallbackMgrT &CallbackMgr, IndirectStubsManagerBuilderT CreateIndirectStubsManager, bool CloneStubsIntoPartitions=true)
Construct a compile-on-demand layer instance.
#define F(x, y, z)
Definition: MD5.cpp:55
std::function< std::set< Function * >(Function &)> PartitioningFtor
Module partitioning functor.
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Definition: JITSymbol.h:37
Expected< ModuleHandleT > addModule(std::shared_ptr< Module > M, std::shared_ptr< JITSymbolResolver > Resolver)
Add a module to the compile-on-demand layer.
const char * Name
GlobalVariable * cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, ValueToValueMapTy *VMap=nullptr)
Clone a global variable declaration into a new module.
This is a class that can be implemented by clients to materialize Values on demand.
Definition: ValueMapper.h:43
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define H(x, y, z)
Definition: MD5.cpp:57
#define A
Definition: LargeTest.cpp:12
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:593
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Look up or compute a value in the value map.
Definition: ValueMapper.h:198
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
Error addExtraModule(ModuleHandleT H, std::shared_ptr< Module > M)
Add extra modules to an existing logical module.
Function * cloneFunctionDecl(Module &Dst, const Function &F, ValueToValueMapTy *VMap=nullptr)
Clone a function declaration into a new module.
unsigned ModuleHandleT
Module.h This file contains the declarations for the Module class.
const DataFlowGraph & G
Definition: RDFGraph.cpp:206
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:560
Error updatePointer(std::string FuncName, JITTargetAddress FnBodyAddr)
Update the stub for the given function to point at FnBodyAddr.
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1435
void makeAllSymbolsExternallyAccessible(Module &M)
Raise linkage types and rename as necessary to ensure that all symbols are accessible for other modul...
typename LogicalDylibList::iterator ModuleHandleT
Handle to loaded module.
JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of a symbol provided by this layer, or some layer below this one. ...
llvm_unreachable("Unknown RegBank!")
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:361
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:109
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:406
static void rename(GlobalValue *GV)
Definition: AutoUpgrade.cpp:35
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
std::shared_ptr< LambdaResolver< DylibLookupFtorT, ExternalLookupFtorT > > createLambdaResolver(DylibLookupFtorT DylibLookupFtor, ExternalLookupFtorT ExternalLookupFtor)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Error removeModule(ModuleHandleT H)
Remove the module represented by the given handle.
JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
Definition: Function.h:128
void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap, ValueMaterializer *Materializer=nullptr, Function *NewF=nullptr)
Move the body of function 'F' to a cloned function declaration in a different module (See related clo...
static GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition: Globals.cpp:399