Skip to content
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

gh-91432: Specialize FOR_ITER #91713

Merged
merged 44 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
3a7f8df
initial attempt
sweeneyde Apr 17, 2022
3b5ce1e
bump magic
sweeneyde Apr 17, 2022
b21b5f4
Merge remote-tracking branch 'upstream/main' into special_for_iter2
sweeneyde Apr 18, 2022
37269cf
NOTRACE_DISPATCH_SAME_OPARG
sweeneyde Apr 18, 2022
ea0a7ee
Update mark_stacks
sweeneyde Apr 18, 2022
0751228
comment out assertions
sweeneyde Apr 19, 2022
dc80fda
Merge branch 'main' into special_for_iter2
sweeneyde Apr 19, 2022
e429410
Make FOR_ITER_RANGE mutate the local
sweeneyde Apr 19, 2022
1cb2de7
Merge remote-tracking branch 'upstream/main' into special_for_iter2
sweeneyde Apr 19, 2022
73fa01c
Fix overflow
sweeneyde Apr 19, 2022
4213582
fix test
sweeneyde Apr 19, 2022
bf58358
fix dis
sweeneyde Apr 19, 2022
bd7575e
Add more tests, add more casts
sweeneyde Apr 19, 2022
db8754b
Merge branch 'main' into special_for_iter2
sweeneyde Apr 20, 2022
2050c4e
merge with main
sweeneyde Apr 24, 2022
824e966
Fix stats, take out some PREDICT
sweeneyde Apr 24, 2022
6b16772
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 24, 2022
c2a75a5
merge with main
sweeneyde Apr 28, 2022
6696384
remove PREDEICTED(STORE_FAST)
sweeneyde Apr 28, 2022
d297091
assert no tracing
sweeneyde Apr 28, 2022
22635a6
Merge branch 'special_for_iter2' of https://github.com/sweeneyde/cpyt…
sweeneyde Apr 28, 2022
f360d65
remove unnecessary cast
sweeneyde Apr 28, 2022
81e0500
merge with main
sweeneyde May 3, 2022
c2eab68
regen
sweeneyde May 3, 2022
25689c3
merge and bump magic
sweeneyde May 4, 2022
b5df047
merge with main
sweeneyde May 8, 2022
2b1c170
Merge remote-tracking branch 'upstream/main' into special_for_iter2
sweeneyde May 10, 2022
91d280c
Fix test_dis
sweeneyde May 10, 2022
eba5e60
merge with main
sweeneyde May 12, 2022
5b153d7
Merge branch 'main' into special_for_iter2
sweeneyde May 12, 2022
76f9a74
merge with main
sweeneyde May 20, 2022
0565a68
Merge branch 'special_for_iter2' of https://github.com/sweeneyde/cpyt…
sweeneyde May 20, 2022
9ba5b79
merge with main
sweeneyde Jun 9, 2022
6cdf0e4
Add comment about re-using the old PyLongObject
sweeneyde Jun 9, 2022
03dde6a
Merge branch 'main' of https://github.com/python/cpython into special…
sweeneyde Jun 10, 2022
f1e2d39
update test_dis.py
sweeneyde Jun 10, 2022
463c3b9
Merge remote-tracking branch 'upstream/main' into special_for_iter2
sweeneyde Jun 14, 2022
ed29777
use the new exponential backoff
sweeneyde Jun 14, 2022
188b357
merge with main
sweeneyde Jun 18, 2022
36d0999
revert using sdigits, add _PyLong_AssignValue
sweeneyde Jun 19, 2022
ad2e969
revert test_sys sizeof check
sweeneyde Jun 19, 2022
d55868f
revert test_range changes
sweeneyde Jun 19, 2022
2d6ee26
add comment and use Py_ssize_t
sweeneyde Jun 20, 2022
db21da1
Add comment: only positive
sweeneyde Jun 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ typedef struct {

#define INLINE_CACHE_ENTRIES_STORE_SUBSCR CACHE_ENTRIES(_PyStoreSubscrCache)

typedef struct {
_Py_CODEUNIT counter;
} _PyForIterCache;

#define INLINE_CACHE_ENTRIES_FOR_ITER CACHE_ENTRIES(_PyForIterCache)

#define QUICKENING_WARMUP_DELAY 8

/* We want to compare to zero for efficiency, so we offset values accordingly */
Expand Down Expand Up @@ -245,6 +251,7 @@ extern void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs,
_Py_CODEUNIT *instr, int oparg);
extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
int oparg);
extern void _Py_Specialize_ForIter(PyObject *iter, _Py_CODEUNIT *instr);

