FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-128685: Specialize (rather than quicken) LOAD_CONST into LOAD_CONST_[IM]MORTAL by markshannon · Pull Request #128708 · python/cpython · GitHub

/ cpython Public

GH-128685: Specialize (rather than quicken) LOAD_CONST into LOAD_CONST_[IM]MORTAL - #128708

Merged
markshannon merged 4 commits into
python:mainfrom
faster-cpython:load-const-spec-not-quick
Jan 13, 2025
Merged

GH-128685: Specialize (rather than quicken) LOAD_CONST into LOAD_CONST_[IM]MORTAL#128708
markshannon merged 4 commits into
python:mainfrom
faster-cpython:load-const-spec-not-quick

Conversation

markshannon commented Jan 10, 2025
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

This PR changes LOAD_CONST to specialize into either LOAD_CONST or LOAD_CONST_IMMORTAL.
This ensures that LOAD_CONST for immortals returns to LOAD_CONST_IMMORTAL after instrumentation.

This is unlikely to make any observable difference, but we generally expect instructions to return to their specialized forms after instrumentation is removed and LOAD_CONST is a very common instruction, so is likely to get instrumented.

Skipping news as this should have no observable effect.

brandtbucher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks good, just a couple of thoughts.

Also, any perf numbers? I doubt it would be too significant, but this is a very perf-sensitive instruction/optimization.

Python 3.14a4 3610 (Add VALUE_WITH_FAKE_GLOBALS format to annotationlib)
Python 3.14a4 3611 (Add NOT_TAKEN instruction)
Python 3.14a4 3612 (Add POP_ITER and INSTRUMENTED_POP_ITER)
Python 3.14a4 3613 (Add LOAD_CONST_MORTAL instruction)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is a specialization, right? So we shouldn't need a magic bump (since the specialized variants don't occur in marshalled data).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Adding the extra instruction changes the allocated numbers for other instructions.
Even if it isn't absolutely necessary, it is harmless.

Comment thread Python/bytecodes.c
Comment on lines +292 to +302
inst(LOAD_CONST, (-- value)) {
/* We can't do this in the bytecode compiler as
* marshalling can intern strings and make them immortal. */
PyObject *obj = GETITEM(FRAME_CO_CONSTS, oparg);
value = PyStackRef_FromPyObjectNew(obj);
#if ENABLE_SPECIALIZATION
if (this_instr->op.code == LOAD_CONST) {
this_instr->op.code = _Py_IsImmortal(obj) ? LOAD_CONST_IMMORTAL : LOAD_CONST_MORTAL;
}
#endif
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why the check for this_instr->op.code == LOAD_CONST?

Also, minor, but maybe a bit more idiomatic with the rest of the bytecode definitions to split the specializing part out and reuse the code for loading the const? I think this would also make LOAD_CONST a viable tier two instruction (which it currently isn't). Though actually tracing through one should be rare, I can think of cases where it might happen.

Suggested change
inst(LOAD_CONST, (-- value)) {
/* We can't do this in the bytecode compiler as
* marshalling can intern strings and make them immortal. */
PyObject *obj = GETITEM(FRAME_CO_CONSTS, oparg);
value = PyStackRef_FromPyObjectNew(obj);
#if ENABLE_SPECIALIZATION
if (this_instr->op.code == LOAD_CONST) {
this_instr->op.code = _Py_IsImmortal(obj) ? LOAD_CONST_IMMORTAL : LOAD_CONST_MORTAL;
}
#endif
}
specializing op(_SPECIALIZE_LOAD_CONST, (--)) {
/* We can't do this in the bytecode compiler as
* marshalling can intern strings and make them immortal. */
#if ENABLE_SPECIALIZATION
this_instr->op.code = _Py_IsImmortal(obj) ? LOAD_CONST_IMMORTAL : LOAD_CONST_MORTAL;
#endif
}
macro(LOAD_CONST) = _SPECIALIZE_LOAD_CONST + LOAD_CONST_MORTAL;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why the check for this_instr->op.code == LOAD_CONST?

Instrumentation. The opcode might be INSTRUMENTED_LINE for example.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

split the specializing part out

The specializing part needs to load the value to test it, so separating out the specialization just duplicates that.

markshannon merged commit ddd9599 into python:main Jan 13, 2025
markshannon deleted the load-const-spec-not-quick branch January 13, 2025 13:40
mpage added a commit to mpage/cpython that referenced this pull request Jan 15, 2025
This was previously handled by `_PyCode_Quicken`, but moved to specialization
in pythongh-128708. Enable it for the free-threaded build.
mpage added a commit to mpage/cpython that referenced this pull request Jan 16, 2025
This was previously handled by `_PyCode_Quicken`, but moved to specialization
in pythongh-128708. Enable it for the free-threaded build.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL