1 //===--------- LLJIT.cpp - An ORC-based JIT for compiling LLVM IR ---------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
11 #include "llvm/Analysis/TargetLibraryInfo.h"
12 #include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_THREADS
13 #include "llvm/ExecutionEngine/Orc/COFFPlatform.h"
14 #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
15 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
16 #include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
17 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
18 #include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
19 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
20 #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
21 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
22 #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
23 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/Mangler.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Support/DynamicLibrary.h"
30 #define DEBUG_TYPE "orc"
33 using namespace llvm::orc
;
37 /// Adds helper function decls and wrapper functions that call the helper with
38 /// some additional prefix arguments.
40 /// E.g. For wrapper "foo" with type i8(i8, i64), helper "bar", and prefix
41 /// args i32 4 and i16 12345, this function will add:
43 /// declare i8 @bar(i32, i16, i8, i64)
45 /// define i8 @foo(i8, i64) {
47 /// %2 = call i8 @bar(i32 4, i16 12345, i8 %0, i64 %1)
51 Function
*addHelperAndWrapper(Module
&M
, StringRef WrapperName
,
52 FunctionType
*WrapperFnType
,
53 GlobalValue::VisibilityTypes WrapperVisibility
,
55 ArrayRef
<Value
*> HelperPrefixArgs
) {
56 std::vector
<Type
*> HelperArgTypes
;
57 for (auto *Arg
: HelperPrefixArgs
)
58 HelperArgTypes
.push_back(Arg
->getType());
59 for (auto *T
: WrapperFnType
->params())
60 HelperArgTypes
.push_back(T
);
62 FunctionType::get(WrapperFnType
->getReturnType(), HelperArgTypes
, false);
63 auto *HelperFn
= Function::Create(HelperFnType
, GlobalValue::ExternalLinkage
,
66 auto *WrapperFn
= Function::Create(
67 WrapperFnType
, GlobalValue::ExternalLinkage
, WrapperName
, M
);
68 WrapperFn
->setVisibility(WrapperVisibility
);
70 auto *EntryBlock
= BasicBlock::Create(M
.getContext(), "entry", WrapperFn
);
71 IRBuilder
<> IB(EntryBlock
);
73 std::vector
<Value
*> HelperArgs
;
74 for (auto *Arg
: HelperPrefixArgs
)
75 HelperArgs
.push_back(Arg
);
76 for (auto &Arg
: WrapperFn
->args())
77 HelperArgs
.push_back(&Arg
);
78 auto *HelperResult
= IB
.CreateCall(HelperFn
, HelperArgs
);
79 if (HelperFn
->getReturnType()->isVoidTy())
82 IB
.CreateRet(HelperResult
);
87 class GenericLLVMIRPlatformSupport
;
89 /// orc::Platform component of Generic LLVM IR Platform support.
90 /// Just forwards calls to the GenericLLVMIRPlatformSupport class below.
91 class GenericLLVMIRPlatform
: public Platform
{
93 GenericLLVMIRPlatform(GenericLLVMIRPlatformSupport
&S
) : S(S
) {}
94 Error
setupJITDylib(JITDylib
&JD
) override
;
95 Error
teardownJITDylib(JITDylib
&JD
) override
;
96 Error
notifyAdding(ResourceTracker
&RT
,
97 const MaterializationUnit
&MU
) override
;
98 Error
notifyRemoving(ResourceTracker
&RT
) override
{
99 // Noop -- Nothing to do (yet).
100 return Error::success();
104 GenericLLVMIRPlatformSupport
&S
;
107 /// This transform parses llvm.global_ctors to produce a single initialization
108 /// function for the module, records the function, then deletes
109 /// llvm.global_ctors.
110 class GlobalCtorDtorScraper
{
112 GlobalCtorDtorScraper(GenericLLVMIRPlatformSupport
&PS
,
113 StringRef InitFunctionPrefix
,
114 StringRef DeInitFunctionPrefix
)
115 : PS(PS
), InitFunctionPrefix(InitFunctionPrefix
),
116 DeInitFunctionPrefix(DeInitFunctionPrefix
) {}
117 Expected
<ThreadSafeModule
> operator()(ThreadSafeModule TSM
,
118 MaterializationResponsibility
&R
);
121 GenericLLVMIRPlatformSupport
&PS
;
122 StringRef InitFunctionPrefix
;
123 StringRef DeInitFunctionPrefix
;
126 /// Generic IR Platform Support
128 /// Scrapes llvm.global_ctors and llvm.global_dtors and replaces them with
129 /// specially named 'init' and 'deinit'. Injects definitions / interposes for
130 /// some runtime API, including __cxa_atexit, dlopen, and dlclose.
131 class GenericLLVMIRPlatformSupport
: public LLJIT::PlatformSupport
{
133 GenericLLVMIRPlatformSupport(LLJIT
&J
, JITDylib
&PlatformJD
)
134 : J(J
), InitFunctionPrefix(J
.mangle("__orc_init_func.")),
135 DeInitFunctionPrefix(J
.mangle("__orc_deinit_func.")) {
137 getExecutionSession().setPlatform(
138 std::make_unique
<GenericLLVMIRPlatform
>(*this));
140 setInitTransform(J
, GlobalCtorDtorScraper(*this, InitFunctionPrefix
,
141 DeInitFunctionPrefix
));
143 SymbolMap StdInterposes
;
145 StdInterposes
[J
.mangleAndIntern("__lljit.platform_support_instance")] = {
146 ExecutorAddr::fromPtr(this), JITSymbolFlags::Exported
};
147 StdInterposes
[J
.mangleAndIntern("__lljit.cxa_atexit_helper")] = {
148 ExecutorAddr::fromPtr(registerCxaAtExitHelper
), JITSymbolFlags()};
150 cantFail(PlatformJD
.define(absoluteSymbols(std::move(StdInterposes
))));
151 cantFail(setupJITDylib(PlatformJD
));
152 cantFail(J
.addIRModule(PlatformJD
, createPlatformRuntimeModule()));
155 ExecutionSession
&getExecutionSession() { return J
.getExecutionSession(); }
157 /// Adds a module that defines the __dso_handle global.
158 Error
setupJITDylib(JITDylib
&JD
) {
160 // Add per-jitdylib standard interposes.
161 SymbolMap PerJDInterposes
;
162 PerJDInterposes
[J
.mangleAndIntern("__lljit.run_atexits_helper")] = {
163 ExecutorAddr::fromPtr(runAtExitsHelper
), JITSymbolFlags()};
164 PerJDInterposes
[J
.mangleAndIntern("__lljit.atexit_helper")] = {
165 ExecutorAddr::fromPtr(registerAtExitHelper
), JITSymbolFlags()};
166 cantFail(JD
.define(absoluteSymbols(std::move(PerJDInterposes
))));
168 auto Ctx
= std::make_unique
<LLVMContext
>();
169 auto M
= std::make_unique
<Module
>("__standard_lib", *Ctx
);
170 M
->setDataLayout(J
.getDataLayout());
172 auto *Int64Ty
= Type::getInt64Ty(*Ctx
);
173 auto *DSOHandle
= new GlobalVariable(
174 *M
, Int64Ty
, true, GlobalValue::ExternalLinkage
,
175 ConstantInt::get(Int64Ty
, reinterpret_cast<uintptr_t>(&JD
)),
177 DSOHandle
->setVisibility(GlobalValue::DefaultVisibility
);
178 DSOHandle
->setInitializer(
179 ConstantInt::get(Int64Ty
, ExecutorAddr::fromPtr(&JD
).getValue()));
181 auto *GenericIRPlatformSupportTy
=
182 StructType::create(*Ctx
, "lljit.GenericLLJITIRPlatformSupport");
184 auto *PlatformInstanceDecl
= new GlobalVariable(
185 *M
, GenericIRPlatformSupportTy
, true, GlobalValue::ExternalLinkage
,
186 nullptr, "__lljit.platform_support_instance");
188 auto *VoidTy
= Type::getVoidTy(*Ctx
);
190 *M
, "__lljit_run_atexits", FunctionType::get(VoidTy
, {}, false),
191 GlobalValue::HiddenVisibility
, "__lljit.run_atexits_helper",
192 {PlatformInstanceDecl
, DSOHandle
});
194 auto *IntTy
= Type::getIntNTy(*Ctx
, sizeof(int) * CHAR_BIT
);
195 auto *AtExitCallbackTy
= FunctionType::get(VoidTy
, {}, false);
196 auto *AtExitCallbackPtrTy
= PointerType::getUnqual(AtExitCallbackTy
);
197 auto *AtExit
= addHelperAndWrapper(
198 *M
, "atexit", FunctionType::get(IntTy
, {AtExitCallbackPtrTy
}, false),
199 GlobalValue::HiddenVisibility
, "__lljit.atexit_helper",
200 {PlatformInstanceDecl
, DSOHandle
});
201 Attribute::AttrKind AtExitExtAttr
=
202 TargetLibraryInfo::getExtAttrForI32Return(J
.getTargetTriple());
203 if (AtExitExtAttr
!= Attribute::None
)
204 AtExit
->addRetAttr(AtExitExtAttr
);
206 return J
.addIRModule(JD
, ThreadSafeModule(std::move(M
), std::move(Ctx
)));
209 Error
notifyAdding(ResourceTracker
&RT
, const MaterializationUnit
&MU
) {
210 auto &JD
= RT
.getJITDylib();
211 if (auto &InitSym
= MU
.getInitializerSymbol())
212 InitSymbols
[&JD
].add(InitSym
, SymbolLookupFlags::WeaklyReferencedSymbol
);
214 // If there's no identified init symbol attached, but there is a symbol
215 // with the GenericIRPlatform::InitFunctionPrefix, then treat that as
216 // an init function. Add the symbol to both the InitSymbols map (which
217 // will trigger a lookup to materialize the module) and the InitFunctions
218 // map (which holds the names of the symbols to execute).
219 for (auto &KV
: MU
.getSymbols())
220 if ((*KV
.first
).starts_with(InitFunctionPrefix
)) {
221 InitSymbols
[&JD
].add(KV
.first
,
222 SymbolLookupFlags::WeaklyReferencedSymbol
);
223 InitFunctions
[&JD
].add(KV
.first
);
224 } else if ((*KV
.first
).starts_with(DeInitFunctionPrefix
)) {
225 DeInitFunctions
[&JD
].add(KV
.first
);
228 return Error::success();
231 Error
initialize(JITDylib
&JD
) override
{
233 dbgs() << "GenericLLVMIRPlatformSupport getting initializers to run\n";
235 if (auto Initializers
= getInitializers(JD
)) {
237 { dbgs() << "GenericLLVMIRPlatformSupport running initializers\n"; });
238 for (auto InitFnAddr
: *Initializers
) {
240 dbgs() << " Running init " << formatv("{0:x16}", InitFnAddr
)
243 auto *InitFn
= InitFnAddr
.toPtr
<void (*)()>();
247 return Initializers
.takeError();
248 return Error::success();
251 Error
deinitialize(JITDylib
&JD
) override
{
253 dbgs() << "GenericLLVMIRPlatformSupport getting deinitializers to run\n";
255 if (auto Deinitializers
= getDeinitializers(JD
)) {
257 dbgs() << "GenericLLVMIRPlatformSupport running deinitializers\n";
259 for (auto DeinitFnAddr
: *Deinitializers
) {
261 dbgs() << " Running deinit " << formatv("{0:x16}", DeinitFnAddr
)
264 auto *DeinitFn
= DeinitFnAddr
.toPtr
<void (*)()>();
268 return Deinitializers
.takeError();
270 return Error::success();
273 void registerInitFunc(JITDylib
&JD
, SymbolStringPtr InitName
) {
274 getExecutionSession().runSessionLocked([&]() {
275 InitFunctions
[&JD
].add(InitName
);
279 void registerDeInitFunc(JITDylib
&JD
, SymbolStringPtr DeInitName
) {
280 getExecutionSession().runSessionLocked(
281 [&]() { DeInitFunctions
[&JD
].add(DeInitName
); });
285 Expected
<std::vector
<ExecutorAddr
>> getInitializers(JITDylib
&JD
) {
286 if (auto Err
= issueInitLookups(JD
))
287 return std::move(Err
);
289 DenseMap
<JITDylib
*, SymbolLookupSet
> LookupSymbols
;
290 std::vector
<JITDylibSP
> DFSLinkOrder
;
292 if (auto Err
= getExecutionSession().runSessionLocked([&]() -> Error
{
293 if (auto DFSLinkOrderOrErr
= JD
.getDFSLinkOrder())
294 DFSLinkOrder
= std::move(*DFSLinkOrderOrErr
);
296 return DFSLinkOrderOrErr
.takeError();
298 for (auto &NextJD
: DFSLinkOrder
) {
299 auto IFItr
= InitFunctions
.find(NextJD
.get());
300 if (IFItr
!= InitFunctions
.end()) {
301 LookupSymbols
[NextJD
.get()] = std::move(IFItr
->second
);
302 InitFunctions
.erase(IFItr
);
305 return Error::success();
307 return std::move(Err
);
310 dbgs() << "JITDylib init order is [ ";
311 for (auto &JD
: llvm::reverse(DFSLinkOrder
))
312 dbgs() << "\"" << JD
->getName() << "\" ";
314 dbgs() << "Looking up init functions:\n";
315 for (auto &KV
: LookupSymbols
)
316 dbgs() << " \"" << KV
.first
->getName() << "\": " << KV
.second
<< "\n";
319 auto &ES
= getExecutionSession();
320 auto LookupResult
= Platform::lookupInitSymbols(ES
, LookupSymbols
);
323 return LookupResult
.takeError();
325 std::vector
<ExecutorAddr
> Initializers
;
326 while (!DFSLinkOrder
.empty()) {
327 auto &NextJD
= *DFSLinkOrder
.back();
328 DFSLinkOrder
.pop_back();
329 auto InitsItr
= LookupResult
->find(&NextJD
);
330 if (InitsItr
== LookupResult
->end())
332 for (auto &KV
: InitsItr
->second
)
333 Initializers
.push_back(KV
.second
.getAddress());
339 Expected
<std::vector
<ExecutorAddr
>> getDeinitializers(JITDylib
&JD
) {
340 auto &ES
= getExecutionSession();
342 auto LLJITRunAtExits
= J
.mangleAndIntern("__lljit_run_atexits");
344 DenseMap
<JITDylib
*, SymbolLookupSet
> LookupSymbols
;
345 std::vector
<JITDylibSP
> DFSLinkOrder
;
347 if (auto Err
= ES
.runSessionLocked([&]() -> Error
{
348 if (auto DFSLinkOrderOrErr
= JD
.getDFSLinkOrder())
349 DFSLinkOrder
= std::move(*DFSLinkOrderOrErr
);
351 return DFSLinkOrderOrErr
.takeError();
353 for (auto &NextJD
: DFSLinkOrder
) {
354 auto &JDLookupSymbols
= LookupSymbols
[NextJD
.get()];
355 auto DIFItr
= DeInitFunctions
.find(NextJD
.get());
356 if (DIFItr
!= DeInitFunctions
.end()) {
357 LookupSymbols
[NextJD
.get()] = std::move(DIFItr
->second
);
358 DeInitFunctions
.erase(DIFItr
);
360 JDLookupSymbols
.add(LLJITRunAtExits
,
361 SymbolLookupFlags::WeaklyReferencedSymbol
);
363 return Error::success();
365 return std::move(Err
);
368 dbgs() << "JITDylib deinit order is [ ";
369 for (auto &JD
: DFSLinkOrder
)
370 dbgs() << "\"" << JD
->getName() << "\" ";
372 dbgs() << "Looking up deinit functions:\n";
373 for (auto &KV
: LookupSymbols
)
374 dbgs() << " \"" << KV
.first
->getName() << "\": " << KV
.second
<< "\n";
377 auto LookupResult
= Platform::lookupInitSymbols(ES
, LookupSymbols
);
380 return LookupResult
.takeError();
382 std::vector
<ExecutorAddr
> DeInitializers
;
383 for (auto &NextJD
: DFSLinkOrder
) {
384 auto DeInitsItr
= LookupResult
->find(NextJD
.get());
385 assert(DeInitsItr
!= LookupResult
->end() &&
386 "Every JD should have at least __lljit_run_atexits");
388 auto RunAtExitsItr
= DeInitsItr
->second
.find(LLJITRunAtExits
);
389 if (RunAtExitsItr
!= DeInitsItr
->second
.end())
390 DeInitializers
.push_back(RunAtExitsItr
->second
.getAddress());
392 for (auto &KV
: DeInitsItr
->second
)
393 if (KV
.first
!= LLJITRunAtExits
)
394 DeInitializers
.push_back(KV
.second
.getAddress());
397 return DeInitializers
;
400 /// Issue lookups for all init symbols required to initialize JD (and any
401 /// JITDylibs that it depends on).
402 Error
issueInitLookups(JITDylib
&JD
) {
403 DenseMap
<JITDylib
*, SymbolLookupSet
> RequiredInitSymbols
;
404 std::vector
<JITDylibSP
> DFSLinkOrder
;
406 if (auto Err
= getExecutionSession().runSessionLocked([&]() -> Error
{
407 if (auto DFSLinkOrderOrErr
= JD
.getDFSLinkOrder())
408 DFSLinkOrder
= std::move(*DFSLinkOrderOrErr
);
410 return DFSLinkOrderOrErr
.takeError();
412 for (auto &NextJD
: DFSLinkOrder
) {
413 auto ISItr
= InitSymbols
.find(NextJD
.get());
414 if (ISItr
!= InitSymbols
.end()) {
415 RequiredInitSymbols
[NextJD
.get()] = std::move(ISItr
->second
);
416 InitSymbols
.erase(ISItr
);
419 return Error::success();
423 return Platform::lookupInitSymbols(getExecutionSession(),
428 static void registerCxaAtExitHelper(void *Self
, void (*F
)(void *), void *Ctx
,
431 dbgs() << "Registering cxa atexit function " << (void *)F
<< " for JD "
432 << (*static_cast<JITDylib
**>(DSOHandle
))->getName() << "\n";
434 static_cast<GenericLLVMIRPlatformSupport
*>(Self
)->AtExitMgr
.registerAtExit(
438 static void registerAtExitHelper(void *Self
, void *DSOHandle
, void (*F
)()) {
440 dbgs() << "Registering atexit function " << (void *)F
<< " for JD "
441 << (*static_cast<JITDylib
**>(DSOHandle
))->getName() << "\n";
443 static_cast<GenericLLVMIRPlatformSupport
*>(Self
)->AtExitMgr
.registerAtExit(
444 reinterpret_cast<void (*)(void *)>(F
), nullptr, DSOHandle
);
447 static void runAtExitsHelper(void *Self
, void *DSOHandle
) {
449 dbgs() << "Running atexit functions for JD "
450 << (*static_cast<JITDylib
**>(DSOHandle
))->getName() << "\n";
452 static_cast<GenericLLVMIRPlatformSupport
*>(Self
)->AtExitMgr
.runAtExits(
456 // Constructs an LLVM IR module containing platform runtime globals,
457 // functions, and interposes.
458 ThreadSafeModule
createPlatformRuntimeModule() {
459 auto Ctx
= std::make_unique
<LLVMContext
>();
460 auto M
= std::make_unique
<Module
>("__standard_lib", *Ctx
);
461 M
->setDataLayout(J
.getDataLayout());
463 auto *GenericIRPlatformSupportTy
=
464 StructType::create(*Ctx
, "lljit.GenericLLJITIRPlatformSupport");
466 auto *PlatformInstanceDecl
= new GlobalVariable(
467 *M
, GenericIRPlatformSupportTy
, true, GlobalValue::ExternalLinkage
,
468 nullptr, "__lljit.platform_support_instance");
470 auto *Int8Ty
= Type::getInt8Ty(*Ctx
);
471 auto *IntTy
= Type::getIntNTy(*Ctx
, sizeof(int) * CHAR_BIT
);
472 auto *VoidTy
= Type::getVoidTy(*Ctx
);
473 auto *BytePtrTy
= PointerType::getUnqual(Int8Ty
);
474 auto *CxaAtExitCallbackTy
= FunctionType::get(VoidTy
, {BytePtrTy
}, false);
475 auto *CxaAtExitCallbackPtrTy
= PointerType::getUnqual(CxaAtExitCallbackTy
);
477 auto *CxaAtExit
= addHelperAndWrapper(
479 FunctionType::get(IntTy
, {CxaAtExitCallbackPtrTy
, BytePtrTy
, BytePtrTy
},
481 GlobalValue::DefaultVisibility
, "__lljit.cxa_atexit_helper",
482 {PlatformInstanceDecl
});
483 Attribute::AttrKind CxaAtExitExtAttr
=
484 TargetLibraryInfo::getExtAttrForI32Return(J
.getTargetTriple());
485 if (CxaAtExitExtAttr
!= Attribute::None
)
486 CxaAtExit
->addRetAttr(CxaAtExitExtAttr
);
488 return ThreadSafeModule(std::move(M
), std::move(Ctx
));
492 std::string InitFunctionPrefix
;
493 std::string DeInitFunctionPrefix
;
494 DenseMap
<JITDylib
*, SymbolLookupSet
> InitSymbols
;
495 DenseMap
<JITDylib
*, SymbolLookupSet
> InitFunctions
;
496 DenseMap
<JITDylib
*, SymbolLookupSet
> DeInitFunctions
;
497 ItaniumCXAAtExitSupport AtExitMgr
;
500 Error
GenericLLVMIRPlatform::setupJITDylib(JITDylib
&JD
) {
501 return S
.setupJITDylib(JD
);
504 Error
GenericLLVMIRPlatform::teardownJITDylib(JITDylib
&JD
) {
505 return Error::success();
508 Error
GenericLLVMIRPlatform::notifyAdding(ResourceTracker
&RT
,
509 const MaterializationUnit
&MU
) {
510 return S
.notifyAdding(RT
, MU
);
513 Expected
<ThreadSafeModule
>
514 GlobalCtorDtorScraper::operator()(ThreadSafeModule TSM
,
515 MaterializationResponsibility
&R
) {
516 auto Err
= TSM
.withModuleDo([&](Module
&M
) -> Error
{
517 auto &Ctx
= M
.getContext();
518 auto *GlobalCtors
= M
.getNamedGlobal("llvm.global_ctors");
519 auto *GlobalDtors
= M
.getNamedGlobal("llvm.global_dtors");
521 auto RegisterCOrDtors
= [&](GlobalVariable
*GlobalCOrDtors
,
522 bool isCtor
) -> Error
{
523 // If there's no llvm.global_c/dtor or it's just a decl then skip.
524 if (!GlobalCOrDtors
|| GlobalCOrDtors
->isDeclaration())
525 return Error::success();
526 std::string InitOrDeInitFunctionName
;
528 raw_string_ostream(InitOrDeInitFunctionName
)
529 << InitFunctionPrefix
<< M
.getModuleIdentifier();
531 raw_string_ostream(InitOrDeInitFunctionName
)
532 << DeInitFunctionPrefix
<< M
.getModuleIdentifier();
534 MangleAndInterner
Mangle(PS
.getExecutionSession(), M
.getDataLayout());
535 auto InternedInitOrDeInitName
= Mangle(InitOrDeInitFunctionName
);
536 if (auto Err
= R
.defineMaterializing(
537 {{InternedInitOrDeInitName
, JITSymbolFlags::Callable
}}))
540 auto *InitOrDeInitFunc
= Function::Create(
541 FunctionType::get(Type::getVoidTy(Ctx
), {}, false),
542 GlobalValue::ExternalLinkage
, InitOrDeInitFunctionName
, &M
);
543 InitOrDeInitFunc
->setVisibility(GlobalValue::HiddenVisibility
);
544 std::vector
<std::pair
<Function
*, unsigned>> InitsOrDeInits
;
545 auto COrDtors
= isCtor
? getConstructors(M
) : getDestructors(M
);
547 for (auto E
: COrDtors
)
548 InitsOrDeInits
.push_back(std::make_pair(E
.Func
, E
.Priority
));
549 llvm::stable_sort(InitsOrDeInits
, llvm::less_second());
551 auto *InitOrDeInitFuncEntryBlock
=
552 BasicBlock::Create(Ctx
, "entry", InitOrDeInitFunc
);
553 IRBuilder
<> IB(InitOrDeInitFuncEntryBlock
);
554 for (auto &KV
: InitsOrDeInits
)
555 IB
.CreateCall(KV
.first
);
559 PS
.registerInitFunc(R
.getTargetJITDylib(), InternedInitOrDeInitName
);
561 PS
.registerDeInitFunc(R
.getTargetJITDylib(), InternedInitOrDeInitName
);
563 GlobalCOrDtors
->eraseFromParent();
564 return Error::success();
567 if (auto Err
= RegisterCOrDtors(GlobalCtors
, true))
569 if (auto Err
= RegisterCOrDtors(GlobalDtors
, false))
572 return Error::success();
576 return std::move(Err
);
578 return std::move(TSM
);
581 /// Inactive Platform Support
583 /// Explicitly disables platform support. JITDylibs are not scanned for special
584 /// init/deinit symbols. No runtime API interposes are injected.
585 class InactivePlatformSupport
: public LLJIT::PlatformSupport
{
587 InactivePlatformSupport() = default;
589 Error
initialize(JITDylib
&JD
) override
{
590 LLVM_DEBUG(dbgs() << "InactivePlatformSupport: no initializers running for "
591 << JD
.getName() << "\n");
592 return Error::success();
595 Error
deinitialize(JITDylib
&JD
) override
{
597 dbgs() << "InactivePlatformSupport: no deinitializers running for "
598 << JD
.getName() << "\n");
599 return Error::success();
603 } // end anonymous namespace
608 Error
ORCPlatformSupport::initialize(orc::JITDylib
&JD
) {
609 using llvm::orc::shared::SPSExecutorAddr
;
610 using llvm::orc::shared::SPSString
;
611 using SPSDLOpenSig
= SPSExecutorAddr(SPSString
, int32_t);
612 using SPSDLUpdateSig
= int32_t(SPSExecutorAddr
);
613 enum dlopen_mode
: int32_t {
614 ORC_RT_RTLD_LAZY
= 0x1,
615 ORC_RT_RTLD_NOW
= 0x2,
616 ORC_RT_RTLD_LOCAL
= 0x4,
617 ORC_RT_RTLD_GLOBAL
= 0x8
620 auto &ES
= J
.getExecutionSession();
621 auto MainSearchOrder
= J
.getMainJITDylib().withLinkOrderDo(
622 [](const JITDylibSearchOrder
&SO
) { return SO
; });
623 StringRef WrapperToCall
= "__orc_rt_jit_dlopen_wrapper";
624 bool dlupdate
= false;
625 const Triple
&TT
= ES
.getTargetTriple();
626 if (TT
.isOSBinFormatMachO() || TT
.isOSBinFormatELF()) {
627 if (InitializedDylib
.contains(&JD
)) {
628 WrapperToCall
= "__orc_rt_jit_dlupdate_wrapper";
631 InitializedDylib
.insert(&JD
);
634 if (auto WrapperAddr
=
635 ES
.lookup(MainSearchOrder
, J
.mangleAndIntern(WrapperToCall
))) {
638 auto E
= ES
.callSPSWrapper
<SPSDLUpdateSig
>(WrapperAddr
->getAddress(),
639 result
, DSOHandles
[&JD
]);
641 return make_error
<StringError
>("dlupdate failed",
642 inconvertibleErrorCode());
645 return ES
.callSPSWrapper
<SPSDLOpenSig
>(WrapperAddr
->getAddress(),
646 DSOHandles
[&JD
], JD
.getName(),
647 int32_t(ORC_RT_RTLD_LAZY
));
649 return WrapperAddr
.takeError();
652 Error
ORCPlatformSupport::deinitialize(orc::JITDylib
&JD
) {
653 using llvm::orc::shared::SPSExecutorAddr
;
654 using SPSDLCloseSig
= int32_t(SPSExecutorAddr
);
656 auto &ES
= J
.getExecutionSession();
657 auto MainSearchOrder
= J
.getMainJITDylib().withLinkOrderDo(
658 [](const JITDylibSearchOrder
&SO
) { return SO
; });
660 if (auto WrapperAddr
= ES
.lookup(
661 MainSearchOrder
, J
.mangleAndIntern("__orc_rt_jit_dlclose_wrapper"))) {
663 auto E
= J
.getExecutionSession().callSPSWrapper
<SPSDLCloseSig
>(
664 WrapperAddr
->getAddress(), result
, DSOHandles
[&JD
]);
668 return make_error
<StringError
>("dlclose failed",
669 inconvertibleErrorCode());
670 DSOHandles
.erase(&JD
);
671 InitializedDylib
.erase(&JD
);
673 return WrapperAddr
.takeError();
674 return Error::success();
677 void LLJIT::PlatformSupport::setInitTransform(
678 LLJIT
&J
, IRTransformLayer::TransformFunction T
) {
679 J
.InitHelperTransformLayer
->setTransform(std::move(T
));
682 LLJIT::PlatformSupport::~PlatformSupport() = default;
684 Error
LLJITBuilderState::prepareForConstruction() {
686 LLVM_DEBUG(dbgs() << "Preparing to create LLJIT instance...\n");
690 dbgs() << " No explicitly set JITTargetMachineBuilder. "
691 "Detecting host...\n";
693 if (auto JTMBOrErr
= JITTargetMachineBuilder::detectHost())
694 JTMB
= std::move(*JTMBOrErr
);
696 return JTMBOrErr
.takeError();
699 if ((ES
|| EPC
) && NumCompileThreads
)
700 return make_error
<StringError
>(
701 "NumCompileThreads cannot be used with a custom ExecutionSession or "
702 "ExecutorProcessControl",
703 inconvertibleErrorCode());
705 #if !LLVM_ENABLE_THREADS
706 if (NumCompileThreads
)
707 return make_error
<StringError
>(
708 "LLJIT num-compile-threads is " + Twine(NumCompileThreads
) +
709 " but LLVM was compiled with LLVM_ENABLE_THREADS=Off",
710 inconvertibleErrorCode());
711 #endif // !LLVM_ENABLE_THREADS
713 // Only used in debug builds.
714 [[maybe_unused
]] bool ConcurrentCompilationSettingDefaulted
=
715 !SupportConcurrentCompilation
;
717 if (!SupportConcurrentCompilation
) {
718 #if LLVM_ENABLE_THREADS
719 SupportConcurrentCompilation
= NumCompileThreads
|| ES
|| EPC
;
721 SupportConcurrentCompilation
= false;
722 #endif // LLVM_ENABLE_THREADS
724 #if !LLVM_ENABLE_THREADS
725 if (*SupportConcurrentCompilation
)
726 return make_error
<StringError
>(
727 "LLJIT concurrent compilation support requested, but LLVM was built "
728 "with LLVM_ENABLE_THREADS=Off",
729 inconvertibleErrorCode());
730 #endif // !LLVM_ENABLE_THREADS
734 dbgs() << " JITTargetMachineBuilder is "
735 << JITTargetMachineBuilderPrinter(*JTMB
, " ")
736 << " Pre-constructed ExecutionSession: " << (ES
? "Yes" : "No")
740 dbgs() << DL
->getStringRepresentation() << "\n";
742 dbgs() << "None (will be created by JITTargetMachineBuilder)\n";
744 dbgs() << " Custom object-linking-layer creator: "
745 << (CreateObjectLinkingLayer
? "Yes" : "No") << "\n"
746 << " Custom compile-function creator: "
747 << (CreateCompileFunction
? "Yes" : "No") << "\n"
748 << " Custom platform-setup function: "
749 << (SetUpPlatform
? "Yes" : "No") << "\n"
750 << " Support concurrent compilation: "
751 << (*SupportConcurrentCompilation
? "Yes" : "No");
752 if (ConcurrentCompilationSettingDefaulted
)
753 dbgs() << " (defaulted based on ES / EPC / NumCompileThreads)\n";
756 dbgs() << " Number of compile threads: " << NumCompileThreads
<< "\n";
759 // Create DL if not specified.
761 if (auto DLOrErr
= JTMB
->getDefaultDataLayoutForTarget())
762 DL
= std::move(*DLOrErr
);
764 return DLOrErr
.takeError();
767 // If neither ES nor EPC has been set then create an EPC instance.
770 dbgs() << "ExecutorProcessControl not specified, "
771 "Creating SelfExecutorProcessControl instance\n";
774 std::unique_ptr
<TaskDispatcher
> D
= nullptr;
775 #if LLVM_ENABLE_THREADS
776 if (*SupportConcurrentCompilation
) {
777 std::optional
<size_t> NumThreads
= std ::nullopt
;
778 if (NumCompileThreads
)
779 NumThreads
= NumCompileThreads
;
780 D
= std::make_unique
<DynamicThreadPoolTaskDispatcher
>(NumThreads
);
782 D
= std::make_unique
<InPlaceTaskDispatcher
>();
783 #endif // LLVM_ENABLE_THREADS
785 SelfExecutorProcessControl::Create(nullptr, std::move(D
), nullptr))
786 EPC
= std::move(*EPCOrErr
);
788 return EPCOrErr
.takeError();
791 dbgs() << "Using explicitly specified ExecutorProcessControl instance "
792 << EPC
.get() << "\n";
796 dbgs() << "Using explicitly specified ExecutionSession instance "
801 // If the client didn't configure any linker options then auto-configure the
803 if (!CreateObjectLinkingLayer
) {
804 auto &TT
= JTMB
->getTargetTriple();
805 bool UseJITLink
= false;
806 switch (TT
.getArch()) {
807 case Triple::riscv64
:
808 case Triple::loongarch64
:
811 case Triple::aarch64
:
812 UseJITLink
= !TT
.isOSBinFormatCOFF();
817 case Triple::thumbeb
:
818 UseJITLink
= TT
.isOSBinFormatELF();
821 UseJITLink
= !TT
.isOSBinFormatCOFF();
824 UseJITLink
= TT
.isPPC64ELFv2ABI();
826 case Triple::ppc64le
:
827 UseJITLink
= TT
.isOSBinFormatELF();
833 if (!JTMB
->getCodeModel())
834 JTMB
->setCodeModel(CodeModel::Small
);
835 JTMB
->setRelocationModel(Reloc::PIC_
);
836 CreateObjectLinkingLayer
=
837 [](ExecutionSession
&ES
,
838 const Triple
&) -> Expected
<std::unique_ptr
<ObjectLayer
>> {
839 auto ObjLinkingLayer
= std::make_unique
<ObjectLinkingLayer
>(ES
);
840 if (auto EHFrameRegistrar
= EPCEHFrameRegistrar::Create(ES
))
841 ObjLinkingLayer
->addPlugin(
842 std::make_unique
<EHFrameRegistrationPlugin
>(
843 ES
, std::move(*EHFrameRegistrar
)));
845 return EHFrameRegistrar
.takeError();
846 return std::move(ObjLinkingLayer
);
851 // If we need a process JITDylib but no setup function has been given then
852 // create a default one.
853 if (!SetupProcessSymbolsJITDylib
&& LinkProcessSymbolsByDefault
) {
854 LLVM_DEBUG(dbgs() << "Creating default Process JD setup function\n");
855 SetupProcessSymbolsJITDylib
= [](LLJIT
&J
) -> Expected
<JITDylibSP
> {
857 J
.getExecutionSession().createBareJITDylib("<Process Symbols>");
858 auto G
= EPCDynamicLibrarySearchGenerator::GetForTargetProcess(
859 J
.getExecutionSession());
861 return G
.takeError();
862 JD
.addGenerator(std::move(*G
));
867 return Error::success();
871 if (auto Err
= ES
->endSession())
872 ES
->reportError(std::move(Err
));
875 JITDylibSP
LLJIT::getProcessSymbolsJITDylib() { return ProcessSymbols
; }
877 JITDylibSP
LLJIT::getPlatformJITDylib() { return Platform
; }
879 Expected
<JITDylib
&> LLJIT::createJITDylib(std::string Name
) {
880 auto JD
= ES
->createJITDylib(std::move(Name
));
882 return JD
.takeError();
884 JD
->addToLinkOrder(DefaultLinks
);
888 Expected
<JITDylib
&> LLJIT::loadPlatformDynamicLibrary(const char *Path
) {
889 auto G
= EPCDynamicLibrarySearchGenerator::Load(*ES
, Path
);
891 return G
.takeError();
893 if (auto *ExistingJD
= ES
->getJITDylibByName(Path
))
896 auto &JD
= ES
->createBareJITDylib(Path
);
897 JD
.addGenerator(std::move(*G
));
901 Error
LLJIT::linkStaticLibraryInto(JITDylib
&JD
,
902 std::unique_ptr
<MemoryBuffer
> LibBuffer
) {
903 auto G
= StaticLibraryDefinitionGenerator::Create(*ObjLinkingLayer
,
904 std::move(LibBuffer
));
906 return G
.takeError();
908 JD
.addGenerator(std::move(*G
));
910 return Error::success();
913 Error
LLJIT::linkStaticLibraryInto(JITDylib
&JD
, const char *Path
) {
914 auto G
= StaticLibraryDefinitionGenerator::Load(*ObjLinkingLayer
, Path
);
916 return G
.takeError();
918 JD
.addGenerator(std::move(*G
));
920 return Error::success();
923 Error
LLJIT::addIRModule(ResourceTrackerSP RT
, ThreadSafeModule TSM
) {
924 assert(TSM
&& "Can not add null module");
927 TSM
.withModuleDo([&](Module
&M
) { return applyDataLayout(M
); }))
930 return InitHelperTransformLayer
->add(std::move(RT
), std::move(TSM
));
933 Error
LLJIT::addIRModule(JITDylib
&JD
, ThreadSafeModule TSM
) {
934 return addIRModule(JD
.getDefaultResourceTracker(), std::move(TSM
));
937 Error
LLJIT::addObjectFile(ResourceTrackerSP RT
,
938 std::unique_ptr
<MemoryBuffer
> Obj
) {
939 assert(Obj
&& "Can not add null object");
941 return ObjTransformLayer
->add(std::move(RT
), std::move(Obj
));
944 Error
LLJIT::addObjectFile(JITDylib
&JD
, std::unique_ptr
<MemoryBuffer
> Obj
) {
945 return addObjectFile(JD
.getDefaultResourceTracker(), std::move(Obj
));
948 Expected
<ExecutorAddr
> LLJIT::lookupLinkerMangled(JITDylib
&JD
,
949 SymbolStringPtr Name
) {
950 if (auto Sym
= ES
->lookup(
951 makeJITDylibSearchOrder(&JD
, JITDylibLookupFlags::MatchAllSymbols
),
953 return Sym
->getAddress();
955 return Sym
.takeError();
958 Expected
<std::unique_ptr
<ObjectLayer
>>
959 LLJIT::createObjectLinkingLayer(LLJITBuilderState
&S
, ExecutionSession
&ES
) {
961 // If the config state provided an ObjectLinkingLayer factory then use it.
962 if (S
.CreateObjectLinkingLayer
)
963 return S
.CreateObjectLinkingLayer(ES
, S
.JTMB
->getTargetTriple());
965 // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
966 // a new SectionMemoryManager for each object.
967 auto GetMemMgr
= []() { return std::make_unique
<SectionMemoryManager
>(); };
969 std::make_unique
<RTDyldObjectLinkingLayer
>(ES
, std::move(GetMemMgr
));
971 if (S
.JTMB
->getTargetTriple().isOSBinFormatCOFF()) {
972 Layer
->setOverrideObjectFlagsWithResponsibilityFlags(true);
973 Layer
->setAutoClaimResponsibilityForObjectSymbols(true);
976 if (S
.JTMB
->getTargetTriple().isOSBinFormatELF() &&
977 (S
.JTMB
->getTargetTriple().getArch() == Triple::ArchType::ppc64
||
978 S
.JTMB
->getTargetTriple().getArch() == Triple::ArchType::ppc64le
))
979 Layer
->setAutoClaimResponsibilityForObjectSymbols(true);
981 // FIXME: Explicit conversion to std::unique_ptr<ObjectLayer> added to silence
982 // errors from some GCC / libstdc++ bots. Remove this conversion (i.e.
983 // just return ObjLinkingLayer) once those bots are upgraded.
984 return std::unique_ptr
<ObjectLayer
>(std::move(Layer
));
987 Expected
<std::unique_ptr
<IRCompileLayer::IRCompiler
>>
988 LLJIT::createCompileFunction(LLJITBuilderState
&S
,
989 JITTargetMachineBuilder JTMB
) {
991 /// If there is a custom compile function creator set then use it.
992 if (S
.CreateCompileFunction
)
993 return S
.CreateCompileFunction(std::move(JTMB
));
995 // If using a custom EPC then use a ConcurrentIRCompiler by default.
996 if (*S
.SupportConcurrentCompilation
)
997 return std::make_unique
<ConcurrentIRCompiler
>(std::move(JTMB
));
999 auto TM
= JTMB
.createTargetMachine();
1001 return TM
.takeError();
1003 return std::make_unique
<TMOwningSimpleCompiler
>(std::move(*TM
));
1006 LLJIT::LLJIT(LLJITBuilderState
&S
, Error
&Err
)
1007 : DL(std::move(*S
.DL
)), TT(S
.JTMB
->getTargetTriple()) {
1009 ErrorAsOutParameter
_(&Err
);
1011 assert(!(S
.EPC
&& S
.ES
) && "EPC and ES should not both be set");
1014 ES
= std::make_unique
<ExecutionSession
>(std::move(S
.EPC
));
1016 ES
= std::move(S
.ES
);
1018 if (auto EPC
= SelfExecutorProcessControl::Create()) {
1019 ES
= std::make_unique
<ExecutionSession
>(std::move(*EPC
));
1021 Err
= EPC
.takeError();
1026 auto ObjLayer
= createObjectLinkingLayer(S
, *ES
);
1028 Err
= ObjLayer
.takeError();
1031 ObjLinkingLayer
= std::move(*ObjLayer
);
1033 std::make_unique
<ObjectTransformLayer
>(*ES
, *ObjLinkingLayer
);
1036 auto CompileFunction
= createCompileFunction(S
, std::move(*S
.JTMB
));
1037 if (!CompileFunction
) {
1038 Err
= CompileFunction
.takeError();
1041 CompileLayer
= std::make_unique
<IRCompileLayer
>(
1042 *ES
, *ObjTransformLayer
, std::move(*CompileFunction
));
1043 TransformLayer
= std::make_unique
<IRTransformLayer
>(*ES
, *CompileLayer
);
1044 InitHelperTransformLayer
=
1045 std::make_unique
<IRTransformLayer
>(*ES
, *TransformLayer
);
1048 if (*S
.SupportConcurrentCompilation
)
1049 InitHelperTransformLayer
->setCloneToNewContextOnEmit(true);
1051 if (S
.SetupProcessSymbolsJITDylib
) {
1052 if (auto ProcSymsJD
= S
.SetupProcessSymbolsJITDylib(*this)) {
1053 ProcessSymbols
= ProcSymsJD
->get();
1055 Err
= ProcSymsJD
.takeError();
1060 if (S
.PrePlatformSetup
) {
1061 if (auto Err2
= S
.PrePlatformSetup(*this)) {
1062 Err
= std::move(Err2
);
1067 if (!S
.SetUpPlatform
)
1068 S
.SetUpPlatform
= setUpGenericLLVMIRPlatform
;
1070 if (auto PlatformJDOrErr
= S
.SetUpPlatform(*this)) {
1071 Platform
= PlatformJDOrErr
->get();
1073 DefaultLinks
.push_back(
1074 {Platform
, JITDylibLookupFlags::MatchExportedSymbolsOnly
});
1076 Err
= PlatformJDOrErr
.takeError();
1080 if (S
.LinkProcessSymbolsByDefault
)
1081 DefaultLinks
.push_back(
1082 {ProcessSymbols
, JITDylibLookupFlags::MatchExportedSymbolsOnly
});
1084 if (auto MainOrErr
= createJITDylib("main"))
1087 Err
= MainOrErr
.takeError();
1092 std::string
LLJIT::mangle(StringRef UnmangledName
) const {
1093 std::string MangledName
;
1095 raw_string_ostream
MangledNameStream(MangledName
);
1096 Mangler::getNameWithPrefix(MangledNameStream
, UnmangledName
, DL
);
1101 Error
LLJIT::applyDataLayout(Module
&M
) {
1102 if (M
.getDataLayout().isDefault())
1103 M
.setDataLayout(DL
);
1105 if (M
.getDataLayout() != DL
)
1106 return make_error
<StringError
>(
1107 "Added modules have incompatible data layouts: " +
1108 M
.getDataLayout().getStringRepresentation() + " (module) vs " +
1109 DL
.getStringRepresentation() + " (jit)",
1110 inconvertibleErrorCode());
1112 return Error::success();
1115 Error
setUpOrcPlatformManually(LLJIT
&J
) {
1116 LLVM_DEBUG({ dbgs() << "Setting up orc platform support for LLJIT\n"; });
1117 J
.setPlatformSupport(std::make_unique
<ORCPlatformSupport
>(J
));
1118 return Error::success();
1121 class LoadAndLinkDynLibrary
{
1123 LoadAndLinkDynLibrary(LLJIT
&J
) : J(J
) {}
1124 Error
operator()(JITDylib
&JD
, StringRef DLLName
) {
1125 if (!DLLName
.ends_with_insensitive(".dll"))
1126 return make_error
<StringError
>("DLLName not ending with .dll",
1127 inconvertibleErrorCode());
1128 auto DLLNameStr
= DLLName
.str(); // Guarantees null-termination.
1129 auto DLLJD
= J
.loadPlatformDynamicLibrary(DLLNameStr
.c_str());
1131 return DLLJD
.takeError();
1132 JD
.addToLinkOrder(*DLLJD
);
1133 return Error::success();
1140 Expected
<JITDylibSP
> ExecutorNativePlatform::operator()(LLJIT
&J
) {
1141 auto ProcessSymbolsJD
= J
.getProcessSymbolsJITDylib();
1142 if (!ProcessSymbolsJD
)
1143 return make_error
<StringError
>(
1144 "Native platforms require a process symbols JITDylib",
1145 inconvertibleErrorCode());
1147 const Triple
&TT
= J
.getTargetTriple();
1148 ObjectLinkingLayer
*ObjLinkingLayer
=
1149 dyn_cast
<ObjectLinkingLayer
>(&J
.getObjLinkingLayer());
1151 if (!ObjLinkingLayer
)
1152 return make_error
<StringError
>(
1153 "ExecutorNativePlatform requires ObjectLinkingLayer",
1154 inconvertibleErrorCode());
1156 std::unique_ptr
<MemoryBuffer
> RuntimeArchiveBuffer
;
1157 if (OrcRuntime
.index() == 0) {
1158 auto A
= errorOrToExpected(MemoryBuffer::getFile(std::get
<0>(OrcRuntime
)));
1160 return A
.takeError();
1161 RuntimeArchiveBuffer
= std::move(*A
);
1163 RuntimeArchiveBuffer
= std::move(std::get
<1>(OrcRuntime
));
1165 auto &ES
= J
.getExecutionSession();
1166 auto &PlatformJD
= ES
.createBareJITDylib("<Platform>");
1167 PlatformJD
.addToLinkOrder(*ProcessSymbolsJD
);
1169 J
.setPlatformSupport(std::make_unique
<ORCPlatformSupport
>(J
));
1171 switch (TT
.getObjectFormat()) {
1172 case Triple::COFF
: {
1173 const char *VCRuntimePath
= nullptr;
1174 bool StaticVCRuntime
= false;
1176 VCRuntimePath
= VCRuntime
->first
.c_str();
1177 StaticVCRuntime
= VCRuntime
->second
;
1179 if (auto P
= COFFPlatform::Create(
1180 *ObjLinkingLayer
, PlatformJD
, std::move(RuntimeArchiveBuffer
),
1181 LoadAndLinkDynLibrary(J
), StaticVCRuntime
, VCRuntimePath
))
1182 J
.getExecutionSession().setPlatform(std::move(*P
));
1184 return P
.takeError();
1188 auto G
= StaticLibraryDefinitionGenerator::Create(
1189 *ObjLinkingLayer
, std::move(RuntimeArchiveBuffer
));
1191 return G
.takeError();
1194 ELFNixPlatform::Create(*ObjLinkingLayer
, PlatformJD
, std::move(*G
)))
1195 J
.getExecutionSession().setPlatform(std::move(*P
));
1197 return P
.takeError();
1200 case Triple::MachO
: {
1201 auto G
= StaticLibraryDefinitionGenerator::Create(
1202 *ObjLinkingLayer
, std::move(RuntimeArchiveBuffer
));
1204 return G
.takeError();
1207 MachOPlatform::Create(*ObjLinkingLayer
, PlatformJD
, std::move(*G
)))
1208 ES
.setPlatform(std::move(*P
));
1210 return P
.takeError();
1214 return make_error
<StringError
>("Unsupported object format in triple " +
1216 inconvertibleErrorCode());
1222 Expected
<JITDylibSP
> setUpGenericLLVMIRPlatform(LLJIT
&J
) {
1224 { dbgs() << "Setting up GenericLLVMIRPlatform support for LLJIT\n"; });
1225 auto ProcessSymbolsJD
= J
.getProcessSymbolsJITDylib();
1226 if (!ProcessSymbolsJD
)
1227 return make_error
<StringError
>(
1228 "Native platforms require a process symbols JITDylib",
1229 inconvertibleErrorCode());
1231 auto &PlatformJD
= J
.getExecutionSession().createBareJITDylib("<Platform>");
1232 PlatformJD
.addToLinkOrder(*ProcessSymbolsJD
);
1234 J
.setPlatformSupport(
1235 std::make_unique
<GenericLLVMIRPlatformSupport
>(J
, PlatformJD
));
1240 Expected
<JITDylibSP
> setUpInactivePlatform(LLJIT
&J
) {
1242 { dbgs() << "Explicitly deactivated platform support for LLJIT\n"; });
1243 J
.setPlatformSupport(std::make_unique
<InactivePlatformSupport
>());
1247 Error
LLLazyJITBuilderState::prepareForConstruction() {
1248 if (auto Err
= LLJITBuilderState::prepareForConstruction())
1250 TT
= JTMB
->getTargetTriple();
1251 return Error::success();
1254 Error
LLLazyJIT::addLazyIRModule(JITDylib
&JD
, ThreadSafeModule TSM
) {
1255 assert(TSM
&& "Can not add null module");
1257 if (auto Err
= TSM
.withModuleDo(
1258 [&](Module
&M
) -> Error
{ return applyDataLayout(M
); }))
1261 return CODLayer
->add(JD
, std::move(TSM
));
1264 LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState
&S
, Error
&Err
) : LLJIT(S
, Err
) {
1266 // If LLJIT construction failed then bail out.
1270 ErrorAsOutParameter
_(&Err
);
1272 /// Take/Create the lazy-compile callthrough manager.
1274 LCTMgr
= std::move(S
.LCTMgr
);
1276 if (auto LCTMgrOrErr
= createLocalLazyCallThroughManager(
1277 S
.TT
, *ES
, S
.LazyCompileFailureAddr
))
1278 LCTMgr
= std::move(*LCTMgrOrErr
);
1280 Err
= LCTMgrOrErr
.takeError();
1285 // Take/Create the indirect stubs manager builder.
1286 auto ISMBuilder
= std::move(S
.ISMBuilder
);
1288 // If none was provided, try to build one.
1290 ISMBuilder
= createLocalIndirectStubsManagerBuilder(S
.TT
);
1292 // No luck. Bail out.
1294 Err
= make_error
<StringError
>("Could not construct "
1295 "IndirectStubsManagerBuilder for target " +
1297 inconvertibleErrorCode());
1301 // Create the IP Layer.
1302 IPLayer
= std::make_unique
<IRPartitionLayer
>(*ES
, *InitHelperTransformLayer
);
1304 // Create the COD layer.
1305 CODLayer
= std::make_unique
<CompileOnDemandLayer
>(*ES
, *IPLayer
, *LCTMgr
,
1306 std::move(ISMBuilder
));
1308 if (*S
.SupportConcurrentCompilation
)
1309 CODLayer
->setCloneToNewContextOnEmit(true);
1312 // In-process LLJIT uses eh-frame section wrappers via EPC, so we need to force
1313 // them to be linked in.
1314 LLVM_ATTRIBUTE_USED
void linkComponents() {
1315 errs() << (void *)&llvm_orc_registerEHFrameSectionWrapper
1316 << (void *)&llvm_orc_deregisterEHFrameSectionWrapper
;
1319 } // End namespace orc.
1320 } // End namespace llvm.