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