LLVM  14.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),
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));
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())
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  auto CompiledObjBuffer = std::make_unique<SmallVectorMemoryBuffer>(
174  std::move(ObjBufferSV), /*RequiresNullTerminator=*/false);
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);
222  }
223  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
224  Dyld.loadObject(*LoadedObject.get());
225 
226  if (Dyld.hasError())
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  std::lock_guard<sys::Mutex> locked(lock);
239 
240  // Resolve any outstanding relocations.
241  Dyld.resolveRelocations();
242 
243  // Check for Dyld error.
244  if (Dyld.hasError())
245  ErrMsg = Dyld.getErrorString().str();
246 
247  OwnedModules.markAllLoadedModulesAsFinalized();
248 
249  // Register EH frame data for any module we own which has been loaded
250  Dyld.registerEHFrames();
251 
252  // Set page permissions.
253  MemMgr->finalizeMemory();
254 }
255 
256 // FIXME: Rename this.
258  std::lock_guard<sys::Mutex> locked(lock);
259 
260  // Generate code for module is going to move objects out of the 'added' list,
261  // so we need to copy that out before using it:
262  SmallVector<Module*, 16> ModsToAdd;
263  for (auto M : OwnedModules.added())
264  ModsToAdd.push_back(M);
265 
266  for (auto M : ModsToAdd)
268 
270 }
271 
273  std::lock_guard<sys::Mutex> locked(lock);
274 
275  // This must be a module which has already been added to this MCJIT instance.
276  assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
277 
278  // If the module hasn't been compiled, just do that.
279  if (!OwnedModules.hasModuleBeenLoaded(M))
281 
283 }
284 
287  return JITSymbol(static_cast<uint64_t>(
288  reinterpret_cast<uintptr_t>(Addr)),
290 
291  return Dyld.getSymbol(Name);
292 }
293 
295  bool CheckFunctionsOnly) {
296  StringRef DemangledName = Name;
297  if (DemangledName[0] == getDataLayout().getGlobalPrefix())
298  DemangledName = DemangledName.substr(1);
299 
300  std::lock_guard<sys::Mutex> locked(lock);
301 
302  // If it hasn't already been generated, see if it's in one of our modules.
303  for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
304  E = OwnedModules.end_added();
305  I != E; ++I) {
306  Module *M = *I;
307  Function *F = M->getFunction(DemangledName);
308  if (F && !F->isDeclaration())
309  return M;
310  if (!CheckFunctionsOnly) {
311  GlobalVariable *G = M->getGlobalVariable(DemangledName);
312  if (G && !G->isDeclaration())
313  return M;
314  // FIXME: Do we need to worry about global aliases?
315  }
316  }
317  // We didn't find the symbol in any of our modules.
318  return nullptr;
319 }
320 
322  bool CheckFunctionsOnly) {
323  std::string MangledName;
324  {
325  raw_string_ostream MangledNameStream(MangledName);
326  Mangler::getNameWithPrefix(MangledNameStream, Name, getDataLayout());
327  }
328  if (auto Sym = findSymbol(MangledName, CheckFunctionsOnly)) {
329  if (auto AddrOrErr = Sym.getAddress())
330  return *AddrOrErr;
331  else
332  report_fatal_error(AddrOrErr.takeError());
333  } else if (auto Err = Sym.takeError())
334  report_fatal_error(Sym.takeError());
335  return 0;
336 }
337 
338 JITSymbol MCJIT::findSymbol(const std::string &Name,
339  bool CheckFunctionsOnly) {
340  std::lock_guard<sys::Mutex> locked(lock);
341 
342  // First, check to see if we already have this symbol.
343  if (auto Sym = findExistingSymbol(Name))
344  return Sym;
345 
346  for (object::OwningBinary<object::Archive> &OB : Archives) {
347  object::Archive *A = OB.getBinary();
348  // Look for our symbols in each Archive
349  auto OptionalChildOrErr = A->findSym(Name);
350  if (!OptionalChildOrErr)
351  report_fatal_error(OptionalChildOrErr.takeError());
352  auto &OptionalChild = *OptionalChildOrErr;
353  if (OptionalChild) {
354  // FIXME: Support nested archives?
356  OptionalChild->getAsBinary();
357  if (!ChildBinOrErr) {
358  // TODO: Actually report errors helpfully.
359  consumeError(ChildBinOrErr.takeError());
360  continue;
361  }
362  std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
363  if (ChildBin->isObject()) {
364  std::unique_ptr<object::ObjectFile> OF(
365  static_cast<object::ObjectFile *>(ChildBin.release()));
366  // This causes the object file to be loaded.
368  // The address should be here now.
369  if (auto Sym = findExistingSymbol(Name))
370  return Sym;
371  }
372  }
373  }
374 
375  // If it hasn't already been generated, see if it's in one of our modules.
376  Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
377  if (M) {
379 
380  // Check the RuntimeDyld table again, it should be there now.
381  return findExistingSymbol(Name);
382  }
383 
384  // If a LazyFunctionCreator is installed, use it to get/create the function.
385  // FIXME: Should we instead have a LazySymbolCreator callback?
386  if (LazyFunctionCreator) {
387  auto Addr = static_cast<uint64_t>(
388  reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name)));
390  }
391 
392  return nullptr;
393 }
394 
396  std::lock_guard<sys::Mutex> locked(lock);
397  uint64_t Result = getSymbolAddress(Name, false);
398  if (Result != 0)
400  return Result;
401 }
402 
404  std::lock_guard<sys::Mutex> locked(lock);
405  uint64_t Result = getSymbolAddress(Name, true);
406  if (Result != 0)
408  return Result;
409 }
410 
411 // Deprecated. Use getFunctionAddress instead.
413  std::lock_guard<sys::Mutex> locked(lock);
414 
415  Mangler Mang;
417  TM->getNameWithPrefix(Name, F, Mang);
418 
419  if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
420  bool AbortOnFailure = !F->hasExternalWeakLinkage();
421  void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
423  return Addr;
424  }
425 
426  Module *M = F->getParent();
427  bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
428 
429  // Make sure the relevant module has been compiled and loaded.
430  if (HasBeenAddedButNotLoaded)
432  else if (!OwnedModules.hasModuleBeenLoaded(M)) {
433  // If this function doesn't belong to one of our modules, we're done.
434  // FIXME: Asking for the pointer to a function that hasn't been registered,
435  // and isn't a declaration (which is handled above) should probably
436  // be an assertion.
437  return nullptr;
438  }
439 
440  // FIXME: Should the Dyld be retaining module information? Probably not.
441  //
442  // This is the accessor for the target address, so make sure to check the
443  // load address of the symbol, not the local address.
444  return (void*)Dyld.getSymbol(Name).getAddress();
445 }
446 
447 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
448  bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
449  for (; I != E; ++I) {
451  }
452 }
453 
455  // Execute global ctors/dtors for each module in the program.
456  runStaticConstructorsDestructorsInModulePtrSet(
457  isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
458  runStaticConstructorsDestructorsInModulePtrSet(
459  isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
460  runStaticConstructorsDestructorsInModulePtrSet(
461  isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
462 }
463 
464 Function *MCJIT::FindFunctionNamedInModulePtrSet(StringRef FnName,
465  ModulePtrSet::iterator I,
466  ModulePtrSet::iterator E) {
467  for (; I != E; ++I) {
468  Function *F = (*I)->getFunction(FnName);
469  if (F && !F->isDeclaration())
470  return F;
471  }
472  return nullptr;
473 }
474 
475 GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(StringRef Name,
476  bool AllowInternal,
477  ModulePtrSet::iterator I,
478  ModulePtrSet::iterator E) {
479  for (; I != E; ++I) {
480  GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal);
481  if (GV && !GV->isDeclaration())
482  return GV;
483  }
484  return nullptr;
485 }
486 
487 
489  Function *F = FindFunctionNamedInModulePtrSet(
490  FnName, OwnedModules.begin_added(), OwnedModules.end_added());
491  if (!F)
492  F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
493  OwnedModules.end_loaded());
494  if (!F)
495  F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
496  OwnedModules.end_finalized());
497  return F;
498 }
499 
501  GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet(
502  Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added());
503  if (!GV)
504  GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(),
505  OwnedModules.end_loaded());
506  if (!GV)
507  GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(),
508  OwnedModules.end_finalized());
509  return GV;
510 }
511 
513  assert(F && "Function *F was null at entry to run()");
514 
515  void *FPtr = getPointerToFunction(F);
516  finalizeModule(F->getParent());
517  assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
518  FunctionType *FTy = F->getFunctionType();
519  Type *RetTy = FTy->getReturnType();
520 
521  assert((FTy->getNumParams() == ArgValues.size() ||
522  (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
523  "Wrong number of arguments passed into function!");
524  assert(FTy->getNumParams() == ArgValues.size() &&
525  "This doesn't support passing arguments through varargs (yet)!");
526 
527  // Handle some common cases first. These cases correspond to common `main'
528  // prototypes.
529  if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
530  switch (ArgValues.size()) {
531  case 3:
532  if (FTy->getParamType(0)->isIntegerTy(32) &&
533  FTy->getParamType(1)->isPointerTy() &&
534  FTy->getParamType(2)->isPointerTy()) {
535  int (*PF)(int, char **, const char **) =
536  (int(*)(int, char **, const char **))(intptr_t)FPtr;
537 
538  // Call the function.
539  GenericValue rv;
540  rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
541  (char **)GVTOP(ArgValues[1]),
542  (const char **)GVTOP(ArgValues[2])));
543  return rv;
544  }
545  break;
546  case 2:
547  if (FTy->getParamType(0)->isIntegerTy(32) &&
548  FTy->getParamType(1)->isPointerTy()) {
549  int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
550 
551  // Call the function.
552  GenericValue rv;
553  rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
554  (char **)GVTOP(ArgValues[1])));
555  return rv;
556  }
557  break;
558  case 1:
559  if (FTy->getNumParams() == 1 &&
560  FTy->getParamType(0)->isIntegerTy(32)) {
561  GenericValue rv;
562  int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
563  rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
564  return rv;
565  }
566  break;
567  }
568  }
569 
570  // Handle cases where no arguments are passed first.
571  if (ArgValues.empty()) {
572  GenericValue rv;
573  switch (RetTy->getTypeID()) {
574  default: llvm_unreachable("Unknown return type for function call!");
575  case Type::IntegerTyID: {
576  unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
577  if (BitWidth == 1)
578  rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
579  else if (BitWidth <= 8)
580  rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
581  else if (BitWidth <= 16)
582  rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
583  else if (BitWidth <= 32)
584  rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
585  else if (BitWidth <= 64)
586  rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
587  else
588  llvm_unreachable("Integer types > 64 bits not supported");
589  return rv;
590  }
591  case Type::VoidTyID:
592  rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
593  return rv;
594  case Type::FloatTyID:
595  rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
596  return rv;
597  case Type::DoubleTyID:
598  rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
599  return rv;
600  case Type::X86_FP80TyID:
601  case Type::FP128TyID:
602  case Type::PPC_FP128TyID:
603  llvm_unreachable("long double not supported yet");
604  case Type::PointerTyID:
605  return PTOGV(((void*(*)())(intptr_t)FPtr)());
606  }
607  }
608 
609  report_fatal_error("MCJIT::runFunction does not support full-featured "
610  "argument passing. Please use "
611  "ExecutionEngine::getFunctionAddress and cast the result "
612  "to the desired function pointer type.");
613 }
614 
615 void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
616  if (!isSymbolSearchingDisabled()) {
617  if (auto Sym = Resolver.findSymbol(std::string(Name))) {
618  if (auto AddrOrErr = Sym.getAddress())
619  return reinterpret_cast<void*>(
620  static_cast<uintptr_t>(*AddrOrErr));
621  } else if (auto Err = Sym.takeError())
623  }
624 
625  /// If a LazyFunctionCreator is installed, use it to get/create the function.
627  if (void *RP = LazyFunctionCreator(std::string(Name)))
628  return RP;
629 
630  if (AbortOnFailure) {
631  report_fatal_error("Program used external function '"+Name+
632  "' which could not be resolved!");
633  }
634  return nullptr;
635 }
636 
638  if (!L)
639  return;
640  std::lock_guard<sys::Mutex> locked(lock);
641  EventListeners.push_back(L);
642 }
643 
645  if (!L)
646  return;
647  std::lock_guard<sys::Mutex> locked(lock);
648  auto I = find(reverse(EventListeners), L);
649  if (I != EventListeners.rend()) {
650  std::swap(*I, EventListeners.back());
651  EventListeners.pop_back();
652  }
653 }
654 
657  uint64_t Key =
658  static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data()));
659  std::lock_guard<sys::Mutex> locked(lock);
660  MemMgr->notifyObjectLoaded(this, Obj);
661  for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
662  EventListeners[I]->notifyObjectLoaded(Key, Obj, L);
663  }
664 }
665 
667  uint64_t Key =
668  static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Obj.getData().data()));
669  std::lock_guard<sys::Mutex> locked(lock);
670  for (JITEventListener *L : EventListeners)
671  L->notifyFreeingObject(Key);
672 }
673 
674 JITSymbol
676  auto Result = ParentEngine.findSymbol(Name, false);
677  if (Result)
678  return Result;
679  if (ParentEngine.isSymbolSearchingDisabled())
680  return nullptr;
681  return ClientResolver->findSymbol(Name);
682 }
683 
684 void LinkingSymbolResolver::anchor() {}
MemoryBuffer.h
llvm::MCJIT::getFunctionAddress
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
Definition: MCJIT.cpp:403
llvm::Type::FloatTyID
@ FloatTyID
32-bit floating point type
Definition: Type.h:58
llvm::Type::DoubleTyID
@ DoubleTyID
64-bit floating point type
Definition: Type.h:59
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
intptr_t
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:217
llvm::MCJIT::finalizeLoadedModules
void finalizeLoadedModules()
Definition: MCJIT.cpp:237
llvm::ExecutionEngine::updateGlobalMapping
uint64_t updateGlobalMapping(const GlobalValue *GV, void *Addr)
updateGlobalMapping - Replace an existing mapping for GV with a new address.
Definition: ExecutionEngine.cpp:241
llvm::Function
Definition: Function.h:62
llvm::Type::VoidTyID
@ VoidTyID
type with no size
Definition: Type.h:63
llvm::ObjectCache::notifyObjectCompiled
virtual void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj)=0
notifyObjectCompiled - Provides a pointer to compiled code for Module M.
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:631
llvm::MCJIT::FindFunctionNamed
Function * FindFunctionNamed(StringRef FnName) override
FindFunctionNamed - Search all of the active modules to find the function that defines FnName.
Definition: MCJIT.cpp:488
double
into xmm2 addss xmm2 xmm1 xmm3 addss xmm3 movaps xmm0 unpcklps xmm0 ret seems silly when it could just be one addps Expand libm rounding functions main should enable SSE DAZ mode and other fast SSE modes Think about doing i64 math in SSE regs on x86 This testcase should have no SSE instructions in and only one load from a constant double
Definition: README-SSE.txt:85
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1177
ErrorHandling.h
SmallVectorMemoryBuffer.h
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::Type::getTypeID
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:135
llvm::MCJIT::setObjectCache
void setObjectCache(ObjectCache *manager) override
Sets the object manager that MCJIT should use to avoid compilation.
Definition: MCJIT.cpp:140
llvm::MCJIT::getPointerToNamedFunction
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:615
llvm::MCJIT::getSymbolAddress
uint64_t getSymbolAddress(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:321
DynamicLibrary.h
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::reverse
auto reverse(ContainerTy &&C, std::enable_if_t< has_rbegin< ContainerTy >::value > *=nullptr)
Definition: STLExtras.h:414
llvm::ExecutionEngine::isSymbolSearchingDisabled
bool isSymbolSearchingDisabled() const
Definition: ExecutionEngine.h:481
llvm::ObjectCache
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:23
llvm::RuntimeDyld::resolveRelocations
void resolveRelocations()
Resolve the relocations for all symbols we currently know about.
Definition: RuntimeDyld.cpp:1403
llvm::MCJIT::generateCodeForModule
void generateCodeForModule(Module *M) override
generateCodeForModule - Run code generation for the specified module and load it into memory.
Definition: MCJIT.cpp:188
llvm::FunctionType::getNumParams
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
STLExtras.h
llvm::Mangler
Definition: Mangler.h:27
LegacyPassManager.h
llvm::MCJIT::getPointerToFunction
void * getPointerToFunction(Function *F) override
getPointerToFunction - The different EE's represent function bodies in different ways.
Definition: MCJIT.cpp:412
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1045
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::ExecutionEngine::Modules
SmallVector< std::unique_ptr< Module >, 1 > Modules
The list of Modules that we are JIT'ing from.
Definition: ExecutionEngine.h:133
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:158
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::FunctionType::isVarArg
bool isVarArg() const
Definition: DerivedTypes.h:123
llvm::RuntimeDyld::loadObject
std::unique_ptr< LoadedObjectInfo > loadObject(const object::ObjectFile &O)
Add the referenced object file to the list of objects to be loaded and relocated.
Definition: RuntimeDyld.cpp:1353
llvm::ObjectCache::getObject
virtual std::unique_ptr< MemoryBuffer > getObject(const Module *M)=0
Returns a pointer to a newly allocated MemoryBuffer that contains the object which corresponds with M...
llvm::Resolver
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2012
llvm::StringRef::substr
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:610
llvm::ExecutionEngine::lock
sys::Mutex lock
lock - This lock protects the ExecutionEngine and MCJIT classes.
Definition: ExecutionEngine.h:160
llvm::GenericValue
Definition: GenericValue.h:23
llvm::MCJIT::runFunction
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
runFunction - Execute the specified function with the specified arguments, and return the result.
Definition: MCJIT.cpp:512
ObjectCache.h
llvm::object::Archive
Definition: Archive.h:158
llvm::GenericValue::IntVal
APInt IntVal
Definition: GenericValue.h:35
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:243
llvm::ExecutionEngine::getVerifyModules
bool getVerifyModules() const
Definition: ExecutionEngine.h:492
llvm::AArch64::RP
@ RP
Definition: AArch64ISelLowering.h:479
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::MCJIT::finalizeObject
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
Definition: MCJIT.cpp:257
int
Clang compiles this i1 i64 store i64 i64 store i64 i64 store i64 i64 store i64 align Which gets codegen d xmm0 movaps rbp movaps rbp movaps rbp movaps rbp rbp rbp rbp rbp It would be better to have movq s of instead of the movaps s LLVM produces ret int
Definition: README.txt:536
llvm::MCJIT::~MCJIT
~MCJIT() override
Definition: MCJIT.cpp:92
llvm::MCJIT::runStaticConstructorsDestructors
void runStaticConstructorsDestructors(bool isDtors) override
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
Definition: MCJIT.cpp:454
llvm::ExecutionEngine::LazyFunctionCreator
FunctionCreator LazyFunctionCreator
LazyFunctionCreator - If an unknown function is needed, this function pointer is invoked to create it...
Definition: ExecutionEngine.h:150
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::GVTOP
void * GVTOP(const GenericValue &GV)
Definition: GenericValue.h:50
llvm::Mangler::getNameWithPrefix
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:119
First
into llvm powi allowing the code generator to produce balanced multiplication trees First
Definition: README.txt:54
llvm::MCJIT::addModule
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
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:143
llvm::MCJIT::findModuleForSymbol
Module * findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:294
llvm::object::OwningBinary::takeBinary
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
Definition: Binary.h:223
GenericValue.h
llvm::MCJIT::notifyFreeingObject
void notifyFreeingObject(const object::ObjectFile &Obj)
Definition: MCJIT.cpp:666
llvm::Type::PointerTyID
@ PointerTyID
Pointers.
Definition: Type.h:73
llvm::MCJIT::removeModule
bool removeModule(Module *M) override
removeModule - Removes a Module from the list of modules, but does not free the module's memory.
Definition: MCJIT.cpp:113
llvm::LinkingSymbolResolver::findSymbol
JITSymbol findSymbol(const std::string &Name) override
This method returns the address of the specified function or variable.
Definition: MCJIT.cpp:675
LLVMLinkInMCJIT
void LLVMLinkInMCJIT()
Definition: MCJIT.cpp:40
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:244
llvm::RuntimeDyld::registerEHFrames
void registerEHFrames()
Register any EH frame sections that have been loaded but not previously registered with the memory ma...
Definition: RuntimeDyld.cpp:1439
llvm::MCJIT::Register
static void Register()
Definition: MCJIT.h:292
llvm::SmallString< 128 >
llvm::ExecutionEngine::runStaticConstructorsDestructors
virtual void runStaticConstructorsDestructors(bool isDtors)
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
Definition: ExecutionEngine.cpp:406
llvm::ExecutionEngine::getDataLayout
const DataLayout & getDataLayout() const
Definition: ExecutionEngine.h:196
llvm::MCJIT::getGlobalValueAddress
uint64_t getGlobalValueAddress(const std::string &Name) override
getGlobalValueAddress - Return the address of the specified global value.
Definition: MCJIT.cpp:395
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:190
llvm::MCJIT::finalizeModule
virtual void finalizeModule(Module *)
Definition: MCJIT.cpp:272
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
llvm::SmallPtrSetImpl< Module * >::iterator
SmallPtrSetIterator< Module * > iterator
Definition: SmallPtrSet.h:353
llvm::MCJIT::emitObject
std::unique_ptr< MemoryBuffer > emitObject(Module *M)
emitObject – Generate a JITed object in memory from the specified module Currently,...
Definition: MCJIT.cpp:145
uint64_t
llvm::find
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:1669
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:79
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::ExecutionEngine::ErrMsg
std::string ErrMsg
Definition: ExecutionEngine.h:155
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::FunctionType::getParamType
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:135
Archive.h
llvm::object::ObjectFile::createObjectFile
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:186
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1707
ObjectFile.h
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:840
llvm::Type::isVoidTy
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:138
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::MCJIT::notifyObjectLoaded
void notifyObjectLoaded(const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &L)
Definition: MCJIT.cpp:655
llvm::JITEventListener
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
Definition: JITEventListener.h:40
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
Mangler.h
llvm::Type::FP128TyID
@ FP128TyID
128-bit floating point type (112-bit significand)
Definition: Type.h:61
llvm::MCJIT::findExistingSymbol
JITSymbol findExistingSymbol(const std::string &Name)
Definition: MCJIT.cpp:285
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
DataLayout.h
llvm::Type::PPC_FP128TyID
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:62
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:227
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition: Error.h:569
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:747
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::GenericValue::DoubleVal
double DoubleVal
Definition: GenericValue.h:29
llvm::Type::IntegerTyID
@ IntegerTyID
Arbitrary bit width integers.
Definition: Type.h:71
llvm::logAllUnhandledErrors
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:61
llvm::GenericValue::FloatVal
float FloatVal
Definition: GenericValue.h:30
llvm::tgtok::IntVal
@ IntVal
Definition: TGLexer.h:64
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::MCJIT::createJIT
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
llvm::ExecutionEngine
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
Definition: ExecutionEngine.h:99
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
std
Definition: BitVector.h:838
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
llvm::RuntimeDyld::LoadedObjectInfo
Information about the loaded object.
Definition: RuntimeDyld.h:69
llvm::legacy::PassManager
PassManager manages ModulePassManagers.
Definition: LegacyPassManager.h:52
Function.h
llvm::BitWidth
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:147
llvm::MCJIT::FindGlobalVariableNamed
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:500
llvm::ExecutionEngine::getPointerToGlobalIfAvailable
void * getPointerToGlobalIfAvailable(StringRef S)
getPointerToGlobalIfAvailable - This returns the address of the specified global value if it is has a...
Definition: ExecutionEngine.cpp:285
llvm::RuntimeDyld::getErrorString
StringRef getErrorString()
Definition: RuntimeDyld.cpp:1416
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:599
llvm::MCJIT::UnregisterJITEventListener
void UnregisterJITEventListener(JITEventListener *L) override
Definition: MCJIT.cpp:644
llvm::PTOGV
GenericValue PTOGV(void *P)
Definition: GenericValue.h:49
SectionMemoryManager.h
llvm::JITEvaluatedSymbol::getAddress
JITTargetAddress getAddress() const
Return the address of this symbol.
Definition: JITSymbol.h:251
llvm::X86II::OB
@ OB
Definition: X86BaseInfo.h:801
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:661
llvm::RuntimeDyld::deregisterEHFrames
void deregisterEHFrames()
Definition: RuntimeDyld.cpp:1444
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:148
llvm::MCJIT::findSymbol
JITSymbol findSymbol(const std::string &Name, bool CheckFunctionsOnly)
Definition: MCJIT.cpp:338
llvm::RuntimeDyld::getSymbol
JITEvaluatedSymbol getSymbol(StringRef Name) const
Get the target address and flags for the named symbol.
Definition: RuntimeDyld.cpp:1391
llvm::object::Binary::getData
StringRef getData() const
Definition: Binary.cpp:40
llvm::legacy::PassManager::run
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
Definition: LegacyPassManager.cpp:1676
llvm::MCJIT
Definition: MCJIT.h:66
DerivedTypes.h
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::JITSymbol
Represents a symbol in the JIT.
Definition: JITSymbol.h:265
llvm::sys::DynamicLibrary::LoadLibraryPermanently
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
Definition: DynamicLibrary.h:85
llvm::MCJIT::addObjectFile
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
Definition: MCJIT.cpp:118
llvm::RuntimeDyld::hasError
bool hasError()
Definition: RuntimeDyld.cpp:1414
llvm::raw_string_ostream::str
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:649
llvm::JITEventListener::createGDBRegistrationListener
static JITEventListener * createGDBRegistrationListener()
Definition: GDBRegistrationListener.cpp:236
llvm::MCJIT::RegisterJITEventListener
void RegisterJITEventListener(JITEventListener *L) override
Registers a listener to be called back on various events within the JIT.
Definition: MCJIT.cpp:637
llvm::MCJIT::addArchive
void addArchive(object::OwningBinary< object::Archive > O) override
addArchive - Add an Archive to the execution engine.
Definition: MCJIT.cpp:136
llvm::FunctionType::getReturnType
Type * getReturnType() const
Definition: DerivedTypes.h:124
llvm::Type::X86_FP80TyID
@ X86_FP80TyID
80-bit floating point type (X87)
Definition: Type.h:60
JITEventListener.h
llvm::JITSymbolFlags::Exported
@ Exported
Definition: JITSymbol.h:85
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103
MCJIT.h