LLVM 20.0.0git
LLJIT.cpp
Go to the documentation of this file.
1//===--------- LLJIT.cpp - An ORC-based JIT for compiling LLVM IR ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
12#include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_THREADS
26#include "llvm/IR/IRBuilder.h"
27#include "llvm/IR/Mangler.h"
28#include "llvm/IR/Module.h"
30
31#define DEBUG_TYPE "orc"
32
33using namespace llvm;
34using namespace llvm::orc;
35
36namespace {
37
38/// Adds helper function decls and wrapper functions that call the helper with
39/// some additional prefix arguments.
40///
41/// E.g. For wrapper "foo" with type i8(i8, i64), helper "bar", and prefix
42/// args i32 4 and i16 12345, this function will add:
43///
44/// declare i8 @bar(i32, i16, i8, i64)
45///
46/// define i8 @foo(i8, i64) {
47/// entry:
48/// %2 = call i8 @bar(i32 4, i16 12345, i8 %0, i64 %1)
49/// ret i8 %2
50/// }
51///
52Function *addHelperAndWrapper(Module &M, StringRef WrapperName,
53 FunctionType *WrapperFnType,
54 GlobalValue::VisibilityTypes WrapperVisibility,
55 StringRef HelperName,
56 ArrayRef<Value *> HelperPrefixArgs) {
57 std::vector<Type *> HelperArgTypes;
58 for (auto *Arg : HelperPrefixArgs)
59 HelperArgTypes.push_back(Arg->getType());
60 for (auto *T : WrapperFnType->params())
61 HelperArgTypes.push_back(T);
62 auto *HelperFnType =
63 FunctionType::get(WrapperFnType->getReturnType(), HelperArgTypes, false);
64 auto *HelperFn = Function::Create(HelperFnType, GlobalValue::ExternalLinkage,
65 HelperName, M);
66
67 auto *WrapperFn = Function::Create(
68 WrapperFnType, GlobalValue::ExternalLinkage, WrapperName, M);
69 WrapperFn->setVisibility(WrapperVisibility);
70
71 auto *EntryBlock = BasicBlock::Create(M.getContext(), "entry", WrapperFn);
72 IRBuilder<> IB(EntryBlock);
73
74 std::vector<Value *> HelperArgs;
75 for (auto *Arg : HelperPrefixArgs)
76 HelperArgs.push_back(Arg);
77 for (auto &Arg : WrapperFn->args())
78 HelperArgs.push_back(&Arg);
79 auto *HelperResult = IB.CreateCall(HelperFn, HelperArgs);
80 if (HelperFn->getReturnType()->isVoidTy())
81 IB.CreateRetVoid();
82 else
83 IB.CreateRet(HelperResult);
84
85 return WrapperFn;
86}
87
88class GenericLLVMIRPlatformSupport;
89
90/// orc::Platform component of Generic LLVM IR Platform support.
91/// Just forwards calls to the GenericLLVMIRPlatformSupport class below.
92class GenericLLVMIRPlatform : public Platform {
93public:
94 GenericLLVMIRPlatform(GenericLLVMIRPlatformSupport &S) : S(S) {}
95 Error setupJITDylib(JITDylib &JD) override;
96 Error teardownJITDylib(JITDylib &JD) override;
98 const MaterializationUnit &MU) override;
100 // Noop -- Nothing to do (yet).
101 return Error::success();
102 }
103
104private:
105 GenericLLVMIRPlatformSupport &S;
106};
107
108/// This transform parses llvm.global_ctors to produce a single initialization
109/// function for the module, records the function, then deletes
110/// llvm.global_ctors.
111class GlobalCtorDtorScraper {
112public:
113 GlobalCtorDtorScraper(GenericLLVMIRPlatformSupport &PS,
114 StringRef InitFunctionPrefix,
115 StringRef DeInitFunctionPrefix)
116 : PS(PS), InitFunctionPrefix(InitFunctionPrefix),
117 DeInitFunctionPrefix(DeInitFunctionPrefix) {}
120
121private:
122 GenericLLVMIRPlatformSupport &PS;
123 StringRef InitFunctionPrefix;
124 StringRef DeInitFunctionPrefix;
125};
126
127/// Generic IR Platform Support
128///
129/// Scrapes llvm.global_ctors and llvm.global_dtors and replaces them with
130/// specially named 'init' and 'deinit'. Injects definitions / interposes for
131/// some runtime API, including __cxa_atexit, dlopen, and dlclose.
132class GenericLLVMIRPlatformSupport : public LLJIT::PlatformSupport {
133public:
134 GenericLLVMIRPlatformSupport(LLJIT &J, JITDylib &PlatformJD)
135 : J(J), InitFunctionPrefix(J.mangle("__orc_init_func.")),
136 DeInitFunctionPrefix(J.mangle("__orc_deinit_func.")) {
137
139 std::make_unique<GenericLLVMIRPlatform>(*this));
140
141 setInitTransform(J, GlobalCtorDtorScraper(*this, InitFunctionPrefix,
142 DeInitFunctionPrefix));
143
144 SymbolMap StdInterposes;
145
146 StdInterposes[J.mangleAndIntern("__lljit.platform_support_instance")] = {
148 StdInterposes[J.mangleAndIntern("__lljit.cxa_atexit_helper")] = {
149 ExecutorAddr::fromPtr(registerCxaAtExitHelper), JITSymbolFlags()};
150
151 cantFail(PlatformJD.define(absoluteSymbols(std::move(StdInterposes))));
152 cantFail(setupJITDylib(PlatformJD));
153 cantFail(J.addIRModule(PlatformJD, createPlatformRuntimeModule()));
154 }
155
157
158 /// Adds a module that defines the __dso_handle global.
159 Error setupJITDylib(JITDylib &JD) {
160
161 // Add per-jitdylib standard interposes.
162 SymbolMap PerJDInterposes;
163 PerJDInterposes[J.mangleAndIntern("__lljit.run_atexits_helper")] = {
164 ExecutorAddr::fromPtr(runAtExitsHelper), JITSymbolFlags()};
165 PerJDInterposes[J.mangleAndIntern("__lljit.atexit_helper")] = {
166 ExecutorAddr::fromPtr(registerAtExitHelper), JITSymbolFlags()};
167 cantFail(JD.define(absoluteSymbols(std::move(PerJDInterposes))));
168
169 auto Ctx = std::make_unique<LLVMContext>();
170 auto M = std::make_unique<Module>("__standard_lib", *Ctx);
171 M->setDataLayout(J.getDataLayout());
172
173 auto *Int64Ty = Type::getInt64Ty(*Ctx);
174 auto *DSOHandle = new GlobalVariable(
175 *M, Int64Ty, true, GlobalValue::ExternalLinkage,
176 ConstantInt::get(Int64Ty, reinterpret_cast<uintptr_t>(&JD)),
177 "__dso_handle");
178 DSOHandle->setVisibility(GlobalValue::DefaultVisibility);
179 DSOHandle->setInitializer(
180 ConstantInt::get(Int64Ty, ExecutorAddr::fromPtr(&JD).getValue()));
181
182 auto *GenericIRPlatformSupportTy =
183 StructType::create(*Ctx, "lljit.GenericLLJITIRPlatformSupport");
184
185 auto *PlatformInstanceDecl = new GlobalVariable(
186 *M, GenericIRPlatformSupportTy, true, GlobalValue::ExternalLinkage,
187 nullptr, "__lljit.platform_support_instance");
188
189 auto *VoidTy = Type::getVoidTy(*Ctx);
190 addHelperAndWrapper(
191 *M, "__lljit_run_atexits", FunctionType::get(VoidTy, {}, false),
192 GlobalValue::HiddenVisibility, "__lljit.run_atexits_helper",
193 {PlatformInstanceDecl, DSOHandle});
194
195 auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT);
196 auto *AtExitCallbackTy = FunctionType::get(VoidTy, {}, false);
197 auto *AtExitCallbackPtrTy = PointerType::getUnqual(AtExitCallbackTy);
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 *Int8Ty = Type::getInt8Ty(*Ctx);
472 auto *IntTy = Type::getIntNTy(*Ctx, sizeof(int) * CHAR_BIT);
473 auto *VoidTy = Type::getVoidTy(*Ctx);
474 auto *BytePtrTy = PointerType::getUnqual(Int8Ty);
475 auto *CxaAtExitCallbackTy = FunctionType::get(VoidTy, {BytePtrTy}, false);
476 auto *CxaAtExitCallbackPtrTy = PointerType::getUnqual(CxaAtExitCallbackTy);
477
478 auto *CxaAtExit = addHelperAndWrapper(
479 *M, "__cxa_atexit",
480 FunctionType::get(IntTy, {CxaAtExitCallbackPtrTy, BytePtrTy, BytePtrTy},
481 false),
482 GlobalValue::DefaultVisibility, "__lljit.cxa_atexit_helper",
483 {PlatformInstanceDecl});
484 Attribute::AttrKind CxaAtExitExtAttr =
485 TargetLibraryInfo::getExtAttrForI32Return(J.getTargetTriple());
486 if (CxaAtExitExtAttr != Attribute::None)
487 CxaAtExit->addRetAttr(CxaAtExitExtAttr);
488
489 return ThreadSafeModule(std::move(M), std::move(Ctx));
490 }
491
492 LLJIT &J;
493 std::string InitFunctionPrefix;
494 std::string DeInitFunctionPrefix;
498 ItaniumCXAAtExitSupport AtExitMgr;
499};
500
501Error GenericLLVMIRPlatform::setupJITDylib(JITDylib &JD) {
502 return S.setupJITDylib(JD);
503}
504
505Error GenericLLVMIRPlatform::teardownJITDylib(JITDylib &JD) {
506 return Error::success();
507}
508
509Error GenericLLVMIRPlatform::notifyAdding(ResourceTracker &RT,
510 const MaterializationUnit &MU) {
511 return S.notifyAdding(RT, MU);
512}
513
515GlobalCtorDtorScraper::operator()(ThreadSafeModule TSM,
517 auto Err = TSM.withModuleDo([&](Module &M) -> Error {
518 auto &Ctx = M.getContext();
519 auto *GlobalCtors = M.getNamedGlobal("llvm.global_ctors");
520 auto *GlobalDtors = M.getNamedGlobal("llvm.global_dtors");
521
522 auto RegisterCOrDtors = [&](GlobalVariable *GlobalCOrDtors,
523 bool isCtor) -> Error {
524 // If there's no llvm.global_c/dtor or it's just a decl then skip.
525 if (!GlobalCOrDtors || GlobalCOrDtors->isDeclaration())
526 return Error::success();
527 std::string InitOrDeInitFunctionName;
528 if (isCtor)
529 raw_string_ostream(InitOrDeInitFunctionName)
530 << InitFunctionPrefix << M.getModuleIdentifier();
531 else
532 raw_string_ostream(InitOrDeInitFunctionName)
533 << DeInitFunctionPrefix << M.getModuleIdentifier();
534
535 MangleAndInterner Mangle(PS.getExecutionSession(), M.getDataLayout());
536 auto InternedInitOrDeInitName = Mangle(InitOrDeInitFunctionName);
537 if (auto Err = R.defineMaterializing(
538 {{InternedInitOrDeInitName, JITSymbolFlags::Callable}}))
539 return Err;
540
541 auto *InitOrDeInitFunc = Function::Create(
542 FunctionType::get(Type::getVoidTy(Ctx), {}, false),
543 GlobalValue::ExternalLinkage, InitOrDeInitFunctionName, &M);
544 InitOrDeInitFunc->setVisibility(GlobalValue::HiddenVisibility);
545 std::vector<std::pair<Function *, unsigned>> InitsOrDeInits;
546 auto COrDtors = isCtor ? getConstructors(M) : getDestructors(M);
547
548 for (auto E : COrDtors)
549 InitsOrDeInits.push_back(std::make_pair(E.Func, E.Priority));
550 llvm::stable_sort(InitsOrDeInits, llvm::less_second());
551
552 auto *InitOrDeInitFuncEntryBlock =
553 BasicBlock::Create(Ctx, "entry", InitOrDeInitFunc);
554 IRBuilder<> IB(InitOrDeInitFuncEntryBlock);
555 for (auto &KV : InitsOrDeInits)
556 IB.CreateCall(KV.first);
557 IB.CreateRetVoid();
558
559 if (isCtor)
560 PS.registerInitFunc(R.getTargetJITDylib(), InternedInitOrDeInitName);
561 else
562 PS.registerDeInitFunc(R.getTargetJITDylib(), InternedInitOrDeInitName);
563
564 GlobalCOrDtors->eraseFromParent();
565 return Error::success();
566 };
567
568 if (auto Err = RegisterCOrDtors(GlobalCtors, true))
569 return Err;
570 if (auto Err = RegisterCOrDtors(GlobalDtors, false))
571 return Err;
572
573 return Error::success();
574 });
575
576 if (Err)
577 return std::move(Err);
578
579 return std::move(TSM);
580}
581
582/// Inactive Platform Support
583///
584/// Explicitly disables platform support. JITDylibs are not scanned for special
585/// init/deinit symbols. No runtime API interposes are injected.
586class InactivePlatformSupport : public LLJIT::PlatformSupport {
587public:
588 InactivePlatformSupport() = default;
589
590 Error initialize(JITDylib &JD) override {
591 LLVM_DEBUG(dbgs() << "InactivePlatformSupport: no initializers running for "
592 << JD.getName() << "\n");
593 return Error::success();
594 }
595
596 Error deinitialize(JITDylib &JD) override {
598 dbgs() << "InactivePlatformSupport: no deinitializers running for "
599 << JD.getName() << "\n");
600 return Error::success();
601 }
602};
603
604} // end anonymous namespace
605
606namespace llvm {
607namespace orc {
608
612 using SPSDLOpenSig = SPSExecutorAddr(SPSString, int32_t);
613 using SPSDLUpdateSig = int32_t(SPSExecutorAddr);
614 enum dlopen_mode : int32_t {
615 ORC_RT_RTLD_LAZY = 0x1,
616 ORC_RT_RTLD_NOW = 0x2,
617 ORC_RT_RTLD_LOCAL = 0x4,
618 ORC_RT_RTLD_GLOBAL = 0x8
619 };
620
621 auto &ES = J.getExecutionSession();
622 auto MainSearchOrder = J.getMainJITDylib().withLinkOrderDo(
623 [](const JITDylibSearchOrder &SO) { return SO; });
624 StringRef WrapperToCall = "__orc_rt_jit_dlopen_wrapper";
625 bool dlupdate = false;
626 const Triple &TT = ES.getTargetTriple();
627 if (TT.isOSBinFormatMachO() || TT.isOSBinFormatELF()) {
628 if (InitializedDylib.contains(&JD)) {
629 WrapperToCall = "__orc_rt_jit_dlupdate_wrapper";
630 dlupdate = true;
631 } else
632 InitializedDylib.insert(&JD);
633 }
634
635 if (auto WrapperAddr =
636 ES.lookup(MainSearchOrder, J.mangleAndIntern(WrapperToCall))) {
637 if (dlupdate) {
638 int32_t result;
639 auto E = ES.callSPSWrapper<SPSDLUpdateSig>(WrapperAddr->getAddress(),
640 result, DSOHandles[&JD]);
641 if (result)
642 return make_error<StringError>("dlupdate failed",
644 return E;
645 }
646 return ES.callSPSWrapper<SPSDLOpenSig>(WrapperAddr->getAddress(),
647 DSOHandles[&JD], JD.getName(),
648 int32_t(ORC_RT_RTLD_LAZY));
649 } else
650 return WrapperAddr.takeError();
651}
652
655 using SPSDLCloseSig = int32_t(SPSExecutorAddr);
656
657 auto &ES = J.getExecutionSession();
658 auto MainSearchOrder = J.getMainJITDylib().withLinkOrderDo(
659 [](const JITDylibSearchOrder &SO) { return SO; });
660
661 if (auto WrapperAddr = ES.lookup(
662 MainSearchOrder, J.mangleAndIntern("__orc_rt_jit_dlclose_wrapper"))) {
663 int32_t result;
664 auto E = J.getExecutionSession().callSPSWrapper<SPSDLCloseSig>(
665 WrapperAddr->getAddress(), result, DSOHandles[&JD]);
666 if (E)
667 return E;
668 else if (result)
669 return make_error<StringError>("dlclose failed",
671 DSOHandles.erase(&JD);
672 InitializedDylib.erase(&JD);
673 } else
674 return WrapperAddr.takeError();
675 return Error::success();
676}
677
680 J.InitHelperTransformLayer->setTransform(std::move(T));
681}
682
684
686
687 LLVM_DEBUG(dbgs() << "Preparing to create LLJIT instance...\n");
688
689 if (!JTMB) {
690 LLVM_DEBUG({
691 dbgs() << " No explicitly set JITTargetMachineBuilder. "
692 "Detecting host...\n";
693 });
694 if (auto JTMBOrErr = JITTargetMachineBuilder::detectHost())
695 JTMB = std::move(*JTMBOrErr);
696 else
697 return JTMBOrErr.takeError();
698 }
699
700 if ((ES || EPC) && NumCompileThreads)
701 return make_error<StringError>(
702 "NumCompileThreads cannot be used with a custom ExecutionSession or "
703 "ExecutorProcessControl",
705
706#if !LLVM_ENABLE_THREADS
707 if (NumCompileThreads)
708 return make_error<StringError>(
709 "LLJIT num-compile-threads is " + Twine(NumCompileThreads) +
710 " but LLVM was compiled with LLVM_ENABLE_THREADS=Off",
712#endif // !LLVM_ENABLE_THREADS
713
714 // Only used in debug builds.
715 [[maybe_unused]] bool ConcurrentCompilationSettingDefaulted =
716 !SupportConcurrentCompilation;
717
718 if (!SupportConcurrentCompilation) {
719#if LLVM_ENABLE_THREADS
720 SupportConcurrentCompilation = NumCompileThreads || ES || EPC;
721#else
722 SupportConcurrentCompilation = false;
723#endif // LLVM_ENABLE_THREADS
724 } else {
725#if !LLVM_ENABLE_THREADS
726 if (*SupportConcurrentCompilation)
727 return make_error<StringError>(
728 "LLJIT concurrent compilation support requested, but LLVM was built "
729 "with LLVM_ENABLE_THREADS=Off",
731#endif // !LLVM_ENABLE_THREADS
732 }
733
734 LLVM_DEBUG({
735 dbgs() << " JITTargetMachineBuilder is "
736 << JITTargetMachineBuilderPrinter(*JTMB, " ")
737 << " Pre-constructed ExecutionSession: " << (ES ? "Yes" : "No")
738 << "\n"
739 << " DataLayout: ";
740 if (DL)
741 dbgs() << DL->getStringRepresentation() << "\n";
742 else
743 dbgs() << "None (will be created by JITTargetMachineBuilder)\n";
744
745 dbgs() << " Custom object-linking-layer creator: "
746 << (CreateObjectLinkingLayer ? "Yes" : "No") << "\n"
747 << " Custom compile-function creator: "
748 << (CreateCompileFunction ? "Yes" : "No") << "\n"
749 << " Custom platform-setup function: "
750 << (SetUpPlatform ? "Yes" : "No") << "\n"
751 << " Support concurrent compilation: "
752 << (*SupportConcurrentCompilation ? "Yes" : "No");
753 if (ConcurrentCompilationSettingDefaulted)
754 dbgs() << " (defaulted based on ES / EPC / NumCompileThreads)\n";
755 else
756 dbgs() << "\n";
757 dbgs() << " Number of compile threads: " << NumCompileThreads << "\n";
758 });
759
760 // Create DL if not specified.
761 if (!DL) {
762 if (auto DLOrErr = JTMB->getDefaultDataLayoutForTarget())
763 DL = std::move(*DLOrErr);
764 else
765 return DLOrErr.takeError();
766 }
767
768 // If neither ES nor EPC has been set then create an EPC instance.
769 if (!ES && !EPC) {
770 LLVM_DEBUG({
771 dbgs() << "ExecutorProcessControl not specified, "
772 "Creating SelfExecutorProcessControl instance\n";
773 });
774
775 std::unique_ptr<TaskDispatcher> D = nullptr;
776#if LLVM_ENABLE_THREADS
777 if (*SupportConcurrentCompilation) {
778 std::optional<size_t> NumThreads = std ::nullopt;
779 if (NumCompileThreads)
780 NumThreads = NumCompileThreads;
781 D = std::make_unique<DynamicThreadPoolTaskDispatcher>(NumThreads);
782 } else
783 D = std::make_unique<InPlaceTaskDispatcher>();
784#endif // LLVM_ENABLE_THREADS
785 if (auto EPCOrErr =
786 SelfExecutorProcessControl::Create(nullptr, std::move(D), nullptr))
787 EPC = std::move(*EPCOrErr);
788 else
789 return EPCOrErr.takeError();
790 } else if (EPC) {
791 LLVM_DEBUG({
792 dbgs() << "Using explicitly specified ExecutorProcessControl instance "
793 << EPC.get() << "\n";
794 });
795 } else {
796 LLVM_DEBUG({
797 dbgs() << "Using explicitly specified ExecutionSession instance "
798 << ES.get() << "\n";
799 });
800 }
801
802 // If the client didn't configure any linker options then auto-configure the
803 // JIT linker.
804 if (!CreateObjectLinkingLayer) {
805 auto &TT = JTMB->getTargetTriple();
806 bool UseJITLink = false;
807 switch (TT.getArch()) {
808 case Triple::riscv64:
810 UseJITLink = true;
811 break;
812 case Triple::aarch64:
813 UseJITLink = !TT.isOSBinFormatCOFF();
814 break;
815 case Triple::arm:
816 case Triple::armeb:
817 case Triple::thumb:
818 case Triple::thumbeb:
819 UseJITLink = TT.isOSBinFormatELF();
820 break;
821 case Triple::x86_64:
822 UseJITLink = !TT.isOSBinFormatCOFF();
823 break;
824 case Triple::ppc64:
825 UseJITLink = TT.isPPC64ELFv2ABI();
826 break;
827 case Triple::ppc64le:
828 UseJITLink = TT.isOSBinFormatELF();
829 break;
830 default:
831 break;
832 }
833 if (UseJITLink) {
834 if (!JTMB->getCodeModel())
835 JTMB->setCodeModel(CodeModel::Small);
836 JTMB->setRelocationModel(Reloc::PIC_);
837 CreateObjectLinkingLayer =
839 const Triple &) -> Expected<std::unique_ptr<ObjectLayer>> {
840 auto ObjLinkingLayer = std::make_unique<ObjectLinkingLayer>(ES);
841 if (auto EHFrameRegistrar = EPCEHFrameRegistrar::Create(ES))
842 ObjLinkingLayer->addPlugin(
843 std::make_unique<EHFrameRegistrationPlugin>(
844 ES, std::move(*EHFrameRegistrar)));
845 else
846 return EHFrameRegistrar.takeError();
847 return std::move(ObjLinkingLayer);
848 };
849 }
850 }
851
852 // If we need a process JITDylib but no setup function has been given then
853 // create a default one.
854 if (!SetupProcessSymbolsJITDylib && LinkProcessSymbolsByDefault) {
855 LLVM_DEBUG(dbgs() << "Creating default Process JD setup function\n");
856 SetupProcessSymbolsJITDylib = [](LLJIT &J) -> Expected<JITDylibSP> {
857 auto &JD =
858 J.getExecutionSession().createBareJITDylib("<Process Symbols>");
860 J.getExecutionSession());
861 if (!G)
862 return G.takeError();
863 JD.addGenerator(std::move(*G));
864 return &JD;
865 };
866 }
867
868 return Error::success();
869}
870
872 if (auto Err = ES->endSession())
873 ES->reportError(std::move(Err));
874}
875
877
879
881 auto JD = ES->createJITDylib(std::move(Name));
882 if (!JD)
883 return JD.takeError();
884
886 return JD;
887}
888
891 if (!G)
892 return G.takeError();
893
894 if (auto *ExistingJD = ES->getJITDylibByName(Path))
895 return *ExistingJD;
896
897 auto &JD = ES->createBareJITDylib(Path);
898 JD.addGenerator(std::move(*G));
899 return JD;
900}
901
903 std::unique_ptr<MemoryBuffer> LibBuffer) {
905 std::move(LibBuffer));
906 if (!G)
907 return G.takeError();
908
909 JD.addGenerator(std::move(*G));
910
911 return Error::success();
912}
913
916 if (!G)
917 return G.takeError();
918
919 JD.addGenerator(std::move(*G));
920
921 return Error::success();
922}
923
925 assert(TSM && "Can not add null module");
926
927 if (auto Err =
928 TSM.withModuleDo([&](Module &M) { return applyDataLayout(M); }))
929 return Err;
930
931 return InitHelperTransformLayer->add(std::move(RT), std::move(TSM));
932}
933
935 return addIRModule(JD.getDefaultResourceTracker(), std::move(TSM));
936}
937
939 std::unique_ptr<MemoryBuffer> Obj) {
940 assert(Obj && "Can not add null object");
941
942 return ObjTransformLayer->add(std::move(RT), std::move(Obj));
943}
944
945Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
946 return addObjectFile(JD.getDefaultResourceTracker(), std::move(Obj));
947}
948
951 if (auto Sym = ES->lookup(
953 Name))
954 return Sym->getAddress();
955 else
956 return Sym.takeError();
957}
958
961
962 // If the config state provided an ObjectLinkingLayer factory then use it.
964 return S.CreateObjectLinkingLayer(ES, S.JTMB->getTargetTriple());
965
966 // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
967 // a new SectionMemoryManager for each object.
968 auto GetMemMgr = []() { return std::make_unique<SectionMemoryManager>(); };
969 auto Layer =
970 std::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr));
971
972 if (S.JTMB->getTargetTriple().isOSBinFormatCOFF()) {
973 Layer->setOverrideObjectFlagsWithResponsibilityFlags(true);
974 Layer->setAutoClaimResponsibilityForObjectSymbols(true);
975 }
976
977 if (S.JTMB->getTargetTriple().isOSBinFormatELF() &&
978 (S.JTMB->getTargetTriple().getArch() == Triple::ArchType::ppc64 ||
979 S.JTMB->getTargetTriple().getArch() == Triple::ArchType::ppc64le))
980 Layer->setAutoClaimResponsibilityForObjectSymbols(true);
981
982 // FIXME: Explicit conversion to std::unique_ptr<ObjectLayer> added to silence
983 // errors from some GCC / libstdc++ bots. Remove this conversion (i.e.
984 // just return ObjLinkingLayer) once those bots are upgraded.
985 return std::unique_ptr<ObjectLayer>(std::move(Layer));
986}
987
991
992 /// If there is a custom compile function creator set then use it.
994 return S.CreateCompileFunction(std::move(JTMB));
995
996 // If using a custom EPC then use a ConcurrentIRCompiler by default.
998 return std::make_unique<ConcurrentIRCompiler>(std::move(JTMB));
999
1000 auto TM = JTMB.createTargetMachine();
1001 if (!TM)
1002 return TM.takeError();
1003
1004 return std::make_unique<TMOwningSimpleCompiler>(std::move(*TM));
1005}
1006
1008 : DL(std::move(*S.DL)), TT(S.JTMB->getTargetTriple()) {
1009
1011
1012 assert(!(S.EPC && S.ES) && "EPC and ES should not both be set");
1013
1014 if (S.EPC) {
1015 ES = std::make_unique<ExecutionSession>(std::move(S.EPC));
1016 } else if (S.ES)
1017 ES = std::move(S.ES);
1018 else {
1019 if (auto EPC = SelfExecutorProcessControl::Create()) {
1020 ES = std::make_unique<ExecutionSession>(std::move(*EPC));
1021 } else {
1022 Err = EPC.takeError();
1023 return;
1024 }
1025 }
1026
1027 auto ObjLayer = createObjectLinkingLayer(S, *ES);
1028 if (!ObjLayer) {
1029 Err = ObjLayer.takeError();
1030 return;
1031 }
1032 ObjLinkingLayer = std::move(*ObjLayer);
1034 std::make_unique<ObjectTransformLayer>(*ES, *ObjLinkingLayer);
1035
1036 {
1037 auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB));
1038 if (!CompileFunction) {
1039 Err = CompileFunction.takeError();
1040 return;
1041 }
1042 CompileLayer = std::make_unique<IRCompileLayer>(
1043 *ES, *ObjTransformLayer, std::move(*CompileFunction));
1044 TransformLayer = std::make_unique<IRTransformLayer>(*ES, *CompileLayer);
1046 std::make_unique<IRTransformLayer>(*ES, *TransformLayer);
1047 }
1048
1050 InitHelperTransformLayer->setCloneToNewContextOnEmit(true);
1051
1053 if (auto ProcSymsJD = S.SetupProcessSymbolsJITDylib(*this)) {
1054 ProcessSymbols = ProcSymsJD->get();
1055 } else {
1056 Err = ProcSymsJD.takeError();
1057 return;
1058 }
1059 }
1060
1061 if (S.PrePlatformSetup) {
1062 if (auto Err2 = S.PrePlatformSetup(*this)) {
1063 Err = std::move(Err2);
1064 return;
1065 }
1066 }
1067
1068 if (!S.SetUpPlatform)
1070
1071 if (auto PlatformJDOrErr = S.SetUpPlatform(*this)) {
1072 Platform = PlatformJDOrErr->get();
1073 if (Platform)
1074 DefaultLinks.push_back(
1076 } else {
1077 Err = PlatformJDOrErr.takeError();
1078 return;
1079 }
1080
1082 DefaultLinks.push_back(
1084
1085 if (auto MainOrErr = createJITDylib("main"))
1086 Main = &*MainOrErr;
1087 else {
1088 Err = MainOrErr.takeError();
1089 return;
1090 }
1091}
1092
1093std::string LLJIT::mangle(StringRef UnmangledName) const {
1094 std::string MangledName;
1095 {
1096 raw_string_ostream MangledNameStream(MangledName);
1097 Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL);
1098 }
1099 return MangledName;
1100}
1101
1103 if (M.getDataLayout().isDefault())
1104 M.setDataLayout(DL);
1105
1106 if (M.getDataLayout() != DL)
1107 return make_error<StringError>(
1108 "Added modules have incompatible data layouts: " +
1109 M.getDataLayout().getStringRepresentation() + " (module) vs " +
1110 DL.getStringRepresentation() + " (jit)",
1112
1113 return Error::success();
1114}
1115
1117 LLVM_DEBUG({ dbgs() << "Setting up orc platform support for LLJIT\n"; });
1118 J.setPlatformSupport(std::make_unique<ORCPlatformSupport>(J));
1119 return Error::success();
1120}
1121
1123public:
1126 if (!DLLName.ends_with_insensitive(".dll"))
1127 return make_error<StringError>("DLLName not ending with .dll",
1129 auto DLLNameStr = DLLName.str(); // Guarantees null-termination.
1130 auto DLLJD = J.loadPlatformDynamicLibrary(DLLNameStr.c_str());
1131 if (!DLLJD)
1132 return DLLJD.takeError();
1133 JD.addToLinkOrder(*DLLJD);
1134 return Error::success();
1135 }
1136
1137private:
1138 LLJIT &J;
1139};
1140
1142 auto ProcessSymbolsJD = J.getProcessSymbolsJITDylib();
1143 if (!ProcessSymbolsJD)
1144 return make_error<StringError>(
1145 "Native platforms require a process symbols JITDylib",
1147
1148 const Triple &TT = J.getTargetTriple();
1149 ObjectLinkingLayer *ObjLinkingLayer =
1150 dyn_cast<ObjectLinkingLayer>(&J.getObjLinkingLayer());
1151
1152 if (!ObjLinkingLayer)
1153 return make_error<StringError>(
1154 "ExecutorNativePlatform requires ObjectLinkingLayer",
1156
1157 std::unique_ptr<MemoryBuffer> RuntimeArchiveBuffer;
1158 if (OrcRuntime.index() == 0) {
1159 auto A = errorOrToExpected(MemoryBuffer::getFile(std::get<0>(OrcRuntime)));
1160 if (!A)
1161 return A.takeError();
1162 RuntimeArchiveBuffer = std::move(*A);
1163 } else
1164 RuntimeArchiveBuffer = std::move(std::get<1>(OrcRuntime));
1165
1166 auto &ES = J.getExecutionSession();
1167 auto &PlatformJD = ES.createBareJITDylib("<Platform>");
1168 PlatformJD.addToLinkOrder(*ProcessSymbolsJD);
1169
1170 J.setPlatformSupport(std::make_unique<ORCPlatformSupport>(J));
1171
1172 switch (TT.getObjectFormat()) {
1173 case Triple::COFF: {
1174 const char *VCRuntimePath = nullptr;
1175 bool StaticVCRuntime = false;
1176 if (VCRuntime) {
1177 VCRuntimePath = VCRuntime->first.c_str();
1178 StaticVCRuntime = VCRuntime->second;
1179 }
1180 if (auto P = COFFPlatform::Create(
1181 *ObjLinkingLayer, PlatformJD, std::move(RuntimeArchiveBuffer),
1182 LoadAndLinkDynLibrary(J), StaticVCRuntime, VCRuntimePath))
1183 J.getExecutionSession().setPlatform(std::move(*P));
1184 else
1185 return P.takeError();
1186 break;
1187 }
1188 case Triple::ELF: {
1190 *ObjLinkingLayer, std::move(RuntimeArchiveBuffer));
1191 if (!G)
1192 return G.takeError();
1193
1194 if (auto P =
1195 ELFNixPlatform::Create(*ObjLinkingLayer, PlatformJD, std::move(*G)))
1196 J.getExecutionSession().setPlatform(std::move(*P));
1197 else
1198 return P.takeError();
1199 break;
1200 }
1201 case Triple::MachO: {
1203 *ObjLinkingLayer, std::move(RuntimeArchiveBuffer));
1204 if (!G)
1205 return G.takeError();
1206
1207 if (auto P =
1208 MachOPlatform::Create(*ObjLinkingLayer, PlatformJD, std::move(*G)))
1209 ES.setPlatform(std::move(*P));
1210 else
1211 return P.takeError();
1212 break;
1213 }
1214 default:
1215 return make_error<StringError>("Unsupported object format in triple " +
1216 TT.str(),
1218 }
1219
1220 return &PlatformJD;
1221}
1222
1224 LLVM_DEBUG(
1225 { dbgs() << "Setting up GenericLLVMIRPlatform support for LLJIT\n"; });
1226 auto ProcessSymbolsJD = J.getProcessSymbolsJITDylib();
1227 if (!ProcessSymbolsJD)
1228 return make_error<StringError>(
1229 "Native platforms require a process symbols JITDylib",
1231
1232 auto &PlatformJD = J.getExecutionSession().createBareJITDylib("<Platform>");
1233 PlatformJD.addToLinkOrder(*ProcessSymbolsJD);
1234
1236 std::make_unique<GenericLLVMIRPlatformSupport>(J, PlatformJD));
1237
1238 return &PlatformJD;
1239}
1240
1242 LLVM_DEBUG(
1243 { dbgs() << "Explicitly deactivated platform support for LLJIT\n"; });
1244 J.setPlatformSupport(std::make_unique<InactivePlatformSupport>());
1245 return nullptr;
1246}
1247
1250 return Err;
1251 TT = JTMB->getTargetTriple();
1252 return Error::success();
1253}
1254
1256 assert(TSM && "Can not add null module");
1257
1258 if (auto Err = TSM.withModuleDo(
1259 [&](Module &M) -> Error { return applyDataLayout(M); }))
1260 return Err;
1261
1262 return CODLayer->add(JD, std::move(TSM));
1263}
1264
1265LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
1266
1267 // If LLJIT construction failed then bail out.
1268 if (Err)
1269 return;
1270
1271 ErrorAsOutParameter _(&Err);
1272
1273 /// Take/Create the lazy-compile callthrough manager.
1274 if (S.LCTMgr)
1275 LCTMgr = std::move(S.LCTMgr);
1276 else {
1277 if (auto LCTMgrOrErr = createLocalLazyCallThroughManager(
1279 LCTMgr = std::move(*LCTMgrOrErr);
1280 else {
1281 Err = LCTMgrOrErr.takeError();
1282 return;
1283 }
1284 }
1285
1286 // Take/Create the indirect stubs manager builder.
1287 auto ISMBuilder = std::move(S.ISMBuilder);
1288
1289 // If none was provided, try to build one.
1290 if (!ISMBuilder)
1292
1293 // No luck. Bail out.
1294 if (!ISMBuilder) {
1295 Err = make_error<StringError>("Could not construct "
1296 "IndirectStubsManagerBuilder for target " +
1297 S.TT.str(),
1299 return;
1300 }
1301
1302 // Create the IP Layer.
1303 IPLayer = std::make_unique<IRPartitionLayer>(*ES, *InitHelperTransformLayer);
1304
1305 // Create the COD layer.
1306 CODLayer = std::make_unique<CompileOnDemandLayer>(*ES, *IPLayer, *LCTMgr,
1307 std::move(ISMBuilder));
1308
1310 CODLayer->setCloneToNewContextOnEmit(true);
1311}
1312
1313// In-process LLJIT uses eh-frame section wrappers via EPC, so we need to force
1314// them to be linked in.
1318}
1319
1320} // End namespace orc.
1321} // 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:212
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 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:173
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:296
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:66
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:67
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:68
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:488
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2705
Flags for symbols in the JIT.
Definition: JITSymbol.h:74
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
Definition: Mangler.cpp:121
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:686
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:229
bool ends_with_insensitive(StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
Definition: StringRef.cpp:51
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:612
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isPPC64ELFv2ABI() const
Tests whether the target 64-bit PowerPC big endian ABI is ELFv2.
Definition: Triple.h:1006
@ loongarch64
Definition: Triple.h:62
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:395
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:752
const std::string & str() const
Definition: Triple.h:462
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:747
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
static Expected< std::unique_ptr< COFFPlatform > > Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< MemoryBuffer > OrcRuntimeArchiveBuffer, LoadDynamicLibrary LoadDynLibrary, bool StaticVCRuntime=false, const char *VCRuntimePath=nullptr, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a COFFPlatform instance, adding the ORC runtime to the given JITDylib.
static Expected< std::unique_ptr< ELFNixPlatform > > Create(ObjectLinkingLayer &ObjLinkingLayer, JITDylib &PlatformJD, std::unique_ptr< DefinitionGenerator > OrcRuntime, std::optional< SymbolAliasMap > RuntimeAliases=std::nullopt)
Try to create a ELFNixPlatform instance, adding the ORC runtime to the given JITDylib.
static Expected< std::unique_ptr< EPCDynamicLibrarySearchGenerator > > Load(ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow=SymbolPredicate(), AddAbsoluteSymbolsFn AddAbsoluteSymbols=nullptr)
Permanently loads the library at the given path and, on success, returns a DynamicLibrarySearchGenera...
static Expected< std::unique_ptr< EPCDynamicLibrarySearchGenerator > > GetForTargetProcess(ExecutionSession &ES, SymbolPredicate Allow=SymbolPredicate(), AddAbsoluteSymbolsFn AddAbsoluteSymbols=nullptr)
Creates a EPCDynamicLibrarySearchGenerator that searches for symbols in the target process.
static Expected< std::unique_ptr< EPCEHFrameRegistrar > > Create(ExecutionSession &ES)
Create from a ExecutorProcessControl instance alone.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1340
void setPlatform(std::unique_ptr< Platform > P)
Set the Platform for this ExecutionSession.
Definition: Core.h:1397
Error callSPSWrapper(ExecutorAddr WrapperFnAddr, WrapperCallArgTs &&...WrapperCallArgs)
Run a wrapper function using SPS to serialize the arguments and deserialize the results.
Definition: Core.h:1594
JITDylib & createBareJITDylib(std::string Name)
Add a new bare JITDylib to this ExecutionSession.
Definition: Core.cpp:1660
decltype(auto) runSessionLocked(Func &&F)
Run the given lambda with the session mutex locked.
Definition: Core.h:1404
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
Expected< JITDylibSP > operator()(LLJIT &J)
Definition: LLJIT.cpp:1141
An interface for Itanium __cxa_atexit interposer implementations.
Represents a JIT'd dynamic library.
Definition: Core.h:897
Error define(std::unique_ptr< MaterializationUnitType > &&MU, ResourceTrackerSP RT=nullptr)
Define all symbols provided by the materialization unit to be part of this JITDylib.
Definition: Core.h:1823
ExecutionSession & getExecutionSession() const
Get a reference to the ExecutionSession for this JITDylib.
Definition: Core.h:916
void addToLinkOrder(const JITDylibSearchOrder &NewLinks)
Append the given JITDylibSearchOrder to the link order for this JITDylib (discarding any elements alr...
Definition: Core.cpp:1019
static Expected< std::vector< JITDylibSP > > getDFSLinkOrder(ArrayRef< JITDylibSP > JDs)
Returns the given JITDylibs and all of their transitive dependencies in DFS order (based on linkage r...
Definition: Core.cpp:1718
auto withLinkOrderDo(Func &&F) -> decltype(F(std::declval< const JITDylibSearchOrder & >()))
Do something with the link order (run under the session lock).
Definition: Core.h:1816
ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
Definition: Core.cpp:672
GeneratorT & addGenerator(std::unique_ptr< GeneratorT > DefGenerator)
Adds a definition generator to this JITDylib and returns a referenece to it.
Definition: Core.h:1806
A utility class for building TargetMachines for JITs.
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
Error prepareForConstruction()
Called prior to JIT class construcion to fix up defaults.
Definition: LLJIT.cpp:685
ProcessSymbolsJITDylibSetupFunction SetupProcessSymbolsJITDylib
Definition: LLJIT.h:323
ObjectLinkingLayerCreator CreateObjectLinkingLayer
Definition: LLJIT.h:324
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:319
unique_function< Error(LLJIT &)> PrePlatformSetup
Definition: LLJIT.h:326
CompileFunctionCreator CreateCompileFunction
Definition: LLJIT.h:325
std::optional< bool > SupportConcurrentCompilation
Definition: LLJIT.h:330
std::unique_ptr< ExecutorProcessControl > EPC
Definition: LLJIT.h:318
std::optional< JITTargetMachineBuilder > JTMB
Definition: LLJIT.h:320
PlatformSetupFunction SetUpPlatform
Definition: LLJIT.h:327
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:678
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:960
void setPlatformSupport(std::unique_ptr< PlatformSupport > PS)
Set the PlatformSupport instance.
Definition: LLJIT.h:188
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:249
LLJIT(LLJITBuilderState &S, Error &Err)
Create an LLJIT instance with a single compile thread.
Definition: LLJIT.cpp:1007
Error addObjectFile(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:938
Expected< JITDylib & > createJITDylib(std::string Name)
Create a new JITDylib with the given name and return a reference to it.
Definition: LLJIT.cpp:880
JITDylib & getMainJITDylib()
Returns a reference to the JITDylib representing the JIT'd main program.
Definition: LLJIT.h:75
JITDylibSearchOrder DefaultLinks
Definition: LLJIT.h:256
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:262
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:1223
virtual ~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:871
std::string mangle(StringRef UnmangledName) const
Returns a linker-mangled version of UnmangledName.
Definition: LLJIT.cpp:1093
JITDylib * Main
Definition: LLJIT.h:254
JITDylibSP getPlatformJITDylib()
Returns the Platform JITDylib, which will contain the ORC runtime (if given) and any platform symbols...
Definition: LLJIT.cpp:878
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:889
std::unique_ptr< IRTransformLayer > InitHelperTransformLayer
Definition: LLJIT.h:265
std::unique_ptr< IRCompileLayer > CompileLayer
Definition: LLJIT.h:263
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:876
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:949
static Expected< std::unique_ptr< IRCompileLayer::IRCompiler > > createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB)
Definition: LLJIT.cpp:989
JITDylib * ProcessSymbols
Definition: LLJIT.h:252
JITDylib * Platform
Definition: LLJIT.h:253
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this instance.
Definition: LLJIT.h:66
std::unique_ptr< IRTransformLayer > TransformLayer
Definition: LLJIT.h:264
SymbolStringPtr mangleAndIntern(StringRef UnmangledName) const
Returns an interned, linker-mangled version of UnmangledName.
Definition: LLJIT.h:231
DataLayout DL
Definition: LLJIT.h:258
Error linkStaticLibraryInto(JITDylib &JD, std::unique_ptr< MemoryBuffer > LibBuffer)
Link a static library into the given JITDylib.
Definition: LLJIT.cpp:902
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:1102
std::unique_ptr< ObjectLayer > ObjLinkingLayer
Definition: LLJIT.h:261
Triple TT
Definition: LLJIT.h:259
Error addIRModule(ResourceTrackerSP RT, ThreadSafeModule TSM)
Adds an IR module with the given ResourceTracker.
Definition: LLJIT.cpp:924
ExecutorAddr LazyCompileFailureAddr
Definition: LLJIT.h:525
std::unique_ptr< LazyCallThroughManager > LCTMgr
Definition: LLJIT.h:526
IndirectStubsManagerBuilderFunction ISMBuilder
Definition: LLJIT.h:527
Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M)
Add a module to be lazily compiled to JITDylib JD.
Definition: LLJIT.cpp:1255
Error operator()(JITDylib &JD, StringRef DLLName)
Definition: LLJIT.cpp:1125
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:653
Error initialize(orc::JITDylib &JD) override
Definition: LLJIT.cpp:609
An ObjectLayer implementation built on JITLink.
Platforms set up standard symbols and mediate interactions between dynamic initializers (e....
Definition: Core.h:1268
virtual Error teardownJITDylib(JITDylib &JD)=0
This method will be called outside the session lock each time a JITDylib is removed to allow the Plat...
virtual Error notifyRemoving(ResourceTracker &RT)=0
This method will be called under the ExecutionSession lock when a ResourceTracker is removed.
static Expected< DenseMap< JITDylib *, SymbolMap > > lookupInitSymbols(ExecutionSession &ES, const DenseMap< JITDylib *, SymbolLookupSet > &InitSyms)
A utility function for looking up initializer symbols.
Definition: Core.cpp:1487
virtual Error notifyAdding(ResourceTracker &RT, const MaterializationUnit &MU)=0
This method will be called under the ExecutionSession lock each time a MaterializationUnit is added t...
virtual Error setupJITDylib(JITDylib &JD)=0
This method will be called outside the session lock each time a JITDylib is created (unless it is cre...
API to remove / transfer ownership of JIT resources.
Definition: Core.h:77
JITDylib & getJITDylib() const
Return the JITDylib targeted by this tracker.
Definition: Core.h:92
static Expected< std::unique_ptr< SelfExecutorProcessControl > > Create(std::shared_ptr< SymbolStringPool > SSP=nullptr, std::unique_ptr< TaskDispatcher > D=nullptr, std::unique_ptr< jitlink::JITLinkMemoryManager > MemMgr=nullptr)
Create a SelfExecutorProcessControl with the given symbol string pool and memory manager.
static Expected< std::unique_ptr< StaticLibraryDefinitionGenerator > > Create(ObjectLayer &L, std::unique_ptr< MemoryBuffer > ArchiveBuffer, std::unique_ptr< object::Archive > Archive, VisitMembersFunction VisitMembers=VisitMembersFunction(), GetObjectFileInterface GetObjFileInterface=GetObjectFileInterface())
Try to create a StaticLibrarySearchGenerator from the given memory buffer and Archive object.
static Expected< std::unique_ptr< StaticLibraryDefinitionGenerator > > Load(ObjectLayer &L, const char *FileName, VisitMembersFunction VisitMembers=VisitMembersFunction(), GetObjectFileInterface GetObjFileInterface=GetObjectFileInterface())
Try to create a StaticLibraryDefinitionGenerator from the given path.
Pointer to a pooled string representing a symbol name.
An LLVM Module together with a shared ThreadSafeContext.
decltype(auto) withModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function on the contained Module.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib * > JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition: Core.h:177
iterator_range< CtorDtorIterator > getDestructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
std::vector< std::pair< JITDylib *, JITDylibLookupFlags > > JITDylibSearchOrder
A list of (JITDylib*, JITDylibLookupFlags) pairs to be used as a search order during symbol lookup.
Definition: Core.h:173
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
Expected< JITDylibSP > setUpInactivePlatform(LLJIT &J)
Configure the LLJIT instance to disable platform support explicitly.
Definition: LLJIT.cpp:1241
LLVM_ATTRIBUTE_USED void linkComponents()
Definition: LLJIT.cpp:1315
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:1223
Error setUpOrcPlatformManually(LLJIT &J)
Configure the LLJIT instance to use orc runtime support.
Definition: LLJIT.cpp:1116
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