Skip to content

Commit 7c59fd5

Browse files
committed
Support FOR_ITER specializations as uops
1 parent ffe70c4 commit 7c59fd5

File tree

7 files changed

+261
-174
lines changed

7 files changed

+261
-174
lines changed

Python/bytecodes.c

+3-13
Original file line numberDiff line numberDiff line change
@@ -2468,11 +2468,8 @@ dummy_func(
24682468
Py_DECREF(seq);
24692469
}
24702470
Py_DECREF(iter);
2471-
STACK_SHRINK(1);
2472-
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
24732471
/* Jump forward oparg, then skip following END_FOR instruction */
2474-
JUMPBY(oparg + 1);
2475-
DISPATCH();
2472+
JUMP_POP_DISPATCH(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
24762473
end_for_iter_list:
24772474
// Common case: no jump, leave it to the code generator
24782475
}
@@ -2491,11 +2488,8 @@ dummy_func(
24912488
Py_DECREF(seq);
24922489
}
24932490
Py_DECREF(iter);
2494-
STACK_SHRINK(1);
2495-
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
24962491
/* Jump forward oparg, then skip following END_FOR instruction */
2497-
JUMPBY(oparg + 1);
2498-
DISPATCH();
2492+
JUMP_POP_DISPATCH(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
24992493
end_for_iter_tuple:
25002494
// Common case: no jump, leave it to the code generator
25012495
}
@@ -2505,12 +2499,8 @@ dummy_func(
25052499
DEOPT_IF(Py_TYPE(r) != &PyRangeIter_Type, FOR_ITER);
25062500
STAT_INC(FOR_ITER, hit);
25072501
if (r->len <= 0) {
2508-
STACK_SHRINK(1);
25092502
Py_DECREF(r);
2510-
SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER);
2511-
// Jump over END_FOR instruction.
2512-
JUMPBY(oparg + 1);
2513-
DISPATCH();
2503+
JUMP_POP_DISPATCH(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
25142504
}
25152505
long value = r->start;
25162506
r->start = value + r->step;

Python/ceval.c

+14
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,14 @@ void Py_LeaveRecursiveCall(void)
27102710

27112711
///////////////////// Experimental UOp Interpreter /////////////////////
27122712

2713+
#undef JUMP_POP_DISPATCH
2714+
#define JUMP_POP_DISPATCH(x) \
2715+
do { \
2716+
frame->prev_instr += (x); \
2717+
stack_pointer--; \
2718+
goto exit; \
2719+
} while (0)
2720+
27132721
#undef DEOPT_IF
27142722
#define DEOPT_IF(COND, INSTNAME) \
27152723
if ((COND)) { \
@@ -2790,6 +2798,12 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
27902798
}
27912799
}
27922800

2801+
exit:
2802+
DPRINTF(2, "Jumping out of trace\n");
2803+
_PyFrame_SetStackPointer(frame, stack_pointer);
2804+
Py_DECREF(self);
2805+
return frame;
2806+
27932807
unbound_local_error:
27942808
format_exc_check_arg(tstate, PyExc_UnboundLocalError,
27952809
UNBOUNDLOCAL_ERROR_MSG,

Python/ceval_macros.h

+9
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ GETITEM(PyObject *v, Py_ssize_t i) {
155155
#define JUMPBY(x) (next_instr += (x))
156156
#define SKIP_OVER(x) (next_instr += (x))
157157

158+
// Helper for FOR_ITER and specializations.
159+
// This macro is defined differently in the Tier 2 (uops) interpreter.
160+
#define JUMP_POP_DISPATCH(x) \
161+
do { \
162+
JUMPBY(x); \
163+
stack_pointer--; \
164+
DISPATCH(); \
165+
} while (0)
166+
158167
/* OpCode prediction macros
159168
Some opcodes tend to come in pairs thus making it possible to
160169
predict the second code when the first is run. For example,

Python/executor_cases.c.h

+106-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)