llvmlite 0.44.0rc1__cp311-cp311-macosx_10_15_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.

Files changed (46) hide show
  1. llvmlite/__init__.py +10 -0
  2. llvmlite/_version.py +11 -0
  3. llvmlite/binding/__init__.py +19 -0
  4. llvmlite/binding/analysis.py +69 -0
  5. llvmlite/binding/common.py +34 -0
  6. llvmlite/binding/context.py +39 -0
  7. llvmlite/binding/dylib.py +45 -0
  8. llvmlite/binding/executionengine.py +330 -0
  9. llvmlite/binding/ffi.py +395 -0
  10. llvmlite/binding/initfini.py +73 -0
  11. llvmlite/binding/libllvmlite.dylib +0 -0
  12. llvmlite/binding/linker.py +20 -0
  13. llvmlite/binding/module.py +349 -0
  14. llvmlite/binding/newpassmanagers.py +357 -0
  15. llvmlite/binding/object_file.py +82 -0
  16. llvmlite/binding/options.py +17 -0
  17. llvmlite/binding/orcjit.py +342 -0
  18. llvmlite/binding/passmanagers.py +946 -0
  19. llvmlite/binding/targets.py +520 -0
  20. llvmlite/binding/transforms.py +151 -0
  21. llvmlite/binding/typeref.py +285 -0
  22. llvmlite/binding/value.py +632 -0
  23. llvmlite/ir/__init__.py +11 -0
  24. llvmlite/ir/_utils.py +80 -0
  25. llvmlite/ir/builder.py +1120 -0
  26. llvmlite/ir/context.py +20 -0
  27. llvmlite/ir/instructions.py +920 -0
  28. llvmlite/ir/module.py +246 -0
  29. llvmlite/ir/transforms.py +64 -0
  30. llvmlite/ir/types.py +734 -0
  31. llvmlite/ir/values.py +1217 -0
  32. llvmlite/tests/__init__.py +57 -0
  33. llvmlite/tests/__main__.py +3 -0
  34. llvmlite/tests/customize.py +407 -0
  35. llvmlite/tests/refprune_proto.py +329 -0
  36. llvmlite/tests/test_binding.py +3208 -0
  37. llvmlite/tests/test_ir.py +2986 -0
  38. llvmlite/tests/test_refprune.py +730 -0
  39. llvmlite/tests/test_valuerepr.py +60 -0
  40. llvmlite/utils.py +29 -0
  41. llvmlite-0.44.0rc1.dist-info/LICENSE +24 -0
  42. llvmlite-0.44.0rc1.dist-info/LICENSE.thirdparty +225 -0
  43. llvmlite-0.44.0rc1.dist-info/METADATA +138 -0
  44. llvmlite-0.44.0rc1.dist-info/RECORD +46 -0
  45. llvmlite-0.44.0rc1.dist-info/WHEEL +5 -0
  46. llvmlite-0.44.0rc1.dist-info/top_level.txt +1 -0
