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-135904: Optimize the JIT's assembly control flow by brandtbucher · Pull Request #135905 · python/cpython

Conversation

@brandtbucher

@brandtbucher brandtbucher commented Jun 25, 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 adds a pass to the JIT build step that optimizes the control flow of the assembly for each template before compiling it to machine code. It replaces our current zero-length jump removal, and does a bit more:

  • It allows the assembler to resolve and efficiently encode jumps to _JIT_CONTINUE by just sticking a label in the assembly itself.
  • It inverts certain branches where branching is the common case and falling through is the uncommon case, increasing our ability to maintain a straight-line sequence of hot code.

Another benefit of this approach is that the machine code in the comments of jit_stencils-*.h actually represents the real code emitted at runtime (currently our jump-removal and nop padding change the code, but not the comment).

The resulting code is over 1% faster. For an idea of how it impacts the stencils themselves, here's a diff.

Note that this mostly punts on AArch64 support... all I've done is implement the same zero-length jump removal that we already had (but @diegorusso is going to take care of the rest).

Later, we'll do proper hot-cold splitting, but that's for another PR.

  • Issue: Optimize the JIT's low-level assembly control flow #135904

Sorry, something went wrong.

Uh oh!

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

tomasr8, Fidget-Spinner, savannahostrowski, akawashiro, and razzmatazz reacted with thumbs up emoji
All reactions
  • 👍 5 reactions
brandtbucher added 18 commits June 11, 2025 18:48
@brandtbucher
Do some textual assembly magic
5606078
@brandtbucher
Switch to a linked list
858624a
@brandtbucher
Handle prefixes
77886d0
@brandtbucher
Rework optimizer and break it out into its own module
da72079
@brandtbucher
Add AArch64 stub
9ebb32c
@brandtbucher
Shake out some bugs on Windows
4f85fab
@brandtbucher
Disable dead code removal for now
d2c9ae9
@brandtbucher
More cleanup, don't add jumps
ee97e87
@brandtbucher
Fix Windows label detection (and cleanup)
b436751
@brandtbucher
fixup
fbb859d
@brandtbucher
Add comment and jump detection for AArch64
4cfabf5
@brandtbucher
Fix bug in predecessor detection
77a8ba1
@brandtbucher
formatting
a987be7
@brandtbucher
Commenting and cleanup
5ec6022
@brandtbucher
More comments
a577c36
@brandtbucher
fixup
807a359
@brandtbucher
fixup
3541ef7
@brandtbucher
blurb add
51456c3
@brandtbucher
brandtbucher requested review from Fidget-Spinner, diegorusso and savannahostrowski June 25, 2025 00:17
@brandtbucher brandtbucher self-assigned this Jun 25, 2025
@brandtbucher brandtbucher added performance Performance or resource usage interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-JIT labels Jun 25, 2025
@bedevere-app bedevere-app Bot added the awaiting core review label Jun 25, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Jun 25, 2025
Optimize the JIT's low-level assembly control flow #135904
Closed
Fidget-Spinner
Fidget-Spinner reviewed Jun 25, 2025
View reviewed changes
Comment thread Tools/jit/_optimizers.py
_re_return: typing.ClassVar[re.Pattern[str]] = _RE_NEVER_MATCH

def __post_init__(self) -> None:
# Split the code into a linked list of basic blocks. A basic block is an

@Fidget-Spinner Fidget-Spinner Jun 25, 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.

So I'm trying to reason about what happens if we miss something, say a branch instruction that we did not include in our branch table?

Or is everything in the x64 spec already included in the table above?

Sorry, something went wrong.

Uh oh!

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

All reactions

@diegorusso diegorusso Jun 25, 2025

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.

If we miss it, it won't be optimised and I don't think it will break the logic anyway.

Sorry, something went wrong.

Uh oh!

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

All reactions

@brandtbucher brandtbucher Jun 25, 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.

Everything is already included (but one was misspelled, thanks for making me double-check).

If we miss one, we will accidentally create a superblock instead of a basic block, and will miss one outgoing edge. This could cause _invert_hot_branches to miscount the number of predecessors for the jump after the branch, and perform an invalid optimization. It could also make our hot-cold splitting incorrect.

So bad things could happen, but those would just be bugs that need fixing.

(Not detecting any branches is a special case, since _invert_hot_branches will never run, so everything is fine. That's why AArch64 works fine now, even though we haven't taught its optimizer about branches yet.)

Sorry, something went wrong.

Uh oh!

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

