[PATCH] Make CrashRecoveryContext's globals use lazy initialization and no exit-time destructors

Filip Pizlo fpizlo at apple.com
Wed Sep 11 20:57:38 PDT 2013


It would be great to eventually get to not having global constructors and exit-time destructors in LLVM.  It's a tough goal, but I thought I'd take care of some easy ones first, to establish some idioms.  So it would be good to decide if the idiom that this patch uses is scalable.  Clearly it isn't going to address CommandLine, but there are a bunch of things that it could address.

The idiom looks as follows.  If you wanted to write:

static Foo gMyFoo;

Then you should instead write:

static Foo& MyFoo() {
  static Foo *foo = new Foo();
  return *foo;
}

This is better in two ways:

- Foo::Foo is called only when and if you actually use MyFoo(), and not eagerly when the code is loaded.  This helps start-up time.
- Foo::~Foo isn't called during exit, so if the program into which llvm is embedded does an exit() on one thread while still using llvm on another thread, you won't get a race.  This allows programs that embed llvm to exit cleanly.

I considered adding some #define magic to make this idiom easier to write; but I abstained from doing that for now.  I'm curious, though, if the following would be cool:

#define DEFINE_STATIC(name, type, arguments) \
  static type& name() { \
    static type *result = new type arguments; \
    return *result; \
  }

Which would then allow MyFoo above to be defined as:

DEFINE_STATIC(Foo, MyFoo, ());

-Filip

-------------- next part --------------
A non-text attachment was scrubbed...
Name: crash-recovery-context-no-globals.diff
Type: application/octet-stream
Size: 4686 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130911/159a1f40/attachment.obj>


More information about the llvm-commits mailing list