Skip to content
Closed
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
53 changes: 51 additions & 2 deletions mypyc/irbuild/for_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
RVec,
bool_rprimitive,
c_pyssize_t_rprimitive,
check_native_int_range,
int_rprimitive,
is_bool_or_bit_rprimitive,
is_bytearray_rprimitive,
Expand Down Expand Up @@ -1468,10 +1469,16 @@ def init(self, start_reg: Value, end_reg: Value, step: int) -> None:
self.end_target = end_reg
if is_short_int_rprimitive(start_reg.type) and is_short_int_rprimitive(end_reg.type):
index_type: RType = short_int_rprimitive
elif is_fixed_width_rtype(end_reg.type):
elif is_fixed_width_rtype(end_reg.type) and check_native_int_range(
end_reg.type, abs(step)
):
index_type = end_reg.type
else:
index_type = int_rprimitive
if is_fixed_width_rtype(end_reg.type):
# The step doesn't fit in the native int type, so use arbitrary
# precision ints instead.
self.end_target = builder.coerce(end_reg, int_rprimitive, self.line)
index_reg = Register(index_type, line=self.line)
builder.assign(index_reg, start_reg, self.line)
self.index_reg = index_reg
Expand Down Expand Up @@ -1532,13 +1539,55 @@ def gen_step(self) -> None:
IntOp.ADD,
line,
)

elif is_fixed_width_rtype(self.index_reg.type):
self.gen_fixed_width_step()
return
else:
new_val = builder.binary_op(
builder.read(self.index_reg, line), Integer(self.step), "+", line
)
builder.assign(self.index_reg, new_val, line)

def gen_fixed_width_step(self) -> None:
builder = self.builder
line = self.line
index_type = self.index_reg.type
assert is_fixed_width_rtype(index_type)
step = abs(self.step)
op = IntOp.ADD if self.step > 0 else IntOp.SUB
done = None
if step > 1:
# The index is known to be less than the end value (or greater, if
# the step is negative), so stepping by 1 can't overflow, but a larger
# step can. If stepping would go past the bounds of the native int type,
# it would also go past the end value, so end the loop by setting the
# index to the end value instead.
bits = index_type.size * 8
if self.step > 0:
if index_type.is_signed:
limit = (1 << (bits - 1)) - 1 - step
else:
limit = (1 << bits) - 1 - step
cmp = ">"
else:
limit = (-(1 << (bits - 1)) if index_type.is_signed else 0) + step
cmp = "<"
overflow = builder.binary_op(
builder.read(self.index_reg, line), Integer(limit, index_type), cmp, line
)
overflow_block, step_block, done = BasicBlock(), BasicBlock(), BasicBlock()
builder.add_bool_branch(overflow, overflow_block, step_block)
builder.activate_block(overflow_block)
builder.assign(self.index_reg, builder.read(self.end_target, line), line)
builder.goto(done)
builder.activate_block(step_block)
new_val = builder.int_op(
index_type, builder.read(self.index_reg, line), Integer(step, index_type), op, line
)
builder.assign(self.index_reg, new_val, line)
if done is not None:
builder.goto_and_activate(done)


class ForInfiniteCounter(ForGenerator):
"""Generate optimized IR for a for loop counting from 0 to infinity."""
Expand Down
35 changes: 35 additions & 0 deletions mypyc/test-data/irbuild-i64.test
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,41 @@ L3:
L4:
return 1

[case testI64ForLoopOverRangeWithStep]
from mypy_extensions import i64

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

[case testI64MethodDefaultValueOverride]
from mypy_extensions import i64

Expand Down
26 changes: 26 additions & 0 deletions mypyc/test-data/irbuild-u64.test
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ L0:
f = r5
return 1

[case testU64ForLoopOverRangeWithNegativeStep]
from mypy_extensions import u64

def f(a: u64, b: u64) -> None:
for x in range(a, b, -1):
y = x
[out]
def f(a, b):
a, b, r0 :: u64
r1 :: bit
x, y, r2 :: u64
L0:
r0 = a
L1:
r1 = r0 > b :: signed
if r1 goto L2 else goto L4 :: bool
L2:
x = r0
y = x
L3:
r2 = r0 - 1
r0 = r2
goto L1
L4:
return 1

[case testU64DetectMoreOutOfRangeLiterals]
from mypy_extensions import u64

