Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-91432: Add FOR_END, alternate: keep FOR_ITER around. #91472

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f7c220a
Implement FOR_END
sweeneyde Feb 20, 2022
4178c30
Use jabs, not jrel
sweeneyde Feb 20, 2022
245b43f
Bump magic
sweeneyde Feb 20, 2022
61a85f0
merge with main
sweeneyde Mar 1, 2022
b0030d5
merge with main
sweeneyde Mar 20, 2022
8f3cd5a
Remove FOR_ITER entirely
sweeneyde Mar 20, 2022
4520a5a
Fix some failng tests
sweeneyde Mar 20, 2022
6284f20
exclude test_frozenmain
sweeneyde Mar 20, 2022
3a0d53e
merge with main
sweeneyde Mar 24, 2022
3b328e7
fix some tracing tests
sweeneyde Mar 24, 2022
bdd7f07
fix swap pop/push in frameobject.c
sweeneyde Mar 24, 2022
b20fed8
merge with main
sweeneyde Apr 9, 2022
77d52e6
Get test.test_trace passing
sweeneyde Apr 10, 2022
53a8734
when marking backwards jumps, set todo=1
sweeneyde Apr 10, 2022
7d32011
remove debugging code
sweeneyde Apr 10, 2022
4a2e781
update test_dis
sweeneyde Apr 10, 2022
0af7e1e
make FOR_END a relative jump
sweeneyde Apr 10, 2022
d3552aa
make test.test_bdb and test.test_pdb pass
sweeneyde Apr 10, 2022
563ca00
refactor maybe_call_line_trace logic
sweeneyde Apr 11, 2022
719e756
add blurb
sweeneyde Apr 11, 2022
8626569
FOR_ITER --> FOR_END in dis.rst
sweeneyde Apr 11, 2022
2884727
FOR_ITER --> FOR_END in specialize.c
sweeneyde Apr 11, 2022
44baacc
Update whatsnew
sweeneyde Apr 11, 2022
0adffbd
Merge branch 'main' into for_end
sweeneyde Apr 11, 2022
7a62601
merge with main
sweeneyde Apr 11, 2022
92e8c6d
Merge branch 'for_end' of https://github.com/sweeneyde/cpython into f…
sweeneyde Apr 11, 2022
e4f240e
fix test_dis
sweeneyde Apr 12, 2022
6c2e53d
Merge branch 'main' into for_end
sweeneyde Apr 12, 2022
32bed37
fix opcode.h
sweeneyde Apr 12, 2022
1a19c91
Re-introduce FOR_ITER
sweeneyde Apr 12, 2022
4c0c180
get test_dis passing
sweeneyde Apr 12, 2022
29decd2
get test_trace passing
sweeneyde Apr 12, 2022
069dd51
fix restore mark_stacks behavior of FOR_ITER
sweeneyde Apr 12, 2022
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
9 changes: 6 additions & 3 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1009,12 +1009,15 @@ iterations of the loop.
.. versionadded:: 3.1


.. opcode:: FOR_ITER (delta)
.. opcode:: FOR_END (delta)

TOS is an :term:`iterator`. Call its :meth:`~iterator.__next__` method. If
this yields a new value, push it on the stack (leaving the iterator below
it). If the iterator indicates it is exhausted, TOS is popped, and the byte
code counter is incremented by *delta*.
it), then decrement the bytecode counter by *delta*. If the iterator
indicates it is exhausted, then pop TOS and do not decrement the bytecode
counter.

.. versionadded:: 3.11


.. opcode:: LOAD_GLOBAL (namei)
Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,12 @@ CPython bytecode changes
opcodes to speed up conditional jumps.


* Replaced :opcode:`FOR_ITER` (jumps if iterator exhausted)
with :opcode:`FOR_END` (jumps if iterator is not exhausted).
At the end of each ``for``-loop iteration, instead of an unconditional
jump targeted at a conditional jump, there is now
only one (conditional) jump.

Deprecated
==========

