LLVM  6.0.0svn
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 
144  std::shared_ptr<Module> SourceMod;
145  std::set<Function*> StubsToClone;
146  };
147 
148  using SourceModulesList = std::vector<SourceModuleEntry>;
149  using SourceModuleHandle = typename SourceModulesList::size_type;
150 
151  SourceModuleHandle
152  addSourceModule(std::shared_ptr<Module> M) {
153  SourceModuleHandle H = SourceModules.size();
154  SourceModules.push_back(SourceModuleEntry());
155  SourceModules.back().SourceMod = std::move(M);
156  return H;
157  }
158 
159  Module& getSourceModule(SourceModuleHandle H) {
160  return *SourceModules[H].SourceMod;
161  }
162 
163  std::set<Function*>& getStubsToClone(SourceModuleHandle H) {
164  return SourceModules[H].StubsToClone;
165  }
166 
167  JITSymbol findSymbol(BaseLayerT &BaseLayer, const std::string &Name,
168  bool ExportedSymbolsOnly) {
169  if (auto Sym = StubsMgr->findStub(Name, ExportedSymbolsOnly))
170  return Sym;
171  for (auto BLH : BaseLayerHandles)
172  if (auto Sym = BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly))
173  return Sym;
174  else if (auto Err = Sym.takeError())
175  return std::move(Err);
176  return nullptr;
177  }
178 
179  Error removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
180  for (auto &BLH : BaseLayerHandles)
181  if (auto Err = BaseLayer.removeModule(BLH))
182  return Err;
183  return Error::success();
184  }
185 
186  std::shared_ptr<JITSymbolResolver> ExternalSymbolResolver;
187  std::unique_ptr<IndirectStubsMgrT> StubsMgr;
188  StaticGlobalRenamer StaticRenamer;
189  SourceModulesList SourceModules;
190  std::vector<BaseLayerModuleHandleT> BaseLayerHandles;
191  };
192 
193  using LogicalDylibList = std::list<LogicalDylib>;
194 
195 public:
196 
197  /// @brief Handle to loaded module.
198  using ModuleHandleT = typename LogicalDylibList::iterator;
199 
200  /// @brief Module partitioning functor.
201  using PartitioningFtor = std::function<std::set<Function*>(Function&)>;
202 
203  /// @brief Builder for IndirectStubsManagers.
205  std::function<std::unique_ptr<IndirectStubsMgrT>()>;
206 
207  /// @brief Construct a compile-on-demand layer instance.
208  CompileOnDemandLayer(BaseLayerT &BaseLayer, PartitioningFtor Partition,
209  CompileCallbackMgrT &CallbackMgr,
210  IndirectStubsManagerBuilderT CreateIndirectStubsManager,
211  bool CloneStubsIntoPartitions = true)
212  : BaseLayer(BaseLayer), Partition(std::move(Partition)),
213  CompileCallbackMgr(CallbackMgr),
214  CreateIndirectStubsManager(std::move(CreateIndirectStubsManager)),
215  CloneStubsIntoPartitions(CloneStubsIntoPartitions) {}
216 
218  // FIXME: Report error on log.
219  while (!LogicalDylibs.empty())
220  consumeError(removeModule(LogicalDylibs.begin()));
221  }
222 
223  /// @brief Add a module to the compile-on-demand layer.
225  addModule(std::shared_ptr<Module> M,
226  std::shared_ptr<JITSymbolResolver> Resolver) {
227 
228  LogicalDylibs.push_back(LogicalDylib());
229  auto &LD = LogicalDylibs.back();
230  LD.ExternalSymbolResolver = std::move(Resolver);
231  LD.StubsMgr = CreateIndirectStubsManager();
232 
233  // Process each of the modules in this module set.
234  if (auto Err = addLogicalModule(LD, std::move(M)))
235  return std::move(Err);
236 
237  return std::prev(LogicalDylibs.end());
238  }
239 
240  /// @brief Add extra modules to an existing logical module.
241  Error addExtraModule(ModuleHandleT H, std::shared_ptr<Module> M) {
242  return addLogicalModule(*H, std::move(M));
243  }
244 
245  /// @brief Remove the module represented by the given handle.
246  ///
247  /// This will remove all modules in the layers below that were derived from
248  /// the module represented by H.
250  auto Err = H->removeModulesFromBaseLayer(BaseLayer);
251  LogicalDylibs.erase(H);
252  return Err;
253  }
254 
255  /// @brief Search for the given named symbol.
256  /// @param Name The name of the symbol to search for.
257  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
258  /// @return A handle for the given named symbol, if it exists.
259  JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) {
260  for (auto LDI = LogicalDylibs.begin(), LDE = LogicalDylibs.end();
261  LDI != LDE; ++LDI) {
262  if (auto Sym = LDI->StubsMgr->findStub(Name, ExportedSymbolsOnly))
263  return Sym;
264  if (auto Sym = findSymbolIn(LDI, Name, ExportedSymbolsOnly))
265  return Sym;
266  else if (auto Err = Sym.takeError())
267  return std::move(Err);
268  }
269  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
270  }
271 
272  /// @brief Get the address of a symbol provided by this layer, or some layer
273  /// below this one.
275  bool ExportedSymbolsOnly) {
276  return H->findSymbol(BaseLayer, Name, ExportedSymbolsOnly);
277  }
278 
279  /// @brief Update the stub for the given function to point at FnBodyAddr.
280  /// This can be used to support re-optimization.
281  /// @return true if the function exists and the stub is updated, false
282  /// otherwise.
283  //
284  // FIXME: We should track and free associated resources (unused compile
285  // callbacks, uncompiled IR, and no-longer-needed/reachable function
286  // implementations).
287  Error updatePointer(std::string FuncName, JITTargetAddress FnBodyAddr) {
288  //Find out which logical dylib contains our symbol
289  auto LDI = LogicalDylibs.begin();
290  for (auto LDE = LogicalDylibs.end(); LDI != LDE; ++LDI) {
291  if (auto LMResources =
292  LDI->getLogicalModuleResourcesForSymbol(FuncName, false)) {
293  Module &SrcM = LMResources->SourceModule->getResource();
294  std::string CalledFnName = mangle(FuncName, SrcM.getDataLayout());
295  if (auto Err = LMResources->StubsMgr->updatePointer(CalledFnName,
296  FnBodyAddr))
297  return Err;
298  return Error::success();
299  }
300  }
301  return make_error<JITSymbolNotFound>(FuncName);
302  }
303 
304 private:
305 
306  Error addLogicalModule(LogicalDylib &LD, std::shared_ptr<Module> SrcMPtr) {
307 
308  // Rename all static functions / globals to $static.X :
309  // This will unique the names across all modules in the logical dylib,
310  // simplifying symbol lookup.
311  LD.StaticRenamer.rename(*SrcMPtr);
312 
313  // Bump the linkage and rename any anonymous/privote members in SrcM to
314  // ensure that everything will resolve properly after we partition SrcM.
316 
317  // Create a logical module handle for SrcM within the logical dylib.
318  Module &SrcM = *SrcMPtr;
319  auto LMId = LD.addSourceModule(std::move(SrcMPtr));
320 
321  // Create stub functions.
322  const DataLayout &DL = SrcM.getDataLayout();
323  {
324  typename IndirectStubsMgrT::StubInitsMap StubInits;
325  for (auto &F : SrcM) {
326  // Skip declarations.
327  if (F.isDeclaration())
328  continue;
329 
330  // Skip weak functions for which we already have definitions.
331  auto MangledName = mangle(F.getName(), DL);
332  if (F.hasWeakLinkage() || F.hasLinkOnceLinkage()) {
333  if (auto Sym = LD.findSymbol(BaseLayer, MangledName, false))
334  continue;
335  else if (auto Err = Sym.takeError())
336  return std::move(Err);
337  }
338 
339  // Record all functions defined by this module.
340  if (CloneStubsIntoPartitions)
341  LD.getStubsToClone(LMId).insert(&F);
342 
343  // Create a callback, associate it with the stub for the function,
344  // and set the compile action to compile the partition containing the
345  // function.
346  if (auto CCInfoOrErr = CompileCallbackMgr.getCompileCallback()) {
347  auto &CCInfo = *CCInfoOrErr;
348  StubInits[MangledName] =
349  std::make_pair(CCInfo.getAddress(),
351  CCInfo.setCompileAction([this, &LD, LMId, &F]() -> JITTargetAddress {
352  if (auto FnImplAddrOrErr = this->extractAndCompile(LD, LMId, F))
353  return *FnImplAddrOrErr;
354  else {
355  // FIXME: Report error, return to 'abort' or something similar.
356  consumeError(FnImplAddrOrErr.takeError());
357  return 0;
358  }
359  });
360  } else
361  return CCInfoOrErr.takeError();
362  }
363 
364  if (auto Err = LD.StubsMgr->createStubs(StubInits))
365  return Err;
366  }
367 
368  // If this module doesn't contain any globals, aliases, or module flags then
369  // we can bail out early and avoid the overhead of creating and managing an
370  // empty globals module.
371  if (SrcM.global_empty() && SrcM.alias_empty() &&
372  !SrcM.getModuleFlagsMetadata())
373  return Error::success();
374 
375  // Create the GlobalValues module.
376  auto GVsM = llvm::make_unique<Module>((SrcM.getName() + ".globals").str(),
377  SrcM.getContext());
378  GVsM->setDataLayout(DL);
379 
380  ValueToValueMapTy VMap;
381 
382  // Clone global variable decls.
383  for (auto &GV : SrcM.globals())
384  if (!GV.isDeclaration() && !VMap.count(&GV))
385  cloneGlobalVariableDecl(*GVsM, GV, &VMap);
386 
387  // And the aliases.
388  for (auto &A : SrcM.aliases())
389  if (!VMap.count(&A))
390  cloneGlobalAliasDecl(*GVsM, A, VMap);
391 
392  // Clone the module flags.
393  cloneModuleFlagsMetadata(*GVsM, SrcM, VMap);
394 
395  // Now we need to clone the GV and alias initializers.
396 
397  // Initializers may refer to functions declared (but not defined) in this
398  // module. Build a materializer to clone decls on demand.
399  Error MaterializerErrors = Error::success();
400  auto Materializer = createLambdaMaterializer(
401  [&LD, &GVsM, &MaterializerErrors](Value *V) -> Value* {
402  if (auto *F = dyn_cast<Function>(V)) {
403  // Decls in the original module just get cloned.
404  if (F->isDeclaration())
405  return cloneFunctionDecl(*GVsM, *F);
406 
407  // Definitions in the original module (which we have emitted stubs
408  // for at this point) get turned into a constant alias to the stub
409  // instead.
410  const DataLayout &DL = GVsM->getDataLayout();
411  std::string FName = mangle(F->getName(), DL);
412  unsigned PtrBitWidth = DL.getPointerTypeSizeInBits(F->getType());
413  JITTargetAddress StubAddr = 0;
414 
415  // Get the address for the stub. If we encounter an error while
416  // doing so, stash it in the MaterializerErrors variable and use a
417  // null address as a placeholder.
418  if (auto StubSym = LD.StubsMgr->findStub(FName, false)) {
419  if (auto StubAddrOrErr = StubSym.getAddress())
420  StubAddr = *StubAddrOrErr;
421  else
422  MaterializerErrors = joinErrors(std::move(MaterializerErrors),
423  StubAddrOrErr.takeError());
424  }
425 
426  ConstantInt *StubAddrCI =
427  ConstantInt::get(GVsM->getContext(), APInt(PtrBitWidth, StubAddr));
428  Constant *Init = ConstantExpr::getCast(Instruction::IntToPtr,
429  StubAddrCI, F->getType());
430  return GlobalAlias::create(F->getFunctionType(),
431  F->getType()->getAddressSpace(),
432  F->getLinkage(), F->getName(),
433  Init, GVsM.get());
434  }
435  // else....
436  return nullptr;
437  });
438 
439  // Clone the global variable initializers.
440  for (auto &GV : SrcM.globals())
441  if (!GV.isDeclaration())
442  moveGlobalVariableInitializer(GV, VMap, &Materializer);
443 
444  // Clone the global alias initializers.
445  for (auto &A : SrcM.aliases()) {
446  auto *NewA = cast<GlobalAlias>(VMap[&A]);
447  assert(NewA && "Alias not cloned?");
448  Value *Init = MapValue(A.getAliasee(), VMap, RF_None, nullptr,
449  &Materializer);
450  NewA->setAliasee(cast<Constant>(Init));
451  }
452 
453  if (MaterializerErrors)
454  return MaterializerErrors;
455 
456  // Build a resolver for the globals module and add it to the base layer.
457  auto GVsResolver = createLambdaResolver(
458  [this, &LD](const std::string &Name) -> JITSymbol {
459  if (auto Sym = LD.StubsMgr->findStub(Name, false))
460  return Sym;
461  if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
462  return Sym;
463  else if (auto Err = Sym.takeError())
464  return std::move(Err);
465  return LD.ExternalSymbolResolver->findSymbolInLogicalDylib(Name);
466  },
467  [&LD](const std::string &Name) {
468  return LD.ExternalSymbolResolver->findSymbol(Name);
469  });
470 
471  if (auto GVsHOrErr =
472  BaseLayer.addModule(std::move(GVsM), std::move(GVsResolver)))
473  LD.BaseLayerHandles.push_back(*GVsHOrErr);
474  else
475  return GVsHOrErr.takeError();
476 
477  return Error::success();
478  }
479 
480  static std::string mangle(StringRef Name, const DataLayout &DL) {
481  std::string MangledName;
482  {
483  raw_string_ostream MangledNameStream(MangledName);
484  Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
485  }
486  return MangledName;
487  }
488 
490  extractAndCompile(LogicalDylib &LD,
491  typename LogicalDylib::SourceModuleHandle LMId,
492  Function &F) {
493  Module &SrcM = LD.getSourceModule(LMId);
494 
495  // If F is a declaration we must already have compiled it.
496  if (F.isDeclaration())
497  return 0;
498 
499  // Grab the name of the function being called here.
500  std::string CalledFnName = mangle(F.getName(), SrcM.getDataLayout());
501 
502  JITTargetAddress CalledAddr = 0;
503  auto Part = Partition(F);
504  if (auto PartHOrErr = emitPartition(LD, LMId, Part)) {
505  auto &PartH = *PartHOrErr;
506  for (auto *SubF : Part) {
507  std::string FnName = mangle(SubF->getName(), SrcM.getDataLayout());
508  if (auto FnBodySym = BaseLayer.findSymbolIn(PartH, FnName, false)) {
509  if (auto FnBodyAddrOrErr = FnBodySym.getAddress()) {
510  JITTargetAddress FnBodyAddr = *FnBodyAddrOrErr;
511 
512  // If this is the function we're calling record the address so we can
513  // return it from this function.
514  if (SubF == &F)
515  CalledAddr = FnBodyAddr;
516 
517  // Update the function body pointer for the stub.
518  if (auto EC = LD.StubsMgr->updatePointer(FnName, FnBodyAddr))
519  return 0;
520 
521  } else
522  return FnBodyAddrOrErr.takeError();
523  } else if (auto Err = FnBodySym.takeError())
524  return std::move(Err);
525  else
526  llvm_unreachable("Function not emitted for partition");
527  }
528 
529  LD.BaseLayerHandles.push_back(PartH);
530  } else
531  return PartHOrErr.takeError();
532 
533  return CalledAddr;
534  }
535 
536  template <typename PartitionT>
538  emitPartition(LogicalDylib &LD,
539  typename LogicalDylib::SourceModuleHandle LMId,
540  const PartitionT &Part) {
541  Module &SrcM = LD.getSourceModule(LMId);
542 
543  // Create the module.
544  std::string NewName = SrcM.getName();
545  for (auto *F : Part) {
546  NewName += ".";
547  NewName += F->getName();
548  }
549 
550  auto M = llvm::make_unique<Module>(NewName, SrcM.getContext());
551  M->setDataLayout(SrcM.getDataLayout());
552  ValueToValueMapTy VMap;
553 
554  auto Materializer = createLambdaMaterializer([&LD, &LMId,
555  &M](Value *V) -> Value * {
556  if (auto *GV = dyn_cast<GlobalVariable>(V))
557  return cloneGlobalVariableDecl(*M, *GV);
558 
559  if (auto *F = dyn_cast<Function>(V)) {
560  // Check whether we want to clone an available_externally definition.
561  if (!LD.getStubsToClone(LMId).count(F))
562  return cloneFunctionDecl(*M, *F);
563 
564  // Ok - we want an inlinable stub. For that to work we need a decl
565  // for the stub pointer.
566  auto *StubPtr = createImplPointer(*F->getType(), *M,
567  F->getName() + "$stub_ptr", nullptr);
568  auto *ClonedF = cloneFunctionDecl(*M, *F);
569  makeStub(*ClonedF, *StubPtr);
570  ClonedF->setLinkage(GlobalValue::AvailableExternallyLinkage);
571  ClonedF->addFnAttr(Attribute::AlwaysInline);
572  return ClonedF;
573  }
574 
575  if (auto *A = dyn_cast<GlobalAlias>(V)) {
576  auto *Ty = A->getValueType();
577  if (Ty->isFunctionTy())
578  return Function::Create(cast<FunctionType>(Ty),
579  GlobalValue::ExternalLinkage, A->getName(),
580  M.get());
581 
582  return new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage,
583  nullptr, A->getName(), nullptr,
585  A->getType()->getAddressSpace());
586  }
587 
588  return nullptr;
589  });
590 
591  // Create decls in the new module.
592  for (auto *F : Part)
593  cloneFunctionDecl(*M, *F, &VMap);
594 
595  // Move the function bodies.
596  for (auto *F : Part)
597  moveFunctionBody(*F, VMap, &Materializer);
598 
599  // Create memory manager and symbol resolver.
600  auto Resolver = createLambdaResolver(
601  [this, &LD](const std::string &Name) -> JITSymbol {
602  if (auto Sym = LD.findSymbol(BaseLayer, Name, false))
603  return Sym;
604  else if (auto Err = Sym.takeError())
605  return std::move(Err);
606  return LD.ExternalSymbolResolver->findSymbolInLogicalDylib(Name);
607  },
608  [&LD](const std::string &Name) {
609  return LD.ExternalSymbolResolver->findSymbol(Name);
610  });
611 
612  return BaseLayer.addModule(std::move(M), std::move(Resolver));
613  }
614 
615  BaseLayerT &BaseLayer;
616  PartitioningFtor Partition;
617  CompileCallbackMgrT &CompileCallbackMgr;
618  IndirectStubsManagerBuilderT CreateIndirectStubsManager;
619 
620  LogicalDylibList LogicalDylibs;
621  bool CloneStubsIntoPartitions;
622 };
623 
624 } // end namespace orc
625 
626 } // end namespace llvm
627 
628 #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
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:159
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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
bool global_empty() const
Definition: Module.h:559
Externally visible function.
Definition: GlobalValue.h:49
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:220
F(f)
Target-independent base class for compile callback management.
Definition: BitVector.h:920
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:614
std::function< std::unique_ptr< IndirectStubsManager >()> 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...
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
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.
std::function< std::set< Function *>(Function &)> PartitioningFtor
Module partitioning functor.
uint64_t JITTargetAddress
Represents an address in the target process&#39;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.
GlobalVariable * cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, ValueToValueMapTy *VMap=nullptr)
Clone a global variable declaration into a new module.
bool alias_empty() const
Definition: Module.h:599
This is a class that can be implemented by clients to materialize Values on demand.
Definition: ValueMapper.h:51
void getModuleFlagsMetadata(SmallVectorImpl< ModuleFlagEntry > &Flags) const
Returns the module flags in the provided vector.
Definition: Module.cpp:282
This is an important base class in LLVM.
Definition: Constant.h:42
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define H(x, y, z)
Definition: MD5.cpp:57
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:962
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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:206
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
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.
Module.h This file contains the declarations for the Module class.
const DataFlowGraph & G
Definition: RDFGraph.cpp:211
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
Class for arbitrary precision integers.
Definition: APInt.h:69
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. ...
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:408
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:220
static void rename(GlobalValue *GV)
Definition: AutoUpgrade.cpp:36
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:201
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: ValueMap.h:154
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:462
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.
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable&#39;s name.
Definition: Mangler.cpp:109
JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
iterator_range< global_iterator > globals()
Definition: Module.h:561
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:136
void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap, ValueMaterializer *Materializer=nullptr, Function *NewF=nullptr)
Move the body of function &#39;F&#39; 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:400
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:265
iterator_range< alias_iterator > aliases()
Definition: Module.h:601