Skip to content

Commit

Permalink
Implement {delay,now,at}{,_mu} and {mu,seconds}_to_{seconds,mu}.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Aug 31, 2015
1 parent 5151adb commit 501ba91
Show file tree
Hide file tree
Showing 22 changed files with 177 additions and 155 deletions.
24 changes: 24 additions & 0 deletions artiq/compiler/builtins.py
Expand Up @@ -138,6 +138,30 @@ def fn_print():
def fn_kernel():
return types.TBuiltinFunction("kernel")

def fn_now():
return types.TBuiltinFunction("now")

def fn_delay():
return types.TBuiltinFunction("delay")

def fn_at():
return types.TBuiltinFunction("at")

def fn_now_mu():
return types.TBuiltinFunction("now_mu")

def fn_delay_mu():
return types.TBuiltinFunction("delay_mu")

def fn_at_mu():
return types.TBuiltinFunction("at_mu")

def fn_mu_to_seconds():
return types.TBuiltinFunction("mu_to_seconds")

def fn_seconds_to_mu():
return types.TBuiltinFunction("seconds_to_mu")

# Accessors

def is_none(typ):
Expand Down
5 changes: 3 additions & 2 deletions artiq/compiler/module.py
Expand Up @@ -42,7 +42,7 @@ def from_filename(cls, filename, engine=None):
return cls(source.Buffer(f.read(), filename, 1), engine=engine)

class Module:
def __init__(self, src):
def __init__(self, src, ref_period=1e-6):
self.engine = src.engine
self.object_map = src.object_map

Expand All @@ -51,7 +51,8 @@ def __init__(self, src):
monomorphism_validator = validators.MonomorphismValidator(engine=self.engine)
escape_validator = validators.EscapeValidator(engine=self.engine)
artiq_ir_generator = transforms.ARTIQIRGenerator(engine=self.engine,
module_name=src.name)
module_name=src.name,
ref_period=ref_period)
dead_code_eliminator = transforms.DeadCodeEliminator(engine=self.engine)
local_access_validator = validators.LocalAccessValidator(engine=self.engine)

Expand Down
17 changes: 17 additions & 0 deletions artiq/compiler/prelude.py
Expand Up @@ -7,17 +7,34 @@

def globals():
return {
# Value constructors
"bool": builtins.fn_bool(),
"int": builtins.fn_int(),
"float": builtins.fn_float(),
"list": builtins.fn_list(),
"range": builtins.fn_range(),

# Exception constructors
"Exception": builtins.fn_Exception(),
"IndexError": builtins.fn_IndexError(),
"ValueError": builtins.fn_ValueError(),
"ZeroDivisionError": builtins.fn_ZeroDivisionError(),

# Built-in Python functions
"len": builtins.fn_len(),
"round": builtins.fn_round(),
"print": builtins.fn_print(),

# ARTIQ decorators
"kernel": builtins.fn_kernel(),

# ARTIQ time management functions
"now": builtins.fn_now(),
"delay": builtins.fn_delay(),
"at": builtins.fn_at(),
"now_mu": builtins.fn_now_mu(),
"delay_mu": builtins.fn_delay_mu(),
"at_mu": builtins.fn_at_mu(),
"mu_to_seconds": builtins.fn_mu_to_seconds(),
"seconds_to_mu": builtins.fn_seconds_to_mu(),
}
6 changes: 3 additions & 3 deletions artiq/compiler/testbench/jit.py
Expand Up @@ -5,9 +5,9 @@
from ..targets import NativeTarget

def main():
libartiq_personality = os.getenv('LIBARTIQ_PERSONALITY')
if libartiq_personality is not None:
llvm.load_library_permanently(libartiq_personality)
libartiq_support = os.getenv('LIBARTIQ_SUPPORT')
if libartiq_support is not None:
llvm.load_library_permanently(libartiq_support)

def process_diagnostic(diag):
print("\n".join(diag.render()))
Expand Down
36 changes: 35 additions & 1 deletion artiq/compiler/transforms/artiq_ir_generator.py
Expand Up @@ -67,10 +67,11 @@ class ARTIQIRGenerator(algorithm.Visitor):

