LLVM  8.0.1
MCJIT.cpp
Go to the documentation of this file.
1 //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
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 #include "MCJIT.h"
11 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Object/Archive.h"
23 #include "llvm/Object/ObjectFile.h"
28 
29 using namespace llvm;
30 
31 namespace {
32 
33 static struct RegisterJIT {
34  RegisterJIT() { MCJIT::Register(); }
35 } JITRegistrator;
36 
37 }
38 
39 extern "C" void LLVMLinkInMCJIT() {
40 }
41 
43 MCJIT::createJIT(std::unique_ptr<Module> M, std::string *ErrorStr,
44  std::shared_ptr<MCJITMemoryManager> MemMgr,
45  std::shared_ptr<LegacyJITSymbolResolver> Resolver,
46  std::unique_ptr<TargetMachine> TM) {
47  // Try to register the program as a source of symbols to resolve against.
48  //
49  // FIXME: Don't do this here.
51 
52  if (!MemMgr || !Resolver) {
53  auto RTDyldMM = std::make_shared<SectionMemoryManager>();
54  if (!MemMgr)
55  MemMgr = RTDyldMM;
56  if (!Resolver)
57  Resolver = RTDyldMM;
58  }
59 
60  return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
61  std::move(Resolver));
62 }
63 
64 MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM,
65  std::shared_ptr<MCJITMemoryManager> MemMgr,
66  std::shared_ptr<LegacyJITSymbolResolver> Resolver)
67  : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)),
68  Ctx(nullptr), MemMgr(std::move(MemMgr)),
69  Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver),
70  ObjCache(nullptr) {
71  // FIXME: We are managing our modules, so we do not want the base class
72  // ExecutionEngine to manage them as well. To avoid double destruction
73  // of the first (and only) module added in ExecutionEngine constructor
74  // we remove it from EE and will destruct it ourselves.
75  //
76  // It may make sense to move our module manager (based on SmallStPtr) back
77  // into EE if the JIT and Interpreter can live with it.
78  // If so, additional functions: addModule, removeModule, FindFunctionNamed,
79  // runStaticConstructorsDestructors could be moved back to EE as well.
80  //
81  std::unique_ptr<Module> First = std::move(Modules[0]);
82  Modules.clear();
83 
84  if (First->getDataLayout().isDefault())
85  First->setDataLayout(getDataLayout());
86 
87  OwnedModules.addModule(std::move(First));
88  RegisterJITEventListener(JITEventListener::createGDBRegistrationListener());
89 }
90 
92  MutexGuard locked(lock);
93 
94  Dyld.deregisterEHFrames();
95 
96  for (auto &Obj : LoadedObjects)
97  if (Obj)
98  notifyFreeingObject(*Obj);
99 
100  Archives.clear();
101 }
102 
103 void MCJIT::addModule(std::unique_ptr<Module> M) {
104  MutexGuard locked(lock);
105 
106  if (M->getDataLayout().isDefault())
107  M->setDataLayout(getDataLayout());
108 
109  OwnedModules.addModule(std::move(M));
110 }
111 
113  MutexGuard locked(lock);
114  return OwnedModules.removeModule(M);
115 }
116 
117 void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
118  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj);
119  if (Dyld.hasError())
120  report_fatal_error(Dyld.getErrorString());
121 
122  notifyObjectLoaded(*Obj, *L);
123 
124  LoadedObjects.push_back(std::move(Obj));
125 }
126 
128  std::unique_ptr<object::ObjectFile> ObjFile;
129  std::unique_ptr<MemoryBuffer> MemBuf;
130  std::tie(ObjFile, MemBuf) = Obj.takeBinary();
131  addObjectFile(std::move(ObjFile));
132  Buffers.push_back(std::move(MemBuf));
133 }
134 
136  Archives.push_back(std::move(A));
137 }
138 
140  MutexGuard locked(lock);
141  ObjCache = NewCache;
142 }
143 
144 std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
145  assert(M && "Can not emit a null module");
146 
147  MutexGuard locked(lock);
148 
149  // Materialize all globals in the module if they have not been
150  // materialized already.
151  cantFail(M->materializeAll());
152 
153  // This must be a module which has already been added but not loaded to this
154  // MCJIT instance, since these conditions are tested by our caller,
155  // generateCodeForModule.
156 
158 
159  // The RuntimeDyld will take ownership of this shortly
160  SmallVector<char, 4096> ObjBufferSV;
161  raw_svector_ostream ObjStream(ObjBufferSV);
162 
163  // Turn the machine code intermediate representation into bytes in memory
164  // that may be executed.
165  if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
166  report_fatal_error("Target does not support MC emission!");
167 
168  // Initialize passes.
169  PM.run(*M);
170  // Flush the output buffer to get the generated code into memory
171 
172  std::unique_ptr<MemoryBuffer> CompiledObjBuffer(
173  new SmallVectorMemoryBuffer(std::move(ObjBufferSV)));
174 
175  // If we have an object cache, tell it about the new object.
176  // Note that we're using the compiled image, not the loaded image (as below).
177  if (ObjCache) {
178  // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
179  // to create a temporary object here and delete it after the call.
180  MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef();
181  ObjCache->notifyObjectCompiled(M, MB);
182  }
183 
184  return CompiledObjBuffer;
185 }
186 
188  // Get a thread lock to make sure we aren't trying to load multiple times
189  MutexGuard locked(lock);
190 
191  // This must be a module which has already been added to this MCJIT instance.
192  assert(OwnedModules.ownsModule(M) &&
193  "MCJIT::generateCodeForModule: Unknown module.");
194 
195  // Re-compilation is not supported
196  if (OwnedModules.hasModuleBeenLoaded(M))
197  return;
198 
199  std::unique_ptr<MemoryBuffer> ObjectToLoad;
200  // Try to load the pre-compiled object from cache if possible
201  if (ObjCache)
202  ObjectToLoad = ObjCache->getObject(M);
203 
204  assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
205 
206  // If the cache did not contain a suitable object, compile the object
207  if (!ObjectToLoad) {
208  ObjectToLoad = emitObject(M);
209  assert(ObjectToLoad && "Compilation did not produce an object.");
210  }
211 
212  // Load the object into the dynamic linker.
213  // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
215  object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
216  if (!LoadedObject) {
217  std::string Buf;
218  raw_string_ostream OS(Buf);
219  logAllUnhandledErrors(LoadedObject.takeError(), OS);
220  OS.flush();
221  report_fatal_error(Buf);
222  }
223  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
224  Dyld.loadObject(*LoadedObject.get());
225 
226  if (Dyld.hasError())
227  report_fatal_error(Dyld.getErrorString());
228 
229  notifyObjectLoaded(*LoadedObject.get(), *L);
230 
231  Buffers.push_back(std::move(ObjectToLoad));
232  LoadedObjects.push_back(std::move(*LoadedObject));
233 
234  OwnedModules.markModuleAsLoaded(M);
235 }
236 
238  MutexGuard locked(lock);
239 
240  // Resolve any outstanding relocations.
241  Dyld.resolveRelocations();
242 
243  OwnedModules.markAllLoadedModulesAsFinalized();
244 
245  // Register EH frame data for any module we own which has been loaded
246  Dyld.registerEHFrames();
247 
248  // Set page permissions.
249  MemMgr->finalizeMemory();
250 }
251 
252 // FIXME: Rename this.
254  MutexGuard locked(lock);
255 
256  // Generate code for module is going to move objects out of the 'added' list,
257  // so we need to copy that out before using it:
258  SmallVector<Module*, 16> ModsToAdd;
259  for (auto M : OwnedModules.added())
260  ModsToAdd.push_back(M);
261 
262  for (auto M : ModsToAdd)
263  generateCodeForModule(M);
264 
265  finalizeLoadedModules();
266 }
267 
269  MutexGuard locked(lock);
270 
271  // This must be a module which has already been added to this MCJIT instance.
272  assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
273 
274  // If the module hasn't been compiled, just do that.
275  if (!OwnedModules.hasModuleBeenLoaded(M))
276  generateCodeForModule(M);
277 
278  finalizeLoadedModules();
279 }
280 
282  if (void *Addr = getPointerToGlobalIfAvailable(Name))
283  return JITSymbol(static_cast<uint64_t>(
284  reinterpret_cast<uintptr_t>(Addr)),
286 
287  return Dyld.getSymbol(Name);
288 }
289 
291  bool CheckFunctionsOnly) {
292  StringRef DemangledName = Name;
293  if (DemangledName[0] == getDataLayout().getGlobalPrefix())
294  DemangledName = DemangledName.substr(1);
295 
296  MutexGuard locked(lock);
297 
298  // If it hasn't already been generated, see if it's in one of our modules.
299  for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
300  E = OwnedModules.end_added();
301  I != E; ++I) {
302  Module *M = *I;
303  Function *F = M->getFunction(DemangledName);
304  if (F && !F->isDeclaration())
305  return M;
306  if (!CheckFunctionsOnly) {
307  GlobalVariable *G = M->getGlobalVariable(DemangledName);
308  if (G && !G->isDeclaration())
309  return M;
310  // FIXME: Do we need to worry about global aliases?
311  }
312  }
313  // We didn't find the symbol in any of our modules.
314  return nullptr;
315 }
316 
317 uint64_t MCJIT::getSymbolAddress(const std::string &Name,
318  bool CheckFunctionsOnly) {
319  std::string MangledName;
320  {
321  raw_string_ostream MangledNameStream(MangledName);
322  Mangler::getNameWithPrefix(MangledNameStream, Name, getDataLayout());
323  }
324  if (auto Sym = findSymbol(MangledName, CheckFunctionsOnly)) {
325  if (auto AddrOrErr = Sym.getAddress())
326  return *AddrOrErr;
327  else
328  report_fatal_error(AddrOrErr.takeError());
329  } else if (auto Err = Sym.takeError())
330  report_fatal_error(Sym.takeError());
331  return 0;
332 }
333 
334 JITSymbol MCJIT::findSymbol(const std::string &Name,
335  bool CheckFunctionsOnly) {
336  MutexGuard locked(lock);
337 
338  // First, check to see if we already have this symbol.
339  if (auto Sym = findExistingSymbol(Name))
340  return Sym;
341 
342  for (object::OwningBinary<object::Archive> &OB : Archives) {
343  object::Archive *A = OB.getBinary();
344  // Look for our symbols in each Archive
345  auto OptionalChildOrErr = A->findSym(Name);
346  if (!OptionalChildOrErr)
347  report_fatal_error(OptionalChildOrErr.takeError());
348  auto &OptionalChild = *OptionalChildOrErr;
349  if (OptionalChild) {
350  // FIXME: Support nested archives?
352  OptionalChild->getAsBinary();
353  if (!ChildBinOrErr) {
354  // TODO: Actually report errors helpfully.
355  consumeError(ChildBinOrErr.takeError());
356  continue;
357  }
358  std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
359  if (ChildBin->isObject()) {
360  std::unique_ptr<object::ObjectFile> OF(
361  static_cast<object::ObjectFile *>(ChildBin.release()));
362  // This causes the object file to be loaded.
363  addObjectFile(std::move(OF));
364  // The address should be here now.
365  if (auto Sym = findExistingSymbol(Name))
366  return Sym;
367  }
368  }
369  }
370 
371  // If it hasn't already been generated, see if it's in one of our modules.
372  Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
373  if (M) {
374  generateCodeForModule(M);
375 
376  // Check the RuntimeDyld table again, it should be there now.
377  return findExistingSymbol(Name);
378  }
379 
380  // If a LazyFunctionCreator is installed, use it to get/create the function.
381  // FIXME: Should we instead have a LazySymbolCreator callback?
382  if (LazyFunctionCreator) {
383  auto Addr = static_cast<uint64_t>(
384  reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name)));
385  return JITSymbol(Addr, JITSymbolFlags::Exported);
386  }
387 
388  return nullptr;
389 }
390 
391 uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
392  MutexGuard locked(lock);
393  uint64_t Result = getSymbolAddress(Name, false);
394  if (Result != 0)
395  finalizeLoadedModules();
396  return Result;
397 }
398 
399 uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
400  MutexGuard locked(lock);
401  uint64_t Result = getSymbolAddress(Name, true);
402  if (Result != 0)
403  finalizeLoadedModules();
404  return Result;
405 }
406 
407 // Deprecated. Use getFunctionAddress instead.
409  MutexGuard locked(lock);
410 
411  Mangler Mang;
413  TM->getNameWithPrefix(Name, F, Mang);
414 
415  if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
416  bool AbortOnFailure = !F->hasExternalWeakLinkage();
417  void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
418  updateGlobalMapping(F, Addr);
419  return Addr;
420  }
421 
422  Module *M = F->getParent();
423  bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
424 
425  // Make sure the relevant module has been compiled and loaded.
426  if (HasBeenAddedButNotLoaded)
427  generateCodeForModule(M);
428  else if (!OwnedModules.hasModuleBeenLoaded(M)) {
429  // If this function doesn't belong to one of our modules, we're done.
430  // FIXME: Asking for the pointer to a function that hasn't been registered,
431  // and isn't a declaration (which is handled above) should probably
432  // be an assertion.
433  return nullptr;
434  }
435 
436  // FIXME: Should the Dyld be retaining module information? Probably not.
437  //
438  // This is the accessor for the target address, so make sure to check the
439  // load address of the symbol, not the local address.
440  return (void*)Dyld.getSymbol(Name).getAddress();
441 }
442 
443 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
444  bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
445  for (; I != E; ++I) {
447  }
448 }
449 
451  // Execute global ctors/dtors for each module in the program.
452  runStaticConstructorsDestructorsInModulePtrSet(
453  isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
454  runStaticConstructorsDestructorsInModulePtrSet(
455  isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
456  runStaticConstructorsDestructorsInModulePtrSet(
457  isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
458 }
459 
460 Function *MCJIT::FindFunctionNamedInModulePtrSet(StringRef FnName,
461  ModulePtrSet::iterator I,
462  ModulePtrSet::iterator E) {
463  for (; I != E; ++I) {
464  Function *F = (*I)->getFunction(FnName);
465  if (F && !F->isDeclaration())
466  return F;
467  }
468  return nullptr;
469 }
470 
471 GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(StringRef Name,
472  bool AllowInternal,
473  ModulePtrSet::iterator I,
474  ModulePtrSet::iterator E) {
475  for (; I != E; ++I) {
476  GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal);
477  if (GV && !GV->isDeclaration())
478  return GV;
479  }
480  return nullptr;
481 }
482 
483 
485  Function *F = FindFunctionNamedInModulePtrSet(
486  FnName, OwnedModules.begin_added(), OwnedModules.end_added());
487  if (!F)
488  F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
489  OwnedModules.end_loaded());
490  if (!F)
491  F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
492  OwnedModules.end_finalized());
493  return F;
494 }
495 
497  GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet(
498  Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added());
499  if (!GV)
500  GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(),
501  OwnedModules.end_loaded());
502  if (!GV)
503  GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(),
504  OwnedModules.end_finalized());
505  return GV;
506 }
507 
509  assert(F && "Function *F was null at entry to run()");
510 
511  void *FPtr = getPointerToFunction(F);
512  finalizeModule(F->getParent());
513  assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
514  FunctionType *FTy = F->getFunctionType();
515  Type *RetTy = FTy->getReturnType();
516 
517  assert((FTy->getNumParams() == ArgValues.size() ||
518  (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
519  "Wrong number of arguments passed into function!");
520  assert(FTy->getNumParams() == ArgValues.size() &&
521  "This doesn't support passing arguments through varargs (yet)!");
522 
523  // Handle some common cases first. These cases correspond to common `main'
524  // prototypes.
525  if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
526  switch (ArgValues.size()) {
527  case 3:
528  if (FTy->getParamType(0)->isIntegerTy(32) &&
529  FTy->getParamType(1)->isPointerTy() &&
530  FTy->getParamType(2)->isPointerTy()) {
531  int (*PF)(int, char **, const char **) =
532  (int(*)(int, char **, const char **))(intptr_t)FPtr;
533 
534  // Call the function.
535  GenericValue rv;
536  rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
537  (char **)GVTOP(ArgValues[1]),
538  (const char **)GVTOP(ArgValues[2])));
539  return rv;
540  }
541  break;
542  case 2:
543  if (FTy->getParamType(0)->isIntegerTy(32) &&
544  FTy->getParamType(1)->isPointerTy()) {
545  int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
546 
547  // Call the function.
548  GenericValue rv;
549  rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
550  (char **)GVTOP(ArgValues[1])));
551  return rv;
552  }
553  break;
554  case 1:
555  if (FTy->getNumParams() == 1 &&
556  FTy->getParamType(0)->isIntegerTy(32)) {
557  GenericValue rv;
558  int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
559  rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
560  return rv;
561  }
562  break;
563  }
564  }
565 
566  // Handle cases where no arguments are passed first.
567  if (ArgValues.empty()) {
568  GenericValue rv;
569  switch (RetTy->getTypeID()) {
570  default: llvm_unreachable("Unknown return type for function call!");
571  case Type::IntegerTyID: {
572  unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
573  if (BitWidth == 1)
574  rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
575  else if (BitWidth <= 8)
576  rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
577  else if (BitWidth <= 16)
578  rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
579  else if (BitWidth <= 32)
580  rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
581  else if (BitWidth <= 64)
582  rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
583  else
584  llvm_unreachable("Integer types > 64 bits not supported");
585  return rv;
586  }
587  case Type::VoidTyID:
588  rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
589  return rv;
590  case Type::FloatTyID:
591  rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
592  return rv;
593  case Type::DoubleTyID:
594  rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
595  return rv;
596  case Type::X86_FP80TyID:
597  case Type::FP128TyID:
598  case Type::PPC_FP128TyID:
599  llvm_unreachable("long double not supported yet");
600  case Type::PointerTyID:
601  return PTOGV(((void*(*)())(intptr_t)FPtr)());
602  }
603  }
604 
605  report_fatal_error("MCJIT::runFunction does not support full-featured "
606  "argument passing. Please use "
607  "ExecutionEngine::getFunctionAddress and cast the result "
608  "to the desired function pointer type.");
609 }
610 
611 void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
612  if (!isSymbolSearchingDisabled()) {
613  if (auto Sym = Resolver.findSymbol(Name)) {
614  if (auto AddrOrErr = Sym.getAddress())
615  return reinterpret_cast<void*>(
616  static_cast<uintptr_t>(*AddrOrErr));
617  } else if (auto Err = Sym.takeError())
618  report_fatal_error(std::move(Err));
619  }
620 
621  /// If a LazyFunctionCreator is installed, use it to get/create the function.
622  if (LazyFunctionCreator)
623  if (void *RP = LazyFunctionCreator(Name))
624  return RP;
625 
626  if (AbortOnFailure) {
627  report_fatal_error("Program used external function '"+Name+
628  "' which could not be resolved!");
629  }
630  return nullptr;
631 }
632 
634  if (!L)
635  return;
636  MutexGuard locked(lock);
637  EventListeners.push_back(L);
638 }
639 
641  if (!L)
642  return;
643  MutexGuard locked(lock);
644  auto I = find(reverse(EventListeners), L);
645  if (I != EventListeners.rend()) {
646  std::swap(*I, EventListeners.back());
647  EventListeners.pop_back();
648  }
649 }
650 
653  uint64_t Key =
654  static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data()));
655  MutexGuard locked(lock);
656  MemMgr->notifyObjectLoaded(this, Obj);
657  for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
658  EventListeners[I]->notifyObjectLoaded(Key, Obj, L);
659  }
660 }
661 
663  uint64_t Key =
664  static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data()));
665  MutexGuard locked(lock);
666  for (JITEventListener *L : EventListeners)
667  L->notifyFreeingObject(Key);
668 }
669 
670 JITSymbol
671 LinkingSymbolResolver::findSymbol(const std::string &Name) {
672  auto Result = ParentEngine.findSymbol(Name, false);
673  if (Result)
674  return Result;
675  if (ParentEngine.isSymbolSearchingDisabled())
676  return nullptr;
677  return ClientResolver->findSymbol(Name);
678 }
679 
680 void LinkingSymbolResolver::anchor() {}
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Function * FindFunctionNamed(StringRef FnName) override
FindFunctionNamed - Search all of the active modules to find the function that defines FnName...
Definition: MCJIT.cpp:484
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:704
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:161
Information about the loaded object.
Definition: RuntimeDyld.h:69
void setObjectCache(ObjectCache *manager) override
Sets the object manager that MCJIT should use to avoid compilation.
Definition: MCJIT.cpp:139
llvm::Error materializeAll()
Make sure all GlobalValues in this Module are fully read and clear the Materializer.
Definition: Module.cpp:403
Represents a symbol in the JIT.
Definition: JITSymbol.h:238
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:135
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
2: 32-bit floating point type
Definition: Type.h:59
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
Expected< Optional< Child > > findSym(StringRef name) const
Definition: Archive.cpp:978
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:423
static JITEventListener * createGDBRegistrationListener()
This class is the base class for all object file types.
Definition: ObjectFile.h:202
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:510
void generateCodeForModule(Module *M) override
generateCodeForModule - Run code generation for the specified module and load it into memory...
Definition: MCJIT.cpp:187
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
Definition: Module.h:387
F(f)
4: 80-bit floating point type (X87)
Definition: Type.h:61
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:437
Error takeError()
Take ownership of the stored error.
Definition: Error.h:553
15: Pointers
Definition: Type.h:75
void UnregisterJITEventListener(JITEventListener *L) override
Definition: MCJIT.cpp:640
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
static void Register()
Definition: MCJIT.h:300
amdgpu Simplify well known AMD library false Value Value const Twine & Name
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:371
virtual void runStaticConstructorsDestructors(bool isDtors)
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:138
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
void runStaticConstructorsDestructors(bool isDtors) override
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
Definition: MCJIT.cpp:450
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Definition: STLExtras.h:267
Key
PAL metadata keys.
uint64_t getGlobalValueAddress(const std::string &Name) override
getGlobalValueAddress - Return the address of the specified global value.
Definition: MCJIT.cpp:391
Class to represent function types.
Definition: DerivedTypes.h:103
virtual void finalizeModule(Module *)
Definition: MCJIT.cpp:268
void notifyObjectLoaded(const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &L)
Definition: MCJIT.cpp:651
GlobalVariable * FindGlobalVariableNamed(StringRef Name, bool AllowInternal=false) override
FindGlobalVariableNamed - Search all of the active modules to find the global variable that defines N...
Definition: MCJIT.cpp:496
bool isVarArg() const
Definition: DerivedTypes.h:123
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void * getPointerToFunction(Function *F) override
getPointerToFunction - The different EE&#39;s represent function bodies in different ways.
Definition: MCJIT.cpp:408
JITSymbol findSymbol(const std::string &Name) override
This method returns the address of the specified function or variable.
Definition: MCJIT.cpp:671
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
JITSymbol findExistingSymbol(const std::string &Name)
Definition: MCJIT.cpp:281
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
Definition: MCJIT.cpp:253
11: Arbitrary bit width integers
Definition: Type.h:71
PassManager manages ModulePassManagers.
bool isVoidTy() const
Return true if this is &#39;void&#39;.
Definition: Type.h:141
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
Definition: MutexGuard.h:27
0: type with no size
Definition: Type.h:57
void addModule(std::unique_ptr< Module > M) override
Add a Module to the list of modules that we can JIT from.
Definition: MCJIT.cpp:103
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
static ExecutionEngine * createJIT(std::unique_ptr< Module > M, std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MemMgr, std::shared_ptr< LegacyJITSymbolResolver > Resolver, std::unique_ptr< TargetMachine > TM)
Definition: MCJIT.cpp:43
bool removeModule(Module *M) override
removeModule - Removes a Module from the list of modules, but does not free the module&#39;s memory...
Definition: MCJIT.cpp:112
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
Definition: MCJIT.cpp:611
void LLVMLinkInMCJIT()
Definition: MCJIT.cpp:39
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:224
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1774
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
Definition: MCJIT.cpp:399
6: 128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:63
JITSymbol findSymbol(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:334
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:982
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1207
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:62
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
void * GVTOP(const GenericValue &GV)
Definition: GenericValue.h:51
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
Definition: Binary.h:201
void addArchive(object::OwningBinary< object::Archive > O) override
addArchive - Add an Archive to the execution engine.
Definition: MCJIT.cpp:135
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Module.h This file contains the declarations for the Module class.
const DataFlowGraph & G
Definition: RDFGraph.cpp:211
Type * getReturnType() const
Definition: DerivedTypes.h:124
reference get()
Returns a reference to the stored T value.
Definition: Error.h:533
uint64_t getSymbolAddress(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:317
void RegisterJITEventListener(JITEventListener *L) override
Registers a listener to be called back on various events within the JIT.
Definition: MCJIT.cpp:633
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:176
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:941
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:164
GenericValue PTOGV(void *P)
Definition: GenericValue.h:50
Class for arbitrary precision integers.
Definition: APInt.h:70
Module * findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:290
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
runFunction - Execute the specified function with the specified arguments, and return the result...
Definition: MCJIT.cpp:508
void notifyFreeingObject(const object::ObjectFile &Obj)
Definition: MCJIT.cpp:662
std::unique_ptr< MemoryBuffer > emitObject(Module *M)
emitObject – Generate a JITed object in memory from the specified module Currently, MCJIT only supports a single module and the module passed to this function call is expected to be the contained module.
Definition: MCJIT.cpp:144
SmallVector-backed MemoryBuffer instance.
#define I(x, y, z)
Definition: MD5.cpp:58
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:23
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:206
3: 64-bit floating point type
Definition: Type.h:60
~MCJIT() override
Definition: MCJIT.cpp:91
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:483
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:566
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:112
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
StringRef getData() const
Definition: Binary.cpp:39
void finalizeLoadedModules()
Definition: MCJIT.cpp:237
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
5: 128-bit floating point type (112-bit mantissa)
Definition: Type.h:62
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
Definition: MCJIT.cpp:117