<div dir="ltr">Hi Haoran,<div><br></div><div>Unfortunately there was a bug in LLVM 10: Constructors from llvm.global_ctors were tracked by LLLazyJIT, but not LLJIT. As you've discovered you can work around this by recording the destructors manually. You can just stick with this workaround while you're on LLVM 10. In LLVM 11 this API has been fixed and renamed: runConstructors has been changed to 'initialize' and runDestructors to 'deinitialize'. Once you upgrade to LLVM 11 you can initialize all modules added to a JITDylib JD by running:</div><div><br></div><div><font face="monospace">J->initialize(JD);</font></div><div><br></div><div>If you're just using the main JITDylib that LLJIT creates for you then this will look like:</div><div><br></div><div><font face="monospace">J->initialize(J->getMainJITDylib());</font></div><div><br></div><div>Regards,</div><div>Lang.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 16, 2020 at 4:09 AM Haoran Xu 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"><div dir="ltr"><div>Hello,</div><div><br></div><div>I have been struggling with making LLJIT runConstructors() working. I saw <a href="https://lists.llvm.org/pipermail/llvm-dev/2019-March/131057.html" target="_blank">this email thread</a> and tried to replicate its logic:</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"><div><pre><font size="1">LLJIT LJ;
// Add Modules.
if (auto Err = LJ.runConstructors())
  ... ; // report error.</font></pre></div></blockquote><div>But it doesn't seem to work. The global constructors in llvm.global_ctor doesn't seem to run (I added a printf in the constructor and it doesn't seem to get executed).</div><div><br></div><div>However, the following code seems to work:</div><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"><pre style="margin:0px;text-indent:0px"><span style="color:rgb(128,0,128)">CtorDtorRunner</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">RR</span>(<span style="font-style:italic;color:rgb(9,46,100)">J</span><span style="font-style:italic;color:rgb(0,103,124)">-></span><span style="font-style:italic;color:rgb(0,103,124)">getMainJITDylib</span><span style="font-style:italic">()</span>);</pre><pre style="margin:0px;text-indent:0px"><span style="color:rgb(9,46,100)">RR</span>.<span style="color:rgb(0,103,124)">add</span>(<span style="color:rgb(0,103,124)">getConstructors</span>(*<span style="color:rgb(9,46,100)">tsm</span>.<span style="color:rgb(0,103,124)">getModuleUnlocked</span>()));</pre><pre style="margin:0px;text-indent:0px"><span style="color:rgb(9,46,100)">exitOnError</span><span style="color:rgb(0,103,124)">(</span><span style="color:rgb(9,46,100)">J</span><span style="color:rgb(0,103,124)">-></span><span style="color:rgb(0,103,124)">addIRModule</span>(<span style="color:rgb(128,0,128)">std</span>::<span style="color:rgb(0,103,124)">move</span>(<span style="font-style:italic;color:rgb(9,46,100)">tsm</span>))<span style="color:rgb(0,103,124)">)</span>;</pre><span style="color:rgb(9,46,100)">exitOnError</span><span style="color:rgb(0,103,124)">(</span><span style="color:rgb(9,46,100)">RR</span>.<span style="color:rgb(0,103,124)">run</span>()<span style="color:rgb(0,103,124)">)</span>;</blockquote>


</div><div><br></div><div>But I can't understand what's wrong with my previous approach by calling LLJIT's <span style="font-family:monospace">runConstructor()</span> method. <br></div><div>Would anyone kindly help on what part I'm missing? Thanks!<br></div><div><br></div><div>Best regards,</div><div>Haoran<br></div><div><br></div></div>
_______________________________________________<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>