artiq/artiq/coredevice/core.py

106 lines
3.3 KiB
Python
Raw Normal View History

import os
from artiq.transforms.inline import inline
from artiq.transforms.lower_units import lower_units
from artiq.transforms.quantize_time import quantize_time
from artiq.transforms.remove_inter_assigns import remove_inter_assigns
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
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
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):
print("*** Unparsing: "+label)
2014-11-03 18:14:33 +08:00
print(unparse(node))
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
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)
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-11-03 18:44:30 +08:00
def transform_stack(self, func_def, rpc_map, exception_map,
debug_unparse=_no_debug_unparse):
lower_units(func_def, rpc_map)
2014-11-03 18:44:30 +08:00
debug_unparse("lower_units", func_def)
remove_inter_assigns(func_def)
2014-11-03 18:44:30 +08:00
debug_unparse("remove_inter_assigns_1", func_def)
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)
unroll_loops(func_def, 500)
2014-11-03 18:44:30 +08:00
debug_unparse("unroll_loops", func_def)
2014-09-13 19:32:21 +08:00
interleave(func_def)
2014-11-03 18:44:30 +08:00
debug_unparse("interleave", func_def)
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)
remove_inter_assigns(func_def)
2014-11-03 18:44:30 +08:00
debug_unparse("remove_inter_assigns_2", 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_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
# 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)