[llvm] [llvm][Support] `thread_local` threadid on Apple systems (PR #87219)
Jeff Niu via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 31 22:05:04 PDT 2024
https://github.com/Mogball created https://github.com/llvm/llvm-project/pull/87219
I was profiling our compiler and noticed that `llvm::get_threadid` was at the top of the hotlist, taking up a surprising 5% (7 seconds) in the profile trace. It seems that computing this on MacOS systems is non-trivial, so cache the result in a thread_local.
>From fee927da78adfc15d2a10a0cbbc90f592244a088 Mon Sep 17 00:00:00 2001
From: Mogball <jeff at modular.com>
Date: Sun, 31 Mar 2024 22:02:54 -0700
Subject: [PATCH] [llvm][Support] `thread_local` threadid on Apple systems
I was profiling our compiler and noticed that `llvm::get_threadid` was
at the top of the hotlist, taking up a surprising 5% (7 seconds) in the
profile trace. It seems that computing this on MacOS systems is
non-trivial, so cache the result in a thread_local.
---
llvm/lib/Support/Unix/Threading.inc | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Support/Unix/Threading.inc b/llvm/lib/Support/Unix/Threading.inc
index 55e7dcfa4678cf..839c00c5ebbf96 100644
--- a/llvm/lib/Support/Unix/Threading.inc
+++ b/llvm/lib/Support/Unix/Threading.inc
@@ -115,8 +115,11 @@ uint64_t llvm::get_threadid() {
// Calling "mach_thread_self()" bumps the reference count on the thread
// port, so we need to deallocate it. mach_task_self() doesn't bump the ref
// count.
- thread_port_t Self = mach_thread_self();
- mach_port_deallocate(mach_task_self(), Self);
+ static thread_local thread_port_t Self = [] {
+ thread_port_t InitSelf = mach_thread_self();
+ mach_port_deallocate(mach_task_self(), Self);
+ return InitSelf;
+ }();
return Self;
#elif defined(__FreeBSD__)
return uint64_t(pthread_getthreadid_np());
More information about the llvm-commits
mailing list