1 /*===---------------- llvm-c/Orc.h - OrcV2 C bindings -----------*- C++ -*-===*\
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
5 |* See https://llvm.org/LICENSE.txt for license information. *|
6 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
8 |*===----------------------------------------------------------------------===*|
10 |* This header declares the C interface to libLLVMOrcJIT.a, which implements *|
11 |* JIT compilation of LLVM IR. Minimal documentation of C API specific issues *|
12 |* (especially memory ownership rules) is provided. Core Orc concepts are *|
13 |* documented in llvm/docs/ORCv2.rst and APIs are documented in the C++ *|
16 |* Many exotic languages can interoperate with C code but have a harder time *|
17 |* with C++ due to name mangling. So in addition to C, this interface enables *|
18 |* tools written in such languages. *|
20 |* Note: This interface is experimental. It is *NOT* stable, and may be *|
21 |* changed without warning. Only C API usage documentation is *|
22 |* provided. See the C++ documentation for all higher level ORC API *|
25 \*===----------------------------------------------------------------------===*/
30 #include "llvm-c/Error.h"
31 #include "llvm-c/TargetMachine.h"
32 #include "llvm-c/Types.h"
37 * Represents an address in the target process.
39 typedef uint64_t LLVMOrcJITTargetAddress;
42 * A reference to an orc::ExecutionSession instance.
44 typedef struct LLVMOrcOpaqueExecutionSession *LLVMOrcExecutionSessionRef;
47 * A reference to an orc::SymbolStringPool table entry.
49 typedef struct LLVMOrcQuaqueSymbolStringPoolEntryPtr
50 *LLVMOrcSymbolStringPoolEntryRef;
53 * A reference to an orc::JITDylib instance.
55 typedef struct LLVMOrcOpaqueJITDylib *LLVMOrcJITDylibRef;
58 * A reference to an orc::JITDylib::DefinitionGenerator.
60 typedef struct LLVMOrcOpaqueJITDylibDefinitionGenerator
61 *LLVMOrcJITDylibDefinitionGeneratorRef;
64 * Predicate function for SymbolStringPoolEntries.
66 typedef int (*LLVMOrcSymbolPredicate)(LLVMOrcSymbolStringPoolEntryRef Sym,
70 * A reference to an orc::ThreadSafeContext instance.
72 typedef struct LLVMOrcOpaqueThreadSafeContext *LLVMOrcThreadSafeContextRef;
75 * A reference to an orc::ThreadSafeModule instance.
77 typedef struct LLVMOrcOpaqueThreadSafeModule *LLVMOrcThreadSafeModuleRef;
80 * A reference to an orc::JITTargetMachineBuilder instance.
82 typedef struct LLVMOrcOpaqueJITTargetMachineBuilder
83 *LLVMOrcJITTargetMachineBuilderRef;
86 * A reference to an orc::LLJITBuilder instance.
88 typedef struct LLVMOrcOpaqueLLJITBuilder *LLVMOrcLLJITBuilderRef;
91 * A reference to an orc::LLJIT instance.
93 typedef struct LLVMOrcOpaqueLLJIT *LLVMOrcLLJITRef;
96 * Intern a string in the ExecutionSession's SymbolStringPool and return a
97 * reference to it. This increments the ref-count of the pool entry, and the
98 * returned value should be released once the client is done with it by
99 * calling LLVMOrReleaseSymbolStringPoolEntry.
101 * Since strings are uniqued within the SymbolStringPool
102 * LLVMOrcSymbolStringPoolEntryRefs can be compared by value to test string
105 * Note that this function does not perform linker-mangling on the string.
107 LLVMOrcSymbolStringPoolEntryRef
108 LLVMOrcExecutionSessionIntern(LLVMOrcExecutionSessionRef ES, const char *Name);
111 * Reduces the ref-count for of a SymbolStringPool entry.
113 void LLVMOrcReleaseSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S);
116 * Dispose of a JITDylib::DefinitionGenerator. This should only be called if
117 * ownership has not been passed to a JITDylib (e.g. because some error
118 * prevented the client from calling LLVMOrcJITDylibAddGenerator).
120 void LLVMOrcDisposeJITDylibDefinitionGenerator(
121 LLVMOrcJITDylibDefinitionGeneratorRef DG);
124 * Add a JITDylib::DefinitionGenerator to the given JITDylib.
126 * The JITDylib will take ownership of the given generator: The client is no
127 * longer responsible for managing its memory.
129 void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD,
130 LLVMOrcJITDylibDefinitionGeneratorRef DG);
133 * Get a DynamicLibrarySearchGenerator that will reflect process symbols into
134 * the JITDylib. On success the resulting generator is owned by the client.
135 * Ownership is typically transferred by adding the instance to a JITDylib
136 * using LLVMOrcJITDylibAddGenerator,
138 * The GlobalPrefix argument specifies the character that appears on the front
139 * of linker-mangled symbols for the target platform (e.g. '_' on MachO).
140 * If non-null, this character will be stripped from the start of all symbol
141 * strings before passing the remaining substring to dlsym.
143 * The optional Filter and Ctx arguments can be used to supply a symbol name
144 * filter: Only symbols for which the filter returns true will be visible to
145 * JIT'd code. If the Filter argument is null then all process symbols will
146 * be visible to JIT'd code. Note that the symbol name passed to the Filter
147 * function is the full mangled symbol: The client is responsible for stripping
148 * the global prefix if present.
150 LLVMErrorRef LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(
151 LLVMOrcJITDylibDefinitionGeneratorRef *Result, char GlobalPrefx,
152 LLVMOrcSymbolPredicate Filter, void *FilterCtx);
155 * Create a ThreadSafeContext containing a new LLVMContext.
157 * Ownership of the underlying ThreadSafeContext data is shared: Clients
158 * can and should dispose of their ThreadSafeContext as soon as they no longer
159 * need to refer to it directly. Other references (e.g. from ThreadSafeModules
160 * will keep the data alive as long as it is needed.
162 LLVMOrcThreadSafeContextRef LLVMOrcCreateNewThreadSafeContext(void);
165 * Get a reference to the wrapped LLVMContext.
168 LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx);
171 * Dispose of a ThreadSafeContext.
173 void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx);
176 * Create a ThreadSafeModule wrapper around the given LLVM module. This takes
177 * ownership of the M argument which should not be disposed of or referenced
178 * after this function returns.
180 * Ownership of the ThreadSafeModule is unique: If it is transferred to the JIT
181 * (e.g. by LLVMOrcLLJITAddLLVMIRModule), in which case the client is no longer
182 * responsible for it. If it is not transferred to the JIT then the client
183 * should call LLVMOrcDisposeThreadSafeModule to dispose of it.
185 LLVMOrcThreadSafeModuleRef
186 LLVMOrcCreateNewThreadSafeModule(LLVMModuleRef M,
187 LLVMOrcThreadSafeContextRef TSCtx);
190 * Dispose of a ThreadSafeModule. This should only be called if ownership has
191 * not been passed to LLJIT (e.g. because some error prevented the client from
192 * adding this to the JIT).
194 void LLVMOrcDisposeThreadSafeModule(LLVMOrcThreadSafeModuleRef TSM);
197 * Create a JITTargetMachineBuilder by detecting the host.
199 * On success the client owns the resulting JITTargetMachineBuilder. It must be
200 * passed to a consuming operation (e.g. LLVMOrcCreateLLJITBuilder) or disposed
201 * of by calling LLVMOrcDisposeJITTargetMachineBuilder.
203 LLVMErrorRef LLVMOrcJITTargetMachineBuilderDetectHost(
204 LLVMOrcJITTargetMachineBuilderRef *Result);
207 * Create a JITTargetMachineBuilder from the given TargetMachine template.
209 * This operation takes ownership of the given TargetMachine and destroys it
210 * before returing. The resulting JITTargetMachineBuilder is owned by the client
211 * and must be passed to a consuming operation (e.g. LLVMOrcCreateLLJITBuilder)
212 * or disposed of by calling LLVMOrcDisposeJITTargetMachineBuilder.
214 LLVMOrcJITTargetMachineBuilderRef
215 LLVMOrcJITTargetMachineBuilderCreateFromTargetMachine(LLVMTargetMachineRef TM);
218 * Dispose of a JITTargetMachineBuilder.
220 void LLVMOrcDisposeJITTargetMachineBuilder(
221 LLVMOrcJITTargetMachineBuilderRef JTMB);
224 * Create an LLJITTargetMachineBuilder.
226 * The client owns the resulting LLJITBuilder and should dispose of it using
227 * LLVMOrcDisposeLLJITBuilder once they are done with it.
229 LLVMOrcLLJITBuilderRef LLVMOrcCreateLLJITBuilder(void);
232 * Dispose of an LLVMOrcLLJITBuilderRef. This should only be called if ownership
233 * has not been passed to LLVMOrcCreateLLJIT (e.g. because some error prevented
234 * that function from being called).
236 void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder);
239 * Set the JITTargetMachineBuilder to be used when constructing the LLJIT
240 * instance. Calling this function is optional: if it is not called then the
241 * LLJITBuilder will use JITTargeTMachineBuilder::detectHost to construct a
242 * JITTargetMachineBuilder.
244 void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder(
245 LLVMOrcLLJITBuilderRef Builder, LLVMOrcJITTargetMachineBuilderRef JTMB);
248 * Create an LLJIT instance from an LLJITBuilder.
250 * This operation takes ownership of the Builder argument: clients should not
251 * dispose of the builder after calling this function (even if the function
252 * returns an error). If a null Builder argument is provided then a
253 * default-constructed LLJITBuilder will be used.
255 * On success the resulting LLJIT instance is uniquely owned by the client and
256 * automatically manages the memory of all JIT'd code and all modules that are
257 * transferred to it (e.g. via LLVMOrcLLJITAddLLVMIRModule). Disposing of the
258 * LLJIT instance will free all memory managed by the JIT, including JIT'd code
259 * and not-yet compiled modules.
261 LLVMErrorRef LLVMOrcCreateLLJIT(LLVMOrcLLJITRef *Result,
262 LLVMOrcLLJITBuilderRef Builder);
265 * Dispose of an LLJIT instance.
267 LLVMErrorRef LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J);
270 * Get a reference to the ExecutionSession for this LLJIT instance.
272 * The ExecutionSession is owned by the LLJIT instance. The client is not
273 * responsible for managing its memory.
275 LLVMOrcExecutionSessionRef LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J);
278 * Return a reference to the Main JITDylib.
280 * The JITDylib is owned by the LLJIT instance. The client is not responsible
281 * for managing its memory.
283 LLVMOrcJITDylibRef LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J);
286 * Return the target triple for this LLJIT instance. This string is owned by
287 * the LLJIT instance and should not be freed by the client.
289 const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J);
292 * Returns the global prefix character according to the LLJIT's DataLayout.
294 char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J);
297 * Mangles the given string according to the LLJIT instance's DataLayout, then
298 * interns the result in the SymbolStringPool and returns a reference to the
299 * pool entry. Clients should call LLVMOrcReleaseSymbolStringPoolEntry to
300 * decrement the ref-count on the pool entry once they are finished with this
303 LLVMOrcSymbolStringPoolEntryRef
304 LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName);
307 * Add a buffer representing an object file to the given JITDylib in the given
308 * LLJIT instance. This operation transfers ownership of the buffer to the
309 * LLJIT instance. The buffer should not be disposed of or referenced once this
312 LLVMErrorRef LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD,
313 LLVMMemoryBufferRef ObjBuffer);
316 * Add an IR module to the given JITDylib of the given LLJIT instance. This
317 * operation transfers ownership of the TSM argument to the LLJIT instance.
318 * The TSM argument should not be 3disposed of or referenced once this
321 LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J,
322 LLVMOrcJITDylibRef JD,
323 LLVMOrcThreadSafeModuleRef TSM);
325 * Look up the given symbol in the main JITDylib of the given LLJIT instance.
327 * This operation does not take ownership of the Name argument.
329 LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J,
330 LLVMOrcJITTargetAddress *Result,
335 #endif /* LLVM_C_ORC_H */