llvmlite 0.46.0b1__cp310-cp310-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/LICENSE +24 -0
  41. llvmlite-0.46.0b1.dist-info/LICENSE.thirdparty +225 -0
  42. llvmlite-0.46.0b1.dist-info/METADATA +137 -0
  43. llvmlite-0.46.0b1.dist-info/RECORD +45 -0
  44. llvmlite-0.46.0b1.dist-info/WHEEL +5 -0
  45. llvmlite-0.46.0b1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,395 @@
1
+ import sys
2
+ import ctypes
3
+ import threading
4
+ import importlib.resources as _impres
5
+
6
+ from llvmlite.binding.common import _decode_string, _is_shutting_down
7
+ from llvmlite.utils import get_library_name
8
+
9
+
10
+ def _make_opaque_ref(name):
11
+ newcls = type(name, (ctypes.Structure,), {})
12
+ return ctypes.POINTER(newcls)
13
+
14
+
15
+ LLVMContextRef = _make_opaque_ref("LLVMContext")
16
+ LLVMModuleRef = _make_opaque_ref("LLVMModule")
17
+ LLVMValueRef = _make_opaque_ref("LLVMValue")
18
+ LLVMTypeRef = _make_opaque_ref("LLVMType")
19
+ LLVMExecutionEngineRef = _make_opaque_ref("LLVMExecutionEngine")
20
+ LLVMPassManagerBuilderRef = _make_opaque_ref("LLVMPassManagerBuilder")
21
+ LLVMPassManagerRef = _make_opaque_ref("LLVMPassManager")
22
+ LLVMTargetDataRef = _make_opaque_ref("LLVMTargetData")
23
+ LLVMTargetLibraryInfoRef = _make_opaque_ref("LLVMTargetLibraryInfo")
24
+ LLVMTargetRef = _make_opaque_ref("LLVMTarget")
25
+ LLVMTargetMachineRef = _make_opaque_ref("LLVMTargetMachine")
26
+ LLVMMemoryBufferRef = _make_opaque_ref("LLVMMemoryBuffer")
27
+ LLVMAttributeListIterator = _make_opaque_ref("LLVMAttributeListIterator")
28
+ LLVMElementIterator = _make_opaque_ref("LLVMElementIterator")
29
+ LLVMAttributeSetIterator = _make_opaque_ref("LLVMAttributeSetIterator")
30
+ LLVMGlobalsIterator = _make_opaque_ref("LLVMGlobalsIterator")
31
+ LLVMFunctionsIterator = _make_opaque_ref("LLVMFunctionsIterator")
32
+ LLVMBlocksIterator = _make_opaque_ref("LLVMBlocksIterator")
33
+ LLVMArgumentsIterator = _make_opaque_ref("LLVMArgumentsIterator")
34
+ LLVMInstructionsIterator = _make_opaque_ref("LLVMInstructionsIterator")
35
+ LLVMOperandsIterator = _make_opaque_ref("LLVMOperandsIterator")
36
+ LLVMIncomingBlocksIterator = _make_opaque_ref("LLVMIncomingBlocksIterator")
37
+ LLVMTypesIterator = _make_opaque_ref("LLVMTypesIterator")
38
+ LLVMObjectCacheRef = _make_opaque_ref("LLVMObjectCache")
39
+ LLVMObjectFileRef = _make_opaque_ref("LLVMObjectFile")
40
+ LLVMSectionIteratorRef = _make_opaque_ref("LLVMSectionIterator")
41
+ LLVMOrcLLJITRef = _make_opaque_ref("LLVMOrcLLJITRef")
42
+ LLVMOrcDylibTrackerRef = _make_opaque_ref("LLVMOrcDylibTrackerRef")
43
+ LLVMTimePassesHandlerRef = _make_opaque_ref("LLVMTimePassesHandler")
44
+ LLVMPipelineTuningOptionsRef = _make_opaque_ref("LLVMPipeLineTuningOptions")
45
+ LLVMModulePassManagerRef = _make_opaque_ref("LLVMModulePassManager")
46
+ LLVMFunctionPassManagerRef = _make_opaque_ref("LLVMFunctionPassManager")
47
+ LLVMPassBuilderRef = _make_opaque_ref("LLVMPassBuilder")
48
+
49
+
50
+ class _LLVMLock:
51
+ """A Lock to guarantee thread-safety for the LLVM C-API.
52
+
53
+ This class implements __enter__ and __exit__ for acquiring and releasing
54
+ the lock as a context manager.
55
+
56
+ Also, callbacks can be attached so that every time the lock is acquired
57
+ and released the corresponding callbacks will be invoked.
58
+ """
59
+ def __init__(self):
60
+ # The reentrant lock is needed for callbacks that re-enter
61
+ # the Python interpreter.
62
+ self._lock = threading.RLock()
63
+ self._cblist = []
64
+
65
+ def register(self, acq_fn, rel_fn):
66
+ """Register callbacks that are invoked immediately after the lock is
67
+ acquired (``acq_fn()``) and immediately before the lock is released
68
+ (``rel_fn()``).
69
+ """
70
+ self._cblist.append((acq_fn, rel_fn))
71
+
72
+ def unregister(self, acq_fn, rel_fn):
73
+ """Remove the registered callbacks.
74
+ """
75
+ self._cblist.remove((acq_fn, rel_fn))
76
+
77
+ def __enter__(self):
78
+ self._lock.acquire()
79
+ # Invoke all callbacks
80
+ for acq_fn, rel_fn in self._cblist:
81
+ acq_fn()
82
+
83
+ def __exit__(self, *exc_details):
84
+ # Invoke all callbacks
85
+ for acq_fn, rel_fn in self._cblist:
86
+ rel_fn()
87
+ self._lock.release()
88
+
89
+
90
+ class _suppress_cleanup_errors:
91
+ def __init__(self, context):
92
+ self._context = context
93
+
94
+ def __enter__(self):
95
+ return self._context.__enter__()
96
+
97
+ def __exit__(self, exc_type, exc_value, traceback):
98
+ try:
99
+ return self._context.__exit__(exc_type, exc_value, traceback)
100
+ except PermissionError:
101
+ pass # Resource dylibs can't be deleted on Windows.
102
+
103
+
104
+ class _lib_wrapper(object):
105
+ """Wrap libllvmlite with a lock such that only one thread may access it at
106
+ a time.
107
+
108
+ This class duck-types a CDLL.
109
+ """
110
+ __slots__ = ['_lib_handle', '_fntab', '_lock']
111
+
112
+ def __init__(self):
113
+ self._lib_handle = None
114
+ self._fntab = {}
115
+ self._lock = _LLVMLock()
116
+
117
+ def _load_lib(self):
118
+ try:
119
+ with _suppress_cleanup_errors(_importlib_resources_path(
120
+ __name__.rpartition(".")[0],
121
+ get_library_name())) as lib_path:
122
+ self._lib_handle = ctypes.CDLL(str(lib_path))
123
+ # Check that we can look up expected symbols.
124
+ _ = self._lib_handle.LLVMPY_GetVersionInfo()
125
+ except (OSError, AttributeError) as e:
126
+ # OSError may be raised if the file cannot be opened, or is not
127
+ # a shared library.
128
+ # AttributeError is raised if LLVMPY_GetVersionInfo does not
129
+ # exist.
130
+ raise OSError("Could not find/load shared object file") from e
131
+
132
+ @property
133
+ def _lib(self):
134
+ # Not threadsafe.
135
+ if not self._lib_handle:
136
+ self._load_lib()
137
+ return self._lib_handle
138
+
139
+ def __getattr__(self, name):
140
+ try:
141
+ return self._fntab[name]
142
+ except KeyError:
143
+ # Lazily wraps new functions as they are requested
144
+ cfn = getattr(self._lib, name)
145
+ wrapped = _lib_fn_wrapper(self._lock, cfn)
146
+ self._fntab[name] = wrapped
147
+ return wrapped
148
+
149
+ @property
150
+ def _name(self):
151
+ """The name of the library passed in the CDLL constructor.
152
+
153
+ For duck-typing a ctypes.CDLL
154
+ """
155
+ return self._lib._name
156
+
157
+ @property
158
+ def _handle(self):
159
+ """The system handle used to access the library.
160
+
161
+ For duck-typing a ctypes.CDLL
162
+ """
163
+ return self._lib._handle
164
+
165
+
166
+ class _lib_fn_wrapper(object):
167
+ """Wraps and duck-types a ctypes.CFUNCTYPE to provide
168
+ automatic locking when the wrapped function is called.
169
+
170
+ TODO: we can add methods to mark the function as threadsafe
171
+ and remove the locking-step on call when marked.
172
+ """
173
+ __slots__ = ['_lock', '_cfn']
174
+
175
+ def __init__(self, lock, cfn):
176
+ self._lock = lock
177
+ self._cfn = cfn
178
+
179
+ @property
180
+ def argtypes(self):
181
+ return self._cfn.argtypes
182
+
183
+ @argtypes.setter
184
+ def argtypes(self, argtypes):
185
+ self._cfn.argtypes = argtypes
186
+
187
+ @property
188
+ def restype(self):
189
+ return self._cfn.restype
190
+
191
+ @restype.setter
192
+ def restype(self, restype):
193
+ self._cfn.restype = restype
194
+
195
+ def __call__(self, *args, **kwargs):
196
+ with self._lock:
197
+ return self._cfn(*args, **kwargs)
198
+
199
+
200
+ def _importlib_resources_path_repl(package, resource):
201
+ """Replacement implementation of `import.resources.path` to avoid
202
+ deprecation warning following code at importlib_resources/_legacy.py
203
+ as suggested by https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy
204
+
205
+ Notes on differences from importlib.resources implementation:
206
+
207
+ The `_common.normalize_path(resource)` call is skipped because it is an
208
+ internal API and it is unnecessary for the use here. What it does is
209
+ ensuring `resource` is a str and that it does not contain path separators.
210
+ """ # noqa E501
211
+ return _impres.as_file(_impres.files(package) / resource)
212
+
213
+
214
+ _importlib_resources_path = (_importlib_resources_path_repl
215
+ if sys.version_info[:2] >= (3, 10)
216
+ else _impres.path)
217
+
218
+
219
+ lib = _lib_wrapper()
220
+
221
+
222
+ def register_lock_callback(acq_fn, rel_fn):
223
+ """Register callback functions for lock acquire and release.
224
+ *acq_fn* and *rel_fn* are callables that take no arguments.
225
+ """
226
+ lib._lock.register(acq_fn, rel_fn)
227
+
228
+
229
+ def unregister_lock_callback(acq_fn, rel_fn):
230
+ """Remove the registered callback functions for lock acquire and release.
231
+ The arguments are the same as used in `register_lock_callback()`.
232
+ """
233
+ lib._lock.unregister(acq_fn, rel_fn)
234
+
235
+
236
+ class _DeadPointer(object):
237
+ """
238
+ Dummy class to make error messages more helpful.
239
+ """
240
+
241
+
242
+ class OutputString(object):
243
+ """
244
+ Object for managing the char* output of LLVM APIs.
245
+ """
246
+ _as_parameter_ = _DeadPointer()
247
+
248
+ @classmethod
249
+ def from_return(cls, ptr):
250
+ """Constructing from a pointer returned from the C-API.
251
+ The pointer must be allocated with LLVMPY_CreateString.
252
+
253
+ Note
254
+ ----
255
+ Because ctypes auto-converts *restype* of *c_char_p* into a python
256
+ string, we must use *c_void_p* to obtain the raw pointer.
257
+ """
258
+ return cls(init=ctypes.cast(ptr, ctypes.c_char_p))
259
+
260
+ def __init__(self, owned=True, init=None):
261
+ self._ptr = init if init is not None else ctypes.c_char_p(None)
262
+ self._as_parameter_ = ctypes.byref(self._ptr)
263
+ self._owned = owned
264
+
265
+ def close(self):
266
+ if self._ptr is not None:
267
+ if self._owned:
268
+ lib.LLVMPY_DisposeString(self._ptr)
269
+ self._ptr = None
270
+ del self._as_parameter_
271
+
272
+ def __enter__(self):
273
+ return self
274
+
275
+ def __exit__(self, exc_type, exc_val, exc_tb):
276
+ self.close()
277
+
278
+ def __del__(self, _is_shutting_down=_is_shutting_down):
279
+ # Avoid errors trying to rely on globals and modules at interpreter
280
+ # shutdown.
281
+ if not _is_shutting_down():
282
+ if self.close is not None:
283
+ self.close()
284
+
285
+ def __str__(self):
286
+ if self._ptr is None:
287
+ return "<dead OutputString>"
288
+ s = self._ptr.value
289
+ assert s is not None
290
+ return _decode_string(s)
291
+
292
+ def __bool__(self):
293
+ return bool(self._ptr)
294
+
295
+ __nonzero__ = __bool__
296
+
297
+ @property
298
+ def bytes(self):
299
+ """Get the raw bytes of content of the char pointer.
300
+ """
301
+ return self._ptr.value
302
+
303
+
304
+ def ret_string(ptr):
305
+ """To wrap string return-value from C-API.
306
+ """
307
+ if ptr is not None:
308
+ return str(OutputString.from_return(ptr))
309
+
310
+
311
+ def ret_bytes(ptr):
312
+ """To wrap bytes return-value from C-API.
313
+ """
314
+ if ptr is not None:
315
+ return OutputString.from_return(ptr).bytes
316
+
317
+
318
+ class ObjectRef(object):
319
+ """
320
+ A wrapper around a ctypes pointer to a LLVM object ("resource").
321
+ """
322
+ _closed = False
323
+ _as_parameter_ = _DeadPointer()
324
+ # Whether this object pointer is owned by another one.
325
+ _owned = False
326
+
327
+ def __init__(self, ptr):
328
+ if ptr is None:
329
+ raise ValueError("NULL pointer")
330
+ self._ptr = ptr
331
+ self._as_parameter_ = ptr
332
+ self._capi = lib
333
+
334
+ def close(self):
335
+ """
336
+ Close this object and do any required clean-up actions.
337
+ """
338
+ try:
339
+ if not self._closed and not self._owned:
340
+ self._dispose()
341
+ finally:
342
+ self.detach()
343
+
344
+ def detach(self):
345
+ """
346
+ Detach the underlying LLVM resource without disposing of it.
347
+ """
348
+ if not self._closed:
349
+ del self._as_parameter_
350
+ self._closed = True
351
+ self._ptr = None
352
+
353
+ def _dispose(self):
354
+ """
355
+ Dispose of the underlying LLVM resource. Should be overriden
356
+ by subclasses. Automatically called by close(), __del__() and
357
+ __exit__() (unless the resource has been detached).
358
+ """
359
+
360
+ @property
361
+ def closed(self):
362
+ """
363
+ Whether this object has been closed. A closed object can't
364
+ be used anymore.
365
+ """
366
+ return self._closed
367
+
368
+ def __enter__(self):
369
+ assert hasattr(self, "close")
370
+ if self._closed:
371
+ raise RuntimeError("%s instance already closed" % (self.__class__,))
372
+ return self
373
+
374
+ def __exit__(self, exc_type, exc_val, exc_tb):
375
+ self.close()
376
+
377
+ def __del__(self, _is_shutting_down=_is_shutting_down):
378
+ if not _is_shutting_down():
379
+ if self.close is not None:
380
+ self.close()
381
+
382
+ def __bool__(self):
383
+ return bool(self._ptr)
384
+
385
+ def __eq__(self, other):
386
+ if not hasattr(other, "_ptr"):
387
+ return False
388
+ return ctypes.addressof(self._ptr[0]) == \
389
+ ctypes.addressof(other._ptr[0])
390
+
391
+ __nonzero__ = __bool__
392
+
393
+ # XXX useful?
394
+ def __hash__(self):
395
+ return hash(ctypes.cast(self._ptr, ctypes.c_void_p).value)
@@ -0,0 +1,85 @@
1
+ from ctypes import c_uint
2
+
3
+ from llvmlite.binding import ffi
4
+
5
+
6
+ def initialize():
7
+ """
8
+ Initialize the LLVM core (deprecated).
9
+
10
+ This function is deprecated and will raise an error when called.
11
+ LLVM initialization is now handled automatically and no longer
12
+ requires explicit initialization calls.
13
+
14
+ Raises:
15
+ RuntimeError: Always raised as this function is no longer needed.
16
+ """
17
+ raise RuntimeError(
18
+ "llvmlite.binding.initialize() is deprecated and will be removed. "
19
+ "LLVM initialization is now handled automatically. "
20
+ "Please remove calls to this function from your code and check for "
21
+ "other behavioral changes that may have occurred due to LLVM updates."
22
+ )
23
+
24
+
25
+ def initialize_all_targets():
26
+ """
27
+ Initialize all targets. Necessary before targets can be looked up
28
+ via the :class:`Target` class.
29
+ """
30
+ ffi.lib.LLVMPY_InitializeAllTargetInfos()
31
+ ffi.lib.LLVMPY_InitializeAllTargets()
32
+ ffi.lib.LLVMPY_InitializeAllTargetMCs()
33
+
34
+
35
+ def initialize_all_asmprinters():
36
+ """
37
+ Initialize all code generators. Necessary before generating
38
+ any assembly or machine code via the :meth:`TargetMachine.emit_object`
39
+ and :meth:`TargetMachine.emit_assembly` methods.
40
+ """
41
+ ffi.lib.LLVMPY_InitializeAllAsmPrinters()
42
+
43
+
44
+ def initialize_native_target():
45
+ """
46
+ Initialize the native (host) target. Necessary before doing any
47
+ code generation.
48
+ """
49
+ ffi.lib.LLVMPY_InitializeNativeTarget()
50
+
51
+
52
+ def initialize_native_asmprinter():
53
+ """
54
+ Initialize the native ASM printer.
55
+ """
56
+ ffi.lib.LLVMPY_InitializeNativeAsmPrinter()
57
+
58
+
59
+ def initialize_native_asmparser():
60
+ """
61
+ Initialize the native ASM parser.
62
+ """
63
+ ffi.lib.LLVMPY_InitializeNativeAsmParser()
64
+
65
+
66
+ def shutdown():
67
+ ffi.lib.LLVMPY_Shutdown()
68
+
69
+
70
+ # =============================================================================
71
+ # Set function FFI
72
+
73
+ ffi.lib.LLVMPY_GetVersionInfo.restype = c_uint
74
+
75
+
76
+ def _version_info():
77
+ v = []
78
+ x = ffi.lib.LLVMPY_GetVersionInfo()
79
+ while x:
80
+ v.append(x & 0xff)
81
+ x >>= 8
82
+ return tuple(reversed(v))
83
+
84
+
85
+ llvm_version_info = _version_info()
Binary file
@@ -0,0 +1,20 @@
1
+ from ctypes import c_int, c_char_p, POINTER
2
+ from llvmlite.binding import ffi
3
+
4
+
5
+ def link_modules(dst, src):
6
+ with ffi.OutputString() as outerr:
7
+ err = ffi.lib.LLVMPY_LinkModules(dst, src, outerr)
8
+ # The underlying module was destroyed
9
+ src.detach()
10
+ if err:
11
+ raise RuntimeError(str(outerr))
12
+
13
+
14
+ ffi.lib.LLVMPY_LinkModules.argtypes = [
15
+ ffi.LLVMModuleRef,
16
+ ffi.LLVMModuleRef,
17
+ POINTER(c_char_p),
18
+ ]
19
+
20
+ ffi.lib.LLVMPY_LinkModules.restype = c_int