forked from M-Labs/artiq
compiler: Implement element type coercion for arrays
So far, this is not exposed to the user beyond implicit conversions. Note that all the implicit conversions, such as triggered by adding arrays of mismatching types, or dividing integer arrays, are currently emitted in a maximally inefficient way, where a temporary copy is first made for the type conversion. The conversions would more sensibly be implemented during the per-element operations to save on the extra copies, but the current behaviour fell out of the rest of the IR generator structure without extra changes.
This commit is contained in:
parent
4426e4144f
commit
da255bee1b
|
@ -1415,7 +1415,19 @@ class ARTIQIRGenerator(algorithm.Visitor):
|
||||||
if node.type.find() == value.type:
|
if node.type.find() == value.type:
|
||||||
return value
|
return value
|
||||||
else:
|
else:
|
||||||
return self.append(ir.Coerce(value, node.type,
|
if builtins.is_array(node.type):
|
||||||
|
result_elt = node.type.find()["elt"]
|
||||||
|
shape = self.append(ir.GetAttr(value, "shape"))
|
||||||
|
result = self._alloate_new_array(result_elt, shape)
|
||||||
|
func = self._get_array_unaryop("Coerce",
|
||||||
|
lambda v: ir.Coerce(v, result_elt),
|
||||||
|
node.type, value.type)
|
||||||
|
self._invoke_arrayop(func, [result, value])
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
return self.append(
|
||||||
|
ir.Coerce(value,
|
||||||
|
node.type,
|
||||||
name="{}.{}".format(_readable_name(value),
|
name="{}.{}".format(_readable_name(value),
|
||||||
node.type.name)))
|
node.type.name)))
|
||||||
|
|
||||||
|
|
|
@ -33,12 +33,15 @@ assert c[0] == 0
|
||||||
assert c[1] == 1
|
assert c[1] == 1
|
||||||
assert c[2] == 0
|
assert c[2] == 0
|
||||||
|
|
||||||
# FIXME: Implement array coercion.
|
cf = b / a
|
||||||
# c = b / a
|
assert cf[0] == 4.0
|
||||||
# assert c[0] == 4.0
|
assert cf[1] == 2.5
|
||||||
# assert c[1] == 2.5
|
assert cf[2] == 2.0
|
||||||
# assert c[2] == 2.0
|
|
||||||
|
|
||||||
|
cf2 = cf + a
|
||||||
|
assert cf2[0] == 5.0
|
||||||
|
assert cf2[1] == 4.5
|
||||||
|
assert cf2[2] == 5.0
|
||||||
|
|
||||||
d = array([[1, 2], [3, 4]])
|
d = array([[1, 2], [3, 4]])
|
||||||
e = array([[5, 6], [7, 8]])
|
e = array([[5, 6], [7, 8]])
|
||||||
|
|
Loading…
Reference in New Issue