_size_type = builtins.TInt(types.TValue(32))

def __init__(self, module_name, engine):
def __init__(self, module_name, engine, ref_period):
self.engine = engine
self.functions = []
self.name = [module_name] if module_name != "" else []
self.ref_period = ir.Constant(ref_period, builtins.TFloat())
self.current_loc = None
self.current_function = None
self.current_class = None
Expand Down Expand Up @@ -1409,6 +1410,39 @@ def body_gen(index):
self.polymorphic_print([self.visit(arg) for arg in node.args],
separator=" ", suffix="\n")
return ir.Constant(None, builtins.TNone())
elif types.is_builtin(typ, "now"):
if len(node.args) == 0 and len(node.keywords) == 0:
now_mu = self.append(ir.Builtin("now_mu", [], builtins.TInt(types.TValue(64))))
now_mu_float = self.append(ir.Coerce(now_mu, builtins.TFloat()))
return self.append(ir.Arith(ast.Mult(loc=None), now_mu_float, self.ref_period))
else:
assert False
elif types.is_builtin(typ, "delay") or types.is_builtin(typ, "at"):
if len(node.args) == 1 and len(node.keywords) == 0:
arg = self.visit(node.args[0])
arg_mu_float = self.append(ir.Arith(ast.Div(loc=None), arg, self.ref_period))
arg_mu = self.append(ir.Coerce(arg_mu_float, builtins.TInt(types.TValue(64))))
self.append(ir.Builtin(typ.name + "_mu", [arg_mu], builtins.TNone()))
else:
assert False
elif types.is_builtin(typ, "now_mu") or types.is_builtin(typ, "delay_mu") \
or types.is_builtin(typ, "at_mu"):
return self.append(ir.Builtin(typ.name,
[self.visit(arg) for arg in node.args], node.type))
elif types.is_builtin(typ, "mu_to_seconds"):
if len(node.args) == 1 and len(node.keywords) == 0:
arg = self.visit(node.args[0])
arg_float = self.append(ir.Coerce(arg, builtins.TFloat()))
return self.append(ir.Arith(ast.Mult(loc=None), arg_float, self.ref_period))
else:
assert False
elif types.is_builtin(typ, "seconds_to_mu"):
if len(node.args) == 1 and len(node.keywords) == 0:
arg = self.visit(node.args[0])
arg_mu = self.append(ir.Arith(ast.Div(loc=None), arg, self.ref_period))
return self.append(ir.Coerce(arg_mu, builtins.TInt(types.TValue(64))))
else:
assert False
elif types.is_exn_constructor(typ):
return self.alloc_exn(node.type, *[self.visit(arg_node) for arg_node in node.args])
elif types.is_constructor(typ):
Expand Down
35 changes: 35 additions & 0 deletions artiq/compiler/transforms/inferencer.py
Expand Up @@ -505,6 +505,17 @@ def diagnose(valid_forms):
node.func.loc, notes=valid_forms)
self.engine.process(diag)

def simple_form(info, arg_types=[], return_type=builtins.TNone()):
self._unify(node.type, return_type,
node.loc, None)

if len(node.args) == len(arg_types) and len(node.keywords) == 0:
for index, arg_type in enumerate(arg_types):
self._unify(node.args[index].type, arg_type,
node.args[index].loc, None)
else:
diagnose([ valid_form(info) ])

