15ffd83dbSDimitry Andric //===--------------- OrcV2CBindings.cpp - C bindings OrcV2 APIs -----------===// 25ffd83dbSDimitry Andric // 35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65ffd83dbSDimitry Andric // 75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===// 85ffd83dbSDimitry Andric 9e8d8bef9SDimitry Andric #include "llvm-c/LLJIT.h" 105ffd83dbSDimitry Andric #include "llvm-c/Orc.h" 11e8d8bef9SDimitry Andric #include "llvm-c/OrcEE.h" 125ffd83dbSDimitry Andric #include "llvm-c/TargetMachine.h" 135ffd83dbSDimitry Andric 145ffd83dbSDimitry Andric #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" 155ffd83dbSDimitry Andric #include "llvm/ExecutionEngine/Orc/LLJIT.h" 16e8d8bef9SDimitry Andric #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" 17e8d8bef9SDimitry Andric #include "llvm/ExecutionEngine/SectionMemoryManager.h" 185ffd83dbSDimitry Andric 195ffd83dbSDimitry Andric using namespace llvm; 205ffd83dbSDimitry Andric using namespace llvm::orc; 215ffd83dbSDimitry Andric 225ffd83dbSDimitry Andric namespace llvm { 235ffd83dbSDimitry Andric namespace orc { 245ffd83dbSDimitry Andric 25e8d8bef9SDimitry Andric class InProgressLookupState; 26e8d8bef9SDimitry Andric 275ffd83dbSDimitry Andric class OrcV2CAPIHelper { 285ffd83dbSDimitry Andric public: 295ffd83dbSDimitry Andric using PoolEntry = SymbolStringPtr::PoolEntry; 305ffd83dbSDimitry Andric using PoolEntryPtr = SymbolStringPtr::PoolEntryPtr; 315ffd83dbSDimitry Andric 325ffd83dbSDimitry Andric static PoolEntryPtr releaseSymbolStringPtr(SymbolStringPtr S) { 335ffd83dbSDimitry Andric PoolEntryPtr Result = nullptr; 345ffd83dbSDimitry Andric std::swap(Result, S.S); 355ffd83dbSDimitry Andric return Result; 365ffd83dbSDimitry Andric } 375ffd83dbSDimitry Andric 38e8d8bef9SDimitry Andric static SymbolStringPtr retainSymbolStringPtr(PoolEntryPtr P) { 39e8d8bef9SDimitry Andric return SymbolStringPtr(P); 40e8d8bef9SDimitry Andric } 41e8d8bef9SDimitry Andric 425ffd83dbSDimitry Andric static PoolEntryPtr getRawPoolEntryPtr(const SymbolStringPtr &S) { 435ffd83dbSDimitry Andric return S.S; 445ffd83dbSDimitry Andric } 455ffd83dbSDimitry Andric 46e8d8bef9SDimitry Andric static void retainPoolEntry(PoolEntryPtr P) { 47e8d8bef9SDimitry Andric SymbolStringPtr S(P); 48e8d8bef9SDimitry Andric S.S = nullptr; 49e8d8bef9SDimitry Andric } 50e8d8bef9SDimitry Andric 515ffd83dbSDimitry Andric static void releasePoolEntry(PoolEntryPtr P) { 525ffd83dbSDimitry Andric SymbolStringPtr S; 535ffd83dbSDimitry Andric S.S = P; 545ffd83dbSDimitry Andric } 55e8d8bef9SDimitry Andric 56e8d8bef9SDimitry Andric static InProgressLookupState *extractLookupState(LookupState &LS) { 57e8d8bef9SDimitry Andric return LS.IPLS.release(); 58e8d8bef9SDimitry Andric } 59e8d8bef9SDimitry Andric 60e8d8bef9SDimitry Andric static void resetLookupState(LookupState &LS, InProgressLookupState *IPLS) { 61e8d8bef9SDimitry Andric return LS.reset(IPLS); 62e8d8bef9SDimitry Andric } 635ffd83dbSDimitry Andric }; 645ffd83dbSDimitry Andric 65e8d8bef9SDimitry Andric } // namespace orc 66e8d8bef9SDimitry Andric } // namespace llvm 675ffd83dbSDimitry Andric 685ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionSession, LLVMOrcExecutionSessionRef) 69e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SymbolStringPool, LLVMOrcSymbolStringPoolRef) 705ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcV2CAPIHelper::PoolEntry, 715ffd83dbSDimitry Andric LLVMOrcSymbolStringPoolEntryRef) 72e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationUnit, 73e8d8bef9SDimitry Andric LLVMOrcMaterializationUnitRef) 745ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITDylib, LLVMOrcJITDylibRef) 75e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ResourceTracker, LLVMOrcResourceTrackerRef) 76e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DefinitionGenerator, 77e8d8bef9SDimitry Andric LLVMOrcDefinitionGeneratorRef) 78e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(InProgressLookupState, LLVMOrcLookupStateRef) 795ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeContext, 805ffd83dbSDimitry Andric LLVMOrcThreadSafeContextRef) 815ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeModule, LLVMOrcThreadSafeModuleRef) 825ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITTargetMachineBuilder, 835ffd83dbSDimitry Andric LLVMOrcJITTargetMachineBuilderRef) 84e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectLayer, LLVMOrcObjectLayerRef) 855ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJITBuilder, LLVMOrcLLJITBuilderRef) 865ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJIT, LLVMOrcLLJITRef) 875ffd83dbSDimitry Andric 885ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef) 895ffd83dbSDimitry Andric 90e8d8bef9SDimitry Andric namespace llvm { 91e8d8bef9SDimitry Andric namespace orc { 92e8d8bef9SDimitry Andric 93e8d8bef9SDimitry Andric class CAPIDefinitionGenerator final : public DefinitionGenerator { 94e8d8bef9SDimitry Andric public: 95e8d8bef9SDimitry Andric CAPIDefinitionGenerator( 96e8d8bef9SDimitry Andric void *Ctx, 97e8d8bef9SDimitry Andric LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate) 98e8d8bef9SDimitry Andric : Ctx(Ctx), TryToGenerate(TryToGenerate) {} 99e8d8bef9SDimitry Andric 100e8d8bef9SDimitry Andric Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, 101e8d8bef9SDimitry Andric JITDylibLookupFlags JDLookupFlags, 102e8d8bef9SDimitry Andric const SymbolLookupSet &LookupSet) override { 103e8d8bef9SDimitry Andric 104e8d8bef9SDimitry Andric // Take the lookup state. 105e8d8bef9SDimitry Andric LLVMOrcLookupStateRef LSR = ::wrap(OrcV2CAPIHelper::extractLookupState(LS)); 106e8d8bef9SDimitry Andric 107e8d8bef9SDimitry Andric // Translate the lookup kind. 108e8d8bef9SDimitry Andric LLVMOrcLookupKind CLookupKind; 109e8d8bef9SDimitry Andric switch (K) { 110e8d8bef9SDimitry Andric case LookupKind::Static: 111e8d8bef9SDimitry Andric CLookupKind = LLVMOrcLookupKindStatic; 112e8d8bef9SDimitry Andric break; 113e8d8bef9SDimitry Andric case LookupKind::DLSym: 114e8d8bef9SDimitry Andric CLookupKind = LLVMOrcLookupKindDLSym; 115e8d8bef9SDimitry Andric break; 116e8d8bef9SDimitry Andric } 117e8d8bef9SDimitry Andric 118e8d8bef9SDimitry Andric // Translate the JITDylibSearchFlags. 119e8d8bef9SDimitry Andric LLVMOrcJITDylibLookupFlags CJDLookupFlags; 120e8d8bef9SDimitry Andric switch (JDLookupFlags) { 121e8d8bef9SDimitry Andric case JITDylibLookupFlags::MatchExportedSymbolsOnly: 122e8d8bef9SDimitry Andric CJDLookupFlags = LLVMOrcJITDylibLookupFlagsMatchExportedSymbolsOnly; 123e8d8bef9SDimitry Andric break; 124e8d8bef9SDimitry Andric case JITDylibLookupFlags::MatchAllSymbols: 125e8d8bef9SDimitry Andric CJDLookupFlags = LLVMOrcJITDylibLookupFlagsMatchAllSymbols; 126e8d8bef9SDimitry Andric break; 127e8d8bef9SDimitry Andric } 128e8d8bef9SDimitry Andric 129e8d8bef9SDimitry Andric // Translate the lookup set. 130e8d8bef9SDimitry Andric std::vector<LLVMOrcCLookupSetElement> CLookupSet; 131e8d8bef9SDimitry Andric CLookupSet.reserve(LookupSet.size()); 132e8d8bef9SDimitry Andric for (auto &KV : LookupSet) { 133e8d8bef9SDimitry Andric LLVMOrcSymbolLookupFlags SLF; 134e8d8bef9SDimitry Andric LLVMOrcSymbolStringPoolEntryRef Name = 135e8d8bef9SDimitry Andric ::wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(KV.first)); 136e8d8bef9SDimitry Andric switch (KV.second) { 137e8d8bef9SDimitry Andric case SymbolLookupFlags::RequiredSymbol: 138e8d8bef9SDimitry Andric SLF = LLVMOrcSymbolLookupFlagsRequiredSymbol; 139e8d8bef9SDimitry Andric break; 140e8d8bef9SDimitry Andric case SymbolLookupFlags::WeaklyReferencedSymbol: 141e8d8bef9SDimitry Andric SLF = LLVMOrcSymbolLookupFlagsWeaklyReferencedSymbol; 142e8d8bef9SDimitry Andric break; 143e8d8bef9SDimitry Andric } 144e8d8bef9SDimitry Andric CLookupSet.push_back({Name, SLF}); 145e8d8bef9SDimitry Andric } 146e8d8bef9SDimitry Andric 147e8d8bef9SDimitry Andric // Run the C TryToGenerate function. 148e8d8bef9SDimitry Andric auto Err = unwrap(TryToGenerate(::wrap(this), Ctx, &LSR, CLookupKind, 149e8d8bef9SDimitry Andric ::wrap(&JD), CJDLookupFlags, 150e8d8bef9SDimitry Andric CLookupSet.data(), CLookupSet.size())); 151e8d8bef9SDimitry Andric 152e8d8bef9SDimitry Andric // Restore the lookup state. 153e8d8bef9SDimitry Andric OrcV2CAPIHelper::resetLookupState(LS, ::unwrap(LSR)); 154e8d8bef9SDimitry Andric 155e8d8bef9SDimitry Andric return Err; 156e8d8bef9SDimitry Andric } 157e8d8bef9SDimitry Andric 158e8d8bef9SDimitry Andric private: 159e8d8bef9SDimitry Andric void *Ctx; 160e8d8bef9SDimitry Andric LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate; 161e8d8bef9SDimitry Andric }; 162e8d8bef9SDimitry Andric 163e8d8bef9SDimitry Andric } // end namespace orc 164e8d8bef9SDimitry Andric } // end namespace llvm 165e8d8bef9SDimitry Andric 166e8d8bef9SDimitry Andric void LLVMOrcExecutionSessionSetErrorReporter( 167e8d8bef9SDimitry Andric LLVMOrcExecutionSessionRef ES, LLVMOrcErrorReporterFunction ReportError, 168e8d8bef9SDimitry Andric void *Ctx) { 169e8d8bef9SDimitry Andric unwrap(ES)->setErrorReporter( 170e8d8bef9SDimitry Andric [=](Error Err) { ReportError(Ctx, wrap(std::move(Err))); }); 171e8d8bef9SDimitry Andric } 172e8d8bef9SDimitry Andric 173e8d8bef9SDimitry Andric LLVMOrcSymbolStringPoolRef 174e8d8bef9SDimitry Andric LLVMOrcExecutionSessionGetSymbolStringPool(LLVMOrcExecutionSessionRef ES) { 175e8d8bef9SDimitry Andric return wrap(unwrap(ES)->getSymbolStringPool().get()); 176e8d8bef9SDimitry Andric } 177e8d8bef9SDimitry Andric 178e8d8bef9SDimitry Andric void LLVMOrcSymbolStringPoolClearDeadEntries(LLVMOrcSymbolStringPoolRef SSP) { 179e8d8bef9SDimitry Andric unwrap(SSP)->clearDeadEntries(); 180e8d8bef9SDimitry Andric } 181e8d8bef9SDimitry Andric 1825ffd83dbSDimitry Andric LLVMOrcSymbolStringPoolEntryRef 1835ffd83dbSDimitry Andric LLVMOrcExecutionSessionIntern(LLVMOrcExecutionSessionRef ES, const char *Name) { 1845ffd83dbSDimitry Andric return wrap( 1855ffd83dbSDimitry Andric OrcV2CAPIHelper::releaseSymbolStringPtr(unwrap(ES)->intern(Name))); 1865ffd83dbSDimitry Andric } 1875ffd83dbSDimitry Andric 188e8d8bef9SDimitry Andric void LLVMOrcRetainSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S) { 189e8d8bef9SDimitry Andric OrcV2CAPIHelper::retainPoolEntry(unwrap(S)); 190e8d8bef9SDimitry Andric } 191e8d8bef9SDimitry Andric 1925ffd83dbSDimitry Andric void LLVMOrcReleaseSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S) { 1935ffd83dbSDimitry Andric OrcV2CAPIHelper::releasePoolEntry(unwrap(S)); 1945ffd83dbSDimitry Andric } 1955ffd83dbSDimitry Andric 196e8d8bef9SDimitry Andric const char *LLVMOrcSymbolStringPoolEntryStr(LLVMOrcSymbolStringPoolEntryRef S) { 197e8d8bef9SDimitry Andric return unwrap(S)->getKey().data(); 198e8d8bef9SDimitry Andric } 199e8d8bef9SDimitry Andric 200e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef 201e8d8bef9SDimitry Andric LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD) { 202e8d8bef9SDimitry Andric auto RT = unwrap(JD)->createResourceTracker(); 203e8d8bef9SDimitry Andric // Retain the pointer for the C API client. 204e8d8bef9SDimitry Andric RT->Retain(); 205e8d8bef9SDimitry Andric return wrap(RT.get()); 206e8d8bef9SDimitry Andric } 207e8d8bef9SDimitry Andric 208e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef 209e8d8bef9SDimitry Andric LLVMOrcJITDylibGetDefaultResourceTracker(LLVMOrcJITDylibRef JD) { 210e8d8bef9SDimitry Andric auto RT = unwrap(JD)->getDefaultResourceTracker(); 211e8d8bef9SDimitry Andric // Retain the pointer for the C API client. 212e8d8bef9SDimitry Andric return wrap(RT.get()); 213e8d8bef9SDimitry Andric } 214e8d8bef9SDimitry Andric 215e8d8bef9SDimitry Andric void LLVMOrcReleaseResourceTracker(LLVMOrcResourceTrackerRef RT) { 216e8d8bef9SDimitry Andric ResourceTrackerSP TmpRT(unwrap(RT)); 217e8d8bef9SDimitry Andric TmpRT->Release(); 218e8d8bef9SDimitry Andric } 219e8d8bef9SDimitry Andric 220e8d8bef9SDimitry Andric void LLVMOrcResourceTrackerTransferTo(LLVMOrcResourceTrackerRef SrcRT, 221e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef DstRT) { 222e8d8bef9SDimitry Andric ResourceTrackerSP TmpRT(unwrap(SrcRT)); 223e8d8bef9SDimitry Andric TmpRT->transferTo(*unwrap(DstRT)); 224e8d8bef9SDimitry Andric } 225e8d8bef9SDimitry Andric 226e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT) { 227e8d8bef9SDimitry Andric ResourceTrackerSP TmpRT(unwrap(RT)); 228e8d8bef9SDimitry Andric return wrap(TmpRT->remove()); 229e8d8bef9SDimitry Andric } 230e8d8bef9SDimitry Andric 231e8d8bef9SDimitry Andric void LLVMOrcDisposeDefinitionGenerator(LLVMOrcDefinitionGeneratorRef DG) { 232e8d8bef9SDimitry Andric std::unique_ptr<DefinitionGenerator> TmpDG(unwrap(DG)); 233e8d8bef9SDimitry Andric } 234e8d8bef9SDimitry Andric 235e8d8bef9SDimitry Andric void LLVMOrcDisposeMaterializationUnit(LLVMOrcMaterializationUnitRef MU) { 236e8d8bef9SDimitry Andric std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU)); 237e8d8bef9SDimitry Andric } 238e8d8bef9SDimitry Andric 239e8d8bef9SDimitry Andric LLVMOrcMaterializationUnitRef 240e8d8bef9SDimitry Andric LLVMOrcAbsoluteSymbols(LLVMOrcCSymbolMapPairs Syms, size_t NumPairs) { 241e8d8bef9SDimitry Andric SymbolMap SM; 242e8d8bef9SDimitry Andric for (size_t I = 0; I != NumPairs; ++I) { 243e8d8bef9SDimitry Andric JITSymbolFlags Flags; 244e8d8bef9SDimitry Andric 245e8d8bef9SDimitry Andric if (Syms[I].Sym.Flags.GenericFlags & LLVMJITSymbolGenericFlagsExported) 246e8d8bef9SDimitry Andric Flags |= JITSymbolFlags::Exported; 247e8d8bef9SDimitry Andric if (Syms[I].Sym.Flags.GenericFlags & LLVMJITSymbolGenericFlagsWeak) 248e8d8bef9SDimitry Andric Flags |= JITSymbolFlags::Weak; 249e8d8bef9SDimitry Andric 250e8d8bef9SDimitry Andric Flags.getTargetFlags() = Syms[I].Sym.Flags.TargetFlags; 251e8d8bef9SDimitry Andric 252e8d8bef9SDimitry Andric SM[OrcV2CAPIHelper::retainSymbolStringPtr(unwrap(Syms[I].Name))] = 253e8d8bef9SDimitry Andric JITEvaluatedSymbol(Syms[I].Sym.Address, Flags); 254e8d8bef9SDimitry Andric } 255e8d8bef9SDimitry Andric 256e8d8bef9SDimitry Andric return wrap(absoluteSymbols(std::move(SM)).release()); 257e8d8bef9SDimitry Andric } 258e8d8bef9SDimitry Andric 259e8d8bef9SDimitry Andric LLVMOrcJITDylibRef 260e8d8bef9SDimitry Andric LLVMOrcExecutionSessionCreateBareJITDylib(LLVMOrcExecutionSessionRef ES, 261e8d8bef9SDimitry Andric const char *Name) { 262e8d8bef9SDimitry Andric return wrap(&unwrap(ES)->createBareJITDylib(Name)); 263e8d8bef9SDimitry Andric } 264e8d8bef9SDimitry Andric 265e8d8bef9SDimitry Andric LLVMErrorRef 266e8d8bef9SDimitry Andric LLVMOrcExecutionSessionCreateJITDylib(LLVMOrcExecutionSessionRef ES, 267e8d8bef9SDimitry Andric LLVMOrcJITDylibRef *Result, 268e8d8bef9SDimitry Andric const char *Name) { 269e8d8bef9SDimitry Andric auto JD = unwrap(ES)->createJITDylib(Name); 270e8d8bef9SDimitry Andric if (!JD) 271e8d8bef9SDimitry Andric return wrap(JD.takeError()); 272e8d8bef9SDimitry Andric *Result = wrap(&*JD); 273e8d8bef9SDimitry Andric return LLVMErrorSuccess; 274e8d8bef9SDimitry Andric } 275e8d8bef9SDimitry Andric 276e8d8bef9SDimitry Andric LLVMOrcJITDylibRef 277e8d8bef9SDimitry Andric LLVMOrcExecutionSessionGetJITDylibByName(LLVMOrcExecutionSessionRef ES, 278e8d8bef9SDimitry Andric const char *Name) { 279e8d8bef9SDimitry Andric return wrap(unwrap(ES)->getJITDylibByName(Name)); 280e8d8bef9SDimitry Andric } 281e8d8bef9SDimitry Andric 282e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcJITDylibDefine(LLVMOrcJITDylibRef JD, 283e8d8bef9SDimitry Andric LLVMOrcMaterializationUnitRef MU) { 284e8d8bef9SDimitry Andric std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU)); 285e8d8bef9SDimitry Andric 286e8d8bef9SDimitry Andric if (auto Err = unwrap(JD)->define(TmpMU)) { 287e8d8bef9SDimitry Andric TmpMU.release(); 288e8d8bef9SDimitry Andric return wrap(std::move(Err)); 289e8d8bef9SDimitry Andric } 290e8d8bef9SDimitry Andric return LLVMErrorSuccess; 291e8d8bef9SDimitry Andric } 292e8d8bef9SDimitry Andric 293e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcJITDylibClear(LLVMOrcJITDylibRef JD) { 294e8d8bef9SDimitry Andric return wrap(unwrap(JD)->clear()); 2955ffd83dbSDimitry Andric } 2965ffd83dbSDimitry Andric 2975ffd83dbSDimitry Andric void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD, 298e8d8bef9SDimitry Andric LLVMOrcDefinitionGeneratorRef DG) { 299e8d8bef9SDimitry Andric unwrap(JD)->addGenerator(std::unique_ptr<DefinitionGenerator>(unwrap(DG))); 300e8d8bef9SDimitry Andric } 301e8d8bef9SDimitry Andric 302e8d8bef9SDimitry Andric LLVMOrcDefinitionGeneratorRef LLVMOrcCreateCustomCAPIDefinitionGenerator( 303e8d8bef9SDimitry Andric LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F, void *Ctx) { 304e8d8bef9SDimitry Andric auto DG = std::make_unique<CAPIDefinitionGenerator>(Ctx, F); 305e8d8bef9SDimitry Andric return wrap(DG.release()); 3065ffd83dbSDimitry Andric } 3075ffd83dbSDimitry Andric 3085ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess( 309e8d8bef9SDimitry Andric LLVMOrcDefinitionGeneratorRef *Result, char GlobalPrefix, 3105ffd83dbSDimitry Andric LLVMOrcSymbolPredicate Filter, void *FilterCtx) { 3115ffd83dbSDimitry Andric assert(Result && "Result can not be null"); 3125ffd83dbSDimitry Andric assert((Filter || !FilterCtx) && 3135ffd83dbSDimitry Andric "if Filter is null then FilterCtx must also be null"); 3145ffd83dbSDimitry Andric 3155ffd83dbSDimitry Andric DynamicLibrarySearchGenerator::SymbolPredicate Pred; 3165ffd83dbSDimitry Andric if (Filter) 3175ffd83dbSDimitry Andric Pred = [=](const SymbolStringPtr &Name) -> bool { 318e8d8bef9SDimitry Andric return Filter(FilterCtx, wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name))); 3195ffd83dbSDimitry Andric }; 3205ffd83dbSDimitry Andric 3215ffd83dbSDimitry Andric auto ProcessSymsGenerator = 3225ffd83dbSDimitry Andric DynamicLibrarySearchGenerator::GetForCurrentProcess(GlobalPrefix, Pred); 3235ffd83dbSDimitry Andric 3245ffd83dbSDimitry Andric if (!ProcessSymsGenerator) { 3255ffd83dbSDimitry Andric *Result = 0; 3265ffd83dbSDimitry Andric return wrap(ProcessSymsGenerator.takeError()); 3275ffd83dbSDimitry Andric } 3285ffd83dbSDimitry Andric 3295ffd83dbSDimitry Andric *Result = wrap(ProcessSymsGenerator->release()); 3305ffd83dbSDimitry Andric return LLVMErrorSuccess; 3315ffd83dbSDimitry Andric } 3325ffd83dbSDimitry Andric 3335ffd83dbSDimitry Andric LLVMOrcThreadSafeContextRef LLVMOrcCreateNewThreadSafeContext(void) { 3345ffd83dbSDimitry Andric return wrap(new ThreadSafeContext(std::make_unique<LLVMContext>())); 3355ffd83dbSDimitry Andric } 3365ffd83dbSDimitry Andric 3375ffd83dbSDimitry Andric LLVMContextRef 3385ffd83dbSDimitry Andric LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx) { 3395ffd83dbSDimitry Andric return wrap(unwrap(TSCtx)->getContext()); 3405ffd83dbSDimitry Andric } 3415ffd83dbSDimitry Andric 3425ffd83dbSDimitry Andric void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx) { 3435ffd83dbSDimitry Andric delete unwrap(TSCtx); 3445ffd83dbSDimitry Andric } 3455ffd83dbSDimitry Andric 3465ffd83dbSDimitry Andric LLVMOrcThreadSafeModuleRef 3475ffd83dbSDimitry Andric LLVMOrcCreateNewThreadSafeModule(LLVMModuleRef M, 3485ffd83dbSDimitry Andric LLVMOrcThreadSafeContextRef TSCtx) { 3495ffd83dbSDimitry Andric return wrap( 3505ffd83dbSDimitry Andric new ThreadSafeModule(std::unique_ptr<Module>(unwrap(M)), *unwrap(TSCtx))); 3515ffd83dbSDimitry Andric } 3525ffd83dbSDimitry Andric 3535ffd83dbSDimitry Andric void LLVMOrcDisposeThreadSafeModule(LLVMOrcThreadSafeModuleRef TSM) { 3545ffd83dbSDimitry Andric delete unwrap(TSM); 3555ffd83dbSDimitry Andric } 3565ffd83dbSDimitry Andric 3575ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcJITTargetMachineBuilderDetectHost( 3585ffd83dbSDimitry Andric LLVMOrcJITTargetMachineBuilderRef *Result) { 3595ffd83dbSDimitry Andric assert(Result && "Result can not be null"); 3605ffd83dbSDimitry Andric 3615ffd83dbSDimitry Andric auto JTMB = JITTargetMachineBuilder::detectHost(); 3625ffd83dbSDimitry Andric if (!JTMB) { 3635ffd83dbSDimitry Andric Result = 0; 3645ffd83dbSDimitry Andric return wrap(JTMB.takeError()); 3655ffd83dbSDimitry Andric } 3665ffd83dbSDimitry Andric 3675ffd83dbSDimitry Andric *Result = wrap(new JITTargetMachineBuilder(std::move(*JTMB))); 3685ffd83dbSDimitry Andric return LLVMErrorSuccess; 3695ffd83dbSDimitry Andric } 3705ffd83dbSDimitry Andric 3715ffd83dbSDimitry Andric LLVMOrcJITTargetMachineBuilderRef 372e8d8bef9SDimitry Andric LLVMOrcJITTargetMachineBuilderCreateFromTargetMachine(LLVMTargetMachineRef TM) { 3735ffd83dbSDimitry Andric auto *TemplateTM = unwrap(TM); 3745ffd83dbSDimitry Andric 3755ffd83dbSDimitry Andric auto JTMB = 3765ffd83dbSDimitry Andric std::make_unique<JITTargetMachineBuilder>(TemplateTM->getTargetTriple()); 3775ffd83dbSDimitry Andric 3785ffd83dbSDimitry Andric (*JTMB) 3795ffd83dbSDimitry Andric .setCPU(TemplateTM->getTargetCPU().str()) 3805ffd83dbSDimitry Andric .setRelocationModel(TemplateTM->getRelocationModel()) 3815ffd83dbSDimitry Andric .setCodeModel(TemplateTM->getCodeModel()) 3825ffd83dbSDimitry Andric .setCodeGenOptLevel(TemplateTM->getOptLevel()) 3835ffd83dbSDimitry Andric .setFeatures(TemplateTM->getTargetFeatureString()) 3845ffd83dbSDimitry Andric .setOptions(TemplateTM->Options); 3855ffd83dbSDimitry Andric 3865ffd83dbSDimitry Andric LLVMDisposeTargetMachine(TM); 3875ffd83dbSDimitry Andric 3885ffd83dbSDimitry Andric return wrap(JTMB.release()); 3895ffd83dbSDimitry Andric } 3905ffd83dbSDimitry Andric 3915ffd83dbSDimitry Andric void LLVMOrcDisposeJITTargetMachineBuilder( 3925ffd83dbSDimitry Andric LLVMOrcJITTargetMachineBuilderRef JTMB) { 3935ffd83dbSDimitry Andric delete unwrap(JTMB); 3945ffd83dbSDimitry Andric } 3955ffd83dbSDimitry Andric 396*d409305fSDimitry Andric void LLVMOrcDisposeObjectLayer(LLVMOrcObjectLayerRef ObjLayer) { 397e8d8bef9SDimitry Andric delete unwrap(ObjLayer); 398e8d8bef9SDimitry Andric } 399e8d8bef9SDimitry Andric 4005ffd83dbSDimitry Andric LLVMOrcLLJITBuilderRef LLVMOrcCreateLLJITBuilder(void) { 4015ffd83dbSDimitry Andric return wrap(new LLJITBuilder()); 4025ffd83dbSDimitry Andric } 4035ffd83dbSDimitry Andric 4045ffd83dbSDimitry Andric void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder) { 4055ffd83dbSDimitry Andric delete unwrap(Builder); 4065ffd83dbSDimitry Andric } 4075ffd83dbSDimitry Andric 4085ffd83dbSDimitry Andric void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder( 4095ffd83dbSDimitry Andric LLVMOrcLLJITBuilderRef Builder, LLVMOrcJITTargetMachineBuilderRef JTMB) { 4105ffd83dbSDimitry Andric unwrap(Builder)->setJITTargetMachineBuilder(*unwrap(JTMB)); 4115ffd83dbSDimitry Andric } 4125ffd83dbSDimitry Andric 413e8d8bef9SDimitry Andric void LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator( 414e8d8bef9SDimitry Andric LLVMOrcLLJITBuilderRef Builder, 415e8d8bef9SDimitry Andric LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction F, void *Ctx) { 416e8d8bef9SDimitry Andric unwrap(Builder)->setObjectLinkingLayerCreator( 417e8d8bef9SDimitry Andric [=](ExecutionSession &ES, const Triple &TT) { 418e8d8bef9SDimitry Andric auto TTStr = TT.str(); 419e8d8bef9SDimitry Andric return std::unique_ptr<ObjectLayer>( 420e8d8bef9SDimitry Andric unwrap(F(Ctx, wrap(&ES), TTStr.c_str()))); 421e8d8bef9SDimitry Andric }); 422e8d8bef9SDimitry Andric } 423e8d8bef9SDimitry Andric 4245ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcCreateLLJIT(LLVMOrcLLJITRef *Result, 4255ffd83dbSDimitry Andric LLVMOrcLLJITBuilderRef Builder) { 4265ffd83dbSDimitry Andric assert(Result && "Result can not be null"); 4275ffd83dbSDimitry Andric 4285ffd83dbSDimitry Andric if (!Builder) 4295ffd83dbSDimitry Andric Builder = LLVMOrcCreateLLJITBuilder(); 4305ffd83dbSDimitry Andric 4315ffd83dbSDimitry Andric auto J = unwrap(Builder)->create(); 4325ffd83dbSDimitry Andric LLVMOrcDisposeLLJITBuilder(Builder); 4335ffd83dbSDimitry Andric 4345ffd83dbSDimitry Andric if (!J) { 4355ffd83dbSDimitry Andric Result = 0; 4365ffd83dbSDimitry Andric return wrap(J.takeError()); 4375ffd83dbSDimitry Andric } 4385ffd83dbSDimitry Andric 4395ffd83dbSDimitry Andric *Result = wrap(J->release()); 4405ffd83dbSDimitry Andric return LLVMErrorSuccess; 4415ffd83dbSDimitry Andric } 4425ffd83dbSDimitry Andric 4435ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J) { 4445ffd83dbSDimitry Andric delete unwrap(J); 4455ffd83dbSDimitry Andric return LLVMErrorSuccess; 4465ffd83dbSDimitry Andric } 4475ffd83dbSDimitry Andric 4485ffd83dbSDimitry Andric LLVMOrcExecutionSessionRef LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J) { 4495ffd83dbSDimitry Andric return wrap(&unwrap(J)->getExecutionSession()); 4505ffd83dbSDimitry Andric } 4515ffd83dbSDimitry Andric 4525ffd83dbSDimitry Andric LLVMOrcJITDylibRef LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J) { 4535ffd83dbSDimitry Andric return wrap(&unwrap(J)->getMainJITDylib()); 4545ffd83dbSDimitry Andric } 4555ffd83dbSDimitry Andric 4565ffd83dbSDimitry Andric const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J) { 4575ffd83dbSDimitry Andric return unwrap(J)->getTargetTriple().str().c_str(); 4585ffd83dbSDimitry Andric } 4595ffd83dbSDimitry Andric 4605ffd83dbSDimitry Andric char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J) { 4615ffd83dbSDimitry Andric return unwrap(J)->getDataLayout().getGlobalPrefix(); 4625ffd83dbSDimitry Andric } 4635ffd83dbSDimitry Andric 4645ffd83dbSDimitry Andric LLVMOrcSymbolStringPoolEntryRef 4655ffd83dbSDimitry Andric LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName) { 4665ffd83dbSDimitry Andric return wrap(OrcV2CAPIHelper::releaseSymbolStringPtr( 4675ffd83dbSDimitry Andric unwrap(J)->mangleAndIntern(UnmangledName))); 4685ffd83dbSDimitry Andric } 4695ffd83dbSDimitry Andric 4705ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD, 4715ffd83dbSDimitry Andric LLVMMemoryBufferRef ObjBuffer) { 4725ffd83dbSDimitry Andric return wrap(unwrap(J)->addObjectFile( 4735ffd83dbSDimitry Andric *unwrap(JD), std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer)))); 4745ffd83dbSDimitry Andric } 4755ffd83dbSDimitry Andric 476e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcLLJITRef J, 477e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef RT, 478e8d8bef9SDimitry Andric LLVMMemoryBufferRef ObjBuffer) { 479e8d8bef9SDimitry Andric return wrap(unwrap(J)->addObjectFile( 480e8d8bef9SDimitry Andric ResourceTrackerSP(unwrap(RT)), 481e8d8bef9SDimitry Andric std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer)))); 482e8d8bef9SDimitry Andric } 483e8d8bef9SDimitry Andric 4845ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J, 4855ffd83dbSDimitry Andric LLVMOrcJITDylibRef JD, 4865ffd83dbSDimitry Andric LLVMOrcThreadSafeModuleRef TSM) { 487e8d8bef9SDimitry Andric std::unique_ptr<ThreadSafeModule> TmpTSM(unwrap(TSM)); 488e8d8bef9SDimitry Andric return wrap(unwrap(J)->addIRModule(*unwrap(JD), std::move(*TmpTSM))); 489e8d8bef9SDimitry Andric } 490e8d8bef9SDimitry Andric 491e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddLLVMIRModuleWithRT(LLVMOrcLLJITRef J, 492e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef RT, 493e8d8bef9SDimitry Andric LLVMOrcThreadSafeModuleRef TSM) { 494e8d8bef9SDimitry Andric std::unique_ptr<ThreadSafeModule> TmpTSM(unwrap(TSM)); 495e8d8bef9SDimitry Andric return wrap(unwrap(J)->addIRModule(ResourceTrackerSP(unwrap(RT)), 496e8d8bef9SDimitry Andric std::move(*TmpTSM))); 4975ffd83dbSDimitry Andric } 4985ffd83dbSDimitry Andric 4995ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J, 5005ffd83dbSDimitry Andric LLVMOrcJITTargetAddress *Result, 5015ffd83dbSDimitry Andric const char *Name) { 5025ffd83dbSDimitry Andric assert(Result && "Result can not be null"); 5035ffd83dbSDimitry Andric 5045ffd83dbSDimitry Andric auto Sym = unwrap(J)->lookup(Name); 5055ffd83dbSDimitry Andric if (!Sym) { 5065ffd83dbSDimitry Andric *Result = 0; 5075ffd83dbSDimitry Andric return wrap(Sym.takeError()); 5085ffd83dbSDimitry Andric } 5095ffd83dbSDimitry Andric 5105ffd83dbSDimitry Andric *Result = Sym->getAddress(); 5115ffd83dbSDimitry Andric return LLVMErrorSuccess; 5125ffd83dbSDimitry Andric } 513e8d8bef9SDimitry Andric 514e8d8bef9SDimitry Andric LLVMOrcObjectLayerRef 515e8d8bef9SDimitry Andric LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager( 516e8d8bef9SDimitry Andric LLVMOrcExecutionSessionRef ES) { 517e8d8bef9SDimitry Andric assert(ES && "ES must not be null"); 518e8d8bef9SDimitry Andric return wrap(new RTDyldObjectLinkingLayer( 519e8d8bef9SDimitry Andric *unwrap(ES), [] { return std::make_unique<SectionMemoryManager>(); })); 520e8d8bef9SDimitry Andric } 521e8d8bef9SDimitry Andric 522e8d8bef9SDimitry Andric void LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener( 523e8d8bef9SDimitry Andric LLVMOrcObjectLayerRef RTDyldObjLinkingLayer, 524e8d8bef9SDimitry Andric LLVMJITEventListenerRef Listener) { 525e8d8bef9SDimitry Andric assert(RTDyldObjLinkingLayer && "RTDyldObjLinkingLayer must not be null"); 526e8d8bef9SDimitry Andric assert(Listener && "Listener must not be null"); 527e8d8bef9SDimitry Andric reinterpret_cast<RTDyldObjectLinkingLayer *>(unwrap(RTDyldObjLinkingLayer)) 528e8d8bef9SDimitry Andric ->registerJITEventListener(*unwrap(Listener)); 529e8d8bef9SDimitry Andric } 530