From ed2b10c5aa2b5016c90ba5a6c3866a17b3cee2ab Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 21 Apr 2017 17:36:44 +0000 Subject: [PATCH] compiler: in codegen for delay(), round fp instead of truncating. Consider delay(8*us). It results in the following computation... >>> 8*1e-06/1e-09 7999.999999999999 with the result promptly getting truncated to 7999. Fixes #706. --- .../compiler/transforms/artiq_ir_generator.py | 2 +- artiq/test/coredevice/test_rtio.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/artiq/compiler/transforms/artiq_ir_generator.py b/artiq/compiler/transforms/artiq_ir_generator.py index 518335e2f..aea58f0d5 100644 --- a/artiq/compiler/transforms/artiq_ir_generator.py +++ b/artiq/compiler/transforms/artiq_ir_generator.py @@ -1717,7 +1717,7 @@ class ARTIQIRGenerator(algorithm.Visitor): 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.TInt64())) + arg_mu = self.append(ir.Builtin("round", [arg_mu_float], builtins.TInt64())) return self.append(ir.Builtin("delay_mu", [arg_mu], builtins.TNone())) else: assert False diff --git a/artiq/test/coredevice/test_rtio.py b/artiq/test/coredevice/test_rtio.py index de1e6c188..3c8fb0131 100644 --- a/artiq/test/coredevice/test_rtio.py +++ b/artiq/test/coredevice/test_rtio.py @@ -314,6 +314,19 @@ class Handover(EnvExperiment): self.k("t2") +class Rounding(EnvExperiment): + def build(self): + self.setattr_device("core") + + @kernel + def run(self): + self.core.reset() + t1 = now_mu() + delay(8*us) + t2 = now_mu() + self.set_dataset("delta", t2 - t1) + + class DummyException(Exception): pass @@ -434,6 +447,11 @@ class CoredeviceTest(ExperimentCase): self.assertEqual(self.dataset_mgr.get("t1") + 1234, self.dataset_mgr.get("t2")) + def test_rounding(self): + self.execute(Rounding) + dt = self.dataset_mgr.get("delta") + self.assertEqual(dt, 8000) + class RPCTiming(EnvExperiment): def build(self, repeats=100):