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

bpo-46329: Split calls into precall and call instructions. #30855

Merged
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
bd00f82
Add PRECALL_FUNCTION opcode.
markshannon Jan 5, 2022
78e6da5
Move all call-shape variables into a struct.
markshannon Jan 6, 2022
42c0460
Move 'call shape' varaibles into struct.
markshannon Jan 7, 2022
a947bf1
Replace CALL_NO_KW and CALL_KW with KW_NAMES and CALL instructions.
markshannon Jan 7, 2022
dbab710
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 7, 2022
f47f8a1
Remove named_args from call_shape struct.
markshannon Jan 7, 2022
7ea553f
Handle bound-methods in PRECALL instructions.
markshannon Jan 7, 2022
0b52de2
Restore specialization of calls to builtin functions.
markshannon Jan 10, 2022
d2e5834
Get specialized versions of CALL working again.
markshannon Jan 11, 2022
6785a3b
Fix test_dis
markshannon Jan 11, 2022
5935a5c
Fix a couple of compiler warnings.
markshannon Jan 11, 2022
e39432f
Specialize for calls to builtin types with any number of arguments.
markshannon Jan 12, 2022
637f5e0
Refine fail stats for calls to classes.
markshannon Jan 12, 2022
28663f4
Refine fail stats for calls.
markshannon Jan 12, 2022
0239c26
Specialize for calls to method-descriptors of the shape obj.meth()
markshannon Jan 12, 2022
e076297
Refine fail stats for calls.
markshannon Jan 12, 2022
d74939c
Factor out bound-method handling code.
markshannon Jan 13, 2022
1294c5e
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 13, 2022
3323b3b
Specialize for builtin methods taking using the METH_FASTCALL | METH_…
markshannon Jan 13, 2022
32dbf1a
Allow kwnames for specialized calls to builtin types.
markshannon Jan 13, 2022
bdf9f15
Specialize calls to tuple() and str().
markshannon Jan 14, 2022
c285be8
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 21, 2022
918b390
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 21, 2022
a77e63e
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 22, 2022
cb5301d
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 24, 2022
100a5f6
Fix test_dis
markshannon Jan 24, 2022
4c2da8b
Fix specialization of method descriptors.
markshannon Jan 24, 2022
a7d7b1e
Move check for bound-methods from PRECALL opcodes back to CALL opcode.
markshannon Jan 24, 2022
43fdcdf
Relax check in CALL_NO_KW_LIST_APPEND to any list subclass.
markshannon Jan 24, 2022
5d35ce3
Correct name of CallShape field.
markshannon Jan 24, 2022
639adb8
Add news item.
markshannon Jan 25, 2022
6914240
Minor tidy ups.
markshannon Jan 25, 2022
3478c55
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 25, 2022
2cb70ad
Fix typos in news item.
markshannon Jan 27, 2022
61ef1b4
Address review comments.
markshannon Jan 27, 2022
4035699
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 27, 2022
38ebe7c
Update docs for new opcodes.
markshannon Jan 27, 2022
ddc2f57
Fix up formatting
markshannon Jan 27, 2022
28a810d
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 27, 2022
d4f710c
Fix up test_dis
markshannon Jan 27, 2022
2fa70a0
Address review comments.
markshannon Jan 27, 2022
2b59fc9
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 27, 2022
5cb8d58
Merge branch 'main' into split-calls-into-precall-and-call-part-2
markshannon Jan 27, 2022
bd77c5d
Clarify ownership of callable in CallShape.
markshannon Jan 28, 2022
3ac2ed8
Fix comment.
markshannon Jan 28, 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
70 changes: 40 additions & 30 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ the following command can be used to display the disassembly of
:func:`myfunc`::

>>> dis.dis(myfunc)
2 0 LOAD_GLOBAL 0 (len)
2 LOAD_FAST 0 (alist)
4 CALL_NO_KW 1
6 RETURN_VALUE
1 0 RESUME 0

2 2 LOAD_GLOBAL 0 (len)
4 LOAD_FAST 0 (alist)
6 PRECALL_FUNCTION 1
8 CALL 0
10 RETURN_VALUE

(The "2" is a line number).

Expand Down Expand Up @@ -102,9 +105,11 @@ Example::
>>> for instr in bytecode:
... print(instr.opname)
...
RESUME
LOAD_GLOBAL
LOAD_FAST
CALL_NO_KW
PRECALL_FUNCTION
CALL
RETURN_VALUE


Expand Down Expand Up @@ -617,7 +622,7 @@ iterations of the loop.
.. opcode:: LOAD_BUILD_CLASS

