llvmlite 0.46.0b1__cp313-cp313-macosx_11_0_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of llvmlite might be problematic. Click here for more details.

Files changed (45) hide show
  1. llvmlite/__init__.py +11 -0
  2. llvmlite/_version.py +11 -0
  3. llvmlite/binding/__init__.py +18 -0
  4. llvmlite/binding/analysis.py +69 -0
  5. llvmlite/binding/common.py +34 -0
  6. llvmlite/binding/config.py +143 -0
  7. llvmlite/binding/context.py +31 -0
  8. llvmlite/binding/dylib.py +45 -0
  9. llvmlite/binding/executionengine.py +330 -0
  10. llvmlite/binding/ffi.py +395 -0
  11. llvmlite/binding/initfini.py +85 -0
  12. llvmlite/binding/libllvmlite.dylib +0 -0
  13. llvmlite/binding/linker.py +20 -0
  14. llvmlite/binding/module.py +349 -0
  15. llvmlite/binding/newpassmanagers.py +1049 -0
  16. llvmlite/binding/object_file.py +82 -0
  17. llvmlite/binding/options.py +17 -0
  18. llvmlite/binding/orcjit.py +342 -0
  19. llvmlite/binding/targets.py +462 -0
  20. llvmlite/binding/typeref.py +267 -0
  21. llvmlite/binding/value.py +632 -0
  22. llvmlite/ir/__init__.py +11 -0
  23. llvmlite/ir/_utils.py +80 -0
  24. llvmlite/ir/builder.py +1120 -0
  25. llvmlite/ir/context.py +20 -0
  26. llvmlite/ir/instructions.py +920 -0
  27. llvmlite/ir/module.py +256 -0
  28. llvmlite/ir/transforms.py +64 -0
  29. llvmlite/ir/types.py +730 -0
  30. llvmlite/ir/values.py +1217 -0
  31. llvmlite/tests/__init__.py +57 -0
  32. llvmlite/tests/__main__.py +3 -0
  33. llvmlite/tests/customize.py +407 -0
  34. llvmlite/tests/refprune_proto.py +330 -0
  35. llvmlite/tests/test_binding.py +3155 -0
  36. llvmlite/tests/test_ir.py +3095 -0
  37. llvmlite/tests/test_refprune.py +574 -0
  38. llvmlite/tests/test_valuerepr.py +60 -0
  39. llvmlite/utils.py +29 -0
  40. llvmlite-0.46.0b1.dist-info/METADATA +145 -0
  41. llvmlite-0.46.0b1.dist-info/RECORD +45 -0
  42. llvmlite-0.46.0b1.dist-info/WHEEL +5 -0
  43. llvmlite-0.46.0b1.dist-info/licenses/LICENSE +24 -0
  44. llvmlite-0.46.0b1.dist-info/licenses/LICENSE.thirdparty +225 -0
  45. llvmlite-0.46.0b1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,82 @@
