2014-09-17 17:06:51 +08:00
|
|
|
import os
|
|
|
|
|
2014-09-05 22:18:31 +08:00
|
|
|
from artiq.transforms.inline import inline
|
|
|
|
from artiq.transforms.lower_units import lower_units
|
2014-11-16 06:26:35 +08:00
|
|
|
from artiq.transforms.quantize_time import quantize_time
|
2014-10-29 17:09:45 +08:00
|
|
|
from artiq.transforms.remove_inter_assigns import remove_inter_assigns
|
2014-09-05 22:18:31 +08:00
|
|
|
from artiq.transforms.fold_constants import fold_constants
|
2014-10-29 20:23:58 +08:00
|
|
|
from artiq.transforms.remove_dead_code import remove_dead_code
|
2014-09-05 22:18:31 +08:00
|
|
|
from artiq.transforms.unroll_loops import unroll_loops
|
|
|
|
from artiq.transforms.interleave import interleave
|
|
|
|
from artiq.transforms.lower_time import lower_time
|
2014-11-03 18:14:33 +08:00
|
|
|
from artiq.transforms.unparse import unparse
|
2014-09-05 22:18:31 +08:00
|
|
|
from artiq.py2llvm import get_runtime_binary
|
2014-05-31 00:20:13 +08:00
|
|
|
|
2014-09-05 12:03:22 +08:00
|
|
|
|
2014-11-03 18:14:33 +08:00
|
|
|
def _announce_unparse(label, node):
|
2014-09-17 17:06:51 +08:00
|
|
|
print("*** Unparsing: "+label)
|
2014-11-03 18:14:33 +08:00
|
|
|
print(unparse(node))
|
2014-09-17 17:06:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _make_debug_unparse(final):
|
|
|
|
try:
|
|
|
|
env = os.environ["ARTIQ_UNPARSE"]
|
|
|
|
except KeyError:
|
|
|
|
env = ""
|
|
|
|
selected_labels = set(env.split())
|
|
|
|
if "all" in selected_labels:
|
2014-11-03 18:14:33 +08:00
|
|
|
return _announce_unparse
|
2014-09-17 17:06:51 +08:00
|
|
|
else:
|
|
|
|
if "final" in selected_labels:
|
|
|
|
selected_labels.add(final)
|
|
|
|
|
|
|
|
def _filtered_unparse(label, node):
|
|
|
|
if label in selected_labels:
|
2014-11-03 18:14:33 +08:00
|
|
|
_announce_unparse(label, node)
|
2014-09-17 17:06:51 +08:00
|
|
|
return _filtered_unparse
|
|
|
|
|
|
|
|
|
2014-11-03 18:44:30 +08:00
|
|
|
def _no_debug_unparse(label, node):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-05-31 00:20:13 +08:00
|
|
|
class Core:
|
2014-11-20 09:44:06 +08:00
|
|
|
def __init__(self, comm, runtime_env=None):
|
2014-09-05 12:03:22 +08:00
|
|
|
if runtime_env is None:
|
2014-11-20 09:44:06 +08:00
|
|
|
runtime_env = comm.get_runtime_env()
|
2014-09-05 12:03:22 +08:00
|
|
|
self.runtime_env = runtime_env
|
2014-11-20 09:44:06 +08:00
|
|
|
self.comm = comm
|
2014-07-06 04:47:54 +08:00
|
|
|
|
2014-11-03 18:44:30 +08:00
|
|
|
def transform_stack(self, func_def, rpc_map, exception_map,
|
|
|
|
debug_unparse=_no_debug_unparse):
|
2014-10-06 23:28:56 +08:00
|
|
|
lower_units(func_def, rpc_map)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("lower_units", func_def)
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2014-10-29 17:09:45 +08:00
|
|
|
remove_inter_assigns(func_def)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("remove_inter_assigns_1", func_def)
|
2014-10-29 17:09:45 +08:00
|
|
|
|
2014-11-16 06:26:35 +08:00
|
|
|
quantize_time(func_def, self.runtime_env.ref_period)
|
|
|
|
debug_unparse("quantize_time", func_def)
|
|
|
|
|
2014-09-13 19:32:21 +08:00
|
|
|
fold_constants(func_def)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("fold_constants_1", func_def)
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2014-09-17 19:52:18 +08:00
|
|
|
unroll_loops(func_def, 500)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("unroll_loops", func_def)
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2014-09-13 19:32:21 +08:00
|
|
|
interleave(func_def)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("interleave", func_def)
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2014-11-16 06:26:35 +08:00
|
|
|
lower_time(func_def, getattr(self.runtime_env, "initial_time", 0))
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("lower_time", func_def)
|
2014-09-17 17:06:51 +08:00
|
|
|
|
2014-10-29 17:09:45 +08:00
|
|
|
remove_inter_assigns(func_def)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("remove_inter_assigns_2", func_def)
|
2014-10-29 17:09:45 +08:00
|
|
|
|
2014-09-13 19:32:21 +08:00
|
|
|
fold_constants(func_def)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("fold_constants_2", func_def)
|
2014-06-17 04:56:08 +08:00
|
|
|
|
2014-10-29 20:23:58 +08:00
|
|
|
remove_dead_code(func_def)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("remove_dead_code_1", func_def)
|
2014-10-31 23:44:07 +08:00
|
|
|
|
|
|
|
remove_inter_assigns(func_def)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("remove_inter_assigns_3", func_def)
|
2014-10-31 23:44:07 +08:00
|
|
|
|
|
|
|
fold_constants(func_def)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("fold_constants_3", func_def)
|
2014-10-31 23:44:07 +08:00
|
|
|
|
|
|
|
remove_dead_code(func_def)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("remove_dead_code_2", func_def)
|
|
|
|
|
|
|
|
def run(self, k_function, k_args, k_kwargs):
|
|
|
|
# transform/simplify AST
|
|
|
|
debug_unparse = _make_debug_unparse("remove_dead_code_2")
|
|
|
|
|
|
|
|
func_def, rpc_map, exception_map = inline(
|
|
|
|
self, k_function, k_args, k_kwargs)
|
|
|
|
debug_unparse("inline", func_def)
|
|
|
|
|
|
|
|
self.transform_stack(func_def, rpc_map, exception_map, debug_unparse)
|
2014-10-29 20:23:58 +08:00
|
|
|
|
2014-09-17 17:06:51 +08:00
|
|
|
# compile to machine code and run
|
2014-09-13 19:32:21 +08:00
|
|
|
binary = get_runtime_binary(self.runtime_env, func_def)
|
2014-11-20 09:44:06 +08:00
|
|
|
self.comm.load(binary)
|
|
|
|
self.comm.run(func_def.name)
|
|
|
|
self.comm.serve(rpc_map, exception_map)
|