llvmlite/ir/module.py ADDED
@@ -0,0 +1,246 @@
1
+ import collections
2
+
3
+ from llvmlite.ir import context, values, types, _utils
4
+
5
+
6
+ class Module(object):
7
+ def __init__(self, name='', context=context.global_context):
8
+ self.context = context
9
+ self.name = name # name is for debugging/informational
10
+ self.data_layout = ""
11
+ self.scope = _utils.NameScope()
12
+ self.triple = 'unknown-unknown-unknown'
13
+ self.globals = collections.OrderedDict()
14
+ # Innamed metadata nodes.
15
+ self.metadata = []
16
+ # Named metadata nodes
17
+ self.namedmetadata = {}
18
+ # Cache for metadata node deduplication
19
+ self._metadatacache = {}
20
+
21
+ def _fix_metadata_operands(self, operands):
22
+ fixed_ops = []
23
+ for op in operands:
24
+ if op is None:
25
+ # A literal None creates a null metadata value
26
+ op = types.MetaDataType()(None)
27
+ elif isinstance(op, str):
28
+ # A literal string creates a metadata string value
29
+ op = values.MetaDataString(self, op)
30
+ elif isinstance(op, (list, tuple)):
31
+ # A sequence creates a metadata node reference
32
+ op = self.add_metadata(op)
33
+ fixed_ops.append(op)
34
+ return fixed_ops
35
+
36
+ def _fix_di_operands(self, operands):
37
+ fixed_ops = []
38
+ for name, op in operands:
39
+ if isinstance(op, (list, tuple)):
40
+ # A sequence creates a metadata node reference
41
+ op = self.add_metadata(op)
42
+ fixed_ops.append((name, op))
43
+ return fixed_ops
44
+
45
+ def add_metadata(self, operands):
46
+ """
47
+ Add an unnamed metadata to the module with the given *operands*
48
+ (a sequence of values) or return a previous equivalent metadata.
49
+ A MDValue instance is returned, it can then be associated to
50
+ e.g. an instruction.
51
+ """
52
+ if not isinstance(operands, (list, tuple)):
53
+ raise TypeError("expected a list or tuple of metadata values, "
54
+ "got %r" % (operands,))
55
+ operands = self._fix_metadata_operands(operands)
56
+ key = tuple(operands)
57
+ if key not in self._metadatacache:
58
+ n = len(self.metadata)
59
+ md = values.MDValue(self, operands, name=str(n))
60
+ self._metadatacache[key] = md
61
+ else:
62
+ md = self._metadatacache[key]
63
+ return md
64
+
65
+ def add_debug_info(self, kind, operands, is_distinct=False):
66
+ """
67
+ Add debug information metadata to the module with the given
68
+ *operands* (a dict of values with string keys) or return
69
+ a previous equivalent metadata. *kind* is a string of the
70
+ debug information kind (e.g. "DICompileUnit").
71
+
72
+ A DIValue instance is returned, it can then be associated to e.g.
73
+ an instruction.
74
+ """
75
+ operands = tuple(sorted(self._fix_di_operands(operands.items())))
76
+ key = (kind, operands, is_distinct)
77
+ if key not in self._metadatacache:
78
+ n = len(self.metadata)
79
+ di = values.DIValue(self, is_distinct, kind, operands, name=str(n))
80
+ self._metadatacache[key] = di
81
+ else:
82
+ di = self._metadatacache[key]
83
+ return di
84
+
85
+ def add_named_metadata(self, name, element=None):
86
+ """
87
+ Add a named metadata node to the module, if it doesn't exist,
88
+ or return the existing node.
89
+ If *element* is given, it will append a new element to
90
+ the named metadata node. If *element* is a sequence of values
91
+ (rather than a metadata value), a new unnamed node will first be
92
+ created.
93
+
94
+ Example::
95
+ module.add_named_metadata("llvm.ident", ["llvmlite/1.0"])
96
+ """
97
+ if name in self.namedmetadata:
98
+ nmd = self.namedmetadata[name]
99
+ else:
100
+ nmd = self.namedmetadata[name] = values.NamedMetaData(self)
101
+ if element is not None:
102
+ if not isinstance(element, values.Value):
103
+ element = self.add_metadata(element)
104
+ if not isinstance(element.type, types.MetaDataType):
105
+ raise TypeError("wrong type for metadata element: got %r"
106
+ % (element,))
107
+ nmd.add(element)
108
+ return nmd
109
+
110
+ def get_named_metadata(self, name):
111
+ """
112
+ Return the metadata node with the given *name*. KeyError is raised
113
+ if no such node exists (contrast with add_named_metadata()).
114
+ """
115
+ return self.namedmetadata[name]
116
+
117
+ @property
118
+ def functions(self):
119
+ """
120
+ A list of functions declared or defined in this module.
121
+ """
122
+ return [v for v in self.globals.values()
123
+ if isinstance(v, values.Function)]
124
+
125
+ @property
126
+ def global_values(self):
127
+ """
128
+ An iterable of global values in this module.
129
+ """
130
+ return self.globals.values()
131
+
132
+ def get_global(self, name):
133
+ """
134
+ Get a global value by name.
135
+ """
136
+ return self.globals[name]
137
+
138
+ def add_global(self, globalvalue):
139
+ """
140
+ Add a new global value.
141
+ """
142
+ assert globalvalue.name not in self.globals
143
+ self.globals[globalvalue.name] = globalvalue
144
+
145
+ def get_unique_name(self, name=''):
146
+ """
147
+ Get a unique global name with the following *name* hint.
148
+ """
149
+ return self.scope.deduplicate(name)
150
+
151
+ def declare_intrinsic(self, intrinsic, tys=(), fnty=None):
152
+ def _error():
153
+ raise NotImplementedError("unknown intrinsic %r with %d types"
154
+ % (intrinsic, len(tys)))
155
+
156
+ if intrinsic in {'llvm.cttz', 'llvm.ctlz', 'llvm.fma'}:
157
+ suffixes = [tys[0].intrinsic_name]
158
+ else:
159
+ suffixes = [t.intrinsic_name for t in tys]
160
+ name = '.'.join([intrinsic] + suffixes)
161
+ if name in self.globals:
162
+ return self.globals[name]
163
+
164
+ if fnty is not None:
165
+ # General case: function type is given
166
+ pass
167
+ # Compute function type if omitted for common cases
168
+ elif len(tys) == 0 and intrinsic == 'llvm.assume':
169
+ fnty = types.FunctionType(types.VoidType(), [types.IntType(1)])
170
+ elif len(tys) == 1:
171
+ if intrinsic == 'llvm.powi':
172
+ fnty = types.FunctionType(tys[0], [tys[0], types.IntType(32)])
173
+ elif intrinsic == 'llvm.pow':
174
+ fnty = types.FunctionType(tys[0], tys * 2)
175
+ elif intrinsic == 'llvm.convert.from.fp16':
176
+ fnty = types.FunctionType(tys[0], [types.IntType(16)])
177
+ elif intrinsic == 'llvm.convert.to.fp16':
178
+ fnty = types.FunctionType(types.IntType(16), tys)
179
+ else:
180
+ fnty = types.FunctionType(tys[0], tys)
181
+ elif len(tys) == 2:
182
+ if intrinsic == 'llvm.memset':
183
+ tys = [tys[0], types.IntType(8), tys[1],
184
+ types.IntType(1)]
185
+ fnty = types.FunctionType(types.VoidType(), tys)
186
+ elif intrinsic in {'llvm.cttz', 'llvm.ctlz'}:
187
+ tys = [tys[0], types.IntType(1)]
188
+ fnty = types.FunctionType(tys[0], tys)
189
+ else:
190
+ _error()
191
+ elif len(tys) == 3:
192
+ if intrinsic in ('llvm.memcpy', 'llvm.memmove'):
193
+ tys = tys + [types.IntType(1)]
194
+ fnty = types.FunctionType(types.VoidType(), tys)
195
+ elif intrinsic == 'llvm.fma':
196
+ tys = [tys[0]] * 3
197
+ fnty = types.FunctionType(tys[0], tys)
198
+ else:
199
+ _error()
200
+ else:
201
+ _error()
202
+ return values.Function(self, fnty, name=name)
203
+
204
+ def get_identified_types(self):
205
+ return self.context.identified_types
206
+
207
+ def _get_body_lines(self):
208
+ # Type declarations
209
+ lines = [it.get_declaration()
210
+ for it in self.get_identified_types().values()]
211
+ # Global values (including function definitions)
212
+ lines += [str(v) for v in self.globals.values()]
213
+ return lines
214
+
215
+ def _get_metadata_lines(self):
216
+ mdbuf = []
217
+ for k, v in self.namedmetadata.items():
218
+ mdbuf.append("!{name} = !{{ {operands} }}".format(
219
+ name=k, operands=', '.join(i.get_reference()
220
+ for i in v.operands)))
221
+ for md in self.metadata:
222
+ mdbuf.append(str(md))
223
+ return mdbuf
224
+
225
+ def _stringify_body(self):
226
+ # For testing
227
+ return "\n".join(self._get_body_lines())
228
+
229
+ def _stringify_metadata(self):
230
+ # For testing
231
+ return "\n".join(self._get_metadata_lines())
232
+
233
+ def __repr__(self):
234
+ lines = []
235
+ # Header
236
+ lines += [
237
+ '; ModuleID = "%s"' % (self.name,),
238
+ 'target triple = "%s"' % (self.triple,),
239
+ 'target datalayout = "%s"' % (self.data_layout,),
240
+ '']
241
+ # Body
242
+ lines += self._get_body_lines()
243
+ # Metadata
244
+ lines += self._get_metadata_lines()
245
+
246
+ return "\n".join(lines)
@@ -0,0 +1,64 @@
1
+ from llvmlite.ir import CallInstr
2
+
3
+
4
+ class Visitor(object):
5
+ def visit(self, module):
6
+ self._module = module
7
+ for func in module.functions:
8
+ self.visit_Function(func)
9
+
10
+ def visit_Function(self, func):
11
+ self._function = func
12
+ for bb in func.blocks:
13
+ self.visit_BasicBlock(bb)
14
+
15
+ def visit_BasicBlock(self, bb):
16
+ self._basic_block = bb
17
+ for instr in bb.instructions:
18
+ self.visit_Instruction(instr)
19
+
20
+ def visit_Instruction(self, instr):
21
+ raise NotImplementedError
22
+
23
+ @property
24
+ def module(self):
25
+ return self._module
26
+
27
+ @property
28
+ def function(self):
29
+ return self._function
30
+
31
+ @property
32
+ def basic_block(self):
33
+ return self._basic_block
34
+
35
+
36
+ class CallVisitor(Visitor):
37
+ def visit_Instruction(self, instr):
38
+ if isinstance(instr, CallInstr):
39
+ self.visit_Call(instr)
40
+
41
+ def visit_Call(self, instr):
42
+ raise NotImplementedError
43
+
44
+
45
+ class ReplaceCalls(CallVisitor):
46
+ def __init__(self, orig, repl):
47
+ super(ReplaceCalls, self).__init__()
48
+ self.orig = orig
49
+ self.repl = repl
50
+ self.calls = []
51
+
52
+ def visit_Call(self, instr):
53
+ if instr.callee == self.orig:
54
+ instr.replace_callee(self.repl)
55
+ self.calls.append(instr)
56
+
57
+
58
+ def replace_all_calls(mod, orig, repl):
59
+ """Replace all calls to `orig` to `repl` in module `mod`.
60
+ Returns the references to the returned calls
61
+ """
62
+ rc = ReplaceCalls(orig, repl)
63
+ rc.visit(mod)
64
+ return rc.calls