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 9*e8d8bef9SDimitry Andric #include "llvm-c/LLJIT.h" 105ffd83dbSDimitry Andric #include "llvm-c/Orc.h" 11*e8d8bef9SDimitry 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" 16*e8d8bef9SDimitry Andric #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" 17*e8d8bef9SDimitry 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 25*e8d8bef9SDimitry Andric class InProgressLookupState; 26*e8d8bef9SDimitry 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 38*e8d8bef9SDimitry Andric static SymbolStringPtr retainSymbolStringPtr(PoolEntryPtr P) { 39*e8d8bef9SDimitry Andric return SymbolStringPtr(P); 40*e8d8bef9SDimitry Andric } 41*e8d8bef9SDimitry Andric 425ffd83dbSDimitry Andric static PoolEntryPtr getRawPoolEntryPtr(const SymbolStringPtr &S) { 435ffd83dbSDimitry Andric return S.S; 445ffd83dbSDimitry Andric } 455ffd83dbSDimitry Andric 46*e8d8bef9SDimitry Andric static void retainPoolEntry(PoolEntryPtr P) { 47*e8d8bef9SDimitry Andric SymbolStringPtr S(P); 48*e8d8bef9SDimitry Andric S.S = nullptr; 49*e8d8bef9SDimitry Andric } 50*e8d8bef9SDimitry Andric 515ffd83dbSDimitry Andric static void releasePoolEntry(PoolEntryPtr P) { 525ffd83dbSDimitry Andric SymbolStringPtr S; 535ffd83dbSDimitry Andric S.S = P; 545ffd83dbSDimitry Andric } 55*e8d8bef9SDimitry Andric 56*e8d8bef9SDimitry Andric static InProgressLookupState *extractLookupState(LookupState &LS) { 57*e8d8bef9SDimitry Andric return LS.IPLS.release(); 58*e8d8bef9SDimitry Andric } 59*e8d8bef9SDimitry Andric 60*e8d8bef9SDimitry Andric static void resetLookupState(LookupState &LS, InProgressLookupState *IPLS) { 61*e8d8bef9SDimitry Andric return LS.reset(IPLS); 62*e8d8bef9SDimitry Andric } 635ffd83dbSDimitry Andric }; 645ffd83dbSDimitry Andric 65*e8d8bef9SDimitry Andric } // namespace orc 66*e8d8bef9SDimitry Andric } // namespace llvm 675ffd83dbSDimitry Andric 685ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionSession, LLVMOrcExecutionSessionRef) 69*e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SymbolStringPool, LLVMOrcSymbolStringPoolRef) 705ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcV2CAPIHelper::PoolEntry, 715ffd83dbSDimitry Andric LLVMOrcSymbolStringPoolEntryRef) 72*e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationUnit, 73*e8d8bef9SDimitry Andric LLVMOrcMaterializationUnitRef) 745ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITDylib, LLVMOrcJITDylibRef) 75*e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ResourceTracker, LLVMOrcResourceTrackerRef) 76*e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DefinitionGenerator, 77*e8d8bef9SDimitry Andric LLVMOrcDefinitionGeneratorRef) 78*e8d8bef9SDimitry 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) 84*e8d8bef9SDimitry 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 90*e8d8bef9SDimitry Andric namespace llvm { 91*e8d8bef9SDimitry Andric namespace orc { 92*e8d8bef9SDimitry Andric 93*e8d8bef9SDimitry Andric class CAPIDefinitionGenerator final : public DefinitionGenerator { 94*e8d8bef9SDimitry Andric public: 95*e8d8bef9SDimitry Andric CAPIDefinitionGenerator( 96*e8d8bef9SDimitry Andric void *Ctx, 97*e8d8bef9SDimitry Andric LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate) 98*e8d8bef9SDimitry Andric : Ctx(Ctx), TryToGenerate(TryToGenerate) {} 99*e8d8bef9SDimitry Andric 100*e8d8bef9SDimitry Andric Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, 101*e8d8bef9SDimitry Andric JITDylibLookupFlags JDLookupFlags, 102*e8d8bef9SDimitry Andric const SymbolLookupSet &LookupSet) override { 103*e8d8bef9SDimitry Andric 104*e8d8bef9SDimitry Andric // Take the lookup state. 105*e8d8bef9SDimitry Andric LLVMOrcLookupStateRef LSR = ::wrap(OrcV2CAPIHelper::extractLookupState(LS)); 106*e8d8bef9SDimitry Andric 107*e8d8bef9SDimitry Andric // Translate the lookup kind. 108*e8d8bef9SDimitry Andric LLVMOrcLookupKind CLookupKind; 109*e8d8bef9SDimitry Andric switch (K) { 110*e8d8bef9SDimitry Andric case LookupKind::Static: 111*e8d8bef9SDimitry Andric CLookupKind = LLVMOrcLookupKindStatic; 112*e8d8bef9SDimitry Andric break; 113*e8d8bef9SDimitry Andric case LookupKind::DLSym: 114*e8d8bef9SDimitry Andric CLookupKind = LLVMOrcLookupKindDLSym; 115*e8d8bef9SDimitry Andric break; 116*e8d8bef9SDimitry Andric } 117*e8d8bef9SDimitry Andric 118*e8d8bef9SDimitry Andric // Translate the JITDylibSearchFlags. 119*e8d8bef9SDimitry Andric LLVMOrcJITDylibLookupFlags CJDLookupFlags; 120*e8d8bef9SDimitry Andric switch (JDLookupFlags) { 121*e8d8bef9SDimitry Andric case JITDylibLookupFlags::MatchExportedSymbolsOnly: 122*e8d8bef9SDimitry Andric CJDLookupFlags = LLVMOrcJITDylibLookupFlagsMatchExportedSymbolsOnly; 123*e8d8bef9SDimitry Andric break; 124*e8d8bef9SDimitry Andric case JITDylibLookupFlags::MatchAllSymbols: 125*e8d8bef9SDimitry Andric CJDLookupFlags = LLVMOrcJITDylibLookupFlagsMatchAllSymbols; 126*e8d8bef9SDimitry Andric break; 127*e8d8bef9SDimitry Andric } 128*e8d8bef9SDimitry Andric 129*e8d8bef9SDimitry Andric // Translate the lookup set. 130*e8d8bef9SDimitry Andric std::vector<LLVMOrcCLookupSetElement> CLookupSet; 131*e8d8bef9SDimitry Andric CLookupSet.reserve(LookupSet.size()); 132*e8d8bef9SDimitry Andric for (auto &KV : LookupSet) { 133*e8d8bef9SDimitry Andric LLVMOrcSymbolLookupFlags SLF; 134*e8d8bef9SDimitry Andric LLVMOrcSymbolStringPoolEntryRef Name = 135*e8d8bef9SDimitry Andric ::wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(KV.first)); 136*e8d8bef9SDimitry Andric switch (KV.second) { 137*e8d8bef9SDimitry Andric case SymbolLookupFlags::RequiredSymbol: 138*e8d8bef9SDimitry Andric SLF = LLVMOrcSymbolLookupFlagsRequiredSymbol; 139*e8d8bef9SDimitry Andric break; 140*e8d8bef9SDimitry Andric case SymbolLookupFlags::WeaklyReferencedSymbol: 141*e8d8bef9SDimitry Andric SLF = LLVMOrcSymbolLookupFlagsWeaklyReferencedSymbol; 142*e8d8bef9SDimitry Andric break; 143*e8d8bef9SDimitry Andric } 144*e8d8bef9SDimitry Andric CLookupSet.push_back({Name, SLF}); 145*e8d8bef9SDimitry Andric } 146*e8d8bef9SDimitry Andric 147*e8d8bef9SDimitry Andric // Run the C TryToGenerate function. 148*e8d8bef9SDimitry Andric auto Err = unwrap(TryToGenerate(::wrap(this), Ctx, &LSR, CLookupKind, 149*e8d8bef9SDimitry Andric ::wrap(&JD), CJDLookupFlags, 150*e8d8bef9SDimitry Andric CLookupSet.data(), CLookupSet.size())); 151*e8d8bef9SDimitry Andric 152*e8d8bef9SDimitry Andric // Restore the lookup state. 153*e8d8bef9SDimitry Andric OrcV2CAPIHelper::resetLookupState(LS, ::unwrap(LSR)); 154*e8d8bef9SDimitry Andric 155*e8d8bef9SDimitry Andric return Err; 156*e8d8bef9SDimitry Andric } 157*e8d8bef9SDimitry Andric 158*e8d8bef9SDimitry Andric private: 159*e8d8bef9SDimitry Andric void *Ctx; 160*e8d8bef9SDimitry Andric LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate; 161*e8d8bef9SDimitry Andric }; 162*e8d8bef9SDimitry Andric 163*e8d8bef9SDimitry Andric } // end namespace orc 164*e8d8bef9SDimitry Andric } // end namespace llvm 165*e8d8bef9SDimitry Andric 166*e8d8bef9SDimitry Andric void LLVMOrcExecutionSessionSetErrorReporter( 167*e8d8bef9SDimitry Andric LLVMOrcExecutionSessionRef ES, LLVMOrcErrorReporterFunction ReportError, 168*e8d8bef9SDimitry Andric void *Ctx) { 169*e8d8bef9SDimitry Andric unwrap(ES)->setErrorReporter( 170*e8d8bef9SDimitry Andric [=](Error Err) { ReportError(Ctx, wrap(std::move(Err))); }); 171*e8d8bef9SDimitry Andric } 172*e8d8bef9SDimitry Andric 173*e8d8bef9SDimitry Andric LLVMOrcSymbolStringPoolRef 174*e8d8bef9SDimitry Andric LLVMOrcExecutionSessionGetSymbolStringPool(LLVMOrcExecutionSessionRef ES) { 175*e8d8bef9SDimitry Andric return wrap(unwrap(ES)->getSymbolStringPool().get()); 176*e8d8bef9SDimitry Andric } 177*e8d8bef9SDimitry Andric 178*e8d8bef9SDimitry Andric void LLVMOrcSymbolStringPoolClearDeadEntries(LLVMOrcSymbolStringPoolRef SSP) { 179*e8d8bef9SDimitry Andric unwrap(SSP)->clearDeadEntries(); 180*e8d8bef9SDimitry Andric } 181*e8d8bef9SDimitry 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 188*e8d8bef9SDimitry Andric void LLVMOrcRetainSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S) { 189*e8d8bef9SDimitry Andric OrcV2CAPIHelper::retainPoolEntry(unwrap(S)); 190*e8d8bef9SDimitry Andric } 191*e8d8bef9SDimitry Andric 1925ffd83dbSDimitry Andric void LLVMOrcReleaseSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S) { 1935ffd83dbSDimitry Andric OrcV2CAPIHelper::releasePoolEntry(unwrap(S)); 1945ffd83dbSDimitry Andric } 1955ffd83dbSDimitry Andric 196*e8d8bef9SDimitry Andric const char *LLVMOrcSymbolStringPoolEntryStr(LLVMOrcSymbolStringPoolEntryRef S) { 197*e8d8bef9SDimitry Andric return unwrap(S)->getKey().data(); 198*e8d8bef9SDimitry Andric } 199*e8d8bef9SDimitry Andric 200*e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef 201*e8d8bef9SDimitry Andric LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD) { 202*e8d8bef9SDimitry Andric auto RT = unwrap(JD)->createResourceTracker(); 203*e8d8bef9SDimitry Andric // Retain the pointer for the C API client. 204*e8d8bef9SDimitry Andric RT->Retain(); 205*e8d8bef9SDimitry Andric return wrap(RT.get()); 206*e8d8bef9SDimitry Andric } 207*e8d8bef9SDimitry Andric 208*e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef 209*e8d8bef9SDimitry Andric LLVMOrcJITDylibGetDefaultResourceTracker(LLVMOrcJITDylibRef JD) { 210*e8d8bef9SDimitry Andric auto RT = unwrap(JD)->getDefaultResourceTracker(); 211*e8d8bef9SDimitry Andric // Retain the pointer for the C API client. 212*e8d8bef9SDimitry Andric return wrap(RT.get()); 213*e8d8bef9SDimitry Andric } 214*e8d8bef9SDimitry Andric 215*e8d8bef9SDimitry Andric void LLVMOrcReleaseResourceTracker(LLVMOrcResourceTrackerRef RT) { 216*e8d8bef9SDimitry Andric ResourceTrackerSP TmpRT(unwrap(RT)); 217*e8d8bef9SDimitry Andric TmpRT->Release(); 218*e8d8bef9SDimitry Andric } 219*e8d8bef9SDimitry Andric 220*e8d8bef9SDimitry Andric void LLVMOrcResourceTrackerTransferTo(LLVMOrcResourceTrackerRef SrcRT, 221*e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef DstRT) { 222*e8d8bef9SDimitry Andric ResourceTrackerSP TmpRT(unwrap(SrcRT)); 223*e8d8bef9SDimitry Andric TmpRT->transferTo(*unwrap(DstRT)); 224*e8d8bef9SDimitry Andric } 225*e8d8bef9SDimitry Andric 226*e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT) { 227*e8d8bef9SDimitry Andric ResourceTrackerSP TmpRT(unwrap(RT)); 228*e8d8bef9SDimitry Andric return wrap(TmpRT->remove()); 229*e8d8bef9SDimitry Andric } 230*e8d8bef9SDimitry Andric 231*e8d8bef9SDimitry Andric void LLVMOrcDisposeDefinitionGenerator(LLVMOrcDefinitionGeneratorRef DG) { 232*e8d8bef9SDimitry Andric std::unique_ptr<DefinitionGenerator> TmpDG(unwrap(DG)); 233*e8d8bef9SDimitry Andric } 234*e8d8bef9SDimitry Andric 235*e8d8bef9SDimitry Andric void LLVMOrcDisposeMaterializationUnit(LLVMOrcMaterializationUnitRef MU) { 236*e8d8bef9SDimitry Andric std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU)); 237*e8d8bef9SDimitry Andric } 238*e8d8bef9SDimitry Andric 239*e8d8bef9SDimitry Andric LLVMOrcMaterializationUnitRef 240*e8d8bef9SDimitry Andric LLVMOrcAbsoluteSymbols(LLVMOrcCSymbolMapPairs Syms, size_t NumPairs) { 241*e8d8bef9SDimitry Andric SymbolMap SM; 242*e8d8bef9SDimitry Andric for (size_t I = 0; I != NumPairs; ++I) { 243*e8d8bef9SDimitry Andric JITSymbolFlags Flags; 244*e8d8bef9SDimitry Andric 245*e8d8bef9SDimitry Andric if (Syms[I].Sym.Flags.GenericFlags & LLVMJITSymbolGenericFlagsExported) 246*e8d8bef9SDimitry Andric Flags |= JITSymbolFlags::Exported; 247*e8d8bef9SDimitry Andric if (Syms[I].Sym.Flags.GenericFlags & LLVMJITSymbolGenericFlagsWeak) 248*e8d8bef9SDimitry Andric Flags |= JITSymbolFlags::Weak; 249*e8d8bef9SDimitry Andric 250*e8d8bef9SDimitry Andric Flags.getTargetFlags() = Syms[I].Sym.Flags.TargetFlags; 251*e8d8bef9SDimitry Andric 252*e8d8bef9SDimitry Andric SM[OrcV2CAPIHelper::retainSymbolStringPtr(unwrap(Syms[I].Name))] = 253*e8d8bef9SDimitry Andric JITEvaluatedSymbol(Syms[I].Sym.Address, Flags); 254*e8d8bef9SDimitry Andric } 255*e8d8bef9SDimitry Andric 256*e8d8bef9SDimitry Andric return wrap(absoluteSymbols(std::move(SM)).release()); 257*e8d8bef9SDimitry Andric } 258*e8d8bef9SDimitry Andric 259*e8d8bef9SDimitry Andric LLVMOrcJITDylibRef 260*e8d8bef9SDimitry Andric LLVMOrcExecutionSessionCreateBareJITDylib(LLVMOrcExecutionSessionRef ES, 261*e8d8bef9SDimitry Andric const char *Name) { 262*e8d8bef9SDimitry Andric return wrap(&unwrap(ES)->createBareJITDylib(Name)); 263*e8d8bef9SDimitry Andric } 264*e8d8bef9SDimitry Andric 265*e8d8bef9SDimitry Andric LLVMErrorRef 266*e8d8bef9SDimitry Andric LLVMOrcExecutionSessionCreateJITDylib(LLVMOrcExecutionSessionRef ES, 267*e8d8bef9SDimitry Andric LLVMOrcJITDylibRef *Result, 268*e8d8bef9SDimitry Andric const char *Name) { 269*e8d8bef9SDimitry Andric auto JD = unwrap(ES)->createJITDylib(Name); 270*e8d8bef9SDimitry Andric if (!JD) 271*e8d8bef9SDimitry Andric return wrap(JD.takeError()); 272*e8d8bef9SDimitry Andric *Result = wrap(&*JD); 273*e8d8bef9SDimitry Andric return LLVMErrorSuccess; 274*e8d8bef9SDimitry Andric } 275*e8d8bef9SDimitry Andric 276*e8d8bef9SDimitry Andric LLVMOrcJITDylibRef 277*e8d8bef9SDimitry Andric LLVMOrcExecutionSessionGetJITDylibByName(LLVMOrcExecutionSessionRef ES, 278*e8d8bef9SDimitry Andric const char *Name) { 279*e8d8bef9SDimitry Andric return wrap(unwrap(ES)->getJITDylibByName(Name)); 280*e8d8bef9SDimitry Andric } 281*e8d8bef9SDimitry Andric 282*e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcJITDylibDefine(LLVMOrcJITDylibRef JD, 283*e8d8bef9SDimitry Andric LLVMOrcMaterializationUnitRef MU) { 284*e8d8bef9SDimitry Andric std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU)); 285*e8d8bef9SDimitry Andric 286*e8d8bef9SDimitry Andric if (auto Err = unwrap(JD)->define(TmpMU)) { 287*e8d8bef9SDimitry Andric TmpMU.release(); 288*e8d8bef9SDimitry Andric return wrap(std::move(Err)); 289*e8d8bef9SDimitry Andric } 290*e8d8bef9SDimitry Andric return LLVMErrorSuccess; 291*e8d8bef9SDimitry Andric } 292*e8d8bef9SDimitry Andric 293*e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcJITDylibClear(LLVMOrcJITDylibRef JD) { 294*e8d8bef9SDimitry Andric return wrap(unwrap(JD)->clear()); 2955ffd83dbSDimitry Andric } 2965ffd83dbSDimitry Andric 2975ffd83dbSDimitry Andric void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD, 298*e8d8bef9SDimitry Andric LLVMOrcDefinitionGeneratorRef DG) { 299*e8d8bef9SDimitry Andric unwrap(JD)->addGenerator(std::unique_ptr<DefinitionGenerator>(unwrap(DG))); 300*e8d8bef9SDimitry Andric } 301*e8d8bef9SDimitry Andric 302*e8d8bef9SDimitry Andric LLVMOrcDefinitionGeneratorRef LLVMOrcCreateCustomCAPIDefinitionGenerator( 303*e8d8bef9SDimitry Andric LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F, void *Ctx) { 304*e8d8bef9SDimitry Andric auto DG = std::make_unique<CAPIDefinitionGenerator>(Ctx, F); 305*e8d8bef9SDimitry Andric return wrap(DG.release()); 3065ffd83dbSDimitry Andric } 3075ffd83dbSDimitry Andric 3085ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess( 309*e8d8bef9SDimitry 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 { 318*e8d8bef9SDimitry 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 372*e8d8bef9SDimitry 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*e8d8bef9SDimitry Andric void lLVMOrcDisposeObjectLayer(LLVMOrcObjectLayerRef ObjLayer) { 397*e8d8bef9SDimitry Andric delete unwrap(ObjLayer); 398*e8d8bef9SDimitry Andric } 399*e8d8bef9SDimitry 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 413*e8d8bef9SDimitry Andric void LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator( 414*e8d8bef9SDimitry Andric LLVMOrcLLJITBuilderRef Builder, 415*e8d8bef9SDimitry Andric LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction F, void *Ctx) { 416*e8d8bef9SDimitry Andric unwrap(Builder)->setObjectLinkingLayerCreator( 417*e8d8bef9SDimitry Andric [=](ExecutionSession &ES, const Triple &TT) { 418*e8d8bef9SDimitry Andric auto TTStr = TT.str(); 419*e8d8bef9SDimitry Andric return std::unique_ptr<ObjectLayer>( 420*e8d8bef9SDimitry Andric unwrap(F(Ctx, wrap(&ES), TTStr.c_str()))); 421*e8d8bef9SDimitry Andric }); 422*e8d8bef9SDimitry Andric } 423*e8d8bef9SDimitry 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 476*e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcLLJITRef J, 477*e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef RT, 478*e8d8bef9SDimitry Andric LLVMMemoryBufferRef ObjBuffer) { 479*e8d8bef9SDimitry Andric return wrap(unwrap(J)->addObjectFile( 480*e8d8bef9SDimitry Andric ResourceTrackerSP(unwrap(RT)), 481*e8d8bef9SDimitry Andric std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer)))); 482*e8d8bef9SDimitry Andric } 483*e8d8bef9SDimitry Andric 4845ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J, 4855ffd83dbSDimitry Andric LLVMOrcJITDylibRef JD, 4865ffd83dbSDimitry Andric LLVMOrcThreadSafeModuleRef TSM) { 487*e8d8bef9SDimitry Andric std::unique_ptr<ThreadSafeModule> TmpTSM(unwrap(TSM)); 488*e8d8bef9SDimitry Andric return wrap(unwrap(J)->addIRModule(*unwrap(JD), std::move(*TmpTSM))); 489*e8d8bef9SDimitry Andric } 490*e8d8bef9SDimitry Andric 491*e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddLLVMIRModuleWithRT(LLVMOrcLLJITRef J, 492*e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef RT, 493*e8d8bef9SDimitry Andric LLVMOrcThreadSafeModuleRef TSM) { 494*e8d8bef9SDimitry Andric std::unique_ptr<ThreadSafeModule> TmpTSM(unwrap(TSM)); 495*e8d8bef9SDimitry Andric return wrap(unwrap(J)->addIRModule(ResourceTrackerSP(unwrap(RT)), 496*e8d8bef9SDimitry 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 } 513*e8d8bef9SDimitry Andric 514*e8d8bef9SDimitry Andric LLVMOrcObjectLayerRef 515*e8d8bef9SDimitry Andric LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager( 516*e8d8bef9SDimitry Andric LLVMOrcExecutionSessionRef ES) { 517*e8d8bef9SDimitry Andric assert(ES && "ES must not be null"); 518*e8d8bef9SDimitry Andric return wrap(new RTDyldObjectLinkingLayer( 519*e8d8bef9SDimitry Andric *unwrap(ES), [] { return std::make_unique<SectionMemoryManager>(); })); 520*e8d8bef9SDimitry Andric } 521*e8d8bef9SDimitry Andric 522*e8d8bef9SDimitry Andric void LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener( 523*e8d8bef9SDimitry Andric LLVMOrcObjectLayerRef RTDyldObjLinkingLayer, 524*e8d8bef9SDimitry Andric LLVMJITEventListenerRef Listener) { 525*e8d8bef9SDimitry Andric assert(RTDyldObjLinkingLayer && "RTDyldObjLinkingLayer must not be null"); 526*e8d8bef9SDimitry Andric assert(Listener && "Listener must not be null"); 527*e8d8bef9SDimitry Andric reinterpret_cast<RTDyldObjectLinkingLayer *>(unwrap(RTDyldObjLinkingLayer)) 528*e8d8bef9SDimitry Andric ->registerJITEventListener(*unwrap(Listener)); 529*e8d8bef9SDimitry Andric } 530