1 //===--------------- OrcV2CBindings.cpp - C bindings OrcV2 APIs -----------===//
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-c/LLJIT.h"
10 #include "llvm-c/Orc.h"
11 #include "llvm-c/OrcEE.h"
12 #include "llvm-c/TargetMachine.h"
14 #include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"
15 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
16 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
17 #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
18 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
19 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
22 using namespace llvm::orc
;
27 class InProgressLookupState
;
29 class OrcV2CAPIHelper
{
31 static InProgressLookupState
*extractLookupState(LookupState
&LS
) {
32 return LS
.IPLS
.release();
35 static void resetLookupState(LookupState
&LS
, InProgressLookupState
*IPLS
) {
36 return LS
.reset(IPLS
);
43 inline LLVMOrcSymbolStringPoolEntryRef
wrap(SymbolStringPoolEntryUnsafe E
) {
44 return reinterpret_cast<LLVMOrcSymbolStringPoolEntryRef
>(E
.rawPtr());
47 inline SymbolStringPoolEntryUnsafe
unwrap(LLVMOrcSymbolStringPoolEntryRef E
) {
48 return reinterpret_cast<SymbolStringPoolEntryUnsafe::PoolEntry
*>(E
);
51 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionSession
, LLVMOrcExecutionSessionRef
)
52 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SymbolStringPool
, LLVMOrcSymbolStringPoolRef
)
53 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationUnit
,
54 LLVMOrcMaterializationUnitRef
)
55 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationResponsibility
,
56 LLVMOrcMaterializationResponsibilityRef
)
57 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITDylib
, LLVMOrcJITDylibRef
)
58 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ResourceTracker
, LLVMOrcResourceTrackerRef
)
59 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DefinitionGenerator
,
60 LLVMOrcDefinitionGeneratorRef
)
61 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(InProgressLookupState
, LLVMOrcLookupStateRef
)
62 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeContext
,
63 LLVMOrcThreadSafeContextRef
)
64 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeModule
, LLVMOrcThreadSafeModuleRef
)
65 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITTargetMachineBuilder
,
66 LLVMOrcJITTargetMachineBuilderRef
)
67 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectLayer
, LLVMOrcObjectLayerRef
)
68 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRTransformLayer
, LLVMOrcIRTransformLayerRef
)
69 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectTransformLayer
,
70 LLVMOrcObjectTransformLayerRef
)
71 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DumpObjects
, LLVMOrcDumpObjectsRef
)
72 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IndirectStubsManager
,
73 LLVMOrcIndirectStubsManagerRef
)
74 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LazyCallThroughManager
,
75 LLVMOrcLazyCallThroughManagerRef
)
76 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJITBuilder
, LLVMOrcLLJITBuilderRef
)
77 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJIT
, LLVMOrcLLJITRef
)
78 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine
, LLVMTargetMachineRef
)
82 class OrcCAPIMaterializationUnit
: public llvm::orc::MaterializationUnit
{
84 OrcCAPIMaterializationUnit(
85 std::string Name
, SymbolFlagsMap InitialSymbolFlags
,
86 SymbolStringPtr InitSymbol
, void *Ctx
,
87 LLVMOrcMaterializationUnitMaterializeFunction Materialize
,
88 LLVMOrcMaterializationUnitDiscardFunction Discard
,
89 LLVMOrcMaterializationUnitDestroyFunction Destroy
)
90 : llvm::orc::MaterializationUnit(
91 Interface(std::move(InitialSymbolFlags
), std::move(InitSymbol
))),
92 Name(std::move(Name
)), Ctx(Ctx
), Materialize(Materialize
),
93 Discard(Discard
), Destroy(Destroy
) {}
95 ~OrcCAPIMaterializationUnit() {
100 StringRef
getName() const override
{ return Name
; }
102 void materialize(std::unique_ptr
<MaterializationResponsibility
> R
) override
{
105 Materialize(Tmp
, wrap(R
.release()));
109 void discard(const JITDylib
&JD
, const SymbolStringPtr
&Name
) override
{
110 Discard(Ctx
, wrap(&JD
), wrap(SymbolStringPoolEntryUnsafe::from(Name
)));
115 LLVMOrcMaterializationUnitMaterializeFunction Materialize
= nullptr;
116 LLVMOrcMaterializationUnitDiscardFunction Discard
= nullptr;
117 LLVMOrcMaterializationUnitDestroyFunction Destroy
= nullptr;
120 static JITSymbolFlags
toJITSymbolFlags(LLVMJITSymbolFlags F
) {
124 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsExported
)
125 JSF
|= JITSymbolFlags::Exported
;
126 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsWeak
)
127 JSF
|= JITSymbolFlags::Weak
;
128 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsCallable
)
129 JSF
|= JITSymbolFlags::Callable
;
130 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly
)
131 JSF
|= JITSymbolFlags::MaterializationSideEffectsOnly
;
133 JSF
.getTargetFlags() = F
.TargetFlags
;
138 static LLVMJITSymbolFlags
fromJITSymbolFlags(JITSymbolFlags JSF
) {
139 LLVMJITSymbolFlags F
= {0, 0};
140 if (JSF
& JITSymbolFlags::Exported
)
141 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsExported
;
142 if (JSF
& JITSymbolFlags::Weak
)
143 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsWeak
;
144 if (JSF
& JITSymbolFlags::Callable
)
145 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsCallable
;
146 if (JSF
& JITSymbolFlags::MaterializationSideEffectsOnly
)
147 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly
;
149 F
.TargetFlags
= JSF
.getTargetFlags();
154 static SymbolNameSet
toSymbolNameSet(LLVMOrcCSymbolsList Symbols
) {
155 SymbolNameSet Result
;
156 Result
.reserve(Symbols
.Length
);
157 for (size_t I
= 0; I
!= Symbols
.Length
; ++I
)
158 Result
.insert(unwrap(Symbols
.Symbols
[I
]).moveToSymbolStringPtr());
162 static SymbolMap
toSymbolMap(LLVMOrcCSymbolMapPairs Syms
, size_t NumPairs
) {
164 for (size_t I
= 0; I
!= NumPairs
; ++I
) {
165 JITSymbolFlags Flags
= toJITSymbolFlags(Syms
[I
].Sym
.Flags
);
166 SM
[unwrap(Syms
[I
].Name
).moveToSymbolStringPtr()] = {
167 ExecutorAddr(Syms
[I
].Sym
.Address
), Flags
};
172 static SymbolDependenceMap
173 toSymbolDependenceMap(LLVMOrcCDependenceMapPairs Pairs
, size_t NumPairs
) {
174 SymbolDependenceMap SDM
;
175 for (size_t I
= 0; I
!= NumPairs
; ++I
) {
176 JITDylib
*JD
= unwrap(Pairs
[I
].JD
);
179 for (size_t J
= 0; J
!= Pairs
[I
].Names
.Length
; ++J
) {
180 auto Sym
= Pairs
[I
].Names
.Symbols
[J
];
181 Names
.insert(unwrap(Sym
).moveToSymbolStringPtr());
188 static LookupKind
toLookupKind(LLVMOrcLookupKind K
) {
190 case LLVMOrcLookupKindStatic
:
191 return LookupKind::Static
;
192 case LLVMOrcLookupKindDLSym
:
193 return LookupKind::DLSym
;
195 llvm_unreachable("unrecognized LLVMOrcLookupKind value");
198 static LLVMOrcLookupKind
fromLookupKind(LookupKind K
) {
200 case LookupKind::Static
:
201 return LLVMOrcLookupKindStatic
;
202 case LookupKind::DLSym
:
203 return LLVMOrcLookupKindDLSym
;
205 llvm_unreachable("unrecognized LookupKind value");
208 static JITDylibLookupFlags
209 toJITDylibLookupFlags(LLVMOrcJITDylibLookupFlags LF
) {
211 case LLVMOrcJITDylibLookupFlagsMatchExportedSymbolsOnly
:
212 return JITDylibLookupFlags::MatchExportedSymbolsOnly
;
213 case LLVMOrcJITDylibLookupFlagsMatchAllSymbols
:
214 return JITDylibLookupFlags::MatchAllSymbols
;
216 llvm_unreachable("unrecognized LLVMOrcJITDylibLookupFlags value");
219 static LLVMOrcJITDylibLookupFlags
220 fromJITDylibLookupFlags(JITDylibLookupFlags LF
) {
222 case JITDylibLookupFlags::MatchExportedSymbolsOnly
:
223 return LLVMOrcJITDylibLookupFlagsMatchExportedSymbolsOnly
;
224 case JITDylibLookupFlags::MatchAllSymbols
:
225 return LLVMOrcJITDylibLookupFlagsMatchAllSymbols
;
227 llvm_unreachable("unrecognized JITDylibLookupFlags value");
230 static SymbolLookupFlags
toSymbolLookupFlags(LLVMOrcSymbolLookupFlags SLF
) {
232 case LLVMOrcSymbolLookupFlagsRequiredSymbol
:
233 return SymbolLookupFlags::RequiredSymbol
;
234 case LLVMOrcSymbolLookupFlagsWeaklyReferencedSymbol
:
235 return SymbolLookupFlags::WeaklyReferencedSymbol
;
237 llvm_unreachable("unrecognized LLVMOrcSymbolLookupFlags value");
240 static LLVMOrcSymbolLookupFlags
fromSymbolLookupFlags(SymbolLookupFlags SLF
) {
242 case SymbolLookupFlags::RequiredSymbol
:
243 return LLVMOrcSymbolLookupFlagsRequiredSymbol
;
244 case SymbolLookupFlags::WeaklyReferencedSymbol
:
245 return LLVMOrcSymbolLookupFlagsWeaklyReferencedSymbol
;
247 llvm_unreachable("unrecognized SymbolLookupFlags value");
250 static LLVMJITEvaluatedSymbol
251 fromExecutorSymbolDef(const ExecutorSymbolDef
&S
) {
252 return {S
.getAddress().getValue(), fromJITSymbolFlags(S
.getFlags())};
255 } // end anonymous namespace
260 class CAPIDefinitionGenerator final
: public DefinitionGenerator
{
262 CAPIDefinitionGenerator(
263 LLVMOrcDisposeCAPIDefinitionGeneratorFunction Dispose
, void *Ctx
,
264 LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate
)
265 : Dispose(Dispose
), Ctx(Ctx
), TryToGenerate(TryToGenerate
) {}
267 ~CAPIDefinitionGenerator() {
272 Error
tryToGenerate(LookupState
&LS
, LookupKind K
, JITDylib
&JD
,
273 JITDylibLookupFlags JDLookupFlags
,
274 const SymbolLookupSet
&LookupSet
) override
{
276 // Take the lookup state.
277 LLVMOrcLookupStateRef LSR
= ::wrap(OrcV2CAPIHelper::extractLookupState(LS
));
279 // Translate the lookup kind.
280 LLVMOrcLookupKind CLookupKind
= fromLookupKind(K
);
282 // Translate the JITDylibLookupFlags.
283 LLVMOrcJITDylibLookupFlags CJDLookupFlags
=
284 fromJITDylibLookupFlags(JDLookupFlags
);
286 // Translate the lookup set.
287 std::vector
<LLVMOrcCLookupSetElement
> CLookupSet
;
288 CLookupSet
.reserve(LookupSet
.size());
289 for (auto &KV
: LookupSet
) {
290 LLVMOrcSymbolStringPoolEntryRef Name
=
291 ::wrap(SymbolStringPoolEntryUnsafe::from(KV
.first
));
292 LLVMOrcSymbolLookupFlags SLF
= fromSymbolLookupFlags(KV
.second
);
293 CLookupSet
.push_back({Name
, SLF
});
296 // Run the C TryToGenerate function.
297 auto Err
= unwrap(TryToGenerate(::wrap(this), Ctx
, &LSR
, CLookupKind
,
298 ::wrap(&JD
), CJDLookupFlags
,
299 CLookupSet
.data(), CLookupSet
.size()));
301 // Restore the lookup state.
302 OrcV2CAPIHelper::resetLookupState(LS
, ::unwrap(LSR
));
308 LLVMOrcDisposeCAPIDefinitionGeneratorFunction Dispose
;
310 LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate
;
313 } // end namespace orc
314 } // end namespace llvm
316 void LLVMOrcExecutionSessionSetErrorReporter(
317 LLVMOrcExecutionSessionRef ES
, LLVMOrcErrorReporterFunction ReportError
,
319 unwrap(ES
)->setErrorReporter(
320 [=](Error Err
) { ReportError(Ctx
, wrap(std::move(Err
))); });
323 LLVMOrcSymbolStringPoolRef
324 LLVMOrcExecutionSessionGetSymbolStringPool(LLVMOrcExecutionSessionRef ES
) {
326 unwrap(ES
)->getExecutorProcessControl().getSymbolStringPool().get());
329 void LLVMOrcSymbolStringPoolClearDeadEntries(LLVMOrcSymbolStringPoolRef SSP
) {
330 unwrap(SSP
)->clearDeadEntries();
333 LLVMOrcSymbolStringPoolEntryRef
334 LLVMOrcExecutionSessionIntern(LLVMOrcExecutionSessionRef ES
, const char *Name
) {
335 return wrap(SymbolStringPoolEntryUnsafe::take(unwrap(ES
)->intern(Name
)));
338 void LLVMOrcExecutionSessionLookup(
339 LLVMOrcExecutionSessionRef ES
, LLVMOrcLookupKind K
,
340 LLVMOrcCJITDylibSearchOrder SearchOrder
, size_t SearchOrderSize
,
341 LLVMOrcCLookupSet Symbols
, size_t SymbolsSize
,
342 LLVMOrcExecutionSessionLookupHandleResultFunction HandleResult
, void *Ctx
) {
343 assert(ES
&& "ES cannot be null");
344 assert(SearchOrder
&& "SearchOrder cannot be null");
345 assert(Symbols
&& "Symbols cannot be null");
346 assert(HandleResult
&& "HandleResult cannot be null");
348 JITDylibSearchOrder SO
;
349 for (size_t I
= 0; I
!= SearchOrderSize
; ++I
)
350 SO
.push_back({unwrap(SearchOrder
[I
].JD
),
351 toJITDylibLookupFlags(SearchOrder
[I
].JDLookupFlags
)});
354 for (size_t I
= 0; I
!= SymbolsSize
; ++I
)
355 SLS
.add(unwrap(Symbols
[I
].Name
).moveToSymbolStringPtr(),
356 toSymbolLookupFlags(Symbols
[I
].LookupFlags
));
359 toLookupKind(K
), SO
, std::move(SLS
), SymbolState::Ready
,
360 [HandleResult
, Ctx
](Expected
<SymbolMap
> Result
) {
362 SmallVector
<LLVMOrcCSymbolMapPair
> CResult
;
363 for (auto &KV
: *Result
)
364 CResult
.push_back(LLVMOrcCSymbolMapPair
{
365 wrap(SymbolStringPoolEntryUnsafe::from(KV
.first
)),
366 fromExecutorSymbolDef(KV
.second
)});
367 HandleResult(LLVMErrorSuccess
, CResult
.data(), CResult
.size(), Ctx
);
369 HandleResult(wrap(Result
.takeError()), nullptr, 0, Ctx
);
371 NoDependenciesToRegister
);
374 void LLVMOrcRetainSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S
) {
378 void LLVMOrcReleaseSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S
) {
382 const char *LLVMOrcSymbolStringPoolEntryStr(LLVMOrcSymbolStringPoolEntryRef S
) {
383 return unwrap(S
).rawPtr()->getKey().data();
386 LLVMOrcResourceTrackerRef
387 LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD
) {
388 auto RT
= unwrap(JD
)->createResourceTracker();
389 // Retain the pointer for the C API client.
391 return wrap(RT
.get());
394 LLVMOrcResourceTrackerRef
395 LLVMOrcJITDylibGetDefaultResourceTracker(LLVMOrcJITDylibRef JD
) {
396 auto RT
= unwrap(JD
)->getDefaultResourceTracker();
397 // Retain the pointer for the C API client.
398 return wrap(RT
.get());
401 void LLVMOrcReleaseResourceTracker(LLVMOrcResourceTrackerRef RT
) {
402 ResourceTrackerSP
TmpRT(unwrap(RT
));
406 void LLVMOrcResourceTrackerTransferTo(LLVMOrcResourceTrackerRef SrcRT
,
407 LLVMOrcResourceTrackerRef DstRT
) {
408 ResourceTrackerSP
TmpRT(unwrap(SrcRT
));
409 TmpRT
->transferTo(*unwrap(DstRT
));
412 LLVMErrorRef
LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT
) {
413 ResourceTrackerSP
TmpRT(unwrap(RT
));
414 return wrap(TmpRT
->remove());
417 void LLVMOrcDisposeDefinitionGenerator(LLVMOrcDefinitionGeneratorRef DG
) {
418 std::unique_ptr
<DefinitionGenerator
> TmpDG(unwrap(DG
));
421 void LLVMOrcDisposeMaterializationUnit(LLVMOrcMaterializationUnitRef MU
) {
422 std::unique_ptr
<MaterializationUnit
> TmpMU(unwrap(MU
));
425 LLVMOrcMaterializationUnitRef
LLVMOrcCreateCustomMaterializationUnit(
426 const char *Name
, void *Ctx
, LLVMOrcCSymbolFlagsMapPairs Syms
,
427 size_t NumSyms
, LLVMOrcSymbolStringPoolEntryRef InitSym
,
428 LLVMOrcMaterializationUnitMaterializeFunction Materialize
,
429 LLVMOrcMaterializationUnitDiscardFunction Discard
,
430 LLVMOrcMaterializationUnitDestroyFunction Destroy
) {
432 for (size_t I
= 0; I
!= NumSyms
; ++I
)
433 SFM
[unwrap(Syms
[I
].Name
).moveToSymbolStringPtr()] =
434 toJITSymbolFlags(Syms
[I
].Flags
);
436 auto IS
= unwrap(InitSym
).moveToSymbolStringPtr();
438 return wrap(new OrcCAPIMaterializationUnit(
439 Name
, std::move(SFM
), std::move(IS
), Ctx
, Materialize
, Discard
, Destroy
));
442 LLVMOrcMaterializationUnitRef
443 LLVMOrcAbsoluteSymbols(LLVMOrcCSymbolMapPairs Syms
, size_t NumPairs
) {
444 SymbolMap SM
= toSymbolMap(Syms
, NumPairs
);
445 return wrap(absoluteSymbols(std::move(SM
)).release());
448 LLVMOrcMaterializationUnitRef
LLVMOrcLazyReexports(
449 LLVMOrcLazyCallThroughManagerRef LCTM
, LLVMOrcIndirectStubsManagerRef ISM
,
450 LLVMOrcJITDylibRef SourceJD
, LLVMOrcCSymbolAliasMapPairs CallableAliases
,
454 for (size_t I
= 0; I
!= NumPairs
; ++I
) {
455 auto pair
= CallableAliases
[I
];
456 JITSymbolFlags Flags
= toJITSymbolFlags(pair
.Entry
.Flags
);
457 SymbolStringPtr Name
= unwrap(pair
.Entry
.Name
).moveToSymbolStringPtr();
458 SAM
[unwrap(pair
.Name
).moveToSymbolStringPtr()] =
459 SymbolAliasMapEntry(Name
, Flags
);
462 return wrap(lazyReexports(*unwrap(LCTM
), *unwrap(ISM
), *unwrap(SourceJD
),
467 void LLVMOrcDisposeMaterializationResponsibility(
468 LLVMOrcMaterializationResponsibilityRef MR
) {
469 std::unique_ptr
<MaterializationResponsibility
> TmpMR(unwrap(MR
));
472 LLVMOrcJITDylibRef
LLVMOrcMaterializationResponsibilityGetTargetDylib(
473 LLVMOrcMaterializationResponsibilityRef MR
) {
474 return wrap(&unwrap(MR
)->getTargetJITDylib());
477 LLVMOrcExecutionSessionRef
478 LLVMOrcMaterializationResponsibilityGetExecutionSession(
479 LLVMOrcMaterializationResponsibilityRef MR
) {
480 return wrap(&unwrap(MR
)->getExecutionSession());
483 LLVMOrcCSymbolFlagsMapPairs
LLVMOrcMaterializationResponsibilityGetSymbols(
484 LLVMOrcMaterializationResponsibilityRef MR
, size_t *NumPairs
) {
486 auto Symbols
= unwrap(MR
)->getSymbols();
487 LLVMOrcCSymbolFlagsMapPairs Result
= static_cast<LLVMOrcCSymbolFlagsMapPairs
>(
488 safe_malloc(Symbols
.size() * sizeof(LLVMOrcCSymbolFlagsMapPair
)));
490 for (auto const &pair
: Symbols
) {
491 auto Name
= wrap(SymbolStringPoolEntryUnsafe::from(pair
.first
));
492 auto Flags
= pair
.second
;
493 Result
[I
] = {Name
, fromJITSymbolFlags(Flags
)};
496 *NumPairs
= Symbols
.size();
500 void LLVMOrcDisposeCSymbolFlagsMap(LLVMOrcCSymbolFlagsMapPairs Pairs
) {
504 LLVMOrcSymbolStringPoolEntryRef
505 LLVMOrcMaterializationResponsibilityGetInitializerSymbol(
506 LLVMOrcMaterializationResponsibilityRef MR
) {
507 auto Sym
= unwrap(MR
)->getInitializerSymbol();
508 return wrap(SymbolStringPoolEntryUnsafe::from(Sym
));
511 LLVMOrcSymbolStringPoolEntryRef
*
512 LLVMOrcMaterializationResponsibilityGetRequestedSymbols(
513 LLVMOrcMaterializationResponsibilityRef MR
, size_t *NumSymbols
) {
515 auto Symbols
= unwrap(MR
)->getRequestedSymbols();
516 LLVMOrcSymbolStringPoolEntryRef
*Result
=
517 static_cast<LLVMOrcSymbolStringPoolEntryRef
*>(safe_malloc(
518 Symbols
.size() * sizeof(LLVMOrcSymbolStringPoolEntryRef
)));
520 for (auto &Name
: Symbols
) {
521 Result
[I
] = wrap(SymbolStringPoolEntryUnsafe::from(Name
));
524 *NumSymbols
= Symbols
.size();
528 void LLVMOrcDisposeSymbols(LLVMOrcSymbolStringPoolEntryRef
*Symbols
) {
532 LLVMErrorRef
LLVMOrcMaterializationResponsibilityNotifyResolved(
533 LLVMOrcMaterializationResponsibilityRef MR
, LLVMOrcCSymbolMapPairs Symbols
,
535 SymbolMap SM
= toSymbolMap(Symbols
, NumSymbols
);
536 return wrap(unwrap(MR
)->notifyResolved(std::move(SM
)));
539 LLVMErrorRef
LLVMOrcMaterializationResponsibilityNotifyEmitted(
540 LLVMOrcMaterializationResponsibilityRef MR
,
541 LLVMOrcCSymbolDependenceGroup
*SymbolDepGroups
, size_t NumSymbolDepGroups
) {
542 std::vector
<SymbolDependenceGroup
> SDGs
;
543 SDGs
.reserve(NumSymbolDepGroups
);
544 for (size_t I
= 0; I
!= NumSymbolDepGroups
; ++I
) {
545 SDGs
.push_back(SymbolDependenceGroup());
546 auto &SDG
= SDGs
.back();
547 SDG
.Symbols
= toSymbolNameSet(SymbolDepGroups
[I
].Symbols
);
548 SDG
.Dependencies
= toSymbolDependenceMap(
549 SymbolDepGroups
[I
].Dependencies
, SymbolDepGroups
[I
].NumDependencies
);
551 return wrap(unwrap(MR
)->notifyEmitted(SDGs
));
554 LLVMErrorRef
LLVMOrcMaterializationResponsibilityDefineMaterializing(
555 LLVMOrcMaterializationResponsibilityRef MR
,
556 LLVMOrcCSymbolFlagsMapPairs Syms
, size_t NumSyms
) {
558 for (size_t I
= 0; I
!= NumSyms
; ++I
)
559 SFM
[unwrap(Syms
[I
].Name
).moveToSymbolStringPtr()] =
560 toJITSymbolFlags(Syms
[I
].Flags
);
562 return wrap(unwrap(MR
)->defineMaterializing(std::move(SFM
)));
565 LLVMErrorRef
LLVMOrcMaterializationResponsibilityReplace(
566 LLVMOrcMaterializationResponsibilityRef MR
,
567 LLVMOrcMaterializationUnitRef MU
) {
568 std::unique_ptr
<MaterializationUnit
> TmpMU(unwrap(MU
));
569 return wrap(unwrap(MR
)->replace(std::move(TmpMU
)));
572 LLVMErrorRef
LLVMOrcMaterializationResponsibilityDelegate(
573 LLVMOrcMaterializationResponsibilityRef MR
,
574 LLVMOrcSymbolStringPoolEntryRef
*Symbols
, size_t NumSymbols
,
575 LLVMOrcMaterializationResponsibilityRef
*Result
) {
577 for (size_t I
= 0; I
!= NumSymbols
; I
++) {
578 Syms
.insert(unwrap(Symbols
[I
]).moveToSymbolStringPtr());
580 auto OtherMR
= unwrap(MR
)->delegate(Syms
);
583 return wrap(OtherMR
.takeError());
585 *Result
= wrap(OtherMR
->release());
586 return LLVMErrorSuccess
;
589 void LLVMOrcMaterializationResponsibilityFailMaterialization(
590 LLVMOrcMaterializationResponsibilityRef MR
) {
591 unwrap(MR
)->failMaterialization();
594 void LLVMOrcIRTransformLayerEmit(LLVMOrcIRTransformLayerRef IRLayer
,
595 LLVMOrcMaterializationResponsibilityRef MR
,
596 LLVMOrcThreadSafeModuleRef TSM
) {
597 std::unique_ptr
<ThreadSafeModule
> TmpTSM(unwrap(TSM
));
598 unwrap(IRLayer
)->emit(
599 std::unique_ptr
<MaterializationResponsibility
>(unwrap(MR
)),
604 LLVMOrcExecutionSessionCreateBareJITDylib(LLVMOrcExecutionSessionRef ES
,
606 return wrap(&unwrap(ES
)->createBareJITDylib(Name
));
610 LLVMOrcExecutionSessionCreateJITDylib(LLVMOrcExecutionSessionRef ES
,
611 LLVMOrcJITDylibRef
*Result
,
613 auto JD
= unwrap(ES
)->createJITDylib(Name
);
615 return wrap(JD
.takeError());
616 *Result
= wrap(&*JD
);
617 return LLVMErrorSuccess
;
621 LLVMOrcExecutionSessionGetJITDylibByName(LLVMOrcExecutionSessionRef ES
,
623 return wrap(unwrap(ES
)->getJITDylibByName(Name
));
626 LLVMErrorRef
LLVMOrcJITDylibDefine(LLVMOrcJITDylibRef JD
,
627 LLVMOrcMaterializationUnitRef MU
) {
628 std::unique_ptr
<MaterializationUnit
> TmpMU(unwrap(MU
));
630 if (auto Err
= unwrap(JD
)->define(TmpMU
)) {
632 return wrap(std::move(Err
));
634 return LLVMErrorSuccess
;
637 LLVMErrorRef
LLVMOrcJITDylibClear(LLVMOrcJITDylibRef JD
) {
638 return wrap(unwrap(JD
)->clear());
641 void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD
,
642 LLVMOrcDefinitionGeneratorRef DG
) {
643 unwrap(JD
)->addGenerator(std::unique_ptr
<DefinitionGenerator
>(unwrap(DG
)));
646 LLVMOrcDefinitionGeneratorRef
LLVMOrcCreateCustomCAPIDefinitionGenerator(
647 LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F
, void *Ctx
,
648 LLVMOrcDisposeCAPIDefinitionGeneratorFunction Dispose
) {
649 auto DG
= std::make_unique
<CAPIDefinitionGenerator
>(Dispose
, Ctx
, F
);
650 return wrap(DG
.release());
653 void LLVMOrcLookupStateContinueLookup(LLVMOrcLookupStateRef S
,
656 OrcV2CAPIHelper::resetLookupState(LS
, ::unwrap(S
));
657 LS
.continueLookup(unwrap(Err
));
660 LLVMErrorRef
LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(
661 LLVMOrcDefinitionGeneratorRef
*Result
, char GlobalPrefix
,
662 LLVMOrcSymbolPredicate Filter
, void *FilterCtx
) {
663 assert(Result
&& "Result can not be null");
664 assert((Filter
|| !FilterCtx
) &&
665 "if Filter is null then FilterCtx must also be null");
667 DynamicLibrarySearchGenerator::SymbolPredicate Pred
;
669 Pred
= [=](const SymbolStringPtr
&Name
) -> bool {
670 return Filter(FilterCtx
, wrap(SymbolStringPoolEntryUnsafe::from(Name
)));
673 auto ProcessSymsGenerator
=
674 DynamicLibrarySearchGenerator::GetForCurrentProcess(GlobalPrefix
, Pred
);
676 if (!ProcessSymsGenerator
) {
678 return wrap(ProcessSymsGenerator
.takeError());
681 *Result
= wrap(ProcessSymsGenerator
->release());
682 return LLVMErrorSuccess
;
685 LLVMErrorRef
LLVMOrcCreateDynamicLibrarySearchGeneratorForPath(
686 LLVMOrcDefinitionGeneratorRef
*Result
, const char *FileName
,
687 char GlobalPrefix
, LLVMOrcSymbolPredicate Filter
, void *FilterCtx
) {
688 assert(Result
&& "Result can not be null");
689 assert(FileName
&& "FileName can not be null");
690 assert((Filter
|| !FilterCtx
) &&
691 "if Filter is null then FilterCtx must also be null");
693 DynamicLibrarySearchGenerator::SymbolPredicate Pred
;
695 Pred
= [=](const SymbolStringPtr
&Name
) -> bool {
696 return Filter(FilterCtx
, wrap(SymbolStringPoolEntryUnsafe::from(Name
)));
699 auto LibrarySymsGenerator
=
700 DynamicLibrarySearchGenerator::Load(FileName
, GlobalPrefix
, Pred
);
702 if (!LibrarySymsGenerator
) {
704 return wrap(LibrarySymsGenerator
.takeError());
707 *Result
= wrap(LibrarySymsGenerator
->release());
708 return LLVMErrorSuccess
;
711 LLVMErrorRef
LLVMOrcCreateStaticLibrarySearchGeneratorForPath(
712 LLVMOrcDefinitionGeneratorRef
*Result
, LLVMOrcObjectLayerRef ObjLayer
,
713 const char *FileName
) {
714 assert(Result
&& "Result can not be null");
715 assert(FileName
&& "Filename can not be null");
716 assert(ObjLayer
&& "ObjectLayer can not be null");
718 auto LibrarySymsGenerator
=
719 StaticLibraryDefinitionGenerator::Load(*unwrap(ObjLayer
), FileName
);
720 if (!LibrarySymsGenerator
) {
722 return wrap(LibrarySymsGenerator
.takeError());
724 *Result
= wrap(LibrarySymsGenerator
->release());
725 return LLVMErrorSuccess
;
728 LLVMOrcThreadSafeContextRef
LLVMOrcCreateNewThreadSafeContext(void) {
729 return wrap(new ThreadSafeContext(std::make_unique
<LLVMContext
>()));
733 LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx
) {
734 return wrap(unwrap(TSCtx
)->getContext());
737 void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx
) {
738 delete unwrap(TSCtx
);
742 LLVMOrcThreadSafeModuleWithModuleDo(LLVMOrcThreadSafeModuleRef TSM
,
743 LLVMOrcGenericIRModuleOperationFunction F
,
745 return wrap(unwrap(TSM
)->withModuleDo(
746 [&](Module
&M
) { return unwrap(F(Ctx
, wrap(&M
))); }));
749 LLVMOrcThreadSafeModuleRef
750 LLVMOrcCreateNewThreadSafeModule(LLVMModuleRef M
,
751 LLVMOrcThreadSafeContextRef TSCtx
) {
753 new ThreadSafeModule(std::unique_ptr
<Module
>(unwrap(M
)), *unwrap(TSCtx
)));
756 void LLVMOrcDisposeThreadSafeModule(LLVMOrcThreadSafeModuleRef TSM
) {
760 LLVMErrorRef
LLVMOrcJITTargetMachineBuilderDetectHost(
761 LLVMOrcJITTargetMachineBuilderRef
*Result
) {
762 assert(Result
&& "Result can not be null");
764 auto JTMB
= JITTargetMachineBuilder::detectHost();
767 return wrap(JTMB
.takeError());
770 *Result
= wrap(new JITTargetMachineBuilder(std::move(*JTMB
)));
771 return LLVMErrorSuccess
;
774 LLVMOrcJITTargetMachineBuilderRef
775 LLVMOrcJITTargetMachineBuilderCreateFromTargetMachine(LLVMTargetMachineRef TM
) {
776 auto *TemplateTM
= unwrap(TM
);
779 std::make_unique
<JITTargetMachineBuilder
>(TemplateTM
->getTargetTriple());
782 .setCPU(TemplateTM
->getTargetCPU().str())
783 .setRelocationModel(TemplateTM
->getRelocationModel())
784 .setCodeModel(TemplateTM
->getCodeModel())
785 .setCodeGenOptLevel(TemplateTM
->getOptLevel())
786 .setFeatures(TemplateTM
->getTargetFeatureString())
787 .setOptions(TemplateTM
->Options
);
789 LLVMDisposeTargetMachine(TM
);
791 return wrap(JTMB
.release());
794 void LLVMOrcDisposeJITTargetMachineBuilder(
795 LLVMOrcJITTargetMachineBuilderRef JTMB
) {
799 char *LLVMOrcJITTargetMachineBuilderGetTargetTriple(
800 LLVMOrcJITTargetMachineBuilderRef JTMB
) {
801 auto Tmp
= unwrap(JTMB
)->getTargetTriple().str();
802 char *TargetTriple
= (char *)malloc(Tmp
.size() + 1);
803 strcpy(TargetTriple
, Tmp
.c_str());
807 void LLVMOrcJITTargetMachineBuilderSetTargetTriple(
808 LLVMOrcJITTargetMachineBuilderRef JTMB
, const char *TargetTriple
) {
809 unwrap(JTMB
)->getTargetTriple() = Triple(TargetTriple
);
812 LLVMErrorRef
LLVMOrcObjectLayerAddObjectFile(LLVMOrcObjectLayerRef ObjLayer
,
813 LLVMOrcJITDylibRef JD
,
814 LLVMMemoryBufferRef ObjBuffer
) {
815 return wrap(unwrap(ObjLayer
)->add(
816 *unwrap(JD
), std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
819 LLVMErrorRef
LLVMOrcObjectLayerAddObjectFileWithRT(LLVMOrcObjectLayerRef ObjLayer
,
820 LLVMOrcResourceTrackerRef RT
,
821 LLVMMemoryBufferRef ObjBuffer
) {
823 unwrap(ObjLayer
)->add(ResourceTrackerSP(unwrap(RT
)),
824 std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
827 void LLVMOrcObjectLayerEmit(LLVMOrcObjectLayerRef ObjLayer
,
828 LLVMOrcMaterializationResponsibilityRef R
,
829 LLVMMemoryBufferRef ObjBuffer
) {
830 unwrap(ObjLayer
)->emit(
831 std::unique_ptr
<MaterializationResponsibility
>(unwrap(R
)),
832 std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
)));
835 void LLVMOrcDisposeObjectLayer(LLVMOrcObjectLayerRef ObjLayer
) {
836 delete unwrap(ObjLayer
);
839 void LLVMOrcIRTransformLayerSetTransform(
840 LLVMOrcIRTransformLayerRef IRTransformLayer
,
841 LLVMOrcIRTransformLayerTransformFunction TransformFunction
, void *Ctx
) {
842 unwrap(IRTransformLayer
)
844 [=](ThreadSafeModule TSM
,
845 MaterializationResponsibility
&R
) -> Expected
<ThreadSafeModule
> {
846 LLVMOrcThreadSafeModuleRef TSMRef
=
847 wrap(new ThreadSafeModule(std::move(TSM
)));
848 if (LLVMErrorRef Err
= TransformFunction(Ctx
, &TSMRef
, wrap(&R
))) {
849 assert(!TSMRef
&& "TSMRef was not reset to null on error");
852 assert(TSMRef
&& "Transform succeeded, but TSMRef was set to null");
853 ThreadSafeModule Result
= std::move(*unwrap(TSMRef
));
854 LLVMOrcDisposeThreadSafeModule(TSMRef
);
855 return std::move(Result
);
859 void LLVMOrcObjectTransformLayerSetTransform(
860 LLVMOrcObjectTransformLayerRef ObjTransformLayer
,
861 LLVMOrcObjectTransformLayerTransformFunction TransformFunction
, void *Ctx
) {
862 unwrap(ObjTransformLayer
)
863 ->setTransform([TransformFunction
, Ctx
](std::unique_ptr
<MemoryBuffer
> Obj
)
864 -> Expected
<std::unique_ptr
<MemoryBuffer
>> {
865 LLVMMemoryBufferRef ObjBuffer
= wrap(Obj
.release());
866 if (LLVMErrorRef Err
= TransformFunction(Ctx
, &ObjBuffer
)) {
867 assert(!ObjBuffer
&& "ObjBuffer was not reset to null on error");
870 return std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
));
874 LLVMOrcDumpObjectsRef
LLVMOrcCreateDumpObjects(const char *DumpDir
,
875 const char *IdentifierOverride
) {
876 assert(DumpDir
&& "DumpDir should not be null");
877 assert(IdentifierOverride
&& "IdentifierOverride should not be null");
878 return wrap(new DumpObjects(DumpDir
, IdentifierOverride
));
881 void LLVMOrcDisposeDumpObjects(LLVMOrcDumpObjectsRef DumpObjects
) {
882 delete unwrap(DumpObjects
);
885 LLVMErrorRef
LLVMOrcDumpObjects_CallOperator(LLVMOrcDumpObjectsRef DumpObjects
,
886 LLVMMemoryBufferRef
*ObjBuffer
) {
887 std::unique_ptr
<MemoryBuffer
> OB(unwrap(*ObjBuffer
));
888 if (auto Result
= (*unwrap(DumpObjects
))(std::move(OB
))) {
889 *ObjBuffer
= wrap(Result
->release());
890 return LLVMErrorSuccess
;
892 *ObjBuffer
= nullptr;
893 return wrap(Result
.takeError());
897 LLVMOrcLLJITBuilderRef
LLVMOrcCreateLLJITBuilder(void) {
898 return wrap(new LLJITBuilder());
901 void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder
) {
902 delete unwrap(Builder
);
905 void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder(
906 LLVMOrcLLJITBuilderRef Builder
, LLVMOrcJITTargetMachineBuilderRef JTMB
) {
907 unwrap(Builder
)->setJITTargetMachineBuilder(std::move(*unwrap(JTMB
)));
908 LLVMOrcDisposeJITTargetMachineBuilder(JTMB
);
911 void LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator(
912 LLVMOrcLLJITBuilderRef Builder
,
913 LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction F
, void *Ctx
) {
914 unwrap(Builder
)->setObjectLinkingLayerCreator(
915 [=](ExecutionSession
&ES
, const Triple
&TT
) {
916 auto TTStr
= TT
.str();
917 return std::unique_ptr
<ObjectLayer
>(
918 unwrap(F(Ctx
, wrap(&ES
), TTStr
.c_str())));
922 LLVMErrorRef
LLVMOrcCreateLLJIT(LLVMOrcLLJITRef
*Result
,
923 LLVMOrcLLJITBuilderRef Builder
) {
924 assert(Result
&& "Result can not be null");
927 Builder
= LLVMOrcCreateLLJITBuilder();
929 auto J
= unwrap(Builder
)->create();
930 LLVMOrcDisposeLLJITBuilder(Builder
);
934 return wrap(J
.takeError());
937 *Result
= wrap(J
->release());
938 return LLVMErrorSuccess
;
941 LLVMErrorRef
LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J
) {
943 return LLVMErrorSuccess
;
946 LLVMOrcExecutionSessionRef
LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J
) {
947 return wrap(&unwrap(J
)->getExecutionSession());
950 LLVMOrcJITDylibRef
LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J
) {
951 return wrap(&unwrap(J
)->getMainJITDylib());
954 const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J
) {
955 return unwrap(J
)->getTargetTriple().str().c_str();
958 char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J
) {
959 return unwrap(J
)->getDataLayout().getGlobalPrefix();
962 LLVMOrcSymbolStringPoolEntryRef
963 LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J
, const char *UnmangledName
) {
964 return wrap(SymbolStringPoolEntryUnsafe::take(
965 unwrap(J
)->mangleAndIntern(UnmangledName
)));
968 LLVMErrorRef
LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J
, LLVMOrcJITDylibRef JD
,
969 LLVMMemoryBufferRef ObjBuffer
) {
970 return wrap(unwrap(J
)->addObjectFile(
971 *unwrap(JD
), std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
974 LLVMErrorRef
LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcLLJITRef J
,
975 LLVMOrcResourceTrackerRef RT
,
976 LLVMMemoryBufferRef ObjBuffer
) {
977 return wrap(unwrap(J
)->addObjectFile(
978 ResourceTrackerSP(unwrap(RT
)),
979 std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
982 LLVMErrorRef
LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J
,
983 LLVMOrcJITDylibRef JD
,
984 LLVMOrcThreadSafeModuleRef TSM
) {
985 std::unique_ptr
<ThreadSafeModule
> TmpTSM(unwrap(TSM
));
986 return wrap(unwrap(J
)->addIRModule(*unwrap(JD
), std::move(*TmpTSM
)));
989 LLVMErrorRef
LLVMOrcLLJITAddLLVMIRModuleWithRT(LLVMOrcLLJITRef J
,
990 LLVMOrcResourceTrackerRef RT
,
991 LLVMOrcThreadSafeModuleRef TSM
) {
992 std::unique_ptr
<ThreadSafeModule
> TmpTSM(unwrap(TSM
));
993 return wrap(unwrap(J
)->addIRModule(ResourceTrackerSP(unwrap(RT
)),
994 std::move(*TmpTSM
)));
997 LLVMErrorRef
LLVMOrcLLJITLookup(LLVMOrcLLJITRef J
,
998 LLVMOrcJITTargetAddress
*Result
,
1000 assert(Result
&& "Result can not be null");
1002 auto Sym
= unwrap(J
)->lookup(Name
);
1005 return wrap(Sym
.takeError());
1008 *Result
= Sym
->getValue();
1009 return LLVMErrorSuccess
;
1012 LLVMOrcObjectLayerRef
LLVMOrcLLJITGetObjLinkingLayer(LLVMOrcLLJITRef J
) {
1013 return wrap(&unwrap(J
)->getObjLinkingLayer());
1016 LLVMOrcObjectTransformLayerRef
1017 LLVMOrcLLJITGetObjTransformLayer(LLVMOrcLLJITRef J
) {
1018 return wrap(&unwrap(J
)->getObjTransformLayer());
1021 LLVMOrcObjectLayerRef
1022 LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(
1023 LLVMOrcExecutionSessionRef ES
) {
1024 assert(ES
&& "ES must not be null");
1025 return wrap(new RTDyldObjectLinkingLayer(
1026 *unwrap(ES
), [] { return std::make_unique
<SectionMemoryManager
>(); }));
1029 LLVMOrcObjectLayerRef
1030 LLVMOrcCreateRTDyldObjectLinkingLayerWithMCJITMemoryManagerLikeCallbacks(
1031 LLVMOrcExecutionSessionRef ES
, void *CreateContextCtx
,
1032 LLVMMemoryManagerCreateContextCallback CreateContext
,
1033 LLVMMemoryManagerNotifyTerminatingCallback NotifyTerminating
,
1034 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection
,
1035 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection
,
1036 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory
,
1037 LLVMMemoryManagerDestroyCallback Destroy
) {
1039 struct MCJITMemoryManagerLikeCallbacks
{
1040 MCJITMemoryManagerLikeCallbacks() = default;
1041 MCJITMemoryManagerLikeCallbacks(
1042 void *CreateContextCtx
,
1043 LLVMMemoryManagerCreateContextCallback CreateContext
,
1044 LLVMMemoryManagerNotifyTerminatingCallback NotifyTerminating
,
1045 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection
,
1046 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection
,
1047 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory
,
1048 LLVMMemoryManagerDestroyCallback Destroy
)
1049 : CreateContextCtx(CreateContextCtx
), CreateContext(CreateContext
),
1050 NotifyTerminating(NotifyTerminating
),
1051 AllocateCodeSection(AllocateCodeSection
),
1052 AllocateDataSection(AllocateDataSection
),
1053 FinalizeMemory(FinalizeMemory
), Destroy(Destroy
) {}
1055 MCJITMemoryManagerLikeCallbacks(MCJITMemoryManagerLikeCallbacks
&&Other
) {
1056 std::swap(CreateContextCtx
, Other
.CreateContextCtx
);
1057 std::swap(CreateContext
, Other
.CreateContext
);
1058 std::swap(NotifyTerminating
, Other
.NotifyTerminating
);
1059 std::swap(AllocateCodeSection
, Other
.AllocateCodeSection
);
1060 std::swap(AllocateDataSection
, Other
.AllocateDataSection
);
1061 std::swap(FinalizeMemory
, Other
.FinalizeMemory
);
1062 std::swap(Destroy
, Other
.Destroy
);
1065 ~MCJITMemoryManagerLikeCallbacks() {
1066 if (NotifyTerminating
)
1067 NotifyTerminating(CreateContextCtx
);
1070 void *CreateContextCtx
= nullptr;
1071 LLVMMemoryManagerCreateContextCallback CreateContext
= nullptr;
1072 LLVMMemoryManagerNotifyTerminatingCallback NotifyTerminating
= nullptr;
1073 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection
= nullptr;
1074 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection
= nullptr;
1075 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory
= nullptr;
1076 LLVMMemoryManagerDestroyCallback Destroy
= nullptr;
1079 class MCJITMemoryManagerLikeCallbacksMemMgr
: public RTDyldMemoryManager
{
1081 MCJITMemoryManagerLikeCallbacksMemMgr(
1082 const MCJITMemoryManagerLikeCallbacks
&CBs
)
1084 Opaque
= CBs
.CreateContext(CBs
.CreateContextCtx
);
1086 ~MCJITMemoryManagerLikeCallbacksMemMgr() override
{ CBs
.Destroy(Opaque
); }
1088 uint8_t *allocateCodeSection(uintptr_t Size
, unsigned Alignment
,
1090 StringRef SectionName
) override
{
1091 return CBs
.AllocateCodeSection(Opaque
, Size
, Alignment
, SectionID
,
1092 SectionName
.str().c_str());
1095 uint8_t *allocateDataSection(uintptr_t Size
, unsigned Alignment
,
1096 unsigned SectionID
, StringRef SectionName
,
1097 bool isReadOnly
) override
{
1098 return CBs
.AllocateDataSection(Opaque
, Size
, Alignment
, SectionID
,
1099 SectionName
.str().c_str(), isReadOnly
);
1102 bool finalizeMemory(std::string
*ErrMsg
) override
{
1103 char *ErrMsgCString
= nullptr;
1104 bool Result
= CBs
.FinalizeMemory(Opaque
, &ErrMsgCString
);
1105 assert((Result
|| !ErrMsgCString
) &&
1106 "Did not expect an error message if FinalizeMemory succeeded");
1107 if (ErrMsgCString
) {
1109 *ErrMsg
= ErrMsgCString
;
1110 free(ErrMsgCString
);
1116 const MCJITMemoryManagerLikeCallbacks
&CBs
;
1117 void *Opaque
= nullptr;
1120 assert(ES
&& "ES must not be null");
1121 assert(CreateContext
&& "CreateContext must not be null");
1122 assert(NotifyTerminating
&& "NotifyTerminating must not be null");
1123 assert(AllocateCodeSection
&& "AllocateCodeSection must not be null");
1124 assert(AllocateDataSection
&& "AllocateDataSection must not be null");
1125 assert(FinalizeMemory
&& "FinalizeMemory must not be null");
1126 assert(Destroy
&& "Destroy must not be null");
1128 MCJITMemoryManagerLikeCallbacks
CBs(
1129 CreateContextCtx
, CreateContext
, NotifyTerminating
, AllocateCodeSection
,
1130 AllocateDataSection
, FinalizeMemory
, Destroy
);
1132 return wrap(new RTDyldObjectLinkingLayer(*unwrap(ES
), [CBs
= std::move(CBs
)] {
1133 return std::make_unique
<MCJITMemoryManagerLikeCallbacksMemMgr
>(CBs
);
1139 void LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(
1140 LLVMOrcObjectLayerRef RTDyldObjLinkingLayer
,
1141 LLVMJITEventListenerRef Listener
) {
1142 assert(RTDyldObjLinkingLayer
&& "RTDyldObjLinkingLayer must not be null");
1143 assert(Listener
&& "Listener must not be null");
1144 reinterpret_cast<RTDyldObjectLinkingLayer
*>(unwrap(RTDyldObjLinkingLayer
))
1145 ->registerJITEventListener(*unwrap(Listener
));
1148 LLVMOrcIRTransformLayerRef
LLVMOrcLLJITGetIRTransformLayer(LLVMOrcLLJITRef J
) {
1149 return wrap(&unwrap(J
)->getIRTransformLayer());
1152 const char *LLVMOrcLLJITGetDataLayoutStr(LLVMOrcLLJITRef J
) {
1153 return unwrap(J
)->getDataLayout().getStringRepresentation().c_str();
1156 LLVMOrcIndirectStubsManagerRef
1157 LLVMOrcCreateLocalIndirectStubsManager(const char *TargetTriple
) {
1158 auto builder
= createLocalIndirectStubsManagerBuilder(Triple(TargetTriple
));
1159 return wrap(builder().release());
1162 void LLVMOrcDisposeIndirectStubsManager(LLVMOrcIndirectStubsManagerRef ISM
) {
1163 std::unique_ptr
<IndirectStubsManager
> TmpISM(unwrap(ISM
));
1166 LLVMErrorRef
LLVMOrcCreateLocalLazyCallThroughManager(
1167 const char *TargetTriple
, LLVMOrcExecutionSessionRef ES
,
1168 LLVMOrcJITTargetAddress ErrorHandlerAddr
,
1169 LLVMOrcLazyCallThroughManagerRef
*Result
) {
1170 auto LCTM
= createLocalLazyCallThroughManager(
1171 Triple(TargetTriple
), *unwrap(ES
), ExecutorAddr(ErrorHandlerAddr
));
1174 return wrap(LCTM
.takeError());
1175 *Result
= wrap(LCTM
->release());
1176 return LLVMErrorSuccess
;
1179 void LLVMOrcDisposeLazyCallThroughManager(
1180 LLVMOrcLazyCallThroughManagerRef LCM
) {
1181 std::unique_ptr
<LazyCallThroughManager
> TmpLCM(unwrap(LCM
));