Skip to content

Commit

Permalink
compiler: allow RPCing builtin functions.
Browse files Browse the repository at this point in the history
Fixes #366.
whitequark committed Apr 26, 2016
1 parent 1464bae commit a88425b
Showing 4 changed files with 26 additions and 13 deletions.
25 changes: 15 additions & 10 deletions artiq/compiler/embedding.py
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
annotated as ``@kernel`` when they are referenced.
"""

import sys, os, re, linecache, inspect, textwrap
import sys, os, re, linecache, inspect, textwrap, types as pytypes
from collections import OrderedDict, defaultdict

from pythonparser import ast, algorithm, source, diagnostic, parse_buffer
@@ -102,7 +102,8 @@ def quote(self, value):
return asttyped.ListT(elts=elts, ctx=None, type=builtins.TList(),
begin_loc=begin_loc, end_loc=end_loc,
loc=begin_loc.join(end_loc))
elif inspect.isfunction(value) or inspect.ismethod(value):
elif inspect.isfunction(value) or inspect.ismethod(value) or \
isinstance(value, pytypes.BuiltinFunctionType):
quote_loc = self._add('`')
repr_loc = self._add(repr(value))
unquote_loc = self._add('`')
@@ -734,17 +735,21 @@ def _quote_syscall(self, function, loc):
self.functions[function] = function_type
return function_type

def _quote_rpc(self, function, loc):
signature = inspect.signature(function)
def _quote_rpc(self, callee, loc):
ret_type = builtins.TNone()

if signature.return_annotation is not inspect.Signature.empty:
ret_type = self._extract_annot(function, signature.return_annotation,
"return type", loc, is_syscall=False)
if isinstance(callee, pytypes.BuiltinFunctionType):
pass
elif isinstance(callee, pytypes.FunctionType):
signature = inspect.signature(callee)
if signature.return_annotation is not inspect.Signature.empty:
ret_type = self._extract_annot(callee, signature.return_annotation,
"return type", loc, is_syscall=False)
else:
ret_type = builtins.TNone()
assert False

function_type = types.TRPC(ret_type, service=self.object_map.store(function))
self.functions[function] = function_type
function_type = types.TRPC(ret_type, service=self.object_map.store(callee))
self.functions[callee] = function_type
return function_type

def _quote_function(self, function, loc):
3 changes: 2 additions & 1 deletion artiq/compiler/transforms/llvm_ir_generator.py
Original file line number Diff line number Diff line change
@@ -412,7 +412,8 @@ def emit_attribute_writeback(self):

for obj_id in self.object_map:
obj_ref = self.object_map.retrieve(obj_id)
if isinstance(obj_ref, (pytypes.FunctionType, pytypes.MethodType)):
if isinstance(obj_ref, (pytypes.FunctionType, pytypes.MethodType,
pytypes.BuiltinFunctionType)):
continue
elif isinstance(obj_ref, type):
_, typ = self.type_map[obj_ref]
4 changes: 2 additions & 2 deletions artiq/coredevice/comm_generic.py
Original file line number Diff line number Diff line change
@@ -349,7 +349,7 @@ def _receive_rpc_value(self, object_map):
else:
raise IOError("Unknown RPC value tag: {}".format(repr(tag)))

def _receive_rpc_args(self, object_map, defaults):
def _receive_rpc_args(self, object_map):
args, kwargs = [], {}
while True:
value = self._receive_rpc_value(object_map)
@@ -447,7 +447,7 @@ def _serve_rpc(self, object_map):
else:
service = object_map.retrieve(service_id)

args, kwargs = self._receive_rpc_args(object_map, service.__defaults__)
args, kwargs = self._receive_rpc_args(object_map)
return_tags = self._read_bytes()
logger.debug("rpc service: [%d]%r %r %r -> %s", service_id, service, args, kwargs, return_tags)

7 changes: 7 additions & 0 deletions artiq/test/coredevice/test_embedding.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from time import sleep

from artiq.experiment import *
from artiq.test.hardware_testbench import ExperimentCase

@@ -97,6 +99,10 @@ def kwargs2(self):
def args1kwargs2(self):
return self.kwargs("X", a="A", b=1)

@kernel
def builtin(self):
sleep(1.0)

class RPCTest(ExperimentCase):
def test_args(self):
exp = self.create(_RPC)
@@ -107,6 +113,7 @@ def test_args(self):
self.assertEqual(exp.kwargs1(), 1)
self.assertEqual(exp.kwargs2(), 2)
self.assertEqual(exp.args1kwargs2(), 2)
exp.builtin()


class _Payload1MB(EnvExperiment):

0 comments on commit a88425b

Please sign in to comment.