2014-09-17 17:06:51 +08:00
|
|
|
import os
|
|
|
|
|
2014-12-03 18:20:30 +08:00
|
|
|
from artiq.language.core import *
|
2015-04-22 01:12:01 +08:00
|
|
|
from artiq.language.units import ns
|
2014-12-03 18:20:30 +08:00
|
|
|
|
2014-09-05 22:18:31 +08:00
|
|
|
from artiq.transforms.inline import inline
|
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-12-03 18:20:30 +08:00
|
|
|
|
2015-07-15 17:20:41 +08:00
|
|
|
from artiq.coredevice.runtime import Runtime
|
2015-05-02 23:41:49 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2015-07-14 04:08:20 +08:00
|
|
|
class Core:
|
|
|
|
def __init__(self, dmgr, ref_period=8*ns, external_clock=False):
|
|
|
|
self.comm = dmgr.get("comm")
|
|
|
|
self.ref_period = ref_period
|
|
|
|
self.external_clock = external_clock
|
2014-12-03 18:20:30 +08:00
|
|
|
|
2015-05-02 23:41:49 +08:00
|
|
|
self.first_run = True
|
2014-11-21 04:38:52 +08:00
|
|
|
self.core = self
|
2015-03-13 21:55:18 +08:00
|
|
|
self.comm.core = self
|
2015-07-15 17:20:41 +08:00
|
|
|
self.runtime = Runtime()
|
2014-12-02 14:06:32 +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-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
|
|
|
|
2015-06-26 22:20:13 +08:00
|
|
|
quantize_time(func_def, self.ref_period)
|
2014-11-16 06:26:35 +08:00
|
|
|
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
|
|
|
|
2015-05-02 23:41:49 +08:00
|
|
|
lower_time(func_def)
|
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)
|
|
|
|
|
2015-04-07 15:40:25 +08:00
|
|
|
def compile(self, k_function, k_args, k_kwargs, with_attr_writeback=True):
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse = _make_debug_unparse("remove_dead_code_2")
|
|
|
|
|
|
|
|
func_def, rpc_map, exception_map = inline(
|
2015-04-07 15:40:25 +08:00
|
|
|
self, k_function, k_args, k_kwargs, with_attr_writeback)
|
2014-11-03 18:44:30 +08:00
|
|
|
debug_unparse("inline", func_def)
|
|
|
|
self.transform_stack(func_def, rpc_map, exception_map, debug_unparse)
|
2014-10-29 20:23:58 +08:00
|
|
|
|
2015-07-15 17:20:41 +08:00
|
|
|
binary = get_runtime_binary(self.runtime, func_def)
|
2015-04-07 15:40:25 +08:00
|
|
|
|
|
|
|
return binary, rpc_map, exception_map
|
|
|
|
|
|
|
|
def run(self, k_function, k_args, k_kwargs):
|
2015-05-02 23:41:49 +08:00
|
|
|
if self.first_run:
|
|
|
|
self.comm.check_ident()
|
|
|
|
self.comm.switch_clock(self.external_clock)
|
|
|
|
|
2015-04-07 15:40:25 +08:00
|
|
|
binary, rpc_map, exception_map = self.compile(
|
|
|
|
k_function, k_args, k_kwargs)
|
2014-11-20 09:44:06 +08:00
|
|
|
self.comm.load(binary)
|
2015-07-07 21:29:38 +08:00
|
|
|
self.comm.run(k_function.__name__)
|
2014-11-20 09:44:06 +08:00
|
|
|
self.comm.serve(rpc_map, exception_map)
|
2015-05-02 23:41:49 +08:00
|
|
|
self.first_run = False
|
2014-11-21 04:38:52 +08:00
|
|
|
|
2015-02-19 00:56:30 +08:00
|
|
|
@kernel
|
2015-07-02 04:22:53 +08:00
|
|
|
def get_rtio_counter_mu(self):
|
|
|
|
return syscall("rtio_get_counter")
|
2015-02-19 00:56:30 +08:00
|
|
|
|
2014-11-21 04:38:52 +08:00
|
|
|
@kernel
|
2015-05-03 20:42:42 +08:00
|
|
|
def break_realtime(self):
|
2015-08-17 23:41:21 +08:00
|
|
|
min_now = syscall("rtio_get_counter") + 125000
|
|
|
|
if now_mu() < min_now:
|
|
|
|
at_mu(min_now)
|