Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM API: correct way to create/dispose

Tags:

llvm

dispose

I'm attempting 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 objects and recreating them.

What I basically would like to do is to clean up the JIT'ted resources the moment they're no longer used by the engine. What I'm basically attempting 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, this doesn't seem to be working correctly: the second iteration of the loop I get a pretty bad error...

So to summarize: what's the correct way to destroy and re-create the LLVM API?

like image 389
atlaste Avatar asked Oct 20 '22 21:10

atlaste


1 Answers

Posting this as Answer because the code's too long. If possible and no other constraints, try to use LLVM like this. I am pretty sure the Shutdown() inside the loop is the culprit here. And I dont think it would hurt to keep the Builder outside, too. 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();
like image 61
antipattern Avatar answered Oct 22 '22 21:10

antipattern