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
20 changes: 15 additions & 5 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,16 +1404,25 @@ def deserialize(cls, data: JsonDict, ctx: DeserMaps) -> RArray:
)


def check_native_int_range(rtype: RPrimitive, n: int) -> bool:
"""Is n within the range of a native, fixed-width int type?
def native_int_bounds(rtype: RPrimitive) -> tuple[int, int]:
"""Return the smallest and largest values of a native, fixed-width int type.

Assume the type is a fixed-width int type.
"""
if not rtype.is_signed:
return 0 <= n < (1 << (8 * rtype.size))
return 0, (1 << (8 * rtype.size)) - 1
else:
limit = 1 << (rtype.size * 8 - 1)
return -limit <= n < limit
return -limit, limit - 1


def check_native_int_range(rtype: RPrimitive, n: int) -> bool:
"""Is n within the range of a native, fixed-width int type?

Assume the type is a fixed-width int type.
"""
lo, hi = native_int_bounds(rtype)
return lo <= n <= hi


def wrap_unsigned_native_int(rtype: RType, n: int) -> int:
Expand All @@ -1424,7 +1433,8 @@ def wrap_unsigned_native_int(rtype: RType, n: int) -> int:
Return n unchanged for other types.
"""
if is_fixed_width_rtype(rtype) and not rtype.is_signed:
return n & ((1 << (8 * rtype.size)) - 1)
# The largest value of an unsigned type is also a mask of all its bits
return n & native_int_bounds(rtype)[1]
return n


