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
)
111 class OrcCAPIMaterializationUnit
: public llvm::orc::MaterializationUnit
{
113 OrcCAPIMaterializationUnit(
114 std::string Name
, SymbolFlagsMap InitialSymbolFlags
,
115 SymbolStringPtr InitSymbol
, void *Ctx
,
116 LLVMOrcMaterializationUnitMaterializeFunction Materialize
,
117 LLVMOrcMaterializationUnitDiscardFunction Discard
,
118 LLVMOrcMaterializationUnitDestroyFunction Destroy
)
119 : llvm::orc::MaterializationUnit(
120 Interface(std::move(InitialSymbolFlags
), std::move(InitSymbol
))),
121 Name(std::move(Name
)), Ctx(Ctx
), Materialize(Materialize
),
122 Discard(Discard
), Destroy(Destroy
) {}
124 ~OrcCAPIMaterializationUnit() {
129 StringRef
getName() const override
{ return Name
; }
131 void materialize(std::unique_ptr
<MaterializationResponsibility
> R
) override
{
134 Materialize(Tmp
, wrap(R
.release()));
138 void discard(const JITDylib
&JD
, const SymbolStringPtr
&Name
) override
{
139 Discard(Ctx
, wrap(&JD
), wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name
)));
144 LLVMOrcMaterializationUnitMaterializeFunction Materialize
= nullptr;
145 LLVMOrcMaterializationUnitDiscardFunction Discard
= nullptr;
146 LLVMOrcMaterializationUnitDestroyFunction Destroy
= nullptr;
149 static JITSymbolFlags
toJITSymbolFlags(LLVMJITSymbolFlags F
) {
153 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsExported
)
154 JSF
|= JITSymbolFlags::Exported
;
155 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsWeak
)
156 JSF
|= JITSymbolFlags::Weak
;
157 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsCallable
)
158 JSF
|= JITSymbolFlags::Callable
;
159 if (F
.GenericFlags
& LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly
)
160 JSF
|= JITSymbolFlags::MaterializationSideEffectsOnly
;
162 JSF
.getTargetFlags() = F
.TargetFlags
;
167 static LLVMJITSymbolFlags
fromJITSymbolFlags(JITSymbolFlags JSF
) {
168 LLVMJITSymbolFlags F
= {0, 0};
169 if (JSF
& JITSymbolFlags::Exported
)
170 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsExported
;
171 if (JSF
& JITSymbolFlags::Weak
)
172 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsWeak
;
173 if (JSF
& JITSymbolFlags::Callable
)
174 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsCallable
;
175 if (JSF
& JITSymbolFlags::MaterializationSideEffectsOnly
)
176 F
.GenericFlags
|= LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly
;
178 F
.TargetFlags
= JSF
.getTargetFlags();
183 static SymbolMap
toSymbolMap(LLVMOrcCSymbolMapPairs Syms
, size_t NumPairs
) {
185 for (size_t I
= 0; I
!= NumPairs
; ++I
) {
186 JITSymbolFlags Flags
= toJITSymbolFlags(Syms
[I
].Sym
.Flags
);
187 SM
[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms
[I
].Name
))] = {
188 ExecutorAddr(Syms
[I
].Sym
.Address
), Flags
};
193 static SymbolDependenceMap
194 toSymbolDependenceMap(LLVMOrcCDependenceMapPairs Pairs
, size_t NumPairs
) {
195 SymbolDependenceMap SDM
;
196 for (size_t I
= 0; I
!= NumPairs
; ++I
) {
197 JITDylib
*JD
= unwrap(Pairs
[I
].JD
);
200 for (size_t J
= 0; J
!= Pairs
[I
].Names
.Length
; ++J
) {
201 auto Sym
= Pairs
[I
].Names
.Symbols
[J
];
202 Names
.insert(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Sym
)));
209 static LookupKind
toLookupKind(LLVMOrcLookupKind K
) {
211 case LLVMOrcLookupKindStatic
:
212 return LookupKind::Static
;
213 case LLVMOrcLookupKindDLSym
:
214 return LookupKind::DLSym
;
216 llvm_unreachable("unrecognized LLVMOrcLookupKind value");
219 static LLVMOrcLookupKind
fromLookupKind(LookupKind K
) {
221 case LookupKind::Static
:
222 return LLVMOrcLookupKindStatic
;
223 case LookupKind::DLSym
:
224 return LLVMOrcLookupKindDLSym
;
226 llvm_unreachable("unrecognized LookupKind value");
229 static JITDylibLookupFlags
230 toJITDylibLookupFlags(LLVMOrcJITDylibLookupFlags LF
) {
232 case LLVMOrcJITDylibLookupFlagsMatchExportedSymbolsOnly
:
233 return JITDylibLookupFlags::MatchExportedSymbolsOnly
;
234 case LLVMOrcJITDylibLookupFlagsMatchAllSymbols
:
235 return JITDylibLookupFlags::MatchAllSymbols
;
237 llvm_unreachable("unrecognized LLVMOrcJITDylibLookupFlags value");
240 static LLVMOrcJITDylibLookupFlags
241 fromJITDylibLookupFlags(JITDylibLookupFlags LF
) {
243 case JITDylibLookupFlags::MatchExportedSymbolsOnly
:
244 return LLVMOrcJITDylibLookupFlagsMatchExportedSymbolsOnly
;
245 case JITDylibLookupFlags::MatchAllSymbols
:
246 return LLVMOrcJITDylibLookupFlagsMatchAllSymbols
;
248 llvm_unreachable("unrecognized JITDylibLookupFlags value");
251 static SymbolLookupFlags
toSymbolLookupFlags(LLVMOrcSymbolLookupFlags SLF
) {
253 case LLVMOrcSymbolLookupFlagsRequiredSymbol
:
254 return SymbolLookupFlags::RequiredSymbol
;
255 case LLVMOrcSymbolLookupFlagsWeaklyReferencedSymbol
:
256 return SymbolLookupFlags::WeaklyReferencedSymbol
;
258 llvm_unreachable("unrecognized LLVMOrcSymbolLookupFlags value");
261 static LLVMOrcSymbolLookupFlags
fromSymbolLookupFlags(SymbolLookupFlags SLF
) {
263 case SymbolLookupFlags::RequiredSymbol
:
264 return LLVMOrcSymbolLookupFlagsRequiredSymbol
;
265 case SymbolLookupFlags::WeaklyReferencedSymbol
:
266 return LLVMOrcSymbolLookupFlagsWeaklyReferencedSymbol
;
268 llvm_unreachable("unrecognized SymbolLookupFlags value");
271 static LLVMJITEvaluatedSymbol
272 fromExecutorSymbolDef(const ExecutorSymbolDef
&S
) {
273 return {S
.getAddress().getValue(), fromJITSymbolFlags(S
.getFlags())};
276 } // end anonymous namespace
281 class CAPIDefinitionGenerator final
: public DefinitionGenerator
{
283 CAPIDefinitionGenerator(
284 LLVMOrcDisposeCAPIDefinitionGeneratorFunction Dispose
, void *Ctx
,
285 LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate
)
286 : Dispose(Dispose
), Ctx(Ctx
), TryToGenerate(TryToGenerate
) {}
288 ~CAPIDefinitionGenerator() {
293 Error
tryToGenerate(LookupState
&LS
, LookupKind K
, JITDylib
&JD
,
294 JITDylibLookupFlags JDLookupFlags
,
295 const SymbolLookupSet
&LookupSet
) override
{
297 // Take the lookup state.
298 LLVMOrcLookupStateRef LSR
= ::wrap(OrcV2CAPIHelper::extractLookupState(LS
));
300 // Translate the lookup kind.
301 LLVMOrcLookupKind CLookupKind
= fromLookupKind(K
);
303 // Translate the JITDylibLookupFlags.
304 LLVMOrcJITDylibLookupFlags CJDLookupFlags
=
305 fromJITDylibLookupFlags(JDLookupFlags
);
307 // Translate the lookup set.
308 std::vector
<LLVMOrcCLookupSetElement
> CLookupSet
;
309 CLookupSet
.reserve(LookupSet
.size());
310 for (auto &KV
: LookupSet
) {
311 LLVMOrcSymbolStringPoolEntryRef Name
=
312 ::wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(KV
.first
));
313 LLVMOrcSymbolLookupFlags SLF
= fromSymbolLookupFlags(KV
.second
);
314 CLookupSet
.push_back({Name
, SLF
});
317 // Run the C TryToGenerate function.
318 auto Err
= unwrap(TryToGenerate(::wrap(this), Ctx
, &LSR
, CLookupKind
,
319 ::wrap(&JD
), CJDLookupFlags
,
320 CLookupSet
.data(), CLookupSet
.size()));
322 // Restore the lookup state.
323 OrcV2CAPIHelper::resetLookupState(LS
, ::unwrap(LSR
));
329 LLVMOrcDisposeCAPIDefinitionGeneratorFunction Dispose
;
331 LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate
;
334 } // end namespace orc
335 } // end namespace llvm
337 void LLVMOrcExecutionSessionSetErrorReporter(
338 LLVMOrcExecutionSessionRef ES
, LLVMOrcErrorReporterFunction ReportError
,
340 unwrap(ES
)->setErrorReporter(
341 [=](Error Err
) { ReportError(Ctx
, wrap(std::move(Err
))); });
344 LLVMOrcSymbolStringPoolRef
345 LLVMOrcExecutionSessionGetSymbolStringPool(LLVMOrcExecutionSessionRef ES
) {
347 unwrap(ES
)->getExecutorProcessControl().getSymbolStringPool().get());
350 void LLVMOrcSymbolStringPoolClearDeadEntries(LLVMOrcSymbolStringPoolRef SSP
) {
351 unwrap(SSP
)->clearDeadEntries();
354 LLVMOrcSymbolStringPoolEntryRef
355 LLVMOrcExecutionSessionIntern(LLVMOrcExecutionSessionRef ES
, const char *Name
) {
357 OrcV2CAPIHelper::moveFromSymbolStringPtr(unwrap(ES
)->intern(Name
)));
360 void LLVMOrcExecutionSessionLookup(
361 LLVMOrcExecutionSessionRef ES
, LLVMOrcLookupKind K
,
362 LLVMOrcCJITDylibSearchOrder SearchOrder
, size_t SearchOrderSize
,
363 LLVMOrcCLookupSet Symbols
, size_t SymbolsSize
,
364 LLVMOrcExecutionSessionLookupHandleResultFunction HandleResult
, void *Ctx
) {
365 assert(ES
&& "ES cannot be null");
366 assert(SearchOrder
&& "SearchOrder cannot be null");
367 assert(Symbols
&& "Symbols cannot be null");
368 assert(HandleResult
&& "HandleResult cannot be null");
370 JITDylibSearchOrder SO
;
371 for (size_t I
= 0; I
!= SearchOrderSize
; ++I
)
372 SO
.push_back({unwrap(SearchOrder
[I
].JD
),
373 toJITDylibLookupFlags(SearchOrder
[I
].JDLookupFlags
)});
376 for (size_t I
= 0; I
!= SymbolsSize
; ++I
)
377 SLS
.add(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Symbols
[I
].Name
)),
378 toSymbolLookupFlags(Symbols
[I
].LookupFlags
));
381 toLookupKind(K
), SO
, std::move(SLS
), SymbolState::Ready
,
382 [HandleResult
, Ctx
](Expected
<SymbolMap
> Result
) {
384 SmallVector
<LLVMOrcCSymbolMapPair
> CResult
;
385 for (auto &KV
: *Result
)
386 CResult
.push_back(LLVMOrcCSymbolMapPair
{
387 wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(KV
.first
)),
388 fromExecutorSymbolDef(KV
.second
)});
389 HandleResult(LLVMErrorSuccess
, CResult
.data(), CResult
.size(), Ctx
);
391 HandleResult(wrap(Result
.takeError()), nullptr, 0, Ctx
);
393 NoDependenciesToRegister
);
396 void LLVMOrcRetainSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S
) {
397 OrcV2CAPIHelper::retainPoolEntry(unwrap(S
));
400 void LLVMOrcReleaseSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S
) {
401 OrcV2CAPIHelper::releasePoolEntry(unwrap(S
));
404 const char *LLVMOrcSymbolStringPoolEntryStr(LLVMOrcSymbolStringPoolEntryRef S
) {
405 return unwrap(S
)->getKey().data();
408 LLVMOrcResourceTrackerRef
409 LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD
) {
410 auto RT
= unwrap(JD
)->createResourceTracker();
411 // Retain the pointer for the C API client.
413 return wrap(RT
.get());
416 LLVMOrcResourceTrackerRef
417 LLVMOrcJITDylibGetDefaultResourceTracker(LLVMOrcJITDylibRef JD
) {
418 auto RT
= unwrap(JD
)->getDefaultResourceTracker();
419 // Retain the pointer for the C API client.
420 return wrap(RT
.get());
423 void LLVMOrcReleaseResourceTracker(LLVMOrcResourceTrackerRef RT
) {
424 ResourceTrackerSP
TmpRT(unwrap(RT
));
428 void LLVMOrcResourceTrackerTransferTo(LLVMOrcResourceTrackerRef SrcRT
,
429 LLVMOrcResourceTrackerRef DstRT
) {
430 ResourceTrackerSP
TmpRT(unwrap(SrcRT
));
431 TmpRT
->transferTo(*unwrap(DstRT
));
434 LLVMErrorRef
LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT
) {
435 ResourceTrackerSP
TmpRT(unwrap(RT
));
436 return wrap(TmpRT
->remove());
439 void LLVMOrcDisposeDefinitionGenerator(LLVMOrcDefinitionGeneratorRef DG
) {
440 std::unique_ptr
<DefinitionGenerator
> TmpDG(unwrap(DG
));
443 void LLVMOrcDisposeMaterializationUnit(LLVMOrcMaterializationUnitRef MU
) {
444 std::unique_ptr
<MaterializationUnit
> TmpMU(unwrap(MU
));
447 LLVMOrcMaterializationUnitRef
LLVMOrcCreateCustomMaterializationUnit(
448 const char *Name
, void *Ctx
, LLVMOrcCSymbolFlagsMapPairs Syms
,
449 size_t NumSyms
, LLVMOrcSymbolStringPoolEntryRef InitSym
,
450 LLVMOrcMaterializationUnitMaterializeFunction Materialize
,
451 LLVMOrcMaterializationUnitDiscardFunction Discard
,
452 LLVMOrcMaterializationUnitDestroyFunction Destroy
) {
454 for (size_t I
= 0; I
!= NumSyms
; ++I
)
455 SFM
[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms
[I
].Name
))] =
456 toJITSymbolFlags(Syms
[I
].Flags
);
458 auto IS
= OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(InitSym
));
460 return wrap(new OrcCAPIMaterializationUnit(
461 Name
, std::move(SFM
), std::move(IS
), Ctx
, Materialize
, Discard
, Destroy
));
464 LLVMOrcMaterializationUnitRef
465 LLVMOrcAbsoluteSymbols(LLVMOrcCSymbolMapPairs Syms
, size_t NumPairs
) {
466 SymbolMap SM
= toSymbolMap(Syms
, NumPairs
);
467 return wrap(absoluteSymbols(std::move(SM
)).release());
470 LLVMOrcMaterializationUnitRef
LLVMOrcLazyReexports(
471 LLVMOrcLazyCallThroughManagerRef LCTM
, LLVMOrcIndirectStubsManagerRef ISM
,
472 LLVMOrcJITDylibRef SourceJD
, LLVMOrcCSymbolAliasMapPairs CallableAliases
,
476 for (size_t I
= 0; I
!= NumPairs
; ++I
) {
477 auto pair
= CallableAliases
[I
];
478 JITSymbolFlags Flags
= toJITSymbolFlags(pair
.Entry
.Flags
);
479 SymbolStringPtr Name
=
480 OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(pair
.Entry
.Name
));
481 SAM
[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(pair
.Name
))] =
482 SymbolAliasMapEntry(Name
, Flags
);
485 return wrap(lazyReexports(*unwrap(LCTM
), *unwrap(ISM
), *unwrap(SourceJD
),
490 void LLVMOrcDisposeMaterializationResponsibility(
491 LLVMOrcMaterializationResponsibilityRef MR
) {
492 std::unique_ptr
<MaterializationResponsibility
> TmpMR(unwrap(MR
));
495 LLVMOrcJITDylibRef
LLVMOrcMaterializationResponsibilityGetTargetDylib(
496 LLVMOrcMaterializationResponsibilityRef MR
) {
497 return wrap(&unwrap(MR
)->getTargetJITDylib());
500 LLVMOrcExecutionSessionRef
501 LLVMOrcMaterializationResponsibilityGetExecutionSession(
502 LLVMOrcMaterializationResponsibilityRef MR
) {
503 return wrap(&unwrap(MR
)->getExecutionSession());
506 LLVMOrcCSymbolFlagsMapPairs
LLVMOrcMaterializationResponsibilityGetSymbols(
507 LLVMOrcMaterializationResponsibilityRef MR
, size_t *NumPairs
) {
509 auto Symbols
= unwrap(MR
)->getSymbols();
510 LLVMOrcCSymbolFlagsMapPairs Result
= static_cast<LLVMOrcCSymbolFlagsMapPairs
>(
511 safe_malloc(Symbols
.size() * sizeof(LLVMOrcCSymbolFlagsMapPair
)));
513 for (auto const &pair
: Symbols
) {
514 auto Name
= wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(pair
.first
));
515 auto Flags
= pair
.second
;
516 Result
[I
] = {Name
, fromJITSymbolFlags(Flags
)};
519 *NumPairs
= Symbols
.size();
523 void LLVMOrcDisposeCSymbolFlagsMap(LLVMOrcCSymbolFlagsMapPairs Pairs
) {
527 LLVMOrcSymbolStringPoolEntryRef
528 LLVMOrcMaterializationResponsibilityGetInitializerSymbol(
529 LLVMOrcMaterializationResponsibilityRef MR
) {
530 auto Sym
= unwrap(MR
)->getInitializerSymbol();
531 return wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Sym
));
534 LLVMOrcSymbolStringPoolEntryRef
*
535 LLVMOrcMaterializationResponsibilityGetRequestedSymbols(
536 LLVMOrcMaterializationResponsibilityRef MR
, size_t *NumSymbols
) {
538 auto Symbols
= unwrap(MR
)->getRequestedSymbols();
539 LLVMOrcSymbolStringPoolEntryRef
*Result
=
540 static_cast<LLVMOrcSymbolStringPoolEntryRef
*>(safe_malloc(
541 Symbols
.size() * sizeof(LLVMOrcSymbolStringPoolEntryRef
)));
543 for (auto &Name
: Symbols
) {
544 Result
[I
] = wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name
));
547 *NumSymbols
= Symbols
.size();
551 void LLVMOrcDisposeSymbols(LLVMOrcSymbolStringPoolEntryRef
*Symbols
) {
555 LLVMErrorRef
LLVMOrcMaterializationResponsibilityNotifyResolved(
556 LLVMOrcMaterializationResponsibilityRef MR
, LLVMOrcCSymbolMapPairs Symbols
,
558 SymbolMap SM
= toSymbolMap(Symbols
, NumPairs
);
559 return wrap(unwrap(MR
)->notifyResolved(std::move(SM
)));
562 LLVMErrorRef
LLVMOrcMaterializationResponsibilityNotifyEmitted(
563 LLVMOrcMaterializationResponsibilityRef MR
) {
564 return wrap(unwrap(MR
)->notifyEmitted());
567 LLVMErrorRef
LLVMOrcMaterializationResponsibilityDefineMaterializing(
568 LLVMOrcMaterializationResponsibilityRef MR
,
569 LLVMOrcCSymbolFlagsMapPairs Syms
, size_t NumSyms
) {
571 for (size_t I
= 0; I
!= NumSyms
; ++I
)
572 SFM
[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms
[I
].Name
))] =
573 toJITSymbolFlags(Syms
[I
].Flags
);
575 return wrap(unwrap(MR
)->defineMaterializing(std::move(SFM
)));
578 LLVMErrorRef
LLVMOrcMaterializationResponsibilityReplace(
579 LLVMOrcMaterializationResponsibilityRef MR
,
580 LLVMOrcMaterializationUnitRef MU
) {
581 std::unique_ptr
<MaterializationUnit
> TmpMU(unwrap(MU
));
582 return wrap(unwrap(MR
)->replace(std::move(TmpMU
)));
585 LLVMErrorRef
LLVMOrcMaterializationResponsibilityDelegate(
586 LLVMOrcMaterializationResponsibilityRef MR
,
587 LLVMOrcSymbolStringPoolEntryRef
*Symbols
, size_t NumSymbols
,
588 LLVMOrcMaterializationResponsibilityRef
*Result
) {
590 for (size_t I
= 0; I
!= NumSymbols
; I
++) {
591 Syms
.insert(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Symbols
[I
])));
593 auto OtherMR
= unwrap(MR
)->delegate(Syms
);
596 return wrap(OtherMR
.takeError());
598 *Result
= wrap(OtherMR
->release());
599 return LLVMErrorSuccess
;
602 void LLVMOrcMaterializationResponsibilityAddDependencies(
603 LLVMOrcMaterializationResponsibilityRef MR
,
604 LLVMOrcSymbolStringPoolEntryRef Name
,
605 LLVMOrcCDependenceMapPairs Dependencies
, size_t NumPairs
) {
607 SymbolDependenceMap SDM
= toSymbolDependenceMap(Dependencies
, NumPairs
);
608 auto Sym
= OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Name
));
609 unwrap(MR
)->addDependencies(Sym
, SDM
);
612 void LLVMOrcMaterializationResponsibilityAddDependenciesForAll(
613 LLVMOrcMaterializationResponsibilityRef MR
,
614 LLVMOrcCDependenceMapPairs Dependencies
, size_t NumPairs
) {
616 SymbolDependenceMap SDM
= toSymbolDependenceMap(Dependencies
, NumPairs
);
617 unwrap(MR
)->addDependenciesForAll(SDM
);
620 void LLVMOrcMaterializationResponsibilityFailMaterialization(
621 LLVMOrcMaterializationResponsibilityRef MR
) {
622 unwrap(MR
)->failMaterialization();
625 void LLVMOrcIRTransformLayerEmit(LLVMOrcIRTransformLayerRef IRLayer
,
626 LLVMOrcMaterializationResponsibilityRef MR
,
627 LLVMOrcThreadSafeModuleRef TSM
) {
628 std::unique_ptr
<ThreadSafeModule
> TmpTSM(unwrap(TSM
));
629 unwrap(IRLayer
)->emit(
630 std::unique_ptr
<MaterializationResponsibility
>(unwrap(MR
)),
635 LLVMOrcExecutionSessionCreateBareJITDylib(LLVMOrcExecutionSessionRef ES
,
637 return wrap(&unwrap(ES
)->createBareJITDylib(Name
));
641 LLVMOrcExecutionSessionCreateJITDylib(LLVMOrcExecutionSessionRef ES
,
642 LLVMOrcJITDylibRef
*Result
,
644 auto JD
= unwrap(ES
)->createJITDylib(Name
);
646 return wrap(JD
.takeError());
647 *Result
= wrap(&*JD
);
648 return LLVMErrorSuccess
;
652 LLVMOrcExecutionSessionGetJITDylibByName(LLVMOrcExecutionSessionRef ES
,
654 return wrap(unwrap(ES
)->getJITDylibByName(Name
));
657 LLVMErrorRef
LLVMOrcJITDylibDefine(LLVMOrcJITDylibRef JD
,
658 LLVMOrcMaterializationUnitRef MU
) {
659 std::unique_ptr
<MaterializationUnit
> TmpMU(unwrap(MU
));
661 if (auto Err
= unwrap(JD
)->define(TmpMU
)) {
663 return wrap(std::move(Err
));
665 return LLVMErrorSuccess
;
668 LLVMErrorRef
LLVMOrcJITDylibClear(LLVMOrcJITDylibRef JD
) {
669 return wrap(unwrap(JD
)->clear());
672 void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD
,
673 LLVMOrcDefinitionGeneratorRef DG
) {
674 unwrap(JD
)->addGenerator(std::unique_ptr
<DefinitionGenerator
>(unwrap(DG
)));
677 LLVMOrcDefinitionGeneratorRef
LLVMOrcCreateCustomCAPIDefinitionGenerator(
678 LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F
, void *Ctx
,
679 LLVMOrcDisposeCAPIDefinitionGeneratorFunction Dispose
) {
680 auto DG
= std::make_unique
<CAPIDefinitionGenerator
>(Dispose
, Ctx
, F
);
681 return wrap(DG
.release());
684 void LLVMOrcLookupStateContinueLookup(LLVMOrcLookupStateRef S
,
687 OrcV2CAPIHelper::resetLookupState(LS
, ::unwrap(S
));
688 LS
.continueLookup(unwrap(Err
));
691 LLVMErrorRef
LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(
692 LLVMOrcDefinitionGeneratorRef
*Result
, char GlobalPrefix
,
693 LLVMOrcSymbolPredicate Filter
, void *FilterCtx
) {
694 assert(Result
&& "Result can not be null");
695 assert((Filter
|| !FilterCtx
) &&
696 "if Filter is null then FilterCtx must also be null");
698 DynamicLibrarySearchGenerator::SymbolPredicate Pred
;
700 Pred
= [=](const SymbolStringPtr
&Name
) -> bool {
701 return Filter(FilterCtx
, wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name
)));
704 auto ProcessSymsGenerator
=
705 DynamicLibrarySearchGenerator::GetForCurrentProcess(GlobalPrefix
, Pred
);
707 if (!ProcessSymsGenerator
) {
709 return wrap(ProcessSymsGenerator
.takeError());
712 *Result
= wrap(ProcessSymsGenerator
->release());
713 return LLVMErrorSuccess
;
716 LLVMErrorRef
LLVMOrcCreateDynamicLibrarySearchGeneratorForPath(
717 LLVMOrcDefinitionGeneratorRef
*Result
, const char *FileName
,
718 char GlobalPrefix
, LLVMOrcSymbolPredicate Filter
, void *FilterCtx
) {
719 assert(Result
&& "Result can not be null");
720 assert(FileName
&& "FileName can not be null");
721 assert((Filter
|| !FilterCtx
) &&
722 "if Filter is null then FilterCtx must also be null");
724 DynamicLibrarySearchGenerator::SymbolPredicate Pred
;
726 Pred
= [=](const SymbolStringPtr
&Name
) -> bool {
727 return Filter(FilterCtx
, wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name
)));
730 auto LibrarySymsGenerator
=
731 DynamicLibrarySearchGenerator::Load(FileName
, GlobalPrefix
, Pred
);
733 if (!LibrarySymsGenerator
) {
735 return wrap(LibrarySymsGenerator
.takeError());
738 *Result
= wrap(LibrarySymsGenerator
->release());
739 return LLVMErrorSuccess
;
742 LLVMErrorRef
LLVMOrcCreateStaticLibrarySearchGeneratorForPath(
743 LLVMOrcDefinitionGeneratorRef
*Result
, LLVMOrcObjectLayerRef ObjLayer
,
744 const char *FileName
) {
745 assert(Result
&& "Result can not be null");
746 assert(FileName
&& "Filename can not be null");
747 assert(ObjLayer
&& "ObjectLayer can not be null");
749 auto LibrarySymsGenerator
=
750 StaticLibraryDefinitionGenerator::Load(*unwrap(ObjLayer
), FileName
);
751 if (!LibrarySymsGenerator
) {
753 return wrap(LibrarySymsGenerator
.takeError());
755 *Result
= wrap(LibrarySymsGenerator
->release());
756 return LLVMErrorSuccess
;
759 LLVMOrcThreadSafeContextRef
LLVMOrcCreateNewThreadSafeContext(void) {
760 return wrap(new ThreadSafeContext(std::make_unique
<LLVMContext
>()));
764 LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx
) {
765 return wrap(unwrap(TSCtx
)->getContext());
768 void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx
) {
769 delete unwrap(TSCtx
);
773 LLVMOrcThreadSafeModuleWithModuleDo(LLVMOrcThreadSafeModuleRef TSM
,
774 LLVMOrcGenericIRModuleOperationFunction F
,
776 return wrap(unwrap(TSM
)->withModuleDo(
777 [&](Module
&M
) { return unwrap(F(Ctx
, wrap(&M
))); }));
780 LLVMOrcThreadSafeModuleRef
781 LLVMOrcCreateNewThreadSafeModule(LLVMModuleRef M
,
782 LLVMOrcThreadSafeContextRef TSCtx
) {
784 new ThreadSafeModule(std::unique_ptr
<Module
>(unwrap(M
)), *unwrap(TSCtx
)));
787 void LLVMOrcDisposeThreadSafeModule(LLVMOrcThreadSafeModuleRef TSM
) {
791 LLVMErrorRef
LLVMOrcJITTargetMachineBuilderDetectHost(
792 LLVMOrcJITTargetMachineBuilderRef
*Result
) {
793 assert(Result
&& "Result can not be null");
795 auto JTMB
= JITTargetMachineBuilder::detectHost();
798 return wrap(JTMB
.takeError());
801 *Result
= wrap(new JITTargetMachineBuilder(std::move(*JTMB
)));
802 return LLVMErrorSuccess
;
805 LLVMOrcJITTargetMachineBuilderRef
806 LLVMOrcJITTargetMachineBuilderCreateFromTargetMachine(LLVMTargetMachineRef TM
) {
807 auto *TemplateTM
= unwrap(TM
);
810 std::make_unique
<JITTargetMachineBuilder
>(TemplateTM
->getTargetTriple());
813 .setCPU(TemplateTM
->getTargetCPU().str())
814 .setRelocationModel(TemplateTM
->getRelocationModel())
815 .setCodeModel(TemplateTM
->getCodeModel())
816 .setCodeGenOptLevel(TemplateTM
->getOptLevel())
817 .setFeatures(TemplateTM
->getTargetFeatureString())
818 .setOptions(TemplateTM
->Options
);
820 LLVMDisposeTargetMachine(TM
);
822 return wrap(JTMB
.release());
825 void LLVMOrcDisposeJITTargetMachineBuilder(
826 LLVMOrcJITTargetMachineBuilderRef JTMB
) {
830 char *LLVMOrcJITTargetMachineBuilderGetTargetTriple(
831 LLVMOrcJITTargetMachineBuilderRef JTMB
) {
832 auto Tmp
= unwrap(JTMB
)->getTargetTriple().str();
833 char *TargetTriple
= (char *)malloc(Tmp
.size() + 1);
834 strcpy(TargetTriple
, Tmp
.c_str());
838 void LLVMOrcJITTargetMachineBuilderSetTargetTriple(
839 LLVMOrcJITTargetMachineBuilderRef JTMB
, const char *TargetTriple
) {
840 unwrap(JTMB
)->getTargetTriple() = Triple(TargetTriple
);
843 LLVMErrorRef
LLVMOrcObjectLayerAddObjectFile(LLVMOrcObjectLayerRef ObjLayer
,
844 LLVMOrcJITDylibRef JD
,
845 LLVMMemoryBufferRef ObjBuffer
) {
846 return wrap(unwrap(ObjLayer
)->add(
847 *unwrap(JD
), std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
850 LLVMErrorRef
LLVMOrcObjectLayerAddObjectFileWithRT(LLVMOrcObjectLayerRef ObjLayer
,
851 LLVMOrcResourceTrackerRef RT
,
852 LLVMMemoryBufferRef ObjBuffer
) {
854 unwrap(ObjLayer
)->add(ResourceTrackerSP(unwrap(RT
)),
855 std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
858 void LLVMOrcObjectLayerEmit(LLVMOrcObjectLayerRef ObjLayer
,
859 LLVMOrcMaterializationResponsibilityRef R
,
860 LLVMMemoryBufferRef ObjBuffer
) {
861 unwrap(ObjLayer
)->emit(
862 std::unique_ptr
<MaterializationResponsibility
>(unwrap(R
)),
863 std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
)));
866 void LLVMOrcDisposeObjectLayer(LLVMOrcObjectLayerRef ObjLayer
) {
867 delete unwrap(ObjLayer
);
870 void LLVMOrcIRTransformLayerSetTransform(
871 LLVMOrcIRTransformLayerRef IRTransformLayer
,
872 LLVMOrcIRTransformLayerTransformFunction TransformFunction
, void *Ctx
) {
873 unwrap(IRTransformLayer
)
875 [=](ThreadSafeModule TSM
,
876 MaterializationResponsibility
&R
) -> Expected
<ThreadSafeModule
> {
877 LLVMOrcThreadSafeModuleRef TSMRef
=
878 wrap(new ThreadSafeModule(std::move(TSM
)));
879 if (LLVMErrorRef Err
= TransformFunction(Ctx
, &TSMRef
, wrap(&R
))) {
880 assert(!TSMRef
&& "TSMRef was not reset to null on error");
883 assert(TSMRef
&& "Transform succeeded, but TSMRef was set to null");
884 ThreadSafeModule Result
= std::move(*unwrap(TSMRef
));
885 LLVMOrcDisposeThreadSafeModule(TSMRef
);
886 return std::move(Result
);
890 void LLVMOrcObjectTransformLayerSetTransform(
891 LLVMOrcObjectTransformLayerRef ObjTransformLayer
,
892 LLVMOrcObjectTransformLayerTransformFunction TransformFunction
, void *Ctx
) {
893 unwrap(ObjTransformLayer
)
894 ->setTransform([TransformFunction
, Ctx
](std::unique_ptr
<MemoryBuffer
> Obj
)
895 -> Expected
<std::unique_ptr
<MemoryBuffer
>> {
896 LLVMMemoryBufferRef ObjBuffer
= wrap(Obj
.release());
897 if (LLVMErrorRef Err
= TransformFunction(Ctx
, &ObjBuffer
)) {
898 assert(!ObjBuffer
&& "ObjBuffer was not reset to null on error");
901 return std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
));
905 LLVMOrcDumpObjectsRef
LLVMOrcCreateDumpObjects(const char *DumpDir
,
906 const char *IdentifierOverride
) {
907 assert(DumpDir
&& "DumpDir should not be null");
908 assert(IdentifierOverride
&& "IdentifierOverride should not be null");
909 return wrap(new DumpObjects(DumpDir
, IdentifierOverride
));
912 void LLVMOrcDisposeDumpObjects(LLVMOrcDumpObjectsRef DumpObjects
) {
913 delete unwrap(DumpObjects
);
916 LLVMErrorRef
LLVMOrcDumpObjects_CallOperator(LLVMOrcDumpObjectsRef DumpObjects
,
917 LLVMMemoryBufferRef
*ObjBuffer
) {
918 std::unique_ptr
<MemoryBuffer
> OB(unwrap(*ObjBuffer
));
919 if (auto Result
= (*unwrap(DumpObjects
))(std::move(OB
))) {
920 *ObjBuffer
= wrap(Result
->release());
921 return LLVMErrorSuccess
;
923 *ObjBuffer
= nullptr;
924 return wrap(Result
.takeError());
928 LLVMOrcLLJITBuilderRef
LLVMOrcCreateLLJITBuilder(void) {
929 return wrap(new LLJITBuilder());
932 void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder
) {
933 delete unwrap(Builder
);
936 void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder(
937 LLVMOrcLLJITBuilderRef Builder
, LLVMOrcJITTargetMachineBuilderRef JTMB
) {
938 unwrap(Builder
)->setJITTargetMachineBuilder(std::move(*unwrap(JTMB
)));
939 LLVMOrcDisposeJITTargetMachineBuilder(JTMB
);
942 void LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator(
943 LLVMOrcLLJITBuilderRef Builder
,
944 LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction F
, void *Ctx
) {
945 unwrap(Builder
)->setObjectLinkingLayerCreator(
946 [=](ExecutionSession
&ES
, const Triple
&TT
) {
947 auto TTStr
= TT
.str();
948 return std::unique_ptr
<ObjectLayer
>(
949 unwrap(F(Ctx
, wrap(&ES
), TTStr
.c_str())));
953 LLVMErrorRef
LLVMOrcCreateLLJIT(LLVMOrcLLJITRef
*Result
,
954 LLVMOrcLLJITBuilderRef Builder
) {
955 assert(Result
&& "Result can not be null");
958 Builder
= LLVMOrcCreateLLJITBuilder();
960 auto J
= unwrap(Builder
)->create();
961 LLVMOrcDisposeLLJITBuilder(Builder
);
965 return wrap(J
.takeError());
968 *Result
= wrap(J
->release());
969 return LLVMErrorSuccess
;
972 LLVMErrorRef
LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J
) {
974 return LLVMErrorSuccess
;
977 LLVMOrcExecutionSessionRef
LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J
) {
978 return wrap(&unwrap(J
)->getExecutionSession());
981 LLVMOrcJITDylibRef
LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J
) {
982 return wrap(&unwrap(J
)->getMainJITDylib());
985 const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J
) {
986 return unwrap(J
)->getTargetTriple().str().c_str();
989 char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J
) {
990 return unwrap(J
)->getDataLayout().getGlobalPrefix();
993 LLVMOrcSymbolStringPoolEntryRef
994 LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J
, const char *UnmangledName
) {
995 return wrap(OrcV2CAPIHelper::moveFromSymbolStringPtr(
996 unwrap(J
)->mangleAndIntern(UnmangledName
)));
999 LLVMErrorRef
LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J
, LLVMOrcJITDylibRef JD
,
1000 LLVMMemoryBufferRef ObjBuffer
) {
1001 return wrap(unwrap(J
)->addObjectFile(
1002 *unwrap(JD
), std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
1005 LLVMErrorRef
LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcLLJITRef J
,
1006 LLVMOrcResourceTrackerRef RT
,
1007 LLVMMemoryBufferRef ObjBuffer
) {
1008 return wrap(unwrap(J
)->addObjectFile(
1009 ResourceTrackerSP(unwrap(RT
)),
1010 std::unique_ptr
<MemoryBuffer
>(unwrap(ObjBuffer
))));
1013 LLVMErrorRef
LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J
,
1014 LLVMOrcJITDylibRef JD
,
1015 LLVMOrcThreadSafeModuleRef TSM
) {
1016 std::unique_ptr
<ThreadSafeModule
> TmpTSM(unwrap(TSM
));
1017 return wrap(unwrap(J
)->addIRModule(*unwrap(JD
), std::move(*TmpTSM
)));
1020 LLVMErrorRef
LLVMOrcLLJITAddLLVMIRModuleWithRT(LLVMOrcLLJITRef J
,
1021 LLVMOrcResourceTrackerRef RT
,
1022 LLVMOrcThreadSafeModuleRef TSM
) {
1023 std::unique_ptr
<ThreadSafeModule
> TmpTSM(unwrap(TSM
));
1024 return wrap(unwrap(J
)->addIRModule(ResourceTrackerSP(unwrap(RT
)),
1025 std::move(*TmpTSM
)));
1028 LLVMErrorRef
LLVMOrcLLJITLookup(LLVMOrcLLJITRef J
,
1029 LLVMOrcJITTargetAddress
*Result
,
1031 assert(Result
&& "Result can not be null");
1033 auto Sym
= unwrap(J
)->lookup(Name
);
1036 return wrap(Sym
.takeError());
1039 *Result
= Sym
->getValue();
1040 return LLVMErrorSuccess
;
1043 LLVMOrcObjectLayerRef
LLVMOrcLLJITGetObjLinkingLayer(LLVMOrcLLJITRef J
) {
1044 return wrap(&unwrap(J
)->getObjLinkingLayer());
1047 LLVMOrcObjectTransformLayerRef
1048 LLVMOrcLLJITGetObjTransformLayer(LLVMOrcLLJITRef J
) {
1049 return wrap(&unwrap(J
)->getObjTransformLayer());
1052 LLVMOrcObjectLayerRef
1053 LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(
1054 LLVMOrcExecutionSessionRef ES
) {
1055 assert(ES
&& "ES must not be null");
1056 return wrap(new RTDyldObjectLinkingLayer(
1057 *unwrap(ES
), [] { return std::make_unique
<SectionMemoryManager
>(); }));
1060 LLVMOrcObjectLayerRef
1061 LLVMOrcCreateRTDyldObjectLinkingLayerWithMCJITMemoryManagerLikeCallbacks(
1062 LLVMOrcExecutionSessionRef ES
, void *CreateContextCtx
,
1063 LLVMMemoryManagerCreateContextCallback CreateContext
,
1064 LLVMMemoryManagerNotifyTerminatingCallback NotifyTerminating
,
1065 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection
,
1066 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection
,
1067 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory
,
1068 LLVMMemoryManagerDestroyCallback Destroy
) {
1070 struct MCJITMemoryManagerLikeCallbacks
{
1071 MCJITMemoryManagerLikeCallbacks() = default;
1072 MCJITMemoryManagerLikeCallbacks(
1073 void *CreateContextCtx
,
1074 LLVMMemoryManagerCreateContextCallback CreateContext
,
1075 LLVMMemoryManagerNotifyTerminatingCallback NotifyTerminating
,
1076 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection
,
1077 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection
,
1078 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory
,
1079 LLVMMemoryManagerDestroyCallback Destroy
)
1080 : CreateContextCtx(CreateContextCtx
), CreateContext(CreateContext
),
1081 NotifyTerminating(NotifyTerminating
),
1082 AllocateCodeSection(AllocateCodeSection
),
1083 AllocateDataSection(AllocateDataSection
),
1084 FinalizeMemory(FinalizeMemory
), Destroy(Destroy
) {}
1086 MCJITMemoryManagerLikeCallbacks(MCJITMemoryManagerLikeCallbacks
&&Other
) {
1087 std::swap(CreateContextCtx
, Other
.CreateContextCtx
);
1088 std::swap(CreateContext
, Other
.CreateContext
);
1089 std::swap(NotifyTerminating
, Other
.NotifyTerminating
);
1090 std::swap(AllocateCodeSection
, Other
.AllocateCodeSection
);
1091 std::swap(AllocateDataSection
, Other
.AllocateDataSection
);
1092 std::swap(FinalizeMemory
, Other
.FinalizeMemory
);
1093 std::swap(Destroy
, Other
.Destroy
);
1096 ~MCJITMemoryManagerLikeCallbacks() {
1097 if (NotifyTerminating
)
1098 NotifyTerminating(CreateContextCtx
);
1101 void *CreateContextCtx
= nullptr;
1102 LLVMMemoryManagerCreateContextCallback CreateContext
= nullptr;
1103 LLVMMemoryManagerNotifyTerminatingCallback NotifyTerminating
= nullptr;
1104 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection
= nullptr;
1105 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection
= nullptr;
1106 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory
= nullptr;
1107 LLVMMemoryManagerDestroyCallback Destroy
= nullptr;
1110 class MCJITMemoryManagerLikeCallbacksMemMgr
: public RTDyldMemoryManager
{
1112 MCJITMemoryManagerLikeCallbacksMemMgr(
1113 const MCJITMemoryManagerLikeCallbacks
&CBs
)
1115 Opaque
= CBs
.CreateContext(CBs
.CreateContextCtx
);
1117 ~MCJITMemoryManagerLikeCallbacksMemMgr() override
{ CBs
.Destroy(Opaque
); }
1119 uint8_t *allocateCodeSection(uintptr_t Size
, unsigned Alignment
,
1121 StringRef SectionName
) override
{
1122 return CBs
.AllocateCodeSection(Opaque
, Size
, Alignment
, SectionID
,
1123 SectionName
.str().c_str());
1126 uint8_t *allocateDataSection(uintptr_t Size
, unsigned Alignment
,
1127 unsigned SectionID
, StringRef SectionName
,
1128 bool isReadOnly
) override
{
1129 return CBs
.AllocateDataSection(Opaque
, Size
, Alignment
, SectionID
,
1130 SectionName
.str().c_str(), isReadOnly
);
1133 bool finalizeMemory(std::string
*ErrMsg
) override
{
1134 char *ErrMsgCString
= nullptr;
1135 bool Result
= CBs
.FinalizeMemory(Opaque
, &ErrMsgCString
);
1136 assert((Result
|| !ErrMsgCString
) &&
1137 "Did not expect an error message if FinalizeMemory succeeded");
1138 if (ErrMsgCString
) {
1140 *ErrMsg
= ErrMsgCString
;
1141 free(ErrMsgCString
);
1147 const MCJITMemoryManagerLikeCallbacks
&CBs
;
1148 void *Opaque
= nullptr;
1151 assert(ES
&& "ES must not be null");
1152 assert(CreateContext
&& "CreateContext must not be null");
1153 assert(NotifyTerminating
&& "NotifyTerminating must not be null");
1154 assert(AllocateCodeSection
&& "AllocateCodeSection must not be null");
1155 assert(AllocateDataSection
&& "AllocateDataSection must not be null");
1156 assert(FinalizeMemory
&& "FinalizeMemory must not be null");
1157 assert(Destroy
&& "Destroy must not be null");
1159 MCJITMemoryManagerLikeCallbacks
CBs(
1160 CreateContextCtx
, CreateContext
, NotifyTerminating
, AllocateCodeSection
,
1161 AllocateDataSection
, FinalizeMemory
, Destroy
);
1163 return wrap(new RTDyldObjectLinkingLayer(*unwrap(ES
), [CBs
= std::move(CBs
)] {
1164 return std::make_unique
<MCJITMemoryManagerLikeCallbacksMemMgr
>(CBs
);
1170 void LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(
1171 LLVMOrcObjectLayerRef RTDyldObjLinkingLayer
,
1172 LLVMJITEventListenerRef Listener
) {
1173 assert(RTDyldObjLinkingLayer
&& "RTDyldObjLinkingLayer must not be null");
1174 assert(Listener
&& "Listener must not be null");
1175 reinterpret_cast<RTDyldObjectLinkingLayer
*>(unwrap(RTDyldObjLinkingLayer
))
1176 ->registerJITEventListener(*unwrap(Listener
));
1179 LLVMOrcIRTransformLayerRef
LLVMOrcLLJITGetIRTransformLayer(LLVMOrcLLJITRef J
) {
1180 return wrap(&unwrap(J
)->getIRTransformLayer());
1183 const char *LLVMOrcLLJITGetDataLayoutStr(LLVMOrcLLJITRef J
) {
1184 return unwrap(J
)->getDataLayout().getStringRepresentation().c_str();
1187 LLVMOrcIndirectStubsManagerRef
1188 LLVMOrcCreateLocalIndirectStubsManager(const char *TargetTriple
) {
1189 auto builder
= createLocalIndirectStubsManagerBuilder(Triple(TargetTriple
));
1190 return wrap(builder().release());
1193 void LLVMOrcDisposeIndirectStubsManager(LLVMOrcIndirectStubsManagerRef ISM
) {
1194 std::unique_ptr
<IndirectStubsManager
> TmpISM(unwrap(ISM
));
1197 LLVMErrorRef
LLVMOrcCreateLocalLazyCallThroughManager(
1198 const char *TargetTriple
, LLVMOrcExecutionSessionRef ES
,
1199 LLVMOrcJITTargetAddress ErrorHandlerAddr
,
1200 LLVMOrcLazyCallThroughManagerRef
*Result
) {
1201 auto LCTM
= createLocalLazyCallThroughManager(
1202 Triple(TargetTriple
), *unwrap(ES
), ExecutorAddr(ErrorHandlerAddr
));
1205 return wrap(LCTM
.takeError());
1206 *Result
= wrap(LCTM
->release());
1207 return LLVMErrorSuccess
;
1210 void LLVMOrcDisposeLazyCallThroughManager(
1211 LLVMOrcLazyCallThroughManagerRef LCM
) {
1212 std::unique_ptr
<LazyCallThroughManager
> TmpLCM(unwrap(LCM
));