Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypyc] Add 'bit' primitive type and streamline branching #9606

Merged
merged 13 commits into from
Oct 17, 2020
Prev Previous commit
Next Next commit
Various fixes
  • Loading branch information
JukkaL committed Oct 17, 2020
commit 5a1919635c9b255474475691916bbe7083a4f2e6
6 changes: 3 additions & 3 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
c_pyssize_t_rprimitive, is_short_int_rprimitive, is_tagged, PyVarObject, short_int_rprimitive,
is_list_rprimitive, is_tuple_rprimitive, is_dict_rprimitive, is_set_rprimitive, PySetObject,
none_rprimitive, RTuple, is_bool_rprimitive, is_str_rprimitive, c_int_rprimitive,
pointer_rprimitive, PyObject, PyListObject, bit_rprimitive
pointer_rprimitive, PyObject, PyListObject, bit_rprimitive, is_bit_rprimitive
)
from mypyc.ir.func_ir import FuncDecl, FuncSignature
from mypyc.ir.class_ir import ClassIR, all_concrete_classes
Expand Down Expand Up @@ -841,7 +841,7 @@ def shortcircuit_helper(self, op: str,

def add_bool_branch(self, value: Value, true: BasicBlock, false: BasicBlock) -> None:
if is_runtime_subtype(value.type, int_rprimitive):
zero = self.add(LoadInt(0))
zero = self.add(LoadInt(0, rtype=value.type))
value = self.binary_op(value, zero, '!=', value.line)
elif is_same_type(value.type, list_rprimitive):
length = self.builtin_len(value, value.line)
Expand Down Expand Up @@ -873,7 +873,7 @@ def add_bool_branch(self, value: Value, true: BasicBlock, false: BasicBlock) ->
remaining = self.unbox_or_cast(value, value_type, value.line)
self.add_bool_branch(remaining, true, false)
return
elif not is_same_type(value.type, bool_rprimitive):
elif not is_bool_rprimitive(value.type) and not is_bit_rprimitive(value.type):
value = self.call_c(bool_op, [value], value.line)
self.add(Branch(value, true, false, Branch.BOOL))

Expand Down
4 changes: 3 additions & 1 deletion mypyc/rt_subtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from mypyc.ir.rtypes import (
RType, RUnion, RInstance, RPrimitive, RTuple, RVoid, RTypeVisitor, RStruct,
is_int_rprimitive, is_short_int_rprimitive,
is_int_rprimitive, is_short_int_rprimitive, is_bool_rprimitive, is_bit_rprimitive
)
from mypyc.subtype import is_subtype

Expand Down Expand Up @@ -43,6 +43,8 @@ def visit_runion(self, left: RUnion) -> bool:
def visit_rprimitive(self, left: RPrimitive) -> bool:
if is_short_int_rprimitive(left) and is_int_rprimitive(self.right):
return True
if is_bit_rprimitive(left) and is_bool_rprimitive(self.right):
return True
return left is self.right

def visit_rtuple(self, left: RTuple) -> bool:
Expand Down
21 changes: 13 additions & 8 deletions mypyc/subtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

from mypyc.ir.rtypes import (
RType, RInstance, RPrimitive, RTuple, RVoid, RTypeVisitor, RUnion, RStruct,
is_bool_rprimitive, is_int_rprimitive, is_tuple_rprimitive,
is_short_int_rprimitive,
is_object_rprimitive
is_bool_rprimitive, is_int_rprimitive, is_tuple_rprimitive, is_short_int_rprimitive,
is_object_rprimitive, is_bit_rprimitive
)


Expand Down Expand Up @@ -42,11 +41,17 @@ def visit_runion(self, left: RUnion) -> bool:
for item in left.items)

def visit_rprimitive(self, left: RPrimitive) -> bool:
if is_bool_rprimitive(left) and is_int_rprimitive(self.right):
return True
if is_short_int_rprimitive(left) and is_int_rprimitive(self.right):
return True
return left is self.right
right = self.right
if is_bool_rprimitive(left):
if is_int_rprimitive(right):
return True
elif is_bit_rprimitive(left):
if is_bool_rprimitive(right) or is_int_rprimitive(right):
return True
elif is_short_int_rprimitive(left):
if is_int_rprimitive(right):
return True
return left is right

def visit_rtuple(self, left: RTuple) -> bool:
if is_tuple_rprimitive(self.right):
Expand Down
27 changes: 27 additions & 0 deletions mypyc/test/test_subtype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Test cases for is_subtype and is_runtime_subtype."""

import unittest

from mypyc.ir.rtypes import bit_rprimitive, bool_rprimitive, int_rprimitive
from mypyc.subtype import is_subtype
from mypyc.rt_subtype import is_runtime_subtype


class TestSubtype(unittest.TestCase):
def test_bit(self) -> None:
assert is_subtype(bit_rprimitive, bool_rprimitive)
assert is_subtype(bit_rprimitive, int_rprimitive)

def test_bool(self) -> None:
assert not is_subtype(bool_rprimitive, bit_rprimitive)
assert is_subtype(bool_rprimitive, int_rprimitive)


class TestRuntimeSubtype(unittest.TestCase):
def test_bit(self) -> None:
assert is_runtime_subtype(bit_rprimitive, bool_rprimitive)
assert not is_runtime_subtype(bit_rprimitive, int_rprimitive)

def test_bool(self) -> None:
assert not is_runtime_subtype(bool_rprimitive, bit_rprimitive)
assert not is_runtime_subtype(bool_rprimitive, int_rprimitive)