LLVM 21.0.0git
LLJIT.cpp
Go to the documentation of this file.
1//===--------- LLJIT.cpp - An ORC-based JIT for compiling LLVM IR ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
12#include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_THREADS
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 for (auto *T : WrapperFnType->params())
62 HelperArgTypes.push_back(T);
63 auto *HelperFnType =
64 FunctionType::get(WrapperFnType->getReturnType(), HelperArgTypes, false);
65 auto *HelperFn = Function::Create(HelperFnType, GlobalValue::ExternalLinkage,
66 HelperName, M);
67
68 auto *WrapperFn = Function::Create(
69 WrapperFnType, GlobalValue::ExternalLinkage, WrapperName, M);
70 WrapperFn->setVisibility(WrapperVisibility);
71
72 auto *EntryBlock = BasicBlock::Create(M.getContext(), "entry", WrapperFn);
73 IRBuilder<> IB(EntryBlock);
74
75 std::vector<Value *> HelperArgs;
76 for (auto *Arg : HelperPrefixArgs)
77 HelperArgs.push_back(Arg);
78 for (auto &Arg : WrapperFn->args())
79 HelperArgs.push_back(&Arg);
80 auto *HelperResult = IB.CreateCall(HelperFn, HelperArgs);
81 if (HelperFn->getReturnType()->isVoidTy())
82 IB.CreateRetVoid();
83 else
84 IB.CreateRet(HelperResult);
85
86 return WrapperFn;
87}
88
89class GenericLLVMIRPlatformSupport;
90
91/// orc::Platform component of Generic LLVM IR Platform support.
92/// Just forwards calls to the GenericLLVMIRPlatformSupport class below.
93class GenericLLVMIRPlatform : public Platform {
94public:
95 GenericLLVMIRPlatform(GenericLLVMIRPlatformSupport &S) : S(S) {}
96 Error setupJITDylib(JITDylib &JD) override;
97 Error teardownJITDylib(JITDylib &JD) override;
99 const MaterializationUnit &MU) override;
100 Error notifyRemoving(ResourceTracker &RT) override {
101 // Noop -- Nothing to do (yet).
102 return Error::success();
103 }
104
105private:
106 GenericLLVMIRPlatformSupport &S;
107};
108
109/// This transform parses llvm.global_ctors to produce a single initialization
110/// function for the module, records the function, then deletes
111/// llvm.global_ctors.
112class GlobalCtorDtorScraper {
113public:
114 GlobalCtorDtorScraper(GenericLLVMIRPlatformSupport &PS,
115 StringRef InitFunctionPrefix,
116 StringRef DeInitFunctionPrefix)
117 : PS(PS), InitFunctionPrefix(InitFunctionPrefix),
118 DeInitFunctionPrefix(DeInitFunctionPrefix) {}
121
122private:
123 GenericLLVMIRPlatformSupport &PS;
124 StringRef InitFunctionPrefix;
125 StringRef DeInitFunctionPrefix;
126};
127
128/// Generic IR Platform Support
129///
130/// Scrapes llvm.global_ctors and llvm.global_dtors and replaces them with
131/// specially named 'init' and 'deinit'. Injects definitions / interposes for
132/// some runtime API, including __cxa_atexit, dlopen, and dlclose.
133class GenericLLVMIRPlatformSupport : public LLJIT::PlatformSupport {
134public:
135 GenericLLVMIRPlatformSupport(LLJIT &J, JITDylib &PlatformJD)
136 : J(J), InitFunctionPrefix(J.mangle("__orc_init_func.")),
137 DeInitFunctionPrefix(J.mangle("__orc_deinit_func.")) {
138
140 std::make_unique<GenericLLVMIRPlatform>(*this));
141
142 setInitTransform(J, GlobalCtorDtorScraper(*this, InitFunctionPrefix,
143 DeInitFunctionPrefix));
144
145 SymbolMap StdInterposes;
146
147 StdInterposes[J.mangleAndIntern("__lljit.platform_support_instance")] = {
149 StdInterposes[J.mangleAndIntern("__lljit.cxa_atexit_helper")] = {
150 ExecutorAddr::fromPtr(registerCxaAtExitHelper), JITSymbolFlags()};
151
152 cantFail(PlatformJD.define(absoluteSymbols(std::move(StdInterposes))));
153 cantFail(setupJITDylib(PlatformJD));
154 cantFail(J.addIRModule(PlatformJD, createPlatformRuntimeModule()));
155 }
156
158
159 /// Adds a module that defines the __dso_handle global.
160 Error setupJITDylib(JITDylib &JD) {
161
162 // Add per-jitdylib standard interposes.
163 SymbolMap PerJDInterposes;
164 PerJDInterposes[J.mangleAndIntern("__lljit.run_atexits_helper")] = {
165 ExecutorAddr::fromPtr(runAtExitsHelper), JITSymbolFlags()};
166 PerJDInterposes[J.mangleAndIntern("__lljit.atexit_helper")] = {
167 ExecutorAddr::fromPtr(registerAtExitHelper), JITSymbolFlags()};
168 cantFail(JD.define(absoluteSymbols(std::move(PerJDInterposes))));
169
170 auto Ctx = std::make_unique<LLVMContext>();
171 auto M = std::make_unique<Module>("__standard_lib", *Ctx);
172 M->setDataLayout(J.getDataLayout());
173
174 auto *Int64Ty = Type::getInt64Ty(*Ctx);
175 auto *DSOHandle = new GlobalVariable(
176 *M, Int64Ty, true, GlobalValue::ExternalLinkage,
177 ConstantInt::get(Int64Ty, reinterpret_cast<uintptr_t>(&JD)),
178 "__dso_handle");
179 DSOHandle->setVisibility(GlobalValue::DefaultVisibility);
180 DSOHandle->setInitializer(
181 ConstantInt::get(Int64Ty, ExecutorAddr::fromPtr(&JD).getValue()));
182
183 auto *GenericIRPlatformSupportTy =
184 StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport");
185
186 auto *PlatformInstanceDecl = new GlobalVariable(
187 *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage,
188 nullptr, "__lljit.platform_support_instance");
189
190 auto *VoidTy = Type::getVoidTy(*Ctx);
191 addHelperAndWrapper(
192 *M, "__lljit_run_atexits", FunctionType::get(VoidTy, {}, false),
193 GlobalValue::HiddenVisibility, "__lljit.run_atexits_helper",
194 {PlatformInstanceDecl, DSOHandle});
195
196 auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT);
197 auto *AtExitCallbackPtrTy = PointerType::getUnqual(*Ctx);
198 auto *AtExit = addHelperAndWrapper(
199 *M, "atexit", FunctionType::get(IntTy, {AtExitCallbackPtrTy}, false),
200 GlobalValue::HiddenVisibility, "__lljit.atexit_helper",
201 {PlatformInstanceDecl, DSOHandle});
202 Attribute::AttrKind AtExitExtAttr =
203 TargetLibraryInfo::getExtAttrForI32Return(J.getTargetTriple());
204 if (AtExitExtAttr != Attribute::None)
205 AtExit->addRetAttr(AtExitExtAttr);
206
207 return J.addIRModule(JD, ThreadSafeModule(std::move(M), std::move(Ctx)));
208 }
209
210 Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU) {
211 auto &JD = RT.getJITDylib();
212 if (auto &InitSym = MU.getInitializerSymbol())
213 InitSymbols[&JD].add(InitSym, SymbolLookupFlags::WeaklyReferencedSymbol);
214 else {
215 // If there's no identified init symbol attached, but there is a symbol
216 // with the GenericIRPlatform::InitFunctionPrefix, then treat that as
217 // an init function. Add the symbol to both the InitSymbols map (which
218 // will trigger a lookup to materialize the module) and the InitFunctions
219 // map (which holds the names of the symbols to execute).
220 for (auto &KV : MU.getSymbols())
221 if ((*KV.first).starts_with(InitFunctionPrefix)) {
222 InitSymbols[&JD].add(KV.first,
223 SymbolLookupFlags::WeaklyReferencedSymbol);
224 InitFunctions[&JD].add(KV.first);
225 } else if ((*KV.first).starts_with(DeInitFunctionPrefix)) {
226 DeInitFunctions[&JD].add(KV.first);
227 }
228 }
229 return Error::success();
230 }
231
232 Error initialize(JITDylib &JD) override {
233 LLVM_DEBUG({
234 dbgs() << "GenericLLVMIRPlatformSupport getting initializers to run\n";
235 });
236 if (auto Initializers = getInitializers(JD)) {
238 { dbgs() << "GenericLLVMIRPlatformSupport running initializers\n"; });
239 for (auto InitFnAddr : *Initializers) {
240 LLVM_DEBUG({
241 dbgs() << " Running init " << formatv("{0:x16}", InitFnAddr)
242 << "...\n";
243 });
244 auto *InitFn = InitFnAddr.toPtr<void (*)()>();
245 InitFn();
246 }
247 } else
248 return Initializers.takeError();
249 return Error::success();
250 }
251
252 Error deinitialize(JITDylib &JD) override {
253 LLVM_DEBUG({
254 dbgs() << "GenericLLVMIRPlatformSupport getting deinitializers to run\n";
255 });
256 if (auto Deinitializers = getDeinitializers(JD)) {
257 LLVM_DEBUG({
258 dbgs() << "GenericLLVMIRPlatformSupport running deinitializers\n";
259 });
260 for (auto DeinitFnAddr : *Deinitializers) {
261 LLVM_DEBUG({
262 dbgs() << " Running deinit " << formatv("{0:x16}", DeinitFnAddr)
263 << "...\n";
264 });
265 auto *DeinitFn = DeinitFnAddr.toPtr<void (*)()>();
266 DeinitFn();
267 }
268 } else
269 return Deinitializers.takeError();
270
271 return Error::success();
272 }
273
274 void registerInitFunc(JITDylib &JD, SymbolStringPtr InitName) {
276 InitFunctions[&JD].add(InitName);
277 });
278 }
279
280 void registerDeInitFunc(JITDylib &JD, SymbolStringPtr DeInitName) {
282 [&]() { DeInitFunctions[&JD].add(DeInitName); });
283 }
284
285private:
286 Expected<std::vector<ExecutorAddr>> getInitializers(JITDylib &JD) {
287 if (auto Err = issueInitLookups(JD))
288 return std::move(Err);
289
291 std::vector<JITDylibSP> DFSLinkOrder;
292
293 if (auto Err = getExecutionSession().runSessionLocked([&]() -> Error {
294 if (auto DFSLinkOrderOrErr = JD.getDFSLinkOrder())
295 DFSLinkOrder = std::move(*DFSLinkOrderOrErr);
296 else
297 return DFSLinkOrderOrErr.takeError();
298
299 for (auto &NextJD : DFSLinkOrder) {
300 auto IFItr = InitFunctions.find(NextJD.get());
301 if (IFItr != InitFunctions.end()) {
302 LookupSymbols[NextJD.get()] = std::move(IFItr->second);
303 InitFunctions.erase(IFItr);
304 }
305 }
306 return Error::success();
307 }))
308 return std::move(Err);
309
310 LLVM_DEBUG({
311 dbgs() << "JITDylib init order is [ ";
312 for (auto &JD : llvm::reverse(DFSLinkOrder))
313 dbgs() << "\"" << JD->getName() << "\" ";
314 dbgs() << "]\n";
315 dbgs() << "Looking up init functions:\n";
316 for (auto &KV : LookupSymbols)
317 dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n";
318 });
319
320 auto &ES = getExecutionSession();
321 auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols);
322
323 if (!LookupResult)
324 return LookupResult.takeError();
325
326 std::vector<ExecutorAddr> Initializers;
327 while (!DFSLinkOrder.empty()) {
328 auto &NextJD = *DFSLinkOrder.back();
329 DFSLinkOrder.pop_back();
330 auto InitsItr = LookupResult->find(&NextJD);
331 if (InitsItr == LookupResult->end())
332 continue;
333 for (auto &KV : InitsItr->second)
334 Initializers.push_back(KV.second.getAddress());
335 }
336
337 return Initializers;
338 }
339
340 Expected<std::vector<ExecutorAddr>> getDeinitializers(JITDylib &JD) {
341 auto &ES = getExecutionSession();
342
343 auto LLJITRunAtExits = J.mangleAndIntern("__lljit_run_atexits");
344
346 std::vector<JITDylibSP> DFSLinkOrder;
347
348 if (auto Err = ES.runSessionLocked([&]() -> Error {
349 if (auto DFSLinkOrderOrErr = JD.getDFSLinkOrder())
350 DFSLinkOrder = std::move(*DFSLinkOrderOrErr);
351 else
352 return DFSLinkOrderOrErr.takeError();
353
354 for (auto &NextJD : DFSLinkOrder) {
355 auto &JDLookupSymbols = LookupSymbols[NextJD.get()];
356 auto DIFItr = DeInitFunctions.find(NextJD.get());
357 if (DIFItr != DeInitFunctions.end()) {
358 LookupSymbols[NextJD.get()] = std::move(DIFItr->second);
359 DeInitFunctions.erase(DIFItr);
360 }
361 JDLookupSymbols.add(LLJITRunAtExits,
362 SymbolLookupFlags::WeaklyReferencedSymbol);
363 }
364 return Error::success();
365 }))
366 return std::move(Err);
367
368 LLVM_DEBUG({
369 dbgs() << "JITDylib deinit order is [ ";
370 for (auto &JD : DFSLinkOrder)
371 dbgs() << "\"" << JD->getName() << "\" ";
372 dbgs() << "]\n";
373 dbgs() << "Looking up deinit functions:\n";
374 for (auto &KV : LookupSymbols)
375 dbgs() << " \"" << KV.first->getName() << "\": " << KV.second << "\n";
376 });
377
378 auto LookupResult = Platform::lookupInitSymbols(ES, LookupSymbols);
379
380 if (!LookupResult)
381 return LookupResult.takeError();
382
383 std::vector<ExecutorAddr> DeInitializers;
384 for (auto &NextJD : DFSLinkOrder) {
385 auto DeInitsItr = LookupResult->find(NextJD.get());
386 assert(DeInitsItr != LookupResult->end() &&
387 "Every JD should have at least __lljit_run_atexits");
388
389 auto RunAtExitsItr = DeInitsItr->second.find(LLJITRunAtExits);
390 if (RunAtExitsItr != DeInitsItr->second.end())
391 DeInitializers.push_back(RunAtExitsItr->second.getAddress());
392
393 for (auto &KV : DeInitsItr->second)
394 if (KV.first != LLJITRunAtExits)
395 DeInitializers.push_back(KV.second.getAddress());
396 }
397
398 return DeInitializers;
399 }
400
401 /// Issue lookups for all init symbols required to initialize JD (and any
402 /// JITDylibs that it depends on).
403 Error issueInitLookups(JITDylib &JD) {
404 DenseMap<JITDylib *, SymbolLookupSet> RequiredInitSymbols;
405 std::vector<JITDylibSP> DFSLinkOrder;
406
407 if (auto Err = getExecutionSession().runSessionLocked([&]() -> Error {
408 if (auto DFSLinkOrderOrErr = JD.getDFSLinkOrder())
409 DFSLinkOrder = std::move(*DFSLinkOrderOrErr);
410 else
411 return DFSLinkOrderOrErr.takeError();
412
413 for (auto &NextJD : DFSLinkOrder) {
414 auto ISItr = InitSymbols.find(NextJD.get());
415 if (ISItr != InitSymbols.end()) {
416 RequiredInitSymbols[NextJD.get()] = std::move(ISItr->second);
417 InitSymbols.erase(ISItr);
418 }
419 }
420 return Error::success();
421 }))
422 return Err;
423
424 return Platform::lookupInitSymbols(getExecutionSession(),
425 RequiredInitSymbols)
426 .takeError();
427 }
428
429 static void registerCxaAtExitHelper(void *Self, void (*F)(void *), void *Ctx,
430 void *DSOHandle) {
431 LLVM_DEBUG({
432 dbgs() << "Registering cxa atexit function " << (void *)F << " for JD "
433 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
434 });
435 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.registerAtExit(
436 F, Ctx, DSOHandle);
437 }
438
439 static void registerAtExitHelper(void *Self, void *DSOHandle, void (*F)()) {
440 LLVM_DEBUG({
441 dbgs() << "Registering atexit function " << (void *)F << " for JD "
442 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
443 });
444 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.registerAtExit(
445 reinterpret_cast<void (*)(void *)>(F), nullptr, DSOHandle);
446 }
447
448 static void runAtExitsHelper(void *Self, void *DSOHandle) {
449 LLVM_DEBUG({
450 dbgs() << "Running atexit functions for JD "
451 << (*static_cast<JITDylib **>(DSOHandle))->getName() << "\n";
452 });
453 static_cast<GenericLLVMIRPlatformSupport *>(Self)->AtExitMgr.runAtExits(
454 DSOHandle);
455 }
456
457 // Constructs an LLVM IR module containing platform runtime globals,
458 // functions, and interposes.
459 ThreadSafeModule createPlatformRuntimeModule() {
460 auto Ctx = std::make_unique<LLVMContext>();
461 auto M = std::make_unique<Module>("__standard_lib", *Ctx);
462 M->setDataLayout(J.getDataLayout());
463
464 auto *GenericIRPlatformSupportTy =
465 StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport");
466
467 auto *PlatformInstanceDecl = new GlobalVariable(
468 *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage,
469 nullptr, "__lljit.platform_support_instance");
470
471 auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT);
472 auto *BytePtrTy = PointerType::getUnqual(*Ctx);
473 auto *CxaAtExitCallbackPtrTy = PointerType::getUnqual(*Ctx);
474
475 auto *CxaAtExit = addHelperAndWrapper(
476 *M, "__cxa_atexit",
477 FunctionType::get(IntTy, {CxaAtExitCallbackPtrTy, BytePtrTy, BytePtrTy},
478 false),
479 GlobalValue::DefaultVisibility, "__lljit.cxa_atexit_helper",
480 {PlatformInstanceDecl});
481 Attribute::AttrKind CxaAtExitExtAttr =
482 TargetLibraryInfo::getExtAttrForI32Return(J.getTargetTriple());
483 if (CxaAtExitExtAttr != Attribute::None)
484 CxaAtExit->addRetAttr(CxaAtExitExtAttr);
485
486 return ThreadSafeModule(std::move(M), std::move(Ctx));
487 }
488
489 LLJIT &J;
490 std::string InitFunctionPrefix;
491 std::string DeInitFunctionPrefix;
495 ItaniumCXAAtExitSupport AtExitMgr;
496};
497
498Error GenericLLVMIRPlatform::setupJITDylib(JITDylib &JD) {
499 return S.setupJITDylib(JD);
500}
501
502Error GenericLLVMIRPlatform::teardownJITDylib(JITDylib &JD) {
503 return Error::success();
504}
505
506Error GenericLLVMIRPlatform::notifyAdding(ResourceTracker &RT,
507 const MaterializationUnit &MU) {
508 return S.notifyAdding(RT, MU);
509}
510
512GlobalCtorDtorScraper::operator()(ThreadSafeModule TSM,
514 auto Err = TSM.withModuleDo([&](Module &M) -> Error {
515 auto &Ctx = M.getContext();
516 auto *GlobalCtors = M.getNamedGlobal("llvm.global_ctors");
517 auto *GlobalDtors = M.getNamedGlobal("llvm.global_dtors");
518
519 auto RegisterCOrDtors = [&](GlobalVariable *GlobalCOrDtors,
520 bool isCtor) -> Error {
521 // If there's no llvm.global_c/dtor or it's just a decl then skip.
522 if (!GlobalCOrDtors || GlobalCOrDtors->isDeclaration())
523 return Error::success();
524 std::string InitOrDeInitFunctionName;
525 if (isCtor)
526 raw_string_ostream(InitOrDeInitFunctionName)
527 << InitFunctionPrefix << M.getModuleIdentifier();
528 else
529 raw_string_ostream(InitOrDeInitFunctionName)
530 << DeInitFunctionPrefix << M.getModuleIdentifier();
531
532 MangleAndInterner Mangle(PS.getExecutionSession(), M.getDataLayout());
533 auto InternedInitOrDeInitName = Mangle(InitOrDeInitFunctionName);
534 if (auto Err = R.defineMaterializing(
535 {{InternedInitOrDeInitName, JITSymbolFlags::Callable}}))
536 return Err;
537
538 auto *InitOrDeInitFunc = Function::Create(
539 FunctionType::get(Type::getVoidTy(Ctx), {}, false),
540 GlobalValue::ExternalLinkage, InitOrDeInitFunctionName, &M);
541 InitOrDeInitFunc->setVisibility(GlobalValue::HiddenVisibility);
542 std::vector<std::pair<Function *, unsigned>> InitsOrDeInits;
543 auto COrDtors = isCtor ? getConstructors(M) : getDestructors(M);
544
545 for (auto E : COrDtors)
546 InitsOrDeInits.push_back(std::make_pair(E.Func, E.Priority));
547 llvm::stable_sort(InitsOrDeInits, llvm::less_second());
548
549 auto *InitOrDeInitFuncEntryBlock =
550 BasicBlock::Create(Ctx, "entry", InitOrDeInitFunc);
551 IRBuilder<> IB(InitOrDeInitFuncEntryBlock);
552 for (auto &KV : InitsOrDeInits)
553 IB.CreateCall(KV.first);
554 IB.CreateRetVoid();
555
556 if (isCtor)
557 PS.registerInitFunc(R.getTargetJITDylib(), InternedInitOrDeInitName);
558 else
559 PS.registerDeInitFunc(R.getTargetJITDylib(), InternedInitOrDeInitName);
560
561 GlobalCOrDtors->eraseFromParent();
562 return Error::success();
563 };
564
565 if (auto Err = RegisterCOrDtors(GlobalCtors, true))
566 return Err;
567 if (auto Err = RegisterCOrDtors(GlobalDtors, false))
568 return Err;
569
570 return Error::success();
571 });
572
573 if (Err)
574 return std::move(Err);
575
576 return std::move(TSM);
577}
578
579/// Inactive Platform Support
580///
581/// Explicitly disables platform support. JITDylibs are not scanned for special
582/// init/deinit symbols. No runtime API interposes are injected.
583class InactivePlatformSupport : public LLJIT::PlatformSupport {
584public:
585 InactivePlatformSupport() = default;
586
587 Error initialize(JITDylib &JD) override {
588 LLVM_DEBUG(dbgs() << "InactivePlatformSupport: no initializers running for "
589 << JD.getName() << "\n");
590 return Error::success();
591 }
592
593 Error deinitialize(JITDylib &JD) override {
595 dbgs() << "InactivePlatformSupport: no deinitializers running for "
596 << JD.getName() << "\n");
597 return Error::success();
598 }
599};
600
601} // end anonymous namespace
602
603namespace llvm {
604namespace orc {
605
609 using SPSDLOpenSig = SPSExecutorAddr(SPSString, int32_t);
610 using SPSDLUpdateSig = int32_t(SPSExecutorAddr);
611 enum dlopen_mode : int32_t {
612 ORC_RT_RTLD_LAZY = 0x1,
613 ORC_RT_RTLD_NOW = 0x2,
614 ORC_RT_RTLD_LOCAL = 0x4,
615 ORC_RT_RTLD_GLOBAL = 0x8
616 };
617
618 auto &ES = J.getExecutionSession();
619 auto MainSearchOrder = J.getMainJITDylib().withLinkOrderDo(
620 [](const JITDylibSearchOrder &SO) { return SO; });
621 StringRef WrapperToCall = "__orc_rt_jit_dlopen_wrapper";
622 bool dlupdate = false;
623 const Triple &TT = ES.getTargetTriple();
624 if (TT.isOSBinFormatMachO() || TT.isOSBinFormatELF()) {
625 if (InitializedDylib.contains(&JD)) {
626 WrapperToCall = "__orc_rt_jit_dlupdate_wrapper";
627 dlupdate = true;
628 } else
629 InitializedDylib.insert(&JD);
630 }
631
632 if (auto WrapperAddr =
633 ES.lookup(MainSearchOrder, J.mangleAndIntern(WrapperToCall))) {
634 if (dlupdate) {
635 int32_t result;
636 auto E = ES.callSPSWrapper<SPSDLUpdateSig>(WrapperAddr->getAddress(),
637 result, DSOHandles[&JD]);
638 if (result)
639 return make_error<StringError>("dlupdate failed",
641 return E;
642 }
643 return ES.callSPSWrapper<SPSDLOpenSig>(WrapperAddr->getAddress(),
644 DSOHandles[&JD], JD.getName(),
645 int32_t(ORC_RT_RTLD_LAZY));
646 } else
647 return WrapperAddr.takeError();
648}
649
652 using SPSDLCloseSig = int32_t(SPSExecutorAddr);
653
654 auto &ES = J.getExecutionSession();
655 auto MainSearchOrder = J.getMainJITDylib().withLinkOrderDo(
656 [](const JITDylibSearchOrder &SO) { return SO; });
657
658 if (auto WrapperAddr = ES.lookup(
659 MainSearchOrder, J.mangleAndIntern("__orc_rt_jit_dlclose_wrapper"))) {
660 int32_t result;
661 auto E = J.getExecutionSession().callSPSWrapper<SPSDLCloseSig>(
662 WrapperAddr->getAddress(), result, DSOHandles[&JD]);
663 if (E)
664 return E;
665 else if (result)
666 return make_error<StringError>("dlclose failed",
668 DSOHandles.erase(&JD);
669 InitializedDylib.erase(&JD);
670 } else
671 return WrapperAddr.takeError();
672 return Error::success();
673}
674
677 J.InitHelperTransformLayer->setTransform(std::move(T));
678}
679
681
683
684 LLVM_DEBUG(dbgs() << "Preparing to create LLJIT instance...\n");
685
686 if (!JTMB) {
687 LLVM_DEBUG({
688 dbgs() << " No explicitly set JITTargetMachineBuilder. "
689 "Detecting host...\n";
690 });
691 if (auto JTMBOrErr = JITTargetMachineBuilder::detectHost())
692 JTMB = std::move(*JTMBOrErr);
693 else
694 return JTMBOrErr.takeError();
695 }
696
697 if ((ES || EPC) && NumCompileThreads)
698 return make_error<StringError>(
699 "NumCompileThreads cannot be used with a custom ExecutionSession or "
700 "ExecutorProcessControl",
702
703#if !LLVM_ENABLE_THREADS
704 if (NumCompileThreads)
705 return make_error<StringError>(
706 "LLJIT num-compile-threads is " + Twine(NumCompileThreads) +
707 " but LLVM was compiled with LLVM_ENABLE_THREADS=Off",
709#endif // !LLVM_ENABLE_THREADS
710
711 // Only used in debug builds.
712 [[maybe_unused]] bool ConcurrentCompilationSettingDefaulted =
713 !SupportConcurrentCompilation;
714
715 if (!SupportConcurrentCompilation) {
716#if LLVM_ENABLE_THREADS
717 SupportConcurrentCompilation = NumCompileThreads || ES || EPC;
718#else
719 SupportConcurrentCompilation = false;
720#endif // LLVM_ENABLE_THREADS
721 } else {
722#if !LLVM_ENABLE_THREADS
723 if (*SupportConcurrentCompilation)
724 return make_error<StringError>(
725 "LLJIT concurrent compilation support requested, but LLVM was built "
726 "with LLVM_ENABLE_THREADS=Off",
728#endif // !LLVM_ENABLE_THREADS
729 }
730
731 LLVM_DEBUG({
732 dbgs() << " JITTargetMachineBuilder is "
733 << JITTargetMachineBuilderPrinter(*JTMB, " ")
734 << " Pre-constructed ExecutionSession: " << (ES ? "Yes" : "No")
735 << "\n"
736 << " DataLayout: ";
737 if (DL)
738 dbgs() << DL->getStringRepresentation() << "\n";
739 else
740 dbgs() << "None (will be created by JITTargetMachineBuilder)\n";
741
742 dbgs() << " Custom object-linking-layer creator: "
743 << (CreateObjectLinkingLayer ? "Yes" : "No") << "\n"
744 << " Custom compile-function creator: "
745 << (CreateCompileFunction ? "Yes" : "No") << "\n"
746 << " Custom platform-setup function: "
747 << (SetUpPlatform ? "Yes" : "No") << "\n"
748 << " Support concurrent compilation: "
749 << (*SupportConcurrentCompilation ? "Yes" : "No");
750 if (ConcurrentCompilationSettingDefaulted)
751 dbgs() << " (defaulted based on ES / EPC / NumCompileThreads)\n";
752 else
753 dbgs() << "\n";
754 dbgs() << " Number of compile threads: " << NumCompileThreads << "\n";
755 });
756
757 // Create DL if not specified.
758 if (!DL) {
759 if (auto DLOrErr = JTMB->getDefaultDataLayoutForTarget())
760 DL = std::move(*DLOrErr);
761 else
762 return DLOrErr.takeError();
763 }
764
765 // If neither ES nor EPC has been set then create an EPC instance.
766 if (!ES && !EPC) {
767 LLVM_DEBUG({
768 dbgs() << "ExecutorProcessControl not specified, "
769 "Creating SelfExecutorProcessControl instance\n";
770 });
771
772 std::unique_ptr<TaskDispatcher> D = nullptr;
773#if LLVM_ENABLE_THREADS
774 if (*SupportConcurrentCompilation) {
775 std::optional<size_t> NumThreads = std ::nullopt;
776 if (NumCompileThreads)
777 NumThreads = NumCompileThreads;
778 D = std::make_unique<DynamicThreadPoolTaskDispatcher>(NumThreads);
779 } else
780 D = std::make_unique<InPlaceTaskDispatcher>();
781#endif // LLVM_ENABLE_THREADS
782 if (auto EPCOrErr =
783 SelfExecutorProcessControl::Create(nullptr, std::move(D), nullptr))
784 EPC = std::move(*EPCOrErr);
785 else
786 return EPCOrErr.takeError();
787 } else if (EPC) {
788 LLVM_DEBUG({
789 dbgs() << "Using explicitly specified ExecutorProcessControl instance "
790 << EPC.get() << "\n";
791 });
792 } else {
793 LLVM_DEBUG({
794 dbgs() << "Using explicitly specified ExecutionSession instance "
795 << ES.get() << "\n";
796 });
797 }
798
799 // If the client didn't configure any linker options then auto-configure the
800 // JIT linker.
801 if (!CreateObjectLinkingLayer) {
802 auto &TT = JTMB->getTargetTriple();
803 bool UseJITLink = false;
804 switch (TT.getArch()) {
805 case Triple::riscv64:
807 UseJITLink = true;
808 break;
809 case Triple::aarch64:
810 UseJITLink = !TT.isOSBinFormatCOFF();
811 break;
812 case Triple::arm:
813 case Triple::armeb:
814 case Triple::thumb:
815 case Triple::thumbeb:
816 UseJITLink = TT.isOSBinFormatELF();
817 break;
818 case Triple::x86_64:
819 UseJITLink = !TT.isOSBinFormatCOFF();
820 break;
821 case Triple::ppc64:
822 UseJITLink = TT.isPPC64ELFv2ABI();
823 break;
824 case Triple::ppc64le:
825 UseJITLink = TT.isOSBinFormatELF();
826 break;
827 default:
828 break;
829 }
830 if (UseJITLink) {
831 if (!JTMB->getCodeModel())
832 JTMB->setCodeModel(CodeModel::Small);
833 JTMB->setRelocationModel(Reloc::PIC_);
834 CreateObjectLinkingLayer =
836 const Triple &) -> Expected<std::unique_ptr<ObjectLayer>> {
837 return std::make_unique<ObjectLinkingLayer>(ES);
838 };
839 }
840 }
841
842 // If we need a process JITDylib but no setup function has been given then
843 // create a default one.
844 if (!SetupProcessSymbolsJITDylib && LinkProcessSymbolsByDefault) {
845 LLVM_DEBUG(dbgs() << "Creating default Process JD setup function\n");
846 SetupProcessSymbolsJITDylib = [](LLJIT &J) -> Expected<JITDylibSP> {
847 auto &JD =
848 J.getExecutionSession().createBareJITDylib("<Process Symbols>");
850 J.getExecutionSession());
851 if (!G)
852 return G.takeError();
853 JD.addGenerator(std::move(*G));
854 return &JD;
855 };
856 }
857
858 return Error::success();
859}
860
862 if (auto Err = ES->endSession())
863 ES->reportError(std::move(Err));
864}
865
867
869
871 auto JD = ES->createJITDylib(std::move(Name));
872 if (!JD)
873 return JD.takeError();
874
876 return JD;
877}
878
881 if (!G)
882 return G.takeError();
883
884 if (auto *ExistingJD = ES->getJITDylibByName(Path))
885 return *ExistingJD;
886
887 auto &JD = ES->createBareJITDylib(Path);
888 JD.addGenerator(std::move(*G));
889 return JD;
890}
891
893 std::unique_ptr<MemoryBuffer> LibBuffer) {
895 std::move(LibBuffer));
896 if (!G)
897 return G.takeError();
898
899 JD.addGenerator(std::move(*G));
900
901 return Error::success();
902}
903
906 if (!G)
907 return G.takeError();
908
909 JD.addGenerator(std::move(*G));
910
911 return Error::success();
912}
913
915 assert(TSM && "Can not add null module");
916
917 if (auto Err =
918 TSM.withModuleDo([&](Module &M) { return applyDataLayout(M); }))
919 return Err;
920
921 return InitHelperTransformLayer->add(std::move(RT), std::move(TSM));
922}
923
925 return addIRModule(JD.getDefaultResourceTracker(), std::move(TSM));
926}
927
929 std::unique_ptr<MemoryBuffer> Obj) {
930 assert(Obj && "Can not add null object");
931
932 return ObjTransformLayer->add(std::move(RT), std::move(Obj));
933}
934
935Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
936 return addObjectFile(JD.getDefaultResourceTracker(), std::move(Obj));
937}
938
941 if (auto Sym = ES->lookup(
943 Name))
944 return Sym->getAddress();
945 else
946 return Sym.takeError();
947}
948
951
952 // If the config state provided an ObjectLinkingLayer factory then use it.
954 return S.CreateObjectLinkingLayer(ES, S.JTMB->getTargetTriple());
955
956 // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
957 // a new SectionMemoryManager for each object.
958 auto GetMemMgr = []() { return std::make_unique<SectionMemoryManager>(); };
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 auto ObjLayer = createObjectLinkingLayer(S, *ES);
1018 if (!ObjLayer) {
1019 Err = ObjLayer.takeError();
1020 return;
1021 }
1022 ObjLinkingLayer = std::move(*ObjLayer);
1024 std::make_unique<ObjectTransformLayer>(*ES, *ObjLinkingLayer);
1025
1026 {
1027 auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB));
1028 if (!CompileFunction) {
1029 Err = CompileFunction.takeError();
1030 return;
1031 }
1032 CompileLayer = std::make_unique<IRCompileLayer>(
1033 *ES, *ObjTransformLayer, std::move(*CompileFunction));
1034 TransformLayer = std::make_unique<IRTransformLayer>(*ES, *CompileLayer);
1036 std::make_unique<IRTransformLayer>(*ES, *TransformLayer);
1037 }
1038
1040 InitHelperTransformLayer->setCloneToNewContextOnEmit(true);
1041
1043 if (auto ProcSymsJD = S.SetupProcessSymbolsJITDylib(*this)) {
1044 ProcessSymbols = ProcSymsJD->get();
1045 } else {
1046 Err = ProcSymsJD.takeError();
1047 return;
1048 }
1049 }
1050
1051 if (S.PrePlatformSetup)
1052 if ((Err = S.PrePlatformSetup(*this)))
1053 return;
1054
1055 if (!S.SetUpPlatform)
1057
1058 if (auto PlatformJDOrErr = S.SetUpPlatform(*this)) {
1059 Platform = PlatformJDOrErr->get();
1060 if (Platform)
1061 DefaultLinks.push_back(
1063 } else {
1064 Err = PlatformJDOrErr.takeError();
1065 return;
1066 }
1067
1069 DefaultLinks.push_back(
1071
1072 if (auto MainOrErr = createJITDylib("main"))
1073 Main = &*MainOrErr;
1074 else {
1075 Err = MainOrErr.takeError();
1076 return;
1077 }
1078}
1079
1080std::string LLJIT::mangle(StringRef UnmangledName) const {
1081 std::string MangledName;
1082 {
1083 raw_string_ostream MangledNameStream(MangledName);
1084 Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL);
1085 }
1086 return MangledName;
1087}
1088
1090 if (M.getDataLayout().isDefault())
1091 M.setDataLayout(DL);
1092
1093 if (M.getDataLayout() != DL)
1094 return make_error<StringError>(
1095 "Added modules have incompatible data layouts: " +
1096 M.getDataLayout().getStringRepresentation() + " (module) vs " +
1097 DL.getStringRepresentation() + " (jit)",
1099
1100 return Error::success();
1101}
1102
1104 LLVM_DEBUG({ dbgs() << "Setting up orc platform support for LLJIT\n"; });
1105 J.setPlatformSupport(std::make_unique<ORCPlatformSupport>(J));
1106 return Error::success();
1107}
1108
1110public:
1113 if (!DLLName.ends_with_insensitive(".dll"))
1114 return make_error<StringError>("DLLName not ending with .dll",
1116 auto DLLNameStr = DLLName.str(); // Guarantees null-termination.
1117 auto DLLJD = J.loadPlatformDynamicLibrary(DLLNameStr.c_str());
1118 if (!DLLJD)
1119 return DLLJD.takeError();
1120 JD.addToLinkOrder(*DLLJD);
1121 return Error::success();
1122 }
1123
1124private:
1125 LLJIT &J;
1126};
1127
1129 auto ProcessSymbolsJD = J.getProcessSymbolsJITDylib();
1130 if (!ProcessSymbolsJD)
1131 return make_error<StringError>(
1132 "Native platforms require a process symbols JITDylib",
1134
1135 const Triple &TT = J.getTargetTriple();
1136 ObjectLinkingLayer *ObjLinkingLayer =
1137 dyn_cast<ObjectLinkingLayer>(&J.getObjLinkingLayer());
1138
1139 if (!ObjLinkingLayer)
1140 return make_error<StringError>(
1141 "ExecutorNativePlatform requires ObjectLinkingLayer",
1143
1144 std::unique_ptr<MemoryBuffer> RuntimeArchiveBuffer;
1145 if (OrcRuntime.index() == 0) {
1146 auto A = errorOrToExpected(MemoryBuffer::getFile(std::get<0>(OrcRuntime)));
1147 if (!A)
1148 return A.takeError();
1149 RuntimeArchiveBuffer = std::move(*A);
1150 } else
1151 RuntimeArchiveBuffer = std::move(std::get<1>(OrcRuntime));
1152
1153 auto &ES = J.getExecutionSession();
1154 auto &PlatformJD = ES.createBareJITDylib("<Platform>");
1155 PlatformJD.addToLinkOrder(*ProcessSymbolsJD);
1156
1157 J.setPlatformSupport(std::make_unique<ORCPlatformSupport>(J));
1158
1159 switch (TT.getObjectFormat()) {
1160 case Triple::COFF: {
1161 const char *VCRuntimePath = nullptr;
1162 bool StaticVCRuntime = false;
1163 if (VCRuntime) {
1164 VCRuntimePath = VCRuntime->first.c_str();
1165 StaticVCRuntime = VCRuntime->second;
1166 }
1167 if (auto P = COFFPlatform::Create(
1168 *ObjLinkingLayer, PlatformJD, std::move(RuntimeArchiveBuffer),
1169 LoadAndLinkDynLibrary(J), StaticVCRuntime, VCRuntimePath))
1170 J.getExecutionSession().setPlatform(std::move(*P));
1171 else
1172 return P.takeError();
1173 break;
1174 }
1175 case Triple::ELF: {
1177 *ObjLinkingLayer, std::move(RuntimeArchiveBuffer));
1178 if (!G)
1179 return G.takeError();
1180
1181 if (auto P =
1182 ELFNixPlatform::Create(*ObjLinkingLayer, PlatformJD, std::move(*G)))
1183 J.getExecutionSession().setPlatform(std::move(*P));
1184 else
1185 return P.takeError();
1186 break;
1187 }
1188 case Triple::MachO: {
1190 *ObjLinkingLayer, std::move(RuntimeArchiveBuffer));
1191 if (!G)
1192 return G.takeError();
1193
1194 if (auto P =
1195 MachOPlatform::Create(*ObjLinkingLayer, PlatformJD, std::move(*G)))
1196 ES.setPlatform(std::move(*P));
1197 else
1198 return P.takeError();
1199 break;
1200 }
1201 default:
1202 return make_error<StringError>("Unsupported object format in triple " +
1203 TT.str(),
1205 }
1206
1207 return &PlatformJD;
1208}
1209
1211 LLVM_DEBUG(
1212 { dbgs() << "Setting up GenericLLVMIRPlatform support for LLJIT\n"; });
1213 auto ProcessSymbolsJD = J.getProcessSymbolsJITDylib();
1214 if (!ProcessSymbolsJD)
1215 return make_error<StringError>(
1216 "Native platforms require a process symbols JITDylib",
1218
1219 auto &PlatformJD = J.getExecutionSession().createBareJITDylib("<Platform>");
1220 PlatformJD.addToLinkOrder(*ProcessSymbolsJD);
1221
1222 if (auto *OLL = dyn_cast<ObjectLinkingLayer>(&J.getObjLinkingLayer())) {
1223
1224 bool UseEHFrames = true;
1225
1226 // Enable compact-unwind support if possible.
1227 if (J.getTargetTriple().isOSDarwin() ||
1229
1230 // Check if the bootstrap map says that we should force eh-frames:
1231 // Older libunwinds require this as they don't have a dynamic
1232 // registration API for compact-unwind.
1233 std::optional<bool> ForceEHFrames;
1234 if (auto Err = J.getExecutionSession().getBootstrapMapValue<bool, bool>(
1235 "darwin-use-ehframes-only", ForceEHFrames))
1236 return Err;
1237 if (ForceEHFrames.has_value())
1238 UseEHFrames = *ForceEHFrames;
1239 else
1240 UseEHFrames = false;
1241
1242 // If UseEHFrames hasn't been set then we're good to use compact-unwind.
1243 if (!UseEHFrames) {
1244 if (auto UIRP =
1246 OLL->addPlugin(std::move(*UIRP));
1247 LLVM_DEBUG(dbgs() << "Enabled compact-unwind support.\n");
1248 } else
1249 return UIRP.takeError();
1250 }
1251 }
1252
1253 // Otherwise fall back to standard unwind registration.
1254 if (UseEHFrames) {
1255 auto &ES = J.getExecutionSession();
1256 if (auto EHFrameRegistrar = EPCEHFrameRegistrar::Create(ES)) {
1257 OLL->addPlugin(std::make_unique<EHFrameRegistrationPlugin>(
1258 ES, std::move(*EHFrameRegistrar)));
1259 LLVM_DEBUG(dbgs() << "Enabled eh-frame support.\n");
1260 } else
1261 return EHFrameRegistrar.takeError();
1262 }
1263 }
1264
1266 std::make_unique<GenericLLVMIRPlatformSupport>(J, PlatformJD));
1267
1268 return &PlatformJD;
1269}
1270
1272 LLVM_DEBUG(
1273 { dbgs() << "Explicitly deactivated platform support for LLJIT\n"; });
1274 J.setPlatformSupport(std::make_unique<InactivePlatformSupport>());
1275 return nullptr;
1276}
1277
1280 return Err;
1281 TT = JTMB->getTargetTriple();
1282 return Error::success();
1283}
1284
1286 assert(TSM && "Can not add null module");
1287
1288 if (auto Err = TSM.withModuleDo(
1289 [&](Module &M) -> Error { return applyDataLayout(M); }))
1290 return Err;
1291
1292 return CODLayer->add(JD, std::move(TSM));
1293}
1294
1295LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
1296
1297 // If LLJIT construction failed then bail out.
1298 if (Err)
1299 return;
1300
1301 ErrorAsOutParameter _(&Err);
1302
1303 /// Take/Create the lazy-compile callthrough manager.
1304 if (S.LCTMgr)
1305 LCTMgr = std::move(S.LCTMgr);
1306 else {
1307 if (auto LCTMgrOrErr = createLocalLazyCallThroughManager(
1309 LCTMgr = std::move(*LCTMgrOrErr);
1310 else {
1311 Err = LCTMgrOrErr.takeError();
1312 return;
1313 }
1314 }
1315
1316 // Take/Create the indirect stubs manager builder.
1317 auto ISMBuilder = std::move(S.ISMBuilder);
1318
1319 // If none was provided, try to build one.
1320 if (!ISMBuilder)
1322
1323 // No luck. Bail out.
1324 if (!ISMBuilder) {
1325 Err = make_error<StringError>("Could not construct "
1326 "IndirectStubsManagerBuilder for target " +
1327 S.TT.str(),
1329 return;
1330 }
1331
1332 // Create the IP Layer.
1333 IPLayer = std::make_unique<IRPartitionLayer>(*ES, *InitHelperTransformLayer);
1334
1335 // Create the COD layer.
1336 CODLayer = std::make_unique<CompileOnDemandLayer>(*ES, *IPLayer, *LCTMgr,
1337 std::move(ISMBuilder));
1338
1340 CODLayer->setCloneToNewContextOnEmit(true);
1341}
1342
1343// In-process LLJIT uses eh-frame section wrappers via EPC, so we need to force
1344// them to be linked in.
1348}
1349
1350} // End namespace orc.
1351} // 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:315
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:507
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:695
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:229
bool ends_with_insensitive(StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
Definition: StringRef.cpp:51
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:612
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isPPC64ELFv2ABI() const
Tests whether the target 64-bit PowerPC big endian ABI is ELFv2.
Definition: Triple.h:1012
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:766
@ loongarch64
Definition: Triple.h:62
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:395
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:758
const std::string & str() const
Definition: Triple.h:462
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, XROS, or DriverKit).
Definition: Triple.h:588
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:753
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
static Expected< std::unique_ptr< COFFPlatform > > Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< MemoryBuffer > OrcRuntimeArchiveBuffer, LoadDynamicLibrary LoadDynLibrary, bool StaticVCRuntime=false, const char *VCRuntimePath=nullptr, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a COFFPlatform instance, adding the ORC runtime to the given JITDylib.
static Expected< std::unique_ptr< ELFNixPlatform > > Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< DefinitionGenerator > OrcRuntime, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a ELFNixPlatform instance, adding the ORC runtime to the given JITDylib.
static Expected< std::unique_ptr< EPCDynamicLibrarySearchGenerator > > Load(ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow=SymbolPredicate(), AddAbsoluteSymbolsFn AddAbsoluteSymbols=nullptr)
Permanently loads the library at the given path and, on success, returns a DynamicLibrarySearchGenera...
static Expected< std::unique_ptr< EPCDynamicLibrarySearchGenerator > > GetForTargetProcess(ExecutionSession &ES, SymbolPredicate Allow=SymbolPredicate(), AddAbsoluteSymbolsFn AddAbsoluteSymbols=nullptr)
Creates a EPCDynamicLibrarySearchGenerator that searches for symbols in the target process.
static Expected< std::unique_ptr< EPCEHFrameRegistrar > > Create(ExecutionSession &ES)
Create from a ExecutorProcessControl instance alone.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1345
void setPlatform(std::unique_ptr< Platform > P)
Set the Platform for this ExecutionSession.
Definition: Core.h:1402
Error callSPSWrapper(ExecutorAddr WrapperFnAddr, WrapperCallArgTs &&...WrapperCallArgs)
Run a wrapper function using SPS to serialize the arguments and deserialize the results.
Definition: Core.h:1623
JITDylib & createBareJITDylib(std::string Name)
Add a new bare JITDylib to this ExecutionSession.
Definition: Core.cpp:1667
Error getBootstrapMapValue(StringRef Key, std::optional< T > &Val) const
Look up and SPS-deserialize a bootstrap map value.
Definition: Core.h:1566
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
Definition: Core.h:1409
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
Expected< JITDylibSP > operator()(LLJIT &J)
Definition: LLJIT.cpp:1128
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:1852
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:1725
auto withLinkOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchOrder & >()))
Do something with the link order (run under the session lock).
Definition: Core.h:1845
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:1835
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:682
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:675
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:950
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:997
Error addObjectFile(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:928
Expected< JITDylib & > createJITDylib(std::string Name)
Create a new JITDylib with the given name and return a reference to it.
Definition: LLJIT.cpp:870
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:1210
virtual ~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:861
std::string mangle(StringRef UnmangledName) const
Returns a linker-mangled version of UnmangledName.
Definition: LLJIT.cpp:1080
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:868
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:879
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:866
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:939
static Expected< std::unique_ptr< IRCompileLayer::IRCompiler > > createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB)
Definition: LLJIT.cpp:979
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:892
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:1089
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:914
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:1285
Error operator()(JITDylib &JD, StringRef DLLName)
Definition: LLJIT.cpp:1112
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:650
Error initialize(orc::JITDylib &JD) override
Definition: LLJIT.cpp:606
An ObjectLayer implementation built on JITLink.
Platforms set up standard symbols and mediate interactions between dynamic initializers (e....
Definition: Core.h:1273
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:1494
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.
static Expected< std::shared_ptr< UnwindInfoRegistrationPlugin > > Create(ExecutionSession &ES, ExecutorAddr Register, ExecutorAddr Deregister)
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:1271
LLVM_ATTRIBUTE_USED void linkComponents()
Definition: LLJIT.cpp:1345
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:1210
Error setUpOrcPlatformManually(LLJIT &J)
Configure the LLJIT instance to use orc runtime support.
Definition: LLJIT.cpp:1103
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