LLVM  12.0.0git
LLJIT.cpp
Go to the documentation of this file.
1 //===--------- LLJIT.cpp - An ORC-based JIT for compiling LLVM IR ---------===//
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 
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Mangler.h"
20 #include "llvm/IR/Module.h"
22 
23 #include <map>
24 
25 #define DEBUG_TYPE "orc"
26 
27 using namespace llvm;
28 using namespace llvm::orc;
29 
30 namespace {
31 
32 /// Adds helper function decls and wrapper functions that call the helper with
33 /// some additional prefix arguments.
34 ///
35 /// E.g. For wrapper "foo" with type i8(i8, i64), helper "bar", and prefix
36 /// args i32 4 and i16 12345, this function will add:
37 ///
38 /// declare i8 @bar(i32, i16, i8, i64)
39 ///
40 /// define i8 @foo(i8, i64) {
41 /// entry:
42 /// %2 = call i8 @bar(i32 4, i16 12345, i8 %0, i64 %1)
43 /// ret i8 %2
44 /// }
45 ///
46 Function *addHelperAndWrapper(Module &M, StringRef WrapperName,
47  FunctionType *WrapperFnType,
48  GlobalValue::VisibilityTypes WrapperVisibility,
49  StringRef HelperName,
50  ArrayRef<Value *> HelperPrefixArgs) {
51  std::vector<Type *> HelperArgTypes;
52  for (auto *Arg : HelperPrefixArgs)
53  HelperArgTypes.push_back(Arg->getType());
54  for (auto *T : WrapperFnType->params())
55  HelperArgTypes.push_back(T);
56  auto *HelperFnType =
57  FunctionType::get(WrapperFnType->getReturnType(), HelperArgTypes, false);
58  auto *HelperFn = Function::Create(HelperFnType, GlobalValue::ExternalLinkage,
59  HelperName, M);
60 
61  auto *WrapperFn = Function::Create(
62  WrapperFnType, GlobalValue::ExternalLinkage, WrapperName, M);
63  WrapperFn->setVisibility(WrapperVisibility);
64 
65  auto *EntryBlock = BasicBlock::Create(M.getContext(), "entry", WrapperFn);
66  IRBuilder<> IB(EntryBlock);
67 
68  std::vector<Value *> HelperArgs;
69  for (auto *Arg : HelperPrefixArgs)
70  HelperArgs.push_back(Arg);
71  for (auto &Arg : WrapperFn->args())
72  HelperArgs.push_back(&Arg);
73  auto *HelperResult = IB.CreateCall(HelperFn, HelperArgs);
74  if (HelperFn->getReturnType()->isVoidTy())
75  IB.CreateRetVoid();
76  else
77  IB.CreateRet(HelperResult);
78 
79  return WrapperFn;
80 }
81 
82 class GenericLLVMIRPlatformSupport;
83 
84 /// orc::Platform component of Generic LLVM IR Platform support.
85 /// Just forwards calls to the GenericLLVMIRPlatformSupport class below.
86 class GenericLLVMIRPlatform : public Platform {
87 public:
88  GenericLLVMIRPlatform(GenericLLVMIRPlatformSupport &S) : S(S) {}
89  Error setupJITDylib(JITDylib &JD) override;
90  Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU) override;
91  Error notifyRemoving(JITDylib &JD, VModuleKey K) override {
92  // Noop -- Nothing to do (yet).
93  return Error::success();
94  }
95 
96 private:
97  GenericLLVMIRPlatformSupport &S;
98 };
99 
100 /// This transform parses llvm.global_ctors to produce a single initialization
101 /// function for the module, records the function, then deletes
102 /// llvm.global_ctors.
103 class GlobalCtorDtorScraper {
104 public:
105 
106  GlobalCtorDtorScraper(GenericLLVMIRPlatformSupport &PS,
107  StringRef InitFunctionPrefix)
108  : PS(PS), InitFunctionPrefix(InitFunctionPrefix) {}
111 
112 private:
113  GenericLLVMIRPlatformSupport &PS;
114  StringRef InitFunctionPrefix;
115 };
116 
117 /// Generic IR Platform Support
118 ///
119 /// Scrapes llvm.global_ctors and llvm.global_dtors and replaces them with
120 /// specially named 'init' and 'deinit'. Injects definitions / interposes for
121 /// some runtime API, including __cxa_atexit, dlopen, and dlclose.
122 class GenericLLVMIRPlatformSupport : public LLJIT::PlatformSupport {
123 public:
124  // GenericLLVMIRPlatform &P) : P(P) {
125  GenericLLVMIRPlatformSupport(LLJIT &J)
126  : J(J), InitFunctionPrefix(J.mangle("__orc_init_func.")) {
127 
128  getExecutionSession().setPlatform(
129  std::make_unique<GenericLLVMIRPlatform>(*this));
130 
131  setInitTransform(J, GlobalCtorDtorScraper(*this, InitFunctionPrefix));
132 
133  SymbolMap StdInterposes;
134 
135  StdInterposes[J.mangleAndIntern("__lljit.platform_support_instance")] =
138  StdInterposes[J.mangleAndIntern("__lljit.cxa_atexit_helper")] =
139  JITEvaluatedSymbol(pointerToJITTargetAddress(registerAtExitHelper),
140  JITSymbolFlags());
141 
142  cantFail(
143  J.getMainJITDylib().define(absoluteSymbols(std::move(StdInterposes))));
144  cantFail(setupJITDylib(J.getMainJITDylib()));
145  cantFail(J.addIRModule(J.getMainJITDylib(), createPlatformRuntimeModule()));
146  }
147 
148  ExecutionSession &getExecutionSession() { return J.getExecutionSession(); }
149 
150  /// Adds a module that defines the __dso_handle global.
151  Error setupJITDylib(JITDylib &JD) {
152 
153  // Add per-jitdylib standard interposes.
154  SymbolMap PerJDInterposes;
155  PerJDInterposes[J.mangleAndIntern("__lljit.run_atexits_helper")] =
157  JITSymbolFlags());
158  cantFail(JD.define(absoluteSymbols(std::move(PerJDInterposes))));
159 
160  auto Ctx = std::make_unique<LLVMContext>();
161  auto M = std::make_unique<Module>("__standard_lib", *Ctx);
163 
164  auto *Int64Ty = Type::getInt64Ty(*Ctx);
165  auto *DSOHandle = new GlobalVariable(
166  *M, Int64Ty, true, GlobalValue::ExternalLinkage,
167  ConstantInt::get(Int64Ty, reinterpret_cast<uintptr_t>(&JD)),
168  "__dso_handle");
169  DSOHandle->setVisibility(GlobalValue::DefaultVisibility);
170  DSOHandle->setInitializer(
172 
173  auto *GenericIRPlatformSupportTy =
174  StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport");
175 
176  auto *PlatformInstanceDecl = new GlobalVariable(
177  *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage,
178  nullptr, "__lljit.platform_support_instance");
179 
180  auto *VoidTy = Type::getVoidTy(*Ctx);
181  addHelperAndWrapper(
182  *M, "__lljit_run_atexits", FunctionType::get(VoidTy, {}, false),
183  GlobalValue::HiddenVisibility, "__lljit.run_atexits_helper",
184  {PlatformInstanceDecl, DSOHandle});
185 
186  return J.addIRModule(JD, ThreadSafeModule(std::move(M), std::move(Ctx)));
187  }
188 
189  Error notifyAdding(JITDylib &JD, const MaterializationUnit &MU) {
190  if (auto &InitSym = MU.getInitializerSymbol())
191  InitSymbols[&JD].add(InitSym, SymbolLookupFlags::WeaklyReferencedSymbol);
192  else {
193  // If there's no identified init symbol attached, but there is a symbol
194  // with the GenericIRPlatform::InitFunctionPrefix, then treat that as
195  // an init function. Add the symbol to both the InitSymbols map (which
196  // will trigger a lookup to materialize the module) and the InitFunctions
197  // map (which holds the names of the symbols to execute).
198  for (auto &KV : MU.getSymbols())
199  if ((*KV.first).startswith(InitFunctionPrefix)) {
200  InitSymbols[&JD].add(KV.first,
202  InitFunctions[&JD].add(KV.first);
203  }
204  }
205  return Error::success();
206  }
207 
208  Error initialize(JITDylib &JD) override {
209  LLVM_DEBUG({
210  dbgs() << "GenericLLVMIRPlatformSupport getting initializers to run\n";
211  });
212  if (auto Initializers = getInitializers(JD)) {
213  LLVM_DEBUG(
214  { dbgs() << "GenericLLVMIRPlatformSupport running initializers\n"; });
215  for (auto InitFnAddr : *Initializers) {
216  LLVM_DEBUG({
217  dbgs() << " Running init " << formatv("{0:x16}", InitFnAddr)
218  << "...\n";
219  });
220  auto *InitFn = jitTargetAddressToFunction<void (*)()>(InitFnAddr);
221  InitFn();
222  }
223  } else
224  return Initializers.takeError();
225  return Error::success();
226  }
227 
228  Error deinitialize(JITDylib &JD) override {
229  LLVM_DEBUG({
230  dbgs() << "GenericLLVMIRPlatformSupport getting deinitializers to run\n";
231  });
232  if (auto Deinitializers = getDeinitializers(JD)) {
233  LLVM_DEBUG({
234  dbgs() << "GenericLLVMIRPlatformSupport running deinitializers\n";
235  });
236  for (auto DeinitFnAddr : *Deinitializers) {
237  LLVM_DEBUG({
238  dbgs() << " Running init " << formatv("{0:x16}", DeinitFnAddr)
239  << "...\n";
240  });
241  auto *DeinitFn = jitTargetAddressToFunction<void (*)()>(DeinitFnAddr);
242  DeinitFn();
243  }
244  } else
245  return Deinitializers.takeError();
246 
247  return Error::success();
248  }
249 
250  void registerInitFunc(JITDylib &JD, SymbolStringPtr InitName) {
251  getExecutionSession().runSessionLocked([&]() {
252  InitFunctions[&JD].add(InitName);
253  });
254  }
255 
256 private:
257 
258  Expected<std::vector<JITTargetAddress>> getInitializers(JITDylib &JD) {
259  if (auto Err = issueInitLookups(JD))
260  return std::move(Err);
261 
263  std::vector<JITDylib *> DFSLinkOrder;
264 
265  getExecutionSession().runSessionLocked([&]() {
266  DFSLinkOrder = getDFSLinkOrder(JD);
267 
268  for (auto *NextJD : DFSLinkOrder) {
269  auto IFItr = InitFunctions.find(NextJD);
270  if (IFItr != InitFunctions.end()) {
271  LookupSymbols[NextJD] = std::move(IFItr->second);
272  InitFunctions.erase(IFItr);
273  }
274  }
275  });
276 
277  LLVM_DEBUG({
278  dbgs() << "JITDylib init order is [ ";
279  for (auto *JD : llvm::reverse(DFSLinkOrder))
280  dbgs() << "\"" << JD->getName() << "\" ";
281  dbgs() << "]\n";
282  dbgs() << "Looking up init functions:\n";
283  for (auto &KV : LookupSymbols)
284  dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n";
285  });
286 
287  auto &ES = getExecutionSession();
288  auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols);
289 
290  if (!LookupResult)
291  return LookupResult.takeError();
292 
293  std::vector<JITTargetAddress> Initializers;
294  while (!DFSLinkOrder.empty()) {
295  auto &NextJD = *DFSLinkOrder.back();
296  DFSLinkOrder.pop_back();
297  auto InitsItr = LookupResult->find(&NextJD);
298  if (InitsItr == LookupResult->end())
299  continue;
300  for (auto &KV : InitsItr->second)
301  Initializers.push_back(KV.second.getAddress());
302  }
303 
304  return Initializers;
305  }
306 
307  Expected<std::vector<JITTargetAddress>> getDeinitializers(JITDylib &JD) {
308  auto &ES = getExecutionSession();
309 
310  auto LLJITRunAtExits = J.mangleAndIntern("__lljit_run_atexits");
311 
313  std::vector<JITDylib *> DFSLinkOrder;
314 
315  ES.runSessionLocked([&]() {
316  DFSLinkOrder = getDFSLinkOrder(JD);
317 
318  for (auto *NextJD : DFSLinkOrder) {
319  auto &JDLookupSymbols = LookupSymbols[NextJD];
320  auto DIFItr = DeInitFunctions.find(NextJD);
321  if (DIFItr != DeInitFunctions.end()) {
322  LookupSymbols[NextJD] = std::move(DIFItr->second);
323  DeInitFunctions.erase(DIFItr);
324  }
325  JDLookupSymbols.add(LLJITRunAtExits,
327  }
328  });
329 
330  LLVM_DEBUG({
331  dbgs() << "JITDylib deinit order is [ ";
332  for (auto *JD : DFSLinkOrder)
333  dbgs() << "\"" << JD->getName() << "\" ";
334  dbgs() << "]\n";
335  dbgs() << "Looking up deinit functions:\n";
336  for (auto &KV : LookupSymbols)
337  dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n";
338  });
339 
340  auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols);
341 
342  if (!LookupResult)
343  return LookupResult.takeError();
344 
345  std::vector<JITTargetAddress> DeInitializers;
346  for (auto *NextJD : DFSLinkOrder) {
347  auto DeInitsItr = LookupResult->find(NextJD);
348  assert(DeInitsItr != LookupResult->end() &&
349  "Every JD should have at least __lljit_run_atexits");
350 
351  auto RunAtExitsItr = DeInitsItr->second.find(LLJITRunAtExits);
352  if (RunAtExitsItr != DeInitsItr->second.end())
353  DeInitializers.push_back(RunAtExitsItr->second.getAddress());
354 
355  for (auto &KV : DeInitsItr->second)
356  if (KV.first != LLJITRunAtExits)
357  DeInitializers.push_back(KV.second.getAddress());
358  }
359 
360  return DeInitializers;
361  }
362 
363  // Returns a DFS traversal order of the JITDylibs reachable (via
364  // links-against edges) from JD, starting with JD itself.
365  static std::vector<JITDylib *> getDFSLinkOrder(JITDylib &JD) {
366  std::vector<JITDylib *> DFSLinkOrder;
367  std::vector<JITDylib *> WorkStack({&JD});
368  DenseSet<JITDylib *> Visited;
369 
370  while (!WorkStack.empty()) {
371  auto &NextJD = *WorkStack.back();
372  WorkStack.pop_back();
373  if (Visited.count(&NextJD))
374  continue;
375  Visited.insert(&NextJD);
376  DFSLinkOrder.push_back(&NextJD);
377  NextJD.withLinkOrderDo([&](const JITDylibSearchOrder &LinkOrder) {
378  for (auto &KV : LinkOrder)
379  WorkStack.push_back(KV.first);
380  });
381  }
382 
383  return DFSLinkOrder;
384  }
385 
386  /// Issue lookups for all init symbols required to initialize JD (and any
387  /// JITDylibs that it depends on).
388  Error issueInitLookups(JITDylib &JD) {
389  DenseMap<JITDylib *, SymbolLookupSet> RequiredInitSymbols;
390  std::vector<JITDylib *> DFSLinkOrder;
391 
392  getExecutionSession().runSessionLocked([&]() {
393  DFSLinkOrder = getDFSLinkOrder(JD);
394 
395  for (auto *NextJD : DFSLinkOrder) {
396  auto ISItr = InitSymbols.find(NextJD);
397  if (ISItr != InitSymbols.end()) {
398  RequiredInitSymbols[NextJD] = std::move(ISItr->second);
399  InitSymbols.erase(ISItr);
400  }
401  }
402  });
403 
404  return Platform::lookupInitSymbols(getExecutionSession(),
405  RequiredInitSymbols)
406  .takeError();
407  }
408 
409  static void registerAtExitHelper(void *Self, void (*F)(void *), void *Ctx,
410  void *DSOHandle) {
411  LLVM_DEBUG({
412  dbgs() << "Registering atexit function " << (void *)F << " for JD "
413  << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
414  });
415  static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.registerAtExit(
416  F, Ctx, DSOHandle);
417  }
418 
419  static void runAtExitsHelper(void *Self, void *DSOHandle) {
420  LLVM_DEBUG({
421  dbgs() << "Running atexit functions for JD "
422  << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
423  });
424  static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.runAtExits(
425  DSOHandle);
426  }
427 
428  // Constructs an LLVM IR module containing platform runtime globals,
429  // functions, and interposes.
430  ThreadSafeModule createPlatformRuntimeModule() {
431  auto Ctx = std::make_unique<LLVMContext>();
432  auto M = std::make_unique<Module>("__standard_lib", *Ctx);
434 
435  auto *GenericIRPlatformSupportTy =
436  StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport");
437 
438  auto *PlatformInstanceDecl = new GlobalVariable(
439  *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage,
440  nullptr, "__lljit.platform_support_instance");
441 
442  auto *Int8Ty = Type::getInt8Ty(*Ctx);
443  auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT);
444  auto *VoidTy = Type::getVoidTy(*Ctx);
445  auto *BytePtrTy = PointerType::getUnqual(Int8Ty);
446  auto *AtExitCallbackTy = FunctionType::get(VoidTy, {BytePtrTy}, false);
447  auto *AtExitCallbackPtrTy = PointerType::getUnqual(AtExitCallbackTy);
448 
449  addHelperAndWrapper(
450  *M, "__cxa_atexit",
451  FunctionType::get(IntTy, {AtExitCallbackPtrTy, BytePtrTy, BytePtrTy},
452  false),
453  GlobalValue::DefaultVisibility, "__lljit.cxa_atexit_helper",
454  {PlatformInstanceDecl});
455 
456  return ThreadSafeModule(std::move(M), std::move(Ctx));
457  }
458 
459  LLJIT &J;
460  std::string InitFunctionPrefix;
464  ItaniumCXAAtExitSupport AtExitMgr;
465 };
466 
467 Error GenericLLVMIRPlatform::setupJITDylib(JITDylib &JD) {
468  return S.setupJITDylib(JD);
469 }
470 
471 Error GenericLLVMIRPlatform::notifyAdding(JITDylib &JD,
472  const MaterializationUnit &MU) {
473  return S.notifyAdding(JD, MU);
474 }
475 
477 GlobalCtorDtorScraper::operator()(ThreadSafeModule TSM,
479  auto Err = TSM.withModuleDo([&](Module &M) -> Error {
480  auto &Ctx = M.getContext();
481  auto *GlobalCtors = M.getNamedGlobal("llvm.global_ctors");
482 
483  // If there's no llvm.global_ctors or it's just a decl then skip.
484  if (!GlobalCtors || GlobalCtors->isDeclaration())
485  return Error::success();
486 
487  std::string InitFunctionName;
488  raw_string_ostream(InitFunctionName)
489  << InitFunctionPrefix << M.getModuleIdentifier();
490 
491  MangleAndInterner Mangle(PS.getExecutionSession(), M.getDataLayout());
492  auto InternedName = Mangle(InitFunctionName);
493  if (auto Err =
494  R.defineMaterializing({{InternedName, JITSymbolFlags::Callable}}))
495  return Err;
496 
497  auto *InitFunc =
499  GlobalValue::ExternalLinkage, InitFunctionName, &M);
500  InitFunc->setVisibility(GlobalValue::HiddenVisibility);
501  std::vector<std::pair<Function *, unsigned>> Inits;
502  for (auto E : getConstructors(M))
503  Inits.push_back(std::make_pair(E.Func, E.Priority));
504  llvm::sort(Inits, [](const std::pair<Function *, unsigned> &LHS,
505  const std::pair<Function *, unsigned> &RHS) {
506  return LHS.first < RHS.first;
507  });
508  auto *EntryBlock = BasicBlock::Create(Ctx, "entry", InitFunc);
509  IRBuilder<> IB(EntryBlock);
510  for (auto &KV : Inits)
511  IB.CreateCall(KV.first);
512  IB.CreateRetVoid();
513 
514  PS.registerInitFunc(R.getTargetJITDylib(), InternedName);
515  GlobalCtors->eraseFromParent();
516  return Error::success();
517  });
518 
519  if (Err)
520  return std::move(Err);
521 
522  return std::move(TSM);
523 }
524 
525 class MachOPlatformSupport : public LLJIT::PlatformSupport {
526 public:
527  using DLOpenType = void *(*)(const char *Name, int Mode);
528  using DLCloseType = int (*)(void *Handle);
529  using DLSymType = void *(*)(void *Handle, const char *Name);
530  using DLErrorType = const char *(*)();
531 
532  struct DlFcnValues {
533  Optional<void *> RTLDDefault;
534  DLOpenType dlopen = nullptr;
535  DLCloseType dlclose = nullptr;
536  DLSymType dlsym = nullptr;
537  DLErrorType dlerror = nullptr;
538  };
539 
541  Create(LLJIT &J, JITDylib &PlatformJITDylib) {
542 
543  // Make process symbols visible.
544  {
545  std::string ErrMsg;
546  auto Lib = sys::DynamicLibrary::getPermanentLibrary(nullptr, &ErrMsg);
547  if (!Lib.isValid())
548  return make_error<StringError>(std::move(ErrMsg),
550  }
551 
552  DlFcnValues DlFcn;
553 
554  // Add support for RTLDDefault on known platforms.
555 #ifdef __APPLE__
556  DlFcn.RTLDDefault = reinterpret_cast<void *>(-2);
557 #endif // __APPLE__
558 
559  if (auto Err = hookUpFunction(DlFcn.dlopen, "dlopen"))
560  return std::move(Err);
561  if (auto Err = hookUpFunction(DlFcn.dlclose, "dlclose"))
562  return std::move(Err);
563  if (auto Err = hookUpFunction(DlFcn.dlsym, "dlsym"))
564  return std::move(Err);
565  if (auto Err = hookUpFunction(DlFcn.dlerror, "dlerror"))
566  return std::move(Err);
567 
568  std::unique_ptr<MachOPlatformSupport> MP(
569  new MachOPlatformSupport(J, PlatformJITDylib, DlFcn));
570  return std::move(MP);
571  }
572 
573  Error initialize(JITDylib &JD) override {
574  LLVM_DEBUG({
575  dbgs() << "MachOPlatformSupport initializing \"" << JD.getName()
576  << "\"\n";
577  });
578 
579  auto InitSeq = MP.getInitializerSequence(JD);
580  if (!InitSeq)
581  return InitSeq.takeError();
582 
583  // If ObjC is not enabled but there are JIT'd ObjC inits then return
584  // an error.
586  for (auto &KV : *InitSeq) {
587  if (!KV.second.getObjCSelRefsSections().empty() ||
588  !KV.second.getObjCClassListSections().empty())
589  return make_error<StringError>("JITDylib " + KV.first->getName() +
590  " contains objc metadata but objc"
591  " is not enabled",
593  }
594 
595  // Run the initializers.
596  for (auto &KV : *InitSeq) {
597  if (objCRegistrationEnabled()) {
598  KV.second.registerObjCSelectors();
599  if (auto Err = KV.second.registerObjCClasses()) {
600  // FIXME: Roll back registrations on error?
601  return Err;
602  }
603  }
604  KV.second.runModInits();
605  }
606 
607  return Error::success();
608  }
609 
610  Error deinitialize(JITDylib &JD) override {
611  auto &ES = J.getExecutionSession();
612  if (auto DeinitSeq = MP.getDeinitializerSequence(JD)) {
613  for (auto &KV : *DeinitSeq) {
614  auto DSOHandleName = ES.intern("___dso_handle");
615 
616  // FIXME: Run DeInits here.
617  auto Result = ES.lookup(
619  SymbolLookupSet(DSOHandleName,
621  if (!Result)
622  return Result.takeError();
623  if (Result->empty())
624  continue;
625  assert(Result->count(DSOHandleName) &&
626  "Result does not contain __dso_handle");
627  auto *DSOHandle = jitTargetAddressToPointer<void *>(
628  Result->begin()->second.getAddress());
629  AtExitMgr.runAtExits(DSOHandle);
630  }
631  } else
632  return DeinitSeq.takeError();
633  return Error::success();
634  }
635 
636 private:
637  template <typename FunctionPtrTy>
638  static Error hookUpFunction(FunctionPtrTy &Fn, const char *Name) {
639  if (auto *FnAddr = sys::DynamicLibrary::SearchForAddressOfSymbol(Name)) {
640  Fn = reinterpret_cast<FunctionPtrTy>(Fn);
641  return Error::success();
642  }
643 
644  return make_error<StringError>((Twine("Can not enable MachO JIT Platform: "
645  "missing function: ") +
646  Name)
647  .str(),
649  }
650 
651  MachOPlatformSupport(LLJIT &J, JITDylib &PlatformJITDylib, DlFcnValues DlFcn)
652  : J(J), MP(setupPlatform(J)), DlFcn(std::move(DlFcn)) {
653 
654  SymbolMap HelperSymbols;
655 
656  // platform and atexit helpers.
657  HelperSymbols[J.mangleAndIntern("__lljit.platform_support_instance")] =
659  HelperSymbols[J.mangleAndIntern("__lljit.cxa_atexit_helper")] =
660  JITEvaluatedSymbol(pointerToJITTargetAddress(registerAtExitHelper),
661  JITSymbolFlags());
662  HelperSymbols[J.mangleAndIntern("__lljit.run_atexits_helper")] =
664  JITSymbolFlags());
665 
666  // dlfcn helpers.
667  HelperSymbols[J.mangleAndIntern("__lljit.dlopen_helper")] =
669  JITSymbolFlags());
670  HelperSymbols[J.mangleAndIntern("__lljit.dlclose_helper")] =
672  JITSymbolFlags());
673  HelperSymbols[J.mangleAndIntern("__lljit.dlsym_helper")] =
675  JITSymbolFlags());
676  HelperSymbols[J.mangleAndIntern("__lljit.dlerror_helper")] =
678  JITSymbolFlags());
679 
680  cantFail(
681  PlatformJITDylib.define(absoluteSymbols(std::move(HelperSymbols))));
682  cantFail(MP.setupJITDylib(J.getMainJITDylib()));
683  cantFail(J.addIRModule(PlatformJITDylib, createPlatformRuntimeModule()));
684  }
685 
686  static MachOPlatform &setupPlatform(LLJIT &J) {
687  auto Tmp = std::make_unique<MachOPlatform>(
689  static_cast<ObjectLinkingLayer &>(J.getObjLinkingLayer()),
690  createStandardSymbolsObject(J));
691  auto &MP = *Tmp;
692  J.getExecutionSession().setPlatform(std::move(Tmp));
693  return MP;
694  }
695 
696  static std::unique_ptr<MemoryBuffer> createStandardSymbolsObject(LLJIT &J) {
697  LLVMContext Ctx;
698  Module M("__standard_symbols", Ctx);
700 
701  auto *Int64Ty = Type::getInt64Ty(Ctx);
702 
703  auto *DSOHandle =
704  new GlobalVariable(M, Int64Ty, true, GlobalValue::ExternalLinkage,
705  ConstantInt::get(Int64Ty, 0), "__dso_handle");
706  DSOHandle->setVisibility(GlobalValue::DefaultVisibility);
707 
708  return cantFail(J.getIRCompileLayer().getCompiler()(M));
709  }
710 
711  ThreadSafeModule createPlatformRuntimeModule() {
712  auto Ctx = std::make_unique<LLVMContext>();
713  auto M = std::make_unique<Module>("__standard_lib", *Ctx);
714  M->setDataLayout(J.getDataLayout());
715 
716  auto *MachOPlatformSupportTy =
717  StructType::create(*Ctx, "lljit.MachOPlatformSupport");
718 
719  auto *PlatformInstanceDecl = new GlobalVariable(
720  *M, MachOPlatformSupportTy, true, GlobalValue::ExternalLinkage, nullptr,
721  "__lljit.platform_support_instance");
722 
723  auto *Int8Ty = Type::getInt8Ty(*Ctx);
724  auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT);
725  auto *VoidTy = Type::getVoidTy(*Ctx);
726  auto *BytePtrTy = PointerType::getUnqual(Int8Ty);
727  auto *AtExitCallbackTy = FunctionType::get(VoidTy, {BytePtrTy}, false);
728  auto *AtExitCallbackPtrTy = PointerType::getUnqual(AtExitCallbackTy);
729 
730  addHelperAndWrapper(
731  *M, "__cxa_atexit",
732  FunctionType::get(IntTy, {AtExitCallbackPtrTy, BytePtrTy, BytePtrTy},
733  false),
734  GlobalValue::DefaultVisibility, "__lljit.cxa_atexit_helper",
735  {PlatformInstanceDecl});
736 
737  addHelperAndWrapper(*M, "dlopen",
738  FunctionType::get(BytePtrTy, {BytePtrTy, IntTy}, false),
739  GlobalValue::DefaultVisibility, "__lljit.dlopen_helper",
740  {PlatformInstanceDecl});
741 
742  addHelperAndWrapper(*M, "dlclose",
743  FunctionType::get(IntTy, {BytePtrTy}, false),
745  "__lljit.dlclose_helper", {PlatformInstanceDecl});
746 
747  addHelperAndWrapper(
748  *M, "dlsym",
749  FunctionType::get(BytePtrTy, {BytePtrTy, BytePtrTy}, false),
750  GlobalValue::DefaultVisibility, "__lljit.dlsym_helper",
751  {PlatformInstanceDecl});
752 
753  addHelperAndWrapper(*M, "dlerror", FunctionType::get(BytePtrTy, {}, false),
755  "__lljit.dlerror_helper", {PlatformInstanceDecl});
756 
757  return ThreadSafeModule(std::move(M), std::move(Ctx));
758  }
759 
760  static void registerAtExitHelper(void *Self, void (*F)(void *), void *Ctx,
761  void *DSOHandle) {
762  static_cast<MachOPlatformSupport *>(Self)->AtExitMgr.registerAtExit(
763  F, Ctx, DSOHandle);
764  }
765 
766  static void runAtExitsHelper(void *Self, void *DSOHandle) {
767  static_cast<MachOPlatformSupport *>(Self)->AtExitMgr.runAtExits(DSOHandle);
768  }
769 
770  void *jit_dlopen(const char *Path, int Mode) {
771  JITDylib *JDToOpen = nullptr;
772  // FIXME: Do the right thing with Mode flags.
773  {
774  std::lock_guard<std::mutex> Lock(PlatformSupportMutex);
775 
776  // Clear any existing error messages.
777  dlErrorMsgs.erase(std::this_thread::get_id());
778 
779  if (auto *JD = J.getExecutionSession().getJITDylibByName(Path)) {
780  auto I = JDRefCounts.find(JD);
781  if (I != JDRefCounts.end()) {
782  ++I->second;
783  return JD;
784  }
785 
786  JDRefCounts[JD] = 1;
787  JDToOpen = JD;
788  }
789  }
790 
791  if (JDToOpen) {
792  if (auto Err = initialize(*JDToOpen)) {
793  recordError(std::move(Err));
794  return 0;
795  }
796  }
797 
798  // Fall through to dlopen if no JITDylib found for Path.
799  return DlFcn.dlopen(Path, Mode);
800  }
801 
802  static void *dlopenHelper(void *Self, const char *Path, int Mode) {
803  return static_cast<MachOPlatformSupport *>(Self)->jit_dlopen(Path, Mode);
804  }
805 
806  int jit_dlclose(void *Handle) {
807  JITDylib *JDToClose = nullptr;
808 
809  {
810  std::lock_guard<std::mutex> Lock(PlatformSupportMutex);
811 
812  // Clear any existing error messages.
813  dlErrorMsgs.erase(std::this_thread::get_id());
814 
815  auto I = JDRefCounts.find(Handle);
816  if (I != JDRefCounts.end()) {
817  --I->second;
818  if (I->second == 0) {
819  JDRefCounts.erase(I);
820  JDToClose = static_cast<JITDylib *>(Handle);
821  } else
822  return 0;
823  }
824  }
825 
826  if (JDToClose) {
827  if (auto Err = deinitialize(*JDToClose)) {
828  recordError(std::move(Err));
829  return -1;
830  }
831  return 0;
832  }
833 
834  // Fall through to dlclose if no JITDylib found for Path.
835  return DlFcn.dlclose(Handle);
836  }
837 
838  static int dlcloseHelper(void *Self, void *Handle) {
839  return static_cast<MachOPlatformSupport *>(Self)->jit_dlclose(Handle);
840  }
841 
842  void *jit_dlsym(void *Handle, const char *Name) {
843  JITDylibSearchOrder JITSymSearchOrder;
844 
845  // FIXME: RTLD_NEXT, RTLD_SELF not supported.
846  {
847  std::lock_guard<std::mutex> Lock(PlatformSupportMutex);
848 
849  // Clear any existing error messages.
850  dlErrorMsgs.erase(std::this_thread::get_id());
851 
852  if (JDRefCounts.count(Handle)) {
853  JITSymSearchOrder.push_back(
854  {static_cast<JITDylib *>(Handle),
856  } else if (Handle == DlFcn.RTLDDefault) {
857  for (auto &KV : JDRefCounts)
858  JITSymSearchOrder.push_back(
859  {static_cast<JITDylib *>(KV.first),
861  }
862  }
863 
864  if (!JITSymSearchOrder.empty()) {
865  auto MangledName = J.mangleAndIntern(Name);
866  SymbolLookupSet Syms(MangledName,
868  if (auto Result = J.getExecutionSession().lookup(JITSymSearchOrder, Syms,
870  auto I = Result->find(MangledName);
871  if (I != Result->end())
872  return jitTargetAddressToPointer<void *>(I->second.getAddress());
873  } else {
874  recordError(Result.takeError());
875  return 0;
876  }
877  }
878 
879  // Fall through to dlsym.
880  return DlFcn.dlsym(Handle, Name);
881  }
882 
883  static void *dlsymHelper(void *Self, void *Handle, const char *Name) {
884  return static_cast<MachOPlatformSupport *>(Self)->jit_dlsym(Handle, Name);
885  }
886 
887  const char *jit_dlerror() {
888  {
889  std::lock_guard<std::mutex> Lock(PlatformSupportMutex);
890  auto I = dlErrorMsgs.find(std::this_thread::get_id());
891  if (I != dlErrorMsgs.end())
892  return I->second->c_str();
893  }
894  return DlFcn.dlerror();
895  }
896 
897  static const char *dlerrorHelper(void *Self) {
898  return static_cast<MachOPlatformSupport *>(Self)->jit_dlerror();
899  }
900 
901  void recordError(Error Err) {
902  std::lock_guard<std::mutex> Lock(PlatformSupportMutex);
903  dlErrorMsgs[std::this_thread::get_id()] =
904  std::make_unique<std::string>(toString(std::move(Err)));
905  }
906 
907  std::mutex PlatformSupportMutex;
908  LLJIT &J;
909  MachOPlatform &MP;
910  DlFcnValues DlFcn;
911  ItaniumCXAAtExitSupport AtExitMgr;
912  DenseMap<void *, unsigned> JDRefCounts;
913  std::map<std::thread::id, std::unique_ptr<std::string>> dlErrorMsgs;
914 };
915 
916 } // end anonymous namespace
917 
918 namespace llvm {
919 namespace orc {
920 
923  J.InitHelperTransformLayer->setTransform(std::move(T));
924 }
925 
927 
929 
930  LLVM_DEBUG(dbgs() << "Preparing to create LLIT instance...\n");
931 
932  if (!JTMB) {
933  LLVM_DEBUG({
934  dbgs() << " No explicitly set JITTargetMachineBuilder. "
935  "Detecting host...\n";
936  });
937  if (auto JTMBOrErr = JITTargetMachineBuilder::detectHost())
938  JTMB = std::move(*JTMBOrErr);
939  else
940  return JTMBOrErr.takeError();
941  }
942 
943  LLVM_DEBUG({
944  dbgs() << " JITTargetMachineBuilder is " << JTMB << "\n"
945  << " Pre-constructed ExecutionSession: " << (ES ? "Yes" : "No")
946  << "\n"
947  << " DataLayout: ";
948  if (DL)
949  dbgs() << DL->getStringRepresentation() << "\n";
950  else
951  dbgs() << "None (will be created by JITTargetMachineBuilder)\n";
952 
953  dbgs() << " Custom object-linking-layer creator: "
954  << (CreateObjectLinkingLayer ? "Yes" : "No") << "\n"
955  << " Custom compile-function creator: "
956  << (CreateCompileFunction ? "Yes" : "No") << "\n"
957  << " Custom platform-setup function: "
958  << (SetUpPlatform ? "Yes" : "No") << "\n"
959  << " Number of compile threads: " << NumCompileThreads;
960  if (!NumCompileThreads)
961  dbgs() << " (code will be compiled on the execution thread)\n";
962  else
963  dbgs() << "\n";
964  });
965 
966  // If the client didn't configure any linker options then auto-configure the
967  // JIT linker.
968  if (!CreateObjectLinkingLayer) {
969  auto &TT = JTMB->getTargetTriple();
970  if (TT.isOSBinFormatMachO() &&
971  (TT.getArch() == Triple::aarch64 || TT.getArch() == Triple::x86_64)) {
972 
973  JTMB->setRelocationModel(Reloc::PIC_);
974  JTMB->setCodeModel(CodeModel::Small);
975  CreateObjectLinkingLayer =
976  [](ExecutionSession &ES,
977  const Triple &) -> std::unique_ptr<ObjectLayer> {
978  auto ObjLinkingLayer = std::make_unique<ObjectLinkingLayer>(
979  ES, std::make_unique<jitlink::InProcessMemoryManager>());
980  ObjLinkingLayer->addPlugin(std::make_unique<EHFrameRegistrationPlugin>(
982  return std::move(ObjLinkingLayer);
983  };
984  }
985  }
986 
987  return Error::success();
988 }
989 
991  if (CompileThreads)
992  CompileThreads->wait();
993 }
994 
996  assert(TSM && "Can not add null module");
997 
998  if (auto Err =
999  TSM.withModuleDo([&](Module &M) { return applyDataLayout(M); }))
1000  return Err;
1001 
1002  return InitHelperTransformLayer->add(JD, std::move(TSM),
1003  ES->allocateVModule());
1004 }
1005 
1006 Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
1007  assert(Obj && "Can not add null object");
1008 
1009  return ObjTransformLayer.add(JD, std::move(Obj), ES->allocateVModule());
1010 }
1011 
1014  return ES->lookup(
1016 }
1017 
1018 std::unique_ptr<ObjectLayer>
1020 
1021  // If the config state provided an ObjectLinkingLayer factory then use it.
1023  return S.CreateObjectLinkingLayer(ES, S.JTMB->getTargetTriple());
1024 
1025  // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
1026  // a new SectionMemoryManager for each object.
1027  auto GetMemMgr = []() { return std::make_unique<SectionMemoryManager>(); };
1028  auto ObjLinkingLayer =
1029  std::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr));
1030 
1031  if (S.JTMB->getTargetTriple().isOSBinFormatCOFF()) {
1032  ObjLinkingLayer->setOverrideObjectFlagsWithResponsibilityFlags(true);
1033  ObjLinkingLayer->setAutoClaimResponsibilityForObjectSymbols(true);
1034  }
1035 
1036  // FIXME: Explicit conversion to std::unique_ptr<ObjectLayer> added to silence
1037  // errors from some GCC / libstdc++ bots. Remove this conversion (i.e.
1038  // just return ObjLinkingLayer) once those bots are upgraded.
1039  return std::unique_ptr<ObjectLayer>(std::move(ObjLinkingLayer));
1040 }
1041 
1044  JITTargetMachineBuilder JTMB) {
1045 
1046  /// If there is a custom compile function creator set then use it.
1047  if (S.CreateCompileFunction)
1048  return S.CreateCompileFunction(std::move(JTMB));
1049 
1050  // Otherwise default to creating a SimpleCompiler, or ConcurrentIRCompiler,
1051  // depending on the number of threads requested.
1052  if (S.NumCompileThreads > 0)
1053  return std::make_unique<ConcurrentIRCompiler>(std::move(JTMB));
1054 
1055  auto TM = JTMB.createTargetMachine();
1056  if (!TM)
1057  return TM.takeError();
1058 
1059  return std::make_unique<TMOwningSimpleCompiler>(std::move(*TM));
1060 }
1061 
1063  : ES(S.ES ? std::move(S.ES) : std::make_unique<ExecutionSession>()), Main(),
1064  DL(""), TT(S.JTMB->getTargetTriple()),
1065  ObjLinkingLayer(createObjectLinkingLayer(S, *ES)),
1066  ObjTransformLayer(*this->ES, *ObjLinkingLayer) {
1067 
1068  ErrorAsOutParameter _(&Err);
1069 
1070  if (auto MainOrErr = this->ES->createJITDylib("main"))
1071  Main = &*MainOrErr;
1072  else {
1073  Err = MainOrErr.takeError();
1074  return;
1075  }
1076 
1077  if (S.DL)
1078  DL = std::move(*S.DL);
1079  else if (auto DLOrErr = S.JTMB->getDefaultDataLayoutForTarget())
1080  DL = std::move(*DLOrErr);
1081  else {
1082  Err = DLOrErr.takeError();
1083  return;
1084  }
1085 
1086  {
1087  auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB));
1088  if (!CompileFunction) {
1089  Err = CompileFunction.takeError();
1090  return;
1091  }
1092  CompileLayer = std::make_unique<IRCompileLayer>(
1093  *ES, ObjTransformLayer, std::move(*CompileFunction));
1094  TransformLayer = std::make_unique<IRTransformLayer>(*ES, *CompileLayer);
1096  std::make_unique<IRTransformLayer>(*ES, *TransformLayer);
1097  }
1098 
1099  if (S.NumCompileThreads > 0) {
1100  InitHelperTransformLayer->setCloneToNewContextOnEmit(true);
1101  CompileThreads =
1102  std::make_unique<ThreadPool>(hardware_concurrency(S.NumCompileThreads));
1103  ES->setDispatchMaterialization(
1104  [this](std::unique_ptr<MaterializationUnit> MU,
1106  // FIXME: Switch to move capture once ThreadPool uses unique_function.
1107  auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU));
1108  auto SharedMR =
1109  std::make_shared<MaterializationResponsibility>(std::move(MR));
1110  auto Work = [SharedMU, SharedMR]() mutable {
1111  SharedMU->materialize(std::move(*SharedMR));
1112  };
1113  CompileThreads->async(std::move(Work));
1114  });
1115  }
1116 
1117  if (S.SetUpPlatform)
1118  Err = S.SetUpPlatform(*this);
1119  else
1121 }
1122 
1123 std::string LLJIT::mangle(StringRef UnmangledName) const {
1124  std::string MangledName;
1125  {
1126  raw_string_ostream MangledNameStream(MangledName);
1127  Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL);
1128  }
1129  return MangledName;
1130 }
1131 
1133  if (M.getDataLayout().isDefault())
1134  M.setDataLayout(DL);
1135 
1136  if (M.getDataLayout() != DL)
1137  return make_error<StringError>(
1138  "Added modules have incompatible data layouts: " +
1139  M.getDataLayout().getStringRepresentation() + " (module) vs " +
1140  DL.getStringRepresentation() + " (jit)",
1142 
1143  return Error::success();
1144 }
1145 
1147  LLVM_DEBUG(
1148  { dbgs() << "Setting up GenericLLVMIRPlatform support for LLJIT\n"; });
1149  J.setPlatformSupport(std::make_unique<GenericLLVMIRPlatformSupport>(J));
1150 }
1151 
1153  LLVM_DEBUG({ dbgs() << "Setting up MachOPlatform support for LLJIT\n"; });
1154  auto MP = MachOPlatformSupport::Create(J, J.getMainJITDylib());
1155  if (!MP)
1156  return MP.takeError();
1157  J.setPlatformSupport(std::move(*MP));
1158  return Error::success();
1159 }
1160 
1163  return Err;
1164  TT = JTMB->getTargetTriple();
1165  return Error::success();
1166 }
1167 
1169  assert(TSM && "Can not add null module");
1170 
1171  if (auto Err = TSM.withModuleDo(
1172  [&](Module &M) -> Error { return applyDataLayout(M); }))
1173  return Err;
1174 
1175  return CODLayer->add(JD, std::move(TSM), ES->allocateVModule());
1176 }
1177 
1178 LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
1179 
1180  // If LLJIT construction failed then bail out.
1181  if (Err)
1182  return;
1183 
1184  ErrorAsOutParameter _(&Err);
1185 
1186  /// Take/Create the lazy-compile callthrough manager.
1187  if (S.LCTMgr)
1188  LCTMgr = std::move(S.LCTMgr);
1189  else {
1190  if (auto LCTMgrOrErr = createLocalLazyCallThroughManager(
1191  S.TT, *ES, S.LazyCompileFailureAddr))
1192  LCTMgr = std::move(*LCTMgrOrErr);
1193  else {
1194  Err = LCTMgrOrErr.takeError();
1195  return;
1196  }
1197  }
1198 
1199  // Take/Create the indirect stubs manager builder.
1200  auto ISMBuilder = std::move(S.ISMBuilder);
1201 
1202  // If none was provided, try to build one.
1203  if (!ISMBuilder)
1205 
1206  // No luck. Bail out.
1207  if (!ISMBuilder) {
1208  Err = make_error<StringError>("Could not construct "
1209  "IndirectStubsManagerBuilder for target " +
1210  S.TT.str(),
1212  return;
1213  }
1214 
1215  // Create the COD layer.
1216  CODLayer = std::make_unique<CompileOnDemandLayer>(
1217  *ES, *InitHelperTransformLayer, *LCTMgr, std::move(ISMBuilder));
1218 
1219  if (S.NumCompileThreads > 0)
1220  CODLayer->setCloneToNewContextOnEmit(true);
1221 }
1222 
1223 } // End namespace orc.
1224 } // End namespace llvm.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:708
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this instance.
Definition: LLJIT.h:61
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:80
static void * SearchForAddressOfSymbol(const char *symbolName)
This function will search through all previously loaded dynamic libraries for the symbol symbolName...
Expected< JITEvaluatedSymbol > lookupLinkerMangled(JITDylib &JD, SymbolStringPtr Name)
Look up a symbol in JITDylib JD by the symbol&#39;s linker-mangled name (to look up symbols based on thei...
Definition: LLJIT.cpp:1012
friend void setUpGenericLLVMIRPlatform(LLJIT &J)
Configure the LLJIT instance to scrape modules for llvm.global_ctors and llvm.global_dtors variables ...
Definition: LLJIT.cpp:1146
SI Whole Quad Mode
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::function< std::unique_ptr< IndirectStubsManager >)> createLocalIndirectStubsManagerBuilder(const Triple &T)
Create a local indriect stubs manager builder.
std::function< Expected< ThreadSafeModule >(ThreadSafeModule, MaterializationResponsibility &R)> TransformFunction
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:67
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Implements a dense probed hash-table based set.
Definition: DenseSet.h:255
LLJIT(LLJITBuilderState &S, Error &Err)
Create an LLJIT instance with a single compile thread.
Definition: LLJIT.cpp:1062
const std::string & getStringRepresentation() const
Returns the string representation of the DataLayout.
Definition: DataLayout.h:241
IRCompileLayer & getIRCompileLayer()
Returns a reference to the IR compile layer.
Definition: LLJIT.h:187
static sys::Mutex Lock
const GlobalVariable * getNamedGlobal(StringRef Name) const
Return the global variable in the module with the specified name, of arbitrary type.
Definition: Module.h:414
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup...
Definition: Core.h:92
Externally visible function.
Definition: GlobalValue.h:48
const DataLayout & getDataLayout() const
Returns a reference to the DataLayout for this instance.
Definition: LLJIT.h:67
Error addIRModule(JITDylib &JD, ThreadSafeModule TSM)
Adds an IR module to the given JITDylib.
Definition: LLJIT.cpp:995
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:391
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
static void setInitTransform(LLJIT &J, IRTransformLayer::TransformFunction T)
Definition: LLJIT.cpp:921
F(f)
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:187
Mediates between MachO initialization and ExecutionSession state.
Definition: MachOPlatform.h:87
Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M)
Add a module to be lazily compiled to JITDylib JD.
Definition: LLJIT.cpp:1168
Optional< DataLayout > DL
Definition: LLJIT.h:270
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols, VModuleKey K=VModuleKey())
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
Definition: Core.h:642
Definition: BitVector.h:959
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:991
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1107
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:397
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:253
Error define(std::unique_ptr< MaterializationUnitType > &&MU)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition: Core.h:1311
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Mangling.h:26
static StringRef getName(Value *V)
std::unique_ptr< IRTransformLayer > TransformLayer
Definition: LLJIT.h:223
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2328
An ObjectLayer implementation built on JITLink.
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
void setPlatformSupport(std::unique_ptr< PlatformSupport > PS)
Set the PlatformSupport instance.
Definition: LLJIT.h:150
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:410
PlatformSetupFunction SetUpPlatform
Definition: LLJIT.h:273
IRCompiler & getCompiler()
Class to represent function types.
Definition: DerivedTypes.h:108
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:62
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
ObjectLayer & getObjLinkingLayer()
Returns a reference to the ObjLinkingLayer.
Definition: LLJIT.h:178
JITDylib & getTargetJITDylib() const
Returns the target JITDylib that these symbols are being materialized into.
Definition: Core.h:424
const std::string & str() const
Definition: Triple.h:369
ReturnInst * CreateRetVoid()
Create a &#39;ret void&#39; instruction.
Definition: IRBuilder.h:928
Pointer to a pooled string representing a symbol name.
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
An interface for Itanium __cxa_atexit interposer implementations.
static Expected< std::unique_ptr< IRCompileLayer::IRCompiler > > createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB)
Definition: LLJIT.cpp:1043
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:137
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
Error setUpMachOPlatform(LLJIT &J)
Configure the LLJIT instance to use MachOPlatform support.
Definition: LLJIT.cpp:1152
JITTargetAddress pointerToJITTargetAddress(T *Ptr)
Convert a pointer to a JITTargetAddress.
Definition: JITSymbol.h:69
static Expected< DenseMap< JITDylib *, SymbolMap > > lookupInitSymbols(ExecutionSession &ES, const DenseMap< JITDylib *, SymbolLookupSet > &InitSyms)
A utility function for looking up initializer symbols.
Definition: Core.cpp:1726
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::unique_ptr< IRTransformLayer > InitHelperTransformLayer
Definition: LLJIT.h:224
Initializer support for LLJIT.
Definition: LLJIT.h:43
ArrayRef< Type * > params() const
Definition: DerivedTypes.h:135
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:170
Flags for symbols in the JIT.
Definition: JITSymbol.h:74
std::unique_ptr< IRCompileLayer > CompileLayer
Definition: LLJIT.h:222
void setPlatform(std::unique_ptr< Platform > P)
Set the Platform for this ExecutionSession.
Definition: Core.h:1113
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
CompileFunctionCreator CreateCompileFunction
Definition: LLJIT.h:272
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:311
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
An LLVM Module together with a shared ThreadSafeContext.
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib *> JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition: Core.h:96
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1433
decltype(auto) withModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function on the contained Module...
const SymbolStringPtr & getInitializerSymbol() const
Returns the initialization symbol for this MaterializationUnit (if any).
Definition: Core.h:575
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:219
SymbolStringPtr mangleAndIntern(StringRef UnmangledName) const
Returns an interned, linker-mangled version of UnmangledName.
Definition: LLJIT.h:193
Triple TT
Definition: LLJIT.h:217
static DynamicLibrary getPermanentLibrary(const char *filename, std::string *errMsg=nullptr)
This function permanently loads the dynamic library at the given path.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
ObjectLinkingLayerCreator CreateObjectLinkingLayer
Definition: LLJIT.h:271
std::unique_ptr< ThreadPool > CompileThreads
Definition: LLJIT.h:218
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:211
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group...
Definition: Core.h:552
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
Definition: DerivedTypes.h:677
Expected< std::unique_ptr< LazyCallThroughManager > > createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr)
Create a LocalLazyCallThroughManager from the given triple and execution session. ...
Module.h This file contains the declarations for the Module class.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:113
Type * getReturnType() const
Definition: DerivedTypes.h:129
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition: Type.cpp:190
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:786
bool isDefault() const
Test if the DataLayout was constructed from an empty string.
Definition: DataLayout.h:246
A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
Definition: LLJIT.h:36
An ExecutionSession represents a running JIT program.
Definition: Core.h:1088
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void lookup(LookupKind K, const JITDylibSearchOrder &SearchOrder, SymbolLookupSet Symbols, SymbolState RequiredState, SymbolsResolvedCallback NotifyComplete, RegisterDependenciesFunction RegisterDependencies)
Search the given JITDylib list for the given symbols.
Definition: Core.cpp:1887
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:229
Helper for Errors used as out-parameters.
Definition: Error.h:1060
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
const SymbolFlagsMap & getSymbols() const
Return the set of symbols that this source provides.
Definition: Core.h:572
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
Error addObjectFile(JITDylib &JD, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:1006
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware ressources are to be used...
Definition: Threading.h:217
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
#define I(x, y, z)
Definition: MD5.cpp:59
Error defineMaterializing(SymbolFlagsMap SymbolFlags)
Attempt to claim responsibility for new definitions.
Definition: Core.cpp:226
bool objCRegistrationEnabled()
const std::string & getName() const
Get the name for this JITDylib.
Definition: Core.h:811
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:1132
JITDylib * getJITDylibByName(StringRef Name)
Return a pointer to the "name" JITDylib.
Definition: Core.cpp:1779
~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:990
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::string mangle(StringRef UnmangledName) const
Returns a linker-mangled version of UnmangledName.
Definition: LLJIT.cpp:1123
std::unique_ptr< LazyCallThroughManager > LCTMgr
Definition: LLJIT.h:387
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:521
Error prepareForConstruction()
Called prior to JIT class construcion to fix up defaults.
Definition: LLJIT.cpp:928
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:451
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
JITTargetAddress LazyCompileFailureAddr
Definition: LLJIT.h:386
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
DataLayout DL
Definition: LLJIT.h:216
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
JITDylib & getMainJITDylib()
Returns a reference to the JITDylib representing the JIT&#39;d main program.
Definition: LLJIT.h:70
static std::unique_ptr< ObjectLayer > createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES)
Definition: LLJIT.cpp:1019
auto reverse(ContainerTy &&C, std::enable_if_t< has_rbegin< ContainerTy >::value > *=nullptr)
Definition: STLExtras.h:341
Optional< JITTargetMachineBuilder > JTMB
Definition: LLJIT.h:269
#define LLVM_DEBUG(X)
Definition: Debug.h:122
JITDylib * Main
Definition: LLJIT.h:214
IndirectStubsManagerBuilderFunction ISMBuilder
Definition: LLJIT.h:388
ObjectTransformLayer ObjTransformLayer
Definition: LLJIT.h:221
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:184
#define _
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:779
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
A utility class for building TargetMachines for JITs.
Platforms set up standard symbols and mediate interactions between dynamic initializers (e...
Definition: Core.h:1062