diff --git a/mypyc/irbuild/for_helpers.py b/mypyc/irbuild/for_helpers.py index 445a9ee7562e..deeac34a7a09 100644 --- a/mypyc/irbuild/for_helpers.py +++ b/mypyc/irbuild/for_helpers.py @@ -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, @@ -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 @@ -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.""" diff --git a/mypyc/test-data/irbuild-i64.test b/mypyc/test-data/irbuild-i64.test index 38c6dd5240ed..2b90efb65838 100644 --- a/mypyc/test-data/irbuild-i64.test +++ b/mypyc/test-data/irbuild-i64.test @@ -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 diff --git a/mypyc/test-data/irbuild-u64.test b/mypyc/test-data/irbuild-u64.test index 765192e9390f..5ee90c3645b0 100644 --- a/mypyc/test-data/irbuild-u64.test +++ b/mypyc/test-data/irbuild-u64.test @@ -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 diff --git a/mypyc/test-data/run-loops.test b/mypyc/test-data/run-loops.test index b8d3e5ceef32..fd46077dccc7 100644 --- a/mypyc/test-data/run-loops.test +++ b/mypyc/test-data/run-loops.test @@ -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