Assign a type variable to all VALUE operands.
A few operands have a fixed type assigned. Create a singleton type variable for these exceptions. Most instructions are polymorphic, so this is a little overhead. Eliminate the Operand.typ field and replace it with an Operand.typevar field which is always a TypeVar, but which only exists in VALUE operands.
This commit is contained in:
@@ -130,10 +130,10 @@ class Instruction(object):
|
||||
"""
|
||||
poly_ins = [
|
||||
i for i in self.format.value_operands
|
||||
if self.ins[i].typ.free_typevar()]
|
||||
if self.ins[i].typevar.free_typevar()]
|
||||
poly_outs = [
|
||||
i for i, o in enumerate(self.outs)
|
||||
if o.typ.free_typevar()]
|
||||
if o.is_value() and o.typevar.free_typevar()]
|
||||
self.is_polymorphic = len(poly_ins) > 0 or len(poly_outs) > 0
|
||||
if not self.is_polymorphic:
|
||||
return
|
||||
@@ -143,7 +143,7 @@ class Instruction(object):
|
||||
typevar_error = None
|
||||
if self.format.typevar_operand is not None:
|
||||
try:
|
||||
tv = self.ins[self.format.typevar_operand].typ
|
||||
tv = self.ins[self.format.typevar_operand].typevar
|
||||
if tv is tv.free_typevar():
|
||||
self.other_typevars = self._verify_ctrl_typevar(tv)
|
||||
self.ctrl_typevar = tv
|
||||
@@ -160,7 +160,7 @@ class Instruction(object):
|
||||
else:
|
||||
raise RuntimeError(
|
||||
"typevar_operand must be a free type variable")
|
||||
tv = self.outs[0].typ
|
||||
tv = self.outs[0].typevar
|
||||
if tv is not tv.free_typevar():
|
||||
raise RuntimeError("first result must be a free type variable")
|
||||
self.other_typevars = self._verify_ctrl_typevar(tv)
|
||||
@@ -181,7 +181,7 @@ class Instruction(object):
|
||||
other_tvs = []
|
||||
# Check value inputs.
|
||||
for opidx in self.format.value_operands:
|
||||
typ = self.ins[opidx].typ
|
||||
typ = self.ins[opidx].typevar
|
||||
tv = typ.free_typevar()
|
||||
# Non-polymorphic or derived form ctrl_typevar is OK.
|
||||
if tv is None or tv is ctrl_typevar:
|
||||
@@ -200,7 +200,9 @@ class Instruction(object):
|
||||
|
||||
# Check outputs.
|
||||
for result in self.outs:
|
||||
typ = result.typ
|
||||
if not result.is_value():
|
||||
continue
|
||||
typ = result.typevar
|
||||
tv = typ.free_typevar()
|
||||
# Non-polymorphic or derived from ctrl_typevar is OK.
|
||||
if tv is None or tv is ctrl_typevar:
|
||||
|
||||
@@ -40,10 +40,6 @@ class OperandKind(object):
|
||||
# type: () -> str
|
||||
return 'OperandKind({})'.format(self.name)
|
||||
|
||||
def free_typevar(self):
|
||||
# Return the free typevariable controlling the type of this operand.
|
||||
return None
|
||||
|
||||
#: An SSA value operand. This is a value defined by another instruction.
|
||||
VALUE = OperandKind(
|
||||
'value', """
|
||||
@@ -129,11 +125,15 @@ class Operand(object):
|
||||
# type: (str, OperandSpec, str) -> None
|
||||
self.name = name
|
||||
self.__doc__ = doc
|
||||
self.typ = typ
|
||||
|
||||
# Decode the operand spec and set self.kind.
|
||||
# Only VALUE operands have a typevar member.
|
||||
if isinstance(typ, ValueType):
|
||||
self.kind = VALUE
|
||||
self.typevar = TypeVar.singleton(typ)
|
||||
elif isinstance(typ, TypeVar):
|
||||
self.kind = VALUE
|
||||
self.typevar = typ
|
||||
else:
|
||||
assert isinstance(typ, OperandKind)
|
||||
self.kind = typ
|
||||
@@ -142,8 +142,9 @@ class Operand(object):
|
||||
# type: () -> str
|
||||
if self.__doc__:
|
||||
return self.__doc__
|
||||
else:
|
||||
return self.typ.__doc__
|
||||
if self.kind is VALUE:
|
||||
return self.typevar.__doc__
|
||||
return self.kind.__doc__
|
||||
|
||||
def __str__(self):
|
||||
# type: () -> str
|
||||
|
||||
@@ -3,6 +3,7 @@ from unittest import TestCase
|
||||
from doctest import DocTestSuite
|
||||
from . import typevar
|
||||
from .typevar import TypeSet, TypeVar
|
||||
from base.types import i32
|
||||
|
||||
|
||||
def load_tests(loader, tests, ignore):
|
||||
@@ -62,3 +63,18 @@ class TestTypeVar(TestCase):
|
||||
self.assertEqual(str(x3.double_width()), '`DoubleWidth(x3)`')
|
||||
with self.assertRaises(AssertionError):
|
||||
x3.half_width()
|
||||
|
||||
def test_singleton(self):
|
||||
x = TypeVar.singleton(i32)
|
||||
self.assertEqual(str(x), '`i32`')
|
||||
self.assertEqual(x.type_set.min_int, 32)
|
||||
self.assertEqual(x.type_set.max_int, 32)
|
||||
self.assertEqual(x.type_set.min_lanes, 1)
|
||||
self.assertEqual(x.type_set.max_lanes, 1)
|
||||
|
||||
x = TypeVar.singleton(i32.by(4))
|
||||
self.assertEqual(str(x), '`i32x4`')
|
||||
self.assertEqual(x.type_set.min_int, 32)
|
||||
self.assertEqual(x.type_set.max_int, 32)
|
||||
self.assertEqual(x.type_set.min_lanes, 4)
|
||||
self.assertEqual(x.type_set.max_lanes, 4)
|
||||
|
||||
@@ -30,8 +30,9 @@ class ValueType(object):
|
||||
# type: () -> str
|
||||
return self.name
|
||||
|
||||
def free_typevar(self):
|
||||
return None
|
||||
def rust_name(self):
|
||||
# type: () -> str
|
||||
return 'types::' + self.name.upper()
|
||||
|
||||
@staticmethod
|
||||
def by_name(name):
|
||||
@@ -63,10 +64,6 @@ class ScalarType(ValueType):
|
||||
# type: () -> str
|
||||
return 'ScalarType({})'.format(self.name)
|
||||
|
||||
def rust_name(self):
|
||||
# type: () -> str
|
||||
return 'types::' + self.name.upper()
|
||||
|
||||
def by(self, lanes):
|
||||
# type: (int) -> VectorType
|
||||
"""
|
||||
|
||||
@@ -6,6 +6,7 @@ polymorphic by using type variables.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
import math
|
||||
from . import types
|
||||
|
||||
try:
|
||||
from typing import Tuple, Union # noqa
|
||||
@@ -242,6 +243,7 @@ class TypeVar(object):
|
||||
# type: (str, str, BoolInterval, BoolInterval, BoolInterval, bool, BoolInterval, TypeVar, str) -> None # noqa
|
||||
self.name = name
|
||||
self.__doc__ = doc
|
||||
self.singleton_type = None # type: types.ValueType
|
||||
self.is_derived = isinstance(base, TypeVar)
|
||||
if base:
|
||||
assert self.is_derived
|
||||
@@ -258,6 +260,34 @@ class TypeVar(object):
|
||||
floats=floats,
|
||||
bools=bools)
|
||||
|
||||
@staticmethod
|
||||
def singleton(typ):
|
||||
# type: (types.ValueType) -> TypeVar
|
||||
"""Create a type variable that can only assume a single type."""
|
||||
if isinstance(typ, types.VectorType):
|
||||
scalar = typ.base
|
||||
lanes = (typ.lanes, typ.lanes)
|
||||
elif isinstance(typ, types.ScalarType):
|
||||
scalar = typ
|
||||
lanes = (1, 1)
|
||||
|
||||
ints = None
|
||||
floats = None
|
||||
bools = None
|
||||
|
||||
if isinstance(scalar, types.IntType):
|
||||
ints = (scalar.bits, scalar.bits)
|
||||
elif isinstance(scalar, types.FloatType):
|
||||
floats = (scalar.bits, scalar.bits)
|
||||
elif isinstance(scalar, types.BoolType):
|
||||
bools = (scalar.bits, scalar.bits)
|
||||
|
||||
tv = TypeVar(
|
||||
typ.name, 'typeof({})'.format(typ),
|
||||
ints, floats, bools, simd=lanes)
|
||||
tv.singleton_type = typ
|
||||
return tv
|
||||
|
||||
def __str__(self):
|
||||
# type: () -> str
|
||||
return "`{}`".format(self.name)
|
||||
@@ -317,5 +347,8 @@ class TypeVar(object):
|
||||
# type: () -> TypeVar
|
||||
if self.is_derived:
|
||||
return self.base
|
||||
elif self.singleton_type:
|
||||
# A singleton type variable is not a proper free variable.
|
||||
return None
|
||||
else:
|
||||
return self
|
||||
|
||||
Reference in New Issue
Block a user