From 58967f14fd9cf74ad915976981185f2de821366a Mon Sep 17 00:00:00 2001 From: whitequark Date: Sat, 21 Apr 2018 18:24:00 +0000 Subject: [PATCH] compiler: do not try to re-coerce fully coerced numerics. --- artiq/compiler/transforms/inferencer.py | 10 +++++++++- artiq/test/lit/inferencer/coerce_explicit.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 artiq/test/lit/inferencer/coerce_explicit.py diff --git a/artiq/compiler/transforms/inferencer.py b/artiq/compiler/transforms/inferencer.py index 7880a6390..9142658b3 100644 --- a/artiq/compiler/transforms/inferencer.py +++ b/artiq/compiler/transforms/inferencer.py @@ -310,7 +310,15 @@ class Inferencer(algorithm.Visitor): node_types = [] for node in nodes: if isinstance(node, asttyped.CoerceT): - node_types.append(node.value.type) + # If we already know exactly what we coerce this value to, use that type, + # or we'll get an unification error in case the coerced type is not the same + # as the type of the coerced value. + # Otherwise, use the potentially more specific subtype when considering possible + # coercions, or we may get stuck. + if node.type.fold(False, lambda acc, ty: acc or types.is_var(ty)): + node_types.append(node.value.type) + else: + node_types.append(node.type) else: node_types.append(node.type) if any(map(types.is_var, node_types)): # not enough info yet diff --git a/artiq/test/lit/inferencer/coerce_explicit.py b/artiq/test/lit/inferencer/coerce_explicit.py new file mode 100644 index 000000000..4455c596c --- /dev/null +++ b/artiq/test/lit/inferencer/coerce_explicit.py @@ -0,0 +1,12 @@ +# RUN: %python -m artiq.compiler.testbench.inferencer +mono %s >%t +# RUN: OutputCheck %s --file-to-check=%t + +# CHECK-L: n:numpy.int32 = +n = 0 +# CHECK-L: a:numpy.int32 = +a = n // 1 +# CHECK-L: b:numpy.int32 = +b = n // 10 +# CHECK-L: q:numpy.int64 = +q = (a << 0) + (b << 8) +core_log(int64(q))