From ab71c9a0bae095cf1d2dcd2c95bb3662bf9bd445 Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 31 Jan 2017 21:28:20 +0000 Subject: [PATCH] compiler: the len builtin is not polymorphic, coerce the result. Fixes #659. --- artiq/compiler/transforms/artiq_ir_generator.py | 5 +++-- artiq/test/lit/regression/bug_659.py | 17 +++++++++++++++++ artiq/test/lit/regression/device_db.pyon | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 artiq/test/lit/regression/bug_659.py create mode 100644 artiq/test/lit/regression/device_db.pyon diff --git a/artiq/compiler/transforms/artiq_ir_generator.py b/artiq/compiler/transforms/artiq_ir_generator.py index 8e10b77de..3ad09f300 100644 --- a/artiq/compiler/transforms/artiq_ir_generator.py +++ b/artiq/compiler/transforms/artiq_ir_generator.py @@ -482,8 +482,9 @@ class ARTIQIRGenerator(algorithm.Visitor): name = None else: name = "{}.len".format(value.name) - return self.append(ir.Builtin("len", [value], typ, - name=name)) + len = self.append(ir.Builtin("len", [value], builtins.TInt32(), + name=name)) + return self.append(ir.Coerce(len, typ)) elif builtins.is_range(value.type): start = self.append(ir.GetAttr(value, "start")) stop = self.append(ir.GetAttr(value, "stop")) diff --git a/artiq/test/lit/regression/bug_659.py b/artiq/test/lit/regression/bug_659.py new file mode 100644 index 000000000..096839725 --- /dev/null +++ b/artiq/test/lit/regression/bug_659.py @@ -0,0 +1,17 @@ +# RUN: %python -m artiq.compiler.testbench.embedding %s + +from artiq.language.core import * +from artiq.language.types import * +import numpy as np + +@kernel +def rotate(array): + '''Rotates an array, deleting the oldest value''' + length = len(array) + for i in range(np.int64(len(array)) - 1): + array[length - i - 1] = array[length - i - 2] + array[0] = 0 + +@kernel +def entrypoint(): + rotate([1,2,3,4]) diff --git a/artiq/test/lit/regression/device_db.pyon b/artiq/test/lit/regression/device_db.pyon new file mode 100644 index 000000000..02d4e06bb --- /dev/null +++ b/artiq/test/lit/regression/device_db.pyon @@ -0,0 +1,14 @@ +{ + "comm": { + "type": "local", + "module": "artiq.coredevice.comm_dummy", + "class": "Comm", + "arguments": {} + }, + "core": { + "type": "local", + "module": "artiq.coredevice.core", + "class": "Core", + "arguments": {"ref_period": 1e-9} + } +}