LLVM 20.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 auto ObjLinkingLayer = std::make_unique<ObjectLinkingLayer>(ES);
837 if (auto EHFrameRegistrar = EPCEHFrameRegistrar::Create(ES))
838 ObjLinkingLayer->addPlugin(
839 std::make_unique<EHFrameRegistrationPlugin>(
840 ES, std::move(*EHFrameRegistrar)));
841 else
842 return EHFrameRegistrar.takeError();
843 return std::move(ObjLinkingLayer);
844 };
845 }
846 }
847
848 // If we need a process JITDylib but no setup function has been given then
849 // create a default one.
850 if (!SetupProcessSymbolsJITDylib && LinkProcessSymbolsByDefault) {
851 LLVM_DEBUG(dbgs() << "Creating default Process JD setup function\n");
852 SetupProcessSymbolsJITDylib = [](LLJIT &J) -> Expected<JITDylibSP> {
853 auto &JD =
854 J.getExecutionSession().createBareJITDylib("<Process Symbols>");
856 J.getExecutionSession());
857 if (!G)
858 return G.takeError();
859 JD.addGenerator(std::move(*G));
860 return &JD;
861 };
862 }
863
864 return Error::success();
865}
866
868 if (auto Err = ES->endSession())
869 ES->reportError(std::move(Err));
870}
871
873
875
877 auto JD = ES->createJITDylib(std::move(Name));
878 if (!JD)
879 return JD.takeError();
880
882 return JD;
883}
884
887 if (!G)
888 return G.takeError();
889
890 if (auto *ExistingJD = ES->getJITDylibByName(Path))
891 return *ExistingJD;
892
893 auto &JD = ES->createBareJITDylib(Path);
894 JD.addGenerator(std::move(*G));
895 return JD;
896}
897
899 std::unique_ptr<MemoryBuffer> LibBuffer) {
901 std::move(LibBuffer));
902 if (!G)
903 return G.takeError();
904
905 JD.addGenerator(std::move(*G));
906
907 return Error::success();
908}
909
912 if (!G)
913 return G.takeError();
914
915 JD.addGenerator(std::move(*G));
916
917 return Error::success();
918}
919
921 assert(TSM && "Can not add null module");
922
923 if (auto Err =
924 TSM.withModuleDo([&](Module &M) { return applyDataLayout(M); }))
925 return Err;
926
927 return InitHelperTransformLayer->add(std::move(RT), std::move(TSM));
928}
929
931 return addIRModule(JD.getDefaultResourceTracker(), std::move(TSM));
932}
933
935 std::unique_ptr<MemoryBuffer> Obj) {
936 assert(Obj && "Can not add null object");
937
938 return ObjTransformLayer->add(std::move(RT), std::move(Obj));
939}
940
941Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
942 return addObjectFile(JD.getDefaultResourceTracker(), std::move(Obj));
943}
944
947 if (auto Sym = ES->lookup(
949 Name))
950 return Sym->getAddress();
951 else
952 return Sym.takeError();
953}
954
957
958 // If the config state provided an ObjectLinkingLayer factory then use it.
960 return S.CreateObjectLinkingLayer(ES, S.JTMB->getTargetTriple());
961
962 // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
963 // a new SectionMemoryManager for each object.
964 auto GetMemMgr = []() { return std::make_unique<SectionMemoryManager>(); };
965 auto Layer =
966 std::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr));
967
968 if (S.JTMB->getTargetTriple().isOSBinFormatCOFF()) {
969 Layer->setOverrideObjectFlagsWithResponsibilityFlags(true);
970 Layer->setAutoClaimResponsibilityForObjectSymbols(true);
971 }
972
973 if (S.JTMB->getTargetTriple().isOSBinFormatELF() &&
974 (S.JTMB->getTargetTriple().getArch() == Triple::ArchType::ppc64 ||
975 S.JTMB->getTargetTriple().getArch() == Triple::ArchType::ppc64le))
976 Layer->setAutoClaimResponsibilityForObjectSymbols(true);
977
978 // FIXME: Explicit conversion to std::unique_ptr<ObjectLayer> added to silence
979 // errors from some GCC / libstdc++ bots. Remove this conversion (i.e.
980 // just return ObjLinkingLayer) once those bots are upgraded.
981 return std::unique_ptr<ObjectLayer>(std::move(Layer));
982}
983
987
988 /// If there is a custom compile function creator set then use it.
990 return S.CreateCompileFunction(std::move(JTMB));
991
992 // If using a custom EPC then use a ConcurrentIRCompiler by default.
994 return std::make_unique<ConcurrentIRCompiler>(std::move(JTMB));
995
996 auto TM = JTMB.createTargetMachine();
997 if (!TM)
998 return TM.takeError();
999
1000 return std::make_unique<TMOwningSimpleCompiler>(std::move(*TM));
1001}
1002
1004 : DL(std::move(*S.DL)), TT(S.JTMB->getTargetTriple()) {
1005
1007
1008 assert(!(S.EPC && S.ES) && "EPC and ES should not both be set");
1009
1010 if (S.EPC) {
1011 ES = std::make_unique<ExecutionSession>(std::move(S.EPC));
1012 } else if (S.ES)
1013 ES = std::move(S.ES);
1014 else {
1015 if (auto EPC = SelfExecutorProcessControl::Create()) {
1016 ES = std::make_unique<ExecutionSession>(std::move(*EPC));
1017 } else {
1018 Err = EPC.takeError();
1019 return;
1020 }
1021 }
1022
1023 auto ObjLayer = createObjectLinkingLayer(S, *ES);
1024 if (!ObjLayer) {
1025 Err = ObjLayer.takeError();
1026 return;
1027 }
1028 ObjLinkingLayer = std::move(*ObjLayer);
1030 std::make_unique<ObjectTransformLayer>(*ES, *ObjLinkingLayer);
1031
1032 {
1033 auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB));
1034 if (!CompileFunction) {
1035 Err = CompileFunction.takeError();
1036 return;
1037 }
1038 CompileLayer = std::make_unique<IRCompileLayer>(
1039 *ES, *ObjTransformLayer, std::move(*CompileFunction));
1040 TransformLayer = std::make_unique<IRTransformLayer>(*ES, *CompileLayer);
1042 std::make_unique<IRTransformLayer>(*ES, *TransformLayer);
1043 }
1044
1046 InitHelperTransformLayer->setCloneToNewContextOnEmit(true);
1047
1049 if (auto ProcSymsJD = S.SetupProcessSymbolsJITDylib(*this)) {
1050 ProcessSymbols = ProcSymsJD->get();
1051 } else {
1052 Err = ProcSymsJD.takeError();
1053 return;
1054 }
1055 }
1056
1057 if (S.PrePlatformSetup)
1058 if ((Err = S.PrePlatformSetup(*this)))
1059 return;
1060
1061 if (!S.SetUpPlatform)
1063
1064 if (auto PlatformJDOrErr = S.SetUpPlatform(*this)) {
1065 Platform = PlatformJDOrErr->get();
1066 if (Platform)
1067 DefaultLinks.push_back(
1069 } else {
1070 Err = PlatformJDOrErr.takeError();
1071 return;
1072 }
1073
1075 DefaultLinks.push_back(
1077
1078 if (auto MainOrErr = createJITDylib("main"))
1079 Main = &*MainOrErr;
1080 else {
1081 Err = MainOrErr.takeError();
1082 return;
1083 }
1084}
1085
1086std::string LLJIT::mangle(StringRef UnmangledName) const {
1087 std::string MangledName;
1088 {
1089 raw_string_ostream MangledNameStream(MangledName);
1090 Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL);
1091 }
1092 return MangledName;
1093}
1094
1096 if (M.getDataLayout().isDefault())
1097 M.setDataLayout(DL);
1098
1099 if (M.getDataLayout() != DL)
1100 return make_error<StringError>(
1101 "Added modules have incompatible data layouts: " +
1102 M.getDataLayout().getStringRepresentation() + " (module) vs " +
1103 DL.getStringRepresentation() + " (jit)",
1105
1106 return Error::success();
1107}
1108
1110 LLVM_DEBUG({ dbgs() << "Setting up orc platform support for LLJIT\n"; });
1111 J.setPlatformSupport(std::make_unique<ORCPlatformSupport>(J));
1112 return Error::success();
1113}
1114
1116public:
1119 if (!DLLName.ends_with_insensitive(".dll"))
1120 return make_error<StringError>("DLLName not ending with .dll",
1122 auto DLLNameStr = DLLName.str(); // Guarantees null-termination.
1123 auto DLLJD = J.loadPlatformDynamicLibrary(DLLNameStr.c_str());
1124 if (!DLLJD)
1125 return DLLJD.takeError();
1126 JD.addToLinkOrder(*DLLJD);
1127 return Error::success();
1128 }
1129
1130private:
1131 LLJIT &J;
1132};
1133
1135 auto ProcessSymbolsJD = J.getProcessSymbolsJITDylib();
1136 if (!ProcessSymbolsJD)
1137 return make_error<StringError>(
1138 "Native platforms require a process symbols JITDylib",
1140
1141 const Triple &TT = J.getTargetTriple();
1142 ObjectLinkingLayer *ObjLinkingLayer =
1143 dyn_cast<ObjectLinkingLayer>(&J.getObjLinkingLayer());
1144
1145 if (!ObjLinkingLayer)
1146 return make_error<StringError>(
1147 "ExecutorNativePlatform requires ObjectLinkingLayer",
1149
1150 std::unique_ptr<MemoryBuffer> RuntimeArchiveBuffer;
1151 if (OrcRuntime.index() == 0) {
1152 auto A = errorOrToExpected(MemoryBuffer::getFile(std::get<0>(OrcRuntime)));
1153 if (!A)
1154 return A.takeError();
1155 RuntimeArchiveBuffer = std::move(*A);
1156 } else
1157 RuntimeArchiveBuffer = std::move(std::get<1>(OrcRuntime));
1158
1159 auto &ES = J.getExecutionSession();
1160 auto &PlatformJD = ES.createBareJITDylib("<Platform>");
1161 PlatformJD.addToLinkOrder(*ProcessSymbolsJD);
1162
1163 J.setPlatformSupport(std::make_unique<ORCPlatformSupport>(J));
1164
1165 switch (TT.getObjectFormat()) {
1166 case Triple::COFF: {
1167 const char *VCRuntimePath = nullptr;
1168 bool StaticVCRuntime = false;
1169 if (VCRuntime) {
1170 VCRuntimePath = VCRuntime->first.c_str();
1171 StaticVCRuntime = VCRuntime->second;
1172 }
1173 if (auto P = COFFPlatform::Create(
1174 *ObjLinkingLayer, PlatformJD, std::move(RuntimeArchiveBuffer),
1175 LoadAndLinkDynLibrary(J), StaticVCRuntime, VCRuntimePath))
1176 J.getExecutionSession().setPlatform(std::move(*P));
1177 else
1178 return P.takeError();
1179 break;
1180 }
1181 case Triple::ELF: {
1183 *ObjLinkingLayer, std::move(RuntimeArchiveBuffer));
1184 if (!G)
1185 return G.takeError();
1186
1187 if (auto P =
1188 ELFNixPlatform::Create(*ObjLinkingLayer, PlatformJD, std::move(*G)))
1189 J.getExecutionSession().setPlatform(std::move(*P));
1190 else
1191 return P.takeError();
1192 break;
1193 }
1194 case Triple::MachO: {
1196 *ObjLinkingLayer, std::move(RuntimeArchiveBuffer));
1197 if (!G)
1198 return G.takeError();
1199
1200 if (auto P =
1201 MachOPlatform::Create(*ObjLinkingLayer, PlatformJD, std::move(*G)))
1202 ES.setPlatform(std::move(*P));
1203 else
1204 return P.takeError();
1205 break;
1206 }
1207 default:
1208 return make_error<StringError>("Unsupported object format in triple " +
1209 TT.str(),
1211 }
1212
1213 return &PlatformJD;
1214}
1215
1217 LLVM_DEBUG(
1218 { dbgs() << "Setting up GenericLLVMIRPlatform support for LLJIT\n"; });
1219 auto ProcessSymbolsJD = J.getProcessSymbolsJITDylib();
1220 if (!ProcessSymbolsJD)
1221 return make_error<StringError>(
1222 "Native platforms require a process symbols JITDylib",
1224
1225 auto &PlatformJD = J.getExecutionSession().createBareJITDylib("<Platform>");
1226 PlatformJD.addToLinkOrder(*ProcessSymbolsJD);
1227
1229 std::make_unique<GenericLLVMIRPlatformSupport>(J, PlatformJD));
1230
1231 return &PlatformJD;
1232}
1233
1235 LLVM_DEBUG(
1236 { dbgs() << "Explicitly deactivated platform support for LLJIT\n"; });
1237 J.setPlatformSupport(std::make_unique<InactivePlatformSupport>());
1238 return nullptr;
1239}
1240
1243 return Err;
1244 TT = JTMB->getTargetTriple();
1245 return Error::success();
1246}
1247
1249 assert(TSM && "Can not add null module");
1250
1251 if (auto Err = TSM.withModuleDo(
1252 [&](Module &M) -> Error { return applyDataLayout(M); }))
1253 return Err;
1254
1255 return CODLayer->add(JD, std::move(TSM));
1256}
1257
1258LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
1259
1260 // If LLJIT construction failed then bail out.
1261 if (Err)
1262 return;
1263
1264 ErrorAsOutParameter _(&Err);
1265
1266 /// Take/Create the lazy-compile callthrough manager.
1267 if (S.LCTMgr)
1268 LCTMgr = std::move(S.LCTMgr);
1269 else {
1270 if (auto LCTMgrOrErr = createLocalLazyCallThroughManager(
1272 LCTMgr = std::move(*LCTMgrOrErr);
1273 else {
1274 Err = LCTMgrOrErr.takeError();
1275 return;
1276 }
1277 }
1278
1279 // Take/Create the indirect stubs manager builder.
1280 auto ISMBuilder = std::move(S.ISMBuilder);
1281
1282 // If none was provided, try to build one.
1283 if (!ISMBuilder)
1285
1286 // No luck. Bail out.
1287 if (!ISMBuilder) {
1288 Err = make_error<StringError>("Could not construct "
1289 "IndirectStubsManagerBuilder for target " +
1290 S.TT.str(),
1292 return;
1293 }
1294
1295 // Create the IP Layer.
1296 IPLayer = std::make_unique<IRPartitionLayer>(*ES, *InitHelperTransformLayer);
1297
1298 // Create the COD layer.
1299 CODLayer = std::make_unique<CompileOnDemandLayer>(*ES, *IPLayer, *LCTMgr,
1300 std::move(ISMBuilder));
1301
1303 CODLayer->setCloneToNewContextOnEmit(true);
1304}
1305
1306// In-process LLJIT uses eh-frame section wrappers via EPC, so we need to force
1307// them to be linked in.
1311}
1312
1313} // End namespace orc.
1314} // 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:1006
@ 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:752
const std::string & str() const
Definition: Triple.h:462
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:747
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:1134
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:956
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:1003
Error addObjectFile(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:934
Expected< JITDylib & > createJITDylib(std::string Name)
Create a new JITDylib with the given name and return a reference to it.
Definition: LLJIT.cpp:876
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:1216
virtual ~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:867
std::string mangle(StringRef UnmangledName) const
Returns a linker-mangled version of UnmangledName.
Definition: LLJIT.cpp:1086
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:874
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:885
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:872
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:945
static Expected< std::unique_ptr< IRCompileLayer::IRCompiler > > createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB)
Definition: LLJIT.cpp:985
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:898
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:1095
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:920
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:1248
Error operator()(JITDylib &JD, StringRef DLLName)
Definition: LLJIT.cpp:1118
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:1234
LLVM_ATTRIBUTE_USED void linkComponents()
Definition: LLJIT.cpp:1308
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:1216
Error setUpOrcPlatformManually(LLJIT &J)
Configure the LLJIT instance to use orc runtime support.
Definition: LLJIT.cpp:1109
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