[llvm-dev] Static constructors with ORC JIT?

Lang Hames via llvm-dev llvm-dev at lists.llvm.org
Fri Mar 15 12:21:25 PDT 2019


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<Module> 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. :)

Cheers,
Lang.

On Fri, Mar 15, 2019 at 11:56 AM Alex Denisov via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> 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 :)
>
> > On 15. Mar 2019, at 17:59, Daniele Vettorel <vettorel at mit.edu> wrote:
> >
> > 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
> > ________________________________________
> > From: Alex Denisov [1101.debian at gmail.com]
> > Sent: 15 March 2019 08:07
> > To: Daniele Vettorel
> > Cc: llvm-dev at lists.llvm.org
> > Subject: Re: [llvm-dev] Static constructors with ORC JIT?
> >
> > 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.
> >
> >> On 14. Mar 2019, at 22:15, Daniele Vettorel via llvm-dev <
> llvm-dev at lists.llvm.org> wrote:
> >>
> >> 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
> >>
> >> _______________________________________________
> >> LLVM Developers mailing list
> >> llvm-dev at lists.llvm.org
> >> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
> >
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190315/40351380/attachment.html>


More information about the llvm-dev mailing list