-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
GH-132554: Fix tier2 FOR_ITER
implementation and optimizations
#135137
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -3125,100 +3125,47 @@ dummy_func( | |||||||
} | ||||||||
|
||||||||
replaced op(_FOR_ITER, (iter, null_or_index -- iter, null_or_index, next)) { | ||||||||
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */ | ||||||||
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter); | ||||||||
if (PyStackRef_IsTaggedInt(null_or_index)) { | ||||||||
next = _PyForIter_NextWithIndex(iter_o, null_or_index); | ||||||||
if (PyStackRef_IsNull(next)) { | ||||||||
JUMPBY(oparg + 1); | ||||||||
DISPATCH(); | ||||||||
} | ||||||||
null_or_index = PyStackRef_IncrementTaggedIntNoOverflow(null_or_index); | ||||||||
} | ||||||||
else { | ||||||||
PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o); | ||||||||
if (next_o == NULL) { | ||||||||
if (_PyErr_Occurred(tstate)) { | ||||||||
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration); | ||||||||
if (!matches) { | ||||||||
ERROR_NO_POP(); | ||||||||
} | ||||||||
_PyEval_MonitorRaise(tstate, frame, this_instr); | ||||||||
_PyErr_Clear(tstate); | ||||||||
} | ||||||||
/* iterator ended normally */ | ||||||||
assert(next_instr[oparg].op.code == END_FOR || | ||||||||
next_instr[oparg].op.code == INSTRUMENTED_END_FOR); | ||||||||
/* Jump forward oparg, then skip following END_FOR */ | ||||||||
JUMPBY(oparg + 1); | ||||||||
DISPATCH(); | ||||||||
_PyStackRef item = _PyForIter_VirtualIteratorNext(tstate, frame, iter, &null_or_index); | ||||||||
if (!PyStackRef_IsValid(item)) { | ||||||||
if (PyStackRef_IsError(item)) { | ||||||||
ERROR_NO_POP(); | ||||||||
} | ||||||||
next = PyStackRef_FromPyObjectSteal(next_o); | ||||||||
JUMPBY(oparg + 1); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add back the comment that this is skipping the
Suggested change
|
||||||||
DISPATCH(); | ||||||||
} | ||||||||
next = item; | ||||||||
} | ||||||||
|
||||||||
op(_FOR_ITER_TIER_TWO, (iter, null_or_index -- iter, null_or_index, next)) { | ||||||||
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */ | ||||||||
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter); | ||||||||
EXIT_IF(!PyStackRef_IsNull(null_or_index)); | ||||||||
PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o); | ||||||||
if (next_o == NULL) { | ||||||||
if (_PyErr_Occurred(tstate)) { | ||||||||
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration); | ||||||||
if (!matches) { | ||||||||
ERROR_NO_POP(); | ||||||||
} | ||||||||
_PyEval_MonitorRaise(tstate, frame, frame->instr_ptr); | ||||||||
_PyErr_Clear(tstate); | ||||||||
_PyStackRef item = _PyForIter_VirtualIteratorNext(tstate, frame, iter, &null_or_index); | ||||||||
if (!PyStackRef_IsValid(item)) { | ||||||||
if (PyStackRef_IsError(item)) { | ||||||||
ERROR_NO_POP(); | ||||||||
} | ||||||||
/* iterator ended normally */ | ||||||||
/* The translator sets the deopt target just past the matching END_FOR */ | ||||||||
EXIT_IF(true); | ||||||||
} | ||||||||
next = PyStackRef_FromPyObjectSteal(next_o); | ||||||||
// Common case: no jump, leave it to the code generator | ||||||||
next = item; | ||||||||
} | ||||||||
|
||||||||
|
||||||||
macro(FOR_ITER) = _SPECIALIZE_FOR_ITER + _FOR_ITER; | ||||||||
|
||||||||
|
||||||||
inst(INSTRUMENTED_FOR_ITER, (unused/1, iter, null_or_index -- iter, null_or_index, next)) { | ||||||||
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter); | ||||||||
if (PyStackRef_IsTaggedInt(null_or_index)) { | ||||||||
next = _PyForIter_NextWithIndex(iter_o, null_or_index); | ||||||||
if (PyStackRef_IsNull(next)) { | ||||||||
JUMPBY(oparg + 1); | ||||||||
DISPATCH(); | ||||||||
} | ||||||||
null_or_index = PyStackRef_IncrementTaggedIntNoOverflow(null_or_index); | ||||||||
INSTRUMENTED_JUMP(this_instr, next_instr, PY_MONITORING_EVENT_BRANCH_LEFT); | ||||||||
} | ||||||||
else { | ||||||||
PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o); | ||||||||
if (next_o != NULL) { | ||||||||
next = PyStackRef_FromPyObjectSteal(next_o); | ||||||||
INSTRUMENTED_JUMP(this_instr, next_instr, PY_MONITORING_EVENT_BRANCH_LEFT); | ||||||||
} | ||||||||
else { | ||||||||
if (_PyErr_Occurred(tstate)) { | ||||||||
int matches = _PyErr_ExceptionMatches(tstate, PyExc_StopIteration); | ||||||||
if (!matches) { | ||||||||
ERROR_NO_POP(); | ||||||||
} | ||||||||
_PyEval_MonitorRaise(tstate, frame, this_instr); | ||||||||
_PyErr_Clear(tstate); | ||||||||
} | ||||||||
/* iterator ended normally */ | ||||||||
assert(next_instr[oparg].op.code == END_FOR || | ||||||||
next_instr[oparg].op.code == INSTRUMENTED_END_FOR); | ||||||||
/* Skip END_FOR */ | ||||||||
JUMPBY(oparg + 1); | ||||||||
DISPATCH(); | ||||||||
_PyStackRef item = _PyForIter_VirtualIteratorNext(tstate, frame, iter, &null_or_index); | ||||||||
if (!PyStackRef_IsValid(item)) { | ||||||||
if (PyStackRef_IsError(item)) { | ||||||||
ERROR_NO_POP(); | ||||||||
} | ||||||||
JUMPBY(oparg + 1); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
DISPATCH(); | ||||||||
} | ||||||||
next = item; | ||||||||
INSTRUMENTED_JUMP(this_instr, next_instr, PY_MONITORING_EVENT_BRANCH_LEFT); | ||||||||
} | ||||||||
|
||||||||
|
||||||||
op(_ITER_CHECK_LIST, (iter, null_or_index -- iter, null_or_index)) { | ||||||||
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter); | ||||||||
EXIT_IF(Py_TYPE(iter_o) != &PyList_Type); | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3439,8 +3439,8 @@ _PyEval_LoadName(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject *na | |||||
return value; | ||||||
} | ||||||
|
||||||
_PyStackRef | ||||||
_PyForIter_NextWithIndex(PyObject *seq, _PyStackRef index) | ||||||
static _PyStackRef | ||||||
foriter_next(PyObject *seq, _PyStackRef index) | ||||||
{ | ||||||
assert(PyStackRef_IsTaggedInt(index)); | ||||||
assert(PyTuple_CheckExact(seq) || PyList_CheckExact(seq)); | ||||||
|
@@ -3459,6 +3459,30 @@ _PyForIter_NextWithIndex(PyObject *seq, _PyStackRef index) | |||||
return PyStackRef_FromPyObjectSteal(item); | ||||||
} | ||||||
|
||||||
_PyStackRef _PyForIter_VirtualIteratorNext(PyThreadState* tstate, _PyInterpreterFrame* frame, _PyStackRef iter, _PyStackRef* index_ptr) | ||||||
{ | ||||||
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter); | ||||||
_PyStackRef index = *index_ptr; | ||||||
if (PyStackRef_IsTaggedInt(index)) { | ||||||
*index_ptr = PyStackRef_IncrementTaggedIntNoOverflow(index); | ||||||
return foriter_next(iter_o, index); | ||||||
} | ||||||
PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o); | ||||||
if (next_o == NULL) { | ||||||
if (_PyErr_Occurred(tstate)) { | ||||||
if (_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) { | ||||||
_PyEval_MonitorRaise(tstate, frame, frame->instr_ptr); | ||||||
_PyErr_Clear(tstate); | ||||||
} | ||||||
else { | ||||||
return PyStackRef_ERROR; | ||||||
|
return PyStackRef_ERROR; | |
return PyStackRef_ERROR; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.