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
llvmlite/ir/module.py ADDED
@@ -0,0 +1,256 @@
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 str_ditok_operands(self, operands):
46
+ str_ops = []
47
+ for name, op in operands:
48
+ if name == 'encoding' and isinstance(op, values.DIToken):
49
+ # use string value instead of address of object
50
+ op = op.value
51
+ str_ops.append((name, op))
52
+ return str_ops
53
+
54
+ def add_metadata(self, operands):
55
+ """
56
+ Add an unnamed metadata to the module with the given *operands*
57
+ (a sequence of values) or return a previous equivalent metadata.
58
+ A MDValue instance is returned, it can then be associated to
59
+ e.g. an instruction.
60
+ """
61
+ if not isinstance(operands, (list, tuple)):
62
+ raise TypeError("expected a list or tuple of metadata values, "
63
+ "got %r" % (operands,))
64
+ operands = self._fix_metadata_operands(operands)
65
+ key = tuple(operands)
66
+ if key not in self._metadatacache:
67
+ n = len(self.metadata)
68
+ md = values.MDValue(self, operands, name=str(n))
69
+ self._metadatacache[key] = md
70
+ else:
71
+ md = self._metadatacache[key]
72
+ return md
73
+
74
+ def add_debug_info(self, kind, operands, is_distinct=False):
75
+ """
76
+ Add debug information metadata to the module with the given
77
+ *operands* (a dict of values with string keys) or return
78
+ a previous equivalent metadata. *kind* is a string of the
79
+ debug information kind (e.g. "DICompileUnit").
80
+
81
+ A DIValue instance is returned, it can then be associated to e.g.
82
+ an instruction.
83
+ """
84
+ operands = tuple(sorted(self._fix_di_operands(operands.items())))
85
+ str_op_key = tuple(sorted(self.str_ditok_operands(operands)))
86
+ key = (kind, str_op_key, is_distinct)
87
+ if key not in self._metadatacache:
88
+ n = len(self.metadata)
89
+ di = values.DIValue(self, is_distinct, kind, operands, name=str(n))
90
+ self._metadatacache[key] = di
91
+ else:
92
+ di = self._metadatacache[key]
93
+ return di
94
+
95
+ def add_named_metadata(self, name, element=None):
96
+ """
97
+ Add a named metadata node to the module, if it doesn't exist,
98
+ or return the existing node.
99
+ If *element* is given, it will append a new element to
100
+ the named metadata node. If *element* is a sequence of values
101
+ (rather than a metadata value), a new unnamed node will first be
102
+ created.
103
+
104
+ Example::
105
+ module.add_named_metadata("llvm.ident", ["llvmlite/1.0"])
106
+ """
107
+ if name in self.namedmetadata:
108
+ nmd = self.namedmetadata[name]
109
+ else:
110
+ nmd = self.namedmetadata[name] = values.NamedMetaData(self)
111
+ if element is not None:
112
+ if not isinstance(element, values.Value):
113
+ element = self.add_metadata(element)
114
+ if not isinstance(element.type, types.MetaDataType):
115
+ raise TypeError("wrong type for metadata element: got %r"
116
+ % (element,))
117
+ nmd.add(element)
118
+ return nmd
119
+
120
+ def get_named_metadata(self, name):
121
+ """
122
+ Return the metadata node with the given *name*. KeyError is raised
123
+ if no such node exists (contrast with add_named_metadata()).
124
+ """
125
+ return self.namedmetadata[name]
126
+
127
+ @property
128
+ def functions(self):
129
+ """
130
+ A list of functions declared or defined in this module.
131
+ """
132
+ return [v for v in self.globals.values()
133
+ if isinstance(v, values.Function)]
134
+
135
+ @property
136
+ def global_values(self):
137
+ """
138
+ An iterable of global values in this module.
139
+ """
140
+ return self.globals.values()
141
+
142
+ def get_global(self, name):
143
+ """
144
+ Get a global value by name.
145
+ """
146
+ return self.globals[name]
147
+
148
+ def add_global(self, globalvalue):
149
+ """
150
+ Add a new global value.
151
+ """
152
+ assert globalvalue.name not in self.globals
153
+ self.globals[globalvalue.name] = globalvalue
154
+
155
+ def get_unique_name(self, name=''):
156
+ """
157
+ Get a unique global name with the following *name* hint.
158
+ """
159
+ return self.scope.deduplicate(name)
160
+
161
+ def declare_intrinsic(self, intrinsic, tys=(), fnty=None):
162
+ def _error():
163
+ raise NotImplementedError("unknown intrinsic %r with %d types"
164
+ % (intrinsic, len(tys)))
165
+
166
+ if intrinsic in {'llvm.cttz', 'llvm.ctlz', 'llvm.fma'}:
167
+ suffixes = [tys[0].intrinsic_name]
168
+ else:
169
+ suffixes = [t.intrinsic_name for t in tys]
170
+ name = '.'.join([intrinsic] + suffixes)
171
+ if name in self.globals:
172
+ return self.globals[name]
173
+
174
+ if fnty is not None:
175
+ # General case: function type is given
176
+ pass
177
+ # Compute function type if omitted for common cases
178
+ elif len(tys) == 0 and intrinsic == 'llvm.assume':
179
+ fnty = types.FunctionType(types.VoidType(), [types.IntType(1)])
180
+ elif len(tys) == 1:
181
+ if intrinsic == 'llvm.powi':
182
+ fnty = types.FunctionType(tys[0], [tys[0], types.IntType(32)])
183
+ elif intrinsic == 'llvm.pow':
184
+ fnty = types.FunctionType(tys[0], tys * 2)
185
+ elif intrinsic == 'llvm.convert.from.fp16':
186
+ fnty = types.FunctionType(tys[0], [types.IntType(16)])
187
+ elif intrinsic == 'llvm.convert.to.fp16':
188
+ fnty = types.FunctionType(types.IntType(16), tys)
189
+ else:
190
+ fnty = types.FunctionType(tys[0], tys)
191
+ elif len(tys) == 2:
192
+ if intrinsic == 'llvm.memset':
193
+ tys = [tys[0], types.IntType(8), tys[1],
194
+ types.IntType(1)]
195
+ fnty = types.FunctionType(types.VoidType(), tys)
196
+ elif intrinsic in {'llvm.cttz', 'llvm.ctlz'}:
197
+ tys = [tys[0], types.IntType(1)]
198
+ fnty = types.FunctionType(tys[0], tys)
199
+ else:
200
+ _error()
201
+ elif len(tys) == 3:
202
+ if intrinsic in ('llvm.memcpy', 'llvm.memmove'):
203
+ tys = tys + [types.IntType(1)]
204
+ fnty = types.FunctionType(types.VoidType(), tys)
205
+ elif intrinsic == 'llvm.fma':
206
+ tys = [tys[0]] * 3
207
+ fnty = types.FunctionType(tys[0], tys)
208
+ else:
209
+ _error()
210
+ else:
211
+ _error()
212
+ return values.Function(self, fnty, name=name)
213
+
214
+ def get_identified_types(self):
215
+ return self.context.identified_types
216
+
217
+ def _get_body_lines(self):
218
+ # Type declarations
219
+ lines = [it.get_declaration()
220
+ for it in self.get_identified_types().values()]
221
+ # Global values (including function definitions)
222
+ lines += [str(v) for v in self.globals.values()]
223
+ return lines
224
+
225
+ def _get_metadata_lines(self):
226
+ mdbuf = []
227
+ for k, v in self.namedmetadata.items():
228
+ mdbuf.append("!{name} = !{{ {operands} }}".format(
229
+ name=k, operands=', '.join(i.get_reference()
230
+ for i in v.operands)))
231
+ for md in self.metadata:
232
+ mdbuf.append(str(md))
233
+ return mdbuf
234
+
235
+ def _stringify_body(self):
236
+ # For testing
237
+ return "\n".join(self._get_body_lines())
238
+
239
+ def _stringify_metadata(self):
240
+ # For testing
241
+ return "\n".join(self._get_metadata_lines())
242
+
243
+ def __repr__(self):
244
+ lines = []
245
+ # Header
246
+ lines += [
247
+ '; ModuleID = "%s"' % (self.name,),
248
+ 'target triple = "%s"' % (self.triple,),
249
+ 'target datalayout = "%s"' % (self.data_layout,),
250
+ '']
251
+ # Body
252
+ lines += self._get_body_lines()
253
+ # Metadata
254
+ lines += self._get_metadata_lines()
255
+
256
+ 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