if types.is_exn_constructor(typ):
valid_forms = lambda: [
valid_form("{exn}() -> {exn}".format(exn=typ.name)),
Expand Down Expand Up @@ -730,6 +741,30 @@ def makenotes(printer, typea, typeb, loca, locb):
pass
else:
diagnose(valid_forms())
elif types.is_builtin(typ, "now"):
simple_form("now() -> float",
[], builtins.TFloat())
elif types.is_builtin(typ, "delay"):
simple_form("delay(time:float) -> None",
[builtins.TFloat()])
elif types.is_builtin(typ, "at"):
simple_form("at(time:float) -> None",
[builtins.TFloat()])
elif types.is_builtin(typ, "now_mu"):
simple_form("now_mu() -> int(width=64)",
[], builtins.TInt(types.TValue(64)))
elif types.is_builtin(typ, "delay_mu"):
simple_form("delay_mu(time_mu:int(width=64)) -> None",
[builtins.TInt(types.TValue(64))])
elif types.is_builtin(typ, "at_mu"):
simple_form("at_mu(time_mu:int(width=64)) -> None",
[builtins.TInt(types.TValue(64))])
elif types.is_builtin(typ, "mu_to_seconds"):
simple_form("mu_to_seconds(time_mu:int(width=64)) -> float",
[builtins.TInt(types.TValue(64))], builtins.TFloat())
elif types.is_builtin(typ, "seconds_to_mu"):
simple_form("seconds_to_mu(time:float) -> int(width=64)",
[builtins.TFloat()], builtins.TInt(types.TValue(64)))
elif types.is_constructor(typ):
# An user-defined class.
self._unify(node.type, typ.find().instance,
Expand Down
32 changes: 25 additions & 7 deletions artiq/compiler/transforms/llvm_ir_generator.py
Expand Up @@ -14,6 +14,7 @@
lli1 = ll.IntType(1)
lli8 = ll.IntType(8)
lli32 = ll.IntType(32)
lli64 = ll.IntType(64)
lldouble = ll.DoubleType()
llptr = ll.IntType(8).as_pointer()
llmetadata = ll.MetaData()
Expand Down Expand Up @@ -331,9 +332,9 @@ def llconst_of_const(self, const):
assert False

def llbuiltin(self, name):
llfun = self.llmodule.get_global(name)
if llfun is not None:
return llfun
llglobal = self.llmodule.get_global(name)
if llglobal is not None:
return llglobal

if name in "llvm.donothing":
llty = ll.FunctionType(llvoid, [])
Expand Down Expand Up @@ -366,13 +367,19 @@ def llbuiltin(self, name):
var_arg=True)
elif name == "recv_rpc":
llty = ll.FunctionType(lli32, [llptr])
elif name == "now":
llty = lli64
else:
assert False

llfun = ll.Function(self.llmodule, llty, name)
if name in ("__artiq_raise", "__artiq_reraise", "llvm.trap"):
llfun.attributes.add("noreturn")
return llfun
if isinstance(llty, ll.FunctionType):
llglobal = ll.Function(self.llmodule, llty, name)
if name in ("__artiq_raise", "__artiq_reraise", "llvm.trap"):
llglobal.attributes.add("noreturn")
else:
llglobal = ll.GlobalVariable(self.llmodule, llty, name)

return llglobal

def map(self, value):
if isinstance(value, (ir.Argument, ir.Instruction, ir.BasicBlock)):
Expand Down Expand Up @@ -774,6 +781,17 @@ def get_outer(llenv, env_ty):
elif insn.op == "exncast":
# This is an identity cast at LLVM IR level.
return self.map(insn.operands[0])
elif insn.op == "now_mu":
return self.llbuilder.load(self.llbuiltin("now"), name=insn.name)
elif insn.op == "delay_mu":
interval, = insn.operands
llnowptr = self.llbuiltin("now")
llnow = self.llbuilder.load(llnowptr)
lladjusted = self.llbuilder.add(llnow, self.map(interval))
return self.llbuilder.store(lladjusted, llnowptr)
elif insn.op == "at_mu":
time, = insn.operands
return self.llbuilder.store(self.map(time), self.llbuiltin("now"))
else:
assert False

Expand Down
2 changes: 1 addition & 1 deletion artiq/coredevice/core.py
Expand Up @@ -39,7 +39,7 @@ def compile(self, function, args, kwargs, with_attr_writeback=True):
stitcher.stitch_call(function, args, kwargs)
stitcher.finalize()

module = Module(stitcher)
module = Module(stitcher, ref_period=self.ref_period)
target = OR1KTarget()

library = target.compile_and_link([module])
Expand Down
64 changes: 0 additions & 64 deletions artiq/py2llvm_old/transforms/lower_time.py

This file was deleted.

0 comments on commit 501ba91

Please sign in to comment.