Expand Down
96 changes: 81 additions & 15 deletions mypyc/irbuild/for_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
from mypy.patterns import AsPattern, MappingPattern, StarredPattern
from mypy.traverser import TraverserVisitor
from mypy.types import LiteralType, TupleType, get_proper_type, get_proper_types
from mypyc.common import GENERATOR_HELPER_NAME
from mypyc.common import GENERATOR_HELPER_NAME, MAX_SHORT_INT, MIN_SHORT_INT
from mypyc.ir.ops import (
ERR_NEVER,
BasicBlock,
Branch,
ComparisonOp,
Integer,
IntOp,
LoadAddress,
Expand All @@ -59,6 +60,7 @@
)
from mypyc.ir.rtypes import (
RInstance,
RPrimitive,
RTuple,
RType,
RVec,
Expand All @@ -76,6 +78,7 @@
is_str_rprimitive,
is_tagged,
is_tuple_rprimitive,
native_int_bounds,
object_pointer_rprimitive,
object_rprimitive,
short_int_rprimitive,
Expand Down Expand Up @@ -1466,7 +1469,11 @@ def init(self, start_reg: Value, end_reg: Value, step: int) -> None:
self.end_reg = end_reg
self.step = step
self.end_target = end_reg
if is_short_int_rprimitive(start_reg.type) and is_short_int_rprimitive(end_reg.type):
if (
is_short_int_rprimitive(start_reg.type)
and is_short_int_rprimitive(end_reg.type)
and not short_int_range_step_may_overflow(end_reg, step)
):
index_type: RType = short_int_rprimitive
elif is_fixed_width_rtype(end_reg.type):
index_type = end_reg.type
Expand Down Expand Up @@ -1519,26 +1526,85 @@ def begin_body(self) -> None:
def gen_step(self) -> None:
builder = self.builder
line = self.line
index_type = self.index_reg.type
index = builder.read(self.index_reg, line)

# Increment index register. If the range is known to fit in short ints, use
# short ints.
if is_short_int_rprimitive(self.start_reg.type) and is_short_int_rprimitive(
self.end_reg.type
):
# short ints (init() has checked that the step can't overflow).
if is_short_int_rprimitive(index_type):
new_val = builder.int_op(
short_int_rprimitive,
builder.read(self.index_reg, line),
Integer(self.step),
IntOp.ADD,
line,
short_int_rprimitive, index, Integer(self.step), IntOp.ADD, line
)

elif is_fixed_width_rtype(index_type):
self.gen_fixed_width_overflow_check(index, index_type)
if self.step < 0 and not index_type.is_signed:
# A negative step isn't a valid unsigned value, so subtract its magnitude.
# Signed types add the step, since its magnitude may not fit (-128 for i8).
new_val = builder.binary_op(index, Integer(-self.step), "-", line)
else:
new_val = builder.binary_op(index, Integer(self.step), "+", line)
else:
new_val = builder.binary_op(
builder.read(self.index_reg, line), Integer(self.step), "+", line
)
# Tagged int arithmetic can't overflow. The step may not fit in a short int.
new_val = builder.binary_op(index, builder.load_int(self.step, line), "+", line)
builder.assign(self.index_reg, new_val, line)

def gen_fixed_width_overflow_check(self, index: Value, index_type: RPrimitive) -> None:
"""Exit the loop if stepping a native int index would overflow.

If index + step doesn't fit in the index type, it's past the end of the range,
so the loop is done. A step of 1 or -1 can't overflow, since index < end (or
index > end) holds here.
"""
step = self.step
if step in (1, -1):
return
lo, hi = native_int_bounds(index_type)
end = self.end_target
# Overflow if index > hi - step (or index < lo - step, if the step is negative)
limit = (hi if step > 0 else lo) - step
if not lo <= limit <= hi:
# The step doesn't fit in the index type. This is reported as an error elsewhere.
return
if isinstance(end, Integer) and not range_step_may_overflow(
end.numeric_value(), step, lo, hi
):
return
if step > 0:
op = ComparisonOp.SGT if index_type.is_signed else ComparisonOp.UGT
else:
op = ComparisonOp.SLT if index_type.is_signed else ComparisonOp.ULT
builder = self.builder
line = self.line
overflow = builder.add(ComparisonOp(index, Integer(limit, index_type, line), op, line))
ok = BasicBlock()
builder.add(Branch(overflow, self.loop_exit, ok, Branch.BOOL, rare=True))
builder.activate_block(ok)


def short_int_range_step_may_overflow(end: Value, step: int) -> bool:
"""Could index + step overflow a short int in a short_int range loop?

Apart from literals, short_int values are lengths, counts and similar values, which
are far from the short int limits. Assume that the magnitude of a non-literal end
value is at most MAX_SHORT_INT // 2. The check is exact for a literal end.
"""
if not MIN_SHORT_INT <= step <= MAX_SHORT_INT:
return True
if isinstance(end, Integer):
end_value = end.numeric_value()
else:
end_value = MAX_SHORT_INT // 2 if step > 0 else -(MAX_SHORT_INT // 2)
return range_step_may_overflow(end_value, step, MIN_SHORT_INT, MAX_SHORT_INT)


def range_step_may_overflow(end: int, step: int, lo: int, hi: int) -> bool:
"""Could index + step fall outside [lo, hi] for an index in range(..., end, step)?

The step is only taken when index < end (or index > end, if the step is negative).
"""
last = end - 1 if step > 0 else end + 1
return not lo <= last + step <= hi


class ForInfiniteCounter(ForGenerator):
"""Generate optimized IR for a for loop counting from 0 to infinity."""
Expand Down
11 changes: 3 additions & 8 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
is_uint16_rprimitive,
is_uint32_rprimitive,
is_uint64_rprimitive,
native_int_bounds,
none_rprimitive,
object_pointer_rprimitive,
object_rprimitive,
Expand Down Expand Up @@ -699,10 +700,8 @@ def check_fixed_width_range(
"""
in_range, in_range2 = BasicBlock(), BasicBlock()

size = target_type.size
upper_bound = 1 << (size * 8 - 1)
if not target_type.is_signed:
upper_bound *= 2
lower_bound, max_value = native_int_bounds(target_type)
upper_bound = max_value + 1

# Check if value < upper_bound
check_upper = self.add(
Expand All @@ -713,10 +712,6 @@ def check_fixed_width_range(
self.activate_block(in_range)

# Check if value >= lower_bound
if target_type.is_signed:
lower_bound = -upper_bound
else:
lower_bound = 0
check_lower = self.add(
ComparisonOp(src, Integer(lower_bound, src.type), ComparisonOp.SGE, line)
)
Expand Down
55 changes: 55 additions & 0 deletions mypyc/test-data/irbuild-i16.test
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,58 @@ L4:
unreachable
L5:
return r7

[case testI16ForLoopOverRangeWithStepAndConstantEnd]
from mypy_extensions import i16

def f(a: i16) -> None:
# The largest index is 32764, and 32764 + 3 fits in i16: no overflow check
for x in range(a, i16(32765), 3):
y = x

def g(a: i16) -> None:
# The largest index is 32765, and 32765 + 3 doesn't fit in i16: overflow check
for x in range(a, i16(32766), 3):
y = x
[out]
def f(a):
a, r0 :: i16
r1 :: bit
x, y, r2 :: i16
L0:
r0 = a
L1:
r1 = r0 < 32765 :: signed
if r1 goto L2 else goto L4 :: bool
L2:
x = r0
y = x
L3:
r2 = r0 + 3
r0 = r2
goto L1
L4:
return 1
def g(a):
a, r0 :: i16
r1 :: bit
x, y :: i16
r2 :: bit
r3 :: i16
L0:
r0 = a
L1:
r1 = r0 < 32766 :: signed
if r1 goto L2 else goto L5 :: bool
L2:
x = r0
y = x
L3:
r2 = r0 > 32764 :: signed
if r2 goto L5 else goto L4 :: bool
L4:
r3 = r0 + 3
r0 = r3
goto L1
L5:
return 1
58 changes: 58 additions & 0 deletions mypyc/test-data/irbuild-i64.test
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,64 @@ L3:
L4:
return 1

[case testI64ForLoopOverRangeWithStep]
from mypy_extensions import i64

def f(a: i64, b: i64) -> None:
for x in range(a, b, 3):
y = x

def g(a: i64, b: i64) -> None:
for x in range(a, b, -2):
y = x
[out]
def f(a, b):
a, b, r0 :: i64
r1 :: bit
x, y :: i64
r2 :: bit
r3 :: i64
L0:
r0 = a
L1:
r1 = r0 < b :: signed
if r1 goto L2 else goto L5 :: bool
L2:
x = r0
y = x
L3:
r2 = r0 > 9223372036854775804 :: signed
if r2 goto L5 else goto L4 :: bool
L4:
r3 = r0 + 3
r0 = r3
goto L1
L5:
return 1
def g(a, b):
a, b, r0 :: i64
r1 :: bit
x, y :: i64
r2 :: bit
r3 :: i64
L0:
r0 = a
L1:
r1 = r0 > b :: signed
if r1 goto L2 else goto L5 :: bool
L2:
x = r0
y = x
L3:
r2 = r0 < -9223372036854775806 :: signed
if r2 goto L5 else goto L4 :: bool
L4:
r3 = r0 + -2
r0 = r3
goto L1
L5:
return 1

[case testI64MethodDefaultValueOverride]
from mypy_extensions import i64

Expand Down
50 changes: 50 additions & 0 deletions mypyc/test-data/irbuild-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,56 @@ L3:
L4:
return 1

[case testForInRangeStepMayOverflowShortInt_64bit]
def f() -> None:
# 2**62 - 3 + 3 doesn't fit in a short int, so this uses a tagged int index
for i in range(4611686018427387901, 4611686018427387903, 3):
pass
def g(n: list[int]) -> None:
# Still a short int index, since a length can't be close to the limit
for i in range(0, len(n), 3):
pass
[out]
def f():
r0 :: int
r1 :: bit
i, r2 :: int
L0:
r0 = 9223372036854775802
L1:
r1 = int_lt r0, 9223372036854775806
if r1 goto L2 else goto L4 :: bool
L2:
i = r0
L3:
r2 = CPyTagged_Add(r0, 6)
r0 = r2
goto L1
L4:
return 1
def g(n):
n :: list
r0 :: native_int
r1, r2 :: short_int
r3 :: bit
i :: int
r4 :: short_int
L0:
r0 = var_object_size n
r1 = r0 << 1
r2 = 0
L1:
r3 = int_lt r2, r1
if r3 goto L2 else goto L4 :: bool
L2:
i = r2
L3:
r4 = r2 + 6
r2 = r4
goto L1
L4:
return 1

[case testForInRangeNativeIndexArg]
class C:
def __index__(self) -> int:
Expand Down
Loading
Loading