LLVM  12.0.0git
OrcV2CBindings.cpp
Go to the documentation of this file.
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 
9 #include "llvm-c/Orc.h"
10 #include "llvm-c/TargetMachine.h"
11 
14 
15 using namespace llvm;
16 using namespace llvm::orc;
17 
18 namespace llvm {
19 namespace orc {
20 
22 public:
25 
27  PoolEntryPtr Result = nullptr;
28  std::swap(Result, S.S);
29  return Result;
30  }
31 
33  return S.S;
34  }
35 
38  S.S = P;
39  }
40 };
41 
42 } // end namespace orc
43 } // end namespace llvm
44 
49 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITDylib::DefinitionGenerator,
58 
60 
63  return wrap(
65 }
66 
69 }
70 
73  delete unwrap(DG);
74 }
75 
78  unwrap(JD)->addGenerator(
79  std::unique_ptr<JITDylib::DefinitionGenerator>(unwrap(DG)));
80 }
81 
84  LLVMOrcSymbolPredicate Filter, void *FilterCtx) {
85  assert(Result && "Result can not be null");
86  assert((Filter || !FilterCtx) &&
87  "if Filter is null then FilterCtx must also be null");
88 
90  if (Filter)
91  Pred = [=](const SymbolStringPtr &Name) -> bool {
92  return Filter(wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name)), FilterCtx);
93  };
94 
95  auto ProcessSymsGenerator =
97 
98  if (!ProcessSymsGenerator) {
99  *Result = 0;
100  return wrap(ProcessSymsGenerator.takeError());
101  }
102 
103  *Result = wrap(ProcessSymsGenerator->release());
104  return LLVMErrorSuccess;
105 }
106 
108  return wrap(new ThreadSafeContext(std::make_unique<LLVMContext>()));
109 }
110 
113  return wrap(unwrap(TSCtx)->getContext());
114 }
115 
117  delete unwrap(TSCtx);
118 }
119 
123  return wrap(
124  new ThreadSafeModule(std::unique_ptr<Module>(unwrap(M)), *unwrap(TSCtx)));
125 }
126 
128  delete unwrap(TSM);
129 }
130 
133  assert(Result && "Result can not be null");
134 
136  if (!JTMB) {
137  Result = 0;
138  return wrap(JTMB.takeError());
139  }
140 
141  *Result = wrap(new JITTargetMachineBuilder(std::move(*JTMB)));
142  return LLVMErrorSuccess;
143 }
144 
147  auto *TemplateTM = unwrap(TM);
148 
149  auto JTMB =
150  std::make_unique<JITTargetMachineBuilder>(TemplateTM->getTargetTriple());
151 
152  (*JTMB)
153  .setCPU(TemplateTM->getTargetCPU().str())
154  .setRelocationModel(TemplateTM->getRelocationModel())
155  .setCodeModel(TemplateTM->getCodeModel())
156  .setCodeGenOptLevel(TemplateTM->getOptLevel())
157  .setFeatures(TemplateTM->getTargetFeatureString())
158  .setOptions(TemplateTM->Options);
159 
161 
162  return wrap(JTMB.release());
163 }
164 
167  delete unwrap(JTMB);
168 }
169 
171  return wrap(new LLJITBuilder());
172 }
173 
175  delete unwrap(Builder);
176 }
177 
180  unwrap(Builder)->setJITTargetMachineBuilder(*unwrap(JTMB));
181 }
182 
185  assert(Result && "Result can not be null");
186 
187  if (!Builder)
188  Builder = LLVMOrcCreateLLJITBuilder();
189 
190  auto J = unwrap(Builder)->create();
192 
193  if (!J) {
194  Result = 0;
195  return wrap(J.takeError());
196  }
197 
198  *Result = wrap(J->release());
199  return LLVMErrorSuccess;
200 }
201 
203  delete unwrap(J);
204  return LLVMErrorSuccess;
205 }
206 
208  return wrap(&unwrap(J)->getExecutionSession());
209 }
210 
212  return wrap(&unwrap(J)->getMainJITDylib());
213 }
214 
216  return unwrap(J)->getTargetTriple().str().c_str();
217 }
218 
220  return unwrap(J)->getDataLayout().getGlobalPrefix();
221 }
222 
224 LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName) {
226  unwrap(J)->mangleAndIntern(UnmangledName)));
227 }
228 
230  LLVMMemoryBufferRef ObjBuffer) {
231  return wrap(unwrap(J)->addObjectFile(
232  *unwrap(JD), std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer))));
233 }
234 
238  return wrap(unwrap(J)->addIRModule(*unwrap(JD), std::move(*unwrap(TSM))));
239 }
240 
242  LLVMOrcJITTargetAddress *Result,
243  const char *Name) {
244  assert(Result && "Result can not be null");
245 
246  auto Sym = unwrap(J)->lookup(Name);
247  if (!Sym) {
248  *Result = 0;
249  return wrap(Sym.takeError());
250  }
251 
252  *Result = Sym->getAddress();
253  return LLVMErrorSuccess;
254 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Types.h:61
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
struct LLVMOrcOpaqueLLJITBuilder * LLVMOrcLLJITBuilderRef
A reference to an orc::LLJITBuilder instance.
Definition: Orc.h:88
void LLVMOrcDisposeJITTargetMachineBuilder(LLVMOrcJITTargetMachineBuilderRef JTMB)
Dispose of a JITTargetMachineBuilder.
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:48
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
std::function< bool(const SymbolStringPtr &)> SymbolPredicate
An LLVMContext together with an associated mutex that can be used to lock the context to prevent conc...
void LLVMOrcDisposeThreadSafeModule(LLVMOrcThreadSafeModuleRef TSM)
Dispose of a ThreadSafeModule.
LLVMErrorRef LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD, LLVMMemoryBufferRef ObjBuffer)
Add a buffer representing an object file to the given JITDylib in the given LLJIT instance...
LLVMOrcExecutionSessionRef LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J)
Get a reference to the ExecutionSession for this LLJIT instance.
struct LLVMOrcQuaqueSymbolStringPoolEntryPtr * LLVMOrcSymbolStringPoolEntryRef
A reference to an orc::SymbolStringPool table entry.
Definition: Orc.h:49
static PoolEntryPtr releaseSymbolStringPtr(SymbolStringPtr S)
char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J)
Returns the global prefix character according to the LLJIT&#39;s DataLayout.
LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J, LLVMOrcJITTargetAddress *Result, const char *Name)
Look up the given symbol in the main JITDylib of the given LLJIT instance.
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:217
void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder(LLVMOrcLLJITBuilderRef Builder, LLVMOrcJITTargetMachineBuilderRef JTMB)
Set the JITTargetMachineBuilder to be used when constructing the LLJIT instance.
LLVMErrorRef LLVMOrcCreateLLJIT(LLVMOrcLLJITRef *Result, LLVMOrcLLJITBuilderRef Builder)
Create an LLJIT instance from an LLJITBuilder.
int(* LLVMOrcSymbolPredicate)(LLVMOrcSymbolStringPoolEntryRef Sym, void *Ctx)
Predicate function for SymbolStringPoolEntries.
Definition: Orc.h:66
void LLVMOrcReleaseSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S)
Reduces the ref-count for of a SymbolStringPool entry.
struct LLVMOpaqueContext * LLVMContextRef
The top-level container for all LLVM global data.
Definition: Types.h:53
LLVMContextRef LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx)
Get a reference to the wrapped LLVMContext.
Pointer to a pooled string representing a symbol name.
struct LLVMOrcOpaqueThreadSafeContext * LLVMOrcThreadSafeContextRef
A reference to an orc::ThreadSafeContext instance.
Definition: Orc.h:72
struct LLVMOpaqueError * LLVMErrorRef
Opaque reference to an error instance.
Definition: Error.h:26
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
#define P(N)
LLVMOrcLLJITBuilderRef LLVMOrcCreateLLJITBuilder(void)
Create an LLJITTargetMachineBuilder.
#define LLVMErrorSuccess
Definition: Error.h:21
LLVMErrorRef LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J)
Dispose of an LLJIT instance.
void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder)
Dispose of an LLVMOrcLLJITBuilderRef.
An LLVM Module together with a shared ThreadSafeContext.
typedefLLVM_C_EXTERN_C_BEGIN struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
Definition: TargetMachine.h:28
assume Assume Builder
static void releasePoolEntry(PoolEntryPtr P)
struct LLVMOrcOpaqueJITDylib * LLVMOrcJITDylibRef
A reference to an orc::JITDylib instance.
Definition: Orc.h:55
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcV2CAPIHelper::PoolEntry, LLVMOrcSymbolStringPoolEntryRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITDylib
void LLVMDisposeTargetMachine(LLVMTargetMachineRef T)
Dispose the LLVMTargetMachineRef instance generated by LLVMCreateTargetMachine.
LLVM_C_EXTERN_C_BEGIN typedef uint64_t LLVMOrcJITTargetAddress
Represents an address in the target process.
Definition: Orc.h:39
A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
Definition: LLJIT.h:36
An ExecutionSession represents a running JIT program.
Definition: Core.h:1088
struct LLVMOrcOpaqueThreadSafeModule * LLVMOrcThreadSafeModuleRef
A reference to an orc::ThreadSafeModule instance.
Definition: Orc.h:77
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:962
static PoolEntryPtr getRawPoolEntryPtr(const SymbolStringPtr &S)
struct LLVMOrcOpaqueExecutionSession * LLVMOrcExecutionSessionRef
A reference to an orc::ExecutionSession instance.
Definition: Orc.h:44
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:212
LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD, LLVMOrcThreadSafeModuleRef TSM)
Add an IR module to the given JITDylib of the given LLJIT instance.
LLVMOrcJITDylibRef LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J)
Return a reference to the Main JITDylib.
void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD, LLVMOrcJITDylibDefinitionGeneratorRef DG)
Add a JITDylib::DefinitionGenerator to the given JITDylib.
Constructs LLJIT instances.
Definition: LLJIT.h:374
LLVMErrorRef LLVMOrcJITTargetMachineBuilderDetectHost(LLVMOrcJITTargetMachineBuilderRef *Result)
Create a JITTargetMachineBuilder by detecting the host.
LLVMOrcThreadSafeModuleRef LLVMOrcCreateNewThreadSafeModule(LLVMModuleRef M, LLVMOrcThreadSafeContextRef TSCtx)
Create a ThreadSafeModule wrapper around the given LLVM module.
void LLVMOrcDisposeJITDylibDefinitionGenerator(LLVMOrcJITDylibDefinitionGeneratorRef DG)
Dispose of a JITDylib::DefinitionGenerator.
struct LLVMOrcOpaqueLLJIT * LLVMOrcLLJITRef
A reference to an orc::LLJIT instance.
Definition: Orc.h:93
LLVMOrcSymbolStringPoolEntryRef LLVMOrcExecutionSessionIntern(LLVMOrcExecutionSessionRef ES, const char *Name)
Intern a string in the ExecutionSession&#39;s SymbolStringPool and return a reference to it...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
aarch64 promote const
LLVMOrcJITTargetMachineBuilderRef LLVMOrcJITTargetMachineBuilderFromTargetMachine(LLVMTargetMachineRef TM)
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
LLVMErrorRef LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(LLVMOrcJITDylibDefinitionGeneratorRef *Result, char GlobalPrefix, LLVMOrcSymbolPredicate Filter, void *FilterCtx)
Get a DynamicLibrarySearchGenerator that will reflect process symbols into the JITDylib.
static Expected< std::unique_ptr< DynamicLibrarySearchGenerator > > GetForCurrentProcess(char GlobalPrefix, SymbolPredicate Allow=SymbolPredicate())
Creates a DynamicLibrarySearchGenerator that searches for symbols in the current process.
struct LLVMOrcOpaqueJITTargetMachineBuilder * LLVMOrcJITTargetMachineBuilderRef
A reference to an orc::JITTargetMachineBuilder instance.
Definition: Orc.h:82
void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx)
Dispose of a ThreadSafeContext.
struct LLVMOrcOpaqueJITDylibDefinitionGenerator * LLVMOrcJITDylibDefinitionGeneratorRef
A reference to an orc::JITDylib::DefinitionGenerator.
Definition: Orc.h:60
const char * LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J)
Return the target triple for this LLJIT instance.
LLVMOrcThreadSafeContextRef LLVMOrcCreateNewThreadSafeContext(void)
Create a ThreadSafeContext containing a new LLVMContext.
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:779
A utility class for building TargetMachines for JITs.
LLVMOrcSymbolStringPoolEntryRef LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName)
Mangles the given string according to the LLJIT instance&#39;s DataLayout, then interns the result in the...