=?utf-8?Q?[PATCH]_D20910:_[tsan]_On_OS_X, _optimize_main_thread=E2=80=99s_?= ThreadState accesses

Kuba Brecka via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 2 07:21:16 PDT 2016


kubabrecka created this revision.
kubabrecka added a reviewer: dvyukov.
kubabrecka added subscribers: llvm-commits, zaks.anna.
kubabrecka added a project: Sanitizers.
Herald added a subscriber: kubabrecka.

This is a very simple optimization that gets about 10% speedup for certain programs.  We’re currently storing the pointer to the main thread’s ThreadState, but we can store the state directly in a static variable, which avoid the load acquire.

http://reviews.llvm.org/D20910

Files:
  lib/tsan/rtl/tsan_platform_mac.cc

Index: lib/tsan/rtl/tsan_platform_mac.cc
===================================================================
--- lib/tsan/rtl/tsan_platform_mac.cc
+++ lib/tsan/rtl/tsan_platform_mac.cc
@@ -67,20 +67,18 @@
 // when TLVs are not accessible (early process startup, thread cleanup, ...).
 // The following provides a "poor man's TLV" implementation, where we use the
 // shadow memory of the pointer returned by pthread_self() to store a pointer to
-// the ThreadState object. The main thread's ThreadState pointer is stored
-// separately in a static variable, because we need to access it even before the
+// the ThreadState object. The main thread's ThreadState is stored separately
+// in a static variable, because we need to access it even before the
 // shadow memory is set up.
 static uptr main_thread_identity = 0;
-static ThreadState *main_thread_state = nullptr;
+ALIGNED(64) static char main_thread_state[sizeof(ThreadState)];
 
 ThreadState *cur_thread() {
-  ThreadState **fake_tls;
   uptr thread_identity = (uptr)pthread_self();
   if (thread_identity == main_thread_identity || main_thread_identity == 0) {
-    fake_tls = &main_thread_state;
-  } else {
-    fake_tls = (ThreadState **)MemToShadow(thread_identity);
+    return (ThreadState *)&main_thread_state;
   }
+  ThreadState **fake_tls = (ThreadState **)MemToShadow(thread_identity);
   ThreadState *thr = (ThreadState *)SignalSafeGetOrAllocate(
       (uptr *)fake_tls, sizeof(ThreadState));
   return thr;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20910.59375.patch
Type: text/x-patch
Size: 1481 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160602/0a5a40dc/attachment.bin>


More information about the llvm-commits mailing list