fix usingJEMalloc() with LTO
authorPádraig Brady <pbrady@fb.com>
Fri, 15 Sep 2017 07:56:25 +0000 (00:56 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 15 Sep 2017 08:10:50 +0000 (01:10 -0700)
Summary:
LTO has better optimization which results in the malloc() being optimized away in this function.

Note we don't use folly::dontOptimizeAway() because
that introduces a circular dependency between the
malloc and benchmark targets.

Reviewed By: marksantaniello, meyering, interwq

Differential Revision: D5840883

fbshipit-source-id: 5dd9b152f70b7a49f125b6d79b4dfa40f4cdac59

folly/Malloc.h

index 42faee417eead69c796537cb8bde402119755d44..388a16679197fbcde64a278f2652e6d588904471 100644 (file)
@@ -183,15 +183,15 @@ FOLLY_MALLOC_NOINLINE inline bool usingJEMalloc() noexcept {
 
     uint64_t origAllocated = *counter;
 
-    // Static because otherwise clever compilers will find out that
-    // the ptr is not used and does not escape the scope, so they will
-    // just optimize away the malloc.
-    static const void* ptr = malloc(1);
+    const void* ptr = malloc(1);
     if (!ptr) {
       // wtf, failing to allocate 1 byte
       return false;
     }
 
+    /* Avoid optimizing away the malloc.  */
+    asm volatile("" ::"m"(ptr) : "memory");
+
     return (origAllocated != *counter);
   }();