Skip to content

GH-112354: END_FOR instruction to only pop one value. - #114247

Merged
markshannon merged 8 commits into
python:mainfrom
faster-cpython:split-end-for
Jan 24, 2024
Merged

markshannon merged 8 commits into
python:mainfrom
faster-cpython:split-end-for

Conversation

@markshannon

@markshannon markshannon commented Jan 18, 2024 •

Copy link
Copy Markdown
Member

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

gvanrossum commented Jan 18, 2024 •

Copy link
Copy Markdown
Member

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.

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member Author

The benchmarks show no performance impact.

@markshannon

Copy link
Copy Markdown
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.

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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

3 participants