-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathAPI.py
More file actions
368 lines (280 loc) · 11.3 KB
/
Copy pathAPI.py
File metadata and controls
368 lines (280 loc) · 11.3 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
import ast as pyast
import inspect
import re
import types
from pathlib import Path
from typing import Optional, Union, List
import exo.rewrite.LoopIR_scheduling as scheduling
from exo.rewrite.LoopIR_scheduling import SchedulingError
from .API_types import ProcedureBase, ExoType
from .core import LoopIR as LoopIR
from .backend.LoopIR_compiler import run_compile, compile_to_strings
from .core.configs import Config
from .frontend.boundscheck import CheckBounds
from .core.memory import Memory
from .frontend.parse_fragment import parse_fragment
from .frontend.pattern_match import match_pattern
from .core.prelude import *
from .rewrite.new_eff import Check_Aliasing
# Moved to new file
from .core.proc_eqv import decl_new_proc, derive_proc, assert_eqv_proc, check_eqv_proc
from .frontend.pyparser import get_ast_from_python, Parser, get_parent_scope
from .frontend.typecheck import TypeChecker
from . import API_cursors as C
from .core import internal_cursors as IC
# --------------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
# Top-level decorator
def proc(f, _instr=None) -> "Procedure":
if not isinstance(f, types.FunctionType):
raise TypeError("@proc decorator must be applied to a function")
body, src_info = get_ast_from_python(f)
assert isinstance(body, pyast.FunctionDef)
parser = Parser(
body,
src_info,
parent_scope=get_parent_scope(depth=3 if _instr else 2),
instr=_instr,
as_func=True,
)
return Procedure(parser.result())
def instr(c_instr, c_global=""):
if not isinstance(c_instr, str):
raise TypeError("@instr decorator must be @instr(<your instruction>)")
def inner(f):
if not isinstance(f, types.FunctionType):
raise TypeError("@instr decorator must be applied to a function")
return proc(f, _instr=(c_instr, c_global))
return inner
def config(_cls=None, *, readwrite=True):
def parse_config(cls):
if not inspect.isclass(cls):
raise TypeError("@config decorator must be applied to a class")
body, src_info = get_ast_from_python(cls)
assert isinstance(body, pyast.ClassDef)
parser = Parser(
body,
src_info,
parent_scope=get_parent_scope(depth=2),
as_config=True,
)
return Config(*parser.result(), not readwrite)
if _cls is None:
return parse_config
else:
return parse_config(_cls)
# --------------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
# iPython Display Object
class MarkDownBlob:
def __init__(self, mkdwn_str):
self.mstr = mkdwn_str
def _repr_markdown_(self):
return self.mstr
class FindBefore(LoopIR.LoopIR_Do):
def __init__(self, proc, stmt):
self.stmt = stmt
self.result = None
super().__init__(proc)
def result(self):
return self.result
def do_stmts(self, stmts):
prev = None
for s in stmts:
if s == self.stmt:
self.result = prev
return
else:
self.do_s(s)
prev = s
class FindDup(LoopIR.LoopIR_Do):
def __init__(self, proc):
self.result = False
self.env = []
super().__init__(proc)
def result(self):
return self.result
def do_s(self, s):
for e in self.env:
if s is e:
self.result = True
print(s)
self.env.append(s)
super().do_s(s)
# --------------------------------------------------------------------------- #
# --------------------------------------------------------------------------- #
# Procedure Objects
def compile_procs(proc_list, basedir: Path, c_file: str, h_file: str):
c_data, h_data = compile_procs_to_strings(proc_list, h_file)
(basedir / c_file).write_text(c_data)
(basedir / h_file).write_text(h_data)
def compile_procs_to_strings(proc_list, h_file_name: str):
assert isinstance(proc_list, list)
assert all(isinstance(p, Procedure) for p in proc_list)
return run_compile([p._loopir_proc for p in proc_list], h_file_name)
class Procedure(ProcedureBase):
def __init__(
self,
proc,
_provenance_eq_Procedure: "Procedure" = None,
_forward=None,
_mod_config=None,
):
super().__init__()
_mod_config = _mod_config or frozenset()
if isinstance(proc, LoopIR.UAST.proc):
proc = TypeChecker(proc).get_loopir()
CheckBounds(proc)
Check_Aliasing(proc)
assert isinstance(proc, LoopIR.LoopIR.proc)
# add this procedure into the equivalence tracking mechanism
if _provenance_eq_Procedure:
derive_proc(
_provenance_eq_Procedure._loopir_proc, proc, frozenset(_mod_config)
)
else:
decl_new_proc(proc)
if _forward is None:
def _forward(_):
raise NotImplementedError(
"This forwarding function has not been implemented"
)
self._loopir_proc = proc
self._provenance_eq_Procedure = _provenance_eq_Procedure
self._forward = _forward
def forward(self, cur: C.Cursor):
p = self
fwds = []
while p is not None and p is not cur.proc():
fwds.append(p._forward)
p = p._provenance_eq_Procedure
ir = cur._impl
for fn in reversed(fwds):
ir = fn(ir)
return C.lift_cursor(ir, self)
def __str__(self):
return str(self._loopir_proc)
def __eq__(self, other):
if not isinstance(other, Procedure):
return False
return self._loopir_proc == other._loopir_proc
def _repr_markdown_(self):
return "```python\n" + self.__str__() + "\n```"
def INTERNAL_proc(self):
return self._loopir_proc
# -------------------------------- #
# introspection operations
# -------------------------------- #
def name(self):
return self._loopir_proc.name
def is_instr(self):
return self._loopir_proc.instr is not None
def get_instr(self):
return self._loopir_proc.instr.c_instr
def args(self):
if args := self._root()._child_block("args"):
return C.lift_cursor(args, self)
return []
def body(self):
"""
Return a BlockCursor selecting the entire body of the Procedure
"""
block = self._root()._child_block("body")
return C.lift_cursor(block, self)
def find(self, pattern, many=False, call_depth=1):
"""
Find the most specific possible cursor for the given pattern.
For example, a pattern matching a single assignment statement
will return an AssignCursor, not a StmtCursor or BlockCursor.
If the optional parameter `many` is set to True, then return a list,
potentially containing more than one Cursor.
In any event, if no matches are found, a SchedulingError is raised
"""
return C.find(self._root(), self, pattern, many, call_depth=call_depth + 1)
def find_loop(self, pattern, many=False):
"""
This is the same as proc.find(...), except if the supplied pattern
is of the form 'name' or 'name #n', then it will be auto-expanded
to 'for name in _:_' or 'for name in _:_ #n'
"""
if not isinstance(pattern, str):
raise TypeError("expected a pattern string")
_name_count_re = r"^([a-zA-Z_]\w*)\s*(\#\s*[0-9]+)?$"
results = re.search(_name_count_re, pattern)
if results:
name, count = results[1], (results[2] if results[2] else "")
pattern = f"for {name} in _: _{count}"
return self.find(pattern, many, call_depth=1)
def find_alloc_or_arg(self, pattern):
_name_count_re = r"^([a-zA-Z_]\w*)\s*(\#\s*[0-9]+)?$"
results = re.search(_name_count_re, pattern)
if results:
name, count = results[1], (results[2] if results[2] else "")
for arg in self.args():
if arg._impl._node.name.name() == name:
return arg
pattern = f"{name}: _{count}"
return self.find(pattern, call_depth=1)
def find_all(self, pattern):
return self.find(pattern, many=True, call_depth=1)
# ---------------------------------------------- #
# execution / compilation operations
# ---------------------------------------------- #
def c_code_str(self):
decls, defns = compile_to_strings("c_code_str", [self._loopir_proc])
return decls + "\n" + defns
def compile_c(self, directory: Path, filename: str):
compile_procs([self], directory, f"{filename}.c", f"{filename}.h")
# ------------------------------- #
# scheduling operations
# ------------------------------- #
def has_dup(self):
"""
Internal check to see if there are any reference diamonds in the AST
"""
return FindDup(self._loopir_proc).result
def unsafe_assert_eq(self, other_proc):
if not isinstance(other_proc, Procedure):
raise TypeError("expected a procedure as argument")
assert_eqv_proc(self._loopir_proc, other_proc._loopir_proc)
return self
def partial_eval(self, *args, **kwargs):
if kwargs and args:
raise ValueError("Must provide EITHER ordered OR named arguments")
if not kwargs and not args:
# Nothing to do if empty partial eval
return self
p = self._loopir_proc
if args:
if len(args) > len(p.args):
raise TypeError(
f"expected no more than {len(p.args)} "
f"arguments, but got {len(args)}"
)
kwargs = {arg.name: val for arg, val in zip(p.args, args)}
else:
# Get the symbols corresponding to the names
params_map = {sym.name.name(): sym.name for sym in p.args}
kwargs = {params_map[k]: v for k, v in kwargs.items()}
p = scheduling.DoPartialEval(kwargs).apply_proc(p)
return Procedure(p) # No provenance because signature changed
def transpose(self, arg_cursor):
if not (isinstance(arg_cursor, C.ArgCursor) and len(arg_cursor.shape()) == 2):
raise TypeError("expected a 2D argument cursor")
ir, _ = scheduling.DoRearrangeDim(arg_cursor._impl, [1, 0])
return Procedure(ir) # No provenance because signature changed
def add_assertion(self, assertion, configs=None):
if not isinstance(assertion, str):
raise TypeError("assertion must be an Exo string")
configs = configs or []
p = self._loopir_proc
assertion = parse_fragment(p, assertion, p.body[0], configs=configs)
p = LoopIR.LoopIR.proc(
p.name, p.args, p.preds + [assertion], p.body, p.instr, p.srcinfo
)
return Procedure(p, _provenance_eq_Procedure=None)
def is_eq(self, proc: "Procedure"):
eqv_set = check_eqv_proc(self._loopir_proc, proc._loopir_proc)
return eqv_set == frozenset()
def _root(self):
return IC.Cursor.create(self._loopir_proc)