LLVM 17.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
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/Mangler.h"
25#include "llvm/IR/Module.h"
27
28#include <map>
29
30#define DEBUG_TYPE "orc"
31
32using namespace llvm;
33using namespace llvm::orc;
34
35namespace {
36
37/// Adds helper function decls and wrapper functions that call the helper with
38/// some additional prefix arguments.
39///
40/// E.g. For wrapper "foo" with type i8(i8, i64), helper "bar", and prefix
41/// args i32 4 and i16 12345, this function will add:
42///
43/// declare i8 @bar(i32, i16, i8, i64)
44///
45/// define i8 @foo(i8, i64) {
46/// entry:
47/// %2 = call i8 @bar(i32 4, i16 12345, i8 %0, i64 %1)
48/// ret i8 %2
49/// }
50///
51Function *addHelperAndWrapper(Module &M, StringRef WrapperName,
52 FunctionType *WrapperFnType,
53 GlobalValue::VisibilityTypes WrapperVisibility,
54 StringRef HelperName,
55 ArrayRef<Value *> HelperPrefixArgs) {
56 std::vector<Type *> HelperArgTypes;
57 for (auto *Arg : HelperPrefixArgs)
58 HelperArgTypes.push_back(Arg->getType());
59 for (auto *T : WrapperFnType->params())
60 HelperArgTypes.push_back(T);
61 auto *HelperFnType =
62 FunctionType::get(WrapperFnType->getReturnType(), HelperArgTypes, false);
63 auto *HelperFn = Function::Create(HelperFnType, GlobalValue::ExternalLinkage,
64 HelperName, M);
65
66 auto *WrapperFn = Function::Create(
67 WrapperFnType, GlobalValue::ExternalLinkage, WrapperName, M);
68 WrapperFn->setVisibility(WrapperVisibility);
69
70 auto *EntryBlock = BasicBlock::Create(M.getContext(), "entry", WrapperFn);
71 IRBuilder<> IB(EntryBlock);
72
73 std::vector<Value *> HelperArgs;
74 for (auto *Arg : HelperPrefixArgs)
75 HelperArgs.push_back(Arg);
76 for (auto &Arg : WrapperFn->args())
77 HelperArgs.push_back(&Arg);
78 auto *HelperResult = IB.CreateCall(HelperFn, HelperArgs);
79 if (HelperFn->getReturnType()->isVoidTy())
80 IB.CreateRetVoid();
81 else
82 IB.CreateRet(HelperResult);
83
84 return WrapperFn;
85}
86
87class ORCPlatformSupport : public LLJIT::PlatformSupport {
88public:
89 ORCPlatformSupport(orc::LLJIT &J) : J(J) {}
90
91 Error initialize(orc::JITDylib &JD) override {
94 using SPSDLOpenSig = SPSExecutorAddr(SPSString, int32_t);
95 enum dlopen_mode : int32_t {
96 ORC_RT_RTLD_LAZY = 0x1,
97 ORC_RT_RTLD_NOW = 0x2,
98 ORC_RT_RTLD_LOCAL = 0x4,
99 ORC_RT_RTLD_GLOBAL = 0x8
100 };
101
102 if (auto WrapperAddr = J.lookup("__orc_rt_jit_dlopen_wrapper")) {
103 return J.getExecutionSession().callSPSWrapper<SPSDLOpenSig>(
104 *WrapperAddr, DSOHandles[&JD], JD.getName(),
105 int32_t(ORC_RT_RTLD_LAZY));
106 } else
107 return WrapperAddr.takeError();
108 }
109
110 Error deinitialize(orc::JITDylib &JD) override {
112 using SPSDLCloseSig = int32_t(SPSExecutorAddr);
113
114 if (auto WrapperAddr = J.lookup("__orc_rt_jit_dlclose_wrapper")) {
115 int32_t result;
116 auto E = J.getExecutionSession().callSPSWrapper<SPSDLCloseSig>(
117 *WrapperAddr, result, DSOHandles[&JD]);
118 if (E)
119 return E;
120 else if (result)
121 return make_error<StringError>("dlclose failed",
123 DSOHandles.erase(&JD);
124 } else
125 return WrapperAddr.takeError();
126 return Error::success();
127 }
128
129private:
130 orc::LLJIT &J;
132};
133
134class GenericLLVMIRPlatformSupport;
135
136/// orc::Platform component of Generic LLVM IR Platform support.
137/// Just forwards calls to the GenericLLVMIRPlatformSupport class below.
138class GenericLLVMIRPlatform : public Platform {
139public:
140 GenericLLVMIRPlatform(GenericLLVMIRPlatformSupport &S) : S(S) {}
141 Error setupJITDylib(JITDylib &JD) override;
142 Error teardownJITDylib(JITDylib &JD) override;
144 const MaterializationUnit &MU) override;
145 Error notifyRemoving(ResourceTracker &RT) override {
146 // Noop -- Nothing to do (yet).
147 return Error::success();
148 }
149
150private:
151 GenericLLVMIRPlatformSupport &S;
152};
153
154/// This transform parses llvm.global_ctors to produce a single initialization
155/// function for the module, records the function, then deletes
156/// llvm.global_ctors.
157class GlobalCtorDtorScraper {
158public:
159 GlobalCtorDtorScraper(GenericLLVMIRPlatformSupport &PS,
160 StringRef InitFunctionPrefix,
161 StringRef DeInitFunctionPrefix)
162 : PS(PS), InitFunctionPrefix(InitFunctionPrefix),
163 DeInitFunctionPrefix(DeInitFunctionPrefix) {}
166
167private:
168 GenericLLVMIRPlatformSupport &PS;
169 StringRef InitFunctionPrefix;
170 StringRef DeInitFunctionPrefix;
171};
172
173/// Generic IR Platform Support
174///
175/// Scrapes llvm.global_ctors and llvm.global_dtors and replaces them with
176/// specially named 'init' and 'deinit'. Injects definitions / interposes for
177/// some runtime API, including __cxa_atexit, dlopen, and dlclose.
178class GenericLLVMIRPlatformSupport : public LLJIT::PlatformSupport {
179public:
180 GenericLLVMIRPlatformSupport(LLJIT &J)
181 : J(J), InitFunctionPrefix(J.mangle("__orc_init_func.")),
182 DeInitFunctionPrefix(J.mangle("__orc_deinit_func.")) {
183
185 std::make_unique<GenericLLVMIRPlatform>(*this));
186
187 setInitTransform(J, GlobalCtorDtorScraper(*this, InitFunctionPrefix,
188 DeInitFunctionPrefix));
189
190 SymbolMap StdInterposes;
191
192 StdInterposes[J.mangleAndIntern("__lljit.platform_support_instance")] = {
194 StdInterposes[J.mangleAndIntern("__lljit.cxa_atexit_helper")] = {
195 ExecutorAddr::fromPtr(registerCxaAtExitHelper), JITSymbolFlags()};
196
197 cantFail(
198 J.getMainJITDylib().define(absoluteSymbols(std::move(StdInterposes))));
199 cantFail(setupJITDylib(J.getMainJITDylib()));
200 cantFail(J.addIRModule(J.getMainJITDylib(), createPlatformRuntimeModule()));
201 }
202
204
205 /// Adds a module that defines the __dso_handle global.
206 Error setupJITDylib(JITDylib &JD) {
207
208 // Add per-jitdylib standard interposes.
209 SymbolMap PerJDInterposes;
210 PerJDInterposes[J.mangleAndIntern("__lljit.run_atexits_helper")] = {
211 ExecutorAddr::fromPtr(runAtExitsHelper), JITSymbolFlags()};
212 PerJDInterposes[J.mangleAndIntern("__lljit.atexit_helper")] = {
213 ExecutorAddr::fromPtr(registerAtExitHelper), JITSymbolFlags()};
214 cantFail(JD.define(absoluteSymbols(std::move(PerJDInterposes))));
215
216 auto Ctx = std::make_unique<LLVMContext>();
217 auto M = std::make_unique<Module>("__standard_lib", *Ctx);
218 M->setDataLayout(J.getDataLayout());
219
220 auto *Int64Ty = Type::getInt64Ty(*Ctx);
221 auto *DSOHandle = new GlobalVariable(
222 *M, Int64Ty, true, GlobalValue::ExternalLinkage,
223 ConstantInt::get(Int64Ty, reinterpret_cast<uintptr_t>(&JD)),
224 "__dso_handle");
225 DSOHandle->setVisibility(GlobalValue::DefaultVisibility);
226 DSOHandle->setInitializer(
227 ConstantInt::get(Int64Ty, ExecutorAddr::fromPtr(&JD).getValue()));
228
229 auto *GenericIRPlatformSupportTy =
230 StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport");
231
232 auto *PlatformInstanceDecl = new GlobalVariable(
233 *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage,
234 nullptr, "__lljit.platform_support_instance");
235
236 auto *VoidTy = Type::getVoidTy(*Ctx);
237 addHelperAndWrapper(
238 *M, "__lljit_run_atexits", FunctionType::get(VoidTy, {}, false),
239 GlobalValue::HiddenVisibility, "__lljit.run_atexits_helper",
240 {PlatformInstanceDecl, DSOHandle});
241
242 auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT);
243 auto *AtExitCallbackTy = FunctionType::get(VoidTy, {}, false);
244 auto *AtExitCallbackPtrTy = PointerType::getUnqual(AtExitCallbackTy);
245 addHelperAndWrapper(*M, "atexit",
246 FunctionType::get(IntTy, {AtExitCallbackPtrTy}, false),
247 GlobalValue::HiddenVisibility, "__lljit.atexit_helper",
248 {PlatformInstanceDecl, DSOHandle});
249
250 return J.addIRModule(JD, ThreadSafeModule(std::move(M), std::move(Ctx)));
251 }
252
253 Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU) {
254 auto &JD = RT.getJITDylib();
255 if (auto &InitSym = MU.getInitializerSymbol())
256 InitSymbols[&JD].add(InitSym, SymbolLookupFlags::WeaklyReferencedSymbol);
257 else {
258 // If there's no identified init symbol attached, but there is a symbol
259 // with the GenericIRPlatform::InitFunctionPrefix, then treat that as
260 // an init function. Add the symbol to both the InitSymbols map (which
261 // will trigger a lookup to materialize the module) and the InitFunctions
262 // map (which holds the names of the symbols to execute).
263 for (auto &KV : MU.getSymbols())
264 if ((*KV.first).startswith(InitFunctionPrefix)) {
265 InitSymbols[&JD].add(KV.first,
266 SymbolLookupFlags::WeaklyReferencedSymbol);
267 InitFunctions[&JD].add(KV.first);
268 } else if ((*KV.first).startswith(DeInitFunctionPrefix)) {
269 DeInitFunctions[&JD].add(KV.first);
270 }
271 }
272 return Error::success();
273 }
274
275 Error initialize(JITDylib &JD) override {
276 LLVM_DEBUG({
277 dbgs() << "GenericLLVMIRPlatformSupport getting initializers to run\n";
278 });
279 if (auto Initializers = getInitializers(JD)) {
281 { dbgs() << "GenericLLVMIRPlatformSupport running initializers\n"; });
282 for (auto InitFnAddr : *Initializers) {
283 LLVM_DEBUG({
284 dbgs() << " Running init " << formatv("{0:x16}", InitFnAddr)
285 << "...\n";
286 });
287 auto *InitFn = InitFnAddr.toPtr<void (*)()>();
288 InitFn();
289 }
290 } else
291 return Initializers.takeError();
292 return Error::success();
293 }
294
295 Error deinitialize(JITDylib &JD) override {
296 LLVM_DEBUG({
297 dbgs() << "GenericLLVMIRPlatformSupport getting deinitializers to run\n";
298 });
299 if (auto Deinitializers = getDeinitializers(JD)) {
300 LLVM_DEBUG({
301 dbgs() << "GenericLLVMIRPlatformSupport running deinitializers\n";
302 });
303 for (auto DeinitFnAddr : *Deinitializers) {
304 LLVM_DEBUG({
305 dbgs() << " Running deinit " << formatv("{0:x16}", DeinitFnAddr)
306 << "...\n";
307 });
308 auto *DeinitFn = DeinitFnAddr.toPtr<void (*)()>();
309 DeinitFn();
310 }
311 } else
312 return Deinitializers.takeError();
313
314 return Error::success();
315 }
316
317 void registerInitFunc(JITDylib &JD, SymbolStringPtr InitName) {
319 InitFunctions[&JD].add(InitName);
320 });
321 }
322
323 void registerDeInitFunc(JITDylib &JD, SymbolStringPtr DeInitName) {
325 [&]() { DeInitFunctions[&JD].add(DeInitName); });
326 }
327
328private:
329 Expected<std::vector<ExecutorAddr>> getInitializers(JITDylib &JD) {
330 if (auto Err = issueInitLookups(JD))
331 return std::move(Err);
332
334 std::vector<JITDylibSP> DFSLinkOrder;
335
336 if (auto Err = getExecutionSession().runSessionLocked([&]() -> Error {
337 if (auto DFSLinkOrderOrErr = JD.getDFSLinkOrder())
338 DFSLinkOrder = std::move(*DFSLinkOrderOrErr);
339 else
340 return DFSLinkOrderOrErr.takeError();
341
342 for (auto &NextJD : DFSLinkOrder) {
343 auto IFItr = InitFunctions.find(NextJD.get());
344 if (IFItr != InitFunctions.end()) {
345 LookupSymbols[NextJD.get()] = std::move(IFItr->second);
346 InitFunctions.erase(IFItr);
347 }
348 }
349 return Error::success();
350 }))
351 return std::move(Err);
352
353 LLVM_DEBUG({
354 dbgs() << "JITDylib init order is [ ";
355 for (auto &JD : llvm::reverse(DFSLinkOrder))
356 dbgs() << "\"" << JD->getName() << "\" ";
357 dbgs() << "]\n";
358 dbgs() << "Looking up init functions:\n";
359 for (auto &KV : LookupSymbols)
360 dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n";
361 });
362
363 auto &ES = getExecutionSession();
364 auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols);
365
366 if (!LookupResult)
367 return LookupResult.takeError();
368
369 std::vector<ExecutorAddr> Initializers;
370 while (!DFSLinkOrder.empty()) {
371 auto &NextJD = *DFSLinkOrder.back();
372 DFSLinkOrder.pop_back();
373 auto InitsItr = LookupResult->find(&NextJD);
374 if (InitsItr == LookupResult->end())
375 continue;
376 for (auto &KV : InitsItr->second)
377 Initializers.push_back(KV.second.getAddress());
378 }
379
380 return Initializers;
381 }
382
383 Expected<std::vector<ExecutorAddr>> getDeinitializers(JITDylib &JD) {
384 auto &ES = getExecutionSession();
385
386 auto LLJITRunAtExits = J.mangleAndIntern("__lljit_run_atexits");
387
389 std::vector<JITDylibSP> DFSLinkOrder;
390
391 if (auto Err = ES.runSessionLocked([&]() -> Error {
392 if (auto DFSLinkOrderOrErr = JD.getDFSLinkOrder())
393 DFSLinkOrder = std::move(*DFSLinkOrderOrErr);
394 else
395 return DFSLinkOrderOrErr.takeError();
396
397 for (auto &NextJD : DFSLinkOrder) {
398 auto &JDLookupSymbols = LookupSymbols[NextJD.get()];
399 auto DIFItr = DeInitFunctions.find(NextJD.get());
400 if (DIFItr != DeInitFunctions.end()) {
401 LookupSymbols[NextJD.get()] = std::move(DIFItr->second);
402 DeInitFunctions.erase(DIFItr);
403 }
404 JDLookupSymbols.add(LLJITRunAtExits,
405 SymbolLookupFlags::WeaklyReferencedSymbol);
406 }
407 return Error::success();
408 }))
409 return std::move(Err);
410
411 LLVM_DEBUG({
412 dbgs() << "JITDylib deinit order is [ ";
413 for (auto &JD : DFSLinkOrder)
414 dbgs() << "\"" << JD->getName() << "\" ";
415 dbgs() << "]\n";
416 dbgs() << "Looking up deinit functions:\n";
417 for (auto &KV : LookupSymbols)
418 dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n";
419 });
420
421 auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols);
422
423 if (!LookupResult)
424 return LookupResult.takeError();
425
426 std::vector<ExecutorAddr> DeInitializers;
427 for (auto &NextJD : DFSLinkOrder) {
428 auto DeInitsItr = LookupResult->find(NextJD.get());
429 assert(DeInitsItr != LookupResult->end() &&
430 "Every JD should have at least __lljit_run_atexits");
431
432 auto RunAtExitsItr = DeInitsItr->second.find(LLJITRunAtExits);
433 if (RunAtExitsItr != DeInitsItr->second.end())
434 DeInitializers.push_back(RunAtExitsItr->second.getAddress());
435
436 for (auto &KV : DeInitsItr->second)
437 if (KV.first != LLJITRunAtExits)
438 DeInitializers.push_back(KV.second.getAddress());
439 }
440
441 return DeInitializers;
442 }
443
444 /// Issue lookups for all init symbols required to initialize JD (and any
445 /// JITDylibs that it depends on).
446 Error issueInitLookups(JITDylib &JD) {
447 DenseMap<JITDylib *, SymbolLookupSet> RequiredInitSymbols;
448 std::vector<JITDylibSP> DFSLinkOrder;
449
450 if (auto Err = getExecutionSession().runSessionLocked([&]() -> Error {
451 if (auto DFSLinkOrderOrErr = JD.getDFSLinkOrder())
452 DFSLinkOrder = std::move(*DFSLinkOrderOrErr);
453 else
454 return DFSLinkOrderOrErr.takeError();
455
456 for (auto &NextJD : DFSLinkOrder) {
457 auto ISItr = InitSymbols.find(NextJD.get());
458 if (ISItr != InitSymbols.end()) {
459 RequiredInitSymbols[NextJD.get()] = std::move(ISItr->second);
460 InitSymbols.erase(ISItr);
461 }
462 }
463 return Error::success();
464 }))
465 return Err;
466
467 return Platform::lookupInitSymbols(getExecutionSession(),
468 RequiredInitSymbols)
469 .takeError();
470 }
471
472 static void registerCxaAtExitHelper(void *Self, void (*F)(void *), void *Ctx,
473 void *DSOHandle) {
474 LLVM_DEBUG({
475 dbgs() << "Registering cxa atexit function " << (void *)F << " for JD "
476 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
477 });
478 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.registerAtExit(
479 F, Ctx, DSOHandle);
480 }
481
482 static void registerAtExitHelper(void *Self, void *DSOHandle, void (*F)()) {
483 LLVM_DEBUG({
484 dbgs() << "Registering atexit function " << (void *)F << " for JD "
485 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
486 });
487 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.registerAtExit(
488 reinterpret_cast<void (*)(void *)>(F), nullptr, DSOHandle);
489 }
490
491 static void runAtExitsHelper(void *Self, void *DSOHandle) {
492 LLVM_DEBUG({
493 dbgs() << "Running atexit functions for JD "
494 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
495 });
496 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.runAtExits(
497 DSOHandle);
498 }
499
500 // Constructs an LLVM IR module containing platform runtime globals,
501 // functions, and interposes.
502 ThreadSafeModule createPlatformRuntimeModule() {
503 auto Ctx = std::make_unique<LLVMContext>();
504 auto M = std::make_unique<Module>("__standard_lib", *Ctx);
505 M->setDataLayout(J.getDataLayout());
506
507 auto *GenericIRPlatformSupportTy =
508 StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport");
509
510 auto *PlatformInstanceDecl = new GlobalVariable(
511 *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage,
512 nullptr, "__lljit.platform_support_instance");
513
514 auto *Int8Ty = Type::getInt8Ty(*Ctx);
515 auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT);
516 auto *VoidTy = Type::getVoidTy(*Ctx);
517 auto *BytePtrTy = PointerType::getUnqual(Int8Ty);
518 auto *CxaAtExitCallbackTy = FunctionType::get(VoidTy, {BytePtrTy}, false);
519 auto *CxaAtExitCallbackPtrTy = PointerType::getUnqual(CxaAtExitCallbackTy);
520
521 addHelperAndWrapper(
522 *M, "__cxa_atexit",
523 FunctionType::get(IntTy, {CxaAtExitCallbackPtrTy, BytePtrTy, BytePtrTy},
524 false),
525 GlobalValue::DefaultVisibility, "__lljit.cxa_atexit_helper",
526 {PlatformInstanceDecl});
527
528 return ThreadSafeModule(std::move(M), std::move(Ctx));
529 }
530
531 LLJIT &J;
532 std::string InitFunctionPrefix;
533 std::string DeInitFunctionPrefix;
537 ItaniumCXAAtExitSupport AtExitMgr;
538};
539
540Error GenericLLVMIRPlatform::setupJITDylib(JITDylib &JD) {
541 return S.setupJITDylib(JD);
542}
543
544Error GenericLLVMIRPlatform::teardownJITDylib(JITDylib &JD) {
545 return Error::success();
546}
547
548Error GenericLLVMIRPlatform::notifyAdding(ResourceTracker &RT,
549 const MaterializationUnit &MU) {
550 return S.notifyAdding(RT, MU);
551}
552
554GlobalCtorDtorScraper::operator()(ThreadSafeModule TSM,
556 auto Err = TSM.withModuleDo([&](Module &M) -> Error {
557 auto &Ctx = M.getContext();
558 auto *GlobalCtors = M.getNamedGlobal("llvm.global_ctors");
559 auto *GlobalDtors = M.getNamedGlobal("llvm.global_dtors");
560
561 auto RegisterCOrDtors = [&](GlobalVariable *GlobalCOrDtors,
562 bool isCtor) -> Error {
563 // If there's no llvm.global_c/dtor or it's just a decl then skip.
564 if (!GlobalCOrDtors || GlobalCOrDtors->isDeclaration())
565 return Error::success();
566 std::string InitOrDeInitFunctionName;
567 if (isCtor)
568 raw_string_ostream(InitOrDeInitFunctionName)
569 << InitFunctionPrefix << M.getModuleIdentifier();
570 else
571 raw_string_ostream(InitOrDeInitFunctionName)
572 << DeInitFunctionPrefix << M.getModuleIdentifier();
573
574 MangleAndInterner Mangle(PS.getExecutionSession(), M.getDataLayout());
575 auto InternedInitOrDeInitName = Mangle(InitOrDeInitFunctionName);
576 if (auto Err = R.defineMaterializing(
577 {{InternedInitOrDeInitName, JITSymbolFlags::Callable}}))
578 return Err;
579
580 auto *InitOrDeInitFunc = Function::Create(
581 FunctionType::get(Type::getVoidTy(Ctx), {}, false),
582 GlobalValue::ExternalLinkage, InitOrDeInitFunctionName, &M);
583 InitOrDeInitFunc->setVisibility(GlobalValue::HiddenVisibility);
584 std::vector<std::pair<Function *, unsigned>> InitsOrDeInits;
585 auto COrDtors = isCtor ? getConstructors(M) : getDestructors(M);
586
587 for (auto E : COrDtors)
588 InitsOrDeInits.push_back(std::make_pair(E.Func, E.Priority));
589 llvm::sort(InitsOrDeInits, llvm::less_second());
590
591 auto *InitOrDeInitFuncEntryBlock =
592 BasicBlock::Create(Ctx, "entry", InitOrDeInitFunc);
593 IRBuilder<> IB(InitOrDeInitFuncEntryBlock);
594 for (auto &KV : InitsOrDeInits)
595 IB.CreateCall(KV.first);
596 IB.CreateRetVoid();
597
598 if (isCtor)
599 PS.registerInitFunc(R.getTargetJITDylib(), InternedInitOrDeInitName);
600 else
601 PS.registerDeInitFunc(R.getTargetJITDylib(), InternedInitOrDeInitName);
602
603 GlobalCOrDtors->eraseFromParent();
604 return Error::success();
605 };
606
607 if (auto Err = RegisterCOrDtors(GlobalCtors, true))
608 return Err;
609 if (auto Err = RegisterCOrDtors(GlobalDtors, false))
610 return Err;
611
612 return Error::success();
613 });
614
615 if (Err)
616 return std::move(Err);
617
618 return std::move(TSM);
619}
620
621/// Inactive Platform Support
622///
623/// Explicitly disables platform support. JITDylibs are not scanned for special
624/// init/deinit symbols. No runtime API interposes are injected.
625class InactivePlatformSupport : public LLJIT::PlatformSupport {
626public:
627 InactivePlatformSupport() = default;
628
629 Error initialize(JITDylib &JD) override {
630 LLVM_DEBUG(dbgs() << "InactivePlatformSupport: no initializers running for "
631 << JD.getName() << "\n");
632 return Error::success();
633 }
634
635 Error deinitialize(JITDylib &JD) override {
637 dbgs() << "InactivePlatformSupport: no deinitializers running for "
638 << JD.getName() << "\n");
639 return Error::success();
640 }
641};
642
643} // end anonymous namespace
644
645namespace llvm {
646namespace orc {
647
650 J.InitHelperTransformLayer->setTransform(std::move(T));
651}
652
654
656
657 LLVM_DEBUG(dbgs() << "Preparing to create LLJIT instance...\n");
658
659 if (!JTMB) {
660 LLVM_DEBUG({
661 dbgs() << " No explicitly set JITTargetMachineBuilder. "
662 "Detecting host...\n";
663 });
664 if (auto JTMBOrErr = JITTargetMachineBuilder::detectHost())
665 JTMB = std::move(*JTMBOrErr);
666 else
667 return JTMBOrErr.takeError();
668 }
669
670 LLVM_DEBUG({
671 dbgs() << " JITTargetMachineBuilder is "
672 << JITTargetMachineBuilderPrinter(*JTMB, " ")
673 << " Pre-constructed ExecutionSession: " << (ES ? "Yes" : "No")
674 << "\n"
675 << " DataLayout: ";
676 if (DL)
677 dbgs() << DL->getStringRepresentation() << "\n";
678 else
679 dbgs() << "None (will be created by JITTargetMachineBuilder)\n";
680
681 dbgs() << " Custom object-linking-layer creator: "
682 << (CreateObjectLinkingLayer ? "Yes" : "No") << "\n"
683 << " Custom compile-function creator: "
684 << (CreateCompileFunction ? "Yes" : "No") << "\n"
685 << " Custom platform-setup function: "
686 << (SetUpPlatform ? "Yes" : "No") << "\n"
687 << " Number of compile threads: " << NumCompileThreads;
688 if (!NumCompileThreads)
689 dbgs() << " (code will be compiled on the execution thread)\n";
690 else
691 dbgs() << "\n";
692 });
693
694 // If neither ES nor EPC has been set then create an EPC instance.
695 if (!ES && !EPC) {
696 LLVM_DEBUG({
697 dbgs() << "ExecutorProcessControl not specified, "
698 "Creating SelfExecutorProcessControl instance\n";
699 });
700 if (auto EPCOrErr = SelfExecutorProcessControl::Create())
701 EPC = std::move(*EPCOrErr);
702 else
703 return EPCOrErr.takeError();
704 } else
705 LLVM_DEBUG({
706 dbgs() << "Using explicitly specified ExecutorProcessControl instance "
707 << EPC.get() << "\n";
708 });
709
710 // If the client didn't configure any linker options then auto-configure the
711 // JIT linker.
712 if (!CreateObjectLinkingLayer) {
713 auto &TT = JTMB->getTargetTriple();
714 bool UseJITLink = false;
715 switch (TT.getArch()) {
716 case Triple::riscv64:
718 UseJITLink = true;
719 break;
720 case Triple::aarch64:
721 UseJITLink = !TT.isOSBinFormatCOFF();
722 break;
723 case Triple::x86_64:
724 UseJITLink = TT.isOSBinFormatMachO();
725 break;
726 default:
727 break;
728 }
729 if (UseJITLink) {
730 JTMB->setRelocationModel(Reloc::PIC_);
731 JTMB->setCodeModel(CodeModel::Small);
732 CreateObjectLinkingLayer =
734 const Triple &) -> Expected<std::unique_ptr<ObjectLayer>> {
735 auto ObjLinkingLayer = std::make_unique<ObjectLinkingLayer>(ES);
736 if (auto EHFrameRegistrar = EPCEHFrameRegistrar::Create(ES))
737 ObjLinkingLayer->addPlugin(
738 std::make_unique<EHFrameRegistrationPlugin>(
739 ES, std::move(*EHFrameRegistrar)));
740 else
741 return EHFrameRegistrar.takeError();
742 return std::move(ObjLinkingLayer);
743 };
744 }
745 }
746
747 return Error::success();
748}
749
751 if (CompileThreads)
752 CompileThreads->wait();
753 if (auto Err = ES->endSession())
754 ES->reportError(std::move(Err));
755}
756
759 if (!G)
760 return G.takeError();
761
762 if (auto *ExistingJD = ES->getJITDylibByName(Path))
763 return *ExistingJD;
764
765 auto &JD = ES->createBareJITDylib(Path);
766 JD.addGenerator(std::move(*G));
767 return JD;
768}
769
771 std::unique_ptr<MemoryBuffer> LibBuffer) {
773 std::move(LibBuffer));
774 if (!G)
775 return G.takeError();
776
777 JD.addGenerator(std::move(*G));
778
779 return Error::success();
780}
781
784 if (!G)
785 return G.takeError();
786
787 JD.addGenerator(std::move(*G));
788
789 return Error::success();
790}
791
793 assert(TSM && "Can not add null module");
794
795 if (auto Err =
796 TSM.withModuleDo([&](Module &M) { return applyDataLayout(M); }))
797 return Err;
798
799 return InitHelperTransformLayer->add(std::move(RT), std::move(TSM));
800}
801
803 return addIRModule(JD.getDefaultResourceTracker(), std::move(TSM));
804}
805
807 std::unique_ptr<MemoryBuffer> Obj) {
808 assert(Obj && "Can not add null object");
809
810 return ObjTransformLayer->add(std::move(RT), std::move(Obj));
811}
812
813Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
814 return addObjectFile(JD.getDefaultResourceTracker(), std::move(Obj));
815}
816
819 if (auto Sym = ES->lookup(
821 Name))
822 return ExecutorAddr(Sym->getAddress());
823 else
824 return Sym.takeError();
825}
826
829
830 // If the config state provided an ObjectLinkingLayer factory then use it.
832 return S.CreateObjectLinkingLayer(ES, S.JTMB->getTargetTriple());
833
834 // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
835 // a new SectionMemoryManager for each object.
836 auto GetMemMgr = []() { return std::make_unique<SectionMemoryManager>(); };
837 auto Layer =
838 std::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr));
839
840 if (S.JTMB->getTargetTriple().isOSBinFormatCOFF()) {
841 Layer->setOverrideObjectFlagsWithResponsibilityFlags(true);
842 Layer->setAutoClaimResponsibilityForObjectSymbols(true);
843 }
844
845 if (S.JTMB->getTargetTriple().isOSBinFormatELF() &&
846 (S.JTMB->getTargetTriple().getArch() == Triple::ArchType::ppc64 ||
847 S.JTMB->getTargetTriple().getArch() == Triple::ArchType::ppc64le))
848 Layer->setAutoClaimResponsibilityForObjectSymbols(true);
849
850 // FIXME: Explicit conversion to std::unique_ptr<ObjectLayer> added to silence
851 // errors from some GCC / libstdc++ bots. Remove this conversion (i.e.
852 // just return ObjLinkingLayer) once those bots are upgraded.
853 return std::unique_ptr<ObjectLayer>(std::move(Layer));
854}
855
859
860 /// If there is a custom compile function creator set then use it.
862 return S.CreateCompileFunction(std::move(JTMB));
863
864 // Otherwise default to creating a SimpleCompiler, or ConcurrentIRCompiler,
865 // depending on the number of threads requested.
866 if (S.NumCompileThreads > 0)
867 return std::make_unique<ConcurrentIRCompiler>(std::move(JTMB));
868
869 auto TM = JTMB.createTargetMachine();
870 if (!TM)
871 return TM.takeError();
872
873 return std::make_unique<TMOwningSimpleCompiler>(std::move(*TM));
874}
875
877 : DL(""), TT(S.JTMB->getTargetTriple()) {
878
880
881 assert(!(S.EPC && S.ES) && "EPC and ES should not both be set");
882
883 if (S.EPC) {
884 ES = std::make_unique<ExecutionSession>(std::move(S.EPC));
885 } else if (S.ES)
886 ES = std::move(S.ES);
887 else {
888 if (auto EPC = SelfExecutorProcessControl::Create()) {
889 ES = std::make_unique<ExecutionSession>(std::move(*EPC));
890 } else {
891 Err = EPC.takeError();
892 return;
893 }
894 }
895
896 if (auto MainOrErr = this->ES->createJITDylib("main"))
897 Main = &*MainOrErr;
898 else {
899 Err = MainOrErr.takeError();
900 return;
901 }
902
903 if (S.DL)
904 DL = std::move(*S.DL);
905 else if (auto DLOrErr = S.JTMB->getDefaultDataLayoutForTarget())
906 DL = std::move(*DLOrErr);
907 else {
908 Err = DLOrErr.takeError();
909 return;
910 }
911
912 auto ObjLayer = createObjectLinkingLayer(S, *ES);
913 if (!ObjLayer) {
914 Err = ObjLayer.takeError();
915 return;
916 }
917 ObjLinkingLayer = std::move(*ObjLayer);
919 std::make_unique<ObjectTransformLayer>(*ES, *ObjLinkingLayer);
920
921 {
922 auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB));
923 if (!CompileFunction) {
924 Err = CompileFunction.takeError();
925 return;
926 }
927 CompileLayer = std::make_unique<IRCompileLayer>(
928 *ES, *ObjTransformLayer, std::move(*CompileFunction));
929 TransformLayer = std::make_unique<IRTransformLayer>(*ES, *CompileLayer);
931 std::make_unique<IRTransformLayer>(*ES, *TransformLayer);
932 }
933
934 if (S.NumCompileThreads > 0) {
935 InitHelperTransformLayer->setCloneToNewContextOnEmit(true);
937 std::make_unique<ThreadPool>(hardware_concurrency(S.NumCompileThreads));
938 ES->setDispatchTask([this](std::unique_ptr<Task> T) {
939 // FIXME: We should be able to use move-capture here, but ThreadPool's
940 // AsyncTaskTys are std::functions rather than unique_functions
941 // (because MSVC's std::packaged_tasks don't support move-only types).
942 // Fix this when all the above gets sorted out.
943 CompileThreads->async([UnownedT = T.release()]() mutable {
944 std::unique_ptr<Task> T(UnownedT);
945 T->run();
946 });
947 });
948 }
949
950 if (S.SetUpPlatform)
951 Err = S.SetUpPlatform(*this);
952 else
954}
955
956std::string LLJIT::mangle(StringRef UnmangledName) const {
957 std::string MangledName;
958 {
959 raw_string_ostream MangledNameStream(MangledName);
960 Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL);
961 }
962 return MangledName;
963}
964
966 if (M.getDataLayout().isDefault())
967 M.setDataLayout(DL);
968
969 if (M.getDataLayout() != DL)
970 return make_error<StringError>(
971 "Added modules have incompatible data layouts: " +
972 M.getDataLayout().getStringRepresentation() + " (module) vs " +
973 DL.getStringRepresentation() + " (jit)",
975
976 return Error::success();
977}
978
981 { dbgs() << "Setting up orc platform support for LLJIT\n"; });
982 J.setPlatformSupport(std::make_unique<ORCPlatformSupport>(J));
983 return Error::success();
984}
985
988 { dbgs() << "Setting up GenericLLVMIRPlatform support for LLJIT\n"; });
989 J.setPlatformSupport(std::make_unique<GenericLLVMIRPlatformSupport>(J));
990}
991
994 { dbgs() << "Explicitly deactivated platform support for LLJIT\n"; });
995 J.setPlatformSupport(std::make_unique<InactivePlatformSupport>());
996 return Error::success();
997}
998
1001 return Err;
1002 TT = JTMB->getTargetTriple();
1003 return Error::success();
1004}
1005
1007 assert(TSM && "Can not add null module");
1008
1009 if (auto Err = TSM.withModuleDo(
1010 [&](Module &M) -> Error { return applyDataLayout(M); }))
1011 return Err;
1012
1013 return CODLayer->add(JD, std::move(TSM));
1014}
1015
1016LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
1017
1018 // If LLJIT construction failed then bail out.
1019 if (Err)
1020 return;
1021
1022 ErrorAsOutParameter _(&Err);
1023
1024 /// Take/Create the lazy-compile callthrough manager.
1025 if (S.LCTMgr)
1026 LCTMgr = std::move(S.LCTMgr);
1027 else {
1028 if (auto LCTMgrOrErr = createLocalLazyCallThroughManager(
1030 LCTMgr = std::move(*LCTMgrOrErr);
1031 else {
1032 Err = LCTMgrOrErr.takeError();
1033 return;
1034 }
1035 }
1036
1037 // Take/Create the indirect stubs manager builder.
1038 auto ISMBuilder = std::move(S.ISMBuilder);
1039
1040 // If none was provided, try to build one.
1041 if (!ISMBuilder)
1043
1044 // No luck. Bail out.
1045 if (!ISMBuilder) {
1046 Err = make_error<StringError>("Could not construct "
1047 "IndirectStubsManagerBuilder for target " +
1048 S.TT.str(),
1050 return;
1051 }
1052
1053 // Create the COD layer.
1054 CODLayer = std::make_unique<CompileOnDemandLayer>(
1055 *ES, *InitHelperTransformLayer, *LCTMgr, std::move(ISMBuilder));
1056
1057 if (S.NumCompileThreads > 0)
1058 CODLayer->setCloneToNewContextOnEmit(true);
1059}
1060
1061// In-process LLJIT uses eh-frame section wrappers via EPC, so we need to force
1062// them to be linked in.
1066}
1067
1068} // End namespace orc.
1069} // End namespace llvm.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
for(auto &MBB :MF)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ATTRIBUTE_USED
Definition: Compiler.h:139
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
#define _
#define F(x, y, z)
Definition: MD5.cpp:55
#define G(x, y, z)
Definition: MD5.cpp:56
Module.h This file contains the declarations for the Module class.
if(VerifyEach)
const char LLVMTargetMachineRef TM
static StringRef getName(Value *V)
llvm::orc::shared::CWrapperFunctionResult llvm_orc_deregisterEHFrameSectionWrapper(const char *Data, uint64_t Size)
llvm::orc::shared::CWrapperFunctionResult llvm_orc_registerEHFrameSectionWrapper(const char *Data, uint64_t Size)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:105
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:888
const std::string & getStringRepresentation() const
Returns the string representation of the DataLayout.
Definition: DataLayout.h:246
Helper for Errors used as out-parameters.
Definition: Error.h:1104
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:136
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:275
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:62
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:63
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:64
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:468
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2564
Flags for symbols in the JIT.
Definition: JITSymbol.h:74
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
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:651
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:533
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:688
@ loongarch64
Definition: Triple.h:62
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:356
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:680
const std::string & str() const
Definition: Triple.h:415
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
static Expected< std::unique_ptr< EPCDynamicLibrarySearchGenerator > > Load(ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow=SymbolPredicate())
Permanently loads the library at the given path and, on success, returns a DynamicLibrarySearchGenera...
static Expected< std::unique_ptr< EPCEHFrameRegistrar > > Create(ExecutionSession &ES, std::optional< ExecutorAddr > RegistrationFunctionsDylib=std::nullopt)
Create from a ExecutorProcessControl instance alone.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1373
void setPlatform(std::unique_ptr< Platform > P)
Set the Platform for this ExecutionSession.
Definition: Core.h:1430
Error callSPSWrapper(ExecutorAddr WrapperFnAddr, WrapperCallArgTs &&...WrapperCallArgs)
Run a wrapper function using SPS to serialize the arguments and deserialize the results.
Definition: Core.h:1621
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
Definition: Core.h:1437
Represents an address in the executor process.
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
An interface for Itanium __cxa_atexit interposer implementations.
Represents a JIT'd dynamic library.
Definition: Core.h:964
Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition: Core.h:1815
static Expected< std::vector< JITDylibSP > > getDFSLinkOrder(ArrayRef< JITDylibSP > JDs)
Returns the given JITDylibs and all of their transitive dependencies in DFS order (based on linkage r...
Definition: Core.cpp:2004
ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
Definition: Core.cpp:662
GeneratorT & addGenerator(std::unique_ptr< GeneratorT > DefGenerator)
Adds a definition generator to this JITDylib and returns a referenece to it.
Definition: Core.h:1798
A utility class for building TargetMachines for JITs.
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
Error prepareForConstruction()
Called prior to JIT class construcion to fix up defaults.
Definition: LLJIT.cpp:655
ObjectLinkingLayerCreator CreateObjectLinkingLayer
Definition: LLJIT.h:293
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:290
CompileFunctionCreator CreateCompileFunction
Definition: LLJIT.h:294
std::unique_ptr< ExecutorProcessControl > EPC
Definition: LLJIT.h:289
std::optional< DataLayout > DL
Definition: LLJIT.h:292
std::optional< JITTargetMachineBuilder > JTMB
Definition: LLJIT.h:291
PlatformSetupFunction SetUpPlatform
Definition: LLJIT.h:295
Initializer support for LLJIT.
Definition: LLJIT.h:44
virtual Error deinitialize(JITDylib &JD)=0
virtual Error initialize(JITDylib &JD)=0
static void setInitTransform(LLJIT &J, IRTransformLayer::TransformFunction T)
Definition: LLJIT.cpp:648
A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
Definition: LLJIT.h:37
static Expected< std::unique_ptr< ObjectLayer > > createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES)
Definition: LLJIT.cpp:828
void setPlatformSupport(std::unique_ptr< PlatformSupport > PS)
Set the PlatformSupport instance.
Definition: LLJIT.h:167
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:228
LLJIT(LLJITBuilderState &S, Error &Err)
Create an LLJIT instance with a single compile thread.
Definition: LLJIT.cpp:876
Error addObjectFile(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:806
JITDylib & getMainJITDylib()
Returns a reference to the JITDylib representing the JIT'd main program.
Definition: LLJIT.h:71
const DataLayout & getDataLayout() const
Returns a reference to the DataLayout for this instance.
Definition: LLJIT.h:68
std::unique_ptr< ObjectTransformLayer > ObjTransformLayer
Definition: LLJIT.h:238
virtual ~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:750
std::string mangle(StringRef UnmangledName) const
Returns a linker-mangled version of UnmangledName.
Definition: LLJIT.cpp:956
JITDylib * Main
Definition: LLJIT.h:231
Expected< ExecutorAddr > lookup(JITDylib &JD, StringRef UnmangledName)
Look up a symbol in JITDylib JD based on its IR symbol name.
Definition: LLJIT.h:157
Expected< JITDylib & > loadPlatformDynamicLibrary(const char *Path)
Load a (real) dynamic library and make its symbols available through a new JITDylib with the same nam...
Definition: LLJIT.cpp:757
std::unique_ptr< IRTransformLayer > InitHelperTransformLayer
Definition: LLJIT.h:241
std::unique_ptr< IRCompileLayer > CompileLayer
Definition: LLJIT.h:239
const Triple & getTargetTriple() const
Returns a reference to the triple for this instance.
Definition: LLJIT.h:65
Expected< ExecutorAddr > lookupLinkerMangled(JITDylib &JD, SymbolStringPtr Name)
Look up a symbol in JITDylib JD by the symbol's linker-mangled name (to look up symbols based on thei...
Definition: LLJIT.cpp:817
static Expected< std::unique_ptr< IRCompileLayer::IRCompiler > > createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB)
Definition: LLJIT.cpp:857
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this instance.
Definition: LLJIT.h:62
std::unique_ptr< IRTransformLayer > TransformLayer
Definition: LLJIT.h:240
SymbolStringPtr mangleAndIntern(StringRef UnmangledName) const
Returns an interned, linker-mangled version of UnmangledName.
Definition: LLJIT.h:210
DataLayout DL
Definition: LLJIT.h:233
Error linkStaticLibraryInto(JITDylib &JD, std::unique_ptr< MemoryBuffer > LibBuffer)
Link a static library into the given JITDylib.
Definition: LLJIT.cpp:770
friend void setUpGenericLLVMIRPlatform(LLJIT &J)
Configure the LLJIT instance to scrape modules for llvm.global_ctors and llvm.global_dtors variables ...
Definition: LLJIT.cpp:986
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:965
std::unique_ptr< ThreadPool > CompileThreads
Definition: LLJIT.h:235
std::unique_ptr< ObjectLayer > ObjLinkingLayer
Definition: LLJIT.h:237
Triple TT
Definition: LLJIT.h:234
Error addIRModule(ResourceTrackerSP RT, ThreadSafeModule TSM)
Adds an IR module with the given ResourceTracker.
Definition: LLJIT.cpp:792
ExecutorAddr LazyCompileFailureAddr
Definition: LLJIT.h:430
std::unique_ptr< LazyCallThroughManager > LCTMgr
Definition: LLJIT.h:431
IndirectStubsManagerBuilderFunction ISMBuilder
Definition: LLJIT.h:432
Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M)
Add a module to be lazily compiled to JITDylib JD.
Definition: LLJIT.cpp:1006
Mangles symbol names then uniques them in the context of an ExecutionSession.
Definition: Mangling.h:26
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:526
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:675
const SymbolFlagsMap & getSymbols() const
Return the set of symbols that this source provides.
Definition: Core.h:705
const SymbolStringPtr & getInitializerSymbol() const
Returns the initialization symbol for this MaterializationUnit (if any).
Definition: Core.h:708
Platforms set up standard symbols and mediate interactions between dynamic initializers (e....
Definition: Core.h:1317
virtual Error teardownJITDylib(JITDylib &JD)=0
This method will be called outside the session lock each time a JITDylib is removed to allow the Plat...
virtual Error notifyRemoving(ResourceTracker &RT)=0
This method will be called under the ExecutionSession lock when a ResourceTracker is removed.
static Expected< DenseMap< JITDylib *, SymbolMap > > lookupInitSymbols(ExecutionSession &ES, const DenseMap< JITDylib *, SymbolLookupSet > &InitSyms)
A utility function for looking up initializer symbols.
Definition: Core.cpp:1792
virtual Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU)=0
This method will be called under the ExecutionSession lock each time a MaterializationUnit is added t...
virtual Error setupJITDylib(JITDylib &JD)=0
This method will be called outside the session lock each time a JITDylib is created (unless it is cre...
API to remove / transfer ownership of JIT resources.
Definition: Core.h:55
JITDylib & getJITDylib() const
Return the JITDylib targeted by this tracker.
Definition: Core.h:70
static Expected< std::unique_ptr< SelfExecutorProcessControl > > Create(std::shared_ptr< SymbolStringPool > SSP=nullptr, std::unique_ptr< TaskDispatcher > D=nullptr, std::unique_ptr< jitlink::JITLinkMemoryManager > MemMgr=nullptr)
Create a SelfExecutorProcessControl with the given symbol string pool and memory manager.
static Expected< std::unique_ptr< StaticLibraryDefinitionGenerator > > Load(ObjectLayer &L, const char *FileName, GetObjectFileInterface GetObjFileInterface=GetObjectFileInterface())
Try to create a StaticLibraryDefinitionGenerator from the given path.
static Expected< std::unique_ptr< StaticLibraryDefinitionGenerator > > Create(ObjectLayer &L, std::unique_ptr< MemoryBuffer > ArchiveBuffer, std::unique_ptr< object::Archive > Archive, GetObjectFileInterface GetObjFileInterface=GetObjectFileInterface())
Try to create a StaticLibrarySearchGenerator from the given memory buffer and Archive object.
Pointer to a pooled string representing a symbol name.
An LLVM Module together with a shared ThreadSafeContext.
decltype(auto) withModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function on the contained Module.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:642
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:165
iterator_range< CtorDtorIterator > getDestructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
Definition: Core.h:773
Error setUpOrcPlatform(LLJIT &J)
Configure the LLJIT instance to use orc runtime support.
Definition: LLJIT.cpp:979
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
LLVM_ATTRIBUTE_USED void linkComponents()
Definition: LLJIT.cpp:1063
std::function< std::unique_ptr< IndirectStubsManager >()> createLocalIndirectStubsManagerBuilder(const Triple &T)
Create a local indriect stubs manager builder.
void setUpGenericLLVMIRPlatform(LLJIT &J)
Configure the LLJIT instance to scrape modules for llvm.global_ctors and llvm.global_dtors variables ...
Definition: LLJIT.cpp:986
Expected< std::unique_ptr< LazyCallThroughManager > > createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES, ExecutorAddr ErrorHandlerAddr)
Create a LocalLazyCallThroughManager from the given triple and execution session.
Error setUpInactivePlatform(LLJIT &J)
Configure the LLJIT instance to disable platform support explicitly.
Definition: LLJIT.cpp:992
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware resources are to be used,...
Definition: Threading.h:185
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:79
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:511
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1744
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:745
Function object to check whether the second component of a container supported by std::get (like std:...
Definition: STLExtras.h:1546