llvmlite 0.44.0rc2__cp310-cp310-macosx_10_14_x86_64.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.
- llvmlite/__init__.py +10 -0
- llvmlite/_version.py +11 -0
- llvmlite/binding/__init__.py +19 -0
- llvmlite/binding/analysis.py +69 -0
- llvmlite/binding/common.py +34 -0
- llvmlite/binding/context.py +39 -0
- llvmlite/binding/dylib.py +45 -0
- llvmlite/binding/executionengine.py +330 -0
- llvmlite/binding/ffi.py +395 -0
- llvmlite/binding/initfini.py +73 -0
- llvmlite/binding/libllvmlite.dylib +0 -0
- llvmlite/binding/linker.py +20 -0
- llvmlite/binding/module.py +349 -0
- llvmlite/binding/newpassmanagers.py +357 -0
- llvmlite/binding/object_file.py +82 -0
- llvmlite/binding/options.py +17 -0
- llvmlite/binding/orcjit.py +342 -0
- llvmlite/binding/passmanagers.py +946 -0
- llvmlite/binding/targets.py +520 -0
- llvmlite/binding/transforms.py +151 -0
- llvmlite/binding/typeref.py +285 -0
- llvmlite/binding/value.py +632 -0
- llvmlite/ir/__init__.py +11 -0
- llvmlite/ir/_utils.py +80 -0
- llvmlite/ir/builder.py +1120 -0
- llvmlite/ir/context.py +20 -0
- llvmlite/ir/instructions.py +920 -0
- llvmlite/ir/module.py +246 -0
- llvmlite/ir/transforms.py +64 -0
- llvmlite/ir/types.py +734 -0
- llvmlite/ir/values.py +1217 -0
- llvmlite/tests/__init__.py +57 -0
- llvmlite/tests/__main__.py +3 -0
- llvmlite/tests/customize.py +407 -0
- llvmlite/tests/refprune_proto.py +329 -0
- llvmlite/tests/test_binding.py +3208 -0
- llvmlite/tests/test_ir.py +2994 -0
- llvmlite/tests/test_refprune.py +730 -0
- llvmlite/tests/test_valuerepr.py +60 -0
- llvmlite/utils.py +29 -0
- llvmlite-0.44.0rc2.dist-info/LICENSE +24 -0
- llvmlite-0.44.0rc2.dist-info/LICENSE.thirdparty +225 -0
- llvmlite-0.44.0rc2.dist-info/METADATA +138 -0
- llvmlite-0.44.0rc2.dist-info/RECORD +46 -0
- llvmlite-0.44.0rc2.dist-info/WHEEL +5 -0
- llvmlite-0.44.0rc2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
from ctypes import (c_char_p, byref, POINTER, c_bool, create_string_buffer,
|
|
2
|
+
c_size_t, string_at)
|
|
3
|
+
|
|
4
|
+
from llvmlite.binding import ffi
|
|
5
|
+
from llvmlite.binding.linker import link_modules
|
|
6
|
+
from llvmlite.binding.common import _decode_string, _encode_string
|
|
7
|
+
from llvmlite.binding.value import ValueRef, TypeRef
|
|
8
|
+
from llvmlite.binding.context import get_global_context
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def parse_assembly(llvmir, context=None):
|
|
12
|
+
"""
|
|
13
|
+
Create Module from a LLVM IR string
|
|
14
|
+
"""
|
|
15
|
+
if context is None:
|
|
16
|
+
context = get_global_context()
|
|
17
|
+
llvmir = _encode_string(llvmir)
|
|
18
|
+
strbuf = c_char_p(llvmir)
|
|
19
|
+
with ffi.OutputString() as errmsg:
|
|
20
|
+
mod = ModuleRef(
|
|
21
|
+
ffi.lib.LLVMPY_ParseAssembly(context, strbuf, errmsg),
|
|
22
|
+
context)
|
|
23
|
+
if errmsg:
|
|
24
|
+
mod.close()
|
|
25
|
+
raise RuntimeError("LLVM IR parsing error\n{0}".format(errmsg))
|
|
26
|
+
return mod
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def parse_bitcode(bitcode, context=None):
|
|
30
|
+
"""
|
|
31
|
+
Create Module from a LLVM *bitcode* (a bytes object).
|
|
32
|
+
"""
|
|
33
|
+
if context is None:
|
|
34
|
+
context = get_global_context()
|
|
35
|
+
buf = c_char_p(bitcode)
|
|
36
|
+
bufsize = len(bitcode)
|
|
37
|
+
with ffi.OutputString() as errmsg:
|
|
38
|
+
mod = ModuleRef(ffi.lib.LLVMPY_ParseBitcode(
|
|
39
|
+
context, buf, bufsize, errmsg), context)
|
|
40
|
+
if errmsg:
|
|
41
|
+
mod.close()
|
|
42
|
+
raise RuntimeError(
|
|
43
|
+
"LLVM bitcode parsing error\n{0}".format(errmsg))
|
|
44
|
+
return mod
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class ModuleRef(ffi.ObjectRef):
|
|
48
|
+
"""
|
|
49
|
+
A reference to a LLVM module.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
def __init__(self, module_ptr, context):
|
|
53
|
+
super(ModuleRef, self).__init__(module_ptr)
|
|
54
|
+
self._context = context
|
|
55
|
+
|
|
56
|
+
def __str__(self):
|
|
57
|
+
with ffi.OutputString() as outstr:
|
|
58
|
+
ffi.lib.LLVMPY_PrintModuleToString(self, outstr)
|
|
59
|
+
return str(outstr)
|
|
60
|
+
|
|
61
|
+
def as_bitcode(self):
|
|
62
|
+
"""
|
|
63
|
+
Return the module's LLVM bitcode, as a bytes object.
|
|
64
|
+
"""
|
|
65
|
+
ptr = c_char_p(None)
|
|
66
|
+
size = c_size_t(-1)
|
|
67
|
+
ffi.lib.LLVMPY_WriteBitcodeToString(self, byref(ptr), byref(size))
|
|
68
|
+
if not ptr:
|
|
69
|
+
raise MemoryError
|
|
70
|
+
try:
|
|
71
|
+
assert size.value >= 0
|
|
72
|
+
return string_at(ptr, size.value)
|
|
73
|
+
finally:
|
|
74
|
+
ffi.lib.LLVMPY_DisposeString(ptr)
|
|
75
|
+
|
|
76
|
+
def _dispose(self):
|
|
77
|
+
self._capi.LLVMPY_DisposeModule(self)
|
|
78
|
+
|
|
79
|
+
def get_function(self, name):
|
|
80
|
+
"""
|
|
81
|
+
Get a ValueRef pointing to the function named *name*.
|
|
82
|
+
NameError is raised if the symbol isn't found.
|
|
83
|
+
"""
|
|
84
|
+
p = ffi.lib.LLVMPY_GetNamedFunction(self, _encode_string(name))
|
|
85
|
+
if not p:
|
|
86
|
+
raise NameError(name)
|
|
87
|
+
return ValueRef(p, 'function', dict(module=self))
|
|
88
|
+
|
|
89
|
+
def get_global_variable(self, name):
|
|
90
|
+
"""
|
|
91
|
+
Get a ValueRef pointing to the global variable named *name*.
|
|
92
|
+
NameError is raised if the symbol isn't found.
|
|
93
|
+
"""
|
|
94
|
+
p = ffi.lib.LLVMPY_GetNamedGlobalVariable(self, _encode_string(name))
|
|
95
|
+
if not p:
|
|
96
|
+
raise NameError(name)
|
|
97
|
+
return ValueRef(p, 'global', dict(module=self))
|
|
98
|
+
|
|
99
|
+
def get_struct_type(self, name):
|
|
100
|
+
"""
|
|
101
|
+
Get a TypeRef pointing to a structure type named *name*.
|
|
102
|
+
NameError is raised if the struct type isn't found.
|
|
103
|
+
"""
|
|
104
|
+
p = ffi.lib.LLVMPY_GetNamedStructType(self, _encode_string(name))
|
|
105
|
+
if not p:
|
|
106
|
+
raise NameError(name)
|
|
107
|
+
return TypeRef(p)
|
|
108
|
+
|
|
109
|
+
def verify(self):
|
|
110
|
+
"""
|
|
111
|
+
Verify the module IR's correctness. RuntimeError is raised on error.
|
|
112
|
+
"""
|
|
113
|
+
with ffi.OutputString() as outmsg:
|
|
114
|
+
if ffi.lib.LLVMPY_VerifyModule(self, outmsg):
|
|
115
|
+
raise RuntimeError(str(outmsg))
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def name(self):
|
|
119
|
+
"""
|
|
120
|
+
The module's identifier.
|
|
121
|
+
"""
|
|
122
|
+
return _decode_string(ffi.lib.LLVMPY_GetModuleName(self))
|
|
123
|
+
|
|
124
|
+
@name.setter
|
|
125
|
+
def name(self, value):
|
|
126
|
+
ffi.lib.LLVMPY_SetModuleName(self, _encode_string(value))
|
|
127
|
+
|
|
128
|
+
@property
|
|
129
|
+
def source_file(self):
|
|
130
|
+
"""
|
|
131
|
+
The module's original source file name
|
|
132
|
+
"""
|
|
133
|
+
return _decode_string(ffi.lib.LLVMPY_GetModuleSourceFileName(self))
|
|
134
|
+
|
|
135
|
+
@property
|
|
136
|
+
def data_layout(self):
|
|
137
|
+
"""
|
|
138
|
+
This module's data layout specification, as a string.
|
|
139
|
+
"""
|
|
140
|
+
# LLVMGetDataLayout() points inside a std::string managed by LLVM.
|
|
141
|
+
with ffi.OutputString(owned=False) as outmsg:
|
|
142
|
+
ffi.lib.LLVMPY_GetDataLayout(self, outmsg)
|
|
143
|
+
return str(outmsg)
|
|
144
|
+
|
|
145
|
+
@data_layout.setter
|
|
146
|
+
def data_layout(self, strrep):
|
|
147
|
+
ffi.lib.LLVMPY_SetDataLayout(self,
|
|
148
|
+
create_string_buffer(
|
|
149
|
+
strrep.encode('utf8')))
|
|
150
|
+
|
|
151
|
+
@property
|
|
152
|
+
def triple(self):
|
|
153
|
+
"""
|
|
154
|
+
This module's target "triple" specification, as a string.
|
|
155
|
+
"""
|
|
156
|
+
# LLVMGetTarget() points inside a std::string managed by LLVM.
|
|
157
|
+
with ffi.OutputString(owned=False) as outmsg:
|
|
158
|
+
ffi.lib.LLVMPY_GetTarget(self, outmsg)
|
|
159
|
+
return str(outmsg)
|
|
160
|
+
|
|
161
|
+
@triple.setter
|
|
162
|
+
def triple(self, strrep):
|
|
163
|
+
ffi.lib.LLVMPY_SetTarget(self,
|
|
164
|
+
create_string_buffer(
|
|
165
|
+
strrep.encode('utf8')))
|
|
166
|
+
|
|
167
|
+
def link_in(self, other, preserve=False):
|
|
168
|
+
"""
|
|
169
|
+
Link the *other* module into this one. The *other* module will
|
|
170
|
+
be destroyed unless *preserve* is true.
|
|
171
|
+
"""
|
|
172
|
+
if preserve:
|
|
173
|
+
other = other.clone()
|
|
174
|
+
link_modules(self, other)
|
|
175
|
+
|
|
176
|
+
@property
|
|
177
|
+
def global_variables(self):
|
|
178
|
+
"""
|
|
179
|
+
Return an iterator over this module's global variables.
|
|
180
|
+
The iterator will yield a ValueRef for each global variable.
|
|
181
|
+
|
|
182
|
+
Note that global variables don't include functions
|
|
183
|
+
(a function is a "global value" but not a "global variable" in
|
|
184
|
+
LLVM parlance)
|
|
185
|
+
"""
|
|
186
|
+
it = ffi.lib.LLVMPY_ModuleGlobalsIter(self)
|
|
187
|
+
return _GlobalsIterator(it, dict(module=self))
|
|
188
|
+
|
|
189
|
+
@property
|
|
190
|
+
def functions(self):
|
|
191
|
+
"""
|
|
192
|
+
Return an iterator over this module's functions.
|
|
193
|
+
The iterator will yield a ValueRef for each function.
|
|
194
|
+
"""
|
|
195
|
+
it = ffi.lib.LLVMPY_ModuleFunctionsIter(self)
|
|
196
|
+
return _FunctionsIterator(it, dict(module=self))
|
|
197
|
+
|
|
198
|
+
@property
|
|
199
|
+
def struct_types(self):
|
|
200
|
+
"""
|
|
201
|
+
Return an iterator over the struct types defined in
|
|
202
|
+
the module. The iterator will yield a TypeRef.
|
|
203
|
+
"""
|
|
204
|
+
it = ffi.lib.LLVMPY_ModuleTypesIter(self)
|
|
205
|
+
return _TypesIterator(it, dict(module=self))
|
|
206
|
+
|
|
207
|
+
def clone(self):
|
|
208
|
+
return ModuleRef(ffi.lib.LLVMPY_CloneModule(self), self._context)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
class _Iterator(ffi.ObjectRef):
|
|
212
|
+
|
|
213
|
+
kind = None
|
|
214
|
+
|
|
215
|
+
def __init__(self, ptr, parents):
|
|
216
|
+
ffi.ObjectRef.__init__(self, ptr)
|
|
217
|
+
self._parents = parents
|
|
218
|
+
assert self.kind is not None
|
|
219
|
+
|
|
220
|
+
def __next__(self):
|
|
221
|
+
vp = self._next()
|
|
222
|
+
if vp:
|
|
223
|
+
return ValueRef(vp, self.kind, self._parents)
|
|
224
|
+
else:
|
|
225
|
+
raise StopIteration
|
|
226
|
+
|
|
227
|
+
next = __next__
|
|
228
|
+
|
|
229
|
+
def __iter__(self):
|
|
230
|
+
return self
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
class _GlobalsIterator(_Iterator):
|
|
234
|
+
|
|
235
|
+
kind = 'global'
|
|
236
|
+
|
|
237
|
+
def _dispose(self):
|
|
238
|
+
self._capi.LLVMPY_DisposeGlobalsIter(self)
|
|
239
|
+
|
|
240
|
+
def _next(self):
|
|
241
|
+
return ffi.lib.LLVMPY_GlobalsIterNext(self)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
class _FunctionsIterator(_Iterator):
|
|
245
|
+
|
|
246
|
+
kind = 'function'
|
|
247
|
+
|
|
248
|
+
def _dispose(self):
|
|
249
|
+
self._capi.LLVMPY_DisposeFunctionsIter(self)
|
|
250
|
+
|
|
251
|
+
def _next(self):
|
|
252
|
+
return ffi.lib.LLVMPY_FunctionsIterNext(self)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
class _TypesIterator(_Iterator):
|
|
256
|
+
|
|
257
|
+
kind = 'type'
|
|
258
|
+
|
|
259
|
+
def _dispose(self):
|
|
260
|
+
self._capi.LLVMPY_DisposeTypesIter(self)
|
|
261
|
+
|
|
262
|
+
def __next__(self):
|
|
263
|
+
vp = self._next()
|
|
264
|
+
if vp:
|
|
265
|
+
return TypeRef(vp)
|
|
266
|
+
else:
|
|
267
|
+
raise StopIteration
|
|
268
|
+
|
|
269
|
+
def _next(self):
|
|
270
|
+
return ffi.lib.LLVMPY_TypesIterNext(self)
|
|
271
|
+
|
|
272
|
+
next = __next__
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
# =============================================================================
|
|
276
|
+
# Set function FFI
|
|
277
|
+
|
|
278
|
+
ffi.lib.LLVMPY_ParseAssembly.argtypes = [ffi.LLVMContextRef,
|
|
279
|
+
c_char_p,
|
|
280
|
+
POINTER(c_char_p)]
|
|
281
|
+
ffi.lib.LLVMPY_ParseAssembly.restype = ffi.LLVMModuleRef
|
|
282
|
+
|
|
283
|
+
ffi.lib.LLVMPY_ParseBitcode.argtypes = [ffi.LLVMContextRef,
|
|
284
|
+
c_char_p, c_size_t,
|
|
285
|
+
POINTER(c_char_p)]
|
|
286
|
+
ffi.lib.LLVMPY_ParseBitcode.restype = ffi.LLVMModuleRef
|
|
287
|
+
|
|
288
|
+
ffi.lib.LLVMPY_DisposeModule.argtypes = [ffi.LLVMModuleRef]
|
|
289
|
+
|
|
290
|
+
ffi.lib.LLVMPY_PrintModuleToString.argtypes = [ffi.LLVMModuleRef,
|
|
291
|
+
POINTER(c_char_p)]
|
|
292
|
+
ffi.lib.LLVMPY_WriteBitcodeToString.argtypes = [ffi.LLVMModuleRef,
|
|
293
|
+
POINTER(c_char_p),
|
|
294
|
+
POINTER(c_size_t)]
|
|
295
|
+
|
|
296
|
+
ffi.lib.LLVMPY_GetNamedFunction.argtypes = [ffi.LLVMModuleRef,
|
|
297
|
+
c_char_p]
|
|
298
|
+
ffi.lib.LLVMPY_GetNamedFunction.restype = ffi.LLVMValueRef
|
|
299
|
+
|
|
300
|
+
ffi.lib.LLVMPY_VerifyModule.argtypes = [ffi.LLVMModuleRef,
|
|
301
|
+
POINTER(c_char_p)]
|
|
302
|
+
ffi.lib.LLVMPY_VerifyModule.restype = c_bool
|
|
303
|
+
|
|
304
|
+
ffi.lib.LLVMPY_GetDataLayout.argtypes = [ffi.LLVMModuleRef, POINTER(c_char_p)]
|
|
305
|
+
ffi.lib.LLVMPY_SetDataLayout.argtypes = [ffi.LLVMModuleRef, c_char_p]
|
|
306
|
+
|
|
307
|
+
ffi.lib.LLVMPY_GetTarget.argtypes = [ffi.LLVMModuleRef, POINTER(c_char_p)]
|
|
308
|
+
ffi.lib.LLVMPY_SetTarget.argtypes = [ffi.LLVMModuleRef, c_char_p]
|
|
309
|
+
|
|
310
|
+
ffi.lib.LLVMPY_GetNamedGlobalVariable.argtypes = [ffi.LLVMModuleRef, c_char_p]
|
|
311
|
+
ffi.lib.LLVMPY_GetNamedGlobalVariable.restype = ffi.LLVMValueRef
|
|
312
|
+
|
|
313
|
+
ffi.lib.LLVMPY_GetNamedStructType.argtypes = [ffi.LLVMModuleRef, c_char_p]
|
|
314
|
+
ffi.lib.LLVMPY_GetNamedStructType.restype = ffi.LLVMTypeRef
|
|
315
|
+
|
|
316
|
+
ffi.lib.LLVMPY_ModuleGlobalsIter.argtypes = [ffi.LLVMModuleRef]
|
|
317
|
+
ffi.lib.LLVMPY_ModuleGlobalsIter.restype = ffi.LLVMGlobalsIterator
|
|
318
|
+
|
|
319
|
+
ffi.lib.LLVMPY_DisposeGlobalsIter.argtypes = [ffi.LLVMGlobalsIterator]
|
|
320
|
+
|
|
321
|
+
ffi.lib.LLVMPY_GlobalsIterNext.argtypes = [ffi.LLVMGlobalsIterator]
|
|
322
|
+
ffi.lib.LLVMPY_GlobalsIterNext.restype = ffi.LLVMValueRef
|
|
323
|
+
|
|
324
|
+
ffi.lib.LLVMPY_ModuleFunctionsIter.argtypes = [ffi.LLVMModuleRef]
|
|
325
|
+
ffi.lib.LLVMPY_ModuleFunctionsIter.restype = ffi.LLVMFunctionsIterator
|
|
326
|
+
|
|
327
|
+
ffi.lib.LLVMPY_ModuleTypesIter.argtypes = [ffi.LLVMModuleRef]
|
|
328
|
+
ffi.lib.LLVMPY_ModuleTypesIter.restype = ffi.LLVMTypesIterator
|
|
329
|
+
|
|
330
|
+
ffi.lib.LLVMPY_DisposeFunctionsIter.argtypes = [ffi.LLVMFunctionsIterator]
|
|
331
|
+
|
|
332
|
+
ffi.lib.LLVMPY_DisposeTypesIter.argtypes = [ffi.LLVMTypesIterator]
|
|
333
|
+
|
|
334
|
+
ffi.lib.LLVMPY_FunctionsIterNext.argtypes = [ffi.LLVMFunctionsIterator]
|
|
335
|
+
ffi.lib.LLVMPY_FunctionsIterNext.restype = ffi.LLVMValueRef
|
|
336
|
+
|
|
337
|
+
ffi.lib.LLVMPY_TypesIterNext.argtypes = [ffi.LLVMTypesIterator]
|
|
338
|
+
ffi.lib.LLVMPY_TypesIterNext.restype = ffi.LLVMTypeRef
|
|
339
|
+
|
|
340
|
+
ffi.lib.LLVMPY_CloneModule.argtypes = [ffi.LLVMModuleRef]
|
|
341
|
+
ffi.lib.LLVMPY_CloneModule.restype = ffi.LLVMModuleRef
|
|
342
|
+
|
|
343
|
+
ffi.lib.LLVMPY_GetModuleName.argtypes = [ffi.LLVMModuleRef]
|
|
344
|
+
ffi.lib.LLVMPY_GetModuleName.restype = c_char_p
|
|
345
|
+
|
|
346
|
+
ffi.lib.LLVMPY_SetModuleName.argtypes = [ffi.LLVMModuleRef, c_char_p]
|
|
347
|
+
|
|
348
|
+
ffi.lib.LLVMPY_GetModuleSourceFileName.argtypes = [ffi.LLVMModuleRef]
|
|
349
|
+
ffi.lib.LLVMPY_GetModuleSourceFileName.restype = c_char_p
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
from ctypes import c_bool, c_int, c_size_t
|
|
2
|
+
from enum import IntFlag
|
|
3
|
+
from llvmlite.binding import ffi
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def create_new_module_pass_manager():
|
|
7
|
+
return ModulePassManager()
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def create_new_function_pass_manager():
|
|
11
|
+
return FunctionPassManager()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def create_pass_builder(tm, pto):
|
|
15
|
+
return PassBuilder(tm, pto)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def create_pipeline_tuning_options(speed_level=2, size_level=0):
|
|
19
|
+
return PipelineTuningOptions(speed_level, size_level)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class RefPruneSubpasses(IntFlag):
|
|
23
|
+
PER_BB = 0b0001 # noqa: E221
|
|
24
|
+
DIAMOND = 0b0010 # noqa: E221
|
|
25
|
+
FANOUT = 0b0100 # noqa: E221
|
|
26
|
+
FANOUT_RAISE = 0b1000
|
|
27
|
+
ALL = PER_BB | DIAMOND | FANOUT | FANOUT_RAISE
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ModulePassManager(ffi.ObjectRef):
|
|
31
|
+
|
|
32
|
+
def __init__(self, ptr=None):
|
|
33
|
+
if ptr is None:
|
|
34
|
+
ptr = ffi.lib.LLVMPY_CreateNewModulePassManager()
|
|
35
|
+
super().__init__(ptr)
|
|
36
|
+
|
|
37
|
+
def run(self, module, pb):
|
|
38
|
+
ffi.lib.LLVMPY_RunNewModulePassManager(self, module, pb)
|
|
39
|
+
|
|
40
|
+
def add_verifier(self):
|
|
41
|
+
ffi.lib.LLVMPY_AddVerifierPass(self)
|
|
42
|
+
|
|
43
|
+
def add_aa_eval_pass(self):
|
|
44
|
+
ffi.lib.LLVMPY_AddAAEvalPass_module(self)
|
|
45
|
+
|
|
46
|
+
def add_simplify_cfg_pass(self):
|
|
47
|
+
ffi.lib.LLVMPY_AddSimplifyCFGPass_module(self)
|
|
48
|
+
|
|
49
|
+
def add_loop_unroll_pass(self):
|
|
50
|
+
ffi.lib.LLVMPY_AddLoopUnrollPass_module(self)
|
|
51
|
+
|
|
52
|
+
def add_loop_rotate_pass(self):
|
|
53
|
+
ffi.lib.LLVMPY_AddLoopRotatePass_module(self)
|
|
54
|
+
|
|
55
|
+
def add_instruction_combine_pass(self):
|
|
56
|
+
ffi.lib.LLVMPY_AddInstructionCombinePass_module(self)
|
|
57
|
+
|
|
58
|
+
def add_jump_threading_pass(self, threshold=-1):
|
|
59
|
+
ffi.lib.LLVMPY_AddJumpThreadingPass_module(self, threshold)
|
|
60
|
+
|
|
61
|
+
def _dispose(self):
|
|
62
|
+
ffi.lib.LLVMPY_DisposeNewModulePassManger(self)
|
|
63
|
+
|
|
64
|
+
# Non-standard LLVM passes
|
|
65
|
+
def add_refprune_pass(self, subpasses_flags=RefPruneSubpasses.ALL,
|
|
66
|
+
subgraph_limit=1000):
|
|
67
|
+
"""Add Numba specific Reference count pruning pass.
|
|
68
|
+
|
|
69
|
+
Parameters
|
|
70
|
+
----------
|
|
71
|
+
subpasses_flags : RefPruneSubpasses
|
|
72
|
+
A bitmask to control the subpasses to be enabled.
|
|
73
|
+
subgraph_limit : int
|
|
74
|
+
Limit the fanout pruners to working on a subgraph no bigger than
|
|
75
|
+
this number of basic-blocks to avoid spending too much time in very
|
|
76
|
+
large graphs. Default is 1000. Subject to change in future
|
|
77
|
+
versions.
|
|
78
|
+
"""
|
|
79
|
+
iflags = RefPruneSubpasses(subpasses_flags)
|
|
80
|
+
ffi.lib.LLVMPY_AddRefPrunePass_module(self, iflags, subgraph_limit)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class FunctionPassManager(ffi.ObjectRef):
|
|
84
|
+
|
|
85
|
+
def __init__(self, ptr=None):
|
|
86
|
+
if ptr is None:
|
|
87
|
+
ptr = ffi.lib.LLVMPY_CreateNewFunctionPassManager()
|
|
88
|
+
super().__init__(ptr)
|
|
89
|
+
|
|
90
|
+
def run(self, fun, pb):
|
|
91
|
+
ffi.lib.LLVMPY_RunNewFunctionPassManager(self, fun, pb)
|
|
92
|
+
|
|
93
|
+
def add_aa_eval_pass(self):
|
|
94
|
+
ffi.lib.LLVMPY_AddAAEvalPass_function(self)
|
|
95
|
+
|
|
96
|
+
def add_simplify_cfg_pass(self):
|
|
97
|
+
ffi.lib.LLVMPY_AddSimplifyCFGPass_function(self)
|
|
98
|
+
|
|
99
|
+
def add_loop_unroll_pass(self):
|
|
100
|
+
ffi.lib.LLVMPY_AddLoopUnrollPass_function(self)
|
|
101
|
+
|
|
102
|
+
def add_loop_rotate_pass(self):
|
|
103
|
+
ffi.lib.LLVMPY_AddLoopRotatePass_function(self)
|
|
104
|
+
|
|
105
|
+
def add_instruction_combine_pass(self):
|
|
106
|
+
ffi.lib.LLVMPY_AddInstructionCombinePass_function(self)
|
|
107
|
+
|
|
108
|
+
def add_jump_threading_pass(self, threshold=-1):
|
|
109
|
+
ffi.lib.LLVMPY_AddJumpThreadingPass_function(self, threshold)
|
|
110
|
+
|
|
111
|
+
def _dispose(self):
|
|
112
|
+
ffi.lib.LLVMPY_DisposeNewFunctionPassManger(self)
|
|
113
|
+
|
|
114
|
+
# Non-standard LLVM passes
|
|
115
|
+
def add_refprune_pass(self, subpasses_flags=RefPruneSubpasses.ALL,
|
|
116
|
+
subgraph_limit=1000):
|
|
117
|
+
"""Add Numba specific Reference count pruning pass.
|
|
118
|
+
|
|
119
|
+
Parameters
|
|
120
|
+
----------
|
|
121
|
+
subpasses_flags : RefPruneSubpasses
|
|
122
|
+
A bitmask to control the subpasses to be enabled.
|
|
123
|
+
subgraph_limit : int
|
|
124
|
+
Limit the fanout pruners to working on a subgraph no bigger than
|
|
125
|
+
this number of basic-blocks to avoid spending too much time in very
|
|
126
|
+
large graphs. Default is 1000. Subject to change in future
|
|
127
|
+
versions.
|
|
128
|
+
"""
|
|
129
|
+
iflags = RefPruneSubpasses(subpasses_flags)
|
|
130
|
+
ffi.lib.LLVMPY_AddRefPrunePass_function(self, iflags, subgraph_limit)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class PipelineTuningOptions(ffi.ObjectRef):
|
|
134
|
+
|
|
135
|
+
def __init__(self, speed_level=2, size_level=0):
|
|
136
|
+
self._speed_level = None
|
|
137
|
+
self._size_level = None
|
|
138
|
+
self.speed_level = speed_level
|
|
139
|
+
self.size_level = size_level
|
|
140
|
+
super().__init__(ffi.lib.LLVMPY_CreatePipelineTuningOptions())
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def speed_level(self):
|
|
144
|
+
return self._speed_level
|
|
145
|
+
|
|
146
|
+
@speed_level.setter
|
|
147
|
+
def speed_level(self, value):
|
|
148
|
+
if not 0 <= value <= 3:
|
|
149
|
+
raise ValueError(
|
|
150
|
+
"Optimization level for speed should be 0, 1, 2, or 3")
|
|
151
|
+
self._speed_level = value
|
|
152
|
+
|
|
153
|
+
@property
|
|
154
|
+
def size_level(self):
|
|
155
|
+
return self._size_level
|
|
156
|
+
|
|
157
|
+
@size_level.setter
|
|
158
|
+
def size_level(self, value):
|
|
159
|
+
if not 0 <= value <= 2:
|
|
160
|
+
raise ValueError("Optimization level for size should be 0, 1, or 2")
|
|
161
|
+
if value != 0 and self.speed_level != 2:
|
|
162
|
+
raise ValueError(
|
|
163
|
+
"Optimization for size should be encoded with speed level == 2")
|
|
164
|
+
self._size_level = value
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def loop_interleaving(self):
|
|
168
|
+
return ffi.lib.LLVMPY_PTOGetLoopInterleaving(self)
|
|
169
|
+
|
|
170
|
+
@loop_interleaving.setter
|
|
171
|
+
def loop_interleaving(self, value):
|
|
172
|
+
ffi.lib.LLVMPY_PTOSetLoopInterleaving(self, value)
|
|
173
|
+
|
|
174
|
+
@property
|
|
175
|
+
def loop_vectorization(self):
|
|
176
|
+
return ffi.lib.LLVMPY_PTOGetLoopVectorization(self)
|
|
177
|
+
|
|
178
|
+
@loop_vectorization.setter
|
|
179
|
+
def loop_vectorization(self, value):
|
|
180
|
+
ffi.lib.LLVMPY_PTOSetLoopVectorization(self, value)
|
|
181
|
+
|
|
182
|
+
@property
|
|
183
|
+
def slp_vectorization(self):
|
|
184
|
+
return ffi.lib.LLVMPY_PTOGetSLPVectorization(self)
|
|
185
|
+
|
|
186
|
+
@slp_vectorization.setter
|
|
187
|
+
def slp_vectorization(self, value):
|
|
188
|
+
ffi.lib.LLVMPY_PTOSetSLPVectorization(self, value)
|
|
189
|
+
|
|
190
|
+
@property
|
|
191
|
+
def loop_unrolling(self):
|
|
192
|
+
return ffi.lib.LLVMPY_PTOGetLoopUnrolling(self)
|
|
193
|
+
|
|
194
|
+
@loop_unrolling.setter
|
|
195
|
+
def loop_unrolling(self, value):
|
|
196
|
+
ffi.lib.LLVMPY_PTOSetLoopUnrolling(self, value)
|
|
197
|
+
|
|
198
|
+
# // FIXME: Available from llvm16
|
|
199
|
+
# @property
|
|
200
|
+
# def inlining_threshold(self):
|
|
201
|
+
# return ffi.lib.LLVMPY_PTOGetInlinerThreshold(self)
|
|
202
|
+
|
|
203
|
+
# @inlining_threshold.setter
|
|
204
|
+
# def inlining_threshold(self, value):
|
|
205
|
+
# ffi.lib.LLVMPY_PTOSetInlinerThreshold(self, value)
|
|
206
|
+
|
|
207
|
+
def _dispose(self):
|
|
208
|
+
ffi.lib.LLVMPY_DisposePipelineTuningOptions(self)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
class PassBuilder(ffi.ObjectRef):
|
|
212
|
+
|
|
213
|
+
def __init__(self, tm, pto):
|
|
214
|
+
super().__init__(ffi.lib.LLVMPY_CreatePassBuilder(tm, pto))
|
|
215
|
+
self._pto = pto
|
|
216
|
+
self._tm = tm
|
|
217
|
+
|
|
218
|
+
def getModulePassManager(self):
|
|
219
|
+
return ModulePassManager(
|
|
220
|
+
ffi.lib.LLVMPY_buildPerModuleDefaultPipeline(
|
|
221
|
+
self, self._pto.speed_level, self._pto.size_level)
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
def getFunctionPassManager(self):
|
|
225
|
+
return FunctionPassManager(
|
|
226
|
+
ffi.lib.LLVMPY_buildFunctionSimplificationPipeline(
|
|
227
|
+
self, self._pto.speed_level, self._pto.size_level)
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
def _dispose(self):
|
|
231
|
+
ffi.lib.LLVMPY_DisposePassBuilder(self)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
# ============================================================================
|
|
235
|
+
# FFI
|
|
236
|
+
|
|
237
|
+
# ModulePassManager
|
|
238
|
+
|
|
239
|
+
ffi.lib.LLVMPY_CreateNewModulePassManager.restype = ffi.LLVMModulePassManagerRef
|
|
240
|
+
|
|
241
|
+
ffi.lib.LLVMPY_RunNewModulePassManager.argtypes = [
|
|
242
|
+
ffi.LLVMModulePassManagerRef, ffi.LLVMModuleRef,
|
|
243
|
+
ffi.LLVMPassBuilderRef,]
|
|
244
|
+
|
|
245
|
+
ffi.lib.LLVMPY_AddVerifierPass.argtypes = [ffi.LLVMModulePassManagerRef,]
|
|
246
|
+
ffi.lib.LLVMPY_AddAAEvalPass_module.argtypes = [ffi.LLVMModulePassManagerRef,]
|
|
247
|
+
ffi.lib.LLVMPY_AddSimplifyCFGPass_module.argtypes = [
|
|
248
|
+
ffi.LLVMModulePassManagerRef,]
|
|
249
|
+
|
|
250
|
+
ffi.lib.LLVMPY_AddLoopUnrollPass_module.argtypes = [
|
|
251
|
+
ffi.LLVMModulePassManagerRef,]
|
|
252
|
+
|
|
253
|
+
ffi.lib.LLVMPY_AddLoopRotatePass_module.argtypes = [
|
|
254
|
+
ffi.LLVMModulePassManagerRef,]
|
|
255
|
+
|
|
256
|
+
ffi.lib.LLVMPY_AddInstructionCombinePass_module.argtypes = [
|
|
257
|
+
ffi.LLVMModulePassManagerRef,]
|
|
258
|
+
|
|
259
|
+
ffi.lib.LLVMPY_AddJumpThreadingPass_module.argtypes = [
|
|
260
|
+
ffi.LLVMModulePassManagerRef,]
|
|
261
|
+
|
|
262
|
+
ffi.lib.LLVMPY_DisposeNewModulePassManger.argtypes = [
|
|
263
|
+
ffi.LLVMModulePassManagerRef,]
|
|
264
|
+
|
|
265
|
+
ffi.lib.LLVMPY_AddRefPrunePass_module.argtypes = [
|
|
266
|
+
ffi.LLVMModulePassManagerRef, c_int, c_size_t,
|
|
267
|
+
]
|
|
268
|
+
|
|
269
|
+
# FunctionPassManager
|
|
270
|
+
|
|
271
|
+
ffi.lib.LLVMPY_CreateNewFunctionPassManager.restype = \
|
|
272
|
+
ffi.LLVMFunctionPassManagerRef
|
|
273
|
+
|
|
274
|
+
ffi.lib.LLVMPY_RunNewFunctionPassManager.argtypes = [
|
|
275
|
+
ffi.LLVMFunctionPassManagerRef, ffi.LLVMValueRef,
|
|
276
|
+
ffi.LLVMPassBuilderRef,]
|
|
277
|
+
|
|
278
|
+
ffi.lib.LLVMPY_AddAAEvalPass_function.argtypes = [
|
|
279
|
+
ffi.LLVMFunctionPassManagerRef,]
|
|
280
|
+
|
|
281
|
+
ffi.lib.LLVMPY_AddSimplifyCFGPass_function.argtypes = [
|
|
282
|
+
ffi.LLVMFunctionPassManagerRef,]
|
|
283
|
+
|
|
284
|
+
ffi.lib.LLVMPY_AddLoopUnrollPass_function.argtypes = [
|
|
285
|
+
ffi.LLVMFunctionPassManagerRef,]
|
|
286
|
+
|
|
287
|
+
ffi.lib.LLVMPY_AddLoopRotatePass_function.argtypes = [
|
|
288
|
+
ffi.LLVMFunctionPassManagerRef,]
|
|
289
|
+
|
|
290
|
+
ffi.lib.LLVMPY_AddInstructionCombinePass_function.argtypes = [
|
|
291
|
+
ffi.LLVMFunctionPassManagerRef,]
|
|
292
|
+
|
|
293
|
+
ffi.lib.LLVMPY_AddJumpThreadingPass_function.argtypes = [
|
|
294
|
+
ffi.LLVMFunctionPassManagerRef, c_int,]
|
|
295
|
+
|
|
296
|
+
ffi.lib.LLVMPY_DisposeNewFunctionPassManger.argtypes = [
|
|
297
|
+
ffi.LLVMFunctionPassManagerRef,]
|
|
298
|
+
|
|
299
|
+
ffi.lib.LLVMPY_AddRefPrunePass_function.argtypes = [
|
|
300
|
+
ffi.LLVMFunctionPassManagerRef, c_int, c_size_t,
|
|
301
|
+
]
|
|
302
|
+
|
|
303
|
+
# PipelineTuningOptions
|
|
304
|
+
|
|
305
|
+
ffi.lib.LLVMPY_CreatePipelineTuningOptions.restype = \
|
|
306
|
+
ffi.LLVMPipelineTuningOptionsRef
|
|
307
|
+
|
|
308
|
+
ffi.lib.LLVMPY_PTOGetLoopInterleaving.restype = c_bool
|
|
309
|
+
ffi.lib.LLVMPY_PTOGetLoopInterleaving.argtypes = [
|
|
310
|
+
ffi.LLVMPipelineTuningOptionsRef,]
|
|
311
|
+
|
|
312
|
+
ffi.lib.LLVMPY_PTOSetLoopInterleaving.argtypes = [
|
|
313
|
+
ffi.LLVMPipelineTuningOptionsRef, c_bool]
|
|
314
|
+
|
|
315
|
+
ffi.lib.LLVMPY_PTOGetLoopVectorization.restype = c_bool
|
|
316
|
+
ffi.lib.LLVMPY_PTOGetLoopVectorization.argtypes = [
|
|
317
|
+
ffi.LLVMPipelineTuningOptionsRef,]
|
|
318
|
+
|
|
319
|
+
ffi.lib.LLVMPY_PTOSetLoopVectorization.argtypes = [
|
|
320
|
+
ffi.LLVMPipelineTuningOptionsRef, c_bool]
|
|
321
|
+
|
|
322
|
+
ffi.lib.LLVMPY_PTOGetSLPVectorization.restype = c_bool
|
|
323
|
+
ffi.lib.LLVMPY_PTOGetSLPVectorization.argtypes = [
|
|
324
|
+
ffi.LLVMPipelineTuningOptionsRef,]
|
|
325
|
+
|
|
326
|
+
ffi.lib.LLVMPY_PTOSetSLPVectorization.argtypes = [
|
|
327
|
+
ffi.LLVMPipelineTuningOptionsRef, c_bool]
|
|
328
|
+
|
|
329
|
+
ffi.lib.LLVMPY_PTOGetLoopUnrolling.restype = c_bool
|
|
330
|
+
ffi.lib.LLVMPY_PTOGetLoopUnrolling.argtypes = [
|
|
331
|
+
ffi.LLVMPipelineTuningOptionsRef,]
|
|
332
|
+
|
|
333
|
+
ffi.lib.LLVMPY_PTOSetLoopUnrolling.argtypes = [
|
|
334
|
+
ffi.LLVMPipelineTuningOptionsRef, c_bool]
|
|
335
|
+
|
|
336
|
+
ffi.lib.LLVMPY_DisposePipelineTuningOptions.argtypes = \
|
|
337
|
+
[ffi.LLVMPipelineTuningOptionsRef,]
|
|
338
|
+
|
|
339
|
+
# PassBuilder
|
|
340
|
+
|
|
341
|
+
ffi.lib.LLVMPY_CreatePassBuilder.restype = ffi.LLVMPassBuilderRef
|
|
342
|
+
ffi.lib.LLVMPY_CreatePassBuilder.argtypes = [ffi.LLVMTargetMachineRef,
|
|
343
|
+
ffi.LLVMPipelineTuningOptionsRef,]
|
|
344
|
+
|
|
345
|
+
ffi.lib.LLVMPY_DisposePassBuilder.argtypes = [ffi.LLVMPassBuilderRef,]
|
|
346
|
+
|
|
347
|
+
# Pipeline builders
|
|
348
|
+
|
|
349
|
+
ffi.lib.LLVMPY_buildPerModuleDefaultPipeline.restype = \
|
|
350
|
+
ffi.LLVMModulePassManagerRef
|
|
351
|
+
ffi.lib.LLVMPY_buildPerModuleDefaultPipeline.argtypes = [
|
|
352
|
+
ffi.LLVMPassBuilderRef, c_int, c_int]
|
|
353
|
+
|
|
354
|
+
ffi.lib.LLVMPY_buildFunctionSimplificationPipeline.restype = \
|
|
355
|
+
ffi.LLVMFunctionPassManagerRef
|
|
356
|
+
ffi.lib.LLVMPY_buildFunctionSimplificationPipeline.argtypes = [
|
|
357
|
+
ffi.LLVMPassBuilderRef, c_int, c_int]
|