diff --git a/mypyc/ir/rtypes.py b/mypyc/ir/rtypes.py index 18b37f0fcb64..6f89e0e4337d 100644 --- a/mypyc/ir/rtypes.py +++ b/mypyc/ir/rtypes.py @@ -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 diff --git a/mypyc/irbuild/builder.py b/mypyc/irbuild/builder.py index 409fe4a0501d..ef0ab899d088 100644 --- a/mypyc/irbuild/builder.py +++ b/mypyc/irbuild/builder.py @@ -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 @@ -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 diff --git a/mypyc/irbuild/constant_fold.py b/mypyc/irbuild/constant_fold.py index 53274dd3f971..69a3b9893d2d 100644 --- a/mypyc/irbuild/constant_fold.py +++ b/mypyc/irbuild/constant_fold.py @@ -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: @@ -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): diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 5fa291a6ae8e..d3fccb52a4ce 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -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 @@ -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) @@ -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): diff --git a/mypyc/test-data/irbuild-u64.test b/mypyc/test-data/irbuild-u64.test index 765192e9390f..ae9e21306f8a 100644 --- a/mypyc/test-data/irbuild-u64.test +++ b/mypyc/test-data/irbuild-u64.test @@ -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 diff --git a/mypyc/test-data/irbuild-u8.test b/mypyc/test-data/irbuild-u8.test index 7ed94757c6f1..b5133956c1ba 100644 --- a/mypyc/test-data/irbuild-u8.test +++ b/mypyc/test-data/irbuild-u8.test @@ -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 diff --git a/mypyc/test-data/run-u16.test b/mypyc/test-data/run-u16.test index 7278cd8d5b1a..c4d5d077894a 100644 --- a/mypyc/test-data/run-u16.test +++ b/mypyc/test-data/run-u16.test @@ -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) diff --git a/mypyc/test-data/run-u32.test b/mypyc/test-data/run-u32.test index 8f7b727aeb12..13d1ac632a40 100644 --- a/mypyc/test-data/run-u32.test +++ b/mypyc/test-data/run-u32.test @@ -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) diff --git a/mypyc/test-data/run-u64.test b/mypyc/test-data/run-u64.test index 012b26815c47..89ddae598d62 100644 --- a/mypyc/test-data/run-u64.test +++ b/mypyc/test-data/run-u64.test @@ -278,6 +278,69 @@ def test_u64_large_literal_implicit_coercion() -> None: def u64_identity(x: u64) -> u64: return x +def neg_literal_to_int() -> int: + return -u64(1) + +def eq_neg_literal(x: u64) -> bool: + return x == -u64(1) + +def gt_neg_literal(x: u64) -> bool: + return x > -u64(5) + +def and_neg_literal(x: u64) -> u64: + return x & -u64(8) + +def div_neg_literal(x: u64) -> u64: + return x // -u64(5) + +def test_negate_literal() -> None: + assert -u64(0) == 0 + assert -u64(1) == 18446744073709551615 + assert int(-u64(1)) == 2**64 - 1 + assert neg_literal_to_int() == 2**64 - 1 + assert str(-u64(1)) == "18446744073709551615" + assert -u64(18446744073709551615) == 1 + assert 0 - u64(1) == 18446744073709551615 + assert ~u64(0) == 18446744073709551615 + one: u64 = 1 + int() + assert -u64(1) == -one + assert -u64(5) == -(one * 5) + assert eq_neg_literal(18446744073709551615) + assert not eq_neg_literal(1) + assert gt_neg_literal(18446744073709551615) + assert not gt_neg_literal(18446744073709551611) + assert and_neg_literal(18446744073709551615) == 18446744073709551608 + assert div_neg_literal(18446744073709551615) == 1 + assert div_neg_literal(5) == 0 + +ONE_FINAL: Final[u64] = 1 +MAX_FINAL: Final[u64] = 18446744073709551615 +NEG_ONE_FINAL: Final[u64] = -ONE_FINAL + +class FinalAttrs: + TWO: Final[u64] = 2 + +def neg_final() -> u64: + return -ONE_FINAL + +def neg_final_to_int() -> int: + return -ONE_FINAL + +def test_final_wraparound() -> None: + one: u64 = 1 + int() + max_val: u64 = 18446744073709551615 + int() + assert -ONE_FINAL == -one == 18446744073709551615 + assert neg_final() == 18446744073709551615 + assert neg_final_to_int() == 18446744073709551615 + assert ONE_FINAL - 2 == one - 2 == 18446744073709551615 + assert ~ONE_FINAL == ~one == 18446744073709551614 + assert MAX_FINAL + 1 == max_val + 1 == 0 + assert MAX_FINAL * 2 == max_val * 2 == 18446744073709551614 + assert (ONE_FINAL - 2) // 2 == (one - 2) // 2 == 9223372036854775807 + assert -ONE_FINAL >> 1 == -one >> 1 == 9223372036854775807 + assert -FinalAttrs.TWO == -(one * 2) == 18446744073709551614 + assert NEG_ONE_FINAL == 18446744073709551615 + def test_u64_truncate_from_i64() -> None: large = i64(2**62 + 157 + int()) x = u64(large) diff --git a/mypyc/test-data/run-u8.test b/mypyc/test-data/run-u8.test index 05844de8ecff..f0a55cce0801 100644 --- a/mypyc/test-data/run-u8.test +++ b/mypyc/test-data/run-u8.test @@ -246,6 +246,69 @@ def test_u8_from_large_small_literal() -> None: x = u8(0) assert x == 0 +def neg_literal_to_int() -> int: + return -u8(1) + +def eq_neg_literal(x: u8) -> bool: + return x == -u8(1) + +def gt_neg_literal(x: u8) -> bool: + return x > -u8(5) + +def and_neg_literal(x: u8) -> u8: + return x & -u8(8) + +def div_neg_literal(x: u8) -> u8: + return x // -u8(5) + +def test_negate_literal() -> None: + assert -u8(0) == 0 + assert -u8(1) == 255 + assert int(-u8(1)) == 2**8 - 1 + assert neg_literal_to_int() == 2**8 - 1 + assert str(-u8(1)) == "255" + assert -u8(255) == 1 + assert 0 - u8(1) == 255 + assert ~u8(0) == 255 + one: u8 = 1 + int() + assert -u8(1) == -one + assert -u8(5) == -(one * 5) + assert eq_neg_literal(255) + assert not eq_neg_literal(1) + assert gt_neg_literal(255) + assert not gt_neg_literal(251) + assert and_neg_literal(255) == 248 + assert div_neg_literal(255) == 1 + assert div_neg_literal(5) == 0 + +ONE_FINAL: Final[u8] = 1 +MAX_FINAL: Final[u8] = 255 +NEG_ONE_FINAL: Final[u8] = -ONE_FINAL + +class FinalAttrs: + TWO: Final[u8] = 2 + +def neg_final() -> u8: + return -ONE_FINAL + +def neg_final_to_int() -> int: + return -ONE_FINAL + +def test_final_wraparound() -> None: + one: u8 = 1 + int() + max_val: u8 = 255 + int() + assert -ONE_FINAL == -one == 255 + assert neg_final() == 255 + assert neg_final_to_int() == 255 + assert ONE_FINAL - 2 == one - 2 == 255 + assert ~ONE_FINAL == ~one == 254 + assert MAX_FINAL + 1 == max_val + 1 == 0 + assert MAX_FINAL * 2 == max_val * 2 == 254 + assert (ONE_FINAL - 2) // 2 == (one - 2) // 2 == 127 + assert -ONE_FINAL >> 1 == -one >> 1 == 127 + assert -FinalAttrs.TWO == -(one * 2) == 254 + assert NEG_ONE_FINAL == 255 + def test_u8_truncate_from_i64() -> None: large = i64(2**32 + 256 + 157 + int()) x = u8(large)