[LLVMdev] PassRegistry thread safety and ManagedStatic interaction

Zachary Turner zturner at google.com
Sun Jun 1 10:36:33 PDT 2014


+cc original authors of these changes.

Is PassRegistry intended to be thread-safe?  The header file explicitly
says that PassRegistry is not thread-safe, but there are mutexes and
locking used in the code.  This is actually creating a problem, because of
a subtle bug involving static initialization order and shutdown.

In particular, the RegisterPass<> static template will get invoked during
static initialization and call

  PassRegistry::getPassRegistry()->registerPass(*this);

Note that PassRegistry, however, is a ManagedStatic.  So the call to
getPassRegistry() creates the backing object of the ManagedStatic here.
 Then registerPass gets called, which attempts to lock the mutex.  This
will initialize the backing object of the SmartRWMutex.

During shutdown, it happens in reverse order.  First the SmartRWMutex is
destroyed, then the PassRegistry is destroyed.  During the PassRegistry's
destructor, it attempts to lock the mutex again.  This works in the current
code because ManagedStatic "accidentally" allocates another SmartRWMutex.
 However, the current implementation of ManagedStatic is already buggy for
other reasons, which I've tried to fix, and am now running into this as a
result of my fix  (true once-only initialization of ManagedStatics).

I'm curious about the history here.  Can we remove the locking from
PassRegistry?  And what would it take to get RegisterPass<> to not rely on
static initialization.  Is there any reason why we can't just initialize
these at runtime early in main?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140601/db260431/attachment.html>


More information about the llvm-dev mailing list