OrcV2CBindings.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. //===--------------- OrcV2CBindings.cpp - C bindings OrcV2 APIs -----------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm-c/LLJIT.h"
  9. #include "llvm-c/Orc.h"
  10. #include "llvm-c/OrcEE.h"
  11. #include "llvm-c/TargetMachine.h"
  12. #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
  13. #include "llvm/ExecutionEngine/Orc/LLJIT.h"
  14. #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
  15. #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
  16. #include "llvm/ExecutionEngine/SectionMemoryManager.h"
  17. using namespace llvm;
  18. using namespace llvm::orc;
  19. namespace llvm {
  20. namespace orc {
  21. class InProgressLookupState;
  22. class OrcV2CAPIHelper {
  23. public:
  24. using PoolEntry = SymbolStringPtr::PoolEntry;
  25. using PoolEntryPtr = SymbolStringPtr::PoolEntryPtr;
  26. // Move from SymbolStringPtr to PoolEntryPtr (no change in ref count).
  27. static PoolEntryPtr moveFromSymbolStringPtr(SymbolStringPtr S) {
  28. PoolEntryPtr Result = nullptr;
  29. std::swap(Result, S.S);
  30. return Result;
  31. }
  32. // Move from a PoolEntryPtr to a SymbolStringPtr (no change in ref count).
  33. static SymbolStringPtr moveToSymbolStringPtr(PoolEntryPtr P) {
  34. SymbolStringPtr S;
  35. S.S = P;
  36. return S;
  37. }
  38. // Copy a pool entry to a SymbolStringPtr (increments ref count).
  39. static SymbolStringPtr copyToSymbolStringPtr(PoolEntryPtr P) {
  40. return SymbolStringPtr(P);
  41. }
  42. static PoolEntryPtr getRawPoolEntryPtr(const SymbolStringPtr &S) {
  43. return S.S;
  44. }
  45. static void retainPoolEntry(PoolEntryPtr P) {
  46. SymbolStringPtr S(P);
  47. S.S = nullptr;
  48. }
  49. static void releasePoolEntry(PoolEntryPtr P) {
  50. SymbolStringPtr S;
  51. S.S = P;
  52. }
  53. static InProgressLookupState *extractLookupState(LookupState &LS) {
  54. return LS.IPLS.release();
  55. }
  56. static void resetLookupState(LookupState &LS, InProgressLookupState *IPLS) {
  57. return LS.reset(IPLS);
  58. }
  59. };
  60. } // namespace orc
  61. } // namespace llvm
  62. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionSession, LLVMOrcExecutionSessionRef)
  63. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SymbolStringPool, LLVMOrcSymbolStringPoolRef)
  64. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcV2CAPIHelper::PoolEntry,
  65. LLVMOrcSymbolStringPoolEntryRef)
  66. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationUnit,
  67. LLVMOrcMaterializationUnitRef)
  68. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationResponsibility,
  69. LLVMOrcMaterializationResponsibilityRef)
  70. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITDylib, LLVMOrcJITDylibRef)
  71. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ResourceTracker, LLVMOrcResourceTrackerRef)
  72. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DefinitionGenerator,
  73. LLVMOrcDefinitionGeneratorRef)
  74. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(InProgressLookupState, LLVMOrcLookupStateRef)
  75. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeContext,
  76. LLVMOrcThreadSafeContextRef)
  77. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeModule, LLVMOrcThreadSafeModuleRef)
  78. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITTargetMachineBuilder,
  79. LLVMOrcJITTargetMachineBuilderRef)
  80. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectLayer, LLVMOrcObjectLayerRef)
  81. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRTransformLayer, LLVMOrcIRTransformLayerRef)
  82. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectTransformLayer,
  83. LLVMOrcObjectTransformLayerRef)
  84. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DumpObjects, LLVMOrcDumpObjectsRef)
  85. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IndirectStubsManager,
  86. LLVMOrcIndirectStubsManagerRef)
  87. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LazyCallThroughManager,
  88. LLVMOrcLazyCallThroughManagerRef)
  89. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJITBuilder, LLVMOrcLLJITBuilderRef)
  90. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJIT, LLVMOrcLLJITRef)
  91. DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
  92. namespace llvm {
  93. namespace orc {
  94. class CAPIDefinitionGenerator final : public DefinitionGenerator {
  95. public:
  96. CAPIDefinitionGenerator(
  97. void *Ctx,
  98. LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate)
  99. : Ctx(Ctx), TryToGenerate(TryToGenerate) {}
  100. Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
  101. JITDylibLookupFlags JDLookupFlags,
  102. const SymbolLookupSet &LookupSet) override {
  103. // Take the lookup state.
  104. LLVMOrcLookupStateRef LSR = ::wrap(OrcV2CAPIHelper::extractLookupState(LS));
  105. // Translate the lookup kind.
  106. LLVMOrcLookupKind CLookupKind;
  107. switch (K) {
  108. case LookupKind::Static:
  109. CLookupKind = LLVMOrcLookupKindStatic;
  110. break;
  111. case LookupKind::DLSym:
  112. CLookupKind = LLVMOrcLookupKindDLSym;
  113. break;
  114. }
  115. // Translate the JITDylibSearchFlags.
  116. LLVMOrcJITDylibLookupFlags CJDLookupFlags;
  117. switch (JDLookupFlags) {
  118. case JITDylibLookupFlags::MatchExportedSymbolsOnly:
  119. CJDLookupFlags = LLVMOrcJITDylibLookupFlagsMatchExportedSymbolsOnly;
  120. break;
  121. case JITDylibLookupFlags::MatchAllSymbols:
  122. CJDLookupFlags = LLVMOrcJITDylibLookupFlagsMatchAllSymbols;
  123. break;
  124. }
  125. // Translate the lookup set.
  126. std::vector<LLVMOrcCLookupSetElement> CLookupSet;
  127. CLookupSet.reserve(LookupSet.size());
  128. for (auto &KV : LookupSet) {
  129. LLVMOrcSymbolLookupFlags SLF;
  130. LLVMOrcSymbolStringPoolEntryRef Name =
  131. ::wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(KV.first));
  132. switch (KV.second) {
  133. case SymbolLookupFlags::RequiredSymbol:
  134. SLF = LLVMOrcSymbolLookupFlagsRequiredSymbol;
  135. break;
  136. case SymbolLookupFlags::WeaklyReferencedSymbol:
  137. SLF = LLVMOrcSymbolLookupFlagsWeaklyReferencedSymbol;
  138. break;
  139. }
  140. CLookupSet.push_back({Name, SLF});
  141. }
  142. // Run the C TryToGenerate function.
  143. auto Err = unwrap(TryToGenerate(::wrap(this), Ctx, &LSR, CLookupKind,
  144. ::wrap(&JD), CJDLookupFlags,
  145. CLookupSet.data(), CLookupSet.size()));
  146. // Restore the lookup state.
  147. OrcV2CAPIHelper::resetLookupState(LS, ::unwrap(LSR));
  148. return Err;
  149. }
  150. private:
  151. void *Ctx;
  152. LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate;
  153. };
  154. } // end namespace orc
  155. } // end namespace llvm
  156. namespace {
  157. class OrcCAPIMaterializationUnit : public llvm::orc::MaterializationUnit {
  158. public:
  159. OrcCAPIMaterializationUnit(
  160. std::string Name, SymbolFlagsMap InitialSymbolFlags,
  161. SymbolStringPtr InitSymbol, void *Ctx,
  162. LLVMOrcMaterializationUnitMaterializeFunction Materialize,
  163. LLVMOrcMaterializationUnitDiscardFunction Discard,
  164. LLVMOrcMaterializationUnitDestroyFunction Destroy)
  165. : llvm::orc::MaterializationUnit(
  166. Interface(std::move(InitialSymbolFlags), std::move(InitSymbol))),
  167. Name(std::move(Name)), Ctx(Ctx), Materialize(Materialize),
  168. Discard(Discard), Destroy(Destroy) {}
  169. ~OrcCAPIMaterializationUnit() {
  170. if (Ctx)
  171. Destroy(Ctx);
  172. }
  173. StringRef getName() const override { return Name; }
  174. void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
  175. void *Tmp = Ctx;
  176. Ctx = nullptr;
  177. Materialize(Tmp, wrap(R.release()));
  178. }
  179. private:
  180. void discard(const JITDylib &JD, const SymbolStringPtr &Name) override {
  181. Discard(Ctx, wrap(&JD), wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name)));
  182. }
  183. std::string Name;
  184. void *Ctx = nullptr;
  185. LLVMOrcMaterializationUnitMaterializeFunction Materialize = nullptr;
  186. LLVMOrcMaterializationUnitDiscardFunction Discard = nullptr;
  187. LLVMOrcMaterializationUnitDestroyFunction Destroy = nullptr;
  188. };
  189. static JITSymbolFlags toJITSymbolFlags(LLVMJITSymbolFlags F) {
  190. JITSymbolFlags JSF;
  191. if (F.GenericFlags & LLVMJITSymbolGenericFlagsExported)
  192. JSF |= JITSymbolFlags::Exported;
  193. if (F.GenericFlags & LLVMJITSymbolGenericFlagsWeak)
  194. JSF |= JITSymbolFlags::Weak;
  195. if (F.GenericFlags & LLVMJITSymbolGenericFlagsCallable)
  196. JSF |= JITSymbolFlags::Callable;
  197. if (F.GenericFlags & LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly)
  198. JSF |= JITSymbolFlags::MaterializationSideEffectsOnly;
  199. JSF.getTargetFlags() = F.TargetFlags;
  200. return JSF;
  201. }
  202. static LLVMJITSymbolFlags fromJITSymbolFlags(JITSymbolFlags JSF) {
  203. LLVMJITSymbolFlags F = {0, 0};
  204. if (JSF & JITSymbolFlags::Exported)
  205. F.GenericFlags |= LLVMJITSymbolGenericFlagsExported;
  206. if (JSF & JITSymbolFlags::Weak)
  207. F.GenericFlags |= LLVMJITSymbolGenericFlagsWeak;
  208. if (JSF & JITSymbolFlags::Callable)
  209. F.GenericFlags |= LLVMJITSymbolGenericFlagsCallable;
  210. if (JSF & JITSymbolFlags::MaterializationSideEffectsOnly)
  211. F.GenericFlags |= LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly;
  212. F.TargetFlags = JSF.getTargetFlags();
  213. return F;
  214. }
  215. static SymbolMap toSymbolMap(LLVMOrcCSymbolMapPairs Syms, size_t NumPairs) {
  216. SymbolMap SM;
  217. for (size_t I = 0; I != NumPairs; ++I) {
  218. JITSymbolFlags Flags = toJITSymbolFlags(Syms[I].Sym.Flags);
  219. SM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms[I].Name))] =
  220. JITEvaluatedSymbol(Syms[I].Sym.Address, Flags);
  221. }
  222. return SM;
  223. }
  224. static SymbolDependenceMap
  225. toSymbolDependenceMap(LLVMOrcCDependenceMapPairs Pairs, size_t NumPairs) {
  226. SymbolDependenceMap SDM;
  227. for (size_t I = 0; I != NumPairs; ++I) {
  228. JITDylib *JD = unwrap(Pairs[I].JD);
  229. SymbolNameSet Names;
  230. for (size_t J = 0; J != Pairs[I].Names.Length; ++J) {
  231. auto Sym = Pairs[I].Names.Symbols[J];
  232. Names.insert(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Sym)));
  233. }
  234. SDM[JD] = Names;
  235. }
  236. return SDM;
  237. }
  238. } // end anonymous namespace
  239. void LLVMOrcExecutionSessionSetErrorReporter(
  240. LLVMOrcExecutionSessionRef ES, LLVMOrcErrorReporterFunction ReportError,
  241. void *Ctx) {
  242. unwrap(ES)->setErrorReporter(
  243. [=](Error Err) { ReportError(Ctx, wrap(std::move(Err))); });
  244. }
  245. LLVMOrcSymbolStringPoolRef
  246. LLVMOrcExecutionSessionGetSymbolStringPool(LLVMOrcExecutionSessionRef ES) {
  247. return wrap(
  248. unwrap(ES)->getExecutorProcessControl().getSymbolStringPool().get());
  249. }
  250. void LLVMOrcSymbolStringPoolClearDeadEntries(LLVMOrcSymbolStringPoolRef SSP) {
  251. unwrap(SSP)->clearDeadEntries();
  252. }
  253. LLVMOrcSymbolStringPoolEntryRef
  254. LLVMOrcExecutionSessionIntern(LLVMOrcExecutionSessionRef ES, const char *Name) {
  255. return wrap(
  256. OrcV2CAPIHelper::moveFromSymbolStringPtr(unwrap(ES)->intern(Name)));
  257. }
  258. void LLVMOrcRetainSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S) {
  259. OrcV2CAPIHelper::retainPoolEntry(unwrap(S));
  260. }
  261. void LLVMOrcReleaseSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S) {
  262. OrcV2CAPIHelper::releasePoolEntry(unwrap(S));
  263. }
  264. const char *LLVMOrcSymbolStringPoolEntryStr(LLVMOrcSymbolStringPoolEntryRef S) {
  265. return unwrap(S)->getKey().data();
  266. }
  267. LLVMOrcResourceTrackerRef
  268. LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD) {
  269. auto RT = unwrap(JD)->createResourceTracker();
  270. // Retain the pointer for the C API client.
  271. RT->Retain();
  272. return wrap(RT.get());
  273. }
  274. LLVMOrcResourceTrackerRef
  275. LLVMOrcJITDylibGetDefaultResourceTracker(LLVMOrcJITDylibRef JD) {
  276. auto RT = unwrap(JD)->getDefaultResourceTracker();
  277. // Retain the pointer for the C API client.
  278. return wrap(RT.get());
  279. }
  280. void LLVMOrcReleaseResourceTracker(LLVMOrcResourceTrackerRef RT) {
  281. ResourceTrackerSP TmpRT(unwrap(RT));
  282. TmpRT->Release();
  283. }
  284. void LLVMOrcResourceTrackerTransferTo(LLVMOrcResourceTrackerRef SrcRT,
  285. LLVMOrcResourceTrackerRef DstRT) {
  286. ResourceTrackerSP TmpRT(unwrap(SrcRT));
  287. TmpRT->transferTo(*unwrap(DstRT));
  288. }
  289. LLVMErrorRef LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT) {
  290. ResourceTrackerSP TmpRT(unwrap(RT));
  291. return wrap(TmpRT->remove());
  292. }
  293. void LLVMOrcDisposeDefinitionGenerator(LLVMOrcDefinitionGeneratorRef DG) {
  294. std::unique_ptr<DefinitionGenerator> TmpDG(unwrap(DG));
  295. }
  296. void LLVMOrcDisposeMaterializationUnit(LLVMOrcMaterializationUnitRef MU) {
  297. std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU));
  298. }
  299. LLVMOrcMaterializationUnitRef LLVMOrcCreateCustomMaterializationUnit(
  300. const char *Name, void *Ctx, LLVMOrcCSymbolFlagsMapPairs Syms,
  301. size_t NumSyms, LLVMOrcSymbolStringPoolEntryRef InitSym,
  302. LLVMOrcMaterializationUnitMaterializeFunction Materialize,
  303. LLVMOrcMaterializationUnitDiscardFunction Discard,
  304. LLVMOrcMaterializationUnitDestroyFunction Destroy) {
  305. SymbolFlagsMap SFM;
  306. for (size_t I = 0; I != NumSyms; ++I)
  307. SFM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms[I].Name))] =
  308. toJITSymbolFlags(Syms[I].Flags);
  309. auto IS = OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(InitSym));
  310. return wrap(new OrcCAPIMaterializationUnit(
  311. Name, std::move(SFM), std::move(IS), Ctx, Materialize, Discard, Destroy));
  312. }
  313. LLVMOrcMaterializationUnitRef
  314. LLVMOrcAbsoluteSymbols(LLVMOrcCSymbolMapPairs Syms, size_t NumPairs) {
  315. SymbolMap SM = toSymbolMap(Syms, NumPairs);
  316. return wrap(absoluteSymbols(std::move(SM)).release());
  317. }
  318. LLVMOrcMaterializationUnitRef LLVMOrcLazyReexports(
  319. LLVMOrcLazyCallThroughManagerRef LCTM, LLVMOrcIndirectStubsManagerRef ISM,
  320. LLVMOrcJITDylibRef SourceJD, LLVMOrcCSymbolAliasMapPairs CallableAliases,
  321. size_t NumPairs) {
  322. SymbolAliasMap SAM;
  323. for (size_t I = 0; I != NumPairs; ++I) {
  324. auto pair = CallableAliases[I];
  325. JITSymbolFlags Flags = toJITSymbolFlags(pair.Entry.Flags);
  326. SymbolStringPtr Name =
  327. OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(pair.Entry.Name));
  328. SAM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(pair.Name))] =
  329. SymbolAliasMapEntry(Name, Flags);
  330. }
  331. return wrap(lazyReexports(*unwrap(LCTM), *unwrap(ISM), *unwrap(SourceJD),
  332. std::move(SAM))
  333. .release());
  334. }
  335. void LLVMOrcDisposeMaterializationResponsibility(
  336. LLVMOrcMaterializationResponsibilityRef MR) {
  337. std::unique_ptr<MaterializationResponsibility> TmpMR(unwrap(MR));
  338. }
  339. LLVMOrcJITDylibRef LLVMOrcMaterializationResponsibilityGetTargetDylib(
  340. LLVMOrcMaterializationResponsibilityRef MR) {
  341. return wrap(&unwrap(MR)->getTargetJITDylib());
  342. }
  343. LLVMOrcExecutionSessionRef
  344. LLVMOrcMaterializationResponsibilityGetExecutionSession(
  345. LLVMOrcMaterializationResponsibilityRef MR) {
  346. return wrap(&unwrap(MR)->getExecutionSession());
  347. }
  348. LLVMOrcCSymbolFlagsMapPairs LLVMOrcMaterializationResponsibilityGetSymbols(
  349. LLVMOrcMaterializationResponsibilityRef MR, size_t *NumPairs) {
  350. auto Symbols = unwrap(MR)->getSymbols();
  351. LLVMOrcCSymbolFlagsMapPairs Result = static_cast<LLVMOrcCSymbolFlagsMapPairs>(
  352. safe_malloc(Symbols.size() * sizeof(LLVMOrcCSymbolFlagsMapPair)));
  353. size_t I = 0;
  354. for (auto const &pair : Symbols) {
  355. auto Name = wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(pair.first));
  356. auto Flags = pair.second;
  357. Result[I] = {Name, fromJITSymbolFlags(Flags)};
  358. I++;
  359. }
  360. *NumPairs = Symbols.size();
  361. return Result;
  362. }
  363. void LLVMOrcDisposeCSymbolFlagsMap(LLVMOrcCSymbolFlagsMapPairs Pairs) {
  364. free(Pairs);
  365. }
  366. LLVMOrcSymbolStringPoolEntryRef
  367. LLVMOrcMaterializationResponsibilityGetInitializerSymbol(
  368. LLVMOrcMaterializationResponsibilityRef MR) {
  369. auto Sym = unwrap(MR)->getInitializerSymbol();
  370. return wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Sym));
  371. }
  372. LLVMOrcSymbolStringPoolEntryRef *
  373. LLVMOrcMaterializationResponsibilityGetRequestedSymbols(
  374. LLVMOrcMaterializationResponsibilityRef MR, size_t *NumSymbols) {
  375. auto Symbols = unwrap(MR)->getRequestedSymbols();
  376. LLVMOrcSymbolStringPoolEntryRef *Result =
  377. static_cast<LLVMOrcSymbolStringPoolEntryRef *>(safe_malloc(
  378. Symbols.size() * sizeof(LLVMOrcSymbolStringPoolEntryRef)));
  379. size_t I = 0;
  380. for (auto &Name : Symbols) {
  381. Result[I] = wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name));
  382. I++;
  383. }
  384. *NumSymbols = Symbols.size();
  385. return Result;
  386. }
  387. void LLVMOrcDisposeSymbols(LLVMOrcSymbolStringPoolEntryRef *Symbols) {
  388. free(Symbols);
  389. }
  390. LLVMErrorRef LLVMOrcMaterializationResponsibilityNotifyResolved(
  391. LLVMOrcMaterializationResponsibilityRef MR, LLVMOrcCSymbolMapPairs Symbols,
  392. size_t NumPairs) {
  393. SymbolMap SM = toSymbolMap(Symbols, NumPairs);
  394. return wrap(unwrap(MR)->notifyResolved(std::move(SM)));
  395. }
  396. LLVMErrorRef LLVMOrcMaterializationResponsibilityNotifyEmitted(
  397. LLVMOrcMaterializationResponsibilityRef MR) {
  398. return wrap(unwrap(MR)->notifyEmitted());
  399. }
  400. LLVMErrorRef LLVMOrcMaterializationResponsibilityDefineMaterializing(
  401. LLVMOrcMaterializationResponsibilityRef MR,
  402. LLVMOrcCSymbolFlagsMapPairs Syms, size_t NumSyms) {
  403. SymbolFlagsMap SFM;
  404. for (size_t I = 0; I != NumSyms; ++I)
  405. SFM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms[I].Name))] =
  406. toJITSymbolFlags(Syms[I].Flags);
  407. return wrap(unwrap(MR)->defineMaterializing(std::move(SFM)));
  408. }
  409. LLVMErrorRef LLVMOrcMaterializationResponsibilityReplace(
  410. LLVMOrcMaterializationResponsibilityRef MR,
  411. LLVMOrcMaterializationUnitRef MU) {
  412. std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU));
  413. return wrap(unwrap(MR)->replace(std::move(TmpMU)));
  414. }
  415. LLVMErrorRef LLVMOrcMaterializationResponsibilityDelegate(
  416. LLVMOrcMaterializationResponsibilityRef MR,
  417. LLVMOrcSymbolStringPoolEntryRef *Symbols, size_t NumSymbols,
  418. LLVMOrcMaterializationResponsibilityRef *Result) {
  419. SymbolNameSet Syms;
  420. for (size_t I = 0; I != NumSymbols; I++) {
  421. Syms.insert(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Symbols[I])));
  422. }
  423. auto OtherMR = unwrap(MR)->delegate(Syms);
  424. if (!OtherMR) {
  425. return wrap(OtherMR.takeError());
  426. }
  427. *Result = wrap(OtherMR->release());
  428. return LLVMErrorSuccess;
  429. }
  430. void LLVMOrcMaterializationResponsibilityAddDependencies(
  431. LLVMOrcMaterializationResponsibilityRef MR,
  432. LLVMOrcSymbolStringPoolEntryRef Name,
  433. LLVMOrcCDependenceMapPairs Dependencies, size_t NumPairs) {
  434. SymbolDependenceMap SDM = toSymbolDependenceMap(Dependencies, NumPairs);
  435. auto Sym = OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Name));
  436. unwrap(MR)->addDependencies(Sym, SDM);
  437. }
  438. void LLVMOrcMaterializationResponsibilityAddDependenciesForAll(
  439. LLVMOrcMaterializationResponsibilityRef MR,
  440. LLVMOrcCDependenceMapPairs Dependencies, size_t NumPairs) {
  441. SymbolDependenceMap SDM = toSymbolDependenceMap(Dependencies, NumPairs);
  442. unwrap(MR)->addDependenciesForAll(SDM);
  443. }
  444. void LLVMOrcMaterializationResponsibilityFailMaterialization(
  445. LLVMOrcMaterializationResponsibilityRef MR) {
  446. unwrap(MR)->failMaterialization();
  447. }
  448. void LLVMOrcIRTransformLayerEmit(LLVMOrcIRTransformLayerRef IRLayer,
  449. LLVMOrcMaterializationResponsibilityRef MR,
  450. LLVMOrcThreadSafeModuleRef TSM) {
  451. std::unique_ptr<ThreadSafeModule> TmpTSM(unwrap(TSM));
  452. unwrap(IRLayer)->emit(
  453. std::unique_ptr<MaterializationResponsibility>(unwrap(MR)),
  454. std::move(*TmpTSM));
  455. }
  456. LLVMOrcJITDylibRef
  457. LLVMOrcExecutionSessionCreateBareJITDylib(LLVMOrcExecutionSessionRef ES,
  458. const char *Name) {
  459. return wrap(&unwrap(ES)->createBareJITDylib(Name));
  460. }
  461. LLVMErrorRef
  462. LLVMOrcExecutionSessionCreateJITDylib(LLVMOrcExecutionSessionRef ES,
  463. LLVMOrcJITDylibRef *Result,
  464. const char *Name) {
  465. auto JD = unwrap(ES)->createJITDylib(Name);
  466. if (!JD)
  467. return wrap(JD.takeError());
  468. *Result = wrap(&*JD);
  469. return LLVMErrorSuccess;
  470. }
  471. LLVMOrcJITDylibRef
  472. LLVMOrcExecutionSessionGetJITDylibByName(LLVMOrcExecutionSessionRef ES,
  473. const char *Name) {
  474. return wrap(unwrap(ES)->getJITDylibByName(Name));
  475. }
  476. LLVMErrorRef LLVMOrcJITDylibDefine(LLVMOrcJITDylibRef JD,
  477. LLVMOrcMaterializationUnitRef MU) {
  478. std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU));
  479. if (auto Err = unwrap(JD)->define(TmpMU)) {
  480. TmpMU.release();
  481. return wrap(std::move(Err));
  482. }
  483. return LLVMErrorSuccess;
  484. }
  485. LLVMErrorRef LLVMOrcJITDylibClear(LLVMOrcJITDylibRef JD) {
  486. return wrap(unwrap(JD)->clear());
  487. }
  488. void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD,
  489. LLVMOrcDefinitionGeneratorRef DG) {
  490. unwrap(JD)->addGenerator(std::unique_ptr<DefinitionGenerator>(unwrap(DG)));
  491. }
  492. LLVMOrcDefinitionGeneratorRef LLVMOrcCreateCustomCAPIDefinitionGenerator(
  493. LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F, void *Ctx) {
  494. auto DG = std::make_unique<CAPIDefinitionGenerator>(Ctx, F);
  495. return wrap(DG.release());
  496. }
  497. LLVMErrorRef LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(
  498. LLVMOrcDefinitionGeneratorRef *Result, char GlobalPrefix,
  499. LLVMOrcSymbolPredicate Filter, void *FilterCtx) {
  500. assert(Result && "Result can not be null");
  501. assert((Filter || !FilterCtx) &&
  502. "if Filter is null then FilterCtx must also be null");
  503. DynamicLibrarySearchGenerator::SymbolPredicate Pred;
  504. if (Filter)
  505. Pred = [=](const SymbolStringPtr &Name) -> bool {
  506. return Filter(FilterCtx, wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name)));
  507. };
  508. auto ProcessSymsGenerator =
  509. DynamicLibrarySearchGenerator::GetForCurrentProcess(GlobalPrefix, Pred);
  510. if (!ProcessSymsGenerator) {
  511. *Result = nullptr;
  512. return wrap(ProcessSymsGenerator.takeError());
  513. }
  514. *Result = wrap(ProcessSymsGenerator->release());
  515. return LLVMErrorSuccess;
  516. }
  517. LLVMErrorRef LLVMOrcCreateDynamicLibrarySearchGeneratorForPath(
  518. LLVMOrcDefinitionGeneratorRef *Result, const char *FileName,
  519. char GlobalPrefix, LLVMOrcSymbolPredicate Filter, void *FilterCtx) {
  520. assert(Result && "Result can not be null");
  521. assert(FileName && "FileName can not be null");
  522. assert((Filter || !FilterCtx) &&
  523. "if Filter is null then FilterCtx must also be null");
  524. DynamicLibrarySearchGenerator::SymbolPredicate Pred;
  525. if (Filter)
  526. Pred = [=](const SymbolStringPtr &Name) -> bool {
  527. return Filter(FilterCtx, wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name)));
  528. };
  529. auto LibrarySymsGenerator =
  530. DynamicLibrarySearchGenerator::Load(FileName, GlobalPrefix, Pred);
  531. if (!LibrarySymsGenerator) {
  532. *Result = nullptr;
  533. return wrap(LibrarySymsGenerator.takeError());
  534. }
  535. *Result = wrap(LibrarySymsGenerator->release());
  536. return LLVMErrorSuccess;
  537. }
  538. LLVMErrorRef LLVMOrcCreateStaticLibrarySearchGeneratorForPath(
  539. LLVMOrcDefinitionGeneratorRef *Result, LLVMOrcObjectLayerRef ObjLayer,
  540. const char *FileName, const char *TargetTriple) {
  541. assert(Result && "Result can not be null");
  542. assert(FileName && "Filename can not be null");
  543. assert(ObjLayer && "ObjectLayer can not be null");
  544. if (TargetTriple) {
  545. auto TT = Triple(TargetTriple);
  546. auto LibrarySymsGenerator =
  547. StaticLibraryDefinitionGenerator::Load(*unwrap(ObjLayer), FileName, TT);
  548. if (!LibrarySymsGenerator) {
  549. *Result = nullptr;
  550. return wrap(LibrarySymsGenerator.takeError());
  551. }
  552. *Result = wrap(LibrarySymsGenerator->release());
  553. return LLVMErrorSuccess;
  554. } else {
  555. auto LibrarySymsGenerator =
  556. StaticLibraryDefinitionGenerator::Load(*unwrap(ObjLayer), FileName);
  557. if (!LibrarySymsGenerator) {
  558. *Result = nullptr;
  559. return wrap(LibrarySymsGenerator.takeError());
  560. }
  561. *Result = wrap(LibrarySymsGenerator->release());
  562. return LLVMErrorSuccess;
  563. }
  564. }
  565. LLVMOrcThreadSafeContextRef LLVMOrcCreateNewThreadSafeContext(void) {
  566. return wrap(new ThreadSafeContext(std::make_unique<LLVMContext>()));
  567. }
  568. LLVMContextRef
  569. LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx) {
  570. return wrap(unwrap(TSCtx)->getContext());
  571. }
  572. void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx) {
  573. delete unwrap(TSCtx);
  574. }
  575. LLVMErrorRef
  576. LLVMOrcThreadSafeModuleWithModuleDo(LLVMOrcThreadSafeModuleRef TSM,
  577. LLVMOrcGenericIRModuleOperationFunction F,
  578. void *Ctx) {
  579. return wrap(unwrap(TSM)->withModuleDo(
  580. [&](Module &M) { return unwrap(F(Ctx, wrap(&M))); }));
  581. }
  582. LLVMOrcThreadSafeModuleRef
  583. LLVMOrcCreateNewThreadSafeModule(LLVMModuleRef M,
  584. LLVMOrcThreadSafeContextRef TSCtx) {
  585. return wrap(
  586. new ThreadSafeModule(std::unique_ptr<Module>(unwrap(M)), *unwrap(TSCtx)));
  587. }
  588. void LLVMOrcDisposeThreadSafeModule(LLVMOrcThreadSafeModuleRef TSM) {
  589. delete unwrap(TSM);
  590. }
  591. LLVMErrorRef LLVMOrcJITTargetMachineBuilderDetectHost(
  592. LLVMOrcJITTargetMachineBuilderRef *Result) {
  593. assert(Result && "Result can not be null");
  594. auto JTMB = JITTargetMachineBuilder::detectHost();
  595. if (!JTMB) {
  596. Result = nullptr;
  597. return wrap(JTMB.takeError());
  598. }
  599. *Result = wrap(new JITTargetMachineBuilder(std::move(*JTMB)));
  600. return LLVMErrorSuccess;
  601. }
  602. LLVMOrcJITTargetMachineBuilderRef
  603. LLVMOrcJITTargetMachineBuilderCreateFromTargetMachine(LLVMTargetMachineRef TM) {
  604. auto *TemplateTM = unwrap(TM);
  605. auto JTMB =
  606. std::make_unique<JITTargetMachineBuilder>(TemplateTM->getTargetTriple());
  607. (*JTMB)
  608. .setCPU(TemplateTM->getTargetCPU().str())
  609. .setRelocationModel(TemplateTM->getRelocationModel())
  610. .setCodeModel(TemplateTM->getCodeModel())
  611. .setCodeGenOptLevel(TemplateTM->getOptLevel())
  612. .setFeatures(TemplateTM->getTargetFeatureString())
  613. .setOptions(TemplateTM->Options);
  614. LLVMDisposeTargetMachine(TM);
  615. return wrap(JTMB.release());
  616. }
  617. void LLVMOrcDisposeJITTargetMachineBuilder(
  618. LLVMOrcJITTargetMachineBuilderRef JTMB) {
  619. delete unwrap(JTMB);
  620. }
  621. char *LLVMOrcJITTargetMachineBuilderGetTargetTriple(
  622. LLVMOrcJITTargetMachineBuilderRef JTMB) {
  623. auto Tmp = unwrap(JTMB)->getTargetTriple().str();
  624. char *TargetTriple = (char *)malloc(Tmp.size() + 1);
  625. strcpy(TargetTriple, Tmp.c_str());
  626. return TargetTriple;
  627. }
  628. void LLVMOrcJITTargetMachineBuilderSetTargetTriple(
  629. LLVMOrcJITTargetMachineBuilderRef JTMB, const char *TargetTriple) {
  630. unwrap(JTMB)->getTargetTriple() = Triple(TargetTriple);
  631. }
  632. LLVMErrorRef LLVMOrcObjectLayerAddObjectFile(LLVMOrcObjectLayerRef ObjLayer,
  633. LLVMOrcJITDylibRef JD,
  634. LLVMMemoryBufferRef ObjBuffer) {
  635. return wrap(unwrap(ObjLayer)->add(
  636. *unwrap(JD), std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer))));
  637. }
  638. LLVMErrorRef LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcObjectLayerRef ObjLayer,
  639. LLVMOrcResourceTrackerRef RT,
  640. LLVMMemoryBufferRef ObjBuffer) {
  641. return wrap(
  642. unwrap(ObjLayer)->add(ResourceTrackerSP(unwrap(RT)),
  643. std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer))));
  644. }
  645. void LLVMOrcObjectLayerEmit(LLVMOrcObjectLayerRef ObjLayer,
  646. LLVMOrcMaterializationResponsibilityRef R,
  647. LLVMMemoryBufferRef ObjBuffer) {
  648. unwrap(ObjLayer)->emit(
  649. std::unique_ptr<MaterializationResponsibility>(unwrap(R)),
  650. std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer)));
  651. }
  652. void LLVMOrcDisposeObjectLayer(LLVMOrcObjectLayerRef ObjLayer) {
  653. delete unwrap(ObjLayer);
  654. }
  655. void LLVMOrcIRTransformLayerSetTransform(
  656. LLVMOrcIRTransformLayerRef IRTransformLayer,
  657. LLVMOrcIRTransformLayerTransformFunction TransformFunction, void *Ctx) {
  658. unwrap(IRTransformLayer)
  659. ->setTransform(
  660. [=](ThreadSafeModule TSM,
  661. MaterializationResponsibility &R) -> Expected<ThreadSafeModule> {
  662. LLVMOrcThreadSafeModuleRef TSMRef =
  663. wrap(new ThreadSafeModule(std::move(TSM)));
  664. if (LLVMErrorRef Err = TransformFunction(Ctx, &TSMRef, wrap(&R))) {
  665. assert(!TSMRef && "TSMRef was not reset to null on error");
  666. return unwrap(Err);
  667. }
  668. return std::move(*unwrap(TSMRef));
  669. });
  670. }
  671. void LLVMOrcObjectTransformLayerSetTransform(
  672. LLVMOrcObjectTransformLayerRef ObjTransformLayer,
  673. LLVMOrcObjectTransformLayerTransformFunction TransformFunction, void *Ctx) {
  674. unwrap(ObjTransformLayer)
  675. ->setTransform([TransformFunction, Ctx](std::unique_ptr<MemoryBuffer> Obj)
  676. -> Expected<std::unique_ptr<MemoryBuffer>> {
  677. LLVMMemoryBufferRef ObjBuffer = wrap(Obj.release());
  678. if (LLVMErrorRef Err = TransformFunction(Ctx, &ObjBuffer)) {
  679. assert(!ObjBuffer && "ObjBuffer was not reset to null on error");
  680. return unwrap(Err);
  681. }
  682. return std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer));
  683. });
  684. }
  685. LLVMOrcDumpObjectsRef LLVMOrcCreateDumpObjects(const char *DumpDir,
  686. const char *IdentifierOverride) {
  687. assert(DumpDir && "DumpDir should not be null");
  688. assert(IdentifierOverride && "IdentifierOverride should not be null");
  689. return wrap(new DumpObjects(DumpDir, IdentifierOverride));
  690. }
  691. void LLVMOrcDisposeDumpObjects(LLVMOrcDumpObjectsRef DumpObjects) {
  692. delete unwrap(DumpObjects);
  693. }
  694. LLVMErrorRef LLVMOrcDumpObjects_CallOperator(LLVMOrcDumpObjectsRef DumpObjects,
  695. LLVMMemoryBufferRef *ObjBuffer) {
  696. std::unique_ptr<MemoryBuffer> OB(unwrap(*ObjBuffer));
  697. if (auto Result = (*unwrap(DumpObjects))(std::move(OB))) {
  698. *ObjBuffer = wrap(Result->release());
  699. return LLVMErrorSuccess;
  700. } else {
  701. *ObjBuffer = nullptr;
  702. return wrap(Result.takeError());
  703. }
  704. }
  705. LLVMOrcLLJITBuilderRef LLVMOrcCreateLLJITBuilder(void) {
  706. return wrap(new LLJITBuilder());
  707. }
  708. void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder) {
  709. delete unwrap(Builder);
  710. }
  711. void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder(
  712. LLVMOrcLLJITBuilderRef Builder, LLVMOrcJITTargetMachineBuilderRef JTMB) {
  713. unwrap(Builder)->setJITTargetMachineBuilder(std::move(*unwrap(JTMB)));
  714. LLVMOrcDisposeJITTargetMachineBuilder(JTMB);
  715. }
  716. void LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator(
  717. LLVMOrcLLJITBuilderRef Builder,
  718. LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction F, void *Ctx) {
  719. unwrap(Builder)->setObjectLinkingLayerCreator(
  720. [=](ExecutionSession &ES, const Triple &TT) {
  721. auto TTStr = TT.str();
  722. return std::unique_ptr<ObjectLayer>(
  723. unwrap(F(Ctx, wrap(&ES), TTStr.c_str())));
  724. });
  725. }
  726. LLVMErrorRef LLVMOrcCreateLLJIT(LLVMOrcLLJITRef *Result,
  727. LLVMOrcLLJITBuilderRef Builder) {
  728. assert(Result && "Result can not be null");
  729. if (!Builder)
  730. Builder = LLVMOrcCreateLLJITBuilder();
  731. auto J = unwrap(Builder)->create();
  732. LLVMOrcDisposeLLJITBuilder(Builder);
  733. if (!J) {
  734. Result = nullptr;
  735. return wrap(J.takeError());
  736. }
  737. *Result = wrap(J->release());
  738. return LLVMErrorSuccess;
  739. }
  740. LLVMErrorRef LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J) {
  741. delete unwrap(J);
  742. return LLVMErrorSuccess;
  743. }
  744. LLVMOrcExecutionSessionRef LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J) {
  745. return wrap(&unwrap(J)->getExecutionSession());
  746. }
  747. LLVMOrcJITDylibRef LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J) {
  748. return wrap(&unwrap(J)->getMainJITDylib());
  749. }
  750. const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J) {
  751. return unwrap(J)->getTargetTriple().str().c_str();
  752. }
  753. char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J) {
  754. return unwrap(J)->getDataLayout().getGlobalPrefix();
  755. }
  756. LLVMOrcSymbolStringPoolEntryRef
  757. LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName) {
  758. return wrap(OrcV2CAPIHelper::moveFromSymbolStringPtr(
  759. unwrap(J)->mangleAndIntern(UnmangledName)));
  760. }
  761. LLVMErrorRef LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD,
  762. LLVMMemoryBufferRef ObjBuffer) {
  763. return wrap(unwrap(J)->addObjectFile(
  764. *unwrap(JD), std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer))));
  765. }
  766. LLVMErrorRef LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcLLJITRef J,
  767. LLVMOrcResourceTrackerRef RT,
  768. LLVMMemoryBufferRef ObjBuffer) {
  769. return wrap(unwrap(J)->addObjectFile(
  770. ResourceTrackerSP(unwrap(RT)),
  771. std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer))));
  772. }
  773. LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J,
  774. LLVMOrcJITDylibRef JD,
  775. LLVMOrcThreadSafeModuleRef TSM) {
  776. std::unique_ptr<ThreadSafeModule> TmpTSM(unwrap(TSM));
  777. return wrap(unwrap(J)->addIRModule(*unwrap(JD), std::move(*TmpTSM)));
  778. }
  779. LLVMErrorRef LLVMOrcLLJITAddLLVMIRModuleWithRT(LLVMOrcLLJITRef J,
  780. LLVMOrcResourceTrackerRef RT,
  781. LLVMOrcThreadSafeModuleRef TSM) {
  782. std::unique_ptr<ThreadSafeModule> TmpTSM(unwrap(TSM));
  783. return wrap(unwrap(J)->addIRModule(ResourceTrackerSP(unwrap(RT)),
  784. std::move(*TmpTSM)));
  785. }
  786. LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J,
  787. LLVMOrcJITTargetAddress *Result,
  788. const char *Name) {
  789. assert(Result && "Result can not be null");
  790. auto Sym = unwrap(J)->lookup(Name);
  791. if (!Sym) {
  792. *Result = 0;
  793. return wrap(Sym.takeError());
  794. }
  795. *Result = Sym->getAddress();
  796. return LLVMErrorSuccess;
  797. }
  798. LLVMOrcObjectLayerRef LLVMOrcLLJITGetObjLinkingLayer(LLVMOrcLLJITRef J) {
  799. return wrap(&unwrap(J)->getObjLinkingLayer());
  800. }
  801. LLVMOrcObjectTransformLayerRef
  802. LLVMOrcLLJITGetObjTransformLayer(LLVMOrcLLJITRef J) {
  803. return wrap(&unwrap(J)->getObjTransformLayer());
  804. }
  805. LLVMOrcObjectLayerRef
  806. LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(
  807. LLVMOrcExecutionSessionRef ES) {
  808. assert(ES && "ES must not be null");
  809. return wrap(new RTDyldObjectLinkingLayer(
  810. *unwrap(ES), [] { return std::make_unique<SectionMemoryManager>(); }));
  811. }
  812. void LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(
  813. LLVMOrcObjectLayerRef RTDyldObjLinkingLayer,
  814. LLVMJITEventListenerRef Listener) {
  815. assert(RTDyldObjLinkingLayer && "RTDyldObjLinkingLayer must not be null");
  816. assert(Listener && "Listener must not be null");
  817. reinterpret_cast<RTDyldObjectLinkingLayer *>(unwrap(RTDyldObjLinkingLayer))
  818. ->registerJITEventListener(*unwrap(Listener));
  819. }
  820. LLVMOrcIRTransformLayerRef LLVMOrcLLJITGetIRTransformLayer(LLVMOrcLLJITRef J) {
  821. return wrap(&unwrap(J)->getIRTransformLayer());
  822. }
  823. const char *LLVMOrcLLJITGetDataLayoutStr(LLVMOrcLLJITRef J) {
  824. return unwrap(J)->getDataLayout().getStringRepresentation().c_str();
  825. }
  826. LLVMOrcIndirectStubsManagerRef
  827. LLVMOrcCreateLocalIndirectStubsManager(const char *TargetTriple) {
  828. auto builder = createLocalIndirectStubsManagerBuilder(Triple(TargetTriple));
  829. return wrap(builder().release());
  830. }
  831. void LLVMOrcDisposeIndirectStubsManager(LLVMOrcIndirectStubsManagerRef ISM) {
  832. std::unique_ptr<IndirectStubsManager> TmpISM(unwrap(ISM));
  833. }
  834. LLVMErrorRef LLVMOrcCreateLocalLazyCallThroughManager(
  835. const char *TargetTriple, LLVMOrcExecutionSessionRef ES,
  836. LLVMOrcJITTargetAddress ErrorHandlerAddr,
  837. LLVMOrcLazyCallThroughManagerRef *Result) {
  838. auto LCTM = createLocalLazyCallThroughManager(Triple(TargetTriple),
  839. *unwrap(ES), ErrorHandlerAddr);
  840. if (!LCTM)
  841. return wrap(LCTM.takeError());
  842. *Result = wrap(LCTM->release());
  843. return LLVMErrorSuccess;
  844. }
  845. void LLVMOrcDisposeLazyCallThroughManager(
  846. LLVMOrcLazyCallThroughManagerRef LCM) {
  847. std::unique_ptr<LazyCallThroughManager> TmpLCM(unwrap(LCM));
  848. }