How to free llvm :: module

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

My limitation is that I need to recreate the module, because I need to process the same input file several times, and after the first compilation, LLVM will complain about a module that already contains x functions, etc. The only way forward I can see is to recreate the module between them, which means that I need to recreate the ExecutionEngine as well.

However, there are a huge number of memory leaks that I want to get rid of. How to free llvm::Module and llvm::ExecutionEngine ?

If I just delete both, it segfaults somewhere in LLVM. My current implementation is as follows:

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

However, there are still about 14,000 leaks, so this seems wrong.

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

0
source share
1 answer

If you understand correctly, you want to create instances of ExecutionEngine and Module several times during the life of your program and properly clean them.

Do not remove the module separately, as the ExecutionEngine "owns" it after addModule . This means that he will take care of removal and cleaning.

In LLVM it is always better to learn from examples. I suggest you take a look at the source code tools/lli/lli.cpp , which does everything you need, including cleanup.

+1
source

Source: https://habr.com/ru/post/1207580/


All Articles