Static constructors with ORC JIT?

Hi all,

Is there way to tell the ORC JIT infrastructure to run the static constructors in a module that has just been compiled? I see that the ExecutionEngine class has a runStaticConstructorsDestructors function, is that relevant with ORC and if so, how should it be accessed? Thanks, Daniele

Hi Daniele,

The easiest way is to collect the constructors from the modules and then execute them manually as you would with any other function.
The simplest solution is to take this code and modify it to get the list of constructor functions:
https://github.com/llvm-mirror/llvm/blob/1154d31e8c429e53307d3fc0edd13d9b261c26dc/lib/ExecutionEngine/ExecutionEngine.cpp#L370

This might be not the most ideal solution, but it works.

Cheers,
Alex.

Thank you Alex,

I went and implemented a solution along those lines. It works well.

It may be worth mentioning static constructors in the Kaleidoscope tutorial.

Cheers,
Daniele

I think the tutorial does not target JITting native code on purpose: that would require few more details, that may distract from an intro.

I also think there should be a page describing how to JIT native code, but we are all still waiting for it to appear :slight_smile:

Hi Daniele,

ORC has a separate utility for this: CtorDtorRunner, from include/llvm/ExecutionEngine/Orc/ExecutionUtils.h.

Usage requires two steps: (1) recording the constructors to be run, and (2) executing them. These steps are separated out because ORC is more aggressive about releasing memory and will destroy the IR module before you get a chance to execute the constructors.

Assuming you have a list of unique_ptr called “Mods” that you want to load, usage looks like:

CtorDtorRunner R;
for (auto &M : Mods) {
R.add(getConstructors(*M));
JIT.add(std::move(M));
}

if (auto Err = R.run())
… ; // report error.

You can also find a concrete usage example in the experimental MCJIT replacement, LLJIT (see include/llvm/ExecutionEngine/Orc/LLJIT.h, lib/ExecutionEngine/Orc/LLJIT.cpp).

If you’re happy to use LLJIT rather than MCJIT things are even easier: it has a built-in CtorDtorRunner instance, so you can just call:

LLJIT LJ;
// Add Modules.
if (auto Err = LJ.runConstructors())
… ; // report error.

Another thing to consider is whether you need static destructor support. While LLVM provides a global_dtors array for describing static destructors, they are often (e.g. in C++) registered with atexit calls from the static constructors instead. For in-process JITing, Orc provides the LocalCXXRuntimeOverrides utility to interpose atexit calls and register destructors so they can be run prior to JIT destruction. If you have atexit-registered destructors and do not use this utility you’ll end up with one of two bugs: (1) Unresolved references to atexit (if you do not expose your process’s symbols to JIT’d code), or worse: (2) JIT’d static destructors will be registered with the process’s atexit, meaning the process will try to call in to them after the JIT is torn down, at which point you’ve got undefined behavior.

Let me know if there’s any other information I can help with. :slight_smile:

Cheers,
Lang.