Skip to content

GH-128914: Remove conditional stack effects from bytecodes.c and the code generators #128918

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

Merged
merged 21 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d85c001
No conditional stack effects for LOAD_GLOBAL or LOAD_ATTR
markshannon Jan 15, 2025
053327a
No conditional stack effects for LOAD_SUPER_ATTR or CALL_FUNCTION_EX
markshannon Jan 15, 2025
029f844
Remove support for conditional stack effects from code generators
markshannon Jan 15, 2025
0f49a42
Fix up tests
markshannon Jan 15, 2025
0515341
Remove 'split' annotation
markshannon Jan 15, 2025
402787c
Rename result of PUSH_NULL
markshannon Jan 16, 2025
6ac95d4
Use full oparg for name index in LOAD_GLOBAL and LOAD_ATTR
markshannon Jan 16, 2025
3e475e8
Use correct magic number
markshannon Jan 16, 2025
a766382
Fix magic number comment
markshannon Jan 16, 2025
4104065
Remove unused function
markshannon Jan 16, 2025
6c1a7eb
Merge branch 'main' into no-conditional-stack-effects
markshannon Jan 16, 2025
17249ba
Update Lib/test/test_monitoring.py
markshannon Jan 16, 2025
9ce0600
Merge branch 'main' into no-conditional-stack-effects
markshannon Jan 17, 2025
7806d43
Merge remote-tracking branch 'faster/no-conditional-stack-effects' in…
markshannon Jan 17, 2025
bbcc0df
Document new LOAD_METHOD instruction. Update docs for LOAD_ATTR
markshannon Jan 17, 2025
0a2f9d1
Add news
markshannon Jan 17, 2025
be67c3e
Remove old docs for LOAD_METHOD
markshannon Jan 17, 2025
5e547a5
Fix example
markshannon Jan 17, 2025
a805197
Fix another example
markshannon Jan 17, 2025
2c2ae8d
Merge branch 'main' into no-conditional-stack-effects
markshannon Jan 20, 2025
d5e47ea
Merge branch 'main' into no-conditional-stack-effects
markshannon Jan 20, 2025
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
31 changes: 17 additions & 14 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ the following command can be used to display the disassembly of
>>> dis.dis(myfunc)
2 RESUME 0
<BLANKLINE>
3 LOAD_GLOBAL 1 (len + NULL)
3 LOAD_GLOBAL 0 (len)
PUSH_NULL
LOAD_FAST 0 (alist)
CALL 1
RETURN_VALUE
Expand Down Expand Up @@ -207,6 +208,7 @@ Example:
...
RESUME
LOAD_GLOBAL
PUSH_NULL
LOAD_FAST
CALL
RETURN_VALUE
Expand Down Expand Up @@ -1215,21 +1217,28 @@ iterations of the loop.

.. opcode:: LOAD_ATTR (namei)

If the low bit of ``namei`` is not set, this replaces ``STACK[-1]`` with
``getattr(STACK[-1], co_names[namei>>1])``.
Replaces ``STACK[-1]`` with ``getattr(STACK[-1], co_names[namei>>1])``.

If the low bit of ``namei`` is set, this will attempt to load a method named
``co_names[namei>>1]`` from the ``STACK[-1]`` object. ``STACK[-1]`` is popped.
.. versionchanged:: 3.12
If the low bit of ``namei`` is set, then a ``NULL`` or ``self`` is
pushed to the stack before the attribute or unbound method respectively.

.. versionchanged:: 3.14
Reverted change from 3.12. The low bit of ``namei`` has no special meaning.


.. opcode:: LOAD_METHOD (namei)

Attempt to load a method named ``co_names[namei>>1]`` from the ``STACK[-1]`` object.
``STACK[-1]`` is popped.
This bytecode distinguishes two cases: if ``STACK[-1]`` has a method with the
correct name, the bytecode pushes the unbound method and ``STACK[-1]``.
``STACK[-1]`` will be used as the first argument (``self``) by :opcode:`CALL`
or :opcode:`CALL_KW` when calling the unbound method.
Otherwise, ``NULL`` and the object returned by
the attribute lookup are pushed.

.. versionchanged:: 3.12
If the low bit of ``namei`` is set, then a ``NULL`` or ``self`` is
pushed to the stack before the attribute or unbound method respectively.
.. versionadded:: 3.14


.. opcode:: LOAD_SUPER_ATTR (namei)
Expand Down Expand Up @@ -1926,12 +1935,6 @@ but are replaced by real opcodes or removed before bytecode is generated.
This opcode is now a pseudo-instruction.


.. opcode:: LOAD_METHOD

Optimized unbound method lookup. Emitted as a ``LOAD_ATTR`` opcode
with a flag set in the arg.


.. _opcode_collections:

Opcode collections
Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ extern void _Py_Specialize_LoadSuperAttr(_PyStackRef global_super, _PyStackRef c
_Py_CODEUNIT *instr, int load_method);
extern void _Py_Specialize_LoadAttr(_PyStackRef owner, _Py_CODEUNIT *instr,
PyObject *name);
extern void _Py_Specialize_LoadMethod(_PyStackRef owner, _Py_CODEUNIT *instr,
PyObject *name);
extern void _Py_Specialize_StoreAttr(_PyStackRef owner, _Py_CODEUNIT *instr,
PyObject *name);
extern void _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins,
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ Known values:
Python 3.14a4 3612 (Add POP_ITER and INSTRUMENTED_POP_ITER)
Python 3.14a4 3613 (Add LOAD_CONST_MORTAL instruction)
Python 3.14a5 3614 (Add BINARY_OP_EXTEND)
Python 3.14a5 3615 (Remove conditional stack effects)

Python 3.15 will start with 3650

Expand All @@ -279,7 +280,7 @@ PC/launcher.c must also be updated.

*/

#define PYC_MAGIC_NUMBER 3614
#define PYC_MAGIC_NUMBER 3615
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
Expand Down
Loading
Loading