-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
base: main
Are you sure you want to change the base?
Changes from all commits
3e090bf
a2e5f6c
363a96c
4e5a0b5
3d384fa
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 |
---|---|---|
|
@@ -56,9 +56,16 @@ jit_alloc(size_t size) | |
int flags = MEM_COMMIT | MEM_RESERVE; | ||
unsigned char *memory = VirtualAlloc(NULL, size, flags, PAGE_READWRITE); | ||
int failed = memory == NULL; | ||
#elif defined(__APPLE__) && defined(__aarch64__) | ||
int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT; | ||
int prot = PROT_READ | PROT_WRITE | PROT_EXEC; | ||
unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0); | ||
int failed = memory == MAP_FAILED; | ||
pthread_jit_write_protect_np(0); | ||
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 will leave the memory protection off for the entire thread if mapping the memory fails. Even though What do you think? |
||
#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; | ||
unsigned char *memory = mmap(NULL, size, prot, flags, -1, 0); | ||
int failed = memory == MAP_FAILED; | ||
#endif | ||
if (failed) { | ||
|
@@ -101,6 +108,10 @@ mark_executable(unsigned char *memory, size_t size) | |
} | ||
int old; | ||
int failed = !VirtualProtect(memory, size, PAGE_EXECUTE_READ, &old); | ||
#elif defined(__APPLE__) && defined(__aarch64__) | ||
int failed = 0; | ||
__builtin___clear_cache((char *)memory, (char *)memory + size); | ||
pthread_jit_write_protect_np(1); | ||
#else | ||
__builtin___clear_cache((char *)memory, (char *)memory + size); | ||
int failed = mprotect(memory, size, PROT_EXEC | PROT_READ); | ||
|
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 work? If so, it seems a bit less fragile.
I'd also like to reduce the duplication of code here. So maybe we can just add the flags in this case and use the same code path as before: