Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,18 @@ def check_native_int_range(rtype: RPrimitive, n: int) -> bool:
return -limit <= n < limit


def wrap_unsigned_native_int(rtype: RType, n: int) -> int:
"""Wrap n around to the range of rtype if it's an unsigned native int type.

Unsigned native int arithmetic wraps around on overflow at runtime, so
compile-time evaluation must do the same (e.g. -u64(1) is 2**64 - 1).
Return n unchanged for other types.
"""
if is_fixed_width_rtype(rtype) and not rtype.is_signed:
return n & ((1 << (8 * rtype.size)) - 1)
return n


# Buffers for vec item types that have a packed representation
#
# Note that the 'items' fields are variable-length arrays, and mypyc IR isn't
Expand Down
9 changes: 8 additions & 1 deletion mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
object_pointer_rprimitive,
object_rprimitive,
str_rprimitive,
wrap_unsigned_native_int,
)
from mypyc.irbuild.constant_fold import constant_fold_expr
from mypyc.irbuild.context import FuncInfo, ImplicitClass
Expand Down Expand Up @@ -1403,7 +1404,13 @@ def emit_load_final(
is_local: whether the name is a local variable
"""
if final_var.final_value is not None: # this is safe even for non-native names
return self.load_literal_value(final_var.final_value, line)
value = final_var.final_value
if isinstance(value, int) and not isinstance(value, bool):
# mypy evaluates Final initializers such as "-X" using int semantics,
# but unsigned native ints wrap around. (Skip bools, since the helper
# returns a plain int when compiled.)
value = wrap_unsigned_native_int(self.type_to_rtype(typ), value)
return self.load_literal_value(value, line)
elif is_native and not is_local and module_prefix(self.graph, fullname):
# A local final variable isn't stored in a static (its fullname is just the
# bare name, which could also be the name of a module), but a literal value
Expand Down
11 changes: 11 additions & 0 deletions mypyc/irbuild/constant_fold.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
UnaryExpr,
Var,
)
from mypyc.ir.rtypes import wrap_unsigned_native_int
from mypyc.irbuild.util import bytes_from_str

if TYPE_CHECKING:
Expand All @@ -41,6 +42,16 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue |

Return None otherwise.
"""
value = constant_fold_expr_inner(builder, expr)
if isinstance(value, int) and not isinstance(value, bool) and not isinstance(expr, IntExpr):
# Evaluate each operation using native int semantics if needed, since
# unsigned native ints wrap around on overflow (e.g. -X if X is a u64).
# Bools are skipped, since the helper returns a plain int when compiled.
value = wrap_unsigned_native_int(builder.node_type(expr), value)
return value


def constant_fold_expr_inner(builder: IRBuilder, expr: Expression) -> ConstantValue | None:
if isinstance(expr, IntExpr):
return expr.value
if isinstance(expr, FloatExpr):
Expand Down
12 changes: 10 additions & 2 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
pointer_rprimitive,
short_int_rprimitive,
str_rprimitive,
wrap_unsigned_native_int,
)
from mypyc.irbuild.util import concrete_arg_kind
from mypyc.irbuild.vec import vec_contains, vec_get_item, vec_len_native
Expand Down Expand Up @@ -826,6 +827,13 @@ def coerce_short_int_to_fixed_width(self, src: Value, target_type: RType, line:
return res

def coerce_fixed_width_to_int(self, src: Value, line: int) -> Value:
if isinstance(src, Integer):
value = src.numeric_value()
if not (MIN_SHORT_INT <= value <= MAX_SHORT_INT):
# The constant doesn't fit in a short int, so load it as an int literal.
# The fast path below would be unreachable, and C compilers may reject
# it, since it would truncate the constant (e.g. u64 on 32-bit platforms).
return self.load_int(value, line)
if (
(is_int32_rprimitive(src.type) and PLATFORM_SIZE == 8)
or is_int16_rprimitive(src.type)
Expand Down Expand Up @@ -2180,8 +2188,8 @@ def unary_minus(self, value: Value, line: int) -> Value:
"""Perform unary '-'."""
typ = value.type
if isinstance(value, Integer):
# TODO: Overflow? Unsigned?
return Integer(-value.numeric_value(), typ, line)
# TODO: Signed overflow?
return Integer(wrap_unsigned_native_int(typ, -value.numeric_value()), typ, line)
elif isinstance(value, Float):
return Float(-value.value, line)
elif is_fixed_width_rtype(typ):
Expand Down
82 changes: 82 additions & 0 deletions mypyc/test-data/irbuild-u64.test
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,88 @@ L0:
c = 1
return 1

[case testU64NegateLiteral]
from mypy_extensions import u64

def f() -> None:
a = -u64(0)
b = -u64(1)
c = -u64(5)
[out]
def f():
a, b, c :: u64
L0:
a = 0
b = 18446744073709551615
c = 18446744073709551611
return 1

[case testU64LargeConstantToInt_64bit]
from mypy_extensions import u64

def f() -> int:
return -u64(1)
def g() -> int:
return -u64(5)
[out]
def f():
r0 :: int
L0:
r0 = object 18446744073709551615
return r0
def g():
r0 :: int
L0:
r0 = object 18446744073709551611
return r0

[case testU64FinalWraparound]
from typing import Final
from mypy_extensions import u64

X: Final[u64] = 1
Y: Final[u64] = -X

def f() -> None:
a = -X
b = X - 2
c = ~X
d = (X - 2) // 2
e = Y
def g() -> str:
return "%d" % (-X,)
[typing fixtures/typing-full.pyi]
[out]
def f():
r0 :: int
a :: u64
r1 :: int
b :: u64
r2 :: int
c :: u64
r3 :: int
d :: u64
r4 :: int
e :: u64
L0:
r0 = object 18446744073709551615
a = 18446744073709551615
r1 = object 18446744073709551615
b = 18446744073709551615
r2 = object 18446744073709551614
c = 18446744073709551614
r3 = object 9223372036854775807
d = 9223372036854775807
r4 = object 18446744073709551615
e = 18446744073709551615
return 1
def g():
r0, r1 :: str
L0:
r0 = '18446744073709551615'
r1 = CPyStr_Build(1, r0)
return r1

[case testU64FromShortInt_64bit]
from mypy_extensions import u64

Expand Down
49 changes: 49 additions & 0 deletions mypyc/test-data/irbuild-u8.test
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,55 @@ L0:
y = x
return y

[case testU8NegateLiteral]
from mypy_extensions import u8

def f() -> None:
a = -u8(0)
b = -u8(1)
c = -u8(5)
[out]
def f():
a, b, c :: u8
L0:
a = 0
b = 255
c = 251
return 1

[case testU8FinalWraparound]
from typing import Final
from mypy_extensions import u8

X: Final[u8] = 1
Y: Final[u8] = -X

def f() -> None:
a = -X
b = X - 2
c = ~X
d = (X - 2) // 2
e = Y
def g() -> str:
return "%d" % (-X,)
[typing fixtures/typing-full.pyi]
[out]
def f():
a, b, c, d, e :: u8
L0:
a = 255
b = 255
c = 254
d = 127
e = 255
return 1
def g():
r0, r1 :: str
L0:
r0 = '255'
r1 = CPyStr_Build(1, r0)
return r1

[case testU8DivisionByConstant]
from mypy_extensions import u8

Expand Down
63 changes: 63 additions & 0 deletions mypyc/test-data/run-u16.test
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,69 @@ def test_u16_from_large_small_literal() -> None:
x = u16(0)
assert x == 0

def neg_literal_to_int() -> int:
return -u16(1)

def eq_neg_literal(x: u16) -> bool:
return x == -u16(1)

def gt_neg_literal(x: u16) -> bool:
return x > -u16(5)

def and_neg_literal(x: u16) -> u16:
return x & -u16(8)

def div_neg_literal(x: u16) -> u16:
return x // -u16(5)

def test_negate_literal() -> None:
assert -u16(0) == 0
assert -u16(1) == 65535
assert int(-u16(1)) == 2**16 - 1
assert neg_literal_to_int() == 2**16 - 1
assert str(-u16(1)) == "65535"
assert -u16(65535) == 1
assert 0 - u16(1) == 65535
assert ~u16(0) == 65535
one: u16 = 1 + int()
assert -u16(1) == -one
assert -u16(5) == -(one * 5)
assert eq_neg_literal(65535)
assert not eq_neg_literal(1)
assert gt_neg_literal(65535)
assert not gt_neg_literal(65531)
assert and_neg_literal(65535) == 65528
assert div_neg_literal(65535) == 1
assert div_neg_literal(5) == 0

ONE_FINAL: Final[u16] = 1
MAX_FINAL: Final[u16] = 65535
NEG_ONE_FINAL: Final[u16] = -ONE_FINAL

class FinalAttrs:
TWO: Final[u16] = 2

def neg_final() -> u16:
return -ONE_FINAL

def neg_final_to_int() -> int:
return -ONE_FINAL

def test_final_wraparound() -> None:
one: u16 = 1 + int()
max_val: u16 = 65535 + int()
assert -ONE_FINAL == -one == 65535
assert neg_final() == 65535
assert neg_final_to_int() == 65535
assert ONE_FINAL - 2 == one - 2 == 65535
assert ~ONE_FINAL == ~one == 65534
assert MAX_FINAL + 1 == max_val + 1 == 0
assert MAX_FINAL * 2 == max_val * 2 == 65534
assert (ONE_FINAL - 2) // 2 == (one - 2) // 2 == 32767
assert -ONE_FINAL >> 1 == -one >> 1 == 32767
assert -FinalAttrs.TWO == -(one * 2) == 65534
assert NEG_ONE_FINAL == 65535

def test_u16_truncate_from_i64() -> None:
large = i64(2**32 + 65536 + 157 + int())
x = u16(large)
Expand Down
63 changes: 63 additions & 0 deletions mypyc/test-data/run-u32.test
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,69 @@ def test_u32_from_large_small_literal() -> None:
x = u32(0)
assert x == 0

def neg_literal_to_int() -> int:
return -u32(1)

def eq_neg_literal(x: u32) -> bool:
return x == -u32(1)

def gt_neg_literal(x: u32) -> bool:
return x > -u32(5)

def and_neg_literal(x: u32) -> u32:
return x & -u32(8)

def div_neg_literal(x: u32) -> u32:
return x // -u32(5)

def test_negate_literal() -> None:
assert -u32(0) == 0
assert -u32(1) == 4294967295
assert int(-u32(1)) == 2**32 - 1
assert neg_literal_to_int() == 2**32 - 1
assert str(-u32(1)) == "4294967295"
assert -u32(4294967295) == 1
assert 0 - u32(1) == 4294967295
assert ~u32(0) == 4294967295
one: u32 = 1 + int()
assert -u32(1) == -one
assert -u32(5) == -(one * 5)
assert eq_neg_literal(4294967295)
assert not eq_neg_literal(1)
assert gt_neg_literal(4294967295)
assert not gt_neg_literal(4294967291)
assert and_neg_literal(4294967295) == 4294967288
assert div_neg_literal(4294967295) == 1
assert div_neg_literal(5) == 0

ONE_FINAL: Final[u32] = 1
MAX_FINAL: Final[u32] = 4294967295
NEG_ONE_FINAL: Final[u32] = -ONE_FINAL

class FinalAttrs:
TWO: Final[u32] = 2

def neg_final() -> u32:
return -ONE_FINAL

def neg_final_to_int() -> int:
return -ONE_FINAL

def test_final_wraparound() -> None:
one: u32 = 1 + int()
max_val: u32 = 4294967295 + int()
assert -ONE_FINAL == -one == 4294967295
assert neg_final() == 4294967295
assert neg_final_to_int() == 4294967295
assert ONE_FINAL - 2 == one - 2 == 4294967295
assert ~ONE_FINAL == ~one == 4294967294
assert MAX_FINAL + 1 == max_val + 1 == 0
assert MAX_FINAL * 2 == max_val * 2 == 4294967294
assert (ONE_FINAL - 2) // 2 == (one - 2) // 2 == 2147483647
assert -ONE_FINAL >> 1 == -one >> 1 == 2147483647
assert -FinalAttrs.TWO == -(one * 2) == 4294967294
assert NEG_ONE_FINAL == 4294967295

def test_u32_truncate_from_i64() -> None:
large = i64(2**32 + 4294967296 + 157 + int())
x = u32(large)
Expand Down
Loading
Loading