How to deallocate llvm::module

两盒软妹~` 提交于 2020-01-14 03:10:08

问题


I have a small language parser that uses LLVM to generate and run code, based on the Kaleidoscope tutorial.

My constraint is that I have to recreate the module because I need to process the same input file multiple times, and after the first compilation, LLVM would complain about the module already containing functions, x, etc. The only way forward I see is to recreate the module in between, and this means that I have to recreate ExecutionEngine as well.

However, there are a huge amount of memory leaks occurring which I want to get rid of. How do I properly deallocate the llvm::Module and llvm::ExecutionEngine ?

If I simply delete both, it segfaults somewhere in LLVM. My current implementation goes like this:

TheExecutionEngine->removeModule(module);
delete module();
delete TheExecutionEngine;

However, there are still about 14000 leaks reported, so this seems wrong.

I do not need to duplicate any existing functions in a module. I just want a fresh, empty one.


回答1:


If I understood you correctly, you want to create ExecutionEngine and Module instances multiple times during the lifetime of your program, and clean them up properly.

Do not delete the Module separately, since ExecutionEngine "owns" it once you addModule. This means it will take care of deleting and cleaning up.

In LLVM it's always best to learn from examples. I suggest you take a look at the source code of tools/lli/lli.cpp, which does all of what you need, including cleanup.



来源:https://stackoverflow.com/questions/23891889/how-to-deallocate-llvmmodule

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!