LLVM 18.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
27#include "llvm/IR/IRBuilder.h"
28#include "llvm/IR/Mangler.h"
29#include "llvm/IR/Module.h"
31
32#include <map>
33
34#define DEBUG_TYPE "orc"
35
36using namespace llvm;
37using namespace llvm::orc;
38
39namespace {
40
41/// Adds helper function decls and wrapper functions that call the helper with
42/// some additional prefix arguments.
43///
44/// E.g. For wrapper "foo" with type i8(i8, i64), helper "bar", and prefix
45/// args i32 4 and i16 12345, this function will add:
46///
47/// declare i8 @bar(i32, i16, i8, i64)
48///
49/// define i8 @foo(i8, i64) {
50/// entry:
51/// %2 = call i8 @bar(i32 4, i16 12345, i8 %0, i64 %1)
52/// ret i8 %2
53/// }
54///
55Function *addHelperAndWrapper(Module &M, StringRef WrapperName,
56 FunctionType *WrapperFnType,
57 GlobalValue::VisibilityTypes WrapperVisibility,
58 StringRef HelperName,
59 ArrayRef<Value *> HelperPrefixArgs) {
60 std::vector<Type *> HelperArgTypes;
61 for (auto *Arg : HelperPrefixArgs)
62 HelperArgTypes.push_back(Arg->getType());
63 for (auto *T : WrapperFnType->params())
64 HelperArgTypes.push_back(T);
65 auto *HelperFnType =
66 FunctionType::get(WrapperFnType->getReturnType(), HelperArgTypes, false);
67 auto *HelperFn = Function::Create(HelperFnType, GlobalValue::ExternalLinkage,
68 HelperName, M);
69
70 auto *WrapperFn = Function::Create(
71 WrapperFnType, GlobalValue::ExternalLinkage, WrapperName, M);
72 WrapperFn->setVisibility(WrapperVisibility);
73
74 auto *EntryBlock = BasicBlock::Create(M.getContext(), "entry", WrapperFn);
75 IRBuilder<> IB(EntryBlock);
76
77 std::vector<Value *> HelperArgs;
78 for (auto *Arg : HelperPrefixArgs)
79 HelperArgs.push_back(Arg);
80 for (auto &Arg : WrapperFn->args())
81 HelperArgs.push_back(&Arg);
82 auto *HelperResult = IB.CreateCall(HelperFn, HelperArgs);
83 if (HelperFn->getReturnType()->isVoidTy())
84 IB.CreateRetVoid();
85 else
86 IB.CreateRet(HelperResult);
87
88 return WrapperFn;
89}
90
91class ORCPlatformSupport : public LLJIT::PlatformSupport {
92public:
93 ORCPlatformSupport(orc::LLJIT &J) : J(J) {}
94
95 Error initialize(orc::JITDylib &JD) override {
98 using SPSDLOpenSig = SPSExecutorAddr(SPSString, int32_t);
99 enum dlopen_mode : int32_t {
100 ORC_RT_RTLD_LAZY = 0x1,
101 ORC_RT_RTLD_NOW = 0x2,
102 ORC_RT_RTLD_LOCAL = 0x4,
103 ORC_RT_RTLD_GLOBAL = 0x8
104 };
105
106 auto &ES = J.getExecutionSession();
107 auto MainSearchOrder = J.getMainJITDylib().withLinkOrderDo(
108 [](const JITDylibSearchOrder &SO) { return SO; });
109
110 if (auto WrapperAddr =
111 ES.lookup(MainSearchOrder,
112 J.mangleAndIntern("__orc_rt_jit_dlopen_wrapper"))) {
113 return ES.callSPSWrapper<SPSDLOpenSig>(WrapperAddr->getAddress(),
114 DSOHandles[&JD], JD.getName(),
115 int32_t(ORC_RT_RTLD_LAZY));
116 } else
117 return WrapperAddr.takeError();
118 }
119
120 Error deinitialize(orc::JITDylib &JD) override {
122 using SPSDLCloseSig = int32_t(SPSExecutorAddr);
123
124 auto &ES = J.getExecutionSession();
125 auto MainSearchOrder = J.getMainJITDylib().withLinkOrderDo(
126 [](const JITDylibSearchOrder &SO) { return SO; });
127
128 if (auto WrapperAddr =
129 ES.lookup(MainSearchOrder,
130 J.mangleAndIntern("__orc_rt_jit_dlclose_wrapper"))) {
131 int32_t result;
132 auto E = J.getExecutionSession().callSPSWrapper<SPSDLCloseSig>(
133 WrapperAddr->getAddress(), result, DSOHandles[&JD]);
134 if (E)
135 return E;
136 else if (result)
137 return make_error<StringError>("dlclose failed",
139 DSOHandles.erase(&JD);
140 } else
141 return WrapperAddr.takeError();
142 return Error::success();
143 }
144
145private:
146 orc::LLJIT &J;
148};
149
150class GenericLLVMIRPlatformSupport;
151
152/// orc::Platform component of Generic LLVM IR Platform support.
153/// Just forwards calls to the GenericLLVMIRPlatformSupport class below.
154class GenericLLVMIRPlatform : public Platform {
155public:
156 GenericLLVMIRPlatform(GenericLLVMIRPlatformSupport &S) : S(S) {}
157 Error setupJITDylib(JITDylib &JD) override;
158 Error teardownJITDylib(JITDylib &JD) override;
160 const MaterializationUnit &MU) override;
161 Error notifyRemoving(ResourceTracker &RT) override {
162 // Noop -- Nothing to do (yet).
163 return Error::success();
164 }
165
166private:
167 GenericLLVMIRPlatformSupport &S;
168};
169
170/// This transform parses llvm.global_ctors to produce a single initialization
171/// function for the module, records the function, then deletes
172/// llvm.global_ctors.
173class GlobalCtorDtorScraper {
174public:
175 GlobalCtorDtorScraper(GenericLLVMIRPlatformSupport &PS,
176 StringRef InitFunctionPrefix,
177 StringRef DeInitFunctionPrefix)
178 : PS(PS), InitFunctionPrefix(InitFunctionPrefix),
179 DeInitFunctionPrefix(DeInitFunctionPrefix) {}
182
183private:
184 GenericLLVMIRPlatformSupport &PS;
185 StringRef InitFunctionPrefix;
186 StringRef DeInitFunctionPrefix;
187};
188
189/// Generic IR Platform Support
190///
191/// Scrapes llvm.global_ctors and llvm.global_dtors and replaces them with
192/// specially named 'init' and 'deinit'. Injects definitions / interposes for
193/// some runtime API, including __cxa_atexit, dlopen, and dlclose.
194class GenericLLVMIRPlatformSupport : public LLJIT::PlatformSupport {
195public:
196 GenericLLVMIRPlatformSupport(LLJIT &J, JITDylib &PlatformJD)
197 : J(J), InitFunctionPrefix(J.mangle("__orc_init_func.")),
198 DeInitFunctionPrefix(J.mangle("__orc_deinit_func.")) {
199
201 std::make_unique<GenericLLVMIRPlatform>(*this));
202
203 setInitTransform(J, GlobalCtorDtorScraper(*this, InitFunctionPrefix,
204 DeInitFunctionPrefix));
205
206 SymbolMap StdInterposes;
207
208 StdInterposes[J.mangleAndIntern("__lljit.platform_support_instance")] = {
210 StdInterposes[J.mangleAndIntern("__lljit.cxa_atexit_helper")] = {
211 ExecutorAddr::fromPtr(registerCxaAtExitHelper), JITSymbolFlags()};
212
213 cantFail(PlatformJD.define(absoluteSymbols(std::move(StdInterposes))));
214 cantFail(setupJITDylib(PlatformJD));
215 cantFail(J.addIRModule(PlatformJD, createPlatformRuntimeModule()));
216 }
217
219
220 /// Adds a module that defines the __dso_handle global.
221 Error setupJITDylib(JITDylib &JD) {
222
223 // Add per-jitdylib standard interposes.
224 SymbolMap PerJDInterposes;
225 PerJDInterposes[J.mangleAndIntern("__lljit.run_atexits_helper")] = {
226 ExecutorAddr::fromPtr(runAtExitsHelper), JITSymbolFlags()};
227 PerJDInterposes[J.mangleAndIntern("__lljit.atexit_helper")] = {
228 ExecutorAddr::fromPtr(registerAtExitHelper), JITSymbolFlags()};
229 cantFail(JD.define(absoluteSymbols(std::move(PerJDInterposes))));
230
231 auto Ctx = std::make_unique<LLVMContext>();
232 auto M = std::make_unique<Module>("__standard_lib", *Ctx);
233 M->setDataLayout(J.getDataLayout());
234
235 auto *Int64Ty = Type::getInt64Ty(*Ctx);
236 auto *DSOHandle = new GlobalVariable(
237 *M, Int64Ty, true, GlobalValue::ExternalLinkage,
238 ConstantInt::get(Int64Ty, reinterpret_cast<uintptr_t>(&JD)),
239 "__dso_handle");
240 DSOHandle->setVisibility(GlobalValue::DefaultVisibility);
241 DSOHandle->setInitializer(
242 ConstantInt::get(Int64Ty, ExecutorAddr::fromPtr(&JD).getValue()));
243
244 auto *GenericIRPlatformSupportTy =
245 StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport");
246
247 auto *PlatformInstanceDecl = new GlobalVariable(
248 *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage,
249 nullptr, "__lljit.platform_support_instance");
250
251 auto *VoidTy = Type::getVoidTy(*Ctx);
252 addHelperAndWrapper(
253 *M, "__lljit_run_atexits", FunctionType::get(VoidTy, {}, false),
254 GlobalValue::HiddenVisibility, "__lljit.run_atexits_helper",
255 {PlatformInstanceDecl, DSOHandle});
256
257 auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT);
258 auto *AtExitCallbackTy = FunctionType::get(VoidTy, {}, false);
259 auto *AtExitCallbackPtrTy = PointerType::getUnqual(AtExitCallbackTy);
260 addHelperAndWrapper(*M, "atexit",
261 FunctionType::get(IntTy, {AtExitCallbackPtrTy}, false),
262 GlobalValue::HiddenVisibility, "__lljit.atexit_helper",
263 {PlatformInstanceDecl, DSOHandle});
264
265 return J.addIRModule(JD, ThreadSafeModule(std::move(M), std::move(Ctx)));
266 }
267
268 Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU) {
269 auto &JD = RT.getJITDylib();
270 if (auto &InitSym = MU.getInitializerSymbol())
271 InitSymbols[&JD].add(InitSym, SymbolLookupFlags::WeaklyReferencedSymbol);
272 else {
273 // If there's no identified init symbol attached, but there is a symbol
274 // with the GenericIRPlatform::InitFunctionPrefix, then treat that as
275 // an init function. Add the symbol to both the InitSymbols map (which
276 // will trigger a lookup to materialize the module) and the InitFunctions
277 // map (which holds the names of the symbols to execute).
278 for (auto &KV : MU.getSymbols())
279 if ((*KV.first).startswith(InitFunctionPrefix)) {
280 InitSymbols[&JD].add(KV.first,
281 SymbolLookupFlags::WeaklyReferencedSymbol);
282 InitFunctions[&JD].add(KV.first);
283 } else if ((*KV.first).startswith(DeInitFunctionPrefix)) {
284 DeInitFunctions[&JD].add(KV.first);
285 }
286 }
287 return Error::success();
288 }
289
290 Error initialize(JITDylib &JD) override {
291 LLVM_DEBUG({
292 dbgs() << "GenericLLVMIRPlatformSupport getting initializers to run\n";
293 });
294 if (auto Initializers = getInitializers(JD)) {
296 { dbgs() << "GenericLLVMIRPlatformSupport running initializers\n"; });
297 for (auto InitFnAddr : *Initializers) {
298 LLVM_DEBUG({
299 dbgs() << " Running init " << formatv("{0:x16}", InitFnAddr)
300 << "...\n";
301 });
302 auto *InitFn = InitFnAddr.toPtr<void (*)()>();
303 InitFn();
304 }
305 } else
306 return Initializers.takeError();
307 return Error::success();
308 }
309
310 Error deinitialize(JITDylib &JD) override {
311 LLVM_DEBUG({
312 dbgs() << "GenericLLVMIRPlatformSupport getting deinitializers to run\n";
313 });
314 if (auto Deinitializers = getDeinitializers(JD)) {
315 LLVM_DEBUG({
316 dbgs() << "GenericLLVMIRPlatformSupport running deinitializers\n";
317 });
318 for (auto DeinitFnAddr : *Deinitializers) {
319 LLVM_DEBUG({
320 dbgs() << " Running deinit " << formatv("{0:x16}", DeinitFnAddr)
321 << "...\n";
322 });
323 auto *DeinitFn = DeinitFnAddr.toPtr<void (*)()>();
324 DeinitFn();
325 }
326 } else
327 return Deinitializers.takeError();
328
329 return Error::success();
330 }
331
332 void registerInitFunc(JITDylib &JD, SymbolStringPtr InitName) {
334 InitFunctions[&JD].add(InitName);
335 });
336 }
337
338 void registerDeInitFunc(JITDylib &JD, SymbolStringPtr DeInitName) {
340 [&]() { DeInitFunctions[&JD].add(DeInitName); });
341 }
342
343private:
344 Expected<std::vector<ExecutorAddr>> getInitializers(JITDylib &JD) {
345 if (auto Err = issueInitLookups(JD))
346 return std::move(Err);
347
349 std::vector<JITDylibSP> DFSLinkOrder;
350
351 if (auto Err = getExecutionSession().runSessionLocked([&]() -> Error {
352 if (auto DFSLinkOrderOrErr = JD.getDFSLinkOrder())
353 DFSLinkOrder = std::move(*DFSLinkOrderOrErr);
354 else
355 return DFSLinkOrderOrErr.takeError();
356
357 for (auto &NextJD : DFSLinkOrder) {
358 auto IFItr = InitFunctions.find(NextJD.get());
359 if (IFItr != InitFunctions.end()) {
360 LookupSymbols[NextJD.get()] = std::move(IFItr->second);
361 InitFunctions.erase(IFItr);
362 }
363 }
364 return Error::success();
365 }))
366 return std::move(Err);
367
368 LLVM_DEBUG({
369 dbgs() << "JITDylib init order is [ ";
370 for (auto &JD : llvm::reverse(DFSLinkOrder))
371 dbgs() << "\"" << JD->getName() << "\" ";
372 dbgs() << "]\n";
373 dbgs() << "Looking up init functions:\n";
374 for (auto &KV : LookupSymbols)
375 dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n";
376 });
377
378 auto &ES = getExecutionSession();
379 auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols);
380
381 if (!LookupResult)
382 return LookupResult.takeError();
383
384 std::vector<ExecutorAddr> Initializers;
385 while (!DFSLinkOrder.empty()) {
386 auto &NextJD = *DFSLinkOrder.back();
387 DFSLinkOrder.pop_back();
388 auto InitsItr = LookupResult->find(&NextJD);
389 if (InitsItr == LookupResult->end())
390 continue;
391 for (auto &KV : InitsItr->second)
392 Initializers.push_back(KV.second.getAddress());
393 }
394
395 return Initializers;
396 }
397
398 Expected<std::vector<ExecutorAddr>> getDeinitializers(JITDylib &JD) {
399 auto &ES = getExecutionSession();
400
401 auto LLJITRunAtExits = J.mangleAndIntern("__lljit_run_atexits");
402
404 std::vector<JITDylibSP> DFSLinkOrder;
405
406 if (auto Err = ES.runSessionLocked([&]() -> Error {
407 if (auto DFSLinkOrderOrErr = JD.getDFSLinkOrder())
408 DFSLinkOrder = std::move(*DFSLinkOrderOrErr);
409 else
410 return DFSLinkOrderOrErr.takeError();
411
412 for (auto &NextJD : DFSLinkOrder) {
413 auto &JDLookupSymbols = LookupSymbols[NextJD.get()];
414 auto DIFItr = DeInitFunctions.find(NextJD.get());
415 if (DIFItr != DeInitFunctions.end()) {
416 LookupSymbols[NextJD.get()] = std::move(DIFItr->second);
417 DeInitFunctions.erase(DIFItr);
418 }
419 JDLookupSymbols.add(LLJITRunAtExits,
420 SymbolLookupFlags::WeaklyReferencedSymbol);
421 }
422 return Error::success();
423 }))
424 return std::move(Err);
425
426 LLVM_DEBUG({
427 dbgs() << "JITDylib deinit order is [ ";
428 for (auto &JD : DFSLinkOrder)
429 dbgs() << "\"" << JD->getName() << "\" ";
430 dbgs() << "]\n";
431 dbgs() << "Looking up deinit functions:\n";
432 for (auto &KV : LookupSymbols)
433 dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n";
434 });
435
436 auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols);
437
438 if (!LookupResult)
439 return LookupResult.takeError();
440
441 std::vector<ExecutorAddr> DeInitializers;
442 for (auto &NextJD : DFSLinkOrder) {
443 auto DeInitsItr = LookupResult->find(NextJD.get());
444 assert(DeInitsItr != LookupResult->end() &&
445 "Every JD should have at least __lljit_run_atexits");
446
447 auto RunAtExitsItr = DeInitsItr->second.find(LLJITRunAtExits);
448 if (RunAtExitsItr != DeInitsItr->second.end())
449 DeInitializers.push_back(RunAtExitsItr->second.getAddress());
450
451 for (auto &KV : DeInitsItr->second)
452 if (KV.first != LLJITRunAtExits)
453 DeInitializers.push_back(KV.second.getAddress());
454 }
455
456 return DeInitializers;
457 }
458
459 /// Issue lookups for all init symbols required to initialize JD (and any
460 /// JITDylibs that it depends on).
461 Error issueInitLookups(JITDylib &JD) {
462 DenseMap<JITDylib *, SymbolLookupSet> RequiredInitSymbols;
463 std::vector<JITDylibSP> DFSLinkOrder;
464
465 if (auto Err = getExecutionSession().runSessionLocked([&]() -> Error {
466 if (auto DFSLinkOrderOrErr = JD.getDFSLinkOrder())
467 DFSLinkOrder = std::move(*DFSLinkOrderOrErr);
468 else
469 return DFSLinkOrderOrErr.takeError();
470
471 for (auto &NextJD : DFSLinkOrder) {
472 auto ISItr = InitSymbols.find(NextJD.get());
473 if (ISItr != InitSymbols.end()) {
474 RequiredInitSymbols[NextJD.get()] = std::move(ISItr->second);
475 InitSymbols.erase(ISItr);
476 }
477 }
478 return Error::success();
479 }))
480 return Err;
481
482 return Platform::lookupInitSymbols(getExecutionSession(),
483 RequiredInitSymbols)
484 .takeError();
485 }
486
487 static void registerCxaAtExitHelper(void *Self, void (*F)(void *), void *Ctx,
488 void *DSOHandle) {
489 LLVM_DEBUG({
490 dbgs() << "Registering cxa atexit function " << (void *)F << " for JD "
491 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
492 });
493 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.registerAtExit(
494 F, Ctx, DSOHandle);
495 }
496
497 static void registerAtExitHelper(void *Self, void *DSOHandle, void (*F)()) {
498 LLVM_DEBUG({
499 dbgs() << "Registering atexit function " << (void *)F << " for JD "
500 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
501 });
502 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.registerAtExit(
503 reinterpret_cast<void (*)(void *)>(F), nullptr, DSOHandle);
504 }
505
506 static void runAtExitsHelper(void *Self, void *DSOHandle) {
507 LLVM_DEBUG({
508 dbgs() << "Running atexit functions for JD "
509 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
510 });
511 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.runAtExits(
512 DSOHandle);
513 }
514
515 // Constructs an LLVM IR module containing platform runtime globals,
516 // functions, and interposes.
517 ThreadSafeModule createPlatformRuntimeModule() {
518 auto Ctx = std::make_unique<LLVMContext>();
519 auto M = std::make_unique<Module>("__standard_lib", *Ctx);
520 M->setDataLayout(J.getDataLayout());
521
522 auto *GenericIRPlatformSupportTy =
523 StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport");
524
525 auto *PlatformInstanceDecl = new GlobalVariable(
526 *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage,
527 nullptr, "__lljit.platform_support_instance");
528
529 auto *Int8Ty = Type::getInt8Ty(*Ctx);
530 auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT);
531 auto *VoidTy = Type::getVoidTy(*Ctx);
532 auto *BytePtrTy = PointerType::getUnqual(Int8Ty);
533 auto *CxaAtExitCallbackTy = FunctionType::get(VoidTy, {BytePtrTy}, false);
534 auto *CxaAtExitCallbackPtrTy = PointerType::getUnqual(CxaAtExitCallbackTy);
535
536 addHelperAndWrapper(
537 *M, "__cxa_atexit",
538 FunctionType::get(IntTy, {CxaAtExitCallbackPtrTy, BytePtrTy, BytePtrTy},
539 false),
540 GlobalValue::DefaultVisibility, "__lljit.cxa_atexit_helper",
541 {PlatformInstanceDecl});
542
543 return ThreadSafeModule(std::move(M), std::move(Ctx));
544 }
545
546 LLJIT &J;
547 std::string InitFunctionPrefix;
548 std::string DeInitFunctionPrefix;
552 ItaniumCXAAtExitSupport AtExitMgr;
553};
554
555Error GenericLLVMIRPlatform::setupJITDylib(JITDylib &JD) {
556 return S.setupJITDylib(JD);
557}
558
559Error GenericLLVMIRPlatform::teardownJITDylib(JITDylib &JD) {
560 return Error::success();
561}
562
563Error GenericLLVMIRPlatform::notifyAdding(ResourceTracker &RT,
564 const MaterializationUnit &MU) {
565 return S.notifyAdding(RT, MU);
566}
567
569GlobalCtorDtorScraper::operator()(ThreadSafeModule TSM,
571 auto Err = TSM.withModuleDo([&](Module &M) -> Error {
572 auto &Ctx = M.getContext();
573 auto *GlobalCtors = M.getNamedGlobal("llvm.global_ctors");
574 auto *GlobalDtors = M.getNamedGlobal("llvm.global_dtors");
575
576 auto RegisterCOrDtors = [&](GlobalVariable *GlobalCOrDtors,
577 bool isCtor) -> Error {
578 // If there's no llvm.global_c/dtor or it's just a decl then skip.
579 if (!GlobalCOrDtors || GlobalCOrDtors->isDeclaration())
580 return Error::success();
581 std::string InitOrDeInitFunctionName;
582 if (isCtor)
583 raw_string_ostream(InitOrDeInitFunctionName)
584 << InitFunctionPrefix << M.getModuleIdentifier();
585 else
586 raw_string_ostream(InitOrDeInitFunctionName)
587 << DeInitFunctionPrefix << M.getModuleIdentifier();
588
589 MangleAndInterner Mangle(PS.getExecutionSession(), M.getDataLayout());
590 auto InternedInitOrDeInitName = Mangle(InitOrDeInitFunctionName);
591 if (auto Err = R.defineMaterializing(
592 {{InternedInitOrDeInitName, JITSymbolFlags::Callable}}))
593 return Err;
594
595 auto *InitOrDeInitFunc = Function::Create(
596 FunctionType::get(Type::getVoidTy(Ctx), {}, false),
597 GlobalValue::ExternalLinkage, InitOrDeInitFunctionName, &M);
598 InitOrDeInitFunc->setVisibility(GlobalValue::HiddenVisibility);
599 std::vector<std::pair<Function *, unsigned>> InitsOrDeInits;
600 auto COrDtors = isCtor ? getConstructors(M) : getDestructors(M);
601
602 for (auto E : COrDtors)
603 InitsOrDeInits.push_back(std::make_pair(E.Func, E.Priority));
604 llvm::sort(InitsOrDeInits, llvm::less_second());
605
606 auto *InitOrDeInitFuncEntryBlock =
607 BasicBlock::Create(Ctx, "entry", InitOrDeInitFunc);
608 IRBuilder<> IB(InitOrDeInitFuncEntryBlock);
609 for (auto &KV : InitsOrDeInits)
610 IB.CreateCall(KV.first);
611 IB.CreateRetVoid();
612
613 if (isCtor)
614 PS.registerInitFunc(R.getTargetJITDylib(), InternedInitOrDeInitName);
615 else
616 PS.registerDeInitFunc(R.getTargetJITDylib(), InternedInitOrDeInitName);
617
618 GlobalCOrDtors->eraseFromParent();
619 return Error::success();
620 };
621
622 if (auto Err = RegisterCOrDtors(GlobalCtors, true))
623 return Err;
624 if (auto Err = RegisterCOrDtors(GlobalDtors, false))
625 return Err;
626
627 return Error::success();
628 });
629
630 if (Err)
631 return std::move(Err);
632
633 return std::move(TSM);
634}
635
636/// Inactive Platform Support
637///
638/// Explicitly disables platform support. JITDylibs are not scanned for special
639/// init/deinit symbols. No runtime API interposes are injected.
640class InactivePlatformSupport : public LLJIT::PlatformSupport {
641public:
642 InactivePlatformSupport() = default;
643
644 Error initialize(JITDylib &JD) override {
645 LLVM_DEBUG(dbgs() << "InactivePlatformSupport: no initializers running for "
646 << JD.getName() << "\n");
647 return Error::success();
648 }
649
650 Error deinitialize(JITDylib &JD) override {
652 dbgs() << "InactivePlatformSupport: no deinitializers running for "
653 << JD.getName() << "\n");
654 return Error::success();
655 }
656};
657
658} // end anonymous namespace
659
660namespace llvm {
661namespace orc {
662
665 J.InitHelperTransformLayer->setTransform(std::move(T));
666}
667
669
671
672 LLVM_DEBUG(dbgs() << "Preparing to create LLJIT instance...\n");
673
674 if (!JTMB) {
675 LLVM_DEBUG({
676 dbgs() << " No explicitly set JITTargetMachineBuilder. "
677 "Detecting host...\n";
678 });
679 if (auto JTMBOrErr = JITTargetMachineBuilder::detectHost())
680 JTMB = std::move(*JTMBOrErr);
681 else
682 return JTMBOrErr.takeError();
683 }
684
685 LLVM_DEBUG({
686 dbgs() << " JITTargetMachineBuilder is "
687 << JITTargetMachineBuilderPrinter(*JTMB, " ")
688 << " Pre-constructed ExecutionSession: " << (ES ? "Yes" : "No")
689 << "\n"
690 << " DataLayout: ";
691 if (DL)
692 dbgs() << DL->getStringRepresentation() << "\n";
693 else
694 dbgs() << "None (will be created by JITTargetMachineBuilder)\n";
695
696 dbgs() << " Custom object-linking-layer creator: "
697 << (CreateObjectLinkingLayer ? "Yes" : "No") << "\n"
698 << " Custom compile-function creator: "
699 << (CreateCompileFunction ? "Yes" : "No") << "\n"
700 << " Custom platform-setup function: "
701 << (SetUpPlatform ? "Yes" : "No") << "\n"
702 << " Number of compile threads: " << NumCompileThreads;
703 if (!NumCompileThreads)
704 dbgs() << " (code will be compiled on the execution thread)\n";
705 else
706 dbgs() << "\n";
707 });
708
709 // Create DL if not specified.
710 if (!DL) {
711 if (auto DLOrErr = JTMB->getDefaultDataLayoutForTarget())
712 DL = std::move(*DLOrErr);
713 else
714 return DLOrErr.takeError();
715 }
716
717 // If neither ES nor EPC has been set then create an EPC instance.
718 if (!ES && !EPC) {
719 LLVM_DEBUG({
720 dbgs() << "ExecutorProcessControl not specified, "
721 "Creating SelfExecutorProcessControl instance\n";
722 });
723 if (auto EPCOrErr = SelfExecutorProcessControl::Create())
724 EPC = std::move(*EPCOrErr);
725 else
726 return EPCOrErr.takeError();
727 } else if (EPC) {
728 LLVM_DEBUG({
729 dbgs() << "Using explicitly specified ExecutorProcessControl instance "
730 << EPC.get() << "\n";
731 });
732 } else {
733 LLVM_DEBUG({
734 dbgs() << "Using explicitly specified ExecutionSession instance "
735 << ES.get() << "\n";
736 });
737 }
738
739 // If the client didn't configure any linker options then auto-configure the
740 // JIT linker.
741 if (!CreateObjectLinkingLayer) {
742 auto &TT = JTMB->getTargetTriple();
743 bool UseJITLink = false;
744 switch (TT.getArch()) {
745 case Triple::riscv64:
747 UseJITLink = true;
748 break;
749 case Triple::aarch64:
750 UseJITLink = !TT.isOSBinFormatCOFF();
751 break;
752 case Triple::x86_64:
753 UseJITLink = !TT.isOSBinFormatCOFF();
754 break;
755 case Triple::ppc64:
756 UseJITLink = TT.isPPC64ELFv2ABI();
757 break;
758 case Triple::ppc64le:
759 UseJITLink = TT.isOSBinFormatELF();
760 break;
761 default:
762 break;
763 }
764 if (UseJITLink) {
765 JTMB->setRelocationModel(Reloc::PIC_);
766 JTMB->setCodeModel(CodeModel::Small);
767 CreateObjectLinkingLayer =
769 const Triple &) -> Expected<std::unique_ptr<ObjectLayer>> {
770 auto ObjLinkingLayer = std::make_unique<ObjectLinkingLayer>(ES);
771 if (auto EHFrameRegistrar = EPCEHFrameRegistrar::Create(ES))
772 ObjLinkingLayer->addPlugin(
773 std::make_unique<EHFrameRegistrationPlugin>(
774 ES, std::move(*EHFrameRegistrar)));
775 else
776 return EHFrameRegistrar.takeError();
777 return std::move(ObjLinkingLayer);
778 };
779 }
780 }
781
782 // If we need a process JITDylib but no setup function has been given then
783 // create a default one.
784 if (!SetupProcessSymbolsJITDylib &&
785 (LinkProcessSymbolsByDefault || EnableDebuggerSupport)) {
786
787 LLVM_DEBUG({
788 dbgs() << "Creating default Process JD setup function (neeeded for";
789 if (LinkProcessSymbolsByDefault)
790 dbgs() << " <link-process-syms-by-default>";
791 if (EnableDebuggerSupport)
792 dbgs() << " <debugger-support>";
793 dbgs() << ")\n";
794 });
795
796 SetupProcessSymbolsJITDylib = [this](LLJIT &J) -> Expected<JITDylibSP> {
797 auto &JD =
798 J.getExecutionSession().createBareJITDylib("<Process Symbols>");
801 if (!G)
802 return G.takeError();
803 JD.addGenerator(std::move(*G));
804 return &JD;
805 };
806 }
807
808 return Error::success();
809}
810
812 if (CompileThreads)
813 CompileThreads->wait();
814 if (auto Err = ES->endSession())
815 ES->reportError(std::move(Err));
816}
817
819
821
823 auto JD = ES->createJITDylib(std::move(Name));
824 if (!JD)
825 return JD.takeError();
826
828 return JD;
829}
830
833 if (!G)
834 return G.takeError();
835
836 if (auto *ExistingJD = ES->getJITDylibByName(Path))
837 return *ExistingJD;
838
839 auto &JD = ES->createBareJITDylib(Path);
840 JD.addGenerator(std::move(*G));
841 return JD;
842}
843
845 std::unique_ptr<MemoryBuffer> LibBuffer) {
847 std::move(LibBuffer));
848 if (!G)
849 return G.takeError();
850
851 JD.addGenerator(std::move(*G));
852
853 return Error::success();
854}
855
858 if (!G)
859 return G.takeError();
860
861 JD.addGenerator(std::move(*G));
862
863 return Error::success();
864}
865
867 assert(TSM && "Can not add null module");
868
869 if (auto Err =
870 TSM.withModuleDo([&](Module &M) { return applyDataLayout(M); }))
871 return Err;
872
873 return InitHelperTransformLayer->add(std::move(RT), std::move(TSM));
874}
875
877 return addIRModule(JD.getDefaultResourceTracker(), std::move(TSM));
878}
879
881 std::unique_ptr<MemoryBuffer> Obj) {
882 assert(Obj && "Can not add null object");
883
884 return ObjTransformLayer->add(std::move(RT), std::move(Obj));
885}
886
887Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
888 return addObjectFile(JD.getDefaultResourceTracker(), std::move(Obj));
889}
890
893 if (auto Sym = ES->lookup(
895 Name))
896 return Sym->getAddress();
897 else
898 return Sym.takeError();
899}
900
903
904 // If the config state provided an ObjectLinkingLayer factory then use it.
906 return S.CreateObjectLinkingLayer(ES, S.JTMB->getTargetTriple());
907
908 // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
909 // a new SectionMemoryManager for each object.
910 auto GetMemMgr = []() { return std::make_unique<SectionMemoryManager>(); };
911 auto Layer =
912 std::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr));
913
914 if (S.JTMB->getTargetTriple().isOSBinFormatCOFF()) {
915 Layer->setOverrideObjectFlagsWithResponsibilityFlags(true);
916 Layer->setAutoClaimResponsibilityForObjectSymbols(true);
917 }
918
919 if (S.JTMB->getTargetTriple().isOSBinFormatELF() &&
920 (S.JTMB->getTargetTriple().getArch() == Triple::ArchType::ppc64 ||
921 S.JTMB->getTargetTriple().getArch() == Triple::ArchType::ppc64le))
922 Layer->setAutoClaimResponsibilityForObjectSymbols(true);
923
924 // FIXME: Explicit conversion to std::unique_ptr<ObjectLayer> added to silence
925 // errors from some GCC / libstdc++ bots. Remove this conversion (i.e.
926 // just return ObjLinkingLayer) once those bots are upgraded.
927 return std::unique_ptr<ObjectLayer>(std::move(Layer));
928}
929
933
934 /// If there is a custom compile function creator set then use it.
936 return S.CreateCompileFunction(std::move(JTMB));
937
938 // Otherwise default to creating a SimpleCompiler, or ConcurrentIRCompiler,
939 // depending on the number of threads requested.
940 if (S.NumCompileThreads > 0)
941 return std::make_unique<ConcurrentIRCompiler>(std::move(JTMB));
942
943 auto TM = JTMB.createTargetMachine();
944 if (!TM)
945 return TM.takeError();
946
947 return std::make_unique<TMOwningSimpleCompiler>(std::move(*TM));
948}
949
951 : DL(std::move(*S.DL)), TT(S.JTMB->getTargetTriple()) {
952
954
955 assert(!(S.EPC && S.ES) && "EPC and ES should not both be set");
956
957 if (S.EPC) {
958 ES = std::make_unique<ExecutionSession>(std::move(S.EPC));
959 } else if (S.ES)
960 ES = std::move(S.ES);
961 else {
962 if (auto EPC = SelfExecutorProcessControl::Create()) {
963 ES = std::make_unique<ExecutionSession>(std::move(*EPC));
964 } else {
965 Err = EPC.takeError();
966 return;
967 }
968 }
969
970 auto ObjLayer = createObjectLinkingLayer(S, *ES);
971 if (!ObjLayer) {
972 Err = ObjLayer.takeError();
973 return;
974 }
975 ObjLinkingLayer = std::move(*ObjLayer);
977 std::make_unique<ObjectTransformLayer>(*ES, *ObjLinkingLayer);
978
979 {
980 auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB));
981 if (!CompileFunction) {
982 Err = CompileFunction.takeError();
983 return;
984 }
985 CompileLayer = std::make_unique<IRCompileLayer>(
986 *ES, *ObjTransformLayer, std::move(*CompileFunction));
987 TransformLayer = std::make_unique<IRTransformLayer>(*ES, *CompileLayer);
989 std::make_unique<IRTransformLayer>(*ES, *TransformLayer);
990 }
991
992 if (S.NumCompileThreads > 0) {
993 InitHelperTransformLayer->setCloneToNewContextOnEmit(true);
995 std::make_unique<ThreadPool>(hardware_concurrency(S.NumCompileThreads));
996 ES->setDispatchTask([this](std::unique_ptr<Task> T) {
997 // FIXME: We should be able to use move-capture here, but ThreadPool's
998 // AsyncTaskTys are std::functions rather than unique_functions
999 // (because MSVC's std::packaged_tasks don't support move-only types).
1000 // Fix this when all the above gets sorted out.
1001 CompileThreads->async([UnownedT = T.release()]() mutable {
1002 std::unique_ptr<Task> T(UnownedT);
1003 T->run();
1004 });
1005 });
1006 }
1007
1009 if (auto ProcSymsJD = S.SetupProcessSymbolsJITDylib(*this)) {
1010 ProcessSymbols = ProcSymsJD->get();
1011 } else {
1012 Err = ProcSymsJD.takeError();
1013 return;
1014 }
1015 }
1016
1017 if (S.EnableDebuggerSupport) {
1018 if (auto *OLL = dyn_cast<ObjectLinkingLayer>(ObjLinkingLayer.get())) {
1019 switch (TT.getObjectFormat()) {
1020 case Triple::ELF: {
1021 auto Registrar = createJITLoaderGDBRegistrar(*ES);
1022 if (!Registrar) {
1023 Err = Registrar.takeError();
1024 return;
1025 }
1026 OLL->addPlugin(std::make_unique<DebugObjectManagerPlugin>(
1027 *ES, std::move(*Registrar), true, true));
1028 break;
1029 }
1030 case Triple::MachO: {
1031 assert(ProcessSymbols && "ProcessSymbols JD should be available when "
1032 "EnableDebuggerSupport is set");
1033 auto DS =
1035 if (!DS) {
1036 Err = DS.takeError();
1037 return;
1038 }
1039 OLL->addPlugin(std::move(*DS));
1040 break;
1041 }
1042 default:
1043 LLVM_DEBUG({
1044 dbgs() << "Cannot enable LLJIT debugger support: "
1046 << " not supported.\n";
1047 });
1048 }
1049 } else {
1050 LLVM_DEBUG({
1051 dbgs() << "Cannot enable LLJIT debugger support: "
1052 " debugger support is only available when using JITLink.\n";
1053 });
1054 }
1055 }
1056
1057 if (!S.SetUpPlatform)
1059
1060 if (auto PlatformJDOrErr = S.SetUpPlatform(*this)) {
1061 Platform = PlatformJDOrErr->get();
1062 if (Platform)
1063 DefaultLinks.push_back(
1065 } else {
1066 Err = PlatformJDOrErr.takeError();
1067 return;
1068 }
1069
1071 DefaultLinks.push_back(
1073
1074 if (auto MainOrErr = createJITDylib("main"))
1075 Main = &*MainOrErr;
1076 else {
1077 Err = MainOrErr.takeError();
1078 return;
1079 }
1080}
1081
1082std::string LLJIT::mangle(StringRef UnmangledName) const {
1083 std::string MangledName;
1084 {
1085 raw_string_ostream MangledNameStream(MangledName);
1086 Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL);
1087 }
1088 return MangledName;
1089}
1090
1092 if (M.getDataLayout().isDefault())
1093 M.setDataLayout(DL);
1094
1095 if (M.getDataLayout() != DL)
1096 return make_error<StringError>(
1097 "Added modules have incompatible data layouts: " +
1098 M.getDataLayout().getStringRepresentation() + " (module) vs " +
1099 DL.getStringRepresentation() + " (jit)",
1101
1102 return Error::success();
1103}
1104
1106 LLVM_DEBUG({ dbgs() << "Setting up orc platform support for LLJIT\n"; });
1107 J.setPlatformSupport(std::make_unique<ORCPlatformSupport>(J));
1108 return Error::success();
1109}
1110
1112public:
1115 if (!DLLName.ends_with_insensitive(".dll"))
1116 return make_error<StringError>("DLLName not ending with .dll",
1118 auto DLLNameStr = DLLName.str(); // Guarantees null-termination.
1119 auto DLLJD = J.loadPlatformDynamicLibrary(DLLNameStr.c_str());
1120 if (!DLLJD)
1121 return DLLJD.takeError();
1122 JD.addToLinkOrder(*DLLJD);
1123 return Error::success();
1124 }
1125
1126private:
1127 LLJIT &J;
1128};
1129
1131 auto ProcessSymbolsJD = J.getProcessSymbolsJITDylib();
1132 if (!ProcessSymbolsJD)
1133 return make_error<StringError>(
1134 "Native platforms require a process symbols JITDylib",
1136
1137 const Triple &TT = J.getTargetTriple();
1138 ObjectLinkingLayer *ObjLinkingLayer =
1139 dyn_cast<ObjectLinkingLayer>(&J.getObjLinkingLayer());
1140
1141 if (!ObjLinkingLayer)
1142 return make_error<StringError>(
1143 "SetUpTargetPlatform requires ObjectLinkingLayer",
1145
1146 std::unique_ptr<MemoryBuffer> RuntimeArchiveBuffer;
1147 if (OrcRuntime.index() == 0) {
1148 auto A = errorOrToExpected(MemoryBuffer::getFile(std::get<0>(OrcRuntime)));
1149 if (!A)
1150 return A.takeError();
1151 RuntimeArchiveBuffer = std::move(*A);
1152 } else
1153 RuntimeArchiveBuffer = std::move(std::get<1>(OrcRuntime));
1154
1155 auto &ES = J.getExecutionSession();
1156 auto &PlatformJD = ES.createBareJITDylib("<Platform>");
1157 PlatformJD.addToLinkOrder(*ProcessSymbolsJD);
1158
1159 J.setPlatformSupport(std::make_unique<ORCPlatformSupport>(J));
1160
1161 switch (TT.getObjectFormat()) {
1162 case Triple::COFF: {
1163 const char *VCRuntimePath = nullptr;
1164 bool StaticVCRuntime = false;
1165 if (VCRuntime) {
1166 VCRuntimePath = VCRuntime->first.c_str();
1167 StaticVCRuntime = VCRuntime->second;
1168 }
1169 if (auto P = COFFPlatform::Create(
1170 ES, *ObjLinkingLayer, PlatformJD, std::move(RuntimeArchiveBuffer),
1171 LoadAndLinkDynLibrary(J), StaticVCRuntime, VCRuntimePath))
1172 J.getExecutionSession().setPlatform(std::move(*P));
1173 else
1174 return P.takeError();
1175 break;
1176 }
1177 case Triple::ELF: {
1179 *ObjLinkingLayer, std::move(RuntimeArchiveBuffer));
1180 if (!G)
1181 return G.takeError();
1182
1183 if (auto P = ELFNixPlatform::Create(ES, *ObjLinkingLayer, PlatformJD,
1184 std::move(*G)))
1185 J.getExecutionSession().setPlatform(std::move(*P));
1186 else
1187 return P.takeError();
1188 break;
1189 }
1190 case Triple::MachO: {
1192 *ObjLinkingLayer, std::move(RuntimeArchiveBuffer));
1193 if (!G)
1194 return G.takeError();
1195
1196 if (auto P = MachOPlatform::Create(ES, *ObjLinkingLayer, PlatformJD,
1197 std::move(*G)))
1198 ES.setPlatform(std::move(*P));
1199 else
1200 return P.takeError();
1201 break;
1202 }
1203 default:
1204 return make_error<StringError>("Unsupported object format in triple " +
1205 TT.str(),
1207 }
1208
1209 return &PlatformJD;
1210}
1211
1213 LLVM_DEBUG(
1214 { dbgs() << "Setting up GenericLLVMIRPlatform support for LLJIT\n"; });
1215 auto ProcessSymbolsJD = J.getProcessSymbolsJITDylib();
1216 if (!ProcessSymbolsJD)
1217 return make_error<StringError>(
1218 "Native platforms require a process symbols JITDylib",
1220
1221 auto &PlatformJD = J.getExecutionSession().createBareJITDylib("<Platform>");
1222 PlatformJD.addToLinkOrder(*ProcessSymbolsJD);
1223
1225 std::make_unique<GenericLLVMIRPlatformSupport>(J, PlatformJD));
1226
1227 return &PlatformJD;
1228}
1229
1231 LLVM_DEBUG(
1232 { dbgs() << "Explicitly deactivated platform support for LLJIT\n"; });
1233 J.setPlatformSupport(std::make_unique<InactivePlatformSupport>());
1234 return nullptr;
1235}
1236
1239 return Err;
1240 TT = JTMB->getTargetTriple();
1241 return Error::success();
1242}
1243
1245 assert(TSM && "Can not add null module");
1246
1247 if (auto Err = TSM.withModuleDo(
1248 [&](Module &M) -> Error { return applyDataLayout(M); }))
1249 return Err;
1250
1251 return CODLayer->add(JD, std::move(TSM));
1252}
1253
1254LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
1255
1256 // If LLJIT construction failed then bail out.
1257 if (Err)
1258 return;
1259
1260 ErrorAsOutParameter _(&Err);
1261
1262 /// Take/Create the lazy-compile callthrough manager.
1263 if (S.LCTMgr)
1264 LCTMgr = std::move(S.LCTMgr);
1265 else {
1266 if (auto LCTMgrOrErr = createLocalLazyCallThroughManager(
1268 LCTMgr = std::move(*LCTMgrOrErr);
1269 else {
1270 Err = LCTMgrOrErr.takeError();
1271 return;
1272 }
1273 }
1274
1275 // Take/Create the indirect stubs manager builder.
1276 auto ISMBuilder = std::move(S.ISMBuilder);
1277
1278 // If none was provided, try to build one.
1279 if (!ISMBuilder)
1281
1282 // No luck. Bail out.
1283 if (!ISMBuilder) {
1284 Err = make_error<StringError>("Could not construct "
1285 "IndirectStubsManagerBuilder for target " +
1286 S.TT.str(),
1288 return;
1289 }
1290
1291 // Create the COD layer.
1292 CODLayer = std::make_unique<CompileOnDemandLayer>(
1293 *ES, *InitHelperTransformLayer, *LCTMgr, std::move(ISMBuilder));
1294
1295 if (S.NumCompileThreads > 0)
1296 CODLayer->setCloneToNewContextOnEmit(true);
1297}
1298
1299// In-process LLJIT uses eh-frame section wrappers via EPC, so we need to force
1300// them to be linked in.
1304}
1305
1306} // End namespace orc.
1307} // End namespace llvm.
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ATTRIBUTE_USED
Definition: Compiler.h:151
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:468
#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.
#define P(N)
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
char getGlobalPrefix() const
Definition: DataLayout.h:316
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:1096
Lightweight error class with error context and mandatory checking.
Definition: Error.h:154
static ErrorSuccess success()
Create a success value.
Definition: Error.h:328
Tagged union holding either a T or a Error.
Definition: Error.h:468
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:138
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:273
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:454
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2628
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
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
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
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:659
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:222
bool ends_with_insensitive(StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
Definition: StringRef.cpp:50
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:514
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isPPC64ELFv2ABI() const
Tests whether the target 64-bit PowerPC big endian ABI is ELFv2.
Definition: Triple.h:923
ObjectFormatType getObjectFormat() const
Get the object format for this triple.
Definition: Triple.h:381
@ loongarch64
Definition: Triple.h:62
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:355
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:680
const std::string & str() const
Definition: Triple.h:414
static StringRef getObjectFormatTypeName(ObjectFormatType ObjectFormat)
Get the name for the Object format.
Definition: Triple.cpp:298
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:675
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< COFFPlatform > > Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< MemoryBuffer > OrcRuntimeArchiveBuffer, LoadDynamicLibrary LoadDynLibrary, bool StaticVCRuntime=false, const char *VCRuntimePath=nullptr, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a COFFPlatform instance, adding the ORC runtime to the given JITDylib.
static Expected< std::unique_ptr< DynamicLibrarySearchGenerator > > GetForCurrentProcess(char GlobalPrefix, SymbolPredicate Allow=SymbolPredicate())
Creates a DynamicLibrarySearchGenerator that searches for symbols in the current process.
static Expected< std::unique_ptr< ELFNixPlatform > > Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< DefinitionGenerator > OrcRuntime, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a ELFNixPlatform instance, adding the ORC runtime to the given JITDylib.
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)
Create from a ExecutorProcessControl instance alone.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1389
void setPlatform(std::unique_ptr< Platform > P)
Set the Platform for this ExecutionSession.
Definition: Core.h:1449
Error callSPSWrapper(ExecutorAddr WrapperFnAddr, WrapperCallArgTs &&...WrapperCallArgs)
Run a wrapper function using SPS to serialize the arguments and deserialize the results.
Definition: Core.h:1652
JITDylib & createBareJITDylib(std::string Name)
Add a new bare JITDylib to this ExecutionSession.
Definition: Core.cpp:1982
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
Definition: Core.h:1456
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
Expected< JITDylibSP > operator()(LLJIT &J)
Definition: LLJIT.cpp:1130
static Expected< std::unique_ptr< GDBJITDebugInfoRegistrationPlugin > > Create(ExecutionSession &ES, JITDylib &ProcessJD, const Triple &TT)
An interface for Itanium __cxa_atexit interposer implementations.
Represents a JIT'd dynamic library.
Definition: Core.h:958
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:1849
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition: Core.h:977
void addToLinkOrder(const JITDylibSearchOrder &NewLinks)
Append the given JITDylibSearchOrder to the link order for this JITDylib (discarding any elements alr...
Definition: Core.cpp:1352
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:2041
auto withLinkOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchOrder & >()))
Do something with the link order (run under the session lock).
Definition: Core.h:1842
ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
Definition: Core.cpp:674
GeneratorT & addGenerator(std::unique_ptr< GeneratorT > DefGenerator)
Adds a definition generator to this JITDylib and returns a referenece to it.
Definition: Core.h:1832
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:670
ProcessSymbolsJITDylibSetupFunction SetupProcessSymbolsJITDylib
Definition: LLJIT.h:319
ObjectLinkingLayerCreator CreateObjectLinkingLayer
Definition: LLJIT.h:320
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:315
CompileFunctionCreator CreateCompileFunction
Definition: LLJIT.h:321
std::unique_ptr< ExecutorProcessControl > EPC
Definition: LLJIT.h:314
std::optional< JITTargetMachineBuilder > JTMB
Definition: LLJIT.h:316
PlatformSetupFunction SetUpPlatform
Definition: LLJIT.h:322
Initializer support for LLJIT.
Definition: LLJIT.h:45
virtual Error deinitialize(JITDylib &JD)=0
virtual Error initialize(JITDylib &JD)=0
static void setInitTransform(LLJIT &J, IRTransformLayer::TransformFunction T)
Definition: LLJIT.cpp:663
A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
Definition: LLJIT.h:38
static Expected< std::unique_ptr< ObjectLayer > > createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES)
Definition: LLJIT.cpp:902
void setPlatformSupport(std::unique_ptr< PlatformSupport > PS)
Set the PlatformSupport instance.
Definition: LLJIT.h:185
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:246
LLJIT(LLJITBuilderState &S, Error &Err)
Create an LLJIT instance with a single compile thread.
Definition: LLJIT.cpp:950
Error addObjectFile(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:880
Expected< JITDylib & > createJITDylib(std::string Name)
Create a new JITDylib with the given name and return a reference to it.
Definition: LLJIT.cpp:822
JITDylib & getMainJITDylib()
Returns a reference to the JITDylib representing the JIT'd main program.
Definition: LLJIT.h:72
JITDylibSearchOrder DefaultLinks
Definition: LLJIT.h:253
const DataLayout & getDataLayout() const
Returns a reference to the DataLayout for this instance.
Definition: LLJIT.h:69
ObjectLayer & getObjLinkingLayer()
Returns a reference to the ObjLinkingLayer.
Definition: LLJIT.h:213
std::unique_ptr< ObjectTransformLayer > ObjTransformLayer
Definition: LLJIT.h:260
friend Expected< JITDylibSP > setUpGenericLLVMIRPlatform(LLJIT &J)
Configure the LLJIT instance to scrape modules for llvm.global_ctors and llvm.global_dtors variables ...
Definition: LLJIT.cpp:1212
virtual ~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:811
std::string mangle(StringRef UnmangledName) const
Returns a linker-mangled version of UnmangledName.
Definition: LLJIT.cpp:1082
JITDylib * Main
Definition: LLJIT.h:251
JITDylibSP getPlatformJITDylib()
Returns the Platform JITDylib, which will contain the ORC runtime (if given) and any platform symbols...
Definition: LLJIT.cpp:820
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:831
std::unique_ptr< IRTransformLayer > InitHelperTransformLayer
Definition: LLJIT.h:263
std::unique_ptr< IRCompileLayer > CompileLayer
Definition: LLJIT.h:261
const Triple & getTargetTriple() const
Returns a reference to the triple for this instance.
Definition: LLJIT.h:66
JITDylibSP getProcessSymbolsJITDylib()
Returns the ProcessSymbols JITDylib, which by default reflects non-JIT'd symbols in the host process.
Definition: LLJIT.cpp:818
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:891
static Expected< std::unique_ptr< IRCompileLayer::IRCompiler > > createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB)
Definition: LLJIT.cpp:931
JITDylib * ProcessSymbols
Definition: LLJIT.h:249
JITDylib * Platform
Definition: LLJIT.h:250
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this instance.
Definition: LLJIT.h:63
std::unique_ptr< IRTransformLayer > TransformLayer
Definition: LLJIT.h:262
SymbolStringPtr mangleAndIntern(StringRef UnmangledName) const
Returns an interned, linker-mangled version of UnmangledName.
Definition: LLJIT.h:228
DataLayout DL
Definition: LLJIT.h:255
Error linkStaticLibraryInto(JITDylib &JD, std::unique_ptr< MemoryBuffer > LibBuffer)
Link a static library into the given JITDylib.
Definition: LLJIT.cpp:844
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:1091
std::unique_ptr< ThreadPool > CompileThreads
Definition: LLJIT.h:257
std::unique_ptr< ObjectLayer > ObjLinkingLayer
Definition: LLJIT.h:259
Triple TT
Definition: LLJIT.h:256
Error addIRModule(ResourceTrackerSP RT, ThreadSafeModule TSM)
Adds an IR module with the given ResourceTracker.
Definition: LLJIT.cpp:866
ExecutorAddr LazyCompileFailureAddr
Definition: LLJIT.h:490
std::unique_ptr< LazyCallThroughManager > LCTMgr
Definition: LLJIT.h:491
IndirectStubsManagerBuilderFunction ISMBuilder
Definition: LLJIT.h:492
Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M)
Add a module to be lazily compiled to JITDylib JD.
Definition: LLJIT.cpp:1244
Error operator()(JITDylib &JD, StringRef DLLName)
Definition: LLJIT.cpp:1114
static Expected< std::unique_ptr< MachOPlatform > > Create(ExecutionSession &ES, ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< DefinitionGenerator > OrcRuntime, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a MachOPlatform instance, adding the ORC runtime to the given JITDylib.
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:527
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Definition: Core.h:662
const SymbolFlagsMap & getSymbols() const
Return the set of symbols that this source provides.
Definition: Core.h:692
const SymbolStringPtr & getInitializerSymbol() const
Returns the initialization symbol for this MaterializationUnit (if any).
Definition: Core.h:695
An ObjectLayer implementation built on JITLink.
Platforms set up standard symbols and mediate interactions between dynamic initializers (e....
Definition: Core.h:1318
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:1824
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:56
JITDylib & getJITDylib() const
Return the JITDylib targeted by this tracker.
Definition: Core.h:71
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:166
iterator_range< CtorDtorIterator > getDestructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
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:162
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
Definition: Core.h:760
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
Expected< JITDylibSP > setUpInactivePlatform(LLJIT &J)
Configure the LLJIT instance to disable platform support explicitly.
Definition: LLJIT.cpp:1230
LLVM_ATTRIBUTE_USED void linkComponents()
Definition: LLJIT.cpp:1301
std::function< std::unique_ptr< IndirectStubsManager >()> createLocalIndirectStubsManagerBuilder(const Triple &T)
Create a local indirect stubs manager builder.
Expected< std::unique_ptr< LazyCallThroughManager > > createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES, ExecutorAddr ErrorHandlerAddr)
Create a LocalLazyCallThroughManager from the given triple and execution session.
Expected< JITDylibSP > setUpGenericLLVMIRPlatform(LLJIT &J)
Configure the LLJIT instance to scrape modules for llvm.global_ctors and llvm.global_dtors variables ...
Definition: LLJIT.cpp:1212
Error setUpOrcPlatformManually(LLJIT &J)
Configure the LLJIT instance to use orc runtime support.
Definition: LLJIT.cpp:1105
Expected< std::unique_ptr< EPCDebugObjectRegistrar > > createJITLoaderGDBRegistrar(ExecutionSession &ES, std::optional< ExecutorAddr > RegistrationFunctionDylib=std::nullopt)
Create a ExecutorProcessControl-based DebugObjectRegistrar that emits debug objects to the GDB JIT in...
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:90
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:429
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1652
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:743
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr<T> to an Expected<T>.
Definition: Error.h:1178
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1854
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Function object to check whether the second component of a container supported by std::get (like std:...
Definition: STLExtras.h:1464