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;
std::vector<PassRegistrationListener *> Listeners;
public:
- PassRegistry() {}
+ PassRegistry() : locked(false) {}
~PassRegistry();
/// getPassRegistry - Access the global registry object, which is
/// 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;
//===----------------------------------------------------------------------===//
#include "llvm/PassRegistry.h"
+#include "llvm/ADT/Optional.h"
#include "llvm/IR/Function.h"
#include "llvm/PassSupport.h"
#include "llvm/Support/Compiler.h"
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).
+ Optional<sys::SmartScopedReader<true>> Guard;
+ if (!locked)
+ Guard.emplace(Lock);
+
MapType::const_iterator I = PassInfoMap.find(TI);
return I != PassInfoMap.end() ? I->second : nullptr;
}
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).
+ Optional<sys::SmartScopedReader<true>> Guard;
+ if (!locked)
+ Guard.emplace(Lock);
+
StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
return I != PassInfoStringMap.end() ? I->second : nullptr;
}
//
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;
if (ShouldFree)
ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
+
+ assert(!locked && "PassRegistry locked during registering a pass");
}
void PassRegistry::enumerateWith(PassRegistrationListener *L) {