forked from APrioriInvestments/typed_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_to_native_converter.py
More file actions
395 lines (300 loc) · 14.7 KB
/
Copy pathpython_to_native_converter.py
File metadata and controls
395 lines (300 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Copyright 2018 Braxton Mckee
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import typed_python.python_ast as python_ast
import typed_python.ast_util as ast_util
import nativepython
import nativepython.native_ast as native_ast
from nativepython.type_wrappers.none_wrapper import NoneWrapper
from nativepython.python_object_representation import pythonObjectRepresentation, typedPythonTypeToTypeWrapper
from nativepython.typed_expression import TypedExpression
from nativepython.conversion_exception import ConversionException
from nativepython.expression_conversion_context import ExpressionConversionContext
from nativepython.function_conversion_context import FunctionConversionContext, FunctionOutput
NoneExprType = NoneWrapper()
from typed_python import *
typeWrapper = lambda t: nativepython.python_object_representation.typedPythonTypeToTypeWrapper(t)
class NativeFunctionConversionContext:
def __init__(self, converter, input_types, output_type, generatingFunction):
self.varnames = 0
self.converter = converter
self._input_types = input_types
self._output_type = output_type
self._generatingFunction = generatingFunction
def let_varname(self):
self.varnames += 1
return ".var_%s" % self.varnames
def stack_varname(self):
return self.let_varname()
def typesAreUnstable(self):
return False
def resetTypeInstabilityFlag(self):
pass
def convertToNativeFunction(self):
return self.getFunction(), self._output_type
def getFunction(self):
subcontext = ExpressionConversionContext(self)
output_type = self._output_type
input_types = self._input_types
generatingFunction = self._generatingFunction
if output_type.is_pass_by_ref:
outputArg = subcontext.inputArg(output_type, '.return')
else:
outputArg = None
inputArgs = [subcontext.inputArg(input_types[i], 'a_%s' % i) if not input_types[i].is_empty
else subcontext.pushPod(native_ast.nullExpr, input_types[i])
for i in range(len(input_types))]
native_input_types = [t.getNativePassingType() for t in input_types if not t.is_empty]
if output_type.is_pass_by_ref:
#the first argument is actually the output
native_output_type = native_ast.Void
native_input_types = [output_type.getNativePassingType()] + native_input_types
else:
native_output_type = output_type.getNativeLayoutType()
generatingFunction(subcontext, outputArg, *inputArgs)
native_args = [('a_%s' % i, input_types[i].getNativePassingType())
for i in range(len(input_types)) if not input_types[i].is_empty]
if output_type.is_pass_by_ref:
native_args = [('.return', output_type.getNativePassingType())] + native_args
return native_ast.Function(
args=native_args,
output_type=native_ast.Void if output_type.is_pass_by_ref else output_type.getNativeLayoutType(),
body=native_ast.FunctionBody.Internal(subcontext.finalize(None))
)
class TypedCallTarget(object):
def __init__(self, named_call_target, input_types, output_type):
super().__init__()
self.named_call_target = named_call_target
self.input_types = input_types
self.output_type = output_type
def call(self, *args):
return native_ast.CallTarget.Named(target=self.named_call_target).call(*args)
@property
def name(self):
return self.named_call_target.name
def __str__(self):
return "TypedCallTarget(name=%s,inputs=%s,outputs=%s)" % (
self.name,
[str(x) for x in self.input_types],
str(self.output_type)
)
class PythonToNativeConverter(object):
def __init__(self):
object.__init__(self)
self._names_for_identifier = {}
self._definitions = {}
self._targets = {}
self._inflight_function_conversions = {}
self._new_native_functions = set()
self._used_names = set()
self.verbose = False
def extract_new_function_definitions(self):
"""Return a list of all new function definitions from the last conversion."""
res = {}
for u in self._new_native_functions:
res[u] = self._definitions[u]
if self.verbose:
print(self._targets[u])
self._new_native_functions = set()
return res
def new_name(self, name, prefix="py."):
suffix = None
getname = lambda: prefix + name + ("" if suffix is None else ".%s" % suffix)
while getname() in self._used_names:
suffix = 1 if not suffix else suffix+1
res = getname()
self._used_names.add(res)
return res
def createConversionContext(self, identity, f, input_types, output_type):
pyast, freevars = self._callable_to_ast_and_vars(f)
if isinstance(pyast, python_ast.Statement.FunctionDef):
body = pyast.body
else:
body = [python_ast.Statement.Return(
value=ast.body,
line_number=ast.body.line_number,
col_offset=ast.body.col_offset,
filename=ast.body.filename
)]
return FunctionConversionContext(self, identity, pyast.args, pyast.body, input_types, output_type, freevars)
def defineNativeFunction(self, name, identity, input_types, output_type, generatingFunction):
"""Define a native function if we haven't defined it before already.
name - the name to actually give the function.
identity - a unique identifier for this function to allow us to cache it.
input_types - list of Wrapper objects for the incoming types
output_type - Wrapper object for the output type.
generatingFunction - a function producing a native_function_definition
returns a TypedCallTarget. 'generatingFunction' may call this recursively if it wants.
"""
output_type = typeWrapper(output_type)
input_types = [typeWrapper(x) for x in input_types]
identity = ("native", identity, tuple(input_types))
if identity in self._names_for_identifier:
return self._targets[self._names_for_identifier[identity]]
new_name = self.new_name(name, "runtime.")
self._names_for_identifier[identity] = new_name
self._inflight_function_conversions[identity] = NativeFunctionConversionContext(self, input_types, output_type, generatingFunction)
self._targets[new_name] = self.getTypedCallTarget(new_name, input_types, output_type)
return self._targets[new_name]
def getTypedCallTarget(self, name, input_types, output_type):
native_input_types = [a.getNativePassingType() for a in input_types if not a.is_empty]
if output_type is None:
native_output_type = native_ast.Type.Void()
elif output_type.is_pass_by_ref:
native_input_types = [output_type.getNativePassingType()] + native_input_types
native_output_type = native_ast.Type.Void()
else:
native_output_type = output_type.getNativeLayoutType()
return TypedCallTarget(
native_ast.NamedCallTarget(
name=name,
arg_types=native_input_types,
output_type=native_output_type,
external=False,
varargs=False,
intrinsic=False,
can_throw=True
),
input_types,
output_type
)
def _callable_to_ast_and_vars(self, f):
pyast = ast_util.pyAstFor(f)
_, lineno = ast_util.getSourceLines(f)
_, fname = ast_util.getSourceFilenameAndText(f)
pyast = ast_util.functionDefOrLambdaAtLineNumber(pyast, lineno)
pyast = python_ast.convertPyAstToAlgebraic(pyast, fname)
freevars = dict(f.__globals__)
if f.__closure__:
for i in range(len(f.__closure__)):
try:
freevars[f.__code__.co_freevars[i]] = f.__closure__[i].cell_contents
except:
print("BAD IS ", f)
raise
return pyast, freevars
def generateCallConverter(self, callTarget):
"""Given a call target that's optimized for C-style dispatch, produce a (native) call-target that
we can dispatch to from our C extension.
we are given
T f(A1, A2, A3 ...)
and want to produce
f(T*, X**)
where X is the union of A1, A2, etc.
returns the name of the defined native function
"""
identifier = ("call_converter", callTarget.name)
if identifier in self._names_for_identifier:
return self._names_for_identifier[identifier]
underlyingDefinition = self._definitions[callTarget.name]
args = []
for i in range(len(callTarget.input_types)):
if not callTarget.input_types[i].is_empty:
argtype = callTarget.input_types[i].getNativeLayoutType()
untypedPtr = native_ast.var('input').ElementPtrIntegers(i).load()
if callTarget.input_types[i].is_pass_by_ref:
#we've been handed a pointer, and it's already a pointer
args.append(untypedPtr.cast(argtype.pointer()))
else:
args.append(untypedPtr.cast(argtype.pointer()).load())
if callTarget.output_type is not None and callTarget.output_type.is_pass_by_ref:
body = callTarget.call(
native_ast.var('return').cast(callTarget.output_type.getNativeLayoutType().pointer()),
*args
)
else:
body = callTarget.call(*args)
if not (callTarget.output_type is None or callTarget.output_type.is_empty):
body = native_ast.var('return').cast(callTarget.output_type.getNativeLayoutType().pointer()).store(body)
body = native_ast.FunctionBody.Internal(body=body)
definition = native_ast.Function(
args=(
('return', native_ast.Type.Void().pointer()),
('input', native_ast.Type.Void().pointer().pointer())
),
body=body,
output_type=native_ast.Type.Void()
)
new_name = self.new_name(callTarget.name + ".dispatch")
self._names_for_identifier[identifier] = new_name
self._definitions[new_name] = definition
self._new_native_functions.add(new_name)
return new_name
def _resolveInflightOnePass(self):
oldCount = len(self._inflight_function_conversions)
repeat = False
for identity, functionConverter in list(self._inflight_function_conversions.items()):
nativeFunction, actual_output_type = functionConverter.convertToNativeFunction()
if nativeFunction is None:
repeat = True
else:
if functionConverter.typesAreUnstable():
functionConverter.resetTypeInstabilityFlag()
repeat = True
name = self._names_for_identifier[identity]
self._targets[name] = self.getTypedCallTarget(name, functionConverter._input_types, actual_output_type)
return repeat or len(self._inflight_function_conversions) != oldCount
def _resolveAllInflightFunctions(self):
passCt = 0
while self._resolveInflightOnePass():
passCt += 1
if passCt > 100:
print("We've done ", passCt, " with ", len(self._inflight_function_conversions))
for c in self._inflight_function_conversions.values():
print(" ", c.identity[1].__name__, c.identity[2], "->", c._output_type)
raise Exception("Exceed max pass count")
def convert(self, f, input_types, output_type, assertIsRoot=False):
"""Convert a single pure python function using args of 'input_types'.
It will return no more than 'output_type'. if output_type is None we produce
the tightest output type possible.
"""
input_types = tuple([typedPythonTypeToTypeWrapper(i) for i in input_types])
identifier = ("pyfunction", f, input_types, output_type)
if identifier in self._names_for_identifier:
name = self._names_for_identifier[identifier]
else:
name = self.new_name(f.__name__)
self._names_for_identifier[identifier] = name
if name in self._targets:
return self._targets[name]
isRoot = len(self._inflight_function_conversions) == 0
if assertIsRoot:
assert isRoot
if identifier not in self._inflight_function_conversions:
functionConverter = self.createConversionContext(identifier, f, input_types, output_type)
self._inflight_function_conversions[identifier] = functionConverter
if isRoot:
try:
self._resolveAllInflightFunctions()
self._installInflightFunctions()
return self._targets[name]
finally:
self._inflight_function_conversions.clear()
else:
#above us on the stack, we are walking a set of function conversions.
#if we have ever calculated this function before, we'll have a call
#target with an output type and we can return that. Otherwise we have to
#return None, which will cause callers to replace this with a throw
#until we have had a chance to do a full pass of conversion.
if name in self._targets:
return self._targets[name]
else:
return None
def _installInflightFunctions(self):
for identifier, functionConverter in self._inflight_function_conversions.items():
nativeFunction, actual_output_type = functionConverter.convertToNativeFunction()
assert nativeFunction is not None
name = self._names_for_identifier[identifier]
self._definitions[name] = nativeFunction
self._new_native_functions.add(name)
self._inflight_function_conversions.clear()