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-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2 by eendebakpt · Pull Request #148146 · python/cpython

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Apr 5, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Adds 2 new fields to _PyBinaryOpSpecializationDescr:

  • result_type: the static type of the result (or NULL).
  • result_unique: whether the action always returns a freshly allocated object (not aliased to either operand).

The tier 2 optimizer now narrows the result of _BINARY_OP_EXTEND symbol's type via sym_new_type(d->result_type) and wraps it in PyJitRef_MakeUnique when the result_unique field is set, enabling downstream tier2 optimizations. A test verifies that (2 + x) * y with x, y floats uses _BINARY_OP_MULTIPLY_FLOAT_INPLACE and elides _GUARD_NOS_FLOAT. With these changes the calculation of (2 + x) * y is now 35% faster.

The changes have been factored out of #128956.

  • Issue: Specialize long tail of binary operations using a table. #100239

Sorry, something went wrong.

Uh oh!

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

All reactions
@eendebakpt
pythongh-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2
c42a7a8
Adds two new fields to _PyBinaryOpSpecializationDescr:

- result_type: the static type of the result (or NULL if unknown).
- result_unique: nonzero iff `action` always returns a freshly
  allocated object (not aliased to either operand).

The tier 2 optimizer now narrows the result symbol's type via
sym_new_type(d->result_type) and wraps it in PyJitRef_MakeUnique when
d->result_unique is set. This lets downstream ops elide their
operand-type guards and pick inplace variants. For example,
(2 + x) * y with x, y floats now compiles to
_BINARY_OP_MULTIPLY_FLOAT_INPLACE with _GUARD_NOS_FLOAT eliminated.

All existing descriptors populate both fields: long-long bitwise ops
produce unique PyLong results, and float/long mixed arithmetic
produces unique PyFloat results.

A test verifies the inplace-multiply case end-to-end.
@eendebakpt
eendebakpt requested review from Fidget-Spinner, markshannon, savannahostrowski and tomasr8 as code owners April 5, 2026 22:41
@bedevere-app bedevere-app Bot added the awaiting review label Apr 5, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Apr 5, 2026
Specialize long tail of binary operations using a table. #100239
Open
Fidget-Spinner
Fidget-Spinner reviewed Apr 6, 2026
View reviewed changes
Comment thread Include/internal/pycore_code.h
Comment on lines +502 to +505
/* Nonzero iff `action` always returns a freshly allocated object (not
aliased to either operand). Used by the tier 2 optimizer to enable
inplace follow-up ops. */
int result_unique;

@Fidget-Spinner Fidget-Spinner Apr 6, 2026

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.

Isn't this always true?

Sorry, something went wrong.

Uh oh!

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

All reactions

@eendebakpt eendebakpt Apr 6, 2026

Copy link
Copy Markdown
Contributor 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.

Yes, here it is. In the pr this was factored out from we had more specializations where this would not always be true.

We can remove it here and add back later. I think it would be needed of we use the binary_op_extend as s mechanism for adding more cases to tier 2 without creating more tier 1 opcodes

Sorry, something went wrong.

Uh oh!

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

All reactions
Fidget-Spinner
Fidget-Spinner approved these changes Apr 6, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Apr 6, 2026
kumaraditya303
kumaraditya303 reviewed Apr 6, 2026
View reviewed changes
Comment thread Python/specialize.c
{NB_INPLACE_OR, compactlongs_guard, compactlongs_or},
{NB_INPLACE_AND, compactlongs_guard, compactlongs_and},
{NB_INPLACE_XOR, compactlongs_guard, compactlongs_xor},
{NB_OR, compactlongs_guard, compactlongs_or, &PyLong_Type, 1},

@kumaraditya303 kumaraditya303 Apr 6, 2026

Copy link
Copy Markdown
Contributor

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.

These can return small int too right? In that case, the result would not be unique

Sorry, something went wrong.

Uh oh!

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

All reactions

@eendebakpt eendebakpt Apr 6, 2026

Copy link
Copy Markdown
Contributor 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.

The contract is that operations making use of the uniqueness can handle small ints. (The inplace versions of BINARY_ADD_INT handle this for example).

Sorry, something went wrong.

Uh oh!

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

All reactions
@Fidget-Spinner
Fidget-Spinner merged commit efda60e into python:main Apr 6, 2026
78 checks passed

Uh oh!

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

@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 6, 2026
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@eendebakpt @ljfp
pythongh-100239: Propagate type info through _BINARY_OP_EXTEND in tie…
f7f40eb
…r 2 (pythonGH-148146)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@kumaraditya303 kumaraditya303 kumaraditya303 left review comments
@Fidget-Spinner Fidget-Spinner Fidget-Spinner approved these changes
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@tomasr8 tomasr8 Awaiting requested review from tomasr8 tomasr8 is a code owner
@savannahostrowski savannahostrowski Awaiting requested review from savannahostrowski

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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

@eendebakpt @Fidget-Spinner @kumaraditya303
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.