summaryrefslogtreecommitdiffstats
path: root/Python/generated_cases.c.h
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2025-01-06 17:54:47 (GMT)
committerGitHub <noreply@github.com>2025-01-06 17:54:47 (GMT)
commitf826beca0cedb8e4b92896544c75fd0d9dcb0446 (patch)
tree87630b6e28e141a0e687e04d2f66d8e4cde21b99 /Python/generated_cases.c.h
parentb9c693dcca01537eee1ef716ffebc632be37594b (diff)
downloadcpython-f826beca0cedb8e4b92896544c75fd0d9dcb0446.zip
cpython-f826beca0cedb8e4b92896544c75fd0d9dcb0446.tar.gz
cpython-f826beca0cedb8e4b92896544c75fd0d9dcb0446.tar.bz2
GH-128375: Better instrument for `FOR_ITER` (GH-128445)
Diffstat (limited to 'Python/generated_cases.c.h')
-rw-r--r--Python/generated_cases.c.h71
1 files changed, 47 insertions, 24 deletions
diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h
index 98743c2..b73844c 100644
--- a/Python/generated_cases.c.h
+++ b/Python/generated_cases.c.h
@@ -3768,11 +3768,15 @@
}
TARGET(END_FOR) {
- frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(END_FOR);
_PyStackRef value;
value = stack_pointer[-1];
+ /* Don't update instr_ptr, so that POP_ITER sees
+ * the FOR_ITER as the previous instruction.
+ * This has the benign side effect that if value is
+ * finalized it will see the location as the FOR_ITER's.
+ */
PyStackRef_CLOSE(value);
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
@@ -3957,10 +3961,8 @@
/* iterator ended normally */
assert(next_instr[oparg].op.code == END_FOR ||
next_instr[oparg].op.code == INSTRUMENTED_END_FOR);
- PyStackRef_CLOSE(iter);
- STACK_SHRINK(1);
- /* Jump forward oparg, then skip following END_FOR and POP_TOP instruction */
- JUMPBY(oparg + 2);
+ /* Jump forward oparg, then skip following END_FOR */
+ JUMPBY(oparg + 1);
DISPATCH();
}
next = PyStackRef_FromPyObjectSteal(next_o);
@@ -4048,10 +4050,8 @@
Py_DECREF(seq);
}
#endif
- PyStackRef_CLOSE(iter);
- STACK_SHRINK(1);
- /* Jump forward oparg, then skip following END_FOR and POP_TOP instructions */
- JUMPBY(oparg + 2);
+ /* Jump forward oparg, then skip following END_FOR instruction */
+ JUMPBY(oparg + 1);
DISPATCH();
}
}
@@ -4091,10 +4091,8 @@
assert(Py_TYPE(r) == &PyRangeIter_Type);
STAT_INC(FOR_ITER, hit);
if (r->len <= 0) {
- STACK_SHRINK(1);
- PyStackRef_CLOSE(iter);
- // Jump over END_FOR and POP_TOP instructions.
- JUMPBY(oparg + 2);
+ // Jump over END_FOR instruction.
+ JUMPBY(oparg + 1);
DISPATCH();
}
}
@@ -4141,10 +4139,8 @@
it->it_seq = NULL;
Py_DECREF(seq);
}
- PyStackRef_CLOSE(iter);
- STACK_SHRINK(1);
- /* Jump forward oparg, then skip following END_FOR and POP_TOP instructions */
- JUMPBY(oparg + 2);
+ /* Jump forward oparg, then skip following END_FOR instruction */
+ JUMPBY(oparg + 1);
DISPATCH();
}
}
@@ -4572,7 +4568,7 @@
}
TARGET(INSTRUMENTED_END_FOR) {
- _Py_CODEUNIT* const this_instr = frame->instr_ptr = next_instr;
+ _Py_CODEUNIT* const this_instr = next_instr;
(void)this_instr;
next_instr += 1;
INSTRUCTION_STATS(INSTRUMENTED_END_FOR);
@@ -4636,6 +4632,7 @@
stack_pointer = _PyFrame_GetStackPointer(frame);
if (next != NULL) {
PUSH(PyStackRef_FromPyObjectSteal(next));
+ INSTRUMENTED_JUMP(this_instr, next_instr, PY_MONITORING_EVENT_BRANCH_LEFT);
}
else {
if (_PyErr_Occurred(tstate)) {
@@ -4653,11 +4650,8 @@
/* iterator ended normally */
assert(next_instr[oparg].op.code == END_FOR ||
next_instr[oparg].op.code == INSTRUMENTED_END_FOR);
- STACK_SHRINK(1);
- PyStackRef_CLOSE(iter_stackref);
- /* Skip END_FOR and POP_TOP */
- _Py_CODEUNIT *target = next_instr + oparg + 2;
- INSTRUMENTED_JUMP(this_instr, target, PY_MONITORING_EVENT_BRANCH_RIGHT);
+ /* Skip END_FOR */
+ JUMPBY(oparg + 1);
}
DISPATCH();
}
@@ -4764,11 +4758,28 @@
}
TARGET(INSTRUMENTED_NOT_TAKEN) {
+ _Py_CODEUNIT* const prev_instr = frame->instr_ptr;
_Py_CODEUNIT* const this_instr = frame->instr_ptr = next_instr;
(void)this_instr;
next_instr += 1;
INSTRUCTION_STATS(INSTRUMENTED_NOT_TAKEN);
- INSTRUMENTED_JUMP(this_instr, next_instr, PY_MONITORING_EVENT_BRANCH_LEFT);
+ (void)this_instr; // INSTRUMENTED_JUMP requires this_instr
+ INSTRUMENTED_JUMP(prev_instr, next_instr, PY_MONITORING_EVENT_BRANCH_LEFT);
+ DISPATCH();
+ }
+
+ TARGET(INSTRUMENTED_POP_ITER) {
+ _Py_CODEUNIT* const prev_instr = frame->instr_ptr;
+ _Py_CODEUNIT* const this_instr = frame->instr_ptr = next_instr;
+ (void)this_instr;
+ next_instr += 1;
+ INSTRUCTION_STATS(INSTRUMENTED_POP_ITER);
+ _PyStackRef iter;
+ iter = stack_pointer[-1];
+ INSTRUMENTED_JUMP(prev_instr, this_instr+1, PY_MONITORING_EVENT_BRANCH_RIGHT);
+ PyStackRef_CLOSE(iter);
+ stack_pointer += -1;
+ assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
@@ -6693,6 +6704,18 @@
DISPATCH();
}
+ TARGET(POP_ITER) {
+ frame->instr_ptr = next_instr;
+ next_instr += 1;
+ INSTRUCTION_STATS(POP_ITER);
+ _PyStackRef value;
+ value = stack_pointer[-1];
+ PyStackRef_CLOSE(value);
+ stack_pointer += -1;
+ assert(WITHIN_STACK_BOUNDS());
+ DISPATCH();
+ }
+
TARGET(POP_JUMP_IF_FALSE) {
_Py_CODEUNIT* const this_instr = frame->instr_ptr = next_instr;
(void)this_instr;