[llvm] r323999 - [ADT] Replace sys::MemoryFence with standard atomics.

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 1 12:28:33 PST 2018


Author: d0k
Date: Thu Feb  1 12:28:33 2018
New Revision: 323999

URL: http://llvm.org/viewvc/llvm-project?rev=323999&view=rev
Log:
[ADT] Replace sys::MemoryFence with standard atomics.

This is a bit faster in theory, in practice it's cold code that's only
active in !NDEBUG, so it probably doesn't make a difference. This is one
of the last users of our homegrown Atomic.h.

Modified:
    llvm/trunk/include/llvm/ADT/Statistic.h
    llvm/trunk/lib/Support/Statistic.cpp

Modified: llvm/trunk/include/llvm/ADT/Statistic.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Statistic.h?rev=323999&r1=323998&r2=323999&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Statistic.h (original)
+++ llvm/trunk/include/llvm/ADT/Statistic.h Thu Feb  1 12:28:33 2018
@@ -26,7 +26,6 @@
 #ifndef LLVM_ADT_STATISTIC_H
 #define LLVM_ADT_STATISTIC_H
 
-#include "llvm/Support/Atomic.h"
 #include "llvm/Support/Compiler.h"
 #include <atomic>
 #include <memory>
@@ -42,7 +41,7 @@ public:
   const char *Name;
   const char *Desc;
   std::atomic<unsigned> Value;
-  bool Initialized;
+  std::atomic<bool> Initialized;
 
   unsigned getValue() const { return Value.load(std::memory_order_relaxed); }
   const char *getDebugType() const { return DebugType; }
@@ -147,10 +146,8 @@ public:
 
 protected:
   Statistic &init() {
-    bool tmp = Initialized;
-    sys::MemoryFence();
-    if (!tmp) RegisterStatistic();
-    TsanHappensAfter(this);
+    if (!Initialized.load(std::memory_order_acquire))
+      RegisterStatistic();
     return *this;
   }
 
@@ -160,7 +157,7 @@ protected:
 // STATISTIC - A macro to make definition of statistics really simple.  This
 // automatically passes the DEBUG_TYPE of the file into the statistic.
 #define STATISTIC(VARNAME, DESC)                                               \
-  static llvm::Statistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC, {0}, false}
+  static llvm::Statistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC, {0}, {false}}
 
 /// \brief Enable the collection and printing of statistics.
 void EnableStatistics(bool PrintOnExit = true);

Modified: llvm/trunk/lib/Support/Statistic.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Statistic.cpp?rev=323999&r1=323998&r2=323999&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Statistic.cpp (original)
+++ llvm/trunk/lib/Support/Statistic.cpp Thu Feb  1 12:28:33 2018
@@ -82,16 +82,12 @@ void Statistic::RegisterStatistic() {
   // If stats are enabled, inform StatInfo that this statistic should be
   // printed.
   sys::SmartScopedLock<true> Writer(*StatLock);
-  if (!Initialized) {
+  if (!Initialized.load(std::memory_order_relaxed)) {
     if (Stats || Enabled)
       StatInfo->addStatistic(this);
 
-    TsanHappensBefore(this);
-    sys::MemoryFence();
     // Remember we have been registered.
-    TsanIgnoreWritesBegin();
-    Initialized = true;
-    TsanIgnoreWritesEnd();
+    Initialized.store(true, std::memory_order_release);
   }
 }
 




More information about the llvm-commits mailing list