Skip to content
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

gh-126195: Use pthread_jit_write_protect_np on macOS #126196

Merged
merged 5 commits into from
Nov 12, 2024

Conversation

diegorusso
Copy link
Contributor

@diegorusso diegorusso commented Oct 30, 2024

Replace mprotect with pthread_jit_write_protect_np on MacOS Apple Silicon.
Improve JIT performance by ~1.4% on this platform.

@mdboom
Copy link
Contributor

mdboom commented Oct 30, 2024

We should probably do one last benchmarking run on macOS for this PR. Unfortunately our Mac benchmark machine is offline at the moment -- I'm waiting for someone to physically go and see what's up. Hopefully less than a couple days or so.

@brandtbucher brandtbucher self-assigned this Oct 30, 2024
@brandtbucher brandtbucher added performance Performance or resource usage skip news interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.14 new features, bugs and security fixes topic-JIT and removed skip news labels Oct 30, 2024
@Fidget-Spinner
Copy link
Member

JIT tests are failing and I have a partial fix at #126166 (comment). But it doesn't work on Windows because I'm not sure how to detect the JIT is running on there.

@mdboom
Copy link
Contributor

mdboom commented Oct 31, 2024

We should probably do one last benchmarking run on macOS for this PR. Unfortunately our Mac benchmark machine is offline at the moment -- I'm waiting for someone to physically go and see what's up. Hopefully less than a couple days or so.

The machine is back up and I'm running the benchmarks now.

@mdboom
Copy link
Contributor

mdboom commented Oct 31, 2024

We should probably do one last benchmarking run on macOS for this PR. Unfortunately our Mac benchmark machine is offline at the moment -- I'm waiting for someone to physically go and see what's up. Hopefully less than a couple days or so.

The machine is back up and I'm running the benchmarks now.

I think the benchmarks are hitting the tests being broken on main -- failing in the test_embed module when building for PGO. We'll probably need to wait for that and then merge that here and re-run the benchmarks.

@Eclips4
Copy link
Member

Eclips4 commented Nov 2, 2024

Merging main to get rid of failures in test_embed
(failures on aarch64macos runners as you might know are expected thing 🙂 )

@diegorusso
Copy link
Contributor Author

I've updated the branch to check if errors disappeared...

@brandtbucher
Copy link
Member

When looking this up, it seems that the documented way to do this now is the pthread_jit_write_with_callback_np API, which is a bit trickier to retrofit onto our JIT. It looks like pthread_jit_write_protect_np probably still works, but it's not well documented anymore and it seems like the new API might work in more places: https://developer.apple.com/documentation/apple-silicon/porting-just-in-time-compilers-to-apple-silicon

Probably makes sense to land this now, then explore the other API after.

Copy link
Member

@brandtbucher brandtbucher left a comment

Choose a reason for hiding this comment

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

One issue with an error path, and a couple of suggestions.

Python/jit.c Outdated Show resolved Hide resolved
Python/jit.c Outdated Show resolved Hide resolved
@bedevere-app
Copy link

bedevere-app bot commented Nov 7, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

diegorusso and others added 3 commits November 8, 2024 14:00
Replace mprotect with pthread_jit_write_protect_np
on MacOS Apple Silicon.
Improve JIT performance by ~1.4% on this platform.
Copy link
Member

@brandtbucher brandtbucher left a comment

Choose a reason for hiding this comment

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

Looks great! Just a couple of cosmetic suggestions, then I'll go ahead and merge.

Thanks for tackling this!

Python/jit.c Outdated Show resolved Hide resolved
Python/jit.c Outdated
Comment on lines 543 to 547
int status = mark_executable(memory, total_size);
#ifdef MAP_JIT
pthread_jit_write_protect_np(1);
#endif
if (status) {
Copy link
Member

Choose a reason for hiding this comment

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

This should work, right? Then we don't need status:

Suggested change
int status = mark_executable(memory, total_size);
#ifdef MAP_JIT
pthread_jit_write_protect_np(1);
#endif
if (status) {
#ifdef MAP_JIT
pthread_jit_write_protect_np(1);
#endif
if (mark_executable(memory, total_size)) {

Copy link
Contributor Author

@diegorusso diegorusso Nov 9, 2024

Choose a reason for hiding this comment

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

I don't think this is the same thing. if we place pthread_jit_write_protect_np(1) in mark_executable, this would be just after the clear cache. The solution you suggested would place it before the clear cache and It could raise an error as it enable the write protection and then the clear cache.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think it matters which order they occur in (it's not like the cache invalidation tries to execute the code or anything). I just tried it locally and didn't experience any issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried not to change the previous code logic, but anyway you are right, it works in our internal CI as well. Unless there are other requests, I think this was the last request for change.

@LeoniClemente LeoniClemente mentioned this pull request Nov 11, 2024
Copy link
Member

@brandtbucher brandtbucher left a comment

Choose a reason for hiding this comment

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

Thanks!

@brandtbucher brandtbucher merged commit c45be8a into python:main Nov 12, 2024
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage topic-JIT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants