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/JITTargetMachineBuilder.h"
15 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
16 #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
17 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
18 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
21 using namespace llvm::orc
;
26 class InProgressLookupState
;
28 class OrcV2CAPIHelper
{
30 using PoolEntry
= SymbolStringPtr::PoolEntry
;
31 using PoolEntryPtr
= SymbolStringPtr::PoolEntryPtr
;
33 // Move from SymbolStringPtr to PoolEntryPtr (no change in ref count).
34 static PoolEntryPtr
moveFromSymbolStringPtr(SymbolStringPtr S
) {
35 PoolEntryPtr Result
= nullptr;
36 std::swap(Result
, S
.S
);
40 // Move from a PoolEntryPtr to a SymbolStringPtr (no change in ref count).
41 static SymbolStringPtr
moveToSymbolStringPtr(PoolEntryPtr P
) {
47 // Copy a pool entry to a SymbolStringPtr (increments ref count).
48 static SymbolStringPtr
copyToSymbolStringPtr(PoolEntryPtr P
) {
49 return SymbolStringPtr(P
);
52 static PoolEntryPtr
getRawPoolEntryPtr(const SymbolStringPtr
&S
) {
56 static void retainPoolEntry(PoolEntryPtr P
) {
61 static void releasePoolEntry(PoolEntryPtr P
) {
66 static InProgressLookupState
*extractLookupState(LookupState
&LS
) {
67 return LS
.IPLS
.release();
70 static void resetLookupState(LookupState
&LS
, InProgressLookupState
*IPLS
) {
71 return LS
.reset(IPLS
);
78 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionSession
, LLVMOrcExecutionSessionRef
)
79 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SymbolStringPool
, LLVMOrcSymbolStringPoolRef
)
80 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcV2CAPIHelper::PoolEntry
,
81 LLVMOrcSymbolStringPoolEntryRef
)
82 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationUnit
,
83 LLVMOrcMaterializationUnitRef
)
84 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationResponsibility
,
85 LLVMOrcMaterializationResponsibilityRef
)
86 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITDylib
, LLVMOrcJITDylibRef
)
87 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ResourceTracker
, LLVMOrcResourceTrackerRef
)
88 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DefinitionGenerator
,
89 LLVMOrcDefinitionGeneratorRef
)
90 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(InProgressLookupState
, LLVMOrcLookupStateRef
)
91 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeContext
,
92 LLVMOrcThreadSafeContextRef
)
93 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeModule
, LLVMOrcThreadSafeModuleRef
)
94 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITTargetMachineBuilder
,
95 LLVMOrcJITTargetMachineBuilderRef
)
96 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectLayer
, LLVMOrcObjectLayerRef
)
97 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRTransformLayer
, LLVMOrcIRTransformLayerRef
)
98 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectTransformLayer
,
99 LLVMOrcObjectTransformLayerRef
)
100 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DumpObjects
, LLVMOrcDumpObjectsRef
)
101 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IndirectStubsManager
,
102 LLVMOrcIndirectStubsManagerRef
)
103 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LazyCallThroughManager
,
104 LLVMOrcLazyCallThroughManagerRef
)
105 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJITBuilder
, LLVMOrcLLJITBuilderRef
)
106 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJIT
, LLVMOrcLLJITRef
)
107 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine
, LLVMTargetMachineRef
)
112 class CAPIDefinitionGenerator final
: public DefinitionGenerator
{
114 CAPIDefinitionGenerator(
116 LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate
)
117 : Ctx(Ctx
), TryToGenerate(TryToGenerate
) {}
119 Error
tryToGenerate(LookupState
&LS
, LookupKind K
, JITDylib
&JD
,
120 JITDylibLookupFlags JDLookupFlags
,
121 const SymbolLookupSet
&LookupSet
) override
{
123 // Take the lookup state.
124 LLVMOrcLookupStateRef LSR
= ::wrap(OrcV2CAPIHelper::extractLookupState(LS
));
126 // Translate the lookup kind.
127 LLVMOrcLookupKind CLookupKind
;
129 case LookupKind::Static
:
130 CLookupKind
= LLVMOrcLookupKindStatic
;
132 case LookupKind::DLSym
:
133 CLookupKind
= LLVMOrcLookupKindDLSym
;
137 // Translate the JITDylibSearchFlags.
138 LLVMOrcJITDylibLookupFlags CJDLookupFlags
;
139 switch (JDLookupFlags
) {
140 case JITDylibLookupFlags::MatchExportedSymbolsOnly
:
141 CJDLookupFlags
= LLVMOrcJITDylibLookupFlagsMatchExportedSymbolsOnly
;
143 case JITDylibLookupFlags::MatchAllSymbols
:
144 CJDLookupFlags
= LLVMOrcJITDylibLookupFlagsMatchAllSymbols
;
148 // Translate the lookup set.
149 std::vector
<LLVMOrcCLookupSetElement
> CLookupSet
;
150 CLookupSet
.reserve(LookupSet
.size());
151 for (auto &KV
: LookupSet
) {
152 LLVMOrcSymbolLookupFlags SLF
;
153 LLVMOrcSymbolStringPoolEntryRef Name
=
154 ::wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(KV
.first
));
156 case SymbolLookupFlags::RequiredSymbol
:
157 SLF
= LLVMOrcSymbolLookupFlagsRequiredSymbol
;
159 case SymbolLookupFlags::WeaklyReferencedSymbol
:
160 SLF
= LLVMOrcSymbolLookupFlagsWeaklyReferencedSymbol
;
163 CLookupSet
.push_back({Name
, SLF
});
166 // Run the C TryToGenerate function.
167 auto Err
= unwrap(TryToGenerate(::wrap(this), Ctx
, &LSR
, CLookupKind
,
168 ::wrap(&JD
), CJDLookupFlags
,
169 CLookupSet
.data(), CLookupSet
.size()));
171 // Restore the lookup state.
172 OrcV2CAPIHelper::resetLookupState(LS
, ::unwrap(LSR
));
179 LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate
;
182 } // end namespace orc
183 } // end namespace llvm
187 class OrcCAPIMaterializationUnit
: public llvm::orc::MaterializationUnit
{
189 OrcCAPIMaterializationUnit(
190 std::string Name
, SymbolFlagsMap InitialSymbolFlags
,
191 SymbolStringPtr InitSymbol
, void *Ctx
,
192 LLVMOrcMaterializationUnitMaterializeFunction Materialize
,
193 LLVMOrcMaterializationUnitDiscardFunction Discard
,
194 LLVMOrcMaterializationUnitDestroyFunction Destroy
)
195 : llvm::orc::MaterializationUnit(std::move(InitialSymbolFlags
),
196 std::move(InitSymbol
)),
197 Name(std::move(Name
)), Ctx(Ctx
), Materialize(Materialize
),
198 Discard(Discard
), Destroy(Destroy
) {}
200 ~OrcCAPIMaterializationUnit() {
205 StringRef
getName() const override
{ return Name
; }
207 void materialize(std::unique_ptr
<MaterializationResponsibility
> R
) override
{
210 Materialize(Tmp
, wrap(R
.release()));
214 void discard(const JITDylib
&JD
, const SymbolStringPtr
&Name
) override
{
215 Discard(Ctx
, wrap(&JD
), wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name
)));
220 LLVMOrcMaterializationUnitMaterializeFunction Materialize
= nullptr;
221 LLVMOrcMaterializationUnitDiscardFunction Discard
= nullptr;
222 LLVMOrcMaterializationUnitDestroyFunction Destroy
= nullptr;
225 static JITSymbolFlags
toJITSymbolFlags(LLVMJITSymbolFlags F
) {
229 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsExported
)
230 JSF
|= JITSymbolFlags::Exported
;
231 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsWeak
)
232 JSF
|= JITSymbolFlags::Weak
;
233 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsCallable
)
234 JSF
|= JITSymbolFlags::Callable
;
235 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly
)
236 JSF
|= JITSymbolFlags::MaterializationSideEffectsOnly
;
238 JSF
.getTargetFlags() = F
.TargetFlags
;
243 static LLVMJITSymbolFlags
fromJITSymbolFlags(JITSymbolFlags JSF
) {
244 LLVMJITSymbolFlags F
= {0, 0};
245 if (JSF
& JITSymbolFlags::Exported
)
246 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsExported
;
247 if (JSF
& JITSymbolFlags::Weak
)
248 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsWeak
;
249 if (JSF
& JITSymbolFlags::Callable
)
250 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsCallable
;
251 if (JSF
& JITSymbolFlags::MaterializationSideEffectsOnly
)
252 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly
;
254 F
.TargetFlags
= JSF
.getTargetFlags();
259 static SymbolMap
toSymbolMap(LLVMOrcCSymbolMapPairs Syms
, size_t NumPairs
) {
261 for (size_t I
= 0; I
!= NumPairs
; ++I
) {
262 JITSymbolFlags Flags
= toJITSymbolFlags(Syms
[I
].Sym
.Flags
);
263 SM
[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms
[I
].Name
))] =
264 JITEvaluatedSymbol(Syms
[I
].Sym
.Address
, Flags
);
269 static SymbolDependenceMap
270 toSymbolDependenceMap(LLVMOrcCDependenceMapPairs Pairs
, size_t NumPairs
) {
271 SymbolDependenceMap SDM
;
272 for (size_t I
= 0; I
!= NumPairs
; ++I
) {
273 JITDylib
*JD
= unwrap(Pairs
[I
].JD
);
276 for (size_t J
= 0; J
!= Pairs
[I
].Names
.Length
; ++J
) {
277 auto Sym
= Pairs
[I
].Names
.Symbols
[J
];
278 Names
.insert(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Sym
)));
285 } // end anonymous namespace
287 void LLVMOrcExecutionSessionSetErrorReporter(
288 LLVMOrcExecutionSessionRef ES
, LLVMOrcErrorReporterFunction ReportError
,
290 unwrap(ES
)->setErrorReporter(
291 [=](Error Err
) { ReportError(Ctx
, wrap(std::move(Err
))); });
294 LLVMOrcSymbolStringPoolRef
295 LLVMOrcExecutionSessionGetSymbolStringPool(LLVMOrcExecutionSessionRef ES
) {
297 unwrap(ES
)->getExecutorProcessControl().getSymbolStringPool().get());
300 void LLVMOrcSymbolStringPoolClearDeadEntries(LLVMOrcSymbolStringPoolRef SSP
) {
301 unwrap(SSP
)->clearDeadEntries();
304 LLVMOrcSymbolStringPoolEntryRef
305 LLVMOrcExecutionSessionIntern(LLVMOrcExecutionSessionRef ES
, const char *Name
) {
307 OrcV2CAPIHelper::moveFromSymbolStringPtr(unwrap(ES
)->intern(Name
)));
310 void LLVMOrcRetainSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S
) {
311 OrcV2CAPIHelper::retainPoolEntry(unwrap(S
));
314 void LLVMOrcReleaseSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S
) {
315 OrcV2CAPIHelper::releasePoolEntry(unwrap(S
));
318 const char *LLVMOrcSymbolStringPoolEntryStr(LLVMOrcSymbolStringPoolEntryRef S
) {
319 return unwrap(S
)->getKey().data();
322 LLVMOrcResourceTrackerRef
323 LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD
) {
324 auto RT
= unwrap(JD
)->createResourceTracker();
325 // Retain the pointer for the C API client.
327 return wrap(RT
.get());
330 LLVMOrcResourceTrackerRef
331 LLVMOrcJITDylibGetDefaultResourceTracker(LLVMOrcJITDylibRef JD
) {
332 auto RT
= unwrap(JD
)->getDefaultResourceTracker();
333 // Retain the pointer for the C API client.
334 return wrap(RT
.get());
337 void LLVMOrcReleaseResourceTracker(LLVMOrcResourceTrackerRef RT
) {
338 ResourceTrackerSP
TmpRT(unwrap(RT
));
342 void LLVMOrcResourceTrackerTransferTo(LLVMOrcResourceTrackerRef SrcRT
,
343 LLVMOrcResourceTrackerRef DstRT
) {
344 ResourceTrackerSP
TmpRT(unwrap(SrcRT
));
345 TmpRT
->transferTo(*unwrap(DstRT
));
348 LLVMErrorRef
LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT
) {
349 ResourceTrackerSP
TmpRT(unwrap(RT
));
350 return wrap(TmpRT
->remove());
353 void LLVMOrcDisposeDefinitionGenerator(LLVMOrcDefinitionGeneratorRef DG
) {
354 std::unique_ptr
<DefinitionGenerator
> TmpDG(unwrap(DG
));
357 void LLVMOrcDisposeMaterializationUnit(LLVMOrcMaterializationUnitRef MU
) {
358 std::unique_ptr
<MaterializationUnit
> TmpMU(unwrap(MU
));
361 LLVMOrcMaterializationUnitRef
LLVMOrcCreateCustomMaterializationUnit(
362 const char *Name
, void *Ctx
, LLVMOrcCSymbolFlagsMapPairs Syms
,
363 size_t NumSyms
, LLVMOrcSymbolStringPoolEntryRef InitSym
,
364 LLVMOrcMaterializationUnitMaterializeFunction Materialize
,
365 LLVMOrcMaterializationUnitDiscardFunction Discard
,
366 LLVMOrcMaterializationUnitDestroyFunction Destroy
) {
368 for (size_t I
= 0; I
!= NumSyms
; ++I
)
369 SFM
[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms
[I
].Name
))] =
370 toJITSymbolFlags(Syms
[I
].Flags
);
372 auto IS
= OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(InitSym
));
374 return wrap(new OrcCAPIMaterializationUnit(
375 Name
, std::move(SFM
), std::move(IS
), Ctx
, Materialize
, Discard
, Destroy
));
378 LLVMOrcMaterializationUnitRef
379 LLVMOrcAbsoluteSymbols(LLVMOrcCSymbolMapPairs Syms
, size_t NumPairs
) {
380 SymbolMap SM
= toSymbolMap(Syms
, NumPairs
);
381 return wrap(absoluteSymbols(std::move(SM
)).release());
384 LLVMOrcMaterializationUnitRef
LLVMOrcLazyReexports(
385 LLVMOrcLazyCallThroughManagerRef LCTM
, LLVMOrcIndirectStubsManagerRef ISM
,
386 LLVMOrcJITDylibRef SourceJD
, LLVMOrcCSymbolAliasMapPairs CallableAliases
,
390 for (size_t I
= 0; I
!= NumPairs
; ++I
) {
391 auto pair
= CallableAliases
[I
];
392 JITSymbolFlags Flags
= toJITSymbolFlags(pair
.Entry
.Flags
);
393 SymbolStringPtr Name
=
394 OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(pair
.Entry
.Name
));
395 SAM
[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(pair
.Name
))] =
396 SymbolAliasMapEntry(Name
, Flags
);
399 return wrap(lazyReexports(*unwrap(LCTM
), *unwrap(ISM
), *unwrap(SourceJD
),
404 void LLVMOrcDisposeMaterializationResponsibility(
405 LLVMOrcMaterializationResponsibilityRef MR
) {
406 std::unique_ptr
<MaterializationResponsibility
> TmpMR(unwrap(MR
));
409 LLVMOrcJITDylibRef
LLVMOrcMaterializationResponsibilityGetTargetDylib(
410 LLVMOrcMaterializationResponsibilityRef MR
) {
411 return wrap(&unwrap(MR
)->getTargetJITDylib());
414 LLVMOrcExecutionSessionRef
415 LLVMOrcMaterializationResponsibilityGetExecutionSession(
416 LLVMOrcMaterializationResponsibilityRef MR
) {
417 return wrap(&unwrap(MR
)->getExecutionSession());
420 LLVMOrcCSymbolFlagsMapPairs
LLVMOrcMaterializationResponsibilityGetSymbols(
421 LLVMOrcMaterializationResponsibilityRef MR
, size_t *NumPairs
) {
423 auto Symbols
= unwrap(MR
)->getSymbols();
424 LLVMOrcCSymbolFlagsMapPairs Result
= static_cast<LLVMOrcCSymbolFlagsMapPairs
>(
425 safe_malloc(Symbols
.size() * sizeof(LLVMOrcCSymbolFlagsMapPair
)));
427 for (auto const &pair
: Symbols
) {
428 auto Name
= wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(pair
.first
));
429 auto Flags
= pair
.second
;
430 Result
[I
] = {Name
, fromJITSymbolFlags(Flags
)};
433 *NumPairs
= Symbols
.size();
437 void LLVMOrcDisposeCSymbolFlagsMap(LLVMOrcCSymbolFlagsMapPairs Pairs
) {
441 LLVMOrcSymbolStringPoolEntryRef
442 LLVMOrcMaterializationResponsibilityGetInitializerSymbol(
443 LLVMOrcMaterializationResponsibilityRef MR
) {
444 auto Sym
= unwrap(MR
)->getInitializerSymbol();
445 return wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Sym
));
448 LLVMOrcSymbolStringPoolEntryRef
*
449 LLVMOrcMaterializationResponsibilityGetRequestedSymbols(
450 LLVMOrcMaterializationResponsibilityRef MR
, size_t *NumSymbols
) {
452 auto Symbols
= unwrap(MR
)->getRequestedSymbols();
453 LLVMOrcSymbolStringPoolEntryRef
*Result
=
454 static_cast<LLVMOrcSymbolStringPoolEntryRef
*>(safe_malloc(
455 Symbols
.size() * sizeof(LLVMOrcSymbolStringPoolEntryRef
)));
457 for (auto &Name
: Symbols
) {
458 Result
[I
] = wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name
));
461 *NumSymbols
= Symbols
.size();
465 void LLVMOrcDisposeSymbols(LLVMOrcSymbolStringPoolEntryRef
*Symbols
) {
469 LLVMErrorRef
LLVMOrcMaterializationResponsibilityNotifyResolved(
470 LLVMOrcMaterializationResponsibilityRef MR
, LLVMOrcCSymbolMapPairs Symbols
,
472 SymbolMap SM
= toSymbolMap(Symbols
, NumPairs
);
473 return wrap(unwrap(MR
)->notifyResolved(std::move(SM
)));
476 LLVMErrorRef
LLVMOrcMaterializationResponsibilityNotifyEmitted(
477 LLVMOrcMaterializationResponsibilityRef MR
) {
478 return wrap(unwrap(MR
)->notifyEmitted());
481 LLVMErrorRef
LLVMOrcMaterializationResponsibilityDefineMaterializing(
482 LLVMOrcMaterializationResponsibilityRef MR
,
483 LLVMOrcCSymbolFlagsMapPairs Syms
, size_t NumSyms
) {
485 for (size_t I
= 0; I
!= NumSyms
; ++I
)
486 SFM
[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms
[I
].Name
))] =
487 toJITSymbolFlags(Syms
[I
].Flags
);
489 return wrap(unwrap(MR
)->defineMaterializing(std::move(SFM
)));
492 LLVMErrorRef
LLVMOrcMaterializationResponsibilityReplace(
493 LLVMOrcMaterializationResponsibilityRef MR
,
494 LLVMOrcMaterializationUnitRef MU
) {
495 std::unique_ptr
<MaterializationUnit
> TmpMU(unwrap(MU
));
496 return wrap(unwrap(MR
)->replace(std::move(TmpMU
)));
499 LLVMErrorRef
LLVMOrcMaterializationResponsibilityDelegate(
500 LLVMOrcMaterializationResponsibilityRef MR
,
501 LLVMOrcSymbolStringPoolEntryRef
*Symbols
, size_t NumSymbols
,
502 LLVMOrcMaterializationResponsibilityRef
*Result
) {
504 for (size_t I
= 0; I
!= NumSymbols
; I
++) {
505 Syms
.insert(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Symbols
[I
])));
507 auto OtherMR
= unwrap(MR
)->delegate(Syms
);
510 return wrap(OtherMR
.takeError());
512 *Result
= wrap(OtherMR
->release());
513 return LLVMErrorSuccess
;
516 void LLVMOrcMaterializationResponsibilityAddDependencies(
517 LLVMOrcMaterializationResponsibilityRef MR
,
518 LLVMOrcSymbolStringPoolEntryRef Name
,
519 LLVMOrcCDependenceMapPairs Dependencies
, size_t NumPairs
) {
521 SymbolDependenceMap SDM
= toSymbolDependenceMap(Dependencies
, NumPairs
);
522 auto Sym
= OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Name
));
523 unwrap(MR
)->addDependencies(Sym
, SDM
);
526 void LLVMOrcMaterializationResponsibilityAddDependenciesForAll(
527 LLVMOrcMaterializationResponsibilityRef MR
,
528 LLVMOrcCDependenceMapPairs Dependencies
, size_t NumPairs
) {
530 SymbolDependenceMap SDM
= toSymbolDependenceMap(Dependencies
, NumPairs
);
531 unwrap(MR
)->addDependenciesForAll(SDM
);
534 void LLVMOrcMaterializationResponsibilityFailMaterialization(
535 LLVMOrcMaterializationResponsibilityRef MR
) {
536 unwrap(MR
)->failMaterialization();
539 void LLVMOrcIRTransformLayerEmit(LLVMOrcIRTransformLayerRef IRLayer
,
540 LLVMOrcMaterializationResponsibilityRef MR
,
541 LLVMOrcThreadSafeModuleRef TSM
) {
542 std::unique_ptr
<ThreadSafeModule
> TmpTSM(unwrap(TSM
));
543 unwrap(IRLayer
)->emit(
544 std::unique_ptr
<MaterializationResponsibility
>(unwrap(MR
)),
549 LLVMOrcExecutionSessionCreateBareJITDylib(LLVMOrcExecutionSessionRef ES
,
551 return wrap(&unwrap(ES
)->createBareJITDylib(Name
));
555 LLVMOrcExecutionSessionCreateJITDylib(LLVMOrcExecutionSessionRef ES
,
556 LLVMOrcJITDylibRef
*Result
,
558 auto JD
= unwrap(ES
)->createJITDylib(Name
);
560 return wrap(JD
.takeError());
561 *Result
= wrap(&*JD
);
562 return LLVMErrorSuccess
;
566 LLVMOrcExecutionSessionGetJITDylibByName(LLVMOrcExecutionSessionRef ES
,
568 return wrap(unwrap(ES
)->getJITDylibByName(Name
));
571 LLVMErrorRef
LLVMOrcJITDylibDefine(LLVMOrcJITDylibRef JD
,
572 LLVMOrcMaterializationUnitRef MU
) {
573 std::unique_ptr
<MaterializationUnit
> TmpMU(unwrap(MU
));
575 if (auto Err
= unwrap(JD
)->define(TmpMU
)) {
577 return wrap(std::move(Err
));
579 return LLVMErrorSuccess
;
582 LLVMErrorRef
LLVMOrcJITDylibClear(LLVMOrcJITDylibRef JD
) {
583 return wrap(unwrap(JD
)->clear());
586 void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD
,
587 LLVMOrcDefinitionGeneratorRef DG
) {
588 unwrap(JD
)->addGenerator(std::unique_ptr
<DefinitionGenerator
>(unwrap(DG
)));
591 LLVMOrcDefinitionGeneratorRef
LLVMOrcCreateCustomCAPIDefinitionGenerator(
592 LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F
, void *Ctx
) {
593 auto DG
= std::make_unique
<CAPIDefinitionGenerator
>(Ctx
, F
);
594 return wrap(DG
.release());
597 LLVMErrorRef
LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(
598 LLVMOrcDefinitionGeneratorRef
*Result
, char GlobalPrefix
,
599 LLVMOrcSymbolPredicate Filter
, void *FilterCtx
) {
600 assert(Result
&& "Result can not be null");
601 assert((Filter
|| !FilterCtx
) &&
602 "if Filter is null then FilterCtx must also be null");
604 DynamicLibrarySearchGenerator::SymbolPredicate Pred
;
606 Pred
= [=](const SymbolStringPtr
&Name
) -> bool {
607 return Filter(FilterCtx
, wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name
)));
610 auto ProcessSymsGenerator
=
611 DynamicLibrarySearchGenerator::GetForCurrentProcess(GlobalPrefix
, Pred
);
613 if (!ProcessSymsGenerator
) {
615 return wrap(ProcessSymsGenerator
.takeError());
618 *Result
= wrap(ProcessSymsGenerator
->release());
619 return LLVMErrorSuccess
;
622 LLVMOrcThreadSafeContextRef
LLVMOrcCreateNewThreadSafeContext(void) {
623 return wrap(new ThreadSafeContext(std::make_unique
<LLVMContext
>()));
627 LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx
) {
628 return wrap(unwrap(TSCtx
)->getContext());
631 void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx
) {
632 delete unwrap(TSCtx
);
636 LLVMOrcThreadSafeModuleWithModuleDo(LLVMOrcThreadSafeModuleRef TSM
,
637 LLVMOrcGenericIRModuleOperationFunction F
,
639 return wrap(unwrap(TSM
)->withModuleDo(
640 [&](Module
&M
) { return unwrap(F(Ctx
, wrap(&M
))); }));
643 LLVMOrcThreadSafeModuleRef
644 LLVMOrcCreateNewThreadSafeModule(LLVMModuleRef M
,
645 LLVMOrcThreadSafeContextRef TSCtx
) {
647 new ThreadSafeModule(std::unique_ptr
<Module
>(unwrap(M
)), *unwrap(TSCtx
)));
650 void LLVMOrcDisposeThreadSafeModule(LLVMOrcThreadSafeModuleRef TSM
) {
654 LLVMErrorRef
LLVMOrcJITTargetMachineBuilderDetectHost(
655 LLVMOrcJITTargetMachineBuilderRef
*Result
) {
656 assert(Result
&& "Result can not be null");
658 auto JTMB
= JITTargetMachineBuilder::detectHost();
661 return wrap(JTMB
.takeError());
664 *Result
= wrap(new JITTargetMachineBuilder(std::move(*JTMB
)));
665 return LLVMErrorSuccess
;
668 LLVMOrcJITTargetMachineBuilderRef
669 LLVMOrcJITTargetMachineBuilderCreateFromTargetMachine(LLVMTargetMachineRef TM
) {
670 auto *TemplateTM
= unwrap(TM
);
673 std::make_unique
<JITTargetMachineBuilder
>(TemplateTM
->getTargetTriple());
676 .setCPU(TemplateTM
->getTargetCPU().str())
677 .setRelocationModel(TemplateTM
->getRelocationModel())
678 .setCodeModel(TemplateTM
->getCodeModel())
679 .setCodeGenOptLevel(TemplateTM
->getOptLevel())
680 .setFeatures(TemplateTM
->getTargetFeatureString())
681 .setOptions(TemplateTM
->Options
);
683 LLVMDisposeTargetMachine(TM
);
685 return wrap(JTMB
.release());
688 void LLVMOrcDisposeJITTargetMachineBuilder(
689 LLVMOrcJITTargetMachineBuilderRef JTMB
) {
693 char *LLVMOrcJITTargetMachineBuilderGetTargetTriple(
694 LLVMOrcJITTargetMachineBuilderRef JTMB
) {
695 auto Tmp
= unwrap(JTMB
)->getTargetTriple().str();
696 char *TargetTriple
= (char *)malloc(Tmp
.size() + 1);
697 strcpy(TargetTriple
, Tmp
.c_str());
701 void LLVMOrcJITTargetMachineBuilderSetTargetTriple(
702 LLVMOrcJITTargetMachineBuilderRef JTMB
, const char *TargetTriple
) {
703 unwrap(JTMB
)->getTargetTriple() = Triple(TargetTriple
);
706 LLVMErrorRef
LLVMOrcObjectLayerAddObjectFile(LLVMOrcObjectLayerRef ObjLayer
,
707 LLVMOrcJITDylibRef JD
,
708 LLVMMemoryBufferRef ObjBuffer
) {
709 return wrap(unwrap(ObjLayer
)->add(
710 *unwrap(JD
), std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
713 LLVMErrorRef
LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcObjectLayerRef ObjLayer
,
714 LLVMOrcResourceTrackerRef RT
,
715 LLVMMemoryBufferRef ObjBuffer
) {
717 unwrap(ObjLayer
)->add(ResourceTrackerSP(unwrap(RT
)),
718 std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
721 void LLVMOrcObjectLayerEmit(LLVMOrcObjectLayerRef ObjLayer
,
722 LLVMOrcMaterializationResponsibilityRef R
,
723 LLVMMemoryBufferRef ObjBuffer
) {
724 unwrap(ObjLayer
)->emit(
725 std::unique_ptr
<MaterializationResponsibility
>(unwrap(R
)),
726 std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
)));
729 void LLVMOrcDisposeObjectLayer(LLVMOrcObjectLayerRef ObjLayer
) {
730 delete unwrap(ObjLayer
);
733 void LLVMOrcIRTransformLayerSetTransform(
734 LLVMOrcIRTransformLayerRef IRTransformLayer
,
735 LLVMOrcIRTransformLayerTransformFunction TransformFunction
, void *Ctx
) {
736 unwrap(IRTransformLayer
)
738 [=](ThreadSafeModule TSM
,
739 MaterializationResponsibility
&R
) -> Expected
<ThreadSafeModule
> {
740 LLVMOrcThreadSafeModuleRef TSMRef
=
741 wrap(new ThreadSafeModule(std::move(TSM
)));
742 if (LLVMErrorRef Err
= TransformFunction(Ctx
, &TSMRef
, wrap(&R
))) {
743 assert(!TSMRef
&& "TSMRef was not reset to null on error");
746 return std::move(*unwrap(TSMRef
));
750 void LLVMOrcObjectTransformLayerSetTransform(
751 LLVMOrcObjectTransformLayerRef ObjTransformLayer
,
752 LLVMOrcObjectTransformLayerTransformFunction TransformFunction
, void *Ctx
) {
753 unwrap(ObjTransformLayer
)
754 ->setTransform([TransformFunction
, Ctx
](std::unique_ptr
<MemoryBuffer
> Obj
)
755 -> Expected
<std::unique_ptr
<MemoryBuffer
>> {
756 LLVMMemoryBufferRef ObjBuffer
= wrap(Obj
.release());
757 if (LLVMErrorRef Err
= TransformFunction(Ctx
, &ObjBuffer
)) {
758 assert(!ObjBuffer
&& "ObjBuffer was not reset to null on error");
761 return std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
));
765 LLVMOrcDumpObjectsRef
LLVMOrcCreateDumpObjects(const char *DumpDir
,
766 const char *IdentifierOverride
) {
767 assert(DumpDir
&& "DumpDir should not be null");
768 assert(IdentifierOverride
&& "IdentifierOverride should not be null");
769 return wrap(new DumpObjects(DumpDir
, IdentifierOverride
));
772 void LLVMOrcDisposeDumpObjects(LLVMOrcDumpObjectsRef DumpObjects
) {
773 delete unwrap(DumpObjects
);
776 LLVMErrorRef
LLVMOrcDumpObjects_CallOperator(LLVMOrcDumpObjectsRef DumpObjects
,
777 LLVMMemoryBufferRef
*ObjBuffer
) {
778 std::unique_ptr
<MemoryBuffer
> OB(unwrap(*ObjBuffer
));
779 if (auto Result
= (*unwrap(DumpObjects
))(std::move(OB
))) {
780 *ObjBuffer
= wrap(Result
->release());
781 return LLVMErrorSuccess
;
783 *ObjBuffer
= nullptr;
784 return wrap(Result
.takeError());
788 LLVMOrcLLJITBuilderRef
LLVMOrcCreateLLJITBuilder(void) {
789 return wrap(new LLJITBuilder());
792 void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder
) {
793 delete unwrap(Builder
);
796 void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder(
797 LLVMOrcLLJITBuilderRef Builder
, LLVMOrcJITTargetMachineBuilderRef JTMB
) {
798 unwrap(Builder
)->setJITTargetMachineBuilder(std::move(*unwrap(JTMB
)));
799 LLVMOrcDisposeJITTargetMachineBuilder(JTMB
);
802 void LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator(
803 LLVMOrcLLJITBuilderRef Builder
,
804 LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction F
, void *Ctx
) {
805 unwrap(Builder
)->setObjectLinkingLayerCreator(
806 [=](ExecutionSession
&ES
, const Triple
&TT
) {
807 auto TTStr
= TT
.str();
808 return std::unique_ptr
<ObjectLayer
>(
809 unwrap(F(Ctx
, wrap(&ES
), TTStr
.c_str())));
813 LLVMErrorRef
LLVMOrcCreateLLJIT(LLVMOrcLLJITRef
*Result
,
814 LLVMOrcLLJITBuilderRef Builder
) {
815 assert(Result
&& "Result can not be null");
818 Builder
= LLVMOrcCreateLLJITBuilder();
820 auto J
= unwrap(Builder
)->create();
821 LLVMOrcDisposeLLJITBuilder(Builder
);
825 return wrap(J
.takeError());
828 *Result
= wrap(J
->release());
829 return LLVMErrorSuccess
;
832 LLVMErrorRef
LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J
) {
834 return LLVMErrorSuccess
;
837 LLVMOrcExecutionSessionRef
LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J
) {
838 return wrap(&unwrap(J
)->getExecutionSession());
841 LLVMOrcJITDylibRef
LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J
) {
842 return wrap(&unwrap(J
)->getMainJITDylib());
845 const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J
) {
846 return unwrap(J
)->getTargetTriple().str().c_str();
849 char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J
) {
850 return unwrap(J
)->getDataLayout().getGlobalPrefix();
853 LLVMOrcSymbolStringPoolEntryRef
854 LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J
, const char *UnmangledName
) {
855 return wrap(OrcV2CAPIHelper::moveFromSymbolStringPtr(
856 unwrap(J
)->mangleAndIntern(UnmangledName
)));
859 LLVMErrorRef
LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J
, LLVMOrcJITDylibRef JD
,
860 LLVMMemoryBufferRef ObjBuffer
) {
861 return wrap(unwrap(J
)->addObjectFile(
862 *unwrap(JD
), std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
865 LLVMErrorRef
LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcLLJITRef J
,
866 LLVMOrcResourceTrackerRef RT
,
867 LLVMMemoryBufferRef ObjBuffer
) {
868 return wrap(unwrap(J
)->addObjectFile(
869 ResourceTrackerSP(unwrap(RT
)),
870 std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
873 LLVMErrorRef
LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J
,
874 LLVMOrcJITDylibRef JD
,
875 LLVMOrcThreadSafeModuleRef TSM
) {
876 std::unique_ptr
<ThreadSafeModule
> TmpTSM(unwrap(TSM
));
877 return wrap(unwrap(J
)->addIRModule(*unwrap(JD
), std::move(*TmpTSM
)));
880 LLVMErrorRef
LLVMOrcLLJITAddLLVMIRModuleWithRT(LLVMOrcLLJITRef J
,
881 LLVMOrcResourceTrackerRef RT
,
882 LLVMOrcThreadSafeModuleRef TSM
) {
883 std::unique_ptr
<ThreadSafeModule
> TmpTSM(unwrap(TSM
));
884 return wrap(unwrap(J
)->addIRModule(ResourceTrackerSP(unwrap(RT
)),
885 std::move(*TmpTSM
)));
888 LLVMErrorRef
LLVMOrcLLJITLookup(LLVMOrcLLJITRef J
,
889 LLVMOrcJITTargetAddress
*Result
,
891 assert(Result
&& "Result can not be null");
893 auto Sym
= unwrap(J
)->lookup(Name
);
896 return wrap(Sym
.takeError());
899 *Result
= Sym
->getAddress();
900 return LLVMErrorSuccess
;
903 LLVMOrcObjectLayerRef
LLVMOrcLLJITGetObjLinkingLayer(LLVMOrcLLJITRef J
) {
904 return wrap(&unwrap(J
)->getObjLinkingLayer());
907 LLVMOrcObjectTransformLayerRef
908 LLVMOrcLLJITGetObjTransformLayer(LLVMOrcLLJITRef J
) {
909 return wrap(&unwrap(J
)->getObjTransformLayer());
912 LLVMOrcObjectLayerRef
913 LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(
914 LLVMOrcExecutionSessionRef ES
) {
915 assert(ES
&& "ES must not be null");
916 return wrap(new RTDyldObjectLinkingLayer(
917 *unwrap(ES
), [] { return std::make_unique
<SectionMemoryManager
>(); }));
920 void LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(
921 LLVMOrcObjectLayerRef RTDyldObjLinkingLayer
,
922 LLVMJITEventListenerRef Listener
) {
923 assert(RTDyldObjLinkingLayer
&& "RTDyldObjLinkingLayer must not be null");
924 assert(Listener
&& "Listener must not be null");
925 reinterpret_cast<RTDyldObjectLinkingLayer
*>(unwrap(RTDyldObjLinkingLayer
))
926 ->registerJITEventListener(*unwrap(Listener
));
929 LLVMOrcIRTransformLayerRef
LLVMOrcLLJITGetIRTransformLayer(LLVMOrcLLJITRef J
) {
930 return wrap(&unwrap(J
)->getIRTransformLayer());
933 const char *LLVMOrcLLJITGetDataLayoutStr(LLVMOrcLLJITRef J
) {
934 return unwrap(J
)->getDataLayout().getStringRepresentation().c_str();
937 LLVMOrcIndirectStubsManagerRef
938 LLVMOrcCreateLocalIndirectStubsManager(const char *TargetTriple
) {
939 auto builder
= createLocalIndirectStubsManagerBuilder(Triple(TargetTriple
));
940 return wrap(builder().release());
943 void LLVMOrcDisposeIndirectStubsManager(LLVMOrcIndirectStubsManagerRef ISM
) {
944 std::unique_ptr
<IndirectStubsManager
> TmpISM(unwrap(ISM
));
947 LLVMErrorRef
LLVMOrcCreateLocalLazyCallThroughManager(
948 const char *TargetTriple
, LLVMOrcExecutionSessionRef ES
,
949 LLVMOrcJITTargetAddress ErrorHandlerAddr
,
950 LLVMOrcLazyCallThroughManagerRef
*Result
) {
951 auto LCTM
= createLocalLazyCallThroughManager(Triple(TargetTriple
),
952 *unwrap(ES
), ErrorHandlerAddr
);
955 return wrap(LCTM
.takeError());
956 *Result
= wrap(LCTM
->release());
957 return LLVMErrorSuccess
;
960 void LLVMOrcDisposeLazyCallThroughManager(
961 LLVMOrcLazyCallThroughManagerRef LCM
) {
962 std::unique_ptr
<LazyCallThroughManager
> TmpLCM(unwrap(LCM
));