[PATCH] Add a lock() function in PassRegistry to speed up multi-thread synchronization.

Erik Eckstein eeckstein at apple.com
Thu Feb 26 04:34:36 PST 2015


http://reviews.llvm.org/D7787

Files:
  include/llvm/PassRegistry.h
  lib/IR/PassRegistry.cpp

Index: include/llvm/PassRegistry.h
===================================================================
--- include/llvm/PassRegistry.h
+++ include/llvm/PassRegistry.h
@@ -41,6 +41,9 @@
 class PassRegistry {
   mutable sys::SmartRWMutex<true> Lock;
 
+  /// Only if false, synchronization must use the Lock mutex.
+  std::atomic<bool> locked;
+
   /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
   typedef DenseMap<const void *, const PassInfo *> MapType;
   MapType PassInfoMap;
@@ -52,7 +55,7 @@
   std::vector<PassRegistrationListener *> Listeners;
 
 public:
-  PassRegistry() {}
+  PassRegistry() : locked(false) {}
   ~PassRegistry();
 
   /// getPassRegistry - Access the global registry object, which is
@@ -60,6 +63,10 @@
   /// llvm_shutdown.
   static PassRegistry *getPassRegistry();
 
+  /// Enables fast thread synchronization in getPassInfo().
+  /// After calling lock() no more passes may be registered.
+  void lock() { locked = true; }
+
   /// getPassInfo - Look up a pass' corresponding PassInfo, indexed by the pass'
   /// type identifier (&MyPass::ID).
   const PassInfo *getPassInfo(const void *TI) const;
Index: lib/IR/PassRegistry.cpp
===================================================================
--- lib/IR/PassRegistry.cpp
+++ lib/IR/PassRegistry.cpp
@@ -39,15 +39,33 @@
 PassRegistry::~PassRegistry() {}
 
 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
-  sys::SmartScopedReader<true> Guard(Lock);
+  // We don't need thread synchronization after the PassRegistry is locked
+  // (that means: is read-only).
+  bool needMutex = !locked;
+  if (needMutex)
+    Lock.lock_shared();
+
   MapType::const_iterator I = PassInfoMap.find(TI);
-  return I != PassInfoMap.end() ? I->second : nullptr;
+  const PassInfo *PI = I != PassInfoMap.end() ? I->second : nullptr;
+
+  if (needMutex)
+    Lock.unlock_shared();
+  return PI;
 }
 
 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
-  sys::SmartScopedReader<true> Guard(Lock);
+  // We don't need thread synchronization after the PassRegistry is locked
+  // (that means: is read-only).
+  bool needMutex = !locked;
+  if (needMutex)
+    Lock.lock_shared();
+
   StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
-  return I != PassInfoStringMap.end() ? I->second : nullptr;
+  const PassInfo *PI = I != PassInfoStringMap.end() ? I->second : nullptr;
+
+  if (needMutex)
+    Lock.unlock_shared();
+  return PI;
 }
 
 //===----------------------------------------------------------------------===//
@@ -55,6 +73,9 @@
 //
 
 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
+
+  assert(!locked && "Trying to register a pass in a locked PassRegistry");
+
   sys::SmartScopedWriter<true> Guard(Lock);
   bool Inserted =
       PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
@@ -68,6 +89,8 @@
 
   if (ShouldFree)
     ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
+
+  assert(!locked && "PassRegistry locked during registering a pass");
 }
 
 void PassRegistry::enumerateWith(PassRegistrationListener *L) {

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7787.20744.patch
Type: text/x-patch
Size: 3116 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150226/8292ec55/attachment.bin>


More information about the llvm-commits mailing list