1
+ from llvmlite.binding import ffi
2
+ from ctypes import (c_bool, c_char_p, c_char, c_size_t, string_at, c_uint64,
3
+ POINTER)
4
+
5
+
6
+ class SectionIteratorRef(ffi.ObjectRef):
7
+ def name(self):
8
+ return ffi.lib.LLVMPY_GetSectionName(self)
9
+
10
+ def is_text(self):
11
+ return ffi.lib.LLVMPY_IsSectionText(self)
12
+
13
+ def size(self):
14
+ return ffi.lib.LLVMPY_GetSectionSize(self)
15
+
16
+ def address(self):
17
+ return ffi.lib.LLVMPY_GetSectionAddress(self)
18
+
19
+ def data(self):
20
+ return string_at(ffi.lib.LLVMPY_GetSectionContents(self), self.size())
21
+
22
+ def is_end(self, object_file):
23
+ return ffi.lib.LLVMPY_IsSectionIteratorAtEnd(object_file, self)
24
+
25
+ def next(self):
26
+ ffi.lib.LLVMPY_MoveToNextSection(self)
27
+
28
+ def _dispose(self):
29
+ ffi.lib.LLVMPY_DisposeSectionIterator(self)
30
+
31
+
32
+ class ObjectFileRef(ffi.ObjectRef):
33
+ @classmethod
34
+ def from_data(cls, data):
35
+ return cls(ffi.lib.LLVMPY_CreateObjectFile(data, len(data)))
36
+
37
+ @classmethod
38
+ def from_path(cls, path):
39
+ with open(path, 'rb') as f:
40
+ data = f.read()
41
+ return cls(ffi.lib.LLVMPY_CreateObjectFile(data, len(data)))
42
+
43
+ def sections(self):
44
+ it = SectionIteratorRef(ffi.lib.LLVMPY_GetSections(self))
45
+ while not it.is_end(self):
46
+ yield it
47
+ it.next()
48
+
49
+ def _dispose(self):
50
+ ffi.lib.LLVMPY_DisposeObjectFile(self)
51
+
52
+
53
+ ffi.lib.LLVMPY_CreateObjectFile.argtypes = [c_char_p, c_size_t]
54
+ ffi.lib.LLVMPY_CreateObjectFile.restype = ffi.LLVMObjectFileRef
55
+
56
+ ffi.lib.LLVMPY_DisposeObjectFile.argtypes = [ffi.LLVMObjectFileRef]
57
+
58
+ ffi.lib.LLVMPY_GetSections.argtypes = [ffi.LLVMObjectFileRef]
59
+ ffi.lib.LLVMPY_GetSections.restype = ffi.LLVMSectionIteratorRef
60
+
61
+ ffi.lib.LLVMPY_DisposeSectionIterator.argtypes = [ffi.LLVMSectionIteratorRef]
62
+
63
+ ffi.lib.LLVMPY_MoveToNextSection.argtypes = [ffi.LLVMSectionIteratorRef]
64
+
65
+ ffi.lib.LLVMPY_IsSectionIteratorAtEnd.argtypes = [
66
+ ffi.LLVMObjectFileRef, ffi.LLVMSectionIteratorRef]
67
+ ffi.lib.LLVMPY_IsSectionIteratorAtEnd.restype = c_bool
68
+
69
+ ffi.lib.LLVMPY_GetSectionName.argtypes = [ffi.LLVMSectionIteratorRef]
70
+ ffi.lib.LLVMPY_GetSectionName.restype = c_char_p
71
+
72
+ ffi.lib.LLVMPY_GetSectionSize.argtypes = [ffi.LLVMSectionIteratorRef]
73
+ ffi.lib.LLVMPY_GetSectionSize.restype = c_uint64
74
+
75
+ ffi.lib.LLVMPY_GetSectionAddress.argtypes = [ffi.LLVMSectionIteratorRef]
76
+ ffi.lib.LLVMPY_GetSectionAddress.restype = c_uint64
77
+
78
+ ffi.lib.LLVMPY_GetSectionContents.argtypes = [ffi.LLVMSectionIteratorRef]
79
+ ffi.lib.LLVMPY_GetSectionContents.restype = POINTER(c_char)
80
+
81
+ ffi.lib.LLVMPY_IsSectionText.argtypes = [ffi.LLVMSectionIteratorRef]
82
+ ffi.lib.LLVMPY_IsSectionText.restype = c_bool
@@ -0,0 +1,17 @@
1
+ from llvmlite.binding import ffi
2
+ from llvmlite.binding.common import _encode_string
3
+ from ctypes import c_char_p
4
+
5
+
6
+ def set_option(name, option):
7
+ """
8
+ Set the given LLVM "command-line" option.
9
+
10
+ For example set_option("test", "-debug-pass=Structure") would display
11
+ all optimization passes when generating code.
12
+ """
13
+ ffi.lib.LLVMPY_SetCommandLine(_encode_string(name),
14
+ _encode_string(option))
15
+
16
+
17
+ ffi.lib.LLVMPY_SetCommandLine.argtypes = [c_char_p, c_char_p]
@@ -0,0 +1,342 @@
1
+ import ctypes
2
+ from ctypes import POINTER, c_bool, c_char_p, c_uint8, c_uint64, c_size_t
3
+
4
+ from llvmlite.binding import ffi, targets
5
+
6
+
7
+ class _LinkElement(ctypes.Structure):
8
+ _fields_ = [("element_kind", c_uint8),
9
+ ("value", c_char_p),
10
+ ("value_len", c_size_t)]
11
+
12
+
13
+ class _SymbolAddress(ctypes.Structure):
14
+ _fields_ = [("name", c_char_p), ("address", c_uint64)]
15
+
16
+
17
+ class JITLibraryBuilder:
18
+ """
19
+ Create a library for linking by OrcJIT
20
+
21
+ OrcJIT operates like a linker: a number of compilation units and
22
+ dependencies are collected together and linked into a single dynamic library
23
+ that can export functions to other libraries or to be consumed directly as
24
+ entry points into JITted code. The native OrcJIT has a lot of memory
25
+ management complications so this API is designed to work well with Python's
26
+ garbage collection.
27
+
28
+ The creation of a new library is a bit like a linker command line where
29
+ compilation units, mostly as LLVM IR, and previously constructed libraries
30
+ are linked together, then loaded into memory, and the addresses of exported
31
+ symbols are extracted. Any static initializers are run and the exported
32
+ addresses and a resource tracker is produced. As long as the resource
33
+ tracker is referenced somewhere in Python, the exported addresses will be
34
+ valid. Once the resource tracker is garbage collected, the static
35
+ destructors will run and library will be unloaded from memory.
36
+ """
37
+ def __init__(self):
38
+ self.__entries = []
39
+ self.__exports = set()
40
+ self.__imports = {}
41
+
42
+ def add_ir(self, llvmir):
43
+ """
44
+ Adds a compilation unit to the library using LLVM IR as the input
45
+ format.
46
+
47
+ This takes a string or an object that can be converted to a string,
48
+ including IRBuilder, that contains LLVM IR.
49
+ """
50
+ self.__entries.append((0, str(llvmir).encode('utf-8')))
51
+ return self
52
+
53
+ def add_native_assembly(self, asm):
54
+ """
55
+ Adds a compilation unit to the library using native assembly as the
56
+ input format.
57
+
58
+ This takes a string or an object that can be converted to a string that
59
+ contains native assembly, which will be
60
+ parsed by LLVM.
61
+ """
62
+ self.__entries.append((1, str(asm).encode('utf-8')))
63
+ return self
64
+
65
+ def add_object_img(self, data):
66
+ """
67
+ Adds a compilation unit to the library using pre-compiled object code.
68
+
69
+ This takes the bytes of the contents of an object artifact which will be
70
+ loaded by LLVM.
71
+ """
72
+ self.__entries.append((2, bytes(data)))
73
+ return self
74
+
75
+ def add_object_file(self, file_path):
76
+ """
77
+ Adds a compilation unit to the library using pre-compiled object file.
78
+
79
+ This takes a string or path-like object that references an object file
80
+ which will be loaded by LLVM.
81
+ """
82
+ with open(file_path, "rb") as f:
83
+ self.__entries.append((2, f.read()))
84
+ return self
85
+
86
+ def add_jit_library(self, name):
87
+ """
88
+ Adds an existing JIT library as prerequisite.
89
+
90
+ The name of the library must match the one provided in a previous link
91
+ command.
92
+ """
93
+ self.__entries.append((3, str(name).encode('utf-8')))
94
+ return self
95
+
96
+ def add_current_process(self):
97
+ """
98
+ Allows the JITted library to access symbols in the current binary.
99
+
100
+ That is, it allows exporting the current binary's symbols, including
101
+ loaded libraries, as imports to the JITted
102
+ library.
103
+ """
104
+ self.__entries.append((3, b''))
105
+ return self
106
+
107
+ def import_symbol(self, name, address):
108
+ """
109
+ Register the *address* of global symbol *name*. This will make
110
+ it usable (e.g. callable) from LLVM-compiled functions.
111
+ """
112
+ self.__imports[str(name)] = c_uint64(address)
113
+ return self
114
+
115
+ def export_symbol(self, name):
116
+ """
117
+ During linking, extract the address of a symbol that was defined in one
118
+ of the compilation units.
119
+
120
+ This allows getting symbols, functions or global variables, out of the
121
+ JIT linked library. The addresses will be
122
+ available when the link method is called.
123
+ """
124
+ self.__exports.add(str(name))
125
+ return self
126
+
127
+ def link(self, lljit, library_name):
128
+ """
129
+ Link all the current compilation units into a JITted library and extract
130
+ the address of exported symbols.
131
+
132
+ An instance of the OrcJIT instance must be provided and this will be the
133
+ scope that is used to find other JITted libraries that are dependencies
134
+ and also be the place where this library will be defined.
135
+
136
+ After linking, the method will return a resource tracker that keeps the
137
+ library alive. This tracker also knows the addresses of any exported
138
+ symbols that were requested.
139
+
140
+ The addresses will be valid as long as the resource tracker is
141
+ referenced.
142
+
143
+ When the resource tracker is destroyed, the library will be cleaned up,
144
+ however, the name of the library cannot be reused.
145
+ """
146
+ assert not lljit.closed, "Cannot add to closed JIT"
147
+ encoded_library_name = str(library_name).encode('utf-8')
148
+ assert len(encoded_library_name) > 0, "Library cannot be empty"
149
+ elements = (_LinkElement * len(self.__entries))()
150
+ for idx, (kind, value) in enumerate(self.__entries):
151
+ elements[idx].element_kind = c_uint8(kind)
152
+ elements[idx].value = c_char_p(value)
153
+ elements[idx].value_len = c_size_t(len(value))
154
+ exports = (_SymbolAddress * len(self.__exports))()
155
+ for idx, name in enumerate(self.__exports):
156
+ exports[idx].name = name.encode('utf-8')
157
+
158
+ imports = (_SymbolAddress * len(self.__imports))()
159
+ for idx, (name, addr) in enumerate(self.__imports.items()):
160
+ imports[idx].name = name.encode('utf-8')
161
+ imports[idx].address = addr
162
+
163
+ with ffi.OutputString() as outerr:
164
+ tracker = lljit._capi.LLVMPY_LLJIT_Link(
165
+ lljit._ptr,
166
+ encoded_library_name,
167
+ elements,
168
+ len(self.__entries),
169
+ imports,
170
+ len(self.__imports),
171
+ exports,
172
+ len(self.__exports),
173
+ outerr)
174
+ if not tracker:
175
+ raise RuntimeError(str(outerr))
176
+ return ResourceTracker(tracker,
177
+ library_name,
178
+ {name: exports[idx].address
179
+ for idx, name in enumerate(self.__exports)})
180
+
181
+
182
+ class ResourceTracker(ffi.ObjectRef):
183
+ """
184
+ A resource tracker is created for each loaded JIT library and keeps the
185
+ module alive.
186
+
187
+ OrcJIT supports unloading libraries that are no longer used. This resource
188
+ tracker should be stored in any object that reference functions or constants
189
+ for a JITted library. When all references to the resource tracker are
190
+ dropped, this will trigger LLVM to unload the library and destroy any
191
+ functions.
192
+
193
+ Failure to keep resource trackers while calling a function or accessing a
194
+ symbol can result in crashes or memory corruption.
195
+
196
+ LLVM internally tracks references between different libraries, so only
197
+ "leaf" libraries need to be tracked.
198
+ """
199
+ def __init__(self, ptr, name, addresses):
200
+ self.__addresses = addresses
201
+ self.__name = name
202
+ ffi.ObjectRef.__init__(self, ptr)
203
+
204
+ def __getitem__(self, item):
205
+ """
206
+ Get the address of an exported symbol as an integer
207
+ """
208
+ return self.__addresses[item]
209
+
210
+ @property
211
+ def name(self):
212
+ return self.__name
213
+
214
+ def _dispose(self):
215
+ with ffi.OutputString() as outerr:
216
+ if self._capi.LLVMPY_LLJIT_Dylib_Tracker_Dispose(self, outerr):
217
+ raise RuntimeError(str(outerr))
218
+
219
+
220
+ class LLJIT(ffi.ObjectRef):
221
+ """
222
+ A OrcJIT-based LLVM JIT engine that can compile and run LLVM IR as a
223
+ collection of JITted dynamic libraries
224
+
225
+ The C++ OrcJIT API has a lot of memory ownership patterns that do not work
226
+ with Python. This API attempts to provide ones that are safe at the expense
227
+ of some features. Each LLJIT instance is a collection of JIT-compiled
228
+ libraries. In the C++ API, there is a "main" library; this API does not
229
+ provide access to the main library. Use the JITLibraryBuilder to create a
230
+ new named library instead.
231
+ """
232
+ def __init__(self, ptr):
233
+ self._td = None
234
+ ffi.ObjectRef.__init__(self, ptr)
235
+
236
+ def lookup(self, dylib, fn):
237
+ """
238
+ Find a function in this dynamic library and construct a new tracking
239
+ object for it
240
+
241
+ If the library or function do not exist, an exception will occur.
242
+
243
+ Parameters
244
+ ----------
245
+ dylib : str or None
246
+ the name of the library containing the symbol
247
+ fn : str
248
+ the name of the function to get
249
+ """
250
+ assert not self.closed, "Cannot lookup in closed JIT"
251
+ address = ctypes.c_uint64()
252
+ with ffi.OutputString() as outerr:
253
+ tracker = ffi.lib.LLVMPY_LLJITLookup(self,
254
+ dylib.encode("utf-8"),
255
+ fn.encode("utf-8"),
256
+ ctypes.byref(address),
257
+ outerr)
258
+ if not tracker:
259
+ raise RuntimeError(str(outerr))
260
+
261
+ return ResourceTracker(tracker, dylib, {fn: address.value})
262
+
263
+ @property
264
+ def target_data(self):
265
+ """
266
+ The TargetData for this LLJIT instance.
267
+ """
268
+ if self._td is not None:
269
+ return self._td
270
+ ptr = ffi.lib.LLVMPY_LLJITGetDataLayout(self)
271
+ self._td = targets.TargetData(ptr)
272
+ self._td._owned = True
273
+ return self._td
274
+
275
+ def _dispose(self):
276
+ if self._td is not None:
277
+ self._td.detach()
278
+ self._capi.LLVMPY_LLJITDispose(self)
279
+
280
+
281
+ def create_lljit_compiler(target_machine=None, *,
282
+ use_jit_link=False,
283
+ suppress_errors=False):
284
+ """
285
+ Create an LLJIT instance
286
+ """
287
+ with ffi.OutputString() as outerr:
288
+ lljit = ffi.lib.LLVMPY_CreateLLJITCompiler(target_machine,
289
+ suppress_errors,
290
+ use_jit_link,
291
+ outerr)
292
+ if not lljit:
293
+ raise RuntimeError(str(outerr))
294
+
295
+ return LLJIT(lljit)
296
+
297
+
298
+ ffi.lib.LLVMPY_LLJITLookup.argtypes = [
299
+ ffi.LLVMOrcLLJITRef,
300
+ c_char_p,
301
+ c_char_p,
302
+ POINTER(c_uint64),
303
+ POINTER(c_char_p),
304
+ ]
305
+ ffi.lib.LLVMPY_LLJITLookup.restype = ffi.LLVMOrcDylibTrackerRef
306
+
307
+ ffi.lib.LLVMPY_LLJITGetDataLayout.argtypes = [
308
+ ffi.LLVMOrcLLJITRef,
309
+ ]
310
+ ffi.lib.LLVMPY_LLJITGetDataLayout.restype = ffi.LLVMTargetDataRef
311
+
312
+ ffi.lib.LLVMPY_CreateLLJITCompiler.argtypes = [
313
+ ffi.LLVMTargetMachineRef,
314
+ c_bool,
315
+ c_bool,
316
+ POINTER(c_char_p),
317
+ ]
318
+ ffi.lib.LLVMPY_CreateLLJITCompiler.restype = ffi.LLVMOrcLLJITRef
319
+
320
+ ffi.lib.LLVMPY_LLJITDispose.argtypes = [
321
+ ffi.LLVMOrcLLJITRef,
322
+ ]
323
+
324
+
325
+ ffi.lib.LLVMPY_LLJIT_Link.argtypes = [
326
+ ffi.LLVMOrcLLJITRef,
327
+ c_char_p,
328
+ POINTER(_LinkElement),
329
+ c_size_t,
330
+ POINTER(_SymbolAddress),
331
+ c_size_t,
332
+ POINTER(_SymbolAddress),
333
+ c_size_t,
334
+ POINTER(c_char_p)
335
+ ]
336
+ ffi.lib.LLVMPY_LLJIT_Link.restype = ffi.LLVMOrcDylibTrackerRef
337
+
338
+ ffi.lib.LLVMPY_LLJIT_Dylib_Tracker_Dispose.argtypes = [
339
+ ffi.LLVMOrcDylibTrackerRef,
340
+ POINTER(c_char_p)
341
+ ]
342
+ ffi.lib.LLVMPY_LLJIT_Dylib_Tracker_Dispose.restype = c_bool