LLVM 10 LLJIT runConstructors()

Hello,

I have been struggling with making LLJIT runConstructors() working. I saw this email thread and tried to replicate its logic:

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

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

However, the following code seems to work:

CtorDtorRunner RR(J->getMainJITDylib());
RR.add(getConstructors(*tsm.getModuleUnlocked()));
exitOnError(J->addIRModule(std::move(tsm)));

exitOnError(RR.run());

But I can’t understand what’s wrong with my previous approach by calling LLJIT’s runConstructor() method.

Would anyone kindly help on what part I’m missing? Thanks!

Best regards,
Haoran

Hi Haoran,

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:

J->initialize(JD);

If you’re just using the main JITDylib that LLJIT creates for you then this will look like:

J->initialize(J->getMainJITDylib());

Regards,
Lang.

Hi Lang,

Thanks for the explanation! That makes sense now.

Best,
Haoran

Lang Hames <lhames@gmail.com> 于2020年10月17日周六 上午11:06写道: