Skip to content

Conversation

@markshannon
Copy link
Member

@markshannon markshannon commented Jan 18, 2024

In order to support tier 2 side-exits from FOR_ITER variants, we need a target that has the same stack as the exhaustion guard.
Currently we can either jump to the END_FOR, as we do for generators or the instruction afterwards, which we do for FOR_ITER.
In order to exit a guard we need a target in between those instructions, that pops the iterator, but not the value.
The compiler emits END_FOR; POP_TOP instead of END_FOR.
Previously:

   END_FOR (Pops two values) <- FOR_ITEN_GEN targets this
   ...                       <- FOR_ITER targets this

Now:

   END_FOR (Pops one value)  <- FOR_ITEN_GEN targets this
   POP_TOP (Pops one value)  <- _GUARD_NOT_EXHAUSTED can target this
   ...                       <- FOR_ITER targets this

@gvanrossum
Copy link
Member

gvanrossum commented Jan 18, 2024

Maybe we can do without a separate END_FOR opcode and instead just generate a POP_TOP?

EDIT: Never mind, it's needed for instrumentation.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, never mind my premature commend about END_FOR/POP_TOP.

Comment on lines +672 to +674
string = &_Py_ID(alias);
assert(_PyUnicode_CheckConsistency(string, 1));
_PyUnicode_InternInPlace(interp, &string);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The updates to this file (and a few other generated files) are spurious and will disappear after merging the latest main.)

@markshannon
Copy link
Member Author

The benchmarks show no performance impact.

@markshannon
Copy link
Member Author

EDIT: Never mind, it's needed for instrumentation.

That's evidently not clear. The order of the POP_TOP and END_FOR is important as well.
I'll add some comments.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@brettcannon brettcannon removed their request for review January 19, 2024 18:31
@gvanrossum
Copy link
Member

Oh, fun stuff. Windows compilation error. :-(

@markshannon markshannon merged commit 981d172 into python:main Jan 24, 2024
@markshannon markshannon deleted the split-end-for branch February 1, 2024 19:50
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…GH-114247)

* Compiler emits END_FOR; POP_TOP instead of END_FOR. To support tier 2 side exits in loops.
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…GH-114247)

* Compiler emits END_FOR; POP_TOP instead of END_FOR. To support tier 2 side exits in loops.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants