Repository navigation
gh-126195: Use pthread_jit_write_protect_np on macOS #126196
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
Changes from 3 commits
71cef5e
24719d7
507e662
138525d
7974378
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Improve JIT performance by 1.4% on macOS Apple Silicon. Patch by Diego Russo. | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -58,7 +58,12 @@ jit_alloc(size_t size) | |||||||||||||||||||
| int failed = memory == NULL; | ||||||||||||||||||||
| #else | ||||||||||||||||||||
| int flags = MAP_ANONYMOUS | MAP_PRIVATE; | ||||||||||||||||||||
| unsigned char *memory = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0); | ||||||||||||||||||||
| int prot = PROT_READ | PROT_WRITE; | ||||||||||||||||||||
| # ifdef MAP_JIT | ||||||||||||||||||||
| flags |= MAP_JIT; | ||||||||||||||||||||
| prot |= PROT_EXEC; | ||||||||||||||||||||
| # endif | ||||||||||||||||||||
| unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0); | ||||||||||||||||||||
| int failed = memory == MAP_FAILED; | ||||||||||||||||||||
| #endif | ||||||||||||||||||||
| if (failed) { | ||||||||||||||||||||
|
|
@@ -102,8 +107,11 @@ mark_executable(unsigned char *memory, size_t size) | |||||||||||||||||||
| int old; | ||||||||||||||||||||
| int failed = !VirtualProtect(memory, size, PAGE_EXECUTE_READ, &old); | ||||||||||||||||||||
| #else | ||||||||||||||||||||
| int failed = 0; | ||||||||||||||||||||
| __builtin___clear_cache((char *)memory, (char *)memory + size); | ||||||||||||||||||||
| int failed = mprotect(memory, size, PROT_EXEC | PROT_READ); | ||||||||||||||||||||
| # ifndef MAP_JIT | ||||||||||||||||||||
| failed = mprotect(memory, size, PROT_EXEC | PROT_READ); | ||||||||||||||||||||
| # endif | ||||||||||||||||||||
|
diegorusso marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
| #endif | ||||||||||||||||||||
| if (failed) { | ||||||||||||||||||||
| jit_error("unable to protect executable memory"); | ||||||||||||||||||||
|
|
@@ -499,6 +507,9 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz | |||||||||||||||||||
| if (memory == NULL) { | ||||||||||||||||||||
| return -1; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| #ifdef MAP_JIT | ||||||||||||||||||||
| pthread_jit_write_protect_np(0); | ||||||||||||||||||||
| #endif | ||||||||||||||||||||
| // Update the offsets of each instruction: | ||||||||||||||||||||
| for (size_t i = 0; i < length; i++) { | ||||||||||||||||||||
| state.instruction_starts[i] += (uintptr_t)memory; | ||||||||||||||||||||
|
|
@@ -529,7 +540,11 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz | |||||||||||||||||||
| data += group->data_size; | ||||||||||||||||||||
| assert(code == memory + code_size); | ||||||||||||||||||||
| assert(data == memory + code_size + data_size); | ||||||||||||||||||||
| if (mark_executable(memory, total_size)) { | ||||||||||||||||||||
| int status = mark_executable(memory, total_size); | ||||||||||||||||||||
| #ifdef MAP_JIT | ||||||||||||||||||||
| pthread_jit_write_protect_np(1); | ||||||||||||||||||||
| #endif | ||||||||||||||||||||
| if (status) { | ||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should work, right? Then we don't need
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is the same thing. if we place
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||||||||||||||||
| jit_free(memory, total_size); | ||||||||||||||||||||
| return -1; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.