/* Deallocator function for static codeobjects used in deepfreeze.py */
extern void _PyStaticCode_Dealloc(PyCodeObject *co);
Expand Down
6 changes: 6 additions & 0 deletions Include/internal/pycore_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ _PyList_AppendTakeRef(PyListObject *self, PyObject *newitem)
return _PyList_AppendTakeRefListResize(self, newitem);
}

typedef struct {
PyObject_HEAD
Py_ssize_t it_index;
PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
} _PyListIterObject;

#ifdef __cplusplus
}
#endif
Expand Down
60 changes: 32 additions & 28 deletions Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Include/internal/pycore_range.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef Py_INTERNAL_RANGE_H
#define Py_INTERNAL_RANGE_H
#ifdef __cplusplus
extern "C" {
#endif

#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif

typedef struct {
PyObject_HEAD
sdigit index;
sdigit start;
sdigit step;
sdigit len;
} _PyRangeIterObject;

#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_RANGE_H */
69 changes: 36 additions & 33 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
LOAD_GLOBAL = opmap['LOAD_GLOBAL']
BINARY_OP = opmap['BINARY_OP']
JUMP_BACKWARD = opmap['JUMP_BACKWARD']
FOR_ITER = opmap['FOR_ITER']

CACHE = opmap["CACHE"]

Expand Down Expand Up @@ -471,6 +472,8 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
elif deop in hasjrel:
signed_arg = -arg if _is_backward_jump(deop) else arg
argval = offset + 2 + signed_arg*2
if deop == FOR_ITER:
argval += 2
argrepr = "to " + repr(argval)
elif deop in haslocal or deop in hasfree:
argval, argrepr = _get_name_info(arg, varname_from_oparg)
Expand Down Expand Up @@ -612,11 +615,14 @@ def findlabels(code):
labels = []
for offset, op, arg in _unpack_opargs(code):
if arg is not None:
if op in hasjrel:
if _is_backward_jump(op):
deop = _deoptop(op)
if deop in hasjrel:
if _is_backward_jump(deop):
arg = -arg
label = offset + 2 + arg*2
elif op in hasjabs:
if deop == FOR_ITER:
label += 2
elif deop in hasjabs:
label = arg*2
else:
continue
Expand Down
4 changes: 2 additions & 2 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.11a7 3492 (make POP_JUMP_IF_NONE/NOT_NONE/TRUE/FALSE relative)
# Python 3.11a7 3493 (Make JUMP_IF_TRUE_OR_POP/JUMP_IF_FALSE_OR_POP relative)
# Python 3.11a7 3494 (New location info table)

# Python 3.12a1 3500 (Remove PRECALL opcode)
# Python 3.12a1 3501 (YIELD_VALUE oparg == stack_depth)
# Python 3.12a1 3502 (LOAD_FAST_CHECK, no NULL-check in LOAD_FAST)
# Python 3.12a1 3503 (Shrink LOAD_METHOD cache)
# Python 3.12a1 3504 (Specialization/Cache for FOR_ITER)

# Python 3.13 will start with 3550

Expand All @@ -421,7 +421,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3503).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3504).to_bytes(2, 'little') + b'\r\n'

_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

Expand Down
8 changes: 8 additions & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ def jabs_op(name, op):
"EXTENDED_ARG": [
"EXTENDED_ARG_QUICK",
],
"FOR_ITER": [
"FOR_ITER_ADAPTIVE",
"FOR_ITER_LIST",
"FOR_ITER_RANGE",
],
"JUMP_BACKWARD": [
"JUMP_BACKWARD_QUICK",
],
Expand Down Expand Up @@ -370,6 +375,9 @@ def jabs_op(name, op):
"type_version": 2,
"func_version": 1,
},
"FOR_ITER": {
"counter": 1,
},
"LOAD_ATTR": {
"counter": 1,
"version": 2,
Expand Down
Loading