LLVM API: The Right Way to Create / Place

I am trying to implement a simple JIT compiler using the LLVM C API. So far, I have no problems generating IR code and executing it, that is, until I start disposing of objects and re-creating them.

What I basically would like to do is clear the JIT resources when they are no longer used by the engine. What I'm basically trying to do is something like this:

while (true) { // Initialize module & builder InitializeCore(GetGlobalPassRegistry()); module = ModuleCreateWithName(some_unique_name); builder = CreateBuilder(); // Initialize target & execution engine InitializeNativeTarget(); engine = CreateExecutionEngineForModule(...); passmgr = CreateFunctionPassManagerForModule(module); AddTargetData(GetExecutionEngineTargetData(engine), passmgr); InitializeFunctionPassManager(passmgr); // [... my fancy JIT code ...] --** Will give a serious error the second iteration // Destroy DisposePassManager(passmgr); DisposeExecutionEngine(engine); DisposeBuilder(builder); // DisposeModule(module); //--> Commented out: Deleted by execution engine Shutdown(); } 

However, it doesn't seem to work correctly: the second iteration of the loop, I get a pretty bad error ...

So, to summarize: what is the right way to destroy and recreate the LLVM API?

+5
source share
1 answer

Passing this as an answer because the code is too long. If possible, and no other restrictions, try using LLVM as follows. I am sure that Shutdown() inside the loop is the culprit here. And I don’t think it would be painful to keep the Builder outside. This reflects well the way I use LLVM in my JIT.

 InitializeCore(GetGlobalPassRegistry()); InitializeNativeTarget(); builder = CreateBuilder(); while (true) { // Initialize module & builder module = ModuleCreateWithName(some_unique_name); // Initialize target & execution engine engine = CreateExecutionEngineForModule(...); passmgr = CreateFunctionPassManagerForModule(module); AddTargetData(GetExecutionEngineTargetData(engine), passmgr); InitializeFunctionPassManager(passmgr); // [... my fancy JIT code ...] --** Will give a serious error the second iteration // Destroy DisposePassManager(passmgr); DisposeExecutionEngine(engine); } DisposeBuilder(builder); Shutdown(); 
+3
source

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


All Articles