Skip to content

Commit b2fc30f

Browse files
authored
[mypyc] Have for ... in range() correctly generate raw comparisons (#8857)
This means we generate "i < n" instead of CPyTagged_Lt or whatever. This doesn't actually help performance on -O3 at all, since clang is able to figure it out. It helps on -Og, though, and it saves the compiler having to do some work.
1 parent 85897bd commit b2fc30f

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

Diff for: mypyc/irbuild/for_helpers.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,6 @@ def begin_body(self) -> None:
604604
class ForRange(ForGenerator):
605605
"""Generate optimized IR for a for loop over an integer range."""
606606

607-
# TODO: Use a separate register for the index to allow safe index mutation.
608-
609607
def init(self, start_reg: Value, end_reg: Value, step: int) -> None:
610608
builder = self.builder
611609
self.start_reg = start_reg
@@ -625,7 +623,7 @@ def gen_condition(self) -> None:
625623
line = self.line
626624
# Add loop condition check.
627625
cmp = '<' if self.step > 0 else '>'
628-
comparison = builder.binary_op(builder.read(self.index_target, line),
626+
comparison = builder.binary_op(builder.read(self.index_reg, line),
629627
builder.read(self.end_target, line), cmp, line)
630628
builder.add_bool_branch(comparison, self.body_block, self.loop_exit)
631629

Diff for: mypyc/test-data/irbuild-lists.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ L0:
185185
r2 = r0
186186
i = r2
187187
L1:
188-
r3 = i < r1 :: int
188+
r3 = r2 < r1 :: short_int
189189
if r3 goto L2 else goto L4 :: bool
190190
L2:
191191
r4 = l[i] :: list

Diff for: mypyc/test-data/irbuild-statements.test

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ L0:
2121
r3 = r1
2222
i = r3
2323
L1:
24-
r4 = i < r2 :: int
24+
r4 = r3 < r2 :: short_int
2525
if r4 goto L2 else goto L4 :: bool
2626
L2:
2727
r5 = x + i :: int
@@ -53,7 +53,7 @@ L0:
5353
r2 = r0
5454
i = r2
5555
L1:
56-
r3 = i > r1 :: int
56+
r3 = r2 > r1 :: short_int
5757
if r3 goto L2 else goto L4 :: bool
5858
L2:
5959
L3:
@@ -107,7 +107,7 @@ L0:
107107
r2 = r0
108108
n = r2
109109
L1:
110-
r3 = n < r1 :: int
110+
r3 = r2 < r1 :: short_int
111111
if r3 goto L2 else goto L4 :: bool
112112
L2:
113113
goto L4
@@ -197,7 +197,7 @@ L0:
197197
r2 = r0
198198
n = r2
199199
L1:
200-
r3 = n < r1 :: int
200+
r3 = r2 < r1 :: short_int
201201
if r3 goto L2 else goto L4 :: bool
202202
L2:
203203
L3:
@@ -991,7 +991,7 @@ L2:
991991
r8 = r2 < r7 :: short_int
992992
if r8 goto L3 else goto L6 :: bool
993993
L3:
994-
r9 = z < r4 :: int
994+
r9 = r5 < r4 :: short_int
995995
if r9 goto L4 else goto L6 :: bool
996996
L4:
997997
r10 = unbox(bool, r6)

0 commit comments

Comments
 (0)