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