[LLVMdev] Use of statics and ManagedStatics in LLVM

Zachary Turner zturner at google.com
Mon Jun 9 11:44:50 PDT 2014


Based on a recent discussion[1], I started trying to remove the functions
llvm_start_multithreaded() and llvm_stop_multithreaded() from the codebase.
 It turns out this is a little bit tricky.  Consider the following scenario:

During program initialization, a global static object's constructor
dereferences a ManagedStatic.  During dereferencing of the ManagedStatic,
it needs to know whether or not to acquire the global lock in order to
allocate the ManagedStatic

There are 3 possible types for the global_lock, and none of them solve the
problem.

1) If global_lock is simply another global static, it may not have been
constructed yet.

2) If global_lock is a raw pointer to a mutex, it would have to be
explicitly allocated, and we can't guarantee this during static
initialization.

3) If global_lock is a ManagedStatic<Mutex>, then it will get into an
infinite recursion here when trying to allocate this ManagedStatic.

I actually started to feel this way since the first time I started looking
at LLVM, but even moreso increasingly I feel that the solution is that
ManagedStatics should not be allowed to be accessed until after main
begins.   llvm_shutdown() gives deterministic order of destroying managed
statics, but we don't have deterministic order of *creation* of those
ManagedStatics.  I have a patch up[2] (still awaiting review) that shows a
possible solution to these problems and how we might migrate the existing
cases where this happens over to a ManagedStatic free static
initialization.

There is only one requirement: You must be able to insert a call very early
in main() that will do the ManagedStatic initialization.  However, we can
catch this with an assertion and all anyone would have to do is add one
line to their main function.  As in [2], all this early main() code would
do is copy fields over from one structure to another.

As a side benefit, this provides the ability to "resurrect" after an
llvm_shutdown(), shoudl that be desired, because the initial static state
of the program always remains in-tact once main() is entered.

Thoughts?  Better ideas?


[1] - http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-June/073543.html
[2] -
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140602/219684.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140609/f211ece6/attachment.html>


More information about the llvm-dev mailing list