I have been struggling with making LLJIT runConstructors() working. I saw this email thread and tried to replicate its logic:
// 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:
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!
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:
If you’re just using the main JITDylib that LLJIT creates for you then this will look like:
Thanks for the explanation! That makes sense now.
Lang Hames <email@example.com> 于2020年10月17日周六 上午11:06写道：