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