xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm-c/LLJIT.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1*5f757f3fSDimitry Andric /*===----------- llvm-c/LLJIT.h - OrcV2 LLJIT C bindings ----------*- C -*-===*\
2e8d8bef9SDimitry Andric |*                                                                            *|
3e8d8bef9SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM          *|
4e8d8bef9SDimitry Andric |* Exceptions.                                                                *|
5e8d8bef9SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.                  *|
6e8d8bef9SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception                    *|
7e8d8bef9SDimitry Andric |*                                                                            *|
8e8d8bef9SDimitry Andric |*===----------------------------------------------------------------------===*|
9e8d8bef9SDimitry Andric |*                                                                            *|
10e8d8bef9SDimitry Andric |* This header declares the C interface to the LLJIT class in                 *|
11e8d8bef9SDimitry Andric |* libLLVMOrcJIT.a, which provides a simple MCJIT-like ORC JIT.               *|
12e8d8bef9SDimitry Andric |*                                                                            *|
13e8d8bef9SDimitry Andric |* Many exotic languages can interoperate with C code but have a harder time  *|
14e8d8bef9SDimitry Andric |* with C++ due to name mangling. So in addition to C, this interface enables *|
15e8d8bef9SDimitry Andric |* tools written in such languages.                                           *|
16e8d8bef9SDimitry Andric |*                                                                            *|
17e8d8bef9SDimitry Andric |* Note: This interface is experimental. It is *NOT* stable, and may be       *|
18e8d8bef9SDimitry Andric |*       changed without warning. Only C API usage documentation is           *|
19e8d8bef9SDimitry Andric |*       provided. See the C++ documentation for all higher level ORC API     *|
20e8d8bef9SDimitry Andric |*       details.                                                             *|
21e8d8bef9SDimitry Andric |*                                                                            *|
22e8d8bef9SDimitry Andric \*===----------------------------------------------------------------------===*/
23e8d8bef9SDimitry Andric 
24e8d8bef9SDimitry Andric #ifndef LLVM_C_LLJIT_H
25e8d8bef9SDimitry Andric #define LLVM_C_LLJIT_H
26e8d8bef9SDimitry Andric 
27e8d8bef9SDimitry Andric #include "llvm-c/Error.h"
28e8d8bef9SDimitry Andric #include "llvm-c/Orc.h"
29e8d8bef9SDimitry Andric #include "llvm-c/TargetMachine.h"
30e8d8bef9SDimitry Andric #include "llvm-c/Types.h"
31e8d8bef9SDimitry Andric 
32e8d8bef9SDimitry Andric LLVM_C_EXTERN_C_BEGIN
33e8d8bef9SDimitry Andric 
34e8d8bef9SDimitry Andric /**
35349cc55cSDimitry Andric  * @defgroup LLVMCExecutionEngineLLJIT LLJIT
36349cc55cSDimitry Andric  * @ingroup LLVMCExecutionEngine
37349cc55cSDimitry Andric  *
38349cc55cSDimitry Andric  * @{
39349cc55cSDimitry Andric  */
40349cc55cSDimitry Andric 
41349cc55cSDimitry Andric /**
42e8d8bef9SDimitry Andric  * A function for constructing an ObjectLinkingLayer instance to be used
43e8d8bef9SDimitry Andric  * by an LLJIT instance.
44e8d8bef9SDimitry Andric  *
45e8d8bef9SDimitry Andric  * Clients can call LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator to
46e8d8bef9SDimitry Andric  * set the creator function to use when constructing an LLJIT instance.
47e8d8bef9SDimitry Andric  * This can be used to override the default linking layer implementation
48e8d8bef9SDimitry Andric  * that would otherwise be chosen by LLJITBuilder.
49e8d8bef9SDimitry Andric  *
50e8d8bef9SDimitry Andric  * Object linking layers returned by this function will become owned by the
51e8d8bef9SDimitry Andric  * LLJIT instance. The client is not responsible for managing their lifetimes
52e8d8bef9SDimitry Andric  * after the function returns.
53e8d8bef9SDimitry Andric  */
54e8d8bef9SDimitry Andric typedef LLVMOrcObjectLayerRef (
55e8d8bef9SDimitry Andric     *LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction)(
56e8d8bef9SDimitry Andric     void *Ctx, LLVMOrcExecutionSessionRef ES, const char *Triple);
57e8d8bef9SDimitry Andric 
58e8d8bef9SDimitry Andric /**
59e8d8bef9SDimitry Andric  * A reference to an orc::LLJITBuilder instance.
60e8d8bef9SDimitry Andric  */
61e8d8bef9SDimitry Andric typedef struct LLVMOrcOpaqueLLJITBuilder *LLVMOrcLLJITBuilderRef;
62e8d8bef9SDimitry Andric 
63e8d8bef9SDimitry Andric /**
64e8d8bef9SDimitry Andric  * A reference to an orc::LLJIT instance.
65e8d8bef9SDimitry Andric  */
66e8d8bef9SDimitry Andric typedef struct LLVMOrcOpaqueLLJIT *LLVMOrcLLJITRef;
67e8d8bef9SDimitry Andric 
68e8d8bef9SDimitry Andric /**
69e8d8bef9SDimitry Andric  * Create an LLVMOrcLLJITBuilder.
70e8d8bef9SDimitry Andric  *
71e8d8bef9SDimitry Andric  * The client owns the resulting LLJITBuilder and should dispose of it using
72e8d8bef9SDimitry Andric  * LLVMOrcDisposeLLJITBuilder once they are done with it.
73e8d8bef9SDimitry Andric  */
74e8d8bef9SDimitry Andric LLVMOrcLLJITBuilderRef LLVMOrcCreateLLJITBuilder(void);
75e8d8bef9SDimitry Andric 
76e8d8bef9SDimitry Andric /**
77e8d8bef9SDimitry Andric  * Dispose of an LLVMOrcLLJITBuilderRef. This should only be called if ownership
78e8d8bef9SDimitry Andric  * has not been passed to LLVMOrcCreateLLJIT (e.g. because some error prevented
79e8d8bef9SDimitry Andric  * that function from being called).
80e8d8bef9SDimitry Andric  */
81e8d8bef9SDimitry Andric void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder);
82e8d8bef9SDimitry Andric 
83e8d8bef9SDimitry Andric /**
84e8d8bef9SDimitry Andric  * Set the JITTargetMachineBuilder to be used when constructing the LLJIT
85e8d8bef9SDimitry Andric  * instance. Calling this function is optional: if it is not called then the
86e8d8bef9SDimitry Andric  * LLJITBuilder will use JITTargeTMachineBuilder::detectHost to construct a
87e8d8bef9SDimitry Andric  * JITTargetMachineBuilder.
88fe6060f1SDimitry Andric  *
89fe6060f1SDimitry Andric  * This function takes ownership of the JTMB argument: clients should not
90fe6060f1SDimitry Andric  * dispose of the JITTargetMachineBuilder after calling this function.
91e8d8bef9SDimitry Andric  */
92e8d8bef9SDimitry Andric void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder(
93e8d8bef9SDimitry Andric     LLVMOrcLLJITBuilderRef Builder, LLVMOrcJITTargetMachineBuilderRef JTMB);
94e8d8bef9SDimitry Andric 
95e8d8bef9SDimitry Andric /**
96e8d8bef9SDimitry Andric  * Set an ObjectLinkingLayer creator function for this LLJIT instance.
97e8d8bef9SDimitry Andric  */
98e8d8bef9SDimitry Andric void LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator(
99e8d8bef9SDimitry Andric     LLVMOrcLLJITBuilderRef Builder,
100e8d8bef9SDimitry Andric     LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction F, void *Ctx);
101e8d8bef9SDimitry Andric 
102e8d8bef9SDimitry Andric /**
103e8d8bef9SDimitry Andric  * Create an LLJIT instance from an LLJITBuilder.
104e8d8bef9SDimitry Andric  *
105e8d8bef9SDimitry Andric  * This operation takes ownership of the Builder argument: clients should not
106e8d8bef9SDimitry Andric  * dispose of the builder after calling this function (even if the function
107e8d8bef9SDimitry Andric  * returns an error). If a null Builder argument is provided then a
108e8d8bef9SDimitry Andric  * default-constructed LLJITBuilder will be used.
109e8d8bef9SDimitry Andric  *
110e8d8bef9SDimitry Andric  * On success the resulting LLJIT instance is uniquely owned by the client and
111e8d8bef9SDimitry Andric  * automatically manages the memory of all JIT'd code and all modules that are
112e8d8bef9SDimitry Andric  * transferred to it (e.g. via LLVMOrcLLJITAddLLVMIRModule). Disposing of the
113e8d8bef9SDimitry Andric  * LLJIT instance will free all memory managed by the JIT, including JIT'd code
114e8d8bef9SDimitry Andric  * and not-yet compiled modules.
115e8d8bef9SDimitry Andric  */
116e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcCreateLLJIT(LLVMOrcLLJITRef *Result,
117e8d8bef9SDimitry Andric                                 LLVMOrcLLJITBuilderRef Builder);
118e8d8bef9SDimitry Andric 
119e8d8bef9SDimitry Andric /**
120e8d8bef9SDimitry Andric  * Dispose of an LLJIT instance.
121e8d8bef9SDimitry Andric  */
122e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J);
123e8d8bef9SDimitry Andric 
124e8d8bef9SDimitry Andric /**
125e8d8bef9SDimitry Andric  * Get a reference to the ExecutionSession for this LLJIT instance.
126e8d8bef9SDimitry Andric  *
127e8d8bef9SDimitry Andric  * The ExecutionSession is owned by the LLJIT instance. The client is not
128e8d8bef9SDimitry Andric  * responsible for managing its memory.
129e8d8bef9SDimitry Andric  */
130e8d8bef9SDimitry Andric LLVMOrcExecutionSessionRef LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J);
131e8d8bef9SDimitry Andric 
132e8d8bef9SDimitry Andric /**
133e8d8bef9SDimitry Andric  * Return a reference to the Main JITDylib.
134e8d8bef9SDimitry Andric  *
135e8d8bef9SDimitry Andric  * The JITDylib is owned by the LLJIT instance. The client is not responsible
136e8d8bef9SDimitry Andric  * for managing its memory.
137e8d8bef9SDimitry Andric  */
138e8d8bef9SDimitry Andric LLVMOrcJITDylibRef LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J);
139e8d8bef9SDimitry Andric 
140e8d8bef9SDimitry Andric /**
141e8d8bef9SDimitry Andric  * Return the target triple for this LLJIT instance. This string is owned by
142e8d8bef9SDimitry Andric  * the LLJIT instance and should not be freed by the client.
143e8d8bef9SDimitry Andric  */
144e8d8bef9SDimitry Andric const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J);
145e8d8bef9SDimitry Andric 
146e8d8bef9SDimitry Andric /**
147e8d8bef9SDimitry Andric  * Returns the global prefix character according to the LLJIT's DataLayout.
148e8d8bef9SDimitry Andric  */
149e8d8bef9SDimitry Andric char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J);
150e8d8bef9SDimitry Andric 
151e8d8bef9SDimitry Andric /**
152e8d8bef9SDimitry Andric  * Mangles the given string according to the LLJIT instance's DataLayout, then
153e8d8bef9SDimitry Andric  * interns the result in the SymbolStringPool and returns a reference to the
154e8d8bef9SDimitry Andric  * pool entry. Clients should call LLVMOrcReleaseSymbolStringPoolEntry to
155e8d8bef9SDimitry Andric  * decrement the ref-count on the pool entry once they are finished with this
156e8d8bef9SDimitry Andric  * value.
157e8d8bef9SDimitry Andric  */
158e8d8bef9SDimitry Andric LLVMOrcSymbolStringPoolEntryRef
159e8d8bef9SDimitry Andric LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName);
160e8d8bef9SDimitry Andric 
161e8d8bef9SDimitry Andric /**
162e8d8bef9SDimitry Andric  * Add a buffer representing an object file to the given JITDylib in the given
163e8d8bef9SDimitry Andric  * LLJIT instance. This operation transfers ownership of the buffer to the
164e8d8bef9SDimitry Andric  * LLJIT instance. The buffer should not be disposed of or referenced once this
165e8d8bef9SDimitry Andric  * function returns.
166e8d8bef9SDimitry Andric  *
167e8d8bef9SDimitry Andric  * Resources associated with the given object will be tracked by the given
168e8d8bef9SDimitry Andric  * JITDylib's default resource tracker.
169e8d8bef9SDimitry Andric  */
170e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD,
171e8d8bef9SDimitry Andric                                        LLVMMemoryBufferRef ObjBuffer);
172e8d8bef9SDimitry Andric 
173e8d8bef9SDimitry Andric /**
174e8d8bef9SDimitry Andric  * Add a buffer representing an object file to the given ResourceTracker's
175e8d8bef9SDimitry Andric  * JITDylib in the given LLJIT instance. This operation transfers ownership of
176e8d8bef9SDimitry Andric  * the buffer to the LLJIT instance. The buffer should not be disposed of or
177e8d8bef9SDimitry Andric  * referenced once this function returns.
178e8d8bef9SDimitry Andric  *
179e8d8bef9SDimitry Andric  * Resources associated with the given object will be tracked by ResourceTracker
180e8d8bef9SDimitry Andric  * RT.
181e8d8bef9SDimitry Andric  */
182e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcLLJITRef J,
183e8d8bef9SDimitry Andric                                              LLVMOrcResourceTrackerRef RT,
184e8d8bef9SDimitry Andric                                              LLVMMemoryBufferRef ObjBuffer);
185e8d8bef9SDimitry Andric 
186e8d8bef9SDimitry Andric /**
187e8d8bef9SDimitry Andric  * Add an IR module to the given JITDylib in the given LLJIT instance. This
188e8d8bef9SDimitry Andric  * operation transfers ownership of the TSM argument to the LLJIT instance.
189e8d8bef9SDimitry Andric  * The TSM argument should not be disposed of or referenced once this
190e8d8bef9SDimitry Andric  * function returns.
191e8d8bef9SDimitry Andric  *
192e8d8bef9SDimitry Andric  * Resources associated with the given Module will be tracked by the given
193e8d8bef9SDimitry Andric  * JITDylib's default resource tracker.
194e8d8bef9SDimitry Andric  */
195e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J,
196e8d8bef9SDimitry Andric                                          LLVMOrcJITDylibRef JD,
197e8d8bef9SDimitry Andric                                          LLVMOrcThreadSafeModuleRef TSM);
198e8d8bef9SDimitry Andric 
199e8d8bef9SDimitry Andric /**
200e8d8bef9SDimitry Andric  * Add an IR module to the given ResourceTracker's JITDylib in the given LLJIT
201e8d8bef9SDimitry Andric  * instance. This operation transfers ownership of the TSM argument to the LLJIT
202e8d8bef9SDimitry Andric  * instance. The TSM argument should not be disposed of or referenced once this
203e8d8bef9SDimitry Andric  * function returns.
204e8d8bef9SDimitry Andric  *
205e8d8bef9SDimitry Andric  * Resources associated with the given Module will be tracked by ResourceTracker
206e8d8bef9SDimitry Andric  * RT.
207e8d8bef9SDimitry Andric  */
208e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddLLVMIRModuleWithRT(LLVMOrcLLJITRef J,
209e8d8bef9SDimitry Andric                                                LLVMOrcResourceTrackerRef JD,
210e8d8bef9SDimitry Andric                                                LLVMOrcThreadSafeModuleRef TSM);
211e8d8bef9SDimitry Andric 
212e8d8bef9SDimitry Andric /**
213e8d8bef9SDimitry Andric  * Look up the given symbol in the main JITDylib of the given LLJIT instance.
214e8d8bef9SDimitry Andric  *
215e8d8bef9SDimitry Andric  * This operation does not take ownership of the Name argument.
216e8d8bef9SDimitry Andric  */
217e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J,
218fe6060f1SDimitry Andric                                 LLVMOrcExecutorAddress *Result,
219e8d8bef9SDimitry Andric                                 const char *Name);
220e8d8bef9SDimitry Andric 
221fe6060f1SDimitry Andric /**
222fe6060f1SDimitry Andric  * Returns a non-owning reference to the LLJIT instance's object linking layer.
223fe6060f1SDimitry Andric  */
224fe6060f1SDimitry Andric LLVMOrcObjectLayerRef LLVMOrcLLJITGetObjLinkingLayer(LLVMOrcLLJITRef J);
225fe6060f1SDimitry Andric 
226fe6060f1SDimitry Andric /**
227fe6060f1SDimitry Andric  * Returns a non-owning reference to the LLJIT instance's object linking layer.
228fe6060f1SDimitry Andric  */
229fe6060f1SDimitry Andric LLVMOrcObjectTransformLayerRef
230fe6060f1SDimitry Andric LLVMOrcLLJITGetObjTransformLayer(LLVMOrcLLJITRef J);
231fe6060f1SDimitry Andric 
232fe6060f1SDimitry Andric /**
233fe6060f1SDimitry Andric  * Returns a non-owning reference to the LLJIT instance's IR transform layer.
234fe6060f1SDimitry Andric  */
235fe6060f1SDimitry Andric LLVMOrcIRTransformLayerRef LLVMOrcLLJITGetIRTransformLayer(LLVMOrcLLJITRef J);
236fe6060f1SDimitry Andric 
237fe6060f1SDimitry Andric /**
238fe6060f1SDimitry Andric  * Get the LLJIT instance's default data layout string.
239fe6060f1SDimitry Andric  *
240fe6060f1SDimitry Andric  * This string is owned by the LLJIT instance and does not need to be freed
241fe6060f1SDimitry Andric  * by the caller.
242fe6060f1SDimitry Andric  */
243fe6060f1SDimitry Andric const char *LLVMOrcLLJITGetDataLayoutStr(LLVMOrcLLJITRef J);
244fe6060f1SDimitry Andric 
245349cc55cSDimitry Andric /**
246349cc55cSDimitry Andric  * @}
247349cc55cSDimitry Andric  */
248349cc55cSDimitry Andric 
249e8d8bef9SDimitry Andric LLVM_C_EXTERN_C_END
250e8d8bef9SDimitry Andric 
251e8d8bef9SDimitry Andric #endif /* LLVM_C_LLJIT_H */
252