Pushes :func:`builtins.__build_class__` onto the stack. It is later called
by :opcode:`CALL_NO_KW` to construct a class.
to construct a class.


.. opcode:: BEFORE_WITH (delta)
Expand Down Expand Up @@ -1058,30 +1063,19 @@ iterations of the loop.
with ``__cause__`` set to ``TOS``)


.. opcode:: CALL_NO_KW (argc)

Calls a callable object with positional arguments.
*argc* indicates the number of positional arguments.
The top of the stack contains positional arguments, with the right-most
argument on top. Below the arguments is a callable object to call.
``CALL_NO_KW`` pops all arguments and the callable object off the stack,
calls the callable object with those arguments, and pushes the return value
returned by the callable object.

.. versionadded:: 3.11
.. opcode:: CALL (named)

Calls a callable object with the number of positional arguments specified by
the preceding :opcode:`PRECALL_FUNCTION` or :opcode:`PRECALL_METHOD` and
the named arguments specified by the preceding :opcode:`KW_NAMES`, if any.
*named* indicates the number of named arguments.
On the stack are (in ascending order):

.. opcode:: CALL_KW (argc)
* The callable
* The positional arguments
* The named arguments

Calls a callable object with positional (if any) and keyword arguments.
*argc* indicates the total number of positional and keyword arguments.
The top element on the stack contains a tuple with the names of the
keyword arguments, which must be strings.
Below that are the values for the keyword arguments,
in the order corresponding to the tuple.
Below that are positional arguments, with the right-most parameter on
top. Below the arguments is a callable object to call.
``CALL_KW`` pops all arguments and the callable object off the stack,
``CALL`` pops all arguments and the callable object off the stack,
calls the callable object with those arguments, and pushes the return value
returned by the callable object.

Expand All @@ -1108,7 +1102,7 @@ iterations of the loop.
Loads a method named ``co_names[namei]`` from the TOS object. TOS is popped.
This bytecode distinguishes two cases: if TOS has a method with the correct
name, the bytecode pushes the unbound method and TOS. TOS will be used as
the first argument (``self``) by :opcode:`CALL_METHOD` when calling the
the first argument (``self``) by :opcode:`PRECALL_METHOD` when calling the
unbound method. Otherwise, ``NULL`` and the object return by the attribute
lookup are pushed.

Expand All @@ -1117,14 +1111,30 @@ iterations of the loop.

.. opcode:: PRECALL_METHOD (argc)

Prefixes either :opcode:`CALL_NO_KW` or :opcode:`CALL_KW`.
Prefixes :opcode:`CALL` (possibly with an intervening ``KW_NAMES``).
This opcode is designed to be used with :opcode:`LOAD_METHOD`.
Sets internal variables, so that :opcode:`CALL_NO_KW` or :opcode:`CALL_KW`
Sets internal variables, so that :opcode:`CALL`
clean up after :opcode:`LOAD_METHOD` correctly.

.. versionadded:: 3.11


.. opcode:: PRECALL_FUNCTION (args)

Prefixes :opcode:`CALL` (possibly with an intervening ``KW_NAMES``).
Sets internal variables, so that :opcode:`CALL` can execute correctly.

.. versionadded:: 3.11


.. opcode:: KW_NAMES (i)

Stores a reference to ``co_consts[consti]`` into an internal variable
for use by :opcode:`CALL`. ``co_consts[consti]`` must be a tuple of strings.

.. versionadded:: 3.11


.. opcode:: MAKE_FUNCTION (flags)

Pushes a new function object on the stack. From bottom to top, the consumed
Expand Down
3 changes: 2 additions & 1 deletion Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ CPython bytecode changes

* Replaced the three call instructions: :opcode:`CALL_FUNCTION`,
:opcode:`CALL_FUNCTION_KW` and :opcode:`CALL_METHOD` with
:opcode:`CALL_NO_KW`, :opcode:`CALL_KW` and :opcode:`PRECALL_METHOD`.
:opcode:`PRECALL_FUNCTION`, :opcode:`PRECALL_METHOD`, :opcode:`CALL`,
and :opcode:`KW_NAMES`.
This decouples the argument shifting for methods from the handling of
keyword arguments and allows better specialization of calls.

Expand Down
5 changes: 3 additions & 2 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef struct {

typedef struct {
uint32_t func_version;
uint16_t defaults_start;
uint16_t min_args;
uint16_t defaults_len;
} _PyCallCache;

Expand Down Expand Up @@ -271,7 +271,8 @@ int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNI
int _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
int _Py_Specialize_StoreSubscr(PyObject *container, PyObject *sub, _Py_CODEUNIT *instr);
int _Py_Specialize_CallNoKw(PyObject *callable, _Py_CODEUNIT *instr, int nargs, SpecializedCacheEntry *cache, PyObject *builtins);
int _Py_Specialize_CallNoKw(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
PyObject *kwnames, SpecializedCacheEntry *cache, PyObject *builtins);
void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
SpecializedCacheEntry *cache);
void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
Expand Down
3 changes: 3 additions & 0 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ _PyThreadState_BumpFramePointer(PyThreadState *tstate, size_t size)

void _PyThreadState_PopFrame(PyThreadState *tstate, InterpreterFrame *frame);

InterpreterFrame *
_PyFrame_Push(PyThreadState *tstate, PyFunctionObject *func);

#ifdef __cplusplus
}
#endif
Expand Down
79 changes: 43 additions & 36 deletions Include/opcode.h

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

7 changes: 6 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,14 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.11a5 3476 (Add ASYNC_GEN_WRAP opcode)
# Python 3.11a5 3477 (Replace DUP_TOP/DUP_TOP_TWO with COPY and
# ROT_TWO/ROT_THREE/ROT_FOUR/ROT_N with SWAP)
# Python 3.11a5 3478 (New CALL opcodes)

# Python 3.12 will start with magic number 3500


# Python 3.12 will start with magic number 3500


#
# MAGIC must change whenever the bytecode emitted by the compiler may no
# longer be understood by older implementations of the eval loop (usually
Expand All @@ -397,7 +402,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 = (3477).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3478).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

_PYCACHE = '__pycache__'
Expand Down
18 changes: 13 additions & 5 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,12 @@ def jabs_op(name, op):
def_op('DICT_MERGE', 164)
def_op('DICT_UPDATE', 165)

def_op('PRECALL_FUNCTION', 167)
def_op('PRECALL_METHOD', 168)
def_op('CALL_NO_KW', 169)
def_op('CALL_KW', 170)

def_op('CALL', 171)
def_op('KW_NAMES', 172)
markshannon marked this conversation as resolved.
Show resolved Hide resolved
hasconst.append(172)

del def_op, name_op, jrel_op, jabs_op

Expand Down Expand Up @@ -245,16 +248,21 @@ def jabs_op(name, op):
"STORE_SUBSCR_ADAPTIVE",
"STORE_SUBSCR_LIST_INT",
"STORE_SUBSCR_DICT",
"CALL_NO_KW_ADAPTIVE",
"CALL_ADAPTIVE",
"CALL_BUILTIN_CLASS",
"CALL_NO_KW_BUILTIN_O",
"CALL_NO_KW_BUILTIN_FAST",
"CALL_BUILTIN_FAST_WITH_KEYWORDS",
"CALL_NO_KW_LEN",
"CALL_NO_KW_ISINSTANCE",
"CALL_NO_KW_PY_SIMPLE",
"CALL_PY_EXACT_ARGS",
"CALL_PY_WITH_DEFAULTS",
"CALL_NO_KW_LIST_APPEND",
"CALL_NO_KW_METHOD_DESCRIPTOR_O",
"CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS",
"CALL_NO_KW_STR_1",
"CALL_NO_KW_TUPLE_1",
"CALL_NO_KW_TYPE_1",
"CALL_NO_KW_BUILTIN_CLASS_1",
"CALL_NO_KW_METHOD_DESCRIPTOR_FAST",
"JUMP_ABSOLUTE_QUICK",
"LOAD_ATTR_ADAPTIVE",
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,9 +837,8 @@ def foo(x):
opcodes = list(dis.get_instructions(func))
instructions = [opcode.opname for opcode in opcodes]
self.assertNotIn('LOAD_METHOD', instructions)
self.assertNotIn('CALL_METHOD', instructions)
self.assertIn('LOAD_ATTR', instructions)
self.assertIn('CALL_NO_KW', instructions)
self.assertIn('PRECALL_FUNCTION', instructions)

def test_lineno_procedure_call(self):
def call():
Expand Down Expand Up @@ -1096,7 +1095,7 @@ def test_multiline_expression(self):
)
"""
compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL_NO_KW',
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
line=1, end_line=3, column=0, end_column=1)

def test_very_long_line_end_offset(self):
Expand All @@ -1106,7 +1105,7 @@ def test_very_long_line_end_offset(self):
snippet = f"g('{long_string}')"

compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL_NO_KW',
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
line=1, end_line=1, column=None, end_column=None)

def test_complex_single_line_expression(self):
Expand Down
Loading