Expand Down
152 changes: 152 additions & 0 deletions mypyc/test-data/run-loops.test
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,158 @@ class Index:
def __index__(self) -> int:
return self.n

[case testForRangeNativeIntStep]
from itertools import islice
from typing import Callable
from mypy_extensions import i8, i16, i32, i64, u8, u64

# Each loop gives up after a few hundred iterations, in case it doesn't terminate.
LIMIT = 300

def u8_up2(start: u8, end: u8) -> list[int]:
result = []
for i in range(start, end, 2):
result.append(i)
if len(result) > LIMIT:
break
return result

def u8_up200(start: u8, end: u8) -> list[int]:
result = []
for i in range(start, end, 200):
result.append(i)
if len(result) > LIMIT:
break
return result

def u8_down1(start: u8, end: u8) -> list[int]:
result = []
for i in range(start, end, -1):
result.append(i)
if len(result) > LIMIT:
break
return result

def u8_down3(start: u8, end: u8) -> list[int]:
result = []
for i in range(start, end, -3):
result.append(i)
if len(result) > LIMIT:
break
return result

def i8_up2(start: i8, end: i8) -> list[int]:
result = []
for i in range(start, end, 2):
result.append(i)
if len(result) > LIMIT:
break
return result

def i8_up200(start: i8, end: i8) -> list[int]:
result = []
for i in range(start, end, 200):
result.append(i)
if len(result) > LIMIT:
break
return result

def i8_down3(start: i8, end: i8) -> list[int]:
result = []
for i in range(start, end, -3):
result.append(i)
if len(result) > LIMIT:
break
return result

def i16_up5(start: i16, end: i16) -> list[int]:
result = []
for i in range(start, end, 5):
result.append(i)
if len(result) > LIMIT:
break
return result

def i32_down2(start: i32, end: i32) -> list[int]:
result = []
for i in range(start, end, -2):
result.append(i)
if len(result) > LIMIT:
break
return result

def i64_up2(start: i64, end: i64) -> list[int]:
result = []
for i in range(start, end, 2):
result.append(i)
if len(result) > LIMIT:
break
return result

def i64_down2(start: i64, end: i64) -> list[int]:
result = []
for i in range(start, end, -2):
result.append(i)
if len(result) > LIMIT:
break
return result

def u64_up2(start: u64, end: u64) -> list[int]:
result = []
for i in range(start, end, 2):
result.append(i)
if len(result) > LIMIT:
break
return result

def u64_down1(start: u64, end: u64) -> list[int]:
result = []
for i in range(start, end, -1):
result.append(i)
if len(result) > LIMIT:
break
return result

def u64_down2(start: u64, end: u64) -> list[int]:
result = []
for i in range(start, end, -2):
result.append(i)
if len(result) > LIMIT:
break
return result

def check(f: Callable[[int, int], list[int]], step: int, values: list[int]) -> None:
for start in values:
for end in values:
expected = list(islice(range(start, end, step), LIMIT + 1))
assert f(start, end) == expected, (start, end, step)

def test_unsigned() -> None:
values = [0, 1, 2, 3, 4, 100, 251, 252, 253, 254, 255]
check(u8_up2, 2, values)
check(u8_up200, 200, values)
check(u8_down1, -1, values)
check(u8_down3, -3, values)
m = 2**64
values = [0, 1, 2, 3, 2**63, m - 4, m - 3, m - 2, m - 1]
check(u64_up2, 2, values)
check(u64_down1, -1, values)
check(u64_down2, -2, values)

def test_signed() -> None:
values = [-128, -127, -126, -125, -1, 0, 1, 124, 125, 126, 127]
check(i8_up2, 2, values)
check(i8_up200, 200, values)
check(i8_down3, -3, values)
n = 2**15
check(i16_up5, 5, [-n, -n + 4, -n + 5, 0, n - 6, n - 5, n - 4, n - 1])
n = 2**31
check(i32_down2, -2, [-n, -n + 1, -n + 2, -n + 3, 0, n - 2, n - 1])
n = 2**63
values = [-n, -n + 1, -n + 2, -n + 3, -1, 0, 1, n - 4, n - 3, n - 2, n - 1]
check(i64_up2, 2, values)
check(i64_down2, -2, values)

[case testForSequences]
from collections.abc import Iterable, Iterator
from typing import Any, NamedTuple, Optional
Expand Down
Loading