<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi Daniele,<div><br></div><div>ORC has a separate utility for this: CtorDtorRunner, from include/llvm/ExecutionEngine/Orc/ExecutionUtils.h.</div><div><br></div><div>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.</div><div><br></div><div>Assuming you have a list of <font face="monospace, monospace">unique_ptr<Module></font> called "<font face="monospace, monospace">Mods</font>" that you want to load, usage looks like: </div><div><br></div><div><font face="monospace, monospace">CtorDtorRunner R;</font></div><div><font face="monospace, monospace">for (auto &M : Mods) {</font></div><div><font face="monospace, monospace">  R.add(getConstructors(*M));</font></div><div><font face="monospace, monospace">  JIT.add(std::move(M));</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"> </font></div><div><font face="monospace, monospace">if (auto Err = R.run())</font></div><div><font face="monospace, monospace">  ... ; // report error.</font></div><div><br></div><div>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).</div><div><br></div><div>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:</div><div><br></div><div><font face="monospace, monospace">LLJIT LJ;</font></div><div><font face="monospace, monospace">// Add Modules.</font></div><div><font face="monospace, monospace">if (auto Err = LJ.runConstructors())</font></div><div><font face="monospace, monospace">  ... ; // report error.</font></div></div></div></div><div><br></div><div>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.</div><div><br></div><div>Let me know if there's any other information I can help with. :)</div><div><br></div><div>Cheers,</div><div>Lang.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Mar 15, 2019 at 11:56 AM Alex Denisov via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">I think the tutorial does not target JITting native code on purpose: that would require few more details, that may distract from an intro.<br>
<br>
I also think there should be a page describing how to JIT native code, but we are all still waiting for it to appear :)<br>
<br>
> On 15. Mar 2019, at 17:59, Daniele Vettorel <<a href="mailto:vettorel@mit.edu" target="_blank">vettorel@mit.edu</a>> wrote:<br>
> <br>
> Thank you Alex,<br>
> <br>
> I went and implemented a solution along those lines. It works well.<br>
> <br>
> It may be worth mentioning static constructors in the Kaleidoscope tutorial.<br>
> <br>
> Cheers,<br>
> Daniele<br>
> ________________________________________<br>
> From: Alex Denisov [<a href="mailto:1101.debian@gmail.com" target="_blank">1101.debian@gmail.com</a>]<br>
> Sent: 15 March 2019 08:07<br>
> To: Daniele Vettorel<br>
> Cc: <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> Subject: Re: [llvm-dev] Static constructors with ORC JIT?<br>
> <br>
> Hi Daniele,<br>
> <br>
> The easiest way is to collect the constructors from the modules and then execute them manually as you would with any other function.<br>
> The simplest solution is to take this code and modify it to get the list of constructor functions:<br>
> <a href="https://github.com/llvm-mirror/llvm/blob/1154d31e8c429e53307d3fc0edd13d9b261c26dc/lib/ExecutionEngine/ExecutionEngine.cpp#L370" rel="noreferrer" target="_blank">https://github.com/llvm-mirror/llvm/blob/1154d31e8c429e53307d3fc0edd13d9b261c26dc/lib/ExecutionEngine/ExecutionEngine.cpp#L370</a><br>
> <br>
> This might be not the most ideal solution, but it works.<br>
> <br>
> Cheers,<br>
> Alex.<br>
> <br>
>> On 14. Mar 2019, at 22:15, Daniele Vettorel via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>> <br>
>> Hi all,<br>
>> <br>
>> 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<br>
>> runStaticConstructorsDestructors<br>
>> function, is that relevant with ORC and if so, how should it be accessed? Thanks, Daniele<br>
>> <br>
>> _______________________________________________<br>
>> LLVM Developers mailing list<br>
>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
> <br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div></div>