From 1c645d8857f27db72fa965b18d45b570aef99a71 Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Sat, 8 Aug 2020 19:57:46 +0100 Subject: [PATCH] compiler: Unbreak quoting of 1D ndarrays Lists and arrays no longer have the same representation all the way through codegen, as used to be the case. This could/should be made more efficient later, eliding the temporary copies. --- artiq/compiler/embedding.py | 12 +--------- artiq/test/lit/embedding/arrays.py | 35 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 artiq/test/lit/embedding/arrays.py diff --git a/artiq/compiler/embedding.py b/artiq/compiler/embedding.py index ddacf947f..03be54a94 100644 --- a/artiq/compiler/embedding.py +++ b/artiq/compiler/embedding.py @@ -233,17 +233,7 @@ class ASTSynthesizer: begin_loc=begin_loc, end_loc=end_loc, loc=begin_loc.join(end_loc)) elif isinstance(value, numpy.ndarray): - begin_loc = self._add("numpy.array([") - elts = [] - for index, elt in enumerate(value): - elts.append(self.quote(elt)) - if index < len(value) - 1: - self._add(", ") - end_loc = self._add("])") - - return asttyped.ListT(elts=elts, ctx=None, type=builtins.TArray(), - begin_loc=begin_loc, end_loc=end_loc, - loc=begin_loc.join(end_loc)) + return self.call(numpy.array, [list(value)], {}) elif inspect.isfunction(value) or inspect.ismethod(value) or \ isinstance(value, pytypes.BuiltinFunctionType) or \ isinstance(value, SpecializedFunction) or \ diff --git a/artiq/test/lit/embedding/arrays.py b/artiq/test/lit/embedding/arrays.py new file mode 100644 index 000000000..3a2c226b9 --- /dev/null +++ b/artiq/test/lit/embedding/arrays.py @@ -0,0 +1,35 @@ +# RUN: %python -m artiq.compiler.testbench.embedding %s + +from artiq.language.core import * +from artiq.language.types import * +from numpy import array + +int_vec = array([1, 2, 3]) +float_vec = array([1.0, 2.0, 3.0]) +int_mat = array([[1, 2], [3, 4]]) +float_mat = array([[1.0, 2.0], [3.0, 4.0]]) + + +@kernel +def entrypoint(): + assert int_vec.shape == (3, ) + assert int_vec[0] == 1 + assert int_vec[1] == 2 + assert int_vec[2] == 3 + + assert float_vec.shape == (3, ) + assert float_vec[0] == 1.0 + assert float_vec[1] == 2.0 + assert float_vec[2] == 3.0 + + # assert int_mat.shape == (2, 2) + # assert int_mat[0][0] == 1 + # assert int_mat[0][1] == 2 + # assert int_mat[1][0] == 3 + # assert int_mat[1][1] == 4 + + # assert float_mat.shape == (2, 2) + # assert float_mat[0][0] == 1.0 + # assert float_mat[0][1] == 2.0 + # assert float_mat[1][0] == 3.0 + # assert float_mat[1][1] == 4.0