diegorusso reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Fidget-Spinner
Fidget-Spinner reviewed Jun 25, 2025
View reviewed changes
Comment thread Tools/jit/_optimizers.py Resolved

Uh oh!

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

diegorusso
diegorusso reviewed Jun 25, 2025
View reviewed changes
Comment thread Tools/jit/_optimizers.py Resolved

Uh oh!

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

Comment thread Tools/jit/_optimizers.py Resolved

Uh oh!

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

Comment thread Tools/jit/_optimizers.py Outdated
class _Block:
label: str | None = None
# Non-instruction lines like labels, directives, and comments:
noise: list[str] = dataclasses.field(default_factory=list)

@diegorusso diegorusso Jun 25, 2025 •
edited
Loading

Uh oh!

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

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.

We have already discuss this, but can we find a better name for this? Noise implies something irrelevant or random data that can be discarded, eliminated or filtered out. We don't do this here, on the contrary we include all these lines otherwise we end up with a broken file.
I would vote for something like:

  • non_instructions: this is very explicit and clear
  • metadata: still good enough
  • other: this is fairly generic

Sorry, something went wrong.

Uh oh!

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

Fidget-Spinner and mizudenus reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

@brandtbucher brandtbucher Jun 25, 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.

I've renamed to noninstructions.

Sorry, something went wrong.

Uh oh!

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

diegorusso reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@diegorusso

diegorusso commented Jun 25, 2025

Copy link
Copy Markdown
Contributor

This is great BTW, I love it.

brandtbucher and Fidget-Spinner reacted with heart emoji Fidget-Spinner reacted with rocket emoji
All reactions
  • ❤️ 2 reactions
  • 🚀 1 reaction

Sorry, something went wrong.

Uh oh!

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

brandtbucher added 2 commits June 25, 2025 11:46
@brandtbucher
Add comments, noise -> noninstructions, jrxz -> jrcxz, predecessor ->…
6ddeaaf
… pre
@brandtbucher
fixup
b6da4e6
diegorusso
diegorusso approved these changes Jun 26, 2025
View reviewed changes

@diegorusso diegorusso left a comment

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.

Thanks for addressing the comments. It looks good to me.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Jun 26, 2025
Fidget-Spinner
Fidget-Spinner approved these changes Jun 26, 2025
View reviewed changes
savannahostrowski
savannahostrowski approved these changes Jun 27, 2025
View reviewed changes

@savannahostrowski savannahostrowski 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.

Nice! This is super cool. Just one small comment, not blocking.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Tools/jit/_targets.py
# was required to disable them.
"-mno-outline-atomics",
]
# -mno-outline-atomics: Keep intrinsics from being emitted.

@savannahostrowski savannahostrowski Jun 27, 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.

Does this still only apply to aarch64 Linux? Just curious why we dropped the platform specificity in the comment?

Sorry, something went wrong.

Uh oh!

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

All reactions

@diegorusso diegorusso Jun 27, 2025

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.

yes, it's in the branch "aarch64-.*-linux-gnu" and the previous comment was just saying that :)

Sorry, something went wrong.

Uh oh!

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

All reactions

@brandtbucher brandtbucher Jun 27, 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.

Yeah, it just seemed redundant given the surrounding context!

Sorry, something went wrong.

Uh oh!

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

All reactions
@brandtbucher
brandtbucher merged commit 0e5d096 into python:main Jun 27, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Jun 27, 2025
AndPuQing pushed a commit to AndPuQing/cpython that referenced this pull request Jul 11, 2025
@brandtbucher @AndPuQing
pythonGH-135904: Optimize the JIT's assembly control flow (pythonGH-1…
d048a8c
…35905)
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@brandtbucher @Pranjal095
pythonGH-135904: Optimize the JIT's assembly control flow (pythonGH-1…
29cab8c
…35905)
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@brandtbucher @taegyunkim
pythonGH-135904: Optimize the JIT's assembly control flow (pythonGH-1…
8d9a6b7
…35905)
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
@brandtbucher @Agent-Hellboy
pythonGH-135904: Optimize the JIT's assembly control flow (pythonGH-1…
a6c7345
…35905)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@diegorusso diegorusso diegorusso approved these changes
@savannahostrowski savannahostrowski savannahostrowski approved these changes
@Fidget-Spinner Fidget-Spinner Fidget-Spinner approved these changes

Uh oh!

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

Assignees

@brandtbucher brandtbucher

Labels

interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage topic-JIT

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.

4 participants

@brandtbucher @diegorusso @savannahostrowski @Fidget-Spinner
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.