From 31048f4b6ae77f737d895fe6637ccf1290484ace Mon Sep 17 00:00:00 2001 From: whitequark Date: Wed, 12 Apr 2017 04:10:08 +0000 Subject: [PATCH] compiler: fix monomorphization of coerced integer literals. Fixes #703. --- artiq/compiler/transforms/inferencer.py | 1 + artiq/compiler/transforms/int_monomorphizer.py | 12 +++++++++++- artiq/test/lit/monomorphism/coercion.py | 10 ++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 artiq/test/lit/monomorphism/coercion.py diff --git a/artiq/compiler/transforms/inferencer.py b/artiq/compiler/transforms/inferencer.py index 11e2dfef2..194c3832d 100644 --- a/artiq/compiler/transforms/inferencer.py +++ b/artiq/compiler/transforms/inferencer.py @@ -5,6 +5,7 @@ from collections import OrderedDict from pythonparser import algorithm, diagnostic, ast from .. import asttyped, types, builtins +from .typedtree_printer import TypedtreePrinter class Inferencer(algorithm.Visitor): """ diff --git a/artiq/compiler/transforms/int_monomorphizer.py b/artiq/compiler/transforms/int_monomorphizer.py index 94711a43a..5002bb086 100644 --- a/artiq/compiler/transforms/int_monomorphizer.py +++ b/artiq/compiler/transforms/int_monomorphizer.py @@ -5,7 +5,7 @@ do not. """ from pythonparser import algorithm, diagnostic -from .. import types, builtins +from .. import types, builtins, asttyped class IntMonomorphizer(algorithm.Visitor): def __init__(self, engine): @@ -35,3 +35,13 @@ class IntMonomorphizer(algorithm.Visitor): typ = node.type.find() if types.is_var(typ["width"]): typ["width"].unify(types.TValue(32)) + + def visit_CoerceT(self, node): + if isinstance(node.value, asttyped.NumT) and \ + builtins.is_int(node.type) and \ + builtins.is_int(node.value.type) and \ + not types.is_var(node.type["width"]) and \ + types.is_var(node.value.type["width"]): + node.value.type.unify(node.type) + + self.generic_visit(node) diff --git a/artiq/test/lit/monomorphism/coercion.py b/artiq/test/lit/monomorphism/coercion.py new file mode 100644 index 000000000..bbd67e936 --- /dev/null +++ b/artiq/test/lit/monomorphism/coercion.py @@ -0,0 +1,10 @@ +# RUN: %python -m artiq.compiler.testbench.signature %s >%t +# RUN: OutputCheck %s --file-to-check=%t + +def f(x): + x = int64(0) + return x + +# CHECK-L: g: ()->numpy.int64 +def g(): + return f(1 + 0)