GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-128685: Specialize (rather than quicken) LOAD_CONST into LOAD_CONST_[IM]MORTAL by markshannon · Pull Request #128708 · python/cpython

Conversation

@markshannon

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

Uh oh!

There was an error while loading. Please reload this page.

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.

  • Issue: LOAD_CONST_IMMORTAL doesn't survive being instrumented and de-instrumented. #128685

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@markshannon
Specialize (rather than quicken) LOAD_CONST into LOAD_CONST_[IM]MORTAL
e7a7f22
@bedevere-app bedevere-app Bot mentioned this pull request Jan 10, 2025
LOAD_CONST_IMMORTAL doesn't survive being instrumented and de-instrumented. #128685
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Jan 10, 2025
@markshannon markshannon added the skip news label Jan 10, 2025
@markshannon
Account for LOAD_CONST_MORTAL in jit optimizer.
043f978
@markshannon
markshannon requested a review from Fidget-Spinner as a code owner January 10, 2025 15:47
Fidget-Spinner
Fidget-Spinner approved these changes Jan 10, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Jan 10, 2025
markshannon added 2 commits January 10, 2025 16:16
@markshannon
Disable specialization for free-threaded build
d342864
@markshannon
#if not #ifdef
6cc0241
brandtbucher
brandtbucher approved these changes Jan 10, 2025
View reviewed changes

@brandtbucher brandtbucher left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Include/internal/pycore_magic_number.h
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)

@brandtbucher brandtbucher Jan 10, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markshannon markshannon Jan 13, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Include/internal/pycore_opcode_metadata.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

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
}

@brandtbucher brandtbucher Jan 10, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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;

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markshannon markshannon Jan 13, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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

Instrumentation. The opcode might be INSTRUMENTED_LINE for example.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markshannon markshannon Jan 13, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

split the specializing part out

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@markshannon
markshannon merged commit ddd9599 into python:main Jan 13, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Jan 13, 2025
@markshannon
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
@mpage
Specialize LOAD_CONST in free-threaded builds
3cedaed
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
@mpage
Specialize LOAD_CONST in free-threaded builds
d2d0d9e
This was previously handled by `_PyCode_Quicken`, but moved to specialization
in pythongh-128708. Enable it for the free-threaded build.
@mpage mpage mentioned this pull request Jan 27, 2025
gh-115999: Enable free-threaded specialization of LOAD_CONST #129365
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Fidget-Spinner Fidget-Spinner Fidget-Spinner approved these changes
@brandtbucher brandtbucher brandtbucher approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@markshannon @Fidget-Spinner @brandtbucher
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.