Expand Down
105 changes: 55 additions & 50 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
LOAD_CONST = opmap['LOAD_CONST']
LOAD_GLOBAL = opmap['LOAD_GLOBAL']
BINARY_OP = opmap['BINARY_OP']
JUMP_BACKWARD = opmap['JUMP_BACKWARD']

CACHE = opmap["CACHE"]

Expand Down Expand Up @@ -393,7 +392,8 @@ def parse_exception_table(code):
return entries

def _is_backward_jump(op):
return 'JUMP_BACKWARD' in opname[op]
name = opname[op]
return 'JUMP_BACKWARD' in name or name == "FOR_END"

def _get_instructions_bytes(code, varname_from_oparg=None,
names=None, co_consts=None,
Expand Down
2 changes: 1 addition & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3492).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3493).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

_PYCACHE = '__pycache__'
Expand Down
4 changes: 4 additions & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def jabs_op(name, op, entries=0):
jrel_op('POP_JUMP_BACKWARD_IF_NONE', 174)
jrel_op('POP_JUMP_BACKWARD_IF_FALSE', 175)
jrel_op('POP_JUMP_BACKWARD_IF_TRUE', 176)
jrel_op('FOR_END', 177)


del def_op, name_op, jrel_op, jabs_op
Expand Down Expand Up @@ -267,6 +268,9 @@ def jabs_op(name, op, entries=0):
"JUMP_BACKWARD": [
"JUMP_BACKWARD_QUICK",
],
"FOR_END": [
"FOR_END_QUICK",
],
"LOAD_ATTR": [
"LOAD_ATTR_ADAPTIVE",
"LOAD_ATTR_INSTANCE_VALUE",
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test__opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def test_stack_effect_jump(self):
self.assertEqual(stack_effect(JUMP_IF_TRUE_OR_POP, 0), 0)
self.assertEqual(stack_effect(JUMP_IF_TRUE_OR_POP, 0, jump=True), 0)
self.assertEqual(stack_effect(JUMP_IF_TRUE_OR_POP, 0, jump=False), -1)
FOR_ITER = dis.opmap['FOR_ITER']
self.assertEqual(stack_effect(FOR_ITER, 0), 1)
self.assertEqual(stack_effect(FOR_ITER, 0, jump=True), -1)
self.assertEqual(stack_effect(FOR_ITER, 0, jump=False), 1)
FOR_END = dis.opmap['FOR_END']
self.assertEqual(stack_effect(FOR_END, 0), 1)
self.assertEqual(stack_effect(FOR_END, 0, jump=True), 1)
self.assertEqual(stack_effect(FOR_END, 0, jump=False), -1)
JUMP_FORWARD = dis.opmap['JUMP_FORWARD']
self.assertEqual(stack_effect(JUMP_FORWARD, 0), 0)
self.assertEqual(stack_effect(JUMP_FORWARD, 0, jump=True), 0)
Expand Down
21 changes: 12 additions & 9 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,15 @@ def bug708901():
%3d PRECALL 2
CALL 2
GET_ITER
>> FOR_ITER 2 (to 40)
STORE_FAST 0 (res)
FOR_ITER 5 (to 46)
>> STORE_FAST 0 (res)

%3d JUMP_BACKWARD 3 (to 34)
%3d NOP

%3d >> LOAD_CONST 0 (None)
%3d FOR_END 3 (to 36)
LOAD_CONST 0 (None)
RETURN_VALUE
>> LOAD_CONST 0 (None)
RETURN_VALUE
""" % (bug708901.__code__.co_firstlineno,
bug708901.__code__.co_firstlineno + 1,
Expand Down Expand Up @@ -569,13 +572,13 @@ def foo(x):
%3d RESUME 0
BUILD_LIST 0
LOAD_FAST 0 (.0)
>> FOR_ITER 7 (to 24)
STORE_FAST 1 (z)
FOR_ITER 7 (to 24)
>> STORE_FAST 1 (z)
LOAD_DEREF 2 (x)
LOAD_FAST 1 (z)
BINARY_OP 0 (+)
LIST_APPEND 2
JUMP_BACKWARD 8 (to 8)
FOR_END 7 (to 10)
>> RETURN_VALUE
""" % (dis_nested_1,
__file__,
Expand Down Expand Up @@ -1226,7 +1229,7 @@ def _prepare_test_cases():
Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=20, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=30, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='FOR_ITER', opcode=93, arg=32, argval=98, argrepr='to 98', offset=32, starts_line=None, is_jump_target=True, positions=None),
Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=34, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=34, starts_line=None, is_jump_target=True, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=36, starts_line=4, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=48, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='PRECALL', opcode=166, arg=1, argval=1, argrepr='', offset=50, starts_line=None, is_jump_target=False, positions=None),
Expand All @@ -1243,7 +1246,7 @@ def _prepare_test_cases():
Instruction(opname='POP_JUMP_FORWARD_IF_FALSE', opcode=114, arg=2, argval=96, argrepr='to 96', offset=90, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=92, starts_line=8, is_jump_target=False, positions=None),
Instruction(opname='JUMP_FORWARD', opcode=110, arg=16, argval=128, argrepr='to 128', offset=94, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='JUMP_BACKWARD', opcode=140, arg=33, argval=32, argrepr='to 32', offset=96, starts_line=7, is_jump_target=True, positions=None),
Instruction(opname='FOR_END', opcode=177, arg=32, argval=34, argrepr='to 34', offset=96, starts_line=3, is_jump_target=True, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=98, starts_line=10, is_jump_target=True, positions=None),
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=110, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='PRECALL', opcode=166, arg=1, argval=1, argrepr='', offset=112, starts_line=None, is_jump_target=False, positions=None),
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,16 @@ def f(x):
def test_assignment_idiom_in_comprehensions(self):
def listcomp():
return [y for x in a for y in [f(x)]]
self.assertEqual(count_instr_recursively(listcomp, 'FOR_ITER'), 1)
self.assertEqual(count_instr_recursively(listcomp, 'FOR_END'), 1)
def setcomp():
return {y for x in a for y in [f(x)]}
self.assertEqual(count_instr_recursively(setcomp, 'FOR_ITER'), 1)
self.assertEqual(count_instr_recursively(setcomp, 'FOR_END'), 1)
def dictcomp():
return {y: y for x in a for y in [f(x)]}
self.assertEqual(count_instr_recursively(dictcomp, 'FOR_ITER'), 1)
self.assertEqual(count_instr_recursively(dictcomp, 'FOR_END'), 1)
def genexpr():
return (y for x in a for y in [f(x)])
self.assertEqual(count_instr_recursively(genexpr, 'FOR_ITER'), 1)
self.assertEqual(count_instr_recursively(genexpr, 'FOR_END'), 1)

def test_format_combinations(self):
flags = '-+ #0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Replaced the ``FOR_ITER`` opcode that lived at the top of ``for``-loops with a
``FOR_END`` opcode that lives at the bottom. This form of `Loop Inversion
<https://en.wikipedia.org/wiki/Loop_inversion>`_ replaces the (unconditional
jump, conditional jump) pair with just one conditional jump during most
iterations.
15 changes: 14 additions & 1 deletion Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ mark_stacks(PyCodeObject *code_obj, int len)
stacks[j] = target_stack;
break;
}
case FOR_END:
{
int64_t target_stack = push_value(next_stack, Object);
stacks[i+1] = pop_value(next_stack);
j = i + 1 - get_arg(code, i);
if (stacks[j] == UNINITIALIZED && j < i) {
todo = 1;
}
assert(j < len);
assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
stacks[j] = target_stack;
break;
}
case END_ASYNC_FOR:
next_stack = pop_value(pop_value(pop_value(next_stack)));
stacks[i+1] = next_stack;
Expand Down Expand Up @@ -580,7 +593,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore
}
else if (err < 0) {
if (start_stack == OVERFLOWED) {
msg = "stack to deep to analyze";
msg = "stack too deep to analyze";
}
else if (start_stack == UNINITIALIZED) {
msg = "can't jump from within an exception handler";
Expand Down
Loading